Author: lehmi
Date: Sat Feb 22 11:22:09 2014
New Revision: 1570819

URL: http://svn.apache.org/r1570819
Log:
PDFBOX-1917: avoid to apply the ctm to the dash array again and again when 
calling stroke path

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfviewer/PageDrawer.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/StrokePath.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfviewer/PageDrawer.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfviewer/PageDrawer.java?rev=1570819&r1=1570818&r2=1570819&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfviewer/PageDrawer.java 
(original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfviewer/PageDrawer.java 
Sat Feb 22 11:22:09 2014
@@ -66,8 +66,6 @@ import org.apache.pdfbox.pdmodel.font.PD
 import org.apache.pdfbox.pdmodel.font.PDType3Font;
 import org.apache.pdfbox.pdmodel.graphics.PDGraphicsState;
 import org.apache.pdfbox.pdmodel.graphics.PDShading;
-import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
-import org.apache.pdfbox.pdmodel.graphics.color.PDPattern;
 import org.apache.pdfbox.pdmodel.graphics.shading.AxialShadingPaint;
 import org.apache.pdfbox.pdmodel.graphics.shading.PDShadingResources;
 import org.apache.pdfbox.pdmodel.graphics.shading.PDShadingType2;
@@ -102,6 +100,8 @@ public class PageDrawer extends PDFStrea
 
     private GeneralPath linePath = new GeneralPath();
 
+    private BasicStroke stroke = null;
+
     private Map<PDFont, Glyph2D> fontGlyph2D = new HashMap<PDFont, Glyph2D>();
     private Map<PDFont, Font> awtFonts = new HashMap<PDFont, Font>();
 
@@ -654,7 +654,7 @@ public class PageDrawer extends PDFStrea
      */
     public void setStroke(BasicStroke newStroke)
     {
-        getGraphics().setStroke(newStroke);
+        stroke = newStroke;
     }
 
     /**
@@ -665,7 +665,50 @@ public class PageDrawer extends PDFStrea
      */
     public BasicStroke getStroke()
     {
-        return (BasicStroke) getGraphics().getStroke();
+        return stroke;
+    }
+
+    /**
+     * Create a new stroke based on the current ctm and the current stroke.
+     * 
+     * @return the transformed stroke
+     */
+    private BasicStroke calculateStroke()
+    {
+        float lineWidth = (float)getGraphicsState().getLineWidth();
+        Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
+        if (ctm != null && ctm.getXScale() > 0)
+        {
+            lineWidth = lineWidth * ctm.getXScale();
+        }
+        if (lineWidth < 0.25)
+        {
+            lineWidth = 0.25f;
+        }
+        BasicStroke currentStroke = null;
+        if (stroke == null)
+        {
+            currentStroke = new BasicStroke(lineWidth);
+        }
+        else
+        {
+            float phaseStart = stroke.getDashPhase();
+            float[] dashArray = stroke.getDashArray();
+            if (dashArray != null)
+            {
+                if (ctm != null && ctm.getXScale() > 0)
+                {
+                    for (int i = 0; i < dashArray.length; ++i)
+                    {
+                        dashArray[i] *= ctm.getXScale();
+                    }
+                }
+                phaseStart *= ctm.getXScale();
+            }
+            currentStroke = new BasicStroke(lineWidth, stroke.getEndCap(), 
stroke.getLineJoin(),
+                    stroke.getMiterLimit(), dashArray, phaseStart);
+        }
+        return currentStroke;
     }
 
     /**
@@ -684,6 +727,7 @@ public class PageDrawer extends PDFStrea
             strokingPaint = Color.WHITE;
         }
         graphics.setPaint(strokingPaint);
+        graphics.setStroke(calculateStroke());
         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_OFF);
         graphics.setClip(getGraphicsState().getCurrentClippingPath());
         GeneralPath path = getLinePath();

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/StrokePath.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/StrokePath.java?rev=1570819&r1=1570818&r2=1570819&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/StrokePath.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/pagedrawer/StrokePath.java
 Sat Feb 22 11:22:09 2014
@@ -18,32 +18,23 @@ package org.apache.pdfbox.util.operator.
 
 import java.util.List;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.pdfviewer.PageDrawer;
-import org.apache.pdfbox.util.Matrix;
 import org.apache.pdfbox.util.PDFOperator;
 import org.apache.pdfbox.util.operator.OperatorProcessor;
 
-import java.awt.BasicStroke;
 import java.io.IOException;
 
 /**
  * Implementation of content stream operator for page drawer.
  *
  * @author <a href="mailto:b...@benlitchfield.com";>Ben Litchfield</a>
- * @version $Revision: 1.3 $
+ * 
  */
 public class StrokePath extends OperatorProcessor
 {
 
     /**
-     * Log instance.
-     */
-    private static final Log log = LogFactory.getLog(StrokePath.class);
-
-    /**
      * S stroke the path.
      *
      * @param operator The operator that is being executed.
@@ -53,51 +44,6 @@ public class StrokePath extends Operator
      */
     public void process(PDFOperator operator, List<COSBase> arguments) throws 
IOException
     {
-        ///dwilson 3/19/07 refactor
-        try
-        {
-            PageDrawer drawer = (PageDrawer) context;
-    
-            float lineWidth = (float) 
context.getGraphicsState().getLineWidth();
-            Matrix ctm = 
context.getGraphicsState().getCurrentTransformationMatrix();
-            if (ctm != null && ctm.getXScale() > 0)
-            {
-                lineWidth = lineWidth * ctm.getXScale();
-            }
-            
-            if (lineWidth < 0.25)
-            {
-                lineWidth = 0.25f;
-            }
-
-            BasicStroke stroke = drawer.getStroke();
-            if (stroke == null)
-            {
-                drawer.setStroke(new BasicStroke(lineWidth));
-            }
-            else
-            {
-                float phaseStart = stroke.getDashPhase();
-                float[] dashArray = stroke.getDashArray();
-                if (dashArray != null)
-                {
-                    if (ctm != null && ctm.getXScale() > 0)
-                    {
-                        for (int i = 0; i < dashArray.length; ++i)
-                        {
-                            dashArray[i] *= ctm.getXScale();
-                        }
-                    }
-                    phaseStart *= ctm.getXScale();
-                }
-                drawer.setStroke(new BasicStroke(lineWidth, 
stroke.getEndCap(), stroke.getLineJoin(),
-                        stroke.getMiterLimit(), dashArray, phaseStart));
-            }
-            drawer.strokePath();
-        }
-        catch (Exception exception)
-        {
-            log.warn(exception, exception);
-        }
+        ((PageDrawer)context).strokePath();
     }
 }


Reply via email to