| 1 | # Custom fonts Implementation Plan |
| 2 | |
| 3 | > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| 4 | |
| 5 | **Goal:** A person uploads fonts to a library in Slides, picks one for a deck, and every surface shows it behind the font gate, with pictures and the PDF embedding static TrueType faces and falling back otherwise. |
| 6 | |
| 7 | **Architecture:** A new package `typeface` in estilo inspects and describes font files. Slides stores a per-person library, records which fonts each deck may serve (as it does for pictures), lists them in the deck's `meta.fontFiles`, loads them through the font gate, and lets the layout and PDF engines use static TrueType faces. |
| 8 | |
| 9 | **Tech Stack:** Go (rastrillo, GORM), Rust/wasm CRDT core, Yjs fixtures, esbuild bundles, chromedp browser tests, `github.com/andybalholm/brotli` (for WOFF2), stdlib `compress/zlib` (for WOFF). |
| 10 | |
| 11 | **Spec:** `docs/superpowers/specs/2026-11-23-custom-fonts-design.md`. |
| 12 | |
| 13 | ## Global Constraints |
| 14 | |
| 15 | - A font file is at most 10 MiB; a family at most 16 faces; a person at most 32 families. |
| 16 | - Accepted: TTF, OTF, WOFF, WOFF2. Anything else, or anything malformed, is refused before it is stored. |
| 17 | - Pictures and PDF use a face only when it is static, TrueType-outlined, and its `OS/2` fsType allows installable, editable or print-and-preview embedding. Otherwise they fall back to the deck's next font. |
| 18 | - A font is served only through a deck that uses it, and only to someone who can see that deck. A library is visible only to its owner. |
| 19 | - Font files live only in the app's object store, never in a repository, a bundle or an export bundle. |
| 20 | - The gate's rule holds: no flash, and a family late past the deadline commits to its fallback. |
| 21 | - Existing decks render byte for byte as today. |
| 22 | - Every user-facing string is a catalog key and is approved by Paul in copy review before it is written (Task 1). |
| 23 | - Shared machine rules, commit format (subject under 80 characters, body, `Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>`), and `fixtures/*.mjs` under 300 lines, as in the previous slice. |
| 24 | |
| 25 | ## Review Focus |
| 26 | |
| 27 | 1. **A font is deleted from the library while decks use it.** Those decks keep showing it. Task 5, `TestDeletingAFontKeepsDecksThatUseIt`. |
| 28 | 2. **A deck or link that doesn't use a font asks for it.** It answers 404, including through another person's deck. Task 5, `TestAFontIsServedOnlyThroughADeckThatUsesIt`. |
| 29 | 3. **A file that isn't a font, or is truncated, renamed or too big.** It is refused with a clear message and nothing is stored. Task 2 `TestInspectRefuses`, Task 3 `TestUploadRefusesWhatIsNotAFont`. |
| 30 | 4. **A family name outside the font-name pattern** (for example `Timeless Sans™` or a non-Latin name). It still works under a safe CSS name. Task 4 vectors, Task 5 `TestAnOddFamilyNameStillLoads`. |
| 31 | 5. **A variable font with bold text.** Screens use its weight range; pictures and PDF fall back without error. Task 7, `TestAVariableFontFallsBackInPicturesAndPDF`. |
| 32 | |
| 33 | ## Calls this plan makes |
| 34 | |
| 35 | | # | Call | Why | |
| 36 | |---|---|---| |
| 37 | | F1 | During development Slides points at the estilo worktree with a `replace` directive; Task 8 swaps it for a pushed estilo commit. | estilo has no tags; Slides pins pseudo-versions. | |
| 38 | | F2 | Library uploads go through the app as a form post (multipart), not the signed bucket upload pictures use. | 10 MiB is small enough, and the Fonts page then works with scripts off. | |
| 39 | | F3 | Agents and the API can read `meta.fontFiles` but cannot add a font in this slice. | Fonts belong to a person's library, which the API doesn't expose. | |
| 40 | | F4 | The CSS family name is the font's family name with characters outside `fontPattern` removed, or `Uploaded font N` if nothing is left. | `safeFontStack` must accept it. | |
| 41 | | F5 | A deck refers to a face by its content hash, so deleting a library entry leaves the bytes in place while any deck still refers to them. | Review Focus 1. | |
| 42 | |
| 43 | --- |
| 44 | |
| 45 | ### Task 1: Branches and the copy gate |
| 46 | |
| 47 | **Files:** none in the repos until the words are approved. |
| 48 | |
| 49 | - [ ] **Step 1:** Create the Slides branch `feat/custom-fonts` from `design/custom-fonts`, and an estilo worktree at `~/amadan.net/oficina/estilo-typeface` on branch `feat/typeface` from `origin/main`. |
| 50 | - [ ] **Step 2:** Draft and run the copy review for: the account menu item; the Fonts page title, intro and empty state; the upload form's label, button and hint (the reminder to upload only fonts the person may use on the web); each refusal (not a font, too big, too many faces, too many families, already in the library); the family row's delete action and its confirm page; Deck settings' group label for the person's fonts and its "upload a font" link. Save the approved words as `approved-strings.json` in the plan workspace. |
| 51 | |
| 52 | --- |
| 53 | |
| 54 | ### Task 2: The `typeface` package in estilo |
| 55 | |
| 56 | **Files:** |
| 57 | - Create: `typeface/typeface.go`, `typeface/woff.go`, `typeface/typeface_test.go`, `typeface/testdata/LiberationSans-Regular.ttf` (copied from Slides `internal/slides/fonts/files/`, SIL OFL) |
| 58 | - Modify: `go.mod`, `go.sum` (add `github.com/andybalholm/brotli`) |
| 59 | |
| 60 | **Interfaces (produced):** |
| 61 | |
| 62 | ```go |
| 63 | package typeface |
| 64 | |
| 65 | const MaxBytes = 10 << 20 |
| 66 | const MaxFacesPerFamily = 16 |
| 67 | const MaxFamilies = 32 |
| 68 | |
| 69 | type Format string // "ttf", "otf", "woff", "woff2" |
| 70 | type Outline string // "truetype", "cff" |
| 71 | |
| 72 | type Face struct { |
| 73 | Ref string // lowercase hex SHA-256 of the file |
| 74 | Format Format |
| 75 | Outline Outline |
| 76 | Family string // name ID 16, else 1 |
| 77 | Style string // name ID 17, else 2 |
| 78 | Weight int // OS/2 usWeightClass, 1..1000 |
| 79 | WeightMin int // = Weight unless variable |
| 80 | WeightMax int |
| 81 | Italic bool // OS/2 fsSelection bit 0 or head macStyle bit 1 |
| 82 | Variable bool // fvar present with a 'wght' axis |
| 83 | FSType uint16 // OS/2 fsType |
| 84 | } |
| 85 | |
| 86 | var ErrNotAFont, ErrTooBig, ErrMalformed error |
| 87 | |
| 88 | func Inspect(data []byte) (Face, error) |
| 89 | func (f Face) Pictures() bool // static, truetype, embedding allowed |
| 90 | func (f Face) ContentType() string // font/ttf, font/otf, font/woff, font/woff2 |
| 91 | func Headers(w http.ResponseWriter, f Format) |
| 92 | ``` |
| 93 | |
| 94 | `Pictures()`: not `Variable`, `Outline == "truetype"`, format `ttf` or `otf`, and fsType allows embedding (bits 0x0002 restricted-licence alone means no; 0x0200 bitmap-only means no). `Headers` sets the content type, `X-Content-Type-Options: nosniff`, `Cache-Control: public, max-age=31536000, immutable`, and `Content-Security-Policy: default-src 'none'`. |
| 95 | |
| 96 | - [ ] **Step 1: Write the failing tests**, building WOFF and WOFF2 copies of the test TTF inside the test (WOFF with zlib; WOFF2 with brotli and null-transformed `glyf`/`loca`), plus synthetic sfnts for CFF (`OTTO` with a stub `CFF ` table), variable (an `fvar` with a `wght` axis 100–900) and restricted fsType: |
| 97 | `TestInspectReadsTheNamesAndStyle` (family `Liberation Sans`, weight 400, not italic, truetype, ttf); |
| 98 | `TestInspectReadsWOFFAndWOFF2` (same answers as the TTF); |
| 99 | `TestInspectReadsCFFAndVariable`; |
| 100 | `TestPicturesOnlyForStaticTrueTypeThatMayEmbed`; |
| 101 | `TestInspectRefuses` (empty, a PNG, a truncated table directory, a table past the end, over `MaxBytes`, a WOFF2 whose brotli stream is bad). |
| 102 | - [ ] **Step 2:** `go test ./typeface/ -count=1` — Expected: FAIL, undefined. |
| 103 | - [ ] **Step 3: Implement.** Read only the table directory, `head`, `name`, `OS/2`, `fvar`; bounds-check every offset; never trust a length. |
| 104 | - [ ] **Step 4:** `go test ./typeface/ -count=1` then `make test` in estilo — Expected: PASS. |
| 105 | - [ ] **Step 5: Commit** in estilo: `typeface: inspect TTF, OTF, WOFF and WOFF2 font files`. |
| 106 | |
| 107 | --- |
| 108 | |
| 109 | ### Task 3: The library in Slides |
| 110 | |
| 111 | **Files:** |
| 112 | - Create: `internal/slides/library.go`, `internal/slides/templates/fonts.html`, `fonts_confirm.html`, `internal/slidestest/library_test.go` |
| 113 | - Modify: `go.mod` (`replace amadan.net/oficina/estilo.git => ../estilo-typeface`, F1), `internal/slides/models.go`, `chrome.go:261`, `app.go` (routes inside the signed-in group), `locales/en.toml`; generate `migrations/0011_*.sql` |
| 114 | |
| 115 | **Interfaces (produced):** |
| 116 | |
| 117 | ```go |
| 118 | type LibraryFont struct { // table library_fonts |
| 119 | ID uint |
| 120 | Owner string // the person's actor id, indexed |
| 121 | Family string |
| 122 | CSSName string // F4 |
| 123 | Ref string // typeface.Face.Ref; unique with Owner |
| 124 | Format, Outline, Style string |
| 125 | Weight, WeightMin, WeightMax int |
| 126 | Italic, Variable, Pictures bool |
| 127 | Size int64 |
| 128 | CreatedAt time.Time |
| 129 | } |
| 130 | func fontObjectKey(ref string) string // "fonts/" + ref |
| 131 | func cssFamilyName(family string, n int) string // F4 |
| 132 | ``` |
| 133 | |
| 134 | Routes: `GET /fonts` (page), `POST /fonts` (multipart field `file`, one or more files), `GET /fonts/delete?family=<css>` (confirm page), `POST /fonts/delete` (form field `family`). Uploading stores the bytes at `fontObjectKey(ref)` through `a.cfg.Blobs` and writes a row per face. Without a store the page says fonts are unavailable (503 for the POST). |
| 135 | |
| 136 | - [ ] **Step 1: Write the failing tests:** `TestUploadAFontAndSeeItListed`, `TestUploadRefusesWhatIsNotAFont` (PNG renamed `.ttf`, 11 MiB, truncated: each a 422 with its approved message and no row or object), `TestTheLibraryLimits` (17th face, 33rd family), `TestALibraryIsOnlyItsOwners`, `TestDeletingAFamilyRemovesItsRows` (and its objects, since no deck refers to them yet), `TestTheFontsPageWorksWithoutScripts`, `TestTheAccountMenuListsFonts`. |
| 137 | - [ ] **Step 2:** Run them — FAIL. |
| 138 | - [ ] **Step 3: Implement;** generate the migration with `go run amadan.net/rastrillo/rastrillo/cmd/rastrillo migration generate` and read it. |
| 139 | - [ ] **Step 4:** `go test ./internal/slidestest -run 'Font|Library|Account' -count=1` — PASS; `make migration-check` — PASS. |
| 140 | - [ ] **Step 5: Commit:** `Fonts: a library of your own fonts`. |
| 141 | |
| 142 | --- |
| 143 | |
| 144 | ### Task 4: `meta.fontFiles` in the document |
| 145 | |
| 146 | **Files:** `core/src/ops.rs`, `core/src/view.rs`, `core/tests/ops.rs`, `fixtures/ops-parse.mjs`, `ops-apply.mjs`, `view.mjs`, `fixtures/data/canvas.json`, `internal/slides/deck/deck.go`, `docs/api.md`; regenerate vectors and both wasm binaries. |
| 147 | |
| 148 | **Interfaces (produced):** |
| 149 | - View key `meta.fontFiles`, omitted when absent: `[{family: string, css: string, faces: [{ref: string, format: string, weight: [min, max], italic: bool, pictures: bool}]}]`. |
| 150 | - `set_meta{fontFiles: [...] | null}`; null removes it. Checks: at most 32 families and 16 faces each; `ref` matches `^[0-9a-f]{64}$`; `css` matches `fontPattern`; weights 1–1000 with min ≤ max; `format` one of ttf, otf, woff, woff2. A wrong-typed value is dropped by the view. |
| 151 | - Go: `deck.Meta.FontFiles []deck.FontFamily`, `deck.FontFamily{Family, CSS string; Faces []deck.FontFace}`, `deck.FontFace{Ref, Format string; Weight [2]int; Italic, Pictures bool}`. |
| 152 | |
| 153 | - [ ] **Step 1: Write the failing tests** in Rust and JS: round-trip and clear; each refusal above; a deck without it has no key. Add one `fontFiles` to a new slide's meta in `canvas.json`. |
| 154 | - [ ] **Step 2:** `cd core && cargo test --test ops -- font` — FAIL. |
| 155 | - [ ] **Step 3: Implement,** regenerate vectors (`cd fixtures && node generate.mjs`), rebuild the wasm (`./scripts/build-wasm.sh`, copy) and the PDF wasm if its inputs changed. |
| 156 | - [ ] **Step 4:** `go test ./internal/slides/core ./internal/slides/deck -count=1`, `cd fixtures && node --test` — PASS. |
| 157 | - [ ] **Step 5: Commit, then the gate:** `Document: meta.fontFiles lists a deck's uploaded fonts`. |
| 158 | |
| 159 | --- |
| 160 | |
| 161 | ### Task 5: Using a font in a deck, and serving it |
| 162 | |
| 163 | **Files:** |
| 164 | - Create: `internal/slides/deckfonts.go`, `internal/slidestest/deckfonts_test.go`, `fixtures/deck-fonts.mjs` |
| 165 | - Modify: `models.go` (`DeckFont{ID, DeckID, Ref}` unique on both; migration `0012_*`), `app.go` (routes), `templates/editor.html:957-975`, `fixtures/inspector.mjs`, `fixtures/inspector-ops.mjs`, `editor.go` (pass the person's families to the template) |
| 166 | |
| 167 | **Interfaces (produced):** |
| 168 | - `POST /d/{sqid}/fonts` with `{family}`: the signed-in editor of the deck who owns that family in their library; writes `DeckFont` rows for each face and answers the `deck.FontFamily` JSON. The browser then applies one batch: `set_meta{fontFiles: <existing + this family>, fonts: {heading|body: "'<css>', <current theme stack>"}}`. |
| 169 | - `GET /d/{sqid}/fonts/{ref}`, `/s/{token}/fonts/{ref}`, `/p/{session}/fonts/{ref}`: 404 unless a `DeckFont` row exists for the deck and the ref is in the deck's `meta.fontFiles`; then the bytes with `typeface.Headers`. Parked-slide rules don't apply (fonts are deck-wide). |
| 170 | - Deck settings: each font select gains an optgroup of the person's library families and a link to `/fonts` (new tab). Choosing the theme's font again removes that family from `fontFiles` when neither role uses it. |
| 171 | |
| 172 | - [ ] **Step 1: Write the failing tests:** `TestPickingAFontRecordsItForTheDeck`, `TestAFontIsServedOnlyThroughADeckThatUsesIt` (another deck, another person's deck, a share link of a deck without it: 404; viewer, share link and presenter of the deck with it: 200, the right content type, `nosniff`), `TestOnlyTheLibraryOwnerCanAddTheirFont`, `TestDeletingAFontKeepsDecksThatUseIt` (deleting a family keeps any object a `DeckFont` row refers to, and those decks still serve it; this also changes Task 3's delete), `TestAnOddFamilyNameStillLoads`, and a browser test `TestDeckSettingsOffersYourFonts`. |
| 173 | - [ ] **Step 2:** Run — FAIL. |
| 174 | - [ ] **Step 3: Implement;** rebuild the editor bundle and credits. |
| 175 | - [ ] **Step 4:** `go test ./internal/slidestest -run 'Font|DeckFont' -count=1` and the browser test — PASS. |
| 176 | - [ ] **Step 5: Commit, then the gate:** `Slides: use a font from your library in a deck`. |
| 177 | |
| 178 | --- |
| 179 | |
| 180 | ### Task 6: The gate loads uploaded fonts |
| 181 | |
| 182 | **Files:** `internal/slides/fontgate.go:73-102`, `render/families.go`, `render/render.go` (stacks), `fixtures/font-families.mjs`, `font-session.mjs`, `fonts-gate.mjs`, `fixtures/vectors/font-families.json`, `render/families_test.go`, `fixtures/font-families.test.mjs`, `internal/slidestest/fontgate_test.go`, `fonts_browser_test.go` |
| 183 | |
| 184 | **Interfaces:** |
| 185 | - `gateManifest` gains `Uploaded map[string]gateFamily` keyed `"u:" + css`; `gateFace` gains `Format string` and a weight range (`Weight` becomes the CSS `"min max"` string for variable faces). URLs use the surface's base (`/d/{sqid}`, `/s/{token}`, `/p/{session}`) plus `/fonts/{ref}`. |
| 186 | - `FamiliesUsed` and `familiesOfView` return uploaded keys (`"u:<css>"`) beside the catalog keys when a deck stack names an uploaded family; the shared vectors gain cases for an uploaded family in heading, in body, unused, and an odd name. |
| 187 | - `font-session.mjs` uses each face's own `format(...)`. |
| 188 | |
| 189 | - [ ] **Step 1: Write the failing tests:** the new vector cases (Go and JS), `TestTheManifestListsTheDecksUploadedFaces`, browser `TestAnUploadedFontIsNotPaintedBeforeItLoads` and `TestALateUploadedFontCommitsToItsFallback`. |
| 190 | - [ ] **Step 2:** Run — FAIL. |
| 191 | - [ ] **Step 3: Implement;** rebuild the gate bundle and credits. |
| 192 | - [ ] **Step 4:** Unit and browser runs (`-run 'Font'`) — PASS; existing font tests unchanged. |
| 193 | - [ ] **Step 5: Commit, then the gate:** `Fonts: the gate waits for a deck's uploaded fonts`. |
| 194 | |
| 195 | --- |
| 196 | |
| 197 | ### Task 7: Pictures and PDF |
| 198 | |
| 199 | **Files:** `internal/slides/fonts/fonts.go`, `fonts/truetype.go` (read `name`/`OS/2` only if needed), `layout/layout.go:66-110`, `layout/text.go`, `layout/blocks.go`, `svg/svg.go:264`, `export.go:172-176`, `thumbs.go`, their tests |
| 200 | |
| 201 | **Interfaces:** |
| 202 | - `fonts.Face` gains an optional uploaded face: `fonts.Face{Family, Bold, Italic, Uploaded *fonts.Uploaded}`, `fonts.Uploaded{CSS string; Weight int; Italic bool; Font *fonts.Font}`; `Advance`, `Metrics` and `Stack()` use it when set. |
| 203 | - `layout.Options{Fonts func(ref string) ([]byte, error)}`; `faceFor` picks an uploaded face when the deck stack's first name is an uploaded family and a face with `Pictures` true exists for the weight/italic needed; otherwise the next name in the stack, as today, with no warning for an uploaded family it skips. |
| 204 | - `Export` adds the uploaded faces' bytes to `faces` for the PDF; the PDF engine already matches by family, weight and style from the SVG. |
| 205 | |
| 206 | - [ ] **Step 1: Write the failing tests:** `TestAStaticTrueTypeUploadIsMeasuredAndDrawn` (SVG names the family; a run's width matches the font's advances), `TestTheExportEmbedsAnUploadedFace` (`/FontFile2` and the font's PostScript name in `/BaseFont`), `TestAVariableFontFallsBackInPicturesAndPDF`, `TestARestrictedFontFallsBack`, `TestAWOFF2FallsBack`, and `TestExistingDecksRenderUnchanged` (goldens). |
| 207 | - [ ] **Step 2:** Run — FAIL. |
| 208 | - [ ] **Step 3: Implement.** Load uploaded faces once per render through `Options.Fonts` (blob read by `fontObjectKey`), parse with `fonts.Parse`, and keep them only for that render. |
| 209 | - [ ] **Step 4:** `go test ./internal/slides/... -count=1` — PASS. |
| 210 | - [ ] **Step 5: Commit, then the gate:** `Fonts: pictures and the PDF use uploaded TrueType faces`. |
| 211 | |
| 212 | --- |
| 213 | |
| 214 | ### Task 8: Docs, the estilo pin, and the whole check |
| 215 | |
| 216 | **Files:** `AGENTS.md` (fonts, the library, the gate), `docs/api.md` (`meta.fontFiles`, F3), `go.mod`, `go.sum` |
| 217 | |
| 218 | - [ ] **Step 1:** Update `AGENTS.md` and `docs/api.md`. |
| 219 | - [ ] **Step 2:** With Paul's yes, push estilo `feat/typeface`; in Slides replace the `replace` directive with `go get amadan.net/oficina/estilo.git@<that commit>` (estilo's merge to main follows Paul's separate yes). |
| 220 | - [ ] **Step 3:** `make ci`, the gate, `node --test`, and the full browser suite once; expected failures only the 12 known on main. |
| 221 | - [ ] **Step 4:** Walk it once by hand in a browser against a local app on `127.0.0.1`: upload Liberation Sans as a library font under another name, use it in a deck, open the viewer, a share link and the presenter, export a PDF. Stop the server; remove scratch. |
| 222 | - [ ] **Step 5: Commit:** `Docs: custom fonts`. |
| 223 | |