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

Discussion and support for general JUCE issues

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

Postby sonic59 » Sat May 05, 2012 10:22 pm

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
Image

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
Image

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
Image

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.
Last edited by sonic59 on Wed Oct 31, 2012 2:18 pm, edited 1 time in total.
sonic59
JUCE UberWeenie
 
Posts: 224
Joined: Tue Mar 09, 2010 5:51 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby TheVinn » Sat May 05, 2012 10:31 pm

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.
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby sonic59 » Sat May 05, 2012 11:20 pm

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 IDWriteFontFile 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.
sonic59
JUCE UberWeenie
 
Posts: 224
Joined: Tue Mar 09, 2010 5:51 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby SwingCoder » Sun May 06, 2012 1:00 am

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, VS2010, Latest JUCE tip
SwingCoder
JUCE UberWeenie
 
Posts: 1160
Joined: Thu Oct 07, 2010 4:44 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby TheVinn » Sun May 06, 2012 1:17 am

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


Yep and that's exactly why I didn't even try!
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby TheVinn » Sun May 06, 2012 1:18 am

sonic if you would like to contribute your demonstration applet to AppletJUCE that would be great!
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby sonic59 » Sun May 06, 2012 1:34 am

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.

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.

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.
sonic59
JUCE UberWeenie
 
Posts: 224
Joined: Tue Mar 09, 2010 5:51 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby TheVinn » Sun May 06, 2012 1:53 am

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?
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby sonic59 » Sun May 06, 2012 2:56 am

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:
Code: Select all
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
sonic59
JUCE UberWeenie
 
Posts: 224
Joined: Tue Mar 09, 2010 5:51 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby SwingCoder » Sun May 06, 2012 4:06 am

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

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

01-soft-msyh.png
01-soft-msyh.png (194.71 KiB) Viewed 1065 times


00-d2d-msyh.png
00-d2d-msyh.png (89.3 KiB) Viewed 1065 times
Windows 7 X64, VS2010, Latest JUCE tip
SwingCoder
JUCE UberWeenie
 
Posts: 1160
Joined: Thu Oct 07, 2010 4:44 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby SwingCoder » Sun May 06, 2012 4:16 am

Font: 宋体, not support ClearType, this and above font has been provided by MS Windows 7.
03-soft-songti.png
03-soft-songti.png (200.05 KiB) Viewed 1064 times


02-d2d-songti.png
02-d2d-songti.png (123.74 KiB) Viewed 1064 times
Windows 7 X64, VS2010, Latest JUCE tip
SwingCoder
JUCE UberWeenie
 
Posts: 1160
Joined: Thu Oct 07, 2010 4:44 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby SwingCoder » Sun May 06, 2012 4:18 am

Font: 行书, not support ClearType. 3rd-party font.
07-soft-Xingshu.png
07-soft-Xingshu.png (149.45 KiB) Viewed 1064 times


06-d2d-Xingshu.png
06-d2d-Xingshu.png (96.19 KiB) Viewed 1064 times
Windows 7 X64, VS2010, Latest JUCE tip
SwingCoder
JUCE UberWeenie
 
Posts: 1160
Joined: Thu Oct 07, 2010 4:44 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby SwingCoder » Sun May 06, 2012 4:20 am

Font: 丫丫体, not support ClearType. 3rd-party font.
09-soft-yaya.png
09-soft-yaya.png (211.89 KiB) Viewed 1064 times


08-d2d-yaya.png
08-d2d-yaya.png (115.71 KiB) Viewed 1064 times
Windows 7 X64, VS2010, Latest JUCE tip
SwingCoder
JUCE UberWeenie
 
Posts: 1160
Joined: Thu Oct 07, 2010 4:44 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby sonic59 » Sun May 06, 2012 4:27 am

Glad you got it working. Your samples are interesting.
How is the performance of JuceDirect2D your machine? Noticeably slow?
Performance is based on both CPU and GPU. I'm running on older hardware (Core 2 Duo, GF 9400m) so I'm wondering how it works on something faster.

It may be easier to test performance in the Juce Demo. You can get the Juce Demo to work with D2D by copying over those 6 files I mentioned in my previous post to your juce folder that the Juce Demo uses. You should ignore the render time as it is clearly wrong.

zh-d2d-clear.png
zh-d2d-clear.png (36.03 KiB) Viewed 1060 times


The glyphs I have pointed out seem pretty unreadable. Is that normal for that font at that size?
Last edited by sonic59 on Sun May 06, 2012 5:03 am, edited 1 time in total.
sonic59
JUCE UberWeenie
 
Posts: 224
Joined: Tue Mar 09, 2010 5:51 pm

Re: Direct2D Renderer (Font Hinting, Windows RT, Win Phone 8

Postby SwingCoder » Sun May 06, 2012 4:37 am

Known issues (d2d):

1) ClearType support seems not good enough, some words to show green or red.

2) CPU consumes too much, increase more than doubled when d2d enabled (12% -> 26%, Intel i7, 2600K).

3) the main interface can't responding anything after maximized the window, application suspended animation.
Windows 7 X64, VS2010, Latest JUCE tip
SwingCoder
JUCE UberWeenie
 
Posts: 1160
Joined: Thu Oct 07, 2010 4:44 pm

Next

Return to General JUCE discussion

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests