X Font Names

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 39.  Fonts and Text Attributes


Fonts can be specified with X font names on all platforms, and you must use X font names in versions of Tk before Tk 8.0. The name fixed is an example of a short X font name. Other short names might include 6x12, 9x15, or times12. However, these aliases are site-dependent. In fact, all X font names are site dependent because different fonts may be installed on different systems. The only font guaranteed to exist on the UNIX platform is named fixed.

The more general form of an X font name has several components that describe the font parameters. Each component is separated by a dash, and the asterisk (*) is used for unspecified components. Short font names are system-defined aliases for these more complete specifications. Here is an example:

 -*-times-medium-r-normal-*-18-*-*-*-*-*-iso8859-1 

The components of X font names are listed in Table 39-2 in the order in which they occur in the font specification. The table gives the possible values for the components. If there is an ellipsis (...), then there are more possibilities, too.

Table 39-2. X Font specification components.
ComponentPossible values
foundryadobe xerox linotype misc ...
familytimes helvetica lucida courier symbol ...
weightbold medium demibold demi normal book light
slanti r o
swidthnormal sans narrow semicondensed
adstylesans
pixels8 10 12 14 18 24 36 48 72 144 ...
points0 80 100 120 140 180 240 360 480 720 ...
resx0 72 75 100
resy0 72 75 100
spacep m c
avgWidth73 94 124 ...
registryiso8859 xerox dec adobe jisx0208.1983 ...
encoding1 fontspecific dectech symbol dingbats

The most common attributes chosen for a font are its family, weight, slant, and size. The weight is usually bold or medium. The slant component is a bit cryptic, but i means italic, r means roman (i.e., normal), and o means oblique. A given font family might have an italic version, or an oblique version, but not both. Similarly, not all weights are offered by all font families. Size can be specified in pixels (i.e., screen pixels) or points. Points are meant to be independent of the screen resolution. On a 75dpi font, there are about 10 points per pixel. Note: These "points" are different than the printer points Tk uses in screen measurements. When you use X font names, the size of the font is not affected by the Tk scaling factor described on page 580.

It is generally a good idea to specify just a few key components and use * for the remaining components. The X server attempts to match the font specification with its set of installed fonts, but it fails if there is a specific component that it cannot match. If the first or last character of the font name is an asterisk, then that can match multiple components. The following selects a 12-pixel times font:

 *times-medium-r-*-*-12* 

Two useful UNIX programs that deal with X fonts are xlsfonts and xfontsel. These are part of the standard X11 distribution. xlsfonts simply lists the available fonts that match a given font name. It uses the same pattern matching that the server does. Because asterisk is special to most UNIX shells, you need to quote the font name argument if you run xlsfonts from your shell. xfontsel has a graphical user interface and displays the font that matches a given font name.

Font Failures before Tk 8.0

Unfortunately, if a font is missing, versions of Tk before 8.0 do not attempt to substitute another font, not even fixed. Current versions of Tk do substitutions only if you use platform-independent font names. Otherwise, the widget creation or reconfiguration command raises an error if the font does not exist. Example 39-1 shows one way to deal with missing fonts, which is to create a wrapper, the FontWidget procedure, around the Tk widget creation routines:

Example 39-1 The FontWidget procedure handles missing fonts.
 proc FontWidget { args } {    # args is a Tcl command    if {[catch $args w]} {       # Delete the font specified in args, if any       set ix [lsearch $args -font]       if {$ix >= 0} {          set args [lreplace $args $ix [expr $ix+1]]       }       # This font overrides the resource database       # The "fixed" font is UNIX-specific       set w [eval $args {-font fixed}]    }    return $w } 

You call FontWidget like this:

 FontWidget button .foo -text Foo -font garbage 

The FontWidget procedure reverts to a default font if the widget creation command fails. It is careful to eliminate the font specified in args, if it exists. The explicit font overrides any setting from the resource database or the Tk defaults. Of course, widget creation might fail for some more legitimate reason, but that is allowed to happen in the backup case. Again, the missing font problem disappears when you use platform-independent font names, so you only need to resort to using FontWidget in early versions of Tk.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net