| Ticket UUID: | f3d964112fb16da161bd6abd026fefa50c9627a5 | |||
| Title: | text widget, Windows: newline chunk gets a negative width (Uniscribe non-monotonic measurement) — right/center-justified lines are misplaced and overflow the widget | |||
| Type: | Patch | Version: | 8 & 9 | |
| Submitter: | Arnault01 | Created on: | 2026-10-30 01:42:42 | |
| Subsystem: | 18. [text] | Assigned To: | jan.nijtmans | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Open | Last Modified: | 2026-11-10 13:16:20 | |
| Resolution: | None | Closed By: | nobody | |
| Closed on: | ||||
| Description: |
On Windows, with `TK_LAYOUT_WITH_BASE_CHUNKS` (defined in `win/tkWinInt.h`),
`CharChunkMeasureChars` measures a span by calling `MeasureChars` over the
prefix `[0, bend)` of the base-chunk string. For some ranges the Uniscribe
backend returns a *right edge to the left of the span start* — i.e.
`*nextXPtr < startX` — which is geometrically impossible. Two observed
manifestations (Tcl 9 + Tk trunk, Arial 14, a one-line widget):
1. When the measured range includes the trailing `\n` of the line
(`bstart=58 bend=59 startX=262`), `MeasureChars` returns `nextX=167`: the
newline chunk is created with width **-95**. `LayoutDLine` then computes
`dlPtr->length = 167` instead of 262.
2. With `-justify right`, `jIndent = maxX - dlPtr->length` overshoots by the
negative width: the display line is placed at x=713 instead of 618, its
right edge (975) is beyond the widget (884), and the `bbox` of the last
characters returns -1. This is visible *before* any tag or selection is
involved — plain trunk behaviour on Windows.
Prefix measurements of bidi text are also non-monotonic in intermediate
ranges (`bend=19 → 975` vs `bend=20 → 885`), which corrupts per-character
bbox positions for the affected characters; the clamp does not mask that
larger issue but prevents the geometrically-impossible negative widths.
**Proposed fix** (attached: `tk-windows-uniscribe-measure.patch`, one hunk in
`generic/tkTextDisp.c`): after the `MeasureChars` call in the `#ifdef _WIN32`
base-chunk block of `CharChunkMeasureChars`, clamp:
```c
if (*nextXPtr < startX) {
*nextXPtr = startX;
}
```
A span cannot end to the left of where it begins; control characters such as
`\n` then get zero width (matching the X11 backend, where the same call
returns a monotonic cumulative width and the newline chunk is zero-width),
the DLine length is correct and justification places the line inside the
widget.
**Evidence:**
- Instrumented diagnostic run on Windows (branch
https://github.com/Surcouf01/tk/tree/win-debug-bidi, probes gated on the
`TK_DEBUG_BIDI` environment variable): newline chunk `w=-95`,
`dlPtr->length=167`, `jIndent=713`, line overflowing the widget; with the
clamp: `CCM-CLAMP: nextX 167 -> 262`, newline chunk `w=0`, `length=262`,
`jIndent=618`, line placed on [618, 880], inside the widget.
- The same clamp is part of `bugfix/dancing-selection-bidi`
(https://github.com/Surcouf01/tk/tree/bugfix/dancing-selection-bidi),
where the attached Windows reproducible passes with it and fails without it.
- X11 is unaffected by construction: the X11/macOS block of
`CharChunkMeasureChars` already receives monotonic cumulative widths from
its backend, and the CI on `bugfix/dancing-selection-bidi` passes unchanged
with and without this hunk.
| |||
| User Comments: |
jan.nijtmans added on 2026-11-10 13:16:20:
Committed to a [b4211c8cfe624f71|branch] Many thanks! Let's wait for the CI round. | |||
Attachments:
- tk-windows-uniscribe-measure.patch [download] added by Arnault01 on 2026-10-30 01:42:55. [details]
