Embed special characters in dynamically created TextFields

I stumbled across Fredrick Bäcker’ UnicodeHexCreator AIR app while I was trying to localise an existing micro site. The trouble was embedding special characters in text fields. The solution is to compile an AS3 project with Flex SDK and embed the fonts within it.

package
{
	import flash.display.Sprite;
	import flash.text.Font;
 
	public class FontList extends Sprite
	{
		// Embed system font with special characters --> Unicode ranges come from flash-unicode-table.xml (FLEXSDK/framworks) and UnicodeHexCreator
		[Embed(systemFont='Arial', fontName='_Arial', mimeType='application/x-font', 
		unicodeRange="U+0061-U+0061,U+00BF-U+00BF,U+00C1-U+00C2,U+00C4-U+00C4,U+00C9-U+00C9,U+00CD-U+00CE,U+00D1-U+00D1,U+00D3-U+00D3,U+00D5-U+00D6,U+00DA-U+00DA,U+00DC-U+00DD,U+00E1-U+00E2,U+00E4-U+00E4,U+00E9-U+00E9,U+00ED-U+00EE,U+00F1-U+00F1,U+00F3-U+00F3,U+00F5-U+00F6,U+00FA-U+00FA,U+00FC-U+00FD,U+0102-U+0107,U+010C-U+010F,U+0118-U+011B,U+0141-U+0144,U+0147-U+0148,U+0158-U+015B,U+015E-U+0161,U+0163-U+0165,U+016E-U+016F,U+0179-U+017E,U+0410-U+042A,U+042C-U+042C,U+042E-U+044A,U+044C-U+044C,U+044E-U+044F,U+2260-U+2260,U+0020,U+0041-U+005A, U+0020,U+0061-U+007A, U+0030-U+0039,U+002E, U+0020-U+002F,U+003A-U+0040,U+005B-U+0060,U+007B-U+007E, 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, U+0020,U+00A1-U+00FF,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183, U+0100-U+01FF,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183, U+0180-U+024F,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183, U+1E00-U+1EFF,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183, U+0400-U+04CE,U+2000-U+206F,U+20A0-U+20CF,U+2100-U+2183")]
		public static var _Arial:Class;
 
		public function FontList()
		{
			try
			{
				Font.registerFont(_Arial);
			}
			catch(e:Error)
			{
				trace("Font could not be loaded: _Arial");
			}
		}
	}
}

Load the created swf into the flash application and refer to the font through a new TextFormat

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.Complete, fontsLoaded);
 
function fontsLoaded(e:Event):void
{
    var textField = new TextField();
    textField.defaultTextFormat = new TextFormat("_Arial");
}

Leave a comment