From: Jeremias Maerki Date: Sat, 15 Mar 2003 16:54:26 +0000 (+0000) Subject: Make PDFDocument as top-level class of the PDF library LogEnabled. (Reason: It access... X-Git-Tag: Alt-Design-integration-base~10 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ccf6170aa09596488e80e60391c26ef09c58552b;p=xmlgraphics-fop.git Make PDFDocument as top-level class of the PDF library LogEnabled. (Reason: It accesses TTFFile which is LogEnabled, so there must be some way to pass a logger. This will be revisited when Avalonization is underway.) Proper resolving of LazyFonts. Add helper method to apply encryption if it's enabled. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196101 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/pdf/PDFDocument.java b/src/java/org/apache/fop/pdf/PDFDocument.java index 26b356c23..7feb2141e 100644 --- a/src/java/org/apache/fop/pdf/PDFDocument.java +++ b/src/java/org/apache/fop/pdf/PDFDocument.java @@ -52,8 +52,10 @@ package org.apache.fop.pdf; import org.apache.fop.util.StreamUtilities; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.fop.fonts.CIDFont; import org.apache.fop.fonts.CustomFont; +import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontDescriptor; import org.apache.fop.fonts.FontMetrics; import org.apache.fop.fonts.FontType; @@ -97,7 +99,8 @@ import java.awt.geom.Rectangle2D; * the object list; enhanced trailer output; cleanups. * */ -public class PDFDocument { +public class PDFDocument extends AbstractLogEnabled { + private static final Integer LOCATION_PLACEHOLDER = new Integer(0); /** * the version of PDF supported which is 1.4 @@ -303,6 +306,16 @@ public class PDFDocument { return filterMap; } + /** + * Apply the encryption filter to a PDFStream if encryption is enabled. + * @param stream PDFStream to encrypt + */ + public void applyEncryption(PDFStream stream) { + if (isEncryptionActive()) { + this.encryption.applyFilter(stream); + } + } + /** * Enables PDF encryption. * @param params The encryption parameters for the pdf file @@ -313,7 +326,7 @@ public class PDFDocument { /**@todo this cast is ugly. PDFObject should be transformed to an interface. */ addTrailerObject((PDFObject)this.encryption); } else { - System.out.println("PDF encryption is unavailable. PDF will be " + getLogger().warn("PDF encryption is unavailable. PDF will be " + "generated without encryption."); } } @@ -1308,13 +1321,19 @@ public class PDFDocument { throw new IllegalArgumentException("Trying to embed unsupported font type: " + desc.getFontType()); } - if (!(desc instanceof CustomFont)) { + + Font tempFont; + if (desc instanceof LazyFont) { + tempFont = ((LazyFont)desc).getRealFont(); + } else { + tempFont = (Font)desc; + } + if (!(tempFont instanceof CustomFont)) { throw new IllegalArgumentException( "FontDescriptor must be instance of CustomFont, but is a " + desc.getClass().getName()); } - - CustomFont font = (CustomFont)desc; + CustomFont font = (CustomFont)tempFont; InputStream in = null; try { @@ -1323,8 +1342,9 @@ public class PDFDocument { try { in = resolveURI(font.getEmbedFileName()); } catch (Exception e) { - System.out.println("Failed to embed fontfile: " - + font.getEmbedFileName()); + getLogger().error("Failed to embed fontfile: " + + font.getEmbedFileName() + + "(" + e.getMessage() + ")"); } } @@ -1334,8 +1354,9 @@ public class PDFDocument { in = new java.io.BufferedInputStream( this.getClass().getResourceAsStream(font.getEmbedResourceName())); } catch (Exception e) { - System.out.println("Failed to embed fontresource: " - + font.getEmbedResourceName()); + getLogger().error("Failed to embed fontresource: " + + font.getEmbedResourceName() + + "(" + e.getMessage() + ")"); } } @@ -1349,6 +1370,7 @@ public class PDFDocument { FontFileReader reader = new FontFileReader(in); TTFSubSetFile subset = new TTFSubSetFile(); + setupLogger(subset); byte[] subsetFont = subset.readFont(reader, mbfont.getTTCName(), mbfont.getUsedGlyphs()); @@ -1367,7 +1389,12 @@ public class PDFDocument { ((PDFTTFStream)embeddedFont).setData(file, file.length); } embeddedFont.addFilter("flate"); - embeddedFont.addFilter("ascii-85"); + if (isEncryptionActive()) { + this.encryption.applyFilter(embeddedFont); + } else { + embeddedFont.addFilter("ascii-85"); + } + return embeddedFont; } finally { in.close();