The FontManager class in Tycho provides a top-level window within
which the user can select a font, together with a set of utilities
for controling X window fonts. The simplest use of the class is as follows:
::tycho::FontManager .fontmanager
.fontmanager centerOnScreen
The FontManager class can be used to set fonts in any Tycho text widget.
Opening a FontManager with a specified -okcommand option is one way
to do this.
For example, the following code will query the user for
a font selection, and then post the font name in a new window when
the user clicks the OK button:
::tycho::FontManager .fm \
-okcommand "::tycho::post \[.fm getCurrentFont\]"
.fm centerOnScreen
getCurrentFont
method until
the OK button is pushed.
For the convenience of applications that wish to directly control fonts, without user intervention, an instance of the FontManager class is created when Tycho starts up in tycho namespace. This instance, called .tychoFonts, is never mapped to the screen. By interacting with it through its public members, however, an application can check the validity of a font, find a font given a prioritized list, or query for the default fonts used in Tycho (which can vary by installation, since X window fonts are not standardized).
The "fontValid" method
of the FontManager returns zero or one
if the specified font is invalid or valid (installed on the current system).
For example, the following checks to see whether the font 9x15
is installed on your system:
if [::tycho::.tychoFonts fontValid 9x15] {
::tycho::post {The 9x15 font is available}
} {
::tycho::post {The 9x15 font is not available}
}
The "getFont" method takes as arguments a
font specification, and checks to see whether the font is
available on the current system. If it is, it return the font name.
Otherwise, return the empty string. A font specification consists of
four items, the family, point size, weight, and style. Common
families are "times", "courier", and "helvetica". Reasonable point
sizes are 12, 14, 18, and 24. The weight is typically either "medium"
or "bold", and the style is either "r" or "i" for roman or italic.
set font [::tycho::.tychoFonts getFont times 18 medium i]
if {$font != {}} {
::tycho::post "The font name is: $font"
} {
::tycho::post {No font was found}
}
Frequently, applications will wish to search through some set of
preferred fonts before resorting to the only font guaranteed to exist
on all systems, "fixed". The method "findFont"
takes a list of
specifications and tries to find a font that matches one in the list.
Specifications earlier in the list are given priority.
A specification is either a font name or a list with four items. The
items are the family, point size, weight, and style.
These items have the same meaning as that used in the
"getFont" method. For example, the following
code will try to use the 9x15 font, but if it is not installed,
it will try to use a courier font, and if that is not installed,
it will resort to "fixed".
::tycho::post [
::tycho::.tychoFonts findFont {
9x15
{courier 14 medium r}
}
]
The "defaultFont" method returns the name of a
preferred font from a list of possibilities.
The possible options are fixed, fixedBold, fixedItalic,
variable, variableBold, and variableItalic.
For example, to identify the font that Tycho is using as its
default fixed-width italic font, issue the following command
::tycho::post [
::tycho::.tychoFonts defaultFont fixedItalic
]