diff --git a/src/UglyToad.PdfPig.Tests/Integration/AssertablePositionData.cs b/src/UglyToad.PdfPig.Tests/Integration/AssertablePositionData.cs index 562fe203..a87471ed 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/AssertablePositionData.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/AssertablePositionData.cs @@ -54,5 +54,10 @@ Assert.Equal(Height, letter.GlyphRectangle.Height, 1); } } + + public override string ToString() + { + return $"{X} {Y} {Width} {Text} {FontSize} {FontName} {Height}"; + } } } \ No newline at end of file diff --git a/src/UglyToad.PdfPig.Tests/Integration/Documents/ICML03-081.jpg b/src/UglyToad.PdfPig.Tests/Integration/Documents/ICML03-081.jpg index e49a3359..9775194d 100644 Binary files a/src/UglyToad.PdfPig.Tests/Integration/Documents/ICML03-081.jpg and b/src/UglyToad.PdfPig.Tests/Integration/Documents/ICML03-081.jpg differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/Documents/Pig Production Handbook.jpg b/src/UglyToad.PdfPig.Tests/Integration/Documents/Pig Production Handbook.jpg index 7f6d6d7d..1dd986eb 100644 Binary files a/src/UglyToad.PdfPig.Tests/Integration/Documents/Pig Production Handbook.jpg and b/src/UglyToad.PdfPig.Tests/Integration/Documents/Pig Production Handbook.jpg differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/Documents/Single Page Simple - from inkscape.jpg b/src/UglyToad.PdfPig.Tests/Integration/Documents/Single Page Simple - from inkscape.jpg index d20e4860..ac2798e5 100644 Binary files a/src/UglyToad.PdfPig.Tests/Integration/Documents/Single Page Simple - from inkscape.jpg and b/src/UglyToad.PdfPig.Tests/Integration/Documents/Single Page Simple - from inkscape.jpg differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/SinglePageInkscapeTests.cs b/src/UglyToad.PdfPig.Tests/Integration/SinglePageInkscapeTests.cs new file mode 100644 index 00000000..0560969c --- /dev/null +++ b/src/UglyToad.PdfPig.Tests/Integration/SinglePageInkscapeTests.cs @@ -0,0 +1,74 @@ +namespace UglyToad.PdfPig.Tests.Integration +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using Xunit; + + public class SinglePageInkscapeTests + { + private static string GetFilename() + { + var documentFolder = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "Integration", "Documents")); + + return Path.Combine(documentFolder, "Single Page Simple - from inkscape.pdf"); + } + + [Fact] + public void LettersHaveCorrectPositionsPdfBox() + { + using (var document = PdfDocument.Open(GetFilename())) + { + var page = document.GetPage(1); + + var letters = page.Letters; + var positions = GetPdfBoxData(); + + for (int i = 0; i < letters.Count; i++) + { + var letter = letters[i]; + var position = positions[i]; + position.AssertWithinTolerance(letter, page, false); + } + } + } + + private static IReadOnlyList GetPdfBoxData() + { + const string data = @"100.57143 687.4286 31.616001 W 32.0 KTICVV+DejaVuSans 47.776 +130.74742 687.4286 13.152 r 32.0 KTICVV+DejaVuSans 36.704002 +143.89941 687.4286 8.864 i 32.0 KTICVV+DejaVuSans 49.792004 +152.76341 687.4286 12.544001 t 32.0 KTICVV+DejaVuSans 46.016003 +165.30742 687.4286 19.68 e 32.0 KTICVV+DejaVuSans 37.632 +184.98743 687.4286 10.144 32.0 KTICVV+DejaVuSans 0.0 +195.13142 687.4286 16.640001 s 32.0 KTICVV+DejaVuSans 37.632 +211.77142 687.4286 19.552 o 32.0 KTICVV+DejaVuSans 37.632 +231.32343 687.4286 31.168001 m 32.0 KTICVV+DejaVuSans 36.704002 +262.49142 687.4286 19.68 e 32.0 KTICVV+DejaVuSans 37.632 +282.17142 687.4286 12.544001 t 32.0 KTICVV+DejaVuSans 46.016003 +294.71542 687.4286 20.256 h 32.0 KTICVV+DejaVuSans 49.792004 +315.06744 687.4286 8.864 i 32.0 KTICVV+DejaVuSans 49.792004 +323.93146 687.4286 20.256 n 32.0 KTICVV+DejaVuSans 36.704002 +344.18747 687.4286 20.288 g 32.0 KTICVV+DejaVuSans 50.336002 +364.47546 687.4286 10.144 32.0 KTICVV+DejaVuSans 0.0 +374.61948 687.4286 8.864 i 32.0 KTICVV+DejaVuSans 49.792004 +383.4835 687.4286 20.256 n 32.0 KTICVV+DejaVuSans 36.704002 +100.57143 647.4286 9.408 I 32.0 KTICVV+DejaVuSans 47.776 +109.97942 647.4286 20.256 n 32.0 KTICVV+DejaVuSans 36.704002 +130.23543 647.4286 18.528002 k 32.0 KTICVV+DejaVuSans 49.792004 +148.76343 647.4286 16.640001 s 32.0 KTICVV+DejaVuSans 37.632 +165.40343 647.4286 17.568 c 32.0 KTICVV+DejaVuSans 37.632 +183.06743 647.4286 19.584002 a 32.0 KTICVV+DejaVuSans 37.632 +202.65143 647.4286 20.288 p 32.0 KTICVV+DejaVuSans 50.336002 +222.93942 647.4286 19.68 e 32.0 KTICVV+DejaVuSans 37.632 +"; + + var result = data.Split(new[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries) + .Select(AssertablePositionData.Parse) + .ToList(); + + return result; + } + } +} diff --git a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterBoundingBoxImages.cs b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterBoundingBoxImages.cs index b3c6e614..4ef57710 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterBoundingBoxImages.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterBoundingBoxImages.cs @@ -35,7 +35,7 @@ [Fact] public void SinglePageSimpleFromInkscape() { - Run(SingleInkscapePage); + Run(SingleInkscapePage, 841); } [Fact] @@ -65,7 +65,7 @@ [Fact] public void PigProductionCompactFontFormat() { - Run(PigProduction); + Run(PigProduction, 680); } private static void Run(string file, int imageHeight = 792) diff --git a/src/UglyToad.PdfPig/Fonts/Simple/TrueTypeSimpleFont.cs b/src/UglyToad.PdfPig/Fonts/Simple/TrueTypeSimpleFont.cs index 4cd88a7c..4729c728 100644 --- a/src/UglyToad.PdfPig/Fonts/Simple/TrueTypeSimpleFont.cs +++ b/src/UglyToad.PdfPig/Fonts/Simple/TrueTypeSimpleFont.cs @@ -1,5 +1,6 @@ namespace UglyToad.PdfPig.Fonts.Simple { + using System; using Cmap; using Composite; using Core; @@ -107,18 +108,24 @@ } decimal width; - if (font == null) + + var index = characterCode - firstCharacter; + if (widths != null && index >= 0 && index < widths.Length) { fromFont = false; - width = widths[characterCode - firstCharacter]; + width = widths[index]; } - else + else if (font != null) { if (!font.TryGetBoundingAdvancedWidth(characterCode, out width)) { width = boundingBoxPreTransform; } } + else + { + throw new InvalidOperationException($"Could not retrieve width for character code: {characterCode} in font {Name}."); + } if (fromFont) {