Fonts and loading times

When embedding fonts there are a couple of things you need to keep in mind.

  1. When will you need them? (i.e. during the loading)
  2. How to embed them? Using the IDE or Flex?
  3. Where will you embed them?

Since there is no way to export you library items in another frame than the first in AS3 you need to look for alternatives.

An alternative would be to just accept the increased file size when embedding them in the first frame and showing no information to the user when this is loading. I have noticed though that a simple Latin-1 font adds about 30 kB. One could reason with the widespread availability of broadband this shouldn’t be too bad.

The other option is to embed the font in an external file and decide not to use it for the loader. This keeps your file size down and increases initial loading, that way you can show the user sooner what is happening.

Also keep in mind that when you use a standard embed tag with Flex the embedded font will take up more space than when you use the Flash IDE to embed the fonts. You can avoid this though by specifying in the embed tag which characters you want to embed. A disadvantage of using the embedding with the IDE is that you won’t know for sure if your embedded fonts work until you have uninstalled them from you machine or test on a machine that doesn’t have the font.

Embedding in using the IDE goes as follows:

  1. In the library click
  2. ..

To embed a font in Flex you use the following code:

ActionScript

  1. // embedding fonts using flex
  2.  
  3. // note: leave the unicodeRange part out is you want to embed the entire font.
  4. [ Embed( source="Arial.ttf", fontName="Arial", mimeType="application/x-font-truetype", unicodeRange="<span><span class="character">U+0020-U+002F</span></span>" ) ]
  5. public var Arial:Class;

Now if you decide to load the font from an external file, you’ll need to register the freshly loaded font with the Flash player.

ActionScript

  1. // register the font with the player
  2. // where appDomain is a reference to the application domain the externally loaded font is in
  3. var arial:Class = Class(appDomain.getDefinition( "Arial" ));
  4. Font.registerFont(arial);

Some useful links about embedding a subset of a (unicode font):
Unicode Character Table
Yahoo on runtime font embedding

That’s about it for embedding fonts.

Update:
If embedded fonts aren’t showing up when compiling with Flex 4, add embedAsCFF=”false” to the embed-tag.