This article originally appeared on the old wiki in early 2007 and addresses font loading in ActionScript 2.0. With the introduction of ActionScript 3.0 there are quite a few simple techniques to load fonts and we found that FontManger was no longer needed.
If you wish to load fonts in AS3 you can do so with SwfLoad and external asset classes.
Many large flash projects or multilingual sites are prime candidates for dynamic font loading and switching. Unfortunately this is not as easy as a loadMovie call. Flash makes it close to impossible to accomplish. So hard in fact that there are costly Flash components to handle this need. Hold onto your money; after reading this article you will be able to dynamically load fonts for free.
As you can see above, dynamic font loading in Flash/ActionScript 2.0 is possible. Click on a font name to load it in! The great advantage of CASA Lib’s implementation is the ability to define which characters you embed (as opposed to embedding an entire font face). It also allows the use of non true type fonts.
The Trick (Hack)
Getting Font loading to work in Flash is really a hack. It is complicated to set the files up, but CASA Lib has made the code aspect simple, quick and elegant.
Please note: This is also fairly well covered in the FontManager class.
Setting Up Your Files
To create an externally loadable Font resource please use the following steps:
- Create a new FLA file and name it
yourFontDesc_lib.fla. - On the root of
yourFontDesc_lib.flacreate a dynamic TextField. - Choose a typeface and embed all characters you want to include. Include “Basic Latin” for this example.
- Enter some text into the TextField. This is required to have the font successfully register. It is recommend to enter the font name into the field.
- Give the TextField an instance name of
font_txt. - Convert the TextField to a symbol, give it a name of
myFont, and make sure its type is MovieClip. - Make sure “Export for runtime sharing” is checked under Linkage, and give it an identifier of
myFont. - In the URL field type
[yourFontDesc_lib].swf(Univers_55_lib.swf in the screenshot below). This path is relative to the HTML file that your shell SWF will be loaded into, so change the path in this URL field if necessary.
- Click OK to apply these changes to the symbol.
- Publish this file and save it. You can now close this FLA.
- Create a new FLA and name it
yourFontDesc.fla. - In
yourFontDesc.flacreate a new symbol, give it a name ofmyFontContainer, and make sure the type is MovieClip. - Check “Import for runtime sharing” under Linkage, and give it an Identifier of
myFontContainer. - In the URL field type the same URL you used in the
[yourFontDec_lib].flaURL field (Univers_55_lib.swf in the screenshot below). It actually doesn’t matter what you type here, because it will automatically update with the correct value (even if it gives you an error) once you complete the next few steps.
- Under Source click the browse button and find
yourFontDesc_lib.flaand click Open. - Select source symbol
myFontand click OK.
- Click OK to apply these changes to the symbol.
- Make sure the the symbol is on the main stage at the root level and give it an instance name of
fontContainer_mc. - On a frame at the root place the following code (REQUIRED):
import org.casalib.load.media.font.FontManager; FontManager.registerFont("myFontRefrenceName", this.fontContainer_mc.font_txt, this, true);Please also read the documentation for registerFont. If you are concerned about the extra file size added by the FontManager class, you can compile against an intrinsic class for your external font SWFs. - Publish this file and save it. You can now close this FLA.
Implementing the Code
First let’s explain the ActionScript you added to your external font SWF:
referenceName- The unique reference name of your font. This can be any name of your choice. You will need this name later to assign the typeface to a TextField.
field- TextField with the embedded font.
sender_mc- Reference to the external SWFs root/shell; if this code is on the top timeline sender_mc should be
this. isLastRegister- [optional] Indicates it’s the last font registered
true, or that there are other fonts left to registerfalse; defaults tofalse. This is used when registering multiple fonts in the same file. You only settrueon the last register so FontManager knows when it can clean up and unload the SWF. - Returns
- Returns
trueif parametersender_mcwas referenced correctly and the font was successfully registered; otherwisefalse.
The hardest parts are over. Let’s create a final FLA, name it shell.fla. In shell.fla on root timeline enter the following code:
- First we need to import the FontManager class into our Flash shell (the SWF loading the fonts):
import org.casalib.load.media.font.FontManager;
- Before we can start the load process we will create a dynamic TextField:
this.createTextField("headline_txt", this.getNextHighestDepth(), 10, 10, 250, 250); this.headline_txt.multiline = this.headline_txt.wordWrap = this.headline_txt.border = true; this.headline_txt.type = "input"; var myFormat:TextFormat = new TextFormat(); this.myFormat.size = 20; this.headline_txt.setNewTextFormat(myFormat); this.headline_txt.text = "Lorem Ipsum."; - Now we create a new FontManager object, passing in the path to the SWF font wrapper containing the code we entered above:
var fontLoad:FontManager = FontManager.loadFont("yourFontDesc.swf"); - We add an event observer so we know when the font has successfully loaded and registered: (See documentation for all dispatched events)
this.fontLoad.addEventObserver(this, FontManager.EVENT_FONT_REGISTER, "onFontRegister");
- Now let us create the function that handles the event:
function onFontRegister(refrenceName:String, fontTextFormat:TextFormat):Void { FontManager.applyTextFormatByReferenceName(refrenceName, this.headline_txt); }The function uses the applyTextFormatByReferenceName method to apply the Font to a TextField with an instance name ofheadline_txt. FontManager automatically determines if the font is a System Font (_sans,_typewriteror_serif) or a non-system font and sets theembedFontsproperty of the TextField accordingly. - Now all we have to do is start the font load process:
this.fontLoad.start();
You should see the TextField with containing “Lorem Ipsum.” set in the typeface you just loaded.
Caveats
If any static/dynamic embedded text field in the flash movie uses the same font as the font you’re trying to load, that font will not be properly registered. This is true for any dynamic font loading and is due to a limitation in Flash and not CASA Lib. Note that this will not generate any error messages. Even though the font loads appear to register properly, they will fail silently. The solution is to not use any fonts in static text fields that you want to load or breaking apart the static text fields into shapes.
Conclusion
This article only covers the basics of FontManager. There are additional options and functions we did not cover. This example should serve as a great start to creating robust, dynamic font loading solutions.
Written by Sergio Daroca on July 13th, 2009
Hi !
Nice framework-lib, class and tips, but I wonder
Can this technique be used with CSS and styleSheets?