简体   繁体   中英

How to correctly draw text with Direct2D and implement outline effect

currently i am rendering text with Direct2D with following code:

IDWriteTextFormat* d2d_text_format;
    m_WriteFactory->CreateTextFormat(
        m_FontData.m_Font,
        NULL,
        DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STYLE_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL,
        m_FontData.m_Size,
        L"",
        &d2d_text_format);

    ID2D1SolidColorBrush* d2d_color;
    m_RenderTarget->CreateSolidColorBrush(D2D1::ColorF(color.rBase(), color.gBase(), color.bBase(), color.aBase()), &d2d_color);

    m_RenderTarget->BeginDraw();

    m_RenderTarget->DrawText(wtext, wcslen(wtext), d2d_text_format, D2D1::RectF(x, y, 800, 600), d2d_color);

    m_RenderTarget->EndDraw();

But from what i know DrawText is the less efficient method of rendering text, it works fine for me but the one problem i have is that i have trouble creating outline effect around the text, i know there's this tutorial that many people like to link but there are many problems with it, one of them is that whole project is based of active template library and the second problem i have with it is that it requires the full path to the font file unlike DrawText which just requires font family name to be defined in CreateTextFormat.

So my question would be how can i efficiently render text with Direct2D that would allow me to create an outline effect on it, do i use DrawGlyphRun? If so, how do i use that function?

This DOC may help you: https://docs.microsoft.com/en-us/windows/win32/directwrite/how-to-implement-a-custom-text-renderer

Just create your own textRenderer and pass it through pTextRenderer_ :

// Draw the text layout using DirectWrite and the CustomTextRenderer class.
hr = pTextLayout_->Draw(
        NULL,
        pTextRenderer_,  // Custom text renderer.
        origin.x,
        origin.y
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM