com.flagstone.transform.util.font
Class Font

java.lang.Object
  extended by com.flagstone.transform.util.font.Font

public final class Font
extends Object

Font is used to add embedded fonts to a movie.

Flash supports two types of font definition: embedded fonts where the Flash file contains the glyphs that are drawn to represents the text characters and device fonts where the font is provided by the Flash Player showing the movie. Embedded fonts are preferred since the movie will always look the same regardless of where it is played - if a Flash Player does not contain a device font it will substitute it with another.

Device fonts can be added to a movie by simply creating a DefineFont or DefineFont2 object which contain the name of the font. An embedded font must contain all the information to draw and layout the glyphs representing the text to be displayed. The Font class hides all this detail and makes it easy to add embedded fonts to a movie.

The Font class can be used to create embedded fonts in three ways:

  1. Using TrueType or OpenType font definition stored in a file.
  2. Using an existing font definition from a flash file.
  3. Using a given Java AWT font as a template.

For OpenType or TrueType fonts, files with the extensions ".otf" or ".ttf" may be used. Files containing collections of fonts ".otc" are not currently supported.

Using an existing Flash font definition is the most interesting. Fonts can initially be created using AWT Font objects or TrueType files and all the visible characters included. If the generated Flash definition is saved to a file it can easily and quickly be loaded. Indeed the overhead of parsing an AWT or TrueType font is significant (sometimes several seconds) so creating libraries of "pre-parsed" flash fonts is the preferred way of use fonts.


Constructor Summary
Font()
           
 
Method Summary
 void addGlyph(char code, Glyph glyph)
          Add a character and the corresponding glyph that is displayed to the font.
 void addMissingGlyph(char code)
          Add a character to the font where the missing glyph will be displayed.
 int advanceForCharacter(char character)
          Returns the default advance for the font as defined in the EM Square - conceptually a font with a point size of 1024.
 boolean canDisplay(char character)
          Tests whether a character can be displayed by the font or whether the "missing" character glyph (usually an empty box) will be displayed instead.
 int canDisplay(String aString)
          Tests whether the font can display all the characters in a string.
 char characterForGlyph(int index)
          Returns the characters code for the glyph at a specified index in the font.
 DefineFont2 defineFont(int identifier, List<Character> characters)
          Create and return a DefineFont2 object that contains information to display a set of characters.
 int getAscent()
          Get the ascent for the font in twips.
 int getDescent()
          Get the descent for the font in twips.
 CharacterFormat getEncoding()
          Get the encoding scheme used for the character codes, either UCS2, ANSI or SJIS.
 FontFace getFace()
          Get the FontFace that contains the font name and style.
 Glyph getGlyph(int index)
          Get the glyph from the specified position in the table.
 char getHighestChar()
          Get the highest character code defined in the font.
 int getLeading()
          Returns the leading for the font in twips.
 int getMissingGlyph()
          Get the index of the glyph used to represent characters that are not supported int the font.
 int getNumberOfGlyphs()
          Get the number of glyphs defined in the font.
 int glyphForCharacter(char character)
          Returns the glyph for the specified character.
 void setAscent(int dist)
          Sets the ascent for the font in twips.
 void setDescent(int dist)
          Sets the descent for the font in twips.
 void setEncoding(CharacterFormat enc)
          Set the encoding scheme used for the character codes, either UCS2, ANSI or SJIS.
 void setFace(FontFace fontFace)
          Set the FontFace that contains the font name and style.
 void setHighestChar(char highest)
          Set the highest character code defined in the font.
 void setLeading(int dist)
          Sets the leading for the font in twips.
 void setMissingGlyph(int index)
          Select the glyph used to represent characters that are not supported int the font.
 void setNumberOfGlyphs(int count)
          Set the number of glyphs defined in the font.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Font

public Font()
Method Detail

getFace

public FontFace getFace()
Get the FontFace that contains the font name and style.

Returns:
the FontFace.

setFace

public void setFace(FontFace fontFace)
Set the FontFace that contains the font name and style.

Parameters:
fontFace - the FontFace.

getEncoding

public CharacterFormat getEncoding()
Get the encoding scheme used for the character codes, either UCS2, ANSI or SJIS.

Returns:
the encoding used for the character codes.

setEncoding

public void setEncoding(CharacterFormat enc)
Set the encoding scheme used for the character codes, either UCS2, ANSI or SJIS.

Parameters:
enc - the encoding used for the character codes.

getAscent

public int getAscent()
Get the ascent for the font in twips.

Returns:
the ascent for the font.

setAscent

public void setAscent(int dist)
Sets the ascent for the font in twips.

Parameters:
dist - the ascent for the font in the range -32768..32767.

getDescent

public int getDescent()
Get the descent for the font in twips.

Returns:
the descent for the font.

setDescent

public void setDescent(int dist)
Sets the descent for the font in twips.

Parameters:
dist - the descent for the font in the range -32768..32767.

getLeading

public int getLeading()
Returns the leading for the font in twips.

Returns:
the leading for the font.

setLeading

public void setLeading(int dist)
Sets the leading for the font in twips.

Parameters:
dist - the descent for the font in the range -32768..32767.

getNumberOfGlyphs

public int getNumberOfGlyphs()
Get the number of glyphs defined in the font.

Returns:
the number of glyphs.

setNumberOfGlyphs

public void setNumberOfGlyphs(int count)
Set the number of glyphs defined in the font.

Parameters:
count - the number of glyphs.

getHighestChar

public char getHighestChar()
Get the highest character code defined in the font.

Returns:
the character with the highest character code.

setHighestChar

public void setHighestChar(char highest)
Set the highest character code defined in the font.

Parameters:
highest - the highest character code.

getMissingGlyph

public int getMissingGlyph()
Get the index of the glyph used to represent characters that are not supported int the font.

Returns:
the index of the glyph for unsupported characters.

setMissingGlyph

public void setMissingGlyph(int index)
Select the glyph used to represent characters that are not supported int the font.

Parameters:
index - the index of the glyph that will be used to display unsupported characters.

getGlyph

public Glyph getGlyph(int index)
Get the glyph from the specified position in the table.

Parameters:
index - the index of the glyph.
Returns:
the corresponding glyph.

addGlyph

public void addGlyph(char code,
                     Glyph glyph)
Add a character and the corresponding glyph that is displayed to the font.

Parameters:
code - the character
glyph - the glyph displayed for the character.

addMissingGlyph

public void addMissingGlyph(char code)
Add a character to the font where the missing glyph will be displayed.

Parameters:
code - the character where there is no corresponding glyph.

defineFont

public DefineFont2 defineFont(int identifier,
                              List<Character> characters)
Create and return a DefineFont2 object that contains information to display a set of characters.

Parameters:
identifier - the unique identifier that will be used to reference the font definition in the flash file.
characters - the set of characters that the font must contain glyphs and layout information for,
Returns:
a font definition that contains information for all the glyphs in the set of characters.

canDisplay

public int canDisplay(String aString)
Tests whether the font can display all the characters in a string.

Parameters:
aString - the string to be displayed.
Returns:
the index of the character that cannot be displayed or -1 if all characters have corresponding glyphs.

canDisplay

public boolean canDisplay(char character)
Tests whether a character can be displayed by the font or whether the "missing" character glyph (usually an empty box) will be displayed instead.

Parameters:
character - the character to be displayed.
Returns:
true if the font contains a glyph for character or false if there is no corresponding glyph and the missing character glyph will be displayed.

glyphForCharacter

public int glyphForCharacter(char character)
Returns the glyph for the specified character.

Parameters:
character - the character.
Returns:
the Glyph object which contains the layout information.

characterForGlyph

public char characterForGlyph(int index)
Returns the characters code for the glyph at a specified index in the font. This method is used for extracting the strings displayed by static text (DefineText, DefineText2) fields.

Parameters:
index - the index of the font.
Returns:
the character code for the glyph.

advanceForCharacter

public int advanceForCharacter(char character)
Returns the default advance for the font as defined in the EM Square - conceptually a font with a point size of 1024. The number returned needs to be scaled to the correct size in order to calculate the advance to the next character.

Parameters:
character - the character code.
Returns:
the advance in twips to the next character.


Copyright © 2002-2010 Flagstone Software Ltd.. All Rights Reserved.