This is a discussion on Retrieving Font Metrics within the Programming forums, part of the Tutorials category; Retrieving Font Metrics Other than creating and rendering fonts, you can find out a lot about a particular font through ...
Retrieving Font Metrics
Other than creating and rendering fonts, you can find out a lot about a particular font through font metrics. This contains a font’s ascent (the typical maximum height of individual letters, such as capital letters), descent (the typical distance letters may dip below the baseline, such as j, g, and y), and the bounding rectangle which includes a string of text.
Possibly one of the most interesting and useful font metrics you can query is a text string’s bounding rectangle. A string’s bounding rectangle is simply the smallest rectangle that still includes a specified string. To recover the bounding rectangle of your text, you will need a FontRenderContext, a TextLayout, and a Rectangle2D object.
A FontRenderContext object includes information on how text is measured. Rather than creating instances of this class yourself, allow your Graphics2D class to do it for you. This way, you are guaranteed that the FontRenderContext will properly represent text information on a particular system. To get the context for font rendering, simply call the getFontRenderContext method from the Graphics2D class, as follows:
// assume g2d refers to a valid Graphics2D object
FontRenderContext frc = g2d.getFontRenderContext();
Now to get the actual metrics for your font, use a TextLayout object. The TextLayout class gives the actual font metrics information for your text. You can create a useful TextLayout object by specifying a Font and FontRenderContext, as well as the text string whose capabilities you are interested in, in your constructor method:
// assume font refers to a valid Font object
TextLayout layout = new TextLayout("TextLayouts are your friend", font, frc);
Now your TextLayout object includes your font metrics information regarding the above string, font, and render context. You can now directly obtain metric information from your layout. Again, since you are interested in the bounding rectangle, you can call the getBounds method from your TextLayout, like the following:
Rectangle2D textBounds = layout.getBounds();
Note that the position of the bounding rectangle of a text string is based relative to the TextLayout and not any screen position. Therefore, if you wish to draw the bounding rectangle around your text, you must translate the rectangle to the same position as your text string.
The following applet, FontBoundsTest, renders a sample String object along with its bounding rectangle:
import java.applet.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
public class FontBoundsTest extends Applet
{
private final String MESSAGE = "Trapped!";
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
// create a Font Object.
Font baseFont = new Font("Helvetica", Font.PLAIN, 50);
// get the FontRenderContext for the Graphics2D context
FontRenderContext frc = g2d.getFontRenderContext();
// get the layout of our message and font, using the above
// FontRenderContext
TextLayout layout = new TextLayout(MESSAGE, baseFont, frc);
// get the bounds of the layout
Rectangle2D textBounds = layout.getBounds();
// draw the message and the bounding rectangle at (45, 50)
g2d.setFont(baseFont);
g2d.setPaint(Color.BLACK);
g2d.drawString(MESSAGE, 45, 50);
g2d.translate(45, 50);
g2d.setPaint(Color.RED);
g2d.draw(textBounds);
}
} // FontBoundsTest
Alternatively, you can also render the contents of your TextLayout object directly to your Graphics2D context. Depending on the condition, drawing text in this manner may prove useful. You can render text directly to your Graphics2D context like the following:
layout.draw(g2d, 45, 50);
There is a lot more you can do with fonts, such as creating editable text, highlighting selections, and performing hit testing on text characters.
Bookmarks