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:

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.

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.

Font Structures

Key structures used in GDI font operations:

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: