DirectWrite

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

Typical Use Cases

DirectWrite is ideal for:

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)
    // ...
}