From cd9ac6ac6ca260bfb1827674be0eabcab263a779 Mon Sep 17 00:00:00 2001 From: BobLd Date: Mon, 12 Oct 2020 12:59:02 +0100 Subject: [PATCH 1/2] - fix letter's PointSize computation by applying the transform to a rectangle of height fontSize - add test with rotated letters --- .../Fonts/PointSizeTests.cs | 22 +++++++++++++++++++ .../Graphics/ContentStreamProcessor.cs | 17 ++------------ 2 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 src/UglyToad.PdfPig.Tests/Fonts/PointSizeTests.cs diff --git a/src/UglyToad.PdfPig.Tests/Fonts/PointSizeTests.cs b/src/UglyToad.PdfPig.Tests/Fonts/PointSizeTests.cs new file mode 100644 index 000000000..1f8d034cc --- /dev/null +++ b/src/UglyToad.PdfPig.Tests/Fonts/PointSizeTests.cs @@ -0,0 +1,22 @@ +namespace UglyToad.PdfPig.Tests.Fonts +{ + using UglyToad.PdfPig.Tests.Dla; + using Xunit; + + public class PointSizeTests + { + [Fact] + public void RotatedText() + { + using (var document = PdfDocument.Open(DlaHelper.GetDocumentPath("complex rotated"))) + { + var page = document.GetPage(1); + + foreach (var letter in page.Letters) + { + Assert.Equal(12, letter.PointSize); + } + } + } + } +} diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 040f7c8ba..15597cccf 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -241,20 +241,7 @@ public void ShowText(IInputBytes bytes) var renderingMatrix = TransformationMatrix.FromValues(fontSize * horizontalScaling, 0, 0, fontSize, 0, rise); - // TODO: this does not seem correct, produces the correct result for now but we need to revisit. - // see: https://stackoverflow.com/questions/48010235/pdf-specification-get-font-size-in-points - var fontSizeMatrix = transformationMatrix.Multiply(TextMatrices.TextMatrix).Multiply(fontSize); - var pointSize = Math.Round(fontSizeMatrix.A, 2); - // Assume a rotated letter - if (pointSize == 0) - { - pointSize = Math.Round(fontSizeMatrix.B, 2); - } - - if (pointSize < 0) - { - pointSize *= -1; - } + var pointSize = Math.Round(transformationMatrix.Multiply(TextMatrices.TextMatrix).Transform(new PdfRectangle(0, 0, 1, fontSize)).Height, 2); while (bytes.MoveNext()) { @@ -292,7 +279,7 @@ public void ShowText(IInputBytes bytes) var boundingBox = font.GetBoundingBox(code); var transformedGlyphBounds = PerformantRectangleTransformer - .Transform(renderingMatrix, textMatrix, transformationMatrix, boundingBox.GlyphBounds); + .Transform(renderingMatrix, textMatrix, transformationMatrix, boundingBox.GlyphBounds); var transformedPdfBounds = PerformantRectangleTransformer .Transform(renderingMatrix, textMatrix, transformationMatrix, new PdfRectangle(0, 0, boundingBox.Width, 0)); From 61d3e94a95802d3f8828875173ba510e353c5020 Mon Sep 17 00:00:00 2001 From: BobLd Date: Mon, 12 Oct 2020 16:52:33 +0100 Subject: [PATCH 2/2] update PointSize description --- src/UglyToad.PdfPig/Content/Letter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index 87d66c038..5c3fca4b7 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -68,7 +68,6 @@ public class Letter /// /// The size of the font in points. - /// This is considered experimental because the calculated value is incorrect for some documents at present. /// public double PointSize { get; }