orbit/js/utils/Fonts.js

23 lines
1015 B
JavaScript

import { config } from '../config/Config.js';
/**
* Wait for the web fonts (data/theme.json) to be ready before the first
* draw, so canvas text uses Ethnocentric/Centauri from frame one.
* Hard timeout so boot can never hang on a flaky asset — we just fall
* back to the stack's system fonts in that case.
*
* Shared by the game boot (js/main.js) and the combat sandbox boot
* (js/combat/main.js) — the two used to each carry a copy.
*/
export async function awaitFonts(timeoutMs = 2000) {
const fonts = globalThis.document?.fonts;
if (!fonts || typeof fonts.load !== 'function') return;
const families = ['header', 'body']
.map((k) => config.get(`theme.fonts.${k}.family`))
.filter((f) => typeof f === 'string' && f.length > 0);
if (families.length === 0) return;
const jobs = families.map((fam) => fonts.load(`16px "${fam}"`).catch(() => {}));
const timeout = new Promise((resolve) => setTimeout(resolve, timeoutMs));
await Promise.race([Promise.allSettled(jobs), timeout]);
}