Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Class java.awt.Font

java.lang.Object
    |
    +----java.awt.Font

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:
getFontList, getFonts, getAllFonts

Field Summary
static int  BOLD
The bold style constant.
static byte  CENTER_BASELINE
 
static Font  DEFAULT
 
String  fontName
 
static byte  HANGING_BASELINE
 
static int  ITALIC
The italicized style constant.
String  name
The logical name of this font.
static int  PLAIN
The plain style constant.
static byte  ROMAN_BASELINE
 
int  size
The point size of this font, rounded to integer.
int  style
The style of the font, as passed to the constructor.
 

Constructor Summary
 Font(String name, int style, int size)
Creates a new font from the specified name, style and point size.
 Font(sun.awt.font.FontObject fontObject)
 
 Font(AttributeSet attributes)
Create a new font with the specified attributes
 

Method Summary
boolean  canDisplay(char c)
Checks if this font has a glyph for the specified character.
int  canDisplayUpTo(String str)
Indicates whether a string is displayable by this Font.
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.
static Font  decode(String str)
Returns the specified font using the name passed in.
Font  deriveFont(int style, float size)
Creates a new Font object by replicating the current Font object with a new style, size and font attributes associated with it.
Font  deriveFont(int style, AffineTransform trans)
Creates a new Font object by replicating the current Font object with a new style, transform and font attributes 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(AffineTransform trans)
Creates a new Font object by replicating the current Font object with a new transform 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(TextAttributeSet 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.
void  finalize()
 
float  getAdvance(char c)
 
float  getAdvance(char[] text)
 
float  getAscent()
Returns the ascent of the font above the roman baseline.
AttributeSet  getAttributes()
Returns an array of font attributes available in this font.
String[]  getAvailableAttributes()
Returns the names of all the attributes supported by this font.
byte  getBaselineFor(char c)
Return the baseline appropriate for displaying this character.
float  getBaselineOffset(byte baselineType)
Returns the offset from the roman baseline of the font to the specified baseline.
float[]  getBaselineOffsetsFor(char c)
Return a list of relative offsets for the different baselines.
static Font  getBestFontFor(AttributedCharacterIterator text)
Resolve styles on the character at start into an instance of Font that can best render the text between start and limit.
static Font  getBestFontFor(AttributedCharacterIterator text, int start, int limit)
Resolve styles on the character at start into an instance of Font that can best render the text between start and limit.
float  getDescent()
Returns the descent of the font above the roman baseline.
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(AttributeSet attributes)
Returns a font appropriate to this attribute set.
static Font  getFont(String nm)
Returns a font from the system properties list.
static Font  getFont(String nm, Font font)
Returns the specified font from the system properties list.
int  getFontID()
 
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.
GlyphJustificationInfo  getGlyphJustificationInfo(int glyphCode)
Return justification info for the glyph specified by glyphCode.
GlyphMetrics  getGlyphMetrics(int glyphCode)
Returns the metrics information for a glyph specified by a glyph code.
Shape  getGlyphOutline(int glyphCode, float x, float y)
Returns the outline description of a glyph specified by a glyph code.
GlyphSet  getGlyphSet(CharacterIterator context, int start, int limit, byte baseline, int[] order, byte[] levels)
Create a glyph set for the text between start and limit.
GlyphSet  getGlyphSet(char[] context, int start, int limit, byte baseline, int[] order, byte[] levels)
Construct a GlyphSet for the text between start and limit.
GlyphSet  getGlyphSet(String str)
Converts all characters in the String Object to font glyph codes.
float  getItalicAngle()
Returns the italic angle of this font.
float  getLeading()
Returns the leading for this font;
float  getMaxAdvance()
Returns the maximum advance of any glyph in this font.
Rectangle2D  getMaxBounds2D()
Returns the maximum bounding box of this font.
float[]  getMetrics(GlyphSet glyphs, AffineTransform tx)
 
int  getMissingGlyphCode()
 
String  getName()
Returns the logical name of the font.
int  getNumGlyphs()
Returns number of glyphs in the font.
Shape  getOutline(GlyphSet glyphs, AffineTransform tx, float x, float y)
 
java.awt.peer.FontPeer  getPeer()
Gets the peer of the font.
String  getPSName()
Returns the postscript name of the font.
AttributeSet  getRequestedAttributes()
Returns the attribute set used to create this font.
int  getSize()
Returns the point size of the font, rounded to integer.
float  getSize2D()
Returns the point size of the font in float.
float  getStrikethroughOffsetFor(char c)
Return the strikethrough offset from the roman baseline for this character.
float  getStrikethroughThicknessFor(char c)
Return the strikethrough thickness for this character.
int  getStyle()
Returns the style of the font.
AffineTransform  getTransform()
Returns the transform associated with this font.
float  getUnderlineOffsetFor(char c)
Return the underline offset from the roman baseline for this character.
float  getUnderlineThicknessFor(char c)
Return the underline thickness for this character.
int  hashCode()
Returns a hashcode for this font.
boolean  isBold()
Indicates whether the font's style is bold.
boolean  isItalic()
Indicates whether the font's style is italic.
boolean  isPlain()
Returns true if the font is plain.
boolean  isUniformBaseline()
Return true if this font places all glyphs on a single baseline.
boolean  isVerticalBaseline()
 
int  sameBaselineUpTo(CharacterIterator iter)
Return the index of the first character with a different baseline from the first character in the range, or end index if all characters in the range have the same baseline.
int  sameBaselineUpTo(char[] text, int start, int limit)
Return the index of the first character with a different baseline from the character at start, or limit if all characters between start and limit have the same baseline.
String  toString()
Converts this object to a String representation.
 
Methods inherited from class java.lang.Object
 clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT

public static final Font DEFAULT

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 byte ROMAN_BASELINE

CENTER_BASELINE

public static final byte CENTER_BASELINE

HANGING_BASELINE

public static final byte HANGING_BASELINE

name

protected String name
The logical name of this font.

fontName

protected String fontName

style

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

size

protected int size
The point size of this font, rounded to integer.
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.
style - the style constant for the font.
size - the point size of the font.
See Also:
getFontList, getFonts, getAllFonts

Font

public Font(sun.awt.font.FontObject fontObject)

Font

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

getPeer

public java.awt.peer.FontPeer getPeer()
Gets the peer of the font.
Returns:
the peer of the font.

getFontID

public int getFontID()

getFont

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

getTransform

public AffineTransform getTransform()
Returns 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.
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.
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.

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

See Also:
getSize2D, getDefaultTransform, getNormalizingTransform

getSize2D

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

isPlain

public boolean isPlain()
Returns true if the font is plain.
See Also:
getStyle

isBold

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

isItalic

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

getFont

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

decode

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

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

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

toString

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

getNumGlyphs

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

getMissingGlyphCode

public int getMissingGlyphCode()

isVerticalBaseline

public boolean isVerticalBaseline()

getGlyphMetrics

public GlyphMetrics getGlyphMetrics(int glyphCode)
Returns the metrics information for a glyph specified by a glyph code.
Parameters:
glyphCode - the glyph to get metrics for.
Returns:
a GlyphMetrics object with metric information for the glyph.

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
See Also:
getBaselineOffsetsFor, ROMAN_BASELINE, CENTER_BASELINE, HANGING_BASELINE

getBaselineOffsetsFor

public float[] getBaselineOffsetsFor(char c)
Return a list of relative offsets for the different baselines.

Large fonts can support different writing systems, and each system can have its own set of preferred baseline offsets. The character argument determines the writing system to use.

These all are relative to the most common baseline used with this font (the one from which ascent and descent are measured). Negative values are up on horizontal lines, and right on vertical lines.

Parameters:
c - a character used to identify the writing system
See Also:
getBaselineFor, ROMAN_BASELINE, CENTER_BASELINE, HANGING_BASELINE

isUniformBaseline

public boolean isUniformBaseline()
Return true if this font places all glyphs on a single baseline.

sameBaselineUpTo

public int sameBaselineUpTo(CharacterIterator iter)
Return the index of the first character with a different baseline from the first character in the range, or end index if all characters in the range have the same baseline.

sameBaselineUpTo

public int sameBaselineUpTo(char[] text,
                            int start,
                            int limit)
Return the index of the first character with a different baseline from the character at start, or limit if all characters between start and limit have the same baseline.

getUnderlineOffsetFor

public float getUnderlineOffsetFor(char c)
Return the underline offset from the roman baseline for this character.

getUnderlineThicknessFor

public float getUnderlineThicknessFor(char c)
Return the underline thickness for this character. Zero means the character is not underlined.

getStrikethroughOffsetFor

public float getStrikethroughOffsetFor(char c)
Return the strikethrough offset from the roman baseline for this character.

getStrikethroughThicknessFor

public float getStrikethroughThicknessFor(char c)
Return the strikethrough thickness for this character. Zero means the character is not underlined.

getGlyphOutline

public Shape getGlyphOutline(int glyphCode,
                             float x,
                             float y)
Returns the outline description of a glyph specified by a glyph code.
Parameters:
glyphCode - the glyph for which to get the outline.
x - the x coordinate for the glyph's position.
y - the y coordinate for the glyph's position, indicating the font baseline for the glyph.
Returns:
a Shape object representing the outline of the glyph.

getOutline

public Shape getOutline(GlyphSet glyphs,
                        AffineTransform tx,
                        float x,
                        float y)

getMetrics

public float[] getMetrics(GlyphSet glyphs,
                          AffineTransform tx)

getAdvance

public float getAdvance(char c)

getAdvance

public float getAdvance(char[] text)

getAttributes

public AttributeSet getAttributes()
Returns an array of font attributes available in this font. Attributes include things like ligatures and glyph substitution.
Returns:
a FontAttribute array.

getAvailableAttributes

public String[] getAvailableAttributes()
Returns the names of all the attributes supported by this font. These attributes may be used to derive other fonts.

getGlyphJustificationInfo

public GlyphJustificationInfo getGlyphJustificationInfo(int glyphCode)
Return justification info for the glyph specified by glyphCode. If left is true, return info for the left side of the glyph, otherwise for the right. If grow is true, return info for growing a line, otherwise for shrinking it. !!! unify into one set of information per glyph? Then it's the accessing code's problem...

getRequestedAttributes

public AttributeSet getRequestedAttributes()
Returns the attribute set used to create this font. NOTE: This method may be moved into another class.

deriveFont

public Font deriveFont(int style,
                       float size)
Creates a new Font object by replicating the current Font object with a new style, size and font attributes associated with it.
Parameters:
style - the style for the new Font.
size - the size in float for the new Font.
attributes - an array of AttributeSets enabled for the new Font.
Returns:
a new Font object.

deriveFont

public Font deriveFont(int style,
                       AffineTransform trans)
Creates a new Font object by replicating the current Font object with a new style, transform and font attributes associated with it.
Parameters:
style - the style for the new Font.
trans - the AffineTransform associated with the new Font.
attributes - an array of AttributeSets enabled for the new Font.
Returns:
a new Font object.

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.

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.

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.

deriveFont

public Font deriveFont(TextAttributeSet attributes)
Creates a new Font object by replicating the current Font object with a new set of font attributes associated with it.
Parameters:
attributes - an array of AttributeSets enabled for the new Font.
Returns:
a new Font object.

getGlyphSet

public GlyphSet getGlyphSet(CharacterIterator context,
                            int start,
                            int limit,
                            byte baseline,
                            int[] order,
                            byte[] levels)
Create a glyph set for the text between start and limit.
Parameters:
context - the text for the entire line. The whole context is provided for fonts that need to do glyph shaping.
start - the start of the subrange for which to create glyphs.
limit - the limit of the subrange for which to create glyphs.
order - a mapping from logical to visual positions for bidirectional text. For example, the value 5 at position 0 in the array means that the character at logical position 0 (from the start of the text in the iterator) displays at visual position 5 (assuming all characters are displayed).

If order is null, the visual position is the logical position. Position zero in this array is position startIndex in the context. The array length must be at least the number of characters in the iterator.

Note the meaning of this array is the inverse of the mapping passed to the GlyphSet constructor. This is to faciliate indexing into the array using start and limit.

levels - an array indicating the bidirection level of each character. Even values are left to right, odd are right to left. This will affect hit testing and cursor positioning. If null, all characters are left to right. Position zero in this array is position beginIndex in the context, and the array length is greater than or equal to endIndex - beginIndex.
See Also:
GlyphSet

getGlyphSet

public GlyphSet getGlyphSet(char[] context,
                            int start,
                            int limit,
                            byte baseline,
                            int[] order,
                            byte[] levels)
Construct a GlyphSet for the text between start and limit.

getGlyphSet

public GlyphSet getGlyphSet(String str)
Converts all characters in the String Object to font glyph codes. Glyph substitution may be performed.
Parameters:
str - a String object.
Returns:
a GlyphSet containing a collection of glyphs and glyph positions.

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.

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.

canDisplayUpTo

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

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.

getBaselineOffset

public float getBaselineOffset(byte baselineType)
Returns the offset from the roman baseline of the font to the specified baseline.

getAscent

public float getAscent()
Returns the ascent of the font above the roman baseline.

getDescent

public float getDescent()
Returns the descent of the font above the roman baseline.

getLeading

public float getLeading()
Returns the leading for this font;

getMaxAdvance

public float getMaxAdvance()
Returns the maximum advance of any glyph in this font.

getMaxBounds2D

public Rectangle2D getMaxBounds2D()
Returns the maximum bounding box of this font.

getItalicAngle

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

getBestFontFor

public static Font getBestFontFor(AttributedCharacterIterator text)
Resolve styles on the character at start into an instance of Font that can best render the text between start and limit. REMIND jk. Move it to graphics environment.

getBestFontFor

public static Font getBestFontFor(AttributedCharacterIterator text,
                                  int start,
                                  int limit)
Resolve styles on the character at start into an instance of Font that can best render the text between start and limit. REMIND jk. Move it to graphics environment.

finalize

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

Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

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.