diff options
author | Keiron Liddle <keiron@apache.org> | 2002-06-28 10:09:07 +0000 |
---|---|---|
committer | Keiron Liddle <keiron@apache.org> | 2002-06-28 10:09:07 +0000 |
commit | 73d16e990b458dfbd9db7d68085da601c49d9c42 (patch) | |
tree | 776879d5a2a26abec2228cacf6ff0781db239256 /src/org/apache/fop/pdf | |
parent | 00dd98af348dcae07fa4f0ea8838ccb2603e7d94 (diff) | |
download | xmlgraphics-fop-73d16e990b458dfbd9db7d68085da601c49d9c42.tar.gz xmlgraphics-fop-73d16e990b458dfbd9db7d68085da601c49d9c42.zip |
handles pattern resources better
made stream filters externally configured
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194934 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/org/apache/fop/pdf')
-rw-r--r-- | src/org/apache/fop/pdf/BitmapImage.java | 6 | ||||
-rw-r--r-- | src/org/apache/fop/pdf/PDFDocument.java | 94 | ||||
-rw-r--r-- | src/org/apache/fop/pdf/PDFPage.java | 45 | ||||
-rw-r--r-- | src/org/apache/fop/pdf/PDFPattern.java | 40 | ||||
-rw-r--r-- | src/org/apache/fop/pdf/PDFResourceContext.java | 83 | ||||
-rw-r--r-- | src/org/apache/fop/pdf/PDFResources.java | 8 | ||||
-rw-r--r-- | src/org/apache/fop/pdf/PDFStream.java | 37 |
7 files changed, 211 insertions, 102 deletions
diff --git a/src/org/apache/fop/pdf/BitmapImage.java b/src/org/apache/fop/pdf/BitmapImage.java index dd11de8bb..cc23f3df1 100644 --- a/src/org/apache/fop/pdf/BitmapImage.java +++ b/src/org/apache/fop/pdf/BitmapImage.java @@ -8,7 +8,7 @@ package org.apache.fop.pdf; import java.io.IOException; - +import java.util.HashMap; public class BitmapImage implements PDFImage { int m_height; int m_width; @@ -18,6 +18,7 @@ public class BitmapImage implements PDFImage { String maskRef; PDFColor transparent = null; String key; + HashMap filters; public BitmapImage(String k, int width, int height, byte[] result, String mask) { @@ -31,6 +32,7 @@ public class BitmapImage implements PDFImage { } public void setup(PDFDocument doc) { + filters = doc.getFilterMap(); } public String getKey() { @@ -87,7 +89,7 @@ public class BitmapImage implements PDFImage { imgStream.setData(m_bitmaps); - imgStream.addDefaultFilters(); + imgStream.addDefaultFilters(filters, PDFStream.CONTENT_FILTER); return imgStream; } diff --git a/src/org/apache/fop/pdf/PDFDocument.java b/src/org/apache/fop/pdf/PDFDocument.java index 79d7dde7b..b1471a3b6 100644 --- a/src/org/apache/fop/pdf/PDFDocument.java +++ b/src/org/apache/fop/pdf/PDFDocument.java @@ -136,10 +136,17 @@ public class PDFDocument { protected HashMap xObjectsMap = new HashMap(); /** + * the Font Map. + */ + protected HashMap fontMap = new HashMap(); + + /** * the objects themselves */ protected ArrayList pendingLinks = null; + protected HashMap filterMap = new HashMap(); + /** * creates an empty PDF document <p> * @@ -176,6 +183,14 @@ public class PDFDocument { this.info.setProducer(producer); } + public void setFilterMap(HashMap map) { + filterMap = map; + } + + public HashMap getFilterMap() { + return filterMap; + } + /** * Make a /Catalog (Root) object. This object is written in * the trailer. @@ -426,7 +441,7 @@ public class PDFDocument { * It's optional, the default is the identity matrix * @param theFunction The PDF Function that maps an (x,y) location to a color */ - public PDFShading makeShading(int theShadingType, + public PDFShading makeShading(PDFResourceContext res, int theShadingType, PDFColorSpace theColorSpace, ArrayList theBackground, ArrayList theBBox, boolean theAntiAlias, ArrayList theDomain, @@ -442,7 +457,11 @@ public class PDFDocument { this.objects.add(shading); // add this shading to resources - this.resources.addShading(shading); + if(res != null) { + res.getPDFResources().addShading(shading); + } else { + this.resources.addShading(shading); + } return (shading); } @@ -466,7 +485,7 @@ public class PDFDocument { * @param theExtend ArrayList of Booleans of whether to extend teh start and end colors past the start and end points * The default is [false, false] */ - public PDFShading makeShading(int theShadingType, + public PDFShading makeShading(PDFResourceContext res, int theShadingType, PDFColorSpace theColorSpace, ArrayList theBackground, ArrayList theBBox, boolean theAntiAlias, ArrayList theCoords, @@ -481,8 +500,11 @@ public class PDFDocument { theDomain, theFunction, theExtend); - this.resources.addShading(shading); - + if(res != null) { + res.getPDFResources().addShading(shading); + } else { + this.resources.addShading(shading); + } this.objects.add(shading); return (shading); } @@ -509,7 +531,7 @@ public class PDFDocument { * @param theDecode ArrayList of Doubles see PDF 1.3 spec pages 303 to 312. * @param theFunction the PDFFunction */ - public PDFShading makeShading(int theShadingType, + public PDFShading makeShading(PDFResourceContext res, int theShadingType, PDFColorSpace theColorSpace, ArrayList theBackground, ArrayList theBBox, boolean theAntiAlias, @@ -528,7 +550,11 @@ public class PDFDocument { theBitsPerFlag, theDecode, theFunction); - this.resources.addShading(shading); + if(res != null) { + res.getPDFResources().addShading(shading); + } else { + this.resources.addShading(shading); + } this.objects.add(shading); return (shading); @@ -554,7 +580,7 @@ public class PDFDocument { * @param theVerticesPerRow number of vertices in each "row" of the lattice. * @param theFunction The PDFFunction that's mapped on to this shape */ - public PDFShading makeShading(int theShadingType, + public PDFShading makeShading(PDFResourceContext res, int theShadingType, PDFColorSpace theColorSpace, ArrayList theBackground, ArrayList theBBox, boolean theAntiAlias, @@ -572,7 +598,11 @@ public class PDFDocument { theBitsPerComponent, theDecode, theVerticesPerRow, theFunction); - this.resources.addShading(shading); + if(res != null) { + res.getPDFResources().addShading(shading); + } else { + this.resources.addShading(shading); + } this.objects.add(shading); @@ -593,7 +623,7 @@ public class PDFDocument { * @param theXUID Optional vector of Integers that uniquely identify the pattern * @param thePatternDataStream The stream of pattern data to be tiled. */ - public PDFPattern makePattern(int thePatternType, // 1 + public PDFPattern makePattern(PDFResourceContext res, int thePatternType, // 1 PDFResources theResources, int thePaintType, int theTilingType, ArrayList theBBox, double theXStep, double theYStep, ArrayList theMatrix, ArrayList theXUID, StringBuffer thePatternDataStream) { @@ -607,7 +637,11 @@ public class PDFDocument { theMatrix, theXUID, thePatternDataStream); - this.resources.addPattern(pattern); + if(res != null) { + res.getPDFResources().addPattern(pattern); + } else { + this.resources.addPattern(pattern); + } this.objects.add(pattern); return (pattern); @@ -622,7 +656,7 @@ public class PDFDocument { * @param theExtGState optional: the extended graphics state, if used. * @param theMatrix Optional:ArrayList of Doubles that specify the matrix. */ - public PDFPattern makePattern(int thePatternType, PDFShading theShading, + public PDFPattern makePattern(PDFResourceContext res, int thePatternType, PDFShading theShading, ArrayList theXUID, StringBuffer theExtGState, ArrayList theMatrix) { String thePatternName = new String("Pa" + (++this.patternCount)); @@ -631,7 +665,11 @@ public class PDFDocument { thePatternName, 2, theShading, theXUID, theExtGState, theMatrix); - this.resources.addPattern(pattern); + if(res != null) { + res.getPDFResources().addPattern(pattern); + } else { + this.resources.addPattern(pattern); + } this.objects.add(pattern); return (pattern); @@ -646,7 +684,7 @@ public class PDFDocument { return; } - public PDFPattern createGradient(boolean radial, + public PDFPattern createGradient(PDFResourceContext res, boolean radial, PDFColorSpace theColorspace, ArrayList theColors, ArrayList theBounds, ArrayList theCoords) { @@ -697,7 +735,7 @@ public class PDFDocument { if (radial) { if (theCoords.size() == 6) { - myShad = this.makeShading(3, this.colorspace, null, null, + myShad = this.makeShading(res, 3, this.colorspace, null, null, false, theCoords, null, myfunky, null); } else { // if the center x, center y, and radius specifiy @@ -711,18 +749,18 @@ public class PDFDocument { newCoords.add(theCoords.get(1)); newCoords.add(new Double(0.0)); - myShad = this.makeShading(3, this.colorspace, null, null, + myShad = this.makeShading(res, 3, this.colorspace, null, null, false, newCoords, null, myfunky, null); } } else { - myShad = this.makeShading(2, this.colorspace, null, null, false, + myShad = this.makeShading(res, 2, this.colorspace, null, null, false, theCoords, null, myfunky, null); } - myPattern = this.makePattern(2, myShad, null, null, null); + myPattern = this.makePattern(res, 2, myShad, null, null, null); return (myPattern); } @@ -757,8 +795,11 @@ public class PDFDocument { this.objects.add(iccStream); return iccStream; } - - + + public HashMap getFontMap() { + return fontMap; + } + /** * make a Type1 /Font object * @@ -772,6 +813,9 @@ public class PDFDocument { public PDFFont makeFont(String fontname, String basefont, String encoding, FontMetric metrics, FontDescriptor descriptor) { + if(fontMap.containsKey(basefont)) { + return (PDFFont)fontMap.get(basefont); + } /* * create a PDFFont with the next object number and add to the @@ -918,7 +962,11 @@ public class PDFDocument { return gstate; } - public PDFXObject addImage(PDFImage img) { + public PDFXObject addImage(PDFResourceContext res, PDFImage img) { + if(res != null) { + res.getPDFResources().setXObjects(xObjects); + } + // check if already created String key = img.getKey(); PDFXObject xObject = (PDFXObject)xObjectsMap.get(key); @@ -1092,7 +1140,7 @@ public class PDFDocument { * * @return the stream object created */ - public PDFStream makeStream() { + public PDFStream makeStream(String type) { /* * create a PDFStream with the next object number and add it @@ -1100,7 +1148,7 @@ public class PDFDocument { * to the list of objects */ PDFStream obj = new PDFStream(++this.objectcount); - obj.addDefaultFilters(); + obj.addDefaultFilters(filterMap, type); this.objects.add(obj); return obj; diff --git a/src/org/apache/fop/pdf/PDFPage.java b/src/org/apache/fop/pdf/PDFPage.java index 739abe955..807dbc1d8 100644 --- a/src/org/apache/fop/pdf/PDFPage.java +++ b/src/org/apache/fop/pdf/PDFPage.java @@ -21,7 +21,7 @@ package org.apache.fop.pdf; * to the memory profile this was causing OOM issues. So, we store * only the object ID of the parent, rather than the parent itself. */ -public class PDFPage extends PDFObject { +public class PDFPage extends PDFResourceContext { /** * the page's parent, a PDF reference object @@ -29,11 +29,6 @@ public class PDFPage extends PDFObject { protected String parent; /** - * the page's /Resource object - */ - protected PDFResources resources; - - /** * the contents stream */ protected PDFStream contents; @@ -49,12 +44,6 @@ public class PDFPage extends PDFObject { protected int pageheight; /** - * the list of annotation objects for this page - */ - protected PDFAnnotList annotList; - protected PDFDocument document; - - /** * create a /Page object * * @param number the object's number @@ -67,16 +56,12 @@ public class PDFPage extends PDFObject { int pagewidth, int pageheight) { /* generic creation of object */ - super(number); + super(number, doc, resources); /* set fields using parameters */ - this.document = doc; - this.resources = resources; this.contents = contents; this.pagewidth = pagewidth; this.pageheight = pageheight; - - this.annotList = null; } /** @@ -91,15 +76,11 @@ public class PDFPage extends PDFObject { int pagewidth, int pageheight) { /* generic creation of object */ - super(number); + super(number, doc, resources); /* set fields using parameters */ - this.document = doc; - this.resources = resources; this.pagewidth = pagewidth; this.pageheight = pageheight; - - this.annotList = null; } /** @@ -121,26 +102,6 @@ public class PDFPage extends PDFObject { } /** - * set this page's annotation list - * - * @param annotList a PDFAnnotList list of annotations - */ - public void addAnnotation(PDFObject annot) { - if(this.annotList == null) { - this.annotList = document.makeAnnotList(); - } - this.annotList.addAnnot(annot); - } - - public void addGState(PDFGState gstate) { - this.resources.addGState(gstate); - } - - public void addShading(PDFShading shading) { - this.resources.addShading(shading); - } - - /** * represent this object as PDF * * @return the PDF string diff --git a/src/org/apache/fop/pdf/PDFPattern.java b/src/org/apache/fop/pdf/PDFPattern.java index ab9c0b645..432af941c 100644 --- a/src/org/apache/fop/pdf/PDFPattern.java +++ b/src/org/apache/fop/pdf/PDFPattern.java @@ -9,6 +9,9 @@ package org.apache.fop.pdf; // Java... import java.util.ArrayList; +import java.util.HashMap; +import java.io.ByteArrayOutputStream; +import java.io.IOException; /** * class representing a PDF Function. @@ -32,7 +35,7 @@ public class PDFPattern extends PDFPathPaint { /** * Either one (1) for tiling, or two (2) for shading. */ - protected int patternType = 2; // Default + protected int patternType = 2; // Default /** * The name of the pattern such as "Pa1" or "Pattern1" @@ -75,11 +78,11 @@ public class PDFPattern extends PDFPathPaint { protected ArrayList xUID = null; /** + * TODO use PDFGState * String representing the extended Graphics state. * Probably will never be used like this. */ protected StringBuffer extGState = null; - // eventually, need a PDFExtGSState object... but not now. /** * ArrayList of Doubles representing the Transformation matrix. @@ -128,7 +131,6 @@ public class PDFPattern extends PDFPathPaint { this.yStep = theYStep; this.matrix = theMatrix; this.xUID = theXUID; - // TODO filter this stream this.patternDataStream = thePatternDataStream; } @@ -198,12 +200,14 @@ public class PDFPattern extends PDFPathPaint { p.append(this.number + " " + this.generation + " obj\n<< \n/Type /Pattern \n"); - if (this.resources != null) { + if(this.resources != null) { p.append("/Resources " + this.resources.referencePDF() + " \n"); } p.append("/PatternType " + this.patternType + " \n"); + PDFStream dataStream = null; + if (this.patternType == 1) { p.append("/PaintType " + this.paintType + " \n"); p.append("/TilingType " + this.tilingType + " \n"); @@ -240,9 +244,19 @@ public class PDFPattern extends PDFPathPaint { } p.append("] \n"); } + // don't forget the length of the stream. if (this.patternDataStream != null) { - p.append("/Length " + (this.patternDataStream.length() + 1) + dataStream = new PDFStream(0); + dataStream.add(this.patternDataStream.toString()); + // TODO get the filters from the doc + dataStream.addDefaultFilters(new HashMap(), PDFStream.CONTENT_FILTER); + try { + p.append(dataStream.applyFilters()); + } catch(IOException e) { + + } + p.append("/Length " + (dataStream.getDataLength() + 1) + " \n"); } @@ -261,8 +275,7 @@ public class PDFPattern extends PDFPathPaint { p.append("] \n"); } - if (this.extGState - != null) { // will probably have to change this if it's used. + if (this.extGState != null) { p.append("/ExtGState " + this.extGState + " \n"); } @@ -280,8 +293,17 @@ public class PDFPattern extends PDFPathPaint { p.append(">> \n"); // stream representing the function - if (this.patternDataStream != null) { - p.append("stream\n" + this.patternDataStream + "\nendstream\n"); + if (dataStream != null) { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write(p.toString().getBytes()); + int length = dataStream.outputStreamData(baos); + + baos.write(("endobj\n").getBytes()); + return baos.toByteArray(); + } catch(IOException e) { + + } } p.append("endobj\n"); diff --git a/src/org/apache/fop/pdf/PDFResourceContext.java b/src/org/apache/fop/pdf/PDFResourceContext.java new file mode 100644 index 000000000..77039ab88 --- /dev/null +++ b/src/org/apache/fop/pdf/PDFResourceContext.java @@ -0,0 +1,83 @@ +/* + * $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources. + */ + +package org.apache.fop.pdf; + +/** + * class representing a /Page object. + * + * There is one of these for every page in a PDF document. The object + * specifies the dimensions of the page and references a /Resources + * object, a contents stream and the page's parent in the page + * hierarchy. + * + * Modified by Mark Lillywhite, mark-fop@inomial.com. The Parent + * object was being referred to by reference, but all that we + * ever used from the Parent was it's PDF object ID, and according + * to the memory profile this was causing OOM issues. So, we store + * only the object ID of the parent, rather than the parent itself. + */ +public class PDFResourceContext extends PDFObject { + + /** + * the page's /Resource object + */ + protected PDFResources resources; + + /** + * the list of annotation objects for this page + */ + protected PDFAnnotList annotList; + protected PDFDocument document; + + /** + * + * @param number the object's number + * @param resources the /Resources object + * @param contents the content stream + * @param pagewidth the page's width in points + * @param pageheight the page's height in points + */ + public PDFResourceContext(int number, PDFDocument doc, PDFResources resources) { + + /* generic creation of object */ + super(number); + + /* set fields using parameters */ + this.document = doc; + this.resources = resources; + this.annotList = null; + } + + public PDFResources getPDFResources() { + return this.resources; + } + + /** + * set this page's annotation list + * + * @param annotList a PDFAnnotList list of annotations + */ + public void addAnnotation(PDFObject annot) { + if(this.annotList == null) { + this.annotList = document.makeAnnotList(); + } + this.annotList.addAnnot(annot); + } + + public void addGState(PDFGState gstate) { + this.resources.addGState(gstate); + } + + public void addShading(PDFShading shading) { + this.resources.addShading(shading); + } + + public byte[] toPDF() { + return null; + } +} diff --git a/src/org/apache/fop/pdf/PDFResources.java b/src/org/apache/fop/pdf/PDFResources.java index 34b5d82b9..778f05e4c 100644 --- a/src/org/apache/fop/pdf/PDFResources.java +++ b/src/org/apache/fop/pdf/PDFResources.java @@ -37,10 +37,8 @@ public class PDFResources extends PDFObject { * @param number the object's number */ public PDFResources(int number) { - /* generic creation of object */ super(number); - } /** @@ -131,7 +129,7 @@ public class PDFResources extends PDFObject { p.append("/ProcSet [ /PDF /ImageC /Text ]\n"); - if (!this.xObjects.isEmpty()) { + if (this.xObjects != null && !this.xObjects.isEmpty()) { p = p.append("/XObject <<"); for (int i = 1; i <= this.xObjects.size(); i++) { p = p.append("/Im" + i + " " @@ -147,9 +145,9 @@ public class PDFResources extends PDFObject { PDFGState gs = (PDFGState)this.gstates.get(i); p = p.append("/" + gs.getName() + " " + gs.referencePDF() - + "\n"); + + " "); } - p = p.append(" >>\n"); + p = p.append(">>\n"); } p = p.append(">>\nendobj\n"); diff --git a/src/org/apache/fop/pdf/PDFStream.java b/src/org/apache/fop/pdf/PDFStream.java index 76f2e6719..ef5dcd5f7 100644 --- a/src/org/apache/fop/pdf/PDFStream.java +++ b/src/org/apache/fop/pdf/PDFStream.java @@ -11,8 +11,8 @@ import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.Enumeration; -import org.apache.fop.configuration.Configuration; /** * class representing a PDF stream. @@ -23,6 +23,11 @@ import org.apache.fop.configuration.Configuration; * length. */ public class PDFStream extends PDFObject { + public static final String DEFAULT_FILTER = "default"; + public static final String CONTENT_FILTER = "content"; + public static final String IMAGE_FILTER = "image"; + public static final String JPEG_FILTER = "jpeg"; + public static final String FONT_FILTER = "font"; /** * the stream of PDF commands @@ -95,29 +100,22 @@ public class PDFStream extends PDFObject { } } - - public void addDefaultFilters() { - ArrayList filters = Configuration.getListValue("stream-filter-list", - Configuration.PDF); - if (filters == null) { - // try getting it as a String - String filter = Configuration.getStringValue("stream-filter-list", - Configuration.PDF); - if (filter == null) { - // built-in default to flate - addFilter(new FlateFilter()); - } else { - addFilter(filter); - } + public void addDefaultFilters(HashMap filters, String type) { + ArrayList filterset = (ArrayList)filters.get(type); + if(filterset == null) { + filterset = (ArrayList)filters.get(DEFAULT_FILTER); + } + if(filterset == null || filterset.size() == 0) { + // built-in default to flate + addFilter(new FlateFilter()); } else { - for (int i = 0; i < filters.size(); i++) { - String v = (String)filters.get(i); + for (int i = 0; i < filterset.size(); i++) { + String v = (String)filterset.get(i); addFilter(v); } } } - /** * append an array of xRGB pixels, ASCII Hex Encoding it first * @@ -175,8 +173,6 @@ public class PDFStream extends PDFObject { } } - - /** * represent as PDF. * @@ -236,7 +232,6 @@ public class PDFStream extends PDFObject { } - /** * Apply the filters to the data * in the order given and return the /Filter and /DecodeParms |