GDI Fonts API
The Graphics Device Interface (GDI) provides a comprehensive set of functions for managing and rendering fonts within the Windows operating system. This documentation outlines the core APIs for font operations, enabling developers to control font selection, manipulation, and rendering in their applications.
Key Concepts
Understanding the fundamental concepts related to fonts in GDI is crucial:
- Logical Fonts: Abstract representations of fonts that can be requested by applications. GDI maps these to physical fonts available on the system.
- Physical Fonts: Actual font files installed on the system that GDI can use for rendering.
- Font Families: Groupings of related fonts, such as Swiss, Modern, Roman, Script, and Decorative.
- Font Metrics: Data describing the characteristics of a font, including character height, width, ascent, descent, and spacing.
Core Font Functions
Creating and Selecting Fonts
The process of using a font typically involves creating a logical font description and then selecting it into a device context.
CreateFont(): Creates a logical font based on specified characteristics like height, width, escapement, orientation, weight, italic attribute, underline attribute, strikeout attribute, character set, output precision, clip precision, quality, and pitch and family.CreateFontIndirect(): Creates a logical font based on aLOGFONTstructure, providing a more structured way to define font attributes.SelectObject(): Selects a GDI object (including a font) into a device context. The function returns the previously selected object of that type. It's important to store the returned object to restore it later.
Example: Creating and Selecting a Font
LOGFONT lf;
// Initialize LOGFONT structure with desired attributes
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = -20; // Approximate height in logical units
lf.lfWeight = FW_NORMAL; // Normal font weight
strcpy(lf.lfFaceName, "Arial"); // Font face name
HFONT hFont = CreateFontIndirect(&lf);
HDC hdc = GetDC(hWnd); // Get device context for the window
// Select the font into the device context
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
// ... Perform GDI drawing operations using the selected font ...
// Restore the original font
SelectObject(hdc, hOldFont);
DeleteObject(hFont); // Clean up the font object
ReleaseDC(hWnd, hdc);
Querying Font Information
Applications can retrieve information about the currently selected font or available fonts on the system.
GetCurrentFont(): Retrieves information about the currently selected font in a device context.EnumFontFamiliesEx(): Enumerates the font families available on a device.GetFontData(): Retrieves font metric data for a specified device font.GetTextMetrics(): Fills aTEXTMETRICstructure with information about the currently selected font in the specified device context.
Font Structures
Key structures used in GDI font operations:
LOGFONT: Contains the attributes of a logical font.TEXTMETRIC: Contains information about the physical attributes of a currently selected TrueType font.
LOGFONT Structure Members
| Member | Description |
|---|---|
lfHeight |
The desired height of the font in logical units. |
lfWidth |
The desired average character width in logical units. |
lfEscapement |
The angle (in tenths of a degree) between the escapement vector and the x-axis. |
lfOrientation |
The angle (in tenths of a degree) between the text's baseline and the x-axis. |
lfWeight |
The font weight. |
lfItalic |
Specifies an italic font. |
lfUnderline |
Specifies an underlined font. |
lfStrikeOut |
Specifies a strikeout font. |
lfCharSet |
The character set. |
lfOutPrecision |
The output precision. |
lfClipPrecision |
The clipping precision. |
lfQuality |
The output quality. |
lfPitchAndFamily |
The pitch and family of the font. |
lfFaceName |
A null-terminated string name for the typeface. |
TEXTMETRIC Structure Members (Common)
| Member | Description |
|---|---|
tmHeight |
The height (ascent + descent) of a line of text. |
tmAscent |
The ascent of a line of text. |
tmDescent |
The descent of a line of text. |
tmInternalLeading |
The number of pixels between the font's ascent and the top of the highest glyph. |
tmExternalLeading |
The number of pixels between the bottom of the highest glyph and the top of the lowest glyph. |
tmAveCharWidth |
The average width of characters in the font. |
tmMaxCharWidth |
The width of the widest character in the font. |
tmWeight |
The font weight. |
Advanced Topics
For more advanced font manipulation, consider exploring:
- TrueType font embedding
- Font linking and substitution
- Working with different character sets