From 364ae6be67bd9e5bb43917cb74dab99af41d5086 Mon Sep 17 00:00:00 2001 From: Adrian Cumiskey Date: Tue, 29 Jul 2008 16:00:14 +0000 Subject: [PATCH] Added a base 12 (Helvetica, Times and Courier) fallback font collection for AFP environments. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@680747 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/fonts/FontInfo.java | 24 ++- .../render/afp/AFPRendererConfigurator.java | 22 +-- .../afp/fonts/AFPBase12FontCollection.java | 155 ++++++++++++++++++ .../render/afp/fonts/AFPFontCollection.java | 64 ++------ .../fop/render/afp/fonts/CharacterSet.java | 13 +- 5 files changed, 208 insertions(+), 70 deletions(-) create mode 100644 src/java/org/apache/fop/render/afp/fonts/AFPBase12FontCollection.java diff --git a/src/java/org/apache/fop/fonts/FontInfo.java b/src/java/org/apache/fop/fonts/FontInfo.java index f576c8208..81b5ff877 100644 --- a/src/java/org/apache/fop/fonts/FontInfo.java +++ b/src/java/org/apache/fop/fonts/FontInfo.java @@ -109,6 +109,19 @@ public class FontInfo { addFontProperties(name, createFontKey(family, style, weight)); } + /** + * Adds a series of new font triplets given an array of font family names. + * @param name internal key + * @param families an array of font family names + * @param style font style (normal, italic, oblique...) + * @param weight font weight + */ + public void addFontProperties(String name, String[] families, String style, int weight) { + for (int i = 0; i < families.length; i++) { + addFontProperties(name, families[i], style, weight); + } + } + /** * Adds a new font triplet. * @param internalFontKey internal font key @@ -215,7 +228,7 @@ public class FontInfo { } private FontTriplet fuzzyFontLookup(String family, String style, - int weight, FontTriplet startKey, boolean substFont) { + int weight, FontTriplet startKey, boolean substitutable) { FontTriplet key; String internalFontKey = null; if (!family.equals(startKey.getName())) { @@ -232,7 +245,8 @@ public class FontInfo { internalFontKey = getInternalFontKey(key); } - if (!substFont && internalFontKey == null) { + // return null if not found and not substitutable + if (!substitutable && internalFontKey == null) { return null; } @@ -263,12 +277,12 @@ public class FontInfo { // fallback 2: try the same font-family with default style and weight /* obsolete: replaced by the loop above - if (f == null) { + if (internalFontKey == null) { key = createFontKey(family, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); - f = getInternalFontKey(key); + internalFontKey = getInternalFontKey(key); }*/ - // fallback 3: try any family with orig style/weight + // fallback 3: try any family with original style/weight if (internalFontKey == null) { return fuzzyFontLookup("any", style, weight, startKey, false); } diff --git a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java index b843ccefb..ef269b7c0 100644 --- a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java +++ b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java @@ -46,7 +46,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator { /** * Default constructor - * + * * @param userAgent user agent */ public AFPRendererConfigurator(FOUserAgent userAgent) { @@ -184,22 +184,24 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator { /** * Builds a list of AFPFontInfo objects for use with the setup() method. - * + * * @param cfg Configuration object * @return List the newly created list of fonts * @throws ConfigurationException if something's wrong with the config data */ - private List buildFontListFromConfiguration(Configuration cfg) + private List/**/ buildFontListFromConfiguration(Configuration cfg) throws ConfigurationException { - List fontList = new java.util.ArrayList(); + List/**/ fontList = new java.util.ArrayList(); Configuration[] font = cfg.getChild("fonts").getChildren("font"); + final String fontPath = null; for (int i = 0; i < font.length; i++) { - AFPFontInfo afi = buildFont(font[i], null); + AFPFontInfo afi = buildFont(font[i], fontPath); if (afi != null) { if (log.isDebugEnabled()) { log.debug("Adding font " + afi.getAFPFont().getFontName()); - for (int j = 0; j < afi.getFontTriplets().size(); ++j) { - FontTriplet triplet = (FontTriplet) afi.getFontTriplets().get(j); + List/*FontTriplet*/ fontTriplets = afi.getFontTriplets(); + for (int j = 0; j < fontTriplets.size(); ++j) { + FontTriplet triplet = (FontTriplet) fontTriplets.get(j); log.debug(" Font triplet " + triplet.getName() + ", " + triplet.getStyle() + ", " @@ -214,7 +216,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator { /** * Configure the AFP renderer. - * + * * @param renderer AFP renderer * @throws FOPException fop exception * @see org.apache.fop.render.PrintRendererConfigurator#configure(Renderer) @@ -224,13 +226,13 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator { if (cfg != null) { AFPRenderer afpRenderer = (AFPRenderer)renderer; try { - List fontList = buildFontListFromConfiguration(cfg); + List/**/ fontList = buildFontListFromConfiguration(cfg); afpRenderer.setFontList(fontList); } catch (ConfigurationException e) { LogUtil.handleException(log, e, userAgent.getFactory().validateUserConfigStrictly()); } - + // image information Configuration imagesCfg = cfg.getChild("images"); if (!"color".equalsIgnoreCase(imagesCfg.getAttribute("mode", "b+w"))) { diff --git a/src/java/org/apache/fop/render/afp/fonts/AFPBase12FontCollection.java b/src/java/org/apache/fop/render/afp/fonts/AFPBase12FontCollection.java new file mode 100644 index 000000000..e0d56f31b --- /dev/null +++ b/src/java/org/apache/fop/render/afp/fonts/AFPBase12FontCollection.java @@ -0,0 +1,155 @@ +/* + * 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.fonts; + +import org.apache.fop.fonts.Base14Font; +import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; +import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.base14.Courier; +import org.apache.fop.fonts.base14.CourierBold; +import org.apache.fop.fonts.base14.CourierBoldOblique; +import org.apache.fop.fonts.base14.CourierOblique; +import org.apache.fop.fonts.base14.Helvetica; +import org.apache.fop.fonts.base14.HelveticaBold; +import org.apache.fop.fonts.base14.HelveticaOblique; +import org.apache.fop.fonts.base14.TimesBold; +import org.apache.fop.fonts.base14.TimesBoldItalic; +import org.apache.fop.fonts.base14.TimesItalic; +import org.apache.fop.fonts.base14.TimesRoman; + +/** + * Sets up a typical Base 12 font configuration for AFP + */ +public class AFPBase12FontCollection implements FontCollection { + + /** standard raster font sizes */ + private static final int[] RASTER_SIZES = {6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 30, 36}; + + /** standard raster font charset references */ + private static final String[] CHARSET_REF = { + "60", "70", "80", "90", "00", "A0", "B0", "D0", "F0", "H0", "J0", "N0", "T0", "Z0"}; + + private void addCharacterSet(RasterFont font, String charsetName, Base14Font base14) { + for (int i = 0; i < RASTER_SIZES.length; i++) { + int size = RASTER_SIZES[i]; + FopCharacterSet characterSet = new FopCharacterSet( + CharacterSet.DEFAULT_CODEPAGE, CharacterSet.DEFAULT_ENCODING, + charsetName + CHARSET_REF[i], size, base14); + font.addCharacterSet(size, characterSet); + } + } + + private int addFontProperties(FontInfo fontInfo, AFPFont font, + String[] names, String style, int weight, int num) { + String internalFontKey = "F" + num; + fontInfo.addMetrics(internalFontKey, font); + fontInfo.addFontProperties(internalFontKey, names, style, weight); + num++; + return num; + } + + /** {@inheritDoc} */ + public int setup(int start, FontInfo fontInfo) { + + /** + * Add the base 12 fonts (Helvetica, Times and Courier) + * + * Note: this default font configuration may not be available + * on your AFP environment. + */ + int num = start; + RasterFont font = null; + + /** standard font family reference names for Helvetica font */ + final String[] helveticaNamesPlusAny = {"Helvetica", "Arial", "sans-serif", "any"}; + font = new RasterFont("Helvetica"); + addCharacterSet(font, "C0H200", new Helvetica()); + num = addFontProperties(fontInfo, font, helveticaNamesPlusAny, + Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num); + + final String[] helveticaNames = {"Helvetica", "Arial", "sans-serif"}; + font = new RasterFont("Helvetica Italic"); + addCharacterSet(font, "C0H300", new HelveticaOblique()); + num = addFontProperties(fontInfo, font, helveticaNames, + Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num); + + font = new RasterFont("Helvetica (Semi) Bold"); + addCharacterSet(font, "C0H400", new HelveticaBold()); + num = addFontProperties(fontInfo, font, helveticaNames, + Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num); + + font = new RasterFont("Helvetica Italic (Semi) Bold"); + addCharacterSet(font, "C0H500", new HelveticaOblique()); + num = addFontProperties(fontInfo, font, helveticaNames, + Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num); + + + final String[] timesNames = {"Times", "TimesRoman", "Times Roman", "Times-Roman", + "Times New Roman", "TimesNewRoman", "serif"}; + + font = new RasterFont("Times Roman"); + addCharacterSet(font, "CON200", new TimesRoman()); + num = addFontProperties(fontInfo, font, timesNames, + Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num); + + font = new RasterFont("Times Roman Italic"); + addCharacterSet(font, "CON300", new TimesItalic()); + num = addFontProperties(fontInfo, font, timesNames, + Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num); + + font = new RasterFont("Times Roman Bold"); + addCharacterSet(font, "CON400", new TimesBold()); + num = addFontProperties(fontInfo, font, timesNames, + Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num); + + font = new RasterFont("Times Roman Italic Bold"); + addCharacterSet(font, "CON500", new TimesBoldItalic()); + num = addFontProperties(fontInfo, font, timesNames, + Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num); + + + /** standard font family reference names for Courier font */ + final String[] courierNames = {"Courier", "monospace"}; + + font = new RasterFont("Courier"); + addCharacterSet(font, "C04200", new Courier()); + num = addFontProperties(fontInfo, font, courierNames, + Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num); + + font = new RasterFont("Courier Italic"); + addCharacterSet(font, "C04300", new CourierOblique()); + num = addFontProperties(fontInfo, font, courierNames, + Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num); + + font = new RasterFont("Courier Bold"); + addCharacterSet(font, "C04400", new CourierBold()); + num = addFontProperties(fontInfo, font, courierNames, + Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num); + + font = new RasterFont("Courier Italic Bold"); + addCharacterSet(font, "C04500", new CourierBoldOblique()); + num = addFontProperties(fontInfo, font, courierNames, + Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num); + + return num; + } + +} diff --git a/src/java/org/apache/fop/render/afp/fonts/AFPFontCollection.java b/src/java/org/apache/fop/render/afp/fonts/AFPFontCollection.java index bce798751..9b8c52cdb 100644 --- a/src/java/org/apache/fop/render/afp/fonts/AFPFontCollection.java +++ b/src/java/org/apache/fop/render/afp/fonts/AFPFontCollection.java @@ -23,13 +23,9 @@ import java.util.Iterator; import java.util.List; import org.apache.fop.events.EventBroadcaster; -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.base14.Courier; -import org.apache.fop.fonts.base14.Helvetica; -import org.apache.fop.fonts.base14.TimesRoman; import org.apache.fop.render.afp.AFPEventProducer; /** @@ -37,34 +33,29 @@ import org.apache.fop.render.afp.AFPEventProducer; */ public class AFPFontCollection implements FontCollection { - private static final String DEFAULT_CODEPAGE = "T1V10500"; - - private static final String DEFAULT_ENCODING = "Cp500"; + private final EventBroadcaster eventBroadcaster; - - private EventBroadcaster eventBroadcaster; - - private List/**/ embedFontInfoList; + private final List/**/ fontInfoList; /** * Main constructor * * @param eventBroadcaster the event broadcaster - * @param embedFontInfoList the embed font info list + * @param fontInfoList the font info list */ public AFPFontCollection(EventBroadcaster eventBroadcaster, - List/**/ embedFontInfoList) { + List/**/ fontInfoList) { this.eventBroadcaster = eventBroadcaster; - this.embedFontInfoList = embedFontInfoList; + this.fontInfoList = fontInfoList; } - + /** {@inheritDoc} */ public int setup(int start, FontInfo fontInfo) { int num = 1; - if (embedFontInfoList != null && embedFontInfoList.size() > 0) { - for (Iterator it = embedFontInfoList.iterator(); it.hasNext();) { + if (fontInfoList != null && fontInfoList.size() > 0) { + for (Iterator it = fontInfoList.iterator(); it.hasNext();) { AFPFontInfo afpFontInfo = (AFPFontInfo)it.next(); - AFPFont afpFont = (AFPFont)afpFontInfo.getAFPFont(); + AFPFont afpFont = afpFontInfo.getAFPFont(); List/**/ tripletList = afpFontInfo.getFontTriplets(); for (Iterator it2 = tripletList.iterator(); it2.hasNext();) { FontTriplet triplet = (FontTriplet)it2.next(); @@ -77,41 +68,10 @@ public class AFPFontCollection implements FontCollection { } else { AFPEventProducer eventProducer = AFPEventProducer.Provider.get(eventBroadcaster); eventProducer.warnDefaultFontSetup(this); - } - // note: these fonts may not exist on your AFP installation - if (fontInfo.fontLookup("sans-serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL) == null) { - CharacterSet cs = new FopCharacterSet(DEFAULT_CODEPAGE, DEFAULT_ENCODING, "CZH200", - 1, new Helvetica()); - AFPFont bf = new OutlineFont("Helvetica", cs); - fontInfo.addFontProperties( - "F" + num, "sans-serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); - fontInfo.addMetrics("F" + num, bf); - num++; - } - if (fontInfo.fontLookup("serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL) == null) { - CharacterSet cs = new FopCharacterSet(DEFAULT_CODEPAGE, DEFAULT_ENCODING, "CZN200", - 1, new TimesRoman()); - AFPFont bf = new OutlineFont("Helvetica", cs); - fontInfo.addFontProperties("F" + num, "serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); - fontInfo.addMetrics("F" + num, bf); - num++; - } - if (fontInfo.fontLookup("monospace", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL) == null) { - CharacterSet cs = new FopCharacterSet(DEFAULT_CODEPAGE, DEFAULT_ENCODING, "CZ4200", - 1, new Courier()); - AFPFont bf = new OutlineFont("Helvetica", cs); - fontInfo.addFontProperties( - "F" + num, "monospace", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); - fontInfo.addMetrics("F" + num, bf); - num++; - } - if (fontInfo.fontLookup("any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL) == null) { - FontTriplet ft = fontInfo.fontLookup( - "sans-serif", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); - fontInfo.addFontProperties( - fontInfo.getInternalFontKey(ft), "any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL); - num++; + // Go with a default base 12 configuration for AFP environments + FontCollection base12FontCollection = new AFPBase12FontCollection(); + num = base12FontCollection.setup(num, fontInfo); } return num; } diff --git a/src/java/org/apache/fop/render/afp/fonts/CharacterSet.java b/src/java/org/apache/fop/render/afp/fonts/CharacterSet.java index f3312c104..d33f093f1 100644 --- a/src/java/org/apache/fop/render/afp/fonts/CharacterSet.java +++ b/src/java/org/apache/fop/render/afp/fonts/CharacterSet.java @@ -49,8 +49,15 @@ public class CharacterSet { /** Static logging instance */ protected static final Log log = LogFactory.getLog(CharacterSet.class.getName()); + /** default codepage */ + protected static final String DEFAULT_CODEPAGE = "T1V10500"; + + /** default encoding */ + protected static final String DEFAULT_ENCODING = "Cp500"; + private static final int MAX_NAME_LEN = 8; - + + /** The code page to which the character set relates */ protected String codePage; @@ -67,7 +74,7 @@ public class CharacterSet { private boolean isMetricsLoaded = false; /** The current orientation (currently only 0 is supported by FOP) */ - private String currentOrientation = "0"; + private final String currentOrientation = "0"; /** The collection of objects for each orientation */ private Map characterSetOrientations = null; @@ -195,7 +202,7 @@ public class CharacterSet { /** * XHeight refers to the height of the lower case letters above the baseline. - * + * * @return the typical height of characters */ public int getXHeight() { -- 2.39.5