Search code examples
directwrite

Handle device loss with IDWriteTextLayout::SetDrawingEffect


Following up on How can I render mixed-colour text in DirectWrite?, what's the best practice to handle device loss when passing a device-dependent resource — e.g. an ID2D1LinearGradientBrush or any other ID2D1Brush — as the first parameter of IDWriteTextLayout::SetDrawingEffect?

To the best of my knowledge, I think IDWriteTextLayout was a device-independent resource.


Solution

  • Drawing effect objects have no special meaning within DirectWrite. They are assigned for text ranges, and I believe do not break shaping - everything works normally, but then Draw() simply covers for different drawing effects by making several callback calls.

    That means that lost device condition exists only at Direct2D side, and most likely you'll get in corresponding error state after DrawTextLayout() returns. When that happens, you'll need to recreate brushes, and set them again, no need to recreate layout objects.