Java Platform 1.2
Beta 4

Class java.awt.Font

java.lang.Object
  |
  +--java.awt.Font
Direct Known Subclasses:
FontUIResource

public class Font
extends Object
implements Serializable
This class represents fonts. The capabilities of this class have been extended over the java.awt.Font class in JDK 1.1 and earlier releases to provide developers the ability to utilize more sophisticated typographic features.

It is important to present the concepts behind using the words character and glyph separately. A character is a symbol that represents items like letters and numbers in a given writing system, for example lowercase-g. When a given character is drawn, a shape now represents this character. This shape is called a glyph.

Chararcter encoding is a conversion table that maps character codes to glyph codes in the font. The character encoding used in the Java 2D API is Unicode. For more information on Unicode you may visit the site http://www.unicode.org.

Characters and glyphs do not have one-to-one correspondence. As an example lowercase-a acute can be represented by two glyphs: lowercase-a and acute. Another example is ligatures such as ligature -fi which is a single glyph representing two characters: f and i.

A font is a collection of glyphs. A font may have many faces, e.g. 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 nameis the same as used by java.awt.Font in JDK 1.1 and earlier releases. The java.awt.Toolkit.getFontList() method returns a short list of these logical names which are mapped onto specific fonts available on specific platforms. 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 face name is the one that should be used to specify fonts. This name signifies actual fonts in the host system, and does not identify font names with shape of font characters as the logical font name does.

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, Helvetica 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. GraphicsEnvironment.getAllFonts() 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.

See Also:
Toolkit.getFontList(), GraphicsEnvironment.getFonts(java.util.Map), GraphicsEnvironment.getAllFonts(), Serialized Form

Field Summary
static int BOLD
          The bold style constant.
static int CENTER_BASELINE
           
static int HANGING_BASELINE
           
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  float pointSize
          The point size of this font in float.
static int ROMAN_BASELINE
           
protected  int size
          The point size of this font, rounded to integer.
protected  int style
          The style of the font, as passed to the constructor.
 
Constructor Summary
Font(Map attributes)
          Create 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
 boolean canDisplay(char c)
          Checks if this font has a glyph for the specified character.
 int canDisplayUpTo(char[] text, int start, int limit)
          A convenience overload.
 int canDisplayUpTo(CharacterIterator iter, int start, int limit)
          Indicates whether a string is displayable by this Font.
 int canDisplayUpTo(String str)
          Indicates whether a string is displayable by this Font.
 GlyphVector createGlyphVector(FontRenderContext frc, char[] chars)
           
 GlyphVector createGlyphVector(FontRenderContext frc, CharacterIterator ci)
           
 GlyphVector createGlyphVector(FontRenderContext frc, int[] glyphCodes)
           
 GlyphVector createGlyphVector(FontRenderContext frc, String str)
           
static Font decode(String str)
          Returns the specified font using the name passed in.
 Font deriveFont(AffineTransform trans)
          Creates a new Font object by replicating the current Font object with a new transform associated with it.
 Font deriveFont(float size)
          Creates a new Font object by replicating the current Font object with a new size associated with it.
 Font deriveFont(int style, AffineTransform trans)
          Creates a new Font object by replicating the current Font object with a new style and transform associated with it.
 Font deriveFont(int style, float size)
          Creates a new Font object by replicating the current Font object with a new style and size associated with it.
 Font deriveFont(int style)
          Creates a new Font object by replicating the current Font object with a new style associated with it.
 Font deriveFont(Map attributes)
          Creates a new Font object by replicating the current Font object with a new set of font attributes associated with it.
 boolean equals(Object obj)
          Compares this object to the specified object.
protected  void finalize()
           
 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.
 byte getBaselineFor(char c)
          Return the baseline appropriate for displaying this character.
 String getFamily()
          Returns the family name of the font (for example, Helvetica).
 String getFamily(Locale l)
          Returns the family name of the font (for example, Helvetica), localized for the given Locale.
static Font getFont(Map attributes)
          Returns a font appropriate to this attribute set.
static Font getFont(String nm, Font font)
          Returns the specified font from the system properties list.
static Font getFont(String nm)
          Returns a font from the system properties list.
 String getFontName()
          Returns the font face name of the font (for example, Helvetica Bold).
 String getFontName(Locale l)
          Returns the font face name of the font (for example, Helvetica Fett) localized for the specified locale.
 float getItalicAngle()
          Returns the italic angle of this font.
 LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc)
           
 LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)
           
 LineMetrics getLineMetrics(String str, FontRenderContext frc)
           
 LineMetrics getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc)
           
 Rectangle2D getMaxCharBounds(FontRenderContext frc)
           
 int getMissingGlyphCode()
          Returns the glyphCode which is used when the font does not have a glyph for a specified unicode
 String getName()
          Returns the logical name of the font.
 int getNumGlyphs()
          Returns number of glyphs in the font.
 java.awt.peer.FontPeer getPeer()
          Deprecated. Font rendering is now platform independent.
 String getPSName()
          Returns the postscript name of the font.
 int getSize()
          Returns the point size of the font, rounded to integer.
 float getSize2D()
          Returns the point size of the font in float.
 Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc)
           
 Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)
           
 Rectangle2D getStringBounds(String str, FontRenderContext frc)
           
 Rectangle2D getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc)
           
 int getStyle()
          Returns the style of the font.
 AffineTransform getTransform()
          Returns a copy of the transform associated with this font.
 int hashCode()
          Returns a hashcode for this font.
 boolean hasUniformLineMetrics()
           
 boolean isBold()
          Indicates whether the font's style is bold.
 boolean isItalic()
          Indicates whether the font's style is italic.
 boolean isPlain()
          Indicates whether the font's style is plain.
 String toString()
          Converts this object to a String representation.
 
Methods inherited from class java.lang.Object
clone , getClass , notify , notifyAll , wait , wait , wait
 

Field Detail

PLAIN

public static final int PLAIN
The plain style constant.

BOLD

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

ITALIC

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

ROMAN_BASELINE

public static final int ROMAN_BASELINE

CENTER_BASELINE

public static final int CENTER_BASELINE

HANGING_BASELINE

public static final int HANGING_BASELINE

name

protected String name
The logical name of this font, as passed to the constructor.
Since:
JDK1.0

style

protected int style
The style of the font, as passed to the constructor. This may be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
Since:
JDK1.0

size

protected int size
The point size of this font, rounded to integer.
Since:
JDK1.0

pointSize

protected float pointSize
The point size of this font in float.
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 one of Dialog, DialogInput, Monospaced, Serif, SansSerif, or Symbol.
style - the style constant for the font.
size - the point size of the font.
Since:
JDK1.0
See Also:
Toolkit.getFontList(), GraphicsEnvironment.getFonts(java.util.Map), GraphicsEnvironment.getAllFonts(), GraphicsEnvironment.getAvailableFontFamilyNames()

Font

public Font(Map attributes)
Create a new font with the specified attributes
Method Detail

getPeer

public java.awt.peer.FontPeer getPeer()
Deprecated. Font rendering is now platform independent.
Gets the peer of the font.
Returns:
the peer of the font.
Since:
JDK1.1

getFont

public static Font getFont(Map attributes)
Returns a font appropriate to this attribute set.
Since:
JDK1.2

getTransform

public AffineTransform getTransform()
Returns a copy of the transform associated with this font.

getFamily

public String getFamily()
Returns the family name of the font (for example, Helvetica). Use getName to get the logical name of the font. Use getFontName to get the font face name of the font.
Since:
JDK1.2
See Also:
getName(), getFontName()

getFamily

public String getFamily(Locale l)
Returns the family name of the font (for example, Helvetica), localized for the given Locale. Use getFontName to get the font face name of the font.
Parameters:
l - Locale for which to get the family name.
Since:
JDK1.2
See Also:
getFontName(), Locale

getPSName

public String getPSName()
Returns the postscript name of the font. Use getFamily to get the family name of the font. Use getFontName to get the font face name of the font.
Since:
JDK1.2

getName

public String getName()
Returns the logical name of the font. Use getFamily to get the family name of the font. Use getFontName to get the font face name of the font.
Since:
JDK1.0
See Also:
getFamily(), getFontName()

getFontName

public String getFontName()
Returns the font face name of the font (for example, Helvetica Bold). Use getFamily to get the family name of the font. Use getName to get the logical name of the font.
Since:
JDK1.2
See Also:
getFamily(), getName()

getFontName

public String getFontName(Locale l)
Returns the font face name of the font (for example, Helvetica Fett) localized for the specified locale. Use getFamily to get the family name of the font.
Parameters:
l - Get the localized font face name for this locale.
See Also:
getFamily(), Locale

getStyle

public int getStyle()
Returns the style of the font. This may be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
Since:
JDK1.0
See Also:
isPlain(), isBold(), isItalic()

getSize

public int getSize()
Returns the point size of the font, rounded to 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 Java2D 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 (see GraphicsConfiguration.getDefaultTransform() and GraphicsConfiguration.getNormalizingTransform()), 72 user space units equal 1 inch in device space. In this case one point is 1/72 of an inch.

Since:
JDK1.0
See Also:
getSize2D(), GraphicsConfiguration.getDefaultTransform(), GraphicsConfiguration.getNormalizingTransform()

getSize2D

public float getSize2D()
Returns the point size of the font in float.
Since:
JDK1.2
See Also:
getSize()

isPlain

public boolean isPlain()
Indicates whether the font's style is plain.
Returns:
true if the font is plain; false otherwise.
Since:
JDK1.0
See Also:
getStyle()

isBold

public boolean isBold()
Indicates whether the font's style is bold.
Returns:
true if the font is bold; false otherwise.
Since:
JDK1.0
See Also:
getStyle()

isItalic

public boolean isItalic()
Indicates whether the font's style is italic.
Returns:
true if the font is italic; false otherwise.
Since:
JDK1.0
See Also:
getStyle()

getFont

public static Font getFont(String nm)
Returns a font from the system properties list.
Parameters:
nm - the property name.
Since:
JDK1.2

decode

public static Font decode(String str)
Returns the specified font using the name passed in.
Parameters:
str - the name.
Since:
JDK1.1

getFont

public static Font getFont(String nm,
                           Font font)
Returns the specified font from the system properties list.
Parameters:
nm - the property name.
font - a default font to return if property 'nm' is not defined.

hashCode

public int hashCode()
Returns a hashcode for this font.
Returns:
a hashcode value for this font.
Overrides:
hashCode in class Object
Since:
JDK1.0

equals

public boolean equals(Object obj)
Compares this object to the specified object.
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
Overrides:
equals in class Object
Since:
JDK1.0

toString

public String toString()
Converts this object to a String representation.
Returns:
a string representation of this object
Overrides:
toString in class Object
Since:
JDK1.0

getNumGlyphs

public int getNumGlyphs()
Returns number of glyphs in the font. Glyph codes for the font will range from 0 to getNumGlyphs() - 1.
Since:
JDK1.2

getMissingGlyphCode

public int getMissingGlyphCode()
Returns the glyphCode which is used when the font does not have a glyph for a specified unicode
Since:
JDK1.2

getBaselineFor

public byte getBaselineFor(char c)
Return 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 will use the same baseline.

Parameters:
c - a character used to identify the writing system
Since:
JDK1.2
See Also:
#getBaselineOffsetsFor, ROMAN_BASELINE, CENTER_BASELINE, HANGING_BASELINE

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

getAvailableAttributes

public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
Returns the keys of all the attributes supported by this font. These attributes may be used to derive other fonts.
Since:
JDK1.2

deriveFont

public Font deriveFont(int style,
                       float size)
Creates a new Font object by replicating the current Font object with a new style and size associated with it.
Parameters:
style - the style for the new Font.
size - the size in float for the new Font.
Returns:
a new Font object.
Since:
JDK1.2

deriveFont

public Font deriveFont(int style,
                       AffineTransform trans)
Creates a new Font object by replicating the current Font object with a new style and transform associated with it.
Parameters:
style - the style for the new Font.
trans - the AffineTransform associated with the new Font.
Returns:
a new Font object.
Since:
JDK1.2

deriveFont

public Font deriveFont(float size)
Creates a new Font object by replicating the current Font object with a new size associated with it.
Parameters:
size - the size in float for the new Font.
Returns:
a new Font object.
Since:
JDK1.2

deriveFont

public Font deriveFont(AffineTransform trans)
Creates a new Font object by replicating the current Font object with a new transform associated with it.
Parameters:
trans - the AffineTransform associated with the new Font.
Returns:
a new Font object.
Since:
JDK1.2

deriveFont

public Font deriveFont(int style)
Creates a new Font object by replicating the current Font object with a new style associated with it.
Parameters:
style - the style for the new Font.
Returns:
a new Font object.
Since:
JDK1.2

deriveFont

public Font deriveFont(Map attributes)
Creates a new Font object by replicating the current Font object with a new set of font attributes associated with it.
Parameters:
attributes - a map of attributes enabled for the new Font.
Returns:
a new Font object.
Since:
JDK1.2

canDisplay

public boolean canDisplay(char c)
Checks if this font has a glyph for the specified character.
Parameters:
c - a unicode character code.
Returns:
true if the font can display the character.
Since:
JDK1.2

canDisplayUpTo

public int canDisplayUpTo(String str)
Indicates whether a string is displayable by this Font. For strings with Unicode encoding, it is important to know if a given Font can display the string. This method returns an offset into the String str which is the first character the Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned.
Parameters:
str - a String object.
Returns:
an offset into the String object that can be displayed by this font.
Since:
JDK1.2

canDisplayUpTo

public int canDisplayUpTo(char[] text,
                          int start,
                          int limit)
A convenience overload.
Since:
JDK1.2

canDisplayUpTo

public int canDisplayUpTo(CharacterIterator iter,
                          int start,
                          int limit)
Indicates whether a string is displayable by this Font. For strings with Unicode encoding, it is important to know if a given Font can display the string. This method returns an offset into the String str which is the first character the Font cannot display without using the missing glyph code . If the Font can display all characters, -1 is returned.
Parameters:
text - a CharacterIterator object.
Returns:
an offset into the String object that can be displayed by this font.
Since:
JDK1.2

getItalicAngle

public float getItalicAngle()
Returns the italic angle of this font.

hasUniformLineMetrics

public boolean hasUniformLineMetrics()

getLineMetrics

public LineMetrics getLineMetrics(String str,
                                  FontRenderContext frc)

getLineMetrics

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

getLineMetrics

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

getLineMetrics

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

getStringBounds

public Rectangle2D getStringBounds(String str,
                                   FontRenderContext frc)

getStringBounds

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

getStringBounds

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

getStringBounds

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

getMaxCharBounds

public Rectangle2D getMaxCharBounds(FontRenderContext frc)

createGlyphVector

public GlyphVector createGlyphVector(FontRenderContext frc,
                                     String str)

createGlyphVector

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

createGlyphVector

public GlyphVector createGlyphVector(FontRenderContext frc,
                                     CharacterIterator ci)

createGlyphVector

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

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object

Java Platform 1.2
Beta 4

Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.