Skip to content

fix(layout): bookmark a split heading where it starts, and let a heading bookmark be closed (#464, #609) - #637

Merged
Tim81 merged 4 commits into
mainfrom
fix/heading-split-bookmark
Nov 20, 2026
Merged

Tim81 merged 4 commits into
mainfrom
fix/heading-split-bookmark

Conversation

@Tim81

@Tim81 Tim81 commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #464
Closes #609

Three commits: one per issue, then a follow-up that corrects the documentation and adds two tests after review. They share a PR because both change the one outline call HeadingRenderer.Draw makes.

#464: a split heading's bookmark

HeadingRenderer.Layout stores the box it was placed in on this, then returns a new HeadingRenderer for the split piece. That copy was built without the box, so its Draw passed layout y 0 to AddOutlineEntry, and ToPdfY(0) is the page height: the bookmark pointed at the top of the page, above the heading. The split copy now carries the box, as ParagraphRenderer's split does.

Sweep of every LayoutResult.Partial site (git grep -n "LayoutResult.Partial(" -- src at 2027421), against the fields each Draw reads:

Site Fields Draw reads Split copy sets them
ParagraphRenderer.cs _para, _startLine, _endLine, _lines, _lineHeight, _occupied, StructType, ParentStructElem, ElementLanguage, RunOwner all
HeadingRenderer.cs _heading, _inner, _occupied _heading, _inner; _occupied was missing (this fix)
ListRenderer.cs (two sites) _list, _items, _startItem, _endItem, _partialContentOverrideAtStart, _occupied, _offered all, at both sites
Table/TableRenderer.cs _table, _startRow, _rowHeights, _occupied (the split also copies _colWidths) all

Deliberate output change (CHANGELOG, ### Fixed): only the /XYZ top value of the bookmark of a heading whose split piece does not start at layout y 0. A split piece that starts above y 0, through a negative document or heading top margin, is part of the change: on a page 300 high with a document top margin of -20 the top moves from 300 to 320. The byte offsets that follow the value move too. Nothing else changes: page count, content streams, titles and exceptions are equal.

#609: Heading.BookmarkExpanded and a four-argument AddOutlineEntry

  • Heading.BookmarkExpanded { get; init; }, default true.
  • DrawContext.AddOutlineEntry(string title, int level, double layoutY, bool expanded). The three-argument overload keeps its behaviour and forwards to an internal core with expanded: true.
  • The new overload refuses a null title (ArgumentNullException) and a destination whose Y is not finite (ArgumentOutOfRangeException) at the call (new API refuses non-compliant input from the start). Its refusals are raised from the Draw of the renderer that calls it, which Document runs inside its save, so a throw leaves that document unable to be saved again. It has its own remarks and exception tags, including the null-page ArgumentNullException raised from the save (ParamName is key).
  • The children rule is stated as BuildOutlineTree applies it: level 0 is always top-level, any other level goes under the most recent earlier entry at exactly one level less, and top-level when there is none. So a level -1 entry never gets children, and BookmarkExpanded = false on it writes no /Count (tested).
  • HeadingRenderer.Draw calls the internal core, not the validating overload, so every heading keeps its exception type, message and timing.
  • PdfOutlineEntry.IsExpanded and BuildOutlineTree already wrote a negative /Count for a closed entry; no Kernel change. An entry without children has no /Count, as ISO 32000-2:2020 Table 151 requires it only for an item with descendants, so the value is not written there. Heading.BookmarkExpanded documents that as the rule for when it takes effect.

Evidence

Differential runner against 2.3.3 and the base commit (2027421), then the PR head (869d360). Both assembly informational versions are printed in each run's versions.txt: 2.3.3+1c61fa6... for 2.3.3 and 2.3.3+<head sha> for the head builds. The probe also prints each Layout DLL's path and md5.

Comparison Result
2.3.3 vs base 0 violations
2.3.3 vs PR head 0 violations. Verdicts equal the base run's row for row (28,735 rows) except 35 rows classified DELIBERATE-464 (34 probe rows, among them the two negative-margin rows, and 1 fuzz row, all holding a split heading), plus one cancel-scan row whose verdict depends on timing
base vs PR head, rows\*.txt 25,922 rows, 176 differ: 1 fuzz row (only file=, skel=, tfile=, tskel= and odest= differ; pages=, content=, norm=, ttl= equal); 43 probe rows (the split-heading rows, and the rows for the new members, which the base lacks); 1 inv row, L2.retry.objstmEncrypt-then-fixed, an encrypted file whose bytes change from run to run (its file= and content= hashes also differ between two runs of 2.3.3, in `main
ows\inv.txt of the base and PR-head runs); and 131 rows from the timing-driven cancel scans and the rows that print the run's output directory (r6a-cancel, r6c-cancel, r6a-links, r6a-hidden, r6a-foreignfonts`)

The runner gained an odest= field (the outline /Dest arrays in object order) and a DELIBERATE-464 rule: the file with every outline destination top blanked has an equal skeleton, the plan holds a heading, and each changed top was the page height on the 2.3.3 side (read from the file's /MediaBox) and is something else on the PR side, in either direction. The two negative-margin rows classify as DELIBERATE-464 and nothing else moved between the earlier run and this one except that timing row.

Probe rows (page 200x300, margins 10, a filler paragraph of 120 words, then a heading; all on 2.3.3, base and PR head):

Input 2.3.3 and base PR head
Heading that fits p1@30.8 p1@30.8
Heading that splits p1@300 (page top) p1@30.8 (equals the control's top)
Split piece starts at layout y 0 (margin 0) p1@300 p1@300
Split piece starts above y 0: document top margin -20, or heading top margin -30 p1@300 p1@320
Heading gets Nothing, moves to page 2 (sweep 19 to 22) p2@290, p2@275.6, ... identical
Heading splits on page 1 after N one-line paragraphs (sweep 4 to 18, 23 to 37) p1@300 smaller top, on the split piece
Header and footer set, split heading p2@300 p2@242.8 (equals the fits row)
Level-0 BookmarkExpanded = false with two level-1 headings n/a /Count -2
Collapsed, no children n/a /Count absent: bytes equal to expanded (byte-pinned in a test)
Level -1 collapsed, then a level-0 heading n/a two top-level items, no /Count -, root /Count 2
Collapsed, followed only by a level-2 heading n/a no /Count -; the level-2 entry is top-level
Collapsed split heading with a child n/a one entry at the split piece, /Count -1
Custom renderer, three-argument overload, with a child expanded, /Count 1 on the parent identical
Three-argument overload, null title NullReferenceException from the save identical
Three-argument overload, NaN Y ArgumentException (PDF does not support NaN) identical
Four-argument overload, null title / NaN Y n/a ArgumentNullException / ArgumentOutOfRangeException from the save; the next save says it cannot be saved again
Heading at an overflowing destination Y (page height and top margin at the extremes of double) ArgumentException, PDF does not support NaN or Infinity identical type; the message prints the operand as ∞ on 2.3.3 and as Infinity on the base and the PR head (the cause was not investigated; base and PR head agree)
Heading whose run holds an unpaired surrogate in an embedded font ArgumentException identical
Retry after a heading-bearing save failed during layout InvalidOperationException: boom again "cannot be saved again", as on the base (the retry rule)

A NaN top margin is not a route to a non-finite heading position: the document refuses it as "too tall" before any heading draws. The overflowing-destination row above is the input that reaches the outline write.

Tests

OutlineBookmarkSplitTests (16 tests, in tests/VellumPdf.Layout.Tests). The split cases parse the outline item's /Dest array and assert the exact numbers against the same-filler control; one case sweeps 1 to 40 preceding paragraphs and asserts the bookmark never sits above the top margin, and that both the "split on page 1" and "moved to page 2" shapes occur. One test pins collapsed-without-children bytes through DocumentRenderer with a fixed timestamp and document id. One test runs qpdf --check ("No syntax or stream encoding errors found"), reads the item back with qpdf --json --json-key=outlines, and checks pdftotext on the page the bookmark names.

Project (Release, oracles required, veraPDF 1.30.4, qpdf 12.4.2) net10.0 net11.0
Layout.Tests 1055 passed, 0 failed, 2 skipped 1055 passed, 0 failed, 2 skipped
Kernel.Tests 2356 passed, 0 failed 2356 passed, 0 failed
Conformance.Tests 1453 passed, 0 failed 1453 passed, 0 failed

Also clean: Release build of the solution (0 warnings), dotnet format --verify-no-changes, eng/clean-room-check.ps1, python eng/generate-roadmap.py --check.

Mutations

Each run on the committed head, with git diff --stat and a -U0 diff shown before the build, the Layout DLL rebuilt, and the sources restored with git checkout -- src.

Mutant Failing tests
Split copy built with default(LayoutBox) (revert the #464 fix) SplitHeading_bookmarkIsAtTheSplitPieceNotThePageTop, Heading_movedToTheNextPageOrSplit_neverTargetsAboveTheTopMargin, SplitHeading_collapsed_hasOneEntryAtTheSplitPiece, SplitHeadingStartingAboveLayoutYZero_hasItsBookmarkAboveThePageHeight; the y-0 control passes
Core ignores expanded (IsExpanded = true) CollapsedParentWithChildren_writesNegativeCount_andRootCountsVisibleItems, FourArgumentOverload_collapsed_writesNegativeCount, SplitHeading_collapsed_hasOneEntryAtTheSplitPiece
Three-argument overload passes false ThreeArgumentOverload_staysExpanded_withChildren
HeadingRenderer.Draw calls the validating overload HeadingAtANonFinitePosition_isRefusedByTheWriterWithArgumentException (the exception becomes ArgumentOutOfRangeException)
Null check dropped from the four-argument overload FourArgumentOverload_refusesNullTitle_andTheDocumentCannotBeSavedAgain

Not done

  • The runner covers net10.0 only.
  • The collapsed state is not carried to a custom renderer through Heading; a custom renderer uses the four-argument overload.

)

The piece of a split heading that stays on the page is the one that draws
the bookmark, but it was built without the box it was placed in, so the
entry targeted layout y 0, the top of the page, above the heading. The
split copy now carries that box, as the paragraph renderer's split does.
Heading.BookmarkExpanded and a four-argument DrawContext.AddOutlineEntry
carry the entry's open or closed state to the outline writer, which
already honours it. The new overload refuses a null title and a
non-finite destination at the call; headings go through an internal core
so their refusals stay as they were.
#609)

Write the four-argument AddOutlineEntry's own remarks instead of
inheriting the three-argument overload's, which said a null title and a
non-finite position are accepted. Name the level -1 case, where level 0
is always top-level and the entry never gets children, and add the null
page tag. Cover a split piece that starts above layout y 0 and the
level -1 case in tests, and word the #464 entry to match.
…kmark

# Conflicts:
#	CHANGELOG.md
#	src/VellumPdf.Layout/PublicAPI.Unshipped.txt
@Tim81
Tim81 merged commit 39e5fe0 into main Nov 20, 2026
4 checks passed
@Tim81
Tim81 deleted the fix/heading-split-bookmark branch November 21, 2026 06:37
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