This is definitely a trickier problem than it sounds at first. As you discovered, the canvas's
scale operation does change the scale of items on the canvas. But for text items, scaling only affects their positions, not the font size. The canvas isn't unique in this behavior. There are plenty of drawing programs out there don't don't scale font size when you scale items that you've drawn.
To solve this problem, you're going to have to write some code that finds all the text items that you're scaling, queries their font attributes, and then scales their font sizes appropriately. Here's a first try at a procedure to do all of this:
Code:
proc zoom {canvas scale} {
$canvas scale all 0 0 $scale $scale
foreach item [$canvas find all] {
if {[$canvas type $item] == "text"} {
set font [font actual [$canvas itemcget $item -font]]
set index [lsearch -exact $font -size]
incr index
set size [lindex $font $index]
set size [expr {round($size * $scale)}]
set font [lreplace $font $index $index $size]
$canvas itemconfigure $item -font $font
}
}
$canvas configure -scrollregion [$canvas bbox all]
}
Note the use of the
font actual command in there. It returns a full font specification of the font given as an argument. You need because if you use a "short form" font specification like "helvetica 16 bold", that's exactly what you'd get back when you queried the item's font value. I need the full specification so that I can search for the "-size"
Also note that when computing the new font size, I had to round it to the nearest integer value. I just discovered while writing this routine that Tcl doesn't support fractional font sizes.
A better approach would be to create named font "styles" with the
font create command and use these named styles for all text that you place on the canvas. Then, when you want to scale the font size, just modify the definitions of the font styles and those chages are automatically applied to all text using those styles. For example:
Code:
font create title -family gillsans -size 14 -weight bold
font create body -family {Lucida Sans} -size 10
canvas .c
pack .c
.c create text 50 50 -anchor nw -text "Avia Training" -font title
.c create text 50 100 -anchor nw -text "and Consulting" -font body
# Later on, when you want to scale the size
set size [font configure title -size]
set size [expr {round($size * 0.8)}]
font configure title -size $size
set size [font configure body -size]
set size [expr {round($size * 0.8)}]
font configure body -size $size
And yes, you are going to have the same kind of problem with bitmaps or images displayed on a canvas. These items are considered by the canvas to have a fixed size. If you want to scale the sizes of these objects, you'll need to do some image manipulation on them. Read up on the
image command and the
photo type for more information on image manipulation. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax