Skip to content

feat(layout): add a {chapter} token for running bands - #681

Merged
Tim81 merged 5 commits into
mainfrom
feat/chapter-token
Nov 22, 2026
Merged

Tim81 merged 5 commits into
mainfrom
feat/chapter-token

Conversation

@Tim81

@Tim81 Tim81 commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Closes #603

What changed

A running band could print the page number and the page count and nothing that depends on the content. RunningBand.ChapterToken turns on a {chapter} token that prints the title of the chapter the page is in.

  • Opt-in. ChapterToken is false by default. A template that holds {chapter} without it prints that text, as 2.3.3 did, so no existing document changes. This is a new public member (PublicAPI.Unshipped.txt).
  • Which chapter. A chapter is a level-0 Heading. A page shows the first one whose first line is on the page, else the last one drawn on an earlier page, else no text where the token stands (the rest of the template is drawn as written). A heading that splits over a page break counts on the page it starts on, because only the piece that holds its first line is a heading renderer; the overflow is a plain paragraph renderer.
  • Title. BookmarkTitle when set, otherwise Text. A tab, line break, form feed or other control white space in it is drawn as a space, so none reaches glyph lookup. The title is copied as it is and never scanned again, so a {page} inside it prints as text.
  • Cost. Chapters are followed only when a band sets the property and its template holds the token, and only in the drawing pass, from the renderers it accepted. The measuring pass never reaches the tracker, so the page count and the number of layout runs are unchanged, and {chapter} alone never triggers a measuring pass. A document without the token pays one bool test per drawn element (DocumentRenderer.cs:1126) and one null test per finished page (:622).
  • Truncation. The title is measured and cut with the band's other text, reported through BandTruncations and BandTruncated. A character the band font lacks is reported as NotdefGlyph as for any band text.
  • The save refuses two titles, both documented on RunningBand.ChapterToken, Heading.BookmarkTitle and Document.Header/Footer: an unpaired surrogate in the title when the band font is embedded (ArgumentException, even when the heading is set in Helvetica), and a template that repeats {chapter} until the text passes int.MaxValue characters (InvalidOperationException; 250,000 tokens of a 10,000-character title throw, 200,000 save).
  • RunningBand.Resolve(int, int) knows no headings and leaves {chapter} as given; its docs say so. Document.Header and Footer docs name the token.
  • Headings of other levels and anything a custom IRenderer draws start no chapter (documented).

Deviations from the plan

  • RunningBand.ChapterToken is a new public property. The plan had no new public member; it is needed so that an existing template holding the text {chapter} keeps printing it literally.
  • The internal Resolve overload is dropped. BandText.Reset takes the chapter title beside the page label, and the compiled template carries the token.
  • Behaviour while the layout runs: chapters are followed in the drawing pass only, so a heading seen in the measuring pass starts no chapter. The docs make no claim about a band assigned to the document during a save.

2.3.3 against this build

Input 2.3.3 This build
Band template with {chapter}, ChapterToken unset text {chapter} printed same bytes
Band template without {chapter}, ChapterToken set n/a same bytes as unset (tested)
Three level-0 headings over six pages, header {chapter} | {page} n/a Hoofdstuk één: Inleiding | 1, ... | 2, then the second chapter on pages 3 and 4, the third on 5 and 6, read back with pdftotext -layout in an embedded font with non-ASCII titles
Page before the first level-0 heading n/a token replaced by nothing, the rest of the template stays
Chapter starting mid-page n/a that page shows the new chapter
Split level-0 heading n/a the page it starts on and the pages its overflow fills show it
Title wider than the band n/a cut, one BandTruncated

There is no deliberate output change: a document that does not set the property saves to the same bytes.

Differential

3,000 random documents saved on origin/main (2.3.3+2316dee) and on this branch (2.3.3+cc80703), assembly paths printed by both runs. Each had a random page size and margins, a header and footer from eight templates (some with {chapter} or a malformed one, ChapterToken unset), Helvetica or embedded DejaVu Sans, page-label ranges, mirrored margins, tagging, level 0 to 2 headings with page breaks, recto page breaks and paragraphs. SHA-256 of the saved bytes, truncation reports and diagnostics count: all 3,000 lines identical, including the 48 that throw (same exception type) and the 131 that report diagnostics.

Existing behaviour the title follows

A title glyph that the band font lacks is reported as NotdefGlyph (severity error) and fails veraPDF 6.2.11.8 under PDF/A-2u, as a literal template character the font lacks already does on main. The title inherits that, and this PR does not change it.

Tests

ChapterTokenTests (and one change to RunningBandTemplateTests, which iterated every entry of the token table and now leaves {chapter} aside). Release, REQUIRE_ORACLES=1, REQUIRE_VERAPDF=1: net10.0 and net11.0 each 1,963 passed, 0 failed, 2 skipped (Linux-only pipe tests). Release build 0 warnings; dotnet format --verify-no-changes, eng/clean-room-check.ps1 and generate-roadmap.py --check clean. The oracle tests run qpdf --check, pdftotext -layout page by page, and veraPDF 1.30.4 PDF/A-2u.

Mutations

Each applied in a detached worktree, built, and run against ChapterTokenTests and RunningBandTemplateTests.

Mutant Failing tests
a page shows the chapter in force at its top, not the first that starts on it 10, among them AChapterThatStartsMidPage_isTheOneThatPageShows
a heading's overflow piece counts as a chapter start ASplitChapterHeading_countsOnThePageItStartsOn_notWhereItsOverflowLands
chapters are followed in the measuring pass too WithTheToken_theTrackerSeesTheDrawingPassOnly_andTheLayoutIsUnchanged and 2 more
{chapter} always recognised WithoutChapterToken_theTextStaysLiteral_asItAlwaysDid
control white space kept in the title AControlCharacterInATitle_isDrawnAsASpace_andNeverReachesTheFont
level 1 headings start a chapter OtherLevels_startNoChapter_andABookmarkTitleIsUsedWhenThereIsOne
BookmarkTitle ignored 3, among them OtherLevels_...
the chapter in force is not carried to the next page 7, among them TheTwoPassesAgree_soAChapterAndThePageCountPrintTogether
the last chapter of a page wins over the first TheFirstChapterOnAPage_winsOverALaterOne
tracker always on WithoutTheToken_theTrackerIsNeverCalled
the int.MaxValue length guard removed ATemplateThatRepeatsTheTokenPastIntMaxValue_isRefusedAtTheSave_andOneBelowItSaves
an empty BookmarkTitle falls back to the heading text AnEmptyBookmarkTitle_isAnEmptyChapter_notTheHeadingText
no-break spaces become U+0020 ANoBreakSpaceInATitle_isNotReplacedByAnOrdinarySpace
tracker on for the header only AFooterAlone_holdsTheToken_whenTheHeaderDoesNot
headings of level 0 or below start a chapter ANegativeLevel_startsNoChapter (both cases)

Not done

  • No {section} token for level 1 headings.
  • An unknown brace text still prints as given and is not reported.
  • Band artifact tagging is as before.
  • A custom renderer contributes no chapter.

A handbook header wants the title of the chapter the page is in. The band
resolved only {page} and {pages}, so a caller had no way to get it.

RunningBand.ChapterToken turns on {chapter}. It is opt-in because 2.3.x
printed that text literally, and an existing template must keep its bytes.
The title is the first level-0 heading that starts on the page, else the
last one drawn earlier, else empty. It is followed in the drawing pass
only, from the renderers that were accepted, so the page count and the
number of layout runs are unchanged.
The title a band prints comes from Heading.BookmarkTitle, so a surrogate pair split in it, or a template repeating the token past int.MaxValue characters, makes a save throw from members that said nothing. Document both on the band, the heading and the header and footer. State which pages show which chapter, and add tests for the empty title, no-break spaces, a footer-only token and negative levels.
@Tim81
Tim81 merged commit a34a281 into main Nov 22, 2026
4 checks passed
@Tim81
Tim81 deleted the feat/chapter-token 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

Development

Successfully merging this pull request may close these issues.

Layout: a running header that shows the title of the current chapter

1 participant