--- library/text.tcl +++ library/text.tcl @@ -484,11 +484,37 @@ } } +# ::tk::TextIsCombining -- +# Internal helper: returns 1 if the character is a zero-width combining +# or bidi-control character (combining diacritics, Hebrew points and +# accents, Arabic vowel points, bidi embedding and directional marks, +# variation selectors), 0 otherwise. Such characters render no pixels +# of their own: they are drawn over the preceding base character, so all +# the indices of a cluster (base character + its marks) occupy the same +# pixels on screen. +# +# Arguments: +# ch - The character to test (a string of length 0 or 1). + +proc ::tk::TextIsCombining {ch} { + regexp -- {[\u0300-\u036F\u0483-\u0489\u0591-\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7-\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u0900-\u0903\u093A-\u093C\u0941-\u0948\u094D\u0951-\u0957\u200B-\u200F\u202A-\u202E\u2060-\u2064\uFE00-\uFE0F\uFE20-\uFE2F]} $ch +} + # ::tk::TextClosestGap -- # Given x and y coordinates, this procedure finds the closest boundary # between characters to the given coordinates and returns the index # of the character just after the boundary. # +# The boundary is snapped to the edge of a grapheme cluster (a base +# character plus its combining marks). The previous implementation split +# at half the width of the character under the pointer; when that +# character is a zero-width combining mark (e.g. Hebrew nikkud or teamim +# in pointed RTL text), the boundary fell *inside* a cluster, where +# several indices map to the same pixels. With a bidi rendering each +# pointer movement could then oscillate between those indices, making +# the mouse selection "dance" while dragging. Snapping every boundary to +# a cluster edge makes the selection visually stable. +# # Arguments: # w - The text window. # x - X-coordinate within the window. @@ -505,12 +531,55 @@ # out of the widget. # The bbox of a newline extends to the right edge of the widget, and # the gap after it is on the next line. [13aabdb3c1] - if {$y < [lindex [$w dlineinfo $pos] 1] || - $x - [lindex $bbox 0] < [lindex $bbox 2]/2 || - [$w get $pos] eq "\n"} { + if {$y < [lindex [$w dlineinfo $pos] 1] || [$w get $pos] eq "\n"} { return $pos } - $w index "$pos + 1 char" + # Back up to the start of the cluster: skip over the zero-width + # marks preceding (and possibly under) the pointer. Do not cross a + # line boundary while backing up. + set cstart $pos + set line [lindex [split $pos .] 0] + while {[::tk::TextIsCombining [string index [$w get $cstart] 0]]} { + set prev [$w index "$cstart - 1 char"] + if {$prev eq $cstart || [lindex [split $prev .] 0] ne $line} { + break + } + set cstart $prev + } + # A cluster of marks with no base character before it on this line + # (e.g. bidi embedding characters at the start of an RTL line) is + # attached to the following base character. + while {[::tk::TextIsCombining [string index [$w get $cstart] 0]]} { + set next [$w index "$cstart + 1 char"] + if {$next eq $cstart} { + break + } + set cstart $next + } + set bbox [$w bbox $cstart] + if {$bbox eq ""} { + return $pos + } + # Same half-width test as before, but on the base character of the + # cluster: the resulting boundary is a cluster edge, stable on + # screen. + if {$x - [lindex $bbox 0] < [lindex $bbox 2]/2} { + return $cstart + } + # End of the cluster: the first non-mark character after the base + # character, so the selection boundary never splits a base character + # from its marks. + set last $cstart + while {1} { + set next [$w index "$last + 1 char"] + if {$next eq $last} { + return [$w index "$last + 1 char"] + } + if {![::tk::TextIsCombining [string index [$w get $next] 0]]} { + return $next + } + set last $next + } } # ::tk::TextButton1 --