Updated 2018-05-02 20:09:32 by MattAdams

Command for working with Tk fonts, whether named or literal (“unnamed”).
http://purl.org/tcl/home/man/tcl8.5/TkCmd/font.htm

Has these subcommands:
font actual font ?-displayof window? ?option?
font configure fontname ?option? ?value option value ...?
font create ?fontname? ?option value ...?
font delete fontname ?fontname ...?
font families ?-displayof window?
font measure font ?-displayof window? text
font metrics font ?-displayof window? ?option?
font names

LV What resources are used to determine what fonts are reported by fonts families? Are they hard coded? [Later] Apparently not. From looking at Tk source code, the determination of fonts available is (window) system specific. For unix, there are two possible routes taken, depending on whether the FreeType support was configured into the system.

If you want background details on what a font really is, see Introduction to Fonts.

The way I understand the above doc and various writing on the subject, the font command is a way to abstract out font information. You create a font object that then can be used with your various widgets. By manipulating the font object, you can influence the fonts used in all widgets using that font object.

Does anyone have some sample code that uses the Tk option command and font objects to set various widgets?

How does one deal with language issues along with fonts - so that the user can change languages, and the appropriate fonts are used?

Are there color objects as well?

This command encapsulates a huge amount of knowledge that's difficult to locate otherwise. The newsgroup thread [1] explains several key points about practices under Microsoft Windows that we'll eventually transcribe here.

Here's how to change a font to italic without having to specify it in full (e.g. if you otherwise want to use the default settings):
 button .b ;# as guinea pig for testing
 array set attributes [font actual [.b cget -font]]
 set attributes(-slant) italic
 set font [eval font create [array get attributes]]
 .b configure -font $font

Found by Pat Thoyts in the Tcl chatroom. I think you can even bypass the "font create" bit and directly say
 .b configure -font [array get attributes] ;# RS

KPV I think you can actually do this in one line:
    $w configure -font "[font actual [$w cget -font]] -slant italic"

A frequently-asked question: how do I specify a default menu (for example) font? The answer: use the option database, as in
    option add *Menu.font $my_preferred_font

MG April 23rd 2004 - A little script that shows an example on all the fonts installed on the computer, in a Tk text widget. Move the mouse over a font to see it's name at the bottom, and click with Button-1 to see an example of the font in bold/italic/underline. Tested in Tcl/Tk 8.4 on Win XP only, but I don't see any reason it wouldn't work elsewhere...
 package require Tk
 set typeThis "The quick brown fox jumps over the lazy dog."
 entry .l -textvariable typeThis -width 30
 pack .l
 button .go -command "go" -text "Go!" -width 6 -underline 0
 bind . <Return> {go}
 bind . <Alt-g> {go}
 pack .go

 proc go {} {
   global typeThis fontOver

   bind . <Alt-g> {}
   bind . <Return> {}
   destroy .l
   destroy .go

   frame .t
   pack .t -expand 1 -fill both
   text .t.t -yscrollcommand {.t.sb set} -undo 1
   pack .t.t -side left -expand 1 -fill both
   scrollbar .t.sb -command {.t.t yview}
   pack .t.sb -side right -fill y
   label .l -textvariable ::fontOver
   set fontOver "Loading Fonts. Please Wait..."
   pack .l

   frame .demo
   pack .demo
   label .demo.d1 -text "Click a font for a demo"
   pack .demo.d1 -side left
   label .demo.d2
   pack .demo.d2 -side left
   label .demo.d3
   pack .demo.d3 -side left
   label .demo.d4
   pack .demo.d4 -side left
   update

   set i 0
   foreach x [lsort [font families]] {
     .t.t tag configure tag$i -font [list $x 12 {}]
     .t.t insert end "$typeThis\n" tag$i
     .t.t tag bind tag$i <Enter> "set ::fontOver [list $x]"
     .t.t tag bind tag$i <ButtonPress-1> "demoOf [list $x]"
     .t.t tag bind tag$i <Leave> {set ::fontOver ""}
     incr i
    }

   set fontOver ""
 };# go

 proc demoOf {x} {

   .demo.d1 configure -text "Font: $x"
   .demo.d2 configure -text "In Bold..."      -font [list $x 12 bold]
   .demo.d3 configure -text "In Italics..."   -font [list $x 12 italic]
   .demo.d4 configure -text "In Underline..." -font [list $x 12 underline]

 };# demoOf

Observations on the names of fonts appear here [2]. [Is there a better place for this reference?]


See also edit