Files
feuerwerk/tests/ui/fonts.test.ts
T
adminandClaude Opus 5.5 0e8585665a
game-maker / gate (push) Successful in 2m47s
game-maker / suite (push) Successful in 8m41s
Load the 500 weight and draw missing glyphs as SVG icons (#214)
The theme asks for Source Sans 3 at weight 500, but fonts.ts only
loaded 400, 600 and 700. The browser synthesised the 500 text. The
issue comment also asked for the symbols the font lacks to become SVG
icons. A scan of src/ against the font's cmap found 20 such characters
in 30 places.

- fonts.ts loads source-sans-3-latin-500-normal.woff2 as well.
- Menus draw their ticks and the "current" dot as icons. The dot is a
  new MenuItem.current field and no longer part of the label text.
- Inspector action buttons (reset, apply, fit width/height) are
  icons. The menu arrow behind them is the drawn Dropdown chevron.
- Kind marks come from a table in markIcons.ts. Picture kinds use an
  icon, # and Aa stay text because the font has them.
- Scene marks in the asset panel, sound play/stop and the transition
  arrows are icons too.
- Apply is an icon although the font has the arrow. It sits next to
  the reset icon and would look different as a glyph.
- A new Icon widget draws on redraw like Button, so Node tests need no
  DOMParser.
- Tests find icons through the SVG id. The new hook
  __gm.locateIcons(id) and the harness helpers findIcon/clickIcon
  replace locate() on the old characters.

Findings:
- Weight 700 is loaded but the theme never uses it. Left as is.
- The old scene-mark check in module 35 accepted any play glyph on
  screen. It now checks the mark on the start scene's row.
- Module 44 needs the textures from module 37 and fails without it.

Tests:
- ./check.sh: 145 files, 2187 tests, exit 0 (before 144 / 2143).
- New tests/ui/fonts.test.ts: every theme weight is loaded, and every
  character in src/ exists in every shipped face. The cmap reader
  matches fontTools (231 characters).
- Icon tests cover the 19 new files. Each kind mark is either an
  icon or text, never both.
- e2e 1-16,22,30,34,35,43,44: 586 green, 0 FAIL, then 44 stopped
  for lack of module 37. e2e 1-16,37,44,47,48: 618 green.
  e2e 1-14 on the final build: 454 green.
- Two new e2e checks: loaded weights in 14, a reset icon pixel in 04.
- Counter-probes all fail as expected: 500 removed, a play glyph put
  back, the struct icon removed (one unit test each). In the browser,
  500 removed and reset icon hidden fail exactly the two new checks.
- Editor-suite check sum 1312 (1310 measured by CI at 056a622, +2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 08:13:22 +02:00

144 lines
6.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The editor draws with the faces it ships (#202). A weight without a file falls back to the next
* one the browser has, and a character outside the Latin subset comes from the system font – in CI
* a different one than on the desk (#214). Both are held here against the files themselves.
*/
import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { brotliDecompressSync } from 'node:zlib';
import { describe, expect, it } from 'vitest';
import { FACES } from '../../src/ui/fonts';
const FONT_DIR = 'src/ui/fonts';
/** Every `.ts` file under `dir`. */
function sources(dir: string): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const path = join(dir, entry.name);
if (entry.isDirectory()) return sources(path);
return entry.name.endsWith('.ts') ? [path] : [];
});
}
/** The code of a file without its comments – a character in a comment is never drawn. */
function codeLines(file: string): { line: number; code: string }[] {
return readFileSync(file, 'utf8')
.split('\n')
.map((text, index) => ({ line: index + 1, text: text.trim() }))
.filter(({ text }) => !text.startsWith('//') && !text.startsWith('*') && !text.startsWith('/*'))
.map(({ line, text }) => ({ line, code: text.replace(/\s\/\/.*$/, '') }));
}
/** Reads a WOFF2 variable-length number (UIntBase128). */
function base128(bytes: Uint8Array, at: { i: number }): number {
let value = 0;
for (let n = 0; n < 5; n++) {
const byte = bytes[at.i++]!;
value = value * 128 + (byte & 0x7f);
if ((byte & 0x80) === 0) return value;
}
throw new Error('UIntBase128 longer than five bytes');
}
/** The code points a WOFF2 file maps to a glyph, read from its `cmap` table (format 4 and 12). */
function codePoints(file: string): Set<number> {
const bytes = new Uint8Array(readFileSync(file));
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
expect(view.getUint32(0)).toBe(0x774f4632); // 'wOF2'
const numTables = view.getUint16(12);
const at = { i: 48 };
let cmap: { offset: number; length: number } | null = null;
let offset = 0;
for (let t = 0; t < numTables; t++) {
const flags = bytes[at.i++]!;
const tag = flags & 0x3f;
if (tag === 0x3f) at.i += 4;
const version = flags >> 6;
const origLength = base128(bytes, at);
// glyf (10) and loca (11) are transformed at version 0, every other table at any other version.
const transformed = tag === 10 || tag === 11 ? version === 0 : version !== 0;
const length = transformed ? base128(bytes, at) : origLength;
if (tag === 0) cmap = { offset, length };
offset += length;
}
const tables = brotliDecompressSync(bytes.subarray(at.i, at.i + view.getUint32(20)));
if (!cmap) throw new Error(`${file} has no cmap`);
const table = new DataView(tables.buffer, tables.byteOffset + cmap.offset, cmap.length);
const found = new Set<number>();
for (let s = 0; s < table.getUint16(2); s++) {
const sub = table.getUint32(4 + s * 8 + 4);
const format = table.getUint16(sub);
if (format === 12) {
for (let g = 0; g < table.getUint32(sub + 12); g++) {
const start = table.getUint32(sub + 16 + g * 12);
const end = table.getUint32(sub + 20 + g * 12);
for (let c = start; c <= end; c++) found.add(c);
}
} else if (format === 4) {
const segments = table.getUint16(sub + 6) / 2;
const ends = sub + 14;
const starts = ends + segments * 2 + 2;
const deltas = starts + segments * 2;
const ranges = deltas + segments * 2;
for (let g = 0; g < segments; g++) {
const start = table.getUint16(starts + g * 2);
const end = table.getUint16(ends + g * 2);
const delta = table.getUint16(deltas + g * 2);
const range = table.getUint16(ranges + g * 2);
for (let c = start; c <= end && c !== 0xffff; c++) {
const glyph =
range === 0 ? (c + delta) & 0xffff : table.getUint16(ranges + g * 2 + range + (c - start) * 2);
if (glyph !== 0) found.add(c);
}
}
}
}
return found;
}
describe('editor fonts', () => {
it('loads a face for every weight the editor sets (#214)', () => {
const loaded = new Set(
FACES.filter(([family]) => family === 'Source Sans 3').map(([, , weight]) => weight),
);
const used = new Set(['400']);
for (const file of sources('src'))
for (const [, setting] of readFileSync(file, 'utf8').matchAll(/fontWeight:([^,\n}]*)/g))
for (const [, weight] of setting!.matchAll(/'(\d{3}|bold|normal)'/g))
used.add(weight === 'bold' ? '700' : weight === 'normal' ? '400' : weight!);
expect(used.has('500')).toBe(true);
for (const weight of used) expect(loaded, `weight ${weight}`).toContain(weight);
});
it('ships a file for every face it loads', () => {
const files = readdirSync(FONT_DIR);
for (const [, url, weight] of FACES)
expect(
files.some((f) => url.endsWith(f)),
`${url} (${weight})`,
).toBe(true);
});
it('reads the Latin subset from the files themselves', () => {
const sans = codePoints(join(FONT_DIR, 'source-sans-3-latin-400-normal.woff2'));
expect(sans.has('A'.charCodeAt(0))).toBe(true);
expect(sans.has('é'.charCodeAt(0))).toBe(true);
expect(sans.has('—'.charCodeAt(0))).toBe(true);
expect(sans.has('▶'.charCodeAt(0))).toBe(false);
});
// A symbol the subset lacks is drawn from whatever the system has – so it is an SVG icon instead.
it('has every character the editor writes in each shipped face of its UI font (#214)', () => {
const faces = readdirSync(FONT_DIR).filter((f) => f.startsWith('source-sans-3-'));
const covered = faces.map((f) => codePoints(join(FONT_DIR, f)));
const missing: string[] = [];
for (const file of sources('src'))
for (const { line, code } of codeLines(file))
for (const char of new Set(code))
if (char.codePointAt(0)! > 0x7e && covered.some((set) => !set.has(char.codePointAt(0)!)))
missing.push(`${file}:${line} ${char}`);
expect(missing).toEqual([]);
});
});