The FontManager and FontDialog Classes

Examining Fonts

The FontManager class in Tycho provides a simple interface to the arcane font specification that is used by the X window system. The FontDialog class in turn uses the FontManager to provide the user with a window within which fonts can be previewed and selected.

The simplest use of these classes is as follows:

::tycho::queryfont Times 12 Italic
This creates an interactive window that permits the user to try out the available fonts by selecting the family, size, and style (roman, bold, italic, and so on) from pull-down menus. Since some of the best X window fonts -- 9x15 and 10x20, for example, are particularly nice screen fonts -- the font manager provides these as a pseudo font "Screen." When the user selects a font, a sample is displayed.

The arguments passed to this procedure represent the initial font to be displayed, as its family, size, and style. Any number may be supplied (provided they remain in the right order). If none are supplied, the font default to "Helvetica 12 Roman." The style can be one of "Roman," "Italic," "Bold," or "Bold Italic" -- if omitted, it defaults to "Roman". Font family names and styles are all case-insensitive.

Setting Fonts Interactively

The FontDialog just given returns the selected font when the user presses OK, or the name of the initial font if the used presses Cancel. This can be used to set fonts in any Tycho text widget. For example, the following code will query the user for a font selection:
::tycho::queryfont Lucida 14 BoldItalic

The FontDialog class returns only a symbolic representation of the font. To convert this into a system-dependent font, use the ::tycho::font procedure. For example:

set font [::tycho::queryfont Screen 14]
eval ::tycho::font $font
The font manager goes to some lengths to find a reasonable alternative font if the requested one is not available on the system. For example, try selecting the "Screen" font in an italic style: you will get a courier font instead. The ::tycho::font procedure will always return a valid system font, even if it cannot find a reasonable one: as a last resort, it will return the font "fixed".

Font Utilities

Font Sizes

The font manager provides procedures that give applications more detailed control over fonts. Firstly, ::tycho::font can "shift" up or down by some number of sizes:

::tycho::font Screen 12 roman -2
The posted font name is a font that is two sizes smaller than the font given to the procedure. To access this facility directly, use ::tycho::FontManager::size:
::tycho::FontManager::size 11 +1
Since 11 is not a valid font size, this procedure rounds it up to the next valid size (12) and then selects the next size up again.

Font Families and Types

The font manager understands three "types" of font: serif, sans-serif, and constant-width. To find out the families in a particular type, use ::tycho::FontManager::family:
::tycho::FontManager::family serif
You can supply these types as arguments to ::tycho::queryfont and ::tycho::font, and the font manager will choose a font of the given type. Unless your installation of tycho has changed the font manager settings, this window will display the system-dependent name for 14-point Courier:
::tycho::font constant-width 14
Some applications may need more fonts than the font manager provides by default. To add a font family, use the ::tycho::FontManager::family again:
::tycho::FontManager::family serif Rockwell times
The first argument is the type of font; the second is the new family, and following arguments are alternate families that are tried if the given family cannot supply a font in the requested size and style. Bring up the font dialog again: you will see the new family in the Family menu.

FontManager class documentation
FontDialog class documentation
Tycho Home Page


Copyright © 1996, The Regents of the University of California. All rights reserved.
Last updated: 96/11/20, comments to: eal@eecs.berkeley.edu