CS551 - Self-Study Exercises

Exercise 5: Fonts and Font Resources

In this assignment, you learn some basics about X's font service, including two standard X clients (xfontsel and xfd) that can save work as you develop new X applications. You will also try out different methods for passing arguments to an X application.

Experienced X programmer periodically re-visit their application defaults file. Settings added later in development often override earlier settings, making them redundant or useless. For the sake of both maintenance and ease-of-use, your final application should not include any extra, non-functional resources or widgets. Re-check each resource setting to be sure that it does something useful (commenting them out, or overriding them through command-line options is a good way of doing this); if not, be sure to remove it.

You should have read Chapter 2.5 in Nye and O'Reilly, Vol. 4, before starting this exercise.


  1. Make a new copy of your program boxes, calling it fonts.

    If the application name is now fonts, what should the resource defaults file be called, according to X naming conventions? Which file(s) will this name appear in?

    Make all necessary changes so that the program works properly with the new name.

    Use the minimum number of resources necessary to alter the sizes of the button and box3, so that they are the same length as the label.

    Should these be hardcoded, or in a resource file? Why?

    Note that the label on the button now appears centered. Add the resource(s) necessary to left-justify the text.

  2. Experiment with the resource file in order to change the fonts used by the application. Read the description of fonts in Appendix B (section B.2). There is an X client that facilitates browing through font styles, xfontsel. Read the manpage for this client, and invoke it. If the display of characters at the bottom of the window looks like Chinese, pull down the "fndry" menu and choose "adobe". You can change all the other attributes using their pulldown menus.

    Use xfontsel to choose a new font for all text. Note that you can use the select option of xfontsel to copy-and- paste the name directly into your copy of .Xdefaults.

    How could you set the quit button to use a different font than the Label does?

  3. Another standard X client, xfd, can be used to view all the characters available in a particular font and the character code with which each can be accessed. Invoke this with the full font specifier you selected earlier; for example:
    xfd -fn "-adobe-times-*-*-*-*-*-*-*-*-*-*-*-*"
    Remember that you should never really have to type strings like this; whenever you need a font specifier, use xfontsel to generate the string, applying its select option to copy the string into the buffer so that you can paste it into any other editable window.

  4. The full X font names are something of a pain to use. Most intalled versions of X define some more convenient names, called aliases, that can also be used to reference fonts. Find the standard location where those font aliases are defined, using the information on What's Where in X.

    Try out a couple of aliases, like 9x15, heb6x13 and variable. Take a look at them using xfd, then use one or more in your program by specifying them as font resources.

    How do you think X's alias names work?

  5. If you just want to change the "default" font (used for all text unless the widget's resource setting has overridden it), you can also do this via a standard command-line option.

    Show how you would change the font to a font alias, using the command-line option.

    Will the same mechanism work if you want to use a full font specifier (such as -adobe-times-*-*-*-*-*-*-*-*-*-*-*-*)? Prove your answer.

  6. Use xfd to determine the octal constant for the ASCII character for dollar sign. Then embed this octal constant in the Label so that it shows as: Long$Label$Goes$Here [Hint: click on the character you want.]

    From your knowledge of strings in C, how else could you have embedded the dollar signs?

  7. Most X applications define their own default font, which will be used whenever the use does not override it via a resource file setting or a command-line option. To do this, you must create a fallback resource setting. Define it as shown in Section 10.2.8 of Vol. 4):
    	static String fallback_resources[] =
    		{"Fonts*font: variable",
    		 NULL,};
    
    (Make sure you choose a font that is distinct from the default used when nothing is specified at all.)

    What is the purpose of the "extra" NULL as the last element in array fallback_resources?

    To inform the X Toolkit manager that you are providing a fallback resource setting, you must refer to it as the next-to-last argument in XtVaAppInitialize.

    Add the code necessary to do this. Test your application by invoking it with and without additional font settings. When no font is specified in either the application defaults file or on the command-line, your fallback resource setting should be used. When the user specifies a font, it should take precedence over the fallback resource.

  8. Not all X servers can handle all font specifications. To see what happens, purposely introduce a spelling error into your fallback resource setting (e.g., "9x15x"), compile and run the application.

    Where does the error message appear?

    Fixing this problem requires quite sophisticated use of the Xlib routines. For the time being, you should be aware that not all fonts are supported, and you should test out your application on more than one type of machine display, noting whether or not the fonts change.