Creating custom Font in Java

Creating a custom font from a TrueTypeFont file in Java is not that hard. However there are some pitfalls:

  • The font size in in pt (Points) where a point is 1/72 of an inch, that is for a normal person about 3.5mm.
  • The created Font has an initial size of 1pt
  • The font size is passed as Float but rounded to the nearest Integer
  • Passing an Integer for the font size does not work (results in unchanged font size)
public Font createDefaultFont(int size)throws FontFormatException, IOException{
	Font font = Font.createFont( Font.TRUETYPE_FONT, getClass().getResourceAsStream( "/"+defaultFont) );
        return font.deriveFont((float)size);
}

Here defaultFont is the filename of the TrueType Font.

Schreibe einen Kommentar