Importing fonts to pure AS3 projects in Flex Builder

There are a lot of issues you run across when dumping the Flash IDE and doing pure AS3 projects in Flex Builder (the subject of many future posts), but one of the first ones you’ll run across is the issue of embedding fonts.

There are a number of solutions, but none of them are very elegant or well organized. I hate the idea of embedding fonts at compile time, because it gives you zero control over when you import and register your fonts, and it increases the necessary load time of your initial SWF. Plus its just plain ugly. If you had an FLA as the root of the project, it wouldn’t be an issue at all, you could simply embed the font in each text field you throw on the stage (which you can still do, so long as your text field is in a sub clip that is not directly referenced in the library), but your fonts are not very organized if you choose to do it that way either.

If you encase all your fonts in a SWF, and then give them direct class representations, you can keep track of all your fonts from a centralized SWF and keep their representative classes in a directory. Then you have control over when you load and register those fonts, as well as their organization. People who work on your projects after you can also see exactly what fonts you’ve embedded as well. With Flex Builder 3’s advanced refactoring you can also change an entire font across all text fields that use that font in just a few clicks (try that in your FLA!).

So, to start this you need a flash extension created by Tink, you can find it at this link. The plugin was made to do a similar process with Movie Clips (which turns out to be a very efficient way of auto generating classes that can then be linked to content in a SWF), but there is an updated version at the bottom that allows for fonts to be exported as well other movie clips. Its very similar to writing the classes in the Flash IDE, linking them to their representative MovieClips then exporting that swf and making use of the classes. Except here the classes are auto generated and exist in the flex project rather than in an FLA project. It isn’t completely necessary to have the command installed, but having it saves you a ton of time.

Here is the link to the source of the project if you want to use it to follow along. Here is a link to the finished work, which consists of two text fields being animated to show that the fonts are embedded.

So to start, create a flex builder ActionScript 3 project. create packages for fonts and swfassets, in the project above I create a com folder and place the view folder in it, then I place the fontassets and swfassets folders under the view folder. Now create another fontassets folder on the same level as the com folder, and create an empty fla in that folder:

Folder view of the Flex Builder project

Once you’ve created the FLA, open it and add fonts to the library (right click on the library and select new Font). I used Futura and Minion Pro. You must make sure the name that you name the font is the exact name of the font (case and space sensitive).

List of my fonts in the library of the fontasset.fla file

Now we need to set the font to export for Actionscript by linking it to a class. Right click on the font and select linkage. You can name the class whatever you want, its the package thats the important part. Make sure that the package exactly resembles the package that you have it stored in Flex. You’ll get a warning from Actionscript that no definition can be found for the class. We don’t give a rats what Flash thinks though, because we’re about to auto generate our own class! In my case the package name com.view.fonts:

Package name of the font

Now we need to actually create the class that will represent our font in our Flex Builder project. To do that its time to activate that command I told you about a bit earlier. Go to window => other panels => create classes. That should open a window up for you. Now change the directory to project directory so that it points to the directory that contains the com directory. This means it will export the classes it sees and place them in the com=>view=>fonts directory. So set your directory and click on the create classes:

Create classes command window

Now you should see these classes auto generated in Flex Builder in the fonts folder. The comment at the top will mention its been auto generated as well. Now all you need to do is add a public static constant to the top of the file that represents the Name of the font, which you’ll use when you set text fields to use the font. Here is the Futura class file. Now the swf that contains the fonts must be imported before they can be used. ApplicationDomain is important in this context, so you must set it to currentDomain so that the fonts will be registered and visible across the project.

fontAssetLoader = new Loader();
fontAssetLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onFontAssetsLoadComplete );
fontAssetLoader.load( new URLRequest( AssetURL.FONT_ASSET_URL ),
new LoaderContext( false, ApplicationDomain.currentDomain ) );

Once they’ve been loaded, you need to register them:

/**
* Called when the font assets have been loaded
*
* @param evt
*
*/
private function onFontAssetsLoadComplete( evt : Event ) : void
{
Font.registerFont( Futura );
Font.registerFont( MinionPro );

isFontAssetLoadComplete = true;

onAllLoadComplete();
}

Once you’ve done that, you can use them in subsequent text fields you create! Here is the class in the project that actually instantiates and uses the text field.

You’ll notice I also do basically the same procedure with a MovieClip class that I create in an FLA and then overwrite. It works essentially the same way with some quirks (that I’ll probably write about at a later date). In that class I have several sub clips on the stage, which I then place the text fields in. This is necessary for this method to work, as the text fields must be dynamically created. Lots of coders let the designers create the actual dynamic text fields (this whole process is obviously irrelevant for static fields). More often than not I end up encasing those text fields in sub movie clips anyways so that they can be tweened and animated according to the designer’s needs. Since the text field itself is dynamic it has to be linked in Actionscript, so the extra 8 or so lines of code it takes to do dynamically create it in Actionscript is worth it.

So there you have it, you’re just a little more separated from those designers than you were before! And you don’t have to use their shitty Flash IDE to write your code in either.

6 Responses to “Importing fonts to pure AS3 projects in Flex Builder”

  1. Ben  on December 10th, 2007

    found this post randomly, very nice.

  2. brainclog  on February 27th, 2008

    I am able to get fonts to embed in Flex Builder 2 by using [Embed etc… if I set NORMAL antiAliasType. The problem is that if I set ADVANCED my fonts disappear! Many people seem to have this problem in CS3 and the solution is that the fonts aren’t added to the library, so any idea on a solution for Flex?

  3. crebstock  on March 6th, 2008

    brainclog:

    Fonts are such a clusterf*** in the flash platform, especially if you want to do an actionscript based project in Flex Builder. You can embed the fonts directly into your main swf, which is equivalent to having them in the library of an FLA (which has issues, as you noted). Another problem with that method is that you now have to load those fonts in your main swf, which means users have to wait that much longer before they even see a loading bar for your app.

    The other option is to embed them in a swf and load that swf dynamically, like its mentioned above. Again, hardly ideal because you cannot set the font of a textfield that isn’t created dynamically. You can also do it the easiest (and least efficient) way, which is to embed the fonts directly into textfields created in actionscript. I hate that way, but I use it because its the easiest.

  4. Gareth Foote  on April 16th, 2008

    Hi Chris,
    Really appreciate this concise and easy to follow type of tutorial. Muchos Gracius.

    I have a query though. In a previous project I got this working without any hassle but I have come back weeks later and tried to implement it again into another project and I’m having the following problem:

    I can get the textfield to appear when I don’t embed the fonts i.e. when I leave out the embed fonts line of code. In this case the fonts show up fine on my machine because they are installed on the system but on anyone elses they obviously don’t.
    When I put in the embed fonts line the textfield dissapears and I’m not sure why?

    I’m sure its something quite simple I have or haven’t done but was hoping that perhaps you had come across this before.

    Thanks again,

    Gareth

  5. crebstock  on April 18th, 2008

    Gareth:

    If you don’t register the font with the system before you turn the embed property on, the text will simply disappear. Also, if you create the text field in Flash, import it in a MovieClip and then set the embed property to true, fonts don’t seem to work as well.

  6. 11111RAKE11111  on October 10th, 2008

    AWESOME THANKS!


Leave a Reply