86 lines
7.4 KiB
JavaScript
86 lines
7.4 KiB
JavaScript
// Generate compact two-byte menu strings; storage/config structures stay upstream-compatible.
|
|
const fs=require('fs'),path=require('path');const root=path.resolve(__dirname,'..'),dir=path.join(root,'feld/src/feld');
|
|
const tr={};for(const row of fs.readFileSync(dir+'/ko.tsv','utf8').split(/\r?\n/)){if(!row)continue;const p=row.indexOf('\t');tr[row.slice(0,p)]=row.slice(p+1);}
|
|
const fields={LAND:'육지',WATER:'수중',SKY:'하늘',FOREST:'숲',TROPICAL:'트로피컬',GLACIER:'글레이셔'};
|
|
const chars=[...new Set((Object.values({...tr,...fields}).join('')+'치트 메뉴휠 이전 다음다마고치 편집지역과 저장다마포인트아이템프로그램A 이동 B 선택 C 뒤로B 변경 C 목록추가수량개휠 수량 조절B 확인 C 목록최대 15개 보유더 추가할 수 없습니다'))].filter(x=>x.charCodeAt(0)>127).sort();
|
|
const native=require('../src/core.js').KO_CHARS;
|
|
const wanted=new Set([...chars,...native]);
|
|
const glyphs=new Map();const bdf=fs.readFileSync(root+'/third-party/Galmuri9.bdf','utf8');
|
|
for(const block of bdf.split('STARTCHAR ').slice(1)){
|
|
const cp=Number(block.match(/ENCODING (\d+)/)?.[1]);if(!wanted.has(String.fromCodePoint(cp||0)))continue;
|
|
const [w,h,x,y]=block.match(/BBX (-?\d+) (-?\d+) (-?\d+) (-?\d+)/).slice(1).map(Number);
|
|
const rows=block.split(/BITMAP\r?\n/)[1].split(/\r?\n/).slice(0,h).map(x=>parseInt(x,16));
|
|
// 10x10 canvas with the baseline at y=9; Galmuri9's Hangul is nine pixels tall.
|
|
const bits=new Uint8Array(13);for(let r=0;r<h;r++)for(let c=0;c<w;c++)if(rows[r]&(2**(Math.ceil(w/8)*8-1-c))){
|
|
const xx=x+c,yy=9-y-h+r;if(xx<0||xx>=10||yy<0||yy>=10)throw Error('Glyph crop: '+String.fromCodePoint(cp));
|
|
const n=yy*10+xx;bits[n>>3]|=128>>(n&7);
|
|
}
|
|
glyphs.set(cp,[...bits]);
|
|
}
|
|
for(const c of chars)if(!glyphs.has(c.codePointAt(0)))throw Error('Missing glyph '+c);
|
|
for(const c of native)if(!glyphs.has(c.codePointAt(0)))throw Error('Missing native glyph '+c);
|
|
const allGlyphs=[...chars,...native];
|
|
const font=Buffer.alloc(8+allGlyphs.length*13);font.writeUInt32LE(0x31464f4b,0);font.writeUInt32LE(allGlyphs.length,4);
|
|
allGlyphs.forEach((c,i)=>font.set(glyphs.get(c.codePointAt(0)),8+i*13));
|
|
fs.writeFileSync(root+'/src/menu-font-ko.js',`// Generated Galmuri9 native Korean font. See third-party/Galmuri-OFL.txt.
|
|
(function(root){
|
|
const fixed=${JSON.stringify(chars.join(''))}, native=${JSON.stringify(native)}, data=${JSON.stringify(font.toString('base64'))};
|
|
function encode(s,max=255){let out='';for(const c of s){let v=c;if(c.charCodeAt(0)>127){let n=fixed.indexOf(c);if(n<0){let k=native.indexOf(c);if(k<0)throw Error('본체 메뉴에서 지원하지 않는 문자: '+c);n=fixed.length+k;}v=String.fromCharCode(128+(n>>7),128+(n&127));}if(out.length+v.length>max)break;out+=v;}return out;}
|
|
function font(){return typeof Buffer!=='undefined'?new Uint8Array(Buffer.from(data,'base64')):Uint8Array.from(atob(data),c=>c.charCodeAt(0));}
|
|
root.ParamixKoreanMenu={encode,font,fixed,native};
|
|
if(typeof module!=='undefined')module.exports=root.ParamixKoreanMenu;
|
|
})(typeof window!=='undefined'?window:globalThis);
|
|
`);
|
|
const enc=s=>[...s].map(c=>c.charCodeAt(0)<128?c.replace(/\\/g,'\\\\').replace(/"/g,'\\"'):[128+(chars.indexOf(c)>>7),128+(chars.indexOf(c)&127)].map(x=>'\\'+x.toString(8).padStart(3,'0')).join('')).join('');
|
|
fs.writeFileSync(dir+'/font_ko.h','/* ITOSM Korean menu subset of Galmuri9, quiple, SIL OFL 1.1.\n * See third-party/Galmuri-OFL.txt. Generated by scripts/build-menu-ko.cjs. */\n#define KO_GLYPHS '+chars.length+'\nstatic const unsigned char ko_font[KO_GLYPHS][13]={\n'+chars.map(c=>'{'+glyphs.get(c.codePointAt(0)).join(',')+'}, /* '+c+' */').join('\n')+'\n};\n');
|
|
let s=fs.readFileSync(dir+'/main.c','utf8').replace(/\r\n/g,'\n');const counts={};
|
|
s=s.replace(/"((?:[^"\\]|\\.)*)"/g,(whole,k)=>{if(!(k in tr))return whole;counts[k]=(counts[k]||0)+1;return '"'+enc(tr[k])+'"';});
|
|
for(const k of Object.keys(tr))if(!counts[k])throw Error('Unused menu translation '+k);
|
|
s=s.replace('#include "font.h"','#include "font.h"\n#include "font_ko.h"');
|
|
const from=s.indexOf('static int text_w('),to=s.indexOf('static void text(int',from);
|
|
s=s.slice(0,from)+`/* Korean pairs have both bytes >= 128. Never interpret truncated input as an index. */
|
|
static int ko_pair(const char *s){ return (u8)s[0]>=128 && (u8)s[1]>=128; }
|
|
static int ko_scale(const char *s,int sc){ for(int i=0;s[i];i++) if((u8)s[i]>=128) return 1; return sc; }
|
|
static int text_w(const char *s,int sc){ sc=ko_scale(s,sc); int w=0; for(int i=0;s[i];i++){if(ko_pair(s+i)){w+=10*sc;i++;}else w+=6*sc;}return w?w-sc:0; }
|
|
static void textn(int x,int y,const char *s,int n,u16 c,int sc){
|
|
sc=ko_scale(s,sc); const int limit=x+n*6*sc;
|
|
for(int i=0;i<n && s[i];i++){
|
|
if(ko_pair(s+i)){
|
|
unsigned k=(((u8)s[i]&127)<<7)|((u8)s[i+1]&127);i++;
|
|
if(x+10*sc>limit || x+10*sc>128)break;
|
|
const volatile u8 *g=0;
|
|
if(k<KO_GLYPHS+2350u && g_cfg.cat_off){u32 a=g_cfg.cat_off+12u+40u*g_cfg.cat_n;if(rd32(a)==0x31464f4bu)g=fl(a+8u+13u*k);}
|
|
#ifdef FELD_CLEAN
|
|
if(!g && k<KO_GLYPHS)g=ko_font[k];
|
|
#endif
|
|
if(g)for(int r=0;r<10;r++)for(int q=0;q<10;q++){int p=r*10+q;if(g[p>>3]&(128>>(p&7)))rect(x+q*sc,y+r*sc,x+(q+1)*sc,y+(r+1)*sc,c);}
|
|
x+=10*sc;
|
|
}else{
|
|
if(x+5*sc>limit || x+5*sc>128)break;
|
|
int ch=(u8)s[i];if(ch>=128)ch='?';if(ch>=0x60 && ch!='x')ch-=32;
|
|
int k=glyph(ch);if(k<0||k>=96)k='?'-32;
|
|
const u8 *g=g_font[k];
|
|
for(int r=0;r<7;r++)for(int q=0;q<5;q++)if(g[r]&(16>>q))rect(x+q*sc,y+r*sc,x+(q+1)*sc,y+(r+1)*sc,c);
|
|
x+=6*sc;
|
|
}
|
|
}
|
|
}
|
|
`+s.slice(to);
|
|
// Bottom help rows used to be seven pixels tall. Keep the ten-pixel Hangul inside 128px.
|
|
s=s.replace(/ctext\(119,/g,'ctext(117,').replace(/text\(5,119,/g,'text(5,117,');
|
|
// In the tall tile the status is below a Korean title (10px vs the original 14px).
|
|
s=s.replace('text(9,y+19,st,stc,1);','text(9,y+17,st,stc,1);');
|
|
s=s.replace('text(9,y+4,name,c,2);\n int w=text_w(st,1), x=121-w, nx=9+text_w(name,2)+4; if(x<nx) x=nx;\n textn(x,y+8,st,(121-x)/6,stc,1);', 'text(9,y+2,name,c,1);\n textn(9,y+12,st,19,stc,1);');
|
|
s=s.replace('rect(5,y+2,123,y+20,BG);','rect(4,y+1,124,y+21,BG);');
|
|
s=s.replace('char nm[12], st[24];','char nm[12], st[48];');
|
|
const nameFunc='static const char *ko_field(const char *s){\n'+Object.entries(fields).map(([a,b])=>' {const char *a="'+a+'";int i=0;while(a[i]&&s[i]&&a[i]==s[i])i++;if(!a[i]&&!s[i])return "'+enc(b)+'";}').join('\n')+'\nreturn s;}\n';
|
|
s=s.replace('static const char *fname(u32 i){ return i<4u ? (const char*)g_cfg.field_name[i] : (const char*)g_cfg.field_name2[i-4u]; }',nameFunc+'static const char *fname(u32 i){ return ko_field(i<4u ? (const char*)g_cfg.field_name[i] : (const char*)g_cfg.field_name2[i-4u]); }');
|
|
s=require('./native-menu-layout.cjs')(s,enc);
|
|
fs.writeFileSync(dir+'/main-ko.c',s);
|
|
let sh=fs.readFileSync(dir+'/build.sh','utf8').replace('main.c hw.c','main-ko.c hw.c').replace('[ "$n" = main ]','[ "$n" = main-ko ]').replace('N=${OUT:-feld}','N=${OUT:-feld-ko}').replace('OUT=feldc','OUT=feldc-ko').replace('-ffreestanding','-flto -ffreestanding').replace('sh "$0" --inner','sh ./build-ko.sh --inner');
|
|
sh=sh.replace(' -Os ', ' -Oz ');
|
|
sh=sh.replace('llvm-size $N.elf; ls -l $N.bin', 'python3 -c \'import pathlib,sys; p=pathlib.Path(sys.argv[1]); p.write_bytes(p.read_bytes()+b"ITOSM_KO_MENU_V1\\x00")\' "$N.bin"\nllvm-size $N.elf; ls -l $N.bin');
|
|
fs.writeFileSync(dir+'/build-ko.sh',sh.replace(/\r\n/g,'\n'));
|
|
fs.writeFileSync(dir+'/ko-encoding.json',JSON.stringify({chars,translations:tr,fields},null,2));
|
|
console.log('Menu:',Object.keys(counts).length,'strings,',chars.length,'glyphs,',chars.length*13,'font bytes');
|