Advanced text rendering

ActionScript 3.0 provides a variety of classes in the flash.text package to control the properties of displayed text, including embedded fonts, anti-aliasing settings, alpha channel control, and other specific settings. The ActionScript 3.0 Language and Components Reference provides detailed descriptions of these classes and properties, including the CSMSettings, Font, and TextRenderer classes.

Subtopics

Using embedded fonts
Controlling sharpness, thickness, and anti-aliasing

Using embedded fonts

When you specify a specific font for a TextField in your application, Flash Player will look for a device font (a font that resides on the user's computer) with the same name. If it doesn't find that font on the user's system, or if the user has a slightly different version of a font with that name, the text display could look very different from what you intend.

To make sure the user sees exactly the right font, you can embed that font in your application's SWF file. Embedded fonts have a number of benefits:

The biggest limitation to using embedded fonts is that they increase the file size or download size of your application.

The exact method of embedding a font file into your application's SWF file varies according to your development environment. <updated: 6/19/2007>

Once you have embedded a font you can make sure a TextField uses the correct embedded font:

Embedding a font in Flash

The Flash authoring tool lets you embed almost any font you have installed on your system, including TrueType fonts and Type 1 Postscript fonts.

There are many ways to embed fonts in a Flash application, including:

For more details about how to embed fonts in Flash applications, see "Embedded fonts for dynamic or input text fields" in Using Flash.

Controlling sharpness, thickness, and anti-aliasing

By default, Flash Player determines the settings for text display controls like sharpness, thickness, and anti-aliasing as text resizes, changes color, or is displayed on various backgrounds. In some cases, like when you have very small or very large text, or text on a variety of unique backgrounds, you may want to maintain your own control over these settings. You can override the Flash Player settings using the flash.text.TextRenderer class and its associated classes, like the CSMSettings class. These classes give you precise control over the rendering quality of embedded text. For more information about embedded fonts, see Using embedded fonts.

NOTE

 

The flash.text.TextField.antiAliasType property must have the value AntiAliasType.ADVANCED in order for you to set the sharpness, thickness, or the gridFitType property, or to use the TextRenderer.setAdvancedAntiAliasingTable() method. [Removed phrase incorrectly identifying the default antiAliasType value as ADVANCED. The correct default value is NORMAL: 4/26/07]

The following example applies custom continuous stroke modulation (CSM) properties and formatting to displayed text using an embedded font called myFont. When the user clicks on the displayed text, Flash Player applies the custom settings:

var format:TextFormat = new TextFormat();
format.color = 0x336699;
format.size = 48;
format.font = "myFont";

var myText:TextField = new TextField();
myText.embedFonts = true;
myText.autoSize = TextFieldAutoSize.LEFT;
myText.antiAliasType = AntiAliasType.ADVANCED;
myText.defaultTextFormat = format;
myText.selectable = false;
myText.mouseEnabled = true;
myText.text = "Hello World";
addChild(myText);
myText.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:Event):void
{
    var myAntiAliasSettings = new CSMSettings(48, 0.8, -0.8);
    var myAliasTable:Array = new Array(myAntiAliasSettings);
    TextRenderer.setAdvancedAntiAliasingTable("myFont", FontStyle.ITALIC, TextColorType.DARK_COLOR, myAliasTable);
}

Flash CS3