Repository navigation
fix: cover non-latin text beyond Google's default latin block - #1
Merged
Merged
Conversation
Port of cardargus (R) issue #29. The "full" font downloaded from the Google CSS API -- both the WOFF2 block picked from the CSS2 response and the TTF served to non-browser clients -- only covers the *latin* subset, so Polish/Vietnamese/Cyrillic text was broken in three places at once: the embedded @font-face lacked the glyphs, the LOCAL fontTools subset was built from the latin-only file (silently dropping the glyphs), and text metrics fell back to the per-character heuristic (wrong layout). Unlike the R fix (multiple @font-face blocks with unicode-range), the Python port upgrades the cached font IN PLACE to a single merged TTF from the legacy `subset=` endpoint, because metrics and local subsetting need one file: a `.gsubsets` sidecar records which Google subsets the cached file covers and doubles as provenance (only CSS-API downloads have one); when text uses characters missing from the real cmap, the covering subsets are looked up in the CSS2 response and the merged TTF replaces the partial block. Fonts added with register_font() have no sidecar and are never replaced. Latin-only cards keep the small WOFF2 and byte-identical output. Upgrades drop stale derived _sub subsets; failed attempts are memoized per process. The difference from R is documented in README "known differences". 149 tests (9 new, offline); validated live end to end (embed, metrics and local subset all cover Polish text; pt-BR stays on the woff2). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- subset_font() no longer caches a subset missing glyphs after a failed
coverage upgrade (fall back to the full font and retry later), and a
poisoned cached subset -- detected via its cmap -- is rebuilt once the
upgrade succeeds; subsets of complete/local fonts that genuinely lack
the glyphs stay cached and served.
- Coverage is attributed per font family (new svg_text_by_family() in
_svgparse, nearest ancestor-or-self attribute or inline style, style
winning on the same node; unresolvable text counts for every family):
a family whose own text is latin keeps its small WOFF2 and triggers no
network, in full-font and subset paths alike.
- Transient network failures are no longer memoized as "family lacks the
glyphs": the CSS2 block table is cached per family only on success
(also collapsing repeated lookups for distinct uncoverable texts into
one request), the upgrade memo is keyed on the subsets actually
requested and recorded only when an attempt was conclusive, and
install_fonts()/register_font() forget per-family memos.
- Numbered CJK slices ("[3]") are not treated as coverage: the legacy
subset= endpoint cannot serve them.
- cmap lookups are cached per file (no repeated WOFF2 decompression);
the old font file is unlinked before its sidecar (a dangling sidecar
is inert, a sidecar-less partial font would masquerade as complete);
_drop_glyph_subsets() matches the exact _sub<10 hex> pattern so a
family named "Foo subheading" can never be deleted by "Foo".
156 tests; validated live (multi-family, subsets and pt-BR paths).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Author
|
Adversarial review of the first commit returned 8 findings (5 confirmed with repro scripts); all addressed in
156 tests (7 more, pinning every fix), 🤖 Generated with Claude Code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Port of StrategicProjects/cardargus#29 (fixed there by StrategicProjects/cardargus#30).
Problem
The "full" font downloaded from the Google CSS API only covers the latin subset — verified empirically for both download paths (the WOFF2 block picked from the CSS2 response and the TTF served to non-browser clients, which the old docstring wrongly called "full": 221 glyphs, no
Ł). Because this port subsets locally and measures text from the cached file, non-latin text (PolishŁódź, Vietnamese, Cyrillic) was broken in three places: the embedded@font-face, the local fontTools subset (silently missing the glyphs) and the text metrics (heuristic fallback → wrong layout).Fix (adapted to this port's architecture)
Where R embeds multiple
@font-faceblocks withunicode-range, Python needs one file that serves embedding, metrics and local subsetting at once. So:.gsubsetssidecar recording the covered subsets; it doubles as provenance (fonts fromregister_font()have no sidecar and are never replaced).cmap, the covering Google subsets are looked up in the CSS2 response (/* name */blocks +unicode-range) and the cache is upgraded in place to a single merged TTF from the legacysubset=endpoint (verified live:subset=latin,latin-ext→ one TTF withŁ)._sub<hash>subsets; failed attempts are memoized per process; latin-only cards keep the small WOFF2, byte-identical.str_metrics()triggers the upgrade when it meets unmapped characters, so layout uses real glyph metrics.register_font()also clears sidecars and stale subsets for the family.The difference from R is documented in README "known differences" and CLAUDE.md.
Validation
Ł Ś ń ź,str_metrics("Ł")exact (4.5, not the 5.5 heuristic), local subset includesŁ(24 glyphs); pt-BR card stays on the latin WOFF2 with no extra fetch.mkdocs build --strictclean.🤖 Generated with Claude Code