The Logical Font Structure

As I mentioned, the best way to create a logical font is to first define a structure of type LOGFONT:

LOGFONT logfont ;

When you call CreateFontIndirect, you give Windows a pointer to this structure:

hFont = CreateFontIndirect (&logfont) ;

The 14 fields of this structure are the same as the 14 parameters to CreateFont.

You don't need to set each and every field of the LOGFONT structure. If your logical font structure is defined as a static variable, it will be initialized to 0. The 0 values are defaults. You can use that structure directly without any changes, and CreateFontIndirect will return a handle to a font. When you select that font into the device context, you'll get a reasonable default font. You can be as specific or as vague as you want in the LOGFONT structure, and Windows will attempt to match your requests with a real font.

The first two fields of the LOGFONT structure are in logical units, so they depend on the current setting of the mapping mode:

lfHeight (short integer)—This is the desired height of the characters (including internal leading but not external leading) in logical units. Because the point size of the font itself is the height of the font less internal leading, you're really specifying a line spacing here. You can set it to 0 for a default size. If you set lfHeight to a negative number, Windows treats the absolute value of that number as a desired ascent size rather than as a full height.

lfWidth (short integer)—This is the desired width of the characters in logical units. In most cases you'll want to set this to 0 and let Windows choose a font based on the height. If you use a nonzero value, Windows might be forced to use a font designed for an aspect ratio different from that of the device context into which you later select the font.

The next two fields specify the ”escapement“ and ”orientation“ of the text. In theory, lfEscapement allows character strings to be written at an angle, and lfOrientation allows characters to be tilted. These two fields are not included in the PICKFONT program, however, because they currently don't work well on the screen. Before you try to use these on a device, you should use the TEXTCAPS index to GetDeviceCaps to check the device's ability to do character rotation.

lfEscapement (short integer)—This is an angle in tenths of a degree, measured from the horizontal in a counterclockwise direction. It specifies the placement of the string when you write text with TextOut. Here are some examples:

Value Placement of Characters0
900 Go up
1800 Run from right to left
2700 Go down

lfOrientation (short integer)—This is an angle in tenths of a degree, measured from the horizontal in a counterclockwise direction. It specifies the appearance of each character. Here are some examples:

Value Character Appearance0
900 Tipped 90 degrees to the left
1800 Upside down
2700 Tipped 90 degrees to the right

The remaining 10 fields follow:

lfWeight (short integer)—This field allows you to specify boldface. Currently, there are only two recommended values:

Value Result400
700 Boldface

In actuality, any value from 0 to 550 is normal, and any value greater than 550 is boldface. If you like to plan for the future, WINDOWS.H has a collection of font weight identifiers:

Value Identifier0
100 FW_THIN
200 FW_EXTRALIGHT or FW_ULTRALIGHT
300 FW_LIGHT
400 FW_NORMAL or FW_REGULAR
500 FW_MEDIUM
600 FW_SEMIBOLD or FW_DEMIBOLD
700 FW_BOLD
800 FW_EXTRABOLD or FW_ULTRABOLD
900 FW_HEAVY or FW_BLACK

lfItalic (BYTE)—When nonzero, this specifies italics. Windows can synthesize italics on GDI fonts. To determine what a particular device can do with a device font, check the TC_IA_ABLE bit of the TEXTCAPS value returned from GetDeviceCaps.

lfUnderline (BYTE)—When nonzero, this specifies underlining, which is synthesized on GDI fonts. For device fonts, check the TC_UA_ABLE bit from GetDeviceCaps.

lfStrikeOut (BYTE)—When nonzero, this specifies that the font should have a line drawn through the characters. This also is synthesized on GDI fonts. For device fonts, check the TC_SO_ABLE bit.

lfCharSet (BYTE)—This is the character set of the font. WINDOWS.H currently contains three identifiers for the character set:

Value Identifier0
2 SYMBOL_CHARSET
128 SHIFTJIS_CHARSET (Japanese Kanji)
255 OEM_CHARSET

Note: The Kanji character sets are not, of course, included with American or Western European releases of Windows.

lfOutPrecision (BYTE)—This specifies how Windows should attempt to match the desired font sizes and characteristics with actual fonts. This field is not yet implemented and is not included in the PICKFONT program. WINDOWS.H contains four identifiers for the field:

Value Identifier0
1 OUT_STRING_PRECIS
2 OUT_CHARACTER_PRECIS
3 OUT_STROKE_PRECIS

lfClipPrecision (BYTE)—This specifies how to clip characters that are partly outside the clipping region. The field is not included in the PICKFONTS program. WINDOWS.H contains three identifiers:

Value Identifier0
1 CLIP_CHARACTER_PRECIS
2 CLIP_STROKE_PRECIS

lfQuality (BYTE)—This is actually an instruction to Windows regarding the matching of a desired font with a real font. You can use three identifiers:

Value Identifier0
1 DRAFT_QUALITY
2 PROOF_QUALITY

If you specify PROOF_QUALITY, you're telling Windows that you don't want a font to be increased to a larger size to match the character height or width that you request. The PROOF_QUALITY fonts are the most attractive, but they may be smaller than what you request.

lfPitchAndFamily (BYTE)—This byte is composed of two parts. You can use the C OR operator to combine two identifiers for this field. The lowest two bits specify the pitch of the font:

Value Identifier0
1 FIXED_PITCH
2 VARIABLE_PITCH

If you specify FIXED_PITCH, Windows will pick a font that has a fixed pitch, because you're essentially telling Windows that your program can't deal with variable-pitch fonts.

The upper half of this byte specifies the font family:

Value Identifier0x00
0x10 FF_ROMAN
0x20 FF_SWISS
0x30 FF_MODERN
0x40 FF_SCRIPT
0x50 FF_DECORATIVE

lfFaceName (BYTE array)—This is the name of a typeface (such as Courier, Helv, or Tms Rmn). WINDOWS.H includes a LF_FACESIZE identifier that equals 32, which is the maximum number of characters allowed for the typeface name.