pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1606283 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/graphics/pattern/ main/java/org/apache/pdfbox/pdmodel/graphics/state/ main/java/org/apache/pdfbox/rendering/ main/java/org/apache/pdfbox/util/ main/java/org/apache/...
Date Sat, 28 Jun 2014 02:39:28 GMT
Author: jahewson
Date: Sat Jun 28 02:39:28 2014
New Revision: 1606283

URL: http://svn.apache.org/r1606283
Log:
PDFBOX-2126: Improve handling of clipping path - thanks to Petr Slaby

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/TilingPaint.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/MoveText.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetMatrix.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipEvenOddRule.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipNonZeroRule.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/Invoke.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/TestMatrix.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/TilingPaint.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/TilingPaint.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/TilingPaint.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/pattern/TilingPaint.java
Sat Jun 28 02:39:28 2014
@@ -124,7 +124,7 @@ public class TilingPaint extends Texture
         else
         {
             // undo translation
-            matrix = (Matrix)pattern.getMatrix().clone();
+            matrix = pattern.getMatrix().clone();
             matrix.setValue(2, 0, matrix.getValue(2, 0) - (float)rect.getX()); // tx
             matrix.setValue(2, 1, matrix.getValue(2, 1) - (float)rect.getY()); // ty
         }

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=1606283&r1=1606282&r2=1606283&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
Sat Jun 28 02:39:28 2014
@@ -20,10 +20,9 @@ import java.awt.BasicStroke;
 import java.awt.Composite;
 import java.awt.Rectangle;
 import java.awt.Shape;
+import java.awt.geom.Area;
 import java.awt.geom.GeneralPath;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern;
 import org.apache.pdfbox.pdmodel.graphics.blend.BlendComposite;
@@ -34,25 +33,14 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.util.Matrix;
 
 /**
- * This class will hold the current state of the graphics parameters when executing a
- * content stream.
+ * The current state of the graphics parameters when executing a content stream.
  *
- * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.5 $
+ * @author Ben Litchfield
  */
 public class PDGraphicsState implements Cloneable
 {
-
-    /**
-     * Log instance.
-     */
-    private static final Log LOG = LogFactory.getLog(PDGraphicsState.class);
-    
+    private Area clippingPath;
     private Matrix currentTransformationMatrix = new Matrix();
-
-    //Here are some attributes of the Graphics state, but have not been created yet.
-    //
-    //clippingPath
     private PDColor strokingColor = PDColor.DEVICE_GRAY_BLACK;
     private PDColor nonStrokingColor = PDColor.DEVICE_GRAY_BLACK;
     private PDColorSpace strokingColorSpace = PDDeviceGray.INSTANCE;
@@ -65,13 +53,13 @@ public class PDGraphicsState implements 
     private PDLineDashPattern lineDashPattern = new PDLineDashPattern();
     private String renderingIntent;
     private boolean strokeAdjustment = false;
-    //blend mode
-    //soft mask
+    private BlendMode blendMode = BlendMode.COMPATIBLE;
+    private PDSoftMask softMask;
     private double alphaConstants = 1.0;
     private double nonStrokingAlphaConstants = 1.0;
     private boolean alphaSource = false;
 
-    //DEVICE DEPENDENT parameters
+    // DEVICE-DEPENDENT parameters
     private boolean overprint = false;
     private double overprintMode = 0;
     //black generation
@@ -81,11 +69,6 @@ public class PDGraphicsState implements 
     private double flatness = 1.0;
     private double smoothness = 0;
 
-    private GeneralPath currentClippingPath;
-
-    private BlendMode blendMode = BlendMode.COMPATIBLE;
-    private PDSoftMask softMask;
-
     /**
      * Default constructor.
      */
@@ -99,7 +82,7 @@ public class PDGraphicsState implements 
      */
     public PDGraphicsState(PDRectangle page)
     {
-        currentClippingPath = new GeneralPath(new Rectangle(page.createDimension()));
+        clippingPath = new Area(new GeneralPath(new Rectangle(page.createDimension())));
         if (page.getLowerLeftX() != 0 || page.getLowerLeftY() != 0)
         {
             //Compensate for offset
@@ -473,33 +456,25 @@ public class PDGraphicsState implements 
         renderingIntent = value;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public Object clone()
+    @Override
+    public PDGraphicsState clone()
     {
-        PDGraphicsState clone = null;
         try
         {
-            clone = (PDGraphicsState)super.clone();
-            clone.setTextState( (PDTextState)textState.clone() );
-            clone.setCurrentTransformationMatrix( currentTransformationMatrix.copy() );
+            PDGraphicsState clone = (PDGraphicsState)super.clone();
+            clone.textState = textState.clone();
+            clone.currentTransformationMatrix = currentTransformationMatrix.clone();
             clone.strokingColor = strokingColor; // immutable
             clone.nonStrokingColor = nonStrokingColor; // immutable
-            if( lineDashPattern != null )
-            {
-                clone.setLineDashPattern( lineDashPattern ); // immutable
-            }
-            if (currentClippingPath != null)
-            {
-                clone.setCurrentClippingPath((GeneralPath)currentClippingPath.clone());
-            }
+            clone.lineDashPattern = lineDashPattern; // immutable
+            clone.clippingPath = (Area) clippingPath.clone();
+            return clone;
         }
-        catch( CloneNotSupportedException e )
+        catch (CloneNotSupportedException e)
         {
-            LOG.error(e,e);
+            // should not happen
+            throw new RuntimeException(e);
         }
-        return clone;
     }
 
     /**
@@ -583,39 +558,22 @@ public class PDGraphicsState implements 
     }
 
     /**
-     * This will set the current clipping path.
-     *
-     * @param pCurrentClippingPath The current clipping path.
-     *
+     * Modify the current clipping path by intersecting it with the given path.
+     * @param path path to intersect with the clipping path
      */
-    public void setCurrentClippingPath(Shape pCurrentClippingPath)
+    public void intersectClippingPath(GeneralPath path)
     {
-        if (pCurrentClippingPath != null)
-        {
-            if (pCurrentClippingPath instanceof GeneralPath)
-            {
-                currentClippingPath = (GeneralPath)pCurrentClippingPath;
-            }
-            else
-            {
-                currentClippingPath = new GeneralPath();
-                currentClippingPath.append(pCurrentClippingPath,false);
-            }
-        }
-        else
-        {
-            currentClippingPath = null;
-        }
+        clippingPath.intersect(new Area(path));
     }
 
     /**
-     * This will get the current clipping path.
+     * This will get the current clipping path. Do not modify this Area object!
      *
      * @return The current clipping path.
      */
-    public Shape getCurrentClippingPath()
+    public Area getCurrentClippingPath()
     {
-        return currentClippingPath;
+        return clippingPath;
     }
 
     public Composite getStrokeJavaComposite() 

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java
Sat Jun 28 02:39:28 2014
@@ -254,19 +254,17 @@ public class PDTextState implements Clon
         knockout = value;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public Object clone()
+    @Override
+    public PDTextState clone()
     {
         try
         {
-            return super.clone();
+            return (PDTextState)super.clone();
         }
-        catch (CloneNotSupportedException ignore)
+        catch (CloneNotSupportedException e)
         {
-            //ignore
+            // should not happen
+            throw new RuntimeException(e);
         }
-        return null;
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Sat Jun
28 02:39:28 2014
@@ -104,9 +104,8 @@ public class PageDrawer extends PDFStrea
 
     private Graphics2D graphics;
 
-    // clipping winding rule used for the clipping path.
-    private int clippingWindingRule = -1;
-
+    // clipping winding rule used for the clipping path
+    private int clipWindingRule = -1;
     private GeneralPath linePath = new GeneralPath();
 
     private Map<PDFont, Glyph2D> fontGlyph2D = new HashMap<PDFont, Glyph2D>();
@@ -764,40 +763,26 @@ public class PageDrawer extends PDFStrea
     }
 
     /**
-     * Set the clipping winding rule.
+     * Modify the current clipping path by intersecting it with the current path.
+     * The clipping path will not be updated until the succeeding painting operator is called.
      * 
      * @param windingRule The winding rule which will be used for clipping.
-     * 
      */
-    public void setClippingWindingRule(int windingRule)
+    public void clip(int windingRule)
     {
-        clippingWindingRule = windingRule;
+        clipWindingRule = windingRule;
     }
 
     /**
-     * Set the clipping Path.
-     * 
+     * Ends the current path without filling or stroking it. The clipping path is updated
here.
      */
     public void endPath()
     {
-        if (clippingWindingRule > -1)
+        if (clipWindingRule != -1)
         {
-            PDGraphicsState graphicsState = getGraphicsState();
-            GeneralPath clippingPath = (GeneralPath) linePath.clone();  // TODO do we really
need to clone this? isn't the line path reset anyway?
-            clippingPath.setWindingRule(clippingWindingRule);
-            // If there is already set a clipping path, we have to intersect the new with
the existing one
-            if (graphicsState.getCurrentClippingPath() != null)
-            {
-                Area currentArea = new Area(getGraphicsState().getCurrentClippingPath());
-                Area newArea = new Area(clippingPath);
-                currentArea.intersect(newArea);
-                graphicsState.setCurrentClippingPath(currentArea);
-            }
-            else
-            {
-                graphicsState.setCurrentClippingPath(clippingPath);
-            }
-            clippingWindingRule = -1;
+            linePath.setWindingRule(clipWindingRule);
+            getGraphicsState().intersectClippingPath(linePath);
+            clipWindingRule = -1;
         }
         linePath.reset();
     }
@@ -984,11 +969,11 @@ public class PageDrawer extends PDFStrea
             }
             matrix = matrix1;
 
-            PDGraphicsState gs = getGraphicsState();
-            gs.setBlendMode(BlendMode.NORMAL);
-            gs.setAlphaConstants(1.0);
-            gs.setNonStrokeAlphaConstants(1.0);
-            gs.setSoftMask(null);
+            PDGraphicsState state = getGraphicsState();
+            state.setBlendMode(BlendMode.NORMAL);
+            state.setAlphaConstants(1.0);
+            state.setNonStrokeAlphaConstants(1.0);
+            state.setSoftMask(null);
             graphics = groupG2D;
             try
             {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Matrix.java Sat Jun 28 02:39:28
2014
@@ -308,7 +308,8 @@ public class Matrix implements Cloneable
      * Clones this object.
      * @return cloned matrix as an object.
      */
-    public Object clone()
+    @Override
+    public Matrix clone()
     {
         Matrix clone = new Matrix();
         System.arraycopy( single, 0, clone.single, 0, 9 );
@@ -316,16 +317,6 @@ public class Matrix implements Cloneable
     }
 
     /**
-     * This will copy the text matrix data.
-     *
-     * @return a matrix that matches this one.
-     */
-    public Matrix copy()
-    {
-        return (Matrix) clone();
-    }
-
-    /**
      * This will return a string representation of the matrix.
      *
      * @return The matrix as a string.

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java Sat Jun
28 02:39:28 2014
@@ -550,7 +550,7 @@ public class PDFStreamEngine
      */
     public void saveGraphicsState()
     {
-        graphicsStack.push((PDGraphicsState) getGraphicsState().clone());
+        graphicsStack.push(getGraphicsState().clone());
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/MoveText.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/MoveText.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/MoveText.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/MoveText.java Sat Jun
28 02:39:28 2014
@@ -43,6 +43,6 @@ public class MoveText extends OperatorPr
         td.setValue( 2, 0, x.floatValue() );
         td.setValue( 2, 1, y.floatValue() );
         context.setTextLineMatrix( td.multiply( context.getTextLineMatrix() ) );
-        context.setTextMatrix( context.getTextLineMatrix().copy() );
+        context.setTextMatrix( context.getTextLineMatrix().clone() );
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetMatrix.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetMatrix.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetMatrix.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetMatrix.java Sat Jun
28 02:39:28 2014
@@ -53,6 +53,6 @@ public class SetMatrix extends OperatorP
         textMatrix.setValue( 2, 0, e.floatValue() );
         textMatrix.setValue( 2, 1, f.floatValue() );
         context.setTextMatrix( textMatrix );
-        context.setTextLineMatrix( textMatrix.copy() );
+        context.setTextLineMatrix( textMatrix.clone() );
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipEvenOddRule.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipEvenOddRule.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipEvenOddRule.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipEvenOddRule.java
Sat Jun 28 02:39:28 2014
@@ -33,6 +33,6 @@ public final class ClipEvenOddRule exten
     @Override
     public void process(PDFOperator operator, List<COSBase> operands)
     {
-        ((PageDrawer)context).setClippingWindingRule(GeneralPath.WIND_EVEN_ODD);
+        ((PageDrawer)context).clip(GeneralPath.WIND_EVEN_ODD);
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipNonZeroRule.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipNonZeroRule.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipNonZeroRule.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/ClipNonZeroRule.java
Sat Jun 28 02:39:28 2014
@@ -33,6 +33,6 @@ public class ClipNonZeroRule extends Ope
     @Override
     public void process(PDFOperator operator, List<COSBase> operands)
     {
-        ((PageDrawer)context).setClippingWindingRule(GeneralPath.WIND_NON_ZERO);
+        ((PageDrawer)context).clip(GeneralPath.WIND_NON_ZERO);
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/Invoke.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/Invoke.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/Invoke.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/Invoke.java
Sat Jun 28 02:39:28 2014
@@ -144,11 +144,7 @@ public final class Invoke extends Operat
                     bboxPath.lineTo((float) p3.getX(), (float) p3.getY());
                     bboxPath.closePath();
                     
-                    Area resultClippingArea = new Area(graphicsState.getCurrentClippingPath());
-                    Area newArea = new Area(bboxPath);            
-                    resultClippingArea.intersect(newArea);
-                    
-                    graphicsState.setCurrentClippingPath(resultClippingArea);
+                    graphicsState.intersectClippingPath(bboxPath);
                 }
                 getContext().processSubStream(pdResources, formContentStream);
 

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/TestMatrix.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/TestMatrix.java?rev=1606283&r1=1606282&r2=1606283&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/TestMatrix.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/TestMatrix.java Sat Jun 28 02:39:28
2014
@@ -46,7 +46,7 @@ public class TestMatrix extends TestCase
         Matrix m1 = new Matrix();
         assertMatrixIsPristine(m1);
 
-        Matrix m2 = m1.copy();
+        Matrix m2 = m1.clone();
         assertNotSame(m1, m2);
         assertMatrixIsPristine(m2);
     }
@@ -68,8 +68,8 @@ public class TestMatrix extends TestCase
             }
         }
 
-        Matrix m1 = testMatrix.copy();
-        Matrix m2 = testMatrix.copy();
+        Matrix m1 = testMatrix.clone();
+        Matrix m2 = testMatrix.clone();
 
         // Multiply two matrices together producing a new result matrix.
         Matrix product = m1.multiply(m2);
@@ -127,8 +127,8 @@ public class TestMatrix extends TestCase
 
 
         // Multiply two matrices together with the result being written into 'this' matrix
-        m1 = testMatrix.copy();
-        m2 = testMatrix.copy();
+        m1 = testMatrix.clone();
+        m2 = testMatrix.clone();
 
         retVal = m1.multiply(m2, m1);
         assertSame(retVal, m1);
@@ -143,7 +143,7 @@ public class TestMatrix extends TestCase
 
 
         // Multiply the same matrix with itself with the result being written into 'this'
matrix
-        m1 = testMatrix.copy();
+        m1 = testMatrix.clone();
 
         retVal = m1.multiply(m1, m1);
         assertSame(retVal, m1);



Mime
View raw message