22

I already know that sub-pixel positioning causes DirectWrite text rendering to be blurry compared to GDI.

However, my question is a bit more fundamental: Why can't DirectWrite (and related methods) be made to render text as sharply as GDI?

In other words:
What prevents DirectWrite from being able to snap text to the nearest pixel, the way GDI can?

Is it, for example, a hardware issue? A driver architecture issue? Is it simply not implemented? Or something else?


Smaller sample:

Larger samples:

Direct2D, aliased:

Direct2D, default:

Direct2D ("classic GDI"):

Direct2D ("natural GDI"):

Actual classic GDI:

![](https://i.stack.imgur.com/kbdPb.png)

Actual ClearType GDI:

enter image description here


Note: If all of these look blurry to you, run

document.body.style.zoom = 1 / window.devicePixelRatio

in Chrome's console and view it afterward.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Hmm, yeah, I'd like to see an answer to this. These technologies produce text that's far too blurry to make them worthwhile; I can't even use products that render text with DirectWrite (cough, Firefox). Unfortunately, I have a sneaking suspicion that the answer is nobody working on the project thinks it is important. – Cody Gray - on strike Dec 23 '11 at 10:31
  • 1
    Because pixel snapping makes the width of the rendered text unpredictable. Resolution independent text rendering is the holy grail. Flubbed by GDI+, a hard requirement for WPF and sustainable improvements in display technology. – Hans Passant Dec 23 '11 at 15:44
  • 5
    DWrite can do it, you just have to ask. Create [custom render parameters](http://msdn.microsoft.com/en-us/library/dd368190(VS.85).aspx) with `clearTypeLevel` set to zero. Note that sharpness comes at the expense of accuracy. See for example [the rotated text](http://www.basschouten.com/blog1.php/font-rendering-gdi-versus-directwrite) and how chunky it looks in GDI, and how uneven the spacing is in GDI compared to subpixel. – Raymond Chen Dec 23 '11 at 18:54
  • @RaymondChen: I've actually tried that before (when trying to see if SciTE's accelerated text rendering can be made sharper), but it doesn't work. Maybe I was doing it wrong -- do you have a demo piece of code that shows it rendering sharply? – user541686 Dec 23 '11 at 19:18
  • @HansPassant: If the width of the rendered text is unpredictable then how does GDI do it? – user541686 Dec 23 '11 at 19:19
  • 1
    @Mehrdad I never tried it myself, but from reading the docs it looks like it should have worked. Maybe it just switches to grayscale anti-aliasing? At any rate, if you don't like DirectWrite, then don't use it. (It's not so much that the width is unpredictable so much as it is uneven. At low resolutions, a whole pixel is BIG.) – Raymond Chen Dec 24 '11 at 00:45
  • @RaymondChen: See my update -- the parameters don't really fix anything; GDI still snaps to pixels, whereas Direct2D doesn't (even in "GDI" mode). – user541686 Dec 24 '11 at 03:12
  • You're not comparing like with like. All your Direct2D samples are rendered in grayscale, but GDI is using cleartype (and the Linux sample is doing something similar). – arx Dec 28 '11 at 23:14
  • @arx: Do you have ideas on how to improve the comparison? – user541686 Dec 29 '11 at 00:50
  • There are some notes here about enabling cleartype: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368170%28v=vs.85%29.aspx – arx Dec 29 '11 at 00:57
  • @arx: Wow, seems like you're right! Raymond did mention switching to grayscale but I didn't think that was what was happening because I thought [the default is ClearType](http://msdn.microsoft.com/en-us/library/windows/desktop/dd368170%28v=vs.85%29.aspx). It seems like the text *size* isn't quite the same, but the look seems to be sharp... I'll look at it more later, but please post that as an answer if you can! Thanks a lot! – user541686 Dec 29 '11 at 01:12

1 Answers1

19

You aren't comparing like with like. Your Direct2D samples are all rendered in grayscale, whereas the GDI and Linux samples are using sub-pixel anti-aliasing (aka ClearType on Windows).

This page describes what you need to do to enable cleartype: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368170%28v=vs.85%29.aspx

N.B. When testing rendering like this, it's always worth using Windows Magnifier or similar to check that you are actually getting what you think you are getting.

arx
  • 16,686
  • 2
  • 44
  • 61
  • 1
    I *think* the part about Linux isn't correct (since "Cleartype" is a Microsoft thing, and since the Linux one actually *isn't* completely like the GDI one) but yeah, the rest is spot-on. When I used `pRenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE)` the Direct2D result was almost exactly like GDI. – user541686 Dec 29 '11 at 03:04
  • Gonna give you a bounty, actually -- I think this is pretty worthy. :) – user541686 Dec 29 '11 at 03:07
  • Yes, ClearType is Microsoft's implementation of sub-pixel anti-aliasing, so it's not what Linux is using. I've clarified the text slightly. And thank you for the bounty! – arx Dec 29 '11 at 03:10
  • Sure! I can't give it right now since it takes a day or so but I'll award it as soon as I can! BTW, [here are the results](http://i.imgur.com/QkJ0p.png) for using Direct2D with ClearType -- it just like GDI in terms of sharpness. :) – user541686 Dec 29 '11 at 03:18
  • @Mehrdad Not quite. It still lacks the consistency vanilla true, software rendered Cleartype. What you've posted is a hardware rending aiming to be as close to software Cleartype as possible. Still blurry for my sharp eyes. – Dmitry Sychov Feb 10 '12 at 05:44