Apr 13, 2009
Embedded fonts that work with Flash IDE and Flex SDK
In a recent project we had problems with the embedding for the fonts. Since we work with both the Flash IDE and the Flex SDK (eclipse FDT) at my work we needed something that worked when compiling in the Flash IDE but also when compiling with fcsh (Flex Compiler Shell). I tested most of the techniques out there but found that most of them didn't work as I wanted together with the Flash IDE. So I just couldn't get them to work for both.
So here is an easy way to do it that work with flash IDE and fcsh. First you need one file for every font you need to embed. Here is an example on how it could look like.
Arial_Black.as
-
package com.arwidthornstrom.assets {
-
import flash.text.Font;
-
-
[Embed(source = '../../../material/Arial_Black.ttf', fontName='ArialBlack' )]
-
public class Arial_Black extends Font {
-
-
}
-
}
The important thing is that it extends Font and that you use the [Embed to embed the font in the swf. You can of course add fontWeight and unicodeRange if you want to. But its not important in this example. When you got all the font files you just need to export the FontLibrary.as
FontLibrary.as
-
package com.arwidthornstrom.assets {
-
import flash.display.Sprite;
-
import flash.text.Font;
-
-
public class FontLibrary extends Sprite {
-
-
public static const ARIAL_BLACK:String = "ArialBlack";
-
-
public function FontLibrary() {
-
Font.registerFont(Arial_Black);
-
}
-
}
-
}
To make it easy to use I add a public static const with the fontname, then you can use FontLibrary.ARIAL_BLACK when you create the textformat. When you load this into whatever framework you are using it will automatically register the fonts with the Font class.
Export the FontLibrary.as either with fcsh or just set it as DocumentClass in the Flash IDE. Now you just load the fontlibrary.swf into whatever swf you like and the use FontLibrary.ARIAL_BLACK when you need that font.
FontTest.as
-
package {
-
-
import com.arwidthornstrom.assets.FontLibrary;
-
import flash.display.Sprite;
-
import flash.display.Loader;
-
import flash.text.TextField;
-
import flash.text.TextFormat;
-
import flash.text.TextFieldAutoSize;
-
import flash.net.URLRequest;
-
import flash.events.Event;
-
-
public class FontTest extends Sprite {
-
-
private var _ldr:Loader;
-
private var _text:TextField;
-
-
public function FontTest() {
-
-
_ldr = new Loader();
-
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
-
_ldr.load(new URLRequest("fontlibrary.swf"));
-
-
}
-
-
private function completeHandler(e:Event):void {
-
-
trace("FONTS LOADED");
-
-
_text = new TextField();
-
var format:TextFormat = new TextFormat(FontLibrary.ARIAL_BLACK, 30, 0xFFFF00);
-
_text.defaultTextFormat = format;
-
_text.autoSize = TextFieldAutoSize.LEFT;
-
_text.embedFonts = true;
-
_text.text = "ARIAL BLACK arial black";
-
-
_text.x = 20;
-
_text.y = 20;
-
-
this.addChild(_text);
-
}
-
-
}
-
-
}
It is important to not use the FontLibrary before the fontlibrary.swf is loaded.
Links:
http://www.betriebsraum.de/blog/2007/06/22/runtime-font-loading-with-as3-flash-cs3-not-flex/
http://blog.sitedaniel.com/2008/10/fontcontrol-class-external-font-library/
Source:
Hey man.
Thank you so much, i’v been having this problem for a while now. This article helped me alot. Thanks dude
/ Sebastian
Thanks for a simple method for embedding fonts without having to deal with Flex’s asset proxy classes. It would be nice if Adobe would have put something like this in their documentation. They just kind of leave you guessing how to embed fonts if you aren’t using MXML.