简体   繁体   English

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

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

currently i am rendering text with Direct2D with following code:目前我正在使用以下代码使用 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();

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.但据我所知,DrawText 是渲染文本效率较低的方法,它对我来说效果很好,但我遇到的一个问题是我在围绕文本创建轮廓效果时遇到了麻烦,我知道有很多人喜欢链接的这个教程,但是它有很多问题,其中之一是整个项目基于活动模板库,我遇到的第二个问题是它需要字体文件的完整路径,而 DrawText 只需要定义字体系列名称在 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?所以我的问题是如何使用 Direct2D 有效地渲染文本,以便在其上创建轮廓效果,我是否使用 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此 DOC 可以帮助您: 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_ :只需创建您自己的 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