Search code examples
c++windowsdirectwrite

Using DirectWrites SetCharacterSpacing function


I'm having a hard time grasping this problem getting the newer/extended functions in dwrite_1.h to work with my existing code and hope that someone might be able to help.

This is my existing set up...

// In the .h file

IDWriteFactory* pDWriteFactory_ = nullptr;
IDWriteTextFormat* pTextFormat_ = nullptr;
IDWriteTextLayout* pTextLayout_ = nullptr;
IDWriteTextRenderer* pTextRenderer_ = nullptr;

//in the .cpp file - this is in my own PrepareText() Function.

hr = pDWriteFactory_->CreateTextLayout(
    str.c_str(),
    str.size(),
    pTextFormat_,
    gfx.GetWindowWidth(),
    gfx.GetWindowHeight(),
    &pTextLayout_);

I would like to be able to use the IDWriteTextLayout1::SetCharacterSpacing function outlined here: MSDN Link which is defined in dwrite_1.h

However if I change my IDWriteTextLayout* to be IDWriteTextLayout1* I get an error (C2664) as pDWriteFactory_ cannot be initialised with an IDWriteTextLayout1 as an argument - cannot convert. This error still occurs if I change IDWriteFactory* to an IDWriteFactory1*.

So I'm missing something here, I had hoped that a IDWriteTextLayout1 would extend functionality but still be backward compatible with the existing IDWriteFactory::CreateTextLayout function but it isn't so.

If anyone could shed some light I would be grateful.


Solution

  • After you get the IDWriteTextLayout do a QueryInterface for the IDWriteTextLayout1.

    IDWriteTextLayout1 * pLayout1 = nullptr;
    hr = pTextLayout->QueryInterface(IID_PPV_ARGS(&pLayout1));
    if(FAILED(hr))
    { /* handle error */ }