// fixtures/font-session.mjs — which of the deck's font families this tab // has, and the one rule every path to a slide goes through: slide text // is never painted in a face it will later leave (Paul, 2026-10-26: "No // flashing, ever!"). Docs' font-session.mjs is the model; Slides has // three families and no stylesheet that declares them, which makes the // rule simpler to keep. // // No stylesheet declares a web face (fontgate.go). A family exists for // the page only once this session has loaded all four of its faces and // added them to document.fonts, together. Until then every stack that // names it falls through to the system faces after it. So: // // - a face that is loading is never drawn: it is not in the set; // - a family is never half on: its four faces join the set in one go, // so a bold run can never be drawn in a fallback bold beside a web // regular; // - a family that is committed to its fallback never joins the set, // so when its files arrive late nothing can swap into them. // // A family is idle, loading, loaded, failed or committed. Loaded, // failed and committed are final. Text may be drawn in a family that is // loaded (and then it is added) or committed (and then it stays in its // system face); "ready" means one of the two. Failed is final but not // ready: a person's own action that needs a failed family is refused, // because applying it in a substitute would be a lie about what they // chose. Only content from elsewhere (the page itself, a collaborator, // the projector's next slide) commits a family that will not load. // // "Commit" is only ever for a family that is not on screen in its web // face; one that is never changes. // // No DOM beyond what is passed in: font-session.test.mjs drives it in // Node with a fake FontFace and FontFaceSet. import { FAMILY_KEYS, uploadedFamily } from './font-families.mjs' export { createSystemSession } from './font-system-session.mjs' // The gate shows the slides at this deadline whatever has arrived, // having committed what has not. Measured from navigation start, and // under the three seconds a browser holds font-display: block, so even // a browser that ignored all of this would not have swapped yet. export const GATE_DEADLINE_MS = 2500 // Content from elsewhere — a collaborator's change, a slide the server // rendered, the projector's next slide — waits this long for a family // that is not on screen, then commits it to its fallback. It is never // held back for longer, and never dropped. export const NEW_FAMILY_TIMEOUT_MS = 3000 // A person's own action waits this long for its family, then is // refused and changes nothing. export const ACTION_TIMEOUT_MS = 4000 function delay (ms, setTimer) { return new Promise(resolve => setTimer(() => resolve('late'), ms)) } /** * The session for a page whose gate is running. manifest is the * server's (, fontgate.go); fontSet is * document.fonts; FontFaceCtor is window.FontFace; root is , on * which the committed families are written as data-slides-fonts-fallback * for the tests and for anyone reading the page. */ export function createFontSession ({ manifest, fontSet, FontFaceCtor, root = null, setTimer = setTimeout } = {}) { const families = new Map() // version (vkey) -> { state, faces, promise, added } const inUse = new Set() let shown = false // The deck's own fonts, keyed "u:" + css (fontgate.go's // manifest.uploaded), and any a change brings in during the visit // (learn). They follow every rule the catalog's three do. const uploaded = { ...(manifest && manifest.uploaded) } const isUploaded = key => typeof key === 'string' && key.startsWith('u:') const familyOf = key => (isUploaded(key) ? uploaded[key] : manifest && manifest.families && manifest.families[key]) const known = key => !!familyOf(key) const allKeys = () => [...FAMILY_KEYS.filter(known), ...Object.keys(uploaded)] // An uploaded family is known by its name AND its set of faces: a // family that gains a bold face is, to this tab, a new family under an // old name (final review, I4). Its state lives under its version, the // key and the set together, so the set on the page is never taken for // one that has not loaded, and a set loaded once (undo, redo) is // ready again at once. const facesOf = fam => fam.faces.map(f => [f.url, f.weight, f.style, f.format].join(' ')).join('\n') const vkey = key => (isUploaded(key) && uploaded[key] ? key + '\n' + facesOf(uploaded[key]) : key) const stateOf = key => families.get(vkey(key)) const committedKeys = () => allKeys().filter(k => stateOf(k) && stateOf(k).state === 'committed') const entry = key => { const v = vkey(key) let e = families.get(v) if (!e) { e = { state: 'idle', faces: [], promise: null, added: false } families.set(v, e) } return e } function writeFallback () { if (!root) return const value = committedKeys().join(' ') if ((root.getAttribute('data-slides-fonts-fallback') || '') !== value) root.setAttribute('data-slides-fonts-fallback', value) } // load starts (once) the four faces of a family and resolves to // whether all four loaded. It adds nothing to the page: use() does. // The name goes in as it is, never quoted: FontFace takes its family // literally, so quotes would become part of the name and no stack // would match it. Each face carries its own format (a catalog face is // woff2) and weight, which for a variable face is its range // ("100 900"). function load (key) { if (!known(key)) return Promise.resolve(false) const e = entry(key) if (e.state === 'committed') return Promise.resolve(false) if (e.promise) return e.promise const fam = familyOf(key) e.state = 'loading' e.promise = Promise.resolve() .then(() => { e.faces = fam.faces.map(f => new FontFaceCtor(fam.css, `url("${f.url}") format("${f.format || 'woff2'}")`, { weight: String(f.weight), style: f.style, display: 'block' })) return Promise.all(e.faces.map(face => face.load().then(() => true, () => false))) }) .then(all => all.every(Boolean), () => false) .then(ok => { if (e.state === 'loading') e.state = ok ? 'loaded' : 'failed' return e.state === 'loaded' }) return e.promise } const isReady = key => { if (!known(key)) return true const s = stateOf(key) ? stateOf(key).state : 'idle' return s === 'loaded' || s === 'committed' } // onPage is another set of the uploaded family key that is on the // page, if one is. const onPage = key => { if (!isUploaded(key)) return null for (const [v, e] of families) if (e.added && v.startsWith(key + '\n') && v !== vkey(key)) return e return null } // commit draws a family in its fallback for the rest of the visit, // unless it is already on screen in its web face. A new set of an // uploaded family committed while an older set is on the page leaves // that set where it is: the page does not move. function commit (keys) { for (const k of keys) { if (!known(k)) continue const e = entry(k) if (e.added || e.state === 'committed') continue e.state = 'committed' } writeFallback() } // use puts families on screen: a loaded one's faces join the set, all // four at once, and a committed one stays in its system face. Callers // only use families that are ready. An uploaded family's loaded set // goes on in place of any other set of it on the page, in the same // task, so the text in it never has a frame without its faces. function use (keys) { for (const k of keys) { if (!known(k)) continue const e = entry(k) if (e.state === 'loaded' && !e.added) { const old = onPage(k) if (old) { for (const face of old.faces) if (typeof fontSet.delete === 'function') fontSet.delete(face) old.added = false } for (const face of e.faces) fontSet.add(face) e.added = true } if (e.state === 'loaded' || e.state === 'committed') inUse.add(k) } } function adopt (fam) { const key = 'u:' + fam.css if (!uploaded[key] || facesOf(uploaded[key]) !== facesOf(fam)) uploaded[key] = fam } const session = { web: true, get manifest () { return manifest }, ready: isReady, loaded: key => known(key) && !!stateOf(key) && stateOf(key).state === 'loaded', committed: key => known(key) && !!stateOf(key) && stateOf(key).state === 'committed', failed: key => known(key) && !!stateOf(key) && stateOf(key).state === 'failed', state: key => (stateOf(key) ? stateOf(key).state : 'idle'), get inUse () { return [...inUse] }, get fallback () { return committedKeys() }, // Every family this page can load: the catalog's, then the deck's own. get keys () { return allKeys() }, get uploaded () { return uploaded }, // learn takes in the families of a deck's meta.fontFiles (a pick, a // collaborator's: editor-fonts.mjs), fetched under base. A family // whose set of faces is new to this tab is that set from now on, not // ready until it loads; a set it has had before keeps its state. learn (fontFiles, base) { for (const f of Array.isArray(fontFiles) ? fontFiles : []) { const fam = uploadedFamily(f, base) if (fam) adopt(fam) } }, // learnUploaded is learn for families already in the manifest's // shape (manifest.uploaded: another page's, a talk's), by key. learnUploaded (families) { for (const fam of Object.values(families || {})) if (fam && typeof fam.css === 'string' && Array.isArray(fam.faces)) adopt(fam) }, use, load, markShown () { shown = true }, get shown () { return shown }, // gate is the page's first wait: every family the page needs, until // the deadline. A family still not loaded then is committed, which // is safe because nothing is on screen yet; the rest are put on. // Resolves to the families committed. async gate (need, deadlineMs) { const keys = need.filter(known) const all = Promise.all(keys.map(load)) await Promise.race([all, delay(deadlineMs, setTimer)]) const late = keys.filter(k => !session.loaded(k)) commit(late) use(keys) return late }, // waitFor is a person's own action: it resolves true once every key // has loaded (or was committed before), false if one failed or the // timeout came first. It commits nothing and puts nothing on. async waitFor (keys, timeout = ACTION_TIMEOUT_MS) { const want = keys.filter(known) if (want.every(isReady)) return true const all = Promise.all(want.map(k => (isReady(k) ? true : load(k)))) await Promise.race([all, delay(timeout, setTimer)]) return want.every(isReady) }, // prepare is content from elsewhere: every family it needs is made // ready. One already on screen is ready by construction; one that is // not gets the timeout and is then committed. It never rejects and // it always puts the families on, so the content can be shown. async prepare (keys, { timeout = NEW_FAMILY_TIMEOUT_MS } = {}) { const want = [...new Set(keys)].filter(known) const cold = want.filter(k => !isReady(k)) if (cold.length) { await Promise.race([Promise.all(cold.map(load)), delay(timeout, setTimer)]) commit(cold.filter(k => !session.loaded(k))) } use(want) }, // commitFallback is for the one change that never waits: an IME // composition. Families not on screen are committed and put on. commitFallback (keys) { commit(keys.filter(k => !isReady(k))) use(keys) }, // prefetch loads every family not loaded yet, one at a time when the // page is idle, so that an action rarely has to wait. idle() resolves // when the page is idle. It commits nothing and puts nothing on. async prefetch ({ idle = () => Promise.resolve() } = {}) { for (const k of allKeys()) { if (!known(k) || stateOf(k)) continue await idle() if (stateOf(k)) continue await load(k) } } } return session }