如何确定 Java 中请求的字体所获得的字体?
当我在 Java 下请求字体时,如果该字体不存在,我需要确定它映射到什么字体(以便我可以记录正在使用的映射)。在Java 1.6下我可以使用:
Font logicalFont = new java.awt.Font(fontname, java.awt.Font.PLAIN, 12);
for (int i = 0; i < FontManager.getRegisteredFonts().length; i++) {
Font2D font2D = FontManager.getRegisteredFonts()[i];
if (font2D instanceof CompositeFont && font2D.getFontName(Locale.getDefault()).equals(logicalFont.getFontName())) {
PhysicalFont physicalFont = ((CompositeFont) font2D).getSlotFont(0);
log.warn("The requested font '" + fontname + "' does not exist on this system. Substituting the font '" + physicalFont.getFamilyName(Locale.getDefault()) + "'");
return;
}
}
但在Java 1.7 FontManager没有 getRegisteredFonts() 静态成员(我认为它现在实际上是一个接口)。那么如何获取映射呢?
谢谢-戴夫
When I request a font under Java, if that font does not exist, I need to determine what font it maps to (so I can log the mapping in use). Under Java 1.6 I could use:
Font logicalFont = new java.awt.Font(fontname, java.awt.Font.PLAIN, 12);
for (int i = 0; i < FontManager.getRegisteredFonts().length; i++) {
Font2D font2D = FontManager.getRegisteredFonts()[i];
if (font2D instanceof CompositeFont && font2D.getFontName(Locale.getDefault()).equals(logicalFont.getFontName())) {
PhysicalFont physicalFont = ((CompositeFont) font2D).getSlotFont(0);
log.warn("The requested font '" + fontname + "' does not exist on this system. Substituting the font '" + physicalFont.getFamilyName(Locale.getDefault()) + "'");
return;
}
}
But in Java 1.7 FontManager has no getRegisteredFonts() static member (I think it's actually an interface now). So how can I get the mapping?
thanks - dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 http://www.docjar.com/docs/api/sun/ java2d/SunGraphicsEnvironment.html。那里似乎有一些方法,比如 getAllFonts(),可以完成 getRegisteredFonts 过去所做的事情。
Try http://www.docjar.com/docs/api/sun/java2d/SunGraphicsEnvironment.html. There seem to be methods there, like getAllFonts(), that do what getRegisteredFonts used to do.