下面列出了java.awt.FontMetrics#getWidths ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a new text drawer.
*
*/
public LWJGLTextDrawer(LWJGL15DrawContext drawContext) {
this.drawContext = drawContext;
font = new Font(FONTNAME, Font.PLAIN, TEXTURE_GENERATION_SIZE);
BufferedImage tmp_bi = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
Graphics tmp_graph = tmp_bi.getGraphics();
tmp_graph.setFont(font);
FontMetrics fm = tmp_graph.getFontMetrics();
char_widths = fm.getWidths();
gentex_line_height = fm.getHeight();
tmp_graph.dispose();
if(char_widths.length != 256) {
throw new IndexOutOfBoundsException("we only support 256 characters (256!="+char_widths.length);
}
generateTexture();
generateGeometry(fm.getDescent());
}
/**
* Return true if this font is fixed width.
* Only the first 256 characters are considered.
* @param font
* @return true if this font is fixed width.
*/
private static boolean isFixedWidth(Component context, Font font) {
FontMetrics metrics = context.getFontMetrics(font);
int[] widths = metrics.getWidths();
int Swidth = widths[0];
for (int cx = 1; cx < widths.length; cx++) {
int width = widths[cx];
if (width == 0) {
continue;
} else if (Swidth != width) {
return false;
}
}
return true;
}
/** Get the width and height of single-line text. */
static public final Dimension getDimensions(final String text, final Font font) {
if (null == frame) { frame = new java.awt.Frame(); frame.pack(); frame.setBackground(Color.white); } // emulating the ImageProcessor class
final FontMetrics fm = frame.getFontMetrics(font);
final int[] w = fm.getWidths(); // the advance widths of the first 256 chars
int width = 0;
for (int i = text.length() -1; i>-1; i--) {
final int c = (int)text.charAt(i);
if (c < 256) width += w[c];
}
return new Dimension(width, fm.getHeight());
}
public static void main(String[] args) throws Exception {
GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts();
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RGB);
for (AntialiasHint antialiasHint : antialiasHints) {
for (Font f : fonts) {
for (StyleAndSize styleAndSize : stylesAndSizes) {
f = f.deriveFont(styleAndSize.style, styleAndSize.size);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
antialiasHint.getHint());
FontMetrics fm = g2d.getFontMetrics(f);
int[] width;
int maxWidth = -1;
int maxAdvance = fm.getMaxAdvance();
if (debug) {
System.out.println("Testing " + f + " in " +
antialiasHint);
System.out.println("getMaxAdvance: " + maxAdvance);
}
if (maxAdvance != -1) {
String failureMessage = null;
width = fm.getWidths();
for (int j = 0; j < width.length; j++) {
if (width[j] > maxWidth) {
maxWidth = width[j];
}
if (width[j] > maxAdvance) {
failureMessage = "FAILED: getMaxAdvance is " +
"not max for font: " +
f.toString() +
" getMaxAdvance(): " +
maxAdvance +
" getWidths()[" + j + "]: " +
width[j];
throw new Exception(failureMessage);
}
}
}
if (debug) {
System.out.println("Max char width: " + maxWidth);
System.out.println("PASSED");
System.out.println(".........................");
}
}
}
}
System.out.println("TEST PASS - OK");
}
public static void main(String[] args) throws Exception {
GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts();
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RGB);
for (AntialiasHint antialiasHint : antialiasHints) {
for (Font f : fonts) {
for (StyleAndSize styleAndSize : stylesAndSizes) {
f = f.deriveFont(styleAndSize.style, styleAndSize.size);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
antialiasHint.getHint());
FontMetrics fm = g2d.getFontMetrics(f);
int[] width;
int maxWidth = -1;
int maxAdvance = fm.getMaxAdvance();
if (debug) {
System.out.println("Testing " + f + " in " +
antialiasHint);
System.out.println("getMaxAdvance: " + maxAdvance);
}
if (maxAdvance != -1) {
String failureMessage = null;
width = fm.getWidths();
for (int j = 0; j < width.length; j++) {
if (width[j] > maxWidth) {
maxWidth = width[j];
}
if (width[j] > maxAdvance) {
failureMessage = "FAILED: getMaxAdvance is " +
"not max for font: " +
f.toString() +
" getMaxAdvance(): " +
maxAdvance +
" getWidths()[" + j + "]: " +
width[j];
throw new Exception(failureMessage);
}
}
}
if (debug) {
System.out.println("Max char width: " + maxWidth);
System.out.println("PASSED");
System.out.println(".........................");
}
}
}
}
System.out.println("TEST PASS - OK");
}
public static void main(String[] args) throws Exception {
GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts();
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RGB);
for (AntialiasHint antialiasHint : antialiasHints) {
for (Font f : fonts) {
for (StyleAndSize styleAndSize : stylesAndSizes) {
f = f.deriveFont(styleAndSize.style, styleAndSize.size);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
antialiasHint.getHint());
FontMetrics fm = g2d.getFontMetrics(f);
int[] width;
int maxWidth = -1;
int maxAdvance = fm.getMaxAdvance();
if (debug) {
System.out.println("Testing " + f + " in " +
antialiasHint);
System.out.println("getMaxAdvance: " + maxAdvance);
}
if (maxAdvance != -1) {
String failureMessage = null;
width = fm.getWidths();
for (int j = 0; j < width.length; j++) {
if (width[j] > maxWidth) {
maxWidth = width[j];
}
if (width[j] > maxAdvance) {
failureMessage = "FAILED: getMaxAdvance is " +
"not max for font: " +
f.toString() +
" getMaxAdvance(): " +
maxAdvance +
" getWidths()[" + j + "]: " +
width[j];
throw new Exception(failureMessage);
}
}
}
if (debug) {
System.out.println("Max char width: " + maxWidth);
System.out.println("PASSED");
System.out.println(".........................");
}
}
}
}
System.out.println("TEST PASS - OK");
}