2009-11-01 71 views
5

我想使用DirectWrite进行混合颜色文本格式设置(语法突出显示,准确而言),但无法在Layout或Typography选项中找到执行此操作的方法。唯一的选择是在渲染文本时传递画笔,这对我来说不起作用,因为我基本上只有一个布局。帮帮我!如何在DirectWrite中渲染混合颜色文本?

回答

8

使用IDWriteTextLayout::SetDrawingEffect在子范围上应用绘图效果。如果您使用的是带有D2D DrawTextLayout的DWrite,这听起来就像是您的图形效果,那么该绘图效果将只是一个画笔(例如ID2D1Brush通过CreateSolidColorBrush或其中一个渐变画笔)。如果您已经为IDWriteTextLayout::Draw实施了自己的IDWriteTextRenderer,那么绘图效果可以是您解释的任何内容。在IDWriteTextRenderer::DrawGlyphRun回调中,您然后在drawingEffect参数上调用QueryInterface,或者如果您确定它是您自己的类型,则只需直接static_cast。

// ... 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);