Skip to content

feat(layout): tab stops with leaders in paragraphs - #653

Merged
Tim81 merged 14 commits into
mainfrom
feat/tab-stops-leaders
Nov 21, 2026
Merged

Tim81 merged 14 commits into
mainfrom
feat/tab-stops-leaders

Conversation

@Tim81

@Tim81 Tim81 commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Closes #298

What changed

Paragraph.TabStops takes a list of TabStop values. A tab character in the text then places the
text after it at the first stop to the right of the end of the text before it. Without stops (null
or empty) a tab is a space, as before.

New public API (Layout, PublicAPI.Unshipped.txt):

  • TabStop(double position, TabAlignment alignment = Left, TabLeader leader = None) with
    Position, Alignment, Leader and an init-only DecimalCharacter (default '.').
  • TabAlignment (Left, Right, Center, Decimal) and TabLeader (None, Dots, Dashes,
    Rule).
  • Paragraph.TabStops (IReadOnlyList<TabStop>?, init-only, copied by the setter).
  • LayoutDiagnosticCode.TabStopBeyondWidth = 106, severity Error.

The rule

  • The text before a tab is followed by one U+0020, and that space counts in the pen. ISO 32000-2
    14.8.2.6.2 requires the white space that separates words to be present in tagged text; without it
    a structure-order extractor reads Inleiding12. It is written whether or not the document is
    tagged, so output does not depend on Tagged.
  • Left starts at the stop, Right ends at it, Center centres on it, Decimal puts the first
    DecimalCharacter at it (text with none ends at the stop).
  • A stop the pen has reached is skipped (a stop at the pen is not to its right). Text that would
    start left of the pen starts at the pen.
  • Every tab with no stop to its right advances the pen by one space. The space written before the
    tab is that advance when text comes straight before it; a tab after another tab, or at the start
    of a line, is drawn as a space of its own. A stop at position 0 is therefore never used (a tab
    looks for a stop greater than the pen); it is accepted and documented as "Do not pass".
  • Text at a right, centre or decimal stop that does not fit after the text before the tab: the
    paragraph checks whether it fits at the same stop on a fresh line. If it does, the line ends
    (the separator space is dropped when the line ends straight after it) and the text is placed
    there, its leader running from the left edge. If it fits at its stop nowhere, the line does not
    end: the text is ordinary text from the pen, so a centred 1 at the right edge draws as
    Inleiding 1. Text at a left stop needs room for its first word only, starts at the stop, wraps
    word by word with continuation lines at the left edge, and when its first word does not fit at
    the stop is ordinary text from the pen with no leader.
  • When no stop lies to the right of the pen with the separator space counted, the stop the text
    itself stayed short of is still the stop for the tab; when one does, the tab goes to that one.
    A Left stop follows the same fresh-line check as the others when its first word does not fit
    after the text before the tab.
  • A line with a tab placed at a stop starts at the left edge whatever the alignment, and is not
    justified; a line whose tabs use no stop follows the alignment like any other.
  • The segmentation is part of the cached wrap, so the measuring run of a document with a footer and
    the second half of a split paragraph reuse it.

Leaders

A leader is a stroked path drawn after the paragraph's text object, not glyphs, so extractors do not
print it. Per leader: /Artifact BMC (tagged documents only), q, r g b RG of the text the
leader leads to, cap, dash, width, m, l, S, Q, EMC. With size s:

Kind Cap Dash Height
Dots 1 J [0 0.5s] 0 d (zero-length dashes with round caps, ISO 32000-2 8.5.3.2) baseline + w/2
Dashes 0 J [3w 2w] 0 d baseline + 0.25s
Rule 0 J [] 0 d baseline + 0.25s

Line width w = 0.08s. The leader starts and ends 0.25s clear of the texts and is left out when
shorter than 0.5s. Dot positions are multiples of 0.5s from the content edge, so dots of consecutive
lines line up. The leader takes its size and colour from the first piece of the text after the tab
(from the run holding the tab when nothing follows). In a tagged document it is an artifact outside
the paragraph's BDC ... EMC, so it stays out of the structure tree (ISO 14289-1 7.1).

Refusals

Input Result
TabStop position NaN, infinite or negative; undefined alignment or leader ArgumentOutOfRangeException from the constructor
DecimalCharacter white space, control or surrogate ArgumentException from the setter
TabStops with a null stop, or positions that do not strictly increase ArgumentNullException / ArgumentException from the setter
A stop beyond the content width of a top-level paragraph (page width less document and paragraph margins), by 5e-6 points or more InvalidOperationException from the save, before any layout; the message prints both numbers with R when they format alike. The document stays saveable: widening the page lets the retry save once
A leader stop in a paragraph with a run whose font size is zero or less, or so small that the leader's line width or dash lengths would be written as 0 InvalidOperationException before any layout for the document's own paragraphs, and from ParagraphRenderer.Layout for a renderer of the caller's (so [0 -6] 0 d or 0 w is never written)
A ParagraphRenderer laid out by a custom renderer at a width narrower than a stop, whose text holds a tab in a styled run The stop is treated as absent and reported once as TabStopBeyondWidth (from the piece that starts at line 0 only); strict mode refuses it. A tab then uses the next stop within the width, and is one space only when none remains, including when every stop lies beyond the width: such a paragraph still takes the tab path with an empty stop list, so the one-space rule holds. A paragraph with no TabStops takes the unchanged plain path, so its bytes are as before

A stop that lies past the width by less than 5e-6 points, the scale the file is written at, is the
right edge, and a segment overflows only when it ends 5e-6 points or more past the width: PageSize.A4 with EdgeInsets(42.52) and a stop at Width - 42.52 - 42.52 differ from
the library's Width - (42.52 + 42.52) by one rounding step. Such a stop is placed at the width.

The stop and leader-size checks run in DocumentRenderer.RunLayout, before HasStartedLayout is
set. The size threshold is 5e-6 / 0.08 points, the smallest size at which the leader's line width
is written as non-zero by PdfCanvas; it is checked on every run of the paragraph.

Existing documents

New API only. A paragraph with no stops, or with an empty list, takes the unchanged wrap path, and
the tokenisation change only acts when stops exist. The existing golden and byte-pinning tests in
VellumPdf.Layout.Tests pass unchanged, and WithoutStops_aTabIsASpace compares a Title\t12
paragraph with and without an empty list against the Title 12 paragraph. No deliberate output
change.

Tests

TabStopTests (93 cases). Positions are computed from font metrics and the constants above, not
from the layout code.

Run net10.0 net11.0
VellumPdf.Layout.Tests, Release, whole project, merged with main 1,507 total, 2 failed, 2 skipped (Linux-only pipe tests) 1,507 total, 2 failed, 2 skipped

The 2 failures per TFM are RunsHeadingListItemGoldenTests.EmbeddedHeadings_pinTheBytes and
EmbeddedLists_pinTheBytes. They fail the same way, with the same computed hashes, on origin/main
without this branch (the pins from #654 meet the font change from #656); a re-pin PR is in flight.
Every other test passes, including all TabStopTests.

Run with the external tools required (qpdf 12.4.2, veraPDF 1.30.4, poppler). Oracles in
TabStopTests: qpdf --check ("No syntax or stream encoding errors found") on dots, dashes and
rule, tagged and untagged; pdftotext -layout prints Inleiding and 12 apart and nothing but
letters, digits and white space (dots, dashes and rule leaders), and -raw on a tagged page keeps
the words apart. Each oracle test first asserts that the fixture holds two leaders and the
(Inleiding ) Tj show string; veraPDF 1.30.4 2u with an embedded font
and leaders.

Mutations

Each applied to a detached worktree, built, run against TabStopTests and RunBoundaryTests,
then restored.

Mutant Failing tests
Tab read as a space even with stops 23 (every placement test)
Dot grid not anchored 2
Leaders drawn as glyphs 10
Butt caps for dots 2
Justification left on for tab lines 1
No space before a tab 9
No q/Q around the leader 10
Wrap redone on every layout when stops exist (stands in for segmenting in Draw) 1 (WrapCalls == 1 with a footer)
Stop lookup by linear scan 2
A stop at the pen counts as to its right 4
No overlap rule 2
Trailing space not part of the pen 5
No beyond-width refusal 3
No leader size refusal 1
Decimal point ignored 2
Stops beyond the width kept 3
Leader not an artifact when tagged 1
Leader shorter than a period still drawn 1
Tab line placed by the paragraph alignment 1
Diagnostic not reported 3
No written-scale tolerance in the save check 2
No written-scale tolerance when the wrap drops stops 2
Leader size checked only before layout 3
Empty fragment only with a leader 3
Segment dropped to the left edge instead of placed again at its stop 1
Separator space kept on a line that ends at a tab 4
Tabs in unstyled runs counted for the report 1
Stop not clamped to the width 2
Fit test without the written-scale tolerance 2
Stop not looked up from the pen before the separator space 1
Carried segment starts from the pen-based stop 1
Unfit text always ends the line 3
All-beyond stops fall back to the plain wrap 1
Clamp skipped for Decimal stops 1
Next-line placement only for Right and Left stops 2
Report from every split piece 1
Fresh-line check without the tolerance 1
Fresh-line check skipped for Decimal 1
Stop before the space always preferred 3
Next-line placement not for Left stops 1

Not done

  • Leaders do not carry /Type /Layout (ISO 19005-2 6.7.3.1 asks for layout artifacts at Level A, as
    a "should"); the typed artifact needs a property list the canvas does not write yet.
  • No raster check of the dots, and no veraPDF 2a run: whether its 2a profile checks word
    separation was not checked, so a green run would not show the separator. The value-level test
    does.
  • Tab stops in table cells, list items and headings are not read; only Paragraph holds them.
  • One very long line builds its fragment text by repeated string concatenation, which is quadratic;
    not changed here.
  • A page reference after a tab (a plan-dependent paragraph) needs no change here: it drops the
    cached wrap and the segmentation is rebuilt with it.

A tab in a paragraph was always drawn as a space, so a table of contents
or a figure column could not be built with Layout. Paragraph.TabStops now
places the text after a tab at a left, right, centre or decimal stop, and
fills the gap with a dot, dash or rule leader.

A leader is a stroked path, not glyphs, so it stays out of extracted text
and, in a tagged document, is an artifact outside the paragraph. The text
before a tab ends in a space so words stay apart for readers. Stops beyond
the content width and leaders at an unwritable size are refused before
layout; a renderer laid out narrower than a stop reports a diagnostic.
Paragraphs without stops are drawn as before.
Tim81 added 13 commits November 21, 2026 11:19
… rule

A right, centre or decimal segment that did not fit after the text before
its tab was set as ordinary text from the left edge, losing its stop. It
now goes to the next line and is placed again at the same stop, with its
leader running from the left edge.

A tab landing on a stop with no leader no longer vanishes when another
tab follows with no stop to its right: the line always records where the
first tab put the pen, and every stopless tab is one space. A line whose
tab text moved on drops the separator space, and only a tab placed at a
stop takes the line off the paragraph alignment.

A stop at the content width is accepted when two ways of computing the
width differ by a rounding step. The leader-size refusal also runs when a
ParagraphRenderer builds its wrap, so a renderer of the caller's cannot
write a negative or all-zero dash array.
# Conflicts:
#	src/VellumPdf.Layout/PublicAPI.Unshipped.txt
#	src/VellumPdf.Layout/Rendering/ParagraphRenderer.cs
…topless tabs

A right stop at the width could drop its number to the next line when two ways of summing the same widths rounded one step apart. The fit test now uses the written-token scale. A segment that fits at its stop nowhere no longer ends the line; it is ordinary text from the pen. A stop the separator space carried the pen past is still found. A paragraph whose stops all lie beyond a custom renderer's width keeps the one-space rule for its tabs.
# Conflicts:
#	src/VellumPdf.Layout/Rendering/ParagraphRenderer.cs
@Tim81
Tim81 merged commit 5aea9ff into main Nov 21, 2026
4 checks passed
@Tim81
Tim81 deleted the feat/tab-stops-leaders 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: tab stops with leaders

1 participant