• canvas: change the background of a text item

    From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Oct 30 13:00:43 2024
    From Newsgroup: comp.lang.tcl

    Is there a way to change the background of a text item on a canvas?
    I can change the color of the text via -fill, but I found no way to
    change the background color of the text:

    pack [canvas .c] -fill both -expand yes
    # red text on yellow background
    .c create text 100 100 -text "Example text" -font {Helvetica 20} -fill red

    how to change the background of the text field to yellow?
    i.e. the same as ".c configure -background yellow", but restricted
    to the area of the text.

    TNX
    R'
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Oct 30 14:42:02 2024
    From Newsgroup: comp.lang.tcl

    * Ralf Fassel <ralfixx@gmx.de>
    | Is there a way to change the background of a text item on a canvas?
    | I can change the color of the text via -fill, but I found no way to
    | change the background color of the text:

    | pack [canvas .c] -fill both -expand yes
    | # red text on yellow background
    | .c create text 100 100 -text "Example text" -font {Helvetica 20} -fill red |
    | => how to change the background of the text field to yellow?
    | i.e. the same as ".c configure -background yellow", but restricted
    | to the area of the text.

    Ok, found a workaround on the WIKI:

    https://wiki.tcl-lang.org/page/canvas+text

    - create a rectangle with the bbox of the text and lower it beneath
    the text.

    Somehow the 'rectangle' item was not there when I scanned the canvas
    manpage earlier :-/

    R'
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From undroidwish@undroidwish@googlemail.com to comp.lang.tcl on Wed Oct 30 20:29:38 2024
    From Newsgroup: comp.lang.tcl

    On 10/30/24 14:42, Ralf Fassel wrote:

    Ok, found a workaround on the WIKI:

    https://wiki.tcl-lang.org/page/canvas+text
    ...

    thanks for digging, that's intriguing. The XLFD allowed in ancient times
    for transformations, didn't know that. Not that it is relevant for now,
    since we all render our texts in Tk using libxft. But until 2.3.5, that library, too, had still problems scaling/rotating/translating
    bitmap fonts (especially the color emojis).

    - create a rectangle with the bbox of the text and lower it beneath
    the text.

    And yes, the challenge now is to make the rectangle (ahem, rotated, a
    polygon) for a somewhat rotated text out of its bbox (if you'd like to
    have a text background).

    And please, enhance the wiki in this regard.

    BR,
    Christian
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Wed Oct 30 17:42:46 2024
    From Newsgroup: comp.lang.tcl

    On 10/30/2024 3:29 PM, undroidwish wrote:
    And yes, the challenge now is to make the rectangle (ahem, rotated, a polygon) for a somewhat rotated text out of its bbox (if you'd like to
    have a text background).


    Not sure when it became available but you can rotate text on a canvas
    using the -angle option. As for the background, the rectangle option
    works. Since, in this case, you will have the coordinates for the
    rectangle, you can translate the four coordinates to match the angle of
    the text. Not sure at all whether bbox can help you with the rotated stuff.

    Tkpath is worth checking out. As I recall, it could rotations before Tk.



    --- Synchronet 3.20a-Linux NewsLink 1.114