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

- **`Preformatted.Anchor`, a code block can be a link target (#693).** The property has the name
rule, the refusals and the named destination of `Paragraph.Anchor`: a name of 1 to 127
characters from `A-Z a-z 0-9 . _ - :` is accepted, any other name throws `ArgumentException` from
the `init` accessor, and the save adds `[page /XYZ 0 top null]` to the `/Names` `/Dests` tree at
the top of the block's first line, below its top margin. A block that splits across pages reports the anchor
from its first piece only, so the destination is on the page the first line is on.
- A name held by a `Preformatted` and by another element of any kind is refused like a name held
by two paragraphs: the save throws `InvalidOperationException` before any layout, naming both
elements, and the document stays saveable.
- A block with no anchor saves to the same bytes as before.

- **Page labels and anchors (#262, Layout).** A document can set the page numbers a viewer shows,
and name places that other documents and links can open it at.
- `PageLabelRange(style, prefix, firstNumber)` describes a run of pages: decimal, upper or lower
Expand Down
7 changes: 4 additions & 3 deletions src/VellumPdf.Layout/Elements/Paragraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,10 @@ public int MinLinesAfterBreak
/// raised from the save, not from this property, as <see cref="ArgumentOutOfRangeException"/>
/// naming <c>top</c>, when the destination's top is not finite or lies beyond ±3.403 × 10^38,
/// the range of a PDF real (ISO 32000-1, Table C.1): a top margin below -3.403 × 10^38 or equal
/// to negative infinity on a paragraph or heading, and also NaN on an image. A paragraph with a
/// NaN top margin never gets that far: it is refused as too tall to fit, anchored or not. The
/// layout had started, so the document cannot be saved again.
/// to negative infinity on a paragraph or heading, and also NaN on an image. A page taller than
/// 3.403 × 10^38 points raises it as well. A paragraph with a NaN top margin never gets that
/// far: it is refused as too tall to fit, anchored or not. The layout had started, so the
/// document cannot be saved again.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Raised from the save, not from this property, when another element in the document holds the
Expand Down
51 changes: 49 additions & 2 deletions src/VellumPdf.Layout/Elements/Preformatted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ namespace VellumPdf.Layout.Elements;
/// <see cref="Document.StrictDiagnostics"/> refuses that save.</para>
/// <para><b>What is ignored.</b> <see cref="Document.SetDefaultFont"/> does not apply, as for lists
/// and tables. Alignment, justification and hyphenation do not exist on this element. A paragraph
/// tab stop does not apply: a tab is a number of spaces. No language is written for the block. The
/// element is not a target of an anchor.</para>
/// tab stop does not apply: a tab is a number of spaces. No language is written for the
/// block.</para>
/// <para>In a tagged document each drawn piece, the part of the block on one page, is one <c>P</c>
/// structure element. Nothing in the character stream separates two lines, so a reader that follows
/// the structure order alone sees the lines run together.</para>
Expand Down Expand Up @@ -176,6 +176,53 @@ public PageBreakMode BreakBefore
init => _breakBefore = PageBreakModes.Validate(value);
}

/// <summary>
/// A name that a link or an outside reference can jump to: the save adds a named destination at
/// the first line of this block. <see langword="null"/>, the default, adds none.
/// </summary>
/// <remarks>
/// <b>A name outside the rule is refused by this <c>init</c> accessor.</b> It throws
/// <see cref="ArgumentException"/> for an empty name, one longer than 127 characters, or one
/// with a character outside <c>A-Z a-z 0-9 . _ - :</c>, as <see cref="Paragraph.Anchor"/> does,
/// and for the same reason.
/// <para><b>A repeated anchor is refused by the save, before any layout.</b> The save throws
/// <see cref="InvalidOperationException"/> naming the anchor and the kind and zero-based
/// position of both elements that hold it, whatever their kinds: a paragraph and a block
/// holding one name are refused as two paragraphs are. Nothing has been laid out then, so the
/// document can be saved once the elements are replaced. The same instance added twice holds
/// its anchor twice.</para>
/// <para>The destination is written whether or not a link refers to it, so a reader can open
/// the file at it by name (<c>file.pdf#nameddest=name</c>). Its position is the top edge of the
/// block's first line, below <see cref="Margins"/>, at the left edge of the page, with the
/// viewer's zoom unchanged. When the block splits across pages, that is the page its first
/// line is on, and no later piece reports the anchor. The destinations are written in the
/// order of their names, so the order the elements were added in does not change a key.</para>
/// <para>Names are compared byte by byte, so <c>Code</c> and <c>code</c> are two destinations,
/// as they are on <see cref="Paragraph.Anchor"/>.</para>
/// </remarks>
/// <exception cref="ArgumentException">
/// Raised from this <c>init</c> accessor when the name breaks the rule above.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// Raised from the save, not from this property, naming <c>top</c>, when the page is taller
/// than 3.403 × 10^38 points: the destination's top is then beyond the range of a PDF real
/// (ISO 32000-1, Table C.1). <see cref="Margins"/> cannot cause it, because it refuses a
/// negative or non-finite edge. A page of exactly 3.403 × 10^38 saves, and the same block with
/// no anchor saves on a taller page. The layout had started, so the document cannot be saved
/// again.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Raised from the save, not from this property, before any layout, when another element in
/// the document holds the same anchor.
/// </exception>
public string? Anchor
{
get => _anchor;
init => _anchor = AnchorNames.Validate(value);
}

private string? _anchor;

/// <summary>Creates a block from a string, in the given style or in Courier at 10 pt.</summary>
/// <remarks>
/// <b>This constructor refuses a null text, a refused character and a refused style.</b> It
Expand Down
2 changes: 2 additions & 0 deletions src/VellumPdf.Layout/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ VellumPdf.Layout.LayoutDiagnosticCode.NotdefGlyph = 102 -> VellumPdf.Layout.Layo
VellumPdf.Layout.LayoutDiagnosticCode.UnembeddedFont = 601 -> VellumPdf.Layout.LayoutDiagnosticCode
VellumPdf.Layout.Document.Add(VellumPdf.Layout.Elements.Preformatted! preformatted) -> VellumPdf.Layout.Document!
VellumPdf.Layout.Elements.Preformatted
VellumPdf.Layout.Elements.Preformatted.Anchor.get -> string?
VellumPdf.Layout.Elements.Preformatted.Anchor.init -> void
VellumPdf.Layout.Elements.Preformatted.BreakBefore.get -> VellumPdf.Layout.Core.PageBreakMode
VellumPdf.Layout.Elements.Preformatted.BreakBefore.init -> void
VellumPdf.Layout.Elements.Preformatted.Margins.get -> VellumPdf.Layout.Core.EdgeInsets
Expand Down
3 changes: 3 additions & 0 deletions src/VellumPdf.Layout/Rendering/NavigationPlan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ internal static NavigationPlan Build(IReadOnlyList<IRenderer> renderers)
case LayoutImageRenderer image:
plan.Add(image.Element.Anchor, LayoutElementKind.Image, i);
break;
case PreformattedRenderer block:
plan.Add(block.Element.Anchor, LayoutElementKind.Preformatted, i);
break;
case ListRenderer list:
plan.WalkList(list.Element, i, ref deep);
break;
Expand Down
12 changes: 11 additions & 1 deletion src/VellumPdf.Layout/Rendering/PreformattedRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace VellumPdf.Layout.Rendering;
/// Both passes lay out the same cached lines and read no pagination plan, so the measuring pass and
/// the drawing pass agree by construction.
/// </remarks>
internal sealed class PreformattedRenderer : IRenderer, IPageBreakSource
internal sealed class PreformattedRenderer : IRenderer, IPageBreakSource, IPlacementSource
{
// The continuation marker is a hook drawn with two open subpaths inside one column, 0.6 em wide
// (the width the wrap reserves, PreformattedText.MarkerUnits), between the baseline and the cap
Expand Down Expand Up @@ -69,6 +69,16 @@ internal PreformattedRenderer(Preformatted element, int startLine = 0)

PageBreakMode IPageBreakSource.BreakBefore => _element.BreakBefore;

// Explicit, as in the other renderers. The anchor lands where the first line does, so only the
// piece that starts at line 0 reports it; the overflow of a split block starts later and
// reports nothing, which keeps the destination on the page the first line is on. The box is
// the occupied area, whose top is the top of the first line.
void IPlacementSource.ReportPlacements(PlacementSink sink)
{
if (_startLine == 0 && _element.Anchor is { } anchor)
sink.ReportAnchor(_element, anchor, _occupied);
}

public LayoutResult Layout(LayoutContext context)
{
var area = context.Area.Deflate(_element.Margins);
Expand Down
Loading
Loading