Tk Source Code

View Ticket
Login
2026-10-14
15:12 • New ticket [e7c85cd9] Revised text: right-to-left text is placed in logical order, so the selection, the cursor and clicks are mirrored. artifact: d67af238 user: nab
15:05 • Closed ticket [ecfcec3a]: Windows: a range of right-to-left text is measured one character too wide plus 6 other changes artifact: abccd9c7 user: nab
15:04
Fix [ecfcec3a40]: on Windows, a range of right-to-left text is measured with the leading edge of its characters, not one character too wide (test textDisp-37.3). check-in: a9090d02 user: nab tags: trunk, main
12:49 • New ticket [ecfcec3a] Windows: a range of right-to-left text is measured one character too wide. artifact: ecbe5f71 user: nab

Ticket UUID: ecfcec3a4025495d70ab90cbab549bebf57d6b7a
Title: Windows: a range of right-to-left text is measured one character too wide
Type: Bug Version: trunk
Submitter: nab Created on: 2026-10-14 12:49:40
Subsystem: 47. Win Fonts Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-10-14 15:05:18
Resolution: Fixed Closed By: nab
    Closed on: 2026-10-14 15:05:18
Description:

On Windows, Tk_MeasureCharsInContext gives a wrong width to a range of right-to-left text inside a longer string: a prefix of a Hebrew or Arabic run is measured one character too wide, and the last character of the run gets no width. The text widget, which measures its characters in context, shows it:

pack [text .t -font {Courier 12} -wrap none]
set heb "\u05e9\u05dc\u05d5\u05dd \u05e2\u05d5\u05dc\u05dd"
.t insert end $heb
update
for {set i 0} {$i < 9} {incr i} {
    puts "1.$i [lindex [.t bbox 1.$i] 2] [font measure {Courier 12} [string index $heb $i]]"
}

The bbox of the first letter is as wide as two letters, each next letter has the width of the letter after it, and the last one is 1 pixel wide (the zero-width fallback of CharBboxProc): 17 3 9 4 _ 3 7 9 1 for 1.0 to 1.8 (1.4 is the space), where macOS gives each letter the width of its glyph. The selection and the insertion cursor follow these positions. Found while porting the right-to-left layout to revised_text, where the letters of a selected Hebrew word were drawn over each other.

Cause: GetVisualXForLogicalIndex (win/tkWinFont.c) gives the x of the boundary before a character. In a right-to-left run it asked ScriptCPtoX for the trailing edge of that character, which is the boundary after it: every boundary inside the run moved by one character. The boundary before a character is its leading edge; the end of the run is the trailing edge of its last character. Unbounded measures of a whole string (font measure) are not affected; bounded ones (line breaking) are.

Branch nab-win-rtl-measure, from trunk b6303a5a40: the fix (07248aee92) and the test textDisp-37.3 (8d30dad374): the bbox of each letter of a Hebrew line has the width of the letter, within one pixel. Red on Windows without the fix; textDisp, text, winFont, font and entry give the same results with and without it otherwise (Windows 11, clang UCRT64, 200 % scaling).

Related: 1b22c74dc7 (a label with Hebrew text in Courier hangs, branch win-script-not-in-font) also changes Tk_MeasureCharsInContext, in another place: the two fixes merge cleanly.

User Comments: nab added on 2026-10-14 15:05:18:

Merged into trunk [a9090d02e3] with Kevin Walzer's go.