18.4.5 Retrieving Information About the Selected Font

Applications can retrieve font information from a device context by using the GetTextMetrics, GetTextFace, and GetOutlineTextMetrics functions.

The GetTextMetrics function copies a TEXTMETRIC structure into a buffer. The TEXTMETRIC structure contains a description of the physical font, including the average dimensions of the character cells within the font, the spacing between lines of text, the number of characters in the font, and the character set on which the font is based. An application working with TrueType fonts can call the GetOutlineTextMetrics function to retrieve information in an OUTLINETEXTMETRIC structure.

Applications often use the TEXTMETRIC structure to determine how much space to specify between lines of text. For example, to compute an appropriate value for single-line spacing, an application could add the values of the tmHeight and tmExternalLeading members. The tmHeight member specifies the height of each character cell, and tmExternalLeading specifies the font designer's recommended spacing between the bottom of one character cell and the top of the next. (More accurate information can be retrieved for TrueType fonts from the OUTLINETEXTMETRIC structure; in this case, applications can add the values of the otmAscent, otmDescent, and otmLineGap members.) The following example writes several lines of single-spaced text:

TEXTMETRIC tm;
int LineSpacing, i, YIncrement;

GetTextMetrics(hdc, &tm);
LineSpacing = tm.tmHeight + tm.tmExternalLeading;





YIncrement = 50;
for (i = 0; i < 4; i++) {
    TextOut(hdc, 10, YIncrement, "Single-line spacing", 19);
    YIncrement += LineSpacing;
}

The GetTextFace function copies a name identifying the typeface of the selected font into a buffer. An application can use this information in dialog boxes and menus.