java.awt

Class FontMetrics

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:
Field Summary
protected Fontfont
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
intbytesWidth(byte[] buf, int offset, int len)
Returns the total width of the specified byte array.
intcharsWidth(char[] buf, int offset, int len)
Returns the total width of the specified character array.
intcharWidth(int ch)
Returns the width of the specified character.
intcharWidth(char ch)
Returns the width of the specified character.
intgetAscent()
Returns the ascent of the font, which is the distance from the base to the top of the majority of characters in the set.
intgetDescent()
Returns the descent of the font, which is the distance from the base to the bottom of the majority of characters in the set.
FontgetFont()
Returns the font that this object is creating metric information fo.
intgetHeight()
Returns the height of a line in this font.
intgetLeading()
Returns the leading, or spacing between lines, for this font.
LineMetricsgetLineMetrics(String text, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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.
LineMetricsgetLineMetrics(String text, int begin, int limit, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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.
LineMetricsgetLineMetrics(char[] chars, int begin, int limit, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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.
LineMetricsgetLineMetrics(CharacterIterator ci, int begin, int limit, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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.
intgetMaxAdvance()
Returns the width of the widest character in the font.
intgetMaxAscent()
Returns the maximum ascent value.
Rectangle2DgetMaxCharBounds(Graphics context)
Returns the bounds of the largest character in a Graphics context.
intgetMaxDecent()
Returns the maximum descent value.
intgetMaxDescent()
Returns the maximum descent value.
Rectangle2DgetStringBounds(String str, Graphics context)
Rectangle2DgetStringBounds(String str, int beginIndex, int limit, Graphics context)
Rectangle2DgetStringBounds(char[] chars, int beginIndex, int limit, Graphics context)
Rectangle2DgetStringBounds(CharacterIterator ci, int beginIndex, int limit, Graphics context)
int[]getWidths()
Returns the widths of the first 256 characters in the font.
booleanhasUniformLineMetrics()
Returns if the font has uniform line metrics.
intstringWidth(String str)
Returns the total width of the specified string
StringtoString()
Returns a string representation of this object.

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

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.

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.

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.

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.

getFont

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

Returns: The font for this object.

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.

getLeading

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

Returns: The font leading.

getLineMetrics

public LineMetrics getLineMetrics(String text, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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 {@link LineMetrics} object.

getLineMetrics

public LineMetrics getLineMetrics(String text, int begin, int limit, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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 {@link 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 {@link LineMetrics} object constructed with the specified text and the {@link 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 {@link LineMetrics} object.

Throws: IndexOutOfBoundsException if the range [begin, limit] is invalid in text.

getLineMetrics

public LineMetrics getLineMetrics(CharacterIterator ci, int begin, int limit, Graphics g)
Returns a {@link LineMetrics} object constructed with the specified text and the {@link 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 {@link LineMetrics} object.

Throws: IndexOutOfBoundsException if the range [begin, limit] is invalid in text.

getMaxAdvance

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

Returns: The width of the widest character in 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.

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

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.

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.

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)

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.

hasUniformLineMetrics

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

See Also: hasUniformLineMetrics

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.

toString

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

Returns: A string representation of this object.