Tk Source Code

View Ticket
Login
Ticket UUID: 1185640
Title: Strange word and line selection in the text widget
Type: Bug Version: None
Submitter: jenglish Created on: 2005-04-19 04:37:16
Subsystem: 46. Unix Fonts Assigned To: jenglish
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2026-10-22 17:22:34
Resolution: Fixed Closed By: serhiy.storchaka
    Closed on: 2026-09-24 11:25:19
Description:
George Petasis reports on c.l.tcl:

| I have noticed a strange behaviour with the Tk text
| widget. When the widget displays non-english text
| (in my case greek), when I double-click (to select
| a word) or tripple-click (to select a line) in
| the widget, the appearence of the selected text is
| bizzare: from the character the click was done the
| text is displayed fine, but before this character
| no text is shown. Also, strange things can happen,
| like characters moving around, as if text was
| inserted/deleted around the selection.
                                                      
                         
Later:
                                                      
                         
| Which happens only under unix (linux fedora core 3)
| when I enable XFT support.  Without XFT, everything
| is normal...
User Comments: serhiy.storchaka added on 2026-09-24 11:25:19:

Fixed by [437a4bc8fb] (lax UTF-8 parsing in Tk_MeasureChars): not reproducible with 8.6 and 9.0. The text widget still measures a partial UTF-8 character in CharBboxProc, which gives wrong results with the new bidi text rendering in 9.1: [7579c335f1].


jenglish added on 2005-04-19 12:45:11:
Logged In: YES 
user_id=68433

The correct fix for #3 is probably to change "return
++curByte;" to "return curByte;" in the "this can't happen"
condition (tkUnixRFont.c r1.8 ~line 522).  Unfortunately
this can cause other parts of the code to enter an infinite
loop.  Haven't tracked this down other than to note that
test unixSelect-1.1 wedges, spins, and eventually OOMs. 
(Also note that test unixSelect-1.1 involves [encoding
convertfrom identity], which really really really needs to die.)

So next best thing is to address #2, and use the lax
Tcl_UtfToUniChar() instead of the strict FcUtf8ToUcs4.  This
introduces a bug in that Tk_MeasureChars can now return an
index larger than the 'numBytes' parameter passed in, but
apparently other parts of the core depend on this behavior.
 It still returns correct output for correct input. 

It looks like the cause of #1 is in
generic/tkTextDisp.c(CharBboxProc), ~lines 7141-7142:

|        MeasureChars(chunkPtr->stylePtr->sValuePtr->tkfont,
|              ciPtr->chars + byteIndex, 1, *xPtr, -1,
widthPtr);

Note the third parameter (numBytes) is 1, which is incorrect
if ciPtr->chars + byteIndex points to the beginning of a
multi-byte sequence.

Fix for #2 committed to CVS (unix/tkUnixRFont.c r1.9),
leaving open for now in case anyone wants to look into #1.

jenglish added on 2005-04-19 12:10:42:
Logged In: YES 
user_id=68433

Analysis of the problem:

(1) The text widget is triggering a "can't happen" condition
in Tk_MeasureChars(), namely, it's passing a (source,
numBytes) pair that ends in the middle of a UTF-8 sequence;

(2) The XFT version of Tk_MeasureChars uses FcUtf8ToUcs4()
to parse UTF-8 sequences, which signals an error (returns
-1) when fed an incomplete sequence (the other versions use
Tcl_UtfToUnichar, which doesn't take a length parameter and
assumes the input is valid);

(3) Tk_MeasureChars() returns garbage when this happens.