Embedding Fonts Using AS3 and Flex 4 Ant Tasks

30
Jun/09
6

I’m posting this as a reminder to myself and as a beacon of hope to others who need to embed a font in ActionScript 3 and build using Ant. It took me forever to find a solution (shown in step 3).

The documention didn’t provide enough information. What I’d like is a chart similar to what they have in the livedocs for mxmlc options but whatever… at least the source code was included. The answers always live there.

1) Embed the Font

[Embed(source="COOLFONT.TTF", fontName="CoolFont", mimeType="application/x-font-truetype")]
public var CoolFont:Class;

2) Prep the TextField and TextFormat Instances

_textFormat = new TextFormat();
_textFormat.font = "CoolFont";
_textField = new TextField();
_textField.embedFonts = true;
_textField.defaultTextFormat = _textFormat;

3) Add Font Managers to Your Build File

<fonts>
<manager class="flash.fonts.AFEFontManager"/>
</fonts>

This was the missing link for me. Without it, you’ll get nothing but transcoding errors. If you’re familiar with the font manager listing in flex-config.xml, you’ll notice it’s close enough to confuse the issue when adding managers to your build script.

If you’re interested in learning more about font managers the livedocs actually do come in handy. You can read about them here.

Filed under: flash
Comments (5) Trackbacks (1)
  1. Matthew Horn
    1:48 pm on September 10th, 2009

    Hi Phil,

    You’re right, the ant doc leaves a lot to be desired. I didn’t have time to include examples on using embedded fonts in ant tasks. I did, however, write the sections on the compiler args and the font managers, so I _should have_ been able to add a section on it fairly easily.

    I’ll see if I can add a section in the next version of the documentation (which will come out with Flex 4). Thanks for pointing out the deficiencies. These things often get by us unnoticed!

    Matthew Horn
    Flex documentation

  2. Elias
    9:42 am on September 17th, 2009

    Hi! I’m sorry for the newbie question, but i think that you can help me

    I have the ttf file embedded in my app. i’m trying to build it using the ANT mxmlc tag.
    But, where do i put de tag? Inside the mxmlc? it does not working there, neither outside!
    i’m getting crazy here!

    is there something else that must be done?

    thanks!

  3. Elias
    9:54 am on September 17th, 2009

    i’m using Flex 3.2!

  4. Phil
    11:12 am on September 17th, 2009

    Hi Elias,

    Everything you need to know regarding Flex 3 Ant tasks can be found here in the livedocs:
    http://livedocs.adobe.com/flex/3/html/help.html?content=anttasks_5.html

    Good luck!

  5. Frazko
    2:45 pm on March 5th, 2010

    this post is GOLD!!!

    I could not see the embedded font until added that manager tags to the build

    Thanks a lot!!

Leave a comment