Skip to content

fix: embed the Latin block, not Google's first (Cyrillic) @font-face - #28

Merged
milkway merged 2 commits into
mainfrom
fix/27-latin-font-block
Oct 27, 2026
Merged

milkway merged 2 commits into
mainfrom
fix/27-latin-font-block

Conversation

@milkway

@milkway milkway commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #27

Problem

The Google Fonts CSS API splits its response into one @font-face block per script, listing Cyrillic first. download_google_font_woff2() took the first url() it found, so the cached "full" Jost.woff2 was actually the Cyrillic subset (82 glyphs, no a-z, digits or pt-BR accents). save_svg() without subset_fonts embedded a font unable to render Latin text and viewers fell back to system fonts, defeating deterministic rendering. The TTF fallback path had the same defect. Only the CSS fallback path was affected — downloads via gfonts were always correct, and subset_fonts = TRUE was never affected.

Fix

  • New internal google_css_latin_url() parses every @font-face block and picks the one whose unicode-range covers Basic Latin (tests U+0041), with a first-URL fallback for single-block responses (&text= subsets, TTF). Applied to both the WOFF2 and the TTF fallback paths. Backed by unicode_range_covers() (ranges, single code points, U+4?? wildcards).
  • Download validation: WOFF2 files are checked by magic bytes (wOF2) in addition to the size rule; failed or invalid downloads are unlink()ed so they never poison the cache via the early file.exists() return.
  • One-time cache migration (.cache-v2 marker in font_cache_dir()): full fonts cached by earlier versions (potentially the Cyrillic subset) are discarded and re-downloaded on demand; glyph subsets (_sub<hash>) are kept.

Validation

  • Verified against the live API: the old behavior picked the Cyrillic URL (...-Cw.woff2); the fix picks the latin block URL, whose unicode-range covers U+0041, and the downloaded file passes WOFF2 validation.
  • 6 new offline tests (realistic CSS2 multi-block fixture, unicode-range parsing, magic bytes, cache migration) — 208 passing total.
  • R CMD check: 0 errors, 0 warnings, 0 notes.

Version bumped to 0.3.1 (first commit of the new cycle), NEWS.md updated. Candidate for the next CRAN submission window (~2026-11-25).

🤖 Generated with Claude Code

milkway and others added 2 commits October 27, 2026 12:43
…27)

The Google Fonts CSS API splits its response into one @font-face block
per script, listing Cyrillic first. The CSS download fallback took the
first url() it found, so the cached "full" WOFF2 was actually the
Cyrillic subset (82 glyphs, no a-z/digits/pt-BR accents) and save_svg()
without subset_fonts embedded a font unable to render Latin text.

- New google_css_latin_url() parses every @font-face block and picks
  the one whose unicode-range covers Basic Latin (tests U+0041), with
  a first-URL fallback for single-block responses (&text= subsets,
  TTF). Applied to both the WOFF2 and the TTF fallback paths.
- Validate downloaded WOFF2 files by magic bytes and unlink failed or
  invalid downloads so they never poison the cache via the early
  file.exists() return.
- One-time cache migration (.cache-v2 marker in font_cache_dir()):
  full fonts cached by earlier versions are discarded and re-downloaded
  on demand; glyph subsets (_sub<hash>) are kept.
- 6 new offline tests (realistic CSS2 fixture, unicode-range parsing,
  magic bytes, cache migration). R CMD check: 0 errors, 0 warnings,
  0 notes.

Bump version to 0.3.1 (first commit of the new cycle).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Cache migration no longer deletes anything: pre-0.3.1 full fonts are
  moved to a stale-pre-0.3.1/ quarantine folder (register_font() copies
  local, possibly irreplaceable fonts into the same cache with the same
  naming scheme), with a one-time cli message. Extracted to
  migrate_font_cache() and skipped when the marker cannot be written
  (read-only cache dirs no longer rescan on every call).
- TTF fallback downloads are validated by sfnt magic bytes
  (00 01 00 00 / true / ttcf / OTTO), like the WOFF2 path.
- google_css_latin_url() takes multiple URL patterns and tries them PER
  BLOCK, so a response mixing URL styles across blocks cannot hide the
  latin block behind the first-URL fallback.
- download_google_font_woff2() checks the cache for alternative
  extensions (ttf/woff/otf) on entry instead of re-downloading a font
  cached by the TTF fallback or gfonts on every call.
- Migration test runs on an isolated directory instead of mutating the
  shared per-session cache; new tests for is_ttf_file() and the
  mixed-URL-style response. 215 tests, R CMD check 0/0/0.
- NEWS.md documents the quarantine and the latin-ext known limitation
  (full-font embedding covers Basic Latin + Latin-1; use subset_fonts
  for arbitrary text).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@milkway
milkway merged commit 5481644 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 caches the Cyrillic subset of Google's CSS response

1 participant