[226] Schriftfamilien im GPU-Text: Familie je Zeile im Szenenformat, Glyphen-Atlas je Familie/Schnitt lazy (WOFF2 in render2d), Messung mit denselben Metriken

This commit is contained in:
Claude committed 2026-11-12 22:43:58 +00:00
1 parent 35c26f9e85
commit cc14b1c837
84 files changed
+1063 -31

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { existsSync } from "node:fs";
import { TEXT_FONTS } from "./fontList";
import { fontFileUrl, loadFontFile } from "./glyphFonts";
describe("glyphFonts", () => {
it("jeder Schlüssel zeigt auf eine vorhandene Datei (ausser fehlenden Schnitten)", () => {
const missing: string[] = [];
for (let id = 2; id <= TEXT_FONTS.length; id++) {
for (let style = 0; style < 4; style++) {
const url = fontFileUrl((id << 8) | style)!;
expect(url).toMatch(/^\/fonts\/[a-z0-9-]+\/[a-z0-9-]+-(400|700)-(normal|italic)\.woff2$/);
if (!existsSync(`public${url}`)) missing.push(url);
}
}
// Nur die Kursiv-Schnitte der Familien ohne Kursivdatei fehlen (Manrope, Outfit, Space Grotesk).
expect(missing.every((u) => /manrope|outfit|space-grotesk/.test(u) && u.includes("italic"))).toBe(true);
expect(fontFileUrl(0)).toBeNull();
expect(fontFileUrl(1 << 8)).toBeNull();
expect(fontFileUrl(99 << 8)).toBeNull();
});
it("lädt je Schlüssel einmal; 404 und Netzfehler ergeben null", async () => {
let calls = 0;
const ok = (async () => {
calls++;
return new Response(new Uint8Array([1, 2, 3]));
}) as unknown as typeof fetch;
const key = (15 << 8) | 1;
expect(Array.from((await loadFontFile(key, ok))!)).toEqual([1, 2, 3]);
await loadFontFile(key, ok);
expect(calls).toBe(1);
const nf = (async () => new Response("x", { status: 404 })) as unknown as typeof fetch;
expect(await loadFontFile((14 << 8) | 2, nf)).toBeNull();
const boom = (async () => {
throw new Error("offline");
}) as unknown as typeof fetch;
expect(await loadFontFile((13 << 8) | 3, boom)).toBeNull();
expect(await loadFontFile(0)).toBeNull();
});
});
+43
View File
@@ -0,0 +1,43 @@
// Schriftdateien für den GPU-Glyphen-Pass ([226]): render2d zeichnet Texte mit der gebündelten Familie der
// Zeile (Szenenformat: Kennung = Index in `TEXT_FONTS` + 1, 0 = Inter) und fordert die Dateien lazy an
// (`needed_fonts`, Schlüssel = Familie << 8 | Schnitt, Bit 0 fett, Bit 1 kursiv). Hier: Schlüssel → URL → Bytes.
// Die Dateien sind dieselben WOFF2 wie im CSS (`public/fonts`, `fonts.css`); render2d entpackt sie selbst.
import { TEXT_FONTS } from "./fontList";
export const FONT_STYLE_BOLD = 1;
export const FONT_STYLE_ITALIC = 2;
/** `Familie << 8 | Schnitt` → URL der WOFF2-Datei (`null` für Inter/unbekannte Kennung). */
export function fontFileUrl(key: number): string | null {
const family = key >>> 8;
const style = key & 0xff;
const name = family >= 2 ? TEXT_FONTS[family - 1] : undefined;
if (!name) return null;
const slug = name.toLowerCase().replace(/\s+/g, "-");
const weight = style & FONT_STYLE_BOLD ? 700 : 400;
const slant = style & FONT_STYLE_ITALIC ? "italic" : "normal";
return `/fonts/${slug}/${slug}-${weight}-${slant}.woff2`;
}
const cache = new Map<number, Promise<Uint8Array | null>>();
/**
* Bytes der Schriftdatei zu `key` laden (je Schlüssel einmal, auch bei Fehlschlag). `null` = es gibt keine
* (404, Netz-/Dateifehler): der Pass bleibt dann bei der Ersatz-Variante bzw. Inter. Lokale App-Dateien,
* kein Netzabruf nach aussen (daher `fetch` statt `netFetch`).
*/
export function loadFontFile(key: number, fetchFn: typeof fetch = fetch): Promise<Uint8Array | null> {
let p = cache.get(key);
if (!p) {
const url = fontFileUrl(key);
p = url
? fetchFn(url).then(
async (r) => (r.ok ? new Uint8Array(await r.arrayBuffer()) : null),
() => null,
)
: Promise.resolve(null);
cache.set(key, p);
}
return p;
}