From 0e84fa34a8ee0ec3e4cec871eac7f8af296ece58 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Tue, 28 Jan 2020 13:37:42 +0000 Subject: [PATCH] fix usages of rectangle constructor. now that rectangle constructor uses the order [ llx, lly, urx, ury ] and does not apply correction for points constructor parameters must be passed in the correct order. this change fixes the hyperlink factory which was passing them in the wrong order. in addition the pdfpath bounding box was using left, right, top and bottom to calculate the minimum bounding box. this produced incorrect values now individual path operator bounding boxes are rotated, since for a rotated rectangle top may be less than bottom. the performance seems to have taken a hit due to these changes however. --- src/UglyToad.PdfPig.Core/PdfRectangle.cs | 18 +++++++++--------- .../Geometry/PdfRectangleTests.cs | 11 +++++++---- .../Annotations/HyperlinkFactory.cs | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/UglyToad.PdfPig.Core/PdfRectangle.cs b/src/UglyToad.PdfPig.Core/PdfRectangle.cs index dc6257c6..eca7e944 100644 --- a/src/UglyToad.PdfPig.Core/PdfRectangle.cs +++ b/src/UglyToad.PdfPig.Core/PdfRectangle.cs @@ -58,24 +58,24 @@ public double Area { get; } /// - /// Left. + /// Left. This value is only valid if the rectangle is not rotated, check . /// - public double Left => TopLeft.X; + public double Left => TopLeft.X < TopRight.X ? TopLeft.X : TopRight.X; /// - /// Top. + /// Top. This value is only valid if the rectangle is not rotated, check . /// - public double Top => TopLeft.Y; + public double Top => TopLeft.Y > BottomLeft.Y ? TopLeft.Y : BottomLeft.Y; /// - /// Right. + /// Right. This value is only valid if the rectangle is not rotated, check . /// - public double Right => BottomRight.X; + public double Right => BottomRight.X > BottomLeft.X ? BottomRight.X : BottomLeft.X; /// - /// Bottom. + /// Bottom. This value is only valid if the rectangle is not rotated, check . /// - public double Bottom => BottomRight.Y; + public double Bottom => BottomRight.Y < TopRight.Y ? BottomRight.Y : TopRight.Y; /// /// Create a new . @@ -136,7 +136,7 @@ double sinT = Math.Sin(t); double cosSqSinSqInv = 1.0 / (cosT * cosT - sinT * sinT); - Rotation = t * 57.2957795130823; // 180 / PI + Rotation = t * 180 / Math.PI; Width = cosSqSinSqInv * (bx * cosT - by * sinT); Height = cosSqSinSqInv * (-bx * sinT + by * cosT); Area = Width * Height; diff --git a/src/UglyToad.PdfPig.Tests/Geometry/PdfRectangleTests.cs b/src/UglyToad.PdfPig.Tests/Geometry/PdfRectangleTests.cs index 3df83b34..48409d77 100644 --- a/src/UglyToad.PdfPig.Tests/Geometry/PdfRectangleTests.cs +++ b/src/UglyToad.PdfPig.Tests/Geometry/PdfRectangleTests.cs @@ -7,6 +7,7 @@ public class PdfRectangleTests { private static readonly DoubleComparer DoubleComparer = new DoubleComparer(3); + private static readonly DoubleComparer PreciseDoubleComparer = new DoubleComparer(6); private static readonly PointComparer PointComparer = new PointComparer(DoubleComparer); private static readonly PdfRectangle UnitRectangle = new PdfRectangle(new PdfPoint(0, 0), new PdfPoint(1, 1)); @@ -134,8 +135,9 @@ Assert.Equal(new PdfPoint(-1, 0), rotated.TopLeft); Assert.Equal(new PdfPoint(-1, 1), rotated.TopRight); - Assert.Equal(1, rotated.Width); - Assert.Equal(-1, rotated.Height); + Assert.Equal(1, rotated.Width, PreciseDoubleComparer); + Assert.Equal(-1, rotated.Height, PreciseDoubleComparer); + Assert.Equal(90, rotated.Rotation, PreciseDoubleComparer); } [Fact] @@ -150,8 +152,9 @@ Assert.Equal(new PdfPoint(0, -1), rotated.TopLeft); Assert.Equal(new PdfPoint(-1, -1), rotated.TopRight); - Assert.Equal(-1, rotated.Width); - Assert.Equal(-1, rotated.Height); + Assert.Equal(-1, rotated.Width, PreciseDoubleComparer); + Assert.Equal(-1, rotated.Height, PreciseDoubleComparer); + Assert.Equal(180, rotated.Rotation, PreciseDoubleComparer); } } } diff --git a/src/UglyToad.PdfPig/Annotations/HyperlinkFactory.cs b/src/UglyToad.PdfPig/Annotations/HyperlinkFactory.cs index 7e0e275a..0ad3c0f1 100644 --- a/src/UglyToad.PdfPig/Annotations/HyperlinkFactory.cs +++ b/src/UglyToad.PdfPig/Annotations/HyperlinkFactory.cs @@ -41,7 +41,7 @@ var bounds = annotation.Rectangle; // Build in tolerance for letters close to the link region. - var tolerantBounds = new PdfRectangle(bounds.TopLeft.Translate(-0.5, 0), bounds.BottomRight.Translate(0.5, 0)); + var tolerantBounds = new PdfRectangle(bounds.BottomLeft.Translate(-0.5, -0.5), bounds.TopRight.Translate(0.5, 0.5)); var linkLetters = new List();