05.14
Always a particularly uncomfortable stone in actionscript’s boot, dynamically loading fonts at runtime has always been difficult. Many of my projects in the past have used library fonts as runtime shared libraries from the standpoint that once downloaded, it’ll be available across projects. It was never the cleanest of solutions and besides, the entire font had to be loaded – no choosing which character sets to embed, it was all or nothing.
This didn’t solve the problem of catering to markets that required special fonts that supported either greek or east asian character sets. I once got round the problem with one of our jaguar sites by an elaborate level loading process that I forget now and probably couldn’t explain any more
.
Enter AS3 and the magical getDefinitionByName() method. With it, you can retrieve and instantiate classes as long as you know their fully qualified class names. Yes – even from loaded SWFs.
From there, all you need do is embed a font (explicitly specifying the unicode ranges of character outlines that you want) into a class inside a SWF, load that swf at run-time and extract the font from it. Add it to your main SWF’s fontlist and it’s instantly available to use!
Granted, I may have simplified my description a tad, but those are the essentials. In my latest AS3 project, my initial setup loads a config XML file, a localised copy file, a CSS styles file and a handfull of fonts needed to render the text within the app. Should I need to configure the application for the Japanese market for example, I can completely reconfigure the app and it’s fonts to support the Japanese characters without republishing.
Now that’s how it’s supposed to be!!
Check these articles out for a more detailed explanation:
http://www.betriebsraum.de/…runtime-font-loading-with-as3/
http://www.mikechambers.com/…class-reference-by-class-name/
http://www.scottgmorgan.com/…runtime-font-embedding-in-as3/
This is exactly what I’m looking for. I went through the links you have included, but I’m still struggling to make this work. Is there anyway that you could possibly share you source files? It would be greatly appreciated!
It would be great if you could publish a very simple tutorial with code.
I am little confused to choose a better method from loading embeded fonts swfs at runtime from xml(font-manager thing) or loading .ttf and its unicode range and embeding it directly
@AWAIS – Not entirely sure it’s possible to just load and parse the .ttf (although it really should be) but if you load the .ttf you would have to load the entire font, whereas embedding it into the SWF at compile time gives you the option to specify a unicode range.
I suppose in these days of Broadband, it’s not a huge overhead to load the entire font, but I still think it’s wise to keep sizes of external assets to a minimum where possible for the sake of user experience.
I chose exactly which character glyphs to compile into my swf by deleting all the unnecessary ones from the TrueType font file using http://www.high-logic.com/fontcreator.html. It cut the swf file size in half.
Another tip, Flash Optimizer http://www.flashoptimizer.com/ works really well with an embedded font, reducing its size by about 30%. This may make it worth the $99 price tag.
Hope this info helps someone!
Excellent article, good looking blog, added it to my favs!