Display strings: chars.c

Displays strings on the screen is very simple: just use the function XDrawStrings. For example, chars.c is a module in which instead of drawing squares, we write a string in a random position. This is done by replacing the call to XFillRectangle with:
      /* draw a string */
      XDrawString(dpy, root, g, random()%wa.width, random()%wa.height,
                  "Ooops!", strlen("Ooops!") );
The parameters of XDrawStrings are: the display, the window to write to, the graphic context, the x and y coordinates, the string to write, and the number of character in the string.

Note that the x and y coordinates are not the coordinates of the upper-left point of the first character. Assuming each letter to be contained in a box, the x represent the leftmost x of the box that contains the first character of the string. The y is instead the coordinate of the feet of the characters. The image below shows three strings: the red crosses are centered in the x,y coordinates of each string.

It is clear that the second string actually goes below the y cooordinate, while the third string does not touch the y axis.


The complete source code: chars.c.

To compile, test, and install, see: testing and installing a new module


Next: using fonts.