java.awt
Class FontMetrics

java.lang.Object
  extended by java.awt.FontMetrics
All Implemented Interfaces:
Serializable

public abstract class FontMetrics
extends Object
implements Serializable

This class returns information about the display characteristics of a font. It is abstract, and concrete subclasses should implement at least the following methods:

See Also:
Serialized Form

Field Summary
protected  Font font
          This is the font for which metrics will be returned.
 
Constructor Summary
protected FontMetrics(Font font)
          Initializes a new instance of FontMetrics for the specified font.
 
Method Summary
 int bytesWidth(byte[] buf, int offset, int len)
          Returns the total width of the specified byte array.
 int charsWidth(char[] buf, int offset, int len)
          Returns the total width of the specified character array.
 int charWidth(char ch)
          Returns the width of the specified character.
 int charWidth(int ch)
          Returns the width of the specified character.
 int getAscent()
          Returns the ascent of the font, which is the distance from the base to the top of the majority of characters in the set.
 int getDescent()
          Returns the descent of the font, which is the distance from the base to the bottom of the majority of characters in the set.
 Font getFont()
          Returns the font that this object is creating metric information fo.
 int getHeight()
          Returns the height of a line in this font.
 int getLeading()
          Returns the leading, or spacing between lines, for this font.
 LineMetrics getLineMetrics(char[] chars, int begin, int limit, Graphics g)
          Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.
 LineMetrics getLineMetrics(CharacterIterator ci, int begin, int limit, Graphics g)
          Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.
 LineMetrics getLineMetrics(String text, Graphics g)
          Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.
 LineMetrics getLineMetrics(String text, int begin, int limit, Graphics g)
          Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.
 int getMaxAdvance()
          Returns the width of the widest character in the font.
 int getMaxAscent()
          Returns the maximum ascent value.
 Rectangle2D getMaxCharBounds(Graphics context)
          Returns the bounds of the largest character in a Graphics context.
 int getMaxDecent()
          Deprecated. This method is deprecated in favor of getMaxDescent().
 int getMaxDescent()
          Returns the maximum descent value.
 Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context)
           
 Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, Graphics context)
           
 Rectangle2D getStringBounds(String str, Graphics context)
           
 Rectangle2D getStringBounds(String str, int beginIndex, int limit, Graphics context)
           
 int[] getWidths()
          Returns the widths of the first 256 characters in the font.
 boolean hasUniformLineMetrics()
          Returns if the font has uniform line metrics.
 int stringWidth(String str)
          Returns the total width of the specified string
 String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

font

protected Font font
This is the font for which metrics will be returned.

Constructor Detail

FontMetrics

protected FontMetrics(Font font)
Initializes a new instance of FontMetrics for the specified font.

Parameters:
font - The font to return metric information for.
Method Detail

getFont

public Font getFont()
Returns the font that this object is creating metric information fo.

Returns:
The font for this object.

getLeading

public int getLeading()
Returns the leading, or spacing between lines, for this font.

Returns:
The font leading.

getAscent

public int getAscent()
Returns the ascent of the font, which is the distance from the base to the top of the majority of characters in the set. Some characters can exceed this value however.

Returns:
The font ascent.

getDescent

public int getDescent()
Returns the descent of the font, which is the distance from the base to the bottom of the majority of characters in the set. Some characters can exceed this value however.

Returns:
The font descent.

getHeight

public int getHeight()
Returns the height of a line in this font. This will be the sum of the leading, the ascent, and the descent.

Returns:
The height of the font.

getMaxAscent

public int getMaxAscent()
Returns the maximum ascent value. This is the maximum distance any character in the font rised above the baseline.

Returns:
The maximum ascent for this font.

getMaxDescent

public int getMaxDescent()
Returns the maximum descent value. This is the maximum distance any character in the font extends below the baseline.

Returns:
The maximum descent for this font.

getMaxDecent

public int getMaxDecent()
Deprecated. This method is deprecated in favor of getMaxDescent().

Returns the maximum descent value. This is the maximum distance any character in the font extends below the baseline.

Returns:
The maximum descent for this font.

getMaxAdvance

public int getMaxAdvance()
Returns the width of the widest character in the font.

Returns:
The width of the widest character in the font.

charWidth

public int charWidth(int ch)
Returns the width of the specified character.

Parameters:
ch - The character to return the width of.
Returns:
The width of the specified character.

charWidth

public int charWidth(char ch)
Returns the width of the specified character.

Parameters:
ch - The character to return the width of.
Returns:
The width of the specified character.

stringWidth

public int stringWidth(String str)
Returns the total width of the specified string

Parameters:
str - The string to return the width of.
Returns:
The width of the string.

charsWidth

public int charsWidth(char[] buf,
                      int offset,
                      int len)
Returns the total width of the specified character array.

Parameters:
buf - The character array containing the data.
offset - The offset into the array to start calculating from.
len - The total number of bytes to process.
Returns:
The width of the requested characters.

bytesWidth

public int bytesWidth(byte[] buf,
                      int offset,
                      int len)
Returns the total width of the specified byte array.

Parameters:
buf - The byte array containing the data.
offset - The offset into the array to start calculating from.
len - The total number of bytes to process.
Returns:
The width of the requested characters.

getWidths

public int[] getWidths()
Returns the widths of the first 256 characters in the font.

Returns:
The widths of the first 256 characters in the font.

toString

public String toString()
Returns a string representation of this object.

Overrides:
toString in class Object
Returns:
A string representation of this object.
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)

getLineMetrics

public LineMetrics getLineMetrics(String text,
                                  Graphics g)
Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.

Parameters:
text - The string to calculate metrics from.
g - The Graphics object that will be used.
Returns:
A new LineMetrics object.

getLineMetrics

public LineMetrics getLineMetrics(String text,
                                  int begin,
                                  int limit,
                                  Graphics g)
Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.

Parameters:
text - The string to calculate metrics from.
begin - Index of first character in text to measure.
limit - Index of last character in text to measure.
g - The Graphics object that will be used.
Returns:
A new LineMetrics object.
Throws:
IndexOutOfBoundsException - if the range [begin, limit] is invalid in text.

getLineMetrics

public LineMetrics getLineMetrics(char[] chars,
                                  int begin,
                                  int limit,
                                  Graphics g)
Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.

Parameters:
chars - The string to calculate metrics from.
begin - Index of first character in text to measure.
limit - Index of last character in text to measure.
g - The Graphics object that will be used.
Returns:
A new LineMetrics object.
Throws:
IndexOutOfBoundsException - if the range [begin, limit] is invalid in text.

getMaxCharBounds

public Rectangle2D getMaxCharBounds(Graphics context)
Returns the bounds of the largest character in a Graphics context.

Parameters:
context - the Graphics context object.
Returns:
a Rectangle2D representing the bounds

getLineMetrics

public LineMetrics getLineMetrics(CharacterIterator ci,
                                  int begin,
                                  int limit,
                                  Graphics g)
Returns a LineMetrics object constructed with the specified text and the FontRenderContext of the Graphics object when it is an instance of Graphics2D or a generic FontRenderContext with a null transform, not anti-aliased and not using fractional metrics.

Parameters:
ci - An iterator over the string to calculate metrics from.
begin - Index of first character in text to measure.
limit - Index of last character in text to measure.
g - The Graphics object that will be used.
Returns:
A new LineMetrics object.
Throws:
IndexOutOfBoundsException - if the range [begin, limit] is invalid in text.

getStringBounds

public Rectangle2D getStringBounds(String str,
                                   Graphics context)

getStringBounds

public Rectangle2D getStringBounds(String str,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)

getStringBounds

public Rectangle2D getStringBounds(char[] chars,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)

getStringBounds

public Rectangle2D getStringBounds(CharacterIterator ci,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)

hasUniformLineMetrics

public boolean hasUniformLineMetrics()
Returns if the font has uniform line metrics.

See Also:
Font.hasUniformLineMetrics()