Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.

JSR 216 (Maintenance Release)

java.awt
Class Font

java.lang.Object
  extended byjava.awt.Font
All Implemented Interfaces:
Serializable

public class Font
extends Object
implements Serializable

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. 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.

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 and font features.

See Also:
Serialized Form

Field Summary
static int BOLD
          The bold style constant.
static int ITALIC
          The italicized style constant.
protected  String name
          The logical name of this Font, as passed to the constructor.
static int PLAIN
          The plain style constant.
protected  int size
          The point size of this Font, rounded to integer.
protected  int style
          The style of this Font, as passed to the constructor.
 
Constructor Summary
Font(Map attributes)
          Creates a new Font with the specified attributes.
Font(String name, int style, int size)
          Creates a new Font from the specified name, style and point size.
 
Method Summary
static Font decode(String str)
          Returns the Font that the str argument describes.
 boolean equals(Object obj)
          Compares this Font object to the specified Object.
 Map getAttributes()
          Returns a map of font attributes available in this Font.
 AttributedCharacterIterator.Attribute[] getAvailableAttributes()
          Returns the keys of all the attributes supported by this Font.
 String getFamily()
          Returns the family name of this Font.
static Font getFont(Map attributes)
          Returns a Font appropriate to this attribute set.
static Font getFont(String nm)
          Returns a Font object from the system properties list.
static Font getFont(String nm, Font font)
          Gets the specified Font from the system properties list.
 String getName()
          Returns the logical name of this Font.
 int getSize()
          Returns the point size of this Font, rounded to an integer.
 int getStyle()
          Returns the style of this Font.
 int hashCode()
          Returns a hashcode for this Font.
 boolean isBold()
          Indicates whether or not this Font object's style is BOLD.
 boolean isItalic()
          Indicates whether or not this Font object's style is ITALIC.
 boolean isPlain()
          Indicates whether or not this Font object's style is PLAIN.
 String toString()
          Converts this Font object to a String representation.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

PLAIN

public static final int PLAIN
The plain style constant.

See Also:
Constant Field Values

BOLD

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

See Also:
Constant Field Values

ITALIC

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

See Also:
Constant Field Values

name

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

Since:
JDK1.0
See Also:
getName()

style

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

Since:
JDK1.0
See Also:
getStyle()

size

protected int size
The point size of this Font, rounded to integer.

Since:
JDK1.0
See Also:
getSize()
Constructor Detail

Font

public Font(String name,
            int style,
            int size)
Creates a new Font from the specified name, style and point size.

Parameters:
name - the font name. This can be a logical font name or a font face name. A logical name must be either: Dialog, DialogInput, Monospaced, Serif, or SansSerif. If name is null, the name of the new Font is set to the name "Default".
style - the style constant for the Font The style argument is an integer bitmask that may be PLAIN, or a bitwise union of BOLD and/or ITALIC (for example, ITALIC or BOLD|ITALIC). If the style argument does not conform to one of the expected integer bitmasks then the style is set to PLAIN.
size - the point size of the Font
Since:
JDK1.0
See Also:
GraphicsEnvironment.getAvailableFontFamilyNames()

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.

Parameters:
attributes - the attributes to assign to the new Font, or null
Method Detail

getFont

public static Font getFont(Map attributes)
Returns a Font appropriate to this attribute set.

Parameters:
attributes - the attributes to assign to the new Font
Returns:
a new Font created with the specified attributes
Since:
1.2
See Also:
TextAttribute

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 GraphicsEnvironment.getAvailableFontFamilyNames() method.

Use getName to get the logical name of the font.

Returns:
a String that is the family name of this Font.
Since:
JDK1.1
See Also:
getName()

getName

public String getName()
Returns the logical name of this Font. Use getFamily to get the family name of the font.

Returns:
a String representing the logical name of this Font.
Since:
JDK1.0
See Also:
getFamily()

getStyle

public int getStyle()
Returns the style of this Font. The style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.

Returns:
the style of this Font
Since:
JDK1.0
See Also:
isPlain(), isBold(), isItalic()

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. In this case one point is 1/72 of an inch.

Returns:
the point size of this Font in 1/72 of an inch units.
Since:
JDK1.0

isPlain

public boolean isPlain()
Indicates whether or not this Font object's style is PLAIN.

Returns:
true if this Font has a PLAIN sytle; false otherwise.
Since:
JDK1.0
See Also:
getStyle()

isBold

public boolean isBold()
Indicates whether or not this Font object's style is BOLD.

Returns:
true if this Font object's style is BOLD; false otherwise.
Since:
JDK1.0
See Also:
getStyle()

isItalic

public boolean isItalic()
Indicates whether or not this Font object's style is ITALIC.

Returns:
true if this Font object's style is ITALIC; false otherwise.
Since:
JDK1.0
See Also:
getStyle()

getFont

public static Font getFont(String nm)
Returns a Font object from the system properties list.

Parameters:
nm - the property name
Returns:
a Font object that the property name describes.
Since:
1.2

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 GraphicsEnvironment.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.

Parameters:
str - the name of the font, or null
Returns:
the Font object that str describes, or a new default Font if str is null.
Since:
JDK1.1
See Also:
getFamily()

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:

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.

Parameters:
nm - the case-insensitive property name
font - a default Font to return if property nm is not defined
Returns:
the Font value of the property.
See Also:
decode(String)

hashCode

public int hashCode()
Returns a hashcode for this Font.

Overrides:
hashCode in class Object
Returns:
a hashcode value for this Font.
Since:
JDK1.0
See Also:
Object.equals(java.lang.Object), Hashtable

equals

public boolean equals(Object obj)
Compares this Font object to the specified Object.

Overrides:
equals in class Object
Parameters:
obj - the Object to compare
Returns:
true if the objects are the same or if the argument is a Font object describing the same font as this object; false otherwise.
Since:
JDK1.0
See Also:
Object.hashCode(), Hashtable

toString

public String toString()
Converts this Font object to a String representation.

Overrides:
toString in class Object
Returns:
a String representation of this Font object.
Since:
JDK1.0

getAttributes

public Map getAttributes()
Returns a map of font attributes available in this Font. Attributes include things like ligatures and glyph substitution.

Returns:
the attributes map of this Font.

getAvailableAttributes

public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
Returns the keys of all the attributes supported by this Font.

Returns:
an array containing the keys of all the attributes supported by this Font.
Since:
1.2

JSR 216 (Maintenance Release)

Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

For more information, please consult the JSR 216 specification.