Skip to content

fix(kernel): write whole reals outside int32 as reals, and form text as text strings - #646

Merged
Tim81 merged 6 commits into
mainfrom
fix/real-tokens-and-form-text
Nov 20, 2026
Merged

Tim81 merged 6 commits into
mainfrom
fix/real-tokens-and-form-text

Conversation

@Tim81

@Tim81 Tim81 commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #639
Closes #641

Two object-writer encoding fixes with disjoint documents (numbers, form text strings), kept as two commits.

#639: a whole number outside the 32-bit range is a real token

PdfReal.WriteTo, PdfCanvas.N and the twelve {0:0.###} appearance-stream sites plus BuildDa in
AcroFormBuilder formatted a whole value with no decimal point, so 2147483648.0 reached the file as
the integer token 2147483648. ISO 32000-1 Table C.1 gives 2,147,483,647 as the largest integer (ISO 32000-2 Table C.1 says only that
integers "can often be expressed within 32 bits"), and ISO 19005-2 6.1.13 fails a PDF/A file with an
integer outside that range; from 2^63 up, qpdf --check drops the object.

The change is one internal formatter, PdfNumberFormat, used by all three. It decides on the written
text, not on the double: if the text has no . and does not parse as an int, it appends .0.
(. alone is also valid 7.3.3 syntax; .0 was chosen to read as a real at a glance. No reader
behaviour is claimed for that choice.) NaN and infinity refusals stay where they were. Every site
is a number-typed slot (/XYZ coordinates, rectangles, shading arrays, alpha, colours, the m, l,
c, re, Td, Tm, Tf, w, cm and colour operands), so 7.3.3's "a real shall not be present
when an integer is expected" is not engaged.

NamedDestinationTable keeps its +-2,147,483,647 bound with its comment rewritten (the old reason is
false after this change; the table is internal and has no caller in src).

Deliberate output change: only tokens whose rounded text is a whole number outside -2^31..2^31-1
change. See the CHANGELOG entry.

#641: AcroFormBuilder.TextString uses PdfLiteralString.FromTextString

Field names, values, options, selected option and captions (/T, /V, /Opt, /MK /CA) go through
the same encoder as every other text string. XML docs on the five Add* methods state the rule once
on AddTextField, with the lone-surrogate and U+001B "Do not pass" naming 3.0, and Save,
SaveAsync and PrepareForSigning list the ArgumentException for a drawn value outside
WinAnsiEncoding. The appearance-stream mapping is unchanged.

Tests

RealTokenTests (token table through PdfReal, canvas m, link /Dest, outline /Dest,
/MediaBox, form /Rect, /BBox, appearance and /DA; qpdf --check and --json), and new
FormTests rows for each site and for the appearance byte.

Mutations

Each applied to the commit named, run against the new test class only (net10.0), then reverted.

Mutant Failing tests
#639 M1: decide on the double (Math.Abs(value) > int.MaxValue) 10 (-2147483648 and 2147483647.000001 rows through link, outline, MediaBox, canvas)
#639 M2: canvas N without the suffix 8 (every CanvasOperand row outside int32)
#639 M3: long.TryParse 28
#639 M4: append . instead of .0 37
#639 M5: AcroForm sites left on {0:0.###} 2 (wide rectangle; huge font size)
#641 M1: old c > 0xFF test 13
#641 M2: skip the FE FF mark check in FromTextString 2 (the leading-mark name and value rows)
#641 M3: route through FromUnicode 10
#641 M4: /Opt on the old encoder 1 (ChoiceField_options_are_written_as_text_strings)

Per-site coverage of the AcroForm appearance numbers: for each of the 12 Num(...) lines in
AcroFormBuilder (check box, radio, push button and text field), replacing that one line with the plain
0.### format fails exactly one new or existing test in RealTokenTests (12 of 12 sites killed; the push
button re S line and the text field re S and Td lines were not killed before the added tests).

Gates

Release, 0 warnings on VellumPdf.slnx; dotnet format --verify-no-changes, the clean-room check and
generate-roadmap.py --check pass. Tests on net10.0 and net11.0 with the oracles required: Kernel 2,476
(0 failed, 0 skipped on both), Layout 1,074 (2 skipped), Reader 2,023 (12 skipped), Conformance 1,453,
Barcodes 1,051 (0 failed in all). The Layout, Reader, Conformance and Barcodes counts were taken one commit
earlier; the last commit adds one test file and touches no source in those projects.

veraPDF

RealTokenVeraPdfTests saves a PDF/A-2u document with a link destination and canvas m operand at
2147483648, -2147483649, 3.403e38 and 3e9, first asserts the file holds the N.0 token, then asserts
veraPDF 1.30.4 (flavour 2u) lists no failed rule. It reads the failed rules from the report, not the exit
code.

Direction, measured on the same four fixtures plus 3.5e38:

Build 2147483648, -2147483649, 3.403e38, 3e9 3.5e38
this branch no failed rule 6.1.13-2 (a real above 3.403e38, two checks)
formatter without the .0 suffix (the old behaviour) 6.1.13-1 failed, two checks each 6.1.13-1

A form is different: a text field draws with the non-embedded Helvetica, so every PDF/A-2u form fails
6.2.11.4.1-1 once, with an ASCII name and with a U+00A0 name alike. The form test therefore asserts the two
failed-rule lists are equal and that /T and /V hold the UTF-16BE strings. veraPDF 1.30.4 has no rule
that tells the old single byte 0xA0 from the new string, so that test cannot discriminate the encoding
change; the byte-exact FormTests rows do.

Differential against 2.3.3

28,631 rows compared at the base commit and at this branch's head. The base shows no violation. The head
shows 36 rows whose files equal 2.3.3's once the N.0 tokens outside int32 are turned back into integer
tokens and the length, offset, date and ID fields are masked, and five rows still reported as output
changes. Those five combine this change with one already on the base:

  • fuzz.1.176, .179, .202 and .212 carry U+202F or U+2007. After the same masking, head's file equals
    2.3.3's file for the same plan with U+00A0 in place of those characters.
  • fuzz.1.206 is a list-item row the base already classes as a deliberate list change. After the same masking,
    head's file equals the base head's file for that row.

Every other row has the verdict it had at the base, with one exception that is not this change: a
cancellation-timing row (a save cancelled partway) went from identical to deliberate between the two runs,
because whether the cancel lands before or after serialisation depends on timing.

PdfReal, the canvas operands and the AcroForm appearance numbers wrote
2147483648.0 as the integer token 2147483648. ISO 32000-2 Table C.1 gives
integers 32 bits and ISO 19005-2 6.1.13 fails a PDF/A file past that
range; from 2^63 up a reader drops the object. One shared formatter now
appends .0 when the written text is whole and not a 32-bit integer.
AcroFormBuilder.TextString wrote Latin-1 bytes for any string without a
character above U+00FF, which a PDFDocEncoding reader decodes as other
characters for U+0018-U+001F, U+007F, U+0080-U+00A0, U+00AD and a
leading byte order mark. It now uses PdfLiteralString.FromTextString,
as the signing strings do. The public documentation states the rule and
the exceptions Save raises.
Saves PDF/A-2u documents with link and canvas numbers at 2147483648,
-2147483649, 3.403e38 and 3e9, asserts the real token is in the file,
and asserts veraPDF lists no failed rule. Without the suffix veraPDF
fails 6.1.13-1 on each. A form with a U+00A0 name fails the same
6.2.11.4.1-1 as an ASCII one, so veraPDF cannot see the text-string
change. Also corrects two phrases in the #639 CHANGELOG entry.
…umber site

The integer bound is ISO 32000-1 Table C.1; ISO 32000-2 says only that
integers can often be expressed within 32 bits. The #641 entry now
excepts U+001B and the byte order mark, SaveAsync documents the WinAnsi
refusal under ArgumentException, and the TextString summary names the
encoders that really share it. New tests kill a plain-format mutant of
each of the 12 appearance-stream number sites.
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