Problems embedding multiple members of the same font-family in Actionscript 3.0

I came across a problem the other day which it turns out is quite common. Normally when I am dealing with fonts in Actionscript I simple create a new font in the library and call that via the SWC in FDT. However, it turns out that if I make a font called Didot_Regular and Didot_Bold and tick the appropriate ‘Bold’ and ‘Regular’ boxes my compiled SWF fails to differentiate between the two.

The solution is create your fonts in a Class and compile this as a separate SWC or SWF. Here’s how :

package fonts {
	
	import flash.display.Sprite;

	/**
	 * @author Dan
	 */
	 
	public class MyFonts extends Sprite {
        
		[Embed(source="/../fonts/Didot.ttc", fontName="Didot", fontStyle="normal", fontWeight="bold", mimeType="application/x-font-truetype")]
		public static var DidotBold		:	Class;
		
		[Embed(source="/../fonts/Didot.ttc", fontName="Didot", fontStyle="normal", fontWeight="normal", mimeType="application/x-font-truetype")]
		public static var Didot			:	Class;
		
	}
}

Now compile this as a SWF or as a SWC library and then use the fonts as normal :


			var tf : TextFormat = new TextFormat();
			tf.font = new MyFonts_DidotBold().fontName;
			tf.bold = true;
			tf.size = 18;
			tf.color = 0xFFFFFF;