JavaTM 2 Platform
Standard Ed. 6

java.awt
类 FontMetrics

java.lang.Object
  继承者 java.awt.FontMetrics
所有已实现的接口:
Serializable

public abstract class FontMetrics
extends Object
implements Serializable

FontMetrics 类定义字体规格对象,该对象封装将在特定屏幕上呈现特定字体的有关信息。

子类注意事项:由于当中很多方法都形成关闭的、相互递归的循环,所以必须注意,在每个这样的循环中至少要实现其中一个方法,以防止在使用子类时产生无穷递归。特别建议,至少要重写以下方法集合以确保正确性,并防止无穷递归(虽然其他子集也可以)。

字母 'p' 显示其“引用点” 注意,这些方法的实现效率低下,因此通常利用更高效的特定于工具包的实现重写它们。

当应用程序要求将字符放置在 (x, y) 位置时,放置字符的方式是使其引用点(附图中显示的点)放置在该位置。引用点指定一条水平线,称为字符的基线 (baseline)。在正常的打印中,字符的基线应该对齐。

此外,字体中的每个字符都具有 ascentdescentadvance width 属性。ascent 是字符超出基线之上的距离。descent 是字符超出基线以下的距离。advance width 指示 AWT 应该放置下一个字符的位置。

字符数组或字符串也可以具有 ascent、descent 和 advance width 属性。数组的 ascent 是数组中所有字符的最大 ascent。descent 是数组中所有字符的最大 descent。advance width 是字符数组中每个字符的 advance width 之和。String 的 advance 是 String 沿基线的距离。此距离是 String 居中或右对齐应该使用的 width。

注意,String 的 advance 不一定是每个字符单独测量的 advance 之和,因为字符的 width 可随上下文变动。例如,在 Arabic 文本中,可以更改一个字符的形状,以连接到其他字符。而在有些脚本中,某些字符序列可以由单个形状(称为连字)表示。单独测量的字符并不能说明这些转换。

字体规格是基线相关的,意味着它们通常是独立应用于该字体的循环(模可能的网格提示作用)。请参阅 Font

从以下版本开始:
JDK1.0
另请参见:
Font, 序列化表格

字段摘要
protected  Font font
          实际的 Font,字体规格是由此创建的。
 
构造方法摘要
protected FontMetrics(Font font)
          创建一个新 FontMetrics 对象,用于查找该 Font 中关于指定 Font 和特定字符字形的 height 和 width 信息。
 
方法摘要
 int bytesWidth(byte[] data, int off, int len)
          返回显示此 Font 中指定字节数组的总 advance width。
 int charsWidth(char[] data, int off, int len)
          返回显示此 Font 中指定字符数组的总 advance width。
 int charWidth(char ch)
          返回此 Font 中指定字符的 advance width。
 int charWidth(int codePoint)
          返回此 Font 中指定字符的 advance width。
 int getAscent()
          确定此 FontMetrics 对象所描述的 Fontfont ascent
 int getDescent()
          确定此 FontMetrics 对象所描述的 Fontfont descent
 Font getFont()
          获取此 FontMetrics 对象所描述的 Font
 FontRenderContext getFontRenderContext()
          获取此 FontMetrics 对象用来测量文本的 FontRenderContext
 int getHeight()
          获取此 Font 中文本行的标准 height。
 int getLeading()
          确定此 FontMetrics 对象所描述的 Font标准行间距
 LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, Graphics context)
          返回指定 Graphics 上下文中指定字符数组的 LineMetrics 对象。
 LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, Graphics context)
          返回指定 Graphics 上下文中指定 CharacterIteratorLineMetrics 对象。
 LineMetrics getLineMetrics(String str, Graphics context)
          返回指定 Graphics 上下文中指定 StringLineMetrics 对象。
 LineMetrics getLineMetrics(String str, int beginIndex, int limit, Graphics context)
          返回指定 Graphics 上下文中指定 StringLineMetrics 对象。
 int getMaxAdvance()
          获取此 Font 中所有字符的最大 advance width。
 int getMaxAscent()
          确定此 FontMetrics 对象所描述的 Font 的最大 ascent。
 Rectangle2D getMaxCharBounds(Graphics context)
          返回指定 Graphics 上下文中具有最大边界的字符的边界。
 int getMaxDecent()
          已过时。 从 JDK version 1.1.1 开始,由 getMaxDescent() 取代。
 int getMaxDescent()
          确定此 FontMetrics 对象所描述的 Font 的最大 descent。
 Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context)
          返回指定 Graphics 上下文中指定字符数组的边界。
 Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, Graphics context)
          返回用指定 Graphics 上下文中指定 CharacterIterator 进行索引的字符的边界。
 Rectangle2D getStringBounds(String str, Graphics context)
          返回指定 Graphics 上下文中指定 String 的边界。
 Rectangle2D getStringBounds(String str, int beginIndex, int limit, Graphics context)
          返回指定 Graphics 上下文中指定 String 的边界。
 int[] getWidths()
          获取此 Font 中前 256 个字符的 advance width。
 boolean hasUniformLineMetrics()
          检查 Font 是否具有统一的行规格。
 int stringWidth(String str)
          返回此 Font 中指定 String 的总 advance width。
 String toString()
          返回此 FontMetrics 对象的值的 String 表示形式。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

font

protected Font font
实际的 Font,字体规格是由此创建的。它不能为 null。

另请参见:
getFont()
构造方法详细信息

FontMetrics

protected FontMetrics(Font font)
创建一个新 FontMetrics 对象,用于查找该 Font 中关于指定 Font 和特定字符字形的 height 和 width 信息。

参数:
font - Font
另请参见:
Font
方法详细信息

getFont

public Font getFont()
获取此 FontMetrics 对象所描述的 Font

返回:
FontMetrics 对象所描述的 Font

getFontRenderContext

public FontRenderContext getFontRenderContext()
获取此 FontMetrics 对象用来测量文本的 FontRenderContext

注意,此类中带有 Graphics 参数的方法测量使用该 Graphics 对象的 FontRenderContext 文本,而非此 FontRenderContext

返回:
FontMetrics 对象使用的 FontRenderContext
从以下版本开始:
1.6

getLeading

public int getLeading()
确定此 FontMetrics 对象所描述的 Font标准行间距。标准行间距(或行间间隔)是保留在一个文本行的 descent 和下一个文本行的 ascent 之间的逻辑间隔量。计算 height 规格时要包括这一额外间隔。

返回:
Font 的标准行间距。
另请参见:
getHeight(), getAscent(), getDescent()

getAscent

public int getAscent()
确定此 FontMetrics 对象所描述的 Fontfont ascent。font ascent 是字体基线到大多数字母数字字符顶部的距离。在 Font 中,有些字符可能扩展超过 font ascent 线。

返回:
Font 的 font ascent。
另请参见:
getMaxAscent()

getDescent

public int getDescent()
确定此 FontMetrics 对象所描述的 Fontfont descent。font descent 是字体基线到大多数字母数字字符底部的距离。在 Font 中,有些字符可能扩展到 font descent 线之下。

返回:
Font 的 font descent。
另请参见:
getMaxDescent()

getHeight

public int getHeight()
获取此 Font 中文本行的标准 height。它是相邻文本行基线之间的距离。它是 leading、ascent、descent 的总和。由于采用了舍入方法,它可能不同于 getAscent()、getDescent()、getLeading() 的总和。不保证以此距离间隔的文本行是不相交的;如果有些字符超出了标准 ascent 或标准 descent,则这些文本行可能会重叠。

返回:
Font 的标准 height。
另请参见:
getLeading(), getAscent(), getDescent()

getMaxAscent

public int getMaxAscent()
确定此 FontMetrics 对象所描述的 Font 的最大 ascent。字符超出字体基线的距离不会大于此 height。

返回:
Font 中所有字符的最大 ascent。
另请参见:
getAscent()

getMaxDescent

public int getMaxDescent()
确定此 FontMetrics 对象所描述的 Font 的最大 descent。字符低于字体基线的距离不会大于此 height。

返回:
Font 中所有字符的最大 descent。
另请参见:
getDescent()

getMaxDecent

@Deprecated
public int getMaxDecent()
已过时。 从 JDK version 1.1.1 开始,由 getMaxDescent() 取代。

只用于向后兼容性。

返回:
Font 中所有字符的最大 descent。
另请参见:
getMaxDescent()

getMaxAdvance

public int getMaxAdvance()
获取此 Font 中所有字符的最大 advance width。advance 是字符串基线上最左边的点到最右边的点之间的距离。String 的 advance 不一定是它所有字符的 advance 之和。

返回:
Font 中所有字符的最大 advance width;如果最大 advance width 未知,则返回 -1

charWidth

public int charWidth(int codePoint)
返回此 Font 中指定字符的 advance width。advance 是字符基线上最左边的点到最右边的点之间的距离。注意,String 的 advance 不一定是它所有字符的 advance 之和。

此方法没有验证指定的字符是否为一个有效的 Unicode 代码点。如有必要,调用者必须使用 Character.isValidCodePoint 验证字符值。

参数:
codePoint - 要测量的字符(Unicode 代码点)
返回:
FontMetrics 对象所描述的 Font 中指定字符的 advance width。
另请参见:
charsWidth(char[], int, int), stringWidth(String)

charWidth

public int charWidth(char ch)
返回此 Font 中指定字符的 advance width。advance 是字符基线上最左边的点到最右边的点之间的距离。注意,String 的 advance 不一定是它所有字符的 advance 之和。

注: 此方法无法处理增补字符。要支持包括增补字符在内的所有 Unicode 字符,请使用 charWidth(int) 方法。

参数:
ch - 要测量的字符
返回:
FontMetrics 对象所描述的 Font 中指定字符的 advance width。
另请参见:
charsWidth(char[], int, int), stringWidth(String)

stringWidth

public int stringWidth(String str)
返回此 Font 中指定 String 的总 advance width。advance 是字符串基线上最左边的点到最右边的点之间的距离。

注意,code>String 的 advance 不一定是它所有字符的 advance 之和。

参数:
str - 要测量的 String
返回:
FontMetrics 描述的 Font 中指定 String 的 advance width。
抛出:
NullPointerException - 如果 str 为 null。
另请参见:
bytesWidth(byte[], int, int), charsWidth(char[], int, int), getStringBounds(String, Graphics)

charsWidth

public int charsWidth(char[] data,
                      int off,
                      int len)
返回显示此 Font 中指定字符数组的总 advance width。advance 是字符串基线上最左边的点到最右边的点之间的距离。String 的 advance 不一定是它所有字符的 advance 之和。这等同于测量 String 指定范围内的字符。

参数:
data - 要测量的字符数组
off - 数组中字符的起始偏移量
len - 数组中要测量的字符数
返回:
FontMetrics 对象所描述的字体中指定 char 数组子数组的 advance width。
抛出:
NullPointerException - 如果 data 为 null。
IndexOutOfBoundsException - 如果 offlen 参数索引字符超出 data 数组范围。
另请参见:
charWidth(int), charWidth(char), bytesWidth(byte[], int, int), stringWidth(String)

bytesWidth

public int bytesWidth(byte[] data,
                      int off,
                      int len)
返回显示此 Font 中指定字节数组的总 advance width。advance 是字符串基线上最左边的点到最右边的点之间的距离。String 的 advance 不一定是它所有字符的 advance 之和。这等同于测量 String 指定范围内的字符。

参数:
data - 要测量的字节数组
off - 数组中字节的起始偏移量
len - 数组中要测量的字节数
返回:
FontMetrics 对象所描述的 Font 中指定 byte 数组的子数组的 advance width。
抛出:
NullPointerException - 如果 data 为 null。
IndexOutOfBoundsException - 如果 offlen 参数索引字节超出 data 数组范围。
另请参见:
charsWidth(char[], int, int), stringWidth(String)

getWidths

public int[] getWidths()
获取此 Font 中前 256 个字符的 advance width。advance 是字符基线上最左边的点到最右边的点之间的距离。注意,String 的 advance 不一定是它所有字符的 advance 之和。

返回:
一个数组,它存储此 FontMetrics 对象所描述的 Font 中字符的 advance width。

hasUniformLineMetrics

public boolean hasUniformLineMetrics()
检查 Font 是否具有统一的行规格。复合字体可能由几种不同的字体组成,涉及到各种字符集。在此情况下,FontLineMetrics 对象是不统一的。不同的字体可能有不同的 ascent、descent、metrics 等等。对于行测量和行断开来说,此信息有时是必需的。

返回:
如果字体具有统一的行规格,则返回 true;否则返回 false
另请参见:
Font.hasUniformLineMetrics()

getLineMetrics

public LineMetrics getLineMetrics(String str,
                                  Graphics context)
返回指定 Graphics 上下文中指定 StringLineMetrics 对象。

参数:
str - 指定的 String
context - 指定的 Graphics 上下文
返回:
使用指定 StringGraphics 上下文创建的 LineMetrics 对象。
另请参见:
Font.getLineMetrics(String, FontRenderContext)

getLineMetrics

public LineMetrics getLineMetrics(String str,
                                  int beginIndex,
                                  int limit,
                                  Graphics context)
返回指定 Graphics 上下文中指定 StringLineMetrics 对象。

参数:
str - 指定的 String
beginIndex - str 的初始偏移量
limit - str 的结束偏移量
context - 指定的 Graphics 上下文
返回:
使用指定 StringGraphics 上下文创建的 LineMetrics 对象。
另请参见:
Font.getLineMetrics(String, int, int, FontRenderContext)

getLineMetrics

public LineMetrics getLineMetrics(char[] chars,
                                  int beginIndex,
                                  int limit,
                                  Graphics context)
返回指定 Graphics 上下文中指定字符数组的 LineMetrics 对象。

参数:
chars - 指定的字符数组
beginIndex - chars 的初始偏移量
limit - chars 的结束偏移量
context - 指定的 Graphics 上下文
返回:
使用指定字符数组和 Graphics 上下文创建的 LineMetrics 对象。
另请参见:
Font.getLineMetrics(char[], int, int, FontRenderContext)

getLineMetrics

public LineMetrics getLineMetrics(CharacterIterator ci,
                                  int beginIndex,
                                  int limit,
                                  Graphics context)
返回指定 Graphics 上下文中指定 CharacterIteratorLineMetrics 对象。

参数:
ci - 指定的 CharacterIterator
beginIndex - ci 中的初始偏移量
limit - ci 的结束索引
context - 指定的 Graphics 上下文
返回:
使用指定参数创建的 LineMetrics 对象。
另请参见:
Font.getLineMetrics(CharacterIterator, int, int, FontRenderContext)

getStringBounds

public Rectangle2D getStringBounds(String str,
                                   Graphics context)
返回指定 Graphics 上下文中指定 String 的边界。边界用来对 String 进行布局。

注意:返回的边界在基线相关的坐标内 (请参阅类注释)。

参数:
str - 指定的 String
context - 指定的 Graphics 上下文
返回:
一个 Rectangle2D,它是指定 Graphics 上下文中指定 String 的边界框。
另请参见:
Font.getStringBounds(String, FontRenderContext)

getStringBounds

public Rectangle2D getStringBounds(String str,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)
返回指定 Graphics 上下文中指定 String 的边界。边界用来对 String 进行布局。

注意:返回的边界在基线相关的坐标内 (请参阅类注释)。

参数:
str - 指定的 String
beginIndex - str 的起始偏移量
limit - str 的结束偏移量
context - 指定的 Graphics 上下文
返回:
一个 Rectangle2D,它是指定 String 上下文中指定 Graphics 的边界框。
另请参见:
Font.getStringBounds(String, int, int, FontRenderContext)

getStringBounds

public Rectangle2D getStringBounds(char[] chars,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)
返回指定 Graphics 上下文中指定字符数组的边界。边界用来对 String 进行布局,String 是使用指定的字符数组、beginIndexlimit 创建的。

注意:返回的边界在基线相关的坐标内 (请参阅类注释)。

参数:
chars - 字符数组
beginIndex - 字符数组的初始偏移量
limit - 字符数组的结束偏移量
context - 指定的 Graphics 上下文
返回:
一个 Rectangle2D,它是指定 Graphics 上下文中指定字符数组的边界框。
另请参见:
Font.getStringBounds(char[], int, int, FontRenderContext)

getStringBounds

public Rectangle2D getStringBounds(CharacterIterator ci,
                                   int beginIndex,
                                   int limit,
                                   Graphics context)
返回用指定 Graphics 上下文中指定 CharacterIterator 进行索引的字符的边界。

注意:返回的边界在基线相关的坐标内(请参阅类注释)。

参数:
ci - 指定的 CharacterIterator
beginIndex - ci 中的初始偏移量
limit - ci 的结束索引
context - 指定的 Graphics 上下文
返回:
一个 Rectangle2D,它是用指定 Graphics 上下文中的指定 CharacterIterator 进行索引的字符的边界框。
另请参见:
Font.getStringBounds(CharacterIterator, int, int, FontRenderContext)

getMaxCharBounds

public Rectangle2D getMaxCharBounds(Graphics context)
返回指定 Graphics 上下文中具有最大边界的字符的边界。

参数:
context - 指定的 Graphics 上下文
返回:
一个 Rectangle2D,它是具有最大边界的字符的边界框。
另请参见:
Font.getMaxCharBounds(FontRenderContext)

toString

public String toString()
返回此 FontMetrics 对象的值的 String 表示形式。

覆盖:
Object 中的 toString
返回:
FontMetrics 对象的 String 表示形式。
从以下版本开始:
JDK1.0.

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only