From 181fa9d837cd83db9d60f49e6c819a3c098996c2 Mon Sep 17 00:00:00 2001 From: Alexander Vinnikov Date: Thu, 23 Oct 2025 15:50:22 +0200 Subject: [PATCH] make transform stack consistent --- .../Graphics/TestOperationContext.cs | 6 +++--- .../Graphics/BaseStreamProcessor.cs | 15 +++++++-------- .../Graphics/ContentStreamProcessor.cs | 1 + src/UglyToad.PdfPig/Graphics/IOperationContext.cs | 2 +- .../ModifyCurrentTransformationMatrix.cs | 3 ++- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs index 3768f286..426c40df 100644 --- a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs +++ b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs @@ -270,10 +270,10 @@ GetCurrentState().FontState.WordSpacing = spacing; } - public void ModifyCurrentTransformationMatrix(double[] value) + public void ModifyCurrentTransformationMatrix(TransformationMatrix value) { - var ctm = GetCurrentState().CurrentTransformationMatrix; - GetCurrentState().CurrentTransformationMatrix = TransformationMatrix.FromArray(value).Multiply(ctm); + var state = GetCurrentState(); + state.CurrentTransformationMatrix = value.Multiply(state.CurrentTransformationMatrix); } public void SetCharacterSpacing(double spacing) diff --git a/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs index 544b99c6..17502c48 100644 --- a/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs @@ -563,8 +563,8 @@ .ToArray()); } - // 2. Update current transformation matrix. - startState.CurrentTransformationMatrix = formMatrix.Multiply(startState.CurrentTransformationMatrix); + // 2. Update current transformation matrix. + ModifyCurrentTransformationMatrix(formMatrix); var contentStream = formStream.Decode(FilterProvider, PdfScanner); @@ -576,9 +576,8 @@ if (formStream.StreamDictionary.TryGet(NameToken.Bbox, PdfScanner, out var bboxToken)) { var points = bboxToken.Data.OfType().Select(x => x.Double).ToArray(); - PdfRectangle bbox = new PdfRectangle(points[0], points[1], points[2], points[3]); - PdfRectangle transformedBox = startState.CurrentTransformationMatrix.Transform(bbox).Normalise(); - ClipToRectangle(transformedBox, FillingRule.EvenOdd); // TODO - Check that Even Odd is valid + PdfRectangle bbox = new PdfRectangle(points[0], points[1], points[2], points[3]).Normalise(); + ClipToRectangle(bbox, FillingRule.EvenOdd); // TODO - Check that Even Odd is valid } // 4. Paint the objects. @@ -958,10 +957,10 @@ } /// - public virtual void ModifyCurrentTransformationMatrix(double[] value) + public virtual void ModifyCurrentTransformationMatrix(TransformationMatrix value) { - var ctm = GetCurrentState().CurrentTransformationMatrix; - GetCurrentState().CurrentTransformationMatrix = TransformationMatrix.FromArray(value).Multiply(ctm); + var state = GetCurrentState(); + state.CurrentTransformationMatrix = value.Multiply(state.CurrentTransformationMatrix); } /// diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index b8bb6106..b1371a11 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -452,6 +452,7 @@ namespace UglyToad.PdfPig.Graphics { // https://github.com/apache/pdfbox/blob/f4bfe47de37f6fe69e8f98b164c3546facfd5e91/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDFStreamEngine.java#L611 var graphicsState = GetCurrentState(); + rectangle = graphicsState.CurrentTransformationMatrix.Transform(rectangle).Normalise(); var clip = rectangle.ToPdfPath(); clip.SetClipping(clippingRule); diff --git a/src/UglyToad.PdfPig/Graphics/IOperationContext.cs b/src/UglyToad.PdfPig/Graphics/IOperationContext.cs index 71a23ff0..6a631dde 100644 --- a/src/UglyToad.PdfPig/Graphics/IOperationContext.cs +++ b/src/UglyToad.PdfPig/Graphics/IOperationContext.cs @@ -243,7 +243,7 @@ /// /// Modify the current transformation matrix by concatenating the specified matrix. /// - void ModifyCurrentTransformationMatrix(double[] value); + void ModifyCurrentTransformationMatrix(TransformationMatrix value); /// /// Set the character spacing to a number expressed in unscaled text space units. diff --git a/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/ModifyCurrentTransformationMatrix.cs b/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/ModifyCurrentTransformationMatrix.cs index c9d18893..df3f012a 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/ModifyCurrentTransformationMatrix.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/ModifyCurrentTransformationMatrix.cs @@ -2,6 +2,7 @@ { using System; using System.IO; + using PdfPig.Core; /// /// @@ -43,7 +44,7 @@ /// public void Run(IOperationContext operationContext) { - operationContext.ModifyCurrentTransformationMatrix(Value); + operationContext.ModifyCurrentTransformationMatrix(TransformationMatrix.FromArray(Value)); } ///