如何在DirectWrite中呈现混合颜色文本?

7

我希望使用DirectWrite进行混合颜色文本格式化(准确地说是语法高亮),但似乎找不到一种方法来实现,无论是在布局(Layout)还是排版(Typography)选项中都没有。唯一的选择是在渲染文本时传递Brush,但这对我来说不起作用,因为我基本上只有一个布局。求助!

1个回答

12
使用 IDWriteTextLayout::SetDrawingEffect 在子范围上应用绘制效果。如果你正在使用 DWrite 和 D2D 的 DrawTextLayout, 就像你所说的那样,那么绘制效果就是一个刷子(例如通过 CreateSolidColorBrush 或渐变刷子之一获取的 ID2D1Brush)。如果你为 IDWriteTextLayout::Draw 实现了自己的 IDWriteTextRenderer,那么绘制效果可以是任何你解释出来的东西。在 IDWriteTextRenderer::DrawGlyphRun 回调中,你需要调用 QueryInterface 来获取绘图效果参数,或者如果你确定它是你自己的类型,直接进行静态转换。
// ... create the colored brushes and determine where to draw ...
wchar_t const* text = L"Red Green";
dwriteFactory->CreateTextLayout(....., OUT &textLayout);

DWRITE_TEXT_RANGE textRange1 = {0,3}, textRange2 = {4,5};

textLayout->SetDrawingEffect(redBrush,  textRange1);
textLayout->SetDrawingEffect(greenBrush, textRange2);

renderer->DrawTextLayout(point, textLayout, defaultBrush);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接