如何在 Java 中使用 Open Type 字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/872569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Open Type Fonts in Java?
提问by HaBaLeS
Is there a way to read Open Type fonts in Java the same way as I do with TrueType fonts?
有没有办法像读取 TrueType 字体一样读取 Java 中的 Open Type 字体?
This works perfectly for TTF but I did not figure out yet how to do the same with Open Type fonts.
这对 TTF 非常有效,但我还没有弄清楚如何对 Open Type 字体做同样的事情。
Font f = Font.createFont( Font.TRUETYPE_FONT,
new FileInputStream("f.ttf") );
Please note I cannot relay on installed fonts. I provide the font with my program but don't want to install it system wide.
请注意,我无法中继已安装的字体。我在我的程序中提供了字体,但不想在系统范围内安装它。
采纳答案by MahdeTo
I don't think there is Open Type Font support in java (not free atleast), iText claimed to have such support, tried it a few month ago and it didn't work, what worked for me is a program called FontForge which I used to create a ttf from the otf which I then used.
我认为 Java 中没有 Open Type Font 支持(至少不是免费的),iText 声称有这种支持,几个月前尝试过但没有用,对我有用的是一个名为 FontForge 的程序,我用于从我随后使用的 otf 创建一个 ttf。
回答by christopheml
Java OpenType font support depends on your OS and JDK version.
Java OpenType 字体支持取决于您的操作系统和 JDK 版本。
Prior to Java 6, you can only use TrueType flavored OpenType fonts. With Java 6 you can use all OpenType fonts but you won't benefit from advanced typographic features like ligatures.
在 Java 6 之前,您只能使用 TrueType 风格的 OpenType 字体。在 Java 6 中,您可以使用所有 OpenType 字体,但您无法从连字等高级排版功能中受益。
回答by luukes
I've used a font-converter to convert the OTFto a TTFfile. There are some online-services out there (like www.freefontconverter.com) which do the job. For me this was a fast and simple workaround.
我用的字体转换器的转换OTF为TTF文件。有一些在线服务(如www.freefontconverter.com)可以完成这项工作。对我来说,这是一个快速而简单的解决方法。
回答by user3781572
Open Type(.otf) font is not supported in java 1.6. From java 1.7, support is added. Below code worked for me:
Java 1.6 不支持 Open Type(.otf) 字体。从 java 1.7 开始,添加了支持。下面的代码对我有用:
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("MyFont.otf"));
font = font.deriveFont(Font.BOLD, 40);
------
label.setFont(font);
Below are few links that justify about this
以下是一些证明这一点的链接
http://bugs.java.com/view_bug.do?bug_id=6992611http://docs.oracle.com/javase/7/docs/webnotes/adoptionGuide/#swing
http://bugs.java.com/view_bug.do?bug_id=6992611 http://docs.oracle.com/javase/7/docs/webnotes/adoptionGuide/#swing

