diff options
author | Mehdi Houshmand <mehdi@apache.org> | 2012-05-31 08:33:36 +0000 |
---|---|---|
committer | Mehdi Houshmand <mehdi@apache.org> | 2012-05-31 08:33:36 +0000 |
commit | eccd73c523bdda6a0634e9849141492f7b14ad63 (patch) | |
tree | 72f06ec1481249bdd639083ee646b3c3fd4be7a4 /src/java/org/apache/fop/render/afp | |
parent | 05761b1df54ada8a762bfa879dc0e3455d33d828 (diff) | |
download | xmlgraphics-fop-eccd73c523bdda6a0634e9849141492f7b14ad63.tar.gz xmlgraphics-fop-eccd73c523bdda6a0634e9849141492f7b14ad63.zip |
Started unifying URI resolution mechanism, redesigned configuration system and created flexible config testing
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_URI_Unification@1344594 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/afp')
7 files changed, 936 insertions, 470 deletions
diff --git a/src/java/org/apache/fop/render/afp/AFPDocumentHandler.java b/src/java/org/apache/fop/render/afp/AFPDocumentHandler.java index 7823a2ce6..cf40b1c6e 100644 --- a/src/java/org/apache/fop/render/afp/AFPDocumentHandler.java +++ b/src/java/org/apache/fop/render/afp/AFPDocumentHandler.java @@ -45,6 +45,7 @@ import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontEventAdapter; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontManager; +import org.apache.fop.render.afp.AFPRendererConfig.AFPRendererConfigParser; import org.apache.fop.render.afp.extensions.AFPElementMapping; import org.apache.fop.render.afp.extensions.AFPIncludeFormMap; import org.apache.fop.render.afp.extensions.AFPInvokeMediumMap; @@ -53,6 +54,8 @@ import org.apache.fop.render.afp.extensions.AFPPageSegmentElement; import org.apache.fop.render.afp.extensions.AFPPageSetup; import org.apache.fop.render.afp.extensions.ExtensionPlacement; import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler; +import org.apache.fop.render.intermediate.IFContext; +import org.apache.fop.render.intermediate.IFDocumentHandler; import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator; import org.apache.fop.render.intermediate.IFException; import org.apache.fop.render.intermediate.IFPainter; @@ -98,7 +101,8 @@ public class AFPDocumentHandler extends AbstractBinaryWritingIFDocumentHandler /** * Default constructor. */ - public AFPDocumentHandler() { + public AFPDocumentHandler(IFContext context) { + super(context); this.resourceManager = new AFPResourceManager(); this.paintingState = new AFPPaintingState(); this.unitConv = paintingState.getUnitConverter(); @@ -116,13 +120,13 @@ public class AFPDocumentHandler extends AbstractBinaryWritingIFDocumentHandler /** {@inheritDoc} */ public IFDocumentHandlerConfigurator getConfigurator() { - return new AFPRendererConfigurator(getUserAgent()); + return new AFPRendererConfigurator(getUserAgent(), new AFPRendererConfigParser()); } /** {@inheritDoc} */ @Override public void setDefaultFontInfo(FontInfo fontInfo) { - FontManager fontManager = getUserAgent().getFactory().getFontManager(); + FontManager fontManager = getUserAgent().getFontManager(); FontCollection[] fontCollections = new FontCollection[] { new AFPFontCollection(getUserAgent().getEventBroadcaster(), null) }; @@ -381,7 +385,7 @@ public class AFPDocumentHandler extends AbstractBinaryWritingIFDocumentHandler } else if (extension instanceof AFPIncludeFormMap) { AFPIncludeFormMap formMap = (AFPIncludeFormMap)extension; ResourceAccessor accessor = new DefaultFOPResourceAccessor( - getUserAgent(), null, null); + getUserAgent().getNewURIResolver()); try { getResourceManager().createIncludedResource(formMap.getName(), formMap.getSrc(), accessor, diff --git a/src/java/org/apache/fop/render/afp/AFPDocumentHandlerMaker.java b/src/java/org/apache/fop/render/afp/AFPDocumentHandlerMaker.java index cbe6d0ca3..f83ede9f7 100644 --- a/src/java/org/apache/fop/render/afp/AFPDocumentHandlerMaker.java +++ b/src/java/org/apache/fop/render/afp/AFPDocumentHandlerMaker.java @@ -19,7 +19,6 @@ package org.apache.fop.render.afp; -import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.MimeConstants; import org.apache.fop.render.intermediate.AbstractIFDocumentHandlerMaker; import org.apache.fop.render.intermediate.IFContext; @@ -36,10 +35,8 @@ public class AFPDocumentHandlerMaker extends AbstractIFDocumentHandlerMaker { }; /** {@inheritDoc} */ - public IFDocumentHandler makeIFDocumentHandler(FOUserAgent ua) { - AFPDocumentHandler handler = new AFPDocumentHandler(); - handler.setContext(new IFContext(ua)); - return handler; + public IFDocumentHandler makeIFDocumentHandler(IFContext ifContext) { + return new AFPDocumentHandler(ifContext); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/afp/AFPFontConfig.java b/src/java/org/apache/fop/render/afp/AFPFontConfig.java new file mode 100644 index 000000000..42c3be54a --- /dev/null +++ b/src/java/org/apache/fop/render/afp/AFPFontConfig.java @@ -0,0 +1,440 @@ +/* + * 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.afp; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.fop.afp.AFPEventProducer; +import org.apache.fop.afp.fonts.AFPFont; +import org.apache.fop.afp.fonts.AFPFontInfo; +import org.apache.fop.afp.fonts.CharacterSet; +import org.apache.fop.afp.fonts.CharacterSetBuilder; +import org.apache.fop.afp.fonts.CharacterSetType; +import org.apache.fop.afp.fonts.DoubleByteFont; +import org.apache.fop.afp.fonts.OutlineFont; +import org.apache.fop.afp.fonts.RasterFont; +import org.apache.fop.afp.util.DefaultFOPResourceAccessor; +import org.apache.fop.afp.util.ResourceAccessor; +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.io.URIResolverWrapper; +import org.apache.fop.events.EventProducer; +import org.apache.fop.fonts.FontConfig; +import org.apache.fop.fonts.FontManager; +import org.apache.fop.fonts.FontManagerConfigurator; +import org.apache.fop.fonts.FontTriplet; +import org.apache.fop.fonts.FontTriplet.Matcher; +import org.apache.fop.fonts.FontUtil; +import org.apache.fop.fonts.Typeface; + +/** + * The config object for AFP fonts, these differ from the the more generic fonts (TTF and Type1). + */ +public final class AFPFontConfig implements FontConfig { + + private static final Log LOG = LogFactory.getLog(AFPFontConfig.class); + + private final List<AFPFontConfigData> fontsConfig; + + private AFPFontConfig() { + fontsConfig = new ArrayList<AFPFontConfigData>(); + } + + /** + * Returns a list of AFP font configuration data. + * @return the AFP font config data + */ + public List<AFPFontConfigData> getFontConfig() { + return fontsConfig; + } + + /** + * The parser for AFP font data. + */ + static final class AFPFontInfoConfigParser implements FontConfigParser { + + /** {@inheritDoc}} */ + public AFPFontConfig parse(Configuration cfg, FontManager fontManager, boolean strict, + EventProducer eventProducer) throws FOPException { + try { + return new ParserHelper(cfg, fontManager, strict, + (AFPEventProducer) eventProducer).fontConfig; + } catch (ConfigurationException ce) { + throw new FOPException(ce); + } + } + + AFPFontConfig getEmptyConfig() { + return new AFPFontConfig(); + } + } + + private static final class AggregateMatcher implements Matcher { + + private final List<Matcher> matchers; + + private AggregateMatcher(Matcher... matchers) { + this.matchers = new ArrayList<Matcher>(); + for (Matcher matcher : matchers) { + if (matcher != null) { + this.matchers.add(matcher); + } + } + } + + public boolean matches(FontTriplet triplet) { + for (Matcher matcher : matchers) { + if (matcher.matches(triplet)) { + return true; + } + } + return false; + } + + } + + private static final class ParserHelper { + + private static final Log LOG = LogFactory.getLog(ParserHelper.class); + + private final AFPFontConfig fontConfig; + + private final Matcher matcher; + + private ParserHelper(Configuration cfg, FontManager fontManager, boolean strict, + AFPEventProducer eventProducer) throws FOPException, ConfigurationException { + Configuration fonts = cfg.getChild("fonts"); + Matcher localMatcher = null; + Configuration referencedFontsCfg = fonts.getChild("referenced-fonts", false); + if (referencedFontsCfg != null) { + localMatcher = FontManagerConfigurator.createFontsMatcher(referencedFontsCfg, strict); + } + matcher = new AggregateMatcher(fontManager.getReferencedFontsMatcher(), localMatcher); + fontConfig = new AFPFontConfig(); + for (Configuration font : fonts.getChildren("font")) { + buildFont(font, eventProducer); + } + } + + private void buildFont(Configuration fontCfg, AFPEventProducer eventProducer) + throws ConfigurationException { + //FontManager fontManager = this.userAgent.getFontManager(); + Configuration[] triplets = fontCfg.getChildren("font-triplet"); + List<FontTriplet> tripletList = new ArrayList<FontTriplet>(); + if (triplets.length == 0) { + eventProducer.fontConfigMissing(this, "<font-triplet...", fontCfg.getLocation()); + return; + } + for (Configuration triplet : triplets) { + int weight = FontUtil.parseCSS2FontWeight(triplet.getAttribute("weight")); + FontTriplet fontTriplet = new FontTriplet(triplet.getAttribute("name"), + triplet.getAttribute("style"), weight); + tripletList.add(fontTriplet); + } + //build the fonts + Configuration[] config = fontCfg.getChildren("afp-font"); + if (config.length == 0) { + eventProducer.fontConfigMissing(this, "<afp-font...", fontCfg.getLocation()); + return; + } + Configuration afpFontCfg = config[0]; + String uri = afpFontCfg.getAttribute("base-uri", null); + try { + String type = afpFontCfg.getAttribute("type"); + if (type == null) { + eventProducer.fontConfigMissing(this, "type attribute", fontCfg.getLocation()); + return; + } + String codepage = afpFontCfg.getAttribute("codepage"); + if (codepage == null) { + eventProducer.fontConfigMissing(this, "codepage attribute", + fontCfg.getLocation()); + return; + } + String encoding = afpFontCfg.getAttribute("encoding"); + if (encoding == null) { + eventProducer.fontConfigMissing(this, "encoding attribute", + fontCfg.getLocation()); + return; + } + + fontFromType(tripletList, type, codepage, encoding, afpFontCfg, eventProducer, uri); + } catch (ConfigurationException ce) { + eventProducer.invalidConfiguration(this, ce); + } + } + + private void fontFromType(List<FontTriplet> fontTriplets, String type, String codepage, + String encoding, Configuration cfg, AFPEventProducer eventProducer, String embedURI) + throws ConfigurationException { + AFPFontConfigData config = null; + if ("raster".equalsIgnoreCase(type)) { + config = getRasterFont(fontTriplets, type, codepage, encoding, cfg, eventProducer, + embedURI); + } else if ("outline".equalsIgnoreCase(type)) { + config = getOutlineFont(fontTriplets, type, codepage, encoding, cfg, eventProducer, + embedURI); + } else if ("CIDKeyed".equalsIgnoreCase(type)) { + config = getCIDKeyedFont(fontTriplets, type, codepage, encoding, cfg, + eventProducer, + embedURI); + } else { + LOG.error("No or incorrect type attribute: " + type); + } + if (config != null) { + fontConfig.fontsConfig.add(config); + } + } + + private CIDKeyedFontConfig getCIDKeyedFont(List<FontTriplet> fontTriplets, String type, + String codepage, String encoding, Configuration cfg, AFPEventProducer eventProducer, + String uri) throws ConfigurationException { + String characterset = cfg.getAttribute("characterset"); + if (characterset == null) { + eventProducer.fontConfigMissing(this, "characterset attribute", + cfg.getLocation()); + return null; + } + String name = cfg.getAttribute("name", characterset); + CharacterSetType charsetType = cfg.getAttributeAsBoolean("ebcdic-dbcs", false) + ? CharacterSetType.DOUBLE_BYTE_LINE_DATA : CharacterSetType.DOUBLE_BYTE; + return new CIDKeyedFontConfig(fontTriplets, type, codepage, encoding, characterset, + name, charsetType, isEmbbedable(fontTriplets), uri); + } + + private OutlineFontConfig getOutlineFont(List<FontTriplet> fontTriplets, String type, + String codepage, String encoding, Configuration cfg, + AFPEventProducer eventProducer, String uri) throws ConfigurationException { + String characterset = cfg.getAttribute("characterset"); + if (characterset == null) { + eventProducer.fontConfigMissing(this, "characterset attribute", + cfg.getLocation()); + return null; + } + String name = cfg.getAttribute("name", characterset); + String base14 = cfg.getAttribute("base14-font", null); + return new OutlineFontConfig(fontTriplets, type, codepage, encoding, characterset, + name, base14, isEmbbedable(fontTriplets), uri); + } + + private RasterFontConfig getRasterFont(List<FontTriplet> triplets, String type, + String codepage, String encoding, Configuration cfg, + AFPEventProducer eventProducer, String uri) + throws ConfigurationException { + String name = cfg.getAttribute("name", "Unknown"); + // Create a new font object + Configuration[] rasters = cfg.getChildren("afp-raster-font"); + if (rasters.length == 0) { + eventProducer.fontConfigMissing(this, "<afp-raster-font...", + cfg.getLocation()); + return null; + } + List<RasterCharactersetData> charsetData = new ArrayList<RasterCharactersetData>(); + for (Configuration rasterCfg : rasters) { + String characterset = rasterCfg.getAttribute("characterset"); + if (characterset == null) { + eventProducer.fontConfigMissing(this, "characterset attribute", + cfg.getLocation()); + return null; + } + float size = rasterCfg.getAttributeAsFloat("size"); + int sizeMpt = (int) (size * 1000); + String base14 = rasterCfg.getAttribute("base14-font", null); + charsetData.add(new RasterCharactersetData(characterset, sizeMpt, base14)); + } + return new RasterFontConfig(triplets, type, codepage, encoding, null, name, uri, charsetData, + isEmbbedable(triplets)); + } + + private boolean isEmbbedable(List<FontTriplet> triplets) { + for (FontTriplet triplet : triplets) { + if (matcher.matches(triplet)) { + return false; + } + } + return true; + } + + } + + abstract static class AFPFontConfigData { + private final List<FontTriplet> triplets; + private final String codePage; + private final String encoding; + private final String name; + private final boolean embeddable; + private final String uri; + + AFPFontConfigData(List<FontTriplet> triplets, String type, String codePage, + String encoding, String name, boolean embeddable, String uri) { + this.triplets = Collections.unmodifiableList(triplets); + this.codePage = codePage; + this.encoding = encoding; + this.name = name; + this.embeddable = embeddable; + this.uri = uri; + } + + static AFPFontInfo getFontInfo(AFPFont font, AFPFontConfigData config) { + return font != null ? new AFPFontInfo(font, config.triplets) : null; + } + + abstract AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) + throws IOException; + + ResourceAccessor getAccessor(URIResolverWrapper resolver) { + return new DefaultFOPResourceAccessor(resolver, uri); + } + } + + static final class CIDKeyedFontConfig extends AFPFontConfigData { + + private final CharacterSetType charsetType; + + private final String characterset; + + private CIDKeyedFontConfig(List<FontTriplet> triplets, String type, String codePage, + String encoding, String characterset, String name, CharacterSetType charsetType, boolean embeddable, String uri) { + super(triplets, type, codePage, encoding, name, embeddable, uri); + this.characterset = characterset; + this.charsetType = charsetType; + } + + @Override + AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) + throws IOException { + ResourceAccessor accessor = getAccessor(resolver); + CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS( + characterset, super.codePage, super.encoding, charsetType, accessor, eventProducer); + return getFontInfo(new DoubleByteFont(super.codePage, super.embeddable, characterSet), + this); + } + } + + static final class OutlineFontConfig extends AFPFontConfigData { + private final String base14; + private final String characterset; + + private OutlineFontConfig(List<FontTriplet> triplets, String type, String codePage, + String encoding, String characterset, String name, String base14, boolean embeddable, String uri) { + super(triplets, type, codePage, encoding, name, embeddable, uri); + this.characterset = characterset; + this.base14 = base14; + } + + @Override + AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) + throws IOException { + CharacterSet characterSet = null; + if (base14 != null) { + try { + Class<? extends Typeface> clazz = Class.forName( + "org.apache.fop.fonts.base14." + base14).asSubclass(Typeface.class); + try { + Typeface tf = clazz.newInstance(); + characterSet = CharacterSetBuilder.getSingleByteInstance() + .build(characterset, super.codePage, + super.encoding, + tf, eventProducer); + } catch (Exception ie) { + String msg = "The base 14 font class " + clazz.getName() + + " could not be instantiated"; + LOG.error(msg); + } + } catch (ClassNotFoundException cnfe) { + String msg = "The base 14 font class for " + characterset + + " could not be found"; + LOG.error(msg); + } + } else { + ResourceAccessor accessor = getAccessor(resolver); + characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS( + characterset, super.codePage, super.encoding, accessor, eventProducer); + } + return getFontInfo(new OutlineFont(super.name, super.embeddable, characterSet), this); + } + } + + static final class RasterFontConfig extends AFPFontConfigData { + private final List<RasterCharactersetData> charsets; + + private RasterFontConfig(List<FontTriplet> triplets, String type, String codePage, + String encoding, String characterset, String name, String uri, + List<RasterCharactersetData> csetData, boolean embeddable) { + super(triplets, type, codePage, encoding, name, embeddable, uri); + this.charsets = Collections.unmodifiableList(csetData); + } + + @Override + AFPFontInfo getFontInfo(URIResolverWrapper resolver, AFPEventProducer eventProducer) + throws IOException { + RasterFont rasterFont = new RasterFont(super.name, super.embeddable); + for (RasterCharactersetData charset : charsets) { + if (charset.base14 != null) { + try { + Class<? extends Typeface> clazz = Class.forName( + "org.apache.fop.fonts.base14." + charset.base14).asSubclass( + Typeface.class); + try { + Typeface tf = clazz.newInstance(); + rasterFont.addCharacterSet(charset.size, + CharacterSetBuilder.getSingleByteInstance().build( + charset.characterset, super.codePage, super.encoding, + tf, eventProducer)); + } catch (Exception ie) { + String msg = "The base 14 font class " + clazz.getName() + + " could not be instantiated"; + LOG.error(msg); + } + } catch (ClassNotFoundException cnfe) { + String msg = "The base 14 font class for " + charset.characterset + + " could not be found"; + LOG.error(msg); + } + } else { + ResourceAccessor accessor = getAccessor(resolver); + rasterFont.addCharacterSet(charset.size, + CharacterSetBuilder.getSingleByteInstance().buildSBCS(charset.characterset, + super.codePage, super.encoding, accessor, eventProducer)); + } + } + return getFontInfo(rasterFont, this); + } + } + + static final class RasterCharactersetData { + private final String characterset; + private final int size; + private final String base14; + + private RasterCharactersetData(String characterset, int size, String base14) { + this.characterset = characterset; + this.size = size; + this.base14 = base14; + } + } +} diff --git a/src/java/org/apache/fop/render/afp/AFPPainter.java b/src/java/org/apache/fop/render/afp/AFPPainter.java index 8b2f31555..1f60ce440 100644 --- a/src/java/org/apache/fop/render/afp/AFPPainter.java +++ b/src/java/org/apache/fop/render/afp/AFPPainter.java @@ -206,7 +206,7 @@ public class AFPPainter extends AbstractIFPainter { //Do we need to embed an external page segment? if (pageSegment.getURI() != null) { ResourceAccessor accessor = new DefaultFOPResourceAccessor ( - documentHandler.getUserAgent(), null, null); + documentHandler.getUserAgent().getNewURIResolver()); try { URI resourceUri = new URI(pageSegment.getURI()); documentHandler.getResourceManager().createIncludedResourceFromExternal( diff --git a/src/java/org/apache/fop/render/afp/AFPRendererConfig.java b/src/java/org/apache/fop/render/afp/AFPRendererConfig.java new file mode 100644 index 000000000..b7331c5c5 --- /dev/null +++ b/src/java/org/apache/fop/render/afp/AFPRendererConfig.java @@ -0,0 +1,407 @@ +/* + * 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.afp; + +import java.io.File; +import java.io.IOException; +import java.util.EnumMap; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.fop.afp.AFPConstants; +import org.apache.fop.afp.AFPDataObjectInfo; +import org.apache.fop.afp.AFPEventProducer; +import org.apache.fop.afp.AFPResourceLevel; +import org.apache.fop.afp.AFPResourceLevelDefaults; +import org.apache.fop.afp.modca.triplets.MappingOptionTriplet; +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.MimeConstants; +import org.apache.fop.fonts.FontManager; +import org.apache.fop.render.RendererConfig; +import org.apache.fop.render.afp.AFPFontConfig.AFPFontInfoConfigParser; +import org.apache.fop.util.LogUtil; + +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.Options.DEFAULT_RESOURCE_LEVELS; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.GOCA; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.GOCA_TEXT; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_DITHERING_QUALITY; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_FS45; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_JPEG; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_MAPPING_OPTION; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_MODE; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_NATIVE; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.IMAGES_WRAP_PSEG; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.JPEG_ALLOW_JPEG_EMBEDDING; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.JPEG_BITMAP_ENCODING_QUALITY; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.LINE_WIDTH_CORRECTION; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.RENDERER_RESOLUTION; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.RESOURCE_GROUP_FILE; +import static org.apache.fop.render.afp.AFPRendererConfig.Options.SHADING; + +public final class AFPRendererConfig implements RendererConfig { + + public enum ImagesModeOptions { + + MODE_GRAYSCALE("b+w", "bits-per-pixel"), + MODE_COLOR("color", "cmyk"); + + private final String name; + + private final String modeAttribute; + + private ImagesModeOptions(String name, String modeAttribute) { + this.name = name; + this.modeAttribute = modeAttribute; + } + + public String getName() { + return name; + } + + public String getModeAttribute() { + return modeAttribute; + } + + public static ImagesModeOptions forName(String name) { + for (ImagesModeOptions option : values()) { + if (option.name.equals(name)) { + return option; + } + } + throw new IllegalArgumentException(name); + } + } + + public enum Options { + + DEFAULT_RESOURCE_LEVELS("default-resource-levels", AFPResourceLevelDefaults.class), + IMAGES("images", null), + IMAGES_JPEG("jpeg", null), + IMAGES_DITHERING_QUALITY("dithering-quality", Float.class), + IMAGES_FS45("fs45", Boolean.class), + IMAGES_MAPPING_OPTION("mapping_option", Byte.class), + IMAGES_MODE("mode", Boolean.class), + IMAGES_NATIVE("native", Boolean.class), + IMAGES_WRAP_PSEG("pseg", Boolean.class), + JPEG_ALLOW_JPEG_EMBEDDING("allow-embedding", Boolean.class), + JPEG_BITMAP_ENCODING_QUALITY("bitmap-encoding-quality", Float.class), + RENDERER_RESOLUTION("renderer-resolution", Integer.class), + RESOURCE_GROUP_FILE("resource-group-file", String.class), + SHADING("shading", AFPShadingMode.class), + LINE_WIDTH_CORRECTION("line-width-correction", Float.class), + GOCA("goca", Boolean.class), + GOCA_TEXT("text", Boolean.class); + + private final String name; + + private final Class<?> type; + + private Options(String name, Class<?> type) { + this.name = name; + this.type = type; + } + + public String getName() { + return name; + } + } + + private final EnumMap<Options, Object> params = new EnumMap<Options, Object>(Options.class); + + private final EnumMap<ImagesModeOptions, Object> imageModeParams + = new EnumMap<ImagesModeOptions, Object>(ImagesModeOptions.class); + + private final AFPFontConfig fontConfig; + + private AFPRendererConfig(AFPFontConfig fontConfig) { + this.fontConfig = fontConfig; + } + + public AFPFontConfig getFontInfoConfig() { + return fontConfig; + } + + public Boolean isColorImages() { + return getParam(IMAGES_MODE, Boolean.class); + } + + public Boolean isCmykImagesSupported() { + if (!isColorImages()) { + throw new IllegalStateException(); + } + return Boolean.class.cast(imageModeParams.get(MODE_COLOR)); + } + + public Integer getBitsPerPixel() { + if (isColorImages()) { + throw new IllegalStateException(); + } + return Integer.class.cast(imageModeParams.get(MODE_GRAYSCALE)); + } + + public Float getDitheringQuality() { + return getParam(IMAGES_DITHERING_QUALITY, Float.class); + } + + public Boolean isNativeImagesSupported() { + return getParam(IMAGES_NATIVE, Boolean.class); + } + + public AFPShadingMode getShadingMode() { + return getParam(SHADING, AFPShadingMode.class); + } + + public Integer getResolution() { + return getParam(RENDERER_RESOLUTION, Integer.class); + } + + public String getDefaultResourceGroupFilePath() { + return getParam(RESOURCE_GROUP_FILE, String.class); + } + + public AFPResourceLevelDefaults getResourceLevelDefaults() { + return getParam(DEFAULT_RESOURCE_LEVELS, AFPResourceLevelDefaults.class); + } + + public Boolean isWrapPseg() { + return getParam(IMAGES_WRAP_PSEG, Boolean.class); + } + + public Boolean isFs45() { + return getParam(IMAGES_FS45, Boolean.class); + } + + public Boolean allowJpegEmbedding() { + return getParam(JPEG_ALLOW_JPEG_EMBEDDING, Boolean.class); + } + + public Float getBitmapEncodingQuality() { + return getParam(JPEG_BITMAP_ENCODING_QUALITY, Float.class); + } + + public Float getLineWidthCorrection() { + return getParam(LINE_WIDTH_CORRECTION, Float.class); + } + + public Boolean isGocaEnabled() { + return getParam(GOCA, Boolean.class); + } + + public Boolean isStrokeGocaText() { + return getParam(GOCA_TEXT, Boolean.class); + } + + private <T> T getParam(Options options, Class<T> type) { + assert options.type.equals(type); + return type.cast(params.get(options)); + } + + private <T> void setParam(Options option, T value) { + assert option.type.isInstance(value); + params.put(option, value); + } + + public static final class AFPRendererConfigParser implements RendererConfigParser { + + private static final Log LOG = LogFactory.getLog(AFPRendererConfigParser.class); + + public AFPRendererConfig build(FOUserAgent userAgent, Configuration cfg) throws FOPException { + boolean strict = userAgent != null ? userAgent.validateUserConfigStrictly() : false; + AFPRendererConfig config = null; + AFPEventProducer eventProducer = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster()); + try { + config = new ParserHelper(cfg, userAgent.getFontManager(), strict, eventProducer).config; + } catch (ConfigurationException e) { + LogUtil.handleException(LOG, e, strict); + } + return config; + } + + public String getMimeType() { + return MimeConstants.MIME_AFP; + } + } + + + private static final class ParserHelper { + + private static final Log LOG = LogFactory.getLog(ParserHelper.class); + + private final AFPRendererConfig config; + + private final boolean strict; + + private final Configuration cfg; + + private ParserHelper(Configuration cfg, FontManager fontManager, boolean strict, + AFPEventProducer eventProducer) + throws ConfigurationException, FOPException { + this.cfg = cfg; + this.strict = strict; + if (cfg != null) { + config = new AFPRendererConfig(new AFPFontInfoConfigParser().parse(cfg, + fontManager, strict, eventProducer)); + configure(); + } else { + config = new AFPRendererConfig(new AFPFontInfoConfigParser().getEmptyConfig()); + } + } + + private void configure() throws ConfigurationException, FOPException { + configureImages(); + setParam(SHADING, AFPShadingMode.valueOf( + cfg.getChild(SHADING.getName()).getValue(AFPShadingMode.COLOR.getName()))); + Configuration rendererResolutionCfg = cfg.getChild(RENDERER_RESOLUTION.getName(), false); + setParam(RENDERER_RESOLUTION, rendererResolutionCfg == null ? 240 + : rendererResolutionCfg.getValueAsInteger(240)); + Configuration lineWidthCorrectionCfg = cfg.getChild(LINE_WIDTH_CORRECTION.getName(), + false); + setParam(LINE_WIDTH_CORRECTION, lineWidthCorrectionCfg != null + ? lineWidthCorrectionCfg.getValueAsFloat() + : AFPConstants.LINE_WIDTH_CORRECTION); + Configuration gocaCfg = cfg.getChild(GOCA.getName()); + boolean gocaEnabled = gocaCfg.getAttributeAsBoolean("enabled", true); + setParam(GOCA, gocaEnabled); + String strokeGocaText = gocaCfg.getAttribute(GOCA_TEXT.getName(), "default"); + setParam(GOCA_TEXT, "stroke".equalsIgnoreCase(strokeGocaText) + || "shapes".equalsIgnoreCase(strokeGocaText)); + //TODO remove + createResourceGroupFile(); + createResourceLevel(); + } + + private void setParam(Options option, Object value) { + config.setParam(option, value); + } + + private void configureImages() throws ConfigurationException, FOPException { + Configuration imagesCfg = cfg.getChild(IMAGES.getName()); + ImagesModeOptions imagesMode = ImagesModeOptions.forName(imagesCfg.getAttribute( + IMAGES_MODE.getName(), MODE_GRAYSCALE.getName())); + boolean colorImages = MODE_COLOR == imagesMode; + setParam(IMAGES_MODE, colorImages); + if (colorImages) { + config.imageModeParams.put(MODE_COLOR, imagesCfg + .getAttributeAsBoolean(imagesMode.getModeAttribute(), false)); + } else { + config.imageModeParams.put(MODE_GRAYSCALE, + imagesCfg.getAttributeAsInteger(imagesMode.getModeAttribute(), 8)); + } + String dithering = imagesCfg.getAttribute(Options.IMAGES_DITHERING_QUALITY.getName(), "medium"); + float dq; + if (dithering.startsWith("min")) { + dq = 0.0f; + } else if (dithering.startsWith("max")) { + dq = 1.0f; + } else { + try { + dq = Float.parseFloat(dithering); + } catch (NumberFormatException nfe) { + //Default value + dq = 0.5f; + } + } + setParam(IMAGES_DITHERING_QUALITY, dq); + setParam(IMAGES_NATIVE, imagesCfg.getAttributeAsBoolean(Options.IMAGES_NATIVE.getName(), false)); + setParam(IMAGES_WRAP_PSEG, + imagesCfg.getAttributeAsBoolean(IMAGES_WRAP_PSEG.getName(), false)); + setParam(IMAGES_FS45, imagesCfg.getAttributeAsBoolean(IMAGES_FS45.getName(), false)); + if ("scale-to-fit".equals(imagesCfg.getAttribute(IMAGES_MAPPING_OPTION.getName(), null))) { + setParam(IMAGES_MAPPING_OPTION, MappingOptionTriplet.SCALE_TO_FILL); + } else { + setParam(IMAGES_MAPPING_OPTION, AFPDataObjectInfo.DEFAULT_MAPPING_OPTION); + } + configureJpegImages(imagesCfg); + } + + private void configureJpegImages(Configuration imagesCfg) { + Configuration jpegConfig = imagesCfg.getChild(IMAGES_JPEG.getName()); + float bitmapEncodingQuality = 1.0f; + boolean allowJpegEmbedding = false; + if (jpegConfig != null) { + allowJpegEmbedding = jpegConfig.getAttributeAsBoolean( + JPEG_ALLOW_JPEG_EMBEDDING.getName(), + false); + String bitmapEncodingQualityStr = jpegConfig.getAttribute( + JPEG_BITMAP_ENCODING_QUALITY.getName(), null); + if (bitmapEncodingQualityStr != null) { + try { + bitmapEncodingQuality = Float.parseFloat(bitmapEncodingQualityStr); + } catch (NumberFormatException nfe) { + //ignore and leave the default above + } + } + } + setParam(JPEG_BITMAP_ENCODING_QUALITY, bitmapEncodingQuality); + setParam(JPEG_ALLOW_JPEG_EMBEDDING, allowJpegEmbedding); + } + + private void createResourceGroupFile() throws FOPException { + try { + Configuration resourceGroupFileCfg = cfg.getChild(RESOURCE_GROUP_FILE.getName(), false); + if (resourceGroupFileCfg != null) { + String resourceGroupDest = null; + resourceGroupDest = resourceGroupFileCfg.getValue(); + if (resourceGroupDest != null) { + File resourceGroupFile = new File(resourceGroupDest); + boolean created = resourceGroupFile.createNewFile(); + if (created && resourceGroupFile.canWrite()) { + setParam(RESOURCE_GROUP_FILE, resourceGroupDest); + } else { + LOG.warn("Unable to write to default external resource group file '" + + resourceGroupDest + "'"); + } + } + } + } catch (ConfigurationException e) { + LogUtil.handleException(LOG, e, strict); + } catch (IOException ioe) { + throw new FOPException("Could not create default external resource group file", ioe); + } + } + + private void createResourceLevel() throws FOPException { + Configuration defaultResourceLevelCfg = cfg.getChild(DEFAULT_RESOURCE_LEVELS.getName(), false); + if (defaultResourceLevelCfg != null) { + AFPResourceLevelDefaults defaults = new AFPResourceLevelDefaults(); + String[] types = defaultResourceLevelCfg.getAttributeNames(); + for (int i = 0, c = types.length; i < c; i++) { + String type = types[i]; + try { + String level = defaultResourceLevelCfg.getAttribute(type); + defaults.setDefaultResourceLevel(type, AFPResourceLevel.valueOf(level)); + } catch (IllegalArgumentException iae) { + LogUtil.handleException(LOG, iae, strict); + } catch (ConfigurationException e) { + LogUtil.handleException(LOG, e, strict); + } + } + setParam(DEFAULT_RESOURCE_LEVELS, defaults); + } + } + } +} diff --git a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java index e93d8b6aa..243087d96 100644 --- a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java +++ b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java @@ -19,42 +19,24 @@ package org.apache.fop.render.afp; -import java.io.File; import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; -import org.apache.fop.afp.AFPConstants; import org.apache.fop.afp.AFPEventProducer; -import org.apache.fop.afp.AFPResourceLevel; import org.apache.fop.afp.AFPResourceLevelDefaults; -import org.apache.fop.afp.fonts.AFPFont; import org.apache.fop.afp.fonts.AFPFontCollection; import org.apache.fop.afp.fonts.AFPFontInfo; -import org.apache.fop.afp.fonts.CharacterSet; -import org.apache.fop.afp.fonts.CharacterSetBuilder; -import org.apache.fop.afp.fonts.CharacterSetType; -import org.apache.fop.afp.fonts.DoubleByteFont; -import org.apache.fop.afp.fonts.OutlineFont; -import org.apache.fop.afp.fonts.RasterFont; -import org.apache.fop.afp.util.DefaultFOPResourceAccessor; -import org.apache.fop.afp.util.ResourceAccessor; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.io.URIResolverWrapper; import org.apache.fop.fonts.FontCollection; -import org.apache.fop.fonts.FontInfo; -import org.apache.fop.fonts.FontManager; -import org.apache.fop.fonts.FontManagerConfigurator; -import org.apache.fop.fonts.FontTriplet; -import org.apache.fop.fonts.FontUtil; -import org.apache.fop.fonts.Typeface; import org.apache.fop.render.PrintRendererConfigurator; -import org.apache.fop.render.Renderer; +import org.apache.fop.render.RendererConfig.RendererConfigParser; +import org.apache.fop.render.afp.AFPFontConfig.AFPFontConfigData; import org.apache.fop.render.intermediate.IFDocumentHandler; import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator; import org.apache.fop.util.LogUtil; @@ -62,8 +44,9 @@ import org.apache.fop.util.LogUtil; /** * AFP Renderer configurator */ -public class AFPRendererConfigurator extends PrintRendererConfigurator - implements IFDocumentHandlerConfigurator { +public class AFPRendererConfigurator extends PrintRendererConfigurator implements + IFDocumentHandlerConfigurator { + private static Log LOG = LogFactory.getLog(AFPRendererConfigurator.class); private final AFPEventProducer eventProducer; @@ -72,470 +55,105 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator * * @param userAgent user agent */ - public AFPRendererConfigurator(FOUserAgent userAgent) { - super(userAgent); + public AFPRendererConfigurator(FOUserAgent userAgent, RendererConfigParser rendererConfigParser) { + super(userAgent, rendererConfigParser); eventProducer = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster()); } - private AFPFontInfo buildFont(Configuration fontCfg, String fontPath) - throws ConfigurationException { - FontManager fontManager = this.userAgent.getFactory().getFontManager(); - Configuration[] triple = fontCfg.getChildren("font-triplet"); - List<FontTriplet> tripletList = new ArrayList<FontTriplet>(); - if (triple.length == 0) { - eventProducer.fontConfigMissing(this, "<font-triplet...", fontCfg.getLocation()); - return null; - } - for (Configuration config : triple) { - int weight = FontUtil.parseCSS2FontWeight(config.getAttribute("weight")); - FontTriplet triplet = new FontTriplet(config.getAttribute("name"), - config.getAttribute("style"), - weight); - tripletList.add(triplet); - } - - //build the fonts - Configuration[] config = fontCfg.getChildren("afp-font"); - if (config.length == 0) { - eventProducer.fontConfigMissing(this, "<afp-font...", fontCfg.getLocation()); - return null; - } - Configuration afpFontCfg = config[0]; - - URI baseURI = null; - String uri = afpFontCfg.getAttribute("base-uri", fontPath); - if (uri == null) { - //Fallback for old attribute which only supports local filenames - String path = afpFontCfg.getAttribute("path", fontPath); - if (path != null) { - File f = new File(path); - baseURI = f.toURI(); - } - } else { - try { - baseURI = new URI(uri); - } catch (URISyntaxException e) { - eventProducer.invalidConfiguration(this, e); - return null; - } - } - ResourceAccessor accessor = new DefaultFOPResourceAccessor( - this.userAgent, - fontManager.getFontBaseURL(), - baseURI); - - AFPFont font = null; - try { - String type = afpFontCfg.getAttribute("type"); - if (type == null) { - eventProducer.fontConfigMissing(this, "type attribute", fontCfg.getLocation()); - return null; - } - String codepage = afpFontCfg.getAttribute("codepage"); - if (codepage == null) { - eventProducer.fontConfigMissing(this, "codepage attribute", - fontCfg.getLocation()); - return null; - } - String encoding = afpFontCfg.getAttribute("encoding"); - if (encoding == null) { - eventProducer.fontConfigMissing(this, "encoding attribute", - fontCfg.getLocation()); - return null; - } - - font = fontFromType(type, codepage, encoding, accessor, afpFontCfg); - } catch (ConfigurationException ce) { - eventProducer.invalidConfiguration(this, ce); - } catch (IOException ioe) { - eventProducer.invalidConfiguration(this, ioe); - } catch (IllegalArgumentException iae) { - eventProducer.invalidConfiguration(this, iae); + @Override + public void configure(IFDocumentHandler documentHandler) throws FOPException { + AFPRendererConfig config = (AFPRendererConfig) getRendererConfig(documentHandler); + if (config != null) { + AFPDocumentHandler afpDocumentHandler = (AFPDocumentHandler) documentHandler; + configure(afpDocumentHandler, config); } - - return font != null ? new AFPFontInfo(font, tripletList) : null; } - - /** - * Create the AFPFont based on type and type-dependent configuration. - * - * @param type font type e.g. 'raster', 'outline' - * @param codepage codepage file - * @param encoding character encoding e.g. 'Cp500', 'UnicodeBigUnmarked' - * @param accessor - * @param afpFontCfg - * @return the created AFPFont - * @throws ConfigurationException - */ - private AFPFont fontFromType(String type, String codepage, String encoding, - ResourceAccessor accessor, Configuration afpFontCfg) throws ConfigurationException, - IOException { - - if ("raster".equalsIgnoreCase(type)) { - - String name = afpFontCfg.getAttribute("name", "Unknown"); - - // Create a new font object - RasterFont font = new RasterFont(name); - - Configuration[] rasters = afpFontCfg.getChildren("afp-raster-font"); - if (rasters.length == 0) { - eventProducer.fontConfigMissing(this, "<afp-raster-font...", - afpFontCfg.getLocation()); - return null; - } - for (int j = 0; j < rasters.length; j++) { - Configuration rasterCfg = rasters[j]; - - String characterset = rasterCfg.getAttribute("characterset"); - - if (characterset == null) { - eventProducer.fontConfigMissing(this, "characterset attribute", - afpFontCfg.getLocation()); - return null; - } - float size = rasterCfg.getAttributeAsFloat("size"); - int sizeMpt = (int) (size * 1000); - String base14 = rasterCfg.getAttribute("base14-font", null); - - if (base14 != null) { - try { - Class<? extends Typeface> clazz = Class.forName( - "org.apache.fop.fonts.base14." + base14).asSubclass(Typeface.class); - try { - Typeface tf = clazz.newInstance(); - font.addCharacterSet(sizeMpt, - CharacterSetBuilder.getSingleByteInstance() - .build(characterset, codepage, encoding, tf, - eventProducer)); - } catch (Exception ie) { - String msg = "The base 14 font class " + clazz.getName() - + " could not be instantiated"; - log.error(msg); - } - } catch (ClassNotFoundException cnfe) { - String msg = "The base 14 font class for " + characterset - + " could not be found"; - log.error(msg); - } - } else { - font.addCharacterSet(sizeMpt, CharacterSetBuilder.getSingleByteInstance() - .buildSBCS(characterset, codepage, encoding, accessor, eventProducer)); - } - } - return font; - - } else if ("outline".equalsIgnoreCase(type)) { - String characterset = afpFontCfg.getAttribute("characterset"); - if (characterset == null) { - eventProducer.fontConfigMissing(this, "characterset attribute", - afpFontCfg.getLocation()); - return null; - } - String name = afpFontCfg.getAttribute("name", characterset); - CharacterSet characterSet = null; - String base14 = afpFontCfg.getAttribute("base14-font", null); - if (base14 != null) { - try { - Class<? extends Typeface> clazz = Class.forName("org.apache.fop.fonts.base14." - + base14).asSubclass(Typeface.class); - try { - Typeface tf = clazz.newInstance(); - characterSet = CharacterSetBuilder.getSingleByteInstance() - .build(characterset, codepage, encoding, tf, eventProducer); - } catch (Exception ie) { - String msg = "The base 14 font class " + clazz.getName() - + " could not be instantiated"; - log.error(msg); - } - } catch (ClassNotFoundException cnfe) { - String msg = "The base 14 font class for " + characterset - + " could not be found"; - log.error(msg); - } + private void configure(AFPDocumentHandler documentHandler, AFPRendererConfig config) { + Boolean colorImages = config.isColorImages(); + if (colorImages != null) { + documentHandler.setColorImages(colorImages); + if (colorImages) { + documentHandler.setCMYKImagesSupported(config.isCmykImagesSupported()); } else { - characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS( - characterset, codepage, encoding, accessor, eventProducer); + documentHandler.setBitsPerPixel(config.getBitsPerPixel()); } - // Return new font object - return new OutlineFont(name, characterSet); - - } else if ("CIDKeyed".equalsIgnoreCase(type)) { - String characterset = afpFontCfg.getAttribute("characterset"); - if (characterset == null) { - eventProducer.fontConfigMissing(this, "characterset attribute", - afpFontCfg.getLocation()); - return null; - } - String name = afpFontCfg.getAttribute("name", characterset); - CharacterSet characterSet = null; - CharacterSetType charsetType = afpFontCfg.getAttributeAsBoolean("ebcdic-dbcs", false) - ? CharacterSetType.DOUBLE_BYTE_LINE_DATA : CharacterSetType.DOUBLE_BYTE; - characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS(characterset, - codepage, encoding, charsetType, accessor, eventProducer); - - // Create a new font object - DoubleByteFont font = new DoubleByteFont(name, characterSet); - return font; - - } else { - log.error("No or incorrect type attribute: " + type); } - - return null; - } - - /** - * Builds a list of AFPFontInfo objects for use with the setup() method. - * - * @param cfg Configuration object - * @param eventProducer for AFP font related events - * @return List the newly created list of fonts - * @throws ConfigurationException if something's wrong with the config data - */ - private List<AFPFontInfo> buildFontListFromConfiguration(Configuration cfg, - AFPEventProducer eventProducer) throws FOPException, ConfigurationException { - - Configuration fonts = cfg.getChild("fonts"); - FontManager fontManager = this.userAgent.getFactory().getFontManager(); - - // General matcher - FontTriplet.Matcher referencedFontsMatcher = fontManager.getReferencedFontsMatcher(); - // Renderer-specific matcher - FontTriplet.Matcher localMatcher = null; - - // Renderer-specific referenced fonts - Configuration referencedFontsCfg = fonts.getChild("referenced-fonts", false); - if (referencedFontsCfg != null) { - localMatcher = FontManagerConfigurator.createFontsMatcher( - referencedFontsCfg, this.userAgent.getFactory().validateUserConfigStrictly()); + if (config.getDitheringQuality() != null) { + documentHandler.setDitheringQuality(config.getDitheringQuality()); } - - List<AFPFontInfo> fontList = new java.util.ArrayList<AFPFontInfo>(); - Configuration[] font = fonts.getChildren("font"); - final String fontPath = null; - for (int i = 0; i < font.length; i++) { - AFPFontInfo afi = buildFont(font[i], fontPath); - if (afi != null) { - if (log.isDebugEnabled()) { - log.debug("Adding font " + afi.getAFPFont().getFontName()); - } - List<FontTriplet> fontTriplets = afi.getFontTriplets(); - for (int j = 0; j < fontTriplets.size(); ++j) { - FontTriplet triplet = fontTriplets.get(j); - if (log.isDebugEnabled()) { - log.debug(" Font triplet " - + triplet.getName() + ", " - + triplet.getStyle() + ", " - + triplet.getWeight()); - } - - if ((referencedFontsMatcher != null && referencedFontsMatcher.matches(triplet)) - || (localMatcher != null && localMatcher.matches(triplet))) { - afi.getAFPFont().setEmbeddable(false); - break; - } - } - - fontList.add(afi); - } + if (config.isNativeImagesSupported() != null) { + documentHandler.setNativeImagesSupported(config.isNativeImagesSupported()); } - return fontList; - } - - /** images are converted to grayscale bitmapped IOCA */ - private static final String IMAGES_MODE_GRAYSCALE = "b+w"; - - /** images are converted to color bitmapped IOCA */ - private static final String IMAGES_MODE_COLOR = "color"; - - /** - * Throws an UnsupportedOperationException. - * - * @param renderer not used - */ - @Override - public void configure(Renderer renderer) { - throw new UnsupportedOperationException(); - } - - private void configure(AFPCustomizable customizable, Configuration cfg) throws FOPException { - - // image information - Configuration imagesCfg = cfg.getChild("images"); - - // default to grayscale images - String imagesMode = imagesCfg.getAttribute("mode", IMAGES_MODE_GRAYSCALE); - if (IMAGES_MODE_COLOR.equals(imagesMode)) { - customizable.setColorImages(true); - - boolean cmyk = imagesCfg.getAttributeAsBoolean("cmyk", false); - customizable.setCMYKImagesSupported(cmyk); - } else { - customizable.setColorImages(false); - // default to 8 bits per pixel - int bitsPerPixel = imagesCfg.getAttributeAsInteger("bits-per-pixel", 8); - customizable.setBitsPerPixel(bitsPerPixel); + if (config.getShadingMode() != null) { + documentHandler.setShadingMode(config.getShadingMode()); } - - String dithering = imagesCfg.getAttribute("dithering-quality", "medium"); - float dq = 0.5f; - if (dithering.startsWith("min")) { - dq = 0.0f; - } else if (dithering.startsWith("max")) { - dq = 1.0f; - } else { - try { - dq = Float.parseFloat(dithering); - } catch (NumberFormatException nfe) { - //ignore and leave the default above - } + if (config.getResolution() != null) { + documentHandler.setResolution(config.getResolution()); } - customizable.setDitheringQuality(dq); - - // native image support - boolean nativeImageSupport = imagesCfg.getAttributeAsBoolean("native", false); - customizable.setNativeImagesSupported(nativeImageSupport); - - Configuration jpegConfig = imagesCfg.getChild("jpeg"); - boolean allowEmbedding = false; - float ieq = 1.0f; - if (jpegConfig != null) { - allowEmbedding = jpegConfig.getAttributeAsBoolean("allow-embedding", false); - String bitmapEncodingQuality = jpegConfig.getAttribute("bitmap-encoding-quality", null); - - if (bitmapEncodingQuality != null) { - try { - ieq = Float.parseFloat(bitmapEncodingQuality); - } catch (NumberFormatException nfe) { - //ignore and leave the default above - } - } + if (config.getDefaultResourceGroupFilePath() != null) { + documentHandler.setDefaultResourceGroupFilePath(config.getDefaultResourceGroupFilePath()); } - customizable.canEmbedJpeg(allowEmbedding); - customizable.setBitmapEncodingQuality(ieq); - - //FS11 and FS45 page segment wrapping - boolean pSeg = imagesCfg.getAttributeAsBoolean("pseg", false); - customizable.setWrapPSeg(pSeg); - - //FS45 image forcing - boolean fs45 = imagesCfg.getAttributeAsBoolean("fs45", false); - customizable.setFS45(fs45); - - // shading (filled rectangles) - Configuration shadingCfg = cfg.getChild("shading"); - AFPShadingMode shadingMode = AFPShadingMode.valueOf( - shadingCfg.getValue(AFPShadingMode.COLOR.getName())); - customizable.setShadingMode(shadingMode); - - // GOCA Support - Configuration gocaCfg = cfg.getChild("goca"); - boolean gocaEnabled = gocaCfg.getAttributeAsBoolean( - "enabled", customizable.isGOCAEnabled()); - customizable.setGOCAEnabled(gocaEnabled); - String gocaText = gocaCfg.getAttribute( - "text", customizable.isStrokeGOCAText() ? "stroke" : "default"); - customizable.setStrokeGOCAText("stroke".equalsIgnoreCase(gocaText) - || "shapes".equalsIgnoreCase(gocaText)); - - // renderer resolution - Configuration rendererResolutionCfg = cfg.getChild("renderer-resolution", false); - if (rendererResolutionCfg != null) { - customizable.setResolution(rendererResolutionCfg.getValueAsInteger(240)); + if (config.isWrapPseg() != null) { + documentHandler.setWrapPSeg(config.isWrapPseg()); } - - // renderer resolution - Configuration lineWidthCorrectionCfg = cfg.getChild("line-width-correction", false); - if (lineWidthCorrectionCfg != null) { - customizable.setLineWidthCorrection(lineWidthCorrectionCfg - .getValueAsFloat(AFPConstants.LINE_WIDTH_CORRECTION)); + if (config.isFs45() != null) { + documentHandler.setFS45(config.isFs45()); } - // a default external resource group file setting - Configuration resourceGroupFileCfg - = cfg.getChild("resource-group-file", false); - if (resourceGroupFileCfg != null) { - String resourceGroupDest = null; - try { - resourceGroupDest = resourceGroupFileCfg.getValue(); - if (resourceGroupDest != null) { - File resourceGroupFile = new File(resourceGroupDest); - boolean created = resourceGroupFile.createNewFile(); - if (created && resourceGroupFile.canWrite()) { - customizable.setDefaultResourceGroupFilePath(resourceGroupDest); - } else { - log.warn("Unable to write to default external resource group file '" - + resourceGroupDest + "'"); - } - } - } catch (ConfigurationException e) { - LogUtil.handleException(log, e, - userAgent.getFactory().validateUserConfigStrictly()); - } catch (IOException ioe) { - throw new FOPException("Could not create default external resource group file" - , ioe); - } + if (config.allowJpegEmbedding() != null) { + documentHandler.canEmbedJpeg(config.allowJpegEmbedding()); } - - Configuration defaultResourceLevelCfg = cfg.getChild("default-resource-levels", false); - if (defaultResourceLevelCfg != null) { - AFPResourceLevelDefaults defaults = new AFPResourceLevelDefaults(); - String[] types = defaultResourceLevelCfg.getAttributeNames(); - for (int i = 0, c = types.length; i < c; i++) { - String type = types[i]; - try { - String level = defaultResourceLevelCfg.getAttribute(type); - defaults.setDefaultResourceLevel(type, AFPResourceLevel.valueOf(level)); - } catch (IllegalArgumentException iae) { - LogUtil.handleException(log, iae, - userAgent.getFactory().validateUserConfigStrictly()); - } catch (ConfigurationException e) { - LogUtil.handleException(log, e, - userAgent.getFactory().validateUserConfigStrictly()); - } - } - customizable.setResourceLevelDefaults(defaults); + if (config.getBitmapEncodingQuality() != null) { + documentHandler.setBitmapEncodingQuality(config.getBitmapEncodingQuality()); + } + if (config.getLineWidthCorrection() != null) { + documentHandler.setLineWidthCorrection(config.getLineWidthCorrection()); + } + if (config.isGocaEnabled() != null) { + documentHandler.setGOCAEnabled(config.isGocaEnabled()); + } + if (config.isStrokeGocaText() != null) { + documentHandler.setStrokeGOCAText(config.isStrokeGocaText()); + } + AFPResourceLevelDefaults resourceLevelDefaults = config.getResourceLevelDefaults(); + if (resourceLevelDefaults != null) { + documentHandler.setResourceLevelDefaults(resourceLevelDefaults); } } - /** {@inheritDoc} */ @Override - public void configure(IFDocumentHandler documentHandler) throws FOPException { - Configuration cfg = super.getRendererConfig(documentHandler.getMimeType()); - if (cfg != null) { - AFPDocumentHandler afpDocumentHandler = (AFPDocumentHandler) documentHandler; - configure(afpDocumentHandler, cfg); - } + protected List<FontCollection> getDefaultFontCollection() { + return new ArrayList<FontCollection>(); } - /** {@inheritDoc} */ @Override - public void setupFontInfo(IFDocumentHandler documentHandler, FontInfo fontInfo) - throws FOPException { - FontManager fontManager = userAgent.getFactory().getFontManager(); - List<AFPFontCollection> fontCollections = new ArrayList<AFPFontCollection>(); - - Configuration cfg = super.getRendererConfig(documentHandler.getMimeType()); - if (cfg != null) { + protected FontCollection getCustomFontCollection(URIResolverWrapper uriResolverWrapper, + String mimeType) throws FOPException { + AFPRendererConfig config = (AFPRendererConfig) getRendererConfig(mimeType); + if (config != null) { try { - List<AFPFontInfo> fontList = buildFontListFromConfiguration(cfg, eventProducer); - fontCollections.add(new AFPFontCollection( - userAgent.getEventBroadcaster(), fontList)); - } catch (ConfigurationException e) { + return new AFPFontCollection(userAgent.getEventBroadcaster(), createFontsList( + config.getFontInfoConfig(), mimeType)); + } catch (IOException e) { eventProducer.invalidConfiguration(this, e); - LogUtil.handleException(log, e, - userAgent.getFactory().validateUserConfigStrictly()); + LogUtil.handleException(LOG, e, userAgent.validateUserConfigStrictly()); + } catch (IllegalArgumentException iae) { + eventProducer.invalidConfiguration(this, iae); + LogUtil.handleException(LOG, iae, userAgent.validateUserConfigStrictly()); } - } else { - fontCollections.add(new AFPFontCollection(userAgent.getEventBroadcaster(), null)); } + return new AFPFontCollection(userAgent.getEventBroadcaster(), null); + } - fontManager.setup(fontInfo, - fontCollections.toArray( - new FontCollection[fontCollections.size()])); - documentHandler.setFontInfo(fontInfo); + private List<AFPFontInfo> createFontsList(AFPFontConfig fontConfig, String mimeType) + throws FOPException, IOException { + List<AFPFontInfo> afpFonts = new ArrayList<AFPFontInfo>(); + for (AFPFontConfigData config : fontConfig.getFontConfig()) { + afpFonts.add(config.getFontInfo(userAgent.getFontManager().getURIResolver(), + eventProducer)); + } + return afpFonts; } } diff --git a/src/java/org/apache/fop/render/afp/AFPSVGHandler.java b/src/java/org/apache/fop/render/afp/AFPSVGHandler.java index 48b82bbf3..b73e22d89 100644 --- a/src/java/org/apache/fop/render/afp/AFPSVGHandler.java +++ b/src/java/org/apache/fop/render/afp/AFPSVGHandler.java @@ -195,7 +195,7 @@ public class AFPSVGHandler extends AbstractGenericSVGHandler { * @return a bridge context */ public static BridgeContext createBridgeContext(FOUserAgent userAgent, AFPGraphics2D g2d) { - ImageManager imageManager = userAgent.getFactory().getImageManager(); + ImageManager imageManager = userAgent.getImageManager(); SVGUserAgent svgUserAgent = new SVGUserAgent(userAgent, new AffineTransform()); |