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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Added

- **`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.
`Heading.Runs` and `ListItem.Runs` return the runs.
- A tagged document gets one `Hn` structure element and one marked-content sequence per
heading, whatever the number of runs. `Heading.Text` is the concatenation of the run texts and
is the outline title unless `BookmarkTitle` is set; `Heading.Style` is the first run's style.
- A run is a link only through its own `LinkUri`. The annotation covers that run and a space
that opens the next run, as in a `Paragraph`. On a list item, `ListItem.Style` styles the
marker and the children that set no style of their own, not the item's runs.
- A null sequence and a null run throw `ArgumentNullException`, and a run with a null style
throws `ArgumentException`, from the constructor. An empty sequence is accepted, as an empty
string is.
- A run that draws no text, because it is empty or holds only white space, has its style
unchecked by the save, as in a `Paragraph`.
- Per-run language is not written: `Heading.Language` and `ListItem.Language` still apply to
the whole element.
- The string constructors are unchanged, and a document built with them saves to the same bytes.

- **`Document.Timestamp` and `Document.DocumentId` (#597).** The Layout `Document` now forwards the
two pins that `PdfDocument` already had. `Timestamp` sets the instant written as the XMP creation
and modification date and mixed into the derived file identifier; with it pinned, two documents
Expand Down
99 changes: 91 additions & 8 deletions src/VellumPdf.Layout/Elements/Heading.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © Timothy van der Ham (@Tim81)
// SPDX-License-Identifier: Apache-2.0

using System.Runtime.CompilerServices;
using VellumPdf.Layout.Core;

namespace VellumPdf.Layout.Elements;
Expand All @@ -13,28 +14,43 @@ namespace VellumPdf.Layout.Elements;
/// Laid out by the same code as <see cref="Paragraph"/>, so wrapping and
/// <see cref="HorizontalAlignment.Justify"/> behave the same. Every heading also adds a bookmark
/// and, in a tagged document, an <c>Hn</c> structure element chosen by <see cref="Level"/>.
/// <para>A heading holds one string in one style, or a sequence of <see cref="TextRun"/>s that
/// style and link parts of it, as a <see cref="Paragraph"/> does; see <see cref="Runs"/>. Runs
/// are drawn as the <see cref="Paragraph"/> remarks describe, including that a run boundary
/// draws a space only where the text has one. However many runs it holds, a tagged document gets
/// one structure element for the heading.</para>
/// </remarks>
public sealed class Heading
{
/// <summary>Heading text (also used as the bookmark title unless <see cref="BookmarkTitle"/> is set).</summary>
/// <remarks>
/// Never null; the constructor refuses a null. White space is drawn as on
/// Never null; the constructors refuse a null. White space is drawn as on
/// <see cref="Paragraph"/>. An empty string draws no text, but the heading still adds a
/// bookmark, titled by <see cref="BookmarkTitle"/> or else empty, and, in a tagged document,
/// its structure element.
/// <para>On a heading built from runs this is the text of every run, concatenated in order and
/// as stored, as <see cref="Paragraph.Text"/> is. The bookmark title is that string, so the
/// runs <c>De </c>, <c>Save</c> and <c>-methode</c> give <c>De Save-methode</c>. Nothing is
/// added at a run boundary and nothing is collapsed, whatever is drawn there.</para>
/// </remarks>
/// <exception cref="ArgumentException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
/// from this property, when the text holds an unpaired surrogate and is measured in an embedded
/// font; see <see cref="TextStyle.FontRef"/>.
/// font; see <see cref="TextStyle.FontRef"/>. A pair split between two runs that share one
/// style instance is joined and does not throw.
/// </exception>
public string Text { get; }

/// <summary>The heading's text style.</summary>
/// <remarks>
/// A null style passed to the constructor becomes 14pt Helvetica; any other is stored as given.
/// Refusals on size, leading, font and text colour are on <see cref="TextStyle"/> and are
/// raised from the save.
/// A null style passed to the string constructor becomes 14pt Helvetica; any other is stored as
/// given. Refusals on size, leading, font and text colour are on <see cref="TextStyle"/> and
/// are raised from the save.
/// <para>On a heading built from runs this is the style of the first run, as
/// <see cref="Paragraph.Style"/> is. It only reports that run: each run is drawn in its own
/// style, and the save does not check the style of a run that draws no text, one that is
/// empty or holds only white space. A heading built from an empty sequence has the 14pt
/// Helvetica style of an empty string heading.</para>
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
Expand All @@ -53,7 +69,7 @@ public sealed class Heading
/// from this property, when the heading's text holds an unpaired surrogate and is measured in
/// an embedded font; see <see cref="TextStyle.FontRef"/>.
/// </exception>
public TextStyle Style { get; }
public TextStyle Style => _runs[0].Style;

/// <summary>Outline nesting level: 0 = top-level, 1 = sub-heading, etc.</summary>
/// <remarks>
Expand Down Expand Up @@ -148,7 +164,7 @@ public PageBreakMode BreakBefore
/// <remarks>
/// A null <paramref name="style"/> becomes 14pt Helvetica. A null <paramref name="text"/> is
/// refused here. An empty string is not
/// null and is accepted.
/// null and is accepted. The heading holds one run, <see cref="Runs"/>, of that text and style.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="text"/> is <see langword="null"/>.
Expand All @@ -158,8 +174,75 @@ public Heading(string text, TextStyle? style = null)
{
ArgumentNullException.ThrowIfNull(text);
Text = text;
Style = style ?? new TextStyle { FontSize = 14 };
_runs = [new TextRun(text, style ?? DefaultStyle())];
}

/// <summary>Creates a heading from a sequence of runs, each with its own style.</summary>
/// <remarks>
/// <b>A null sequence, a null run and a run with a null style are refused here.</b> The first
/// two throw <see cref="ArgumentNullException"/> and the third <see cref="ArgumentException"/>;
/// <c>ParamName</c> is <c>runs</c> and the message of the last two names the index of the run.
/// <see cref="TextRun"/> stores a null style, and a paragraph saves one on a run that draws no
/// text; this constructor refuses it whatever the run's text, so an empty run needs a style
/// too. An empty sequence is accepted and builds a heading with one empty run in 14pt
/// Helvetica, as an empty string does. The sequence is copied: changing it afterwards changes
/// nothing. A <see langword="null"/> literal binds to the string constructor, not to this one.
/// <para>Each run is drawn in its own style, and <see cref="Style"/> does not style the others.
/// A run is a link only through its own <see cref="TextStyle.LinkUri"/>. The annotation
/// covers that run and also a space that opens the next run, because the space belongs to the
/// run before it, as in a <see cref="Paragraph"/>.</para>
/// <para>The style of a run that draws no text, one that is empty or holds only white space,
/// is not checked by the save: a size or colour it would refuse on a run that draws text does
/// not throw here.</para>
/// <para>A tagged document gets one <c>Hn</c> structure element, and <see cref="Language"/>
/// applies to all of the heading. <b>Per-run language is not written.</b> Do not build a
/// heading from runs in other languages expecting each to be marked as such.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="runs"/> is <see langword="null"/> or contains <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// A run's <see cref="TextRun.Style"/> is <see langword="null"/>. It is also raised from
/// <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not from this
/// constructor, when a run's text holds an unpaired surrogate and is measured in an embedded
/// font; see <see cref="TextStyle.FontRef"/>. A pair split between two runs that share one
/// style instance is joined and does not throw.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
/// from this constructor, when the style of a run that draws text has its size or leading
/// refused, or a channel of its text colour is not finite; see
/// <see cref="TextStyle.FontSize"/>, <see cref="TextStyle.Leading"/> and
/// <see cref="TextStyle.Color"/>.
/// </exception>
/// <exception cref="IndexOutOfRangeException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
/// from this constructor, when a run's style holds a <see cref="VellumPdf.Fonts.Standard14"/>
/// value the enumeration does not name and the font is selected on a page; see
/// <see cref="TextStyle.FontRef"/>.
/// </exception>
[OverloadResolutionPriority(-1)]
public Heading(IEnumerable<TextRun> runs)
{
ArgumentNullException.ThrowIfNull(runs);
_runs = TextRunList.Copy(runs, nameof(runs), DefaultStyle());
Text = TextRunList.Concat(_runs);
}

/// <summary>The runs the heading is drawn from, in order.</summary>
/// <remarks>
/// Never empty and never holds a null, and every run has a style. A heading built from a
/// string holds one run of that text in <see cref="Style"/>. The list is a read-only view of a
/// copy: changing the sequence the constructor was given does not change it, and a cast of
/// this property cannot change it either.
/// <para><b>Per-run language is not written.</b> Every run is part of the one structure element
/// of the heading, and <see cref="Language"/> is the only language it carries.</para>
/// </remarks>
public IReadOnlyList<TextRun> Runs => _runs.AsReadOnly();

private readonly List<TextRun> _runs;

private static TextStyle DefaultStyle() => new() { FontSize = 14 };

internal string ResolvedBookmarkTitle => BookmarkTitle ?? Text;
}
103 changes: 101 additions & 2 deletions src/VellumPdf.Layout/Elements/ListItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © Timothy van der Ham (@Tim81)
// SPDX-License-Identifier: Apache-2.0

using System.Runtime.CompilerServices;
using VellumPdf.Layout.Core;

namespace VellumPdf.Layout.Elements;
Expand All @@ -11,21 +12,29 @@ namespace VellumPdf.Layout.Elements;
/// <remarks>
/// Nesting is one level. An item reached only as a child of a child is not laid out or drawn, so
/// nothing it holds makes the save throw; see <see cref="Children"/>.
/// <para>An item holds one string, or a sequence of <see cref="TextRun"/>s that style and link
/// parts of it, as a <see cref="Paragraph"/> does; see <see cref="Runs"/>. Runs are drawn as the
/// <see cref="Paragraph"/> remarks describe, including that a run boundary draws a space only
/// where the text has one.</para>
/// </remarks>
public sealed class ListItem
{
private List<ListItem>? _children;

/// <summary>The item's text.</summary>
/// <remarks>
/// <b>Never null.</b> A null passed to the constructor is stored as an empty string and
/// <b>Never null.</b> A null passed to the string constructor is stored as an empty string and
/// remembered; see the constructor for what the save does with it. White space is drawn as on
/// <see cref="Paragraph"/>.
/// <para>On an item built from runs this is the text of every run, concatenated in order and as
/// stored, as <see cref="Paragraph.Text"/> is. Nothing is added at a run boundary and nothing
/// is collapsed, whatever is drawn there.</para>
/// </remarks>
/// <exception cref="ArgumentException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
/// from this property, when the text holds an unpaired surrogate and is measured in an embedded
/// font; see <see cref="TextStyle.FontRef"/>. It is not raised for an item reached only as a
/// font; see <see cref="TextStyle.FontRef"/>. A pair split between two runs that share one
/// style instance is joined and does not throw. It is not raised for an item reached only as a
/// child of a child.
/// </exception>
public string Text { get; }
Expand All @@ -36,6 +45,11 @@ public sealed class ListItem
/// <see cref="TextStyle.Default"/>. On a child, null means the parent item's style after that
/// same fallback. Refusals on size, leading, font and text colour are on
/// <see cref="TextStyle"/> and are raised from the save.
/// <para>On an item built from runs this styles the marker and the children that set no style
/// of their own, and none of this item's runs, because each run has its own style. The
/// marker of a child item takes its own style, or its parent's. A run is a link only through
/// its own <see cref="TextStyle.LinkUri"/>; one in this style links the marker, and the
/// marker and text of a child that inherits it.</para>
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
Expand Down Expand Up @@ -153,6 +167,91 @@ public ListItem(string text, TextStyle? style = null)
Style = style;
}

/// <summary>
/// Creates a list item from a sequence of runs, each with its own style. <see cref="Style"/>
/// styles the marker and the children without a style, not the runs.
/// </summary>
/// <remarks>
/// <b>A null sequence, a null run and a run with a null style are refused here.</b> The first
/// two throw <see cref="ArgumentNullException"/> and the third <see cref="ArgumentException"/>;
/// <c>ParamName</c> is <c>runs</c> and the message of the last two names the index of the run.
/// <see cref="TextRun"/> stores a null style, and a paragraph saves one on a run that draws no
/// text; this constructor refuses it whatever the run's text, so an empty run needs a style
/// too. The refusal is made here, whatever place the item later takes in a list. An empty
/// sequence is accepted and builds an item with one empty run, as an empty string does. The
/// sequence is copied: changing it afterwards changes nothing. A <see langword="null"/>
/// literal binds to the string constructor, not to this one.
/// <para>Each run is drawn in its own style. A run is a link only through its own
/// <see cref="TextStyle.LinkUri"/>. The annotation covers that run and also a space that opens
/// the next run, because the space belongs to the run before it, as in a
/// <see cref="Paragraph"/>. The marker is drawn in <see cref="Style"/>, and when that is null,
/// in the style <see cref="Style"/> says is inherited, so set it to the style of the runs for
/// a marker that matches them: <c>new ListItem(runs) { Style = style }</c>.</para>
/// <para>The style of a run that draws no text, one that is empty or holds only white space,
/// is not checked by the save: a size or colour it would refuse on a run that draws text does
/// not throw here.</para>
/// <para>A tagged document gets one structure element for the item content, and
/// <see cref="Language"/> applies to all of it. <b>Per-run language is not written.</b> Do not
/// build an item from runs in other languages expecting each to be marked as such.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="runs"/> is <see langword="null"/> or contains <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// A run's <see cref="TextRun.Style"/> is <see langword="null"/>. It is also raised from
/// <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not from this
/// constructor, when a run's text holds an unpaired surrogate and is measured in an embedded
/// font; see <see cref="TextStyle.FontRef"/>. A pair split between two runs that share one
/// style instance is joined and does not throw. The save does not raise it for an item
/// reached only as a child of a child.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
/// from this constructor, when the style of a run that draws text has its size or leading
/// refused, or a channel of its text colour is not finite; see
/// <see cref="TextStyle.FontSize"/>, <see cref="TextStyle.Leading"/> and
/// <see cref="TextStyle.Color"/>. It is not raised for an item reached only as a child of a
/// child.
/// </exception>
/// <exception cref="IndexOutOfRangeException">
/// Raised from <see cref="Document.Save(System.IO.Stream)"/> and the other save overloads, not
/// from this constructor, when a run's style holds a <see cref="VellumPdf.Fonts.Standard14"/>
/// value the enumeration does not name and the font is selected on a page; see
/// <see cref="TextStyle.FontRef"/>. It is not raised for an item reached only as a child of a
/// child.
/// </exception>
[OverloadResolutionPriority(-1)]
public ListItem(IEnumerable<TextRun> runs)
{
ArgumentNullException.ThrowIfNull(runs);
_runs = TextRunList.Copy(runs, nameof(runs), TextStyle.Default);
Text = TextRunList.Concat(_runs);
}

/// <summary>The runs the item's text is drawn from, in order.</summary>
/// <remarks>
/// Never empty and never holds a null. An item built from runs returns them, each with its own
/// style. An item built from a string returns one run of its text in <see cref="Style"/>, or
/// in <see cref="TextStyle.Default"/> when that is null; that run does not show the style the
/// item inherits from the list, which the list applies when it is laid out. The list is a
/// read-only view of a copy: changing the sequence the constructor was given does not change
/// it, and a cast of this property cannot change it either.
/// <para><b>Per-run language is not written.</b> Every run is part of the one structure
/// element of the item, and <see cref="Language"/> is the only language it carries.</para>
/// </remarks>
public IReadOnlyList<TextRun> Runs =>
_runs?.AsReadOnly() ?? [new TextRun(Text, Style ?? TextStyle.Default)];

private readonly List<TextRun>? _runs;

/// <summary>
/// The runs the content is drawn from: the item's own, or its text in
/// <paramref name="inherited"/>, the style a string-built item takes from the list or its
/// parent.
/// </summary>
internal IReadOnlyList<TextRun> ContentRuns(TextStyle inherited) =>
_runs ?? [new TextRun(Text, inherited)];

/// <summary>True when the constructor was given a null text.</summary>
internal bool HasNullText { get; }

Expand Down
Loading
Loading