46 lines2.5 KBJavaScript
1/*
2make-names.mjs: the language screen's Chinese, Japanese and Korean names as pictures, so drawing
3the screen doesn't load the PS3's CJK fonts from dev_flash (about 11.5 MB, a second or two).
4
5 node tools/make-names.mjs
6
7 data/names/<code>.png the name in white (C_TEXT), bold, 22 px, at twice the size
8 data/names/<code>-on.png the same in cyan (C_CYAN), for the language in use
9
10Each is as wide as its text and 64 px high, the baseline at 46 (ui.c draws them at half that, where
11font_draw's line would be). Drawn with Windows' own CJK fonts; uses sharp from generator/node_modules.
12*/
13import fs from 'node:fs';
14import path from 'node:path';
15import { createRequire } from 'node:module';
16import { fileURLToPath } from 'node:url';
17
18const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
19const sharp = createRequire(path.join(ROOT, 'generator/'))('sharp');
20
21// lang.c's codes[] and names[], in its order: the ones with CJK characters
22const lang = fs.readFileSync(path.join(ROOT, 'src/lang.c'), 'utf8');
23const list = name => [...lang.match(new RegExp(`${name}\\[LANG_COUNT\\] = \\{([^}]*)\\}`))[1].matchAll(/"([^"]*)"/g)].map(m => m[1]);
24const codes = list('codes'), names = list('names');
25const FONTS = { zh: 'Microsoft YaHei', ja: 'Yu Gothic', ko: 'Malgun Gothic' };
26const COLOURS = { '': '#ffffff', '-on': '#4ad9ff' };
27const H = 64, BASE = 46, PX = 44, W = 600;
28
29fs.mkdirSync(path.join(ROOT, 'data/names'), { recursive: true });
30for (let i = 0; i < codes.length; i++) {
31 if (!/[⺀-鿿가-힯]/.test(names[i])) continue;
32 const font = FONTS[codes[i]];
33 if (!font) throw new Error(`no font for ${codes[i]}`);
34 for (const [suffix, fill] of Object.entries(COLOURS)) {
35 const text = names[i].replace(/&/g, '&amp;').replace(/</g, '&lt;');
36 const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}">
37 <text x="2" y="${BASE}" font-family="${font}" font-size="${PX}" font-weight="700" fill="${fill}">${text}</text></svg>`;
38 const full = await sharp(Buffer.from(svg)).png().toBuffer();
39 // as wide as the text (with a pixel each side), the full height kept so the baseline stays put
40 const { info } = await sharp(full).trim({ threshold: 1 }).toBuffer({ resolveWithObject: true });
41 const left = Math.max(0, -info.trimOffsetLeft - 1), width = Math.min(W - left, info.width + 2);
42 const out = path.join(ROOT, 'data/names', `${codes[i]}${suffix}.png`);
43 await sharp(full).extract({ left, top: 0, width: width + (width % 2), height: H }).png().toFile(out);
44 }
45 console.log(`data/names/${codes[i]}: ${names[i]}`);
46}