Tk Source Code

View Ticket
Login
Ticket UUID: f5b47c533fb781719ae9d2399abeea4bd359fc79
Title: text widget: RTL line layout scrambled when a tag (e.g. the selection) splits it — "dancing selection"
Type: Patch Version: 8 & 9
Submitter: Arnault01 Created on: 2026-10-30 01:39:58
Subsystem: 18. [text] Assigned To: kevin_walzer
Priority: 5 Medium Severity: Severe
Status: Closed Last Modified: 2026-10-31 18:05:09
Resolution: Fixed Closed By: kevin_walzer
    Closed on: 2026-10-31 18:05:09
Description:
When a tag — including the implicit `sel` tag — splits a display line containing
RTL text into several chunks, `LayoutDLine` places the chunks left-to-right in
logical order while the font backend shapes each individual chunk right-to-left.
The result is a scrambled line: reading order jumps around, and while dragging
the mouse the moving selection boundary keeps re-splitting the line, so the
layout changes under the pointer at every index step. Visually, the selection
"dances".

Reproducer (X11, Tk trunk built with `--enable-bidi`):

```tcl
package require Tk
text .t -font {DejaVuSans 16} -wrap none
.t insert 1.0 "שלום עליכם, זהו טקסט מימין לשמאל"
pack .t
update
# record the x of each character bbox, then apply the sel tag:
.t tag add sel 1.4 1.8
update
# record the x of each character bbox again: every index has moved
```

Expected: the visual position of every character is unchanged by the tag.
Observed (trunk): all 32 character positions move when the tag is applied; the
first four characters even end up on the wrong side of the line. Sample data
(x per char, before → after): `949 938 933 920 …` → `684 673 668 655 725 713 …`

**Root cause:**

The bidi resolution happens inside the font backend for a single contiguous
run, but nothing reorders the *chunks* of the display line when a tag splits
that run. This is the chunk-granularity counterpart of what the revised text
widget already solved ([e7c85cd941]).

**Proposed fix** (attached: `tkTextDisp-bidi-reorder.patch` against trunk
`generic/tkTextDisp.c`):

1. `BidiReorderChunks()`, called at the end of `LayoutDLine()`: resolves the
   strong bidi direction of each char chunk (`ChunkBidiDir()`, a tri-state
   variant of `ChunkIsRtl` using the same Unicode ranges — Hebrew, Arabic,
   Syriac, NKo, Thaana, Samaritan, presentation forms, historical RTL scripts),
   resolves neutrals (simplified N1/N2), then applies rule L2 by reversing each
   maximal run of RTL chunks, reassigning their x positions in visual order.
   The run keeps its left edge and its total width, so the line length and all
   downstream geometry are unaffected. Non-character chunks never move.
2. `CharBboxProc()`: with `TK_LAYOUT_WITH_BASE_CHUNKS`, `CharChunkMeasureChars`
   always anchors the advance at the *base* chunk, so the mirrored bbox of an
   RTL chunk must be computed from the base chunk, not from the chunk itself:
   mirror around the right edge of the run (the right edge of the base chunk,
   which the reorder puts first-logical → rightmost). Without this, every char
   of the non-base chunks collapses to a single x. The mirrored x is *not*
   clamped to the chunk box: in base-chunk space it is expressed in the line's
   coordinate system and legitimately falls outside the chunk that merely
   carries the character's style (e.g. a `sel` chunk placed at the right edge
   of an RTL line while its characters render further left) — clamping it
   there re-created the dancing selection under Windows.
3. `TkTextDLineInfo()`: after the reorder the first chunk of the list is no
   longer the leftmost one, so the left edge of the line is the minimum chunk x.
4. Windows only (`#ifdef _WIN32`, base-chunk block of
   `CharChunkMeasureChars`): clamp the value returned by `MeasureChars` to be
   no less than the span start. The Uniscribe backend returns non-monotonic
   positions (notably when the measured range ends with `\n`, see Ticket 3);
   without the clamp the newline chunk gets a negative width that corrupts the
   DLine length and, with `-justify right`, shifts RTL lines out of the
   widget.

Deliberate simplifications (documented in the code): the paragraph base level
is always LTR; European numbers are treated as neutrals; reordering is at chunk
granularity, not character granularity.

**Evidence:**

- Branch: https://github.com/Surcouf01/tk/tree/bugfix/dancing-selection-bidi
- CI (headless Xvfb, repro above scripted): `PASS: RTL layout is stable under
  the sel tag` — all 32 character x positions identical before/after the tag,
  `dlineinfo` unchanged (655 3 308 24 19). Logs:
  https://github.com/Surcouf01/tk/tree/ci-results/bugfix/dancing-selection-bidi
  (`.ci-logs/test1.log`); baseline on trunk-aligned `main` FAILs the same test.
- Windows (manual build, MSVC `nmake -f makefile.vc`, Arial 14, widget
  884x532, `-wrap none`, same scripted test with `-justify right`): all 32
  character x positions identical before/after the tag,
  `indices whose x moved:` empty — `PASS`. Diagnostic branch with
  instrumentation: https://github.com/Surcouf01/tk/tree/win-debug-bidi
  (probes are compiled out in the attached patch; nothing is gated on
  environment variables in the submitted code).
- Manual drag-testing of an interactive selection on a pointed-Hebrew line is
  still recommended as final confirmation (CI is headless).
User Comments: kevin_walzer added on 2026-10-31 18:05:09:
Fixed in [59d6d5f6c0]. Closing this ticket.

Attachments: