Embedding fonts in Flex, AIR and Actionscript projects

Published on: June 13, 2009
Categories: Actionscript 3, AIR, Flex
Comments: 10 Comments

Question:

I’m trying to develop a Flex application that uses a Text component which will take an html rendered text. So far, no problem there. However, I would like to use more than 1 font (regular, bold, italic) in this text component and I’m not sure if the fonts are all on the user’s computer. What’s the best way of tackling this problem?

Solution:

If you don’t know for sure the fonts are installed on the user’s computer (because it’s some exotic font or some typical OS dependant font, the best way of dealing with that would be to embed the font in your Flex application (or any Actionscript application). Now, how do you do this exactly when you want to embed multiple fonts for a single component? Well, I’ll show you.

There are actually 3 ways of embedding your fonts in an application:

  1. embed fonts in your Actionscript code
  2. embed fonts directly in CSS
  3. embed fonts in CSS using a SWF file that has (part of) the fonts embedded

1. Embed your fonts directly in Actionscript

To embed a font in Actionscript, you’ll have to assign it to some static variable to be able to incorporate it in the compilation of your application. Just before that variable definition, you specify a metadata tag “Embed” (just like you would if you were to embed an image). The properties for this metadata tag are:

  • source: the location of your font file
  • fontName: the name you want to use in the application for your font. I’ve chosen to use myFont to indicate it can be anything you like. But you could also choose the font name itself if you prefer it like that
  • fontStyle: italic if it is an italic font (can be in combination with fontWeight)
  • fontWeight: bold if it is a bold font (can be in combination with fontStyle)
  • mimeType: e.g. application/x-font-truetype if it’s a truetype font

Repeat this step for all the different fonts you want to embed, but make sure that the fontName property is exactly the same for all of them. So, when for example you want to embed a Verdana font in regular, bold, italic and boldItalic, your code would look something like this:

[Embed(source="C:\\Windows\\Fonts\\Verdana.ttf",
       fontName="myFont",
       mimeType="application/x-font-truetype")]
public static const VerdanaTTF:String;
 
[Embed(source="C:\\Windows\\Fonts\\VerdanaBold.ttf",
       fontWeight="bold",
       fontName="myFont",
       mimeType="application/x-font-truetype")]
public static const VerdanaBoldTTF:String;
 
[Embed(source="C:\\Windows\\Fonts\\VerdanaItalic.ttf",
       fontStyle="italic",
       fontName="myFont",
       mimeType="application/x-font-truetype")]
public static const VerdanaItalicTTF:String;
 
[Embed(source="C:\\Windows\\Fonts\\VerdanaBoldItalic.ttf",
       fontWeight="bold",
       fontStyle="italic",
       fontName="myFont",
       mimeType="application/x-font-truetype")]
public static const VerdanaBoldItalicTTF:String;

2. Embed your font directly in CSS

Now, you can do exactly the same using only an external CSS file. The embedding part itself is a little bit different though. You would have to use the @font-face style for each of the separate font styles you want to incorporate in the resulting SWF file and provide it with the following attributes:

  • src: the location of your font file.
  • fontFamily: the name you want to use in your application for your font. Again, I’ve chosen to use myFont to indicate it can be anything you like. But you could also choose the font name itself if you prefer it like that
  • fontStyle: italic if it is an italic font (can be combined with fontWeight)
  • fontWeight: bold if it is a bold font (can be combined with fontStyle)

The src style attribute can either point to a file on the local file system or to a file somewhere on the web or another location. For embedding a font which resides on the local file system, you would specify local(“<font name>”) . The font name will remain the same for all the variations you want to embed, because it’s the style attributes that will make up the difference here. You’ll probably use this local specification most of the time, because you’ll probably want to embed a font that you have on your own computer. However, if you were to incorporate a font file which resides somewhere on a remote location, you would use url(“<path to font file>”). This path can either be a relative or absolute path.

Of course you need to repeat the @font-face style for every font variation you want to embed. Again, make sure that the fontFamily property is exactly the same for all of them, because you will use that name in the fontFamily attribute of the style you are going to apply on the component in order to use the different font styles.

For the same Verdana font definitions, your code should look like this:

@font-face {
	src: local("Verdana");
	fontFamily: myFont;
}
 
@font-face {
	src: local("Verdana");
	fontFamily: myFont;
	fontWeight: bold;
}
 
@font-face {
	src: local("Verdana");
	fontFamily: myFont;
	fontStyle: italic;
}
 
@font-face {
	src: local("Verdana");
	fontFamily: myFont;
	fontWeight: bold;
	fontStyle: italic;
}

3. Use a SWF file with partially embedded fonts

These first 2 methods work just fine. However, they do add several hundreds of kilobytes to your SWF file size. And this is only for 1 specific font, so imagine what it would do to a SWF file that compiles a couple of fonts into your application. This is due to the fact that a font is more than just alphabetical characters. Most of the time you don’t need all of those characters, but just the basic ones like digits, lowercase and uppercase letters and punctuation signs.

Fortunately, there is also another solution that allows you to limit the number of glyphs (this is what the individual font signs are called) to incorporate in your application. To be able to do this, we are going to have to take our development out of Flex Builder (or Flash builder of course) and bring it into Flash.

So, in Flash, you start by creating a new ActionScript 3 Flash file. On the stage, you place a text field and type some text. In the properties of that text field, you have to set it to Dynamic Text. In the Character subframe of the properties panel, you then select the font you want to embed as well as the style (regular, bold, italic or bold italic).

Properties panel

At the bottom of that same properties panel, you also see a button labeled “Character Embedding”. Just click on that one and you’ll get a dialog box which allows you to select the glyphs you want to embed in the Flash file. Select the uppercase, lowercase, numerals and punctuation glyphs and click ok to embed them in the SWF.

Embed settings

Now, perform the same procedure for the other font styles you want to embed, so you’ll eventually end up with 4 different dynamic text fields that all include a different style of your font.

The next step is then to compile your SWF file, so you can use it in your Flex or AIR application. To do that, you’ll first have to go to the Publish Settings, which you can find in the File menu. In the Format tab of the settings dialog, make sure that only Flash type is selected and then click on Publish. A SWF file will now be generated and that file will contain all the characters from the different font styles that you’ve just indicated.

Go back to Flex Builder (or Flash Builder of course) and add a fonts directory to your project. In this directory you place the SWF file that you have just generated. Now, to actually use the embedded fonts from the SWF file, we have to rely again on CSS. This is quite similar to the second method I’ve explained. However, this time the fontFamily property must be exactly the same as the actual font that you have included. So, in my example here, it has to be “Verdana”. You cannot choose the fontFamily name you want to use in your application freely anymore.

So, enter the different @font-face styles that match the embedded fonts from the SWF file in the same way as you would do when using the second method, bearing in mind that the font name is fixed now. Your code should look like this:

@font-face {
	src: url("../fonts/Verdana.swf");
	fontFamily: Verdana;
}
 
@font-face {
	src: url("../fonts/Verdana.swf");
	fontFamily: Verdana;
	fontWeight: bold;
}
 
@font-face {
	src: url("../fonts/Verdana.swf");
	fontFamily: Verdana;
	fontStyle: italic;
}
 
@font-face {
	src: url("../fonts/Verdana.swf");
	fontFamily: Verdana;
	fontWeight: bold;
	fontStyle: italic;
}

When you take a closer look at the generated SWF file, you will certainly notice a big difference in file size between the first 2 methods and this last one. This is because now, not all glyphs are embedded in the SWF file.

All you have to do now, is just set the fontFamily for the necessary components to the name you’ve just used in your CSS or Actionscript style embedding of the fonts, make sure you use the htmlText property and you’re all set to go.

10 Comments - Leave a comment
  1. John Daggett says:

    The syntax of local() in your CSS example is incorrect, it needs to be the name of a single face not the family name. So “Verdana Bold” in the bold, non-italic case.

  2. Hey John,

    Sorry for the late reply, but I’ve been on holiday for the past week with no access to internet.

    The local() in my CSS has been tried and tested several times and works just fine. When I try to put e.g. local(“Verdana Bold”), I even get compilation errors. It’s the name of the font, plus the indication of bold and italic that actually defines the font file that needs to be included. You don’t select an “Verdana Bold” font in Word either, don’t you? Well, it’s the same here…

  3. I have been looking looking around for this kind of information. Will you post some more in future? I’ll be grateful if you will.

  4. admin says:

    I’ll definitely be doing a lot more of this stuff, but for the moment I’m a little short on time. Hopefully I can get a couple more articles online by next month. I’m also looking into doing something with tutorial video’s online, but that will take up even more of my time :-(. We’ll see how far I can go…

    In the mean time, if you have a specific question about thiese kinds of things, do not hesitate to submit it via the contact form on this site.

  5. […] Embedding fonts in Flex, AIR and Actionscript projects […]

  6. […] Czytaj więcej: Flexpert to the rescue | Embedding fonts in Flex, AIR and … […]

  7. JeffreyC says:

    Great post. Thank you for the clear and thorough explanation.

  8. DJ says:

    Thanks for the post. But how do we use these fonts? I need to render a html text.

  9. Hey DJ,

    Using the font is exactly the same as using any other font. You just type the name that you’ve given to your custom embedded font in the font-family style of your component.

    This can be done either inline or via CSS.

    Cheers,
    Steven

  10. Mark says:

    One tip I discovered is that if you want to use the embedded font with htmltext on a component, you first have to set the text style for that component with one of the embedded fonts.
    @font-face {
    src:url(“assets/fonts/pala.ttf”);
    fontFamily: Palatino;
    }
    then
    myTextArea.setStyle(“fontFamily”, “Palatino”);
    then you can use any embedded font in the htmltext
    myTextArea.htmlText = ‘Some TextSome more text, etc.’;

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is Tuesday, April 25, 2017