git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_URI_Unification@1353992 13f79535-47bb-0310-9956-ffa450edef68tags/fop-2_0
throws UnsupportedEncodingException { | throws UnsupportedEncodingException { | ||||
//The first 8 bytes of the field data represent the resource name | //The first 8 bytes of the field data represent the resource name | ||||
byte[] nameBytes = new byte[8]; | byte[] nameBytes = new byte[8]; | ||||
byte[] fieldData = field.getData(); | |||||
if (fieldData.length < 8) { | |||||
throw new IllegalArgumentException("Field data does not contain a resource name"); | |||||
} | |||||
System.arraycopy(fieldData, 0, nameBytes, 0, 8); | |||||
return new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); | |||||
System.arraycopy(field.getData(), 0, nameBytes, 0, 8); | |||||
String asciiName; | |||||
asciiName = new String(nameBytes, AFPConstants.EBCIDIC_ENCODING); | |||||
return asciiName; | |||||
} | } | ||||
/** | /** | ||||
public static void copyNamedResource(String name, | public static void copyNamedResource(String name, | ||||
final InputStream in, final OutputStream out) throws IOException { | final InputStream in, final OutputStream out) throws IOException { | ||||
final MODCAParser parser = new MODCAParser(in); | final MODCAParser parser = new MODCAParser(in); | ||||
Collection<String> resourceNames = new java.util.HashSet<String>(); | |||||
Collection resourceNames = new java.util.HashSet(); | |||||
//Find matching "Begin" field | //Find matching "Begin" field | ||||
final UnparsedStructuredField fieldBegin; | final UnparsedStructuredField fieldBegin; | ||||
while (true) { | while (true) { | ||||
final UnparsedStructuredField field = parser.readNextStructuredField(); | |||||
UnparsedStructuredField field = parser.readNextStructuredField(); | |||||
if (field == null) { | if (field == null) { | ||||
throw new IOException("Requested resource '" + name | throw new IOException("Requested resource '" + name | ||||
+ "' not found. Encountered resource names: " + resourceNames); | + "' not found. Encountered resource names: " + resourceNames); | ||||
if (field.getSfTypeCode() != TYPE_CODE_BEGIN) { //0xA8=Begin | if (field.getSfTypeCode() != TYPE_CODE_BEGIN) { //0xA8=Begin | ||||
continue; //Not a "Begin" field | continue; //Not a "Begin" field | ||||
} | } | ||||
final String resourceName = getResourceName(field); | |||||
String resourceName = getResourceName(field); | |||||
resourceNames.add(resourceName); | resourceNames.add(resourceName); | ||||
if (resourceName.equals(name)) { | if (resourceName.equals(name)) { | ||||
if (LOG.isDebugEnabled()) { | if (LOG.isDebugEnabled()) { | ||||
LOG.debug("Start of requested structured field found:\n" | LOG.debug("Start of requested structured field found:\n" | ||||
if (wrapInResource) { | if (wrapInResource) { | ||||
ResourceObject resourceObject = new ResourceObject(name) { | ResourceObject resourceObject = new ResourceObject(name) { | ||||
protected void writeContent(OutputStream os) throws IOException { | protected void writeContent(OutputStream os) throws IOException { | ||||
copyNamedStructuredFields(name, fieldBegin, parser, out); | |||||
copyStructuredFields(name, fieldBegin, parser, out); | |||||
} | } | ||||
}; | }; | ||||
resourceObject.setType(ResourceObject.TYPE_PAGE_SEGMENT); | resourceObject.setType(ResourceObject.TYPE_PAGE_SEGMENT); | ||||
resourceObject.writeToStream(out); | resourceObject.writeToStream(out); | ||||
} else { | } else { | ||||
copyNamedStructuredFields(name, fieldBegin, parser, out); | |||||
copyStructuredFields(name, fieldBegin, parser, out); | |||||
} | } | ||||
} | } | ||||
private static void copyNamedStructuredFields(final String name, | |||||
UnparsedStructuredField fieldBegin, MODCAParser parser, | |||||
OutputStream out) throws IOException { | |||||
private static void copyStructuredFields(String name, UnparsedStructuredField fieldBegin, | |||||
MODCAParser parser, OutputStream out) throws IOException { | |||||
boolean inRequestedResource; | |||||
UnparsedStructuredField field = fieldBegin; | |||||
//The "Begin" field first | |||||
out.write(MODCAParser.CARRIAGE_CONTROL_CHAR); | |||||
fieldBegin.writeTo(out); | |||||
UnparsedStructuredField field; | |||||
while (true) { | |||||
//Then the rest of the fields until the corresponding "End" field | |||||
inRequestedResource = true; | |||||
do { | |||||
field = parser.readNextStructuredField(); | |||||
if (field == null) { | if (field == null) { | ||||
throw new IOException("Ending structured field not found for resource " + name); | |||||
break; //Unexpected EOF | |||||
} | } | ||||
out.write(MODCAParser.CARRIAGE_CONTROL_CHAR); | |||||
field.writeTo(out); | |||||
if (field.getSfTypeCode() == TYPE_CODE_END | |||||
&& fieldBegin.getSfCategoryCode() == field.getSfCategoryCode() | |||||
&& name.equals(getResourceName(field))) { | |||||
break; | |||||
if (field.getSfTypeCode() == TYPE_CODE_END) { | |||||
String resourceName = getResourceName(field); | |||||
if (resourceName.equals(name)) { | |||||
inRequestedResource = false; //Signal end of loop | |||||
} | |||||
} | } | ||||
field = parser.readNextStructuredField(); | |||||
out.write(MODCAParser.CARRIAGE_CONTROL_CHAR); | |||||
field.writeTo(out); | |||||
} while (inRequestedResource); | |||||
if (inRequestedResource) { | |||||
throw new IOException("Ending structured field not found for resource " + name); | |||||
} | } | ||||
} | } | ||||
} | } |
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
/** | |||||
* Implementations of this interface resolve URIs for temporary files used by FOP. The temporary- | |||||
* resource URI scheme comes from {@link TempResourceURIGenerator#TMP_SCHEMA}. | |||||
*/ | |||||
public interface TempResourceResolver { | public interface TempResourceResolver { | ||||
/** | |||||
* Get a temporary-resource given the URI pointing to said resource. | |||||
* | |||||
* @param uri the resource URI | |||||
* @return the resource | |||||
* @throws IOException if an I/O error occured during resource acquisition | |||||
*/ | |||||
Resource getResource(String id) throws IOException; | Resource getResource(String id) throws IOException; | ||||
/** | |||||
* Gets an temporary-output stream of a given URI. | |||||
* | |||||
* @param uri the output stream URI | |||||
* @return the output stream | |||||
* @throws IOException if an I/O error occured while creating an output stream | |||||
*/ | |||||
OutputStream getOutputStream(String id) throws IOException; | OutputStream getOutputStream(String id) throws IOException; | ||||
} | } |
*/ | */ | ||||
public final class TempResourceURIGenerator { | public final class TempResourceURIGenerator { | ||||
private static final String TMP_SCHEMA = "tmp"; | |||||
public static final String TMP_SCHEMA = "tmp"; | |||||
private final String tempURIPrefix; | private final String tempURIPrefix; | ||||
import org.apache.fop.render.intermediate.IFContext; | import org.apache.fop.render.intermediate.IFContext; | ||||
import org.apache.fop.render.intermediate.IFDocumentHandler; | import org.apache.fop.render.intermediate.IFDocumentHandler; | ||||
import org.apache.fop.render.intermediate.IFSerializer; | import org.apache.fop.render.intermediate.IFSerializer; | ||||
import org.apache.fop.render.pdf.PDFEncryptionOption; | |||||
import org.apache.fop.render.print.PagesMode; | import org.apache.fop.render.print.PagesMode; | ||||
import org.apache.fop.render.print.PrintRenderer; | import org.apache.fop.render.print.PrintRenderer; | ||||
import org.apache.fop.render.xml.XMLRenderer; | import org.apache.fop.render.xml.XMLRenderer; | ||||
import org.apache.fop.util.CommandLineLogger; | import org.apache.fop.util.CommandLineLogger; | ||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.ENCRYPTION_PARAMS; | |||||
/** | /** | ||||
* Options parses the commandline arguments | * Options parses the commandline arguments | ||||
*/ | */ | ||||
} | } | ||||
private PDFEncryptionParams getPDFEncryptionParams() throws FOPException { | private PDFEncryptionParams getPDFEncryptionParams() throws FOPException { | ||||
PDFEncryptionParams params = (PDFEncryptionParams) renderingOptions.get(ENCRYPTION_PARAMS); | |||||
PDFEncryptionParams params = (PDFEncryptionParams) renderingOptions.get(PDFEncryptionOption.ENCRYPTION_PARAMS); | |||||
if (params == null) { | if (params == null) { | ||||
if (!PDFEncryptionManager.checkAvailableAlgorithms()) { | if (!PDFEncryptionManager.checkAvailableAlgorithms()) { | ||||
throw new FOPException("PDF encryption requested but it is not available." | throw new FOPException("PDF encryption requested but it is not available." | ||||
+ " Please make sure MD5 and RC4 algorithms are available."); | + " Please make sure MD5 and RC4 algorithms are available."); | ||||
} | } | ||||
params = new PDFEncryptionParams(); | params = new PDFEncryptionParams(); | ||||
renderingOptions.put(ENCRYPTION_PARAMS, params); | |||||
renderingOptions.put(PDFEncryptionOption.ENCRYPTION_PARAMS, params); | |||||
} | } | ||||
return params; | return params; | ||||
} | } |
private boolean useKerning = true; | private boolean useKerning = true; | ||||
private boolean useAdvanced = true; | private boolean useAdvanced = true; | ||||
/** the character map, mapping Unicode ranges to glyph indices. */ | |||||
protected CMapSegment[] cmap; | |||||
/** | /** | ||||
* @param resourceResolver the URI resource resolver for controlling file access | * @param resourceResolver the URI resource resolver for controlling file access | ||||
public CustomFont(InternalResourceResolver resourceResolver) { | public CustomFont(InternalResourceResolver resourceResolver) { | ||||
this.resourceResolver = resourceResolver; | this.resourceResolver = resourceResolver; | ||||
} | } | ||||
/** the character map, mapping Unicode ranges to glyph indices. */ | |||||
protected CMapSegment[] cmap; | |||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
public String getFontName() { | public String getFontName() { | ||||
} | } | ||||
/** | /** | ||||
* Returns the embedding mode for this font. | * Returns the embedding mode for this font. | ||||
* @return embedding mode | * @return embedding mode | ||||
*/ | */ |
fontCfg.getAttribute("sub-font", null), fontCfg.getAttributeAsBoolean( | fontCfg.getAttribute("sub-font", null), fontCfg.getAttributeAsBoolean( | ||||
"kerning", true), fontCfg.getAttributeAsBoolean("advanced", true), | "kerning", true), fontCfg.getAttributeAsBoolean("advanced", true), | ||||
fontCfg.getAttribute("encoding-mode", EncodingMode.AUTO.getName()), | fontCfg.getAttribute("encoding-mode", EncodingMode.AUTO.getName()), | ||||
fontCfg.getAttribute("embedding-mode", EmbeddingMode.AUTO.getName())); | |||||
fontCfg.getAttribute("embedding-mode", EncodingMode.AUTO.getName())); | |||||
instance.fonts.add(font); | instance.fonts.add(font); | ||||
boolean hasTriplets = false; | boolean hasTriplets = false; | ||||
for (Configuration tripletCfg : fontCfg.getChildren("font-triplet")) { | for (Configuration tripletCfg : fontCfg.getChildren("font-triplet")) { | ||||
private final String embeddingMode; | private final String embeddingMode; | ||||
public String getEncodingMode() { | |||||
return encodingMode; | |||||
} | |||||
private final List<FontTriplet> tripletList = new ArrayList<FontTriplet>(); | private final List<FontTriplet> tripletList = new ArrayList<FontTriplet>(); | ||||
public List<FontTriplet> getTripletList() { | public List<FontTriplet> getTripletList() { | ||||
return subFont; | return subFont; | ||||
} | } | ||||
public String getEncodingMode() { | |||||
return encodingMode; | |||||
} | |||||
public String getEmbeddingMode() { | public String getEmbeddingMode() { | ||||
return embeddingMode; | return embeddingMode; | ||||
} | } |
protected final boolean advanced; | protected final boolean advanced; | ||||
/** the requested encoding mode for the font */ | /** the requested encoding mode for the font */ | ||||
private final EncodingMode encodingMode; | private final EncodingMode encodingMode; | ||||
/** the requested embedding mode for this font */ | |||||
private final EmbeddingMode embeddingMode; | private final EmbeddingMode embeddingMode; | ||||
/** the PostScript name of the font */ | /** the PostScript name of the font */ | ||||
* @param embedURI Path to the embeddable font file (may be null) | * @param embedURI Path to the embeddable font file (may be null) | ||||
* @param subFontName the sub-fontname used for TrueType Collections (null otherwise) | * @param subFontName the sub-fontname used for TrueType Collections (null otherwise) | ||||
* @param encodingMode the encoding mode to use for this font | * @param encodingMode the encoding mode to use for this font | ||||
* @param embeddingMode the embedding mode for this font | |||||
*/ | */ | ||||
public EmbedFontInfo(URI metricsURI, boolean kerning, boolean advanced, | public EmbedFontInfo(URI metricsURI, boolean kerning, boolean advanced, | ||||
List<FontTriplet> fontTriplets, URI embedURI, String subFontName, | List<FontTriplet> fontTriplets, URI embedURI, String subFontName, |
} | } | ||||
} | } | ||||
/** | |||||
* Detect fonts from the operating system via FOPs autodetect mechanism. | |||||
* | |||||
* @param autoDetectFonts if autodetect has been enabled | |||||
* @param fontAdder the font adding mechanism | |||||
* @param strict whether to enforce strict validation | |||||
* @param listener the listener for font related events | |||||
* @param fontInfoList a list of font info objects | |||||
* @throws FOPException if an exception was thrown auto-detecting fonts | |||||
*/ | |||||
public void autoDetectFonts(boolean autoDetectFonts, FontAdder fontAdder, boolean strict, | public void autoDetectFonts(boolean autoDetectFonts, FontAdder fontAdder, boolean strict, | ||||
FontEventListener listener, List<EmbedFontInfo> fontInfoList) throws FOPException { | FontEventListener listener, List<EmbedFontInfo> fontInfoList) throws FOPException { | ||||
if (autoDetectFonts) { | if (autoDetectFonts) { |
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
@Override | |||||
public void startElement(String uri, String localName, String qName, Attributes attributes) | public void startElement(String uri, String localName, String qName, Attributes attributes) | ||||
throws SAXException { | throws SAXException { | ||||
if (localName.equals("font-metrics")) { | if (localName.equals("font-metrics")) { |
private final boolean useKerning; | private final boolean useKerning; | ||||
private final boolean useAdvanced; | private final boolean useAdvanced; | ||||
private final EncodingMode encodingMode; | private final EncodingMode encodingMode; | ||||
private final EmbeddingMode embeddingMode; | |||||
private final boolean embedded; | |||||
private EmbeddingMode embeddingMode; | |||||
private final String subFontName; | private final String subFontName; | ||||
private final boolean embedded; | |||||
private final InternalResourceResolver resourceResolver; | private final InternalResourceResolver resourceResolver; | ||||
private boolean isMetricsLoaded; | private boolean isMetricsLoaded; | ||||
} else { | } else { | ||||
this.useAdvanced = fontInfo.getAdvanced(); | this.useAdvanced = fontInfo.getAdvanced(); | ||||
} | } | ||||
this.encodingMode = fontInfo.getEncodingMode(); | |||||
this.embeddingMode = fontInfo.getEmbeddingMode(); | |||||
this.encodingMode = fontInfo.getEncodingMode() != null ? fontInfo.getEncodingMode() | |||||
: EncodingMode.AUTO; | |||||
this.embeddingMode = fontInfo.getEmbeddingMode() != null ? fontInfo.getEmbeddingMode() | |||||
: EmbeddingMode.AUTO; | |||||
this.subFontName = fontInfo.getSubFontName(); | this.subFontName = fontInfo.getSubFontName(); | ||||
this.embedded = fontInfo.isEmbedded(); | this.embedded = fontInfo.isEmbedded(); | ||||
this.resourceResolver = resourceResolver; | this.resourceResolver = resourceResolver; |
private int lastUnmapped; | private int lastUnmapped; | ||||
/** | /** | ||||
* @param resourceResolver the resource resolver for accessing the font | |||||
* Default constructor | |||||
*/ | */ | ||||
public MultiByteFont(InternalResourceResolver resourceResolver) { | public MultiByteFont(InternalResourceResolver resourceResolver) { | ||||
super(resourceResolver); | super(resourceResolver); |
} | } | ||||
try { | try { | ||||
TTFFontLoader ttfLoader = new TTFFontLoader(fontURI, fontName, true, | TTFFontLoader ttfLoader = new TTFFontLoader(fontURI, fontName, true, | ||||
EmbeddingMode.AUTO, EncodingMode.AUTO, useKerning, useAdvanced, resourceResolver); | |||||
EmbeddingMode.AUTO, EncodingMode.AUTO, useKerning, useAdvanced, | |||||
resourceResolver); | |||||
customFont = ttfLoader.getFont(); | customFont = ttfLoader.getFont(); | ||||
if (this.eventListener != null) { | if (this.eventListener != null) { | ||||
customFont.setEventListener(this.eventListener); | customFont.setEventListener(this.eventListener); |
final byte buf = read(); | final byte buf = read(); | ||||
if (buf < 0) { | if (buf < 0) { | ||||
return (int)(256 + buf); | |||||
return (256 + buf); | |||||
} else { | } else { | ||||
return (int)buf; | |||||
return buf; | |||||
} | } | ||||
} | } | ||||
*/ | */ | ||||
public final int readTTFUShort() throws IOException { | public final int readTTFUShort() throws IOException { | ||||
final int ret = (readTTFUByte() << 8) + readTTFUByte(); | final int ret = (readTTFUByte() << 8) + readTTFUByte(); | ||||
return (int)ret; | |||||
return ret; | |||||
} | } | ||||
/** | /** |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.fonts.truetype; | |||||
/** | |||||
* The CMap entry contains information of a Unicode range and the | |||||
* the glyph indexes related to the range | |||||
*/ | |||||
public class TTFCmapEntry { | |||||
private int unicodeStart; | |||||
private int unicodeEnd; | |||||
private int glyphStartIndex; | |||||
TTFCmapEntry() { | |||||
unicodeStart = 0; | |||||
unicodeEnd = 0; | |||||
glyphStartIndex = 0; | |||||
} | |||||
TTFCmapEntry(int unicodeStart, int unicodeEnd, int glyphStartIndex) { | |||||
this.unicodeStart = unicodeStart; | |||||
this.unicodeEnd = unicodeEnd; | |||||
this.glyphStartIndex = glyphStartIndex; | |||||
} | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | |||||
public int hashCode() { | |||||
int hc = super.hashCode(); | |||||
hc ^= ( hc * 11 ) + unicodeStart; | |||||
hc ^= ( hc * 19 ) + unicodeEnd; | |||||
hc ^= ( hc * 23 ) + glyphStartIndex; | |||||
return hc; | |||||
} | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | |||||
public boolean equals(Object o) { | |||||
if (o instanceof TTFCmapEntry) { | |||||
TTFCmapEntry ce = (TTFCmapEntry)o; | |||||
if (ce.unicodeStart == this.unicodeStart | |||||
&& ce.unicodeEnd == this.unicodeEnd | |||||
&& ce.glyphStartIndex == this.glyphStartIndex) { | |||||
return true; | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
/** | |||||
* Returns the glyphStartIndex. | |||||
* @return int | |||||
*/ | |||||
public int getGlyphStartIndex() { | |||||
return glyphStartIndex; | |||||
} | |||||
/** | |||||
* Returns the unicodeEnd. | |||||
* @return int | |||||
*/ | |||||
public int getUnicodeEnd() { | |||||
return unicodeEnd; | |||||
} | |||||
/** | |||||
* Returns the unicodeStart. | |||||
* @return int | |||||
*/ | |||||
public int getUnicodeStart() { | |||||
return unicodeStart; | |||||
} | |||||
/** | |||||
* Sets the glyphStartIndex. | |||||
* @param glyphStartIndex The glyphStartIndex to set | |||||
*/ | |||||
public void setGlyphStartIndex(int glyphStartIndex) { | |||||
this.glyphStartIndex = glyphStartIndex; | |||||
} | |||||
/** | |||||
* Sets the unicodeEnd. | |||||
* @param unicodeEnd The unicodeEnd to set | |||||
*/ | |||||
public void setUnicodeEnd(int unicodeEnd) { | |||||
this.unicodeEnd = unicodeEnd; | |||||
} | |||||
/** | |||||
* Sets the unicodeStart. | |||||
* @param unicodeStart The unicodeStart to set | |||||
*/ | |||||
public void setUnicodeStart(int unicodeStart) { | |||||
this.unicodeStart = unicodeStart; | |||||
} | |||||
} |
try { | try { | ||||
boolean useKerning = true; | boolean useKerning = true; | ||||
boolean useAdvanced = true; | boolean useAdvanced = true; | ||||
stream = new FileInputStream(args[0]); | |||||
TTFFile ttfFile = new TTFFile(useKerning, useAdvanced); | TTFFile ttfFile = new TTFFile(useKerning, useAdvanced); | ||||
stream = new FileInputStream(args[0]); | |||||
FontFileReader reader = new FontFileReader(stream); | FontFileReader reader = new FontFileReader(stream); | ||||
String name = null; | String name = null; | ||||
} finally { | } finally { | ||||
IOUtils.closeQuietly(stream); | IOUtils.closeQuietly(stream); | ||||
} | } | ||||
} | } | ||||
} | } |
* @param useAdvanced true to enable loading advanced info if available, false to disable | * @param useAdvanced true to enable loading advanced info if available, false to disable | ||||
* @param resolver the FontResolver for font URI resolution | * @param resolver the FontResolver for font URI resolution | ||||
*/ | */ | ||||
public TTFFontLoader(URI fontFileURI, String subFontName, | |||||
boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode, | |||||
boolean useKerning, boolean useAdvanced, InternalResourceResolver resourceResolver) { | |||||
super(fontFileURI, embedded, useKerning, useAdvanced, resourceResolver); | |||||
public TTFFontLoader(URI fontFileURI, String subFontName, boolean embedded, | |||||
EmbeddingMode embeddingMode, EncodingMode encodingMode, boolean useKerning, | |||||
boolean useAdvanced, InternalResourceResolver resolver) { | |||||
super(fontFileURI, embedded, useKerning, useAdvanced, resolver); | |||||
this.subFontName = subFontName; | this.subFontName = subFontName; | ||||
this.encodingMode = encodingMode; | this.encodingMode = encodingMode; | ||||
this.embeddingMode = embeddingMode; | this.embeddingMode = embeddingMode; |
import java.util.Set; | import java.util.Set; | ||||
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
import org.apache.commons.logging.Log; | |||||
import org.apache.commons.logging.LogFactory; | |||||
import org.apache.fop.apps.io.InternalResourceResolver; | import org.apache.fop.apps.io.InternalResourceResolver; | ||||
import org.apache.fop.fonts.CodePointMapping; | import org.apache.fop.fonts.CodePointMapping; | ||||
*/ | */ | ||||
public class Type1FontLoader extends FontLoader { | public class Type1FontLoader extends FontLoader { | ||||
private static final Log log = LogFactory.getLog(Type1FontLoader.class); | |||||
private SingleByteFont singleFont; | private SingleByteFont singleFont; | ||||
/** | /** |
import org.apache.xmlgraphics.java2d.color.ColorUtil; | import org.apache.xmlgraphics.java2d.color.ColorUtil; | ||||
import org.apache.xmlgraphics.java2d.color.NamedColorSpace; | import org.apache.xmlgraphics.java2d.color.NamedColorSpace; | ||||
import org.apache.xmlgraphics.xmp.Metadata; | import org.apache.xmlgraphics.xmp.Metadata; | ||||
import org.apache.fop.fonts.CIDFont; | import org.apache.fop.fonts.CIDFont; |
this.fontInfoConfigurator = fontInfoConfigurator; | this.fontInfoConfigurator = fontInfoConfigurator; | ||||
} | } | ||||
/** | |||||
* Returns the renderer configuration data for a specific renderer. | |||||
* | |||||
* @param documentHandler the document handler | |||||
* @return the renderer configuration data | |||||
* @throws FOPException if an error occurs | |||||
*/ | |||||
protected RendererConfig getRendererConfig(IFDocumentHandler documentHandler) throws FOPException { | protected RendererConfig getRendererConfig(IFDocumentHandler documentHandler) throws FOPException { | ||||
return getRendererConfig(documentHandler.getMimeType()); | |||||
return getRendererConfig(documentHandler.getMimeType()); | |||||
} | } | ||||
/** | |||||
* gets the renderer configuration data for a specific renderer. | |||||
* | |||||
* @param mimeType the MIME type | |||||
* @return the renderer configuration data | |||||
* @throws FOPException if an error occurs | |||||
*/ | |||||
protected RendererConfig getRendererConfig(String mimeType) throws FOPException { | protected RendererConfig getRendererConfig(String mimeType) throws FOPException { | ||||
return userAgent.getRendererConfig(mimeType, rendererConfigParser); | return userAgent.getRendererConfig(mimeType, rendererConfigParser); | ||||
} | } | ||||
/** | |||||
* gets the renderer configuration data for a specific renderer. | |||||
* | |||||
* @param renderer the renderer | |||||
* @return the renderer configuration data | |||||
* @throws FOPException if an error occurs | |||||
*/ | |||||
protected RendererConfig getRendererConfig(Renderer renderer) throws FOPException { | protected RendererConfig getRendererConfig(Renderer renderer) throws FOPException { | ||||
return getRendererConfig(renderer.getMimeType()); | return getRendererConfig(renderer.getMimeType()); | ||||
} | } | ||||
protected abstract List<FontCollection> getDefaultFontCollection(); | protected abstract List<FontCollection> getDefaultFontCollection(); | ||||
/** | |||||
* Returns the font collection for custom configured fonts. | |||||
* | |||||
* @param resolver the resource resolver | |||||
* @param mimeType the renderer MIME type | |||||
* @return the font collection | |||||
* @throws FOPException if an error occurs | |||||
*/ | |||||
protected FontCollection getCustomFontCollection(InternalResourceResolver resolver, String mimeType) | protected FontCollection getCustomFontCollection(InternalResourceResolver resolver, String mimeType) | ||||
throws FOPException { | throws FOPException { | ||||
List<EmbedFontInfo> fontList; | List<EmbedFontInfo> fontList; | ||||
return createCollectionFromFontList(resolver, fontList); | return createCollectionFromFontList(resolver, fontList); | ||||
} | } | ||||
/*** | |||||
* Creates the font collection given a list of embedded font infomation. | |||||
* | |||||
* @param resolver the resource resolver | |||||
* @param fontList the embedded font infomation | |||||
* @return the font collection | |||||
*/ | |||||
protected FontCollection createCollectionFromFontList(InternalResourceResolver resolver, | protected FontCollection createCollectionFromFontList(InternalResourceResolver resolver, | ||||
List<EmbedFontInfo> fontList) { | List<EmbedFontInfo> fontList) { | ||||
return new CustomFontCollection(resolver, fontList, | return new CustomFontCollection(resolver, fontList, |
import static org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions.MODE_COLOR; | import static org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions.MODE_COLOR; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions.MODE_GRAYSCALE; | import static org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions.MODE_GRAYSCALE; | ||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.DEFAULT_RESOURCE_LEVELS; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.GOCA; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.GOCA_TEXT; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_DITHERING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_FS45; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_JPEG; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_MAPPING_OPTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_MODE; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_NATIVE; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_WRAP_PSEG; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.JPEG_ALLOW_JPEG_EMBEDDING; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.JPEG_BITMAP_ENCODING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.LINE_WIDTH_CORRECTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.RENDERER_RESOLUTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.RESOURCE_GROUP_URI; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.SHADING; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.DEFAULT_RESOURCE_LEVELS; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.GOCA; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.GOCA_TEXT; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_DITHERING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_FS45; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_JPEG; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_MAPPING_OPTION; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_MODE; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_NATIVE; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_WRAP_PSEG; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.JPEG_ALLOW_JPEG_EMBEDDING; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.JPEG_BITMAP_ENCODING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.LINE_WIDTH_CORRECTION; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.RENDERER_RESOLUTION; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.RESOURCE_GROUP_URI; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.SHADING; | |||||
/** | /** | ||||
* The AFP renderer config object. | * The AFP renderer config object. | ||||
} | } | ||||
} | } | ||||
private final EnumMap<AFPRendererConfigOption, Object> params = new EnumMap<AFPRendererConfigOption, Object>(AFPRendererConfigOption.class); | |||||
private final EnumMap<AFPRendererOption, Object> params = new EnumMap<AFPRendererOption, Object>(AFPRendererOption.class); | |||||
private final EnumMap<ImagesModeOptions, Object> imageModeParams | private final EnumMap<ImagesModeOptions, Object> imageModeParams | ||||
= new EnumMap<ImagesModeOptions, Object>(ImagesModeOptions.class); | = new EnumMap<ImagesModeOptions, Object>(ImagesModeOptions.class); | ||||
return getParam(GOCA_TEXT, Boolean.class); | return getParam(GOCA_TEXT, Boolean.class); | ||||
} | } | ||||
private <T> T getParam(AFPRendererConfigOption options, Class<T> type) { | |||||
private <T> T getParam(AFPRendererOption options, Class<T> type) { | |||||
assert options.getType().equals(type); | assert options.getType().equals(type); | ||||
return type.cast(params.get(options)); | return type.cast(params.get(options)); | ||||
} | } | ||||
private <T> void setParam(AFPRendererConfigOption option, T value) { | |||||
private <T> void setParam(AFPRendererOption option, T value) { | |||||
assert option.getType().isInstance(value); | assert option.getType().isInstance(value); | ||||
params.put(option, value); | params.put(option, value); | ||||
} | } | ||||
createResourceLevel(); | createResourceLevel(); | ||||
} | } | ||||
private void setParam(AFPRendererConfigOption option, Object value) { | |||||
private void setParam(AFPRendererOption option, Object value) { | |||||
config.setParam(option, value); | config.setParam(option, value); | ||||
} | } | ||||
config.imageModeParams.put(MODE_GRAYSCALE, | config.imageModeParams.put(MODE_GRAYSCALE, | ||||
imagesCfg.getAttributeAsInteger(imagesMode.getModeAttribute(), 8)); | imagesCfg.getAttributeAsInteger(imagesMode.getModeAttribute(), 8)); | ||||
} | } | ||||
String dithering = imagesCfg.getAttribute(AFPRendererConfigOption.IMAGES_DITHERING_QUALITY.getName(), "medium"); | |||||
String dithering = imagesCfg.getAttribute(AFPRendererOption.IMAGES_DITHERING_QUALITY.getName(), "medium"); | |||||
float dq; | float dq; | ||||
if (dithering.startsWith("min")) { | if (dithering.startsWith("min")) { | ||||
dq = 0.0f; | dq = 0.0f; |
/** | /** | ||||
* An enumeration of the configuration options available for the AFP renderer. | * An enumeration of the configuration options available for the AFP renderer. | ||||
*/ | */ | ||||
public enum AFPRendererConfigOption implements RendererConfigOption { | |||||
public enum AFPRendererOption implements RendererConfigOption { | |||||
DEFAULT_RESOURCE_LEVELS("default-resource-levels", AFPResourceLevelDefaults.class), | DEFAULT_RESOURCE_LEVELS("default-resource-levels", AFPResourceLevelDefaults.class), | ||||
IMAGES("images", null), | IMAGES("images", null), | ||||
IMAGES_JPEG("jpeg", null), | IMAGES_JPEG("jpeg", null), | ||||
private final Class<?> type; | private final Class<?> type; | ||||
private AFPRendererConfigOption(String name, Class<?> type) { | |||||
private AFPRendererOption(String name, Class<?> type) { | |||||
this.name = name; | this.name = name; | ||||
this.type = type; | this.type = type; | ||||
} | } |
import org.apache.fop.render.java2d.Java2DRendererConfig.Java2DRendererConfigParser; | import org.apache.fop.render.java2d.Java2DRendererConfig.Java2DRendererConfigParser; | ||||
import org.apache.fop.util.ColorUtil; | import org.apache.fop.util.ColorUtil; | ||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.ANTI_ALIASING; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.BACKGROUND_COLOR; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_QUALITY_ELEMENT; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_SPEED; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.ANTI_ALIASING; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.BACKGROUND_COLOR; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY_ELEMENT; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_SPEED; | |||||
/** | /** | ||||
* The Bitmap renderer config data object. | * The Bitmap renderer config data object. | ||||
*/ | */ | ||||
public class BitmapRendererConfig implements RendererConfig { | public class BitmapRendererConfig implements RendererConfig { | ||||
private final EnumMap<BitmapRendererConfigOption, Object> params | |||||
= new EnumMap<BitmapRendererConfigOption, Object>(BitmapRendererConfigOption.class); | |||||
private final EnumMap<BitmapRendererOption, Object> params | |||||
= new EnumMap<BitmapRendererOption, Object>(BitmapRendererOption.class); | |||||
private final DefaultFontConfig fontConfig; | private final DefaultFontConfig fontConfig; | ||||
: JAVA2D_TRANSPARENT_PAGE_BACKGROUND.getDefaultValue()); | : JAVA2D_TRANSPARENT_PAGE_BACKGROUND.getDefaultValue()); | ||||
} | } | ||||
private Object get(BitmapRendererConfigOption option) { | |||||
private Object get(BitmapRendererOption option) { | |||||
return params.get(option); | return params.get(option); | ||||
} | } | ||||
this.mimeType = mimeType; | this.mimeType = mimeType; | ||||
} | } | ||||
private void setParam(BitmapRendererConfig config, BitmapRendererConfigOption option, | |||||
private void setParam(BitmapRendererConfig config, BitmapRendererOption option, | |||||
Object value) { | Object value) { | ||||
config.params.put(option, value != null ? value : option.getDefaultValue()); | config.params.put(option, value != null ? value : option.getDefaultValue()); | ||||
} | } | ||||
ColorUtil.parseColorString(userAgent, background)); | ColorUtil.parseColorString(userAgent, background)); | ||||
} | } | ||||
setParam(config, BitmapRendererConfigOption.ANTI_ALIASING, | |||||
setParam(config, BitmapRendererOption.ANTI_ALIASING, | |||||
getChild(cfg, ANTI_ALIASING).getValueAsBoolean( | getChild(cfg, ANTI_ALIASING).getValueAsBoolean( | ||||
(Boolean) ANTI_ALIASING.getDefaultValue())); | (Boolean) ANTI_ALIASING.getDefaultValue())); | ||||
String optimization = getValue(cfg, RENDERING_QUALITY_ELEMENT); | String optimization = getValue(cfg, RENDERING_QUALITY_ELEMENT); | ||||
setParam(config, RENDERING_QUALITY, | setParam(config, RENDERING_QUALITY, | ||||
!(BitmapRendererConfigOption.getValue(optimization) == RENDERING_SPEED)); | |||||
!(BitmapRendererOption.getValue(optimization) == RENDERING_SPEED)); | |||||
String color = getValue(cfg, COLOR_MODE); | String color = getValue(cfg, COLOR_MODE); | ||||
setParam(config, COLOR_MODE, | setParam(config, COLOR_MODE, | ||||
getBufferedImageIntegerFromColor(BitmapRendererConfigOption.getValue(color))); | |||||
getBufferedImageIntegerFromColor(BitmapRendererOption.getValue(color))); | |||||
} | } | ||||
} | } | ||||
return config; | return config; | ||||
} | } | ||||
private Integer getBufferedImageIntegerFromColor(BitmapRendererConfigOption option) { | |||||
private Integer getBufferedImageIntegerFromColor(BitmapRendererOption option) { | |||||
if (option == null) { | if (option == null) { | ||||
return null; | return null; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private Configuration getChild(Configuration cfg, BitmapRendererConfigOption option) { | |||||
private Configuration getChild(Configuration cfg, BitmapRendererOption option) { | |||||
return cfg.getChild(option.getName()); | return cfg.getChild(option.getName()); | ||||
} | } | ||||
private String getValue(Configuration cfg, BitmapRendererConfigOption option) { | |||||
private String getValue(Configuration cfg, BitmapRendererOption option) { | |||||
return cfg.getChild(option.getName()).getValue(null); | return cfg.getChild(option.getName()).getValue(null); | ||||
} | } | ||||
* An enumeration of the Bitmap renderer configuration opetions available to the user via the FOP | * An enumeration of the Bitmap renderer configuration opetions available to the user via the FOP | ||||
* conf. | * conf. | ||||
*/ | */ | ||||
public enum BitmapRendererConfigOption implements RendererConfigOption { | |||||
public enum BitmapRendererOption implements RendererConfigOption { | |||||
JAVA2D_TRANSPARENT_PAGE_BACKGROUND("transparent-page-background", false), | JAVA2D_TRANSPARENT_PAGE_BACKGROUND("transparent-page-background", false), | ||||
BACKGROUND_COLOR("background-color", Color.WHITE), | BACKGROUND_COLOR("background-color", Color.WHITE), | ||||
ANTI_ALIASING("anti-aliasing", true), | ANTI_ALIASING("anti-aliasing", true), | ||||
private final String name; | private final String name; | ||||
private final Object defaultValue; | private final Object defaultValue; | ||||
private BitmapRendererConfigOption(String name, Object defaultValue) { | |||||
private BitmapRendererOption(String name, Object defaultValue) { | |||||
this.name = name; | this.name = name; | ||||
this.defaultValue = defaultValue; | this.defaultValue = defaultValue; | ||||
} | } | ||||
private BitmapRendererConfigOption(String name) { | |||||
private BitmapRendererOption(String name) { | |||||
this(name, null); | this(name, null); | ||||
} | } | ||||
return defaultValue; | return defaultValue; | ||||
} | } | ||||
public static BitmapRendererConfigOption getValue(String str) { | |||||
for (BitmapRendererConfigOption opt : BitmapRendererConfigOption.values()) { | |||||
public static BitmapRendererOption getValue(String str) { | |||||
for (BitmapRendererOption opt : BitmapRendererOption.values()) { | |||||
if (opt.getName().equalsIgnoreCase(str)) { | if (opt.getName().equalsIgnoreCase(str)) { | ||||
return opt; | return opt; | ||||
} | } |
import org.apache.fop.render.java2d.Java2DRenderingSettings; | import org.apache.fop.render.java2d.Java2DRenderingSettings; | ||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.ANTI_ALIASING; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.ANTI_ALIASING; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY; | |||||
/** | /** | ||||
* This class holds settings used when rendering to bitmaps. | * This class holds settings used when rendering to bitmaps. |
*/ | */ | ||||
public class TIFFDocumentHandler extends AbstractBitmapDocumentHandler { | public class TIFFDocumentHandler extends AbstractBitmapDocumentHandler { | ||||
TIFFDocumentHandler(IFContext context) { | TIFFDocumentHandler(IFContext context) { | ||||
super(context); | super(context); | ||||
} | } | ||||
public IFDocumentHandlerConfigurator getConfigurator() { | public IFDocumentHandlerConfigurator getConfigurator() { | ||||
return new TIFFRendererConfigurator(getUserAgent(), new TIFFRendererConfigParser()); | return new TIFFRendererConfigurator(getUserAgent(), new TIFFRendererConfigParser()); | ||||
} | } | ||||
} | } |
*/ | */ | ||||
public final class TIFFRendererConfig extends BitmapRendererConfig { | public final class TIFFRendererConfig extends BitmapRendererConfig { | ||||
public enum TIFFRendererConfigOption implements RendererConfigOption { | |||||
public enum TIFFRendererOption implements RendererConfigOption { | |||||
COMPRESSION("compression", TIFFCompressionValues.PACKBITS); | COMPRESSION("compression", TIFFCompressionValues.PACKBITS); | ||||
private final String name; | private final String name; | ||||
private final Object defaultValue; | private final Object defaultValue; | ||||
private TIFFRendererConfigOption(String name, Object defaultValue) { | |||||
private TIFFRendererOption(String name, Object defaultValue) { | |||||
this.name = name; | this.name = name; | ||||
this.defaultValue = defaultValue; | this.defaultValue = defaultValue; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private final EnumMap<TIFFRendererConfigOption, Object> params | |||||
= new EnumMap<TIFFRendererConfigOption, Object>(TIFFRendererConfigOption.class); | |||||
private final EnumMap<TIFFRendererOption, Object> params | |||||
= new EnumMap<TIFFRendererOption, Object>(TIFFRendererOption.class); | |||||
private TIFFRendererConfig(DefaultFontConfig fontConfig) { | private TIFFRendererConfig(DefaultFontConfig fontConfig) { | ||||
super(fontConfig); | super(fontConfig); | ||||
} | } | ||||
public TIFFCompressionValues getCompressionType() { | public TIFFCompressionValues getCompressionType() { | ||||
return (TIFFCompressionValues) params.get(TIFFRendererConfigOption.COMPRESSION); | |||||
return (TIFFCompressionValues) params.get(TIFFRendererOption.COMPRESSION); | |||||
} | } | ||||
/** | /** | ||||
private TIFFRendererConfig config; | private TIFFRendererConfig config; | ||||
private void setParam(TIFFRendererConfigOption option, Object value) { | |||||
private void setParam(TIFFRendererOption option, Object value) { | |||||
config.params.put(option, value != null ? value : option.getDefaultValue()); | config.params.put(option, value != null ? value : option.getDefaultValue()); | ||||
} | } | ||||
private String getValue(Configuration cfg, TIFFRendererConfigOption option) { | |||||
private String getValue(Configuration cfg, TIFFRendererOption option) { | |||||
return cfg.getChild(option.getName()).getValue(null); | return cfg.getChild(option.getName()).getValue(null); | ||||
} | } | ||||
.parse(cfg, userAgent.validateStrictly())); | .parse(cfg, userAgent.validateStrictly())); | ||||
super.build(config, userAgent, cfg); | super.build(config, userAgent, cfg); | ||||
if (cfg != null) { | if (cfg != null) { | ||||
setParam(TIFFRendererConfigOption.COMPRESSION, | |||||
setParam(TIFFRendererOption.COMPRESSION, | |||||
TIFFCompressionValues.getValue(getValue(cfg, | TIFFCompressionValues.getValue(getValue(cfg, | ||||
TIFFRendererConfigOption.COMPRESSION))); | |||||
TIFFRendererOption.COMPRESSION))); | |||||
} | } | ||||
return config; | return config; | ||||
} | } |
import org.apache.fop.apps.io.InternalResourceResolver; | import org.apache.fop.apps.io.InternalResourceResolver; | ||||
import org.apache.fop.fonts.CustomFont; | import org.apache.fop.fonts.CustomFont; | ||||
import org.apache.fop.fonts.EmbedFontInfo; | import org.apache.fop.fonts.EmbedFontInfo; | ||||
import org.apache.fop.fonts.EncodingMode; | |||||
import org.apache.fop.fonts.FontCollection; | import org.apache.fop.fonts.FontCollection; | ||||
import org.apache.fop.fonts.FontInfo; | import org.apache.fop.fonts.FontInfo; | ||||
import org.apache.fop.fonts.FontLoader; | import org.apache.fop.fonts.FontLoader; | ||||
InputStream fontSource = resourceResolver.getResource(fontURI); | InputStream fontSource = resourceResolver.getResource(fontURI); | ||||
font = new CustomFontMetricsMapper(fontMetrics, fontSource); | font = new CustomFontMetricsMapper(fontMetrics, fontSource); | ||||
} else { | } else { | ||||
CustomFont fontMetrics = FontLoader.loadFont( | |||||
fontURI, null, true, configFontInfo.getEmbeddingMode(), | |||||
EncodingMode.AUTO, configFontInfo.getKerning(), | |||||
configFontInfo.getAdvanced(), resourceResolver); | |||||
CustomFont fontMetrics = FontLoader.loadFont(fontURI, null, true, | |||||
configFontInfo.getEmbeddingMode(), configFontInfo.getEncodingMode(), | |||||
configFontInfo.getKerning(), configFontInfo.getAdvanced(), | |||||
resourceResolver); | |||||
font = new CustomFontMetricsMapper(fontMetrics); | font = new CustomFontMetricsMapper(fontMetrics); | ||||
} | } | ||||
* An enumeration of the renderer configuration options available to the Java2D renderer via the | * An enumeration of the renderer configuration options available to the Java2D renderer via the | ||||
* FOP conf. | * FOP conf. | ||||
*/ | */ | ||||
public enum Java2DRendererConfigOption implements RendererConfigOption { | |||||
public enum Java2DRendererOption implements RendererConfigOption { | |||||
RENDERING_MODE("rendering", PCLRenderingMode.class), | RENDERING_MODE("rendering", PCLRenderingMode.class), | ||||
TEXT_RENDERING("text-rendering", Boolean.class), | TEXT_RENDERING("text-rendering", Boolean.class), | ||||
private final Class<?> type; | private final Class<?> type; | ||||
private Java2DRendererConfigOption(String name, Class<?> type) { | |||||
private Java2DRendererOption(String name, Class<?> type) { | |||||
this.name = name; | this.name = name; | ||||
this.type = type; | this.type = type; | ||||
} | } |
import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser; | import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser; | ||||
import org.apache.fop.render.RendererConfig; | import org.apache.fop.render.RendererConfig; | ||||
import static org.apache.fop.render.pcl.Java2DRendererConfigOption.DISABLE_PJL; | |||||
import static org.apache.fop.render.pcl.Java2DRendererConfigOption.RENDERING_MODE; | |||||
import static org.apache.fop.render.pcl.Java2DRendererConfigOption.TEXT_RENDERING; | |||||
import static org.apache.fop.render.pcl.Java2DRendererOption.DISABLE_PJL; | |||||
import static org.apache.fop.render.pcl.Java2DRendererOption.RENDERING_MODE; | |||||
import static org.apache.fop.render.pcl.Java2DRendererOption.TEXT_RENDERING; | |||||
/** | /** | ||||
* The PCL renderer configuration data object. | * The PCL renderer configuration data object. | ||||
*/ | */ | ||||
public final class PCLRendererConfig implements RendererConfig { | public final class PCLRendererConfig implements RendererConfig { | ||||
private final Map<Java2DRendererConfigOption, Object> params | |||||
= new EnumMap<Java2DRendererConfigOption, Object>(Java2DRendererConfigOption.class); | |||||
private final Map<Java2DRendererOption, Object> params | |||||
= new EnumMap<Java2DRendererOption, Object>(Java2DRendererOption.class); | |||||
private final DefaultFontConfig fontConfig; | private final DefaultFontConfig fontConfig; | ||||
return getParam(DISABLE_PJL, Boolean.class); | return getParam(DISABLE_PJL, Boolean.class); | ||||
} | } | ||||
private <T> T getParam(Java2DRendererConfigOption option, Class<T> type) { | |||||
private <T> T getParam(Java2DRendererOption option, Class<T> type) { | |||||
assert option.getType().equals(type); | assert option.getType().equals(type); | ||||
return type.cast(params.get(option)); | return type.cast(params.get(option)); | ||||
} | } | ||||
private <T> void setParam(Java2DRendererConfigOption option, T value) { | |||||
private <T> void setParam(Java2DRendererOption option, T value) { | |||||
assert option.getType().isInstance(value); | assert option.getType().isInstance(value); | ||||
params.put(option, value); | params.put(option, value); | ||||
} | } |
return this.documentNavigationHandler; | return this.documentNavigationHandler; | ||||
} | } | ||||
PDFRenderingUtil getPDFUtil() { | |||||
return this.pdfUtil; | |||||
void mergeRendererOptionsConfig(PDFRendererOptionsConfig config) { | |||||
pdfUtil.mergeRendererOptionsConfig(config); | |||||
} | } | ||||
PDFLogicalStructureHandler getLogicalStructureHandler() { | PDFLogicalStructureHandler getLogicalStructureHandler() { |
import org.apache.fop.pdf.PDFXMode; | import org.apache.fop.pdf.PDFXMode; | ||||
import org.apache.fop.render.RendererConfigOption; | import org.apache.fop.render.RendererConfigOption; | ||||
public enum PDFRendererConfigOption implements RendererConfigOption { | |||||
FILTER_LIST("filterList"), | |||||
/** Rendering Options key for the PDF/A mode, default: {@link PDFAMode#DISABLED} */ | |||||
PDF_A_MODE("pdf-a-mode", PDFAMode.DISABLED), | |||||
/** Rendering Options key for the PDF/X mode, default: {@link PDFXMode#DISABLED} */ | |||||
PDF_X_MODE("pdf-x-mode", PDFXMode.DISABLED), | |||||
/** PDF version entry: specify the version of the PDF document created, datatype: String */ | |||||
VERSION("version"), | |||||
/** | |||||
* Rendering Options key for disabling the sRGB color space (only possible if no PDF/A or | |||||
* PDF/X profile is active), default: false | |||||
*/ | |||||
DISABLE_SRGB_COLORSPACE("disable-srgb-colorspace", false), | |||||
/** Rendering Options key for the ICC profile for the output intent. */ | |||||
OUTPUT_PROFILE("output-profile"), | |||||
/** PDF encryption parameter: all parameters as object, datatype: PDFEncryptionParams */ | |||||
ENCRYPTION_PARAMS("encryption-params"), | |||||
public enum PDFEncryptionOption implements RendererConfigOption { | |||||
/** | /** | ||||
* PDF encryption length parameter: must be a multiple of 8 between 40 and 128, | * PDF encryption length parameter: must be a multiple of 8 between 40 and 128, | ||||
* default value 40, datatype: int, default: 40 | * default value 40, datatype: int, default: 40 | ||||
/** PDF encryption parameter: owner password, datatype: String, default: "" */ | /** PDF encryption parameter: owner password, datatype: String, default: "" */ | ||||
OWNER_PASSWORD("owner-password", ""); | OWNER_PASSWORD("owner-password", ""); | ||||
public static final String ENCRYPTION_PARAMS = "encryption-params"; | |||||
private final String name; | private final String name; | ||||
private final Object defaultValue; | private final Object defaultValue; | ||||
private PDFRendererConfigOption(String name, Object defaultValue) { | |||||
private PDFEncryptionOption(String name, Object defaultValue) { | |||||
this.name = name; | this.name = name; | ||||
this.defaultValue = defaultValue; | this.defaultValue = defaultValue; | ||||
} | } | ||||
private PDFRendererConfigOption(String name) { | |||||
private PDFEncryptionOption(String name) { | |||||
this(name, null); | this(name, null); | ||||
} | } | ||||
return this.documentHandler.getContext(); | return this.documentHandler.getContext(); | ||||
} | } | ||||
PDFRenderingUtil getPDFUtil() { | |||||
return this.documentHandler.pdfUtil; | |||||
} | |||||
PDFDocument getPDFDoc() { | PDFDocument getPDFDoc() { | ||||
return this.documentHandler.pdfDoc; | return this.documentHandler.pdfDoc; | ||||
} | } |
package org.apache.fop.render.pdf; | package org.apache.fop.render.pdf; | ||||
import java.net.URI; | |||||
import java.net.URISyntaxException; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.EnumMap; | import java.util.EnumMap; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.apps.io.InternalResourceResolver; | |||||
import org.apache.fop.fonts.DefaultFontConfig; | import org.apache.fop.fonts.DefaultFontConfig; | ||||
import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser; | import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser; | ||||
import org.apache.fop.pdf.PDFAMode; | |||||
import org.apache.fop.pdf.PDFEncryptionParams; | import org.apache.fop.pdf.PDFEncryptionParams; | ||||
import org.apache.fop.pdf.PDFFilterList; | import org.apache.fop.pdf.PDFFilterList; | ||||
import org.apache.fop.pdf.PDFXMode; | |||||
import org.apache.fop.pdf.Version; | |||||
import org.apache.fop.render.RendererConfig; | import org.apache.fop.render.RendererConfig; | ||||
import org.apache.fop.render.RendererConfigOption; | |||||
import org.apache.fop.util.LogUtil; | import org.apache.fop.util.LogUtil; | ||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.DISABLE_SRGB_COLORSPACE; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.ENCRYPTION_LENGTH; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.ENCRYPTION_PARAMS; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.FILTER_LIST; | |||||
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.PDFRendererConfigOption.VERSION; | |||||
import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_LENGTH; | |||||
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; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.VERSION; | |||||
/** | /** | ||||
* The PDF renderer configuration data object. | * The PDF renderer configuration data object. | ||||
private static final Log LOG = LogFactory.getLog(PDFRendererConfig.class); | private static final Log LOG = LogFactory.getLog(PDFRendererConfig.class); | ||||
private final Map<PDFRendererConfigOption, Object> configOptions | |||||
= new EnumMap<PDFRendererConfigOption, Object>(PDFRendererConfigOption.class); | |||||
private final PDFRendererOptionsConfig configOption; | |||||
private final DefaultFontConfig fontConfig; | private final DefaultFontConfig fontConfig; | ||||
private PDFRendererConfig(DefaultFontConfig fontConfig) { | |||||
private PDFRendererConfig(DefaultFontConfig fontConfig, PDFRendererOptionsConfig config) { | |||||
this.fontConfig = fontConfig; | this.fontConfig = fontConfig; | ||||
this.configOption = config; | |||||
} | } | ||||
public DefaultFontConfig getFontInfoConfig() { | |||||
return fontConfig; | |||||
} | |||||
public Map<String, List<String>> getFilterMap() { | |||||
return (Map<String, List<String>>) configOptions.get(FILTER_LIST); | |||||
} | |||||
public PDFAMode getPDFAMode() { | |||||
return (PDFAMode) configOptions.get(PDF_A_MODE); | |||||
} | |||||
public PDFXMode getPDFXMode() { | |||||
return (PDFXMode) configOptions.get(PDF_X_MODE); | |||||
public PDFRendererOptionsConfig getConfigOptions() { | |||||
return configOption; | |||||
} | } | ||||
public PDFEncryptionParams getEncryptionParameters() { | |||||
return (PDFEncryptionParams) configOptions.get(ENCRYPTION_PARAMS); | |||||
} | |||||
public URI getOutputProfileURI() { | |||||
return (URI) configOptions.get(OUTPUT_PROFILE); | |||||
} | |||||
public Boolean getDisableSRGBColorSpace() { | |||||
return (Boolean) configOptions.get(DISABLE_SRGB_COLORSPACE); | |||||
} | |||||
public Version getPDFVersion() { | |||||
String pdfVersion = (String) configOptions.get(VERSION); | |||||
return pdfVersion == null ? null : Version.getValueOf(pdfVersion); | |||||
public DefaultFontConfig getFontInfoConfig() { | |||||
return fontConfig; | |||||
} | } | ||||
/** | /** | ||||
private static final class ParserHelper { | private static final class ParserHelper { | ||||
private final Map<PDFRendererOption, Object> configOptions | |||||
= new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); | |||||
private PDFEncryptionParams encryptionConfig; | |||||
private PDFRendererConfig pdfConfig; | private PDFRendererConfig pdfConfig; | ||||
private ParserHelper(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException { | private ParserHelper(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException { | ||||
pdfConfig = new PDFRendererConfig(new DefaultFontConfigParser().parse(cfg, strict)); | |||||
if (cfg != null) { | if (cfg != null) { | ||||
configure(cfg, userAgent, strict); | configure(cfg, userAgent, strict); | ||||
} | } | ||||
pdfConfig = new PDFRendererConfig(new DefaultFontConfigParser().parse(cfg, strict), | |||||
new PDFRendererOptionsConfig(configOptions, encryptionConfig)); | |||||
} | |||||
private void parseAndPut(PDFRendererOption option, Configuration cfg) { | |||||
put(option, option.parse(parseConfig(cfg, option))); | |||||
} | } | ||||
private void put(PDFRendererConfigOption option, Object value) { | |||||
private void put(PDFRendererOption option, Object value) { | |||||
if (value != null && !value.equals(option.getDefaultValue())) { | if (value != null && !value.equals(option.getDefaultValue())) { | ||||
pdfConfig.configOptions.put(option, value); | |||||
configOptions.put(option, value); | |||||
} | } | ||||
} | } | ||||
private void configure(Configuration cfg, FOUserAgent userAgent, boolean strict) | |||||
throws FOPException { | |||||
private void configure(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException { | |||||
try { | try { | ||||
buildFilterMapFromConfiguration(cfg); | buildFilterMapFromConfiguration(cfg); | ||||
put(PDF_A_MODE, PDFAMode.getValueOf(parseConfig(cfg, PDF_A_MODE))); | |||||
put(PDF_X_MODE, PDFXMode.getValueOf(parseConfig(cfg, PDF_X_MODE))); | |||||
Configuration encryptCfg = cfg.getChild(ENCRYPTION_PARAMS.getName(), false); | |||||
if (encryptCfg != null) { | |||||
PDFEncryptionParams encryptionConfig = new PDFEncryptionParams(); | |||||
encryptionConfig.setOwnerPassword(parseConfig(encryptCfg, OWNER_PASSWORD)); | |||||
encryptionConfig.setUserPassword(parseConfig(encryptCfg, USER_PASSWORD)); | |||||
encryptionConfig.setAllowPrint(!doesValueExist(encryptCfg, NO_PRINT)); | |||||
encryptionConfig.setAllowCopyContent(!doesValueExist(encryptCfg, NO_COPY_CONTENT)); | |||||
encryptionConfig.setAllowEditContent(!doesValueExist(encryptCfg, NO_EDIT_CONTENT)); | |||||
encryptionConfig.setAllowEditAnnotations(!doesValueExist(encryptCfg, NO_ANNOTATIONS)); | |||||
encryptionConfig.setAllowFillInForms(!doesValueExist(encryptCfg, NO_FILLINFORMS)); | |||||
encryptionConfig.setAllowAccessContent(!doesValueExist(encryptCfg, NO_ACCESSCONTENT)); | |||||
encryptionConfig.setAllowAssembleDocument(!doesValueExist(encryptCfg, | |||||
NO_ASSEMBLEDOC)); | |||||
encryptionConfig.setAllowPrintHq(!doesValueExist(encryptCfg, NO_PRINTHQ)); | |||||
String encryptionLength = parseConfig(encryptCfg, ENCRYPTION_LENGTH); | |||||
if (encryptionLength != null) { | |||||
int validatedLength = checkEncryptionLength(Integer.parseInt(encryptionLength), | |||||
userAgent); | |||||
encryptionConfig.setEncryptionLengthInBits(validatedLength); | |||||
} | |||||
put(ENCRYPTION_PARAMS, encryptionConfig); | |||||
} | |||||
put(OUTPUT_PROFILE, InternalResourceResolver.cleanURI(parseConfig(cfg, OUTPUT_PROFILE))); | |||||
put(DISABLE_SRGB_COLORSPACE, Boolean.valueOf(parseConfig(cfg, DISABLE_SRGB_COLORSPACE))); | |||||
put(VERSION, getPDFDocVersion(cfg)); | |||||
parseAndPut(PDF_A_MODE, cfg); | |||||
parseAndPut(PDF_X_MODE, cfg); | |||||
configureEncryptionParams(cfg, userAgent, strict); | |||||
parseAndPut(OUTPUT_PROFILE, cfg); | |||||
parseAndPut(DISABLE_SRGB_COLORSPACE, cfg); | |||||
parseAndPut(VERSION, cfg); | |||||
} catch (ConfigurationException e) { | } catch (ConfigurationException e) { | ||||
LogUtil.handleException(LOG, e, strict); | LogUtil.handleException(LOG, e, strict); | ||||
} catch (URISyntaxException use) { | |||||
LogUtil.handleException(LOG, use, strict); | |||||
} | } | ||||
} | } | ||||
private void buildFilterMapFromConfiguration(Configuration cfg) | |||||
throws ConfigurationException, FOPException { | |||||
private void configureEncryptionParams(Configuration cfg, FOUserAgent userAgent, boolean strict) { | |||||
Configuration encryptCfg = cfg.getChild(ENCRYPTION_PARAMS, false); | |||||
if (encryptCfg != null) { | |||||
encryptionConfig = new PDFEncryptionParams(); | |||||
encryptionConfig.setOwnerPassword(parseConfig(encryptCfg, OWNER_PASSWORD)); | |||||
encryptionConfig.setUserPassword(parseConfig(encryptCfg, USER_PASSWORD)); | |||||
encryptionConfig.setAllowPrint(!doesValueExist(encryptCfg, NO_PRINT)); | |||||
encryptionConfig.setAllowCopyContent(!doesValueExist(encryptCfg, NO_COPY_CONTENT)); | |||||
encryptionConfig.setAllowEditContent(!doesValueExist(encryptCfg, NO_EDIT_CONTENT)); | |||||
encryptionConfig.setAllowEditAnnotations(!doesValueExist(encryptCfg, NO_ANNOTATIONS)); | |||||
encryptionConfig.setAllowFillInForms(!doesValueExist(encryptCfg, NO_FILLINFORMS)); | |||||
encryptionConfig.setAllowAccessContent(!doesValueExist(encryptCfg, NO_ACCESSCONTENT)); | |||||
encryptionConfig.setAllowAssembleDocument(!doesValueExist(encryptCfg, NO_ASSEMBLEDOC)); | |||||
encryptionConfig.setAllowPrintHq(!doesValueExist(encryptCfg, NO_PRINTHQ)); | |||||
String encryptionLength = parseConfig(encryptCfg, ENCRYPTION_LENGTH); | |||||
if (encryptionLength != null) { | |||||
int validatedLength = checkEncryptionLength(Integer.parseInt(encryptionLength), userAgent); | |||||
encryptionConfig.setEncryptionLengthInBits(validatedLength); | |||||
} | |||||
} | |||||
} | |||||
private void buildFilterMapFromConfiguration(Configuration cfg) throws ConfigurationException, | |||||
FOPException { | |||||
Configuration[] filterLists = cfg.getChildren(FILTER_LIST.getName()); | Configuration[] filterLists = cfg.getChildren(FILTER_LIST.getName()); | ||||
Map<String, List<String>> filterMap = new HashMap<String, List<String>>(); | Map<String, List<String>> filterMap = new HashMap<String, List<String>>(); | ||||
for (Configuration filters : filterLists) { | for (Configuration filters : filterLists) { | ||||
put(FILTER_LIST, filterMap); | put(FILTER_LIST, filterMap); | ||||
} | } | ||||
private String parseConfig(Configuration cfg, PDFRendererConfigOption option) { | |||||
private String parseConfig(Configuration cfg, RendererConfigOption option) { | |||||
Configuration child = cfg.getChild(option.getName()); | Configuration child = cfg.getChild(option.getName()); | ||||
return child.getValue(null); | return child.getValue(null); | ||||
} | } | ||||
private boolean doesValueExist(Configuration cfg, PDFRendererConfigOption option) { | |||||
private boolean doesValueExist(Configuration cfg, RendererConfigOption option) { | |||||
return cfg.getChild(option.getName(), false) != null; | return cfg.getChild(option.getName(), false) != null; | ||||
} | } | ||||
private String getPDFDocVersion(Configuration cfg) throws FOPException { | |||||
Configuration pdfVersion = cfg.getChild(VERSION.getName(), false); | |||||
if (pdfVersion != null) { | |||||
String version = pdfVersion.getValue(null); | |||||
if (version != null && version.length() != 0) { | |||||
return version; | |||||
} else { | |||||
throw new FOPException("The PDF version has not been set."); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
private int checkEncryptionLength(int encryptionLength, FOUserAgent userAgent) { | private int checkEncryptionLength(int encryptionLength, FOUserAgent userAgent) { | ||||
int correctEncryptionLength = encryptionLength; | int correctEncryptionLength = encryptionLength; | ||||
if (encryptionLength < 40) { | if (encryptionLength < 40) { |
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.pdf.PDFEncryptionParams; | |||||
import org.apache.fop.render.DefaultRendererConfigurator; | import org.apache.fop.render.DefaultRendererConfigurator; | ||||
import org.apache.fop.render.RendererConfig.RendererConfigParser; | import org.apache.fop.render.RendererConfig.RendererConfigParser; | ||||
import org.apache.fop.render.intermediate.IFDocumentHandler; | import org.apache.fop.render.intermediate.IFDocumentHandler; | ||||
super(userAgent, rendererConfigParser); | super(userAgent, rendererConfigParser); | ||||
} | } | ||||
// ---=== IFDocumentHandler configuration ===--- | |||||
/** {@inheritDoc} */ | /** {@inheritDoc} */ | ||||
public void configure(IFDocumentHandler documentHandler) throws FOPException { | public void configure(IFDocumentHandler documentHandler) throws FOPException { | ||||
final PDFRendererConfig pdfConfig = (PDFRendererConfig) getRendererConfig(documentHandler); | |||||
if (pdfConfig != null) { | |||||
PDFDocumentHandler pdfDocumentHandler = (PDFDocumentHandler) documentHandler; | |||||
PDFRenderingUtil pdfUtil = pdfDocumentHandler.getPDFUtil(); | |||||
if (pdfConfig.getFilterMap() != null) { | |||||
pdfUtil.setFilterMap(pdfConfig.getFilterMap()); | |||||
} | |||||
if (pdfConfig.getPDFAMode() != null) { | |||||
pdfUtil.setAMode(pdfConfig.getPDFAMode()); | |||||
} | |||||
if (pdfConfig.getPDFXMode() != null) { | |||||
pdfUtil.setXMode(pdfConfig.getPDFXMode()); | |||||
} | |||||
if (pdfConfig.getOutputProfileURI() != null) { | |||||
pdfUtil.setOutputProfileURI(pdfConfig.getOutputProfileURI()); | |||||
} | |||||
if (pdfConfig.getPDFVersion() != null) { | |||||
pdfUtil.setPDFVersion(pdfConfig.getPDFVersion()); | |||||
} | |||||
if (pdfConfig.getDisableSRGBColorSpace() != null) { | |||||
pdfUtil.setDisableSRGBColorSpace(pdfConfig.getDisableSRGBColorSpace()); | |||||
} | |||||
PDFEncryptionParams config = pdfConfig.getEncryptionParameters(); | |||||
if (config != null) { | |||||
PDFEncryptionParams utilParams = pdfUtil.getEncryptionParams(); | |||||
if (config.getUserPassword() != null) { | |||||
utilParams.setUserPassword(config.getUserPassword()); | |||||
} | |||||
if (config.getOwnerPassword() != null) { | |||||
utilParams.setOwnerPassword(config.getOwnerPassword()); | |||||
} | |||||
utilParams.setAllowPrint(config.isAllowPrint()); | |||||
utilParams.setAllowCopyContent(config.isAllowCopyContent()); | |||||
utilParams.setAllowEditContent(config.isAllowEditContent()); | |||||
utilParams.setAllowAssembleDocument(config.isAllowAssembleDocument()); | |||||
utilParams.setAllowAccessContent(config.isAllowAccessContent()); | |||||
utilParams.setAllowFillInForms(config.isAllowFillInForms()); | |||||
utilParams.setAllowPrintHq(config.isAllowPrintHq()); | |||||
utilParams.setEncryptionLengthInBits(config.getEncryptionLengthInBits()); | |||||
} | |||||
} | |||||
((PDFDocumentHandler) documentHandler).mergeRendererOptionsConfig( | |||||
((PDFRendererConfig) getRendererConfig(documentHandler)).getConfigOptions()); | |||||
} | } | ||||
} | } |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.pdf; | |||||
import java.net.URI; | |||||
import java.net.URISyntaxException; | |||||
import org.apache.fop.apps.io.InternalResourceResolver; | |||||
import org.apache.fop.pdf.PDFAMode; | |||||
import org.apache.fop.pdf.PDFXMode; | |||||
import org.apache.fop.pdf.Version; | |||||
import org.apache.fop.render.RendererConfigOption; | |||||
public enum PDFRendererOption implements RendererConfigOption { | |||||
FILTER_LIST("filterList", null) { | |||||
@Override | |||||
Object deserialize(String value) { | |||||
throw new UnsupportedOperationException(); | |||||
} | |||||
}, | |||||
/** Rendering Options key for the PDF/A mode, default: {@link PDFAMode#DISABLED} */ | |||||
PDF_A_MODE("pdf-a-mode", PDFAMode.DISABLED){ | |||||
@Override | |||||
PDFAMode deserialize(String value) { | |||||
return PDFAMode.getValueOf(value); | |||||
} | |||||
}, | |||||
/** Rendering Options key for the PDF/X mode, default: {@link PDFXMode#DISABLED} */ | |||||
PDF_X_MODE("pdf-x-mode", PDFXMode.DISABLED){ | |||||
@Override | |||||
PDFXMode deserialize(String value) { | |||||
return PDFXMode.getValueOf(value); | |||||
} | |||||
}, | |||||
/** PDF version entry: specify the version of the PDF document created, datatype: String */ | |||||
VERSION("version") { | |||||
@Override | |||||
Version deserialize(String value) { | |||||
return Version.getValueOf(value); | |||||
} | |||||
}, | |||||
/** | |||||
* Rendering Options key for disabling the sRGB color space (only possible if no PDF/A or | |||||
* PDF/X profile is active), default: false | |||||
*/ | |||||
DISABLE_SRGB_COLORSPACE("disable-srgb-colorspace", false) { | |||||
@Override | |||||
Boolean deserialize(String value) { | |||||
return Boolean.valueOf(value); | |||||
} | |||||
}, | |||||
/** Rendering Options key for the ICC profile for the output intent. */ | |||||
OUTPUT_PROFILE("output-profile") { | |||||
@Override | |||||
URI deserialize(String value) { | |||||
try { | |||||
return InternalResourceResolver.cleanURI(value); | |||||
} catch (URISyntaxException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
} | |||||
}; | |||||
private final String name; | |||||
private final Object defaultValue; | |||||
private PDFRendererOption(String name, Object defaultValue) { | |||||
this.name = name; | |||||
this.defaultValue = defaultValue; | |||||
} | |||||
private PDFRendererOption(String name) { | |||||
this(name, null); | |||||
} | |||||
public String getName() { | |||||
return name; | |||||
} | |||||
public Object getDefaultValue() { | |||||
return defaultValue; | |||||
} | |||||
public Object parse(Object object) { | |||||
if (object instanceof String) { | |||||
return deserialize((String) object); | |||||
} else { | |||||
return object; | |||||
} | |||||
} | |||||
abstract Object deserialize(String value); | |||||
} |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.pdf; | |||||
import java.net.URI; | |||||
import java.util.EnumMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import org.apache.fop.pdf.PDFAMode; | |||||
import org.apache.fop.pdf.PDFEncryptionParams; | |||||
import org.apache.fop.pdf.PDFXMode; | |||||
import org.apache.fop.pdf.Version; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.VERSION; | |||||
/** | |||||
* The renderer options configuration data. | |||||
*/ | |||||
public final class PDFRendererOptionsConfig { | |||||
static final PDFRendererOptionsConfig DEFAULT; | |||||
static { | |||||
final EnumMap<PDFRendererOption, Object> props | |||||
= new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); | |||||
for(PDFRendererOption option : PDFRendererOption.values()) { | |||||
props.put(option, option.getDefaultValue()); | |||||
} | |||||
DEFAULT = new PDFRendererOptionsConfig(props, null); | |||||
} | |||||
private final Map<PDFRendererOption, Object> properties | |||||
= new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); | |||||
private final PDFEncryptionParams encryptionConfig; | |||||
PDFRendererOptionsConfig(Map<PDFRendererOption, Object> props, | |||||
PDFEncryptionParams encryptionParams) { | |||||
properties.putAll(props); | |||||
this.encryptionConfig = copyPDFEncryptionParams(encryptionParams); | |||||
} | |||||
private static PDFEncryptionParams copyPDFEncryptionParams(PDFEncryptionParams source) { | |||||
return source == null ? null : new PDFEncryptionParams(source); | |||||
} | |||||
PDFRendererOptionsConfig merge(PDFRendererOptionsConfig config) { | |||||
if (config == null) { | |||||
return this; | |||||
} else { | |||||
return new PDFRendererOptionsConfig(merge(this.properties, config.properties), | |||||
config.getEncryptionParameters() == null | |||||
? copyPDFEncryptionParams(this.encryptionConfig) | |||||
: copyPDFEncryptionParams(config.getEncryptionParameters())); | |||||
} | |||||
} | |||||
private static Map<PDFRendererOption, Object> merge( | |||||
Map<PDFRendererOption, Object> first, Map<PDFRendererOption, Object> second) { | |||||
final EnumMap<PDFRendererOption, Object> merged | |||||
= new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); | |||||
for (PDFRendererOption option : PDFRendererOption.values()) { | |||||
Object value = second.get(option); | |||||
if (value != null) { | |||||
merged.put(option, value); | |||||
} else { | |||||
merged.put(option, first.get(option)); | |||||
} | |||||
} | |||||
return merged; | |||||
} | |||||
public Map<String, List<String>> getFilterMap() { | |||||
return (Map<String, List<String>>) properties.get(FILTER_LIST); | |||||
} | |||||
public PDFAMode getPDFAMode() { | |||||
return (PDFAMode) properties.get(PDF_A_MODE); | |||||
} | |||||
public PDFXMode getPDFXMode() { | |||||
return (PDFXMode) properties.get(PDF_X_MODE); | |||||
} | |||||
public PDFEncryptionParams getEncryptionParameters() { | |||||
return encryptionConfig; | |||||
} | |||||
public URI getOutputProfileURI() { | |||||
return (URI) properties.get(OUTPUT_PROFILE); | |||||
} | |||||
public Boolean getDisableSRGBColorSpace() { | |||||
return (Boolean) properties.get(DISABLE_SRGB_COLORSPACE); | |||||
} | |||||
public Version getPDFVersion() { | |||||
return (Version) properties.get(VERSION); | |||||
} | |||||
} |
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.net.URI; | import java.net.URI; | ||||
import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||
import java.util.EnumMap; | |||||
import java.util.Map; | import java.util.Map; | ||||
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
import org.apache.fop.pdf.VersionController; | import org.apache.fop.pdf.VersionController; | ||||
import org.apache.fop.render.pdf.extensions.PDFEmbeddedFileExtensionAttachment; | 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 | * Utility class which enables all sorts of features that are not directly connected to the | ||||
/** the PDF Document being created */ | /** the PDF Document being created */ | ||||
private PDFDocument pdfDoc; | 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. */ | /** the ICC stream used as output profile by this document for PDF/A and PDF/X functionality. */ | ||||
private PDFICCStream outputProfile; | private PDFICCStream outputProfile; | ||||
/** the default sRGB color space. */ | /** the default sRGB color space. */ | ||||
private PDFICCBasedColorSpace sRGBColorSpace; | 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) { | PDFRenderingUtil(FOUserAgent userAgent) { | ||||
this.userAgent = userAgent; | this.userAgent = userAgent; | ||||
initialize(); | 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() { | 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. | //Enable accessibility if PDF/A-1a is enabled because it requires tagged PDF. | ||||
userAgent.getRendererOptions().put(Accessibility.ACCESSIBILITY, Boolean.TRUE); | 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() { | private void updateInfo() { | ||||
} | } | ||||
private void updatePDFProfiles() { | 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 { | 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" | throw new IllegalStateException("It is not possible to disable the sRGB color" | ||||
+ " space if PDF/A or PDF/X functionality is enabled or an" | + " space if PDF/A or PDF/X functionality is enabled or an" | ||||
+ " output profile is set!"); | + " output profile is set!"); | ||||
} | } | ||||
ICC_Profile profile; | ICC_Profile profile; | ||||
InputStream in = null; | InputStream in = null; | ||||
if (this.outputProfileURI != null) { | |||||
URI outputProfileUri = rendererConfig.getOutputProfileURI(); | |||||
if (outputProfileUri != null) { | |||||
this.outputProfile = pdfDoc.getFactory().makePDFICCStream(); | this.outputProfile = pdfDoc.getFactory().makePDFICCStream(); | ||||
in = getUserAgent().getResourceResolver().getResource(outputProfileURI); | |||||
in = userAgent.getResourceResolver().getResource(rendererConfig.getOutputProfileURI()); | |||||
try { | try { | ||||
profile = ColorProfileUtil.getICC_Profile(in); | profile = ColorProfileUtil.getICC_Profile(in); | ||||
} finally { | } finally { | ||||
} | } | ||||
String producer = userAgent.getProducer() != null ? userAgent.getProducer() : ""; | String producer = userAgent.getProducer() != null ? userAgent.getProducer() : ""; | ||||
final Version maxPDFVersion = rendererConfig.getPDFVersion(); | |||||
if (maxPDFVersion == null) { | if (maxPDFVersion == null) { | ||||
this.pdfDoc = new PDFDocument(producer); | this.pdfDoc = new PDFDocument(producer); | ||||
} else { | } else { | ||||
} | } | ||||
updateInfo(); | updateInfo(); | ||||
updatePDFProfiles(); | updatePDFProfiles(); | ||||
pdfDoc.setFilterMap(filterMap); | |||||
pdfDoc.setFilterMap(rendererConfig.getFilterMap()); | |||||
pdfDoc.outputHeader(out); | pdfDoc.outputHeader(out); | ||||
//Setup encryption if necessary | //Setup encryption if necessary | ||||
PDFEncryptionManager.setupPDFEncryption(encryptionParams, pdfDoc); | |||||
PDFEncryptionManager.setupPDFEncryption(rendererConfig.getEncryptionParameters(), pdfDoc); | |||||
addsRGBColorSpace(); | addsRGBColorSpace(); | ||||
if (this.outputProfileURI != null) { | |||||
if (rendererConfig.getOutputProfileURI() != null) { | |||||
addDefaultOutputProfile(); | addDefaultOutputProfile(); | ||||
} | } | ||||
PDFXMode pdfXMode = rendererConfig.getPDFXMode(); | |||||
if (pdfXMode != PDFXMode.DISABLED) { | if (pdfXMode != PDFXMode.DISABLED) { | ||||
log.debug(pdfXMode + " is active."); | log.debug(pdfXMode + " is active."); | ||||
log.warn("Note: " + pdfXMode | log.warn("Note: " + pdfXMode | ||||
+ " support is work-in-progress and not fully implemented, yet!"); | + " support is work-in-progress and not fully implemented, yet!"); | ||||
addPDFXOutputIntent(); | addPDFXOutputIntent(); | ||||
} | } | ||||
PDFAMode pdfAMode = rendererConfig.getPDFAMode(); | |||||
if (pdfAMode.isPDFA1LevelB()) { | if (pdfAMode.isPDFA1LevelB()) { | ||||
log.debug("PDF/A is active. Conformance Level: " + pdfAMode); | log.debug("PDF/A is active. Conformance Level: " + pdfAMode); | ||||
addPDFA1OutputIntent(); | addPDFA1OutputIntent(); | ||||
} catch (URISyntaxException use) { | } catch (URISyntaxException use) { | ||||
throw new RuntimeException(use); | throw new RuntimeException(use); | ||||
} | } | ||||
InputStream in = getUserAgent().getResourceResolver().getResource(srcURI); | |||||
InputStream in = userAgent.getResourceResolver().getResource(srcURI); | |||||
if (in == null) { | if (in == null) { | ||||
throw new FileNotFoundException(embeddedFile.getSrc()); | throw new FileNotFoundException(embeddedFile.getSrc()); | ||||
} | } | ||||
nameArray.add(new PDFReference(fileSpec)); | 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."); | |||||
} | |||||
} | |||||
} | } | ||||
} | } |
import org.apache.fop.render.RendererConfig; | import org.apache.fop.render.RendererConfig; | ||||
import org.apache.fop.util.LogUtil; | import org.apache.fop.util.LogUtil; | ||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.AUTO_ROTATE_LANDSCAPE; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.DSC_COMPLIANT; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.LANGUAGE_LEVEL; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.OPTIMIZE_RESOURCES; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.RENDERING_MODE; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.SAFE_SET_PAGE_DEVICE; | |||||
import static org.apache.fop.render.ps.PSRendererOption.AUTO_ROTATE_LANDSCAPE; | |||||
import static org.apache.fop.render.ps.PSRendererOption.DSC_COMPLIANT; | |||||
import static org.apache.fop.render.ps.PSRendererOption.LANGUAGE_LEVEL; | |||||
import static org.apache.fop.render.ps.PSRendererOption.OPTIMIZE_RESOURCES; | |||||
import static org.apache.fop.render.ps.PSRendererOption.RENDERING_MODE; | |||||
import static org.apache.fop.render.ps.PSRendererOption.SAFE_SET_PAGE_DEVICE; | |||||
/** | /** | ||||
* The PostScript renderer configuration data object. | * The PostScript renderer configuration data object. | ||||
*/ | */ | ||||
public final class PSRendererConfig implements RendererConfig { | public final class PSRendererConfig implements RendererConfig { | ||||
private final EnumMap<PSRendererConfigurationOption, Object> params | |||||
= new EnumMap<PSRendererConfigurationOption, Object>(PSRendererConfigurationOption.class); | |||||
private final EnumMap<PSRendererOption, Object> params | |||||
= new EnumMap<PSRendererOption, Object>(PSRendererOption.class); | |||||
private final DefaultFontConfig fontConfig; | private final DefaultFontConfig fontConfig; | ||||
} | } | ||||
} | } | ||||
private void setConfigParameter(PSRendererConfigurationOption option, | |||||
private void setConfigParameter(PSRendererOption option, | |||||
Object value) { | Object value) { | ||||
config.params.put(option, value != null ? value : option.getDefaultValue()); | config.params.put(option, value != null ? value : option.getDefaultValue()); | ||||
} | } | ||||
private void setBoolConfigParam(Configuration cfg, PSRendererConfigurationOption option) { | |||||
private void setBoolConfigParam(Configuration cfg, PSRendererOption option) { | |||||
setConfigParameter(option, cfg.getChild( | setConfigParameter(option, cfg.getChild( | ||||
option.getName()).getValueAsBoolean((Boolean) option.getDefaultValue())); | option.getName()).getValueAsBoolean((Boolean) option.getDefaultValue())); | ||||
} | } |
/** | /** | ||||
* An enumeration of the PostScript renderer configuration options along with their default values. | * An enumeration of the PostScript renderer configuration options along with their default values. | ||||
*/ | */ | ||||
public enum PSRendererConfigurationOption implements RendererConfigOption { | |||||
public enum PSRendererOption implements RendererConfigOption { | |||||
/** Indicates whether landscape pages should be rotated, default: false */ | /** Indicates whether landscape pages should be rotated, default: false */ | ||||
AUTO_ROTATE_LANDSCAPE("auto-rotate-landscape", false), | AUTO_ROTATE_LANDSCAPE("auto-rotate-landscape", false), | ||||
/** Sets the PostScript language leven, default: {@see PSGenerator#DEFAULT_LANGUAGE_LEVEL}*/ | /** Sets the PostScript language leven, default: {@see PSGenerator#DEFAULT_LANGUAGE_LEVEL}*/ | ||||
private final String name; | private final String name; | ||||
private final Object defaultValue; | private final Object defaultValue; | ||||
private PSRendererConfigurationOption(String name, Object defaultValue) { | |||||
private PSRendererOption(String name, Object defaultValue) { | |||||
this.name = name; | this.name = name; | ||||
this.defaultValue = defaultValue; | this.defaultValue = defaultValue; | ||||
} | } |
import org.apache.fop.render.ps.extensions.PSExtensionAttachment; | import org.apache.fop.render.ps.extensions.PSExtensionAttachment; | ||||
import org.apache.fop.render.ps.extensions.PSSetupCode; | import org.apache.fop.render.ps.extensions.PSSetupCode; | ||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.AUTO_ROTATE_LANDSCAPE; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.LANGUAGE_LEVEL; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.OPTIMIZE_RESOURCES; | |||||
import static org.apache.fop.render.ps.PSRendererOption.AUTO_ROTATE_LANDSCAPE; | |||||
import static org.apache.fop.render.ps.PSRendererOption.LANGUAGE_LEVEL; | |||||
import static org.apache.fop.render.ps.PSRendererOption.OPTIMIZE_RESOURCES; | |||||
/** | /** | ||||
* Utility class which enables all sorts of features that are not directly connected to the | * Utility class which enables all sorts of features that are not directly connected to the |
import org.apache.fop.util.HexEncoder; | import org.apache.fop.util.HexEncoder; | ||||
/** | /** | ||||
* Renders the attributed character iterator of a text node. | |||||
* Renders the attributed character iterator of a {@link org.apache.batik.gvt.TextNode TextNode}. | |||||
* This class draws the text directly using PostScript text operators so | * This class draws the text directly using PostScript text operators so | ||||
* the text is not drawn using shapes which makes the PS files larger. | * the text is not drawn using shapes which makes the PS files larger. | ||||
* <p> | * <p> |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.ps.fonts; | |||||
import java.io.IOException; | |||||
import org.apache.xmlgraphics.ps.PSGenerator; | |||||
import org.apache.xmlgraphics.util.io.ASCIIHexOutputStream; | |||||
/** | |||||
* This is a wrapper for {@link PSGenerator} that contains some members specific for streaming | |||||
* TrueType fonts to a PostScript document. | |||||
*/ | |||||
public class PSTTFGenerator { | |||||
private PSGenerator gen; | |||||
private ASCIIHexOutputStream hexOut; | |||||
/** | |||||
* The buffer is used to store the font file in an array of hex-encoded strings. Strings are | |||||
* limited to 65535 characters, string will start with a newline, 2 characters are needed to | |||||
* hex-encode each byte. | |||||
*/ | |||||
public static final int MAX_BUFFER_SIZE = 32764; | |||||
/** | |||||
* Creates a new instance wrapping the given generator. | |||||
* @param gen the PSGenerator to wrap | |||||
*/ | |||||
public PSTTFGenerator(PSGenerator gen) { | |||||
this.gen = gen; | |||||
hexOut = new ASCIIHexOutputStream(gen.getOutputStream()); | |||||
} | |||||
/** | |||||
* Writes the '<' character that starts a string. | |||||
*/ | |||||
public void startString() throws IOException { | |||||
// We need to reset the streamer so that it starts a new line in the PS document | |||||
hexOut = new ASCIIHexOutputStream(gen.getOutputStream()); | |||||
gen.writeln("<"); | |||||
} | |||||
/** | |||||
* Writes the given string to the output. | |||||
* @param cmd a string | |||||
*/ | |||||
public void write(String cmd) throws IOException { | |||||
gen.write(cmd); | |||||
} | |||||
/** | |||||
* Writes the given string to the output, followed by a newline. | |||||
* @param cmd a string | |||||
*/ | |||||
public void writeln(String cmd) throws IOException { | |||||
gen.writeln(cmd); | |||||
} | |||||
/** | |||||
* Writes bytes from the given byte array to the output. | |||||
* | |||||
* @param byteArray byte[] a byte array | |||||
* @param offset the position in the byte array where the streaming must start | |||||
* @param length the number of bytes to stream. This MUST be less than | |||||
* {@link #MAX_BUFFER_SIZE} - 1 since strings are suffixed by '00' (see Section 4.2 of | |||||
* Adobe Technical Note #5012, <em>The Type 42 Font Format Specification</em>.). | |||||
*/ | |||||
public void streamBytes(byte[] byteArray, int offset, int length) throws IOException { | |||||
if (length > MAX_BUFFER_SIZE) { | |||||
throw new UnsupportedOperationException("Attempting to write a string to a PostScript" | |||||
+ " file that is greater than the buffer size."); | |||||
} | |||||
hexOut.write(byteArray, offset, length); | |||||
} | |||||
/** | |||||
* Finishes writing a string by appending '00' and '>' to the end. | |||||
*/ | |||||
public void endString() throws IOException { | |||||
/* Appends a '00' to the end of the string as specified in the spec */ | |||||
gen.write("00\n> "); | |||||
} | |||||
} |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.ps.fonts; | |||||
import java.io.IOException; | |||||
import org.apache.fop.fonts.truetype.TTFGlyphOutputStream; | |||||
/** | |||||
* Streams glyphs in accordance with the constraints of the PostScript file format. | |||||
* Mainly, PostScript strings have a limited capacity and the font data may have to be | |||||
* broken down into several strings; however, this must occur at well-defined places like | |||||
* table or glyph boundaries. See also Adobe Technical Note #5012, <em>The Type 42 Font | |||||
* Format Specification</em>. | |||||
*/ | |||||
public class PSTTFGlyphOutputStream implements TTFGlyphOutputStream { | |||||
/** Total number of bytes written so far. */ | |||||
private int byteCounter; | |||||
private int lastStringBoundary; | |||||
private PSTTFGenerator ttfGen; | |||||
/** | |||||
* Constructor | |||||
* @param ttfGen PSTTFGenerator | |||||
*/ | |||||
public PSTTFGlyphOutputStream(PSTTFGenerator ttfGen) { | |||||
this.ttfGen = ttfGen; | |||||
} | |||||
public void startGlyphStream() throws IOException { | |||||
ttfGen.startString(); | |||||
} | |||||
public void streamGlyph(byte[] glyphData, int offset, int size) throws IOException { | |||||
if (size > PSTTFGenerator.MAX_BUFFER_SIZE) { | |||||
throw new UnsupportedOperationException("The glyph is " + size | |||||
+ " bytes. There may be an error in the font file."); | |||||
} | |||||
if (size + (byteCounter - lastStringBoundary) < PSTTFGenerator.MAX_BUFFER_SIZE) { | |||||
ttfGen.streamBytes(glyphData, offset, size); | |||||
} else { | |||||
ttfGen.endString(); | |||||
lastStringBoundary = byteCounter; | |||||
ttfGen.startString(); | |||||
ttfGen.streamBytes(glyphData, offset, size); | |||||
} | |||||
byteCounter += size; | |||||
} | |||||
public void endGlyphStream() throws IOException { | |||||
ttfGen.endString(); | |||||
} | |||||
} |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.ps.fonts; | |||||
import java.io.IOException; | |||||
import org.apache.xmlgraphics.ps.PSGenerator; | |||||
import org.apache.fop.fonts.truetype.TTFGlyphOutputStream; | |||||
import org.apache.fop.fonts.truetype.TTFOutputStream; | |||||
import org.apache.fop.fonts.truetype.TTFTableOutputStream; | |||||
/** | |||||
* Streams a TrueType font according to the PostScript format. | |||||
*/ | |||||
public class PSTTFOutputStream implements TTFOutputStream { | |||||
private final PSTTFGenerator ttfGen; | |||||
/** | |||||
* Creates a new instance wrapping the given generator. | |||||
* | |||||
* @param gen the generator to wrap | |||||
*/ | |||||
public PSTTFOutputStream(PSGenerator gen) { | |||||
this.ttfGen = new PSTTFGenerator(gen); | |||||
} | |||||
public void startFontStream() throws IOException { | |||||
ttfGen.write("/sfnts["); | |||||
} | |||||
public TTFTableOutputStream getTableOutputStream() { | |||||
return new PSTTFTableOutputStream(ttfGen); | |||||
} | |||||
public TTFGlyphOutputStream getGlyphOutputStream() { | |||||
return new PSTTFGlyphOutputStream(ttfGen); | |||||
} | |||||
public void endFontStream() throws IOException { | |||||
ttfGen.writeln("] def"); | |||||
} | |||||
} |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.ps.fonts; | |||||
import java.io.IOException; | |||||
import org.apache.fop.fonts.truetype.TTFTableOutputStream; | |||||
/** | |||||
* Streams a TrueType table according to the PostScript format. | |||||
*/ | |||||
public class PSTTFTableOutputStream implements TTFTableOutputStream { | |||||
private PSTTFGenerator ttfGen; | |||||
/** | |||||
* Constructor. | |||||
* @param ttfGen the helper object to stream TrueType data | |||||
*/ | |||||
public PSTTFTableOutputStream(PSTTFGenerator ttfGen) { | |||||
this.ttfGen = ttfGen; | |||||
} | |||||
public void streamTable(byte[] ttfData, int offset, int size) throws IOException { | |||||
int offsetPosition = offset; | |||||
// Need to split the table into MAX_BUFFER_SIZE chunks | |||||
for (int i = 0; i < size / PSTTFGenerator.MAX_BUFFER_SIZE; i++) { | |||||
streamString(ttfData, offsetPosition, PSTTFGenerator.MAX_BUFFER_SIZE); | |||||
offsetPosition += PSTTFGenerator.MAX_BUFFER_SIZE; | |||||
} | |||||
if (size % PSTTFGenerator.MAX_BUFFER_SIZE > 0) { | |||||
streamString(ttfData, offsetPosition, size % PSTTFGenerator.MAX_BUFFER_SIZE); | |||||
} | |||||
} | |||||
private void streamString(byte[] byteArray, int offset, int length) throws IOException { | |||||
ttfGen.startString(); | |||||
ttfGen.streamBytes(byteArray, offset, length); | |||||
ttfGen.endString(); | |||||
} | |||||
} |
*/ | */ | ||||
public final class TxtRendererConfig implements RendererConfig { | public final class TxtRendererConfig implements RendererConfig { | ||||
public enum TxtRendererConfigOption implements RendererConfigOption { | |||||
public enum TxtRendererOption implements RendererConfigOption { | |||||
ENCODING("encoding", "UTF-8"); | ENCODING("encoding", "UTF-8"); | ||||
private final String name; | private final String name; | ||||
private final Object defaultValue; | private final Object defaultValue; | ||||
private TxtRendererConfigOption(String name, Object defaultValue) { | |||||
private TxtRendererOption(String name, Object defaultValue) { | |||||
this.name = name; | this.name = name; | ||||
this.defaultValue = defaultValue; | this.defaultValue = defaultValue; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private final EnumMap<TxtRendererConfigOption, Object> params | |||||
= new EnumMap<TxtRendererConfigOption, Object>(TxtRendererConfigOption.class); | |||||
private final EnumMap<TxtRendererOption, Object> params | |||||
= new EnumMap<TxtRendererOption, Object>(TxtRendererOption.class); | |||||
private final DefaultFontConfig fontConfig; | private final DefaultFontConfig fontConfig; | ||||
} | } | ||||
public String getEncoding() { | public String getEncoding() { | ||||
return (String) params.get(TxtRendererConfigOption.ENCODING); | |||||
return (String) params.get(TxtRendererOption.ENCODING); | |||||
} | } | ||||
/** | /** | ||||
TxtRendererConfig config = new TxtRendererConfig(new DefaultFontConfigParser().parse(cfg, | TxtRendererConfig config = new TxtRendererConfig(new DefaultFontConfigParser().parse(cfg, | ||||
userAgent.validateStrictly())); | userAgent.validateStrictly())); | ||||
if (cfg != null) { | if (cfg != null) { | ||||
TxtRendererConfigOption option = TxtRendererConfigOption.ENCODING; | |||||
TxtRendererOption option = TxtRendererOption.ENCODING; | |||||
String value = cfg.getChild(option.getName(), true).getValue(null); | String value = cfg.getChild(option.getName(), true).getValue(null); | ||||
config.params.put(option, value != null ? value : option.getDefaultValue()); | config.params.put(option, value != null ? value : option.getDefaultValue()); | ||||
} | } |
try { | try { | ||||
//Filter map | //Filter map | ||||
PDFRendererConfig pdfConfig = new PDFRendererConfigParser().build(null, cfg); | PDFRendererConfig pdfConfig = new PDFRendererConfigParser().build(null, cfg); | ||||
pdfDoc.setFilterMap(pdfConfig.getFilterMap()); | |||||
pdfDoc.setFilterMap(pdfConfig.getConfigOptions().getFilterMap()); | |||||
} catch (FOPException e) { | } catch (FOPException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
} | } |
@RunWith(Suite.class) | @RunWith(Suite.class) | ||||
@SuiteClasses({ | @SuiteClasses({ | ||||
IncludeObjectTestCase.class, | IncludeObjectTestCase.class, | ||||
AFPResourceUtilTestCase.class, | |||||
AFPObjectAreaInfoTestCase.class, | AFPObjectAreaInfoTestCase.class, | ||||
AFPPaintingStateTestCase.class | AFPPaintingStateTestCase.class | ||||
}) | }) |
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | ||||
import org.apache.fop.render.afp.AFPRendererConfig; | import org.apache.fop.render.afp.AFPRendererConfig; | ||||
import org.apache.fop.render.afp.AFPRendererConfigOption; | |||||
import org.apache.fop.render.afp.AFPRendererOption; | |||||
import org.apache.fop.render.afp.AFPShadingMode; | import org.apache.fop.render.afp.AFPShadingMode; | ||||
import static org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions.MODE_GRAYSCALE; | import static org.apache.fop.render.afp.AFPRendererConfig.ImagesModeOptions.MODE_GRAYSCALE; | ||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.DEFAULT_RESOURCE_LEVELS; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_DITHERING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_FS45; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_JPEG; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_MAPPING_OPTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_MODE; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_NATIVE; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.IMAGES_WRAP_PSEG; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.JPEG_ALLOW_JPEG_EMBEDDING; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.JPEG_BITMAP_ENCODING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.LINE_WIDTH_CORRECTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.RENDERER_RESOLUTION; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.RESOURCE_GROUP_URI; | |||||
import static org.apache.fop.render.afp.AFPRendererConfigOption.SHADING; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.DEFAULT_RESOURCE_LEVELS; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_DITHERING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_FS45; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_JPEG; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_MAPPING_OPTION; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_MODE; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_NATIVE; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_WRAP_PSEG; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.JPEG_ALLOW_JPEG_EMBEDDING; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.JPEG_BITMAP_ENCODING_QUALITY; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.LINE_WIDTH_CORRECTION; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.RENDERER_RESOLUTION; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.RESOURCE_GROUP_URI; | |||||
import static org.apache.fop.render.afp.AFPRendererOption.SHADING; | |||||
/** | /** | ||||
* A config builder specific to a particular renderer for specific MIME type. | * A config builder specific to a particular renderer for specific MIME type. | ||||
super(MimeConstants.MIME_AFP); | super(MimeConstants.MIME_AFP); | ||||
} | } | ||||
private AFPRendererConfBuilder createTextElement(AFPRendererConfigOption option, String value) { | |||||
private AFPRendererConfBuilder createTextElement(AFPRendererOption option, String value) { | |||||
createTextElement(option.getName(), value); | createTextElement(option.getName(), value); | ||||
return this; | return this; | ||||
} | } | ||||
return AFPRendererConfBuilder.this.endImages(); | return AFPRendererConfBuilder.this.endImages(); | ||||
} | } | ||||
private ImagesBuilder setAttribute(AFPRendererConfigOption options, Object value) { | |||||
private ImagesBuilder setAttribute(AFPRendererOption options, String value) { | |||||
return setAttribute(options.getName(), value); | |||||
} | |||||
private ImagesBuilder setAttribute(AFPRendererOption options, Object value) { | |||||
return setAttribute(options.getName(), value); | return setAttribute(options.getName(), value); | ||||
} | } | ||||
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | ||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.ANTI_ALIASING; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.BACKGROUND_COLOR; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_QUALITY_ELEMENT; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.ANTI_ALIASING; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.BACKGROUND_COLOR; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY_ELEMENT; | |||||
public class BitmapRendererConfBuilder extends RendererConfBuilder { | public class BitmapRendererConfBuilder extends RendererConfBuilder { | ||||
import org.w3c.dom.Element; | import org.w3c.dom.Element; | ||||
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | ||||
import org.apache.fop.pdf.PDFEncryptionParams; | |||||
import org.apache.fop.render.RendererConfigOption; | import org.apache.fop.render.RendererConfigOption; | ||||
import org.apache.fop.render.pdf.PDFRendererConfigOption; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.DISABLE_SRGB_COLORSPACE; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.ENCRYPTION_LENGTH; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.ENCRYPTION_PARAMS; | |||||
import static org.apache.fop.render.pdf.PDFRendererConfigOption.FILTER_LIST; | |||||
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.PDFRendererConfigOption.VERSION; | |||||
import org.apache.fop.render.pdf.PDFEncryptionOption; | |||||
import org.apache.fop.render.pdf.PDFRendererOption; | |||||
import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_LENGTH; | |||||
import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_PARAMS; | |||||
import static org.apache.fop.render.pdf.PDFEncryptionOption.OWNER_PASSWORD; | |||||
import static org.apache.fop.render.pdf.PDFEncryptionOption.USER_PASSWORD; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.VERSION; | |||||
/** | /** | ||||
* A config builder specific to a particular renderer for specific MIME type. | * A config builder specific to a particular renderer for specific MIME type. | ||||
private final Element el; | private final Element el; | ||||
private EncryptionParamsBuilder() { | private EncryptionParamsBuilder() { | ||||
el = createElement(ENCRYPTION_PARAMS.getName()); | |||||
el = createElement(ENCRYPTION_PARAMS); | |||||
} | } | ||||
public EncryptionParamsBuilder setEncryptionLength(int length) { | public EncryptionParamsBuilder setEncryptionLength(int length) { | ||||
return this; | return this; | ||||
} | } | ||||
public EncryptionParamsBuilder setAllowParam(PDFRendererConfigOption option) { | |||||
public EncryptionParamsBuilder setAllowParam(PDFEncryptionOption option) { | |||||
el.appendChild(createElement(option.getName())); | el.appendChild(createElement(option.getName())); | ||||
return this; | return this; | ||||
} | } |
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | ||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.AUTO_ROTATE_LANDSCAPE; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.DSC_COMPLIANT; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.LANGUAGE_LEVEL; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.OPTIMIZE_RESOURCES; | |||||
import static org.apache.fop.render.ps.PSRendererConfigurationOption.SAFE_SET_PAGE_DEVICE; | |||||
import static org.apache.fop.render.ps.PSRendererOption.AUTO_ROTATE_LANDSCAPE; | |||||
import static org.apache.fop.render.ps.PSRendererOption.DSC_COMPLIANT; | |||||
import static org.apache.fop.render.ps.PSRendererOption.LANGUAGE_LEVEL; | |||||
import static org.apache.fop.render.ps.PSRendererOption.OPTIMIZE_RESOURCES; | |||||
import static org.apache.fop.render.ps.PSRendererOption.SAFE_SET_PAGE_DEVICE; | |||||
/** | /** | ||||
* A fop conf builder specific to a particular renderer for Postscript. | * A fop conf builder specific to a particular renderer for Postscript. |
package org.apache.fop.apps; | package org.apache.fop.apps; | ||||
import static org.apache.fop.render.bitmap.TIFFRendererConfig.TIFFRendererConfigOption.COMPRESSION; | |||||
import static org.apache.fop.render.bitmap.TIFFRendererConfig.TIFFRendererOption.COMPRESSION; | |||||
public class TIFFRendererConfBuilder extends BitmapRendererConfBuilder { | public class TIFFRendererConfBuilder extends BitmapRendererConfBuilder { | ||||
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | ||||
import static org.apache.fop.render.txt.TxtRendererConfig.TxtRendererConfigOption.ENCODING; | |||||
import static org.apache.fop.render.txt.TxtRendererConfig.TxtRendererOption.ENCODING; | |||||
public class TxtRendererConfBuilder extends RendererConfBuilder { | public class TxtRendererConfBuilder extends RendererConfBuilder { | ||||
import org.apache.fop.apps.BitmapRendererConfBuilder; | import org.apache.fop.apps.BitmapRendererConfBuilder; | ||||
import org.apache.fop.render.bitmap.BitmapRendererConfig.BitmapRendererConfigParser; | import org.apache.fop.render.bitmap.BitmapRendererConfig.BitmapRendererConfigParser; | ||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_BILEVEL; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_BINARY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_GRAY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_RGB; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_RGBA; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_SPEED; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_BILEVEL; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_BINARY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_GRAY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_RGB; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_RGBA; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_SPEED; | |||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.assertFalse; | import static org.junit.Assert.assertFalse; | ||||
import static org.junit.Assert.assertNull; | import static org.junit.Assert.assertNull; |
import org.apache.fop.render.intermediate.IFDocumentHandler; | import org.apache.fop.render.intermediate.IFDocumentHandler; | ||||
import org.apache.fop.util.ColorUtil; | import org.apache.fop.util.ColorUtil; | ||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_BILEVEL; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_BINARY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_GRAY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_RGB; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.COLOR_MODE_RGBA; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererConfigOption.RENDERING_SPEED; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_BILEVEL; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_BINARY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_GRAY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_RGB; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE_RGBA; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY; | |||||
import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_SPEED; | |||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.assertFalse; | import static org.junit.Assert.assertFalse; | ||||
import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; |
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import static org.apache.fop.render.pcl.Java2DRendererConfigOption.DISABLE_PJL; | |||||
import static org.apache.fop.render.pcl.Java2DRendererConfigOption.RENDERING_MODE; | |||||
import static org.apache.fop.render.pcl.Java2DRendererConfigOption.TEXT_RENDERING; | |||||
import static org.apache.fop.render.pcl.Java2DRendererOption.DISABLE_PJL; | |||||
import static org.apache.fop.render.pcl.Java2DRendererOption.RENDERING_MODE; | |||||
import static org.apache.fop.render.pcl.Java2DRendererOption.TEXT_RENDERING; | |||||
/** | /** | ||||
* A config builder specific to a particular renderer for specific MIME type. | * A config builder specific to a particular renderer for specific MIME type. | ||||
*/ | */ | ||||
return createTextElement(DISABLE_PJL, String.valueOf(value)); | return createTextElement(DISABLE_PJL, String.valueOf(value)); | ||||
} | } | ||||
private PCLRendererConfBuilder createTextElement(Java2DRendererConfigOption option, String value) { | |||||
private PCLRendererConfBuilder createTextElement(Java2DRendererOption option, String value) { | |||||
createTextElement(option.getName(), value); | createTextElement(option.getName(), value); | ||||
return this; | return this; | ||||
} | } |
package org.apache.fop.render.pdf; | package org.apache.fop.render.pdf; | ||||
import static org.junit.Assert.fail; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.pdf.PDFConformanceException; | import org.apache.fop.pdf.PDFConformanceException; | ||||
import static org.junit.Assert.fail; | |||||
/** | /** | ||||
* Tests PDF/A-1 functionality. | * Tests PDF/A-1 functionality. | ||||
*/ | */ | ||||
* Test exception when PDF/A-1 is enabled together with encryption. | * Test exception when PDF/A-1 is enabled together with encryption. | ||||
* @throws Exception if the test fails | * @throws Exception if the test fails | ||||
*/ | */ | ||||
@Test | |||||
@Test(expected = PDFConformanceException.class) | |||||
public void testNoEncryption() throws Exception { | public void testNoEncryption() throws Exception { | ||||
final FOUserAgent ua = getUserAgent(); | final FOUserAgent ua = getUserAgent(); | ||||
ua.getRendererOptions().put("owner-password", "mypassword"); //To enabled encryption | ua.getRendererOptions().put("owner-password", "mypassword"); //To enabled encryption | ||||
File foFile = new File(foBaseDir, "minimal-pdf-a.fo"); | File foFile = new File(foBaseDir, "minimal-pdf-a.fo"); | ||||
try { | |||||
convertFO(foFile, ua, dumpPDF); | |||||
fail("Expected PDFConformanceException. PDF/A-1 and PDF encryption don't go together."); | |||||
} catch (PDFConformanceException e) { | |||||
//Good! | |||||
} | |||||
convertFO(foFile, ua, dumpPDF); | |||||
} | } | ||||
/** | /** |
import org.junit.Test; | import org.junit.Test; | ||||
import org.apache.fop.apps.AbstractRendererConfigParserTester; | import org.apache.fop.apps.AbstractRendererConfigParserTester; | ||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FopConfBuilder; | |||||
import org.apache.fop.apps.PDFRendererConfBuilder; | import org.apache.fop.apps.PDFRendererConfBuilder; | ||||
import org.apache.fop.pdf.PDFAMode; | import org.apache.fop.pdf.PDFAMode; | ||||
import org.apache.fop.pdf.PDFXMode; | import org.apache.fop.pdf.PDFXMode; | ||||
.startEncryptionParams() | .startEncryptionParams() | ||||
.setUserPassword(testPassword) | .setUserPassword(testPassword) | ||||
.endEncryptionParams()); | .endEncryptionParams()); | ||||
assertEquals(testPassword, conf.getEncryptionParameters().getUserPassword()); | |||||
assertEquals(testPassword, conf.getConfigOptions().getEncryptionParameters().getUserPassword()); | |||||
} | } | ||||
private void testRestrictEncryptionParameter(PDFRendererConfigOption option) | |||||
throws Exception { | |||||
private void testRestrictEncryptionParameter(PDFEncryptionOption option) throws Exception { | |||||
parseConfig(createRenderer().startEncryptionParams() | parseConfig(createRenderer().startEncryptionParams() | ||||
.setAllowParam(option) | .setAllowParam(option) | ||||
.endEncryptionParams()); | .endEncryptionParams()); | ||||
assertTrue(testEncryptionParameter(option)); | assertTrue(testEncryptionParameter(option)); | ||||
} | } | ||||
public boolean testEncryptionParameter(PDFRendererConfigOption option) throws Exception { | |||||
public boolean testEncryptionParameter(PDFEncryptionOption option) throws Exception { | |||||
switch (option) { | switch (option) { | ||||
case NO_PRINT: | case NO_PRINT: | ||||
return conf.getEncryptionParameters().isAllowPrint(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowPrint(); | |||||
case NO_ACCESSCONTENT: | case NO_ACCESSCONTENT: | ||||
return conf.getEncryptionParameters().isAllowAccessContent(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowAccessContent(); | |||||
case NO_ANNOTATIONS: | case NO_ANNOTATIONS: | ||||
return conf.getEncryptionParameters().isAllowEditAnnotations(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowEditAnnotations(); | |||||
case NO_ASSEMBLEDOC: | case NO_ASSEMBLEDOC: | ||||
return conf.getEncryptionParameters().isAllowAssembleDocument(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowAssembleDocument(); | |||||
case NO_COPY_CONTENT: | case NO_COPY_CONTENT: | ||||
return conf.getEncryptionParameters().isAllowCopyContent(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowCopyContent(); | |||||
case NO_EDIT_CONTENT: | case NO_EDIT_CONTENT: | ||||
return conf.getEncryptionParameters().isAllowEditContent(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowEditContent(); | |||||
case NO_FILLINFORMS: | case NO_FILLINFORMS: | ||||
return conf.getEncryptionParameters().isAllowFillInForms(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowFillInForms(); | |||||
case NO_PRINTHQ: | case NO_PRINTHQ: | ||||
return conf.getEncryptionParameters().isAllowPrintHq(); | |||||
return conf.getConfigOptions().getEncryptionParameters().isAllowPrintHq(); | |||||
default: | default: | ||||
throw new IllegalStateException("Wrong parameter given"); | throw new IllegalStateException("Wrong parameter given"); | ||||
} | } | ||||
@Test | @Test | ||||
public void testAllEncryptionRestrictions() throws Exception { | public void testAllEncryptionRestrictions() throws Exception { | ||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_PRINT); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_ACCESSCONTENT); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_ANNOTATIONS); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_ASSEMBLEDOC); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_COPY_CONTENT); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_EDIT_CONTENT); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_FILLINFORMS); | |||||
testRestrictEncryptionParameter(PDFRendererConfigOption.NO_PRINTHQ); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_PRINT); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_ACCESSCONTENT); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_ANNOTATIONS); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_ASSEMBLEDOC); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_COPY_CONTENT); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_EDIT_CONTENT); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_FILLINFORMS); | |||||
testRestrictEncryptionParameter(PDFEncryptionOption.NO_PRINTHQ); | |||||
} | } | ||||
@Test | @Test | ||||
.startEncryptionParams() | .startEncryptionParams() | ||||
.setOwnerPassword(testPassword) | .setOwnerPassword(testPassword) | ||||
.endEncryptionParams()); | .endEncryptionParams()); | ||||
assertEquals(testPassword, conf.getEncryptionParameters().getOwnerPassword()); | |||||
assertEquals(testPassword, conf.getConfigOptions().getEncryptionParameters().getOwnerPassword()); | |||||
} | } | ||||
@Test | @Test | ||||
public void testFilterListDefaultFlate() throws Exception { | public void testFilterListDefaultFlate() throws Exception { | ||||
parseConfig(createRenderer().createFilterList(null, "flate")); | parseConfig(createRenderer().createFilterList(null, "flate")); | ||||
assertEquals("flate", conf.getFilterMap().get("default").get(0)); | |||||
assertEquals("flate", conf.getConfigOptions().getFilterMap().get("default").get(0)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testFilterListDefaultNull() throws Exception { | public void testFilterListDefaultNull() throws Exception { | ||||
parseConfig(createRenderer().createFilterList(null, "null")); | parseConfig(createRenderer().createFilterList(null, "null")); | ||||
assertEquals("null", conf.getFilterMap().get("default").get(0)); | |||||
assertEquals("null", conf.getConfigOptions().getFilterMap().get("default").get(0)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testFilterListImage() throws Exception { | public void testFilterListImage() throws Exception { | ||||
parseConfig(createRenderer().createFilterList("image", "flate", "ascii-85")); | parseConfig(createRenderer().createFilterList("image", "flate", "ascii-85")); | ||||
assertEquals("flate", conf.getFilterMap().get("image").get(0)); | |||||
assertEquals("ascii-85", conf.getFilterMap().get("image").get(1)); | |||||
assertEquals("flate", conf.getConfigOptions().getFilterMap().get("image").get(0)); | |||||
assertEquals("ascii-85", conf.getConfigOptions().getFilterMap().get("image").get(1)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testPDFAMode() throws Exception { | public void testPDFAMode() throws Exception { | ||||
parseConfig(createRenderer().setPDFAMode(PDFAMode.PDFA_1A.getName())); | parseConfig(createRenderer().setPDFAMode(PDFAMode.PDFA_1A.getName())); | ||||
assertEquals(PDFAMode.PDFA_1A, conf.getPDFAMode()); | |||||
assertEquals(PDFAMode.PDFA_1A, conf.getConfigOptions().getPDFAMode()); | |||||
parseConfig(createRenderer().setPDFAMode(PDFAMode.PDFA_1B.getName())); | parseConfig(createRenderer().setPDFAMode(PDFAMode.PDFA_1B.getName())); | ||||
assertEquals(PDFAMode.PDFA_1B, conf.getPDFAMode()); | |||||
assertEquals(PDFAMode.PDFA_1B, conf.getConfigOptions().getPDFAMode()); | |||||
parseConfig(createRenderer().setPDFAMode(PDFAMode.DISABLED.getName())); | parseConfig(createRenderer().setPDFAMode(PDFAMode.DISABLED.getName())); | ||||
assertEquals(null, conf.getPDFAMode()); | |||||
assertEquals(null, conf.getConfigOptions().getPDFAMode()); | |||||
} | } | ||||
@Test | @Test | ||||
public void testPDFXMode() throws Exception { | public void testPDFXMode() throws Exception { | ||||
parseConfig(createRenderer().setPDFXMode(PDFXMode.PDFX_3_2003.getName())); | parseConfig(createRenderer().setPDFXMode(PDFXMode.PDFX_3_2003.getName())); | ||||
assertEquals(PDFXMode.PDFX_3_2003, conf.getPDFXMode()); | |||||
assertEquals(PDFXMode.PDFX_3_2003, conf.getConfigOptions().getPDFXMode()); | |||||
parseConfig(createRenderer().setPDFXMode(PDFXMode.DISABLED.getName())); | parseConfig(createRenderer().setPDFXMode(PDFXMode.DISABLED.getName())); | ||||
assertEquals(null, conf.getPDFXMode()); | |||||
assertEquals(null, conf.getConfigOptions().getPDFXMode()); | |||||
} | } | ||||
@Test | @Test | ||||
.startEncryptionParams() | .startEncryptionParams() | ||||
.setEncryptionLength(i) | .setEncryptionLength(i) | ||||
.endEncryptionParams()); | .endEncryptionParams()); | ||||
assertEquals(40, conf.getEncryptionParameters().getEncryptionLengthInBits()); | |||||
assertEquals(40, conf.getConfigOptions().getEncryptionParameters().getEncryptionLengthInBits()); | |||||
} | } | ||||
for (int i = 40; i <= 128; i++) { | for (int i = 40; i <= 128; i++) { | ||||
.setEncryptionLength(i) | .setEncryptionLength(i) | ||||
.endEncryptionParams()); | .endEncryptionParams()); | ||||
int expectedLen = Math.round(i / 8.0f) * 8; | int expectedLen = Math.round(i / 8.0f) * 8; | ||||
assertEquals(expectedLen, conf.getEncryptionParameters() | |||||
assertEquals(expectedLen, conf.getConfigOptions().getEncryptionParameters() | |||||
.getEncryptionLengthInBits()); | .getEncryptionLengthInBits()); | ||||
} | } | ||||
.startEncryptionParams() | .startEncryptionParams() | ||||
.setEncryptionLength(i) | .setEncryptionLength(i) | ||||
.endEncryptionParams()); | .endEncryptionParams()); | ||||
assertEquals(128, conf.getEncryptionParameters().getEncryptionLengthInBits()); | |||||
assertEquals(128, conf.getConfigOptions().getEncryptionParameters().getEncryptionLengthInBits()); | |||||
} | } | ||||
} | } | ||||
private void pdfVersionTester(String version) throws Exception { | private void pdfVersionTester(String version) throws Exception { | ||||
parseConfig(createRenderer().setPDFVersion(version)); | parseConfig(createRenderer().setPDFVersion(version)); | ||||
assertEquals(Version.getValueOf(version), conf.getPDFVersion()); | |||||
assertEquals(Version.getValueOf(version), conf.getConfigOptions().getPDFVersion()); | |||||
} | } | ||||
@Test(expected = IllegalArgumentException.class) | @Test(expected = IllegalArgumentException.class) | ||||
pdfVersionTester("0.9"); | pdfVersionTester("0.9"); | ||||
} | } | ||||
@Test(expected = FOPException.class) | |||||
@Test(expected = IllegalArgumentException.class) | |||||
public void testErroneousPDFVersionsNotSet() throws Exception { | public void testErroneousPDFVersionsNotSet() throws Exception { | ||||
pdfVersionTester(""); | pdfVersionTester(""); | ||||
} | } |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.pdf; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.OutputStream; | |||||
import java.net.URI; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import org.junit.Test; | |||||
import org.xml.sax.SAXException; | |||||
import org.apache.xmlgraphics.util.MimeConstants; | |||||
import org.apache.fop.apps.AbstractRendererConfiguratorTest; | |||||
import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.PDFRendererConfBuilder; | |||||
import org.apache.fop.events.Event; | |||||
import org.apache.fop.events.EventListener; | |||||
import org.apache.fop.pdf.PDFAMode; | |||||
import org.apache.fop.pdf.PDFEncryptionParams; | |||||
import org.apache.fop.pdf.PDFXMode; | |||||
import org.apache.fop.pdf.Version; | |||||
import org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser; | |||||
import static org.junit.Assert.assertEquals; | |||||
import static org.junit.Assert.assertTrue; | |||||
import static org.mockito.Mockito.mock; | |||||
import static org.mockito.Mockito.times; | |||||
import static org.mockito.Mockito.verify; | |||||
import static org.mockito.Mockito.when; | |||||
/** | |||||
* Tests that encryption length is properly set up. | |||||
*/ | |||||
public class PDFRendererConfiguratorTestCase extends | |||||
AbstractRendererConfiguratorTest<PDFRendererConfigurator, PDFRendererConfBuilder> { | |||||
private boolean eventTriggered; | |||||
private PDFRenderingUtil pdfUtil; | |||||
public PDFRendererConfiguratorTestCase() { | |||||
super(MimeConstants.MIME_PDF, PDFRendererConfBuilder.class, PDFDocumentHandler.class); | |||||
} | |||||
@Override | |||||
protected PDFRendererConfigurator createConfigurator() { | |||||
return new PDFRendererConfigurator(userAgent, new PDFRendererConfigParser()); | |||||
} | |||||
@Override | |||||
public void setUpDocumentHandler() { | |||||
pdfUtil = new PDFRenderingUtil(userAgent); | |||||
when(((PDFDocumentHandler) docHandler).getPDFUtil()).thenReturn(pdfUtil); | |||||
} | |||||
private void parseConfig(RendererConfBuilder builder, EventListener listener) | |||||
throws SAXException, IOException { | |||||
parseConfigWithUtil(builder, listener, false); | |||||
} | |||||
private void parseConfigWithUtil(RendererConfBuilder builder, EventListener listener, | |||||
boolean mockUtil) throws SAXException, IOException { | |||||
userAgent = FopFactory.newInstance( | |||||
new File(".").toURI(), builder.endRendererConfig().build()).newFOUserAgent(); | |||||
userAgent.getEventBroadcaster().addEventListener(listener); | |||||
if (mockUtil) { | |||||
this.pdfUtil = mock(PDFRenderingUtil.class); | |||||
when(((PDFDocumentHandler) docHandler).getPDFUtil()).thenReturn(pdfUtil); | |||||
} else { | |||||
setUpDocumentHandler(); | |||||
} | |||||
sut = createConfigurator(); | |||||
sut.configure(docHandler); | |||||
} | |||||
private void parseConfigMockUtil(RendererConfBuilder builder) | |||||
throws SAXException, IOException { | |||||
parseConfigWithUtil(builder, null, true); | |||||
} | |||||
/** | |||||
* Non-multiple of 8 should be rounded. | |||||
* | |||||
* @throws Exception if an error occurs | |||||
*/ | |||||
@Test | |||||
public void testRoundUp() throws Exception { | |||||
testEncryptionAndEvent(55, 56); | |||||
} | |||||
/** | |||||
* Non-multiple of 8 should be rounded. | |||||
* | |||||
* @throws Exception if an error occurs | |||||
*/ | |||||
@Test | |||||
public void testRoundDown() throws Exception { | |||||
testEncryptionAndEvent(67, 64); | |||||
} | |||||
/** | |||||
* Encryption length must be at least 40. | |||||
* | |||||
* @throws Exception if an error occurs | |||||
*/ | |||||
@Test | |||||
public void testBelow40() throws Exception { | |||||
testEncryptionAndEvent(32, 40); | |||||
} | |||||
/** | |||||
* Encryption length must be at most 128. | |||||
* | |||||
* @throws Exception if an error occurs | |||||
*/ | |||||
@Test | |||||
public void testAbove128() throws Exception { | |||||
testEncryptionAndEvent(233, 128); | |||||
} | |||||
/** | |||||
* A correct value must be properly set up. | |||||
* | |||||
* @throws Exception if an error occurs | |||||
*/ | |||||
@Test | |||||
public void testCorrectValue() throws Exception { | |||||
runEncryptionTest(128, 128); | |||||
} | |||||
private void testEncryptionAndEvent(int specifiedEncryptionLength, | |||||
int actualEncryptionLength) throws Exception { | |||||
runEncryptionTest(specifiedEncryptionLength, actualEncryptionLength); | |||||
assertTrue(eventTriggered); | |||||
} | |||||
private void runEncryptionTest(int specifiedEncryptionLength, int actualEncryptionLength) | |||||
throws Exception { | |||||
parseConfig(createBuilder().startEncryptionParams() | |||||
.setEncryptionLength(specifiedEncryptionLength) | |||||
.endEncryptionParams(), | |||||
new EncryptionEventFilter(specifiedEncryptionLength, actualEncryptionLength)); | |||||
thenEncryptionLengthShouldBe(actualEncryptionLength); | |||||
} | |||||
private void thenEncryptionLengthShouldBe(int expectedEncryptionLength) { | |||||
PDFEncryptionParams encryptionParams = pdfUtil.getEncryptionParams(); | |||||
assertEquals(expectedEncryptionLength, encryptionParams.getEncryptionLengthInBits()); | |||||
} | |||||
private class EncryptionEventFilter implements EventListener { | |||||
private final int specifiedEncryptionLength; | |||||
private final int correctedEncryptionLength; | |||||
EncryptionEventFilter(int specifiedEncryptionLength, int correctedEncryptionLength) { | |||||
this.specifiedEncryptionLength = specifiedEncryptionLength; | |||||
this.correctedEncryptionLength = correctedEncryptionLength; | |||||
} | |||||
public void processEvent(Event event) { | |||||
assertEquals(PDFEventProducer.class.getName() + ".incorrectEncryptionLength", | |||||
event.getEventID()); | |||||
assertEquals(specifiedEncryptionLength, event.getParam("originalValue")); | |||||
assertEquals(correctedEncryptionLength, event.getParam("correctedValue")); | |||||
eventTriggered = true; | |||||
} | |||||
} | |||||
@Test | |||||
public void testFilterMaps() throws Exception { | |||||
parseConfig(createBuilder().createFilterList("image", "flate", "ascii-85")); | |||||
OutputStream outStream = mock(OutputStream.class); | |||||
Map<String, List<String>> filterMap = pdfUtil.setupPDFDocument(outStream).getFilterMap(); | |||||
assertEquals("flate", filterMap.get("image").get(0)); | |||||
assertEquals("ascii-85", filterMap.get("image").get(1)); | |||||
} | |||||
@Test | |||||
public void testPDFAMode() throws Exception { | |||||
parseConfigMockUtil(createBuilder().setPDFAMode(PDFAMode.DISABLED.getName())); | |||||
// DISABLED is the default setting, it doesn't need to be set | |||||
verify(pdfUtil, times(0)).setAMode(PDFAMode.DISABLED); | |||||
parseConfigMockUtil(createBuilder().setPDFAMode(PDFAMode.PDFA_1A.getName())); | |||||
verify(pdfUtil, times(1)).setAMode(PDFAMode.PDFA_1A); | |||||
parseConfigMockUtil(createBuilder().setPDFAMode(PDFAMode.PDFA_1B.getName())); | |||||
verify(pdfUtil, times(1)).setAMode(PDFAMode.PDFA_1B); | |||||
} | |||||
@Test | |||||
public void testPDFXMode() throws Exception { | |||||
parseConfigMockUtil(createBuilder().setPDFXMode(PDFXMode.DISABLED.getName())); | |||||
// DISABLED is the default setting, it doesn't need to be set | |||||
verify(pdfUtil, times(0)).setXMode(PDFXMode.DISABLED); | |||||
parseConfigMockUtil(createBuilder().setPDFXMode(PDFXMode.PDFX_3_2003.getName())); | |||||
verify(pdfUtil, times(1)).setXMode(PDFXMode.PDFX_3_2003); | |||||
} | |||||
@Test | |||||
public void testSetProfile() throws Exception { | |||||
String testString = "this.uri.is.purely.for.testing.and.has.no.contextual.meaning"; | |||||
parseConfigMockUtil(createBuilder().setOutputProfile(testString)); | |||||
verify(pdfUtil).setOutputProfileURI(URI.create(testString)); | |||||
} | |||||
@Test | |||||
public void testDisableSRGBColourspace() throws Exception { | |||||
parseConfigMockUtil(createBuilder().disableSRGBColorSpace(true)); | |||||
verify(pdfUtil).setDisableSRGBColorSpace(true); | |||||
parseConfigMockUtil(createBuilder().disableSRGBColorSpace(false)); | |||||
verify(pdfUtil, times(0)).setDisableSRGBColorSpace(false); | |||||
} | |||||
@Test | |||||
public void testPDFVersion() throws Exception { | |||||
for (Version version : Version.values()) { | |||||
parseConfigMockUtil(createBuilder().setPDFVersion(version.toString())); | |||||
verify(pdfUtil).setPDFVersion(version); | |||||
} | |||||
} | |||||
} |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.render.pdf; | |||||
import java.net.URI; | |||||
import java.net.URISyntaxException; | |||||
import java.util.Collections; | |||||
import java.util.EnumMap; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import org.junit.Test; | |||||
import org.apache.fop.pdf.PDFAMode; | |||||
import org.apache.fop.pdf.PDFEncryptionParams; | |||||
import org.apache.fop.pdf.PDFXMode; | |||||
import org.apache.fop.pdf.Version; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE; | |||||
import static org.apache.fop.render.pdf.PDFRendererOption.VERSION; | |||||
import static org.junit.Assert.assertEquals; | |||||
public class PDFRendererOptionsConfigTestCase { | |||||
private static final Map<PDFRendererOption, Object> DEFAULT_OPTIONS; | |||||
private static final PDFEncryptionParams DEFAULT_ENCRYPTION_PARAMS = null; | |||||
static { | |||||
final EnumMap<PDFRendererOption, Object> props | |||||
= new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); | |||||
for(PDFRendererOption option : PDFRendererOption.values()) { | |||||
props.put(option, option.getDefaultValue()); | |||||
} | |||||
DEFAULT_OPTIONS = Collections.unmodifiableMap(props); | |||||
} | |||||
@Test | |||||
public void testDefaults() { | |||||
assertConfigEquals(DEFAULT_OPTIONS, DEFAULT_ENCRYPTION_PARAMS, PDFRendererOptionsConfig.DEFAULT); | |||||
} | |||||
@Test | |||||
public void testConstructor() throws URISyntaxException { | |||||
final Map<PDFRendererOption, Object> options = createNonDefaultOptions(); | |||||
final PDFEncryptionParams encryptionParams = new PDFEncryptionParams(); | |||||
assertConfigEquals(options, encryptionParams, new PDFRendererOptionsConfig(options, encryptionParams)); | |||||
} | |||||
@Test | |||||
public void testMerge() throws URISyntaxException { | |||||
final PDFRendererOptionsConfig defaults = PDFRendererOptionsConfig.DEFAULT; | |||||
final PDFEncryptionParams encryptionParams = new PDFEncryptionParams(); | |||||
final Map<PDFRendererOption, Object> options = createNonDefaultOptions(); | |||||
final PDFRendererOptionsConfig otherConfig = new PDFRendererOptionsConfig(options, encryptionParams); | |||||
assertConfigEquals(options, encryptionParams, defaults.merge(otherConfig)); | |||||
assertEquals(defaults, defaults.merge(null)); | |||||
} | |||||
private Map<PDFRendererOption, Object> createNonDefaultOptions() throws URISyntaxException { | |||||
final EnumMap<PDFRendererOption, Object> options | |||||
= new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class); | |||||
options.put(DISABLE_SRGB_COLORSPACE, Boolean.TRUE); | |||||
options.put(FILTER_LIST, new HashMap<String, List<String>>()); | |||||
options.put(OUTPUT_PROFILE, new URI("xxx")); | |||||
options.put(PDF_A_MODE, PDFAMode.PDFA_1A); | |||||
options.put(PDF_X_MODE, PDFXMode.PDFX_3_2003); | |||||
options.put(VERSION, Version.V1_0); | |||||
return Collections.unmodifiableMap(options); | |||||
} | |||||
private static void assertConfigEquals(Map<PDFRendererOption, Object> expectedOptions, | |||||
PDFEncryptionParams expectedEncryptionParams, PDFRendererOptionsConfig actual) { | |||||
assertEquals((Boolean) expectedOptions.get(DISABLE_SRGB_COLORSPACE), actual.getDisableSRGBColorSpace()); | |||||
assertEquals((Map) expectedOptions.get(FILTER_LIST), actual.getFilterMap()); | |||||
assertEquals((URI) expectedOptions.get(OUTPUT_PROFILE), actual.getOutputProfileURI()); | |||||
assertEquals((PDFAMode) expectedOptions.get(PDF_A_MODE), actual.getPDFAMode()); | |||||
assertEquals((PDFXMode) expectedOptions.get(PDF_X_MODE), actual.getPDFXMode()); | |||||
assertEquals((Version) expectedOptions.get(VERSION), actual.getPDFVersion()); | |||||
assertEncryptionParamsEquals(expectedEncryptionParams, actual.getEncryptionParameters()); | |||||
} | |||||
private static void assertEncryptionParamsEquals(PDFEncryptionParams expected, | |||||
PDFEncryptionParams actual) { | |||||
assertEquals(expected == null, actual == null); | |||||
if (actual != null) { | |||||
assertEquals(expected.getEncryptionLengthInBits(), actual.getEncryptionLengthInBits()); | |||||
assertEquals(expected.getOwnerPassword(), actual.getOwnerPassword()); | |||||
assertEquals(expected.getUserPassword(), actual.getUserPassword()); | |||||
assertEquals(expected.isAllowAccessContent(), actual.isAllowAccessContent()); | |||||
assertEquals(expected.isAllowAssembleDocument(), actual.isAllowAssembleDocument()); | |||||
assertEquals(expected.isAllowCopyContent(), actual.isAllowCopyContent()); | |||||
assertEquals(expected.isAllowEditAnnotations(), actual.isAllowEditAnnotations()); | |||||
assertEquals(expected.isAllowEditContent(), actual.isAllowEditContent()); | |||||
assertEquals(expected.isAllowFillInForms(), actual.isAllowFillInForms()); | |||||
assertEquals(expected.isAllowPrint(), actual.isAllowPrint()); | |||||
assertEquals(expected.isAllowPrintHq(), actual.isAllowPrintHq()); | |||||
} | |||||
} | |||||
} |
* A test suite for org.apache.fop.render.pdf.* | * A test suite for org.apache.fop.render.pdf.* | ||||
*/ | */ | ||||
@RunWith(Suite.class) | @RunWith(Suite.class) | ||||
@SuiteClasses({ PDFRendererConfiguratorTestCase.class }) | |||||
@SuiteClasses({ | |||||
PDFRendererConfigParserTestCase.class | |||||
}) | |||||
public final class RenderPDFTestSuite { | public final class RenderPDFTestSuite { | ||||
} | } |
import org.apache.fop.apps.AbstractRendererConfigParserTester; | import org.apache.fop.apps.AbstractRendererConfigParserTester; | ||||
import org.apache.fop.apps.TxtRendererConfBuilder; | import org.apache.fop.apps.TxtRendererConfBuilder; | ||||
import org.apache.fop.render.RendererConfig.RendererConfigParser; | import org.apache.fop.render.RendererConfig.RendererConfigParser; | ||||
import org.apache.fop.render.txt.TxtRendererConfig.TxtRendererConfigOption; | |||||
import org.apache.fop.render.txt.TxtRendererConfig.TxtRendererOption; | |||||
import org.apache.fop.render.txt.TxtRendererConfig.TxtRendererConfigParser; | import org.apache.fop.render.txt.TxtRendererConfig.TxtRendererConfigParser; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
// Check the default behaviour is expected | // Check the default behaviour is expected | ||||
parseConfig(createRenderer()); | parseConfig(createRenderer()); | ||||
assertEquals(TxtRendererConfigOption.ENCODING.getDefaultValue(), conf.getEncoding()); | |||||
assertEquals(TxtRendererOption.ENCODING.getDefaultValue(), conf.getEncoding()); | |||||
} | } | ||||
} | } |
/* | |||||
* Licensed to the Apache Software Foundation (ASF) under one or more | |||||
* contributor license agreements. See the NOTICE file distributed with | |||||
* this work for additional information regarding copyright ownership. | |||||
* The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
* (the "License"); you may not use this file except in compliance with | |||||
* the License. You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.svg; | |||||
import java.awt.BasicStroke; | |||||
import org.junit.Test; | |||||
import static org.junit.Assert.assertTrue; | |||||
public class PDFGraphics2DTestCase { | |||||
@Test | |||||
public void testApplyStrokeNullDash() { | |||||
PDFGraphics2D g2d = new PDFGraphics2D(false); | |||||
BasicStroke stroke = new BasicStroke(); | |||||
g2d.applyStroke(stroke); | |||||
assertTrue(g2d.getString().contains("[] 0 d\n")); | |||||
} | |||||
@Test | |||||
public void testApplyStrokeNonNullDash() { | |||||
PDFGraphics2D g2d = new PDFGraphics2D(false); | |||||
float[] dashArray = {3.0f, 5.0f}; | |||||
BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, | |||||
dashArray, 0.0f); | |||||
g2d.applyStroke(stroke); | |||||
assertTrue(g2d.getString().contains("[3 5] 0 d\n")); | |||||
} | |||||
} |