// font-files.mjs — meta.fontFiles: the uploaded fonts a deck uses, as // {family, css, faces: [{ref, format, weight: [min, max], italic, // pictures}]}. core/src/schema.rs's font_files is the twin, message for // message: the ops refuse a list on the first thing wrong with it, the // view drops the whole list to absent. export const FONT_FORMATS = ['ttf', 'otf', 'woff', 'woff2'] export const MAX_FONT_FAMILIES = 32 export const MAX_FONT_FACES = 16 export const MAX_FONT_NAME = 200 const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v) // The one pattern for an uploaded family's CSS name, shared with the // library and layout (internal/slides/deck's FontCSSName). No comma or // quote: the name is written into a quoted font-family as it is. const CSS_NAME = /^[A-Za-z0-9][A-Za-z0-9 ._-]{0,199}$/ const REF = /^[0-9a-f]{64}$/ // CSS names an uploaded family may not take, lowercase: the catalog's // families, which it would join in document.fonts, and CSS's generic // family keywords. core/src/schema.rs's RESERVED_FONT_NAMES and // internal/slides/library.go's reservedFontName say the same. export const RESERVED_FONT_NAMES = ['liberation sans', 'liberation serif', 'liberation mono', 'serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui', 'ui-serif', 'ui-sans-serif', 'ui-monospace', 'ui-rounded', 'math', 'emoji', 'fangsong'] const isWeight = n => Number.isInteger(n) && n >= 1 && n <= 1000 /** The list rebuilt from its known fields; throws Error(message) on the first thing wrong. */ export function fontFilesValue (v) { const fail = msg => { throw new Error(msg) } if (!Array.isArray(v)) fail('fontFiles must be a list') if (v.length > MAX_FONT_FAMILIES) fail(`fontFiles has more than ${MAX_FONT_FAMILIES} families`) const seen = new Set() return v.map((fam, i) => { const at = `fontFiles[${i}]` if (!isObject(fam)) fail(`${at} must be a map`) if (typeof fam.family !== 'string') fail(`${at}.family must be a string`) if ([...fam.family].length > MAX_FONT_NAME) fail(`${at}.family is longer than ${MAX_FONT_NAME} characters`) if (typeof fam.css !== 'string') fail(`${at}.css must be a string`) if (!CSS_NAME.test(fam.css)) fail(`${at}.css is not a font family name`) if (RESERVED_FONT_NAMES.includes(fam.css.toLowerCase())) fail(`${at}.css ${JSON.stringify(fam.css)} is a name stacks already use`) // A browser matches family names without regard to case. if (seen.has(fam.css.toLowerCase())) fail(`${at}.css ${JSON.stringify(fam.css)} is used twice`) seen.add(fam.css.toLowerCase()) if (!Array.isArray(fam.faces)) fail(`${at}.faces must be a list`) if (fam.faces.length > MAX_FONT_FACES) fail(`${at}.faces has more than ${MAX_FONT_FACES} faces`) const faces = fam.faces.map((face, j) => { const fa = `${at}.faces[${j}]` if (!isObject(face)) fail(`${fa} must be a map`) if (typeof face.ref !== 'string') fail(`${fa}.ref must be a string`) if (!REF.test(face.ref)) fail(`${fa}.ref is not 64 lowercase hex digits`) if (typeof face.format !== 'string') fail(`${fa}.format must be a string`) if (!FONT_FORMATS.includes(face.format)) fail(`${fa}.format ${JSON.stringify(face.format)} is not one of ${FONT_FORMATS.join(', ')}`) if (!Array.isArray(face.weight) || face.weight.length !== 2) fail(`${fa}.weight must be [min, max]`) const [min, max] = face.weight if (!isWeight(min) || !isWeight(max)) fail(`${fa}.weight must be whole numbers from 1 to 1000`) if (min > max) fail(`${fa}.weight min is above max`) if (typeof face.italic !== 'boolean') fail(`${fa}.italic must be true or false`) if (typeof face.pictures !== 'boolean') fail(`${fa}.pictures must be true or false`) return { ref: face.ref, format: face.format, weight: [min, max], italic: face.italic, pictures: face.pictures } }) return { family: fam.family, css: fam.css, faces } }) }