Newer
Older
rpg / tools / font-probe.mjs
/**
 * Проба читаемости VT323 c НАСТОЯЩИМ конвейером игры: текст растеризуется
 * в канвас 480×270 (как текстура Pixi при resolution 1), затем растягивается
 * целым множителем с pixelated. Сравнение кеглей и resolution-подхода.
 * Запуск: node tools/font-probe.mjs [скриншот]
 */
import puppeteer from 'puppeteer-core';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const ttf = readFileSync(path.join(root, 'packages/engine/assets/fonts/VT323-Regular.ttf')).toString('base64');
const shot = process.argv[2] ?? '/tmp/rpg_font_probe.png';

const html = `<!doctype html><html><head><meta charset="utf-8"><style>
@font-face { font-family: VT323; src: url(data:font/ttf;base64,${ttf}) format('truetype'); }
body { margin: 0; background: #000; }
canvas { image-rendering: pixelated; }
</style></head><body>
<canvas id="out" width="960" height="540"></canvas>
<script>
const SIZES = [8, 9, 10, 11, 12, 14, 16];
const out = document.getElementById('out').getContext('2d');
out.imageSmoothingEnabled = false;

function raster(size, hint) {
    // Растеризуем строку в маленький канвас (как Pixi Text при resolution 1),
    // затем растягиваем x2 pixelated на выходной канвас.
    const small = document.createElement('canvas');
    small.width = 480; small.height = 34;
    const g = small.getContext('2d');
    g.font = size + 'px VT323';
    g.fillStyle = '#999988';
    g.fillText(size + 'px: клик — идти, Space — удар, I — сумка', 2, 26);
    out.drawImage(small, 0, (SIZES.indexOf(size)) * 68, 960, 68);
}
window.start = () => SIZES.forEach((s) => raster(s));
</script></body></html>`;

const browser = await puppeteer.launch({
    executablePath: '/usr/bin/chromium',
    headless: true,
    args: ['--no-sandbox', '--enable-unsafe-swiftshader', '--use-angle=swiftshader', '--window-size=960,540']
});
const page = await browser.newPage();
await page.setViewport({ width: 960, height: 540 });
await page.setContent(html, { waitUntil: 'networkidle0' });
await page.evaluate(() => document.fonts.ready);
await page.evaluate(() => window.start());
await new Promise((r) => setTimeout(r, 300));
await page.screenshot({ path: shot });
console.log(`Скриншот: ${shot}`);
await browser.close();