How to embed fonts in pure AS3

On my travels as a contractor, I've seen various methods used for embedding fonts in ActionScript 3 projects (code embedded, creating font SWFs, runtime loading, etc). Each of them has its own merits (and limitations), but generally you're looking for something that's easy to do, easy to maintain (for others, not just yourself) and of course that works!

So, here's the method I use for pure AS3 projects I compile with the Flex SDK. I embed the TTF or OTF file in a 'Style' class and include those font files in the CVS/SVN repository too - it does my head in when fonts go missing, because a designer can't remember which font they used:

Actionscript:
  1. import flash.text.Font;
  2. import flash.text.TextFormat;
  3.  
  4. // UNICODE RANGE REFERENCE
  5. /*
  6. Default ranges
  7. U+0020-U+0040, // Punctuation, Numbers
  8. U+0041-U+005A, // Upper-Case A-Z
  9. U+005B-U+0060, // Punctuation and Symbols
  10. U+0061-U+007A, // Lower-Case a-z
  11. U+007B-U+007E, // Punctuation and Symbols
  12. Extended ranges (if multi-lingual required)
  13. U+0080-U+00FF, // Latin I
  14. U+0100-U+017F, // Latin Extended A
  15. U+0400-U+04FF, // Cyrillic
  16. U+0370-U+03FF, // Greek
  17. U+1E00-U+1EFF, // Latin Extended Additional
  18. */
  19.  
  20. [Embed(source = '../fonts/myfont.ttf', fontName = 'MY_FONT',
  21. fontWeight = 'regular', unicodeRange =
  22. 'U+0020-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E',
  23. mimeType = 'application/x-font')]
  24. public static const MY_FONT :Class;
  25.  
  26. public static const DEFAULT_FONT :String        = "MY_FONT";
  27. public static const DEFAULT_TEXT_COLOUR :int = 0xFFFFFF;
  28. public static const DEFAULT_TEXT_SIZE :int = 14;
  29. public static const MY_TEXT_FORMAT :TextFormat = new
  30. TextFormat(DEFAULT_FONT, DEFAULT_TEXT_SIZE, DEFAULT_TEXT_COLOUR);

Through experience, this seems to be the most robust and maintainable way of dealing with fonts (not to throw out more flexible, but perhaps less robust ways, such as runtime loading), especially when you need control of the unicode ranges you are embedding - which I have included for reference.

About Liam

Creative developer and webmaster of spikything.com
This entry was posted in AS3, Flash and tagged , , , . Bookmark the permalink.

One Response to How to embed fonts in pure AS3

  1. mdugue says:

    Nice Post, didnt know about the unicodeRange-Part. Anyhow, I'm having a Project, where the embedded Font (Verdana) is drawing latin I characters (like äöü) in a wrong way, so the unicodeRange-part seems to be ignored. Any Experience with that?
    Cheers
    Manuel

Leave a Reply