[英]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.