本文整理匯總了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;
}