// scene-fonts.mjs — the deck's faces a scene is sent (scenes and clips // spec §3, docs/scenes.md "Fonts"), for scene-layer.mjs: the families // its data names in data.fonts, found in the page's font manifest, each // face read once a page as a data: URL, the only way a face reaches a // sealed frame. /** The deck faces a scene's data names (data.fonts), from a page's font manifest. */ export function sceneFonts (data, manifest) { const names = data && Array.isArray(data.fonts) ? data.fonts.filter(n => typeof n === 'string') : [] if (!names.length || !manifest) return [] const families = [...Object.values(manifest.uploaded || {}), ...Object.values(manifest.families || {})] const out = [] for (const name of names) { const fam = families.find(f => f && f.css === name) for (const face of (fam && Array.isArray(fam.faces) ? fam.faces : [])) { if (face && face.url) out.push({ family: name, url: face.url, weight: String(face.weight || 400), style: face.style || 'normal' }) } } return out } // asData is a font file as a data: URL. Each file is read once a page. const read = new Map() function asData (win, url) { if (!read.has(url)) { read.set(url, win.fetch(url, { credentials: 'same-origin' }) .then(r => (r.ok ? r.blob() : null)) .then(b => b && new Promise(resolve => { const f = new win.FileReader() f.onload = () => resolve(f.result) f.onerror = () => resolve(null) f.readAsDataURL(b) })) .catch(() => null)) } return read.get(url) } function manifestOf (doc) { try { const m = doc.querySelector('meta[name="slides-fonts"]') return m ? JSON.parse(m.getAttribute('content') || 'null') : null } catch { return null } } /** The faces data names, each as {family, src, weight, style} with src a data: URL; a face that cannot be read is left out. */ export function frameFonts (data, doc, win) { return Promise.all(sceneFonts(data, manifestOf(doc)).map(f => asData(win, f.url).then(src => src && { family: f.family, src, weight: f.weight, style: f.style }))).then(l => l.filter(Boolean)) }