18.2.4 Text and Character Attributes

Character attributes are such features as whether a character is bold or italic and whether it has serifs. Text attributes are such features as line and character spacing and text justification. This section introduces some of these attribute categories. For descriptions of individual attributes, see the descriptions of the LOGFONT, NEWTEXTMETRIC, TEXTMETRIC, and OUTLINETEXTMETRIC structures in the Microsoft Windows Programmer's Reference, Volume 3.

18.2.4.1 Line and Character Spacing

Before the introduction of TrueType fonts, it was difficult for an application to position characters exactly, especially if the characters were in a string that included bold or italic text. Instead of the width of the character glyph, most Windows functions use the advance width of characters, which includes space on either side of the glyph, as in the following figure:

Applications can control the spacing of TrueType characters accurately by using ABC character spacing. GDI constructs ABC spacing from information provided by the TrueType rasterizer. The “A” spacing is the width to add to the current position before placing the glyph. The “B” spacing is the width of the glyph itself. The “C” spacing is the white space to the right of the glyph. The total advance width is given by A+B+C.

Because either or both of the A and C increments can be negative, characters can overhang or underhang the character cell in a way that was not previously possible with GDI. For example, in the following figure the A, B, and C increments for the letter “g” are all positive, but the A and C increments for the letter “f” are negative.

An application can use the GetCharABCWidths function to retrieve the ABC spacing for characters in a TrueType font.

When an application using TrueType fonts calls a text-output function, GDI uses the font's complete set of ABC widths to provide character-placement information to the device driver.

Some applications determine the line spacing between text lines of different sizes by using a font's maximum ascender and descender. An application can retrieve these values by calling the GetTextMetrics function and then checking the tmAscent and tmDescent members of the NEWTEXTMETRIC structure.

The maximum ascent and descent are different from the typographic ascent and descent; in TrueType fonts, the typographic ascent and descent are typically the top of the “f” glyph and bottom of the “g” glyph. Rounded characters typically extend slightly beyond the limits of characters with straight edges, to overcome an optical illusion that would make them appear too small otherwise. An application can retrieve the typographic ascender and descender for a TrueType font by calling the GetOutlineTextMetrics function and checking the values in the otmAscent and otmDescent members of the OUTLINETEXTMETRIC structure.

Applications that use the HPPCL5A printer driver may experience problems with line spacing for the scalable fonts that are built into the HP LaserJet III printer. These fonts use external leading in the place of internal leading; accent marks for capital letters print outside the character cell reported by the tmHeight member of the NEWTEXTMETRIC structure.

TrueType font metrics do not correspond exactly to the metrics for Windows raster fonts, because TrueType font metrics have been designed by Apple Computer, Inc. TrueType metrics are required for any application that produces a document that is portable between Windows and an Apple Macintosh computer.

The following figure shows the difference between the vertical text metric values returned in the NEWTEXTMETRIC and OUTLINETEXTMETRIC structures. (The names beginning with “otm” are members of the OUTLINETEXTMETRIC structure.)

The overhang added by GDI when it synthesizes a bold or italic font is not taken into account by the GetTextExtent function. For more information about taking the overhang into account in character spacing, see Section 18.4.7.3, “Using Multiple Fonts in a Line.”

18.2.4.2 Logical and Physical Inches

A logical inch is a measure Windows uses for presenting legible fonts on the screen; it is generally 30 to 40 percent larger than a physical inch. A 10-point font on a screen is larger than a 10-point font produced by a printer. Fonts on the screen are made larger because most screens do not have high enough resolutions to make a 10-point font legible. Furthermore, users generally read text on screens from a greater distance than they read text on paper.

Although logical inches solve the problem of legible fonts on the screen, they prevent a perfect match between the output of the screen and printer. The text on a screen is not simply a scaled version of the text that will appear on the page, particularly if graphics are incorporated into the text.

An application can retrieve the physical dimensions of a font by calling the GetOutlineTextMetrics function. To determine the dimensions of an output device, an application can call the GetDeviceCaps function. GetDeviceCaps returns both physical and logical dimensions.

18.2.4.3 Font Sizes

Most Windows applications use the MM_TEXT mapping mode instead of MM_TWIPS, because MM_TEXT makes possible a relatively simple conversion from logical to physical font sizes. With MM_TEXT, each logical unit is mapped to one pixel.

To determine the point size for a font, an application must first convert the information returned in the NEWTEXTMETRIC or OUTLINETEXTMETRIC structure using the size of the logical inch for the output device. For example, an application using MM_TEXT units might use a font that has a cell height (tmHeight) of 12 and an internal leading (tmInternalLeading) of 2. The cell height minus the internal leading gives the point size in logical units; in this case, the point size of the font is 10 units (pixels).

To convert this value into a typographic point size (that is, a value in which one point equals 1/72 inch), the application should use the GetDeviceCaps function to determine the vertical size and resolution of the screen and the number of pixels per logical inch supported by that device. For example, if an application working in MM_TEXT mapping mode requires a 12-point font, it could use the value produced by the following algorithm in the lfHeight member of the LOGFONT structure:

–1 * ((LOGPIXELSY * 12) / 72)

Using a negative value in the lfHeight member causes GDI to use the value as the height of the character glyphs, not the height of the character cell. The LOGPIXELSY value is returned by a call to the GetDeviceCaps function. The point size of the requested font is 12, and the number of points in a physical inch is 72.

Similarly, an application could use the following algorithm to determine the point size of a font from information returned in the NEWTEXTMETRIC structure:

((tmHeighttmInternalLeading) * 72) / LOGPIXELSY

For more information about setting a point size, see Section 18.4.4, “Creating a Logical Font.” For more information about querying a point size, see Section 18.4.5, “Retrieving Information About the Selected Font.”