Tk Source Code

View Ticket
Login
Ticket UUID: 28a4535fa278d81abe48cb0de1396e2b5e32277d
Title: Off-by-1 error on text widget index on OS X
Type: Bug Version: core-8-6-branch
Submitter: fvogel Created on: 2017-01-01 20:44:15
Subsystem: 18. [text] Assigned To: fvogel
Priority: 6 Severity: Minor
Status: Closed Last Modified: 2017-01-07 15:16:03
Resolution: Fixed Closed By: fvogel
    Closed on: 2017-01-07 15:16:03
Description:

On OS X only, in some cases it is possible to get a wrong index returned by the text widget.

Example:

package require Tk
pack [text .t]
for {set i 1} {$i < 5} {incr i} {
  .t insert end "Line $i\n"
}
update
.t index "1.5 + 2 d lines"    ;# shall return 3.5, but returns 3.4
.t index "1.4 + 2 d lines"    ;# returns 3.4 and this is correct

This does not happen on Linux nor on Windows.

User Comments: fvogel added on 2017-01-07 15:16:03:
Merged into core-8-6-branch and trunk.

fvogel added on 2017-01-02 13:22:59:

Item B. of my previous post below is now fixed by [09309b7e].

No change in the tests suite results otherwise. This branch bug-28a4535fa2 has therefore 14 tests failures solved, and no regression detected.


fvogel added on 2017-01-02 00:24:19:

Results for the complet Tk test suite on OS X:

Note: the test suite hangs in menu.test, so I ran:

make -C ./macosx test TESTFLAGS="-notfile menu.test"

A. Do no longer fail after fixing the present bug through [fc7b584b]:

canvText-19.1
entry-3.73
event-3.1
event-7.2
spinbox-3.73
textDisp-14.13
textDisp-14.14
textDisp-19.11.17
textDisp-19.11.18
textDisp-19.11.20
textDisp-19.11.21
textDisp-19.11.23
textDisp-19.11.24
textIndex-19.8

B. Fail differently before and after the bug fix [fc7b584b]:

font-24.11
unixfont-5.12

C. New failures after the bug fix [fc7b584b]: nil

Conclusion:

- 14 test failures now fixed (looking at the detailed results all failures were indeed due to this off-by-1 bug)

- 2 test failures (B. above) I will look at more closely now

- Overall, looks quite good...


fvogel added on 2017-01-02 00:03:10:
Btw, the problem was that:

- The text widget index calculation first finds the correct target display line and then looks for the index under the required x position in pixels. The latter is DlineIndexOfX().

- DlineIndexOfX() performs by measuring the rendered width of a string.

- This width measurement wrongly trimmed trailing spaces from the string. Code that should only run with the flag TK_WHOLE_WORD was mistakenly run.

The bug could only be seen when the target index to return was just after one or more space characters in the display line.

fvogel added on 2017-01-01 20:58:51:

Fixed by [fc7b584b].

No new test failure withing textDisp.test, and it fixes the 8 failures mentioned below.

Must also check there is no regression at the whole Tk level.


fvogel added on 2017-01-01 20:51:09:

Just within textDisp.test, this problem is the cause for the following 8 failures of the test suite on OS X:

==== textDisp-14.13 TkTextXviewCmd procedure FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert end xxxxxxxxx\n
    .t insert end "a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9\n"
    .t insert end "xxxx xxxxxxxxx xxxxxxxxxxxxx"
    .t xview moveto 0
    .t xview scroll 2 pa
    set x [.t index @0,22]
    .t xview scroll -1 pa
    lappend x [.t index @0,22]
    .t xview scroll -2 pages
    lappend x [.t index @0,22]

---- Result was:
2.35 2.17 2.0
---- Result should have been (exact matching):
2.36 2.18 2.0
==== textDisp-14.13 FAILED



==== textDisp-14.14 TkTextXviewCmd procedure FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert end xxxxxxxxx\n
    .t insert end "a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9\n"
    .t insert end "xxxx xxxxxxxxx xxxxxxxxxxxxx"
    .t xview moveto 0
    .t xview scroll 21 u
    set x [.t index @0,22]
    .t xview scroll -1 u
    lappend x [.t index @0,22]
    .t xview scroll 100 units
    lappend x [.t index @0,22]
    .t xview scroll -15 units
    lappend x [.t index @0,22]

---- Result was:
2.20 2.20 2.98 2.83
---- Result should have been (exact matching):
2.21 2.20 2.99 2.84
==== textDisp-14.14 FAILED



==== textDisp-19.11.17 TextWidgetCmd procedure, "index +displaylines" FAILED
==== Contents of test case:

    .t tag remove elide 1.0 end
    .t tag add elide "12.0" "14.0"
    list [.t index "11.5 +2d lines"]  [.t index "12.0 +2d lines"] [.t index "11.0 +2d lines"]  [.t index "13.0 +2d lines"] [.t index "13.1 +3d lines"]  [.t index "13.0 +4d lines"]

---- Result was:
15.4 16.0 15.0 16.0 16.21 16.39
---- Result should have been (exact matching):
15.5 16.0 15.0 16.0 16.21 16.39
==== textDisp-19.11.17 FAILED



==== textDisp-19.11.18 TextWidgetCmd procedure, "index +displaylines" FAILED
==== Contents of test case:

    .t tag remove elide 1.0 end
    .t tag add elide "12.0" "14.0"
    list [.t index "15.5 -2d lines"]  [.t index "16.0 -2d lines"] [.t index "15.0 -2d lines"]  [.t index "16.0 -3d lines"] [.t index "16.23 -4d lines"]  [.t index "16.42 -5d lines"]

---- Result was:
11.4 14.0 11.0 11.0 11.2 11.3
---- Result should have been (exact matching):
11.5 14.0 11.0 11.0 11.2 11.3
==== textDisp-19.11.18 FAILED



==== textDisp-19.11.20 TextWidgetCmd procedure, "index +displaylines" FAILED
==== Contents of test case:

    .t tag remove elide 1.0 end
    .t tag add elide "12.0" "16.0 +1displaylines"
    list [.t index "11.5 +2d lines"]  [.t index "12.0 +2d lines"] [.t index "11.0 +2d lines"]  [.t index "13.0 +2d lines"] [.t index "13.0 +3d lines"]  [.t index "13.0 +4d lines"]

---- Result was:
16.43 16.57 16.39 16.57 16.74 17.0
---- Result should have been (exact matching):
16.44 16.57 16.39 16.57 16.74 17.0
==== textDisp-19.11.20 FAILED



==== textDisp-19.11.21 TextWidgetCmd procedure, "index +displaylines" FAILED
==== Contents of test case:

    .t tag remove elide 1.0 end
    .t tag add elide "12.0" "16.0 +1displaylines"
    list [.t index "16.44 -2d lines"]  [.t index "16.57 -3d lines"] [.t index "16.39 -2d lines"]  [.t index "16.60 -4d lines"] [.t index "16.76 -4d lines"]  [.t index "17.0 -5d lines"]

---- Result was:
11.4 11.0 11.0 10.3 11.2 11.0
---- Result should have been (exact matching):
11.5 11.0 11.0 10.3 11.2 11.0
==== textDisp-19.11.21 FAILED



==== textDisp-19.11.23 TextWidgetCmd procedure, "index +displaylines" FAILED
==== Contents of test case:

    .t tag remove elide 1.0 end
    .t tag add elide "12.3" "16.0 +1displaylines"
    list [.t index "11.5 +1d lines"] [.t index "11.5 +2d lines"]  [.t index "12.0 +1d lines"]  [.t index "12.0 +2d lines"] [.t index "11.0 +2d lines"]  [.t index "13.0 +2d lines"] [.t index "13.0 +3d lines"]  [.t index "13.0 +4d lines"]

---- Result was:
16.23 16.43 16.39 16.57 16.39 16.60 16.77 16.79
---- Result should have been (exact matching):
16.23 16.44 16.39 16.57 16.39 16.60 16.77 16.79
==== textDisp-19.11.23 FAILED



==== textDisp-19.11.24 TextWidgetCmd procedure, "index +/-displaylines" FAILED
==== Contents of test case:

    list [.t index "11.5 + -1 display lines"]  [.t index "11.5 + +1 disp lines"]  [.t index "11.5 - -1 disp lines"]  [.t index "11.5 - +1 disp lines"]  [.t index "11.5 -1 disp lines"]  [.t index "11.5 +1 disp lines"]  [.t index "11.5 +0 disp lines"]

---- Result was:
10.4 12.4 12.4 10.4 10.4 12.4 11.5
---- Result should have been (exact matching):
10.5 12.5 12.5 10.5 10.5 12.5 11.5
==== textDisp-19.11.24 FAILED