JavaFX – Register Font


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 created 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.

For Applet mode, click on above image

For standalone mode

Note: You may have to wait for a while until the font is downloaded from the URL and registered. Sample requires JavaSE 6.0 or above.


// Register Font
var registerFontTask = RegisterFontTask {
    url: font-url
    onDone: function() {
        text.font = Font 
            name: "<font-name>" 
            size: 40 
        };
    }
};
registerFontTask.start();

Try it out and let me know feedback

var dzone_style = ‘2’;

Advertisements

About Rakesh Menon
-

6 Responses to JavaFX – Register Font

  1. Ernie Kent says:

    Very clever – the touch of the master ๐Ÿ™‚

  2. Rakesh Menon says:

    @Philippe Lhoste Thanks!
    @Ernie Kent Thanks! I’m more of a messenger of masters.. ๐Ÿ™‚

  3. Ernie Kent says:

    Hi, Rakesh. If you can bear with me a bit further I have a question concerning this method. When I first tried it I thought the signing was required because the font was obtained from a different domain, which would not be a problem for us. However I now see that accessing the class in the local JRE also raises a security exception. Do you think it is possible to do something like this but package the method in a jar downloaded with the applet? Thanks!
    Ernie

  4. Rakesh Menon says:

    @Ernie I will look into this. I have sent you an email, please reply with more info.

  5. Ernie Kent says:

    Thanks, Rakesh
    I did reply to your email last week. Basically the issue is that when using your method a signed applet seems to be required even when the font is loaded from the same place as the applet, The reason appears to be that accessing the JRE6 methods as done in your solution requires security privileges. I have found a different workaround by replacing the JRE5 jar in the JavaFX SDK and the Netbeans JAvaFX extension with a JRE6 jar. Netbeans then still flags an error on screen when RegisterFont is used, but the applet compiles and runs correctly.
    Ernie

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: