Super easy external font loading in AS3 Flash

Here’s a super easy way to build out a project with external fonts in separate swfs for flash.

I use FlashDevelop and the FlexSDK so most of this is related to that usage, but you can just as easily do it with Flash using Library Symbols in lieu of the embed tag for embedding the font.

First, built a font swf:

package
{
 import flash.display.Sprite;
 import flash.text.Font;
 
 /**
  * …
  * @author Bela Korcsog
  */
 public class MainFont extends Sprite
 {
  
  [Embed(source = "../lib/metaboo.ttf",fontName="MetaRoman",mimeType="application/x-font-truetype")]
  public const FontClass:Class;
  
  public function MainFont():void {
   Font.registerFont(this.FontClass);
  }
 }
}

There’s lots of functions related to the TTF font embedding ability and you can restrict the Unicode ranges for non-roman characters using this embed:

[Embed(source="../lib/arialuni.ttf", fontFamily="ArialUni", fontStyle = "normal",advancedAntiAliasing="true", mimeType="application/x-font-truetype", unicodeRange = "U+0000-U+2019,U+0002-U+201d,U+0004-U+303f,U+0068-U+312c,U+0108-U+31bf,U+0140-U+9faf,U+21052-U+faff,U+ff01-U+ff60")]

You need to use TTF fonts for this as that’s the only format that the compiler will recognize. You can use an online tool for converting fonts to the correct format (there’s a lot of them out there but search for online font conversion tool).

In the parent swf, you’ll need to reregister the fonts for use in the main application. Although the Font Class is static, the Font symbols are not available outside of the registered ApplicationDomain for the swf requesting the Font, so this process is important to complete for the fonts to be recognized and rendered:

Here’s a snippet that will register the fonts correctly:

package
{
 import flash.display.Loader;
 import flash.display.MovieClip;
 import flash.text.Font;
 /**
  * …
  * @author Bela Korcsog
  */
 public class Main extends MovieClip
 {
  public var fontloader:Loader;
   
  public function Main():void
  {
   if (stage) init();
   else addEventListener(Event.ADDED_TO_STAGE, init);
  }
  
  private function init(e:Event = null):void {

   removeEventListener(Event.ADDED_TO_STAGE, init);
   fontloader = new Loader();
   fontloader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.fontsloaded);
   fontloader.load(new URLRequest(“FONTLOADER.swf”));
  }
  
  private function fontsloaded(e:Event):void {
   e.currentTarget.loader.removeEventListener(Event.COMPLETE, this.fontsloaded);
   for each (var f:Font in Font.enumerateFonts()) {
    if (e.currentTarget.loader.content.loaderInfo.applicationDomain.hasDefinition(f.fontName))
     Font.registerFont(e.currentTarget.loader.content.loaderInfo.applicationDomain.getDefinition(f.fontName) as Class);
   }
  }
 }
}

Next you need to create some text fields to present the font values and use ‘embedFonts = true’ on the text field.

et voila.

About Bela Korcsog

Proud father of two children, happy husband to one wife. I've been programming various technologies and leading the development of huge projects for most of the last ten years. I've got some specific likes and dislikes through my experiences in the web site business but generally I'm pretty straightforward about it. Not a huge fan of the latest and greatest shiny toy (it took me four years to show an interest in Flash) I'm more than happy to code in any language that comes along (Actionscript is just so darn fun).
This entry was posted in Flash, Flex. Bookmark the permalink.

Comments are closed.