Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,42 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Documentation

- **The public members that refuse input now say so, and say what not to pass.** These
boundaries were created by the fixes already listed above and documented almost nowhere: of
Layout's 298 documented public members, exactly one carried an `<exception>` tag. That matters
because of where the refusals fire. They are raised from `Save`, not from the property the
caller set, so a programmer who assigns a bad value gets an exception from a call they never
made while the member they did set says nothing about it.

Nineteen members gain a boundary paragraph and thirteen now carry an `<exception>` tag, up from
one. The pattern is the one on the text style's font size: a bold sentence naming what is
refused, which call throws it and why, then a paragraph beginning "Do not pass" for input that
is accepted today, what it does now, and which major version will reject it. Covered: the table
cell's spans and padding, the table border width, the image width and height, the separator line
width, the pie chart's slices, diameter, start angle, stroke width and alignment, the heading
level, the list indent and nesting depth, the running band's template, height and alignment, and
the document margins.

Writing them turned up eight statements of mine the code did not support. The pie chart does not
skip a zero-width stroke: it strokes on the stroke colour being set, and emits `0 w`. A table's
grid cannot be suppressed at all, since the border colour is not nullable and every cell is
stroked unconditionally. A non-finite document margin is refused only when it is positive
infinity; `NaN` and negative infinity slip past the check, because a comparison against them is
false, and surface as a message about an element being too tall or about the page-continuation
cap, which is the wrong-cause defect #481 corrected elsewhere, filed as #502. A positive-infinity
image width is likewise not refused but clamped to the content box. A marker wider than the list
indent does not overprint the item text, because the gutter is widened per item; the first roman
marker to exceed the default indent is item 17, not 38. Padding wider than its column does not
collapse the cell to nothing: the inner width is clamped to one point, so the text wraps to one
glyph per line and every line lands outside the page. A non-finite band height is refused, by
three different exceptions, none of which names the band height. And `H6` is this library's
deepest heading tag, not the format's: ISO 32000-2 Table 366 defines `Hn` for any integer from
one upward and its NOTE 2 names `H7` explicitly.

The rule is written into `CONTRIBUTING.md`, which ships, rather than only into the agent
guidance, which is untracked and reaches no clone: a public member's boundary is documented in
the same commit as the member.

- **Where the conformance rules knowingly disagree with veraPDF is written down (#418, #419).**
`docs/conformance-divergences.md` records each case with what the standard requires, what an
independent second reading found, what veraPDF does, which one this library follows, and whether a
Expand Down
27 changes: 27 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ otherwise. Use the IDE code-fix (`RS0016` / `RS0017`) or edit the file manually.
Do not add symbols to `PublicAPI.Shipped.txt` — that file is updated only at
release time.

## Public XML documentation states the boundary

Every public member documents where its boundary is and what a caller must not do, not only what
it is. Write it in the same commit as the member.

The shape, copied from `TextStyle.FontSize` in `src/VellumPdf.Layout/Core/TextStyle.cs`:

- `<summary>` says what the member is.
- An `<exception>` tag for every type a call can raise — **including when the throw lands in a
later call rather than this one**, which in the layout engine usually means `Document.Save`.
List each type separately; `NotSupportedException` does not derive from `InvalidDataException`,
and a caller who catches one and not the other has a crash waiting.
- `<remarks>` opens with a bold sentence naming what is **refused**, which call throws it, and the
reason. Not just the fact.
- A following `<para>` beginning "**Do not pass ...**" for input that is accepted today but should
not be relied on: what it does now, and which major version will reject it.

Two rules about the writing itself, both learned the hard way:

- **Measure each non-finite value separately.** `NaN`, positive infinity and negative infinity
take different branches, and a sentence about "a non-finite value" that was measured on one of
them is usually wrong about the other two. A positive-infinity image width, for instance, is
clamped and accepted, while `NaN` is refused.
- **Check the member does not already have a `<remarks>` block.** Two `<remarks>` elements on one
member compile without a warning, and renderers show one of the two, so a contradiction between
them is invisible until a reader hits it.

## Branch and PR etiquette

- **Target `main`.** All pull requests should be opened against the `main`
Expand Down
29 changes: 17 additions & 12 deletions src/VellumPdf.Layout/Core/TextStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,30 @@ public Standard14 Font
/// The font size in points. Defaults to 12. Must be a finite number.
/// </summary>
/// <remarks>
/// <b>A non-finite size is refused.</b> <see cref="Document.Save(System.IO.Stream)"/> throws
/// <see cref="InvalidOperationException"/> naming the size, because every height derived from
/// it is also non-finite and nothing can be measured or placed.
/// <para><b>Do not pass zero or a negative size.</b> Neither is refused today: both reach the
/// content stream as a font operator a reader accepts, so a document is produced, but a size of
/// zero shows nothing and a negative size inverts the glyphs. A later major version will reject
/// both.</para>
/// A non-finite size is refused. <see cref="Document.Save(System.IO.Stream)"/> throws
/// <see cref="InvalidOperationException"/> and names the size. Every height in a layout is
/// derived from the font size, so a non-finite size leaves nothing that can be measured or
/// placed.
/// <para>Attention: a size of zero or less is <b>not</b> refused. It reaches the content
/// stream as a font operator that readers accept, so you still get a document. At zero the
/// text is invisible. Below zero the glyphs are inverted. If you do not want either, check the
/// value before you set it. A later major version will reject both.</para>
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Raised from a save rather than from this property, when the size is not finite. The
/// message names the element the style is attached to and the size itself.
/// </exception>
public double FontSize { get; init; } = 12;

/// <summary>
/// The line leading in points; 0 (the default) means auto, which is the font size times 1.2.
/// </summary>
/// <remarks>
/// Any value at or below zero selects the automatic leading, so there is no way to ask for
/// lines that overlap exactly.
/// <para><b>Do not pass a non-finite value.</b> It is not refused today and does not reach the
/// content stream, so the page is emitted with the text placed as though the leading were
/// automatic. A later major version will reject it.</para>
/// Any value at or below zero selects the automatic leading. There is therefore no way to
/// ask for lines that overlap exactly.
/// <para>Attention: a non-finite value is <b>not</b> refused, and it does not reach the
/// content stream either. The page is emitted with the text placed as though you had asked
/// for automatic leading. A later major version will reject it.</para>
/// </remarks>
public double Leading { get; init; } = 0; // 0 = auto (font-size * 1.2)

Expand Down
105 changes: 105 additions & 0 deletions src/VellumPdf.Layout/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public sealed class Document : IDisposable
public PdfDocumentInfo Info => _pdf.Info;

/// <summary>The default page size used for newly created pages.</summary>
/// <remarks>
/// Validated at save, not here. A width or height that is not a positive finite number is
/// refused with <see cref="ArgumentOutOfRangeException"/> naming the axis and the value, and
/// a page narrower or shorter than <see cref="Margins"/> is refused with
/// <see cref="ArgumentException"/>.
/// <para>Attention: changing this after adding elements does not re-lay out what you already
/// added. The layout runs once, at the save, against whatever size is set then.</para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// Raised from a save rather than from this property, when the width or height is zero,
/// negative or not finite.
/// </exception>
public PdfRectangle PageSize
{
get => _pdf.DefaultPageSize;
Expand Down Expand Up @@ -102,6 +114,35 @@ public bool UseObjectStreams
}

/// <summary>Page margins applied to the content area. Defaults to 72 points (1 inch) on all sides.</summary>
/// <remarks>
/// Margins that meet or exceed the page are refused. Saving throws
/// <see cref="ArgumentException"/> and names the axis and both figures. The content box would
/// otherwise have no positive size, and no element could be placed in it.
/// <para>The header and footer count toward this. Their heights come off the same box, so
/// margins that fit on their own can still leave nothing once you set a running band.</para>
/// <para>Attention: a negative inset is not refused. The document saves and the content is
/// placed outside the page's boundaries, where a reader clips it. On a one-paragraph document
/// with every inset at -72, the file is written with no invalid token in it, so nothing
/// downstream reports the loss either. A later major version will reject it.</para>
/// <para>A non-finite inset is refused in only one of its three forms, and each form fails
/// differently, so you cannot catch them together. Positive infinity reaches the margin check
/// and throws <see cref="ArgumentException"/> naming the margin. <c>NaN</c> slips past the
/// check, because a comparison against it is false, and surfaces as
/// <see cref="InvalidOperationException"/> saying an element is too tall to fit, which is not
/// what happened. Negative infinity slips past for the same reason and surfaces as the
/// page-continuation cap. Both wrong-cause messages are the defect #481 corrected elsewhere,
/// still outstanding here (#502).</para>
/// </remarks>
/// <exception cref="ArgumentException">
/// Raised from a save rather than from this property, when the margins on either axis meet
/// or exceed the page, or when they leave the content area no positive size once the header
/// and footer are taken off. Positive infinity reaches this check.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Raised from a save when an inset is <c>NaN</c> or negative infinity. Neither reaches the
/// check above, so each surfaces as one of the unrelated messages described in the remarks.
/// Catching <see cref="ArgumentException"/> alone will not catch them.
/// </exception>
public EdgeInsets Margins { get; set; } = new EdgeInsets(72); // 1 inch

/// <summary>
Expand Down Expand Up @@ -308,6 +349,30 @@ private void CollectDiagnostics(DocumentRenderer renderer)
}

/// <summary>Runs the layout pass and writes the resulting PDF to the given stream.</summary>
/// <remarks>
/// A document is single-use. The layout runs here, not when you add an element, so most of
/// what can go wrong goes wrong at this call rather than at the one that set the bad value.
/// <para>Attention: calling this twice throws. The second call reports that the document has
/// already been written and tells you to create a new one. Nothing is appended and nothing is
/// overwritten, but you cannot use one <see cref="Document"/> to write two files.</para>
/// <para>A document with no pages throws as well. Add at least one element before you
/// save.</para>
/// </remarks>
/// <exception cref="InvalidOperationException">
/// The document has already been written, or has no pages, or an element's input cannot be
/// laid out. The boundary documentation on the individual properties says which inputs those
/// are.
/// </exception>
/// <exception cref="ArgumentException">
/// The margins, header and footer together leave the content area no positive size.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <see cref="PageSize"/> has a width or height that is not a positive finite number.
/// </exception>
/// <exception cref="NotSupportedException">
/// <see cref="UseObjectStreams"/> was set and <see cref="Encrypt"/> was called. The two
/// cannot be combined.
/// </exception>
public void Save(Stream destination)
{
var renderer = new DocumentRenderer(_pdf, _pdf.DefaultPageSize, Margins)
Expand All @@ -321,6 +386,14 @@ public void Save(Stream destination)
}

/// <summary>Runs the layout pass and writes the resulting PDF to a file at the given path.</summary>
/// <remarks>
/// Everything on <see cref="Save(System.IO.Stream)"/> applies, and one thing more.
/// <para>Attention: the file is opened before the layout runs, so a failure destroys whatever
/// the path held. Measured: a path holding a 1,535-byte document, saved again from a document
/// whose layout then fails, is left existing and zero bytes long. If the target matters,
/// write to a temporary path and move it into place yourself, or save to a stream you control
/// (#508).</para>
/// </remarks>
public void Save(string path)
{
using var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
Expand All @@ -338,6 +411,30 @@ public void Save(string path)
/// final write, but does not abort layout or serialisation already in progress.
/// </para>
/// </summary>
/// <remarks>
/// A document is single-use. The layout runs here, not when you add an element, so most of
/// what can go wrong goes wrong at this call rather than at the one that set the bad value.
/// <para>Attention: calling this twice throws. The second call reports that the document has
/// already been written and tells you to create a new one. Nothing is appended and nothing is
/// overwritten, but you cannot use one <see cref="Document"/> to write two files.</para>
/// <para>A document with no pages throws as well. Add at least one element before you
/// save.</para>
/// </remarks>
/// <exception cref="InvalidOperationException">
/// The document has already been written, or has no pages, or an element's input cannot be
/// laid out. The boundary documentation on the individual properties says which inputs those
/// are.
/// </exception>
/// <exception cref="ArgumentException">
/// The margins, header and footer together leave the content area no positive size.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <see cref="PageSize"/> has a width or height that is not a positive finite number.
/// </exception>
/// <exception cref="NotSupportedException">
/// <see cref="UseObjectStreams"/> was set and <see cref="Encrypt"/> was called. The two
/// cannot be combined.
/// </exception>
// RS0026 flags multiple overloads with optional parameters as a future-ambiguity risk;
// Stream and string share no implicit conversion, so overload resolution can never be
// ambiguous between these two.
Expand All @@ -357,6 +454,14 @@ public async Task SaveAsync(Stream destination, CancellationToken cancellationTo
}

/// <summary>Asynchronously runs the layout pass and writes the resulting PDF to a file at the given path.</summary>
/// <remarks>
/// Everything on <see cref="Save(System.IO.Stream)"/> applies, and one thing more.
/// <para>Attention: the file is opened before the layout runs, so a failure destroys whatever
/// the path held. Measured: a path holding a 1,535-byte document, saved again from a document
/// whose layout then fails, is left existing and zero bytes long. If the target matters,
/// write to a temporary path and move it into place yourself, or save to a stream you control
/// (#508).</para>
/// </remarks>
#pragma warning disable RS0026
public async Task SaveAsync(string path, CancellationToken cancellationToken = default)
#pragma warning restore RS0026
Expand Down
13 changes: 13 additions & 0 deletions src/VellumPdf.Layout/Elements/Heading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public sealed class Heading
public TextStyle Style { get; }

/// <summary>Outline nesting level: 0 = top-level, 1 = sub-heading, etc.</summary>
/// <remarks>
/// Zero-based: level 0 is tagged <c>H1</c>, level 4 is <c>H5</c>, and every level at or
/// above 5 is tagged <c>H6</c>. That ceiling is this library's, not the format's: ISO 32000-2
/// Table 366 defines <c>Hn</c> for any unsigned integer from 1 upward, and its NOTE 2 says
/// outright that <c>H7</c> may be used for a seventh-level heading.
/// <para>Attention: a negative level is <b>not</b> refused, and it does not clamp the way
/// you would expect. The mapping's catch-all sends it to <c>H6</c>, the deepest tag, where
/// you almost certainly meant the shallowest. The raw value also reaches the outline builder,
/// so your bookmark tree nests on it. A later major version will reject it.</para>
/// <para>Every level from 5 upward is the same <c>H6</c> tag. Assistive technology therefore
/// cannot tell level 5 from level 500, while the outline still nests on the number you gave.
/// Do not use a large level to express depth.</para>
/// </remarks>
public int Level { get; init; }

/// <summary>
Expand Down
Loading