View Full Version : Setting the font size

02-12-2005, 12:31 PM
In XWIndows how do I set the font size to draw with? There's probably something simple I'm missing on this.

I know you can get a font's size with something like this:

int FontSize(Display Disp, char *FontName)
XFontStruct *FontInfo;
int Height;
FontInfo = XLoadQueryFont(Disp, "fixed");
Height = FontInfo.ascent + FontInfo.descent
XFreeFont(Disp, FontInfo);
return Height;

But how can I tell it I want to use font size 18 when I output text?

02-16-2005, 10:08 AM
Maybe an answer?

This is a crude way of setting the font size, its by no means very thorough though. If you read the font.alias file (not sure what its default location is, I found mine at /usr/X11R6/lib/X11/fonts/misc/fonts.alias) it has a list of alias names for fonts. Among the aliases you see things like 5x7, 5x8, 6x9 and so on. Those appear to be average character sizes, width x height.

You can load fonts based on these aliases. Like this:

Font font;
font = XLoadFont(cdisDisplay, "6x9");
gc = XCreateGC(/*Code Omitted*/);
XSetFont(cdisDisplay, Context, font);

Any text drawing done with that GC (XDrawText() function calls) will write using the font aliased by 6x9. This also permits wildcards, so you can do stuff like this:

font = XLoadFont(cdisDisplay, "*x9");
And it will load any font with x9 in the name. By using that it seems you can choose a font height (which is pretty close to selecting a font size) and it will look for a matching name or alias.

This is obviously not the best way of doing this, since not all sizes will have a convenient alias and I don't know how well you can predict what aliases will be available from one system to the next. The other thing it doesn't permit is selecting a font and size, since you are tied to the fonts that are aliases (which appear to be all fixed width fonts).

As a side note, you can take a look at fonts.dir files (do a search for then, their appears to be multiple) to get a full listing of the available fonts. No aliases, but you can load them by name, and wildcards are still permitted in loading them. So you can load "*helvetica*" to get the helvetica font, but no size. Not sure how to get a size and font together though.

Theres a number of functions that allow you to enumerate the fonts set in those files. But it appears that loading fonts is tied to what is set in these files.

Its not perfect, but its a pretty decent method of setting the size of a font.

Ken Fitlike
02-19-2005, 08:11 AM
Although you seem to have worked out a solution for yourself , I thought you might still be interested in this page (http://www.ac3.edu.au/SGI_Developer/books/XLib_PG/sgi_html/apa.html) (ie. Appendix A of Xlib Programming Manual (http://www.ac3.edu.au/SGI_Developer/books/XLib_PG/sgi_html/index.html) ) which seems to provide a more formal approach to enumerating and using fonts with xlib.

02-21-2005, 06:38 AM
Thanks, that clears up a lot of the questions I had left.