diff options
author | Adrian Cumiskey <acumiskey@apache.org> | 2008-05-06 16:18:51 +0000 |
---|---|---|
committer | Adrian Cumiskey <acumiskey@apache.org> | 2008-05-06 16:18:51 +0000 |
commit | 175c6a8d234fc44e8b41bad5816645c2a9184a87 (patch) | |
tree | 43ff4a8ddf613e27336201697e83d0028e4ab277 /src/java/org/apache/fop/render | |
parent | e413e9c8a102146703e2f47291f5894476edc48b (diff) | |
download | xmlgraphics-fop-175c6a8d234fc44e8b41bad5816645c2a9184a87.tar.gz xmlgraphics-fop-175c6a8d234fc44e8b41bad5816645c2a9184a87.zip |
Merged revisions 653826 via svnmerge from
https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk
........
r653826 | acumiskey | 2008-05-06 17:14:09 +0100 (Tue, 06 May 2008) | 6 lines
* Added font substitution configuration reader, model, business logic, junit test and docs.
* Added java 1.5 generics comments to many methods.
* Performed some preparatory cleanup and refactoring which includes (but is not limited to..)
- Creating a FontManager delegating class that is called upon when renderers are setting up fonts
- A new FontCollection interface and concrete implementing classes to supercede the static FontSetup mechanism.
........
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@653827 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render')
12 files changed, 563 insertions, 449 deletions
diff --git a/src/java/org/apache/fop/render/AbstractRendererConfigurator.java b/src/java/org/apache/fop/render/AbstractRendererConfigurator.java index d6851c3c5..bce5abfbd 100644 --- a/src/java/org/apache/fop/render/AbstractRendererConfigurator.java +++ b/src/java/org/apache/fop/render/AbstractRendererConfigurator.java @@ -45,7 +45,6 @@ public abstract class AbstractRendererConfigurator { super(); this.userAgent = userAgent; } - /** * Returns the configuration subtree for a specific renderer. @@ -61,16 +60,15 @@ public abstract class AbstractRendererConfigurator { return null; } - return getRendererConfig(userAgent, mimeType); + return getRendererConfig(mimeType); } /** * Returns the configuration subtree for a specific renderer. - * @param userAgent the user agent containing the user configuration * @param mimeType the MIME type of the renderer * @return the requested configuration subtree, null if there's no configuration */ - public static Configuration getRendererConfig(FOUserAgent userAgent, String mimeType) { + private Configuration getRendererConfig(String mimeType) { Configuration cfg = userAgent.getFactory().getUserConfig(); if (cfg == null) { if (log.isDebugEnabled()) { diff --git a/src/java/org/apache/fop/render/PrintRenderer.java b/src/java/org/apache/fop/render/PrintRenderer.java index 6f1bfeea6..23f8a1f62 100644 --- a/src/java/org/apache/fop/render/PrintRenderer.java +++ b/src/java/org/apache/fop/render/PrintRenderer.java @@ -25,7 +25,6 @@ import org.apache.fop.area.Trait; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontResolver; -import org.apache.fop.fonts.FontSetup; import org.apache.fop.fonts.FontTriplet; import org.w3c.dom.Document; @@ -45,25 +44,32 @@ public abstract class PrintRenderer extends AbstractRenderer { protected FontResolver fontResolver = null; /** list of fonts */ - protected List fontList = null; + protected List/*<EmbedFontInfo>*/ embedFontInfoList = null; /** - * adds a font list to current list of fonts - * @param fontInfoList font list + * Adds a font list to current list of fonts + * @param fontList a font info list */ - public void addFontList(List fontInfoList) { - if (this.fontList == null) { - setFontList(fontInfoList); + public void addFontList(List/*<EmbedFontInfo>*/ fontList) { + if (embedFontInfoList == null) { + setFontList(fontList); } else { - this.fontList.addAll(fontInfoList); + fontList.addAll(fontList); } } /** - * @param fontList list of available fonts + * @param embedFontInfoList list of available fonts */ - public void setFontList(List fontList) { - this.fontList = fontList; + public void setFontList(List/*<EmbedFontInfo>*/ embedFontInfoList) { + this.embedFontInfoList = embedFontInfoList; + } + + /** + * @return list of available embedded fonts + */ + public List/*<EmbedFontInfo>*/ getFontList() { + return this.embedFontInfoList; } /** @@ -73,8 +79,7 @@ public abstract class PrintRenderer extends AbstractRenderer { */ public void setupFontInfo(FontInfo inFontInfo) { this.fontInfo = inFontInfo; - FontSetup.setup(fontInfo, fontList, fontResolver, - userAgent.getFactory().isBase14KerningEnabled()); + userAgent.getFactory().getFontManager().setupRenderer(this); } /** @@ -180,4 +185,11 @@ public abstract class PrintRenderer extends AbstractRenderer { } return this.fontResolver; } + + /** + * @return the font info + */ + public FontInfo getFontInfo() { + return this.fontInfo; + } } diff --git a/src/java/org/apache/fop/render/PrintRendererConfigurator.java b/src/java/org/apache/fop/render/PrintRendererConfigurator.java index 674c357df..6273b7050 100644 --- a/src/java/org/apache/fop/render/PrintRendererConfigurator.java +++ b/src/java/org/apache/fop/render/PrintRendererConfigurator.java @@ -44,8 +44,8 @@ import org.apache.fop.apps.FopFactory; import org.apache.fop.fonts.EmbedFontInfo; import org.apache.fop.fonts.FontCache; import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.FontManager; import org.apache.fop.fonts.FontResolver; -import org.apache.fop.fonts.FontSetup; import org.apache.fop.fonts.FontTriplet; import org.apache.fop.fonts.FontUtil; import org.apache.fop.fonts.autodetect.FontFileFinder; @@ -78,28 +78,30 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator public void configure(Renderer renderer) throws FOPException { Configuration cfg = getRendererConfig(renderer); if (cfg == null) { + log.trace("no configuration found for " + renderer); return; } PrintRenderer printRenderer = (PrintRenderer)renderer; FontResolver fontResolver = printRenderer.getFontResolver(); + FopFactory factory = userAgent.getFactory(); + FontManager fontManager = factory.getFontManager(); if (fontResolver == null) { //Ensure that we have minimal font resolution capabilities - fontResolver = FontSetup.createMinimalFontResolver(); + fontResolver = FontManager.createMinimalFontResolver(); } - FopFactory factory = userAgent.getFactory(); boolean strict = factory.validateUserConfigStrictly(); - FontCache fontCache = factory.getFontCache(); + FontCache fontCache = fontManager.getFontCache(); - List fontInfoList = buildFontListFromConfiguration(cfg, + List/*<EmbedFontInfo>*/ embedFontInfoList = buildFontListFromConfiguration(cfg, userAgent.getFontBaseURL(), fontResolver, strict, fontCache); if (fontCache != null && fontCache.hasChanged()) { fontCache.save(); } - printRenderer.addFontList(fontInfoList); + printRenderer.addFontList(embedFontInfoList); } /** @@ -113,10 +115,11 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator * @return a List of EmbedFontInfo objects. * @throws FOPException If an error occurs while processing the configuration */ - public static List buildFontListFromConfiguration(Configuration cfg, + public static List/*<EmbedFontInfo>*/ buildFontListFromConfiguration(Configuration cfg, String fontBaseURL, FontResolver fontResolver, boolean strict, FontCache fontCache) throws FOPException { - List fontInfoList = new java.util.ArrayList(); + List/*<EmbedFontInfo>*/ fontInfoList + = new java.util.ArrayList/*<EmbedFontInfo>*/(); Configuration fonts = cfg.getChild("fonts", false); if (fonts != null) { @@ -204,12 +207,13 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator // font file (singular) configuration Configuration[] font = fonts.getChildren("font"); for (int i = 0; i < font.length; i++) { - EmbedFontInfo fontInfo = getFontInfoFromConfiguration( + EmbedFontInfo embedFontInfo = getFontInfoFromConfiguration( font[i], fontResolver, strict, fontCache); - if (fontInfo != null) { - fontInfoList.add(fontInfo); + if (embedFontInfo != null) { + fontInfoList.add(embedFontInfo); } } + if (log.isDebugEnabled()) { log.debug("Finished font configuration in " + (System.currentTimeMillis() - start) + "ms"); @@ -221,11 +225,12 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator /** * Iterates over font file list adding font info to list * @param fontFileList font file list - * @param fontInfoList font info list + * @param embedFontInfoList a configured font info list * @param resolver font resolver */ private static void addFontInfoListFromFileList( - List fontFileList, List fontInfoList, FontResolver resolver, FontCache fontCache) { + List fontFileList, List/*<EmbedFontInfo>*/ embedFontInfoList, + FontResolver resolver, FontCache fontCache) { for (Iterator iter = fontFileList.iterator(); iter.hasNext();) { URL fontUrl = (URL)iter.next(); // parse font to ascertain font info @@ -242,7 +247,7 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator for (int i = 0, c = embedFontInfos.length; i < c; i++) { EmbedFontInfo fontInfo = embedFontInfos[i]; if (fontInfo != null) { - fontInfoList.add(fontInfo); + embedFontInfoList.add(fontInfo); } } } @@ -257,24 +262,64 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator } /** + * Creates a new FontTriplet given a triple Configuration + * + * @param tripletCfg a triplet configuration + * @param strict use strict validation + * @return a font triplet font key + * @throws FOPException thrown if a FOP exception occurs + */ + private static FontTriplet getFontTripletFromConfiguration( + Configuration tripletCfg, boolean strict) throws FOPException { + try { + String name = tripletCfg.getAttribute("name"); + if (name == null) { + LogUtil.handleError(log, "font-triplet without name", strict); + return null; + } + + String weightStr = tripletCfg.getAttribute("weight"); + if (weightStr == null) { + LogUtil.handleError(log, "font-triplet without weight", strict); + return null; + } + int weight = FontUtil.parseCSS2FontWeight(FontUtil.stripWhiteSpace(weightStr)); + + String style = tripletCfg.getAttribute("style"); + if (style == null) { + LogUtil.handleError(log, "font-triplet without style", strict); + return null; + } else { + style = FontUtil.stripWhiteSpace(style); + } + return FontInfo.createFontKey(name, style, weight); + } catch (ConfigurationException e) { + LogUtil.handleException(log, e, strict); + } + return null; + } + + /** * Returns a font info from a font node Configuration definition * * @param fontCfg Configuration object (font node) * @param fontResolver font resolver used to resolve font * @param strict validate configuration strictly * @param fontCache the font cache (or null if it is disabled) - * @return font info + * @return the embedded font info * @throws FOPException if something's wrong with the config data */ - public static EmbedFontInfo getFontInfoFromConfiguration( + private static EmbedFontInfo getFontInfoFromConfiguration( Configuration fontCfg, FontResolver fontResolver, boolean strict, FontCache fontCache) throws FOPException { String metricsUrl = fontCfg.getAttribute("metrics-url", null); String embedUrl = fontCfg.getAttribute("embed-url", null); String subFont = fontCfg.getAttribute("sub-font", null); - + if (metricsUrl == null && embedUrl == null) { - LogUtil.handleError(log, "Font configuration without metric-url or embed-url", strict); + LogUtil.handleError(log, + "Font configuration without metric-url or embed-url attribute", + strict); return null; } if (strict) { @@ -299,15 +344,13 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator } } } - boolean useKerning = fontCfg.getAttributeAsBoolean("kerning", true); - EmbedFontInfo fontInfo = null; Configuration[] tripletCfg = fontCfg.getChildren("font-triplet"); + // no font triplet info if (tripletCfg.length == 0) { LogUtil.handleError(log, "font without font-triplet", strict); - - // if not strict try to determine font info from the embed/metrics url + File fontFile = FontCache.getFileFromUrls(new String[] {embedUrl, metricsUrl}); URL fontUrl; try { @@ -324,53 +367,36 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator } else { return null; } - } else { - List tripleList = new java.util.ArrayList(); - for (int j = 0; j < tripletCfg.length; j++) { - try { - String name = tripletCfg[j].getAttribute("name"); - if (name == null) { - LogUtil.handleError(log, "font-triplet without name", strict); - continue; - } - String weightStr = tripletCfg[j].getAttribute("weight"); - if (weightStr == null) { - LogUtil.handleError(log, "font-triplet without weight", strict); - continue; - } - int weight = FontUtil.parseCSS2FontWeight(weightStr); - String style = tripletCfg[j].getAttribute("style"); - if (style == null) { - LogUtil.handleError(log, "font-triplet without style", strict); - continue; - } - tripleList.add(FontInfo.createFontKey(name, style, weight)); - } catch (ConfigurationException e) { - LogUtil.handleException(log, e, strict); - } - } - - fontInfo = new EmbedFontInfo(metricsUrl, useKerning, tripleList, embedUrl, subFont); - - if (fontCache != null) { - if (!fontCache.containsFont(fontInfo)) { - fontCache.addFont(fontInfo); - } + } + + List/*<FontTriplet>*/ tripletList = new java.util.ArrayList/*<FontTriplet>*/(); + for (int j = 0; j < tripletCfg.length; j++) { + FontTriplet fontTriplet = getFontTripletFromConfiguration(tripletCfg[j], strict); + tripletList.add(fontTriplet); + } + + boolean useKerning = fontCfg.getAttributeAsBoolean("kerning", true); + EmbedFontInfo embedFontInfo + = new EmbedFontInfo(metricsUrl, useKerning, tripletList, embedUrl, subFont); + if (fontCache != null) { + if (!fontCache.containsFont(embedFontInfo)) { + fontCache.addFont(embedFontInfo); } - - if (log.isDebugEnabled()) { - log.debug("Adding font " + fontInfo.getEmbedFile() - + ", metric file " + fontInfo.getMetricsFile()); - for (int j = 0; j < tripleList.size(); ++j) { - FontTriplet triplet = (FontTriplet) tripleList.get(j); - log.debug(" Font triplet " - + triplet.getName() + ", " - + triplet.getStyle() + ", " - + triplet.getWeight()); - } - } } - return fontInfo; + + if (log.isDebugEnabled()) { + String embedFile = embedFontInfo.getEmbedFile(); + log.debug("Adding font " + (embedFile != null ? embedFile + ", " : "") + + "metric file " + embedFontInfo.getMetricsFile()); + for (int j = 0; j < tripletList.size(); ++j) { + FontTriplet triplet = (FontTriplet) tripletList.get(j); + log.debug(" Font triplet " + + triplet.getName() + ", " + + triplet.getStyle() + ", " + + triplet.getWeight()); + } + } + return embedFontInfo; } } diff --git a/src/java/org/apache/fop/render/afp/AFPRenderer.java b/src/java/org/apache/fop/render/afp/AFPRenderer.java index 526fc43f3..14eb33510 100644 --- a/src/java/org/apache/fop/render/afp/AFPRenderer.java +++ b/src/java/org/apache/fop/render/afp/AFPRenderer.java @@ -283,8 +283,8 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { public void setupFontInfo(FontInfo inFontInfo) { this.fontInfo = inFontInfo; int num = 1; - if (this.fontList != null && this.fontList.size() > 0) { - for (Iterator it = this.fontList.iterator(); it.hasNext();) { + if (super.embedFontInfoList != null && super.embedFontInfoList.size() > 0) { + for (Iterator it = super.embedFontInfoList.iterator(); it.hasNext();) { AFPFontInfo afi = (AFPFontInfo)it.next(); AFPFont bf = (AFPFont)afi.getAFPFont(); for (Iterator it2 = afi.getFontTriplets().iterator(); it2.hasNext();) { diff --git a/src/java/org/apache/fop/render/java2d/Base14FontCollection.java b/src/java/org/apache/fop/render/java2d/Base14FontCollection.java new file mode 100644 index 000000000..f97a68191 --- /dev/null +++ b/src/java/org/apache/fop/render/java2d/Base14FontCollection.java @@ -0,0 +1,181 @@ +/* + * 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.java2d; + +import java.awt.Graphics2D; + +import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; +import org.apache.fop.fonts.FontInfo; + +/** + * A base 14 font collection for graphics 2D + */ +public class Base14FontCollection implements FontCollection { + + private Graphics2D graphics2d = null; + + /** + * Main constructor + * @param graphics2d a graphics 2D + */ + public Base14FontCollection(Graphics2D graphics2d) { + this.graphics2d = graphics2d; + } + + /** + * {@inheritDoc} + */ + public int setup(int start, FontInfo fontInfo) { + /* + * available java fonts are: + * Serif - bold, normal, italic, bold-italic + * SansSerif - bold, normal, italic, bold-italic + * MonoSpaced - bold, normal, italic, bold-italic + */ + final int normal = java.awt.Font.PLAIN; + final int bold = java.awt.Font.BOLD; + final int italic = java.awt.Font.ITALIC; + final int bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC; + + FontMetricsMapper metric; + metric = new SystemFontMetricsMapper("SansSerif", normal, graphics2d); + // --> goes to F1 + fontInfo.addMetrics("F1", metric); + metric = new SystemFontMetricsMapper("SansSerif", italic, graphics2d); + // --> goes to F2 + fontInfo.addMetrics("F2", metric); + metric = new SystemFontMetricsMapper("SansSerif", bold, graphics2d); + // --> goes to F3 + fontInfo.addMetrics("F3", metric); + metric = new SystemFontMetricsMapper("SansSerif", bolditalic, graphics2d); + // --> goes to F4 + fontInfo.addMetrics("F4", metric); + + + metric = new SystemFontMetricsMapper("Serif", normal, graphics2d); + // --> goes to F5 + fontInfo.addMetrics("F5", metric); + metric = new SystemFontMetricsMapper("Serif", italic, graphics2d); + // --> goes to F6 + fontInfo.addMetrics("F6", metric); + metric = new SystemFontMetricsMapper("Serif", bold, graphics2d); + // --> goes to F7 + fontInfo.addMetrics("F7", metric); + metric = new SystemFontMetricsMapper("Serif", bolditalic, graphics2d); + // --> goes to F8 + fontInfo.addMetrics("F8", metric); + + metric = new SystemFontMetricsMapper("MonoSpaced", normal, graphics2d); + // --> goes to F9 + fontInfo.addMetrics("F9", metric); + metric = new SystemFontMetricsMapper("MonoSpaced", italic, graphics2d); + // --> goes to F10 + fontInfo.addMetrics("F10", metric); + metric = new SystemFontMetricsMapper("MonoSpaced", bold, graphics2d); + // --> goes to F11 + fontInfo.addMetrics("F11", metric); + metric = new SystemFontMetricsMapper("MonoSpaced", bolditalic, graphics2d); + // --> goes to F12 + fontInfo.addMetrics("F12", metric); + + metric = new SystemFontMetricsMapper("Serif", normal, graphics2d); + //"Symbol" doesn't seem to work here, but "Serif" does the job just fine. *shrug* + // --> goes to F13 and F14 + fontInfo.addMetrics("F13", metric); + fontInfo.addMetrics("F14", metric); + + // Custom type 1 fonts step 1/2 + // fontInfo.addMetrics("F15", new OMEP()); + // fontInfo.addMetrics("F16", new GaramondLightCondensed()); + // fontInfo.addMetrics("F17", new BauerBodoniBoldItalic()); + + /* any is treated as serif */ + fontInfo.addFontProperties("F5", "any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "any", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "any", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F7", "any", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "any", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "any", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + + fontInfo.addFontProperties("F1", "sans-serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F2", "sans-serif", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F2", "sans-serif", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F3", "sans-serif", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F4", "sans-serif", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F4", "sans-serif", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F5", "serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "serif", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "serif", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F7", "serif", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "serif", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "serif", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F9", "monospace", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F10", "monospace", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F10", "monospace", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F11", "monospace", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F12", "monospace", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F12", "monospace", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + + fontInfo.addFontProperties("F1", "Helvetica", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F2", "Helvetica", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F2", "Helvetica", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F3", "Helvetica", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F4", "Helvetica", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F4", "Helvetica", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F5", "Times", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "Times", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "Times", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F7", "Times", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "Times", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "Times", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F9", "Courier", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F10", "Courier", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F10", "Courier", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F11", "Courier", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F12", "Courier", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F12", "Courier", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F13", "Symbol", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F14", "ZapfDingbats", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + + // Custom type 1 fonts step 2/2 + // fontInfo.addFontProperties("F15", "OMEP", "normal", FontInfo.NORMAL); + // fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", FontInfo.NORMAL); + // fontInfo.addFontProperties("F17", "BauerBodoni", "italic", FontInfo.BOLD); + + /* for compatibility with PassiveTex */ + fontInfo.addFontProperties("F5", "Times-Roman", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "Times-Roman", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "Times-Roman", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F7", "Times-Roman", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "Times-Roman", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "Times-Roman", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F5", "Times Roman", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "Times Roman", Font.STYLE_OBLIQUE, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F6", "Times Roman", Font.STYLE_ITALIC, Font.WEIGHT_NORMAL); + fontInfo.addFontProperties("F7", "Times Roman", Font.STYLE_NORMAL, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "Times Roman", Font.STYLE_OBLIQUE, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F8", "Times Roman", Font.STYLE_ITALIC, Font.WEIGHT_BOLD); + fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter", + "normal", Font.WEIGHT_NORMAL); + + return 15; + } +} diff --git a/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java b/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java new file mode 100644 index 000000000..771d3f2d7 --- /dev/null +++ b/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java @@ -0,0 +1,111 @@ +/* + * 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.java2d; + +import java.util.List; + +import javax.xml.transform.Source; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.fonts.CustomFont; +import org.apache.fop.fonts.EmbedFontInfo; +import org.apache.fop.fonts.FontCollection; +import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.FontLoader; +import org.apache.fop.fonts.FontManager; +import org.apache.fop.fonts.FontResolver; +import org.apache.fop.fonts.FontTriplet; +import org.apache.fop.fonts.LazyFont; +import org.apache.fop.render.PrintRenderer; + +/** + * A java2d configured font collection + */ +public class ConfiguredFontCollection implements FontCollection { + + private static Log log = LogFactory.getLog(ConfiguredFontCollection.class); + + private PrintRenderer renderer = null; + + /** + * Main constructor + * + * @param renderer a print renderer + */ + public ConfiguredFontCollection(PrintRenderer renderer) { + this.renderer = renderer; + } + + /** + * {@inheritDoc} + */ + public int setup(int start, FontInfo fontInfo) { + List/*<EmbedFontInfo>*/ fontList = renderer.getFontList(); + FontResolver resolver = renderer.getFontResolver(); + int num = start; + if (fontList == null || fontList.size() < 1) { + log.debug("No user configured fonts found."); + return num; + } + if (resolver == null) { + // Ensure that we have minimal font resolution capabilities + resolver = FontManager.createMinimalFontResolver(); + } + String internalName = null; + + for (int i = 0; i < fontList.size(); i++) { + + EmbedFontInfo configFontInfo = (EmbedFontInfo) fontList.get(i); + String fontFile = configFontInfo.getEmbedFile(); + internalName = "F" + num; + num++; + try { + FontMetricsMapper font = null; + String metricsUrl = configFontInfo.getMetricsFile(); + // If the user specified an XML-based metrics file, we'll use it + // Otherwise, calculate metrics directly from the font file. + if (metricsUrl != null) { + LazyFont fontMetrics = new LazyFont(configFontInfo, resolver); + Source fontSource = resolver.resolve(configFontInfo.getEmbedFile()); + font = new CustomFontMetricsMapper(fontMetrics, fontSource); + } else { + CustomFont fontMetrics = FontLoader.loadFont(fontFile, null, resolver); + font = new CustomFontMetricsMapper(fontMetrics); + } + + fontInfo.addMetrics(internalName, font); + + List triplets = configFontInfo.getFontTriplets(); + for (int c = 0; c < triplets.size(); c++) { + FontTriplet triplet = (FontTriplet) triplets.get(c); + + if (log.isDebugEnabled()) { + log.debug("Registering: " + triplet + " under " + internalName); + } + fontInfo.addFontProperties(internalName, triplet); + } + } catch (Exception e) { + log.warn("Unable to load custom font from file '" + fontFile + "'", e); + } + } + return num; + } +} diff --git a/src/java/org/apache/fop/render/java2d/FontSetup.java b/src/java/org/apache/fop/render/java2d/FontSetup.java deleted file mode 100644 index ade457d95..000000000 --- a/src/java/org/apache/fop/render/java2d/FontSetup.java +++ /dev/null @@ -1,353 +0,0 @@ -/* - * 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.java2d; - -// FOP -import java.awt.Graphics2D; -import java.awt.GraphicsEnvironment; -import java.util.List; -import java.util.Set; - -import javax.xml.transform.Source; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.apache.fop.fonts.CustomFont; -import org.apache.fop.fonts.EmbedFontInfo; -import org.apache.fop.fonts.Font; -import org.apache.fop.fonts.FontInfo; -import org.apache.fop.fonts.FontLoader; -import org.apache.fop.fonts.FontResolver; -import org.apache.fop.fonts.FontTriplet; -import org.apache.fop.fonts.FontUtil; -import org.apache.fop.fonts.LazyFont; - -/** - * Sets up the Java2D/AWT fonts. It is similar to - * org.apache.fop.render.fonts.FontSetup. - * Assigns the font (with metrics) to internal names like "F1" and - * assigns family-style-weight triplets to the fonts. - */ -public class FontSetup { - - /** logging instance */ - protected static Log log = LogFactory.getLog(FontSetup.class); - - private static final int LAST_PREDEFINED_FONT_NUMBER = 14; - - private static final Set HARDCODED_FONT_NAMES; - - static { - HARDCODED_FONT_NAMES = new java.util.HashSet(); - HARDCODED_FONT_NAMES.add("any"); - HARDCODED_FONT_NAMES.add("sans-serif"); - HARDCODED_FONT_NAMES.add("serif"); - HARDCODED_FONT_NAMES.add("monospace"); - - HARDCODED_FONT_NAMES.add("Helvetica"); - HARDCODED_FONT_NAMES.add("Times"); - HARDCODED_FONT_NAMES.add("Courier"); - HARDCODED_FONT_NAMES.add("Symbol"); - HARDCODED_FONT_NAMES.add("ZapfDingbats"); - HARDCODED_FONT_NAMES.add("Times Roman"); - HARDCODED_FONT_NAMES.add("Times-Roman"); - HARDCODED_FONT_NAMES.add("Computer-Modern-Typewriter"); - } - - /** - * Sets up the font info object. - * - * Adds metrics for basic fonts and useful family-style-weight - * triplets for lookup. - * - * @param fontInfo the font info object to set up - * @param configuredFontList of fop config fonts - * @param resolver for resolving the font file URI - * @param graphics needed for access to font metrics - */ - public static void setup(FontInfo fontInfo, List configuredFontList, - FontResolver resolver, Graphics2D graphics) { - FontMetricsMapper metric; - int normal, bold, bolditalic, italic; - - /* - * available java fonts are: - * Serif - bold, normal, italic, bold-italic - * SansSerif - bold, normal, italic, bold-italic - * MonoSpaced - bold, normal, italic, bold-italic - */ - normal = java.awt.Font.PLAIN; - bold = java.awt.Font.BOLD; - italic = java.awt.Font.ITALIC; - bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC; - - metric = new SystemFontMetricsMapper("SansSerif", normal, graphics); - // --> goes to F1 - fontInfo.addMetrics("F1", metric); - metric = new SystemFontMetricsMapper("SansSerif", italic, graphics); - // --> goes to F2 - fontInfo.addMetrics("F2", metric); - metric = new SystemFontMetricsMapper("SansSerif", bold, graphics); - // --> goes to F3 - fontInfo.addMetrics("F3", metric); - metric = new SystemFontMetricsMapper("SansSerif", bolditalic, graphics); - // --> goes to F4 - fontInfo.addMetrics("F4", metric); - - - metric = new SystemFontMetricsMapper("Serif", normal, graphics); - // --> goes to F5 - fontInfo.addMetrics("F5", metric); - metric = new SystemFontMetricsMapper("Serif", italic, graphics); - // --> goes to F6 - fontInfo.addMetrics("F6", metric); - metric = new SystemFontMetricsMapper("Serif", bold, graphics); - // --> goes to F7 - fontInfo.addMetrics("F7", metric); - metric = new SystemFontMetricsMapper("Serif", bolditalic, graphics); - // --> goes to F8 - fontInfo.addMetrics("F8", metric); - - metric = new SystemFontMetricsMapper("MonoSpaced", normal, graphics); - // --> goes to F9 - fontInfo.addMetrics("F9", metric); - metric = new SystemFontMetricsMapper("MonoSpaced", italic, graphics); - // --> goes to F10 - fontInfo.addMetrics("F10", metric); - metric = new SystemFontMetricsMapper("MonoSpaced", bold, graphics); - // --> goes to F11 - fontInfo.addMetrics("F11", metric); - metric = new SystemFontMetricsMapper("MonoSpaced", bolditalic, graphics); - // --> goes to F12 - fontInfo.addMetrics("F12", metric); - - metric = new SystemFontMetricsMapper("Serif", normal, graphics); - //"Symbol" doesn't seem to work here, but "Serif" does the job just fine. *shrug* - // --> goes to F13 and F14 - fontInfo.addMetrics("F13", metric); - fontInfo.addMetrics("F14", metric); - - // Custom type 1 fonts step 1/2 - // fontInfo.addMetrics("F15", new OMEP()); - // fontInfo.addMetrics("F16", new GaramondLightCondensed()); - // fontInfo.addMetrics("F17", new BauerBodoniBoldItalic()); - - /* any is treated as serif */ - fontInfo.addFontProperties("F5", "any", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "any", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "any", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F7", "any", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "any", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "any", "oblique", Font.WEIGHT_BOLD); - - fontInfo.addFontProperties("F1", "sans-serif", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F2", "sans-serif", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F2", "sans-serif", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F3", "sans-serif", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F4", "sans-serif", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F4", "sans-serif", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F5", "serif", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "serif", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "serif", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F7", "serif", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "serif", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "serif", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F9", "monospace", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F10", "monospace", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F10", "monospace", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F11", "monospace", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F12", "monospace", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F12", "monospace", "italic", Font.WEIGHT_BOLD); - - fontInfo.addFontProperties("F1", "Helvetica", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F2", "Helvetica", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F2", "Helvetica", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F3", "Helvetica", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F4", "Helvetica", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F4", "Helvetica", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F5", "Times", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "Times", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "Times", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F7", "Times", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "Times", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "Times", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F9", "Courier", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F10", "Courier", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F10", "Courier", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F11", "Courier", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F12", "Courier", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F12", "Courier", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F13", "Symbol", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", Font.WEIGHT_NORMAL); - - // Custom type 1 fonts step 2/2 - // fontInfo.addFontProperties("F15", "OMEP", "normal", FontInfo.NORMAL); - // fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", FontInfo.NORMAL); - // fontInfo.addFontProperties("F17", "BauerBodoni", "italic", FontInfo.BOLD); - - /* for compatibility with PassiveTex */ - fontInfo.addFontProperties("F5", "Times-Roman", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "Times-Roman", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "Times-Roman", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F7", "Times-Roman", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "Times-Roman", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "Times-Roman", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F5", "Times Roman", "normal", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "Times Roman", "oblique", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F6", "Times Roman", "italic", Font.WEIGHT_NORMAL); - fontInfo.addFontProperties("F7", "Times Roman", "normal", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "Times Roman", "oblique", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F8", "Times Roman", "italic", Font.WEIGHT_BOLD); - fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter", - "normal", Font.WEIGHT_NORMAL); - - int lastNum = configureInstalledAWTFonts(fontInfo, graphics, LAST_PREDEFINED_FONT_NUMBER + 1); - addConfiguredFonts(fontInfo, configuredFontList, resolver, lastNum++); - } - - private static int configureInstalledAWTFonts(FontInfo fontInfo, Graphics2D graphics, - int startNumber) { - int num = startNumber; - GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); - - java.awt.Font[] fonts = env.getAllFonts(); - for (int i = 0; i < fonts.length; i++) { - java.awt.Font f = fonts[i]; - if (HARDCODED_FONT_NAMES.contains(f.getName())) { - continue; //skip - } - - if (log.isTraceEnabled()) { - log.trace("AWT Font: " + f.getFontName() - + ", family: " + f.getFamily() - + ", PS: " + f.getPSName() - + ", Name: " + f.getName() - + ", Angle: " + f.getItalicAngle() - + ", Style: " + f.getStyle()); - } - - String searchName = FontUtil.stripWhiteSpace(f.getName()).toLowerCase(); - String guessedStyle = FontUtil.guessStyle(searchName); - int guessedWeight = FontUtil.guessWeight(searchName); - - num++; - String fontKey = "F" + num; - int style = convertToAWTFontStyle(guessedStyle, guessedWeight); - addFontMetricsMapper(fontInfo, f.getName(), fontKey, graphics, style); - - //Register appropriate font triplets matching the font. Two different strategies: - //Example: "Arial Bold", normal, normal - addFontTriplet(fontInfo, f.getName(), - Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, fontKey); - if (!f.getName().equals(f.getFamily())) { - //Example: "Arial", bold, normal - addFontTriplet(fontInfo, f.getFamily(), - guessedStyle, guessedWeight, fontKey); - } - } - return num; - - } - - /** - * Add fonts from configuration file starting with internal name F<num>. - * - * @param fontInfo the font info object to set up - * @param fontList a list of EmbedFontInfo objects - * @param num starting index for internal font numbering - * @param resolver the font resolver - */ - private static void addConfiguredFonts(FontInfo fontInfo, List fontList, FontResolver resolver, int num) { - - if (fontList == null || fontList.size() < 1) { - log.debug("No user configured fonts found."); - return; - } - if (resolver == null) { - // Ensure that we have minimal font resolution capabilities - resolver = org.apache.fop.fonts.FontSetup - .createMinimalFontResolver(); - } - String internalName = null; - - for (int i = 0; i < fontList.size(); i++) { - - EmbedFontInfo configFontInfo = (EmbedFontInfo) fontList.get(i); - String fontFile = configFontInfo.getEmbedFile(); - internalName = "F" + num; - num++; - try { - FontMetricsMapper font = null; - String metricsUrl = configFontInfo.getMetricsFile(); - // If the user specified an XML-based metrics file, we'll use it - // Otherwise, calculate metrics directly from the font file. - if (metricsUrl != null) { - LazyFont fontMetrics = new LazyFont(configFontInfo, resolver); - Source fontSource = resolver.resolve(configFontInfo.getEmbedFile()); - font = new CustomFontMetricsMapper(fontMetrics, fontSource); - } else { - CustomFont fontMetrics = FontLoader.loadFont(fontFile, null, resolver); - font = new CustomFontMetricsMapper(fontMetrics); - } - - fontInfo.addMetrics(internalName, font); - - List triplets = configFontInfo.getFontTriplets(); - for (int c = 0; c < triplets.size(); c++) { - FontTriplet triplet = (FontTriplet) triplets.get(c); - - if (log.isDebugEnabled()) { - log.debug("Registering: " + triplet + " under " + internalName); - } - fontInfo.addFontProperties(internalName, triplet); - } - } catch (Exception e) { - log.warn("Unable to load custom font from file '" + fontFile + "'", e); - } - } - } - - - private static void addFontTriplet(FontInfo fontInfo, String fontName, String fontStyle, - int fontWeight, String fontKey) { - FontTriplet triplet = FontInfo.createFontKey(fontName, fontStyle, fontWeight); - fontInfo.addFontProperties(fontKey, triplet); - } - - private static void addFontMetricsMapper(FontInfo fontInfo, String family, String fontKey, - Graphics2D graphics, int style) { - FontMetricsMapper metric = new SystemFontMetricsMapper(family, style, graphics); - fontInfo.addMetrics(fontKey, metric); - } - - private static int convertToAWTFontStyle(String fontStyle, int fontWeight) { - int style = java.awt.Font.PLAIN; - if (fontWeight >= Font.WEIGHT_BOLD) { - style |= java.awt.Font.BOLD; - } - if (!"normal".equals(fontStyle)) { - style |= java.awt.Font.ITALIC; - } - return style; - } - -} - diff --git a/src/java/org/apache/fop/render/java2d/InstalledFontCollection.java b/src/java/org/apache/fop/render/java2d/InstalledFontCollection.java new file mode 100644 index 000000000..4cdd30c41 --- /dev/null +++ b/src/java/org/apache/fop/render/java2d/InstalledFontCollection.java @@ -0,0 +1,138 @@ +/* + * 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.java2d; + +import java.awt.Graphics2D; +import java.awt.GraphicsEnvironment; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; +import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.FontTriplet; +import org.apache.fop.fonts.FontUtil; + +/** + * A custom AWT font collection + */ +public class InstalledFontCollection implements FontCollection { + + private static Log log = LogFactory.getLog(InstalledFontCollection.class); + + private static final Set HARDCODED_FONT_NAMES; + + static { + HARDCODED_FONT_NAMES = new java.util.HashSet(); + HARDCODED_FONT_NAMES.add("any"); + HARDCODED_FONT_NAMES.add("sans-serif"); + HARDCODED_FONT_NAMES.add("serif"); + HARDCODED_FONT_NAMES.add("monospace"); + + HARDCODED_FONT_NAMES.add("Helvetica"); + HARDCODED_FONT_NAMES.add("Times"); + HARDCODED_FONT_NAMES.add("Courier"); + HARDCODED_FONT_NAMES.add("Symbol"); + HARDCODED_FONT_NAMES.add("ZapfDingbats"); + HARDCODED_FONT_NAMES.add("Times Roman"); + HARDCODED_FONT_NAMES.add("Times-Roman"); + HARDCODED_FONT_NAMES.add("Computer-Modern-Typewriter"); + } + + private Graphics2D graphics2D = null; + + /** + * Main constructor + * + * @param graphics2D a graphics 2D + */ + public InstalledFontCollection(Graphics2D graphics2D) { + this.graphics2D = graphics2D; + } + + /** + * {@inheritDoc} + */ + public int setup(int start, FontInfo fontInfo) { + int num = start; + GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); + + java.awt.Font[] fonts = env.getAllFonts(); + for (int i = 0; i < fonts.length; i++) { + java.awt.Font f = fonts[i]; + if (HARDCODED_FONT_NAMES.contains(f.getName())) { + continue; //skip + } + + if (log.isTraceEnabled()) { + log.trace("AWT Font: " + f.getFontName() + + ", family: " + f.getFamily() + + ", PS: " + f.getPSName() + + ", Name: " + f.getName() + + ", Angle: " + f.getItalicAngle() + + ", Style: " + f.getStyle()); + } + + String searchName = FontUtil.stripWhiteSpace(f.getName()).toLowerCase(); + String guessedStyle = FontUtil.guessStyle(searchName); + int guessedWeight = FontUtil.guessWeight(searchName); + + num++; + String fontKey = "F" + num; + int style = convertToAWTFontStyle(guessedStyle, guessedWeight); + addFontMetricsMapper(fontInfo, f.getName(), fontKey, graphics2D, style); + + //Register appropriate font triplets matching the font. Two different strategies: + //Example: "Arial Bold", normal, normal + addFontTriplet(fontInfo, f.getName(), + Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, fontKey); + if (!f.getName().equals(f.getFamily())) { + //Example: "Arial", bold, normal + addFontTriplet(fontInfo, f.getFamily(), + guessedStyle, guessedWeight, fontKey); + } + } + return num; + } + + private static void addFontTriplet(FontInfo fontInfo, String fontName, String fontStyle, + int fontWeight, String fontKey) { + FontTriplet triplet = FontInfo.createFontKey(fontName, fontStyle, fontWeight); + fontInfo.addFontProperties(fontKey, triplet); + } + + private static void addFontMetricsMapper(FontInfo fontInfo, String family, String fontKey, + Graphics2D graphics, int style) { + FontMetricsMapper metric = new SystemFontMetricsMapper(family, style, graphics); + fontInfo.addMetrics(fontKey, metric); + } + + private static int convertToAWTFontStyle(String fontStyle, int fontWeight) { + int style = java.awt.Font.PLAIN; + if (fontWeight >= Font.WEIGHT_BOLD) { + style |= java.awt.Font.BOLD; + } + if (!"normal".equals(fontStyle)) { + style |= java.awt.Font.ITALIC; + } + return style; + } +} diff --git a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java index db8ed6250..eea165525 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java +++ b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java @@ -168,14 +168,15 @@ public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implem public void setupFontInfo(FontInfo inFontInfo) { //Don't call super.setupFontInfo() here! Java2D needs a special font setup // create a temp Image to test font metrics on - fontInfo = inFontInfo; + this.fontInfo = inFontInfo; BufferedImage fontImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); - Graphics2D g = fontImage.createGraphics(); + Graphics2D graphics2D = fontImage.createGraphics(); //The next line is important to get accurate font metrics! - g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, + graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - FontSetup.setup(fontInfo, fontList, fontResolver, g); + + userAgent.getFactory().getFontManager().setupRenderer(this, graphics2D); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/pcl/PCLRenderer.java b/src/java/org/apache/fop/render/pcl/PCLRenderer.java index 921856b07..5ffef15ba 100644 --- a/src/java/org/apache/fop/render/pcl/PCLRenderer.java +++ b/src/java/org/apache/fop/render/pcl/PCLRenderer.java @@ -89,7 +89,6 @@ import org.apache.fop.render.RendererContext; import org.apache.fop.render.RendererContextConstants; import org.apache.fop.render.RendererEventProducer; import org.apache.fop.render.java2d.FontMetricsMapper; -import org.apache.fop.render.java2d.FontSetup; import org.apache.fop.render.java2d.Java2DRenderer; import org.apache.fop.render.pcl.extensions.PCLElementMapping; import org.apache.fop.traits.BorderProps; @@ -202,11 +201,12 @@ public class PCLRenderer extends PrintRenderer { fontInfo = inFontInfo; BufferedImage fontImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); - Graphics2D g = fontImage.createGraphics(); + Graphics2D graphics2D = fontImage.createGraphics(); //The next line is important to get accurate font metrics! - g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, + graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - FontSetup.setup(fontInfo, fontList, fontResolver, g); + + userAgent.getFactory().getFontManager().setupRenderer(this, graphics2D); } /** diff --git a/src/java/org/apache/fop/render/ps/AbstractPSTranscoder.java b/src/java/org/apache/fop/render/ps/AbstractPSTranscoder.java index 75ed0de52..ea4cc50bb 100644 --- a/src/java/org/apache/fop/render/ps/AbstractPSTranscoder.java +++ b/src/java/org/apache/fop/render/ps/AbstractPSTranscoder.java @@ -98,7 +98,7 @@ public abstract class AbstractPSTranscoder extends AbstractFOPTranscoder { if (!isTextStroked()) { FontInfo fontInfo = new FontInfo(); //TODO Do custom font configuration here somewhere/somehow - FontSetup.setup(fontInfo, null, null); + FontSetup.setup(fontInfo); graphics.setCustomTextHandler(new NativeTextHandler(graphics, fontInfo)); } diff --git a/src/java/org/apache/fop/render/ps/NativeTextHandler.java b/src/java/org/apache/fop/render/ps/NativeTextHandler.java index 5b840484d..174fe8077 100644 --- a/src/java/org/apache/fop/render/ps/NativeTextHandler.java +++ b/src/java/org/apache/fop/render/ps/NativeTextHandler.java @@ -71,7 +71,7 @@ public class NativeTextHandler implements PSTextHandler { private void setupFontInfo() { //Sets up a FontInfo with default fonts fontInfo = new FontInfo(); - FontSetup.setup(fontInfo, null, null); + FontSetup.setup(fontInfo); } /** |