Navigation Logo 12.5  Fonts Navigation Logo

 

 

Originally, Tcl adopted X Window's very flexible system for dealing with fonts. Tk's font names are in X Logical Font Description format. Here is a sample,

-*-arial-bold-r-normal--*-*-*-*-*-*

I do not intend to describe the format in any detail but the variable substitution in the following is illustrative of what you need to know:

-*-$Family-$Weight-$Slant-normal--$Size-*-*-*-*-*-*-*
The variables have these meanings:

Family

This is what we normally think of as the name of the font. Some possible values are: courier, helvetica, and times.

Weight

This describes how heavy the characters are. Use it to achieve bold face. Some possible values are: medium and bold.

Slant

Are the characters up and down like normal or do they slant sideways like italics. Some possible values are: italics, oblique, and normal.

Size

The size of the font as measured in points. This size is the same measurement your word processor probably shows you. Some possible values are: 8, 10, 12, 14, 17, and 24.

Widgets that deal with texts have a default font. You override this font with the -font option followed by a font description in the form explained above.

With version 8.0, Tk has a font action family. Once a font has been created and named you can use the name as the value for a -font option.

Create a font with one of these forms of the font create action.

font create ?OPTIONS?
Creates and names a new font with the properties specified by ?OPTIONS. Returns the name of the font.
font create NAME ?OPTIONS?
Creates a font named NAME with the properties specified by ?OPTIONS. Returns NAME.

Any options you do not specify will be given a default value. Here are some possible options.

-family
Possible values are "Courier," "Times," and "Helvetica."

-size
A positive value is measured in points, a negative value in pixels. Tk will choose the closest available font to the size you specify.

-weight
Possible values are "normal" and "bold."

-slant
Possible values are "roman" and "italic."

You cannot only use font names as the value of any -font option, but you can also use an option/value list made up from the options expected by font create. Figure 12.5a shows two ways of creating the same label.

Figure 12.5a: Using the font Procedure of Exercise 12.5a.
pack [label .lbl1 -text {Hellow Orld} \

-font {-family Times -slant italic -size 17 }] font create LabelFont -family Times -slant italic -size 17 pack [label .lbl2 -text {Hellow Orld} -font LabelFont] set LabelFont [font create -family Times -slant italic -size 17] pack [label .lbl3 -text {Hellow Orld} -font $LabelFont]

Exercise 12.5a

Finish implementing this procedure which is useful in Tk 4.2 or earlier.
proc font {Family Weight Slant Size} {
   ### Family: one of courier helvetica times
   ### Weight: one of b n  (for bold and normal)
   ### Slant:  one of i n  (for italics/oblique and normal)
   ### Size:   one of 8 10 12 14 17 24
   ### returns a font name usable on multiple platforms
...
}

Note that both Weight and Slant take different values than described above for X Windows fonts. You will need to translate these values. When translating the slant, map "i" to "italic" for the courier and helvetica fonts and to "oblique" for the times font. When translating the weight, map "n" to "medium."

The solution to this exercise is available as Script ES12.5a.

Solution

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.