Skip to content

Metal: align the caret to the rounded glyph cell grid (fixes cursor drift that grows with column) - #614

Open
kriera wants to merge 1 commit into
migueldeicaza:mainfrom
kriera:fix/metal-caret-cell-grid
Open

Metal: align the caret to the rounded glyph cell grid (fixes cursor drift that grows with column)#614
kriera wants to merge 1 commit into
migueldeicaza:mainfrom
kriera:fix/metal-caret-cell-grid

Conversation

@kriera

@kriera kriera commented Jul 29, 2026

Copy link
Copy Markdown

Problem

On the Metal renderer, the text cursor drifts to the right of the glyphs, and the offset grows with the column — invisible near the left edge, a visible multi-cell gap at a long prompt (e.g. a ~30-column shell prompt leaves ~1–2 empty cells between the prompt and the cursor / typed input). The CoreGraphics renderer is unaffected.

Root cause

Glyphs and the caret are placed on different horizontal grids:

  • Glyphs (buildRowData) snap to a rounded, pixel-aligned grid:
    x = round(lineOriginPx.x) + column * round(cellWidthPx)
  • The caret (buildCursorDrawData) used the unrounded fractional grid:
    x = lineOriginPx.x + buffer.x * cellWidthPx

So the caret diverges from the glyphs by round(lineOriginPx.x) - lineOriginPx.x plus buffer.x * (round(cellWidthPx) - cellWidthPx) — a per-column error that accumulates with buffer.x.

Fix

Position the caret on the same rounded/aligned grid the glyphs use (baseCellWidthPx + alignedOriginX), preserving the existing doublePosition and cursorColumnWidth (full-width/CJK) behavior:

let baseCellWidthPx = CGFloat(max(1, Int(round(cellWidthPx))))
let alignedOriginX  = round(lineOriginPx.x)
...
let x0 = alignedOriginX + CGFloat(buffer.x) * baseCellWidthPx * doublePosition
let x1 = x0 + baseCellWidthPx * doublePosition * cursorColumnWidth

Note (related, optional)

The same fractional-vs-rounded inconsistency exists for background/decoration cells in buildRowData (they use column * cellWidthPx), which can make the cell highlight / underline drift from the glyph on long lines too. I kept this PR surgical (caret only) since that's the most visible symptom — happy to fold those into the same rounded grid here or in a follow-up.

Repro

Retina Mac, Metal renderer on, any shell with a ~30-column prompt (e.g. conda's (base) …). Type a character: it appears ~1–2 cells right of the prompt's end; the gap scales with prompt length. Off on CoreGraphics. After the patch: the caret sits tight against the prompt at all columns.

swift build passes.

On the Metal renderer the text cursor drifts right of the glyphs, and the
offset grows with the column — invisible near the left edge, a visible
multi-cell gap at a long prompt (a ~30-column shell prompt leaves ~1–2 empty
cells between the prompt and the cursor). CoreGraphics is unaffected.

Cause: glyphs (buildRowData) snap to a rounded, pixel-aligned grid
(round(origin) + col * round(cellWidthPx)), but the caret (buildCursorDrawData)
used the unrounded fractional grid (origin + buffer.x * cellWidthPx), so it
diverged by buffer.x * (round(cellWidthPx) - cellWidthPx) — a per-column error
that accumulates with buffer.x.

Fix: position the caret on the same baseCellWidthPx / alignedOriginX grid the
glyphs use, preserving the existing doublePosition and cursorColumnWidth
behavior. The same fractional-vs-rounded inconsistency also affects
background/decoration cells in buildRowData (left as a follow-up).
@migueldeicaza

Copy link
Copy Markdown
Owner

This looks like the same case in #615 which was fixed in 13732b7 can you check that please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants