Text Metrics in the .Net Framework, Part I (
Page 1 of 3 )
Some applications require precise placement of text in screen display and printed output. The .Net Framework's tools in this area are far from complete, but knowing what's available will help you get the best possible output.
I think that the tools that the .Net Framework provides for text output are truly impressive. Even so, the precise positioning of text remains problematic. This is the first of two articles that explore tools and techniques for positioning text output. Let's start with some background.
Font Families and Font Metrics
ADVERTISEMENT
In the .Net framework (technically, in GDI+), font families are used to group fonts that have the same typeface but different styles. The styles are regular, bold, italic, and bold italic (and, in some families, strikeout). Thus, the font family Arial contains the four font styles Arial Regular, Arial Bold, Arial Italics, and Arial Bold Italics. Unfortunately you cannot easily tell what font families are on your system by looking at the Fonts folder in Control panel. You can, however, easily list them with the following code (which lists all available font families in a ListBox):
Dim ff As FontFamily
For Each ff In FontFamily.Families
ListBox1.Items.Add(ff.Name)
Next
In .Net, a font family is represented by the FontFamily class, instantiated as follows:
Dim ff As New FontFamily(FontFamilyName)
One important aspect of a font family is that it has no inherent size. This makes sense, of course, because a font such as Arial can be displayed at almost any size. A font family does, however, have metrics that describe certain aspects of the typeface that are independent of the eventual display size. Unfortunately, .Net does not provide access to all font metrics, just a few important ones. When getting these metrics you must specify the style as a member of the FontStyle enumeration: Regular, Bold, Italic, Strikeout, and Underline (combining two or more styles with Or, if needed).
The FontFamily.GetEmHeight(style) gets the height of the em square for the font, expressed in design units. The em square is the design grid that was used to design the typeface, and the design units are purely arbitrary. For almost all Windows fonts, the em height is 2048 because that is the height of the design grid used to design the TrueType or OpenType fonts that Windows uses.
The GetCellAscent(style) and GetCellDescent(style) methods return the maximum distance that the characters extend above and below the baseline, and the GetLineSpacing(style) method returns the recommended line spacing. These are illustrated in Figure 1. Again, the returned values are in design units.
You are probably asking "what good does any of this do me?" So far, all we have is some information about the internal design of the font, when what we want is information that will help us to precisely display text on the screen and in printed output. Be patient, I'm getting to that!
Fonts and Display Units
When you are going to display text, you will create a Font object that defines not only the typeface but also the size and style of the display font. Unlike a FontFamily object, therefore, a Font object has a definite size associated with it. You can specify a font size in these units, members of the GraphicsUnit enumeration:
Point (1/72 inch)
Pixel
Inch
Millimeter
Document (1/300 inch)
For example, this line of code creates a font that uses the Arial typeface and is 1/2 inch tall.
Dim ff As FontFamily = New FontFamily("Arial")
Dim f As New Font(ff, 150, FontStyle.Regular, GraphicsUnit.Document)
Note that I use the GraphicsUnit.Document unit of measurement. Because this offers the finest increment, 1/300 inch, I prefer it when I am working with precise text placement. While not required, I think it's a good idea to set your Graphics object to the same measurement units. Assuming that g is a reference to the Graphics object that will be used to render the text:
g.PageUnit = GraphicsUnit.Document
Now that we have a specific font in a specific size, we can start looking at techniques for precise placement.