Skip to content

fix(layout): name the input a renderer refuses, instead of the page size - #497

Merged
Tim81 merged 2 commits into
mainfrom
fix/layout-validation
Sep 12, 2026
Merged

Tim81 merged 2 commits into
mainfrom
fix/layout-validation

Conversation

@Tim81

@Tim81 Tim81 commented Sep 12, 2026 •

Copy link
Copy Markdown
Owner

Five inputs, in two groups. Neither group touches a document that renders correctly today, which is
what keeps this inside the patch. Closes #478 and #481.

Input Before After
Font size of NaN "An element is too tall to fit on a single page" "A paragraph run has a font size of NaN, which cannot be laid out"
ColSpan of 0 the same "Table row 0, cell 0 has a ColSpan of 0"
Image width or height of 0 saved a document a reader cannot render refused, naming the extent
Image width or height non-finite the same refused, naming the extent

Two different defects, not one

Three refusals described something else. A renderer that cannot place an element reports placing
nothing, and DocumentRenderer reads that as "this element can never fit" and raises the too-tall
message. For a genuinely oversized element that is the right answer. For a font size that is not a
number, or a cell covering zero columns, it is not: the element is not too tall, and the remedy the
message offers cannot help. The exception type and the site are unchanged, so only the wording moves.

Two more saved silently and wrote invalid content. Read off the emitted stream:

Width = 0    ->  0 0 0 0 10 390 cm        singular, cannot be inverted
Width = NaN  ->  NaN 0 0 NaN 10 NaN cm    NaN is not a PDF number

ISO 32000-2 leaves an uninvertible matrix undefined for a painted XObject. A zero width also
collapses the height, because the height is derived from the width through the aspect ratio and the
height check passes trivially at zero. There was no message to correct here, because there was no
message.

The boundary was measured, after the first attempt got it wrong

My first version rejected whole classes: any non-positive font size, any non-positive extent, any
span below one. That broke OffPagePlacementTests.Image_negativeDeflatedWidth_emitsTheMirroredMatrixUnclamped,
which exists to record that a negative extent renders as a mirrored image on purpose, decided in
an earlier pull request. The failing test was right and the change was wrong.

So each neighbouring input was rendered and its content stream read, rather than reasoned about:

Input Emits Verdict
Font size 0 or negative /F1 0 Tf valid operator, renders
Leading non-finite 1 0 0 1 10 380 Tm valid matrix, renders
RowSpan below one — renders; nothing derives the column count from it
Image width negative -40 0 0 -40 10 430 cm valid matrix, mirrors the image
Image width positive infinity — clamped to the content box by #472 before anything sees it

All five keep rendering. Refusing any of them would turn a working document into an exception, which
this release defers to the next major, and section (c) of the new test file pins that they were left
alone. The positive-infinity case is the one I would not have predicted: it never reaches validation
because the over-wide clamp fires first.

Evidence

  • Ten known-answer cases, asserting the exact message rather than that some exception was raised.
  • Discrimination by mutation in a detached worktree. Removing every validation call fails nine of
    the ten. The tenth is the guard that the neighbours still render, which passes either way by
    design, since it exists to catch over-reach rather than to prove the fix.
  • Gates. Layout 357, Kernel 1445, Conformance 1300, Reader 1869, no failures in any, Release.
    Format and clean-room clean.

What is not here

#476, a list indent at or beyond the content width, which discards every item's text and keeps
the marker. Measured on 5e4c93d: a 180pt content box keeps the text at an indent of 179 and loses
it at 180. It is left out because reporting a silent drop needs a diagnostics channel, the two that
exist are narrow one-offs, and inventing a third inside a patch is the decision #496 was filed to
prevent. It is content loss rather than a wrong message, so it does not belong with these.

Five inputs, in two groups, neither of which touches a document that
renders correctly today.

Three were refused by a message about something else. A renderer that
cannot place an element reports placing nothing, and DocumentRenderer
reads that as "this element can never fit" and raises the too-tall
message. For an oversized element that is the right answer. For a font
size of NaN or a ColSpan of zero it is not: the element is not too tall,
and the remedy the message offers, reducing its content or enlarging the
page, cannot help. The exception type and the site are unchanged, so
only the wording moves (#481).

Two more saved without complaint and wrote content a reader cannot
render. A zero image extent emitted "0 0 0 0 10 390 cm", a singular
matrix ISO 32000-2 leaves undefined for a painted XObject; a non-finite
one emitted "NaN 0 0 NaN 10 NaN cm", where the token is not a PDF number
at all. A zero width also collapsed the height, since the height derives
from the width through the aspect ratio and the height check passes
trivially at zero (#478).

The boundary was measured rather than assumed, and the first attempt got
it wrong. Rejecting a whole class at once broke the mirrored-image case
in OffPagePlacementTests, which exists to record that a negative extent
renders on purpose. Reading the emitted stream for each neighbouring
input settled it: a font size of zero gives "/F1 0 Tf", a valid
operator; a non-finite leading gives a valid text matrix; a RowSpan
below one renders; a negative width gives "-40 0 0 -40 10 430 cm", a
valid matrix that mirrors; and a positive-infinity width is clamped to
the content box by the off-page fix before anything else sees it. All
five keep rendering, and a test pins that, because refusing any of them
would turn a working document into an exception and belongs in the next
major with the rest of the contract tightening.

Nine of the ten new cases fail when the validation is removed. The tenth
is the guard that the neighbours still render, which passes either way
by design.

Refs #478, #481
Four reviewers, four highs between them, all mine. This closes every
medium and high, plus the lows worth acting on.

The extent guard tested the wrong thing. PdfCanvas formats coordinates
to five decimals, so a width of 4e-6 was written as 0 and produced the
identical singular matrix the guard's own comment quoted as the thing it
refuses. It now tests the written token, with the boundary measured:
5e-6 writes 0.00001, 4.9e-6 writes 0.

The running band was the one path to a font operator that reaches no
renderer, so it reached no validation either, and saved documents
containing /F1 NaN Tf. It had no test in any suite. The comment in
DocumentRenderer had already asked for this check.

The two table messages counted cells in different index spaces, so a row
of two cells could be reported as "cell 3". Both single-cell fixtures
were blind to it. The style check now names the authoring index, and a
case with a preceding span of two makes the indices diverge.

The change was incomplete against its own rule: cell padding, table
border width and separator line width all still wrote NaN into the
stream, two of them in the renderer it had just guarded.

A positive-infinity leading still raised the too-tall message, because
it was taken by the positive branch while NaN fell through to the font
size. I measured NaN and wrote "a non-finite leading renders" in six
places. Every non-finite leading is now treated as automatic.

#481 is now actually closed rather than claimed: its third refusal, a
table resolving to no columns, says so instead of blaming the page, and
all six pie chart refusals name the property set rather than _chart, a
private field of the renderer. #478's test rename is done too; the old
name promised a zero-dimension refusal its own body said it could not
construct.

Section (c) asserted Assert.NotEmpty, which a PDF always satisfies.
Deleting the image paint operator, or returning early from paragraph
drawing, left all ten cases passing. It now asserts the operator each
neighbour emits, and the file went from 10 cases to 37.

Several prose corrections of the same shape, measuring one value and
writing the claim for the class: a negative font size emits /F1 -12 Tf,
not /F1 0 Tf; a zero height emits 280 0 0 0 10 390 cm, not the width
case's matrix; the ISO citation is 8.3.4 NOTE 3, informative, about
painting graphics objects generally, not an "undefined" scoped to
XObjects; and "no document that renders correctly today is affected" was
false for two inputs.

Closes #478, closes #481
@Tim81
Tim81 force-pushed the fix/layout-validation branch from 2c0aab0 to 4fc2c9f Compare September 12, 2026 07:09
@Tim81
Tim81 merged commit 50e9fa4 into main Sep 12, 2026
4 checks passed
@Tim81
Tim81 deleted the fix/layout-validation 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

Development

Successfully merging this pull request may close these issues.

Layout: a zero image dimension emits a non-invertible transformation matrix

1 participant