From 96d15aa21a00f19dc84f4f1d5dec203bd8494774 Mon Sep 17 00:00:00 2001 From: Keiron Liddle Date: Fri, 19 Jul 2002 07:16:21 +0000 Subject: [PATCH] reuse old patterns and shadings to reduce file size 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 | 96 ++++++++++++++++++++++-- src/org/apache/fop/pdf/PDFFunction.java | 3 + src/org/apache/fop/pdf/PDFPattern.java | 87 ++++++++++++++++++++- src/org/apache/fop/pdf/PDFResources.java | 20 ++--- src/org/apache/fop/pdf/PDFShading.java | 94 +++++++++++++++++++++++ 5 files changed, 278 insertions(+), 22 deletions(-) diff --git a/src/org/apache/fop/pdf/PDFDocument.java b/src/org/apache/fop/pdf/PDFDocument.java index afc1014f1..715e95742 100644 --- a/src/org/apache/fop/pdf/PDFDocument.java +++ b/src/org/apache/fop/pdf/PDFDocument.java @@ -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

@@ -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); } diff --git a/src/org/apache/fop/pdf/PDFFunction.java b/src/org/apache/fop/pdf/PDFFunction.java index 35d1480a5..ad265ff91 100644 --- a/src/org/apache/fop/pdf/PDFFunction.java +++ b/src/org/apache/fop/pdf/PDFFunction.java @@ -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; } diff --git a/src/org/apache/fop/pdf/PDFPattern.java b/src/org/apache/fop/pdf/PDFPattern.java index d11112f35..ba0296a4a 100644 --- a/src/org/apache/fop/pdf/PDFPattern.java +++ b/src/org/apache/fop/pdf/PDFPattern.java @@ -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; + } + } diff --git a/src/org/apache/fop/pdf/PDFResources.java b/src/org/apache/fop/pdf/PDFResources.java index 312d37d3f..890131c48 100644 --- a/src/org/apache/fop/pdf/PDFResources.java +++ b/src/org/apache/fop/pdf/PDFResources.java @@ -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() + " "); } diff --git a/src/org/apache/fop/pdf/PDFShading.java b/src/org/apache/fop/pdf/PDFShading.java index d4521e7ae..fdbb2339e 100644 --- a/src/org/apache/fop/pdf/PDFShading.java +++ b/src/org/apache/fop/pdf/PDFShading.java @@ -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; + } } -- 2.39.5