FontManager

Kind of class:class
Inherits from:MediaLoad < BytesLoad < RetryableLoad < Load < EventDispatcher < CoreObject
Version:03/21/08
Author:Aaron Clinger
Classpath:org.casalib.load.media.font.FontManager
File last modified:Monday, 01 December 2008, 13:34:40
Manages dynamic font switching/loading.

To create an externally loadable Font resource please use the following steps:
  1. Create a new FLA file and name it yourFontDesc_lib.fla.
  2. On the root of yourFontDesc_lib.fla create a dynamic TextField.
  3. Choose a typeface and embed all characters you want to include. Include "Basic Latin" for this example.
  4. 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.
  5. Give the TextField an instance name of font_txt.
  6. Convert the TextField to a symbol, give it a name of myFont, and make sure its type is MovieClip.
  7. Make sure "Export for runtime sharing" is checked under Linkage, and give it an Indentifier of myFont.
  8. In the URL field type [yourFontDesc_lib].swf. 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.
  9. Click OK to apply these changes to the symbol.
  10. Publish this file and save it. You can now close this FLA.
  11. Create a new FLA and name it yourFontDesc.fla.
  12. In yourFontDesc.fla create a new symbol, give it a name of myFontContainer, and make sure its type is MovieClip.
  13. Check "Import for runtime sharing" under Linkage, and give it an Indentifier of myFontContainer.
  14. In the URL field type the same URL you used in the [yourFontDec_lib].fla URL 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.
  15. Under Source click the browse button and find yourFontDesc_lib.fla and click Open.
  16. Select source symbol myFont and click OK.
  17. Click OK to apply these changes to the symbol.
  18. Make sure the the symbol is on the main stage at the root level and give it an instance name of fontContainer_mc.
  19. On a frame at the root place the following code (REQUIRED):
    import org.casalib.load.media.font.FontManager;
    FontManager.registerFont("myFontReferenceName", this.fontContainer_mc.font_txt, this, true);
    Please note: 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.
  20. Publish this file and save it. You can now close this FLA.
  21. Now you can use the code in the example below to load and apply the font. Follow these steps for each font you would like to load.
Since:
  • Flash Player 7
Example:
  • this.createTextField("headline_txt", this.getNextHighestDepth(), 50, 50, 300, 100);
    this.headline_txt.border = this.headline_txt.background = this.headline_txt.wordWrap = this.headline_txt.multiline = true;
    this.headline_txt.type = "input";
    this.headline_txt.text = "This is an example of dynamic font loading with FontManager.";
    
    var fontLoad:FontManager = FontManager.loadFont("testFont.swf");
    this.fontLoad.addEventObserver(this, FontManager.EVENT_FONT_REGISTER);
    this.fontLoad.start();
    
    function onFontRegister(referenceName:String, fontTextFormat:TextFormat):Void {
        FontManager.applyTextFormatByReferenceName(referenceName, this.headline_txt);
    
        this.fontLoad.destroy();
        delete this.fontLoad;
    }
Events broadcasted to listeners:
  • onFontRegister = function (referenceName:String, fontTextFormat:TextFormat) {}
  • onLastFontRegister = function (sender:FontManager) {}

Summary


Class methods
  • loadFont (filePath:String) : FontManager
    • Load a SWF wrapped font from a external location.
  • getTextFormatByReferenceName (referenceName:String) : TextFormat
    • Looks up and returns a TextFormat by its reference name.
  • applyTextFormatByReferenceName (referenceName:String, field_txt:Object) : Boolean
    • Assigns a TextFormat to a TextField and determines if embedFonts should be true or false.
  • applyNewTextFormatByReferenceName (referenceName:String, field_txt:Object) : Boolean
    • Assigns a new TextFormat to a TextField and determines if embedFonts should be true or false.
  • getFontReferenceNames : Array
    • Gets a list of all of the refrence names of registered fonts.
  • registerFont (referenceName:String, field:Object, sender_mc:MovieClip, isLastRegister:Boolean) : Boolean
    • Used by external font SWF files to register fonts.

Class properties

EVENT_FONT_REGISTER

static EVENT_FONT_REGISTER:String = 'onFontRegister'
(read,write)

EVENT_LAST_FONT_REGISTER

static EVENT_LAST_FONT_REGISTER:String = 'onLastFontRegister'
(read,write)

Class methods

applyNewTextFormatByReferenceName

static function applyNewTextFormatByReferenceName (
referenceName:String, field_txt:Object) : Boolean

Assigns a new TextFormat to a TextField and determines if embedFonts should be true or false.
Parameters:
referenceName:
Reference name of font.
field_txt :
TextField or org.casalib.textfield.CoreTextField to apply the TextFormat.
Returns:
  • Returns true if TextFormat by referenceName was successfully found; otherwise false.
Usage note:
  • Automatically sets embedFonts to true if font is NOT _sans, _serif or _typewriter; otherwise sets embedFonts to false.

applyTextFormatByReferenceName

static function applyTextFormatByReferenceName (
referenceName:String, field_txt:Object) : Boolean

Assigns a TextFormat to a TextField and determines if embedFonts should be true or false.
Parameters:
referenceName:
Reference name of font.
field_txt :
TextField or org.casalib.textfield.CoreTextField to apply the TextFormat.
Returns:
  • Returns true if TextFormat by referenceName was successfully found; otherwise false.
Usage note:
  • Automatically sets embedFonts to true if font is NOT _sans, _serif or _typewriter; otherwise sets embedFonts to false.

getFontReferenceNames

static function getFontReferenceNames (
) : Array

Gets a list of all of the refrence names of registered fonts.
Returns:
  • Array of font reference names.

getTextFormatByReferenceName

static function getTextFormatByReferenceName (
referenceName:String) : TextFormat

Looks up and returns a TextFormat by its reference name.
Parameters:
referenceName:
Reference name of font.
Returns:
  • A new TextFormat with font defined.

loadFont

static function loadFont (
filePath:String) : FontManager

Load a SWF wrapped font from a external location.
Parameters:
filePath:
The absolute or relative URL of the SWF.
Returns:

registerFont

static function registerFont (
referenceName:String, field:Object, sender_mc:MovieClip, isLastRegister:Boolean) : Boolean

Used by external font SWF files to register fonts. Should not be called by anything else.
Parameters:
referenceName :
The unique reference name of your font. Used to look up at a later time with getTextFormatByReferenceName.
field :
TextField or org.casalib.textfield.CoreTextField 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 register false; defaults to false.
Returns:
  • Returns true if parameter sender_mc was referenced correctly and the font was successfully registered; otherwise false.
Usage note:
  • Parameter isLastRegister should only be true during the registering of the last font in the loaded SWF. If there is only one font being registered it should always be true.

Instance methods

destroy

function destroy (
) : Void

Usage note:
  • destroy is called automatically after event onLastFontRegister.