Flash CS5 Dynamic Text Formatting – Part 4

Embedding font in a Class file

My next approach was to read up on embedding fonts programmatically. So I broke out my Essential Actionscript 3.0 book and on page 740 read up on embedding fonts and going through the exercises in that chapter.

The following is an example of embedding font in a class which can then be used in a class to control the rendering of text in a TextField.

package {
	
	//-------------------------------------------------------------------------
	// Class
	//-------------------------------------------------------------------------
	/**
	 *
	 * Class used to embed Arial Regular font 
     * ONLY in a font library
	 * for use by Flash 5 Pro generated swc libraries
	 *
	 **/ 
	public class ArialEmbedder {
		
		public static const FONT_NAME:String = "_arial";
		
		//these are the elements of the font family 
		//we want to embed using direct path location 
        //to Font directory on Windows XP
		[Embed(source='C:/Windows/Fonts/ARIAL.TTF', 
			   fontFamily='_arial',
			   fontWeight='regular',
			   fontStyle='regular',
			   embedAsCFF='false')]
		public static var _arial:Class;
		
	}
}

Note that this increases the file size dramatically since it includes all characters in the font set. We just want to restrict ourselves to a subset of the characters. So this class is modified in the following manner:

package {
	
	//-------------------------------------------------------------------------
	// Class
	//-------------------------------------------------------------------------
	/**
	 *
	 * Class used to embed Arial Regular font 
	 * ONLY in a font library
	 * for use by Flash 5 Pro generated swc libraries
	 * 
	 *
	 * Commonly used ranges
	 * 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
	 * Special Chars: U+00B5 for µ,U+2265 for ≥
	 * alt+230,alt+242
	 * 
	 * 
	 **/ 
	public class ArialEmbedder {
		
		public static const FONT_NAME:String = "_arial";

		//these are the elements of the font family 
		//we want to embed using direct path location 		
		//to Font directory on Windows XP
		//
		[Embed(source='C:/Windows/Fonts/ARIAL.TTF', 
			   fontFamily='_arial',
			   fontWeight='regular',
			   fontStyle='regular',
			   unicodeRange='U+00B5,U+2265,U+0041-U+005A,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',
			   embedAsCFF='false')]
		public static var _arial:Class;
		
	}
}

Final step is to note this class only contains font for Arial regular font. If you have a flash file that uses more than just regular arial font – perhaps you need bold font, italic font, and bolditalic font, then you should create a class which contains all of these but note that the inclusion increases the file size of the font class which will be embedded in the flash file. The following is an example of embedding multiple fonts in a single class:

package {
	
	//-------------------------------------------------------------------------
	// Class
	//-------------------------------------------------------------------------
	/**
	 * Class used to embed Ms Reference Sans Serif 
	 * font in a font library for use by Flash 5 Pro 
	 * generated swc libraries
	 *
	 * Commonly used ranges
	 * 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
	 * Special Chars: U+00B5 for µ,U+2265 for ≥
	 * alt+230,alt+242
	 * @author Margo Powell
	 * @version $Id$
	 **/ 
	public class MsRefEmbedder {
		
		public static const FONT_NAME:String = "_msRef";

		//these are the elements of the font family 
		//we want to embed using direct path location 		
		//to Font directory on Windows XP
		[Embed(source='C:/Windows/Fonts/REFSAN.TTF', 
			   fontFamily='_msRef',
			   fontWeight='regular',
			   fontStyle='regular',
			   unicodeRange='U+00B5,U+2265,U+0041-U+005A,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',
			   embedAsCFF='false')]
		public static var _msRef:Class;

		//note we are using complete fontWeight and fontStyle attributes
		[Embed(source='C:/Windows/Fonts/MS Reference Sans Serif Bold.ttf', 
			   fontFamily='_msRef', 
			   fontWeight='bold',
			   fontStyle='regular',
			   unicodeRange='U+00B5,U+2265,U+0041-U+005A,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',
			   embedAsCFF='false' )]
		public static var _msRefBold:Class;

		//note we are using embedAsCFF set to false, since using DefineFont3 for TextFields
		//if left to true, would useDefineFont4 and not render in the TextField
		[Embed(source='C:/Windows/Fonts/MS Reference Sans Serif Bold Italic.ttf', 
			   fontFamily='_msRef', 
			   fontWeight='bold', 
			   fontStyle='italic',
			   unicodeRange='U+00B5,U+2265,U+0041-U+005A,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',
			   embedAsCFF='false')]
		public static var _msRefBoldItalic:Class;

		[Embed(source='C:/Windows/Fonts/MS Reference Sans Serif Italic.ttf', 
			   fontFamily='_msRef', 
			   fontWeight='regular', 
			   fontStyle='italic',
			   unicodeRange='U+00B5,U+2265,U+0041-U+005A,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',
			   embedAsCFF='false')]
		public static var _msRefItalic:Class;
	}
}

Next in Series Part 5

Advertisements

One Response to Flash CS5 Dynamic Text Formatting – Part 4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: