Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#887 Improve text readability when using DirectWrite

Committed
closed
Neil Hodgson
5
2013-12-12
2012-01-10
Marko Njezic
No

When using DirectWrite - SCI_SETTECHNOLOGY(1) and leaving everything else at default, smaller text (10-20pt) is much harder to read compared to the GDI. Text is much lighter and thinner with DirectWrite and it can be problematic to look at it for a longer period of time. The main issue is that greyscale antialiasing mode is used instead of ClearType. According to MSDN Direct2D uses ClearType by default, but when the render target has alpha channel antialiasing mode gets changed to greyscale. Thankfully, this particular issue can be fixed by forcing the use of ClearType using SCI_SETFONTQUALITY(SC_EFF_QUALITY_LCD_OPTIMIZED). However, this brings us to another issue. Colors are way off when DirectWrite ClearType implementation is used compared to the GDI ClearType implementation. This is because these two different ClearType implementations use different gamma correction values (GDI usually 1.2, DirectWrite usually 2.2). Setting gamma value of DirectWrite to the same value as GDI brings us much closer to the GDI look (although text will appear a little darker since there is another parameter that controls contrast). In order to be able to change gamma value Scintilla should allow containers to set custom DirectWrite rendering parameters, particularly: gamma, enhancedContrast and clearTypeLevel parameters of IDWriteFactory::CreateCustomRenderingParams() method should be available to the container. Something like SCI_SETDWRITEGAMMA, SCI_SETDWRITEENHANCEDCONTRAST and SCI_SETCLEARTYPELEVEL. Exporting other parameters, like renderingMode would require more internal changes related to text measurement and is of no real use since the main point is to have sub pixel positioning and antialiasing.

I've attached a sample screenshot that shows the difference between text rendering using the worst case scenario - Consolas 10pt (it is the worst case since Consolas simply requires ClearType in order to look good, Visual Studio 2010 always enables ClearType for Consolas, for example). I also attached a sample patch that would set gamma value to the same level as GDI. The patch is made just to illustrate the difference between text rendering, the actual Scintilla modification should export above mentioned DirectWrite parameters and leave up to the container to change them. However, Scintilla should at least automatically activate ClearType (if enabled on user's computer) when using DirectWrite in the same way how it's automatically done by Windows when using GDI.

In my opinion ultimate goal should be to make Scintilla draw text and colors (gamma and contrast) as close to the Visual Studio 2010 as possible when using DirectWrite, since MS did make it look very nice to the eye with all the changes to the WPF and text rendering during Visual Studio 2010 development cycle.

Discussion

  • Marko Njezic
    Marko Njezic
    2012-01-10

    Comparison of DirectWrite rendering

     
    Attachments
  • Marko Njezic
    Marko Njezic
    2012-01-10

    Export SCI_SETFONTQUALITY

     
    Attachments
  • Marko Njezic
    Marko Njezic
    2012-01-10

    Sample patch that sets gamma to the GDI value

     
    Attachments
    • Neil Hodgson
      Neil Hodgson
      2013-10-23

      Committed as [ab916a].

       

      Related

      Commit: [ab916a]

      • Marko Njezic
        Marko Njezic
        2013-10-23

        I was quite surprised when I received an e-mail notification about this feature request that I posted almost two years ago and that my patch was committed.

        I would like to point out that attached patch was just a sample written for demonstration purposes and as such it is incomplete. Custom DirectWrite rendering parameters with GDI gamma value should also be recreated in response to WM_SETTINGCHANGE message in case that gamma value was changed while the application was running, for the implementation to be complete.

         
        • Neil Hodgson
          Neil Hodgson
          2013-10-24

          Its a low impact change that might help some people. I doubt anyone will be sufficiently troubled to contribute a WM_SETTINGCHANGE patch.

           
  • Neil Hodgson
    Neil Hodgson
    2012-01-20

    • assigned_to: nobody --> nyamatongwe
     
  • Neil Hodgson
    Neil Hodgson
    2012-01-20

    When using DirectWrite/Direct2D, there is additional buffering so SCI_SETBUFFEREDDRAW (SciTE: buffered.draw=0) should be used to turn off Scintilla's buffering. That changes default text rendering. The differing colours with ClearType resulted in colours similar to current browsers (IE and Chrome) for me - I thought this was due to the colour profile I was using as it improved a lot when the profile was changed.

    I'd expect IE to be a better app for colour compatibility as matching colours between elements to accurately display the page intent is important in its role. The dull look of your IE screenshot looks like my monitor before changing to a profile that looked good.

     
  • Neil Hodgson
    Neil Hodgson
    2012-01-20

    Since DirectWrite supports fractional sizes you may be happier with some minor tweaks like:

    font.base=font:Verdana,size:9.7
    font.comment=font:Segoe UI,size:8.9,italics
    font.monospace=font:Consolas,size:8.9

     
  • Marko Njezic
    Marko Njezic
    2012-01-20

    Disabling double buffering did bring back ClearType, so that fixes the first issue. However, the fact that Scintilla's additional double buffering triggers inferior greyscale antialiasing instead of Direct2D default ClearType antialiasing should be mentioned somewhere in the documentation, just to avoid getting the same question from someone else in the future.

    Which leaves us now only with the issue with "wrong" colors. First, I would like to clarify that the main issue is not what are the correct colors (as that is open for debate), the issue is the difference between GDI and DirectWrite ClearType implementations and related text rendering. If you see the same text colors in IE9 and Chrome, your monitor is not properly calibrated. Chrome doesn't support color management at all and uses GDI to draw text (that's the reason why I did not include it in my screenshot, only DirectWrite applications are in the screenshot). IE9 has a half baked color management support, for text it relies completely on DirectWrite, while for images it reads any embedded ICC profile and converts the image to sRGB colorspace. Which obviously won't work for wide gamut monitors. In order to be truly color managed application IE9 should actually respect monitor's profile, which it doesn't. Only fully color managed web browser that uses actual monitor profile is Firefox. However, even Firefox relies on DirectWrite to draw text and can actually render the same text in different colors depending on text size due to some of the recent internal changes that they implemented in order to minimize the backlash from users since the introduction of DirectWrite (like fallback to GDI classic rendering mode in DirectWrite for fonts smaller than 15pt).

    The only reason why colors are different is because there are two different gamma values for GDI and DirectWrite ClearType implementations. GDI gamma value is stored in registry at: HKEY_CURRENT_USER\Control Panel\Desktop\FontSmoothingGamma (and is usually set to a value between 1000 and 1400), while DirectWrite gamma value is at: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Avalon.Graphics\DISPLAY[1,2,3,...]\GammaLevel (usually set to 2200). Now, like I said, what are the correct colors is a personal preference, but judging from the various responses from people to all the text rendering changes in the latest Visual Studio 2010 (including WPF 3 vs. WPF 4) and Firefox, GDI rendering is the preferred one. And in order to have the best from both worlds, the most sensible thing would be to allow users to be able to adjust DirectWrite parameters to their liking, like Firefox did by exporting all DirectWrite rendering parameters (in Firefox parameters are exported as various gfx.font_rendering.cleartype_params.* options). Firefox does quite a lot of additional work under the hood, but as a start, exporting gamma, enhancedContrast and clearTypeLevel parameters in Scintilla would be enough.

    I've also attached another screenshot that shows the difference in text colors between GDI and DirectWrite. On a calibrated monitor, the difference should be clearly seen (personally, I'm currently looking at a couple of HP IPS monitors from their performance series).

     
  • Marko Njezic
    Marko Njezic
    2012-01-20

    Color difference

     
    Attachments
  • Neil Hodgson
    Neil Hodgson
    2012-01-23

    SF is doing its error message for download thing. The question for me is why has DirectWrite chosen a particular default gamma? Supporting extra platform-specific tweakables doesn't sound like being a good long-term solution.

     
  • Marko Njezic
    Marko Njezic
    2012-01-23

    When SF starts to throw errors, I found that the only way to download something is to try several times repeatedly and one of the tries will be successful.

    Regarding your question about DirectWrite default values, good luck trying to get that answer from Microsoft. :-)

    In case of DirectWrite, only way to satisfy everyone is to allow customization, like Firefox did. People respond differently to text rendering and there is no set of universally acceptable values that can be used.

     
  • Neil Hodgson
    Neil Hodgson
    2013-10-23

    • labels: Scintilla --> Scintilla, directwrite
    • Group: --> Committed
     
  • Neil Hodgson
    Neil Hodgson
    2013-12-12

    • status: open --> closed