Version 3 of string geometry

Updated 2013-11-07 19:54:27 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]

regexp normally stops at the first match, and returns 1 then (or 0 if there were none). With -all, it returns the number of matches, so we can use it as a character counter.

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
 }