You have a flash / flex application where you want to allow users to customize the text formatting, font style / font weight for some text box. So you would provide a small set of fonts which would all get embedded into the swf.
If we working with external fonts in Actionscript 3.0, the external font must have a class definition for allowing the Font class and ApplicationDomain to detect and register it. We can produce class definition for the external font with embed tag in Flex or Font symbol in Flash library.
With Flex 3.0 and mxmlc (Flex SDK 2-3) there’s the embed metatag where you can even define font character ranges which should be embedded for reducing compiled external font swf size:
[Embed( source='font.ttf', fontName='Font Name', unicodeRange='U+0021-U+0021, ... , U+2122-U+2122')]
or with this tag :
[Embed( source='font.ttf', fontName='Font Name', fontWeight='bold', fontStyle='italic')]
Another nice article here
Unfortunatelly if we combining subset tag (unicodeRange) with fontWeight / fontStyle tag will cause a Flex compile error. I hope in Flex 4 this issue fixed.
This article explain the same approach, but it need to add extra font family (Bold or Italic) at specific font but Flex will still treat them as separate fonts.
All the examples (both Flash CS3 or Flex 3.0) I’ve seen so far only address the loading of one font either in regular weight or bold, but not one (subset characters ranges) fonts with regular, weight, italic or bold-italic at the same time. And it seem very difficult for export one (subset characters ranges) font as external font, (ie loading the font from an external swf) and change font weight / font style at runtime in loader (main app) swf.
Luckily I was able make something worked, here what I did :
- Embed font in FLA file with instance textfield at the stage (Benefit: we can make subset of required glyphs only, and font style/weight). We need create four instances textfield (Regular, Bold, Italic and Bold-Italic texfield) and publish the swf for each font, (ie. Verdana_Style.swf for Verdana). In this step the published swf doesn’t contain class definition yet. So we can not import it at runtime.

- Transcoding the published swf (also give a class definition) with Flex 3.0 compiler with Embed metatag and publish to final external swf font (i.e Verdana.swf)
import flash.display.Sprite; import flash.text.Font; public class Verdana extends Sprite { [Embed( source='library/fontstyle/Verdana_Style.swf', fontName='Verdana')] public static var regular:Class; [Embed( source='library/fontstyle/Verdana_Style.swf', fontName='Verdana', fontWeight='bold')] public static var bold:Class; [Embed( source='library/fontstyle/Verdana_Style.swf', fontName='Verdana', fontStyle='italic')] public static var italic:Class; [Embed( source='library/fontstyle/Verdana_Style.swf', fontName='Verdana', fontWeight='bold', fontStyle='italic')] public static var boldItalic:Class; public function Verdana() { super(); Font.registerFont ( regular ); Font.registerFont ( bold ); Font.registerFont ( italic ); Font.registerFont ( boldItalic ); } }
- Import the final external swf font at runtime to main application.
Here the simple actionscript 3.0 with Lithos Pro Regular font demo.package { import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; import flash.text.*; public class DemoExternalFontSimple extends Sprite { public static const FONT_NAME:String = 'Lithos Pro Regular'; public static const FONT_LIB:String = "LithosProRegular.swf"; public function DemoExternalFontSimple () { loadFont ('fontlib/'+FONT_LIB); } private function loadFont ( url:String ) : void { var loader:Loader = new Loader (); loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, fontLoaded ); loader.load (new URLRequest ( url)); } private function fontLoaded ( event:Event ) : void { drawText(); } public function drawText () : void { var tf:TextField = new TextField (); tf.defaultTextFormat = new TextFormat ( FONT_NAME, 16, 0); tf.embedFonts = true; tf.antiAliasType = AntiAliasType.ADVANCED; tf.autoSize = TextFieldAutoSize.LEFT; tf.text = FONT_NAME + " Regular was here...:;*&^% "; tf.selectable = false; tf.rotation = 1; tf.x = 10; tf.y = 10 var tf2:TextField = new TextField (); tf2.defaultTextFormat = new TextFormat ( FONT_NAME, 16, 0, true); tf2.embedFonts = true; tf2.antiAliasType = AntiAliasType.ADVANCED; tf2.autoSize = TextFieldAutoSize.LEFT; tf2.text = FONT_NAME + " Bold was here...:;*&^% "; tf2.selectable = false; tf2.rotation = 1 tf2.x = 10; tf2.y = 50 var tf3:TextField = new TextField (); tf3.defaultTextFormat = new TextFormat ( FONT_NAME, 16, 0, false, true); tf3.embedFonts = true; tf3.antiAliasType = AntiAliasType.ADVANCED; tf3.autoSize = TextFieldAutoSize.LEFT; tf3.text = FONT_NAME + " Italic was here...:;*&^% "; tf3.selectable = false; tf3.x = 10; tf3.y = 100 var tf4:TextField = new TextField (); tf4.defaultTextFormat = new TextFormat( FONT_NAME, 16, 0, true, true); tf4.embedFonts = true; tf4.antiAliasType = AntiAliasType.ADVANCED; tf4.autoSize = TextFieldAutoSize.LEFT; tf4.text = FONT_NAME + " Bold Italic was here...:;*&^% "; tf4.selectable = false; tf4.x = 10; tf4.y = 150; addChild(tf); addChild(tf2); addChild(tf3); addChild(tf4); } } }
October 1st, 2008 at 3:29 am
Hmm..
I don’t believe this is the case, as I do it for all the fonts I export. You can see this right in the code I posted at the article you linked to (http://www.visible-form.com/blog/dynamically-loading-fonts-at-runtime/).
Flex still generates a seperate FontAsset reference for each version of the font you embed, so as far as I can tell, the only real difference between the way you are doing it and the way i was doing is that you are embedding a swf first, and referencing the the embedded fonts, whereas I referenced the ttf files directly.
Whatever works though…probably shouldn’t be such an issue in the first place.
October 1st, 2008 at 3:53 am
Hi Rich,
Thanks for the comment..
Yup I think, the difference between the way I was doing is it only need one font file for embedding all style and weight of font, whereas you did for Arial Narrow need 4 font files (Arial Narrow.ttf, Arial Narrow Bold.ttf, Arial Narrow Italic.ttf, Arial Narrow Bold Italic.ttf).
IMHO, with only need one font to externalize, I think it will easier for working with some rare fonts (and we didn’t need font family for reference the bold/italic style).
BTW, I’ve countered compile error if embedding some fonts with combining fontWeight/fontStyle with unicodeRange.
Thanks!
October 4th, 2008 at 4:08 pm
[…] > ini na!! » Blog Archive » Externalize Font with font subset, font weight and font style at the same time […]
October 9th, 2008 at 10:17 am
thanks. that’s solve my problem.