How to Embed Font in AS3 ans Shared Library

Here is a simple way how to embed font using ActionScript 3 and shared library.

I rename this file and should have MyFont.swf

// simple way to embed font using AS3
package {
	import flash.display.*;
	import flash.text.*;
	// I'm using FlashDevelop so it is "extends Sprite"
	// If using Flash IDE replace "Sprite" with "MovieClip"
	public class MyFont extends Sprite {
		// embeding font with font weight, unicode range
		// i used public static to share publicly
		[Embed(source = "C:/Windows/Fonts/Arial.ttf", fontName = "Arial", mimeType = "application/x-font", fontWeight = "normal", unicodeRange = "U+0020-U+007E, U+005B-U+0060, U+007B-U+007E")]
    	public static var RegularFont:Class;
		public function MyFont():void {
			// need to register the font

for unicode range:
* Uppercase : U+0020,U+0041-U+005A
* Lowercase : U+0020,U+0061-U+007A
* Numerals : U+0030-U+0039,U+002E
* Punctuation : U+0020-U+002F,U+003A-U+0040,U+005B-U+0060,U+007B-U+007E
* Basic Latin : U+0020-U+002F, U+0030-U+0039, U+003A-U+0040, U+0041-U+005A, U+005B-U+0060, U+0061-U+007A, U+007B-U+007E
Some say that unicodeRange=”English” is working, but I haven’t tested it yet.
You can also learn more about the Latin Unicode characters from Wikipedia.
You can check here the unicode viewer.

for font weight:
* bold
* regular
* italic
* underline

In other source code or other project ( with Main.swf)

package {
	import flash.display.*;
	import flash.text.*;
	public class Main extends MovieClip {
		private var fontsSwf:String = "MyFont.swf";
		private var fontsPath:String = "assets/swfs/fonts/";
		private var tFormat:TextFormat = new TextFormat();
		public function Main():void {
			var loader:Loader = new Loader();
			loader.load(new URLRequest(fontsPath + fontsSwf));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontInit);
		function fontInit(evt:Event) {
			// to get the access the shared library
			var MyFont:Class = evt.currentTarget.applicationDomain.getDefinition("MyFont_RegularFont") as Class;
			// initialize the font
			var myFont:Font = new MyFont();
			// setting textfield & textformat
			var tFormat:TextFormat = new TextFormat();
			tFormat.font = myFont.fontName
			var tf:TextField = new TextField();
			tf.embedFonts = true;
			tf.x = stage.stageWidth / 2;
			tf.y = stage.stageHeight / 2;
			tf.autoSize = "center";
			tf.multiline = true;
			tf.text = "\"Do not be a hard-worker, \nbut be a smart worker.\" - Mykhel Trinitaria";
			// rotate the textfield to check if embeding font is correct
			tf.rotation = 10;

This is very useful when developing games or flash applications with very minimal filesize required. Instead of embedding the whole “FONT”, we can embed the selected character only.

I can tell “goodbye” to the Flash IDE’s Library, we have learned here how to embed fonts (can also be used in images, songs, flvs), and how to use the external shared library.

4 Comments to “How to Embed Font in AS3 ans Shared Library”

  1. By Album Lyrics, April 14, 2010 @ 8:34 am

    Thanks for a great post and interesting comments. I found this post while surfing the web for downloads. Thanks for sharing this article.

  2. By karlus, July 18, 2010 @ 3:19 pm

    to preserve the beauty of your font add these :D

    textFormat.kerning = true;

    tf.antiAlias = AntiAliasType.ADVANCED;

  3. By Ezz, January 20, 2011 @ 8:52 pm

    thanks very much

  4. By Hubert, December 4, 2012 @ 12:25 am

    Hi I just try do do online loading swf contain font and can not manage to find good example – yours look quite clear just do not unsderstand – what “MyFont_RegularFont” in beacket is refering to !? main class or font class !???

    var MyFont:Class = evt.currentTarget.applicationDomain.getDefinition(“MyFont_RegularFont”) as Class;

    I did simple and it is work when I usting if offline but when I upload then I load font Textfield completly going to heigth 0 and can not type any text – it so strange as when I do it exacly this same offline work perfectly , any advice or reference would be very appriseited :)


RSS feed for comments on this post. TrackBack URI

Leave a Reply