DirectWrite
What is DirectWrite?
DirectWrite is a modern, high‑performance text layout and rendering API for Windows. It provides support for ClearType, OpenType, Unicode, advanced typographic features, and hardware‑accelerated rendering.
Key Features
- High‑quality text rendering with sub‑pixel precision.
- Comprehensive Unicode coverage (up to Unicode 13.0).
- Support for OpenType features such as ligatures, kerning, and alternate glyphs.
- Hardware‑accelerated rendering via Direct2D.
- Layout engine optimized for complex scripts.
- Built‑in text measurement and font fallback.
Typical Use Cases
DirectWrite is ideal for:
- Desktop applications requiring crisp text (e.g., Office, Visual Studio).
- Games and UI overlays that need fast, high‑quality fonts.
- Custom controls and rich text editors.
- Cross‑platform engines that target Windows.
Quick Sample
The following code creates a simple DirectWrite text layout and draws it with Direct2D:
#include <d2d1.h>
#include <dwrite.h>
using namespace Microsoft::WRL;
int main()
{
// Initialize COM
CoInitializeEx(nullptr, COINITBASE_MULTITHREADED);
// Create Direct2D factory
ComPtr<ID2D1Factory> d2dFactory;
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, d2dFactory.GetAddressOf());
// Create DirectWrite factory
ComPtr<IDWriteFactory> dwFactory;
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(dwFactory.GetAddressOf()));
// Create a text format
ComPtr<IDWriteTextFormat> textFormat;
dwFactory->CreateTextFormat(L"Segoe UI", nullptr, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 32.0f,
L"", textFormat.GetAddressOf());
// Create a text layout
const wchar_t* text = L"Hello, DirectWrite!";
ComPtr<IDWriteTextLayout> textLayout;
dwFactory->CreateTextLayout(text, (UINT32)wcslen(text), textFormat.Get(),
500.0f, 200.0f, textLayout.GetAddressOf());
// Render with Direct2D (window creation omitted for brevity)
// ...
}