Render Using Direct2D
Direct2D provides methods for rendering either text with formatting described by only an IDWriteTextFormat or an IDWriteTextLayout to a Direct2D surface.
Rendering Text Described by IDWriteTextFormat
To render a string using an IDWriteTextFormat object to describe the formatting for the entire string, use the ID2D1RenderTarget::DrawText method provided by Direct2D.
Define the area for the text layout by retrieving the dimensions of the rendering area, and create a Direct2D rectangle that has the same dimensions.
D2D1_RECT_F layoutRect = D2D1::RectF( static_cast<FLOAT>(rc.left) / dpiScaleX_, static_cast<FLOAT>(rc.top) / dpiScaleY_, static_cast<FLOAT>(rc.right - rc.left) / dpiScaleX_, static_cast<FLOAT>(rc.bottom - rc.top) / dpiScaleY_ );
Use the ID2D1RenderTarget::DrawText method and the IDWriteTextFormat object to render text to the screen. The ID2D1RenderTarget::DrawText method takes the following parameters:
- A string to render.
- A pointer to an IDWriteTextFormat interface.
- A Direct2D layout rectangle.
- A pointer to an interface that exposes ID2D1Brush.
pRT_->DrawText( wszText_, // The string to render. cTextLength_, // The string's length. pTextFormat_, // The text format. layoutRect, // The region of the window where the text will be rendered. pBlackBrush_ // The brush used to draw the text. );
Rendering a IDWriteText Layout Object
To draw the text with the text layout settings specified by the IDWriteTextLayout object, change the code in the MultiformattedText::DrawText method to use IDWriteTextLayout::DrawTextLayout.
Delcare a D2D1_POINT_2F variable and set it to the upper-left point of the window.
D2D1_POINT_2F origin = D2D1::Point2F( static_cast<FLOAT>(rc.left / dpiScaleX_), static_cast<FLOAT>(rc.top / dpiScaleY_) );
Draw the text to the screen by calling the ID2D1RenderTarget::DrawTextLayout method of the Direct2D render target and passing the IDWriteTextLayout pointer.
pRT_->DrawTextLayout( origin, pTextLayout_, pBlackBrush_ );