Skip to content

test: record tagged Layout output with a structure dumper (A11Y-12) - #580

Draft
Tim81 wants to merge 10 commits into
mainfrom
test/a11y12-structure-goldens
Draft

Tim81 wants to merge 10 commits into
mainfrom
test/a11y12-structure-goldens

Conversation

@Tim81

@Tim81 Tim81 commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Part 1 of A11Y-12. Tests only: nothing under src/ changes, and no public API file changes.

Summary

The 2026-11-16 handbook research measured veraPDF 1.30.2 ua1 and PdfPreflight passing tagged
Layout output that is not accessible (A11Y-02, -04, -05, -06, -07, -09, -15 among others). The tagging PRs that follow need a test that fails on today's
defect and passes on the fix, and neither validator can be that test.

This PR adds the instrument and records today's output with it:

  • StructureDumper in VellumPdf.Conformance.Tests prints a PDF's structure tree, each page's
    marked content and each page's annotations as text. It checks the parent tree against them and
    prints a problem line where they disagree. It walks /K and /ParentTree itself, because
    StructureTree collapses MCID, MCR and OBJR kids into a flag, de-duplicates by object number and
    truncates silently at its caps. Shown text is decoded through /ToUnicode, because the Reader's
    text extraction has no marked-content awareness. A line marker shows where a text line breaks, so
    a run boundary inside a word is visible.
  • Ten Layout documents are pinned as Verify goldens. The first line of each golden lists the audit
    findings it still shows, so the PR that fixes one finding changes that golden in the same diff.

The problem-line footer reports structural inconsistencies, not PDF/UA findings. A golden can show a
defect with 0 problem line(s).

What the goldens show

All ten documents use Conformance = PdfUA1, a title, Language = "nl-NL", an A4 page, the embedded
DejaVu Sans and an explicit style on every element. The "emphasis" runs in case 6 differ by colour,
because no bold face is embedded in the test project.

Case Document Findings in the header
1 Paragraph split over a page break, with one word longer than the line A11Y-04, A11Y-17
2 Table over three pages, one header row, a border A11Y-02, A11Y-04, A11Y-05, A11Y-16
3 Ordered list with a nested list, split over a page break A11Y-04, A11Y-06, A11Y-09, A11Y-15
4 Header and footer bands with {page} and {pages} A11Y-02, A11Y-04, A11Y-17
5 Two images, one with AltText A11Y-07, A11Y-16
6 The audit's runs (a word split across styles) and a LinkUri run mid-sentence A11Y-01, BUG-01
7 Headings at levels 0 to 6 A11Y-14
8 Table with an empty cell A11Y-02, A11Y-16, A11Y-18
9 Element-level Language overrides in an nl-NL document A11Y-08 (baseline: element-level language only; per-run language not expressible yet), A11Y-15
10 One long list item split over a page break A11Y-04, A11Y-09, A11Y-15, A11Y-17

A table row taller than a page is refused by the layout engine with an InvalidOperationException
("An element is too tall to fit on a single page") instead of being split. That was observed for one
input, a 60-sentence cell in a 300 pt column. There is therefore no table counterpart to case 10.

How the dumper is bounded

The dumper reads trusted test data: PDFs that this repository's tests generate and bytes they write
by hand. On any input it terminates with a dump, an InvalidOperationException naming the cap it hit,
or an exception from the Reader. Its output is bounded, and no single line or printed value is
unbounded. The cost of pathological input is not bounded beyond termination and is out of scope.
Three examples are named in the XML remarks: descending /ToUnicode ranges that each cover what the
ones before them left open make flattening quadratic; a property list is sorted again for every
BDC that names it; the CMap tokenizer holds every token of the stream at once.

Caps that throw an InvalidOperationException instead of truncating:

  • element depth 256, structure nodes plus parent-tree entries 100,000, parent-tree depth 64,
    marked-content depth 64 (counted across a page and the forms it draws);
  • the whole dump, 8,000,000 characters. Every line is charged against that budget when it is created.

A line longer than 20,000 characters is cut, not refused.

Caps on a single printed value, each saying so with …(+N):

  • a string, a dictionary key and a name printed as a value: 200 characters;
  • a name that labels something (an element's /S and its role-map target, an annotation's
    /Subtype, an action's /S, a destination's name or fit type, an OBJR target's type): 64;
  • a shown text: the code that takes it to 200 decoded characters (a code adds at most 256); raw codes
    after 100 bytes;
  • an element line: 32 keys;
  • a path keeps its last segments up to 200 characters, and its leaf segment is cut at 200, so a path
    prints in at most 203.

The cut is made on the source text, before escaping, and one UTF-16 unit escapes to at most eight
characters, so a string, key or name prints in at most 1,600 characters before its suffix and a shown
text in at most 3,640. A /ToUnicode destination over 512 bytes is unmapped (ISO 32000-2 9.10.3).

Problems are counted when they are emitted. Nothing reads the printed text back to decide what was a
problem, so a name such as /Span#0AORPHAN stays on its line and is not counted. Every
document-controlled string goes through one escape and cap. Integer fields the checks read (MCIDs,
parent-tree keys, /StructParent(s), /ParentTreeNextKey, revision numbers) go through one reader
that reports a real, negative or oversized value as BAD-*; the null object and a reference to
nothing are absent.

Two reports follow ISO 32000-2 14.7.5.2. A form that carries MCIDs and is drawn more than once is
reported as MULTI-DO, and a form with MCIDs painted inside an MCID-bearing sequence is reported as
FORM-MCIDS-INSIDE-CONTENT-ITEM.

Verified

Counts are from runs on the head 1acac44, without the external oracles.

  • The four StructureDump classes pass in Release: LayoutStructureGoldenTests 10,
    StructureDumperTests 22, StructureDumperHardeningTests 26, StructureDumperClassTests 25. That
    is 83 tests.
  • dotnet test --project tests/VellumPdf.Conformance.Tests -c Release: 1408 total, 1125 succeeded,
    283 skipped, 0 failed. The 283 skips are the oracle tests.
  • dotnet build VellumPdf.slnx -c Release gives 0 warnings and 0 errors. dotnet format VellumPdf.slnx --verify-no-changes and eng/clean-room-check.ps1 pass.
  • Each golden is built twice in one process and the two dumps are compared.

Mutation proofs, each run in a detached worktree and reverted. The head each was measured on is
stated, because the dumper changed between rounds.

Writer and Kernel mutations, on 66b7e12 (Debug):

Mutation Result
ListRenderer.cs:226 "LI" to "Div" Cases 3, 9 and 10 fail at the golden
Header and footer band artifact wrapping removed Case 4 fails; its dump shows UNMARKED show "Kop 1" and the three other band lines
PdfStructureTree.cs:214 read shifted: refList.Add(indexed[(i + 1) % n] ?? PdfNull.Instance) Eight goldens fail (cases 2, 3, 5, 6, 7, 8, 9, 10), with PARENTTREE MISMATCH lines such as parent tree has Document/L[0]/LI[0]/LBody[1]/P[0], element holding it is Document/L[0]/LI[0]/Lbl[0]/P[0]. Cases 1 and 4 do not fail: each of their pages has a single MCID.

An earlier form of the last mutation, writing indexed[(elem.Mcid + 1) % n], throws at Save from the
Kernel's duplicate-MCID guard and never reaches a golden, so it is not used as the proof.

Dumper mutations, each failing the test written for it:

Head Mutation Failing tests
66b7e12 DestFrom recursion unbounded the test host dies with Stack overflow.
66b7e12 no UNCLOSED-MC lines 3
66b7e12 no MULTI-DO line 1
66b7e12 no P-MISMATCH 1
66b7e12 no line marker 10 (one unit test and nine goldens)
66b7e12 footer counts the checks section only 4
66b7e12 MCR kids skipped 6
66b7e12 parent-tree comparison skipped 1
44c5e49 output budget not enforced 1
44c5e49 path cap removed 1
44c5e49 escape removed 6
44c5e49 problems counted by matching text 3
44c5e49 non-integers read as absent 3
44c5e49 no BAD-PROPERTIES line 1
44c5e49 CMap cache per page 1
f13d3fa plus the later line-cap test decode every code 2
f13d3fa plus the later line-cap test no key cap 1
f13d3fa plus the later line-cap test no line cut 1
f13d3fa plus the later line-cap test check lines not charged 1
f13d3fa plus the later line-cap test dangling reference read as bad 1
1acac44 GoTo /D read without the null-as-absent helper 1

A linear CMap lookup passed the first complexity test, because with the wide range first the
flattened table has a single segment. The test added in 1bdd0ce puts the wide range last; with a
linear lookup it fails after about 15 seconds. The 1bdd0ce commit message says the table has about
100,000 segments; the measured count is 50,002, and the squash message does not repeat the claim.
GC.GetAllocatedBytesForCurrentThread over one Dump of a 4,000-code show string measured 138,360
bytes against 123,936 for 400 codes, the same in Debug and in Release.

The original five mutation proofs were run on 66b7e12 and were not repeated on the final head.

Not verified

  • Encrypted documents. The dumper opens a PDF with no password, so a document the empty user password
    does not authenticate throws PdfPasswordException. Neither case is tested.
  • CI and Linux. All runs were on one Windows machine. The goldens contain no object numbers, paths or
    dates, but the Verify comparison on the first CI run is the check.
  • The oracle tests were skipped locally (283 skips), and the Release run was not made under CI load.

Review history

The brief was reviewed twice before implementation. After it, four rounds reviewed the branch, each
by independent reviewers: round 1 on 150294e, round 2 on 66b7e12, round 3 on 1bdd0ce and
round 4 on 3e16906. Round 1 found two kinds of problem: a crash on a self-referencing destination
dictionary and several things the dump silently skipped. Round 2 found that the round-1 fixes covered
the sites named and not their siblings, so output charging, string escaping, problem counting and
integer reading were each made structural. Round 3 raised a decode that built a whole string before
capping it, and a missing test for uncharged check lines. Round 4 found no HIGH and eight MEDIUM
findings, almost all comment and documentation corrections. After round 3 the orchestrator decided
that the dumper is a test oracle for trusted input, so pathological-cost inputs are documented as out
of scope instead of hardened further. That decision is pending the maintainer's confirmation. The
branch's commits are meant to be squashed; the squash message is drafted separately.

Tim81 added 10 commits November 18, 2026 00:03
veraPDF and PdfPreflight pass tagged Layout output that is not accessible,
so the fixes planned for it need a test that fails on today's defect and
passes on the fix. This dumper prints the structure tree, the marked
content each element claims, the parent tree and the annotations as text,
and prints a problem line where they disagree.

It walks /K and /ParentTree itself instead of reusing StructureTree, which
collapses MCID, MCR and OBJR kids into a flag, de-duplicates by object
number and truncates silently at its caps. Show operands are decoded
through /ToUnicode, since the Reader's text extraction returns nothing for
Type0 fonts and has no marked-content awareness.

Unit tests build the PDFs by hand, because the writer cannot yet produce
MCR or OBJR kids, attribute arrays, or a wrong parent tree.
Nine Layout documents (split paragraph, three-page table, nested list,
header and footer bands, images, runs split inside a word, headings,
empty cell, language overrides) are dumped with the structure dumper and
pinned with Verify. Each golden's first line names the audit findings it
still shows, so the change that fixes one updates the golden in the same
diff instead of passing unnoticed.

Each case is built twice and the two dumps compared, so a golden never
rests on a nondeterministic run.
Review of the first dumper found inputs that crashed or silently skipped
what a golden has to show. A destination dictionary whose /D points at
itself overflowed the stack; named property lists printed as unresolved
because the Reader exposes resources only around image callbacks; a form
drawn inside a sequence printed as UNMARKED; unclosed sequences, a /P that
is not the walking parent, an out-of-range MCID or key and an odd /Nums
were not reported at all.

The dumper now tracks resources itself, reports each of those as a marker
line, counts every marker in the footer, expands a shared attribute object
once, caps printed values with a per-dump budget, marks line breaks so a
run boundary inside a word is visible, and checks an OBJR through its own
target's /StructParent whatever the target is. ToUnicodeMap keys codes by
byte length, searches ranges by binary search and cannot throw on wide
ranges.

A form that carries MCIDs and is drawn more than once is reported as
MULTI-DO: ISO 32000-2 14.7.5.2 allows such a form into the structure only
by the Do-inside-a-sequence method. Each behaviour has a test on a
hand-built file.
The goldens now carry the text-line markers and per-page /Tabs the
hardened dumper prints, and each header lists every defect the golden
visibly shows (borders as bare artifacts, missing /BBox, split items).
Case 6 uses a smaller font so every run of the findings' paragraph sits on
one text line, which makes the extra space inside "astro logie" visible.

Case 10 records a single list item split across a page break: the
continuation becomes a second L whose LI has no Lbl. A table row taller
than a page is refused by the layout engine with an InvalidOperationException
instead of being split, so there is no table counterpart.

Two builds in one process must give equal dumps; whether that holds across
processes is what the Verify comparison and CI check.
Round-two review found that each round-one fix covered the sites it
named and left their siblings: some output lines were uncharged, some
document strings printed raw, problems were recognised by matching the
printed text, and integer fields were read three different ways.

Every line now goes through one Note or Problem call that charges a
single 8,000,000 character budget and, for a problem, increments the
counter the footer prints, so text that looks like a marker (a forged
name, a /Contents string) is no longer counted and cannot add a line.
Document text goes through one Safe (cut, then escape). Element paths
are computed from the parent chain, capped to their tail, instead of
stored. One ReadInt reports a real, negative or oversized integer as
BAD-* wherever an integer is required. A named property list that does
not resolve is a counted BAD-PROPERTIES.

ToUnicodeMap flattens its ranges into disjoint segments at first use
(first range in the file wins) and binary-searches them, and a CMap is
parsed once per document. A form with MCIDs painted inside an MCID
sequence is reported per ISO 32000-2 14.7.5.2.

The goldens are unchanged: none of this changes what the writer's output
prints.
The earlier complexity test put the wide range first, where first-wins
leaves a single segment and a linear scan costs nothing, so it could not
tell a scan from a binary search. With the wide range last there are about
100,000 segments, and a linear lookup fails the comparison bound.
The dumper is a test oracle that reads PDFs generated by this
repository's tests and bytes they write by hand. Its guarantee is
termination with a diagnosable result and bounded output, plus cheap
guards; the cost of pathological input is documented as out of scope.

Show text stops decoding once 200 characters are produced and reports the
codes left, and a /ToUnicode destination over 512 bytes (ISO 32000-2
9.10.3) is unmapped, so a 1-byte code expanding to 256 characters no
longer builds the whole string first. An element line prints at most 32
keys and no line passes 20,000 characters. Presence checks treat the
null object and a reference to nothing as absent, and revision numbers
in /A and /C are read as integers with BAD-REVISION on a bad one.

Tests: the allocation of a 4,000-code show string, a line with 7,000
keys, check lines that alone exceed the output budget, null and dangling
values, bad revision numbers. The goldens' first lines now list A11Y-04
and A11Y-17 (cases 4, 10), A11Y-09 (case 10) and A11Y-16 (cases 2, 8).
Comments are corrected: the ISO 32000-2 14.7.5.2 rule for forms, the
named-destination clause, and the resource fallback deviation.
An element line with 32 keys of 32 dictionaries of 32 arrays is about
80,000 characters before the 20,000 cap, and has no more than the 32 keys
the key cap allows, so only the line cap shortens it.
The caps paragraph said a name was cut at 64 or 512 characters and that
a capped value prints in at most 1,600; it now separates strings, keys
and printed names (200) from the names that label something (64), and
gives the real worst cases for a shown text and a path. The guarantee
now says the dumper may also end with an exception from the Reader.

Comments are corrected against ISO 32000-2: the form clause quotes the
sentence it paraphrases, the named-destination comment describes the
dictionary form of 12.3.2.4, the revision number comment gives the
reason a negative one is reported (14.7.6.3, NOTE 4), and the resource
fallback comment says when the page's and the invoker's resources
differ.

Tabs, Lang, MarkInfo, DisplayDocTitle and a GoTo action's /D now go
through the same absent-if-null helper as the other presence checks.

This branch has not been deployed

No deployments
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