本文整理汇总了Java中java.awt.font.TextLayout.getPixelBounds方法的典型用法代码示例。如果您正苦于以下问题:Java TextLayout.getPixelBounds方法的具体用法?Java TextLayout.getPixelBounds怎么用?Java TextLayout.getPixelBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.font.TextLayout
的用法示例。
在下文中一共展示了TextLayout.getPixelBounds方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: measureText
import java.awt.font.TextLayout; //导入方法依赖的package包/类
static void measureText() {
Font font = new Font(FONT, Font.PLAIN, 36);
if (!font.getFamily(Locale.ENGLISH).equals(FONT)) {
return;
}
FontRenderContext frc = new FontRenderContext(null, false, false);
TextLayout tl1 = new TextLayout(STR1, font, frc);
TextLayout tl2 = new TextLayout(STR2, font, frc);
Rectangle r1 = tl1.getPixelBounds(frc, 0f, 0f);
Rectangle r2 = tl2.getPixelBounds(frc, 0f, 0f);
if (r1.height > r2.height) {
System.out.println(font);
System.out.println(r1);
System.out.println(r2);
throw new RuntimeException("BAD BOUNDS");
}
}
示例2: getWidth
import java.awt.font.TextLayout; //导入方法依赖的package包/类
/**
* Compute a most appropriate width of the given text layout.
*/
public static float getWidth(TextLayout textLayout, String textLayoutText, Font font) {
// For italic fonts the textLayout.getAdvance() includes some extra horizontal space.
// On the other hand index2X() for TL.getCharacterCount() is width along baseline
// so when TL ends with e.g. 'd' char the end of 'd' char is cut off.
float width;
int tlLen = textLayoutText.length();
if (!font.isItalic() ||
tlLen == 0 ||
Character.isWhitespace(textLayoutText.charAt(tlLen - 1)) ||
Bidi.requiresBidi(textLayoutText.toCharArray(), 0, textLayoutText.length()))
{
width = textLayout.getAdvance();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("TLUtils.getWidth(\"" + CharSequenceUtilities.debugText(textLayoutText) + // NOI18N
"\"): Using TL.getAdvance()=" + width + // NOI18N
// textLayoutDump(textLayout) +
'\n');
}
} else {
// Compute pixel bounds (with frc being null - means use textLayout's frc; and with default bounds)
Rectangle pixelBounds = textLayout.getPixelBounds(null, 0, 0);
width = (float) pixelBounds.getMaxX();
// On Mac OS X with retina displays the TL.getPixelBounds() give incorrect results. Luckily
// TL.getAdvance() gives a correct result in that case.
// Therefore use a minimum of both values (on all platforms).
float tlAdvance = textLayout.getAdvance();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("TLUtils.getWidth(\"" + CharSequenceUtilities.debugText(textLayoutText) + // NOI18N
"\"): Using minimum of TL.getPixelBounds().getMaxX()=" + width + // NOI18N
" or TL.getAdvance()=" + tlAdvance +
textLayoutDump(textLayout) +
'\n');
}
width = Math.min(width, tlAdvance);
}
// For RTL text the hit-info of the first char is above the hit-info of ending char.
// However textLayout.isLeftToRight() returns true in case of mixture of LTR and RTL text
// in a single textLayout.
// Ceil the width to avoid rendering artifacts.
width = (float) Math.ceil(width);
return width;
}