Direct2D Renderer (Font Hinting, Windows RT, No Win Phone 8)

24 posts / 0 new
Last post
sonic59
Offline
Last seen: 3 months 3 weeks ago
Joined: 9 Mar 2010 - 16:51
Direct2D Renderer (Font Hinting, Windows RT, No Win Phone 8)

Direct2D Background
Direct2D is a 2D graphics API introduced in Windows 7. It was backported to Windows Vista and is available to users that have installed the Vista Platform Update. It is the replacement for the older Windows 2D graphics API known as GDI/GDI+. Direct2D is the only 2D graphics API available on Windows RT and for Metro applications. Metro applications are a special class of applications that operate on Windows 8 x32/x64 and Windows RT. Only Metro applications will be allowed in the new Windows App Store. Direct2D will likely be the only 2D graphics API available on Windows Phone 8 (Edit Oct 31: Windows Phone 8 does not support Direct2D.). Direct2D has sister API called DirectWrite which is used for text rendering in Direct2D.

Juce Direct2D Background
Juce has the ability to change how it renders its graphics. On Windows there are currently two working renderers: the Software Renderer and the OpenGL renderer. Around 2010, Jules began work on a Direct2D renderer however it was never completed and remains unfinished in the Juce source tree. In Nov 2011, Jules introduced support for text rendering via DirectWrite and in May 2012 he introduced support for font families and font styles with DirectWrite.

Juce Font Rendering Background
The Juce software renderer and OpenGL renderer do not support font hinting or LCD optimized rendering via Juce APIs. Jules will not implement font hinting in Juce itself but has helped to allow 3rd party code to implement hinting. TheVinn has written such code which allows you to have hinted font rendering by utilizing FreeType. This library is only able to hint fonts that have been included as an Embedded CustomTypeface. It currently does not allow you to hint fonts from a font file or from the system font collection. It likely could be adapted to do so but TheVinn has no plans on doing this himself as it is beyond his own needs for the library.

Initial Results
Now that the DirectWrite work is pretty much complete. I thought I'd take a look at the Direct2D code Juce wrote. While not done, it is actually near complete functionality wise. The current code was broken around font handling so I updated that code to use the new DirectWrite code.

I just picked two fonts at random without putting much thought into it. I have no idea whether these fonts were manually hinted, auto-hinted or have no hints at all. Though by the looks of it, I'm pretty sure MS Gothic has some sort of hinting going on.
English - Verdana
Chinese - MS Gothic
I suppose I probably should have looked harder for specifically hinted fonts.

Software Renderer vs Direct2D Renderer - English

Software Renderer English Image - http://i.minus.com/iV34PD6mYn6p0.png
Direct2D Renderer English Image - http://i.minus.com/iRZNNXC9G0Hkv.png

Software Renderer vs Direct2D Renderer - Chinese

Software Renderer Chinese Image - http://i.minus.com/ibipPsH2uNGh5C.png
Direct2D Renderer Chinese Image - http://i.minus.com/ibbvyEtVMgn7wF.png

Juce Text Rendering vs Sub-pixel ClearType

Juce Text Rendering - http://i.minus.com/iRg8QwSa2y6Uq.png
Sub-pixel ClearType - http://i.minus.com/i9AyWm5hYfQzX.png

Work To Do
While everything looks good visually, the performance is not good. Just simple mouse over standard buttons is laggy and the Animated Graphics Demo in the Juce Demo has a bad framerate.
I am hopeful the performance can be improved, IE9 and Firefox both use D2D and can render all sorts of complex graphics quickly, but it will take a lot of optimization.

The small amount of missing functionality needs to be added and the code should be refactored a bit to make it production ready.

If anyone is interested in helping out getting this into tip, please let me know.

Thanks to TheVinn who's test app served as the inspiration for my test app.

Edited by: sonic59 on 31 Oct 2012 - 13:18
TheVinn
Offline
Last seen: 3 weeks 2 days ago
Joined: 29 Aug 2009 - 11:31
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

That's some really nice work there!

Let me point out that FreeType has an "autohinting" module which is quite good. This means that even a font file that does not containing hinting, can still produce hinted output. Things like vertical stems and horizontal bars get detected by the FreeType autohinter. There's some examples of the quality of the autohinter floating around.

Now as for hinting system fonts, it is entirely possible. All you need is to write the platform specific code to locate the font file and then pass it to FreeType - my FreeType outline extraction class could be easily modified to use the resulting file.

sonic59
Offline
Last seen: 3 months 3 weeks ago
Joined: 9 Mar 2010 - 16:51
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Quote:
Now as for hinting system fonts, it is entirely possible. All you need is to write the platform specific code to locate the font file and then pass it to FreeType - my FreeType outline extraction class could be easily modified to use the resulting file.

It's all kinds of nasty on Windows. Even though there is an [url=http://msdn.microsoft.com/en-us/library/windows/desktop/dd371060(v=vs.85).aspx]IDWriteFontFile[/url] interface in DirectWrite, it doesn't return a string of the font file path. I could not find a way to get the font file location via DirectWrite. Without that you are left with doing string matching in the registry:

On some machines:
HKLM\Software\Microsoft\Windows\CurrentVersion\Fonts

On my Windows 7 64-bit machine:
HKLM\Software\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Fonts

The font name strings in the registry don't all cleanly match what you get from Win32 or DirectWrite font enumeration so you will have to be crafty with your string matching.
Finally you will probably want some form of path caching if you are dealing with all kinds of font families, styles and sizes.

I think it is better just to get the D2D renderer up to snuff and get the added benefit of better Juce compatibility with Windows RT and potentially Windows Phone 8.

SwingCoder
Offline
Last seen: 6 months 1 week ago
Joined: 7 Oct 2010 - 16:44
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

There is no doubt, D2D rendering quality is better (at least on Windows).

However, the Chinese fonts that you are using may not support ClearType, its quality didn't achieve the best. If use the Chinese fonts which can support ClearType, this will certainly be better.

I'm interested in this. If I can do something, please let me know.
miti-net#126.com or: SwingCoder2#gmail.com

And...could you send me this application's source code and VS project? I want to see the Chinese font more clearly too impatient to wait. To this end, I have been waiting a long time.

Thanks.

Windows 7 X64, VS2012, Latest JUCE tip

TheVinn
Offline
Last seen: 3 weeks 2 days ago
Joined: 29 Aug 2009 - 11:31
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

sonic59 wrote:
It's all kinds of nasty on Windows.

Yep and that's exactly why I didn't even try!

TheVinn
Offline
Last seen: 3 weeks 2 days ago
Joined: 29 Aug 2009 - 11:31
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

sonic if you would like to contribute your demonstration applet to AppletJUCE that would be great!

sonic59
Offline
Last seen: 3 months 3 weeks ago
Joined: 9 Mar 2010 - 16:51
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Quote:
However, the Chinese fonts that you are using may not support ClearType, its quality didn't achieve the best.

Very possible. I just picked the first Chinese font that MS Word spit out. Also as you've figured out from the text in that screenshot, I clearly don't speak Chinese. However I am interested in making text as readable as possible for CJK users.

Quote:
If use the Chinese fonts which can support ClearType, this will certainly be better.

If you do have much better examples, please link to some screenshots, I definitely want to show the best example of this. I have PM'd you the project.

Quote:
sonic if you would like to contribute your demonstration applet to AppletJUCE that would be great!

Do I have to use JUCEAmalgam to do so? I had to make changes to the Juce code itself, so I can't just use stock Juce or a stock JUCE amalgamation.
The project is already self-contained otherwise.

TheVinn
Offline
Last seen: 3 weeks 2 days ago
Joined: 29 Aug 2009 - 11:31
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

sonic59 wrote:
Do I have to use JUCEAmalgam to do so? I had to make changes to the Juce code itself, so I can't just use stock Juce or a stock JUCE amalgamation.

Oh :oops: .. you don't have to use JUCEAmalgam but you would need to use the JUCE that comes with AppletJUCE. If it requires a change to the JUCE sources maybe its best left as a stand-alone project.

How extensive are the changes?

sonic59
Offline
Last seen: 3 months 3 weeks ago
Joined: 9 Mar 2010 - 16:51
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Yeah it may not be a good idea to dilute AppletJUCE with Applets that have their own special Juce.

A little under 200 lines were changed across the following files:

JuceLibraryCode/modules/juce_graphics/juce_graphics.cpp
JuceLibraryCode/modules/juce_graphics/juce_graphics.h
JuceLibraryCode/modules/juce_graphics/native/juce_win32_Direct2DGraphicsContext.cpp
JuceLibraryCode/modules/juce_graphics/native/juce_win32_DirectWriteTypeface.cpp
JuceLibraryCode/modules/juce_gui_basics/juce_gui_basics.cpp
JuceLibraryCode/modules/juce_gui_basics/native/juce_win32_Windowing.cpp
SwingCoder
Offline
Last seen: 6 months 1 week ago
Joined: 7 Oct 2010 - 16:44
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Thanks sonic59, I have compiled and run this application successfully. preliminary results:

Font: MS Yahei (微软雅黑), Support ClearType.

[attachment=0]01-soft-msyh.png[/attachment]

[attachment=1]00-d2d-msyh.png[/attachment]

Windows 7 X64, VS2012, Latest JUCE tip

SwingCoder
Offline
Last seen: 6 months 1 week ago
Joined: 7 Oct 2010 - 16:44
Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Font: 宋体, not support ClearType, this and above font has been provided by MS Windows 7.
[attachment=0]03-soft-songti.png[/attachment]

[attachment=1]02-d2d-songti.png[/attachment]

Windows 7 X64, VS2012, Latest JUCE tip

Pages