Tuesday, June 19, 2007

Flash CS3 & ActionScript 3 Example: Using hasGlyphs() with embedded fonts

This example demonstrates the hasGlyphs() function with an embedded font (no need for Flex2).

Step 1: Embed Font:

Create a new movie and add a new font to your Library:

now edit the linkage properties:




Step 2: Add Code:

Create a new text file with the name Test.as in the same folder and add the following code:

package {
import flash.display.Sprite;
import flash.text.TextField;

public class Test extends Sprite {

private var textFieldFactory:TextFieldFactory;

public function Test() {
trace("Embedded Font Test")
textFieldFactory = new TextFieldFactory();
var tf:TextField = textFieldFactory.getNewTextField("Bingo!", 0x000000, 14);
addChild(tf);
}

}
}

Now create a new text file with the name TextFieldFactory.as in the same folder and add the following code:

package {

import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormatAlign;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.utils.*;


public class TextFieldFactory {

private static var embeddedFont:Font = null;

public function TextFieldFactory() {
var embeddedFontClass:Class = getDefinitionByName("EmbeddedGaramond") as Class;
Font.registerFont(embeddedFontClass);
var embeddedFontsArray:Array = Font.enumerateFonts(false);
embeddedFont = embeddedFontsArray[0];
}

public function getNewTextField(t:String, c:uint, s:uint):TextField {
if ((embeddedFont != null) && embeddedFont.hasGlyphs(t)) {
return getNewTextFieldEmbeddedFont(t, c, s)
}
else {
return getNewTextFieldDeviceFont(t, c, s)
}
}

public function getNewTextFieldEmbeddedFont(t:String, c:uint, s:uint):TextField {
trace("getNewTextFieldEmbeddedFont: "+t);

var tf:TextField = new TextField();
var fo:TextFormat = new TextFormat();

fo.font = embeddedFont.fontName;
fo.color = c;
fo.size = s
tf.defaultTextFormat = fo;
tf.autoSize = TextFieldAutoSize.LEFT
tf.type = TextFieldType.DYNAMIC;
tf.multiline = false;
tf.selectable = false;
tf.textColor = c;
tf.embedFonts = true;
tf.antiAliasType = AntiAliasType.ADVANCED;
tf.border = true;
tf.wordWrap = false;
tf.text = t;
return tf;
}

public function getNewTextFieldDeviceFont(t:String, c:uint, s:uint):TextField {
var tf:TextField = new TextField();
var fo:TextFormat = new TextFormat();

trace("getNewTextFieldDeviceFont: "+t);
fo.font = "_serif";
fo.color = c;
fo.size = s
tf.defaultTextFormat = fo;
tf.autoSize = TextFieldAutoSize.LEFT
tf.type = TextFieldType.DYNAMIC;
tf.multiline = false;
tf.selectable = false;
tf.textColor = c;
tf.embedFonts = false;
tf.border = true;
tf.wordWrap = false;
tf.text = t;
return tf;
}

}
}

Set the document class to Test and save all files. Pulish and run the movie and you will see the words "Bingo!" set with the embedded font.

1 comment:

ain said...

If you'd add some special characters to the 'Bingo!' String there, e.g. 'Bingožšžšžš!', you'd unfortunately end up with an empty field.

Click here to read another article on embedding fonts in Flash CS3