From: Kelly Campbell Date: Mon, 2 Apr 2001 03:45:38 +0000 (+0000) Subject: Refactored FontState so it doesn't do excessive font lookups per character. X-Git-Tag: fop-0_18_1~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d22dd92a6166e2d6307fe5eb7112f2d2c396b5d2;p=xmlgraphics-fop.git Refactored FontState so it doesn't do excessive font lookups per character. PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194187 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/layout/FontInfo.java b/src/org/apache/fop/layout/FontInfo.java index 84a2a4979..884b7fa39 100644 --- a/src/org/apache/fop/layout/FontInfo.java +++ b/src/org/apache/fop/layout/FontInfo.java @@ -71,7 +71,7 @@ public class FontInfo { /* add the given family, style and weight as a lookup for the font with the given name */ - String key = family + "," + style + "," + weight; + String key = createFontKey(family,style,weight); this.triplets.put(key,name); } @@ -81,39 +81,57 @@ public class FontInfo { this.fonts.put(name,metrics); } - public String fontLookup(String family, String style, String weight) throws FOPException { - // given a family, style and weight, return the font name - int i; - - try { - i = Integer.parseInt(weight); - } catch (NumberFormatException e) { - i = 0; + public String fontLookup(String family, String style, String weight) + throws FOPException + { + return fontLookup(createFontKey(family,style,weight)); + } + + public String fontLookup(String key) + throws FOPException + { + + String f = (String)this.triplets.get(key); + if (f == null) { + int i = key.indexOf(','); + String s = "any"+key.substring(i); + f = (String)this.triplets.get(s); + if (f == null) { + f = (String)this.triplets.get("any,normal,normal"); + if (f == null) { + throw new FOPException("no default font defined by OutputConverter"); + } + MessageHandler.errorln("WARNING: defaulted font to any,normal,normal"); + } + MessageHandler.errorln("WARNING: unknown font "+key+" so defaulted font to any"); + } + + usedFonts.put(f, fonts.get(f)); + return f; } - if (i > 600) - weight = "bold"; - else if (i > 0) - weight = "normal"; - - String key = family + "," + style + "," + weight; - - String f = (String)this.triplets.get(key); - if (f == null) { - f = (String)this.triplets.get("any," + style + "," + weight); - if (f == null) { - f = (String)this.triplets.get("any,normal,normal"); - if (f == null) { - throw new FOPException("no default font defined by OutputConverter"); - } - MessageHandler.errorln("WARNING: defaulted font to any,normal,normal"); - } - MessageHandler.errorln("WARNING: unknown font "+family+" so defaulted font to any"); + /** + * Creates a key from the given strings + */ + public static String createFontKey(String family, String style, String weight) + { + int i; + + try { + i = Integer.parseInt(weight); + } catch (NumberFormatException e) { + i = 0; + } + + if (i > 600) + weight = "bold"; + else if (i > 0) + weight = "normal"; + + return family + "," + style + "," + weight; } + - usedFonts.put(f, fonts.get(f)); - return f; - } public Hashtable getFonts() { return this.fonts; @@ -128,8 +146,4 @@ public class FontInfo { return (FontMetric)fonts.get(fontName); } - public FontMetric getMetricsFor(String family, String style, String weight) throws FOPException { - // given a family, style and weight, return the metric - return (FontMetric)fonts.get(fontLookup(family,style,weight)); - } } diff --git a/src/org/apache/fop/layout/FontState.java b/src/org/apache/fop/layout/FontState.java index e000b5e77..8bb1851e4 100644 --- a/src/org/apache/fop/layout/FontState.java +++ b/src/org/apache/fop/layout/FontState.java @@ -1,159 +1,116 @@ -/*-- $Id$ -- - - ============================================================================ - The Apache Software License, Version 1.1 - ============================================================================ - - Copyright (C) 1999 The Apache Software Foundation. All rights reserved. - - Redistribution and use in source and binary forms, with or without modifica- - tion, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - 3. The end-user documentation included with the redistribution, if any, must - include the following acknowledgment: "This product includes software - developed by the Apache Software Foundation (http://www.apache.org/)." - Alternately, this acknowledgment may appear in the software itself, if - and wherever such third-party acknowledgments normally appear. - - 4. The names "Fop" and "Apache Software Foundation" must not be used to - endorse or promote products derived from this software without prior - written permission. For written permission, please contact - apache@apache.org. - - 5. Products derived from this software may not be called "Apache", nor may - "Apache" appear in their name, without prior written permission of the - Apache Software Foundation. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources." + */ - This software consists of voluntary contributions made by many individuals - on behalf of the Apache Software Foundation and was originally created by - James Tauber . For more information on the Apache - Software Foundation, please see . +// -- $Id$ -- - */ package org.apache.fop.layout; +import java.util.Hashtable; + import org.apache.fop.apps.FOPException; import org.apache.fop.fo.properties.FontVariant; public class FontState { - protected FontInfo fontInfo; - private String fontName; - private int fontSize; - private String fontFamily; - private String fontStyle; - private String fontWeight; - private FontMetric metric; - private int fontVariant; - - public FontState(FontInfo fontInfo, String fontFamily, String fontStyle, String fontWeight, int fontSize, int fontVariant) throws FOPException { - this.fontInfo = fontInfo; - this.fontFamily = fontFamily; - this.fontStyle = fontStyle; - this.fontWeight = fontWeight; - this.fontSize = fontSize; - this.fontName = fontInfo.fontLookup(fontFamily,fontStyle,fontWeight); - this.metric = fontInfo.getMetricsFor(fontName); - this.fontVariant = fontVariant; + private FontInfo _fontInfo; + private String _fontName; + private int _fontSize; + private String _fontFamily; + private String _fontStyle; + private String _fontWeight; + private int _fontVariant; + + private FontMetric _metric; + + private static Hashtable EMPTY_HASHTABLE = new Hashtable(); + + + public FontState(FontInfo fontInfo, String fontFamily, + String fontStyle, String fontWeight, + int fontSize, int fontVariant) + throws FOPException + { + _fontInfo = fontInfo; + _fontFamily = fontFamily; + _fontStyle = fontStyle; + _fontWeight = fontWeight; + _fontSize = fontSize; + _fontName = fontInfo.fontLookup(fontFamily,fontStyle,fontWeight); + _metric = fontInfo.getMetricsFor(_fontName); + _fontVariant = fontVariant; } public int getAscender() { - return metric.getAscender(fontSize) / 1000; + return _metric.getAscender(_fontSize) / 1000; } public int getCapHeight() { - return metric.getCapHeight(fontSize) / 1000; + return _metric.getCapHeight(_fontSize) / 1000; } public int getDescender() { - return metric.getDescender(fontSize) / 1000; + return _metric.getDescender(_fontSize) / 1000; } public String getFontName() { - return this.fontName; + return _fontName; } public int getFontSize() { - return this.fontSize; + return _fontSize; } public String getFontWeight() { - return this.fontWeight; + return _fontWeight; } public String getFontFamily() { - return this.fontFamily; + return _fontFamily; } public String getFontStyle() { - return this.fontStyle; + return _fontStyle; } public int getFontVariant() { - return this.fontVariant; + return _fontVariant; } public FontInfo getFontInfo() { - return this.fontInfo; + return _fontInfo; } public int getXHeight() { - return metric.getXHeight(fontSize) / 1000; + return _metric.getXHeight(_fontSize) / 1000; } - public java.util.Hashtable getKerning() { - java.util.Hashtable ret=new java.util.Hashtable(); - try { - FontMetric fm=fontInfo.getMetricsFor(fontFamily, fontStyle, - fontWeight); - if (fm instanceof org.apache.fop.layout.FontDescriptor) { - org.apache.fop.layout.FontDescriptor fdes= - (org.apache.fop.layout.FontDescriptor)fm; - ret=fdes.getKerningInfo(); - } - } catch (Exception e) {} - return ret; + public Hashtable getKerning() { + if (_metric instanceof FontDescriptor) { + Hashtable ret = ((FontDescriptor)_metric).getKerningInfo(); + if (ret != null) return ret; + } + return EMPTY_HASHTABLE; } public int width(int charnum) { - // returns width of given character number in millipoints - return (metric.width(charnum, fontSize) / 1000); + // returns width of given character number in millipoints + return (_metric.width(charnum, _fontSize) / 1000); } - /** - * Map a java character (unicode) to a font character - * Default uses CodePointMapping - */ + /** + * Map a java character (unicode) to a font character + * Default uses CodePointMapping + */ public char mapChar(char c) { - try { - FontMetric fm=fontInfo.getMetricsFor(fontFamily, fontStyle, - fontWeight); - if (fm instanceof org.apache.fop.render.pdf.Font) { - org.apache.fop.render.pdf.Font f= - (org.apache.fop.render.pdf.Font)fm; - return f.mapChar(c); - } - } catch (Exception e) {} - - // Use default CodePointMapping + + if (_metric instanceof org.apache.fop.render.pdf.Font) { + return ((org.apache.fop.render.pdf.Font)_metric).mapChar(c); + } + + // Use default CodePointMapping if (c > 127) { char d = org.apache.fop.render.pdf.CodePointMapping.map[c]; if (d != 0) {