Merge: add your own fonts in Settings

This commit is contained in:
2026-11-04 18:45:25 +00:00
28 changed files with 2329 additions and 122 deletions
+76
View File
@@ -42,3 +42,79 @@ same compressed amount (not measured).
RobotoMono; they now follow the chosen font too.
- On the web Cupertino fixture, "System default" names an unregistered
`CupertinoSystemText` family, which the browser engine substitutes.
## Fonts the user adds
Settings > Font ends with **Add font…**. It opens the host picker
(`file_picker`; .ttf/.otf filter on the web and Apple hosts, every file on
Android, whose document picker filters by MIME type), then:
1. `CustomFontDraft.inspect` refuses other extensions, files over 10 MB,
more than 16 files at once, two files of the same weight and style, and a
total over 40 MB of added fonts. Each file is read by
`custom_fonts/font_file_info.dart`, a bounded pure-Dart sfnt reader: it
rejects TTC collections, WOFF, damaged table directories and fonts
without `cmap`/`head`/outlines, reads the family from the `name` table
(ID 16, else ID 1; Windows English first) and the weight from
`OS/2.usWeightClass` (400 when absent) plus the italic bit.
2. A name dialog is prefilled from the font's family (else the file name)
and lists each file's style; the user can rename it there or later.
3. `AppFontController.addCustom` registers the bytes with a `FontLoader`
under the new family first; only if that succeeds are the bytes and the
index stored, and the font is chosen. A failure stores nothing. (The
engine ignores undecodable bytes rather than throwing, so the structural
check in step 1 is the real gate; the loader catches the rest.)
Rows for added fonts sit below the bundled ones, named in their own face,
with their styles ("Regular, Bold") and an options button (Rename, Remove)
beside the row, outside the tile's merged semantics. Remove asks first and
deletes the files; if the font was chosen, the device returns to System
default. The engine keeps a removed family registered until the next start,
but nothing uses it.
The choice key is unchanged (`pipelines.next.font.v1` now holds either a
bundled id or `custom-<id>`). A stored added font loads at startup within
the same bounded wait; missing bytes keep the system face and the picker
says "Could not load this font."
### Storage
- Index: `shared_preferences` key `pipelines.next.custom-fonts.v1`, JSON of
names, weights and byte keys (a few hundred bytes per font).
- Native bytes: one file per face under the app support directory
(`custom-fonts/<key>`), written to a `.partial` file and renamed. Support,
not Documents: it is private (not in the iOS Files app) and leaves with
the app.
- Web bytes: IndexedDB database `pipelines-custom-fonts` via `package:web`.
`localStorage` (which `shared_preferences` uses on the web) was rejected:
browsers cap it at about 5 MB of UTF-16 text per origin, shared with every
other preference, and base64 inflates a four-weight family to 1–2 MB, so a
15 MB cap could never be honoured and an overflow would break other
settings. Cache Storage needs a secure context, which a plain `http://`
LAN address is not. IndexedDB stores binary values directly with a
disk-sized quota; each write asks `navigator.storage.persist()` so the
browser keeps the fonts under disk pressure (best effort).
- Bytes are written before the index names them, and the index forgets a
font before its bytes are deleted, so the index never names a partial
font.
### Evidence
Tests: `test/ui/theme/custom_fonts/` (parser on the bundled fonts and
damaged/hostile inputs; draft limits; library round trip with an in-memory
byte store; native directory store on a temp dir; controller add, reject,
rename, remove, restore, missing bytes, real `FontLoader`) and
`test/features/settings/settings_custom_font_test.dart` (Material and
Cupertino add/rename/remove, invalid file, cancel). The IndexedDB store has
no VM test (it needs a browser); the browser run below covers it.
Browser (`tool/capture/`, new `files=` step answering the file chooser):
the fixture app at 880x1400, Material and Cupertino, adding the bundled
Atkinson Hyperlegible Next Regular and Bold as a user font named
"My Atkinson". Shots: the name dialog prefilled "Atkinson Hyperlegible
Next" with both styles; the added row chosen and the app redrawn in it; the
same after a page reload (restored from IndexedDB); run 7 in the added
face; the options menu and action sheet; rename to "Body face"; the remove
confirmation; the fallback to System default; and a fake `.ttf` refused
with "“fake.ttf” cannot be used: The file is too short." Android and iOS
were not run on a device.