In one of my previous post I discussed about how to bundle custom font along with application. Recently I came across another query related to registering a custom font. Java API - Font.createFont(int fontFormat, InputStream fontStream) provides a way to create an instance of AWT font. Once loaded the font needs to be registered so that it can be loaded using the font name or family name. JavaSE 6.0 and above has API - GraphicsEnvironment.registerFont(Font font) for this purpose.

Problem:
  • API is available only since JavaSE 6.0, it may not be possible to use these APIs directly from JavaFX (which enforce JavaSE 5.0 compatibility)
  • API involves loading of font over network, so we need to perform this operation in background thread so as to avoid freezing the user interface


Solution:

In below example, font - RRRaghuMalayalam.ttf is downloaded from specified URL and registered. Launch application and click on "Register Font" button. The application is signed since it access resource from another domain.