Freetype2 + Harfbuzz for font rendering

For code related discussions and questions
Post Reply
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Freetype2 + Harfbuzz for font rendering

Post by Vincent »

I implemented font rendering using freetype 2 and harfbuzz directly instead of using a third party lib as quesoglc.

Quesoglc is rather old, it still relies on fixed function pipeline which means it can't be used with an OpenGL Core context (which is mandatory to use GL 3.1+ on Linux Mesa and OSX). The current implementation is also using glScale to scale font, I don't know if it's a limitation of QuesoGLC or not, but it makes text blurry.
There are others libs like Freetype-gl but it's tied to OpenGL atm. There is no support for Vulkan or Metal afaik. Currently Warzone 2100 is OpenGL only but I think it would be good not to make usage of another API impossible because of a third party.

Using harfbuzz and Freetype 2 is not that hard so far, it takes around 100-200 lines of code to implement a basic font rendering infrastructure in Warzone 2100 ; this means code that can shape text (ie transform utf8 or utf16 string to a list of glyph (the "visual characters") and position that supports langage dépendent ligature and kerning) and rasterize glyph into a texture atlas on the fly, for different texture font. What's missing is the usage of bold characters and handling font file loading for different platform.
It adds 2 dependencies : freetype2, which is already a dependency of quesoglc, and harfbuzz (which does the shaping btw).

For reference I did a capture of the main menu on Windows with quesoglc and harfbuzz + FT2.
glc
glc
harfbuzz
harfbuzz
The donate link is quite blurry in the glc screen while it's not in the harfbuzz case.

I would like to know if I can continue working on this ; as far as I know it was planned to switch to freetype-gl however it is in discussion since 2013 so I don't know what the current status is.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Freetype2 + Harfbuzz for font rendering

Post by NoQ »

Some bikeshedding:
(1) Woohoo, would it fix our problems with Chinese(?)
(2) Yeah, small fonts look cool.
(3) However, something strange is going on with large fonts (too much hinting, can it be?) For instance, in the word "Multi", why is "l" so much closer to "u" than to "t"? I feel strange about it, and for me personally it looks worse than the blurry text on the first screenshot. I don't think it'd be hard to fix, just noticed. The main thing is, how the normal size looks (eg., new skirmish game screen).
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

Is there an issue with Chinese font with current master ? In theory harfbuzz and freetype 2 should support Chinese text rendering properly ; but quesoglc should do too. Text rendering is a chain which starts from proper encoding (is const char* a properly UTF8 string or is it a latin1 string ?), then there is font support (does DejaVu support all glyphes, in chinese case the CJK set ?) and at last the shaping.
I think DejaVu doesn't support CJK. On Google people are hinting at Droid Sans as a replacement (looks like it uses DejaVu and add glyph for CKJ)

The variable distance between character is a feature of harfbuzz (kerning), it can be disabled.

[EDIT] Added skirmish menu screenshot:
skirmish
skirmish
Cyp
Evitcani
Evitcani
Posts: 784
Joined: 17 Jan 2010, 23:35

Re: Freetype2 + Harfbuzz for font rendering

Post by Cyp »

Kerning shouldn't be disabled, but it looks like something went wrong with the kerning there. (Is the kerning offset between two letters being subtracted from the kerning offset of the next two, or something like that?)
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

I found what's wrong, I need to implement glyph sub pixel positioning (character are wrongly positioned because of rounding error to snap them on the grid) It shouldn't be hard.
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

Now with subpixel positionning and light autohint :
wz2100hb4.PNG
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

Is it ok/possible to raise the minimum resolution to 800x600 or even 1024 x 768 ?
As far as I know 1024x768 was the minimum resolution required for netbook under Windows 8 so this makes to use it as a minimal requirement.

This increase in resolution would make possible to raise virtual dpi to 96 which is the default value on Windows and likely others OSes (currently the value is 72). This would make text bigger and looks a lot better but will require some tweaking to the main menu (others menu seems to automatically scale).

Here visual results :
wz2100hb5.PNG
skirmish2.png
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Freetype2 + Harfbuzz for font rendering

Post by Per »

It would be really nice to get rid of quesoglc.

Do you have the code available somewhere?
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Freetype2 + Harfbuzz for font rendering

Post by Per »

Have you considered using freetype-gl (https://github.com/rougier/freetype-gl) instead of reinventing the wheel? I haven't looked closely at the font rendering quality of it, but its graphics pipeline seemed quite good last time I looked. I also have some old code to implement it in Warzone at https://github.com/perim/warzone2100/commits/gfxqueue that maybe you want to have a look at (warning: fairly old now). As usual, I got a bit carried away / too ambitious, and it never got finished.
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

The issue with freetype-gl is that it seems to work only with GL and doesn't support others API like Vulkan.

Additionnally freetype-gl doesn't look like it saves a lot of code when using in conjunction with harfbuzz : the sample code https://github.com/rougier/freetype-gl/ ... harfbuzz.c is nearly 300 lines long while my patch changes 400 lines in text_draw.cpp ; it's likely that using freetype-gl with harfbuzz in wz2100 will result in the same amount of code.
Actually freetype-gl only wraps freetype font rendering and provide a texture atlas however harfbuzz requires to redo all the metrics computation (x_advance, x_offset...) ; so the only benefits of ftgl are the nicer c++ api and an utility to atlas glyphes at the cost of proper subpixel rendering (I tried to use an atlas but it translated to either wrong font positionning or non grid fitting glyphes), gfx api support.

Of course we could also not use harfbuzz but that means dropping support for proper kerning and complexe layout support (required for instance for Arabic or hindic scripts).
Vincent
Trained
Trained
Posts: 103
Joined: 06 Aug 2016, 17:24

Re: Freetype2 + Harfbuzz for font rendering

Post by Vincent »

Post Reply