Tag Archives: fonts

Embed tag gotchas in Flex SDK 4

In moving to compiling projects with the new Flex SDK 4, I noticed a couple of gotchas to do with the EMBED metatag that I thought I’d share:

Runtime Shared Libraries
If you wish to embed assets in your SWF with the EMBED metatag, so you can manage and update things easily, there’s an extra compiler parameter you must add, in order for your project to compile properly:

--static-link-runtime-shared-libraries=true

This is already added as a new default parameter in FlashDevelop projects. But if you’re planning to build projects from outside a similar IDE, you must add this to your compiler string. Otherwise, the compiler will think you have uninitialised constants and warn you so.

Embedding Fonts
Using the EMBED metatag, or even better runtime loading, for fonts is the sensible way forward. The amount of projects I’ve seen where you need to build from an FLA file full of fonts, which you need to hunt down and install is crazy. With Flex SDK 4, you’ll need to add an extra attribute to your embed tag for fonts, called ’embedAsCFF’:

[Embed(source='myfont.ttf', fontName='MY_FONT', fontWeight='regular', unicodeRange='U+0020-U+0040,U+0041-U+005A', mimeType='application/x-font', embedAsCFF='false')]
public static const MY_FONT :Class;

Happy compiling!

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:

import flash.text.Font;
import flash.text.TextFormat;

// UNICODE RANGE REFERENCE
/*
Default ranges
U+0020-U+0040, // Punctuation, Numbers
U+0041-U+005A, // Upper-Case A-Z
U+005B-U+0060, // Punctuation and Symbols
U+0061-U+007A, // Lower-Case a-z
U+007B-U+007E, // Punctuation and Symbols

Extended ranges (if multi-lingual required)
U+0080-U+00FF, // Latin I
U+0100-U+017F, // Latin Extended A
U+0400-U+04FF, // Cyrillic
U+0370-U+03FF, // Greek
U+1E00-U+1EFF, // Latin Extended Additional
*/

[Embed(source = '../fonts/myfont.ttf', fontName = 'MY_FONT',
fontWeight = 'regular', unicodeRange =
'U+0020-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E',
mimeType = 'application/x-font')]
public static const MY_FONT :Class;

public static const DEFAULT_FONT :String        = "MY_FONT";
public static const DEFAULT_TEXT_COLOUR :int = 0xFFFFFF;
public static const DEFAULT_TEXT_SIZE :int = 14;
public static const MY_TEXT_FORMAT :TextFormat = new
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.