Skip to content

fix(kernel,layout): report glyph 0 and unembedded fonts under a claim - #657

Merged
Tim81 merged 8 commits into
mainfrom
fix/notdef-report-default-font
Nov 21, 2026
Merged

Tim81 merged 8 commits into
mainfrom
fix/notdef-report-default-font

Conversation

@Tim81

@Tim81 Tim81 commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Closes #592
Closes #593

A character that an embedded font has no glyph for is written as glyph 0, and a PDF/A or PDF/UA document that falls back to an unembedded Helvetica for a list, table or running band is written as it always was. Both files fail veraPDF, and the save said nothing about either. This PR makes the save say so. It changes no content stream.

What changed

Kernel (first commit).

  • PdfCanvas.NotdefGlyphs, PdfCanvas.NotdefGlyphsDropped, PdfDocument.NotdefGlyphs, PdfDocument.NotdefGlyphsDropped and the new NotdefGlyphWarning list every glyph 0 the canvas writes: one entry per character and font resource, in order of first occurrence, with a count. At most 1,000 entries per canvas and per document; the rest are counted in ...Dropped. The type has an internal constructor and getters only, so it takes no input and nothing throws. TextEncodingWarnings is untouched and does not list glyph 0.
  • Public ShowGlyphs sees ids, not text, so its entries have no character. The library's own text drawing names the character and the font.
  • Finish stores a snapshot of the canvas's entries on the page beside its content bytes. Setting PdfPage.ContentBytes directly clears them. PdfDocument.NotdefGlyphs is built on each read from the pages, and Save, SaveAsync and PrepareForSigning leave it as it was.
  • Deliberate output change. The ToUnicode map no longer lists a code point against glyph 0. It carries one entry for it, <0000> <FFFD>, when any character was mapped to glyph 0. Before, a font whose subset used glyph 0 wrote one <0000> <...> line per missing character. The entry exists because glyph 0 is itself what ISO 19005-2, 6.2.11.8, and ISO 14289-1, 7.21.8, refuse, and U+FFFD satisfies the value rule of 7.21.7. A document in which no character was mapped to glyph 0, drawn or only measured, is unchanged. One in which a character was (a character that was only measured, such as a running band's cut tail, counts) differs only in that stream, which tests pin, including the measured-only case. Glyph ids passed straight to ShowGlyphs add no entry, which a test pins too.

Layout (second commit).

  • LayoutDiagnosticCode.NotdefGlyph (102) reports each character and font written as glyph 0, with the count. The clause is ISO 19005-2:2011, 6.2.11.8 under a PDF/A claim, ISO 14289-1:2014, 7.21.8 under PDF/UA-1, and null with no claim.
  • LayoutDiagnosticCode.UnembeddedFont (601) reports each element that writes a font operator for a named Standard 14 font while Conformance is not None, once per element and font, with the number of font operators. The clause is ISO 19005-2:2011, 6.2.11.4.1 or ISO 14289-1:2014, 7.21.4.1. SetDefaultFont still applies only to Document.Add(string); making it reach every element is not part of this PR.
  • Both save by default and StrictDiagnostics refuses them.
  • Paragraph, table-cell and running-band text now selects its font and shows its text through one TextEmitter, so one report covers every built-in element. Each site keeps its own text object, colour and matrix, and their order.
  • XML documentation on the new members, and on every member that falls back to Helvetica (Document.SetDefaultFont, Document.Add(string, TextStyle), SetHeader, SetFooter, Document.Conformance, Paragraph, Heading, ListElement.DefaultStyle, ListItem.Style, TableElement.DefaultCellStyle, Cell.Style, RunningBand, TextStyle.FontRef, TextStyle.Default, Cell.Content), names the report, the strict switch and the exception.

Compatibility

Input Before After, default After, StrictDiagnostics
Embedded font, no missing character, any claim saves same bytes same
DejaVu Sans, "A ⚹ B \U0001F31F C", PDF/A-2u saves, ToUnicode <0000> <26B9> and <0000> <D83CDF1F> saves, content stream the same, ToUnicode <0000> <FFFD> once, two NotdefGlyph items InvalidOperationException, stream empty
Same text, no claim / PDF/A-2b / PDF/UA-1 saves clause null / 6.2.11.8 / 7.21.8 throws
Liberation Mono and U+2609 saves, glyph 0 one item naming LiberationMono (TT1) throws
Liberation Mono and U+202F saves, glyph 98 glyph 98, no item saves
Helvetica and U+26B9 ? and one UnencodableCharacter item unchanged, no NotdefGlyph throws as before
Custom renderer, ShowGlyphs([0, 36]) saves one item, character unknown throws
Default font on Add(string) only; a list and a table, PDF/A-2u saves, veraPDF fails 6.2.11.4.1 same bytes; UnembeddedFont for the List (count 2) and the Table (count 1) throws naming the List
Same with embedded styles on the list and table, or with no claim saves same bytes, no item saves
Empty cell in the default style, PDF/A-2u saves same bytes, one UnembeddedFont item throws
TextStyle { Font = (Standard14)99 }, PDF/A-2u IndexOutOfRangeException from the write same, no item same

No document that saved before is refused by default. Strict mode is opt-in. A Kernel save is not refused for glyph 0, because that would tighten ShowGlyphs, which has shipped.

Two existing tests whose pinned decision this changes, both rewritten on purpose:

  • ToUnicodeStandInTests.CodePointsThatAreNotStandIns_keepTheirEntries_glyphZeroGetsOneReplacementEntry (renamed) pinned that missing characters keep their own glyph-0 entries. It now expects one entry, (0, 0xFFFD).
  • NoBreakSpaceBundledFontTests.LiberationMono_toUnicode_missingCharactersThatAreNotSpacesShareOneReplacementEntryForGlyphZero (renamed) pinned the same for two CJK code points. It now asserts the (0, 0xFFFD) entry is present and neither code point is listed.

Byte-identity: PlacementLoopGoldenTests and every other existing golden pass unchanged. FontReportByteIdentityTests pins six hashes over five documents: a PDF/A-2u document with a default font and an unstyled list, table, paragraph, heading and footer; the empty unstyled cell; a document whose two runs in one font differ in size (still written as one Tf, which this PR does not change); a no-claim document mixing Standard 14 and embedded text in every kind of element and both bands; and, for the sixth hash, the missing-character document with its ToUnicode stream removed. They were first taken on the branch point before any change.

Re-pins after merging main. The font bounding box change (#656) moves every embedded-font digest, so these moved, and nothing in this PR moves them: I computed the same values on main without this PR's source for the six hashes of FontReportByteIdentityTests (DefaultFontWithUnstyledListAndTable_pinsBytes, UnstyledElementsAndFooterUnderAClaim_pinsBytes, EmptyUnstyledCell_pinsBytes, TwoSizesOfOneFont_pinsBytes_andWritesOneFontOperator, MixedNoClaimDocument_pinsBytes, AMissingCharacterDocument_differsOnlyInItsToUnicodeStream) and for the two goldens that merged with #654, RunsHeadingListItemGoldenTests.EmbeddedHeadings_pinTheBytes and EmbeddedLists_pinTheBytes, which fail on main itself after #656 and are re-pinned here.

Tests

Project net10.0 net11.0
VellumPdf.Kernel.Tests 2660 passed 2660 passed
VellumPdf.Layout.Tests 1463 passed, 2 skipped (Linux-only) 1463 passed, 2 skipped (Linux-only)
VellumPdf.Conformance.Tests 1469 passed 1469 passed
VellumPdf.TestSupport.Tests 17 passed 17 passed

All in Release with veraPDF 1.30.4, qpdf 12.4.2 and poppler as the oracles required. Kernel, Conformance and TestSupport counts are from the head before the last merge of main (#655, layout only); Layout was re-run after it. dotnet build VellumPdf.slnx -c Release has 0 warnings, dotnet format --verify-no-changes, eng/clean-room-check.ps1 and python eng/generate-roadmap.py --check exit 0.

Oracle checks on the PDF/A-2u missing-character file, each asserting the fixture holds the change first (<0000> <FFFD> present, no <0000> <26B9>):

  • veraPDF 2u: failed rules exactly 6.2.11.8-1, so the new ToUnicode entry does not trip 6.2.11.7.2. veraPDF ua1 on the PDF/UA-1 variant: 7.21.8-1 fails, 7.21.7-1 does not.
  • pdftotext -enc UTF-8: exactly two U+FFFD, no U+26B9 or U+1F31F.
  • qpdf --check reports "No syntax or stream encoding errors found" on it and on the default-font file.
  • The library's own preflight reports ISO19005-2:6.2.11.8-notdef and not ISO19005-2:6.2.11.7.2-tounicode.
  • Default-font file: pdffonts lists Helvetica as not embedded and veraPDF 2u fails exactly 6.2.11.4.1-1. The file with the empty unstyled cell fails exactly that rule too. The file whose styles are all embedded has no unembedded row in pdffonts and veraPDF 2u reports no failed rule.

tests/VellumPdf.TestSupport/ExternalTool.cs now resolves pdffonts through POPPLER_HOME and checks its identity (pdffonts version) like the other poppler tools, so a missing tool skips instead of failing and a wrong POPPLER_HOME is reported. It is in ExternalToolResolutionTests.Tools.

Mutation proof

Each mutant was built in a detached worktree and killed by the named tests, then restored.

# Mutant Failing tests
1 keep glyph-0 pairs in ToUnicode 3 in NotdefGlyphTests, ToUnicodeStandInTests (1), 6 in NotdefGlyphReportTests, the byte-identity projection test
2 write no glyph-0 entry the same set; veraPDF then also fails 6.2.11.7.2-1
3 ShowEmbeddedText records nothing 5 in NotdefGlyphTests, 15 in NotdefGlyphReportTests
4 public ShowGlyphs records nothing 12 in NotdefGlyphTests, 2 in NotdefGlyphReportTests (custom renderer, sink drop count)
5 paragraph site writes ids with GetGlyphIds and ShowGlyphs 11 in NotdefGlyphReportTests
6 cell site the same ATableCell_isReported
7 band site the same AHeaderBand_isReported, AFooterBand_isReported
8 sync honours report: false for glyph 0 TheSink_reportsGlyphZeroWhateverReportSays_asAListMarkerNeeds
9 font selection never reuses the font (a size-sensitive compare writes a second Tf) Sizes_pinsBytes_andWritesOneFontOperator, TwoRunsInOneFontAtTwoSizes_countOneFontOperator
10 drop the conformance condition WithNoClaim_nothingIsReported, AHelveticaParagraph_reportsTheUnencodableCharacters_andNoGlyphZero
11 report UnembeddedFont when text is shown, not at Tf AnEmptyCell_isReported_becauseTheFontOperatorIsWritten
12 drop the named-value check both cases of AStandard14ValueTheEnumerationDoesNotName_isNotReported_andTheSaveThrowsAsBefore
13 cell site selects its font without the emitter 5 in UnembeddedFontReportTests
14 band site the same 3 in UnembeddedFontReportTests
15 Finish stores the live tally DrawingAfterFinish_isNotCountedUntilFinishRunsAgain
16 the ContentBytes setter does not clear SettingContentBytesDirectly_clearsThePagesEntries
17 cap test > instead of >= TheCanvasListHoldsAThousandEntries_andCountsTheRestAsDropped, TheDocumentList_isCappedAtAThousandEntries_andAddsEveryDropCount
18 the document read does not merge across pages TheDocumentList_addsTheCountsOfEveryFinishedPage, TheDocumentList_isCappedAtAThousandEntries_andAddsEveryDropCount
19 the sink ignores the canvas's dropped count TheSink_countsWhatTheCanvasDropped_andReadsItOnce
21 tally key without the resource name 6 in NotdefGlyphTests
22 the sink dedupes by code point only TheSameCharacter_isOneItemPerFont_countedAcrossElements, TheSink_countsWhatTheCanvasDropped_andReadsItOnce
23 the sink reports NotdefGlyph per element TheSameCharacter_isOneItemPerFont_countedAcrossElements
24 the sink adds the cumulative count TheSameCharacter_isOneItemPerFont_countedAcrossElements
25 the UnembeddedFont subject is constant AnExplicitStandard14Style_underPdfUa_isReportedUnderThePdfUaClause, TwoStandard14FontsInOneParagraph_areTwoItems

Mutants 1 to 20 were run against the first two commits' tests; 21 to 25 against the head after the review round, with the new grouping and counting tests (the same-character, two-fonts, two-Standard-14-fonts and three-item-list cases).

The mutant that drops the sink's per-sync shortcut (reading every entry on every sync) changes no output, so no test can fail on it; its cost is in the bound table.

Bounds

GC.GetAllocatedBytesForCurrentThread() around Save, PDF/A-2u, one paragraph of n U+26B9 characters separated by spaces, before and after (net10.0 Release):

n before after items after, first count
1,000 2,919,144 B 2,920,656 B 1, 1000
10,000 9,453,720 B 9,457,992 B 1, 10000
100,000 72,480,160 B 72,516,880 B (+0.05%) 1, 100000

The public list: 100,000 further ShowGlyphs([0]) calls on a stored entry allocate 2,096,928 B, the same as 100,000 calls with glyph 36 (the Tj bytes), and the entry's count grows. The same calls past the 1,000-entry cap allocate 2,031,736 B, add 100,001 to NotdefGlyphsDropped and store nothing. One read of PdfDocument.NotdefGlyphs on a 1,000-page document with one entry per page allocates 235,904 B, and 0 B when no page wrote glyph 0. A page without glyph 0 costs the sink one comparison per sync.

Not done

  • SetDefaultFont is not applied to lists, tables, headings or bands, and nothing is refused by default.
  • The 601 report does not cover a Standard 14 font a custom renderer selects on the canvas itself, nor the human-readable text of a one-dimensional barcode, which selects its font without the channel (named on Barcode1D.TextFont and in the UnembeddedFont remarks).
  • A control character in Standard 14 text, such as a tab in a cell, is written as its WinAnsi code and is not reported as NotdefGlyph; the NotdefGlyph remarks name it.
  • The UnembeddedFont trigger is the font operator, as veraPDF's rule is: an empty cell in Helvetica is reported, while ISO 14289-1, 7.21.4.1, counts a font as used only when a glyph is referenced. The remarks and the CHANGELOG say so.
  • A Kernel save is not refused for glyph 0.
  • The font operator is still skipped when a later run has the same font at another size.

Differential against 2.3.3

A harness that runs 28,631 inputs against the 2.3.3 libraries and against the build reported 0 violations on the branch point, on the first commit and on the second. Comparing the verdict rows of the branch point with those of each commit, after masking output paths and file hashes, no row changed except cancel-scan on the first commit (a cancellation race; the second commit's run does not repeat it). One row's hash moves on every run: it saves an encrypted document, which is never reproducible. Each build's assembly informational version is printed by the harness: 2.3.3+3bfb8cf, 2.3.3+cdd49f3 and 2.3.3+111e4ef.

An embedded font drew a missing character as glyph 0 and the save said
nothing. PdfCanvas.NotdefGlyphs and PdfDocument.NotdefGlyphs now list each
glyph 0 the canvas writes, one entry per character and font resource,
capped at 1,000 with the rest counted in NotdefGlyphsDropped. The
ToUnicode map no longer lists a code point against glyph 0; it carries
one entry, <0000> <FFFD>, which ISO 14289-1 7.21.7 accepts. The content
stream is unchanged.
…#593)

The layout now reports through Document.Diagnostics what a PDF/A or PDF/UA
file fails on without saying so. NotdefGlyph (102) lists each character
an embedded font has no glyph for, per font, with its count. UnembeddedFont
(601) lists each element that selects a Standard 14 font while a
conformance claim is set, which is what a list, table, heading or running
band without its own style does because SetDefaultFont reaches only
Document.Add(string). Both cite the clause of the claim, save by default
and are refused by StrictDiagnostics.

Paragraphs, table cells and running bands now select the font and show
text through one TextEmitter, so one report covers every built-in
element. The bytes written do not change.
…ault-font

# Conflicts:
#	src/VellumPdf.Layout/Elements/Heading.cs
#	src/VellumPdf.Layout/Elements/ListItem.cs
#	src/VellumPdf.Layout/PublicAPI.Unshipped.txt
…ault-font

# Conflicts:
#	CHANGELOG.md
#	tests/VellumPdf.Kernel.Tests/VellumPdf.Kernel.Tests.csproj
…ded-font reports (#592, #593)

State where each list and report stops, name the validator-versus-clause
difference for a font selected with no glyph shown, say why the single
U+FFFD entry exists and when it is written, and name the cases the
reports do not see (one-dimensional barcode text, control characters in
Standard 14 text). Build the 601 message only for a new subject, make
the tally copy self-consistent, resolve pdffonts through the same
identity probe as the other poppler tools, and pin the grouping and
counting rules, the measured-only and raw-id ToUnicode cases and the
refused SaveAsync and PrepareForSigning cases with tests. Re-pin the
byte-identity hashes that the font bounding box change moved.
…ault-font

# Conflicts:
#	src/VellumPdf.Layout/PublicAPI.Unshipped.txt
@Tim81
Tim81 merged commit 2dd2a83 into main Nov 21, 2026
4 checks passed
@Tim81
Tim81 deleted the fix/notdef-report-default-font branch November 26, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant