You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2023/07/28 15:20:22 UTC

svn commit: r1911327 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: contentstream/PDFStreamEngine.java pdmodel/graphics/state/PDGraphicsState.java

Author: tilman
Date: Fri Jul 28 15:20:22 2023
New Revision: 1911327

URL: http://svn.apache.org/viewvc?rev=1911327&view=rev
Log:
PDFBOX-5359: move textMatrix and textLineMatrix into graphics state, as suggested by Jaromír Dobrý

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDFStreamEngine.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDFStreamEngine.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDFStreamEngine.java?rev=1911327&r1=1911326&r2=1911327&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDFStreamEngine.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDFStreamEngine.java Fri Jul 28 15:20:22 2023
@@ -78,9 +78,6 @@ public abstract class PDFStreamEngine
 
     private final Map<String, OperatorProcessor> operators = new HashMap<>(80);
 
-    private Matrix textMatrix;
-    private Matrix textLineMatrix;
-
     private Deque<PDGraphicsState> graphicsStack = new ArrayDeque<>();
 
     private PDResources resources;
@@ -123,8 +120,6 @@ public abstract class PDFStreamEngine
         currentPage = page;
         graphicsStack.clear();
         graphicsStack.push(new PDGraphicsState(page.getCropBox()));
-        textMatrix = null;
-        textLineMatrix = null;
         resources = null;
         initialMatrix = page.getMatrix();
     }
@@ -200,20 +195,15 @@ public abstract class PDFStreamEngine
         saveGraphicsState();
         Matrix softMaskCTM = getGraphicsState().getSoftMask().getInitialTransformationMatrix();
         getGraphicsState().setCurrentTransformationMatrix(softMaskCTM);
+        getGraphicsState().setTextMatrix(new Matrix());
+        getGraphicsState().setTextLineMatrix(new Matrix());
 
-        // PDFBOX-5621: save text matrices (softmasks may contain BT/ET)
-        Matrix textMatrixOld = textMatrix;
-        textMatrix = new Matrix();
-        Matrix textLineMatrixOld = textLineMatrix;
-        textLineMatrix = new Matrix();
         try
         {
             processTransparencyGroup(group);
         }
         finally
         {
-            textMatrix = textMatrixOld;
-            textLineMatrix = textLineMatrixOld;
             restoreGraphicsState();
         }
     }
@@ -295,11 +285,8 @@ public abstract class PDFStreamEngine
 
         // note: we don't clip to the BBox as it is often wrong, see PDFBOX-1917
 
-        // save text matrices (Type 3 stream may contain BT/ET, see PDFBOX-2137)
-        Matrix textMatrixOld = textMatrix;
-        textMatrix = new Matrix();
-        Matrix textLineMatrixOld = textLineMatrix;
-        textLineMatrix = new Matrix();
+        getGraphicsState().setTextMatrix(new Matrix());
+        getGraphicsState().setTextLineMatrix(new Matrix());
 
         try
         {
@@ -307,10 +294,6 @@ public abstract class PDFStreamEngine
         }
         finally
         {
-            // restore text matrices
-            textMatrix = textMatrixOld;
-            textLineMatrix = textLineMatrixOld;
-
             restoreGraphicsStack(savedStack);
             popResources(parent);
         }
@@ -436,18 +419,12 @@ public abstract class PDFStreamEngine
         // clip to bounding box
         clipToRect(tilingBBox);
 
-        // save text matrices (pattern stream may contain BT/ET, see PDFBOX-4896)
-        Matrix textMatrixSave = textMatrix;
-        Matrix textLineMatrixSave = textLineMatrix;
-
         try
         {
             processStreamOperators(tilingPattern);
         }
         finally
         {
-            textMatrix = textMatrixSave;
-            textLineMatrix = textLineMatrixSave;
             initialMatrix = parentMatrix;
             restoreGraphicsStack(savedStack);
             popResources(parent);
@@ -713,7 +690,7 @@ public abstract class PDFStreamEngine
     protected void applyTextAdjustment(float tx, float ty)
     {
         // update the text matrix
-        textMatrix.translate(tx, ty);
+        getGraphicsState().getTextMatrix().translate(tx, ty);
     }
 
     /**
@@ -766,7 +743,7 @@ public abstract class PDFStreamEngine
 
             // text rendering matrix (text space -> device space)
             Matrix ctm = state.getCurrentTransformationMatrix();
-            Matrix textRenderingMatrix = parameters.multiply(textMatrix).multiply(ctm);
+            Matrix textRenderingMatrix = parameters.multiply(getGraphicsState().getTextMatrix()).multiply(ctm);
 
             // get glyph's position vector if this is vertical text
             // changes to vertical text should be tested with PDFBOX-2294 and PDFBOX-1422
@@ -800,7 +777,7 @@ public abstract class PDFStreamEngine
             }
 
             // update the text matrix
-            textMatrix.translate(tx, ty);
+            getGraphicsState().getTextMatrix().translate(tx, ty);
         }
     }
 
@@ -1031,7 +1008,7 @@ public abstract class PDFStreamEngine
      */
     public Matrix getTextLineMatrix()
     {
-        return textLineMatrix;
+        return getGraphicsState().getTextLineMatrix();
     }
 
     /**
@@ -1039,7 +1016,7 @@ public abstract class PDFStreamEngine
      */
     public void setTextLineMatrix(Matrix value)
     {
-        textLineMatrix = value;
+        getGraphicsState().setTextLineMatrix(value);
     }
 
     /**
@@ -1047,7 +1024,7 @@ public abstract class PDFStreamEngine
      */
     public Matrix getTextMatrix()
     {
-        return textMatrix;
+        return getGraphicsState().getTextMatrix();
     }
 
     /**
@@ -1055,7 +1032,7 @@ public abstract class PDFStreamEngine
      */
     public void setTextMatrix(Matrix value)
     {
-        textMatrix = value;
+        getGraphicsState().setTextMatrix(value);
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java?rev=1911327&r1=1911326&r2=1911327&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java Fri Jul 28 15:20:22 2023
@@ -488,6 +488,8 @@ public class PDGraphicsState implements
             clone.clippingPaths = clippingPaths; // not cloned, see intersectClippingPath
             clone.clippingCache = clippingCache;
             clone.isClippingPathDirty = false;
+            clone.textLineMatrix = textLineMatrix == null ? null : textLineMatrix.clone();
+            clone.textMatrix = textMatrix == null ? null : textMatrix.clone();
             return clone;
         }
         catch (CloneNotSupportedException e)
@@ -686,4 +688,27 @@ public class PDGraphicsState implements
     {
         this.transfer = transfer;
     }
-}
+
+    private Matrix textMatrix = null;
+    private Matrix textLineMatrix = null;
+
+    public Matrix getTextLineMatrix()
+    {
+        return textLineMatrix;
+    }
+
+    public void setTextLineMatrix(Matrix value)
+    {
+        textLineMatrix = value;
+    }
+
+    public Matrix getTextMatrix()
+    {
+        return textMatrix;
+    }
+
+    public void setTextMatrix(Matrix value)
+    {
+        textMatrix = value;
+    }
+}
\ No newline at end of file