]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
reuse old patterns and shadings to reduce file size
authorKeiron Liddle <keiron@apache.org>
Fri, 19 Jul 2002 07:16:21 +0000 (07:16 +0000)
committerKeiron Liddle <keiron@apache.org>
Fri, 19 Jul 2002 07:16:21 +0000 (07:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195010 13f79535-47bb-0310-9956-ffa450edef68

src/org/apache/fop/pdf/PDFDocument.java
src/org/apache/fop/pdf/PDFFunction.java
src/org/apache/fop/pdf/PDFPattern.java
src/org/apache/fop/pdf/PDFResources.java
src/org/apache/fop/pdf/PDFShading.java

index afc1014f1433c33f4ba3e095e1b799d4d16df1fa..715e957427fd5c7b98d795a26e3abd3a95044955 100644 (file)
@@ -139,6 +139,8 @@ public class PDFDocument {
 
     protected ArrayList gstates = new ArrayList();
     protected ArrayList functions = new ArrayList();
+    protected ArrayList shadings = new ArrayList();
+    protected ArrayList patterns = new ArrayList();
 
     /**
      * creates an empty PDF document <p>
@@ -370,6 +372,32 @@ public class PDFDocument {
         return null;
     }
 
+    private PDFShading findShading(PDFShading compare) {
+        for(Iterator iter = shadings.iterator(); iter.hasNext(); ) {
+            Object shad = iter.next();
+            if(compare.equals(shad)) {
+                return (PDFShading)shad;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Find a previous pattern.
+     * The problem with this is for tiling patterns the pattern
+     * data stream is stored and may use up memory, usually this
+     * would only be a small amount of data.
+     */
+    private PDFPattern findPattern(PDFPattern compare) {
+        for(Iterator iter = patterns.iterator(); iter.hasNext(); ) {
+            Object patt = iter.next();
+            if(compare.equals(patt)) {
+                return (PDFPattern)patt;
+            }
+        }
+        return null;
+    }
+
     /**
      * Make a Type 3 Stitching function
      *
@@ -488,7 +516,16 @@ public class PDFDocument {
                                             theColorSpace, theBackground,
                                             theBBox, theAntiAlias, theDomain,
                                             theMatrix, theFunction);
-        this.objects.add(shading);
+
+        PDFShading oldshad = findShading(shading);
+        if(oldshad == null) {
+            shadings.add(shading);
+            this.objects.add(shading);
+        } else {
+            this.objectcount--;
+            this.shadingCount--;
+            shading = oldshad;
+        }
 
         // add this shading to resources
         if(res != null) {
@@ -534,12 +571,22 @@ public class PDFDocument {
                                             theDomain, theFunction,
                                             theExtend);
 
+        PDFShading oldshad = findShading(shading);
+        if(oldshad == null) {
+            shadings.add(shading);
+            this.objects.add(shading);
+        } else {
+            this.objectcount--;
+            this.shadingCount--;
+            shading = oldshad;
+        }
+
         if(res != null) {
             res.getPDFResources().addShading(shading);
         } else {
             this.resources.addShading(shading);
         }
-        this.objects.add(shading);
+
         return (shading);
     }
 
@@ -584,13 +631,22 @@ public class PDFDocument {
                                             theBitsPerFlag, theDecode,
                                             theFunction);
 
+        PDFShading oldshad = findShading(shading);
+        if(oldshad == null) {
+            shadings.add(shading);
+            this.objects.add(shading);
+        } else {
+            this.objectcount--;
+            this.shadingCount--;
+            shading = oldshad;
+        }
+
         if(res != null) {
             res.getPDFResources().addShading(shading);
         } else {
             this.resources.addShading(shading);
         }
 
-        this.objects.add(shading);
         return (shading);
     }
 
@@ -632,14 +688,22 @@ public class PDFDocument {
                                             theBitsPerComponent, theDecode,
                                             theVerticesPerRow, theFunction);
 
+        PDFShading oldshad = findShading(shading);
+        if(oldshad == null) {
+            shadings.add(shading);
+            this.objects.add(shading);
+        } else {
+            this.objectcount--;
+            this.shadingCount--;
+            shading = oldshad;
+        }
+
         if(res != null) {
             res.getPDFResources().addShading(shading);
         } else {
             this.resources.addShading(shading);
         }
 
-        this.objects.add(shading);
-
         return (shading);
     }
 
@@ -671,12 +735,21 @@ public class PDFDocument {
                                             theMatrix, theXUID,
                                             thePatternDataStream);
 
+        PDFPattern oldpatt = findPattern(pattern);
+        if(oldpatt == null) {
+            patterns.add(pattern);
+            this.objects.add(pattern);
+        } else {
+            this.objectcount--;
+            this.patternCount--;
+            pattern = oldpatt;
+        }
+
         if(res != null) {
             res.getPDFResources().addPattern(pattern);
         } else {
             this.resources.addPattern(pattern);
         }
-        this.objects.add(pattern);
 
         return (pattern);
     }
@@ -699,12 +772,21 @@ public class PDFDocument {
                                             thePatternName, 2, theShading,
                                             theXUID, theExtGState, theMatrix);
 
+        PDFPattern oldpatt = findPattern(pattern);
+        if(oldpatt == null) {
+            patterns.add(pattern);
+            this.objects.add(pattern);
+        } else {
+            this.objectcount--;
+            this.patternCount--;
+            pattern = oldpatt;
+        }
+
         if(res != null) {
             res.getPDFResources().addPattern(pattern);
         } else {
             this.resources.addPattern(pattern);
         }
-        this.objects.add(pattern);
 
         return (pattern);
     }
index 35d1480a553ffaafa63b91b52ddaea41046d519f..ad265ff91b72831ab2fb0ec1153ed22f2e4cf0d3 100644 (file)
@@ -671,6 +671,9 @@ public class PDFFunction extends PDFObject {
         if(obj == null) {
             return false;
         }
+        if(obj == this) {
+            return true;
+        }
         if(!(obj instanceof PDFFunction)) {
             return false;
         }
index d11112f35f676cc2c7e23a0b499a1a5426b4559f..ba0296a4a507316bf996acfd486adfdb235b3119 100644 (file)
@@ -94,7 +94,6 @@ public class PDFPattern extends PDFPathPaint {
      */
     protected StringBuffer patternDataStream = null;
 
-
     /**
      * Create a tiling pattern (type 1).
      *
@@ -308,4 +307,90 @@ public class PDFPattern extends PDFPathPaint {
 
     public byte[] toPDF() { return null; }
 
+    public boolean equals(Object obj) {
+        if(obj == null) {
+            return false;
+        }
+        if(obj == this) {
+            return true;
+        }
+        if(!(obj instanceof PDFPattern)) {
+            return false;
+        }
+        PDFPattern patt = (PDFPattern)obj;
+        if(patternType != patt.patternType) {
+            return false;
+        }
+        if(paintType != patt.paintType) {
+            return false;
+        }
+        if(tilingType != patt.tilingType) {
+            return false;
+        }
+        if(xStep != patt.xStep) {
+            return false;
+        }
+        if(yStep != patt.yStep) {
+            return false;
+        }
+        if(bBox != null) {
+            if(!bBox.equals(patt.bBox)) {
+                return false;
+            }
+        } else if(patt.bBox != null) {
+            return false;
+        }
+        if(bBox != null) {
+            if(!bBox.equals(patt.bBox)) {
+                return false;
+            }
+        } else if(patt.bBox != null) {
+            return false;
+        }
+        if(xUID != null) {
+            if(!xUID.equals(patt.xUID)) {
+                return false;
+            }
+        } else if(patt.xUID != null) {
+            return false;
+        }
+        if(extGState != null) {
+            if(!extGState.equals(patt.extGState)) {
+                return false;
+            }
+        } else if(patt.extGState != null) {
+            return false;
+        }
+        if(matrix != null) {
+            if(!matrix.equals(patt.matrix)) {
+                return false;
+            }
+        } else if(patt.matrix != null) {
+            return false;
+        }
+        if(resources != null) {
+            if(!resources.equals(patt.resources)) {
+                return false;
+            }
+        } else if(patt.resources != null) {
+            return false;
+        }
+        if(shading != null) {
+            if(!shading.equals(patt.shading)) {
+                return false;
+            }
+        } else if(patt.shading != null) {
+            return false;
+        }
+        if(patternDataStream != null) {
+            if(!patternDataStream.equals(patt.patternDataStream)) {
+                return false;
+            }
+        } else if(patt.patternDataStream != null) {
+            return false;
+        }
+
+        return true;
+    }
+
 }
index 312d37d3fbb8acdf2d50923ecd10bb9a046e62ce..890131c489f2abc0a8d91598180f3a5f6d6061db 100644 (file)
@@ -28,8 +28,8 @@ public class PDFResources extends PDFObject {
     protected HashMap fonts = new HashMap();
 
     protected HashSet xObjects = new HashSet();
-    protected ArrayList patterns = new ArrayList();
-    protected ArrayList shadings = new ArrayList();
+    protected HashSet patterns = new HashSet();
+    protected HashSet shadings = new HashSet();
     protected HashSet gstates = new HashSet();
 
     /**
@@ -94,12 +94,8 @@ public class PDFResources extends PDFObject {
         if (!this.shadings.isEmpty()) {
             p.append("/Shading << ");
 
-            for (int currentShadingNumber = 0;
-                    currentShadingNumber < this.shadings.size();
-                    currentShadingNumber++) {
-                currentShading =
-                    ((PDFShading)this.shadings.get(currentShadingNumber));
-
+            for (Iterator iter = shadings.iterator(); iter.hasNext(); ) {
+                currentShading = (PDFShading)iter.next();
                 p.append("/" + currentShading.getName() + " "
                          + currentShading.referencePDF() + " ");    // \n ??????
             }
@@ -113,12 +109,8 @@ public class PDFResources extends PDFObject {
         if (!this.patterns.isEmpty()) {
             p.append("/Pattern << ");
 
-            for (int currentPatternNumber = 0;
-                    currentPatternNumber < this.patterns.size();
-                    currentPatternNumber++) {
-                currentPattern =
-                    ((PDFPattern)this.patterns.get(currentPatternNumber));
-
+            for (Iterator iter = patterns.iterator(); iter.hasNext(); ) {
+                currentPattern = (PDFPattern)iter.next();
                 p.append("/" + currentPattern.getName() + " "
                          + currentPattern.referencePDF() + " ");
             }
index d4521e7aea398365ce8e1248a19c21373eb3c593..fdbb2339e7471c75583f2d0863e2f094b250e9a3 100644 (file)
@@ -505,4 +505,98 @@ public class PDFShading extends PDFObject {
         return (p.toString().getBytes());
     }
 
+    public boolean equals(Object obj) {
+        if(obj == null) {
+            return false;
+        }
+        if(obj == this) {
+            return true;
+        }
+        if(!(obj instanceof PDFShading)) {
+            return false;
+        }
+        PDFShading shad = (PDFShading)obj;
+        if(shadingType != shad.shadingType) {
+            return false;
+        }
+        if(antiAlias != shad.antiAlias) {
+            return false;
+        }
+        if(bitsPerCoordinate != shad.bitsPerCoordinate) {
+            return false;
+        }
+        if(bitsPerFlag != shad.bitsPerFlag) {
+            return false;
+        }
+        if(bitsPerComponent != shad.bitsPerComponent) {
+            return false;
+        }
+        if(verticesPerRow != shad.verticesPerRow) {
+            return false;
+        }
+        if(colorSpace != null) {
+            if(!colorSpace.equals(shad.colorSpace)) {
+                return false;
+            }
+        } else if(shad.colorSpace != null) {
+            return false;
+        }
+        if(background != null) {
+            if(!background.equals(shad.background)) {
+                return false;
+            }
+        } else if(shad.background != null) {
+            return false;
+        }
+        if(bBox != null) {
+            if(!bBox.equals(shad.bBox)) {
+                return false;
+            }
+        } else if(shad.bBox != null) {
+            return false;
+        }
+        if(domain != null) {
+            if(!domain.equals(shad.domain)) {
+                return false;
+            }
+        } else if(shad.domain != null) {
+            return false;
+        }
+        if(matrix != null) {
+            if(!matrix.equals(shad.matrix)) {
+                return false;
+            }
+        } else if(shad.matrix != null) {
+            return false;
+        }
+        if(coords != null) {
+            if(!coords.equals(shad.coords)) {
+                return false;
+            }
+        } else if(shad.coords != null) {
+            return false;
+        }
+        if(extend != null) {
+            if(!extend.equals(shad.extend)) {
+                return false;
+            }
+        } else if(shad.extend != null) {
+            return false;
+        }
+        if(decode != null) {
+            if(!decode.equals(shad.decode)) {
+                return false;
+            }
+        } else if(shad.decode != null) {
+            return false;
+        }
+        if(function != null) {
+            if(!function.equals(shad.function)) {
+                return false;
+            }
+        } else if(shad.function != null) {
+            return false;
+        }
+        return true;
+    }
 }