views:

462

answers:

3

I'm experimenting with Xlib to gain a deeper understanding of how Linux GUI programs work. I've written a simple program that display "Hello, world" in a window, but it looks rather anachronistic since the text is not anti-aliased.

What is the best way to display anti-aliased text in X11? How is anti-aliasing implemented in GTK, Qt, and other toolkits?

+4  A: 

FreeType. GTK+ uses Pango, and Qt has its own text layout library, but both of them use FreeType in the end, and several apps (for example XTerm with antialiased fonts enabled) use FreeType through the lower-level libXft library which comes with Xorg.

hobbs
+3  A: 

The X protocol's text-rendering facilities do not support anti-aliasing and aren't used much these days. (I think the reason is that the X font protocol doesn't have any place for an alpha channel.)

GTK and Qt render text in the client using the FreeType library, getting a pixmap with an alpha channel as the result. If the X server supports the RENDER extension, the client can send that pixmap to the server to have it blended onto the display using its alpha channel. If the X server doesn't support RENDER, the client has to retrieve the region of the screen where the text is to be displayed (taking a small screenshot, basically), do the alpha blending client-side, and send the resulting opaque pixmap back to the X server to be displayed.

Wyzard
Marking accepted since this describes what actually happens at a low level. It surprises me that font handling is all on the client's side now.
Jay Conrod
It sounds like it'd be less efficient, but from what I've read, there's actually not much of a difference. Yes, the client now has to send rendered text pixmaps to the server, but it no longer has to query the server for all the font metrics data that it needs while doing text layout.It also means no more ugly XLFD strings. :-)
Wyzard
+4  A: 

FreeType is at the wrong level of the stack. It will only allow you to draw glyphs at certain places. Typically you need at least a font selection mechanism (supplied by Fontconfig) and a shaping engine (supplied by Pango or Qt).

Both Pango and Qt use a forked version of an abandoned FreeType layout engine, but this is being reconciled into the HarfBuzz project.

See also this post by Behdad Esfahbod: Pango vs HarfBuzz, and this longer and more comprehensive document: State of Text Rendering.

Adam Goode
The State of Text Rendering article was an interesting read. Thank you.
Jay Conrod