How can I render mixed-colour text in DirectWrite?
Asked Answered
F

1

7

I want to use DirectWrite for mixed-colour text formatting (syntax highlighting, to be precise), but can't seem to find a way to do it, either in the Layout or Typography options. The only option is passing a Brush when rendering the text, which doesn't work for me because I basically have just one Layout. Help!

Fe answered 1/11, 2009 at 0:21 Comment(0)
Z
12

Use IDWriteTextLayout::SetDrawingEffect to apply drawing effects on subranges. If you're using DWrite with D2D DrawTextLayout, which it sounds like you are, then that drawing effect would just be a brush (such as ID2D1Brush via CreateSolidColorBrush or one of the gradient brushes). If you have implemented your own IDWriteTextRenderer for IDWriteTextLayout::Draw, then the drawing effect can be whatever you interpret it to be. In the IDWriteTextRenderer::DrawGlyphRun callback, you then call QueryInterface on the drawingEffect parameter, or if you are certain it is your own type, just static_cast it directly.

// ... 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);
Zincograph answered 10/9, 2011 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.