FileDocCategorySizeDatePackage
FontMetrics.javaAPI DocAndroid 1.5 API14950Wed May 06 22:41:54 BST 2009java.awt

FontMetrics

public abstract class FontMetrics extends Object implements Serializable
The FontMetrics class contains information about the rendering of a particular font on a particular screen.

Each character in the Font has three values that help define where to place it: an ascent, a descent, and an advance. The ascent is the distance the character extends above the baseline. The descent is the distance the character extends below the baseline. The advance width defines the position at which the next character should be placed.

An array of characters or a string has an ascent, a descent, and an advance width too. The ascent or descent of the array is specified by the maximum ascent or descent of the characters in the array. The advance width is the sum of the advance widths of each of the characters in the character array.

since
Android 1.0

Fields Summary
private static final long
serialVersionUID
The Constant serialVersionUID.
protected Font
font
The font from which the FontMetrics is created.
Constructors Summary
protected FontMetrics(Font fnt)
Instantiates a new font metrics from the specified Font.

param
fnt the Font.


                                  
       
        this.font = fnt;
    
Methods Summary
public intbytesWidth(byte[] data, int off, int len)
Returns the distance from the leftmost point to the rightmost point on the string's baseline showing the specified array of bytes in this Font.

param
data the array of bytes to be measured.
param
off the start offset.
param
len the number of bytes to be measured.
return
the advance width of the array.

        int width = 0;
        if ((off >= data.length) || (off < 0)) {
            // awt.13B=offset off is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13B")); //$NON-NLS-1$
        }

        if ((off + len > data.length)) {
            // awt.13C=number of elemets len is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13C")); //$NON-NLS-1$
        }

        for (int i = off; i < off + len; i++) {
            width += charWidth(data[i]);
        }

        return width;
    
public intcharWidth(int ch)
Returns the distance from the leftmost point to the rightmost point of the specified character in this Font.

param
ch the specified Unicode point code of character to be measured.
return
the advance width of the character.

        return 0;
    
public intcharWidth(char ch)
Returns the distance from the leftmost point to the rightmost point of the specified character in this Font.

param
ch the specified character to be measured.
return
the advance width of the character.

        return 0;
    
public intcharsWidth(char[] data, int off, int len)
Returns the distance from the leftmost point to the rightmost point on the string's baseline showing the specified array of characters in this Font.

param
data the array of characters to be measured.
param
off the start offset.
param
len the number of bytes to be measured.
return
the advance width of the array.

        int width = 0;
        if ((off >= data.length) || (off < 0)) {
            // awt.13B=offset off is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13B")); //$NON-NLS-1$
        }

        if ((off + len > data.length)) {
            // awt.13C=number of elemets len is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13C")); //$NON-NLS-1$
        }

        for (int i = off; i < off + len; i++) {
            width += charWidth(data[i]);
        }

        return width;
    
public intgetAscent()
Gets the font ascent of the Font associated with this FontMetrics. The font ascent is the distance from the font's baseline to the top of most alphanumeric characters.

return
the ascent of the Font associated with this FontMetrics.

        return 0;
    
public intgetDescent()
Gets the font descent of the Font associated with this FontMetrics. The font descent is the distance from the font's baseline to the bottom of most alphanumeric characters with descenders.

return
the descent of the Font associated with this FontMetrics.

        return 0;
    
private java.awt.font.FontRenderContextgetFRCFromGraphics(java.awt.Graphics context)
Returns a FontRenderContext instance of the Graphics context specified.

param
context the specified Graphics context.
return
a FontRenderContext of the specified Graphics context.

        FontRenderContext frc;
        if (context instanceof Graphics2D) {
            frc = ((Graphics2D)context).getFontRenderContext();
        } else {
            frc = new FontRenderContext(null, false, false);
        }

        return frc;
    
public java.awt.FontgetFont()
Gets the font associated with this FontMetrics.

return
the font associated with this FontMetrics.

        return font;
    
public intgetHeight()
Gets the height of the text line in this Font.

return
the height of the text line in this Font.

        return this.getAscent() + this.getDescent() + this.getLeading();
    
public intgetLeading()
Gets the leading of the Font associated with this FontMetrics.

return
the leading of the Font associated with this FontMetrics.

        return 0;
    
public java.awt.font.LineMetricsgetLineMetrics(char[] chars, int beginIndex, int limit, java.awt.Graphics context)
Gets the LineMetrics object for the specified character array in the specified Graphics.

param
chars the character array.
param
beginIndex the offset of array.
param
limit the number of characters to be used.
param
context the Graphics.
return
the LineMetrics object for the specified character array in the specified Graphics.

        return font.getLineMetrics(chars, beginIndex, limit, this.getFRCFromGraphics(context));
    
public java.awt.font.LineMetricsgetLineMetrics(java.lang.String str, int beginIndex, int limit, java.awt.Graphics context)
Gets the LineMetrics object for the specified String in the specified Graphics.

param
str the String.
param
beginIndex the offset.
param
limit the number of characters to be used.
param
context the Graphics.
return
the LineMetrics object for the specified String in the specified Graphics.

        return font.getLineMetrics(str, beginIndex, limit, this.getFRCFromGraphics(context));
    
public java.awt.font.LineMetricsgetLineMetrics(java.text.CharacterIterator ci, int beginIndex, int limit, java.awt.Graphics context)
Gets the LineMetrics object for the specified CharacterIterator in the specified Graphics.

param
ci the CharacterIterator.
param
beginIndex the offset.
param
limit the number of characters to be used.
param
context the Graphics.
return
the LineMetrics object for the specified CharacterIterator in the specified Graphics.

        return font.getLineMetrics(ci, beginIndex, limit, this.getFRCFromGraphics(context));
    
public java.awt.font.LineMetricsgetLineMetrics(java.lang.String str, java.awt.Graphics context)
Gets the LineMetrics object for the specified String in the specified Graphics.

param
str the String.
param
context the Graphics.
return
the LineMetrics object for the specified String in the specified Graphics.

        return font.getLineMetrics(str, this.getFRCFromGraphics(context));
    
public intgetMaxAdvance()
Gets the maximum advance width of character in this Font.

return
the maximum advance width of character in this Font.

        return 0;
    
public intgetMaxAscent()
Gets the maximum font ascent of the Font associated with this FontMetrics.

return
the maximum font ascent of the Font associated with this FontMetrics.

        return 0;
    
public java.awt.geom.Rectangle2DgetMaxCharBounds(java.awt.Graphics context)
Returns the character's maximum bounds in the specified Graphics context.

param
context the Graphics context.
return
the character's maximum bounds in the specified Graphics context.

        return this.font.getMaxCharBounds(this.getFRCFromGraphics(context));
    
public intgetMaxDecent()
Gets the maximum font descent of character in this Font.

return
the maximum font descent of character in this Font.
deprecated
Replaced by getMaxDescent() method.

        return 0;
    
public intgetMaxDescent()
Gets the maximum font descent of character in this Font.

return
the maximum font descent of character in this Font.

        return 0;
    
public java.awt.geom.Rectangle2DgetStringBounds(java.text.CharacterIterator ci, int beginIndex, int limit, java.awt.Graphics context)
Gets the bounds of the specified CharacterIterator in the specified Graphics context.

param
ci the CharacterIterator.
param
beginIndex the begin offset of the array.
param
limit the number of characters.
param
context the Graphics.
return
the bounds of the specified CharacterIterator in the specified Graphics context.

        return font.getStringBounds(ci, beginIndex, limit, this.getFRCFromGraphics(context));
    
public java.awt.geom.Rectangle2DgetStringBounds(java.lang.String str, int beginIndex, int limit, java.awt.Graphics context)
Gets the bounds of the specified String in the specified Graphics context.

param
str the String.
param
beginIndex the begin offset of the array.
param
limit the number of characters.
param
context the Graphics.
return
the bounds of the specified String in the specified Graphics context.

        return font.getStringBounds(str, beginIndex, limit, this.getFRCFromGraphics(context));
    
public java.awt.geom.Rectangle2DgetStringBounds(char[] chars, int beginIndex, int limit, java.awt.Graphics context)
Gets the bounds of the specified characters array in the specified Graphics context.

param
chars the characters array.
param
beginIndex the begin offset of the array.
param
limit the number of characters.
param
context the Graphics.
return
the bounds of the specified characters array in the specified Graphics context.

        return font.getStringBounds(chars, beginIndex, limit, this.getFRCFromGraphics(context));
    
public java.awt.geom.Rectangle2DgetStringBounds(java.lang.String str, java.awt.Graphics context)
Gets the bounds of the specified String in the specified Graphics context.

param
str the String.
param
context the Graphics.
return
the bounds of the specified String in the specified Graphics context.

        return font.getStringBounds(str, this.getFRCFromGraphics(context));
    
public int[]getWidths()
Gets the advance widths of the characters in the Font.

return
the advance widths of the characters in the Font.

        return null;
    
public booleanhasUniformLineMetrics()
Checks if the Font has uniform line metrics or not. The Font can contain characters of other fonts for covering character set. In this case the Font isn't uniform.

return
true, if the Font has uniform line metrics, false otherwise.

        return this.font.hasUniformLineMetrics();
    
public intstringWidth(java.lang.String str)
Returns the advance width for the specified String in this Font.

param
str String to be measured.
return
the the advance width for the specified String in this Font.

        return 0;
    
public java.lang.StringtoString()
Returns the String representation of this FontMetrics.

return
the string.

        return this.getClass().getName() + "[font=" + this.getFont() + //$NON-NLS-1$
                "ascent=" + this.getAscent() + //$NON-NLS-1$
                ", descent=" + this.getDescent() + //$NON-NLS-1$
                ", height=" + this.getHeight() + "]"; //$NON-NLS-1$ //$NON-NLS-2$