下面列出了怎么用sun.font.FontDesignMetrics的API类实例代码及写法,或者点击链接到github查看源代码。
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
/**
* 获取文本所占宽度
*
* @param font 字体
* @param content 文本内容
* @return int 返回宽度
*/
private static int getWordWidth(Font font, String content) {
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
int width = 0;
for (int i = 0; i < content.length(); i++) {
width += metrics.charWidth(content.charAt(i));
}
return width;
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
/**
* Returns the logical bounds of the specified array of characters
* in the specified {@code FontRenderContext}. The logical
* bounds contains the origin, ascent, advance, and height, which
* includes the leading. The logical bounds does not always enclose
* all the text. For example, in some languages and in some fonts,
* accent marks can be positioned above the ascent or below the
* descent. To obtain a visual bounding box, which encloses all the
* text, use the {@link TextLayout#getBounds() getBounds} method of
* {@code TextLayout}.
* <p>Note: The returned bounds is in baseline-relative coordinates
* (see {@link java.awt.Font class notes}).
* @param chars an array of characters
* @param beginIndex the initial offset in the array of
* characters
* @param limit the end offset in the array of characters
* @param frc the specified {@code FontRenderContext}
* @return a {@code Rectangle2D} that is the bounding box of the
* specified array of characters in the specified
* {@code FontRenderContext}.
* @throws IndexOutOfBoundsException if {@code beginIndex} is
* less than zero, or {@code limit} is greater than the
* length of {@code chars}, or {@code beginIndex}
* is greater than {@code limit}.
* @see FontRenderContext
* @see Font#createGlyphVector
* @since 1.2
*/
public Rectangle2D getStringBounds(char [] chars,
int beginIndex, int limit,
FontRenderContext frc) {
if (beginIndex < 0) {
throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
}
if (limit > chars.length) {
throw new IndexOutOfBoundsException("limit: " + limit);
}
if (beginIndex > limit) {
throw new IndexOutOfBoundsException("range length: " +
(limit - beginIndex));
}
// this code should be in textlayout
// quick check for simple text, assume GV ok to use if simple
boolean simple = values == null ||
(values.getKerning() == 0 && values.getLigatures() == 0 &&
values.getBaselineTransform() == null);
if (simple) {
simple = ! FontUtilities.isComplexText(chars, beginIndex, limit);
}
if (simple) {
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(this, frc);
return metrics.getSimpleBounds(chars, beginIndex, limit-beginIndex);
} else {
// need char array constructor on textlayout
String str = new String(chars, beginIndex, limit - beginIndex);
TextLayout tl = new TextLayout(str, this, frc);
return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(),
tl.getAscent() + tl.getDescent() +
tl.getLeading());
}
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
public FontMetrics getFontMetrics(Font font) {
if ((this.fontMetrics != null) && (font == this.font)) {
return this.fontMetrics;
}
FontMetrics fm =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
if (this.font == font) {
this.fontMetrics = fm;
}
return fm;
}
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}