在使用Java的AWT时,创建Font
对象的成本有多高?我是否应该尽可能缓存Font
,或者它只是对AWT已经内部缓存的重量级字体的轻量级引用?
Font
对象的成本较高。如果可能的话,建议缓存Font
,因为它只是对AWT已经内部缓存的重量级字体的轻量级引用。在使用Java的AWT时,创建Font
对象的成本有多高?我是否应该尽可能缓存Font
,或者它只是对AWT已经内部缓存的重量级字体的轻量级引用?
Font
对象的成本较高。如果可能的话,建议缓存Font
,因为它只是对AWT已经内部缓存的重量级字体的轻量级引用。public Font(String name, int style, int size) {
this.name = (name != null) ? name : "Default";
this.style = (style & ~0x03) == 0 ? style : 0;
this.size = size;
this.pointSize = size;
}
private Font(File fontFile, int fontFormat,
boolean isCopy, CreatedFontTracker tracker)
throws FontFormatException {
this.createdFont = true;
/* Font2D instances created by this method track their font file
* so that when the Font2D is GC'd it can also remove the file.
*/
this.font2DHandle =
FontManager.createFont2D(fontFile, fontFormat,
isCopy, tracker).handle;
this.name = this.font2DHandle.font2D.getFontName(Locale.getDefault());
this.style = Font.PLAIN;
this.size = 1;
this.pointSize = 1f;
}
显然很笨重(特别是FontManager.createFont2D(...)
这一部分)。这个构造函数仅被Font.createFont()使用。
总的来说,如果你正在使用系统中已有的字体,那么只需创建并通过名称引用即可。如果您提供自己的字体(例如:从TrueType文件中提取),则最好将其缓存。(话虽如此,据我所知,有一种方法可以将文件简单地加载到AWT的缓存中,以便您可以仅通过名称引用它。)
进一步深入源代码,所有诸如getFamily()、getFontName()、getNumGlyphs()等函数都首先调用getFont2D(),其本质上是:
private Font2D getFont2D() {
// snip
if (font2DHandle == null) {
font2DHandle =
FontManager.findFont2D(name, style,
FontManager.LOGICAL_FALLBACK).handle;
}
/* Do not cache the de-referenced font2D. It must be explicitly
* de-referenced to pick up a valid font in the event that the
* original one is marked invalid
*/
return font2DHandle.font2D;
}
因此,这表明每个字体都是轻量级的,并且它从负责缓存字体的FontManager中提取必要的信息。