Skip to content
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Added

- **`Preformatted`, a block that keeps every space and tab (#591).** `Document.Add(Preformatted)`
draws text whose spacing is its meaning: command blocks, configuration, ASCII tables. The default
style is Courier at 10 pt. `Document.SetDefaultFont` does not apply.
- A line ends at CR LF, a lone CR or a lone LF. Every space is kept, leading, repeated and
trailing. A tab becomes the spaces that reach the next stop, `TabWidth` columns apart (1 to 32,
default 8), counting extended grapheme clusters, so the content stream holds no tab byte. In a
monospace font, for text of one character per column, the columns line up and
`pdftotext -fixed` returns them. A zero-width character that forms a cluster of its own, such as
a combining mark at the start of a line or after a tab, shifts the columns after it. `-layout`
collapses runs of spaces, so it is not an exact oracle.
- A line wider than the content box is split into segments, each but the last followed by a hook
drawn as an artifact, so it enters neither the structure tree nor extracted text. No character
is dropped. The save lists the wrapped lines as the new `LayoutDiagnosticCode.PreformattedLineWrapped`
(104, a warning, so `StrictDiagnostics` still saves). New `LayoutElementKind.Preformatted` (11).
- The constructor throws `ArgumentException` for control characters other than tab, CR and LF,
for U+2028 and U+2029, and for an unpaired surrogate. It also refuses a style with a font size
below 0.000005 or above 1e30 (the ceiling keeps every number the block writes far inside the
±3.403 × 10^38 range of ISO 32000-1, Annex C, Table C.1, which ISO 19005-2, 6.1.13 applies) or
not finite, a non-finite leading, a colour channel outside 0 to 1, a Standard-14 value the
enumeration does not name, Symbol, ZapfDingbats, or a `LinkUri`.
- Before any layout, the save throws `InvalidOperationException` when the content box cannot
hold one line. Under a PDF/A or PDF/UA-1 claim it also throws when the font is not embedded or
has no glyph for a character, and, under PDF/A-2a, PDF/A-2u and PDF/UA-1, for U+FEFF and
U+FFFE. These refusals leave the document saveable once the named setting is changed.
- With no claim, a character the embedded font lacks is drawn as glyph 0 and listed as
`NotdefGlyph` (102), as for a paragraph, so `StrictDiagnostics` refuses that save.
- In a tagged document each drawn piece is one `P` structure element. Nothing in the character
stream separates two lines. Documents without the element save to the same bytes as before.

- **`Heading` and `ListItem` take runs (#367).** `new Heading(IEnumerable<TextRun>)` and
`new ListItem(IEnumerable<TextRun>)` build the element from runs in their own styles, as a
`Paragraph` does, so one word of a heading can be bold and one run of a list item can be a link.
Expand Down
1 change: 1 addition & 0 deletions docs/layout-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ a guard test keeps the two copies byte-identical.
| Paragraph flow: wrapping, alignment (left/center/right/justify), mixed-style inline runs | ✅ Supported | — |
| Tables: rows, columns, row spanning, borders | ✅ Supported | — |
| Lists: ordered (decimal/alpha/roman), unordered, nested | ✅ Supported | — |
| Preformatted text: spaces and tabs kept, wrap marked | ✅ Supported | — |
| Headers and footers with page-number templates | ✅ Supported | — |
| Images: JPEG, PNG (incl. interlaced and 16-bit), CCITT G4, TIFF-LZW | ✅ Supported | — |
| Font embedding and subsetting: TrueType (`glyf`) and OpenType/CFF | ✅ Supported | — |
Expand Down
5 changes: 5 additions & 0 deletions src/VellumPdf.Kernel/Fonts/EmbeddedFontHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public double MeasureString(string text, double pointSize) =>
/// </remarks>
public ushort GetGlyphId(int codePoint) => Embedder.GetGlyphId(codePoint);

/// <summary>
/// The glyph id <see cref="GetGlyphId"/> would return, without registering it for the subset.
/// </summary>
internal ushort PeekGlyphId(int codePoint) => Embedder.PeekGlyphId(codePoint);

/// <summary>
/// Converts every Unicode scalar in <paramref name="text"/> to glyph ids and writes
/// them into <paramref name="buffer"/>. Returns the number of glyph ids written.
Expand Down
11 changes: 11 additions & 0 deletions src/VellumPdf.Kernel/Fonts/TrueTypeFontEmbedder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ public ushort GetGlyphId(int codePoint)
return gid;
}

/// <summary>
/// The glyph id <see cref="GetGlyphId"/> returns for <paramref name="codePoint"/>, found
/// without registering it: the subset and the ToUnicode map are left as they were. A caller
/// that checks text before drawing it uses this, so a check that refuses leaves nothing behind
/// for a retry to inherit.
/// </summary>
internal ushort PeekGlyphId(int codePoint) =>
_unicodeToGid.TryGetValue(codePoint, out var cachedGid)
? cachedGid
: NoBreakSpaces.ResolveGlyph(codePoint, _cmap.GetGlyphId);

/// <summary>Advance width in PDF units (1/1000 pt) at 1-pt font size.</summary>
public int GetAdvanceWidth(ushort gid)
{
Expand Down
3 changes: 2 additions & 1 deletion src/VellumPdf.Layout/Core/ColorRgb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace VellumPdf.Layout.Core;
/// paragraph run, table cell text or line separator names the element first. A colour that is not
/// written is not refused; see <c>Document</c>.
/// <para>A finite channel outside 0 to 1 is <b>not</b> refused, except in a row background passed
/// to <see cref="Elements.Table.TableElement.Add(Elements.Table.Row)"/>. Elsewhere it is written
/// to <see cref="Elements.Table.TableElement.Add(Elements.Table.Row)"/> and in the text colour of a
/// <see cref="Elements.Preformatted"/>, whose constructor refuses it. Elsewhere it is written
/// as given after rounding to five decimals, so (5, 0, -1) is written as <c>5 0 -1 rg</c>.
/// ISO 32000-2, 8.6.4.3, says each DeviceRGB component "shall" be a number from 0.0 to 1.0, so
/// that output is not conforming even though it parses and renders.</para>
Expand Down
10 changes: 6 additions & 4 deletions src/VellumPdf.Layout/Core/PageBreakMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ namespace VellumPdf.Layout.Core;
/// property's <c>init</c> accessor, so the error names the line that wrote the bad value.
/// <para>The mode is read when the document places a top-level element: a
/// <see cref="Elements.Paragraph"/>, <see cref="Elements.Heading"/>,
/// <see cref="Elements.Table.TableElement"/>, <see cref="Elements.ListElement"/> or
/// <see cref="Elements.LayoutImage"/> added with its own <c>Document.Add</c> overload, or the
/// public renderer of a paragraph, table, list or image added with
/// <see cref="Elements.Table.TableElement"/>, <see cref="Elements.ListElement"/>,
/// <see cref="Elements.Preformatted"/> or <see cref="Elements.LayoutImage"/> added with its own
/// <c>Document.Add</c> overload, or the public renderer of a paragraph, table, list or image added
/// with
/// <see cref="Document.Add(IRenderer)"/> or
/// <see cref="Rendering.DocumentRenderer.Add(IRenderer)"/>. A heading's renderer is internal, so a
/// heading has no other route.</para>
Expand All @@ -30,7 +31,8 @@ namespace VellumPdf.Layout.Core;
/// i, ii, iii still starts a <see cref="Recto"/> chapter on an odd physical page.</para>
/// <para>Do not rely on a form feed (U+000C) in text to break the page. It does not start a page in
/// any built-in element, and 2.x does not change that. A paragraph, a heading and a list item draw
/// it as a space, like most white-space characters other than a line feed. A table cell and a
/// it as a space, like most white-space characters other than a line feed. A preformatted element
/// refuses it. A table cell and a
/// running band pass it to the font unchanged, so the content stream holds the raw 0x0C character.
/// Use <see cref="Page"/> or a <see cref="Elements.PageBreak"/>.</para>
/// </remarks>
Expand Down
16 changes: 15 additions & 1 deletion src/VellumPdf.Layout/Core/TextStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public TextStyle() { }
/// <see cref="VellumPdf.Layout.Document.StrictDiagnostics"/> set, either report refuses the
/// save. <b>Do not pass</b> a Standard 14 font under either claim. A later major version
/// refuses text the font cannot draw, and a Standard 14 font, under either claim.</para>
/// <para>A <see cref="VellumPdf.Layout.Elements.Preformatted"/> refuses a Standard-14 value the
/// enumeration does not name, Symbol and ZapfDingbats from its constructor, with
/// <see cref="ArgumentException"/>, and under a conformance claim its save refuses a font that
/// is not embedded.</para>
/// </remarks>
/// <exception cref="IndexOutOfRangeException">
/// Raised from <see cref="VellumPdf.Layout.Document.Save(System.IO.Stream)"/> and the other
Expand Down Expand Up @@ -135,6 +139,9 @@ public Standard14 Font
/// <para>Each paragraph run, and a heading's text, is checked when it holds a character other
/// than white space. The no-break spaces U+00A0, U+202F and U+2007 count as such characters; a
/// tab and other white space do not. A table cell or list item is checked whatever its text holds.</para>
/// <para>A <see cref="VellumPdf.Layout.Elements.Preformatted"/> refuses a size that is not
/// finite, is below 0.000005 or is above 1e30 from its constructor, with
/// <see cref="ArgumentException"/>, not from the save.</para>
/// <para><b>Attention</b>: 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
Expand Down Expand Up @@ -182,6 +189,9 @@ public Standard14 Font
/// <remarks>
/// Any value at or below zero selects the automatic leading. There is therefore no way to
/// ask for lines that overlap exactly.
/// <para>A <see cref="VellumPdf.Layout.Elements.Preformatted"/> refuses a non-finite leading
/// from its constructor, with <see cref="ArgumentException"/>. Zero and negative still mean
/// automatic.</para>
/// <para>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>
Expand Down Expand Up @@ -231,7 +241,9 @@ public Standard14 Font
/// list item or a table cell that draws text, the element's own check names it. On a running band,
/// and on a band that draws nothing, <see cref="VellumPdf.Canvas.PdfCanvas"/> decides: it refuses
/// the value when the band is drawn, and an empty template writes no colour. <b>Do not pass</b> a
/// non-finite channel: 3.0 refuses it.
/// non-finite channel: 3.0 refuses it. A <see cref="VellumPdf.Layout.Elements.Preformatted"/>
/// refuses a channel that is not a number from 0 to 1 from its constructor, with
/// <see cref="ArgumentException"/>.
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Raised from a save, when a channel is <c>NaN</c> or infinite on a paragraph run, heading, list
Expand Down Expand Up @@ -284,6 +296,8 @@ public Standard14 Font
/// drawn with.
/// <para>Do not pass a value that is not an absolute URI. A later major version will refuse
/// one.</para>
/// <para>A <see cref="VellumPdf.Layout.Elements.Preformatted"/> refuses any value from its
/// constructor, with <see cref="ArgumentException"/>.</para>
/// </remarks>
/// <exception cref="ArgumentException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
Expand Down
47 changes: 43 additions & 4 deletions src/VellumPdf.Layout/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ public Document() { }
/// <see cref="LayoutDiagnosticCode.NotdefGlyph"/> (a character an embedded font has no glyph
/// for, written as glyph 0), <see cref="LayoutDiagnosticCode.UnembeddedFont"/> (an element that
/// selects a Standard 14 font under a conformance claim),
/// <see cref="LayoutDiagnosticCode.BandTruncated"/> (mirroring <see cref="BandTruncations"/>)
/// and <see cref="LayoutDiagnosticCode.DiagnosticsDropped"/>. Every one saves by default; only
/// <see cref="StrictDiagnostics"/> refuses it, and a later major version refuses them
/// all.</para>
/// <see cref="LayoutDiagnosticCode.BandTruncated"/> (mirroring <see cref="BandTruncations"/>),
/// <see cref="LayoutDiagnosticCode.PreformattedLineWrapped"/> (a preformatted line split with
/// a marker; a warning, which <see cref="StrictDiagnostics"/> saves)
/// and <see cref="LayoutDiagnosticCode.DiagnosticsDropped"/>. Every other one saves by
/// default; only <see cref="StrictDiagnostics"/> refuses it, and a later major version refuses
/// them all.</para>
/// <para>The channel stores at most 1,000 distinct items, and adds
/// <see cref="LayoutDiagnosticCode.DiagnosticsDropped"/> after them when it dropped any, so the
/// list can hold 1,001. That item's <see cref="LayoutDiagnostic.Count"/> is the number of
Expand Down Expand Up @@ -975,6 +977,43 @@ public Document Add(Heading heading)
return this;
}

/// <summary>
/// Adds a block of text whose spaces, tabs and line ends are kept. Returns this document for
/// chaining.
/// </summary>
/// <remarks>
/// <b>A null block is refused.</b> It throws <see cref="ArgumentNullException"/> from this
/// call. What the block holds was checked when it was built, by <see cref="Preformatted"/>; the
/// save refuses what depends on the document: the content box, and the font and the characters
/// under a <see cref="Conformance"/> claim. A refusal of that kind is raised before any layout,
/// so the document stays saveable once you have changed what the message names. The block is
/// listed in <see cref="Diagnostics"/> as <see cref="LayoutElementKind.Preformatted"/> when a
/// line is wrapped.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="preformatted"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Raised from <see cref="Save(System.IO.Stream)"/> and the other save overloads, not from this
/// call, before any layout, when the content box the margins leave is not wider than zero or is
/// shorter than one line, and, under a <see cref="Conformance"/> other than
/// <see cref="PdfConformance.None"/>, when the style's font is not embedded or the embedded
/// font has no glyph for a character of the text. Under PDF/A-2a, PDF/A-2u and PDF/UA-1 a
/// U+FEFF or U+FFFE is refused as well. It is also raised when the block needs more than
/// 50,000 page continuations; see <see cref="Add(IRenderer)"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// Raised from the save, not from this call, when the style's embedded font came from another
/// document that the saving document does not define a font under the handle's resource name
/// for; see <see cref="FontReference(EmbeddedFontHandle)"/>.
/// </exception>
public Document Add(Preformatted preformatted)
{
ArgumentNullException.ThrowIfNull(preformatted);
_content.Add(new PreformattedRenderer(preformatted));
return this;
}

/// <summary>
/// Adds a page break: the next element starts on a new page, or on the next right-hand page
/// when <see cref="PageBreak.Recto"/> is set. Returns this document for chaining.
Expand Down
Loading
Loading