diff options
author | Mehdi Houshmand <mehdi@apache.org> | 2012-06-26 12:31:50 +0000 |
---|---|---|
committer | Mehdi Houshmand <mehdi@apache.org> | 2012-06-26 12:31:50 +0000 |
commit | c9f6e74ddf229f879ea7df1b389d6018286ca1aa (patch) | |
tree | ad95d6d23e332d8320964a843c366b58c1b455bd /src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java | |
parent | 13a68b516babeccef8772be65b83b3de35e854e7 (diff) | |
download | xmlgraphics-fop-c9f6e74ddf229f879ea7df1b389d6018286ca1aa.tar.gz xmlgraphics-fop-c9f6e74ddf229f879ea7df1b389d6018286ca1aa.zip |
Improved the handling of defaults for PDF config and general clean up
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_URI_Unification@1353992 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java')
-rw-r--r-- | src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java | 291 |
1 files changed, 111 insertions, 180 deletions
diff --git a/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java b/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java index 2a6a364e6..fba9d922f 100644 --- a/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java +++ b/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; +import java.util.EnumMap; import java.util.Map; import org.apache.commons.io.IOUtils; @@ -66,21 +67,18 @@ import org.apache.fop.pdf.Version; import org.apache.fop.pdf.VersionController; import org.apache.fop.render.pdf.extensions.PDFEmbeddedFileExtensionAttachment; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.DISABLE_SRGB_COLORSPACE; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.ENCRYPTION_PARAMS; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_ACCESSCONTENT; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_ANNOTATIONS; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_ASSEMBLEDOC; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_COPY_CONTENT; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_EDIT_CONTENT; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_FILLINFORMS; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_PRINT; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.NO_PRINTHQ; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.OUTPUT_PROFILE; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.OWNER_PASSWORD; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.PDF_A_MODE; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.PDF_X_MODE; -import static org.apache.fop.render.pdf.PDFRendererConfigOption.USER_PASSWORD; +import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_PARAMS; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ACCESSCONTENT; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ANNOTATIONS; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ASSEMBLEDOC; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_COPY_CONTENT; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_EDIT_CONTENT; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_FILLINFORMS; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_PRINT; +import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_PRINTHQ; +import static org.apache.fop.render.pdf.PDFEncryptionOption.OWNER_PASSWORD; +import static org.apache.fop.render.pdf.PDFEncryptionOption.USER_PASSWORD; + /** * Utility class which enables all sorts of features that are not directly connected to the @@ -96,168 +94,40 @@ class PDFRenderingUtil { /** the PDF Document being created */ private PDFDocument pdfDoc; - /** the PDF/A mode (Default: disabled) */ - private PDFAMode pdfAMode = (PDFAMode) PDFRendererConfigOption.PDF_A_MODE.getDefaultValue(); - - /** the PDF/X mode (Default: disabled) */ - private PDFXMode pdfXMode = (PDFXMode) PDFRendererConfigOption.PDF_X_MODE.getDefaultValue(); - - /** the (optional) encryption parameters */ - private PDFEncryptionParams encryptionParams; - - /** Registry of PDF filters */ - private Map filterMap; + private PDFRendererOptionsConfig rendererConfig; /** the ICC stream used as output profile by this document for PDF/A and PDF/X functionality. */ private PDFICCStream outputProfile; + /** the default sRGB color space. */ private PDFICCBasedColorSpace sRGBColorSpace; - /** controls whether the sRGB color space should be installed */ - private boolean disableSRGBColorSpace = false; - - /** Optional URI to an output profile to be used. */ - private URI outputProfileURI; - - private Version maxPDFVersion; - PDFRenderingUtil(FOUserAgent userAgent) { this.userAgent = userAgent; initialize(); } - private static boolean booleanValueOf(Object obj) { - if (obj instanceof Boolean) { - return ((Boolean)obj).booleanValue(); - } else if (obj instanceof String) { - return Boolean.valueOf((String)obj).booleanValue(); - } else { - throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected."); - } - } - private void initialize() { - PDFEncryptionParams params - = (PDFEncryptionParams) userAgent.getRendererOption(ENCRYPTION_PARAMS); - if (params != null) { - this.encryptionParams = params; //overwrite if available - } - String userPassword = (String) userAgent.getRendererOption(USER_PASSWORD); - if (userPassword != null) { - getEncryptionParams().setUserPassword(userPassword); - } - String ownerPassword = (String) userAgent.getRendererOption(OWNER_PASSWORD); - if (ownerPassword != null) { - getEncryptionParams().setOwnerPassword(ownerPassword); - } - Object noPrint = userAgent.getRendererOption(NO_PRINT); - if (noPrint != null) { - getEncryptionParams().setAllowPrint(!booleanValueOf(noPrint)); - } - Object noCopyContent = userAgent.getRendererOption(NO_COPY_CONTENT); - if (noCopyContent != null) { - getEncryptionParams().setAllowCopyContent(!booleanValueOf(noCopyContent)); - } - Object noEditContent = userAgent.getRendererOption(NO_EDIT_CONTENT); - if (noEditContent != null) { - getEncryptionParams().setAllowEditContent(!booleanValueOf(noEditContent)); - } - Object noAnnotations = userAgent.getRendererOption(NO_ANNOTATIONS); - if (noAnnotations != null) { - getEncryptionParams().setAllowEditAnnotations(!booleanValueOf(noAnnotations)); - } - Object noFillInForms = userAgent.getRendererOption(NO_FILLINFORMS); - if (noFillInForms != null) { - getEncryptionParams().setAllowFillInForms(!booleanValueOf(noFillInForms)); - } - Object noAccessContent = userAgent.getRendererOption(NO_ACCESSCONTENT); - if (noAccessContent != null) { - getEncryptionParams().setAllowAccessContent(!booleanValueOf(noAccessContent)); - } - Object noAssembleDoc = userAgent.getRendererOption(NO_ASSEMBLEDOC); - if (noAssembleDoc != null) { - getEncryptionParams().setAllowAssembleDocument(!booleanValueOf(noAssembleDoc)); - } - Object noPrintHQ = userAgent.getRendererOption(NO_PRINTHQ); - if (noPrintHQ != null) { - getEncryptionParams().setAllowPrintHq(!booleanValueOf(noPrintHQ)); - } - String s = (String) userAgent.getRendererOption(PDF_A_MODE); - if (s != null) { - this.pdfAMode = PDFAMode.getValueOf(s); - } - if (this.pdfAMode.isPDFA1LevelA()) { + rendererConfig = PDFRendererOptionsConfig.DEFAULT.merge(createFromUserAgent(userAgent)); + if (rendererConfig.getPDFAMode().isPDFA1LevelA()) { //Enable accessibility if PDF/A-1a is enabled because it requires tagged PDF. userAgent.getRendererOptions().put(Accessibility.ACCESSIBILITY, Boolean.TRUE); } - s = (String) userAgent.getRendererOption(PDF_X_MODE); - if (s != null) { - this.pdfXMode = PDFXMode.getValueOf(s); - } - s = (String) userAgent.getRendererOption(OUTPUT_PROFILE); - if (s != null) { - this.outputProfileURI = URI.create(s); - } - Object disableSRGBColorSpace = userAgent.getRendererOption(DISABLE_SRGB_COLORSPACE); - if (disableSRGBColorSpace != null) { - this.disableSRGBColorSpace = booleanValueOf(disableSRGBColorSpace); - } - } - - public FOUserAgent getUserAgent() { - return this.userAgent; - } - - /** - * Sets the PDF/A mode for the PDF renderer. - * @param mode the PDF/A mode - */ - public void setAMode(PDFAMode mode) { - this.pdfAMode = mode; - } - - /** - * Sets the PDF/X mode for the PDF renderer. - * @param mode the PDF/X mode - */ - public void setXMode(PDFXMode mode) { - this.pdfXMode = mode; } - /** - * Sets the output color profile for the PDF renderer. - * @param outputProfileURI the URI to the output color profile - */ - public void setOutputProfileURI(URI outputProfileURI) { - this.outputProfileURI = outputProfileURI; - } - - /** - * Enables or disables the default sRGB color space needed for the PDF document to preserve - * the sRGB colors used in XSL-FO. - * @param disable true to disable, false to enable - */ - public void setDisableSRGBColorSpace(boolean disable) { - this.disableSRGBColorSpace = disable; - } - - /** - * Sets the filter map to be used by the PDF renderer. - * @param filterMap the filter map - */ - public void setFilterMap(Map filterMap) { - this.filterMap = filterMap; + private static PDFRendererOptionsConfig createFromUserAgent(FOUserAgent userAgent) { + Map<PDFRendererOption, Object> properties + = new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); + for (PDFRendererOption option : PDFRendererOption.values()) { + Object value = userAgent.getRendererOption(option); + properties.put(option, option.parse(value)); + } + PDFEncryptionParams encryptionConfig = new EncryptionParamsBuilder().createParams(userAgent); + return new PDFRendererOptionsConfig(properties, encryptionConfig); } - /** - * Gets the encryption parameters used by the PDF renderer. - * @return encryptionParams the encryption parameters - */ - PDFEncryptionParams getEncryptionParams() { - if (this.encryptionParams == null) { - this.encryptionParams = new PDFEncryptionParams(); - } - return this.encryptionParams; + void mergeRendererOptionsConfig(PDFRendererOptionsConfig config) { + rendererConfig = rendererConfig.merge(config); } private void updateInfo() { @@ -271,15 +141,15 @@ class PDFRenderingUtil { } private void updatePDFProfiles() { - pdfDoc.getProfile().setPDFAMode(this.pdfAMode); - pdfDoc.getProfile().setPDFXMode(this.pdfXMode); + pdfDoc.getProfile().setPDFAMode(rendererConfig.getPDFAMode()); + pdfDoc.getProfile().setPDFXMode(rendererConfig.getPDFXMode()); } private void addsRGBColorSpace() throws IOException { - if (disableSRGBColorSpace) { - if (this.pdfAMode != PDFAMode.DISABLED - || this.pdfXMode != PDFXMode.DISABLED - || this.outputProfileURI != null) { + if (rendererConfig.getDisableSRGBColorSpace()) { + if (rendererConfig.getPDFAMode() != PDFAMode.DISABLED + || rendererConfig.getPDFXMode() != PDFXMode.DISABLED + || rendererConfig.getOutputProfileURI() != null) { throw new IllegalStateException("It is not possible to disable the sRGB color" + " space if PDF/A or PDF/X functionality is enabled or an" + " output profile is set!"); @@ -299,9 +169,10 @@ class PDFRenderingUtil { } ICC_Profile profile; InputStream in = null; - if (this.outputProfileURI != null) { + URI outputProfileUri = rendererConfig.getOutputProfileURI(); + if (outputProfileUri != null) { this.outputProfile = pdfDoc.getFactory().makePDFICCStream(); - in = getUserAgent().getResourceResolver().getResource(outputProfileURI); + in = userAgent.getResourceResolver().getResource(rendererConfig.getOutputProfileURI()); try { profile = ColorProfileUtil.getICC_Profile(in); } finally { @@ -386,7 +257,7 @@ class PDFRenderingUtil { } String producer = userAgent.getProducer() != null ? userAgent.getProducer() : ""; - + final Version maxPDFVersion = rendererConfig.getPDFVersion(); if (maxPDFVersion == null) { this.pdfDoc = new PDFDocument(producer); } else { @@ -396,22 +267,24 @@ class PDFRenderingUtil { } updateInfo(); updatePDFProfiles(); - pdfDoc.setFilterMap(filterMap); + pdfDoc.setFilterMap(rendererConfig.getFilterMap()); pdfDoc.outputHeader(out); //Setup encryption if necessary - PDFEncryptionManager.setupPDFEncryption(encryptionParams, pdfDoc); + PDFEncryptionManager.setupPDFEncryption(rendererConfig.getEncryptionParameters(), pdfDoc); addsRGBColorSpace(); - if (this.outputProfileURI != null) { + if (rendererConfig.getOutputProfileURI() != null) { addDefaultOutputProfile(); } + PDFXMode pdfXMode = rendererConfig.getPDFXMode(); if (pdfXMode != PDFXMode.DISABLED) { log.debug(pdfXMode + " is active."); log.warn("Note: " + pdfXMode + " support is work-in-progress and not fully implemented, yet!"); addPDFXOutputIntent(); } + PDFAMode pdfAMode = rendererConfig.getPDFAMode(); if (pdfAMode.isPDFA1LevelB()) { log.debug("PDF/A is active. Conformance Level: " + pdfAMode); addPDFA1OutputIntent(); @@ -467,7 +340,7 @@ class PDFRenderingUtil { } catch (URISyntaxException use) { throw new RuntimeException(use); } - InputStream in = getUserAgent().getResourceResolver().getResource(srcURI); + InputStream in = userAgent.getResourceResolver().getResource(srcURI); if (in == null) { throw new FileNotFoundException(embeddedFile.getSrc()); } @@ -507,14 +380,72 @@ class PDFRenderingUtil { nameArray.add(new PDFReference(fileSpec)); } - /** - * Sets the PDF version of the output document. See {@link Version} for the format of - * <code>version</code>. - * @param version the PDF version - * @throws IllegalArgumentException if the format of version doesn't conform to that specified - * by {@link Version} - */ - public void setPDFVersion(Version version) { - maxPDFVersion = version; + private static final class EncryptionParamsBuilder { + private PDFEncryptionParams params; + + private EncryptionParamsBuilder() { + } + + private PDFEncryptionParams createParams(FOUserAgent userAgent) { + params = (PDFEncryptionParams) userAgent.getRendererOptions().get(ENCRYPTION_PARAMS); + String userPassword = (String) userAgent.getRendererOption(USER_PASSWORD); + if (userPassword != null) { + getEncryptionParams().setUserPassword(userPassword); + } + String ownerPassword = (String) userAgent.getRendererOption(OWNER_PASSWORD); + if (ownerPassword != null) { + getEncryptionParams().setOwnerPassword(ownerPassword); + } + Object noPrint = userAgent.getRendererOption(NO_PRINT); + if (noPrint != null) { + getEncryptionParams().setAllowPrint(!booleanValueOf(noPrint)); + } + Object noCopyContent = userAgent.getRendererOption(NO_COPY_CONTENT); + if (noCopyContent != null) { + getEncryptionParams().setAllowCopyContent(!booleanValueOf(noCopyContent)); + } + Object noEditContent = userAgent.getRendererOption(NO_EDIT_CONTENT); + if (noEditContent != null) { + getEncryptionParams().setAllowEditContent(!booleanValueOf(noEditContent)); + } + Object noAnnotations = userAgent.getRendererOption(NO_ANNOTATIONS); + if (noAnnotations != null) { + getEncryptionParams().setAllowEditAnnotations(!booleanValueOf(noAnnotations)); + } + Object noFillInForms = userAgent.getRendererOption(NO_FILLINFORMS); + if (noFillInForms != null) { + getEncryptionParams().setAllowFillInForms(!booleanValueOf(noFillInForms)); + } + Object noAccessContent = userAgent.getRendererOption(NO_ACCESSCONTENT); + if (noAccessContent != null) { + getEncryptionParams().setAllowAccessContent(!booleanValueOf(noAccessContent)); + } + Object noAssembleDoc = userAgent.getRendererOption(NO_ASSEMBLEDOC); + if (noAssembleDoc != null) { + getEncryptionParams().setAllowAssembleDocument(!booleanValueOf(noAssembleDoc)); + } + Object noPrintHQ = userAgent.getRendererOption(NO_PRINTHQ); + if (noPrintHQ != null) { + getEncryptionParams().setAllowPrintHq(!booleanValueOf(noPrintHQ)); + } + return params; + } + + private PDFEncryptionParams getEncryptionParams() { + if (params == null) { + params = new PDFEncryptionParams(); + } + return params; + } + + private static boolean booleanValueOf(Object obj) { + if (obj instanceof Boolean) { + return ((Boolean)obj).booleanValue(); + } else if (obj instanceof String) { + return Boolean.valueOf((String)obj).booleanValue(); + } else { + throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected."); + } + } } } |