You can find out which fonts are available for a given device by using the EnumFonts function. This function sends information about the available fonts to a callback function that you supply. The callback function receives both logical-font and text-metric information. From this information you can determine which fonts you want to use and create appropriate font handles for them. If you create font handles by using the supplied information, you are guaranteed to get an exact match for the font when you select it for writing text.
The EnumFonts function usually provides font information about all the fonts that have a specific typeface name. You can supply the name when you call EnumFonts. If you do not supply a name, EnumFonts supplies information about arbitrarily selected fonts, each representing a typeface currently available. The way to examine all available fonts is to get a list of the available typefaces, then examine each font in each typeface.
The following example shows how to use EnumFonts to find out how many fonts having the Courier typeface are available. The callback function, EnumFunc, receives the font information and creates handles for each font:
FARPROC lpEnumFunc;
.
.
.
int FAR PASCAL EnumFunc( )
{
}
hDC = GetDC(hWnd);
lpEnumFunc = MakeProcInstance(EnumFunc, hInst);
EnumFonts(hDC, “Courier”, lpEnumFunc, NULL);
FreeProcInstance(lpEnumFunc);
To use the EnumFonts function, you must supply a callback function. As with all callback functions, EnumFunc must be explicitly named in the EXPORTS statement in your module-definition file and must be declared with the FAR and PASCAL attributes. For each font to be enumerated, the EnumFunc callback function receives a pointer to a logical-font structure, a pointer to a text-metrics structure, a pointer to any data you may have passed in the EnumFonts function call, and an integer specifying the font type. The following example shows a simple callback function that creates a list of all the sizes (in terms of height) of a given set of raster fonts:
short SizeList[10];
short SizeCnt = 0;
.
.
.
int FAR PASCAL EnumFunc(lpLogFont, lpTextMetric, FontType, lpData)
LPLOGFONT lpLogFont;
LPTEXTMETRIC lpTextMetric;
short FontType;
LPSTR lpData;
{
if (FontType & RASTER_FONTTYPE) {
SizeList[SizeCnt++] = lpLogFont->lfHeight;
if (SizeCnt >= 10)
return (0);
}
return (1);
}
This example first checks the font to make sure it is a raster font. If the RASTER_FONTTYPE bit is 1, the font is a raster font; otherwise, it is a vector font. The next step is to save the value of the lfHeight field in the SizeList array. The callback function saves the first 10 sizes, then returns zero to stop the enumeration.
You can also use the DEVICE_FONTTYPE bit of the FontType parameter to distinguish GDI-supplied fonts from device-supplied fonts. This is useful if you want GDI to simulate bold, italic, underline, and strikeout attributes. GDI can simulate these attributes for GDI-supplied fonts, but not for device-supplied fonts.