DirectWrite API
Introduction to DirectWrite
DirectWrite is a high-performance, hardware-accelerated text rendering API that provides advanced typographical features for Windows. It offers superior text rendering quality compared to older Windows text APIs, supporting rich text formatting, multiple font technologies, and subpixel anti-aliasing.
Key features include:
- High-quality text rendering.
- Support for OpenType, TrueType, and PostScript fonts.
- Advanced text layout and shaping.
- Globalization features.
- Direct integration with Direct3D for GPU-accelerated rendering.
Core Concepts
Understanding the following core concepts is crucial for working with DirectWrite:
- IDWriteFactory: The starting point for all DirectWrite operations. You use this factory to create other DirectWrite objects.
- IDWriteTextFormat: Represents text formatting properties such as font family, size, weight, style, and paragraph alignment.
- IDWriteTextLayout: Represents the layout of a text string, including its geometry, formatting, and glyph metrics.
- IDWriteGDIInteroperability: Enables interoperability between DirectWrite and GDI.
- IDWriteTypography: Represents advanced typographical features.
Key Interfaces
DirectWrite exposes several key interfaces for managing text rendering and layout:
-
IDWriteFactory
Provides methods for creating DirectWrite objects like text formats, layouts, and font collections.
Example Usage:
IDWriteFactory* pFactory = nullptr; DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast
(&pFactory) ); // ... use pFactory ... if (pFactory) pFactory->Release(); -
IDWriteTextFormat
Defines the properties of a text format, such as font face, size, weight, style, and paragraph alignment.
-
IDWriteTextLayout
Represents a formatted text string and provides methods for determining text metrics, hit-testing, and applying inline formatting.
-
IDWriteTypography
Controls advanced typographical features like ligatures, stylistic sets, and contextual alternates.
-
IDWriteGlyphRun Analysis
Analyzes a glyph run to determine glyph indices, positions, and other metrics.
Common Enumerations
Enumerations are used to specify various properties and states:
DWRITE_FONT_WEIGHT
: Specifies font weight (e.g., Thin, Light, Normal, Bold).DWRITE_FONT_STYLE
: Specifies font style (e.g., Normal, Italic, Oblique).DWRITE_TEXT_ALIGNMENT
: Specifies text alignment (e.g., Left, Center, Right, Justify).DWRITE_PARAGRAPH_ALIGNMENT
: Specifies paragraph alignment.DWRITE_WORD_WRAPPING
: Specifies word wrapping behavior.
Key Structures
Structures are used to pass data to and receive data from DirectWrite functions:
DWRITE_FONT_METRICS
: Contains metrics for a font.DWRITE_TEXT_RANGE
: Specifies a range of text.DWRITE_HIT_TEST_METRICS
: Contains metrics for hit-testing results.
Utility Functions
These functions are often used for initialization and interop:
DWriteCreateFactory
: Creates a DirectWrite factory object.IDWriteTextLayout::Draw
: Draws the text layout.