|
Enumerating the Installed Fonts
In some instances, an application must be able to enumerate the available
fonts and select the one most appropriate for a particular operation. An
application can enumerate the available fonts by calling the EnumFonts or EnumFontFamilies function. These functions send information about the available fonts to a
callback function that the application supplies. The callback function receives
information in LOGFONT and NEWTEXTMETRIC structures. (The NEWTEXTMETRIC structure contains information about a TrueType font. When the callback
function receives information about a non-TrueType font, the information is
contained in a TEXTMETRIC structure.) By using this information, an application can limit the user's
choices to only those fonts that are available.
The EnumFontFamilies function is similar to the EnumFonts function but includes some extra functionality. EnumFontFamilies allows an application to take advantage of styles available with TrueType
fonts. New and upgraded applications should use EnumFontFamilies instead of EnumFonts.
In previous versions of Windows, the only style attributes were weight and
italic; any other styles were specified in the family name for the font. For
example, when an application used the EnumFonts function to query the available Courier fonts, EnumFonts might return information for Courier, Courier Bold, Courier Bold Italic, and
Courier Italic. It would not return information about any other Courier fonts
that might be installed, because any other Courier fonts would typically have a
different family name.
TrueType fonts are organized around a typeface name (for example, Courier New)
and style names (for example, italic, bold, and extra-bold). The EnumFontFamilies function enumerates all the styles associated with a given family name, not
simply the bold and italic attributes. For example, when the system includes a
TrueType font called Courier New Extra-Bold, EnumFontFamilies lists it with the other Courier New fonts. The capabilities of EnumFontFamilies are helpful for fonts with many or unusual styles and for fonts that cross
international borders.
If an application does not supply a typeface name, the EnumFonts and EnumFontFamilies functions supply information about one font in each available family. To
enumerate all the fonts in a device context, the application can specify NULL for
the typeface name, compile a list of the available typefaces, and then enumerate
each font in each typeface.
The following example uses the EnumFontFamilies function to retrieve the number of available raster, vector, and TrueType
font families.
UINT uAlignPrev;
int aFontCount[] = { 0, 0, 0 };
char szCount[8];
EnumFontFamilies(hdc, (LPCTSTR) NULL,
(FONTENUMPROC) EnumFamCallBack, (LPARAM) aFontCount);
uAlignPrev = SetTextAlign(hdc, TA_UPDATECP);
MoveToEx(hdc, 10, 50, (LPPOINT)NULL);
TextOut(hdc, 0, 0, "Number of raster fonts: ", 24);
itoa(aFontCount[0], szCount, 10);
TextOut(hdc, 0, 0, szCount, strlen(szCount));
MoveToEx(hdc, 10, 75, (LPPOINT)NULL);
TextOut(hdc, 0, 0, "Number of vector fonts: ", 24);
itoa(aFontCount[1], szCount, 10);
TextOut(hdc, 0, 0, szCount, strlen(szCount));
MoveToEx(hdc, 10, 100, (LPPOINT)NULL);
TextOut(hdc, 0, 0, "Number of TrueType fonts: ", 26);
itoa(aFontCount[2], szCount, 10);
TextOut(hdc, 0, 0, szCount, strlen(szCount));
SetTextAlign(hdc, uAlignPrev);
.
.
.
BOOL FAR PASCAL EnumFamCallBack(lplf, lpntm, FontType, aFontCount)
LPLOGFONT lplf;
LPNEWTEXTMETRIC lpntm;
DWORD FontType;
LPVOID aFontCount;
{
int far * aiFontCount = (int far *) aFontCount;
/*
* Record the number of raster, TrueType, and vector
* fonts in the font-count array.
*/
if (FontType & RASTER_FONTTYPE)
aiFontCount[0]++;
else if (FontType & TRUETYPE_FONTTYPE)
aiFontCount[2]++;
else
aiFontCount[1]++;
if (aiFontCount[0] || aiFontCount[1] || aiFontCount[2])
return TRUE;
else
return FALSE;
UNREFERENCED_PARAMETER( lplf );
UNREFERENCED_PARAMETER( lpntm );
}
This example uses two masks, RASTER_FONTTYPE and TRUETYPE_FONTTYPE, to
determine the type of font being enumerated. If the RASTER_FONTTYPE bit is set, the
font is a raster font. If the TRUETYPE_FONTTYPE bit is set, the font is a
TrueType font. If neither bit is set, the font is a vector font. A third mask,
DEVICE_FONTTYPE, is set when a device (for example, a laser printer) supports
downloading TrueType fonts; it is zero if the device is a display adapter, dot-matrix
printer, or other raster device. An application can also use the DEVICE_FONTTYPE
mask to distinguish GDI-supplied raster fonts from device-supplied fonts.
Windows can simulate bold, italic, underline, and strikeout attributes for
GDI-supplied raster fonts, but not for device-supplied fonts.
An application can also check bits 1 and 2 in the tmPitchAndFamily member of the NEWTEXTMETRIC structure to identify a TrueType font. If bit 1 is 0 and bit 2 is 1, the font
is a TrueType font.
Vector fonts are categorized as OEM_CHARSET instead of ANSI_CHARSET. Some
applications identify vector fonts by using this information, checking the tmCharSet member of the NEWTEXTMETRIC structure. This categorization usually prevents the font mapper from choosing
vector fonts unless they are specifically requested. (Most applications no
longer use vector fonts because their strokes are single lines and they take
longer to draw than TrueType fonts, which offer many of the same scaling and
rotation features that required vector fonts in earlier versions of Windows.)
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|