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
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- The marker the library draws on a list item, such as the nested bullet in a Standard 14 font,
is not listed as an unencodable character when the document draws the list. A list that your
own renderer draws through a `DrawContext` you built can still list it.
- **`PdfCanvas.NotdefGlyphs`, `PdfDocument.NotdefGlyphs` and `NotdefGlyphWarning` (#592).** A
character an embedded font has no glyph for is written as glyph 0, and until now nothing said
so. `PdfCanvas.NotdefGlyphs` lists each glyph 0 the canvas writes, one entry per character and
font resource in order of first occurrence, with a `Count`; `PdfDocument.NotdefGlyphs` gathers
the lists of the pages whose canvas was finished. `Character` is null for glyph ids passed to
`PdfCanvas.ShowGlyphs`, because the canvas sees ids and not text. Each list holds at most 1,000
entries, and `NotdefGlyphsDropped` counts the rest. `TextEncodingWarnings` is unchanged and does
not list glyph 0.
- **`LayoutDiagnosticCode.NotdefGlyph` (#592).** The layout reports each character and font
that was written as glyph 0, with the count, in `Document.Diagnostics`: built-in elements, list
markers and running bands name the character, and a custom renderer that passes glyph ids is
reported with the character unknown. The clause is ISO 19005-2:2011, 6.2.11.8 under a PDF/A
claim and ISO 14289-1:2014, 7.21.8 under PDF/UA-1; it is null with no claim. The save still
succeeds. `StrictDiagnostics` refuses it.
- **`LayoutDiagnosticCode.UnembeddedFont` (#593).** While `Document.Conformance` is not `None`,
each paragraph, heading, list, table cell or running band that selects a named Standard 14 font
is reported once per element and font, with the number of font operators it wrote. A list,
table, heading, paragraph or running band without a style of its own reaches this, because
`SetDefaultFont` still applies only to `Document.Add(string)`. The clause is ISO
19005-2:2011, 6.2.11.4.1 or ISO 14289-1:2014, 7.21.4.1. The save still succeeds, and
`StrictDiagnostics` refuses it.

**Attention:** the report follows the font operator, so an empty table cell in Helvetica is
reported. That matches veraPDF, which fails 6.2.11.4.1 on it, while ISO 14289-1, 7.21.4.1,
counts a font as used only when a glyph is referenced. The human-readable text of a
one-dimensional barcode, set in `Barcode1D.TextFont` (Helvetica by default), selects its font
without the diagnostics channel and is not reported.
- **Internal name tree and number tree writers (#241).** `NameTreeWriter` and `NumberTreeWriter`
in `VellumPdf.Kernel` build the structures ISO 32000-2, 7.9.6 and 7.9.7 define, which the
catalog's `/Names` and `/PageLabels` entries, and later named destinations and embedded files,
Expand Down Expand Up @@ -392,6 +419,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
change:** every `/FontFile2` stream the Kernel writes gains `/Length1 <n>`, so the stream
dictionary, the offsets and the cross-reference move; the program bytes do not change. An
OpenType CFF program is a `/FontFile3` stream and carries no `/Length1`, as before.
- **The ToUnicode map no longer lists a code point against glyph 0 (#592).** A font whose
subset used glyph 0 wrote one `<0000> <...>` line per missing character. On a test file with
U+26B9 and U+1F31F, pdftotext (poppler 26.08) extracted U+1F31F for both. The map now has one
entry for glyph 0, `<0000> <FFFD>`, and no other. The entry exists because glyph 0 is itself
what ISO 19005-2, 6.2.11.8, and ISO 14289-1, 7.21.8, refuse, and U+FFFD satisfies the value
rule of 7.21.7. A document in which no character was mapped to glyph 0, drawn or only
measured, is unchanged. One in which a character was mapped to it changes only in that
stream, and the content stream still shows glyph 0. Glyph ids passed straight to
`PdfCanvas.ShowGlyphs` add no entry.

**Attention:** the file still references `.notdef`, so veraPDF still fails ISO 19005-2,
6.2.11.8 and ISO 14289-1, 7.21.8. The reports above are how to find it.

- **On .NET 10 an empty stream was written as `/FlateDecode` with no data (#640).** `PdfStream`
compressed through `ZLibStream`, which on .NET 10.0.15 writes nothing for empty input, so an empty
page's `/Contents` came out as `/Filter /FlateDecode /Length 0` with an empty body, which is not
Expand Down
7 changes: 7 additions & 0 deletions src/VellumPdf.Barcodes/Barcode1D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ private protected Barcode1D()
public bool ShowText { get; init; } = true;

/// <summary>The Standard-14 font used for the HRI text. Defaults to <see cref="Standard14.Helvetica"/>.</summary>
/// <remarks>
/// The font is not embedded. A document with a PDF/A or PDF/UA conformance claim that draws
/// this text is not told so by <c>VellumPdf.Layout</c>: the barcode selects the font without
/// its diagnostics channel, so no <c>UnembeddedFont</c> item is reported for it. <b>Do not
/// rely</b> on an empty <c>Document.Diagnostics</c> to show that every font in such a document
/// is embedded; set <see cref="ShowText"/> to <c>false</c> or draw the text yourself.
/// </remarks>
public Standard14 TextFont { get; init; } = Standard14.Helvetica;

/// <summary>The HRI text size, in points. Zero (the default) sizes it automatically from <see cref="BarHeight"/>.</summary>
Expand Down
79 changes: 79 additions & 0 deletions src/VellumPdf.Kernel/Canvas/NotdefGlyphTally.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © Timothy van der Ham (@Tim81)
// SPDX-License-Identifier: Apache-2.0

using System.Text;

namespace VellumPdf.Canvas;

/// <summary>
/// The glyph-0 writes of one canvas: at most <see cref="MaxEntries"/> distinct (character, font
/// resource) entries in order of first occurrence, and a count of the occurrences that did not fit.
/// Created on the first glyph 0, so a canvas without one allocates nothing for it.
/// </summary>
internal sealed class NotdefGlyphTally
{
/// <summary>
/// The most entries held per canvas and per document. It is the cap the layout channel uses,
/// not a figure any standard gives; it bounds memory against text that produces a distinct
/// entry per character.
/// </summary>
internal const int MaxEntries = 1000;

// A code point that cannot be a Unicode scalar stands for "character not known".
internal const int UnknownCodePoint = 0x110000;

private readonly List<NotdefGlyphWarning> _entries = [];
private readonly Dictionary<(int CodePoint, string? Resource), int> _index = [];

internal NotdefGlyphTally()
{
}

private NotdefGlyphTally(NotdefGlyphTally source)
{
foreach (var e in source._entries)
{
_index[(CodePointOf(e.Character), e.FontResourceName)] = _entries.Count;
_entries.Add(new NotdefGlyphWarning(e.Character, e.FontResourceName, e.FontName, e.Count));
}
Dropped = source.Dropped;
Total = source.Total;
}

internal IReadOnlyList<NotdefGlyphWarning> Entries => _entries;

/// <summary>Occurrences not stored because the entry would have been past the cap.</summary>
internal int Dropped { get; private set; }

/// <summary>Every occurrence, stored or dropped. Changes whenever anything is added.</summary>
internal int Total { get; private set; }

internal static int CodePointOf(Rune? character) => character?.Value ?? UnknownCodePoint;

internal void Add(Rune? character, string? resource, string? fontName, int count = 1)
{
Total = Saturate(Total, count);
var key = (CodePointOf(character), resource);
if (_index.TryGetValue(key, out var at))
{
var entry = _entries[at];
entry.Count = Saturate(entry.Count, count);
return;
}

if (_entries.Count >= MaxEntries)
{
Dropped = Saturate(Dropped, count);
return;
}

_index[key] = _entries.Count;
_entries.Add(new NotdefGlyphWarning(character, resource, fontName, count));
}

/// <summary>A copy that later writes to this tally do not change.</summary>
internal NotdefGlyphTally Snapshot() => new(this);

internal static int Saturate(int current, int add) =>
add > int.MaxValue - current ? int.MaxValue : current + add;
}
88 changes: 88 additions & 0 deletions src/VellumPdf.Kernel/Canvas/NotdefGlyphWarning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright © Timothy van der Ham (@Tim81)
// SPDX-License-Identifier: Apache-2.0

using System.Text;

namespace VellumPdf.Canvas;

/// <summary>
/// One character, in one font, that a canvas wrote as glyph 0 (the <c>.notdef</c> glyph) because
/// the font has no glyph for it, with the number of times it was written. Read the entries from
/// <see cref="PdfCanvas.NotdefGlyphs"/> and
/// <see cref="VellumPdf.Document.PdfDocument.NotdefGlyphs"/>.
/// </summary>
/// <remarks>
/// <b>Nothing here is refused and nothing throws.</b> The type has no public constructor and only
/// getters, so it takes no input that could be wrong. ISO 19005-2, 6.2.11.8, and ISO 14289-1,
/// 7.21.8, forbid a reference to the <c>.notdef</c> glyph in a PDF/A-2 or PDF/UA-1 file; this
/// type reports that a canvas wrote one and leaves the decision to the caller.
/// <para><see cref="PdfCanvas.NotdefGlyphs"/> lists an entry at once for every glyph id 0 the
/// canvas writes through <see cref="PdfCanvas.ShowGlyphs(System.ReadOnlySpan{ushort})"/> and
/// through the library's own text drawing, past the 1,000 entries it holds;
/// <see cref="PdfCanvas.NotdefGlyphsDropped"/> counts the occurrences of those. It has none for
/// content bytes set directly on a <see cref="VellumPdf.Document.PdfPage"/>.
/// <see cref="VellumPdf.Document.PdfDocument.NotdefGlyphs"/> lists only what each page's last
/// <see cref="PdfCanvas.Finish"/> stored, so a canvas whose <c>Finish</c> has not run adds
/// nothing there. <b>Do not</b> read an empty list as proof that the file holds no glyph 0.</para>
/// <para>Reading an entry while another thread draws on the canvas that holds it is not
/// supported.</para>
/// </remarks>
public sealed class NotdefGlyphWarning
{
internal NotdefGlyphWarning(Rune? character, string? fontResourceName, string? fontName, int count)
{
Character = character;
FontResourceName = fontResourceName;
FontName = fontName;
Count = count;
}

/// <summary>
/// The character that had no glyph, or null when the canvas never saw the text.
/// </summary>
/// <remarks>
/// <b>Nothing here is refused and nothing throws.</b> The value is null for glyph ids passed to
/// <see cref="PdfCanvas.ShowGlyphs(System.ReadOnlySpan{ushort})"/>, because the canvas receives
/// ids and not text. The caller of that method holds the text and can find the characters with
/// <c>EmbeddedFontHandle.GetGlyphId</c>, which returns 0 for exactly these. A character outside
/// the Basic Multilingual Plane is one <see cref="Rune"/>, so it is one entry.
/// </remarks>
public Rune? Character { get; }

/// <summary>
/// The resource name the content stream selects the font by, such as <c>TT1</c>, or null when
/// no font had been selected on the canvas.
/// </summary>
/// <remarks>
/// <b>Nothing here is refused and nothing throws.</b> The name is the one given to the last
/// <see cref="PdfCanvas.SetFont(VellumPdf.Fonts.PdfFontResource, double)"/> or
/// <see cref="PdfCanvas.SetFontByName(string, double)"/> call on the canvas. It does not follow
/// a font that <see cref="PdfCanvas.RestoreState"/> brings back. Resource names are counters
/// local to one document, so within a document the pair of character and name identifies one
/// character in one font.
/// </remarks>
public string? FontResourceName { get; }

/// <summary>
/// The font's PostScript name without the subset tag, such as <c>DejaVuSans</c>, or null when
/// the canvas does not know it.
/// </summary>
/// <remarks>
/// <b>Nothing here is refused and nothing throws.</b> Only the library's own text drawing knows
/// the name. Glyph ids passed to
/// <see cref="PdfCanvas.ShowGlyphs(System.ReadOnlySpan{ushort})"/> leave it null; use
/// <see cref="FontResourceName"/> to find the font.
/// </remarks>
public string? FontName { get; }

/// <summary>
/// The number of times glyph 0 was written for this character in this font, saturating at
/// <see cref="int.MaxValue"/>.
/// </summary>
/// <remarks>
/// <b>Nothing here is refused and nothing throws.</b> The list a canvas returns is live, so the
/// count of an entry grows while the canvas draws. <b>Do not</b> read it from another thread
/// while the canvas draws.
/// </remarks>
public int Count { get; internal set; }
}
Loading
Loading