Skip to content

fix(core): pages with many CJK characters fetch fonts in several requests - #7823

Merged
miguel-heygen merged 4 commits into
mainfrom
fix/core-cjk-font-text-chunks
Nov 15, 2026
Merged

miguel-heygen merged 4 commits into
mainfrom
fix/core-cjk-font-text-chunks

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

What

Pages whose text no longer fits one Google Fonts text= request now fetch their fonts in several bounded text= requests. They no longer fetch the whole family.

Why

extractGoogleFontsText dropped the page text once it encoded past 1,700 characters, and the compile then asked Google for the entire family. Each CJK character encodes to 9 bytes, so any page with roughly 190 or more distinct CJK characters hit this. A whole CJK family is hundreds of woff2 files, and they do not download inside the compile's 20 s font budget, so the render failed with a font timeout or "Font fetch budget exhausted".

To reproduce: a page using a Google family such as Noto Serif SC, with 600 distinct ideographs. Before this change, its one CSS request carries no text=.

Related work

None.

How

  • A page whose characters fit one text= request makes exactly the request it does on main.
  • Past that limit, pageTextRuns builds several runs, each within the existing 1,700 encoded characters:
    • Only Han, Hiragana, Katakana and precomposed Hangul syllables that stand alone are spread across runs. Each is one glyph that does not shape against its neighbours.
    • Everything else stays in the first run: Latin, Arabic and other joining scripts, marks and variation selectors.
    • So does every grapheme cluster (Intl.Segmenter, as in text/pretext.ts) that has more than one code point or composes under NFC: 가+ᆨ, a+◌́, カ+゙, ガ, 葛+VS17. Its NFC and NFD forms go with it, each case-closed with the page's locales like every other character, so the face's range covers whichever form the browser draws (각, á, ガ, and Á under uppercase).
    • If those characters do not fit one request, or the page uses text-transform: full-width or full-size-kana (which draw characters the source does not contain), the family is fetched whole, as on main.
  • fetchGoogleFont makes one request per run, for both the default and the authored-link URLs. With more than one run, each returned face gets a unicode-range of exactly its run's codepoints, so same-weight faces do not shadow each other.
  • An authored link that already has its own text= is fetched once, as before.

Test plan

  • Unit tests added/updated
    • New deterministicFonts-textChunks.test.ts, 600 distinct CJK characters:
      • every CSS request carries a text= within 1,700 encoded characters, and together they request every character
      • each face claims its own request's codepoints, so every character is owned by exactly one face
    • Shaping, on the same file. Each case sits beside the 600 CJK characters:
      • Arabic lam … alef: owned by one face.
      • Hangul 가+ᆨ, Latin a+◌́, kana カ+゙, half-width ガ, ideograph+VS17: every character of the cluster, and its composed form, are owned by one face.
      • 400 Cyrillic characters, which must stay together and overflow one request, fetch the whole family as on main.
      • text-transform: full-width with 600 CJK fetches the whole family as on main.
      • a+◌́ under uppercase keeps Á in its face; A+◌́ under lowercase keeps á.
    • The first two tests fail on main. Each rule turns its own tests red when mutated: dropping the range tag, splitting every character, dropping cluster pinning (5 red), dropping the NFC/NFD closure (5 red), ignoring text-transform (1 red), and skipping case closure of the composed forms (2 red).
    • googleSubsets (authored link) and aliasSupplement asserted the old drop-to-whole-family behaviour for CJK and now assert the split. The aliasSupplement test keeps its subject, collapsing interleaved shared sources, with a short page.
    • src/fonts: 26 files, 304 tests pass; the changed files passed three runs in a row.
    • tsc --noEmit, oxfmt, lint, fallow audit (0 dead code, complexity or duplication) and the comment ratchet all pass.
  • Manual testing performed
    • Live Google Fonts, a 600-ideograph Noto Serif SC page, injectDeterministicFontFaces with fail-closed font fetch:

      CSS requests requests without text= woff2 files time
      released 0.9.52 1 1 808 12.0 s
      this branch 4 0 32 0.8 s

      The 12 s was on a fast link; on a slower one, 808 files miss the 20 s budget.

    • Real-font shaping in Chromium, fonts fetched live from Google. Each case sits beside 600 CJK characters at 100 px. "One face" is the same case alone on a page, one text= request, drawn from Google's own bytes; widths are also checked against the complete font file.

      case complete font width 0.9.52 (whole family) previous head this head
      Hangul 가+ᆨ (Noto Sans KR) 92 192, differs 184, differs 92, same pixels
      Latin a+◌́ (Noto Sans KR) 56 same pixels differs same pixels
      kana カ+゙ (Noto Sans JP) 100 same pixels differs same pixels
      half-width ガ (Noto Sans JP) 100 differs differs same pixels
      葛+VS17 (Noto Sans JP) 100 same pixels same pixels same pixels
      a+◌́ under uppercase (Noto Sans KR) same pixels differs at 78794d37 same pixels
      A+◌́ under lowercase (Noto Sans KR) same pixels differs at 78794d37 same pixels

      The whole-family fetch on main already splits 가+ᆨ and ガ, because Google's own unicode-range slices put the two halves in different faces.

  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 81 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

Unstable (1)

  • seqrotatenudge-none-pct-r0-nested-z100: tracking 0.02, pressJump 0, drop 0, reload 3.03, render 0.06, renderKey -, undo false, teleport true / tracking 0.02, pressJump 0, drop 0, reload 0, render 0.06, renderKey -, undo true, teleport true / tracking 0.02, pressJump 0, drop 0, reload 0, render 0.06, renderKey -, undo true, teleport true

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 78be1e4645ece5958cc9c74bcdef316a6ee53cb2.

The tested oversized CJK case stays in bounded text= requests, and the existing fetch implementation, cache and retry deadline are reused. One rendering regression blocks this head.

Important — preserve shaping across subsets (packages/core/src/fonts/deterministicFonts.ts:1657, 1703–1705). The chunks partition the page's unique characters, then assign disjoint unicode-ranges. Characters that need to shape together can therefore select different font faces. This changes glyphs and layout, even with every request succeeding.

I reproduced this through injectDeterministicFontFaces with a real DejaVu Sans woff2 and Chromium: an early lam, 600 distinct CJK characters, then visible Arabic لا. Lam lands in chunk 1 and alef in chunk 4. The same 100px text measures 57.03125px on the base, but 60.9375px at this head, matching a deliberately split-face control. All relevant font faces loaded. Font responses were mocked with real font bytes; this is a browser/shaping check, not a live Google Fonts test.

Please keep shaping-connected characters in the same font subset, including joins across grapheme boundaries; merely splitting on graphemes will not preserve Arabic joining. A full-font fallback for affected complex scripts is another option if it preserves the intended request/deadline limits. Add a real-font regression that preserves the joined result while retaining the bounded CJK requests.

Validation: full bun run build passed; the 26 committed font suites passed 294 tests. Both new textChunks tests fail with the production change reverted and pass after restoration. An additional 503 probe confirmed that a later transient failure rejects a fail-closed compile. A later permanent 400 can retain earlier chunks, consistent with the existing skip-missing-face policy; that is separate from the shaping blocker.

Scope notes: distributed chunks consume the same compiled, hashed plan artifact rather than independently choosing fonts. The default/authored/fallback requests all still use the existing font fetch seam; this PR adds no transport or proxy bypass, but I did not validate the deployed capture proxy/CIDR enforcement. Each generated text payload is capped at 1,700 encoded characters; the 600-CJK fixture makes four requests, but total count remains input-dependent, not capped by a small constant. The existing fail-closed wall-clock budget remains. This changes output for consumers of the shared core font resolver.

Nit: Consider an explicit chunk-count ceiling with a typed failure, so very large character inventories has an explicit request-count bound in addition to the time budget.

Need: yes · Size: ok · Reuse: existing font fetch/cache/retry helpers · Simpler: preserve shaping groups or use a complex-script fallback · Structure: fine (checked authoredGoogleFonts and existing family resolution; no extraction needed)
Risk: medium — shared font resolution changes rendered glyphs and layout.

Verdict: REQUEST CHANGES
Reasoning: The oversized CJK request test passes, but arbitrary character partitions introduce a reproduced Arabic shaping regression.

— Rames / cutroom-review

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Fixed at 29942819.

Shaping. Only Han, Hiragana, Katakana and precomposed Hangul syllables are now spread across requests. Each of those is a single glyph that does not shape against its neighbours. Everything else stays in the first request: Latin, Arabic and other joining scripts, marks and variation selectors. So does any ideograph or syllable followed by a mark or selector. Your lam-alef case is now one face. If the characters that must stay together overflow one request, the family is fetched whole, as on main.

Regression tests in deterministicFonts-textChunks.test.ts:

  • lam + 600 CJK + alef: lam, alef and Latin are owned by one face
  • ideograph + VS17 share one face
  • 400 Cyrillic still asks for the whole family

Each rule has a mutant that turns its own test red: splitting every character turns the lam-alef and Cyrillic tests red, and dropping the attached-mark rule turns the selector test red. The test checks face ownership, not a browser measurement. Same face means the same font file, and that is the invariant shaping depends on. I did not add a Chromium real-font test to core.

Chunk-count ceiling (nit): not added. Only standalone characters are spread, so the count is about distinct ideographs ÷ 188. The fail-closed wall-clock budget already bounds it. A second limit would be a second owner for the same decision.

Live Google Fonts at this head, 600-ideograph Noto Serif SC page: 4 requests, all with text=, 32 woff2, 0.8 s (0.9.52: 1 request, 808 woff2, 12.0 s).

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 2994281965334cf89336314158bc8cb9c86b7d66; re-reviewed the delta from 78be1e46 against my change request 5910019218.

Arabic is fixed: the real DejaVu Sans woff2/Chromium probe now renders lam-alef at 57.03125px, with pixels identical to the full-face control. Thai and Devanagari mark probes also match. The shaping contract still fails for other compositions.

Important — keep composed sequences and their normalized forms together (packages/core/src/fonts/deterministicFonts.ts:1937–1941, 1956–1973, 1683).

  • Hangul 각 (U+AC00 + U+11A8) composes to 각. The syllable is classified as loose, while trailing Jamo is not a mark and stays in the first run. With 600 earlier CJK characters, they select different faces. With real Noto Sans KR, the same 100px text measures 91.999908px on the base/full face, 183.999817px at this head.
  • Latin a + U+0301 is assigned to one face, but its generated range omits composed U+00E1. In the real DejaVu probe, the full face/base draws á; this head draws only the acute accent. The advance width happens to remain equal, so a width-only test misses it. Full-width and half-width kana/dakuten probes also differ in pixels from their full-face controls.

These are actual injectDeterministicFontFaces outputs in Chromium 1208 with loaded real woff2 bytes and mocked network responses. The same fixtures match their full-face controls with the original base production file at ded5e3eec25cd2da405fb30c2751d6406726607d. This continues the existing shaping finding rather than a request to change the intended CJK behavior.

Please include Hangul LV+T/grapheme continuations in the characters that must stay together, and preserve normalization closure in both requested glyphs and emitted unicode-ranges. A conservative whole-family fallback for sequences that cannot safely be partitioned is also acceptable. Add real-font regressions for Hangul composition and the Latin/kana mark cases, checking pixels as well as width.

Validation: gated bun run build passed; bunx vitest run src/fonts --maxWorkers=2 passed 297 tests. Reverting to the previous reviewed production file makes the committed keeps characters that shape together in one face: Arabic lam-alef beside 600 CJK and asks for the whole family, as before, when characters that must share a face overflow one request tests fail (2 failed / 3 passed); restoring this head gives 5 passed. The real-font/browser probes above run separately from those committed tests; no live Google Fonts or cloud render was run.

Nit, still open — request count: there is still no explicit ceiling. The count probe produced 4 / 27 / 54 CSS requests for 600 / 5,000 / 10,000 distinct CJK characters, all payloads at most 1,699 encoded characters. Two fresh lookups produced identical request lists and HTML. The existing shared deadline remains; it is not a request-count cap. This is separate from the shaping blocker. Fetch/cache/retry transport remains unchanged.

Need: yes · Size: ok · Reuse: existing font fetch/cache/retry helpers · Simpler: conservatively retain unsafe shaping groups · Structure: fine (checked authoredGoogleFonts and every PageText caller; no extraction needed)
Risk: medium — shared font resolution can omit or split rendered glyphs.

Verdict: REQUEST CHANGES
Reasoning: Arabic is repaired, but real-font Hangul and combining-mark probes still show rendering regressions against the base.

— Rames / cutroom-review

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Fixed at 78794d37.

Grapheme clusters and normalization closure. Past the limit, every grapheme cluster (Intl.Segmenter, as text/pretext.ts uses) that has more than one code point, or composes under NFC, stays in the first request. Its NFC and NFD forms go with it, so the face's unicode-range covers 각, á and ガ. That covers 가+ᆨ, a+◌́, カ+゙, ガ and 葛+VS17.

text-transform: full-width or full-size-kana draws characters the source does not contain, so a page using one is never split. It fetches the whole family, as on main. A page that fits one request now makes exactly main's request. Before, the run reordering changed the URL.

Real-font check, pixels and width. Chromium, fonts live from Google, each case beside 600 CJK at 100px. Control: the same case alone on a page, one text= face from Google's own bytes; widths also against the complete font file.

case complete font 0.9.52 whole family 29942819 78794d37
가+ᆨ (Noto Sans KR) 92 192, differs 184, differs 92, same pixels
a+◌́ (Noto Sans KR) 56 same differs same pixels
カ+゙ (Noto Sans JP) 100 same differs same pixels
ガ (Noto Sans JP) 100 differs differs same pixels
葛+VS17 (Noto Sans JP) 100 same same same pixels

Main's whole-family fetch already splits 가+ᆨ and ガ, because Google's own range slices separate them. This head draws them as the complete font does.

Committed tests. In core, these are face-ownership tests: each cluster character and its composed form are owned by one face, and a text-transform page fetches the whole family. Each rule turns its own tests red when mutated: cluster pinning off is 5 red, NFC/NFD closure off is 3 red, transform guard off is 1 red. I kept the Chromium pixel probe out of core's unit suite, because core has no browser or font fixtures today. If you want it committed, say where it should live and I'll add it.

Request count (nit). Still not capped. A cap would be a second limit beside the fail-closed deadline, and the deadline already decides when a font fetch has taken too long. A page with 5,000 or more distinct ideographs runs into that deadline and fails typed, rather than hanging. If you'd rather have it fail at a fixed count, I'll add the cap with a FontFetchError.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 78794d377d7302f9e1b6e902e26106c392d22f82; this is one commit ahead of 29942819, with no rebase.

The earlier exact cases are repaired. In Chromium 1208 with real font bytes, lam-alef remains 57.03125px and Hangul LV+T is now 91.999908px rather than doubled. Plain Latin+acute, full/half-width kana+dakuten, Thai and Devanagari also match the full-font controls in pixels.

Important — case-close the newly normalized characters too (packages/core/src/fonts/deterministicFonts.ts:1952, 1987, 2113). addCaseClosure runs on the source codepoints, then composingCharacters adds NFC/NFD forms directly with pinned.add. For source a + U+0301 with text-transform: uppercase, the first face claims á but not Á; uppercase source with lowercase has the inverse omission. This is a remaining instance of the composing-glyph coverage issue.

I checked both DOM screenshots and canvas output. Complete DejaVu Sans woff2 responses lose the base letter. More importantly, using live Google Noto Sans KR CSS and font responses, the uppercase case keeps the letter but renders the acute separately to its right instead of above it. Equal advance widths hide the error. The same-family control explicitly requesting the composed forms draws the accent correctly. Live Noto Sans responses were less severe, so I am not treating every pixel difference as a missing-glyph blocker.

Minimal live fixture: 600 distinct Han characters in an HTML comment, followed by <h1 style="font-family: 'Noto Sans KR'; font-size:100px; text-transform:uppercase">a&#x301;</h1>. Use A&#x301; with lowercase for the reverse case.

Please pass the already-collected locales through to composingCharacters and reuse addCaseClosure for the original/NFC/NFD characters, keeping any case-generated normalization forms in the same face. Add uppercase and lowercase cluster assertions for Á/á. A temporary local implementation of that change makes both new assertions pass and restores the accent above the letter in the live KR probe; it also restores exact DOM/canvas parity in the DejaVu probe. The local change was reverted after verification.

Validation, all gated:

  • Full build and the 302 committed font tests pass at this head.
  • Restoring 29942819 makes 5 of the 10 committed chunk tests fail; restoring 78be1e46 makes the lam-alef test fail. Restoring this head gives 10 passed.
  • The two added case-transform assertions fail here (2 failed / 10 passed) and pass with the local helper repair (12 passed).
  • Real-font shaping, live subset fetches and DOM screenshots are separate probes; no cloud render was run.

The earlier nonblocking request-count concern is unchanged; it is not the reason for this verdict.

Need: yes · Size: ok · Reuse: existing locale-aware addCaseClosure · Simpler: apply the existing closure to normalized forms · Structure: fine (checked PageText callers and existing character closure; no new abstraction needed)
Risk: medium — composed glyphs can still render incorrectly after CSS case transformation.

Verdict: REQUEST CHANGES
Reasoning: The original lam-alef and Hangul failures are fixed, but case transformation still breaks normalization coverage; the proposed small helper reuse was verified against the failing cases.

— Rames / cutroom-review

@miguel-heygen

miguel-heygen commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Fixed at 20121172, the way you suggested.

composingCharacters now takes the locales extractGoogleFontsText already collects. It runs each original, NFC and NFD character through addCaseClosure, so case-generated forms land in the same first-run face.

Two new assertions:

  • a+◌́ under uppercase keeps Á in its face
  • A+◌́ under lowercase keeps á in its face

Both fail at 78794d37 (2 red when the closure is mutated out) and pass here. The fonts suite is 304/304, and every earlier mutant is still red.

Live Google Noto Sans KR in Chromium, beside 600 CJK at 100px, compared against the same case alone in one text= face:

case 78794d37 20121172
a+◌́ uppercase pixels differ same pixels, 61px
A+◌́ lowercase pixels differ same pixels, 56px

The five earlier cases in my probe (가+ᆨ, a+◌́, カ+゙, ガ, 葛+VS17) still match at this head; I did not rerun your lam-alef probe.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 201211725bf8149594c68fcaff9ff8b915c0324b; the delta from 78794d37 is two files, with no rebase.

The remaining case/normalization blocker is fixed. composingCharacters now uses the existing addCaseClosure for original/NFC/NFD codepoints, with the page's collected locales passed through pageTextRuns (packages/core/src/fonts/deterministicFonts.ts:1946, 1993, 2128). Added variants remain inside the first face and its request-size check; fetch and network-policy paths are unchanged in this delta.

Validation through the shared gate:

  • Full build passed; font suite plus the retained reviewer assertions: 316 passed.
  • Both original uppercase/lowercase assertions pass. Restoring 78794d37 source makes those two and the two new committed cases fail (4 failed / 20 passed); restoring this head passes all 24.
  • Chromium 1208 with complete DejaVu Sans woff2: both cases match the full-font control exactly in canvas pixels and DOM screenshots.
  • Live Google Noto Sans KR: both accents are correctly above their letters, with matching widths. These screenshots exactly match the previously verified temporary repair. The separate single-face control still has minor rasterization differences; I am not claiming live pixel identity.

No cloud render was run. The earlier nonblocking total-request-count concern is unchanged.

Need: yes · Size: ok · Reuse: existing locale-aware addCaseClosure · Simpler: already minimal · Structure: fine (checked character collection, normalization and PageText consumers; no split)
Risk: medium — font partitioning affects rendered glyphs; the previously failing cases now have browser and mutation controls.

Verdict: APPROVE
Reasoning: The reviewed delta closes the missing case variants without changing request routing or the existing fallback boundaries.

— Rames / cutroom-review

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit 6a8f4b5 Nov 15, 2026
141 of 142 checks passed
@miguel-heygen
miguel-heygen deleted the fix/core-cjk-font-text-chunks branch November 15, 2026 12:45
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.

2 participants