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.

<script src="http://dl.javafx.com/1.2/dtfx.js"></script> <script src="/rakeshmenonp/resource/RegisterFont/RegisterFont.js"></script>

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


<script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src="/rakeshmenonp/resource/RegisterFont/zoneit.js"></script>

Comments:

Nice trick!

Posted by Philippe Lhoste on September 07, 2009 at 03:17 PM IST #

Very clever - the touch of the master :)

Posted by Ernie Kent on September 07, 2009 at 07:18 PM IST #

@Philippe Lhoste Thanks!
@Ernie Kent Thanks! I'm more of a messenger of masters.. :)

Posted by Rakesh Menon on September 08, 2009 at 01:57 AM IST #

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

Posted by Ernie Kent on September 10, 2009 at 05:05 PM IST #

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

Posted by Rakesh Menon on September 14, 2009 at 02:25 AM IST #

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

Posted by Ernie Kent on September 18, 2009 at 06:35 PM IST #

Post a Comment:
Comments are closed for this entry.
About


The views expressed on this blog are my own and do not necessarily reflect that of my organization

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today