FontMetrics class defines a font metrics object, which
encapsulates information about the rendering of a particular font on a
particular screen.
Note to subclassers: Since many of these methods form closed, mutually recursive loops, you must take care that you implement at least one of the methods in each such loop to prevent infinite recursion when your subclass is used. In particular, the following is the minimal suggested set of methods to override in order to ensure correctness and prevent infinite recursion (though other subsets are equally feasible):
Note that the implementations of these methods are
inefficient, so they are usually overridden with more efficient
toolkit-specific implementations.
When an application asks to place a character at the position (x, y), the character is placed so that its reference point (shown as the dot in the accompanying image) is put at that position. The reference point specifies a horizontal line called the baseline of the character. In normal printing, the baselines of characters should align.
In addition, every character in a font has an ascent, a descent, and an advance width. The ascent is the amount by which the character ascends above the baseline. The descent is the amount by which the character descends below the baseline. The advance width indicates the position at which AWT should place the next character.
An array of characters or a string can also have an ascent, a
descent, and an advance width. The ascent of the array is the
maximum ascent of any character in the array. The descent is the
maximum descent of any character in the array. The advance width
is the sum of the advance widths of each of the characters in the
character array. The advance of a String is the
distance along the baseline of the String. This
distance is the width that should be used for centering or
right-aligning the String.
Note that the advance of a String is not necessarily
the sum of the advances of its characters measured in isolation
because the width of a character can vary depending on its context.
For example, in Arabic text, the shape of a character can change
in order to connect to other characters. Also, in some scripts,
certain character sequences can be represented by a single shape,
called a ligature. Measuring characters individually does
not account for these transformations.
Font metrics are baseline-relative, meaning that they are
generally independent of the rotation applied to the font (modulo
possible grid hinting effects). See Font.
java.awt.Font
| Modifier and Type | Field and Description |
|---|---|
| private static final FontRenderContext | |
| protected Font | |
| private static final long | serialVersionUID
Use serialVersionUID from JDK 1.1 for interoperability. |
| Access | Constructor and Description |
|---|---|
| protected | FontMetrics(Font
the font)FontCreates a new |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the advance width of the subarray of the specifiedbyte array in the Font
described by
this FontMetrics object.the array of bytes to be measured data, int the start offset of the bytes in the array off, int the number of bytes to be measured from the array len)Returns the total advance width for showing the specified array
of bytes in this |
| public int | Returns: the advance width of the subarray of the specifiedchar array in the font described by
this FontMetrics object.the array of characters to be measured data, int the start offset of the characters in the array off, int the number of characters to be measured from the array len)Returns the total advance width for showing the specified array
of characters in this |
| public int | Returns: the advance width of the specified character in theFont described by this
FontMetrics object.the character (Unicode code point) to be measured codePoint)Returns the advance width of the specified character in this
|
| public int | Returns: the advance width of the specified character in theFont described by this
FontMetrics object.the character to be measured ch)Returns the advance width of the specified character in this
|
| public int | Returns: the font ascent of theFont.Determines the font ascent of the |
| public int | Returns: the font descent of theFont.Determines the font descent of the |
| public Font | Returns: theFont described by this
FontMetrics object.Gets the |
| public FontRenderContext | Returns: theFontRenderContext used by this
FontMetrics object.Gets the |
| public int | Returns: the standard height of the font.Gets the standard height of a line of text in this font. |
| public int | Returns: the standard leading of theFont.Determines the standard leading of the
|
| public LineMetrics | Returns: aLineMetrics object created with the
specified String and Graphics context.the specified str, Graphics Stringthe specified context)Graphics contextReturns the |
| public LineMetrics | Returns: aLineMetrics object created with the
specified String and Graphics context.the specified str, int Stringthe initial offset of beginIndex, int strthe end offset of limit, Graphics strthe specified context)Graphics contextReturns the |
| public LineMetrics | Returns: aLineMetrics object created with the
specified character array and Graphics context.the specified character array chars, int the initial offset of beginIndex, int charsthe end offset of limit, Graphics charsthe specified context)Graphics contextReturns the |
| public LineMetrics | Returns: aLineMetrics object created with the
specified arguments.the specified ci, int CharacterIteratorthe initial offset in beginIndex, int cithe end index of limit, Graphics cithe specified context)Graphics contextReturns the |
| public int | Returns: an estimate of the maximum advance width of any character in theFont, or -1 if the
maximum advance width is not known.Returns an estimate of the maximum advance width of any character
in the |
| public int | Returns: the maximum ascent of any character in theFont.Determines the maximum ascent of the |
| public Rectangle2D | Returns: aRectangle2D that is the
bounding box for the character with the maximum bounds.the specified context)Graphics contextReturns the bounds for the character with the maximum bounds
in the specified |
| public int | Returns: the maximum descent of any character in theFont.
Deprecated
As of JDK version 1.1.1,
replaced by
getMaxDescent().
For backward compatibility only. |
| public int | Returns: the maximum descent of any character in theFont.Determines the maximum descent of the |
| public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
specified String in the specified
Graphics context.the specified str, Graphics Stringthe specified context)Graphics contextReturns the bounds of the specified |
| public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
specified String in the specified
Graphics context.the specified str, int Stringthe offset of the beginning of beginIndex, int strthe end offset of limit, Graphics strthe specified context)Graphics contextReturns the bounds of the specified |
| public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
specified character array in the specified
Graphics context.an array of characters chars, int the initial offset of the array of
characters beginIndex, int the end offset of the array of characters limit, Graphics the specified context)Graphics contextReturns the bounds of the specified array of characters
in the specified |
| public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
characters indexed in the specified CharacterIterator
in the specified Graphics context.the specified ci, int CharacterIteratorthe initial offset in beginIndex, int cithe end index of limit, Graphics cithe specified context)Graphics contextReturns the bounds of the characters indexed in the specified
|
| public int[] | Returns: an array storing the advance widths of the characters in theFont
described by this FontMetrics object.Gets the advance widths of the first 256 characters in the
|
| public boolean | Returns: true if the font has uniform line metrics;
false otherwise.Checks to see if the |
| private static native void | |
| private FontRenderContext | |
| public int | Returns: the advance width of the specifiedString
in the Font described by this
FontMetrics.the str)String to be measuredReturns the total advance width for showing the specified
|
| public String | Returns: aString representation of this
FontMetrics object.Overrides java. Returns a representation of this |
| DEFAULT_FRC | back to summary |
|---|---|
| private static final FontRenderContext DEFAULT_FRC | |
| font | back to summary |
|---|---|
| protected Font font The actual
| |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Use serialVersionUID from JDK 1.1 for interoperability. | |
| FontMetrics | back to summary |
|---|---|
| protected FontMetrics(Font font) Creates a new
| |
| bytesWidth | back to summary |
|---|---|
| public int bytesWidth(byte[] data, int off, int len) Returns the total advance width for showing the specified array
of bytes in this
| |
| charsWidth | back to summary |
|---|---|
| public int charsWidth(char[] data, int off, int len) Returns the total advance width for showing the specified array
of characters in this
| |
| charWidth | back to summary |
|---|---|
| public int charWidth(int codePoint) Returns the advance width of the specified character in this
This method doesn't validate the specified character to be a
valid Unicode code point. The caller must validate the
character value using
| |
| charWidth | back to summary |
|---|---|
| public int charWidth(char ch) Returns the advance width of the specified character in this
Note This method cannot handle
supplementary characters.
To support all Unicode characters, including
supplementary characters, use the
| |
| getAscent | back to summary |
|---|---|
| public int getAscent() Determines the font ascent of the
| |
| getDescent | back to summary |
|---|---|
| public int getDescent() Determines the font descent of the
| |
| getFont | back to summary |
|---|---|
| public Font getFont() Gets the
| |
| getFontRenderContext | back to summary |
|---|---|
| public FontRenderContext getFontRenderContext() Gets the
Note that methods in this class which take a
| |
| getHeight | back to summary |
|---|---|
| public int getHeight() Gets the standard height of a line of text in this font. This is the distance between the baseline of adjacent lines of text. It is the sum of the leading + ascent + descent. Due to rounding this may not be the same as getAscent() + getDescent() + getLeading(). There is no guarantee that lines of text spaced at this distance are disjoint; such lines may overlap if some characters overshoot either the standard ascent or the standard descent metric.
| |
| getLeading | back to summary |
|---|---|
| public int getLeading() Determines the standard leading of the
| |
| getLineMetrics | back to summary |
|---|---|
| public LineMetrics getLineMetrics(String str, Graphics context) Returns the
| |
| getLineMetrics | back to summary |
|---|---|
| public LineMetrics getLineMetrics(String str, int beginIndex, int limit, Graphics context) Returns the
| |
| getLineMetrics | back to summary |
|---|---|
| public LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, Graphics context) Returns the
| |
| getLineMetrics | back to summary |
|---|---|
| public LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, Graphics context) Returns the
| |
| getMaxAdvance | back to summary |
|---|---|
| public int getMaxAdvance() Returns an estimate of the maximum advance width of any character
in the The advance is the distance from the leftmost point used to position the character to the rightmost point along the baseline. This is not the same thing as the visible width of the glyph image representing the character.
The advance of a Some of the caveats of the reported value include
String
is not necessarily the sum of the advances the value still needs
to be used with caution.
In summary, this method makes no absolute guarantee, nor can it even make a guarantee to be correct within some margin of error. So it should be used at most only for estimating the total space sufficient to display some number of as yet unknown characters from the font. And that might be either an overestimate, or an underestimate depending on the specific text and rendering context.
| |
| getMaxAscent | back to summary |
|---|---|
| public int getMaxAscent() Determines the maximum ascent of the
| |
| getMaxCharBounds | back to summary |
|---|---|
| public Rectangle2D getMaxCharBounds(Graphics context) Returns the bounds for the character with the maximum bounds
in the specified
| |
| getMaxDecent | back to summary |
|---|---|
| public int getMaxDecent()
Deprecated As of JDK version 1.1.1,
replaced by For backward compatibility only.
| |
| getMaxDescent | back to summary |
|---|---|
| public int getMaxDescent() Determines the maximum descent of the
| |
| getStringBounds | back to summary |
|---|---|
| public Rectangle2D getStringBounds(String str, Graphics context) Returns the bounds of the specified Note The returned bounds is in baseline-relative coordinates
(see
| |
| getStringBounds | back to summary |
|---|---|
| public Rectangle2D getStringBounds(String str, int beginIndex, int limit, Graphics context) Returns the bounds of the specified Note The returned bounds is in baseline-relative coordinates
(see
| |
| getStringBounds | back to summary |
|---|---|
| public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context) Returns the bounds of the specified array of characters
in the specified Note The returned bounds is in baseline-relative coordinates
(see
| |
| getStringBounds | back to summary |
|---|---|
| public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, Graphics context) Returns the bounds of the characters indexed in the specified
Note The returned bounds is in baseline-relative coordinates
(see
| |
| getWidths | back to summary |
|---|---|
| public int[] getWidths() Gets the advance widths of the first 256 characters in the
| |
| hasUniformLineMetrics | back to summary |
|---|---|
| public boolean hasUniformLineMetrics() Checks to see if the
| |
| initIDs | back to summary |
|---|---|
| private static native void initIDs() Initialize JNI field and method IDs | |
| myFRC | back to summary |
|---|---|
| private FontRenderContext myFRC(Graphics context) | |
| stringWidth | back to summary |
|---|---|
| public int stringWidth(String str) Returns the total advance width for showing the specified
Note that the advance of a
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a representation of this
| |