Version 2 of string geometry

Updated 2013-11-07 19:51:18 by suchenwi

Richard Suchenwirth 2013-11-07 - Everything is a string, and every string has its string length. But if we want to display a string in a text widget with tight fitting (i.e. just the right size, not too big, not too small), we need to specify the geometry of a string, i.e. its width and heigth.

If we disregard word wrapping, things are very easy:

 set height [regexp -all \n $str\n]

I have appended an extra newline at the end, because many strings don't have (and in fact don't need) it. Perfectionists may first do string trimright before that. And here's the width of a string:

 proc string_width str {
     set w 0
     foreach line [split $str \n] {
        set length [string length $line]
        if {$length > $w} {set w $length}
     }
     return $w
 }