| 1 | // fixtures/fonts.mjs — the font vocabulary, read from the one manifest. |
| 2 | // |
| 3 | // internal/fonts/fonts.json names every family: its key (its mark is |
| 4 | // font_<key>), its name, its styles. internal/fonts embeds the same file |
| 5 | // for the server and the PDF, core/src/fragment.rs is tested against it, |
| 6 | // and this module imports it into the editor bundle. There used to be |
| 7 | // four hand-copied lists; with sixteen families there is one. |
| 8 | // |
| 9 | // The rule the vocabulary turns on: THE DEFAULT CARRIES NO MARK. |
| 10 | // Choosing it clears the group rather than setting anything, so a |
| 11 | // document only contains a font mark where somebody asked for a face |
| 12 | // other than the one they would have got anyway. The default is |
| 13 | // Carlito (Paul, 2026-10-24). sans, serif and mono are Liberation, as |
| 14 | // they were when there were only three. |
| 15 | |
| 16 | import manifest from '../internal/fonts/fonts.json' with { type: 'json' } |
| 17 | |
| 18 | // The style names the manifest uses, and what each is in CSS. |
| 19 | const STYLE_CSS = { |
| 20 | regular: { weight: 400, style: 'normal' }, |
| 21 | bold: { weight: 700, style: 'normal' }, |
| 22 | italic: { weight: 400, style: 'italic' }, |
| 23 | boldItalic: { weight: 700, style: 'italic' }, |
| 24 | } |
| 25 | |
| 26 | // FAMILIES is every family in the picker's order, with what the editor |
| 27 | // needs to load one: its private CSS name and the faces it has. |
| 28 | export const FAMILIES = manifest.families.map((f, i) => ({ |
| 29 | key: f.key, |
| 30 | name: f.name, |
| 31 | index: i + 1, |
| 32 | cssName: 'Docs ' + f.name, |
| 33 | docx: f.docx.name, |
| 34 | fallback: f.fallback, |
| 35 | faces: Object.keys(f.styles).map((s) => STYLE_CSS[s]), |
| 36 | })) |
| 37 | |
| 38 | const BY_KEY = new Map(FAMILIES.map((f) => [f.key, f])) |
| 39 | |
| 40 | // The faces a person can choose, in the order the toolbar shows them — |
| 41 | // default first, because a <select> shows its first option until |
| 42 | // something sets a value, and with no JavaScript nothing ever does. |
| 43 | export const FONTS = FAMILIES.map((f) => f.key) |
| 44 | |
| 45 | // The face an unmarked run is in, and the one a code run is in. |
| 46 | export const DEFAULT_FONT = manifest.default |
| 47 | export const CODE_FONT = manifest.code |
| 48 | |
| 49 | // Every font mark, including the default's, in the order a run carrying |
| 50 | // more than one resolves: the LAST in the manifest wins, because |
| 51 | // core/src/fragment.rs opens its spans in manifest order, so that one is |
| 52 | // innermost and the cascade draws it. internal/fonts' ForMarks says the |
| 53 | // same. The editor's schema makes two font marks on a run impossible; |
| 54 | // POST /d/{sqid}/updates does not. |
| 55 | export const FONT_MARKS = FONTS.map((k) => 'font_' + k).reverse() |
| 56 | |
| 57 | // family is the manifest entry for a key, or undefined. |
| 58 | export function family(key) { |
| 59 | return BY_KEY.get(key) |
| 60 | } |
| 61 | |
| 62 | // fontMark maps a chosen face to the mark to set: null for the default, |
| 63 | // which is spelled as the absence of one, and undefined for a value |
| 64 | // this module does not know. |
| 65 | // |
| 66 | // undefined and null are different answers on purpose. null means |
| 67 | // "clear the group", which is a real edit; undefined means "this is not |
| 68 | // one of ours", and a caller that treated it as null would clear a |
| 69 | // person's font because a stale page sent an option this build dropped. |
| 70 | export function fontMark(value) { |
| 71 | if (!BY_KEY.has(value)) return undefined |
| 72 | if (value === DEFAULT_FONT) return null |
| 73 | return 'font_' + value |
| 74 | } |
| 75 | |
| 76 | // fontForMarks is the inverse: given the mark names active on the |
| 77 | // selection, which face is it in. No font mark means the default. |
| 78 | export function fontForMarks(names) { |
| 79 | for (const mark of FONT_MARKS) { |
| 80 | if (names.includes(mark)) return mark.slice('font_'.length) |
| 81 | } |
| 82 | return DEFAULT_FONT |
| 83 | } |
| 84 | |
| 85 | // fontKeysIn is every family a set of mark names asks for. |
| 86 | export function fontKeysIn(names) { |
| 87 | const out = [] |
| 88 | for (const n of names) { |
| 89 | if (n.startsWith('font_') && BY_KEY.has(n.slice(5))) out.push(n.slice(5)) |
| 90 | } |
| 91 | return out |
| 92 | } |
| 93 | |
| 94 | // familyForStack is the family a pasted CSS font-family stack stands for, |
| 95 | // the same rule the native app applies (DocsFonts.family(cssStack:)) and |
| 96 | // from the same manifest, so a paste lands in the same face on both. The |
| 97 | // stack is read in order and the first name that maps wins. A name maps |
| 98 | // by a family's Word name (Calibri is Carlito, Cambria is Caladea, as |
| 99 | // internal/fonts' ByDocxName has it; Docs' own private name, "Docs |
| 100 | // Inter", first, for a run copied out of Docs), then its own name, then a name in |
| 101 | // its fallback stack, in picker order (Georgia is Caladea, Helvetica is |
| 102 | // Liberation Sans, generic serif is Caladea). null when no name maps, |
| 103 | // which is the default, Carlito. fixtures/vectors/font-stacks.json holds |
| 104 | // the cases both sides test. |
| 105 | export function familyForStack(stack) { |
| 106 | const names = String(stack || '') |
| 107 | .split(',') |
| 108 | .map((n) => n.trim().replace(/^["']|["']$/g, '').toLowerCase()) |
| 109 | .filter(Boolean) |
| 110 | for (const name of names) { |
| 111 | const f = |
| 112 | // A run copied out of Docs itself carries its private name. |
| 113 | FAMILIES.find((f) => f.cssName.toLowerCase() === name) || |
| 114 | FAMILIES.find((f) => f.docx.toLowerCase() === name) || |
| 115 | FAMILIES.find((f) => f.name.toLowerCase() === name) || |
| 116 | FAMILIES.find((f) => f.fallback.some((n) => n.toLowerCase() === name)) |
| 117 | if (f) return f.key |
| 118 | } |
| 119 | return null |
| 120 | } |
| 121 | |
| 122 | // The vocabulary is whole: the default and the code face are families, |
| 123 | // and the default is listed first. Runs at module load so the gate's |
| 124 | // tests (fonts.test.mjs imports this) execute it, rather than only a |
| 125 | // browser mounting the editor. |
| 126 | function fontsAreCovered() { |
| 127 | if (!BY_KEY.has(DEFAULT_FONT)) throw new Error(`fonts: the default ${DEFAULT_FONT} is not a family`) |
| 128 | if (!BY_KEY.has(CODE_FONT)) throw new Error(`fonts: the code face ${CODE_FONT} is not a family`) |
| 129 | if (FONTS[0] !== DEFAULT_FONT) { |
| 130 | throw new Error(`fonts: the toolbar lists ${FONTS[0]} first but the default is ${DEFAULT_FONT}`) |
| 131 | } |
| 132 | for (const f of FAMILIES) { |
| 133 | if (!f.faces.some((s) => s.weight === 400 && s.style === 'normal')) throw new Error(`fonts: ${f.key} has no regular`) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fontsAreCovered() |
| 138 | |