Top RIA blogs award

Top RIA blogs

Confessions of an Flash Addict Rotating Header Image

Font Embedding in AS3

Just as a reminder for myself, so i wont forget (not my text, copied from here:

The [Embed] meta-tag is used to embed a font. There are 3 different ways to specify the font:

[Embed(source='/assets/fonts/FONT.TTF', fontName="Font", mimeType="application/x-font-truetype")]
[Embed(source='C:\WINDOWS\Fonts\FONT.ttf', fontName="Font", mimeType="application/x-font-truetype")]

[Embed(systemFont='Font', fontName="Font", mimeType="application/x-font-truetype")]

If you use a relative path, you can start with a slash to begin your path from the root of the project. If you embed with an absolute path, keep in mind that you need to double the slashes as they are escape characters. If you embed a system font, then use the actual name of the font as it appears installed on your system. For any of those three options, the fontName should match what you are setting either in your html or TextFormat, like one of the following two:

mytextFormat.font = "Font";
// or
mytextField.htmlText = '<font face="Font">My text</font>';

When you use meta tags, there are two options: They can either go inside the package and outside the class definition (like you would do to specify swf options), or they can go inside the class. Placing them outside the class will work, however you cannot do more than one font embedding in this manner. If you try to do two subesquent Embeds outside the class, you will get an error “Error: Unable to transcode /assets/fonts/FONT.TTF”. If you put them inside the class definition, you will need to place a variable for them to be assigned to for each one, like so:

[Embed(source='/assets/fonts/FONT.TTF', fontName="Font", mimeType="application/x-font-truetype")]
private static var EMBEDDED_FONT:String;

For font embedding, the variable is not actually needed for usage, though for embedding image resources you will actually use it. If you are using an AssetManager class to consolidate all your external needs (highly recommended in my book), then you need to remember that if you have *only* fonts in there you will need to reference something in the class to get it compiled in to your swf. Simply referencing one of the variables you assigned the font identifier to will work, like so:

var n:String = AssetManager.FONT_VERDANA;
package {
public class AssetManager {
[Embed(systemFont='Verdana', fontName="Verdana", mimeType="application/x-font-truetype")]
public static const FONT_VERDANA:String;

[Embed(source='/assets/fonts/HAMBURGER.TTF', fontName="Hamburger", mimeType="application/x-font-truetype")]
public static const FONT_HAMBURGER:String;

[Embed(source="C:\WINDOWS\Fonts\seguibk.ttf", fontName="Segoe", mimeType="application/x-font-truetype")]
public static const FONT_SEGOE:String;
}
}

Hopefully, this will help create a little extra awareness for what is involved with embedding fonts in AS3, as well as document what causes the transcoding error. In summary, I really like the way we can manage embedded assets in AS3 now. I still see one large problem that is left unaddressed (to my knowledge) in AS3 however : Shared Libraries. Especially with how Flash is maturing as a presentation medium for rich applications, it is difficult to still see this thorn persisting. As an example: If you were going to make a small Flash application to design T-Shirts for Cafe Press, you could probably fit most of the base code to do the designing in 20-50k in AS3. But wait – you want the user to be able to choose from an array of 30 fonts. If you embed each of those fonts, you would kick the size of the application up to the order of several mb. Ideally, I’d like to see loadSharedResource made as a public method we can call to load images, fonts or swf files with common classes, which would make those items available in any running code after that.

8 Comments on “Font Embedding in AS3”

  1. #1 Robert
    on Sep 16th, 2008 at 8:49 pm

    You can build individual swfs or entire shared library swfs. This has been around since the old days, but the way to do it is so convoluted that I have to re-google-teach myself every time.

    Good luck
    Robert

  2. #2 Jloa
    on Mar 14th, 2009 at 11:59 am

    Hi there, do u know if there is a chance to load ttf fonts using load method and embe them at run-time with out rebuilding?
    Ex: the same as image loading

  3. #3 Halvet
    on May 16th, 2009 at 8:38 am

    I use AS2 but with the transition to AS3 I have big problems ..

  4. #4 Sidney de Koning
    on Jun 24th, 2009 at 11:42 am

    Hi Halvet,

    Its best to use AS3 for this else it wont work.

    Cheers, Sid

  5. #5 Hjeldin
    on May 8th, 2010 at 2:35 pm

    Man, you just saved my life!

  6. #6 Sidney de Koning
    on May 9th, 2010 at 2:08 pm

    Your welcome, glad to help :)

  7. #7 Top 20 Things every Senior Flash Developer should know – Confessions of an Flash Addict
    on Jun 6th, 2010 at 7:50 pm
  8. #8 Embedding Fonts in the ComboBox Component – Confessions of an Flash Addict
    on Jun 28th, 2010 at 5:04 pm

    [...] components that Flash provides you. The problem I had was embedding the fonts in to it. I wrote alot on font embedding. But there are always special cases . Here’s how to do [...]

Leave a Comment