Using fonts: fonts.c

The previous module draws strings using the default font. We now show how to use fonts different from the default one. This is done by first loading a font, and then setting it as the font of the graphic context we use for drawing. The example module fonts.c use an array of colors for displaying the string in different colors.

First, we have to define a variable of type Font. This is necessary for loading the font.

  Font f;
After that the graphic context has been created, we can load the font, and setting it to be the font of the graphic context.
  /* load a font */
  f=XLoadFont(dpy, "-*-helvetica-bold-r-*-*-20-*-*-*-*-*-*-*");
  XSetFont(dpy, g, f);
The first instruction loads a font, given its name (the string -*-helvetica-...). To choose a font, use the program xfontsel. The second instruction sets f as the font of the graphic context. This means that any subsequent call to XDrawString will draw the character using this font.
Next: using font metrics.