Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Open Type Fonts in Java?

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.

like image 843
HaBaLeS Avatar asked May 16 '09 14:05

HaBaLeS


People also ask

How do I use new fonts in Java?

createFont(Font. TRUETYPE_FONT, new File("A. ttf"))); After this step is done, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.

Is OTF better or TTF?

OTF is more likely to be a “better” font, as it supports more advanced typesetting features (smallcaps, alternates, ligatures and so on actually inside the font rather than in fiddly separate expert set fonts).

How do I open an OpenType file?

The easiest way to do that is to hit command+T or alt+t on windows then select the “Opentype” tab on the window that pops up. You can also go to Window>Type>Opentype.


2 Answers

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.

like image 100
christopheml Avatar answered Sep 28 '22 07:09

christopheml


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.

like image 32
MahdeTo Avatar answered Sep 28 '22 08:09

MahdeTo