Skip to content

feat(viewer): draw text the embedded font lacks with host fonts - #442

Merged
ElbertePlinio merged 12 commits into
mainfrom
feat/421-viewer-font-fallback
Oct 29, 2026
Merged

ElbertePlinio merged 12 commits into
mainfrom
feat/421-viewer-font-fallback

Conversation

@ElbertePlinio

@ElbertePlinio ElbertePlinio commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

The native viewer draws its text with one embedded font, Geist Medium, which lacks CJK, Arabic, Hebrew, Greek, Thai, Devanagari and more. A character Geist cannot draw is now drawn with the first host font that has it. Geist stays the primary font. The attention banner (an agent's request_takeover message) and the thumbnail label use the fallback, and the ellipsis from #367 now stands only for characters no host font can draw.

How it works. fallback.rs finds host fonts through fontdb in pure Rust, with no bundled font and no new C runtime library (ldd on the release binary shows only libc, libm and libgcc_s). The font directories fontconfig names are scanned once, on the first character Geist lacks, so Latin text never pays for it. Laying out a text costs at most one scan, including the mirrored characters that right-to-left runs draw. A font the viewer keeps is read into memory when first chosen, so a font file truncated later cannot crash the viewer. The scan itself still maps each file only while it reads it. Text is ordered with unicode-bidi: pure Arabic or Hebrew reads right to left with joined forms, and brackets in right-to-left runs are mirrored. A combining mark takes a loaded font that draws its whole cluster. Default-ignorable characters, such as U+FE0F, never become an ellipsis. CJK faces prefer the region named in LC_ALL, LC_CTYPE or LANG. Bidi control characters are stripped from the attention message, so U+202E cannot reverse what the human reads; the host already refuses format characters in takeover messages, so this is defense in depth.

Closes #421

Requirement Verified by Scenario and edge cases Result Evidence
Choose a fallback: system font or a small bundled one Decision recorded in the issue thread; ldd; live test Host fonts through fontdb, lazy and cached No bundled CJK font, no new C dependency 421/before-live-test.log ("cjk: label ink 0" on the old code)
Draw the banner message and the label with it QA on a private Xvfb, live and unit tests CJK, Arabic, mixed Latin and Arabic, bracketed Arabic and Hebrew, "⚠️ Approve payment", U+202E message Readable text at Geist's size and baseline, brackets face their content, no stray ellipsis, override text in logical order 421/qa-*.png, 421/round2/qa-*.png, 421/round3/after-*.png
Test a CJK and an Arabic message Viewer unit and live tests Host-font tests skip cleanly without a host font Pass; each review fix has a test that fails without it 421/round2/before/, 421/round3/before-test.log, 421/round4/, 421/round5/
No UI stall from a crafted message (review) Scan counter test, live timing 250 distinct ideographs each with U+1AB0; mirrored math symbols in a right-to-left run One host scan per text; the banner appeared after 46 ms instead of 4383 ms 421/round3/qa-before.log, 421/round3/qa-after.log

Evidence paths are under ~/.pickforge/investigations/ on the review host.

Memory: a kept face is read whole, and for a TTC that is the whole collection. On the QA host the CJK face is NotoSansCJK-Medium.ttc, so a CJK label raised the viewer from 19.6 MB to 38.3 MB, and the warning sign, which also comes from that file, costs the same (421/round2/rss.txt). Before, these bytes were shared page cache through a map; now they are private heap, read once per viewer.

CI's rust job has no CJK or Arabic fonts, so those host-font tests skip there; the scan, locale, retain and bidi tests do not need host fonts.

Reviews. Sol round 1: 6/10 (SIGBUS on a truncated mapped font, bracket mirroring, combining marks). Fable round 1: 8/10 (variation selectors, CJK region, bidi controls in the banner). All six were fixed in 972607f, each with a test. Round 2: Sol 9/10 (test nit), Fable 8/10 (one scan per uncovered cluster could stall the UI about 5 s, and the override QA shot was missing), fixed in 866b01f. Fable round 3: 8/10, the one-scan bound relied on callers; 36c69df moves the batch into layout. Sol round 4: 5/10, mirrored characters were not in the batch; fixed in 54ca515. Sol round 5: 10/10, no findings.

Declined: right alignment of right-to-left banners, the "Sans" family name heuristic, CI font packages, and skipping visible format characters such as U+0600 in the batch (that would draw them as empty boxes). Follow-ups from QA: #438 (the banner wrap can split a base character from its mark) and #439 (a mark that no font draws together with its base shows as an empty box).

Checks: full vitest coverage run with the desktop, window, wait, browser and viewer requirements on at 30e4811 (191 files, 3427 passed, 4 skipped) and PICKFORGE_REQUIRE_XVFB=1 cargo test --workspace --locked (342 passed). After the last two viewer commits: cargo fmt, clippy with -D warnings, the workspace cargo tests (342 passed) and the three native viewer vitest files (34 passed). eabe1d2 merges main, which changed only TypeScript. PickCheck 0.4.0 against main is clean, and a local gitleaks scan of the branch is clean.

Dependency audit: fontdb brings in ttf-parser 0.25, which RUSTSEC-2026-0192 marks as unmaintained (no known vulnerability). 0be90fe adds an osv-scanner.toml exception with a reason and an expiry of 2027-01-31, as that file requires. #443 tracks moving host font discovery off ttf-parser before then. A local osv-scanner scan source --recursive ./ is clean.

The thumbnail label and the attention banner now draw characters that
Geist Medium lacks, such as CJK, Arabic and Hebrew, with the first host
font that has them. fontdb reads the fontconfig font directories in pure
Rust on the first such character, so Latin text never scans them.
unicode-bidi orders right-to-left runs, and swash joins Arabic letters.
Only characters no host font draws become an ellipsis.

Closes #421
Keep owned bytes for retained host fonts, so a font file that shrinks
later cannot crash the viewer with SIGBUS. Mirror paired punctuation in
right-to-left runs, pick a font that draws a whole cluster, and treat
default ignorable code points as text that needs no font. Prefer the
CJK face of the locale's region, and strip bidi controls from attention
messages.
find_cluster now searches only loaded fonts, and font_of first loads the
characters the embedded font lacks with one batched find_all. Many
distinct clusters that no font covers no longer scan the host fonts once
each. The marks test also requires nonzero glyph ids.
Layout now loads every character the embedded font lacks with one scan, so the one-scan bound holds for any caller, not only after displayable.
A right-to-left run draws a mirrored character, such as U+220B for U+2208, that the text itself may not contain, so each one cost its own host scan.
…1-31

fontdb uses ttf-parser to read installed host fonts; the advisory names no vulnerability. Replacement is tracked in #443.
@ElbertePlinio
ElbertePlinio merged commit f0bbbb6 into main Oct 29, 2026
10 checks passed
@ElbertePlinio
ElbertePlinio deleted the feat/421-viewer-font-fallback branch October 29, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viewer: draw banner and label text in scripts the embedded font lacks

1 participant