You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by ss...@apache.org on 2014/09/15 11:37:22 UTC

svn commit: r1624980 - /xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java

Author: ssteiner
Date: Mon Sep 15 09:37:21 2014
New Revision: 1624980

URL: http://svn.apache.org/r1624980
Log:
FOP-2412: Reduce PDF to PS filesize

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java?rev=1624980&r1=1624979&r2=1624980&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java Mon Sep 15 09:37:21 2014
@@ -44,6 +44,8 @@ import java.awt.image.ImageObserver;
 import java.awt.image.RenderedImage;
 import java.awt.image.renderable.RenderableImage;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.xmlgraphics.java2d.AbstractGraphics2D;
 import org.apache.xmlgraphics.java2d.GraphicContext;
@@ -69,6 +71,8 @@ public class PSGraphics2D extends Abstra
 
     private static final boolean DEBUG = false;
 
+    protected List<Integer> pathHashCache = new ArrayList<Integer>();
+    protected boolean startCache;
     /**
      * The G2D instance that represents the root instance
      * (used in context with create()/dispose()). Null if this instance is the root instance.
@@ -378,7 +382,7 @@ public class PSGraphics2D extends Abstra
      * @return the winding rule of the path defining the shape
      * @throws IOException In case of an I/O problem.
      */
-    public int processShape(Shape s) throws IOException {
+    public int processShape(Shape s, boolean cached) throws IOException {
         if (s instanceof Rectangle2D) {
             // Special optimization in case of Rectangle Shape
             Rectangle2D r = (Rectangle2D) s;
@@ -386,54 +390,79 @@ public class PSGraphics2D extends Abstra
             return PathIterator.WIND_NON_ZERO;
         } else {
             PathIterator iter = s.getPathIterator(IDENTITY_TRANSFORM);
-            processPathIterator(iter);
+            if (cached) {
+                processPathIteratorCached(s);
+            } else {
+                processPathIterator(iter);
+            }
             return iter.getWindingRule();
         }
     }
 
-    /**
-     * Processes a path iterator generating the necessary painting operations.
-     * @param iter PathIterator to process
-     * @throws IOException In case of an I/O problem.
-     */
-    public void processPathIterator(PathIterator iter) throws IOException {
+    protected String processPathIteratorToString(PathIterator iter) throws IOException {
+        StringBuilder cmd = new StringBuilder();
         double[] vals = new double[6];
         while (!iter.isDone()) {
             int type = iter.currentSegment(vals);
             switch (type) {
             case PathIterator.SEG_CUBICTO:
-                gen.writeln(gen.formatDouble(vals[0]) + " "
-                                 + gen.formatDouble(vals[1]) + " "
-                                 + gen.formatDouble(vals[2]) + " "
-                                 + gen.formatDouble(vals[3]) + " "
-                                 + gen.formatDouble(vals[4]) + " "
-                                 + gen.formatDouble(vals[5]) + " "
-                                 + gen.mapCommand("curveto"));
+                cmd.append(gen.formatDouble(vals[0])).append(" ").append(gen.formatDouble(vals[1])).append(" ")
+                        .append(gen.formatDouble(vals[2])).append(" ").append(gen.formatDouble(vals[3])).append(" ")
+                        .append(gen.formatDouble(vals[4])).append(" ").append(gen.formatDouble(vals[5])).append(" ")
+                        .append(gen.mapCommand("curveto")).append("\n");
                 break;
             case PathIterator.SEG_LINETO:
-                gen.writeln(gen.formatDouble(vals[0]) + " "
-                                 + gen.formatDouble(vals[1]) + " "
-                                 + gen.mapCommand("lineto"));
+                cmd.append(gen.formatDouble(vals[0])).append(" ").append(gen.formatDouble(vals[1])).append(" ")
+                        .append(gen.mapCommand("lineto")).append("\n");
                 break;
             case PathIterator.SEG_MOVETO:
-                gen.writeln(gen.formatDouble(vals[0]) + " "
-                                 + gen.formatDouble(vals[1]) + " "
-                                 + gen.mapCommand("moveto"));
+                cmd.append(gen.formatDouble(vals[0])).append(" ").append(gen.formatDouble(vals[1])).append(" ")
+                        .append(gen.mapCommand("moveto")).append("\n");
                 break;
             case PathIterator.SEG_QUADTO:
-                gen.writeln(gen.formatDouble(vals[0]) + " "
-                          + gen.formatDouble(vals[1]) + " "
-                          + gen.formatDouble(vals[2]) + " "
-                          + gen.formatDouble(vals[3]) + " QT");
+                cmd.append(gen.formatDouble(vals[0])).append(" ").append(gen.formatDouble(vals[1])).append(" ")
+                        .append(gen.formatDouble(vals[2])).append(" ").append(gen.formatDouble(vals[3])).append(" QT")
+                        .append("\n");
                 break;
             case PathIterator.SEG_CLOSE:
-                gen.writeln(gen.mapCommand("closepath"));
+                cmd.append(gen.mapCommand("closepath")).append("\n");
                 break;
             default:
                 break;
             }
             iter.next();
         }
+        return cmd.toString().trim();
+    }
+
+    protected void processPathIteratorCached(Shape s) throws IOException {
+        String cmd = processPathIteratorToString(s.getPathIterator(IDENTITY_TRANSFORM));
+        int hash = cmd.hashCode();
+        if (!startCache) {
+            if (pathHashCache.contains(hash)) {
+                startCache = true;
+                pathHashCache.clear();
+            } else {
+                gen.writeln(cmd);
+                pathHashCache.add(hash);
+            }
+        }
+        if (startCache) {
+            if (!pathHashCache.contains(hash)) {
+                gen.writeln("/f" + hash + "{" + cmd + "}def");
+                pathHashCache.add(hash);
+            }
+            gen.writeln("f" + hash);
+        }
+    }
+
+    /**
+     * Processes a path iterator generating the nexessary painting operations.
+     * @param iter PathIterator to process
+     * @throws IOException In case of an I/O problem.
+     */
+    public void processPathIterator(PathIterator iter) throws IOException {
+        gen.writeln(processPathIteratorToString(iter));
     }
 
     /**
@@ -473,7 +502,7 @@ public class PSGraphics2D extends Abstra
             applyStroke(getStroke());
 
             gen.writeln(gen.mapCommand("newpath"));
-            processShape(s);
+            processShape(s, false);
             doDrawing(false, true, false);
             gen.restoreGraphicsState();
         } catch (IOException ioe) {
@@ -513,7 +542,7 @@ public class PSGraphics2D extends Abstra
             preparePainting();
             try {
                 gen.writeln(gen.mapCommand("newpath"));
-                processShape(s);
+                processShape(s, false);
                 // clip area
                 gen.writeln(gen.mapCommand("clip"));
             } catch (IOException ioe) {
@@ -776,7 +805,7 @@ public class PSGraphics2D extends Abstra
             applyPaint(getPaint(), true);
 
             gen.writeln(gen.mapCommand("newpath"));
-            int windingRule = processShape(s);
+            int windingRule = processShape(s, true);
             doDrawing(true, false,
                     windingRule == PathIterator.WIND_EVEN_ODD);
             gen.restoreGraphicsState();



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org