DirectWrite API Reference
Overview
DirectWrite is a high-performance, high-quality, GPU-accelerated text rendering API for Windows. It provides APIs for drawing text, formatting, and font management, enabling rich typography and text layout capabilities in your applications.
Core Concepts
- Text Layout: Handling complex text shaping, bidirectional text, international scripts, and text wrapping.
- Font Management: Enumerating system fonts, accessing font properties, and using font collections.
- Text Rendering: Generating high-quality glyphs for display on screen or print.
- Text Effects: Applying various effects like anti-aliasing, gamma correction, and subpixel rendering.
Interfaces
DirectWrite provides a set of interfaces for interacting with its features. Here are some of the most commonly used:
| Interface | Description |
|---|---|
| IDWriteFactory | The primary interface for creating other DirectWrite objects. |
| IDWriteTextFormat | Represents text formatting properties like font family, size, weight, style, and paragraph alignment. |
| IDWriteTextLayout | Represents a block of text with layout properties applied, allowing for rich text manipulation. |
| IDWriteTypography | Enables control over OpenType typography features. |
| IDWriteInlineObject | Represents inline objects within text, such as images or other graphical elements. |
Structures
Various structures are used to pass parameters and return information from DirectWrite functions.
| Structure | Description |
|---|---|
| DWRITE_TEXT_METRICS | Contains text layout metrics such as width, height, and line count. |
| DWRITE_FONT_METRICS | Provides font metrics like ascent, descent, and units per em. |
| DWRITE_GLYPH_METRICS | Contains metrics for individual glyphs. |
Enumerations
Enumerations define a set of constants used to specify options and states.
| Enumeration | Description |
|---|---|
| DWRITE_FONT_WEIGHT | Specifies the weight (boldness) of a font. |
| DWRITE_FONT_STYLE | Specifies the style (italic, oblique) of a font. |
| DWRITE_TEXT_ALIGNMENT | Specifies horizontal text alignment within a paragraph. |
| DWRITE_PARAGRAPH_ALIGNMENT | Specifies vertical text alignment within a paragraph. |
Functions
Key functions for creating and managing DirectWrite objects.
Creating the Factory:
HRESULT DWriteCreateFactory(
DWRITE_FACTORY_TYPE factoryType,
const IID& iid,
_COM_Outptr_ IUnknown** factory
);
This function creates an instance of the DirectWrite factory.
Creating Text Formats:
HRESULT CreateTextFormat(
const WCHAR* fontFamilyName,
IDWriteFontCollection* fontCollection,
DWRITE_FONT_WEIGHT fontWeight,
DWRITE_FONT_STYLE fontStyle,
DWRITE_FONT_STRETCH fontStretch,
FLOAT fontSize,
const WCHAR* localeName,
_Outptr_ IDWriteTextFormat** textFormat
);
Creates an IDWriteTextFormat object with specified font properties.
Rendering Text:
Text is typically rendered using Direct2D's ID2D1RenderTarget::DrawTextLayout method, passing an IDWriteTextLayout object.