O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Determining Glyph Availability

To determine whether a specific embedded font has a glyph for a specific character or set of characters, we use the Font class's instance method hasGlyphs( ). When provided with a string argument, the hasGlyphs( ) method returns a Boolean value indicating whether the font has all the glyphs required to display that string.

Warning

The Font class's instance method hasGlyphs( ) works with embedded fonts only. There is no way to determine whether a given device font has a glyph for a specific character.

To use the hasGlyphs( ) method, we must first obtain a reference to the Font object for the font in question. To do so, we use a for loop to search the array returned by enumerateFonts( ). For example, the following code retrieves a reference to the Font object for the font Verdana, and assigns it to the variable font:

var fontName:String = "Verdana";
var font:Font;
var fonts:Array = Font.enumerateFonts(true);
for (var i:int = 0; i < fonts.length; i++) {
  if (fonts[i].fontName == fontName) {
    font = fonts[i];
    break;
  }
}

Once a reference to the desired Font object has been obtained, we can then use hasGlyphs( ) to check if the corresponding font has the glyphs required to display a given string. For example, the following code checks if the font Verdana can display the English string "Hello world":

trace(font.hasGlyphs("Hello world"));  // Displays: true

The following code checks if the font Verdana can display the Japanese string "":

trace(font.hasGlyphs()); // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required