Skip to content

Print Chinese, Japanese and Korean text in PDF exports - #631

Merged
QR-Madness merged 2 commits into
masterfrom
pdf-cjk-fonts
Dec 1, 2026
Merged

QR-Madness merged 2 commits into
masterfrom
pdf-cjk-fonts

Conversation

@QR-Madness

Copy link
Copy Markdown
Collaborator

What

PDF exports embedded only the document's font, so Chinese, Japanese and Korean text came out blank, with no warning. Now any text the current face lacks falls back:

  • Noto Sans SC Regular (Han and kana) covers Chinese and Japanese.

  • Noto Sans KR Regular covers Hangul, which SC doesn't have.

  • FallbackDoc (src/utils/pdfExport/fallbackDoc.ts) stands in front of the document the layout draws on. It splits each string into runs by font, then measures and draws each run in its font.

    • Text the face covers goes straight through.
    • Latin documents export byte for byte as before: all 29 golden exports were compared with PDF_DUMP_DIR before and after.
  • Measuring never needs the font files.

    • fallbackMetrics.json (6.6 KB) holds each character's width as jsPDF measures it. scripts/gen-pdf-fallback-metrics.ts generates it from the fonts with jsPDF's own parser, and a drift test fails until it's regenerated.
    • So Paper Mode never downloads a fallback font, and the export fetches one only when its text needs it.
    • An offline export still lays out correctly. It leaves the missing font's text blank and says so.
  • Fonts are hashed assets (src/assets/fonts/noto-sans-{sc,kr}/, OFL, licence beside each).

    • They're never precached. The existing runtime rule keeps them once fetched, so later exports work offline.
    • jsPDF already embeds only the glyphs used, and exports are compressed (DSK-250). A CJK font adds about 50 KB to a PDF, so no subsetter is needed.
  • Line breaking (lineBreaks.ts):

    • CJK text breaks between characters, with basic kinsoku: no line starts with a stop, a closing bracket or a small kana, and none ends with an opening bracket.
    • Text that breaks at spaces tokenizes exactly as before.
    • Also fixes a Latin defect: a space that didn't fit used to open the next line, indenting it by one space. That happened whenever a line filled exactly, and three golden documents had it: their bytes change, their text doesn't. CJK text hits it constantly.
  • The export says what it left out. jsPDF can't draw characters outside the BMP whatever the font, because it reads only BMP cmaps and encodes text in 16-bit units, and most emoji are out there. Scripts that need shaping (Arabic, Hebrew, the Indic scripts) aren't covered either.

    • A notice names them: "This PDF leaves out Arabic text and emoji, which PDF exports can't include yet."
    • A font that failed to download gets a separate, temporary notice.

Known limits (in the guide)

  • Bold and italic CJK text prints in the regular weight. Only Regular is bundled.
  • Japanese kanji use the Simplified Chinese shapes where they differ.
  • Hangul breaks between syllables, as browsers do by default.
  • Emoji are a pending decision, kept out of this PR.

Size

  • Main chunk: +14 KB raw, +5 KB gzipped.
  • dist: +16.7 MB for the two fonts (10.5 and 6.2 MB). The PWA never precaches them; the desktop installer carries them.

Verification

  • New tests:
    • line breaking (14);
    • fallback fonts (10), including a font that fails to load;
    • FallbackDoc (11), including table widths equal to jsPDF's with the fonts loaded, to nine places;
    • CJK layout (3);
    • the metrics drift guard (1).
  • Golden: a new scripts/cjk document: 5 pages, with contents, outline, a mixed-script line, a list, a table and code.
    • Every CJK character of the source comes back out of pdf.js.
    • Nothing is reported left out.
    • paginate() matches the export.
    • No other golden entry changed.
  • Mutations: 13 of 14 caught. The one survivor is equivalent: noteMissing's early return while measuring only saves a scan, because count() guards too.
  • Code review (high): 5 findings. Two were real and are fixed, with tests that fail first: code-line indentation lost when CJK wraps, and the look-ahead's width-cache key. Two nits are fixed. Over-fetching for CJK in attributes is documented and kept.
  • Pages rendered with poppler: glyphs correct for all three scripts.
  • In the browser (local dev server, local document, Paper Mode on):
    • no font bytes fetched;
    • the export fetched both fonts once;
    • the notice listed Arabic and emoji.
  • Suites: tsc clean; the suite passes 5,547 tests in 516 files; bun run build passes. The OSS leak grep is clean.

Assisted-by: Opus 5.5

PDF exports embedded only the document's font, so Chinese, Japanese and
Korean text came out blank, silently. Text the current face lacks now
falls back to Noto Sans SC (Han and kana) or Noto Sans KR (Hangul).

- FallbackDoc stands in front of the document the layout draws on and
  splits each string into runs by font. Text the face covers goes straight
  through, so Latin documents export byte for byte as before.
- The layout measures the fallback fonts from fallbackMetrics.json,
  generated from the fonts with jsPDF's own parser. Paper Mode never
  downloads them, and an export fetches one only when its text needs it.
  The fonts are hashed assets, never precached, and kept by the service
  worker after their first fetch.
- CJK text breaks between characters, with basic kinsoku rules. A space
  that doesn't fit ends a line instead of opening the next one, which also
  fixes Latin lines that began with a space.
- jsPDF can't draw characters outside the BMP (most emoji), and scripts
  that need shaping (Arabic, Hebrew, Indic) aren't covered. The export now
  says what it left out instead of dropping it silently.

Assisted-by: Opus 5.5
From the review: wrapTokens dropped the spaces a paragraph opens with,
so a wrapped code line with CJK in it lost its indentation. MeasureDoc
now carries the fallbackFonts flag of what it measures with, so a
heading's look-ahead never shares width-cache entries with a plain
document. Contents labels shorten by characters, never inside a UTF-16
pair, and the two base64 helpers are one.

Assisted-by: Opus 5.5
@QR-Madness
QR-Madness merged commit ab8070d into master Dec 1, 2026
4 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