Documentation  › java.awt  › Font
 
 


  Font
  public

  Inherits From:   Object
  Conforms To:   Serializable
  Declared In:   java.awt


Class Description
 
The Font class represents fonts, which are used to render text in a visible way. A font provides the information needed to map sequences of characters to sequences of glyphs and to render sequences of glyphs on Graphics and Component objects.

Characters and Glyphs

A character is a symbol that represents an item such as a letter, a digit, or punctuation in an abstract way. For example, 'g', LATIN SMALL LETTER G, is a character.

A glyph is a shape used to render a character or a sequence of characters. In simple writing systems, such as Latin, typically one glyph represents one character. In general, however, characters and glyphs do not have one-to-one correspondence. For example, the character 'á' LATIN SMALL LETTER A WITH ACUTE, can be represented by two glyphs: one for 'a' and one for '´'. On the other hand, the two-character string "fi" can be represented by a single glyph, an "fi" ligature. In complex writing systems, such as Arabic or the South and South-East Asian writing systems, the relationship between characters and glyphs can be more complicated and involve context-dependent selection of glyphs as well as glyph reordering. A font encapsulates the collection of glyphs needed to render a selected set of characters as well as the tables needed to map sequences of characters to corresponding sequences of glyphs.

Physical and Logical Fonts

The Java 2 platform distinguishes between two kinds of fonts: physical fonts and logical fonts.

Physical fonts are the actual font libraries containing glyph data and tables to map from character sequences to glyph sequences, using a font technology such as TrueType or PostScript Type 1. All implementations of the Java 2 platform must support TrueType fonts; support for other font technologies is implementation dependent. Physical fonts may use names such as Helvetica, Palatino, HonMincho, or any number of other font names. Typically, each physical font supports only a limited set of writing systems, for example, only Latin characters or only Japanese and Basic Latin. The set of available physical fonts varies between configurations. Applications that require specific fonts can bundle them and instantiate them using the createFont method.

Logical fonts are the five font families defined by the Java platform which must be supported by any Java runtime environment: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries. Instead, the logical font names are mapped to physical fonts by the Java runtime environment. The mapping is implementation and usually locale dependent, so the look and the metrics provided by them vary. Typically, each logical font name maps to several physical fonts in order to cover a large range of characters.

Peered AWT components, such as Label and TextField, can only use logical fonts.

For a discussion of the relative advantages and disadvantages of using physical or logical fonts, see the Internationalization FAQ document.

Font Faces and Names

A Font can have many faces, such as heavy, medium, oblique, gothic and regular. All of these faces have similar typographic design.

There are three different names that you can get from a Font object. The logical font name is simply the name that was used to construct the font. The font face name, or just font name for short, is the name of a particular font face, like Helvetica Bold. The family name is the name of the font family that determines the typographic design across several faces, like Helvetica.

The Font class represents an instance of a font face from a collection of font faces that are present in the system resources of the host system. As examples, Arial Bold and Courier Bold Italic are font faces. There can be several Font objects associated with a font face, each differing in size, style, transform and font features. The getAllFonts method of the GraphicsEnvironment class returns an array of all font faces available in the system. These font faces are returned as Font objects with a size of 1, identity transform and default font features. These base fonts can then be used to derive new Font objects with varying sizes, styles, transforms and font features via the deriveFont methods in this class.



Class Variables
 
PLAIN
public static final int

The plain style constant.


BOLD
public static final int

The bold style constant. This can be combined with the other style constants (except PLAIN) for mixed styles.


ITALIC
public static final int

The italicized style constant. This can be combined with the other style constants (except PLAIN) for mixed styles.


ROMAN_BASELINE
public static final int

The baseline used in most Roman scripts when laying out text.


CENTER_BASELINE
public static final int

The baseline used in ideographic scripts like Chinese, Japanese, and Korean when laying out text.


HANGING_BASELINE
public static final int

The baseline used in Devanigiri and similar scripts when laying out text.


TRUETYPE_FONT
public static final int

Create a Font of type TRUETYPE. In future other types may be added to support other font types.


LAYOUT_LEFT_TO_RIGHT
public static final int

A flag to layoutGlyphVector indicating that text is left-to-right as determined by Bidi analysis.


LAYOUT_RIGHT_TO_LEFT
public static final int

A flag to layoutGlyphVector indicating that text is right-to-left as determined by Bidi analysis.


LAYOUT_NO_START_CONTEXT
public static final int

A flag to layoutGlyphVector indicating that text in the char array before the indicated start should not be examined.


LAYOUT_NO_LIMIT_CONTEXT
public static final int

A flag to layoutGlyphVector indicating that text in the char array after the indicated limit should not be examined.


Instance Variables
 
name
protected String

The logical name of this Font, as passed to the constructor.


style
protected int

The style of this Font, as passed to the constructor. This style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.


size
protected int

The point size of this Font, rounded to integer.


pointSize
protected float

The point size of this Font in float.


Constructors
 
Font
public Font( Map attributes )

Creates a new Font with the specified attributes. This Font only recognizes keys defined in TextAttribute as attributes. If attributes is null, a new Font is initialized with default attributes.


Font
public Font( String name, int style, int size )

Creates a new Font from the specified name, style and point size.


Class Methods
 
createFont
public static Font createFont( int fontFormat, InputStream fontStream ) throws FontFormatException, IOException

Returns a new Font with the specified font type and input data. The new Font is created with a point size of 1 and style PLAIN. This base font can then be used with the deriveFont methods in this class to derive new Font objects with varying sizes, styles, transforms and font features. This method does not close the InputStream.


decode
public static Font decode( String str )

Returns the Font that the str argument describes. To ensure that this method returns the desired Font, format the str parameter in one of two ways:

"fontfamilyname-style-pointsize" or
"fontfamilyname style pointsize"

in which style is one of the three case-insensitive strings: "BOLD", "BOLDITALIC", or "ITALIC", and pointsize is a decimal representation of the point size. For example, if you want a font that is Arial, bold, and a point size of 18, you would call this method with: "Arial-BOLD-18".

The default size is 12 and the default style is PLAIN. If you don't specify a valid size, the returned Font has a size of 12. If you don't specify a valid style, the returned Font has a style of PLAIN. If you do not provide a valid font family name in the str argument, this method still returns a valid font with a family name of "dialog". To determine what font family names are available on your system, use the getAvailableFontFamilyNames() method. If str is null, a new Font is returned with the family name "dialog", a size of 12 and a PLAIN style. If str is null, a new Font is returned with the name "dialog", a size of 12 and a PLAIN style.


getFont
public static Font getFont( Map attributes )

Returns a Font appropriate to this attribute set.


getFont
public static Font getFont( String nm )

Returns a Font object from the system properties list.


getFont
public static Font getFont( String nm, Font font )

Gets the specified Font from the system properties list. As in the getProperty method of System, the first argument is treated as the name of a system property to be obtained. The String value of this property is then interpreted as a Font object.

The property value should be one of the following forms:

  • fontname-style-pointsize
  • fontname-pointsize
  • fontname-style
  • fontname
where style is one of the three case-insensitive strings "BOLD", "BOLDITALIC", or "ITALIC", and point size is a decimal representation of the point size.

The default style is PLAIN. The default point size is 12.

If the specified property is not found, the font argument is returned instead.



Instance Methods
 
canDisplay
public boolean canDisplay( char c )

Checks if this Font has a glyph for the specified character.


canDisplayUpTo
public int canDisplayUpTo( char[] text, int start, int limit )

Indicates whether or not this Font can display the characters in the specified text starting at start and ending at limit. This method is a convenience overload.


canDisplayUpTo
public int canDisplayUpTo( CharacterIterator iter, int start, int limit )

Indicates whether or not this Font can display the specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code . If this Font can display all characters, -1 is returned.


canDisplayUpTo
public int canDisplayUpTo( String str )

Indicates whether or not this Font can display a specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned.


createGlyphVector
public GlyphVector createGlyphVector( FontRenderContext frc, char[] chars )

Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font. This method does no other processing besides the mapping of glyphs to characters. This means that this method is not useful for some scripts, such as Arabic, Hebrew, Thai, and Indic, that require reordering, shaping, or ligature substitution.


createGlyphVector
public GlyphVector createGlyphVector( FontRenderContext frc, CharacterIterator ci )

Creates a GlyphVector by mapping the specified characters to glyphs one-to-one based on the Unicode cmap in this Font. This method does no other processing besides the mapping of glyphs to characters. This means that this method is not useful for some scripts, such as Arabic, Hebrew, Thai, and Indic, that require reordering, shaping, or ligature substitution.


createGlyphVector
public GlyphVector createGlyphVector( FontRenderContext frc, int[] glyphCodes )

Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font. This method does no other processing besides the mapping of glyphs to characters. This means that this method is not useful for some scripts, such as Arabic, Hebrew, Thai, and Indic, that require reordering, shaping, or ligature substitution.


createGlyphVector
public GlyphVector createGlyphVector( FontRenderContext frc, String str )

Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font. This method does no other processing besides the mapping of glyphs to characters. This means that this method is not useful for some scripts, such as Arabic, Hebrew, Thai, and Indic, that require reordering, shaping, or ligature substitution.


deriveFont
public Font deriveFont( AffineTransform trans )

Creates a new Font object by replicating the current Font object and applying a new transform to it.


deriveFont
public Font deriveFont( float size )

Creates a new Font object by replicating the current Font object and applying a new size to it.


deriveFont
public Font deriveFont( int style )

Creates a new Font object by replicating the current Font object and applying a new style to it.


deriveFont
public Font deriveFont( int style, AffineTransform trans )

Creates a new Font object by replicating this Font object and applying a new style and transform.


deriveFont
public Font deriveFont( int style, float size )

Creates a new Font object by replicating this Font object and applying a new style and size.


deriveFont
public Font deriveFont( Map attributes )

Creates a new Font object by replicating the current Font object and applying a new set of font attributes to it.


equals
public boolean equals( Object obj )

Compares this Font object to the specified Object.


finalize
protected void finalize( ) throws Throwable

Disposes the native Font object.


getAttributes
public Map getAttributes( )

Returns a map of font attributes available in this Font. Attributes include things like ligatures and glyph substitution.


getAvailableAttributes
public AttributedCharacterIterator.Attribute[] getAvailableAttributes( )

Returns the keys of all the attributes supported by this Font. These attributes can be used to derive other fonts.


getBaselineFor
public byte getBaselineFor( char c )

Returns the baseline appropriate for displaying this character.

Large fonts can support different writing systems, and each system can use a different baseline. The character argument determines the writing system to use. Clients should not assume all characters use the same baseline.


getFamily
public String getFamily( )

Returns the family name of this Font.

The family name of a font is font specific. Two fonts such as Helvetica Italic and Helvetica Bold have the same family name, Helvetica, whereas their font face names are Helvetica Bold and Helvetica Italic. The list of available family names may be obtained by using the getAvailableFontFamilyNames() method.

Use getName to get the logical name of the font. Use getFontName to get the font face name of the font.


getFamily
public String getFamily( Locale l )

Returns the family name of this Font, localized for the specified locale.

The family name of a font is font specific. Two fonts such as Helvetica Italic and Helvetica Bold have the same family name, Helvetica, whereas their font face names are Helvetica Bold and Helvetica Italic. The list of available family names may be obtained by using the getAvailableFontFamilyNames() method.

Use getFontName to get the font face name of the font.


getFontName
public String getFontName( )

Returns the font face name of this Font. For example, Helvetica Bold could be returned as a font face name. Use getFamily to get the family name of the font. Use getName to get the logical name of the font.


getFontName
public String getFontName( Locale l )

Returns the font face name of the Font, localized for the specified locale. For example, Helvetica Fett could be returned as the font face name. Use getFamily to get the family name of the font.


getItalicAngle
public float getItalicAngle( )

Returns the italic angle of this Font. The italic angle is the inverse slope of the caret which best matches the posture of this Font.


getLineMetrics
public LineMetrics getLineMetrics( char[] chars, int beginIndex, int limit, FontRenderContext frc )

Returns a LineMetrics object created with the specified arguments.


getLineMetrics
public LineMetrics getLineMetrics( CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc )

Returns a LineMetrics object created with the specified arguments.


getLineMetrics
public LineMetrics getLineMetrics( String str, FontRenderContext frc )

Returns a LineMetrics object created with the specified String and FontRenderContext.


getLineMetrics
public LineMetrics getLineMetrics( String str, int beginIndex, int limit, FontRenderContext frc )

Returns a LineMetrics object created with the specified arguments.


getMaxCharBounds
public Rectangle2D getMaxCharBounds( FontRenderContext frc )

Returns the bounds for the character with the maximum bounds as defined in the specified FontRenderContext.


getMissingGlyphCode
public int getMissingGlyphCode( )

Returns the glyphCode which is used when this Font does not have a glyph for a specified unicode.


getName
public String getName( )

Returns the logical name of this Font. Use getFamily to get the family name of the font. Use getFontName to get the font face name of the font.


getNumGlyphs
public int getNumGlyphs( )

Returns the number of glyphs in this Font. Glyph codes for this Font range from 0 to getNumGlyphs() - 1.


getPeer
public FontPeer getPeer( )

Gets the peer of this Font.


getPSName
public String getPSName( )

Returns the postscript name of this Font. Use getFamily to get the family name of the font. Use getFontName to get the font face name of the font.


getSize
public int getSize( )

Returns the point size of this Font, rounded to an integer. Most users are familiar with the idea of using point size to specify the size of glyphs in a font. This point size defines a measurement between the baseline of one line to the baseline of the following line in a single spaced text document. The point size is based on typographic points, approximately 1/72 of an inch.

The Java(tm)2D API adopts the convention that one point is equivalent to one unit in user coordinates. When using a normalized transform for converting user space coordinates to device space coordinates 72 user space units equal 1 inch in device space. In this case one point is 1/72 of an inch.


getSize2D
public float getSize2D( )

Returns the point size of this Font in float value.


getStringBounds
public Rectangle2D getStringBounds( char[] chars, int beginIndex, int limit, FontRenderContext frc )

Returns the logical bounds of the specified array of characters in the specified FontRenderContext. The logical bounds contains the origin, ascent, advance, and height, which includes the leading. The logical bounds does not always enclose all the text. For example, in some languages and in some fonts, accent marks can be positioned above the ascent or below the descent. To obtain a visual bounding box, which encloses all the text, use the getBounds method of TextLayout.


getStringBounds
public Rectangle2D getStringBounds( CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc )

Returns the logical bounds of the characters indexed in the specified CharacterIterator in the specified FontRenderContext. The logical bounds contains the origin, ascent, advance, and height, which includes the leading. The logical bounds does not always enclose all the text. For example, in some languages and in some fonts, accent marks can be positioned above the ascent or below the descent. To obtain a visual bounding box, which encloses all the text, use the getBounds method of TextLayout.


getStringBounds
public Rectangle2D getStringBounds( String str, FontRenderContext frc )

Returns the logical bounds of the specified String in the specified FontRenderContext. The logical bounds contains the origin, ascent, advance, and height, which includes the leading. The logical bounds does not always enclose all the text. For example, in some languages and in some fonts, accent marks can be positioned above the ascent or below the descent. To obtain a visual bounding box, which encloses all the text, use the getBounds method of TextLayout.


getStringBounds
public Rectangle2D getStringBounds( String str, int beginIndex, int limit, FontRenderContext frc )

Returns the logical bounds of the specified String in the specified FontRenderContext. The logical bounds contains the origin, ascent, advance, and height, which includes the leading. The logical bounds does not always enclose all the text. For example, in some languages and in some fonts, accent marks can be positioned above the ascent or below the descent. To obtain a visual bounding box, which encloses all the text, use the getBounds method of TextLayout.


getStyle
public int getStyle( )

Returns the style of this Font. The style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.


getTransform
public AffineTransform getTransform( )

Returns a copy of the transform associated with this Font.


hashCode
public int hashCode( )

Returns a hashcode for this Font.


hasUniformLineMetrics
public boolean hasUniformLineMetrics( )

Checks whether or not this Font has uniform line metrics. A logical Font might be a composite font, which means that it is composed of different physical fonts to cover different code ranges. Each of these fonts might have different LineMetrics. If the logical Font is a single font then the metrics would be uniform.


isBold
public boolean isBold( )

Indicates whether or not this Font object's style is BOLD.


isItalic
public boolean isItalic( )

Indicates whether or not this Font object's style is ITALIC.


isPlain
public boolean isPlain( )

Indicates whether or not this Font object's style is PLAIN.


isTransformed
public boolean isTransformed( )

Indicates whether or not this Font object has a transform that affects its size in addition to the Size attribute.


layoutGlyphVector
public GlyphVector layoutGlyphVector( FontRenderContext frc, char[] text, int start, int limit, int flags )

Returns a new GlyphVector object, performing full layout of the text if possible. Full layout is required for complex text, such as Arabic or Hindi. Support for different scripts depends on the font and implementation.

Bidi, and should only be performed on text that has a uniform direction. The direction is indicated in the flags parameter,by using LAYOUT_RIGHT_TO_LEFT to indicate a right-to-left (Arabic and Hebrew) run direction, or LAYOUT_LEFT_TO_RIGHT to indicate a left-to-right (English) run direction.

In addition, some operations, such as Arabic shaping, require context, so that the characters at the start and limit can have the proper shapes. Sometimes the data in the buffer outside the provided range does not have valid data. The values LAYOUT_NO_START_CONTEXT and LAYOUT_NO_LIMIT_CONTEXT can be added to the flags parameter to indicate that the text before start, or after limit, respectively, should not be examined for context.

All other values for the flags parameter are reserved.


toString
public String toString( )

Converts this Font object to a String representation.



Known Subclasses
 
FontUIResource



 
 
  dydoc
  3/10/05