How to use Open Type Fonts in Java?
Solution 1
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.
Solution 2
Java OpenType font support depends on your OS and JDK version.
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.
Solution 3
Open Type (.otf
) font files are not supported in Java 1.6. From Java 1.7, support is added:
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("MyFont.otf"));
font = font.deriveFont(Font.BOLD, 40);
label.setFont(font);
More details:
- http://bugs.java.com/view_bug.do?bug_id=6992611
- http://docs.oracle.com/javase/7/docs/webnotes/adoptionGuide/#swing
Solution 4
I've used a font-converter to convert the OTF to a TTF file. There are some online-services out there (like www.freefontconverter.com ) which do the job. For me this was a fast and simple workaround.
HaBaLeS
Professional Java Developer focused on Web Development Hobbyist Game Developer
Updated on July 20, 2022Comments
-
HaBaLeS almost 2 years
Is there a way to read Open Type fonts in Java the same way as I do with TrueType fonts?
This works perfectly for TTF but I did not figure out yet how to do the same with Open Type fonts.
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.
-
Emmanuel Bourg almost 3 yearsThe converted font may look different, that's not recommended.