Skip to content

Draw a font the document does not hold in a registered fallback font - #132

Merged
bobsingor merged 1 commit into
embedpdf/mainfrom
embedpdf/fix/registered-fallback-crash
Nov 25, 2026
Merged

bobsingor merged 1 commit into
embedpdf/mainfrom
embedpdf/fix/registered-fallback-crash

Conversation

@bobsingor

Copy link
Copy Markdown
Contributor

Cause

When PDFium's substitute for a font has no glyph for a character, CPDF_Font::FallbackFontFromCharcode picks the font that draws it. Upstream that is always LoadSubstFace("Arial"), which comes with a CFX_SubstFont. Ours takes a registered fallback font first, and those are loaded from their own file, not through the font mapper, so they have no CFX_SubstFont.

CPDF_Font::ShouldApplyGlyphSpacingHeuristic reads the record without a check, so a render that reaches it with a registered font crashes: a read of address 0x1a in native builds, memory access out of bounds in WebAssembly.

It takes a font that the document names but does not embed, that is not one of the 14 standard fonts, has widths, is written horizontally, and has a character that the substitute lacks and a registered fallback font has. The test of 18d23c9 did not reach it: its font is named Helvetica and has no widths, and either returns before the read.

Change

  • A font without a substitution record stands in for the font the document names, so the heuristic applies to it: a glyph wider than its width in the document is drawn narrower, and one narrower than it is centred, as with any substitute.
  • The ledger's fonts rule says what a registered fallback font does for the page's own text, names the guard, and says that a font from GetFontFallback() may have no CFX_SubstFont. This was the only place that read it unchecked.

Tests

  • EPDFAnnotEmbedderTest.RegisteredFallbackFontRendersUnembeddedFont: a character in a font the document does not hold, as a Type0 font and as a simple TrueType font, with the widths 1000 and 500. It is drawn, and stays inside its width. It runs without system fonts, as WebAssembly does (ScopedNoSystemFonts).
  • It is listed as a layer-mode writer.

Checked

  • Without the change the test crashes at cpdf_font.cpp:261, address 0x1a, the crash of the released library. With the heuristic off for such a font instead (return false), it fails on the widths, 67 pixels where 36 fit.
  • macOS: unit 1412 passed. Embedder 1883 passed, 9 skipped and 13 failed: the 12 golden failures an unchanged build of the base has, with the same output line by line, and EPDFSlicedLoadEmbedderTest.SlicesKeepToTheirBudget, a time budget on a busy machine (run in turns with the base build, both passed 0 of 15).
  • Layer mode on macOS for the annotation and font suites: no error.
  • check-fork-tags.py --strict, check-c-api.py (604 functions), check-globals.py on the macOS library.
  • WebAssembly built from this branch, in the 3.3.0 engine: PDFs with unembedded Chinese, Hebrew and Arabic fonts render in Node and Chrome, where 3.3.0 fails. A macOS library built from this branch: Node renders them, where the 3.3.0 one exits with 139.

Left

  • Linux x64 suites and the whole suite in layer mode: still running when this was opened. The fork's checks don't run them.
  • Release, and the pin in embed-pdf-viewer.
  • Not part of this change: Latin characters of an unembedded CID font that the built-in serif draws are not fitted to their widths, so narrow digits overlap. They did before too.

A registered fallback font is loaded from its own file, not through the
font mapper, so it has no substitution record.
CPDF_Font::ShouldApplyGlyphSpacingHeuristic() read that record from every
font that stands in for one the document names, and crashed: a read of
null in native builds, "memory access out of bounds" in WebAssembly.

Take a font without a record as standing in for the named one, so its
glyphs are fitted to the widths the document gives.
@bobsingor
bobsingor merged commit 7f399f8 into embedpdf/main Nov 25, 2026
1 check passed
@bobsingor
bobsingor deleted the embedpdf/fix/registered-fallback-crash branch November 25, 2026 10:17
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