font
Guide: Styling and themes
Makes named fonts, changes them, and measures text in a font.
font SUBCOMMAND ?ARG ...?
Description
A font is a font description or the name of a named font. A description is a list: a family, a size, and style words, such as {Helvetica 12 bold}. It can also be a list of options and values, such as {-family Times -size 10}. The style words are normal, bold, roman, italic, underline, and overstrike.
A size above 0 is in points, and a size below 0 is in pixels. [tk scaling](cmd:tk scaling) gives the count of pixels in one point.
A named font has a name and the six attributes -family, -size, -weight, -slant, -underline, and -overstrike. A widget with a named font in its -font option draws again when the font changes. The standard fonts, such as TkDefaultFont and TkFixedFont, are named fonts. Each one gives the font of the theme until a script sets one of its attributes.
ztk draws text with the faces in its package: Inter Regular and Bold, and JetBrains Mono Regular. A monospace family, such as Courier, gets JetBrains Mono, and each other family gets Inter. On Linux, the ztk program also finds the installed fonts, and a family of an installed font gets its nearest face. On Windows, DirectWrite draws the fonts of the system. The headless backend uses only the faces of the package. [font load](cmd:font load) adds the faces of a font file of the app, and their family comes before each other face of that name.
Subcommands
| Subcommand | Description |
|---|---|
font create | Makes a named font. |
font configure | Gives or sets the attributes of a named font. |
font actual | Gives the attributes of a font description or a named font. |
font delete | Deletes one or more named fonts. |
font families | Gives the names of the font families that ztk knows. |
font load | Reads a font file of the app and gives its family. |
font measure | Gives the width of a text in a font, in pixels. |
font metrics | Gives the heights of a font and tells if its characters have one width. |
font names | Gives the names of the named fonts. |
Examples
% font measure {Courier 12} abc
29
% font create Title -family Helvetica -size 18 -weight bold
> font actual Title -weight
bold
Errors
% font
wrong # args: should be "font option ?arg?"
-errorcode TCL WRONGARGS
% font size x
bad option "size": must be actual, configure, create, delete, families, load, measure, metrics, or names
-errorcode NONE
Differences from Tcl
font actualgives the family of the description, such asHelvetica, and not the family of the face that draws the text. Tk 8.6.16 gives the family that it found on the system, such asNimbus Sans.- A size in a description or in
-sizecan have a fraction, such as12.5, and ztk drops the fraction. Tk 8.6.16 givesexpected integer but got "12.5". - A size in pixels, such as
-size -20, is kept as the size in points:font configure F -sizegives 15. Tk 8.6.16 keeps -20. - The errors have the
-errorcodeNONE, except a wrong count of arguments, the bad name of an option, and a bad value of an attribute. Tk 8.6.16 gives codes such asTK LOOKUP FONT. font cgivesbad option "c". Tk 8.6.16 givesambiguous option "c".
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1