Skip to content

Commit

Permalink
Use double in fonts instead of decimals and tidy up remaining decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Mar 6, 2024
1 parent c25368e commit ac0276f
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 277 deletions.
12 changes: 1 addition & 11 deletions src/UglyToad.PdfPig.Core/PdfPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public readonly struct PdfPoint
/// <summary>
/// The origin of the coordinates system.
/// </summary>
public static PdfPoint Origin { get; } = new PdfPoint(0m, 0m);
public static PdfPoint Origin { get; } = new PdfPoint(0.0, 0.0);

/// <summary>
/// The X coordinate for this point. (Horizontal axis).
Expand All @@ -28,16 +28,6 @@ public readonly struct PdfPoint
/// </summary>
public double Y { get; }

/// <summary>
/// Create a new <see cref="PdfPoint"/> at this position.
/// </summary>
[DebuggerStepThrough]
public PdfPoint(decimal x, decimal y)
{
X = (double)x;
Y = (double)y;
}

/// <summary>
/// Create a new <see cref="PdfPoint"/> at this position.
/// </summary>
Expand Down
22 changes: 0 additions & 22 deletions src/UglyToad.PdfPig.Core/PdfRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ public readonly struct PdfRange
private readonly IReadOnlyList<double> rangeArray;
private readonly int startingIndex;

/// <summary>
/// Constructor assumes a starting index of 0.
/// </summary>
/// <param name="range">The array that describes the range.</param>
public PdfRange(IEnumerable<decimal> range)
: this(range.Select(v => (double)v), 0)
{
}

/// <summary>
/// Constructor with an index into an array. Because some arrays specify
/// multiple ranges ie [0, 1, 0, 2, 2, 3]. It is convenient for this
/// class to take an index into an array. So if you want this range to
/// represent 0, 2 in the above example then you would say <c>new PDRange(array, 1)</c>.
/// </summary>
/// <param name="range">The array that describes the index</param>
/// <param name="index">The range index into the array for the start of the range.</param>
public PdfRange(IEnumerable<decimal> range, int index)
: this(range.Select(v => (double)v), index)
{
}

/// <summary>
/// Constructor assumes a starting index of 0.
/// </summary>
Expand Down
8 changes: 0 additions & 8 deletions src/UglyToad.PdfPig.Core/TransformationMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,6 @@ public static TransformationMatrix FromValues(double a, double b, double c, doub
public static TransformationMatrix FromValues(double a, double b, double c, double d)
=> new TransformationMatrix(a, b, 0, c, d, 0, 0, 0, 1);

/// <summary>
/// Create a new <see cref="TransformationMatrix"/> from the values.
/// </summary>
/// <param name="values">Either all 9 values of the matrix, 6 values in the default PDF order or the 4 values of the top left square.</param>
/// <returns></returns>
public static TransformationMatrix FromArray(decimal[] values)
=> FromArray(values.Select(x => (double)x).ToArray());

/// <summary>
/// Create a new <see cref="TransformationMatrix"/> from the values.
/// </summary>
Expand Down
40 changes: 20 additions & 20 deletions src/UglyToad.PdfPig.Fonts/AdobeFontMetrics/AdobeFontMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AdobeFontMetrics
/// <summary>
/// Version of the Adobe Font Metrics specification used to generate this file.
/// </summary>
public decimal AfmVersion { get; }
public double AfmVersion { get; }

/// <summary>
/// Any comments in the file.
Expand Down Expand Up @@ -103,38 +103,38 @@ public class AdobeFontMetrics
/// <summary>
/// Usually the y-value of the top of capital 'H'.
/// </summary>
public decimal CapHeight { get; }
public double CapHeight { get; }

/// <summary>
/// Usually the y-value of the top of lowercase 'x'.
/// </summary>
public decimal XHeight { get; }
public double XHeight { get; }

/// <summary>
/// Usually the y-value of the top of lowercase 'd'.
/// </summary>
public decimal Ascender { get; }
public double Ascender { get; }

/// <summary>
/// Usually the y-value of the bottom of lowercase 'p'.
/// </summary>
public decimal Descender { get; }
public double Descender { get; }

/// <summary>
/// Distance from the baseline for underlining.
/// </summary>
public decimal UnderlinePosition { get; }
public double UnderlinePosition { get; }

/// <summary>
/// Width of the line for underlining.
/// </summary>
public decimal UnderlineThickness { get; }
public double UnderlineThickness { get; }

/// <summary>
/// Angle in degrees counter-clockwise from the vertical of the vertical linea.
/// Zero for non-italic fonts.
/// </summary>
public decimal ItalicAngle { get; }
public double ItalicAngle { get; }

/// <summary>
/// If present all characters have this width and height.
Expand All @@ -144,12 +144,12 @@ public class AdobeFontMetrics
/// <summary>
/// Horizontal stem width.
/// </summary>
public decimal HorizontalStemWidth { get; }
public double HorizontalStemWidth { get; }

/// <summary>
/// Vertical stem width.
/// </summary>
public decimal VerticalStemWidth { get; }
public double VerticalStemWidth { get; }

/// <summary>
/// Metrics for the individual characters.
Expand All @@ -159,7 +159,7 @@ public class AdobeFontMetrics
/// <summary>
/// Create a new <see cref="AdobeFontMetrics"/>.
/// </summary>
public AdobeFontMetrics(decimal afmVersion, IReadOnlyList<string> comments, int metricSets, string fontName,
public AdobeFontMetrics(double afmVersion, IReadOnlyList<string> comments, int metricSets, string fontName,
string fullName,
string familyName,
string weight,
Expand All @@ -174,16 +174,16 @@ public AdobeFontMetrics(decimal afmVersion, IReadOnlyList<string> comments, int
bool isBaseFont,
AdobeFontMetricsVector vVector,
bool isFixedV,
decimal capHeight,
decimal xHeight,
decimal ascender,
decimal descender,
decimal underlinePosition,
decimal underlineThickness,
decimal italicAngle,
double capHeight,
double xHeight,
double ascender,
double descender,
double underlinePosition,
double underlineThickness,
double italicAngle,
AdobeFontMetricsCharacterSize characterWidth,
decimal horizontalStemWidth,
decimal verticalStemWidth,
double horizontalStemWidth,
double verticalStemWidth,
IReadOnlyDictionary<string, AdobeFontMetricsIndividualCharacterMetric> characterMetrics)
{
AfmVersion = afmVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace UglyToad.PdfPig.Fonts.AdobeFontMetrics
{
using Core;
using System.Collections.Generic;
using System.Linq;
using Core;
using Standard14Fonts;

internal class AdobeFontMetricsBuilder
{
public decimal AfmVersion { get; }
public double AfmVersion { get; }

public List<string> Comments { get; }

Expand Down Expand Up @@ -37,7 +36,7 @@ internal class AdobeFontMetricsBuilder
/// <summary>
/// Angle in degrees counter-clockwise from vertical of vertical strokes of the font.
/// </summary>
public decimal ItalicAngle { get; set; }
public double ItalicAngle { get; set; }

/// <summary>
/// Whether the font is monospaced or not.
Expand All @@ -52,12 +51,12 @@ internal class AdobeFontMetricsBuilder
/// <summary>
/// Distance from the baseline for underlining.
/// </summary>
public decimal UnderlinePosition { get; set; }
public double UnderlinePosition { get; set; }

/// <summary>
/// The stroke width for underlining.
/// </summary>
public decimal UnderlineThickness { get; set; }
public double UnderlineThickness { get; set; }

/// <summary>
/// Version identifier for the font program.
Expand Down Expand Up @@ -86,32 +85,32 @@ internal class AdobeFontMetricsBuilder
/// <summary>
/// The y-value of the top of a capital H.
/// </summary>
public decimal CapHeight { get; set; }
public double CapHeight { get; set; }

/// <summary>
/// The y-value of the top of lowercase x.
/// </summary>
public decimal XHeight { get; set; }
public double XHeight { get; set; }

/// <summary>
/// Generally the y-value of the top of lowercase d.
/// </summary>
public decimal Ascender { get; set; }
public double Ascender { get; set; }

/// <summary>
/// The y-value of the bottom of lowercase p.
/// </summary>
public decimal Descender { get; set; }
public double Descender { get; set; }

/// <summary>
/// Width of horizontal stems.
/// </summary>
public decimal StdHw { get; set; }
public double StdHw { get; set; }

/// <summary>
/// Width of vertical stems.
/// </summary>
public decimal StdVw { get; set; }
public double StdVw { get; set; }

public int EscapeCharacter { get; set; }

Expand All @@ -123,7 +122,7 @@ internal class AdobeFontMetricsBuilder

public bool IsFixedV { get; set; }

public AdobeFontMetricsBuilder(decimal afmVersion)
public AdobeFontMetricsBuilder(double afmVersion)
{
AfmVersion = afmVersion;
Comments = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public static AdobeFontMetrics Parse(IInputBytes bytes, bool useReducedDataSet)
throw new InvalidFontFormatException($"The AFM file was not valid, it did not start with {StartFontMetrics}.");
}

var version = ReadDecimal(bytes, stringBuilder);
var version = ReadDouble(bytes, stringBuilder);

var builder = new AdobeFontMetricsBuilder(version);

Expand All @@ -368,7 +368,7 @@ public static AdobeFontMetrics Parse(IInputBytes bytes, bool useReducedDataSet)
builder.Weight = ReadLine(bytes, stringBuilder);
break;
case ItalicAngle:
builder.ItalicAngle = ReadDecimal(bytes, stringBuilder);
builder.ItalicAngle = ReadDouble(bytes, stringBuilder);
break;
case IsFixedPitch:
builder.IsFixedPitch = ReadBool(bytes, stringBuilder);
Expand All @@ -378,10 +378,10 @@ public static AdobeFontMetrics Parse(IInputBytes bytes, bool useReducedDataSet)
ReadDouble(bytes, stringBuilder), ReadDouble(bytes, stringBuilder));
break;
case UnderlinePosition:
builder.UnderlinePosition = ReadDecimal(bytes, stringBuilder);
builder.UnderlinePosition = ReadDouble(bytes, stringBuilder);
break;
case UnderlineThickness:
builder.UnderlineThickness = ReadDecimal(bytes, stringBuilder);
builder.UnderlineThickness = ReadDouble(bytes, stringBuilder);
break;
case Version:
builder.Version = ReadLine(bytes, stringBuilder);
Expand All @@ -393,37 +393,37 @@ public static AdobeFontMetrics Parse(IInputBytes bytes, bool useReducedDataSet)
builder.EncodingScheme = ReadLine(bytes, stringBuilder);
break;
case MappingScheme:
builder.MappingScheme = (int)ReadDecimal(bytes, stringBuilder);
builder.MappingScheme = (int)ReadDouble(bytes, stringBuilder);
break;
case CharacterSet:
builder.CharacterSet = ReadLine(bytes, stringBuilder);
break;
case EscChar:
builder.EscapeCharacter = (int) ReadDecimal(bytes, stringBuilder);
builder.EscapeCharacter = (int)ReadDouble(bytes, stringBuilder);
break;
case Characters:
builder.Characters = (int) ReadDecimal(bytes, stringBuilder);
builder.Characters = (int)ReadDouble(bytes, stringBuilder);
break;
case IsBaseFont:
builder.IsBaseFont = ReadBool(bytes, stringBuilder);
break;
case CapHeight:
builder.CapHeight = ReadDecimal(bytes, stringBuilder);
builder.CapHeight = ReadDouble(bytes, stringBuilder);
break;
case XHeight:
builder.XHeight = ReadDecimal(bytes, stringBuilder);
builder.XHeight = ReadDouble(bytes, stringBuilder);
break;
case Ascender:
builder.Ascender = ReadDecimal(bytes, stringBuilder);
builder.Ascender = ReadDouble(bytes, stringBuilder);
break;
case Descender:
builder.Descender = ReadDecimal(bytes, stringBuilder);
builder.Descender = ReadDouble(bytes, stringBuilder);
break;
case StdHw:
builder.StdHw = ReadDecimal(bytes, stringBuilder);
builder.StdHw = ReadDouble(bytes, stringBuilder);
break;
case StdVw:
builder.StdVw = ReadDecimal(bytes, stringBuilder);
builder.StdVw = ReadDouble(bytes, stringBuilder);
break;
case CharWidth:
builder.SetCharacterWidth(ReadDouble(bytes, stringBuilder), ReadDouble(bytes, stringBuilder));
Expand All @@ -435,7 +435,7 @@ public static AdobeFontMetrics Parse(IInputBytes bytes, bool useReducedDataSet)
builder.IsFixedV = ReadBool(bytes, stringBuilder);
break;
case StartCharMetrics:
var count = (int)ReadDecimal(bytes, stringBuilder);
var count = (int)ReadDouble(bytes, stringBuilder);
for (var i = 0; i < count; i++)
{
var metric = ReadCharacterMetric(bytes, stringBuilder);
Expand All @@ -457,17 +457,9 @@ public static AdobeFontMetrics Parse(IInputBytes bytes, bool useReducedDataSet)
return builder.Build();
}

private static decimal ReadDecimal(IInputBytes input, StringBuilder stringBuilder)
{
var str = ReadString(input, stringBuilder);

return decimal.Parse(str, CultureInfo.InvariantCulture);
}

private static double ReadDouble(IInputBytes input, StringBuilder stringBuilder)
{
var dec = ReadDecimal(input, stringBuilder);
return (double) dec;
return double.Parse(ReadString(input, stringBuilder), CultureInfo.InvariantCulture);
}

private static bool ReadBool(IInputBytes input, StringBuilder stringBuilder)
Expand All @@ -484,7 +476,7 @@ private static bool ReadBool(IInputBytes input, StringBuilder stringBuilder)
throw new InvalidFontFormatException($"The AFM should have contained a boolean but instead contained: {boolean}.");
}
}

private static string ReadString(IInputBytes input, StringBuilder stringBuilder)
{
stringBuilder.Clear();
Expand Down
Loading

0 comments on commit ac0276f

Please sign in to comment.