| 1 | /* |
| 2 | make-names.mjs: the language screen's Chinese, Japanese and Korean names as pictures, so drawing |
| 3 | the 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 |
|
| 10 | Each is as wide as its text and 64 px high, the baseline at 46 (ui.c draws them at half that, where |
| 11 | font_draw's line would be). Drawn with Windows' own CJK fonts; uses sharp from generator/node_modules. |
| 12 | */ |
| 13 | import fs from 'node:fs'; |
| 14 | import path from 'node:path'; |
| 15 | import { createRequire } from 'node:module'; |
| 16 | import { fileURLToPath } from 'node:url'; |
| 17 |
|
| 18 | const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 19 | const sharp = createRequire(path.join(ROOT, 'generator/'))('sharp'); |
| 20 |
|
| 21 | // lang.c's codes[] and names[], in its order: the ones with CJK characters |
| 22 | const lang = fs.readFileSync(path.join(ROOT, 'src/lang.c'), 'utf8'); |
| 23 | const list = name => [...lang.match(new RegExp(`${name}\\[LANG_COUNT\\] = \\{([^}]*)\\}`))[1].matchAll(/"([^"]*)"/g)].map(m => m[1]); |
| 24 | const codes = list('codes'), names = list('names'); |
| 25 | const FONTS = { zh: 'Microsoft YaHei', ja: 'Yu Gothic', ko: 'Malgun Gothic' }; |
| 26 | const COLOURS = { '': '#ffffff', '-on': '#4ad9ff' }; |
| 27 | const H = 64, BASE = 46, PX = 44, W = 600; |
| 28 |
|
| 29 | fs.mkdirSync(path.join(ROOT, 'data/names'), { recursive: true }); |
| 30 | for (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, '&').replace(/</g, '<'); |
| 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 | } |