import figlet from './vendor/figlet.js'; import {fonts} from './font-catalog.js'; export const styles = fonts; export const FONT_CACHE = `ascii-pwa-fonts-${encodeURIComponent(new URL('./', import.meta.url).href)}-figlet-1.8.2`; const pendingFonts = new Map(); // Keep existing lowercase font links working with the library's font names. export function resolveFont(name) { return fonts.find(font => font.toLowerCase() === String(name).toLowerCase()) || 'Standard'; } export function registerFont(name, data) { if (!fonts.includes(name)) throw new Error('Unknown lettering style'); figlet.parseFont(name, data); } export function renderLoaded(text, font = 'Standard') { if (text.length > 500) throw new Error('Text is too long'); if (!fonts.includes(font)) throw new Error('Unknown lettering style'); return figlet.textSync(text, { font, width: 80, whitespaceBreak: true, }).split('\n').filter(line => line.trim()).join('\n'); } export async function render(text, font = 'Standard') { if (text.length > 500) throw new Error('Text is too long'); if (!fonts.includes(font)) throw new Error('Unknown lettering style'); if (!figlet.figFonts[font]) { if (!pendingFonts.has(font)) { pendingFonts.set(font, (async () => { const url = new URL(`./fonts/figlet/${font}.flf`, import.meta.url).href; let cache; let cached; try { if ('caches' in globalThis) { cache = await caches.open(FONT_CACHE); cached = await cache.match(url); } } catch { /* Rendering still works if browser storage is unavailable. */ } const response = cached || await fetch(url); if (!response.ok) throw new Error('Cannot load font'); registerFont(font, await response.clone().text()); if (cache && !cached) { try { await cache.put(url, response); } catch { /* Storage may be full. */ } } })().finally(() => pendingFonts.delete(font))); } await pendingFonts.get(font); } return renderLoaded(text, font); }