From c5d03bca9703ffe4d20d2c2ab1c60b8fd383fa0b Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Thu, 8 Aug 2019 21:19:18 +0100 Subject: [PATCH] move application of transformation matrix outside path --- .../CharStrings/Type2BuildCharContext.cs | 2 +- .../Commands/Type1BuildCharContext.cs | 2 +- src/UglyToad.PdfPig/Geometry/PdfPath.cs | 25 ++++++------------- .../Graphics/ContentStreamProcessor.cs | 2 +- .../AppendDualControlPointBezierCurve.cs | 11 +++++--- .../AppendEndControlPointBezierCurve.cs | 14 ++++++----- .../PathConstruction/AppendRectangle.cs | 3 ++- .../AppendStartControlPointBezierCurve.cs | 12 +++++---- .../AppendStraightLineSegment.cs | 5 ++-- .../PathConstruction/BeginNewSubpath.cs | 5 ++-- 10 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CharStrings/Type2BuildCharContext.cs b/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CharStrings/Type2BuildCharContext.cs index 3be3094b..538556e3 100644 --- a/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CharStrings/Type2BuildCharContext.cs +++ b/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CharStrings/Type2BuildCharContext.cs @@ -18,7 +18,7 @@ /// /// The current path. /// - public PdfPath Path { get; } = new PdfPath(Core.TransformationMatrix.Identity); + public PdfPath Path { get; } = new PdfPath(); /// /// The current location of the active point. diff --git a/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Commands/Type1BuildCharContext.cs b/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Commands/Type1BuildCharContext.cs index 35ec3a10..c6f421ae 100644 --- a/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Commands/Type1BuildCharContext.cs +++ b/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Commands/Type1BuildCharContext.cs @@ -22,7 +22,7 @@ public bool IsFlexing { get; set; } [NotNull] - public PdfPath Path { get; private set; } = new PdfPath(Core.TransformationMatrix.Identity); + public PdfPath Path { get; private set; } = new PdfPath(); public PdfPoint CurrentPosition { get; set; } diff --git a/src/UglyToad.PdfPig/Geometry/PdfPath.cs b/src/UglyToad.PdfPig/Geometry/PdfPath.cs index 7b4061ed..89a2861f 100644 --- a/src/UglyToad.PdfPig/Geometry/PdfPath.cs +++ b/src/UglyToad.PdfPig/Geometry/PdfPath.cs @@ -5,7 +5,6 @@ namespace UglyToad.PdfPig.Geometry using System.Collections.Generic; using System.Linq; using System.Text; - using Core; /// /// A path in a PDF document, used by glyphs and page content. Can contain multiple sub-paths. @@ -20,21 +19,10 @@ namespace UglyToad.PdfPig.Geometry public IReadOnlyList Commands => commands; private PdfPoint? currentPosition; - - private readonly TransformationMatrix currentTransformationMatrix; - - /// - /// Create a new . - /// - /// The transformation to apply to all points in this path. - public PdfPath(TransformationMatrix transformationMatrix) - { - currentTransformationMatrix = transformationMatrix; - } - + internal void MoveTo(decimal x, decimal y) { - currentPosition = currentTransformationMatrix.Transform(new PdfPoint(x, y)); + currentPosition = new PdfPoint(x, y); commands.Add(new Move(currentPosition.Value)); } @@ -42,12 +30,13 @@ namespace UglyToad.PdfPig.Geometry { if (currentPosition.HasValue) { - var to = currentTransformationMatrix.Transform(new PdfPoint(x, y)); + var to = new PdfPoint(x, y); commands.Add(new Line(currentPosition.Value, to)); currentPosition = to; } else { + // TODO: probably the wrong behaviour here, maybe line starts from (0, 0)? MoveTo(x, y); } } @@ -58,9 +47,9 @@ namespace UglyToad.PdfPig.Geometry { if (currentPosition.HasValue) { - var to = currentTransformationMatrix.Transform(new PdfPoint(x3, y3)); + var to = new PdfPoint(x3, y3); commands.Add(new BezierCurve(currentPosition.Value, - currentTransformationMatrix.Transform(new PdfPoint(x1, y1)), currentTransformationMatrix.Transform(new PdfPoint(x2, y2)), to)); + new PdfPoint(x1, y1), new PdfPoint(x2, y2), to)); currentPosition = to; } else @@ -460,7 +449,7 @@ namespace UglyToad.PdfPig.Geometry internal void Rectangle(decimal x, decimal y, decimal width, decimal height) { - currentPosition = currentTransformationMatrix.Transform(new PdfPoint(x, y)); + currentPosition = new PdfPoint(x, y); LineTo(x + width, y); LineTo(x + width, y + height); LineTo(x, y + height); diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 7e921474..972049bf 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -288,7 +288,7 @@ public void BeginSubpath() { - CurrentPath = new PdfPath(CurrentTransformationMatrix); + CurrentPath = new PdfPath(); } public void StrokePath(bool close) diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs index cf191f9c..39e5945f 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs @@ -52,10 +52,13 @@ /// public void Run(IOperationContext operationContext) { - operationContext.CurrentPath.BezierCurveTo(ControlPoint1.X, ControlPoint1.Y, - ControlPoint2.X, ControlPoint2.Y, - End.X, End.Y); - operationContext.CurrentPosition = End; + var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(ControlPoint1); + var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(ControlPoint2); + var endTransform = operationContext.CurrentTransformationMatrix.Transform(End); + operationContext.CurrentPath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, + controlPoint2Transform.X, controlPoint2Transform.Y, + endTransform.X, endTransform.Y); + operationContext.CurrentPosition = endTransform; } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs index 5c1ebfc5..f1d300c3 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs @@ -44,12 +44,14 @@ /// public void Run(IOperationContext operationContext) { - operationContext.CurrentPath.BezierCurveTo(ControlPoint1.X, ControlPoint1.Y, - End.X, - End.Y, - End.X, - End.Y); - operationContext.CurrentPosition = End; + var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(ControlPoint1); + var endTransform = operationContext.CurrentTransformationMatrix.Transform(End); + operationContext.CurrentPath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, + endTransform.X, + endTransform.Y, + endTransform.X, + endTransform.Y); + operationContext.CurrentPosition = endTransform; } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs index 87d721cc..ab27dc48 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs @@ -51,7 +51,8 @@ public void Run(IOperationContext operationContext) { operationContext.BeginSubpath(); - operationContext.CurrentPath.Rectangle(LowerLeft.X, LowerLeft.Y, Width, Height); + var lowerLeftTransform = operationContext.CurrentTransformationMatrix.Transform(LowerLeft); + operationContext.CurrentPath.Rectangle(lowerLeftTransform.X, lowerLeftTransform.Y, Width, Height); } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs index 9895670c..55851649 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs @@ -44,13 +44,15 @@ /// public void Run(IOperationContext operationContext) { + var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(ControlPoint2); + var endTransform = operationContext.CurrentTransformationMatrix.Transform(End); operationContext.CurrentPath.BezierCurveTo(operationContext.CurrentPosition.X, operationContext.CurrentPosition.Y, - ControlPoint2.X, - ControlPoint2.Y, - End.X, - End.Y); - operationContext.CurrentPosition = End; + controlPoint2Transform.X, + controlPoint2Transform.Y, + endTransform.X, + endTransform.Y); + operationContext.CurrentPosition = endTransform; } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs index 22ccd217..b6d513e4 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs @@ -35,8 +35,9 @@ /// public void Run(IOperationContext operationContext) { - operationContext.CurrentPath.LineTo(End.X, End.Y); - operationContext.CurrentPosition = End; + var endPoint = operationContext.CurrentTransformationMatrix.Transform(new PdfPoint(End.X, End.Y)); + operationContext.CurrentPath.LineTo(endPoint.X, endPoint.Y); + operationContext.CurrentPosition = endPoint; } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs index b08d83ba..5c28dc74 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs @@ -36,8 +36,9 @@ public void Run(IOperationContext operationContext) { operationContext.BeginSubpath(); - operationContext.CurrentPosition = Point; - operationContext.CurrentPath.LineTo(Point.X, Point.Y); + var pointTransform = operationContext.CurrentTransformationMatrix.Transform(Point); + operationContext.CurrentPosition = pointTransform; + operationContext.CurrentPath.MoveTo(pointTransform.X, pointTransform.Y); } ///