Repository navigation
fix: embed extra @font-face blocks (latin-ext etc.) for non-latin text (#29) - #30
Conversation
#29) The "full" font downloaded from the Google Fonts CSS API is only the *latin* block (Basic Latin + Latin-1 Supplement), so glyphs from other blocks — Polish/Czech (latin-ext), Vietnamese, Cyrillic... — rendered as missing-glyph boxes when save_svg() was used without subset_fonts. Now, when the SVG text uses code points the primary block does not cover, the blocks that cover them are downloaded from the same CSS response, cached as <key>_blk-<subset>.<ext> with a .range sidecar, and embedded as additional @font-face rules with their unicode-range preserved (the primary face also gets its explicit range, so rule order is irrelevant). The .range sidecar doubles as provenance: only CSS-API downloads have one, so complete fonts from gfonts and local fonts added with register_font() are embedded alone, as before — no Google block can override a user's registered font. Latin-only SVGs (all of pt-BR) produce byte-identical output and never fetch anything extra. Also refactors the CSS fetching into google_css_urls()/fetch_google_css() and the block parsing into google_css_font_blocks(), which google_css_latin_url() (#27) now wraps. Closes #29. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- register_font() and the gfonts copy path now unlink a stale .range sidecar left by a previous CSS-API download: without this, a complete local/gfonts font was treated as a latin-only block and Google's extra blocks could override the user's registered font — the exact invariant the fix promises to keep. - Extra blocks are now gated on the FILE actually embedded (glyph-subset "_sub<hash>" vs. full font), not on the requested mode: when a subset_fonts download fails and the full font is the fallback, it gets its extra blocks too. - Code points are attributed per font family (new svg_text_by_family(), nearest-ancestor font-family resolution; unattributable text counts for every family): a family whose text is ASCII-only no longer downloads or embeds blocks another family needed. - A cached extra block is only reused after magic-byte validation, and its existing sidecar range (describing the cached bytes) is preferred over the range of the freshly fetched CSS. - The 6x-duplicated family-to-cache-key sanitization is extracted into font_cache_key(); dead capture group removed from the block parser. 254 tests, R CMD check --as-cran 0/0/0. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Second commit addresses the /code-review findings:
Not fixed on purpose: full fonts cached by 0.3.1-dev (post-#27, pre-#29) have no sidecar and stay inert for extra blocks. 0.3.1 was never released — every CRAN upgrader comes from ≤0.3.0, whose 254 tests, 🤖 Generated with Claude Code |
- svg_text_by_family(): the NEAREST node declaring font-family wins,
whether via presentation attribute or inline style (style beats the
attribute on the same node, as in CSS). Before, a distant ancestor
attribute shadowed a closer inline style, charging text to the wrong
family.
- .range sidecars are now per FILE (path + ".range"), not shared across
extensions of the same cache key: register_font('Jost.ttf') no longer
deletes the provenance of a Google Jost.woff2 that stays in the cache
and still wins embedding.
- A reused cached extra block whose sidecar range covers none of the
requested code points while the freshly fetched CSS range does is
re-downloaded (Google widened the block upstream) instead of shadowing
the new bytes forever.
- list_fonts() also hides _sub<hash> glyph-subset cache artifacts.
- unicode_range_covers_v() parses the range tokens once into lo/hi
vectors; the scalar unicode_range_covers() is now a wrapper.
- global_cp deduplicated through non_ascii_codepoints().
260 tests, R CMD check --as-cran 0/0/0.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Final review pass (
Dismissed with rationale: (1) warm-0.3.1-dev caches staying inert — repeat of the earlier finding, 0.3.1 was never released so only dev machines are affected and provenance is indistinguishable from 260 tests, 🤖 Generated with Claude Code |
Closes #29 (follow-up to #27 / PR #28).
Problem
Since 0.3.1 the CSS-API path correctly picks Google's latin
@font-faceblock, but that block only spans Basic Latin + Latin-1 Supplement. Text in Polish (Łódź), Czech, Vietnamese (Việt), Cyrillic etc. rendered as missing-glyph boxes withsave_svg()withoutsubset_fonts.Fix (the "multiple blocks with unicode-range" direction from the issue)
<key>_blk-<subset>.<ext>+ a.rangesidecar, and embedded as additional@font-facerules with theirunicode-rangepreserved. With extras present, the primary face also gets its explicit range, so each face has explicit coverage and rule order doesn't matter..rangesidecar doubles as provenance: only CSS-API downloads write one. Complete fonts fromgfontsand local fonts added withregister_font()have no sidecar and are embedded alone, exactly as before — a Google block can never override a user's registered font (the gotcha that shaped the Full-font embedding caches the Cyrillic subset of Google's CSS response #27 quarantine).Việtin Roboto embeds the vietnamese block,Москваin Jost embeds cyrillic. A glyph the font genuinely lacks (e.g. Vietnamese in Jost, which has no vietnamese subset) keeps today's fallback behavior.google_css_urls()/fetch_google_css(); block parsing togoogle_css_font_blocks(), whichgoogle_css_latin_url()(Full-font embedding caches the Cyrillic subset of Google's CSS response #27) now wraps. No exported API changes.Validation
R CMD check --as-cran: 0 errors / 0 warnings / 0 notes.🤖 Generated with Claude Code