post merge tidy up

This commit is contained in:
Eliot Jones
2023-01-08 12:00:35 -05:00
parent 7b891edb69
commit 57e9acbc12
5 changed files with 144 additions and 149 deletions

View File

@@ -1,16 +1,15 @@
namespace UglyToad.PdfPig.Core
{
using System;
/// <summary>
/// Interprets numbers in octal format.
/// </summary>
public static class OctalHelpers
{
namespace UglyToad.PdfPig.Core
{
using System;
/// <summary>
/// Read a short.
/// </summary>
/// <summary>
/// Interprets numbers in octal format.
/// </summary>
public static class OctalHelpers
{
/// <summary>
/// Read a short.
/// </summary>
public static short CharacterToShort(this char c)
{
switch (c)
@@ -40,9 +39,9 @@
}
}
/// <summary>
/// Read an integer from octal digits.
/// </summary>
/// <summary>
/// Read an integer from octal digits.
/// </summary>
public static int FromOctalDigits(short[] octal)
{
int sum = 0;
@@ -55,39 +54,29 @@
return sum;
}
/// <summary>
/// Interpret an int as octal.
/// </summary>
public static int FromOctalInt(int input)
/// <summary>
/// Interpret an int as octal.
/// </summary>
public static int FromOctalInt(int input)
{
return System.Convert.ToInt32($"{input}", 8);
//var str = input.ToString();
//int sum = 0;
//for (var i = 0; i < str.Length; i++)
//{
// var part = str[str.Length - 1 - i].CharacterToShort();
// sum += part * QuickPower(8, i);
//}
//return sum;
return Convert.ToInt32($"{input}", 8);
}
private static int QuickPower(int x, int pow)
{
int ret = 1;
while (pow != 0)
{
if ((pow & 1) == 1)
ret *= x;
x *= x;
pow >>= 1;
}
return ret;
}
private static int QuickPower(int x, int pow)
{
int ret = 1;
while (pow != 0)
{
if ((pow & 1) == 1)
{
ret *= x;
}
}
}
x *= x;
pow >>= 1;
}
return ret;
}
}
}

View File

@@ -6,10 +6,10 @@ namespace UglyToad.PdfPig.Tests.Geometry
public class ClippingTests
{
[Fact]
public void ContainsRectangleEvenOdd()
public void ContainsRectangleEvenOdd()
{
using (var document = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("SPARC - v9 Architecture Manual"),
new ParsingOptions() { ClipPaths = true }))
new ParsingOptions { ClipPaths = true }))
{
var page = document.GetPage(45);
Assert.Equal(28, page.ExperimentalAccess.Paths.Count);

View File

@@ -1,5 +1,5 @@
namespace UglyToad.PdfPig.Tests.Writer
{
namespace UglyToad.PdfPig.Tests.Writer
{
using System.IO;
using System.Linq;
using Content;
@@ -9,7 +9,7 @@
using PdfPig.Tokens;
using PdfPig.Writer;
using System.Collections.Generic;
using Tests.Fonts.TrueType;
using Tests.Fonts.TrueType;
using Xunit;
public class PdfDocumentBuilderTests
@@ -1129,9 +1129,11 @@
{
builder.AddPage(doc, i);
}
builder.Build();
}
Assert.Equal(tw.Objects, 0); // No objects in sample file
Assert.Equal(0, tw.Objects); // No objects in sample file
Assert.True(tw.Tokens > 1000); // Roughly 1065
Assert.True(tw.WroteCrossReferenceTable);
}
@@ -1180,4 +1182,4 @@
WroteCrossReferenceTable = true;
}
}
}
}

View File

@@ -1,58 +1,59 @@
// ReSharper disable CompareOfFloatsByEqualityOperator
namespace UglyToad.PdfPig.PdfFonts.Simple
{
// ReSharper disable CompareOfFloatsByEqualityOperator
namespace UglyToad.PdfPig.PdfFonts.Simple
{
using System;
using System.Diagnostics;
using Core;
using Fonts;
using Fonts.AdobeFontMetrics;
using Fonts.Encodings;
using Tokens;
/// <summary>
/// A font using one of the Adobe Standard 14 fonts. Can use a custom encoding.
/// </summary>
internal class Type1Standard14Font: IFont
{
private readonly AdobeFontMetrics standardFontMetrics;
private readonly Encoding encoding;
public NameToken Name { get; }
public bool IsVertical { get; }
public FontDetails Details { get; }
private readonly TransformationMatrix fontMatrix = TransformationMatrix.FromValues(0.001, 0, 0, 0.001, 0, 0);
public Type1Standard14Font(AdobeFontMetrics standardFontMetrics, Encoding overrideEncoding = null)
{
this.standardFontMetrics = standardFontMetrics ?? throw new ArgumentNullException(nameof(standardFontMetrics));
encoding = overrideEncoding ?? new AdobeFontMetricsEncoding(standardFontMetrics);
Name = NameToken.Create(standardFontMetrics.FontName);
IsVertical = false;
Details = new FontDetails(Name.Data,
standardFontMetrics.Weight == "Bold",
standardFontMetrics.Weight == "Bold" ? 700 : FontDetails.DefaultWeight,
standardFontMetrics.ItalicAngle != 0);
}
public int ReadCharacterCode(IInputBytes bytes, out int codeLength)
{
codeLength = 1;
return bytes.CurrentByte;
}
public bool TryGetUnicode(int characterCode, out string value)
{
using Core;
using Fonts;
using Fonts.AdobeFontMetrics;
using Fonts.Encodings;
using Tokens;
/// <summary>
/// A font using one of the Adobe Standard 14 fonts. Can use a custom encoding.
/// </summary>
internal class Type1Standard14Font: IFont
{
private readonly AdobeFontMetrics standardFontMetrics;
private readonly Encoding encoding;
public NameToken Name { get; }
public bool IsVertical { get; }
public FontDetails Details { get; }
private readonly TransformationMatrix fontMatrix = TransformationMatrix.FromValues(0.001, 0, 0, 0.001, 0, 0);
public Type1Standard14Font(AdobeFontMetrics standardFontMetrics, Encoding overrideEncoding = null)
{
this.standardFontMetrics = standardFontMetrics ?? throw new ArgumentNullException(nameof(standardFontMetrics));
encoding = overrideEncoding ?? new AdobeFontMetricsEncoding(standardFontMetrics);
Name = NameToken.Create(standardFontMetrics.FontName);
IsVertical = false;
Details = new FontDetails(Name.Data,
standardFontMetrics.Weight == "Bold",
standardFontMetrics.Weight == "Bold" ? 700 : FontDetails.DefaultWeight,
standardFontMetrics.ItalicAngle != 0);
}
public int ReadCharacterCode(IInputBytes bytes, out int codeLength)
{
codeLength = 1;
return bytes.CurrentByte;
}
public bool TryGetUnicode(int characterCode, out string value)
{
var name = encoding.GetName(characterCode);
if (name is ".notdef")
{
value = null;
return false;
}
}
if (encoding is ZapfDingbatsEncoding)
{
var listed = GlyphList.ZapfDingbats.NameToUnicode(name);
@@ -61,61 +62,63 @@ namespace UglyToad.PdfPig.PdfFonts.Simple
return true;
}
else if (encoding is StandardEncoding or SymbolEncoding)
if (encoding is StandardEncoding or SymbolEncoding)
{
var listed = GlyphList.AdobeGlyphList.NameToUnicode(name);
value = listed;
return true;
} else
{
}
else
{
Debug.WriteLine($"Warning: Type1Standard14Font with unexpected encoding: '{encoding.EncodingName}' Expected: 'ZapfDingbatsEncoding','SymbolEncoding' or 'StandardEncoding' . Font: '{standardFontMetrics.FontName}'");
var listed = GlyphList.AdobeGlyphList.NameToUnicode(name);
value = listed;
return true;
}
}
public CharacterBoundingBox GetBoundingBox(int characterCode)
{
var boundingBox = GetBoundingBoxInGlyphSpace(characterCode);
boundingBox = fontMatrix.Transform(boundingBox);
return new CharacterBoundingBox(boundingBox, boundingBox.Width);
}
private PdfRectangle GetBoundingBoxInGlyphSpace(int characterCode)
{
var name = encoding.GetName(characterCode);
if (!standardFontMetrics.CharacterMetrics.TryGetValue(name, out var metrics))
{
return new PdfRectangle(0, 0, 250, 0);
}
var x = metrics.Width.X;
var y = metrics.Width.Y;
if (metrics.Width.X == 0 && metrics.BoundingBox.Width > 0)
{
x = metrics.BoundingBox.Width;
}
if (metrics.Width.Y == 0 && metrics.BoundingBox.Height > 0)
{
y = metrics.BoundingBox.Height;
}
return new PdfRectangle(0, 0, x, y);
}
public TransformationMatrix GetFontMatrix()
{
return fontMatrix;
}
}
}
}
}
public CharacterBoundingBox GetBoundingBox(int characterCode)
{
var boundingBox = GetBoundingBoxInGlyphSpace(characterCode);
boundingBox = fontMatrix.Transform(boundingBox);
return new CharacterBoundingBox(boundingBox, boundingBox.Width);
}
private PdfRectangle GetBoundingBoxInGlyphSpace(int characterCode)
{
var name = encoding.GetName(characterCode);
if (!standardFontMetrics.CharacterMetrics.TryGetValue(name, out var metrics))
{
return new PdfRectangle(0, 0, 250, 0);
}
var x = metrics.Width.X;
var y = metrics.Width.Y;
if (metrics.Width.X == 0 && metrics.BoundingBox.Width > 0)
{
x = metrics.BoundingBox.Width;
}
if (metrics.Width.Y == 0 && metrics.BoundingBox.Height > 0)
{
y = metrics.BoundingBox.Height;
}
return new PdfRectangle(0, 0, x, y);
}
public TransformationMatrix GetFontMatrix()
{
return fontMatrix;
}
}
}

View File

@@ -5,7 +5,8 @@ using System.IO;
namespace UglyToad.PdfPig.Writer
{
/// <summary>
/// Class to remove text from PDFs, useful as a preprocessing step for Optical Character Recognition (OCR)
/// Class to remove text from PDFs, useful as a preprocessing step for Optical Character Recognition (OCR).
/// Note that this should not be used to redact content from PDFs, this is not a secure or reliable way to redact text.
/// </summary>
public static class PdfTextRemover
{