Skip to content

feat(layout): let a Preformatted block carry an Anchor - #694

Merged
Tim81 merged 4 commits into
mainfrom
feat/preformatted-anchor
Nov 23, 2026
Merged

Tim81 merged 4 commits into
mainfrom
feat/preformatted-anchor

Conversation

@Tim81

@Tim81 Tim81 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Closes #693.

What changed

Preformatted.Anchor { get; init; } gives a code block the same named destination the other
elements already have, so a code listing or command block can be the target of an internal link,
a page reference or file.pdf#nameddest=name.

  • The name rule and the refusals are Paragraph.Anchor's: the init accessor goes through
    AnchorNames.Validate, so an empty name, one longer than 127 characters, or one with a character
    outside A-Z a-z 0-9 . _ - : throws ArgumentException (ParamName value), with the same
    message.
  • PreformattedRenderer implements IPlacementSource and reports the anchor through
    PlacementSink.ReportAnchor, as the paragraph, table and list renderers do. Only the piece that
    starts at line 0 reports it, so a block that splits across pages reports once, on the page of its
    first line.
  • Destination: the top edge of the block's first line, below the block's top margin, at the left
    edge of the page, [page /XYZ 0 top null]. It is the occupied box's Y, the same value
    ParagraphRenderer reports for a paragraph (its occupied box has the top margin already
    deducted).
  • NavigationPlan.Build walks PreformattedRenderer, so a name held by a block and by any other
    element is refused by the save before any layout, naming both ("Paragraph 0 and Preformatted 1"),
    and the document stays saveable. The same instance added twice holds its anchor twice.
  • The type's remark "The element is not a target of an anchor" is removed; the new property carries
    its own boundary documentation: a bold refusal sentence and its own <exception> tags, which
    list the causes a block can have (see below).
  • PublicAPI.Unshipped.txt gains the getter and the init accessor. CHANGELOG entry under
    Unreleased, Added.

Paragraph.Anchor's ArgumentException tag gains one cause it was missing, a page taller than
3.403 x 10^38 points (doc-only change).

No other renderer, option or default changed. Document.Add(IRenderer) is unchanged because
PreformattedRenderer is internal.

Refused set

Input Result
a name outside the rule ArgumentException from the init accessor, same message as Paragraph.Anchor
a name held by this block and another element InvalidOperationException from the save, before any layout; the document stays saveable
the same block instance added twice the same, naming both positions
a page taller than 3.403 x 10^38 points, anchored block ArgumentOutOfRangeException naming top from the save, after layout started, so the document cannot be saved again

The last row is not new behaviour of a block: a paragraph with an anchor does the same. Measured
with a 300 pt wide page and one block (or one paragraph) with an anchor: a page height of
3.403 x 10^38 saves, the next double above it throws, 3.5e38 and 1e39 throw, and the same block with
no anchor saves at 3.5e38 and 1e39. A block's Margins refuse negative and non-finite edges, so
they cannot cause it; the <exception> tags on Preformatted.Anchor say so and name only the
causes a block can have. Document.Add(Preformatted) still does not mention the repeated-anchor
refusal, as none of the other Add overloads does; the Save tags carry it.

Nothing that saved before is refused: the property is new, and a block without an anchor takes none
of these paths.

Byte comparison against 215ba05

A probe program built once against a detached worktree of 215ba05 and once against this branch
(both with Timestamp and DocumentId fixed, so no normalisation is applied), five documents that
hold Preformatted blocks and no anchor. All five compare equal with cmp:

Document Bytes SHA-256 (first 16)
70 lines with tabs, split over 4 pages, after a paragraph, plus a second block 3,337 113e123c0ae9f7cc
long lines wrapped with the continuation marker, margins, TabWidth 4 3,614 c959075237a58214
PDF/A-2u, embedded monospace font, split over 6 pages, BreakBefore 34,101 335c47f5457acc6e
tagged, with header and footer, heading, list, table and a recto break 6,910 1207bc3ca03a1f7e
PDF/A-2a tagged, 65 lines split 33,322 4e53a2b07414b53f

A sixth document, with Anchor set, is built only under the branch and its file holds /Dests;
that confirms the branch build under test contains the change. Documents with no anchor hold no
/Dests in either build.

Limit of the claim: these are five documents, not every input. A block with no anchor reports
nothing (ReportPlacements returns without reporting, and NavigationPlan.Add returns on a null
name). When another anchor or a labelled break is in the document, the renderer still calls
ReportPlacements for each block piece, which reports nothing for it; seven of thirteen further
documents compared byte for byte during review took that path, all identical.

Tests

PreformattedAnchorTests (14 tests, each on net10.0 and net11.0):

  • the /Dests name tree holds the name and its array has the page object and the top derived from
    the geometry: top of the content box, below a filler paragraph, below a 7 pt top margin, on the
    second page after a full page, and after BreakBefore;
  • a split block (3 filler lines, then 50 lines over 3 pages) reports once, on page 0 at
    20 + 3 x 12, counted from the draw plan and read back from the file;
  • pdfinfo -dests resolves the name to the expected page and top (the by-name check; Layout has no
    link-by-name member yet (Layout: internal links and destinations, not just URIs #299), although the Kernel has PdfLinkAnnotation.DestName and
    PdfDocument.AddNamedDestination, and neither the Reader nor qpdf --json resolves named
    destinations);
  • every name the rule refuses throws the same type, parameter name and message as
    Paragraph.Anchor;
  • the anchor is reported under the element, so a plan target for the element resolves to the page
    and top of the first line (TryGetTarget on the element);
  • a labelled PageBreak followed only by an anchored block saves, and the destination is on the new
    page;
  • a duplicate across a paragraph and a block, and across a block and a list item, is refused before
    layout and a second save meets the same refusal; the same instance added twice.

Mutations

Run in a detached worktree at the commit, one at a time, source restored after each. The first five ran against the 12 tests of the first commit, the last two against the 14 now.

Mutant Failing tests
anchor never reported (false && in ReportPlacements) 7 of 12: all the destination tests and the pdfinfo test
reported by every piece (_startLine == 0 removed) 1 of 12: the split-block test
wrong y (box moved 5 pt down) 7 of 12: all the destination tests and the pdfinfo test
validation skipped (init => _anchor = value) 1 of 12: the refusal-parity test
block left out of NavigationPlan 10 of 12: the destination tests, both cross-element duplicate tests and the added-twice test
anchor reported under the renderer (this) instead of the element 1 of 14: the report-key test
continue in place of break in the new NavigationPlan case 1 of 14: the labelled-break test

Gates

Run after the last code commit (a CHANGELOG rewrap followed), on a single machine, Release, REQUIRE_ORACLES=1:

  • build, 0 warnings
  • VellumPdf.Layout.Tests on net10.0 and net11.0 together: 4,950 run, 4,946 passed, 0 failed, 4 skipped
  • dotnet format VellumPdf.slnx --verify-no-changes
  • pwsh eng/clean-room-check.ps1

Removed in 3.0

Nothing: no compatibility device was added.

A code listing or command block could not be the target of a link, a page
reference or a named destination, unlike a paragraph, heading, list,
table or image. Preformatted.Anchor takes the name rule, the refusals
and the destination of Paragraph.Anchor, and joins the document's anchor
walk, so a name held by a block and by another element is refused before
any layout. Only the piece of a split block that starts at line 0
reports it, so the destination is on the page of the first line.

A block with no anchor saves to the same bytes as before.
@Tim81 Tim81 added enhancement New feature or request area/layout high-level document/layout engine labels Nov 23, 2026
…raph

Preformatted.Anchor inherited the exception tags of Paragraph.Anchor,
which list causes a block cannot have: its Margins refuse negative and
non-finite edges, and every block is drawn. The one cause that can
happen was missing: a page taller than 3.403 x 10^38 points makes the
save throw ArgumentOutOfRangeException naming top. The block now has its
own tags, and Paragraph.Anchor names the same page-height cause. The
case-sensitivity note is a plain statement, not a deferred refusal, and
the changelog entry no longer reads as if a valid name throws.
The anchor must be reported under the element, since a page reference
resolves its target by that object. A labelled page break followed only
by an anchored block must save, which depends on the plan clearing the
pending break when it meets a block.
@Tim81
Tim81 merged commit 73b6c60 into main Nov 23, 2026
4 checks passed
Tim81 added a commit that referenced this pull request Nov 23, 2026
@Tim81
Tim81 deleted the feat/preformatted-anchor 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

area/layout high-level document/layout engine enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Layout: Preformatted has no Anchor, so a code block cannot be a link target

1 participant