Creating a Logical Font

A logical font is a list of font attributes, such as height, width, character set, and typeface, that you want GDI to consider when choosing a font for writing text. You can create a logical font by using the CreateFont function. CreateFont returns a handle to the logical font. You can use this handle in the SelectObject function to select the font for the device context. When you select a logical font, GDI chooses a physical font, based on your request, to write subsequent text. GDI attempts to choose a physical font that matches your logical font exactly, but if it cannot find an exact match in its internal pool of fonts, it chooses the closest matching font.

In the following example, the CreateFont function creates a logical font:

hFont = CreateFont(

10, /* lfHeight */

8, /* lfWidth */

0, /* lfEscapement */

0, /* lfOrientation */

FW_NORMAL, /* lfWeight */

FALSE, /* lfItalic */

FALSE, /* lfUnderline */

FALSE, /* lfStrikeOut */

ANSI_CHARSET, /* lfCharSet */

OUT_DEFAULT_PRECIS, /* lfOutPrecision */

CLIP_DEFAULT_PRECIS, /* lfClipPrecision */

DEFAULT_QUALITY, /* lfQuality */

FIXED_PITCH | FF_MODERN, /* lfPitchAndFamily */

“System” /* lfFaceName */

);

This logical font asks for a fixed-pitch font in which each character is 10 pixels high and 8 pixels wide. Font dimensions are always described in pixels. The requested escapement and orientation are zero, which means the baseline along which the characters are displayed is horizontal and none of the characters will be rotated. FW_NORMAL is the requested weight. Other typical weights are FW_BOLD (for darker, heavier characters) and FW_LIGHT (for lighter characters). Italic, underlined, or strikethrough characters are not desired in this example. The requested character set is ANSI, the standard character set of Windows. Default output precision, clipping precision, and quality are requested. These attributes affect the way the characters are displayed. Setting these attributes to default values lets the display device take advantage of its own capabilities to display characters. The requested font family is FF_MODERN. The font name is System.

When you supply a logical font to SelectObject, the function examines the pool of available fonts to find a font that satisfies the requested attributes. If it finds an exact match, it returns a handle to that font. If it fails to find an exact match, it chooses the closest possible font and returns that handle. In some cases, SelectObject might not find an exact match but nevertheless can synthesize the requested font by using an existing font that is close. For example, if the only available system font were 5 pixels high and your logical font specified a height of 10 pixels, SelectObject could synthesize the requested font by doubling the height. In such cases, SelectObject returns the synthesized font for writing text.