繁体   English   中英

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

[英]How to correctly draw text with Direct2D and implement outline effect

目前我正在使用以下代码使用 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 是渲染文本效率较低的方法,它对我来说效果很好,但我遇到的一个问题是我在围绕文本创建轮廓效果时遇到了麻烦,我知道有很多人喜欢链接的这个教程,但是它有很多问题,其中之一是整个项目基于活动模板库,我遇到的第二个问题是它需要字体文件的完整路径,而 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
        );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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