diff options
author | Jeremias Maerki <jeremias@apache.org> | 2006-02-10 14:39:34 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2006-02-10 14:39:34 +0000 |
commit | 17094f3329733ab1e0b68b8e9b3c302382572f25 (patch) | |
tree | 848c68e3210147b900273d3a6da29825ccc3753a | |
parent | 77f908f5cce1fea0b4b5f694b31a4070bd33cfb8 (diff) | |
download | xmlgraphics-fop-17094f3329733ab1e0b68b8e9b3c302382572f25.tar.gz xmlgraphics-fop-17094f3329733ab1e0b68b8e9b3c302382572f25.zip |
Creation of Font instances centralized in FontInfo and added a cache for them. This removes duplicate code in a number of places, improves speed a little and saves memory.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@376706 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/java/org/apache/fop/area/AreaTreeParser.java | 3 | ||||
-rwxr-xr-x | src/java/org/apache/fop/fo/properties/CommonFont.java | 9 | ||||
-rw-r--r-- | src/java/org/apache/fop/fonts/FontInfo.java | 29 | ||||
-rw-r--r-- | src/java/org/apache/fop/fonts/FontSetup.java | 2 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/PrintRenderer.java | 13 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/java2d/Java2DRenderer.java | 15 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/ps/PSGraphics2D.java | 6 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/ps/PSTextPainter.java | 15 | ||||
-rw-r--r-- | src/java/org/apache/fop/svg/PDFGraphics2D.java | 11 | ||||
-rw-r--r-- | src/java/org/apache/fop/svg/PDFTextPainter.java | 15 |
10 files changed, 53 insertions, 65 deletions
diff --git a/src/java/org/apache/fop/area/AreaTreeParser.java b/src/java/org/apache/fop/area/AreaTreeParser.java index 075119d21..13643f5ef 100644 --- a/src/java/org/apache/fop/area/AreaTreeParser.java +++ b/src/java/org/apache/fop/area/AreaTreeParser.java @@ -54,6 +54,7 @@ import org.apache.fop.fo.Constants; import org.apache.fop.fo.ElementMappingRegistry; import org.apache.fop.fo.extensions.ExtensionAttachment; import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontTriplet; import org.apache.fop.image.FopImage; import org.apache.fop.image.ImageFactory; @@ -933,7 +934,7 @@ public class AreaTreeParser { int fontWeight = getAttributeAsInteger( attributes, "font-weight", Font.NORMAL); area.addTrait(trait, - new FontTriplet(fontName, fontStyle, fontWeight)); + FontInfo.createFontKey(fontName, fontStyle, fontWeight)); } } } diff --git a/src/java/org/apache/fop/fo/properties/CommonFont.java b/src/java/org/apache/fop/fo/properties/CommonFont.java index 581126cea..d9bcdf5b5 100755 --- a/src/java/org/apache/fop/fo/properties/CommonFont.java +++ b/src/java/org/apache/fop/fo/properties/CommonFont.java @@ -150,7 +150,7 @@ public class CommonFont { } } - String style = "normal"; + String style; switch (fontStyle) { case Constants.EN_ITALIC: style = "italic"; @@ -161,16 +161,15 @@ public class CommonFont { case Constants.EN_BACKSLANT: style = "backslant"; break; + default: + style = "normal"; } // NOTE: this is incomplete. font-size may be specified with // various kinds of keywords too //int fontVariant = propertyList.get("font-variant").getEnum(); FontTriplet triplet = fontInfo.fontLookup(getFontFamily(), style, font_weight); - String fname = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fname); - FontMetrics metrics = fontInfo.getMetricsFor(fname); - fontState = new Font(fname, triplet, metrics, fontSize.getValue(context)); + fontState = fontInfo.getFontInstance(triplet, fontSize.getValue(context)); } return fontState; } diff --git a/src/java/org/apache/fop/fonts/FontInfo.java b/src/java/org/apache/fop/fonts/FontInfo.java index 38059c6ba..4bd315941 100644 --- a/src/java/org/apache/fop/fonts/FontInfo.java +++ b/src/java/org/apache/fop/fonts/FontInfo.java @@ -54,6 +54,9 @@ public class FontInfo { private Collection loggedFontKeys; + /** Cache for Font instances. */ + private Map fontInstanceCache = new java.util.HashMap(); + /** * Main constructor */ @@ -80,7 +83,7 @@ public class FontInfo { * @param weight font weight */ public void addFontProperties(String name, String family, String style, int weight) { - addFontProperties(name, new FontTriplet(family, style, weight)); + addFontProperties(name, createFontKey(family, style, weight)); } /** @@ -164,6 +167,30 @@ public class FontInfo { } /** + * Retrieves a (possibly cached) Font instance based on a FontTriplet and a font size. + * @param triplet the font triplet designating the requested font + * @param fontSize the font size + * @return the requested Font instance + */ + public Font getFontInstance(FontTriplet triplet, int fontSize) { + Map sizes = (Map)fontInstanceCache.get(triplet); + if (sizes == null) { + sizes = new java.util.HashMap(); + fontInstanceCache.put(triplet, sizes); + } + Integer size = new Integer(fontSize); + Font font = (Font)sizes.get(size); + if (font == null) { + String fname = getInternalFontKey(triplet); + useFont(fname); + FontMetrics metrics = getMetricsFor(fname); + font = new Font(fname, triplet, metrics, fontSize); + sizes.put(size, font); + } + return font; + } + + /** * Lookup a font. * <br> * Locate the font name for a given family, style and weight. diff --git a/src/java/org/apache/fop/fonts/FontSetup.java b/src/java/org/apache/fop/fonts/FontSetup.java index 8ac36bbd5..140fcb8b8 100644 --- a/src/java/org/apache/fop/fonts/FontSetup.java +++ b/src/java/org/apache/fop/fonts/FontSetup.java @@ -227,7 +227,7 @@ public class FontSetup { List tripleList = new java.util.ArrayList(); for (int j = 0; j < triple.length; j++) { int weight = FontUtil.parseCSS2FontWeight(triple[j].getAttribute("weight")); - tripleList.add(new FontTriplet(triple[j].getAttribute("name"), + tripleList.add(FontInfo.createFontKey(triple[j].getAttribute("name"), triple[j].getAttribute("style"), weight)); } diff --git a/src/java/org/apache/fop/render/PrintRenderer.java b/src/java/org/apache/fop/render/PrintRenderer.java index f1e943c56..585017a11 100644 --- a/src/java/org/apache/fop/render/PrintRenderer.java +++ b/src/java/org/apache/fop/render/PrintRenderer.java @@ -23,7 +23,6 @@ import org.apache.fop.area.Area; import org.apache.fop.area.Trait; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontInfo; -import org.apache.fop.fonts.FontMetrics; import org.apache.fop.fonts.FontSetup; import org.apache.fop.fonts.FontTriplet; @@ -64,19 +63,11 @@ public abstract class PrintRenderer extends AbstractRenderer { * Returns a Font object constructed based on the font traits in an area * @param area the area from which to retrieve the font triplet information * @return the requested Font instance or null if not found - * @todo This would make a nice opportunity for a cache! */ protected Font getFontFromArea(Area area) { FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT); - String name = fontInfo.getInternalFontKey(triplet); - if (name != null) { - int size = ((Integer)area.getTrait(Trait.FONT_SIZE)).intValue(); - FontMetrics metrics = fontInfo.getMetricsFor(name); - Font font = new Font(name, null, metrics, size); - return font; - } else { - return null; - } + int size = ((Integer)area.getTrait(Trait.FONT_SIZE)).intValue(); + return fontInfo.getFontInstance(triplet, size); } /** diff --git a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java index 173595cce..5255c7727 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java +++ b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2005 The Apache Software Foundation. + * Copyright 1999-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -69,7 +69,6 @@ import org.apache.fop.datatypes.ColorType; import org.apache.fop.fo.Constants; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontInfo; -import org.apache.fop.fonts.FontMetrics; import org.apache.fop.fonts.FontTriplet; import org.apache.fop.image.FopImage; import org.apache.fop.image.ImageFactory; @@ -831,19 +830,11 @@ public abstract class Java2DRenderer extends AbstractRenderer implements Printab * Returns a Font object constructed based on the font traits in an area * @param area the area from which to retrieve the font triplet information * @return the requested Font instance or null if not found - * @todo This would make a nice opportunity for a cache! */ protected Font getFontFromArea(Area area) { FontTriplet triplet = (FontTriplet)area.getTrait(Trait.FONT); - String name = fontInfo.getInternalFontKey(triplet); - if (name != null) { - int size = ((Integer)area.getTrait(Trait.FONT_SIZE)).intValue(); - FontMetrics metrics = fontInfo.getMetricsFor(name); - Font font = new Font(name, null, metrics, size); - return font; - } else { - return null; - } + int size = ((Integer)area.getTrait(Trait.FONT_SIZE)).intValue(); + return fontInfo.getFontInstance(triplet, size); } /** diff --git a/src/java/org/apache/fop/render/ps/PSGraphics2D.java b/src/java/org/apache/fop/render/ps/PSGraphics2D.java index f85d0474a..5522656e2 100644 --- a/src/java/org/apache/fop/render/ps/PSGraphics2D.java +++ b/src/java/org/apache/fop/render/ps/PSGraphics2D.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2005 The Apache Software Foundation. + * Copyright 1999-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -920,9 +920,7 @@ public class PSGraphics2D extends AbstractGraphics2D { if (triplet == null) { triplet = fontInfo.findAdjustWeight("sans-serif", style, weight); } - String fontKey = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fontKey); - return new Font(fontKey, triplet, fontInfo.getMetricsFor(fontKey), fontSize); + return fontInfo.getFontInstance(triplet, fontSize); } private void establishCurrentFont() throws IOException { diff --git a/src/java/org/apache/fop/render/ps/PSTextPainter.java b/src/java/org/apache/fop/render/ps/PSTextPainter.java index aba67759c..db654adb2 100644 --- a/src/java/org/apache/fop/render/ps/PSTextPainter.java +++ b/src/java/org/apache/fop/render/ps/PSTextPainter.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2004,2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,6 @@ import org.apache.batik.gvt.renderer.StrokingTextPainter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.fop.fonts.FontMetrics; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontTriplet; @@ -62,7 +61,7 @@ import org.apache.fop.fonts.FontTriplet; * (todo) use drawString(AttributedCharacterIterator iterator...) for some * * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a> - * @version $Id: PSTextPainter.java,v 1.15 2003/01/08 14:03:55 jeremias Exp $ + * @version $Id$ */ public class PSTextPainter implements TextPainter { @@ -387,20 +386,14 @@ public class PSTextPainter implements TextPainter { if (fontInfo.hasFont(fontFamily, style, weight)) { FontTriplet triplet = fontInfo.fontLookup( fontFamily, style, weight); - String fname = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fname); - FontMetrics metrics = fontInfo.getMetricsFor(fname); int fsize = (int)(fontSize.floatValue() * 1000); - return new Font(fname, triplet, metrics, fsize); + return fontInfo.getFontInstance(triplet, fsize); } } } FontTriplet triplet = fontInfo.fontLookup("any", style, Font.NORMAL); - String fname = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fname); - FontMetrics metrics = fontInfo.getMetricsFor(fname); int fsize = (int)(fontSize.floatValue() * 1000); - return new Font(fname, triplet, metrics, fsize); + return fontInfo.getFontInstance(triplet, fsize); } private java.awt.Font makeAWTFont(AttributedCharacterIterator aci, Font font) { diff --git a/src/java/org/apache/fop/svg/PDFGraphics2D.java b/src/java/org/apache/fop/svg/PDFGraphics2D.java index 732cd95bd..9a8ef1d7a 100644 --- a/src/java/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/java/org/apache/fop/svg/PDFGraphics2D.java @@ -35,7 +35,6 @@ import org.apache.fop.pdf.BitmapImage; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontSetup; -import org.apache.fop.fonts.FontMetrics; import org.apache.fop.fonts.FontTriplet; import org.apache.fop.fonts.LazyFont; import org.apache.fop.image.JpegImage; @@ -1396,14 +1395,10 @@ public class PDFGraphics2D extends AbstractGraphics2D { String style = gFont.isItalic() ? "italic" : "normal"; int weight = gFont.isBold() ? Font.BOLD : Font.NORMAL; FontTriplet triplet = fontInfo.fontLookup(n, style, weight); - String fname = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fname); - FontMetrics metrics = fontInfo.getMetricsFor(fname); - fontState = new Font(fname, triplet, metrics, siz * 1000); + fontState = fontInfo.getFontInstance(triplet, siz * 1000); } else { - FontMetrics metrics = fontInfo.getMetricsFor(ovFontState.getFontName()); - fontState = new Font(ovFontState.getFontName(), ovFontState.getFontTriplet(), - metrics, ovFontState.getFontSize()); + fontState = fontInfo.getFontInstance( + ovFontState.getFontTriplet(), ovFontState.getFontSize()); ovFontState = null; } String name; diff --git a/src/java/org/apache/fop/svg/PDFTextPainter.java b/src/java/org/apache/fop/svg/PDFTextPainter.java index c9c4a0c64..6747b8409 100644 --- a/src/java/org/apache/fop/svg/PDFTextPainter.java +++ b/src/java/org/apache/fop/svg/PDFTextPainter.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2004,2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,6 @@ import org.apache.batik.gvt.font.GVTFontFamily; import org.apache.batik.bridge.SVGFontFamily; import org.apache.batik.gvt.renderer.StrokingTextPainter; -import org.apache.fop.fonts.FontMetrics; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontTriplet; @@ -58,7 +57,7 @@ import org.apache.fop.fonts.FontTriplet; * (todo) use drawString(AttributedCharacterIterator iterator...) for some * * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a> - * @version $Id: PDFTextPainter.java,v 1.16 2003/03/07 09:51:25 jeremias Exp $ + * @version $Id$ */ public class PDFTextPainter implements TextPainter { private FontInfo fontInfo; @@ -179,11 +178,8 @@ public class PDFTextPainter implements TextPainter { if (fi.hasFont(fontFamily, style, weight)) { FontTriplet triplet = fontInfo.fontLookup(fontFamily, style, weight); - String fname = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fname); - FontMetrics metrics = fontInfo.getMetricsFor(fname); int fsize = (int)(size.floatValue() * 1000); - fontState = new Font(fname, triplet, metrics, fsize); + fontState = fontInfo.getFontInstance(triplet, fsize); found = true; break; } @@ -191,11 +187,8 @@ public class PDFTextPainter implements TextPainter { } if (!found) { FontTriplet triplet = fontInfo.fontLookup("any", style, Font.NORMAL); - String fname = fontInfo.getInternalFontKey(triplet); - fontInfo.useFont(fname); - FontMetrics metrics = fontInfo.getMetricsFor(fname); int fsize = (int)(size.floatValue() * 1000); - fontState = new Font(fname, triplet, metrics, fsize); + fontState = fontInfo.getFontInstance(triplet, fsize); } else { if (g2d instanceof PDFGraphics2D) { ((PDFGraphics2D) g2d).setOverrideFontState(fontState); |