Previous section   Next section

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

Table of Contents
Chapter 39.  Fonts and Text Attributes

Naming a Font

There are two basic ways to name a font. You can use predefined font names (e.g., system), or you can specify a set of font attributes with a platform-independent name:

label .foo -text "Hello" -font {times 12 bold}

In this form, the font is specified with a three element list. The first element is the font family, the second is the size, in points, and the third is a list of style parameters. The family determines the basic look, such as courier or helvetica.

The complete set of style parameters are normal, bold, roman, italic, underline, and overstrike. For example, to specify both bold and italic:

label .foo -text "Hello" -font {times 12 {bold italic}}

The font size is points, which are 1/72 inch. Tk maintains a scale factor that maps from points to pixels. The default scale is derived from the screen resolution, and you can change it with the tk scaling command, which is described on page 582. You can specify pixel-based sizes with negative numbers. The advantage of points over pixels is that text appears about the same size regardless of the screen resolution. (This works better on Windows and Macintosh than on Unix.) However, sometimes you want to control font size relative to other widget geometry, in which case pixel-based sizes are better.

An alternate way to name font attributes uses name-value pairs. These are summarized in Table 39-1. The format is less compact, but it is useful for changing part of a font configuration because you do not need to specify everything. The same specification can be made like this:

label .foo -text "Hello" -font \
     {-family times -size 12 -weight bold -slant italic}

Table 39-1. Font attributes.
-family nameThe name can be times, courier, helvetica, and others returned by the font families command.
-size pointsThe font size is given in points, which are 1/72 inch.
-weight valueThe value is bold or normal.
-slant valueThe value is roman or italic.
-underline boolIf bool is true, an underline is drawn.
-overstrike boolIf bool is true, an overstrike line is drawn.

Tk matches a font specification with the fonts available on your system. It will use the best possible font, but it may have to substitute some font parameters. Tk guarantees that the Times, Courier, and Helvetica families exist. It also understands the synonyms of Courier New for Courier, and Arial or Geneva for Helvetica.The font actual command returns the parameters chosen to match a font specification:

font actual {times 13 bold}
-family Times -size 13 -weight bold -slant roman 
    -underline 0 -overstrike 0

The Macintosh and Windows platforms have a system-defined default size. You can get this size by specifying a size of 0 in your specification. The system font uses this:

font actual system
-family Chicago -size 0 -weight normal -slant roman
    -underline 0 -  overstrike 0

Named Fonts

You can define your own names for fonts with the font create command. Creating a named font provides a level of indirection between the font parameters and the widgets that use the fonts. If you reconfigure a named font, the widgets using it will update their display automatically. This makes it easy to support a user preference for font size. For example, we can define a font name default on all platforms:

font create default -family times -size 12

The default font can be made larger at any time with font configure. Widgets using the fonts will update automatically:

font configure default -size 14

System Fonts

The Windows and Macintosh platforms have system-defined fonts that are used by most applications. When you query the configuration of the Tk widgets, you will see the system font names. The parameters for the system fonts can be tuned by the user via the system control panel. You can find out the attributes of the system font with font actual. These are the system fonts for each platform:

  • The Windows platform supports system, systemfixed, ansi, ansifixed, device, oemfixed. The fixed suffix refers to a font where each character is the same size.

  • The Macintosh platform has system and application.

  • The UNIX platform has fixed. This is the only X font name that is guaranteed to exist. X font names are described in the next section.

Unicode Fonts

Tk does character-by-character font substitution when it displays Unicode characters. This supports mixed display of ASCII and Kanji characters, for example. The great thing about this is that you do not have to worry too much about choosing fonts in the simple case. The problem with font substitution is that it can be slow. In the worst case, Tk will query every font installed in your system to find out whether it can display a particular character. If you know you will be displaying characters in a particular character set, you can optimize your interface by specifying a font that matches what you expect to display.

      Previous section   Next section