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
Open
Conversation
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).
Owner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
buildRowData) snap to a rounded, pixel-aligned grid:x = round(lineOriginPx.x) + column * round(cellWidthPx)buildCursorDrawData) used the unrounded fractional grid:x = lineOriginPx.x + buffer.x * cellWidthPxSo the caret diverges from the glyphs by
round(lineOriginPx.x) - lineOriginPx.xplusbuffer.x * (round(cellWidthPx) - cellWidthPx)— a per-column error that accumulates withbuffer.x.Fix
Position the caret on the same rounded/aligned grid the glyphs use (
baseCellWidthPx+alignedOriginX), preserving the existingdoublePositionandcursorColumnWidth(full-width/CJK) behavior:Note (related, optional)
The same fractional-vs-rounded inconsistency exists for background/decoration cells in
buildRowData(they usecolumn * 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 buildpasses.