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;
int fontWeight = getAttributeAsInteger(
attributes, "font-weight", Font.NORMAL);
area.addTrait(trait,
- new FontTriplet(fontName, fontStyle, fontWeight));
+ FontInfo.createFontKey(fontName, fontStyle, fontWeight));
}
}
}
}
}
- String style = "normal";
+ String style;
switch (fontStyle) {
case Constants.EN_ITALIC:
style = "italic";
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;
}
private Collection loggedFontKeys;
+ /** Cache for Font instances. */
+ private Map fontInstanceCache = new java.util.HashMap();
+
/**
* Main constructor
*/
* @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));
}
/**
usedFonts.put(internalName, fonts.get(internalName));
}
+ /**
+ * 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>
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));
}
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;
* 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);
}
/**
/*
- * 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.
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;
* 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);
}
/**
/*
- * 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.
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 {
/*
- * 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.
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;
* (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 {
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) {
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;
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;
/*
- * 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.
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;
* (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;
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;
}
}
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);