How to get ttf font data from system fonts in java

I have some ttf fonts installed on the system.

I get this list using

GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() 

These are not only ttf fonts, but all the fonts I assume. In the end, if I use:

 Font.decode(fontName) 

I can get an awt.Font instance.

As far as I know, Font is not related to the actual PhysicalFont, so how can I get either a ttf font file or byte data from this ttf file for a font from this list or from awt.Font? I am trying to get physical font data or something like that. Should this data be somewhere to the right?

The reason I need this is to ultimately use FreeTypeFontGenerator with libGDX to generate BitmapFont

This should work on windows, osx and linux.

+8
java fonts true-type-fonts libgdx
source share
2 answers

It's impossible. The best you can do is use reflection , but it will only work with Oracle JRE and access the private API, so any new release of Oracle may break.

Perhaps you could write a native lib to list the fonts and their files.

+3
source share

As @NateS remarked, it looks like what I want to achieve is not entirely possible.

So, I just take advantage of the solution I used in my case:

What is this class: FontManager.java

This allows you to pre-cache existing ttf files at known system locations based on your system, and then create a map for the font type Name-> fontFile. this then goes to settings and loads the next time it starts.

Known Issues:

  • awt.Font has a known bug that cannot read some ttf font family names on osx systems (mostly some Arabic and Chinese fonts).
  • Not tested on Linux, most likely will fail.
  • The first launch may be slow if you have many fonts.

It would be ideal to write a native lib, but time has an essence ...

+3
source share

All Articles