Search code examples
cfontsx11xlib

How to select and resize an Xlib font?


I am making a cross-platform graphics library (please don't ask why I am re-inventing the wheel) and want to add something I thought was simple: changing the size of fonts. Before, my Expose event looked like this:

XTextItem textitem;
textitem.chars = some_string;
for (textitem.nchars = 0; textitem.chars[textitem.nchars] != '\0'; textitem.nchars++); // Basically just strlen
textitem.delta = 0;
textitem.font = None;

XDrawText(display, xwindow, gc, x, y, &textitem, 1);

Using Quartz, you could just use [NSFont systemFontOfSize:12]. I was looking for something like that for Xlib.

Looking on the internet, I have found nothing at all about resizing fonts with Xlib. I have gone through the handbook looking what feels like dozens of times, I made countless google searches, but still I cannot find anything. I have found XLoadFont, but there is practically nothing online about it at all, and it will just always give a BadName error.

char* xfontid; asprintf(&xfontid, "-*-*-*-*-*-*-*-%d-*-*-*-*-*-*", fontsize);
textitem.font = XLoadFont(display, xfontid);
free(xfontid);

XDrawText(display, xwindow, gc, x, y, &textitem, 1);

Solution

  • Xlib fonts are fixed-size bitmaps. The only way to obtain a different size in theory is to install it on the system you're using. But no-one really uses xlib fonts for any practical purpose precisely because it's awkward and inflexible. Graphical programming in xlib is generally done using a toolkit like Gtk or Cairo.

    https://www.cairographics.org/manual/cairo-fonts.html