Direct2D を使用したレンダリング

Direct2D には、 IDWriteTextFormat または IDWriteTextLayout だけが記述した書式を持つテキストを Direct2D サーフェスにレンダリングするためのメソッドが用意されています。

IDWriteTextFormat で記述されたテキストのレンダリング

IDWriteTextFormat オブジェクトを使用して文字列をレンダリングし、文字列全体の書式を記述するには、Direct2D によって提供される ID2D1RenderTarget::D rawText メソッドを使用します。

  1. レンダリング領域の寸法を取得してテキスト レイアウトの領域を定義し、同じ寸法を持つ Direct2D 四角形を作成します。

    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_
        );
    
    
  2. ID2D1RenderTarget::D rawText メソッドと IDWriteTextFormat オブジェクトを使用して、テキストを画面にレンダリングします。 ID2D1RenderTarget::D rawText メソッドは、次のパラメーターを受け取ります。

    • レンダリングする文字列。
    • IDWriteTextFormat インターフェイスへのポインター。
    • Direct2D レイアウトの四角形。
    • 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.
        );
    
    

IDWriteText レイアウト オブジェクトのレンダリング

IDWriteTextLayout オブジェクトで指定されたテキスト レイアウト設定でテキストを描画するには、MultiformattedText::D rawText メソッドのコードを、IDWriteTextLayout::D rawTextLayout を使用するように変更します。

  1. D2D1_POINT_2F変数を Delcare し、ウィンドウの左上のポイントに設定します。

    D2D1_POINT_2F origin = D2D1::Point2F(
        static_cast<FLOAT>(rc.left / dpiScaleX_),
        static_cast<FLOAT>(rc.top / dpiScaleY_)
        );
    
    
  2. Direct2D レンダー ターゲットの ID2D1RenderTarget::D rawTextLayout メソッドを呼び出し、IDWriteTextLayout ポインターを渡して、テキストを画面に描画します。

    pRT_->DrawTextLayout(
        origin,
        pTextLayout_,
        pBlackBrush_
        );