Interoperating with GDI

DirectWrite provides a migration path from, and some interoperability with, GDI's font model, as well as interfaces for rendering text to a bitmap that can then be drawn on a window.

This overview contains the following parts:

Introduction

DirectWrite provides methods for converting between GDI's LOGFONT structure and DirectWrite font interfaces. This allows you to use GDI for some or all of the font enumeration and selection, while taking advantage of the improved functionality and performance of DirectWrite. DirectWrite also has interfaces for rendering to a bitmap if you want to display text on a GDI surface.

Part 1: IDWriteGdiInterop

The IDWriteGdiInterop interface is used to convert between GDI font structures and DirectWrite font interfaces, and also to create an IDWriteBitmapRenderTarget object. Get an IDWriteGdiInterop object by using the IDWriteFactory::GetGdiInterop method, as shown in the following code.

// Create a GDI interop interface.
if (SUCCEEDED(hr))
{
    hr = g_pDWriteFactory->GetGdiInterop(&g_pGdiInterop);
}

Part 2: Font Objects

GDI uses the LOGFONT structure to store information about the font and style of text. The IDWriteGdiInterop::CreateFontFromLOGFONT method will convert a LOGFONT structure to an IDWriteFont object, as seen in the following code.

// Convert to a DirectWrite font.
if (SUCCEEDED(hr))
{
    hr = g_pGdiInterop->CreateFontFromLOGFONT(&lf, &pFont);
}

However, IDWriteFont does not encapsulate all of the same information that a LOGFONT does. A LOGFONT structure contains the font size, weight, style, underline, strikeout, font face name, and some other information. IDWriteFont objects contain information about a font and its weight and style, but not the font size, underline, and so on. With DirectWrite, formatting information elements such as these are encapsulated by an IDWriteTextFormat object or, for specific ranges of text, an IDWriteTextLayout object.

You do have the option to convert a IDWriteFont to a LOGFONT by using the IDWriteGdiInterop::ConvertFontToLOGFONT.

Part 3: Rendering

To render DirectWrite text to a GDI surface you use a custom text renderer. See the Render to a GDI Surface topic.