Skip to content

fix: embed extra @font-face blocks (latin-ext etc.) for non-latin text (#29) - #30

Merged
milkway merged 3 commits into
mainfrom
fix/29-latin-ext-blocks
Oct 27, 2026
Merged

milkway merged 3 commits into
mainfrom
fix/29-latin-ext-blocks

Conversation

@milkway

@milkway milkway commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Closes #29 (follow-up to #27 / PR #28).

Problem

Since 0.3.1 the CSS-API path correctly picks Google's latin @font-face block, 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 with save_svg() without subset_fonts.

Fix (the "multiple blocks with unicode-range" direction from the issue)

  • When the SVG text uses code points the primary block doesn't cover, the covering blocks are taken from the same CSS response, downloaded with magic-byte validation, cached as <key>_blk-<subset>.<ext> + a .range sidecar, and embedded as additional @font-face rules with their unicode-range preserved. With extras present, the primary face also gets its explicit range, so each face has explicit coverage and rule order doesn't matter.
  • The .range sidecar doubles as provenance: only CSS-API downloads write one. Complete fonts from gfonts and local fonts added with register_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).
  • Block selection is by the code points actually rendered, so it generalizes beyond latin-ext: Việt in 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.
  • Latin-only SVGs (all of pt-BR) produce byte-identical output and never trigger an extra fetch; cached blocks are served offline on re-renders.
  • Refactors: CSS fetching extracted to google_css_urls() / fetch_google_css(); block parsing to google_css_font_blocks(), which google_css_latin_url() (Full-font embedding caches the Cyrillic subset of Google's CSS response #27) now wraps. No exported API changes.

Validation

  • 247 tests passing (32 new, all offline); R CMD check --as-cran: 0 errors / 0 warnings / 0 notes.
  • End-to-end with network: Polish card (Jost + latin-ext) rendered via Chrome headless with all diacritics correct; Vietnamese (Roboto) and Cyrillic (Jost) blocks verified; second render is cache-only and byte-identical.
  • NEWS.md: the 0.3.1 "known limitation" entry is now the fix entry.

🤖 Generated with Claude Code

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

milkway commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Second commit addresses the /code-review findings:

  • Stale .range sidecar (the invariant-breaker): register_font() and the gfonts copy path now unlink() the sidecar when writing a complete font, so no Google block can ever override a locally registered font. Covered by a test.
  • Subset-fallback gap: extra blocks are gated on the file actually embedded (_sub<hash> subset vs. full font), not on the requested mode — a failed subset download falling back to the full latin block now gets its extra blocks.
  • Per-family code points: new svg_text_by_family() resolves each text node's family via the nearest font-family attribute/style (unattributable text counts for all families); ASCII-only families no longer download or embed blocks they don't need.
  • Cached block reuse: magic-byte validation before reuse; the existing sidecar range (describing the cached bytes) wins over the freshly fetched CSS range.
  • Cleanups: font_cache_key() extracted (6 duplicated sanitizations), dead capture group removed.

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 .cache-v2 migration already quarantines and re-downloads fonts with sidecars. Only dev machines are affected, and a sidecar-less file is indistinguishable from a register_font() font, so "treat as complete" is the safe default.

254 tests, --as-cran 0/0/0; per-family behavior smoke-tested end-to-end (Montserrat with "Łódź" gets latin-ext, Jost with ASCII stays single-face).

🤖 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>
@milkway

milkway commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Final review pass (/code-review 30) returned 8 findings; 6 applied in 17dca43:

  • style vs. attribute precedence in svg_text_by_family(): the nearest declaring node wins, and inline style beats the presentation attribute on the same node (before, a distant ancestor attribute shadowed a closer inline style — wrong-family attribution, tofu).
  • Per-file sidecars (<path>.range instead of <key>.range): register_font('Jost.ttf') no longer erases the provenance of a Google Jost.woff2 that stays in the cache and still wins embedding.
  • Stale cached block: if its sidecar range covers none of the requested code points but the fresh CSS range does, the block is re-downloaded (Google widened it upstream) instead of shadowing the new bytes forever.
  • list_fonts() also hides _sub<hash> artifacts; unicode_range_covers_v() parses tokens once; global_cp deduplicated.

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 register_font() files; (2) double XML parse — negligible next to download/base64, and the second parse only runs when non-ASCII text is present.

260 tests, --as-cran 0/0/0. Merging.

🤖 Generated with Claude Code

@milkway
milkway merged commit 02ed6cf into main Oct 27, 2026
1 check 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.

Full-font embedding covers only Google's latin block (latin-ext glyphs missing)

1 participant