Preload Shared Fonts in Flash
Once of the things I’ve been trying to achieve for a long time is the ability to load fonts dynamically in Flash and track it with a preloader. Anyone who has spend any time with flash knows that if you want to embed fonts into the file, you need to include the font in the library. If you need a dozen or so fonts for whatever reason, then you’re basically out of luck since it will skyrocket your file size to 500Kb just for the fonts alone.
What you say? Just use loadMovie as you need it! No dice. With loadMovie to bring in the font, it will not registered as embedded and therefor will not format the font. Well, then, why don’t you just use as a shared asset? Forget it. Shared Assent need to be loaded at runtime, so you end up dealing with the heavy front load anyway.
After dealing with this ridiculous problem for a long time and searching high and low all over the internet for anyone that has solved this problem, I found some people who have. I’ve build my own little version here of which you can download all the source files.
I am no Actionscript guru. I feel like a caveman chiseling this stuff out of stone. Most of the advances I’ve made are because there are kind souls out there who are willing to help and post their work for you to download and tinker with. I thought I might type up this tutorial to try and return the favor, or help someone else out for a change.
Anyone who has tried to solve this problem has invariably ended up at sharedfonts.net. I’m sure his means of doing it are much more advanced than mine, but I didn’t want to have to out source from my site to get what should just work. I stumbled across deja-vue.net who really illustrated that preloading shared fonts could be done.
Apparently the key is that the fonts need to be individually created through swfmill, an open source generator of flash files. I don’t know what this does in the creation of these font files that allows their loading and usage in dynamically generated textfields, but it works for me.
In the available source files is a folder named swfmill. It contains 3 folders (fonts, output and xml), a .bat file and the swf.exe. Put the fonts you wish to convert into the fonts folder (they have to be .ttf, by the way). Then you edit an xml file per font. The xml looks like this:
<?xml version="1.0" encoding="iso-8859-1" ?>
<movie width="1" height="1" framerate="12">
<frame>
<library>
<font name="accid" import="fonts/accid___.ttf"/>
</library>
<Import url="http://www.rickworks.org/swf/fonts/accid.swf"></Import>
</frame>
</movie>
Note the Import URL. This is pointed to my site, but you’d probably want to make sure that was pointed wherever you’re going to upload too.
Once you have created an XML file for each font you wish to create, the .bat file must be executed to generate the fonts. Open the .bat file with notepad (for which I don’t get a right-click “open with”. You might need to start notepad and drill down to where the .bat file is located. )
The .bat file contents look as so:
//buildFonts.bat
swfmill simple "xml/kaliberr.xml" "output/kaliberr.swf"
swfmill simple "xml/KentuckyFriedChickenFont.xml" "output/KentuckyFriedChickenFont.swf"
swfmill simple "xml/accid.xml" "output/accid.swf"
swfmill simple "xml/Achafexp.xml" "output/Achafexp.swf"
pause
I had had no previous experience with .bat files, but the just seem to run shell level commands. When you double click on it, the CMD window will pop up and if everything is in place then it should output these to the ‘build’ folder. Mac users out there may wish to check out the swfmill site and see if they have any options.
Once these files are created, you can load them into your main flash movie and access them as if they were shared at runtime.
I used a preloader from senocular.com (who must be the coolest guy in the world, Flashwise).
In the flash file, I embed the “_sans” font just because I didn’t feel like making the logic to determine if embed fonts was true or false. When creating a textfield, embedFonts must be set to true for this to work.
So the buttons simply load one of the fonts into the movie, and once it’s finished loading, it calls a formatText() function to change the font style.
There are many improvements that can be made to this script and I hope that someone will help me sharpen my Actionscript skills with any suggestions.



Nice work [Rick]. I’ll certainly try it.
(Sorry for my bad English..) but please help…
Hello,
thanks for your advise and this great tutorial
I’ve tried your example on my server, it’s ok..
>>> BUT !
1- when I open the fla in Flash8 and I publish it into an swf (without doing anything else in or out this file !!!) > then I do not have anything no more (no font loading)
2- When I load one more time YOUR swf and put it on my server again, it works
etc.
>>> So with several tests I guess there is a problem in the choices of the publishing parameters with the fla file,
would you like to tell me witch are your parameters and characteristics ?
(… are could it come from a other problem?)
appreciate your answer..
Regards
Lea
I’ve got Flash 8 pro.
I don’t think the parameters and characteristics are the problem, but that the .swf font files need to be build correctly.
Look at the .xml files in the shardFont/swfmill/xml folder. When you create fonts you want to load, you have to put the location of the .swf file where you wish it to be.
My xml file says:
<import url="http://www.rickworks.org/swf/fonts/accid.swf"></import>
but when you build them you should put the location of where your fonts should be.
All of these were created for flash player 8 and actionscript 2 and they still seem to be working.
Hope this helps!