// fixtures/font-families.mjs — which of the three font families, and of // the deck's own uploaded ones, a deck's slides can be drawn in: // internal/slides/render/families.go's FamiliesUsed and UploadedUsed, // rule for rule, for a deck the browser has just changed // (a local edit, a collaborator's update, a resync) and the server has // not rendered yet. fixtures/vectors/font-families.json holds the two to // the same answers; internal/slidestest's // TestFontTheGateCoversEveryFaceADeckUses holds both to what a browser // actually draws. // // Families, not faces (Docs' review 4). A family is referenced when the // deck's heading or body stack names it — the theme's own, or the // deck's meta.fonts override where it passes render.go's safeFontStack — // or when any slide carries code (a code block, or a run with the code // mark in any block's text or table cell), which is set in the code // stack. A stack names a family wherever it appears in it, because a // browser falls through a stack name by name and glyph by glyph. // // The stacks themselves come from the server, on the font manifest // (fontgate.go: themes, defaultTheme, code), so the theme table lives in // one place, render/themes.go. // // No DOM: font-families.test.mjs runs it in Node. // FAMILY_NAMES is fonts.Family.CSSName by key. export const FAMILY_NAMES = { sans: 'Liberation Sans', serif: 'Liberation Serif', mono: 'Liberation Mono' } export const FAMILY_KEYS = ['sans', 'serif', 'mono'] // render.go's fontPattern: names, quotes, commas and the generic // families; no parentheses. const FONT_PATTERN = /^[A-Za-z0-9 ,'._-]{1,200}$/ /** render.go's safeFontStack: the trimmed stack if it passes the allowlist, else ''. */ export function safeFontStack (v) { if (typeof v !== 'string') return '' const t = v.trim() return FONT_PATTERN.test(t) ? t : '' } /** fonts.FamiliesInStack: every family key the stack names, anywhere, in FAMILY_KEYS order. */ export function familiesInStack (stack) { if (typeof stack !== 'string' || !stack) return [] const named = new Set() for (const part of stack.split(',')) { const name = part.trim().replace(/^["']+|["']+$/g, '').toLowerCase() for (const k of FAMILY_KEYS) if (name === FAMILY_NAMES[k].toLowerCase()) named.add(k) } return FAMILY_KEYS.filter(k => named.has(k)) } /** render.DeckStacks: [heading, body] for a view's meta, given the manifest's stacks. */ export function deckStacks (meta, manifest) { const themes = manifest.themes || {} const theme = meta && typeof meta.theme === 'string' && themes[meta.theme] ? meta.theme : manifest.defaultTheme let [heading, body] = themes[theme] || ['', ''] const f = meta && meta.fonts if (f && typeof f === 'object') { const h = safeFontStack(f.heading) const b = safeFontStack(f.body) if (h) heading = h if (b) body = b } return [heading, body] } function textHasCode (text) { if (!Array.isArray(text)) return false return text.some(p => Array.isArray(p) && p.some(r => r && r.c)) } /** render.HasCode: a code block, or a code-marked run in any block's text or table cell. */ export function hasCode (view) { for (const s of (view && view.slides) || []) { for (const b of s.blocks || []) { if (b.type === 'code' || textHasCode(b.text)) return true if (b.type === 'table') { const cells = b.props && Array.isArray(b.props.cells) ? b.props.cells : [] if (cells.some(textHasCode)) return true } } } return false } /** render.LeadFamily: the first family a stack names, trimmed and unquoted ('' for none). */ export function leadFamily (stack) { if (typeof stack !== 'string') return '' return stack.split(',')[0].trim().replace(/^["']+|["']+$/g, '') } // The uploaded families' keys ("u:" + css) among names, in the order // of csses, each once: render.UploadedUsed's rule, a family is used // when a stack leads with it, compared without regard to case. function uploadedKeys (csses, stacks) { const leads = new Set(stacks.map(leadFamily).filter(Boolean).map(n => n.toLowerCase())) const seen = new Set() const out = [] for (const css of csses) { if (typeof css !== 'string' || !css) continue const k = css.toLowerCase() if (seen.has(k) || !leads.has(k)) continue seen.add(k) out.push('u:' + css) } return out } /** * render.FamiliesUsed then render.UploadedUsed: the families a view's * slides can be drawn in, the catalog's in FAMILY_KEYS order and then * the deck's own (meta.fontFiles) its heading or body stack leads with, * as "u:" + css, in fontFiles order. */ export function familiesOfView (view, manifest) { if (!view || !Array.isArray(view.slides) || view.slides.length === 0) return [] const [heading, body] = deckStacks(view.meta, manifest) const stacks = [heading, body] if (hasCode(view)) stacks.push(manifest.code) const named = new Set(stacks.flatMap(familiesInStack)) const files = view.meta && Array.isArray(view.meta.fontFiles) ? view.meta.fontFiles : [] return [...FAMILY_KEYS.filter(k => named.has(k)), ...uploadedKeys(files.map(f => f && f.css), [heading, body])] } // fontgate.go's uploadedFormats: meta.fontFiles' format as CSS format(). const FORMATS = { ttf: 'truetype', otf: 'opentype', woff: 'woff', woff2: 'woff2' } const REF = /^[0-9a-f]{64}$/ /** * fontgate.go's uploadedFamily: one family of meta.fontFiles as the * manifest carries it, its faces fetched from base ("/d/{sqid}", * "/s/{token}" or "/p/{session}"); null for one with no face to load. */ export function uploadedFamily (family, base) { if (!family || typeof family.css !== 'string' || !family.css) return null const faces = [] for (const f of Array.isArray(family.faces) ? family.faces : []) { if (!f || !FORMATS[f.format] || !REF.test(f.ref) || !Array.isArray(f.weight)) continue const [min, max] = f.weight faces.push({ weight: min === max ? min : `${min} ${max}`, style: f.italic ? 'italic' : 'normal', url: `${base}/fonts/${f.ref}`, format: FORMATS[f.format] }) } return faces.length ? { css: family.css, faces } : null } /** * The families a slide section the server rendered can be drawn in: the * heading and body stacks on its style attribute, and the code stack if * it carries code; and the uploaded families the manifest knows * (manifest.uploaded) that either stack leads with. What the stage and the projector install is checked * with this, because the server can be ahead of this tab: a slide * fetched after a local edit already holds a collaborator's change the * tab has not received yet. A conservative reading of the markup, so a * false positive costs one download, never a flash. * * styleOf(html) is the section's style attribute and hasCodeIn(html) * whether it holds a
 or , so this runs with no DOM in a
 * test; sectionReader below is the browser's pair.
 */
export function familiesOfSection (html, manifest, { styleOf, hasCodeIn } = sectionReader()) {
  if (typeof html !== 'string' || !html) return []
  const style = styleOf(html) || ''
  const stacks = []
  for (const decl of style.split(';')) {
    const i = decl.indexOf(':')
    if (i < 0) continue
    const name = decl.slice(0, i).trim()
    if (name === '--slides-font-heading' || name === '--slides-font-body') stacks.push(decl.slice(i + 1))
  }
  const own = [...stacks]
  if (hasCodeIn(html)) stacks.push(manifest.code)
  const named = new Set(stacks.flatMap(familiesInStack))
  const uploaded = Object.values(manifest.uploaded || {}).map(f => f && f.css)
  return [...FAMILY_KEYS.filter(k => named.has(k)), ...uploadedKeys(uploaded, own)]
}

/** Reads a section through an inert