Re: Underlining text on canvas
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Underlining text on canvas

From: Donal K. Fellows <donal.k.fellows@manchester.ac.uk>
Date: Mon Jan 02 2006 - 18:35:38 CET

bluetit wrote:
> Given a text tag on a canvas, I would like to underline one letter in
> that text... using font measure to get it's start and end point and
> then drawing a line with those coords doesn't do a good job (line does
> not evenly underline the character, with some characters it's to the
> right more, with others to the left more).

The underlining code (which the canvas widget doesn't expose IIRC) and
the [font measure] implementation share the same text measuring engine.
You're probably just making some simple error (like assuming that all
characters are the same width - they aren't in general). That said, it
is fairly easy to underline a character, as you can see from this little
script:

   # Basic setup
   set s "qwertyuiopasdfghjklzxcvbnm"
   set c s
   pack [canvas .c]
   .c create text 10 10 -anchor nw -tags txt -text $s

   # Make an underlined font
   eval font create foo [font actual [.c itemcget txt -font]]
   font config foo -underline 1

   # Make an underlined item and align it precisely in the right spot
   # First, make the item with the text to underline in the right font
   .c create text 10 10 -anchor nw -tags ul -text $c -font foo
   # Use an RE trick to get the text *before* the char to underline
   set precedingSubstring [regexp -inline ".*(?=$c)" $s]
   # Now compute the offset relative to the main text and move that
   # underlined character.
   .c move ul [font measure foo $precedingSubstring] 0

You probably do not need to create the font afresh each time. ;-)

Donal.
Received on Tue Jan 3 03:10:07 2006