Getting Information About a Logical Font

You can retrieve information about a font from the font handle by using the GetObject function. The GetObject function copies logical-font information, such as the height, width, weight, and character set, to a structure that you supply. You can use the logical-font information to see if the given font meets your needs. GetObject is often used after creating a font with the CreateFont function to see how closely the font matches the requested font. In the following example, GetObject retrieves logical-font information for a newly created font and compares the character-set values and facenames:

HFONT hFont;

LOGFONT LogFont;

.

.

.

hFont = CreateFont(

10, /* Height */

10, /* Width */

0, /* Escapement */

0, /* Orientation */

FW_NORMAL, /* Weight */

FALSE, /* Italic */

FALSE, /* Underline */

FALSE, /* StrikeOut */

OEM_CHARSET, /* CharSet */

OUT_DEFAULT_PRECIS, /* OutPrecision */

CLIP_DEFAULT_PRECIS, /* ClipPrecision */

DEFAULT_QUALITY, /* Quality */

FIXED_PITCH | FF_MODERN, /* PitchAndFamily */

“Courier”, /* Typeface */

);

GetObject(hFont, sizeof(LogFont), (LPSTR) &LogFont);

if (LogFont.lfCharSet != OEM_CHARSET) {

.

.

.

}

if (strcmp(LogFont.lfFaceName, “Courier”)) {

.

.

.

}

The font that GDI uses when you actually select a font by using the SelectObject function may vary widely from system to system. The selected font, which depends on the fonts available at the time of the selection, may or may not closely match your request. The only way to guarantee a request is to determine which fonts are actually available and request only those fonts, or add the appropriate font resource to the system font table before making the request, or change the method the font mapper uses to choose a font.