From: Jeremias Maerki Date: Thu, 27 Mar 2003 11:03:26 +0000 (+0000) Subject: Adjust to the PDF Stream changes. X-Git-Tag: Root_Temp_KnuthStylePageBreaking~1707 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=05d6a1943c642a95f1e852f2b66eab830024c5c1;p=xmlgraphics-fop.git Adjust to the PDF Stream changes. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196166 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/pdf/PDFFormXObject.java b/src/java/org/apache/fop/pdf/PDFFormXObject.java index 8907945f6..a483240b2 100644 --- a/src/java/org/apache/fop/pdf/PDFFormXObject.java +++ b/src/java/org/apache/fop/pdf/PDFFormXObject.java @@ -68,13 +68,12 @@ public class PDFFormXObject extends PDFXObject { * create a FormXObject with the given number and name and load the * image in the object * - * @param number the pdf object number * @param xnumber the pdf object X number * @param cont the pdf stream contents * @param ref the resource PDF reference */ - public PDFFormXObject(int number, int xnumber, PDFStream cont, String ref) { - super(number, xnumber, null); + public PDFFormXObject(int xnumber, PDFStream cont, String ref) { + super(xnumber, null); contents = cont; resRef = ref; } @@ -91,36 +90,50 @@ public class PDFFormXObject extends PDFXObject { protected int output(OutputStream stream) throws IOException { int length = 0; - String dictEntries = contents.applyFilters(); + String dictEntries = getFilterList().buildFilterDictEntries(); - String p = this.number + " " + this.generation + " obj\n"; - p = p + "<>\n"; + StringBuffer sb = new StringBuffer(128); + sb.append(getObjectID()); + sb.append("<>\n"); // push the pdf dictionary on the writer - byte[] pdfBytes = p.getBytes(); + byte[] pdfBytes = encode(sb.toString()); stream.write(pdfBytes); length += pdfBytes.length; - // push all the image data on the writer - // and takes care of length for trailer - length += contents.outputStreamData(stream); - pdfBytes = ("endobj\n").getBytes(); + //Send encoded stream to target OutputStream + length += outputStreamData(encodedStream, stream); + encodedStream.clear(); //Encoded stream can now be discarded + + pdfBytes = encode("endobj\n"); stream.write(pdfBytes); length += pdfBytes.length; + // let it gc // this object is retained as a reference to inserting // the same image but the image data is no longer needed contents = null; return length; } + + /** + * @see org.apache.fop.pdf.PDFStream#outputRawStreamData(OutputStream) + */ + protected void outputRawStreamData(OutputStream out) throws IOException { + contents.outputRawStreamData(out); + } + + } diff --git a/src/java/org/apache/fop/pdf/PDFICCStream.java b/src/java/org/apache/fop/pdf/PDFICCStream.java index 039a82fff..80263b552 100644 --- a/src/java/org/apache/fop/pdf/PDFICCStream.java +++ b/src/java/org/apache/fop/pdf/PDFICCStream.java @@ -51,6 +51,8 @@ package org.apache.fop.pdf; import java.awt.color.ICC_Profile; +import java.io.IOException; +import java.io.OutputStream; /** * Special PDFStream for ICC profiles (color profiles). @@ -64,10 +66,10 @@ public class PDFICCStream extends PDFStream { private PDFColorSpace pdfColorSpace; /** - * @see org.apache.fop.pdf.PDFObject#PDFObject(int) + * @see org.apache.fop.pdf.PDFObject#PDFObject() */ - public PDFICCStream(int num) { - super(num); + public PDFICCStream() { + super(); cp = null; } @@ -87,29 +89,37 @@ public class PDFICCStream extends PDFStream { * @see org.apache.fop.pdf.PDFObject#output(OutputStream) */ protected int output(java.io.OutputStream stream) - throws java.io.IOException { - - setData(cp.getData()); - - int length = 0; - String filterEntry = applyFilters(); - StringBuffer pb = new StringBuffer(); - pb.append(this.number).append(" ").append(this.generation).append(" obj\n<< "); - pb.append("/N ").append(cp.getNumComponents()).append(" "); + throws java.io.IOException { + int length = super.output(stream); + this.cp = null; //Free ICC stream when it's not used anymore + return length; + } + + /** + * @see org.apache.fop.pdf.PDFStream#outputRawStreamData(OutputStream) + */ + protected void outputRawStreamData(OutputStream out) throws IOException { + cp.write(out); + } + + /** + * @see org.apache.fop.pdf.AbstractPDFStream#buildStreamDict(String) + */ + protected String buildStreamDict(String lengthEntry) { + final String filterEntry = getFilterList().buildFilterDictEntries(); + final StringBuffer sb = new StringBuffer(128); + sb.append(getObjectID()); + sb.append("<< "); + sb.append("/N " + cp.getNumComponents()); if (pdfColorSpace != null) { - pb.append("/Alternate /").append(pdfColorSpace.getColorSpacePDFString()).append(" "); + sb.append("\n/Alternate /" + pdfColorSpace.getColorSpacePDFString() + " "); } - pb.append("/Length ").append((data.getSize() + 1)).append(" ").append(filterEntry); - pb.append(" >>\n"); - byte[] p = pb.toString().getBytes(); - stream.write(p); - length += p.length; - length += outputStreamData(stream); - p = "endobj\n".getBytes(); - stream.write(p); - length += p.length; - return length; + sb.append("\n/Length " + lengthEntry); + sb.append("\n" + filterEntry); + sb.append("\n>>\n"); + return sb.toString(); } + } diff --git a/src/java/org/apache/fop/pdf/PDFXObject.java b/src/java/org/apache/fop/pdf/PDFXObject.java index 94dabac25..91778109a 100644 --- a/src/java/org/apache/fop/pdf/PDFXObject.java +++ b/src/java/org/apache/fop/pdf/PDFXObject.java @@ -67,7 +67,8 @@ import java.io.OutputStream; * This is used as a reference for inserting the same image in the * document in another place. */ -public class PDFXObject extends PDFObject { +public class PDFXObject extends AbstractPDFStream { + private PDFImage pdfimage; private int xnum; @@ -75,12 +76,11 @@ public class PDFXObject extends PDFObject { * create an XObject with the given number and name and load the * image in the object * - * @param number the pdf object number * @param xnumber the pdf object X number * @param img the pdf image that contains the image data */ - public PDFXObject(int number, int xnumber, PDFImage img) { - super(number); + public PDFXObject(int xnumber, PDFImage img) { + super(); this.xnum = xnumber; pdfimage = img; } @@ -103,73 +103,8 @@ public class PDFXObject extends PDFObject { * @return the length of the data written */ protected int output(OutputStream stream) throws IOException { - int length = 0; - int i = 0; - - if (pdfimage.isPS()) { - length = outputEPSImage(stream); - } else { - - PDFStream imgStream = pdfimage.getDataStream(); - - String dictEntries = imgStream.applyFilters(); - - String p = this.number + " " + this.generation + " obj\n"; - p = p + "<>\n"; - - // push the pdf dictionary on the writer - byte[] pdfBytes = p.getBytes(); - stream.write(pdfBytes); - length += pdfBytes.length; - // push all the image data on the writer - // and takes care of length for trailer - length += imgStream.outputStreamData(stream); - - pdfBytes = ("endobj\n").getBytes(); - stream.write(pdfBytes); - length += pdfBytes.length; - } + int length = super.output(stream); + // let it gc // this object is retained as a reference to inserting // the same image but the image data is no longer needed @@ -177,37 +112,118 @@ public class PDFXObject extends PDFObject { return length; } - byte[] toPDF() { - return null; + /** + * @see org.apache.fop.pdf.AbstractPDFStream#buildStreamDict(String) + */ + protected String buildStreamDict(String lengthEntry) { + String dictEntries = getFilterList().buildFilterDictEntries(); + if (pdfimage.isPS()) { + return buildDictionaryFromPS(lengthEntry, dictEntries); + } else { + return buildDictionaryFromImage(lengthEntry, dictEntries); + } + } + + private String buildDictionaryFromPS(String lengthEntry, + String dictEntries) { + StringBuffer sb = new StringBuffer(128); + sb.append(getObjectID()); + sb.append("<>\n"); + return sb.toString(); } - private int outputEPSImage(OutputStream stream) throws IOException { - int length = 0; - int i = 0; - - PDFStream imgStream = pdfimage.getDataStream(); - String dictEntries = imgStream.applyFilters(); + private String buildDictionaryFromImage(String lengthEntry, + String dictEntries) { + StringBuffer sb = new StringBuffer(128); + sb.append(getObjectID()); + sb.append("<>\n"; + if (pdfimage.isTransparent()) { + PDFColor transp = pdfimage.getTransparentColor(); + sb.append("/Mask [" + + transp.red255() + " " + + transp.red255() + " " + + transp.green255() + " " + + transp.green255() + " " + + transp.blue255() + " " + + transp.blue255() + "]\n"); + } + String ref = pdfimage.getSoftMask(); + if (ref != null) { + sb.append("/SMask " + ref + "\n"); + } - // push the pdf dictionary on the writer - byte[] pdfBytes = p.getBytes(); - stream.write(pdfBytes); - length += pdfBytes.length; - // push all the image data on the writer and takes care of length for trailer - length += imgStream.outputStreamData(stream); + sb.append(dictEntries); + sb.append("\n>>\n"); + return sb.toString(); + } + + /** + * @see org.apache.fop.pdf.PDFStream#outputRawStreamData(OutputStream) + */ + protected void outputRawStreamData(OutputStream out) throws IOException { + pdfimage.outputContents(out); + } - pdfBytes = ("endobj\n").getBytes(); - stream.write(pdfBytes); - length += pdfBytes.length; + /** + * @see org.apache.fop.pdf.AbstractPDFStream#getSizeHint() + */ + protected int getSizeHint() throws IOException { + return 0; + } - return length; + /** + * @see org.apache.fop.pdf.AbstractPDFStream#prepareImplicitFilters() + */ + protected void prepareImplicitFilters() { + if (pdfimage.isDCT()) { + getFilterList().ensureDCTFilterInPlace(); + } + } + + /** + * This sets up the default filters for XObjects. It uses the PDFImage + * instance to determine what default filters to apply. + * @see org.apache.fop.pdf.AbstractPDFStream#setupFilterList() + */ + protected void setupFilterList() { + if (!getFilterList().isInitialized()) { + getFilterList().addDefaultFilters( + getDocumentSafely().getFilterMap(), + pdfimage.getFilterHint()); + } + super.setupFilterList(); } + }