Skip to content

Commit 323c0a8

Browse files
fix(web): keep wide terminal glyphs on the cell grid (#27948)
1 parent d6e5f2e commit 323c0a8

1 file changed

Lines changed: 71 additions & 11 deletions

File tree

‎apps/web/src/terminal/ghostty/renderer.ts‎

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,55 @@ function ghosttyTextRunEnd(
100100
return end;
101101
}
102102

103+
// Snapshots decode `wide` only for empty cells, so a wide glyph is known by its spacer tail.
104+
function isWideGlyph(cells: readonly GhosttyCell[], index: number): boolean {
105+
return cells[index + 1]?.wide === GHOSTTY_CELL_WIDE.spacerTail;
106+
}
107+
108+
// Wide glyphs rarely advance exactly two cells, so each is centred in its own box
109+
// and the narrow text after it restarts at its own column instead of drifting.
110+
function fillGhosttyRunText(
111+
context: CanvasRenderingContext2D,
112+
cells: readonly GhosttyCell[],
113+
start: number,
114+
end: number,
115+
text: string,
116+
left: number,
117+
baseline: number,
118+
cellWidth: number,
119+
): void {
120+
let segment = start;
121+
while (segment < end) {
122+
let segmentEnd = segment;
123+
while (segmentEnd < end && !isWideGlyph(cells, segmentEnd)) segmentEnd += 1;
124+
if (segmentEnd > segment) {
125+
const segmentText =
126+
segment === start && segmentEnd === end
127+
? text
128+
: cells
129+
.slice(segment, segmentEnd)
130+
.map((cell) => cell.text)
131+
.join("");
132+
context.fillText(
133+
segmentText,
134+
left + (segment - start) * cellWidth,
135+
baseline,
136+
(segmentEnd - segment) * cellWidth,
137+
);
138+
}
139+
if (segmentEnd === end) break;
140+
context.textAlign = "center";
141+
context.fillText(
142+
cells[segmentEnd]?.text ?? "",
143+
left + (segmentEnd - start + 1) * cellWidth,
144+
baseline,
145+
cellWidth * 2,
146+
);
147+
context.textAlign = "start";
148+
segment = segmentEnd + 2;
149+
}
150+
}
151+
103152
function fontForCell(cell: GhosttyCell, fontSize: number, fontFamily: string): string {
104153
const style = cell.italic ? "italic" : "normal";
105154
const weight = cell.bold ? "700" : "400";
@@ -278,11 +327,15 @@ export function renderGhosttySnapshot(options: {
278327
context.clip();
279328
context.font = fontForCell(first, fontSize, fontFamily);
280329
context.fillStyle = cssColor(first.foreground);
281-
context.fillText(
330+
fillGhosttyRunText(
331+
context,
332+
row.cells,
333+
runStart,
334+
runEnd,
282335
text,
283336
padding + runStart * metrics.width,
284337
top + metrics.baseline,
285-
(runEnd - runStart) * metrics.width,
338+
metrics.width,
286339
);
287340
context.restore();
288341
}
@@ -316,27 +369,34 @@ export function renderGhosttySnapshot(options: {
316369
const left = padding + snapshot.cursorX * metrics.width;
317370
const top = rowTop(snapshot.cursorY);
318371
const height = rowTop(snapshot.cursorY + 1) - top;
372+
const cells = snapshot.rowData[snapshot.cursorY]?.cells ?? [];
373+
const wide = isWideGlyph(cells, snapshot.cursorX);
374+
const width = wide ? metrics.width * 2 : metrics.width;
319375
context.fillStyle = cssColor(snapshot.cursor);
320-
if (!focused) {
376+
if (!focused || snapshot.cursorStyle === 3) {
321377
context.strokeStyle = cssColor(snapshot.cursor);
322-
context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, height - 1);
378+
context.strokeRect(left + 0.5, top + 0.5, width - 1, height - 1);
323379
} else if (snapshot.cursorStyle === 0) {
324380
context.fillRect(left, top, 2, height);
325381
} else if (snapshot.cursorStyle === 2) {
326-
context.fillRect(left, top + height - 2, metrics.width, 2);
327-
} else if (snapshot.cursorStyle === 3) {
328-
context.strokeStyle = cssColor(snapshot.cursor);
329-
context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, height - 1);
382+
context.fillRect(left, top + height - 2, width, 2);
330383
} else {
331-
context.fillRect(left, top, metrics.width, height);
332-
const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
384+
const cell = cells[snapshot.cursorX];
385+
context.fillRect(left, top, width, height);
333386
if (cell?.text) {
334387
context.fillStyle = cssColor(snapshot.background);
335388
if (isSolidPowerlineSeparator(cell.text)) {
336389
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height, 0);
337390
} else {
338391
context.font = fontForCell(cell, fontSize, fontFamily);
339-
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
392+
context.textAlign = wide ? "center" : "start";
393+
context.fillText(
394+
cell.text,
395+
wide ? left + metrics.width : left,
396+
top + metrics.baseline,
397+
width,
398+
);
399+
context.textAlign = "start";
340400
}
341401
}
342402
}

0 commit comments

Comments
 (0)