From 57e9acbc122474e2303139791db37ea80c313b23 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sun, 8 Jan 2023 12:00:35 -0500 Subject: [PATCH] post merge tidy up --- src/UglyToad.PdfPig.Core/OctalHelpers.cs | 83 ++++---- .../Geometry/ClippingTests.cs | 4 +- .../Writer/PdfDocumentBuilderTests.cs | 12 +- .../PdfFonts/Simple/Type1Standard14Font.cs | 191 +++++++++--------- src/UglyToad.PdfPig/Writer/PdfTextRemover.cs | 3 +- 5 files changed, 144 insertions(+), 149 deletions(-) diff --git a/src/UglyToad.PdfPig.Core/OctalHelpers.cs b/src/UglyToad.PdfPig.Core/OctalHelpers.cs index 5d4dde57..55a43afb 100644 --- a/src/UglyToad.PdfPig.Core/OctalHelpers.cs +++ b/src/UglyToad.PdfPig.Core/OctalHelpers.cs @@ -1,16 +1,15 @@ -namespace UglyToad.PdfPig.Core -{ - using System; - - /// - /// Interprets numbers in octal format. - /// - public static class OctalHelpers - { +namespace UglyToad.PdfPig.Core +{ + using System; - /// - /// Read a short. - /// + /// + /// Interprets numbers in octal format. + /// + public static class OctalHelpers + { + /// + /// Read a short. + /// public static short CharacterToShort(this char c) { switch (c) @@ -40,9 +39,9 @@ } } - /// - /// Read an integer from octal digits. - /// + /// + /// Read an integer from octal digits. + /// public static int FromOctalDigits(short[] octal) { int sum = 0; @@ -55,39 +54,29 @@ return sum; } - /// - /// Interpret an int as octal. - /// - public static int FromOctalInt(int input) + /// + /// Interpret an int as octal. + /// + 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; + } + } +} diff --git a/src/UglyToad.PdfPig.Tests/Geometry/ClippingTests.cs b/src/UglyToad.PdfPig.Tests/Geometry/ClippingTests.cs index cd5cf667..f73e82fd 100644 --- a/src/UglyToad.PdfPig.Tests/Geometry/ClippingTests.cs +++ b/src/UglyToad.PdfPig.Tests/Geometry/ClippingTests.cs @@ -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); diff --git a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs index 45b4b24f..86f3bf57 100644 --- a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs +++ b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs @@ -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; } } -} +} diff --git a/src/UglyToad.PdfPig/PdfFonts/Simple/Type1Standard14Font.cs b/src/UglyToad.PdfPig/PdfFonts/Simple/Type1Standard14Font.cs index 34359905..713f9eb1 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Simple/Type1Standard14Font.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Simple/Type1Standard14Font.cs @@ -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; - - /// - /// A font using one of the Adobe Standard 14 fonts. Can use a custom encoding. - /// - 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; + + /// + /// A font using one of the Adobe Standard 14 fonts. Can use a custom encoding. + /// + 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; + } + } +} diff --git a/src/UglyToad.PdfPig/Writer/PdfTextRemover.cs b/src/UglyToad.PdfPig/Writer/PdfTextRemover.cs index 369c55b3..0f209a37 100644 --- a/src/UglyToad.PdfPig/Writer/PdfTextRemover.cs +++ b/src/UglyToad.PdfPig/Writer/PdfTextRemover.cs @@ -5,7 +5,8 @@ using System.IO; namespace UglyToad.PdfPig.Writer { /// - /// 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. /// public static class PdfTextRemover {