Java 字体系列名称差异
在我的 JDK6 下的 WinXP 机器上,以下代码:
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class TestFontFamily
{
public static void main(String[] args)
{
for( String s : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() )
{
Font f = Font.decode(s);
if( f != null && !s.equals(f.getFamily()) )
System.out.println(String.format("%-20s %s", s, f));
}
}
}
打印以下列表:
Bauhaus 93 java.awt.Font[family=Dialog,name=Bauhaus,style=plain,size=93]
Bookshelf Symbol 7 java.awt.Font[family=Dialog,name=Bookshelf Symbol,style=plain,size=7]
Britannic Bold java.awt.Font[family=Dialog,name=Britannic,style=bold,size=12]
Harlow Solid Italic java.awt.Font[family=Dialog,name=Harlow Solid,style=italic,size=12]
Modern No. 20 java.awt.Font[family=Dialog,name=Modern No.,style=plain,size=20]
Wingdings 2 java.awt.Font[family=Wingdings,name=Wingdings,style=plain,size=2]
Wingdings 3 java.awt.Font[family=Wingdings,name=Wingdings,style=plain,size=3]
那么问题是为什么 Font#decode
中的字体系列名称不同?我应该使用哪一个?
似乎两者都有问题:getAvailableFontFamilyNames()
包括样式/大小,而我从Font#decode
得到的可能有错误的家族/名称。
那么一个额外的问题是:获取字体列表的可靠方法是什么?
以下代码也不起作用,因为某些字体的“名称”与“家族名称”不同,例如“Berlin Sans FB Demi Bold”的姓氏是“Berlin Sans FB Demi”。
for( Font f : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts() )
{
if( f.getFamily().equals(f.getName()) )
System.out.println(f);
}
On my WinXP box under JDK6, the following code:
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class TestFontFamily
{
public static void main(String[] args)
{
for( String s : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() )
{
Font f = Font.decode(s);
if( f != null && !s.equals(f.getFamily()) )
System.out.println(String.format("%-20s %s", s, f));
}
}
}
prints the following list:
Bauhaus 93 java.awt.Font[family=Dialog,name=Bauhaus,style=plain,size=93]
Bookshelf Symbol 7 java.awt.Font[family=Dialog,name=Bookshelf Symbol,style=plain,size=7]
Britannic Bold java.awt.Font[family=Dialog,name=Britannic,style=bold,size=12]
Harlow Solid Italic java.awt.Font[family=Dialog,name=Harlow Solid,style=italic,size=12]
Modern No. 20 java.awt.Font[family=Dialog,name=Modern No.,style=plain,size=20]
Wingdings 2 java.awt.Font[family=Wingdings,name=Wingdings,style=plain,size=2]
Wingdings 3 java.awt.Font[family=Wingdings,name=Wingdings,style=plain,size=3]
So the question is why font family name from Font#decode
is different? And which one should I use?
It seems both have problems: getAvailableFontFamilyNames()
includes style/size, and what I get from Font#decode
may have wrong family/name.
A bonus question is then: What is a reliable way to get a list of fonts?
The following code does not work either, as some fonts' "name" differs from "family name", e.g. the family name of "Berlin Sans FB Demi Bold" is "Berlin Sans FB Demi".
for( Font f : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts() )
{
if( f.getFamily().equals(f.getName()) )
System.out.println(f);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(我想知道回答我自己的风滚草问题可能会得到什么徽章。)
我发现
Font.decode(s + " plain")
为我提供了具有正确家族名称的字体。(I wonder what badge I may get for answering my own tumbleweed question.)
I found that
Font.decode(s + " plain")
gives me fonts with the right family names.