如何使用Direct2D正确绘制文本并实现轮廓效果

问题描述

当前,我正在使用Direct2D通过以下代码渲染文本:

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();

但是据我所知,DrawText是渲染文本的效率较低的方法,对我来说效果很好,但是我遇到的一个问题是我无法在文本周围创建轮廓效果,我知道有this教程很多人喜欢链接,但是有很多问题,其中之一是整个项目都基于活动模板库,而我遇到的第二个问题是,它需要字体文件的完整路径,这与DrawText不同,后者只是需要在Createtextformat中定义字体系列名称

所以我的问题是我如何使用Direct2D有效地渲染文本,从而可以在其上创建轮廓效果,我是否使用DrawGlyphRun?如果是这样,我该如何使用该功能

解决方法

此DOC可能会帮助您:https://docs.microsoft.com/en-us/windows/win32/directwrite/how-to-implement-a-custom-text-renderer

只需创建自己的textRenderer并将其通过 pTextRenderer _

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