Skip to content

fix: cover non-latin text beyond Google's default latin block - #1

Merged
milkway merged 2 commits into
mainfrom
fix/latin-ext-coverage
Oct 27, 2026
Merged

milkway merged 2 commits into
mainfrom
fix/latin-ext-coverage

Conversation

@milkway

@milkway milkway commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

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-face blocks with unicode-range, Python needs one file that serves embedding, metrics and local subsetting at once. So:

  • CSS-API downloads get a .gsubsets sidecar recording the covered subsets; it doubles as provenance (fonts from register_font() have no sidecar and are never replaced).
  • When text uses characters missing from the cached font's real 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 legacy subset= endpoint (verified live: subset=latin,latin-ext → one TTF with Ł).
  • Upgrades drop stale derived _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

  • 149 tests passing (9 new, all offline with mocked HTTP + synthetic FontBuilder fonts).
  • Live end to end: Polish card → embedded font covers Ł Ś ń ź, 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 --strict clean.

🤖 Generated with Claude Code

milkway and others added 2 commits October 27, 2026 15:36
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>
@milkway

milkway commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Adversarial review of the first commit returned 8 findings (5 confirmed with repro scripts); all addressed in d429e86:

  1. Poisoned subset cache (confirmed): a subset built from the latin-only font after a failed upgrade was cached and served forever. Now subset_font() refuses to cache a subset missing glyphs when an upgrade is still possible, and a poisoned cached subset is detected via its cmap and rebuilt.
  2. Cross-family upgrades (confirmed): the whole-SVG charset upgraded (and downloaded merged TTFs for) families whose own text was pure latin. New svg_text_by_family() attributes text to the nearest declaring element (style wins over attribute on the same node); latin-only families keep their small WOFF2 with zero network, in full-font and subset paths.
  3. Transient failure memoized as no-coverage (confirmed): network blips permanently disabled the upgrade per process. CSS2 failures are no longer memoized; the block table is cached per family on success; install_fonts()/register_font() forget per-family memos.
  4. Repeated CSS2 fetches for uncoverable texts (confirmed): now one request per family (block table cache).
  5. Repeated WOFF2 decompression (confirmed): cmap code points cached per file.
  6. Crash-window unlink order (plausible): font file now unlinked before its sidecar — a dangling sidecar is inert, a sidecar-less partial font would masquerade as a complete local font.
  7. Glob over-match (plausible): _drop_glyph_subsets() matches the exact _sub<10 hex> pattern, so a family named "Foo subheading" can never be deleted by "Foo".
  8. CJK numbered slices (plausible): /* [3] */ blocks are not treated as coverage, since the legacy subset= endpoint cannot serve them.

156 tests (7 more, pinning every fix), mkdocs build --strict clean, live multi-family/subset/pt-BR validation OK.

🤖 Generated with Claude Code

@milkway
milkway merged commit 4b5ca1e into main Oct 27, 2026
3 checks passed
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.

1 participant