10

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 rely on installed fonts. I provide the font with my program but don't want to install it system-wide.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
HaBaLeS
  • 1,809
  • 3
  • 16
  • 26

4 Answers4

4

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.

christopheml
  • 2,444
  • 17
  • 25
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:

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user3781572
  • 175
  • 1
  • 4
3

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.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
1

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.

luukes
  • 1,444
  • 10
  • 9