oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// font-files.mjs — meta.fontFiles: the uploaded fonts a deck uses, as
2// {family, css, faces: [{ref, format, weight: [min, max], italic,
3// pictures}]}. core/src/schema.rs's font_files is the twin, message for
4// message: the ops refuse a list on the first thing wrong with it, the
5// view drops the whole list to absent.
6
7export const FONT_FORMATS = ['ttf', 'otf', 'woff', 'woff2']
8export const MAX_FONT_FAMILIES = 32
9export const MAX_FONT_FACES = 16
10export const MAX_FONT_NAME = 200
11
12const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v)
13// The one pattern for an uploaded family's CSS name, shared with the
14// library and layout (internal/slides/deck's FontCSSName). No comma or
15// quote: the name is written into a quoted font-family as it is.
16const CSS_NAME = /^[A-Za-z0-9][A-Za-z0-9 ._-]{0,199}$/
17const REF = /^[0-9a-f]{64}$/
18// CSS names an uploaded family may not take, lowercase: the catalog's
19// families, which it would join in document.fonts, and CSS's generic
20// family keywords. core/src/schema.rs's RESERVED_FONT_NAMES and
21// internal/slides/library.go's reservedFontName say the same.
22export const RESERVED_FONT_NAMES = ['liberation sans', 'liberation serif', 'liberation mono', 'serif', 'sans-serif', 'monospace',
23 'cursive', 'fantasy', 'system-ui', 'ui-serif', 'ui-sans-serif', 'ui-monospace', 'ui-rounded', 'math', 'emoji', 'fangsong']
24const isWeight = n => Number.isInteger(n) && n >= 1 && n <= 1000
25
26/** The list rebuilt from its known fields; throws Error(message) on the first thing wrong. */
27export function fontFilesValue (v) {
28 const fail = msg => { throw new Error(msg) }
29 if (!Array.isArray(v)) fail('fontFiles must be a list')
30 if (v.length > MAX_FONT_FAMILIES) fail(`fontFiles has more than ${MAX_FONT_FAMILIES} families`)
31 const seen = new Set()
32 return v.map((fam, i) => {
33 const at = `fontFiles[${i}]`
34 if (!isObject(fam)) fail(`${at} must be a map`)
35 if (typeof fam.family !== 'string') fail(`${at}.family must be a string`)
36 if ([...fam.family].length > MAX_FONT_NAME) fail(`${at}.family is longer than ${MAX_FONT_NAME} characters`)
37 if (typeof fam.css !== 'string') fail(`${at}.css must be a string`)
38 if (!CSS_NAME.test(fam.css)) fail(`${at}.css is not a font family name`)
39 if (RESERVED_FONT_NAMES.includes(fam.css.toLowerCase())) fail(`${at}.css ${JSON.stringify(fam.css)} is a name stacks already use`)
40 // A browser matches family names without regard to case.
41 if (seen.has(fam.css.toLowerCase())) fail(`${at}.css ${JSON.stringify(fam.css)} is used twice`)
42 seen.add(fam.css.toLowerCase())
43 if (!Array.isArray(fam.faces)) fail(`${at}.faces must be a list`)
44 if (fam.faces.length > MAX_FONT_FACES) fail(`${at}.faces has more than ${MAX_FONT_FACES} faces`)
45 const faces = fam.faces.map((face, j) => {
46 const fa = `${at}.faces[${j}]`
47 if (!isObject(face)) fail(`${fa} must be a map`)
48 if (typeof face.ref !== 'string') fail(`${fa}.ref must be a string`)
49 if (!REF.test(face.ref)) fail(`${fa}.ref is not 64 lowercase hex digits`)
50 if (typeof face.format !== 'string') fail(`${fa}.format must be a string`)
51 if (!FONT_FORMATS.includes(face.format)) fail(`${fa}.format ${JSON.stringify(face.format)} is not one of ${FONT_FORMATS.join(', ')}`)
52 if (!Array.isArray(face.weight) || face.weight.length !== 2) fail(`${fa}.weight must be [min, max]`)
53 const [min, max] = face.weight
54 if (!isWeight(min) || !isWeight(max)) fail(`${fa}.weight must be whole numbers from 1 to 1000`)
55 if (min > max) fail(`${fa}.weight min is above max`)
56 if (typeof face.italic !== 'boolean') fail(`${fa}.italic must be true or false`)
57 if (typeof face.pictures !== 'boolean') fail(`${fa}.pictures must be true or false`)
58 return { ref: face.ref, format: face.format, weight: [min, max], italic: face.italic, pictures: face.pictures }
59 })
60 return { family: fam.family, css: fam.css, faces }
61 })
62}
63