| 1 | // fixtures/font-families.mjs — which of the three font families, and of |
| 2 | // the deck's own uploaded ones, a deck's slides can be drawn in: |
| 3 | // internal/slides/render/families.go's FamiliesUsed and UploadedUsed, |
| 4 | // rule for rule, for a deck the browser has just changed |
| 5 | // (a local edit, a collaborator's update, a resync) and the server has |
| 6 | // not rendered yet. fixtures/vectors/font-families.json holds the two to |
| 7 | // the same answers; internal/slidestest's |
| 8 | // TestFontTheGateCoversEveryFaceADeckUses holds both to what a browser |
| 9 | // actually draws. |
| 10 | // |
| 11 | // Families, not faces (Docs' review 4). A family is referenced when the |
| 12 | // deck's heading or body stack names it — the theme's own, or the |
| 13 | // deck's meta.fonts override where it passes render.go's safeFontStack — |
| 14 | // or when any slide carries code (a code block, or a run with the code |
| 15 | // mark in any block's text or table cell), which is set in the code |
| 16 | // stack. A stack names a family wherever it appears in it, because a |
| 17 | // browser falls through a stack name by name and glyph by glyph. |
| 18 | // |
| 19 | // The stacks themselves come from the server, on the font manifest |
| 20 | // (fontgate.go: themes, defaultTheme, code), so the theme table lives in |
| 21 | // one place, render/themes.go. |
| 22 | // |
| 23 | // No DOM: font-families.test.mjs runs it in Node. |
| 24 | |
| 25 | // FAMILY_NAMES is fonts.Family.CSSName by key. |
| 26 | export const FAMILY_NAMES = { sans: 'Liberation Sans', serif: 'Liberation Serif', mono: 'Liberation Mono' } |
| 27 | export const FAMILY_KEYS = ['sans', 'serif', 'mono'] |
| 28 | |
| 29 | // render.go's fontPattern: names, quotes, commas and the generic |
| 30 | // families; no parentheses. |
| 31 | const FONT_PATTERN = /^[A-Za-z0-9 ,'._-]{1,200}$/ |
| 32 | |
| 33 | /** render.go's safeFontStack: the trimmed stack if it passes the allowlist, else ''. */ |
| 34 | export function safeFontStack (v) { |
| 35 | if (typeof v !== 'string') return '' |
| 36 | const t = v.trim() |
| 37 | return FONT_PATTERN.test(t) ? t : '' |
| 38 | } |
| 39 | |
| 40 | /** fonts.FamiliesInStack: every family key the stack names, anywhere, in FAMILY_KEYS order. */ |
| 41 | export function familiesInStack (stack) { |
| 42 | if (typeof stack !== 'string' || !stack) return [] |
| 43 | const named = new Set() |
| 44 | for (const part of stack.split(',')) { |
| 45 | const name = part.trim().replace(/^["']+|["']+$/g, '').toLowerCase() |
| 46 | for (const k of FAMILY_KEYS) if (name === FAMILY_NAMES[k].toLowerCase()) named.add(k) |
| 47 | } |
| 48 | return FAMILY_KEYS.filter(k => named.has(k)) |
| 49 | } |
| 50 | |
| 51 | /** render.DeckStacks: [heading, body] for a view's meta, given the manifest's stacks. */ |
| 52 | export function deckStacks (meta, manifest) { |
| 53 | const themes = manifest.themes || {} |
| 54 | const theme = meta && typeof meta.theme === 'string' && themes[meta.theme] ? meta.theme : manifest.defaultTheme |
| 55 | let [heading, body] = themes[theme] || ['', ''] |
| 56 | const f = meta && meta.fonts |
| 57 | if (f && typeof f === 'object') { |
| 58 | const h = safeFontStack(f.heading) |
| 59 | const b = safeFontStack(f.body) |
| 60 | if (h) heading = h |
| 61 | if (b) body = b |
| 62 | } |
| 63 | return [heading, body] |
| 64 | } |
| 65 | |
| 66 | function textHasCode (text) { |
| 67 | if (!Array.isArray(text)) return false |
| 68 | return text.some(p => Array.isArray(p) && p.some(r => r && r.c)) |
| 69 | } |
| 70 | |
| 71 | /** render.HasCode: a code block, or a code-marked run in any block's text or table cell. */ |
| 72 | export function hasCode (view) { |
| 73 | for (const s of (view && view.slides) || []) { |
| 74 | for (const b of s.blocks || []) { |
| 75 | if (b.type === 'code' || textHasCode(b.text)) return true |
| 76 | if (b.type === 'table') { |
| 77 | const cells = b.props && Array.isArray(b.props.cells) ? b.props.cells : [] |
| 78 | if (cells.some(textHasCode)) return true |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | /** render.LeadFamily: the first family a stack names, trimmed and unquoted ('' for none). */ |
| 86 | export function leadFamily (stack) { |
| 87 | if (typeof stack !== 'string') return '' |
| 88 | return stack.split(',')[0].trim().replace(/^["']+|["']+$/g, '') |
| 89 | } |
| 90 | |
| 91 | // The uploaded families' keys ("u:" + css) among names, in the order |
| 92 | // of csses, each once: render.UploadedUsed's rule, a family is used |
| 93 | // when a stack leads with it, compared without regard to case. |
| 94 | function uploadedKeys (csses, stacks) { |
| 95 | const leads = new Set(stacks.map(leadFamily).filter(Boolean).map(n => n.toLowerCase())) |
| 96 | const seen = new Set() |
| 97 | const out = [] |
| 98 | for (const css of csses) { |
| 99 | if (typeof css !== 'string' || !css) continue |
| 100 | const k = css.toLowerCase() |
| 101 | if (seen.has(k) || !leads.has(k)) continue |
| 102 | seen.add(k) |
| 103 | out.push('u:' + css) |
| 104 | } |
| 105 | return out |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * render.FamiliesUsed then render.UploadedUsed: the families a view's |
| 110 | * slides can be drawn in, the catalog's in FAMILY_KEYS order and then |
| 111 | * the deck's own (meta.fontFiles) its heading or body stack leads with, |
| 112 | * as "u:" + css, in fontFiles order. |
| 113 | */ |
| 114 | export function familiesOfView (view, manifest) { |
| 115 | if (!view || !Array.isArray(view.slides) || view.slides.length === 0) return [] |
| 116 | const [heading, body] = deckStacks(view.meta, manifest) |
| 117 | const stacks = [heading, body] |
| 118 | if (hasCode(view)) stacks.push(manifest.code) |
| 119 | const named = new Set(stacks.flatMap(familiesInStack)) |
| 120 | const files = view.meta && Array.isArray(view.meta.fontFiles) ? view.meta.fontFiles : [] |
| 121 | return [...FAMILY_KEYS.filter(k => named.has(k)), ...uploadedKeys(files.map(f => f && f.css), [heading, body])] |
| 122 | } |
| 123 | |
| 124 | // fontgate.go's uploadedFormats: meta.fontFiles' format as CSS format(). |
| 125 | const FORMATS = { ttf: 'truetype', otf: 'opentype', woff: 'woff', woff2: 'woff2' } |
| 126 | const REF = /^[0-9a-f]{64}$/ |
| 127 | |
| 128 | /** |
| 129 | * fontgate.go's uploadedFamily: one family of meta.fontFiles as the |
| 130 | * manifest carries it, its faces fetched from base ("/d/{sqid}", |
| 131 | * "/s/{token}" or "/p/{session}"); null for one with no face to load. |
| 132 | */ |
| 133 | export function uploadedFamily (family, base) { |
| 134 | if (!family || typeof family.css !== 'string' || !family.css) return null |
| 135 | const faces = [] |
| 136 | for (const f of Array.isArray(family.faces) ? family.faces : []) { |
| 137 | if (!f || !FORMATS[f.format] || !REF.test(f.ref) || !Array.isArray(f.weight)) continue |
| 138 | const [min, max] = f.weight |
| 139 | faces.push({ weight: min === max ? min : `${min} ${max}`, style: f.italic ? 'italic' : 'normal', url: `${base}/fonts/${f.ref}`, format: FORMATS[f.format] }) |
| 140 | } |
| 141 | return faces.length ? { css: family.css, faces } : null |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * The families a slide section the server rendered can be drawn in: the |
| 146 | * heading and body stacks on its style attribute, and the code stack if |
| 147 | * it carries code; and the uploaded families the manifest knows |
| 148 | * (manifest.uploaded) that either stack leads with. What the stage and the projector install is checked |
| 149 | * with this, because the server can be ahead of this tab: a slide |
| 150 | * fetched after a local edit already holds a collaborator's change the |
| 151 | * tab has not received yet. A conservative reading of the markup, so a |
| 152 | * false positive costs one download, never a flash. |
| 153 | * |
| 154 | * styleOf(html) is the section's style attribute and hasCodeIn(html) |
| 155 | * whether it holds a <pre> or <code>, so this runs with no DOM in a |
| 156 | * test; sectionReader below is the browser's pair. |
| 157 | */ |
| 158 | export function familiesOfSection (html, manifest, { styleOf, hasCodeIn } = sectionReader()) { |
| 159 | if (typeof html !== 'string' || !html) return [] |
| 160 | const style = styleOf(html) || '' |
| 161 | const stacks = [] |
| 162 | for (const decl of style.split(';')) { |
| 163 | const i = decl.indexOf(':') |
| 164 | if (i < 0) continue |
| 165 | const name = decl.slice(0, i).trim() |
| 166 | if (name === '--slides-font-heading' || name === '--slides-font-body') stacks.push(decl.slice(i + 1)) |
| 167 | } |
| 168 | const own = [...stacks] |
| 169 | if (hasCodeIn(html)) stacks.push(manifest.code) |
| 170 | const named = new Set(stacks.flatMap(familiesInStack)) |
| 171 | const uploaded = Object.values(manifest.uploaded || {}).map(f => f && f.css) |
| 172 | return [...FAMILY_KEYS.filter(k => named.has(k)), ...uploadedKeys(uploaded, own)] |
| 173 | } |
| 174 | |
| 175 | /** Reads a section through an inert <template>, which fetches nothing and runs nothing. */ |
| 176 | export function sectionReader () { |
| 177 | const parse = html => { |
| 178 | const t = document.createElement('template') |
| 179 | t.innerHTML = html |
| 180 | return t.content |
| 181 | } |
| 182 | return { |
| 183 | styleOf: html => { |
| 184 | const s = parse(html).querySelector('[slides-slide]') |
| 185 | return s ? s.getAttribute('style') : '' |
| 186 | }, |
| 187 | hasCodeIn: html => !!parse(html).querySelector('pre, code') |
| 188 | } |
| 189 | } |
| 190 | |