4

I use ID2D1RenderTarget::DrawGlyphRun method to render text, but it gets a little blurry. However if I use ID2D1RenderTarget::DrawTextLayout method the text comes out very clearly because DirectWrite performs PixelSnapping.

So my question is how can I do Pixel Snapping even when I'm using the ID2D1RenderTarget::DrawGlyphRun method?

I tried to round coordinates:

(int)(baselineOrigin.Y + 0.5f)

But it only helped in some situations.

What is the correct way to round coordinates before text rendering with Direct2D ?

Thank you very much, any help will be appreciated.

2
  • By definition, DrawGlyphRun provides rasterization when you already know the position of the glyphs you want to render. Why can't you use DrawTextLayout (or DirectWrite)? And are you sure you have a pixel snapping issue? Blurryness can have many origins, have you tried playing with ID2D1RenderTarget::SetTextAntialiasMode, ID2D1RenderTarget::SetTextRenderingParams? Jul 19, 2019 at 7:32
  • 1
    @SimonMourier I can't use IDWriteTextLayout because I break lines in a special way and place glyphs in a special way. And I'm sure the reason is pixel snapping because I used IDWriteTextRenderer to extract all GlyphRun created by IDWriteTextLayout, and I drew GlyphRun myself, and it came out blurred. And when I was drew with IDWriteTextLayout and with D2D1_DRAW_TEXT_OPTIONS_NO_SNAP it was also blurred.
    – codeDom
    Jul 19, 2019 at 7:52

2 Answers 2

1

Short answer is that you can't do that with DrawGlyphRun(). Pixel snapping mode is conveyed to IDWriteTextLayout::Draw() through IDWriteTextRenderer that Direct2D implements. DrawGlyphRun() does not operate on layout, so you don't have same way to control it.

Regarding coordinate rounding, you probably want to round in horizontal direction for horizontal layout, and your example does not do that.

0
+50

In order to avoid text blurring, the Y position of the glyphs should be rounded. And as you did:

(int)(baselineOrigin.Y + 0.5f)

But, you should also take into account the ID2D1RenderTarget::GetTransform. If Transform causes a scale other than 1 or causes a vertical shift, you need to calculate the actual location where the glyph will appear and only then round.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.