Tk Source Code

View Ticket
Login
Ticket UUID: 9fd94c63b0882081ff8ce465b296ee21881dfcae
Title: Text containing "wide" character not correctly refreshed
Type: Bug Version: 8.6
Submitter: hejin517 Created on: 2021-12-29 13:44:10
Subsystem: 05. Canvas Items Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2022-01-01 08:32:09
Resolution: None Closed By: nobody
    Closed on:
Description:
When the app runs it first shows a black character "f" in Times New Roman.
By clicking the button, I expect that the color of the whole character will be changed to red, but actually only part is changed.

In FontForge (a font editor), I find the character "f" in Times New Roman is wider than its "width".
Please look into this problem. Thanks.

Code to reproduce:
----------------------
pack [canvas .c -width 500 -height 500]
font create .f -family "Times New Roman" -size 200
set t [.c create text 200 200 -text f -font .f]
pack [button .b -text "Change Color" -command ".c itemconfig $t -fill red"]
User Comments: hejin517 added on 2022-01-01 08:32:09:
Thanks marc_culler. I guess that appending a space extends the redraw region to the size big enough to cover the text. I found another workaround that create a rectangle the same size as the canvas, and set its color to its current color when text should be updated.

----------------------
set width 500
set height 500
pack [canvas .c -width $width -height $height]
font create .f -family "Times New Roman" -size 200
set r [.c create rectangle 0 0 $width $height -fill white]
set t [.c create text 200 200 -text f -font .f]
pack [button .b -text "Change Color" -command ".c itemconfig $r -fill white; .c itemconfig $t -fill red"]
----------------------

I hope that the correct (extended) redraw region can be retrieved somewhere and used by Tk so that no workarounds are needed.

Since I’m not clear about how the redraw process internally works, please correct if I’m wrong.

marc_culler (claiming to be Marc Culler) added on 2021-12-31 00:11:56:
What I said about kerning is not right.  For digital fonts (not actual
type) it is possible to make characters overlap with kerning.

Nonetheless, I still think it is an error to have a character extend
out of its enclosing box.  Whether I am correct or not, according to
the internet, it is fairly common for fonts to have glyphs like that.

I verified with your program that on macOS the Times New Roman font has
ligatures disabled and simulates ligatures with kerning.  The pair "fi"
is kerned enough that the top of the "f" overlaps part of the "i" and
the dot on the "i" touches the "f".  But the ligature is not used.  It
would look much better if they did use ligatures.  Presumably that would
involve text processing that Apple does not consider worth the effort.
One should use LaTeX to do typesetting of that quality.

The bottom line, though, is the same.  You should add a thin space on
the right of your "f" to ensure that the entire glyph gets updated when
the font is reconfigured.  Tk does not have access to the actual font
metrics, such as widths of characters and kerning tables.  It simply asks
the OS to compute the width and height of a string when set with a
specified font.  If the OS reports a width less than the actual display
width there is nothing that Tk can do about it.

marc_culler (claiming to be Marc Culler) added on 2021-12-30 16:25:56:
Incidentally, a workaround would be to follow your letter f with a
narrow space character.  Perhaps the unicode character U+202F would
work.

marc_culler (claiming to be Marc Culler) added on 2021-12-30 15:37:24:
I think you are reporting a font design error.  Your Times New Roman is
probably a substitute font which contains some shortcuts that are errors.
I do not think it is a Tk bug.

I think it is an error (in the font) for the glyph for the letter f to be
wider than the character width.  There are many cases where the letter f
overhangs the next character, notably the ligature "fi".  Not all fonts
support ligatures, and they may support different sets of ligatures.
A font without any ligatures involving the letter f cannot make the f
overhang the i to its right, no matter what value is used for the kern.
The designer of your (probably fake) Times New Roman font may have tried
to compensate for the lack of ligatures by using a hack which consists
of making the f extend outside of its enclosing box.

Typesetting is complex, and Tk does not attempt to be a typesetting program.
Tk simply uses what the OS provides.  (And Apple is probably providing
the original font as designed by the Monotype foundry for the Times of
London, while your other OS's are probably providing substitutes.)

hejin517 added on 2021-12-30 04:45:10:
Hi nab,
On Linux both Tk8.6 and Tk8.7 have the problem.
I also found the problem in Python's tkinter, which wraps Tk8.6, on Windows and Linux.
Here's my another post.
https://bugs.python.org/issue46189

nab added on 2021-12-29 17:10:03:
Hi,
no issue with this script on macOS running Tk8.7

++