INFO: Retrieving Font Styles Using EnumFontFamilies()

ID: Q84131


The information in this article applies to:


SUMMARY

Windows version 3.1 introduces the concept of a font style. In previous versions of Windows, a font could have the bold, italic, underline, and strikeout properties, which were supported by respective members in the LOGFONT and TEXTMETRIC structures. Windows 3.1 also supports these properties, as well as a style name for TrueType fonts. The article describes how to obtain the font style name during font enumeration, using the EnumFontFamilies function. For more information about obtaining style information without enumerating the fonts, query on the following words in the Microsoft Knowledge Base:

prod(winsdk) and getoutlinetextmetrics


MORE INFORMATION

In Windows 3.1, "style" refers to the weight and slant of a font. Windows supports a wide range of weights in the lfWeight member of the LOGFONT structure. (Two examples of weights are FW_BOLD, which is defined as 700, and FW_THIN, which is defined as 100). Very few applications, however, use any weights other than FW_BOLD and FW_DONTCARE (defined as 0).

Windows 3.1 builds on the support presently in Windows for these variations in weight and slant. Style names are NOT used in the LOGFONT structure except when the fonts are enumerated with EnumFontFamilies.

The ChooseFont dialog box in the common dialog boxes Dynamic-Link Library (COMMDLG.DLL) demonstrates how style names are used. The ChooseFont dialog box has two list boxes: Font and Font Style. The Font list box lists the face names for all fonts installed and the Font Style list box lists the font styles for the currently selected face. For example, if any non-TrueType font (such as MS Sans Serif) is selected, the following styles appear in the Font Style list box:


   Regular
   Bold
   Italic
   Bold Italic 
TrueType fonts may have these or more elaborate styles. For example, the "Lucida Sans" face includes the following style names:

   Regular
   Italic
   Demibold Roman
   Demibold Italic 
In the case of Lucida Sans with the style of Demibold Roman or Demibold Italic, the lfWeight value is 600 (FW_DEMIBOLD).

In Windows 3.1, the EnumFontFamilies function can be used to obtain the style name of a font during font enumeration. The EnumFontFamilies function works in a manner very similar to the Windows 3.0 EnumFonts function.

The EnumFontFamilies function is prototyped as follows:

   int EnumFontFamilies(HDC hdc, LPCSTR lpszFamily,
                        FONTENUMPROC lpfnEnumProc, LPARAM lpData) 
The lpszFamily parameter points to a null-terminated string that specifies the family name (or typeface name) of the desired fonts. If this parameter is NULL, EnumFontFamilies selects and enumerates one font of each available font family. For example, to enumerate all fonts in the "Arial" family, lpszFamily points to a string buffer containing "Arial."

The following table illustrates the meanings of the terms, "typeface name," "font name," and "font style:"

   Typeface Name  Font Name                    Font Style
   -------------  ---------                    ----------
   Arial          Arial                        Regular
                  Arial Bold                   Bold
                  Arial Italic                 Italic
                  Arial Bold Italic            Bold Italic

   Courier New    Courier New                  Regular
                  Courier New Bold             Bold
                  Courier New Italic           Italic
                  Courier New Bold Italic      Bold Italic

   Lucida Sans    Lucida Sans                  Regular
                  Lucida Sans Italic           Italic
                  Lucida Sans Demibold Roman   Demibold Roman
                  Lucida Sans Demibold Italic  Demibold Italic

   MS Sans Serif  MS Sans Serif                Regular
                  MS Sans Serif                Bold
                  MS Sans Serif                Italic
                  MS Sans Serif                Bold Italic 
The first three typefaces in the above table are TrueType faces, the remaining typeface is MS Sans Serif. The typeface name is also sometimes referred to as the family name.

When dealing with non-TrueType fonts, typeface name and font name are the same. However, it is important to recognize the distinction when dealing with a TrueType font.

For example, CreateFont takes a pointer to a string containing the typeface name of the font to create. It is not valid to use Arial Bold as this string because Arial is a TrueType font and Arial Bold is a font name, not a typeface name.

If EnumFontFamilies is called with the lpszFamily parameter pointing to a valid TrueType typeface name, the callback function, which is specified in fntenmprc, will be called once for each font name for that typeface name. For example, if EnumFontFamilies is called with lpszFamily pointing to Lucida Sans, the callback function will be called four times; once for each font name.

If the lpszFamily parameter points to the typeface name of a non- TrueType font, such as MS Sans Serif, the callback will be called once for each face size supported by the font. The number of face sizes supported by the font can vary from font to font and from device to device. Note that the callback is called for different sizes, not for different styles. This behavior is identical to that found using the EnumFonts function.

Remember that, because TrueType fonts are continuously scalable, there is no reason for the callback function to be called for each size. If the callback function was called for each size that a TrueType font supported, the callback function would be called an infinite number of times!

The EnumFontFamilies callback function is prototyped as follows:

   int CALLBACK EnumFontFamProc(LPNEWLOGFONT lpnlf,
                             LPNEWTEXTMETRIC lpntm,
                             int FontType, LPARAM lpData) 
The lpnlf parameter points to a LOGFONT structure that contains information about the logical attributes of the font. If the typeface being enumerated is a TrueType font [(nFontType | TRUETYPE_FONTTYPE) is TRUE], this LOGFONT structure will have two additional members appended to the end of the structure, as follows:

   char   lfFullName[LF_FACESIZE*2];
   char   lfStyleName[LF_FACESIZE]; 
It is important to remember that these two additional fields are used *only* during enumeration with EnumFontFamilies and nowhere else in Windows. The documentation for the EnumFontFamilies function on pages 266-268 of the "Microsoft Windows Software Development Kit: Programmer's Reference, Volume 2: Functions" manual refers to the NEWLOGFONT structure which contains the additional members listed above. However, the NEWLOGFONT structure is not defined in the WINDOWS.H header file. To address this situation, use the ENUMLOGFONT structure which is defined in the WINDOWS.H file but is not listed in the Windows SDK documentation.

To retrieve the style name and full name of a font without using enumeration, use the GetOutlineTextMetrics function.

Additional query words: 3.10 3.50 4.00 win16sdk


Keywords          : kbNTOS350 kbNTOS351 kbNTOS400 kbSDKWin32 kbWinOS95 
Version           : WINDOWS:3.1,95; winnt:3.5,3.51,4.0
Platform          : WINDOWS winnt 
Issue type        : kbinfo 

Last Reviewed: June 22, 1999