java.awt

Class FontMetrics

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

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
charWidth(char ch)
Returns the width of the specified character.
int
charWidth(int ch)
Returns the width of the specified character.
int
charsWidth(char[] buf, int offset, int len)
Returns the total width of the specified character array.
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(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.
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(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.
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(String str, int beginIndex, int limit, Graphics context)
Rectangle2D
getStringBounds(String str, Graphics context)
Rectangle2D
getStringBounds(CharacterIterator ci, 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, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

font

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

Constructor Details

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 Details

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.

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.

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.

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.

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

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

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(char[] chars,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)

getStringBounds

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

getStringBounds

public Rectangle2D getStringBounds(String str,
                                   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.

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.
Overrides:
toString in interface Object
Returns:
A string representation of this object.

FontMetrics.java -- Information about about a fonts display characteristics Copyright (C) 1999, 2002, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.