Flash, Dynamic font embedding
It’s now easy to dynamically embed fonts loaded from a swf using Denis Kolyako’s getDefinitionNames class. This combined with external styleSheets is a great solution for multilingual projects which require different fonts to be used for different languages.
To get it working you first need to create a fonts swf. Create font symbols for the fonts you require in an fla, ensuring you tick ‘Export for actionscript’ and ‘Export in frame 1′ in the symbols properties window. Publish as a swf.
Now you can use something like the following code in your application to load the fonts swf and embed all the available fonts in the swf.
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import ru.etcs.utils.getDefinitionNames;
import flash.text.Font;
var fontLoader:Loader = new Loader();
fontLoader.contentLoaderInfo.addEventListener(Event.INIT, fontLoadedHandler);
fontLoader.load(new URLRequest("fonts/fonts.swf"));
function fontLoadedHandler(event : Event) : void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
var def:Array = getDefinitionNames(loaderInfo, false, true);
for(var i : int = 0; i < def.length; i++)
{
var c : Class = Class(loaderInfo.applicationDomain.getDefinition(def[i]));
if(new c() is Font)
{
Font.registerFont(c);
}
}
var textFormat:TextFormat = new TextFormat();
textFormat.font = "Compacta BT";
textFormat.size = 40;
var textField:TextField = new TextField();
textField.defaultTextFormat = textFormat;
textField.embedFonts = true;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.antiAliasType = AntiAliasType.ADVANCED;
textField.text = "embedded font text";
addChild(textField);
}
You can either use TextFormat or Styles once the font is registered, I used TextFormat to keep the example nice and quick.