Embed fonts with Adobe Flash IDE

Almost every Flash project needs some embedded fonts and it is a good practice to compile the fonts into a separate SWF file. So here is a simple way to create a file that contains only embedded fonts. The SWF file will also show the exact internal font names:

  1. Copy and paste the ActionScript code below into the first keyframe of your timeline.
  2. Create new fonts in flash library and export them for ActionScript.
    A good tutorial can be found here.
  3. Run ONLY the code for step 2 to evalute and trace the exact font names.
  4. Assign the exact font names to the actual library symbols and their export class name.
  5. Run ONLY the code for step 4 to export the swf.
Security.allowDomain( "*" );
 
// Step 1: Create new fonts in flash library
//         and select "Export for ActionScript".
 
 
// Step 2: Run ONLY this code to evalute and trace the exact font names.
/**/
var embeddedFonts : Array = Font.enumerateFonts ( false );
var c : uint = embeddedFonts.length;
for ( var i : uint = 0; i < c; i++ )
{
    var font : Font = embeddedFonts[ i ] as Font;
    var className : String = font.fontName.split( " " ).join( "" );
 
    var lowerFontStyle : String = font.fontStyle.toLowerCase();
    if ( lowerFontStyle.indexOf( "bold" ) != -1 ) className += "Bold";
    if ( lowerFontStyle.indexOf( "italic" ) != -1 ) className += "Italic";
 
    trace( className );
}
/**/
 
// Step 3: Assign the exact font names to the actual
//         library symbols and their export class name.
 
 
// Step 4: Export the swf using ONLY this code. 
/**/
var posX : int = 10;
var posY : int = 10;
 
var embeddedFonts : Array = Font.enumerateFonts ( false );
    embeddedFonts.sortOn( "fontName", Array.CASEINSENSITIVE );
 
var c : uint = embeddedFonts.length;
for ( var i : uint = 0; i < c; i++ )
{
    var font : Font = embeddedFonts[ i ] as Font;
 
    var lowerFontStyle : String = font.fontStyle.toLowerCase();
 
    var textFormat : TextFormat = new TextFormat();
        textFormat.bold = ( lowerFontStyle.indexOf( "bold" ) != -1 );
        textFormat.italic = ( lowerFontStyle.indexOf( "italic" ) != -1 );
        textFormat.font = font.fontName;
        textFormat.size = 14;
 
    var className : String = font.fontName.split( " " ).join( "" );
    if ( textFormat.bold ) className += "Bold";
    if ( textFormat.italic ) className += "Italic";
 
    Font.registerFont( Class( getDefinitionByName( className ) ) );
 
    var boldItalic : String = "";
    if ( textFormat.bold && textFormat.italic )
    {
        boldItalic = "( needs to be BOLD and ITALIC ! )";
    }
    else if ( textFormat.bold )
    {
        boldItalic = "( needs to be BOLD ! )";
    }
    else if ( textFormat.italic )
    {
        boldItalic = "( needs to be ITALIC ! )";
    }
 
    var textField : TextField = new TextField();
        textField.autoSize = TextFieldAutoSize.LEFT;
        textField.defaultTextFormat = textFormat;
        textField.embedFonts = true;
        textField.text = font.fontName + "\t" + boldItalic;
        textField.x = posX;
        textField.y = posY;
 
    posY += textField.height + 5;
 
    addChild( textField );
}
/**/

This entry was posted in ActionScript, Flash, How To.... Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">