Jump to content
  • entries
    941
  • comments
    5,894
  • views
    867,617

Rendering Text on Linux with Xft


Josh

6,344 views

 Share

Diving into the innermost workings of the Linux operating system is a pretty interesting challenge. Pretty much nobody nowadays implements anything with raw X11 anymore. They use QT, SDL, or for drawing Cairo, with Pango for text rendering. The thing is, all of these libraries use X11 as the backend. I need full control and understanding over what my code is doing, so I've opted to cut out the middleman and go directly to the innermost core of Linux.

 

Today I improved our text rendering by implementing the XFT extension for X11. This library uses FreeType to make True-type Fonts renderable in a Linux window. Although the documentation looks intimidating, usage is actually very simple if you already have a renderable X11 window working.

 

First, you create an XFT drawable:

XftDraw* xftdraw = XftDrawCreate(display,drawable,DefaultVisual(display,0),DefaultColormap(display,0));

 

Now, load your font:

XftFont* xfont = XftFontOpen(Window::display, DefaultScreen(Window::display), XFT_FAMILY, XftTypeString, "ubuntu", XFT_SIZE, XftTypeDouble, 10.0, NULL);

 

Drawing is fairly straightforwardish:

XRenderColor xrcolor;
XftColor	 xftcolor;
xrcolor.red = 65535;
xrcolor.green = 65535;
xrcolor.blue = 65535;
xrcolor.alpha = 65535;
XftColorAllocValue(display,DefaultVisual(display,0),DefaultColormap(display,0),&xrcolor,&xftcolor);
XftDrawString8(xftdraw, &xftcolor, xfont, x, y + GetFontHeight(), (unsigned char*)text.c_str(), text.size());
XftColorFree(display,DefaultVisual(display,0),DefaultColormap(display,0),&xftcolor);

 

And if you need to set clipping it's easy:

XftDrawSetClipRectangles(xftdraw,0,0,&xrect,1);

 

Here is the result, rendered with 11 point Arial font:

blogentry-1-0-23187000-1473189923_thumb.png

 

As you can see below, sub-pixel antialiasing is used. Character spacing also seems correct:

blogentry-1-0-10080500-1473189959_thumb.png

 

By using Xft directly we can avoid the extra dependency on Pango, and the resulting text looks great. Next I will be looking at the XRender extension for alpha blending. This would allow us to forgo use of the Cairo graphics library, if it works.

  • Upvote 2
 Share

5 Comments


Recommended Comments

Fun fact: Resizing the window seems to invalidate the XftDraw object, so simply set it to NULL any time a window size event occurs:

XftDraws are internally allocated and freed by Xft; the programmer does not ordinarily need to allocate or free storage for them.
Link to comment
Quote

By using Xft directly we can avoid the extra dependency on Pango

Is correct, but instead we face to loss the Han characters, Persian, Arabic, Hebrew, Thai and many Indic scripts!

Link to comment

This info is a good start. XFT/FreeType feels a lot like leaving the highway and four-wheeling it through a swamp. Thanks.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...