Archive

Posts Tagged ‘font’

JavaFX – Register Font

September 7, 2009 6 comments

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’;

Categories: javafx Tags: , ,

"Can JavaFX speak my language?"

March 19, 2009 11 comments

An attempt to answer two simple questions…

  • Can JavaFX speak my language?
  • How to load custom fonts in JavaFX?

There are many languages for which Java does not provide built-in support. Also you may want to use some nice custom font instead of the logical font. You may also want to ensure that the application looks fine across platforms and does not render boxes or look ugly on some platforms.

JavaFX allows you to package your font along with the application. So that you can use the font with your application and its guaranteed that the application looks fine on all platforms. Here I’ll try to render some characters using my mother tongue – Malayalam. Oh! Yes! its a palindrome.

First I downloaded Malayalam font from Indix website. I’ll use RRRaghuMalayalam.ttf in my application.

I copied the RRRaghuMalayalam.ttf font file under src/fontttf/fonts/ directory. fontttf is package name of the application. Next I created a mapping file – fonts.mf under src/META-INF/. In mapping file I specified a name for the font and the location of font as shown below. I named the font as Malayalam Font.


Malayalam\ Font = /fontttf/fonts/RRRaghuMalayalam.ttf

Now the directory structure looks as shown below.

Now lets use this new font in our application. That is simple!.
We can just specify the font name as “Malayalam Font”.
No additional settings required.


Text {
    font: Font {
        name: "Malayalam Font"
        size: 40
    }
    content: "\u0d2e\u0d32\u0d2f\u0d3e\u0d33\u0d02"
}

For Applet mode, click on above image

For standalone mode

Cool! Now JavaFX can speak my language! Can it speak your language?

You need to try it out!

Source:

var dzone_url = “http://blogs.sun.com/rakeshmenonp/entry/javafx_custom_fonts&#8221;;
var dzone_style = ‘2’;

digg_skin = ‘compact’;
digg_window = ‘new’;

Categories: javafx Tags: , ,