Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to scale text item in canvas?

Status
Not open for further replies.

charmdream

Programmer
Oct 26, 2001
25
US
I can use "(canvas) scale" command to scale the size of line, rectangle, ... items in canvas.
But how can I scale the text (make it bigger or smaller to some scale) in canvas?

Thanks a lot!
 
You have to use itemconfigure to change the font of the text:
Code:
set ::fontsize 10
grid [canvas .c -width 150 -height 100 -bg white] -row 0 -column 0 -columnspan 2
grid [button .p -text + -width 10 -command plus] -row 1 -column 0
grid [button .m -text - -width 10 -command minus] -row 1 -column 1
.c create text 0 0 -tags mytext -anchor nw -text "some text" -font [list Courier $::fontsize]
proc plus {} { .c itemconfigure mytext -font [list Courier [incr ::fontsize]] }
proc minus {} { .c itemconfigure mytext -font [list Courier [incr ::fontsize -1]] }
When you push the + or the - button, the corresponding proc updates the size of the font.

ulis

 
Thank you, Ulis!

But what I want is a function of changing the view size of all items in the canvas according to the percentage user chooses. Eg. if choosing 150%, all the items should be as 150% large as their original sizes. It would be better for users to choose any percentage. So I think the method you said may not be a perfect way for this function.

BTW, It seems that not only text items, but also bitmap, image items have the same problems.

Could it be done with tcl/tk?

thanks

 
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
 
Thank you for your suggestion.

But it seems that we cannot zoom the bitmaps and images in the canvas.
Maybe we can do it MANUALLY. But it is not a realistic way. :-(
Thanks anyway.

Is there anyone who can provide a good method?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top