Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all structs readonly when possible #707

Merged
merged 2 commits into from
Oct 18, 2023
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
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Core/IndirectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
/// Used to uniquely identify and refer to objects in the PDF file.
/// </summary>
public struct IndirectReference
public readonly struct IndirectReference
{
/// <summary>
/// A positive integer object number.
Expand Down
6 changes: 2 additions & 4 deletions src/UglyToad.PdfPig.Core/PdfLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
using System;

/// <summary>
/// A line in a PDF file.
/// A line in a PDF file.
/// </summary>
/// <remarks>
/// PDF coordinates are defined with the origin at the lower left (0, 0).
/// 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 struct PdfLine
public readonly struct PdfLine
{
/// <summary>
/// Length of the line.
Expand Down Expand Up @@ -71,7 +71,6 @@ public PdfRectangle GetBoundingRectangle()
/// Returns a value indicating whether this <see cref="PdfLine"/> is equal to a specified <see cref="PdfLine"/> .
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is PdfLine line)
Expand All @@ -84,7 +83,6 @@ public override bool Equals(object obj)
/// <summary>
/// Returns the hash code for this <see cref="PdfLine"/>.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return (Point1, Point2).GetHashCode();
Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig.Core/PdfPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using System.Globalization;

/// <summary>
/// A point in a PDF file.
/// A point in a PDF file.
/// </summary>
/// <remarks>
/// PDF coordinates are defined with the origin at the lower left (0, 0).
/// 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 struct PdfPoint
public readonly struct PdfPoint
{
/// <summary>
/// The origin of the coordinates system.
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Core/PdfRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// <summary>
/// This class will be used to signify a range. a(min) &lt;= a* &lt;= a(max)
/// </summary>
public struct PdfRange
public readonly struct PdfRange
{
private readonly IReadOnlyList<double> rangeArray;
private readonly int startingIndex;
Expand Down
32 changes: 12 additions & 20 deletions src/UglyToad.PdfPig.Core/PdfRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Globalization;

/// <summary>
/// A rectangle in a PDF file.
/// A rectangle in a PDF file.
/// </summary>
/// <remarks>
/// PDF coordinates are defined with the origin at the lower left (0, 0).
Expand Down Expand Up @@ -36,7 +36,7 @@ public struct PdfRectangle
/// <summary>
/// Centroid point of the rectangle.
/// </summary>
public PdfPoint Centroid
public readonly PdfPoint Centroid
{
get
{
Expand Down Expand Up @@ -86,13 +86,7 @@ public double Height
/// Rotation angle of the rectangle. Counterclockwise, in degrees.
/// <para>-180 ≤ θ ≤ 180</para>
/// </summary>
public double Rotation
{
get
{
return GetT() * 180 / Math.PI;
}
}
public readonly double Rotation => GetT() * 180 / Math.PI;

/// <summary>
/// Area of the rectangle.
Expand All @@ -102,22 +96,22 @@ public double Rotation
/// <summary>
/// Left. This value is only valid if the rectangle is not rotated, check <see cref="Rotation"/>.
/// </summary>
public double Left => TopLeft.X < TopRight.X ? TopLeft.X : TopRight.X;
public readonly double Left => TopLeft.X < TopRight.X ? TopLeft.X : TopRight.X;

/// <summary>
/// Top. This value is only valid if the rectangle is not rotated, check <see cref="Rotation"/>.
/// </summary>
public double Top => TopLeft.Y > BottomLeft.Y ? TopLeft.Y : BottomLeft.Y;
public readonly double Top => TopLeft.Y > BottomLeft.Y ? TopLeft.Y : BottomLeft.Y;

/// <summary>
/// Right. This value is only valid if the rectangle is not rotated, check <see cref="Rotation"/>.
/// </summary>
public double Right => BottomRight.X > BottomLeft.X ? BottomRight.X : BottomLeft.X;
public readonly double Right => BottomRight.X > BottomLeft.X ? BottomRight.X : BottomLeft.X;

/// <summary>
/// Bottom. This value is only valid if the rectangle is not rotated, check <see cref="Rotation"/>.
/// </summary>
public double Bottom => BottomRight.Y < TopRight.Y ? BottomRight.Y : TopRight.Y;
public readonly double Bottom => BottomRight.Y < TopRight.Y ? BottomRight.Y : TopRight.Y;

/// <summary>
/// Create a new <see cref="PdfRectangle"/>.
Expand Down Expand Up @@ -171,7 +165,7 @@ public PdfRectangle(PdfPoint topLeft, PdfPoint topRight, PdfPoint bottomLeft, Pd
/// <param name="dx">The distance to move the rectangle in the x direction relative to its current location.</param>
/// <param name="dy">The distance to move the rectangle in the y direction relative to its current location.</param>
/// <returns>A new rectangle shifted on the y axis by the given delta value.</returns>
public PdfRectangle Translate(double dx, double dy)
public readonly PdfRectangle Translate(double dx, double dy)
{
return new PdfRectangle(TopLeft.Translate(dx, dy), TopRight.Translate(dx, dy),
BottomLeft.Translate(dx, dy), BottomRight.Translate(dx, dy));
Expand All @@ -180,17 +174,15 @@ public PdfRectangle Translate(double dx, double dy)
/// <summary>
/// -π ≤ θ ≤ π
/// </summary>
private double GetT()
private readonly double GetT()
{
if (!BottomRight.Equals(BottomLeft))
{
return Math.Atan2(BottomRight.Y - BottomLeft.Y, BottomRight.X - BottomLeft.X);
}
else
{
// handle the case where both bottom points are identical
return Math.Atan2(TopLeft.Y - BottomLeft.Y, TopLeft.X - BottomLeft.X) - Math.PI / 2;
}

// handle the case where both bottom points are identical
return Math.Atan2(TopLeft.Y - BottomLeft.Y, TopLeft.X - BottomLeft.X) - Math.PI / 2;
}

private void GetWidthHeight()
Expand Down
62 changes: 31 additions & 31 deletions src/UglyToad.PdfPig.Core/TransformationMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,47 +135,47 @@ public static TransformationMatrix GetRotationMatrix(double degreesCounterclockw
switch (row)
{
case 0:
{
switch (col)
{
switch (col)
{
case 0:
return A;
case 1:
return B;
case 2:
return row1;
default:
case 0:
return A;
case 1:
return B;
case 2:
return row1;
default:
throw new ArgumentOutOfRangeException($"Trying to access {row}, {col} which was not in the value array.");
}
}
}
case 1:
{
switch (col)
{
switch (col)
{
case 0:
return C;
case 1:
return D;
case 2:
return row2;
default:
case 0:
return C;
case 1:
return D;
case 2:
return row2;
default:
throw new ArgumentOutOfRangeException($"Trying to access {row}, {col} which was not in the value array.");
}
}
}
case 2:
{
switch (col)
{
switch (col)
{
case 0:
return E;
case 1:
return F;
case 2:
return row3;
default:
case 0:
return E;
case 1:
return F;
case 2:
return row3;
default:
throw new ArgumentOutOfRangeException($"Trying to access {row}, {col} which was not in the value array.");
}
}
}
default:
throw new ArgumentOutOfRangeException($"Trying to access {row}, {col} which was not in the value array.");
}
Expand Down Expand Up @@ -404,7 +404,7 @@ public TransformationMatrix Multiply(TransformationMatrix matrix)
var r3 = (E * matrix.row1) + (F * matrix.row2) + (row3 * matrix.row3);

return new TransformationMatrix(a, b, r1,
c, d, r2,
c, d, r2,
e, f, r3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ private static double AngleBL(Word pivot, Word candidate)
/// <summary>
/// The bounds for the angle between two words for them to have a certain type of relationship.
/// </summary>
public struct AngleBounds
public readonly struct AngleBounds
{
/// <summary>
/// The lower bound in degrees.
Expand Down Expand Up @@ -646,7 +646,7 @@ public class DocstrumBoundingBoxesOptions : IPageSegmenterOptions
public AngleBounds WithinLineBounds { get; set; } = new AngleBounds(-30, 30);

/// <summary>
/// Multiplier that gives the maximum euclidian distance between
/// Multiplier that gives the maximum euclidean distance between
/// words for building lines. Maximum distance will be this number times the within-line
/// distance found by the analysis.
/// <para>Default value is 3.</para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// A vector in the Adobe Font Metrics.
/// </summary>
public struct AdobeFontMetricsVector
public readonly struct AdobeFontMetricsVector
{
/// <summary>
/// The x component of the vector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public override string ToString()
return stringBuilder.ToString();
}

public struct CommandIdentifier
public readonly struct CommandIdentifier
{
public int CommandIndex { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/// <summary>
/// The header table for the binary data of a Compact Font Format file.
/// </summary>
public struct CompactFontFormatHeader
public readonly struct CompactFontFormatHeader
{
/// <summary>
/// The major version of this font format. Starting at 1.
/// </summary>
public byte MajorVersion { get; }

/// <summary>
/// The minor version of this font format. Starting at 0. Indicates extensions to the format which
/// are undetectable by readers which do not support them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public int GetFontDictionaryIndex(int glyphId)
return 0;
}

internal struct Range3
internal readonly struct Range3
{
public int First { get; }

Expand All @@ -415,7 +415,4 @@ public override string ToString()
}
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ protected static decimal[] ReadDeltaToArray(List<Operand> operands)
return results;
}

protected struct Operand
protected readonly struct Operand
{
public int? Int { get; }

Expand All @@ -309,13 +309,13 @@ public Operand(decimal d)
}
}

protected struct OperandKey
protected readonly struct OperandKey
{
public byte Byte0 { get; }

public byte? Byte1 { get; }

public OperandKey(Byte byte0)
public OperandKey(byte byte0)
{
Byte0 = byte0;
Byte1 = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class CompactFontFormatTopLevelDictionary

public CidFontOperators CidFontOperators { get; set; } = new CidFontOperators();

public struct SizeAndOffset
public readonly struct SizeAndOffset
{
public int Size { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Fonts/SystemFonts/SystemFontRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using System;

internal struct SystemFontRecord
internal readonly struct SystemFontRecord
{
public string Path { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace UglyToad.PdfPig.Fonts.TrueType.Glyphs
{
using Core;

internal struct CompositeTransformMatrix3By2
internal readonly struct CompositeTransformMatrix3By2
{
private readonly double r0c0;
private readonly double r0c1;
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Fonts/TrueType/Glyphs/GlyphPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using UglyToad.PdfPig.Core;

internal struct GlyphPoint
internal readonly struct GlyphPoint
{
public short X { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// The pair of horizontal metrics for an individual glyph.
/// </summary>
public struct HorizontalMetric
public readonly struct HorizontalMetric
{
/// <summary>
/// The advance width.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static string GetName(int nameId, TrueTypeNameRecord[] names)
return windows ?? any;
}

private struct NameRecordBuilder
private readonly struct NameRecordBuilder
{
public TrueTypePlatformIdentifier PlatformId { get; }

Expand Down
Loading