Miscellaneous minor changes

This commit is contained in:
BobLd
2025-05-31 22:25:07 +01:00
parent fe3d15d5db
commit 8f9194c9a4
18 changed files with 147 additions and 60 deletions

View File

@@ -53,5 +53,17 @@
{
return $"{ObjectNumber} {Generation}";
}
/// <inheritdoc/>
public static bool operator ==(IndirectReference left, IndirectReference right)
{
return left.Equals(right);
}
/// <inheritdoc/>
public static bool operator !=(IndirectReference left, IndirectReference right)
{
return !(left == right);
}
}
}

View File

@@ -10,7 +10,7 @@
/// The Y-axis extends vertically upwards and the X-axis horizontally to the right.
/// Unless otherwise specified on a per-page basis, units in PDF space are equivalent to a typographic point (1/72 inch).
/// </remarks>
public readonly struct PdfLine
public readonly struct PdfLine : IEquatable<PdfLine>
{
/// <summary>
/// Length of the line.
@@ -70,14 +70,17 @@
/// <summary>
/// Returns a value indicating whether this <see cref="PdfLine"/> is equal to a specified <see cref="PdfLine"/> .
/// </summary>
/// <param name="obj"></param>
public override bool Equals(object obj)
{
if (obj is PdfLine line)
{
return line.Point1.Equals(Point1) && line.Point2.Equals(Point2);
}
return false;
return obj is PdfLine other && Equals(other);
}
/// <summary>
/// Returns a value indicating whether this <see cref="PdfLine"/> is equal to a specified <see cref="PdfLine"/> .
/// </summary>
public bool Equals(PdfLine other)
{
return Point1.Equals(other.Point1) && Point2.Equals(other.Point2);
}
/// <summary>
@@ -87,5 +90,17 @@
{
return HashCode.Combine(Point1, Point2);
}
/// <inheritdoc/>
public static bool operator ==(PdfLine left, PdfLine right)
{
return left.Equals(right);
}
/// <inheritdoc/>
public static bool operator !=(PdfLine left, PdfLine right)
{
return !(left == right);
}
}
}

View File

@@ -12,7 +12,7 @@
/// The Y-axis extends vertically upwards and the X-axis horizontally to the right.
/// Unless otherwise specified on a per-page basis, units in PDF space are equivalent to a typographic point (1/72 inch).
/// </remarks>
public readonly struct PdfPoint
public readonly struct PdfPoint : IEquatable<PdfPoint>
{
/// <summary>
/// The origin of the coordinates system.
@@ -83,14 +83,17 @@
/// <summary>
/// Returns a value indicating whether this <see cref="PdfPoint"/> is equal to a specified <see cref="PdfPoint"/> .
/// </summary>
/// <param name="obj"></param>
public override bool Equals(object obj)
{
if (obj is PdfPoint point)
{
return point.X == X && point.Y == Y;
}
return false;
return obj is PdfPoint other && Equals(other);
}
/// <summary>
/// Returns a value indicating whether this <see cref="PdfPoint"/> is equal to a specified <see cref="PdfPoint"/> .
/// </summary>
public bool Equals(PdfPoint other)
{
return X.Equals(other.X) && Y.Equals(other.Y);
}
/// <summary>
@@ -106,5 +109,17 @@
{
return $"(x:{X.ToString(CultureInfo.InvariantCulture)}, y:{Y.ToString(CultureInfo.InvariantCulture)})";
}
/// <inheritdoc/>
public static bool operator ==(PdfPoint left, PdfPoint right)
{
return left.Equals(right);
}
/// <inheritdoc/>
public static bool operator !=(PdfPoint left, PdfPoint right)
{
return !(left == right);
}
}
}

View File

@@ -11,7 +11,7 @@
/// The Y-axis extends vertically upwards and the X-axis horizontally to the right.
/// Unless otherwise specified on a per-page basis, units in PDF space are equivalent to a typographic point (1/72 inch).
/// </remarks>
public readonly struct PdfRectangle
public readonly struct PdfRectangle : IEquatable<PdfRectangle>
{
/// <summary>
/// Top left point of the rectangle.
@@ -166,5 +166,38 @@
{
return $"[{TopLeft}, {Width.ToString(CultureInfo.InvariantCulture)}, {Height.ToString(CultureInfo.InvariantCulture)}]";
}
/// <inheritdoc />
public bool Equals(PdfRectangle other)
{
return TopLeft.Equals(other.TopLeft) &&
TopRight.Equals(other.TopRight) &&
BottomRight.Equals(other.BottomRight) &&
BottomLeft.Equals(other.BottomLeft);
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is PdfRectangle other && Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(TopLeft, TopRight, BottomRight, BottomLeft);
}
/// <inheritdoc/>
public static bool operator ==(PdfRectangle left, PdfRectangle right)
{
return left.Equals(right);
}
/// <inheritdoc/>
public static bool operator !=(PdfRectangle left, PdfRectangle right)
{
return !(left == right);
}
}
}

View File

@@ -32,17 +32,17 @@
{
if (stream == null)
{
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(stream));
}
if (!stream.CanRead)
{
throw new ArgumentException("The provided stream did not support reading.");
throw new ArgumentException("The provided stream did not support reading.", nameof(stream));
}
if (!stream.CanSeek)
{
throw new ArgumentException("The provided stream did not support seeking.");
throw new ArgumentException("The provided stream did not support seeking.", nameof(stream));
}
this.stream = stream;

View File

@@ -6,7 +6,7 @@
/// <summary>
/// Specifies the conversion from the transformed coordinate space to the original untransformed coordinate space.
/// </summary>
public readonly struct TransformationMatrix
public readonly struct TransformationMatrix : IEquatable<TransformationMatrix>
{
/// <summary>
/// The default <see cref="TransformationMatrix"/>.
@@ -465,12 +465,21 @@
/// <inheritdoc />
public override bool Equals(object obj)
{
if (!(obj is TransformationMatrix m))
{
return false;
}
return obj is TransformationMatrix other && Equals(other);
}
return Equals(this, m);
/// <inheritdoc />
public bool Equals(TransformationMatrix other)
{
return row1.Equals(other.row1) &&
row2.Equals(other.row2) &&
row3.Equals(other.row3) &&
A.Equals(other.A) &&
B.Equals(other.B) &&
C.Equals(other.C) &&
D.Equals(other.D) &&
E.Equals(other.E) &&
F.Equals(other.F);
}
/// <summary>
@@ -478,25 +487,13 @@
/// </summary>
public static bool Equals(TransformationMatrix a, TransformationMatrix b)
{
for (var i = 0; i < Rows; i++)
{
for (var j = 0; j < Columns; j++)
{
if (a[i, j] != b[i, j])
{
return false;
}
}
}
return true;
return a.Equals(b);
}
/// <inheritdoc />
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(row1);
hashCode.Add(row2);
hashCode.Add(row3);
@@ -506,7 +503,6 @@
hashCode.Add(D);
hashCode.Add(E);
hashCode.Add(F);
return hashCode.ToHashCode();
}

View File

@@ -187,7 +187,7 @@
/// <summary>
/// When parsing PDF files with tables containing multiple lines in a cell or "merged" cells,
/// the separate words can appear out of horizontal order. This option can better predict the
// spaces between the words. Default <see langword="false"/>.
/// spaces between the words. Default <see langword="false"/>.
/// </summary>
public bool NegativeGapAsWhitespace { get; set; }
}

View File

@@ -22,7 +22,7 @@ namespace UglyToad.PdfPig.Fonts.TrueType.Glyphs
r2c1 = r2C1;
}
public static CompositeTransformMatrix3By2 Identity { get; } = new CompositeTransformMatrix3By2(1, 0, 0, 1, 0, 0);
public static readonly CompositeTransformMatrix3By2 Identity = new CompositeTransformMatrix3By2(1, 0, 0, 1, 0, 0);
public static CompositeTransformMatrix3By2 CreateTranslation(double x, double y) => new CompositeTransformMatrix3By2(1, 0, 0, 1, x, y);
public CompositeTransformMatrix3By2 WithTranslation(double x, double y)

View File

@@ -183,7 +183,7 @@
return result;
}
private static IToken PeekNext(IReadOnlyList<IToken> tokens, int currentIndex)
private static IToken PeekNext(List<IToken> tokens, int currentIndex)
{
if (tokens.Count - 1 < currentIndex + 1)
{

View File

@@ -6,7 +6,7 @@
/// Parser for PDF Type 4 functions. This implements a small subset of the PostScript
/// language but is no full PostScript interpreter.
/// </summary>
internal sealed class Parser
internal static class Parser
{
/// <summary>
/// Used to indicate the parsers current state.
@@ -16,11 +16,6 @@
NEWLINE, WHITESPACE, COMMENT, TOKEN
}
private Parser()
{
//nop
}
/// <summary>
/// Parses a Type 4 function and sends the syntactic elements to the given syntax handler.
/// </summary>

View File

@@ -317,6 +317,9 @@
/// </summary>
public ReadOnlySpan<byte> ColorTable => colorTable;
/// <summary>
/// Create a new <see cref="IndexedColorSpaceDetails"/>.
/// </summary>
public IndexedColorSpaceDetails(ColorSpaceDetails baseColorSpaceDetails, byte hiVal, byte[] colorTable)
: this(baseColorSpaceDetails, hiVal, colorTable, false)
{ }

View File

@@ -5,7 +5,7 @@
/// <summary>
/// A pixel in a <see cref="Png"/> image.
/// </summary>
internal readonly struct Pixel
internal readonly struct Pixel : IEquatable<Pixel>
{
/// <summary>
/// The red value for the pixel.
@@ -104,5 +104,17 @@
{
return $"({R}, {G}, {B}, {A})";
}
/// <inheritdoc/>
public static bool operator ==(Pixel left, Pixel right)
{
return left.Equals(right);
}
/// <inheritdoc/>
public static bool operator !=(Pixel left, Pixel right)
{
return !(left == right);
}
}
}

View File

@@ -8,6 +8,7 @@
using Tokenization;
using Tokenization.Scanner;
using Tokens;
using UglyToad.PdfPig.Util;
internal static class CrossReferenceTableParser
{
@@ -124,7 +125,7 @@
&& (operatorToken.Data == OperatorToken.Xref.Data
|| (isLenientParsing
&& operatorToken.Data.StartsWith(OperatorToken.Xref.Data)
&& int.TryParse(operatorToken.Data.Substring(OperatorToken.Xref.Data.Length), out _))));
&& int.TryParse(operatorToken.Data.AsSpanOrSubstring(OperatorToken.Xref.Data.Length), out _))));
}
private static int ProcessTokens(ReadOnlySpan<IToken> tokens, CrossReferenceTablePartBuilder builder, bool isLenientParsing,

View File

@@ -64,7 +64,7 @@
private static HeaderVersion GetHeaderVersionAndResetScanner(CommentToken comment, ISeekableTokenScanner scanner, bool isLenientParsing, ILog log)
{
if (comment.Data.IndexOf("PDF-1.", StringComparison.OrdinalIgnoreCase) != 0 && comment.Data.IndexOf("FDF-1.", StringComparison.OrdinalIgnoreCase) != 0)
if (!comment.Data.StartsWith("PDF-1.", StringComparison.OrdinalIgnoreCase) && !comment.Data.StartsWith("FDF-1.", StringComparison.OrdinalIgnoreCase))
{
return HandleMissingVersion(comment, isLenientParsing, log);
}

View File

@@ -148,7 +148,7 @@
{
newOffset = -1;
BruteForceSearchForStartxref(reader);
long newStartXRefOffset = SearchNearestValue(bfSearchStartXRefTablesOffsets, xrefOffset);
long newStartXRefOffset = SearchNearestValue(bfSearchStartXRefTablesOffsets!, xrefOffset);
if (newStartXRefOffset < reader.Length)
{
long tempNewOffset = -1;

View File

@@ -137,7 +137,7 @@
}
}
private static IEnumerable<long> GetObjectNumbers(DictionaryToken dictionary)
private static ReadOnlySpan<long> GetObjectNumbers(DictionaryToken dictionary)
{
// The number one greater than the highest object number used in this section or in any section for which this is an update.
if (!dictionary.TryGet(NameToken.Size, out var sizeToken) || !(sizeToken is NumericToken sizeNumeric))
@@ -170,7 +170,11 @@
}
}
return objNums;
#if NET
return System.Runtime.InteropServices.CollectionsMarshal.AsSpan(objNums);
#else
return objNums.ToArray();
#endif
}
}
}

View File

@@ -88,7 +88,7 @@
{
if (objectToken is null)
{
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(objectToken));
}
// Don't cache incorrect locations.

View File

@@ -5,7 +5,7 @@
using System.IO;
using Tokens;
internal class PdfDedupStreamWriter : PdfStreamWriter
internal sealed class PdfDedupStreamWriter : PdfStreamWriter
{
private readonly Dictionary<byte[], IndirectReferenceToken> hashes = new Dictionary<byte[], IndirectReferenceToken>(new FNVByteComparison());
@@ -69,7 +69,7 @@
base.Dispose();
}
class FNVByteComparison : IEqualityComparer<byte[]>
private sealed class FNVByteComparison : IEqualityComparer<byte[]>
{
public bool Equals(byte[] x, byte[] y)
{
@@ -91,7 +91,7 @@
/// <summary>
/// A hash combiner that is implemented with the Fowler/Noll/Vo algorithm (FNV-1a). This is a mutable struct for performance reasons.
/// </summary>
struct FnvHash
private struct FnvHash
{
/// <summary>
/// The starting point of the FNV hash.
@@ -113,9 +113,10 @@
/// </summary>
public static FnvHash Create()
{
var result = new FnvHash();
result.HashCode = Offset;
return result;
return new FnvHash
{
HashCode = Offset
};
}
/// <summary>