Return to Snippet

Revision: 49362
at July 20, 2011 21:08 by adrianparr


Updated Code
///////////////////////////////////////////////////////////////////////////////////////////
// FontSwfDocumentClass.as (Publish this to create FontSWF.swf, which we will then load in to our main application below)
//
package {
	
	import flash.display.Sprite;
	
	public class FontSwfDocumentClass extends Sprite {
		
		// This font was downloaded from here ...
		// http://www.dafont.com/sketchetik.font
		[Embed(source = "C:/Users/Adrian/Desktop/Sketchetik-Light.otf",
		fontName = "Sketchetik-Light",
		fontStyle = "normal",
		fontWeight = "normal",
		unicodeRange = "U+0020-003C,U+003E-007E,U+00A0,U+00A3,U+00A9,U+00AC,U+00AE,U+00BA,U+2013,U+2018-2019,U+201C-201D,U+20AC",
		mimeType = "application/x-font",
		advancedAntiAliasing = true
		,embedAsCFF=false)]
		public static var SketchetikLight:Class;
		
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
// This is the Document Class that actually loads in the FontSWF and uses the embedded font
//
package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			loadFontSwf("FontSWF.swf");
		}
		
		private function loadFontSwf(url:String):void {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontSwfLoaded);
			loader.load(new URLRequest(url));
		}
		
		private function onFontSwfLoaded(e:Event):void {
			var FontLibrary:Class = e.target.applicationDomain.getDefinition("FontSwfDocumentClass") as Class;
			Font.registerFont(FontLibrary.SketchetikLight);
			drawText();
		}

		public function drawText():void {
			var tfmt:TextFormat = new TextFormat();
			tfmt.color = 0x000000;
			tfmt.size = 150;
			tfmt.font = "Sketchetik-Light";
			
			var tf:TextField = new TextField();
			tf.defaultTextFormat = tfmt;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.embedFonts = true;
			tf.width = 800;
			tf.height = 600;
			tf.x = 100;
			tf.y = 100;
			tf.border = true;
			tf.rotation = 15;
			tf.text = "Hello World";
			addChild(tf);
		}
		
	}
	
}

// Handy URLs ...
// http://blog.madebypi.co.uk/2011/02/28/fontswffer-1-1/
// http://nochump.com/blog/archives/20
// http://www.tillschneidereit.de/unicode_range_tool.html
// http://yourpalmark.com/2009/04/05/embedding-fonts-using-external-swf-files/
// http://blog.flexexamples.com/2007/10/25/embedding-fonts-from-a-flash-swf-file-into-a-flex-application/

Revision: 49361
at July 20, 2011 21:06 by adrianparr


Updated Code
///////////////////////////////////////////////////////////////////////////////////////////
// FontSwfDocumentClass.as
//
package {
	
	import flash.display.Sprite;
	
	public class FontSwfDocumentClass extends Sprite {
		
		// This font was downloaded from here ...
		// http://www.dafont.com/sketchetik.font
		[Embed(source = "C:/Users/Adrian/Desktop/Sketchetik-Light.otf",
		fontName = "Sketchetik-Light",
		fontStyle = "normal",
		fontWeight = "normal",
		unicodeRange = "U+0020-003C,U+003E-007E,U+00A0,U+00A3,U+00A9,U+00AC,U+00AE,U+00BA,U+2013,U+2018-2019,U+201C-201D,U+20AC",
		mimeType = "application/x-font",
		advancedAntiAliasing = true
		,embedAsCFF=false)]
		public static var SketchetikLight:Class;
		
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
// This is the Document Class that actually loads in the FontSWF and uses the embedded font
//
package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			loadFontSwf("FontSWF.swf");
		}
		
		private function loadFontSwf(url:String):void {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontSwfLoaded);
			loader.load(new URLRequest(url));
		}
		
		private function onFontSwfLoaded(e:Event):void {
			var FontLibrary:Class = e.target.applicationDomain.getDefinition("FontSwfDocumentClass") as Class;
			Font.registerFont(FontLibrary.SketchetikLight);
			drawText();
		}

		public function drawText():void {
			var tfmt:TextFormat = new TextFormat();
			tfmt.color = 0x000000;
			tfmt.size = 150;
			tfmt.font = "Sketchetik-Light";
			
			var tf:TextField = new TextField();
			tf.defaultTextFormat = tfmt;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.embedFonts = true;
			tf.width = 800;
			tf.height = 600;
			tf.x = 100;
			tf.y = 100;
			tf.border = true;
			tf.rotation = 15;
			tf.text = "Hello World";
			addChild(tf);
		}
		
	}
	
}

// Handy URLs ...
// http://blog.madebypi.co.uk/2011/02/28/fontswffer-1-1/
// http://nochump.com/blog/archives/20
// http://www.tillschneidereit.de/unicode_range_tool.html
// http://yourpalmark.com/2009/04/05/embedding-fonts-using-external-swf-files/
// http://blog.flexexamples.com/2007/10/25/embedding-fonts-from-a-flash-swf-file-into-a-flex-application/

Revision: 49360
at July 20, 2011 21:01 by adrianparr


Initial Code
///////////////////////////////////////////////////////////////////////////////////////////
// FontSwfDocumentClass.as
//
package {
	
	import flash.display.Sprite;
	
	public class FontSwfDocumentClass extends Sprite {
		
		// This font was downloaded from here ...
		// http://www.dafont.com/sketchetik.font
		[Embed(source = "C:/Users/Adrian/Desktop/Sketchetik-Light.otf",
		fontName = "Sketchetik-Light",
		fontStyle = "normal",
		fontWeight = "normal",
		unicodeRange = "U+0020-003C,U+003E-007E,U+00A0,U+00A3,U+00A9,U+00AC,U+00AE,U+00BA,U+2013,U+2018-2019,U+201C-201D,U+20AC",
		mimeType = "application/x-font",
		advancedAntiAliasing = true
		,embedAsCFF=false)]
		public static var SketchetikLight:Class;
		
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
// This is the Document Class that actually loads in the FontSWF and uses the embedded font
//
package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			loadFontSwf("FontSWF.swf");
		}
		
		private function loadFontSwf(url:String):void {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontSwfLoaded);
			loader.load(new URLRequest(url));
		}
		
		private function onFontSwfLoaded(e:Event):void {
			var FontLibrary:Class = e.target.applicationDomain.getDefinition("FontSwfDocumentClass") as Class;
			Font.registerFont(FontLibrary.SketchetikLight);
			drawText();
		}

		public function drawText():void {
			var tfmt:TextFormat = new TextFormat();
			tfmt.color = 0x000000;
			tfmt.size = 150;
			tfmt.font = "Sketchetik-Light";
			
			var tf:TextField = new TextField();
			tf.defaultTextFormat = tfmt;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.embedFonts = true;
			tf.width = 800;
			tf.height = 600;
			tf.x = 100;
			tf.y = 100;
			tf.border = true;
			tf.rotation = 15;
			tf.text = "Hello World";
			addChild(tf);
		}
		
	}
	
}

Initial URL
http://nochump.com/blog/archives/20

Initial Description
These two classes demonstrate how to create an external font SWF that contains embedded fonts, and how to then load that external font SWF and access and use the fonts inside it.

Initial Title
AS3 Loading and Accessing fonts in external SWF at Runtime

Initial Tags
class, load

Initial Language
ActionScript 3