Home > actionscript 2, actionscript 3 > Loading Fonts Dynamically in Actionscript 2 and 3

Loading Fonts Dynamically in Actionscript 2 and 3

March 22nd, 2007

Fonts bloat swfs, but there are times when you can lessen the size impact by loading them dynamically. I do this most often when a skin is loaded at runtime or a swf has to support multiple languagues. For a lot of larger projects, I always just load the fonts so that they can be swapped easily, thus coping with design changes without recompiling.

Fortunately in actionscript 3, the solution is rather simple. Because of the flash.text.Font class, you can register a font to be used across swfs. For instance say your flash application decides because of user interaction it wants to start using the Goudy font. All is well because you previously made another actionscript 3 project swf whose code consisted wholly of this:

package {
import flash.display.Sprite;

import flash.text.Font;

public class FontFileGoudy extends Sprite
{

//here image/Goudy.ttf points to the font file relative to this file
[Embed(source="image/Goudy.ttf", fontName="Goudy",fontWeight="bold", mimeType="application/x-font-truetype")]
private var font1:Class;

public function fontFile()
{
trace(”Embedded font in loaded swf”);

Font.registerFont(font1);//registers font

trace(”registered font”);
}
}
}

Notice here that I embedded the font by pointing to the actually font file, “image/Goudy.ttf”.

All that your actionscript 3 project would need to do is load the swf created by FontFileGoudy, and then use Goudy at will. You can even check that it is loaded by by looking through the fonts returned by Font.enumerateFonts(). Here is an example class that loads goudy:

package
{

import flash.display.Sprite;
import flash.text.Font;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.system.LoaderContext;
import flash.display.LoaderInfo;

public class Main extends Sprite
{

public function Main(){

var fonts:Array = Font.enumerateFonts();

var font:Font;
for(var x:uint=0; x<fonts.length;x++){

font = fonts[x];
trace(”name : “+font.fontName);
trace(”style : “+font.fontStyle);
trace(”type : “+font.fontType);

}

//load font
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
loader.load(new URLRequest(”FontFileGoudy.swf”));

}

private function onLoaded(event:Event):void{

trace(”swf loaded”);
var fonts:Array = Font.enumerateFonts();
var font:Font;
for(var x:uint=0; x<fonts.length;x++){

font = fonts[x];
trace(”name : “+font.fontName);
trace(”style : “+font.fontStyle);
trace(”type : “+font.fontType);

}

}

}

}

For actionscript 2 however, the task is much more complicated. I have seen many people use this solution, but I have not found it described well on the web, so I am going to attempt to explain it here.

To dynamically load a font into a swf and then use it in TextFields you will need to:

1. Create a swf with one library item, which is a MovieClip which has a TextField with a font embedded. This library MovieClip will be exported for runtime sharing, for use in a shared library. Let’s call this swf a font swf.

2. Create a swf that uses the item in the library of the font swf, importing it for runtime sharing. This swf also has a callback to its _parent so that another swf loading it will know when its shared library is imported. Let’s call this swf the font loader swf.

3. Load the font loader swf into your application, wait for the callback from the font loader swf, and then use the font.

Click here to view my small no frills example
– Just loads in four different fonts, showing the font before and after it is loaded. They might look pretty similar (for instance if you already have the font on your computer) but you should be able to see the change in each one as it loads.

Source for font loading example

Download the source for this example and open in the Flash IDE src/dinLib.fla. This is one of the fla’s that hold a single MovieClip in its library, and that MovieClip contains one TextField with an embedded font, in this case DIN-Black. If you right click on fontField in the library, and go to linkage, you will see it is exported for actionscript, exported for runtime sharing, and importantly exported in the first frame.

Now open src/dinLoaded.fla in your Flash IDE. You will see that this fla also has only one thing in its library, the same fontField from dinLib.fla. If you right click and again select linkage, you will see that fontField is imported for runtime sharing from dinLib.swf.

Also notice in dinLoaded.fla that an instance of fontField is on the stage in the first frame, and the first frame has a small amount of code:

System.security.allowDomain(’*');
_parent.onFontLoaded();

The first line just allows a swf from any domain to load this swf and use its font. The second line calls back into the swf that loaded this swf, telling it’s parent it can now use DIN-Black.

Thats pretty much it. All of the other fla’s in the source are similar font files, except fontless_example.fla. fontless_example.fla just kicks off the Main.as class that handles loading each font swf when asked.

So this is far from a “pretty” solution for loading fonts dynamically but it is definitely a useable one.

actionscript 2, actionscript 3

  1. Eddy
    March 23rd, 2007 at 10:32 | #1

    Awesome, I got this working and it is great.

  2. René
    May 15th, 2007 at 06:03 | #2

    Thank you for the code.

    Your small example does not work on a Mac using Safari or Firefox. On Win with Firefox or IE7 Impact is displayed correct, the other ones are not.

  3. admin
    December 13th, 2008 at 16:00 | #3

    should be fixed now, thanks Rene

  4. February 6th, 2009 at 10:19 | #4

    Greate, some things seems so complicated… till someone show you the right way :-D

  5. Chris D.
    February 18th, 2009 at 17:28 | #5

    This definitely a great solution for AS3. Keep in mind (other readers)… that you can also specify unicode ranges in your embed call.

    [Embed(source="fonts/AvenirLTStd-Black.otf", fontName="Avenir LT Std 95 Black", unicodeRange="U+0020-U+002F,U+0030-U+0039,U+003A-U+0040", mimeType="application/x-font")]

  6. March 17th, 2009 at 08:34 | #6

    Thanks man. good work.

  7. March 17th, 2009 at 08:34 | #7

    Thank you!

  8. Jonas
    April 9th, 2009 at 08:38 | #8

    Looked at your setup and it’s the easiest and best explanation I’ve found so far for dynamically loading fonts in AS2.0.
    Going to try it out now,
    Cheers!

  9. Damian
    August 25th, 2009 at 06:42 | #9

    Thanks for the clear explanation! Only thing is I’m trying to get it working with dynamic text fields on the stage (as versus via actionscript). Can you post up an example for this or talk through how I can go about it? Thanks again!

  10. Lezlea
    August 27th, 2009 at 06:57 | #10

    Hey Brian,

    Here is the problem I have with the AS2 version…

    If I republish say the impactLib file (keeping it as CS3 or changing to CS4), initially you can see the text…but when the font is loaded it disappears.

    If I remove the embedFonts = true; the font appears but isn’t as clear as it should be therefore I either need to find out why there is a problem when I publish or how to get the font to work with embedFonts = true;

    Here’s a link to your example and all I have done is republish the lib files in the ones that disappear.

    Any ideas what’s going on?

    I’m on a mac but I tried it on a PC and it still didn’t work!
    http://www.lezleatalbot.co.uk/fontLoading/fontless_example.html

    Thanks,

    Lezlea

  11. admin
    August 27th, 2009 at 08:41 | #11

    @Lezlea
    Hey Lezlea,
    Sounds like you might not have the fonts on your computer. The computer publishing the .fla must have the font being used so that it can be embedded into the swf.

  12. admin
    August 27th, 2009 at 08:43 | #12

    @Damian
    Hey Damian,
    I’m not that familiar with doing stuff on the Stage or timeline because I try to keep it all in code. However I think it should be possible. You will have to set the text format through code though I think, not through the components inspector.
    Good Luck.

  13. October 9th, 2009 at 07:17 | #13

    Many thanks for the explanations about AS 2.0 tricks: that’s the first time I read and understand perfectly how to make it working at well.

  14. Jacob SF
    December 17th, 2009 at 17:53 | #14

    the font class in AS3 is amazing; unfortunately most agencies ive worked with are stuck in AS2, very nice articulation of the AS2 process, best Ive seen on the web

  15. January 18th, 2010 at 04:51 | #15

    Thanks for good and detailed explanation.Thank you for sharing your experience-you have a talent.

  1. No trackbacks yet.