Skip to content

Commit da7f623

Browse files
fix(web): keep the block cursor's rounded cap inside its cell
The seam overlap that joins a rounded cap to its segment painted the background colour into the neighbouring cell when the cap sat under a block cursor. Skip the overlap there, and replace the mocked-context unit test with a pixel test beside the existing renderer browser tests. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 99d9307 commit da7f623

3 files changed

Lines changed: 60 additions & 120 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ function pixelsOffBackground(context: CanvasRenderingContext2D): number {
2929
return count;
3030
}
3131

32+
/** Whether the device pixel under a CSS pixel position is closer to black than white. */
33+
function isDarkAt(context: CanvasRenderingContext2D, x: number, y: number): boolean {
34+
const { a, d } = context.getTransform();
35+
const { data } = context.getImageData(Math.floor(x * a), Math.floor(y * d), 1, 1);
36+
return data[0]! < 128 && data[1]! < 128 && data[2]! < 128;
37+
}
38+
3239
/** A 40 by 60 CSS pixel canvas drawn at the given device pixel ratio. */
3340
function scaledContext(scale: number): CanvasRenderingContext2D {
3441
const canvas = document.createElement("canvas");
@@ -114,4 +121,51 @@ describe.each([1.25, 2.2])("renderGhosttySnapshot at a %sx device pixel ratio",
114121
renderGhosttySnapshot({ ...options, snapshot: firstRow(blank) });
115122
expect(pixelsOffBackground(context)).toBe(0);
116123
});
124+
125+
// Cell 1 spans x 11.8 to 19.6 and row 0 spans y 4 to 21 in CSS pixels.
126+
it.each([
127+
["right", "\ue0b4", { flat: 12.3, round: 19.1, neighbour: 0, outside: 11.3 }],
128+
["left", "\ue0b6", { flat: 19.1, round: 12.3, neighbour: 2, outside: 20.1 }],
129+
])("fills the rounded %s cap to the cell edges, under a block cursor too", (_side, text, x) => {
130+
const context = scaledContext(scale);
131+
const segment = { ...blank, background: { r: 0, g: 0, b: 0 } };
132+
const cells = [blank, { ...blank, text }, blank];
133+
cells[x.neighbour] = segment;
134+
const snapshot = (cursorVisible: boolean): GhosttySnapshot => ({
135+
cols: 3,
136+
rows: 1,
137+
foreground: { r: 0, g: 0, b: 0 },
138+
background,
139+
cursor: { r: 0, g: 0, b: 0 },
140+
cursorX: 1,
141+
cursorY: 0,
142+
cursorVisible,
143+
cursorBlinking: false,
144+
cursorStyle: 1,
145+
dirtyRows: new Set([0]),
146+
rowData: [{ cells, text: "", isWrapContinuation: false, wrapsToNext: false }],
147+
});
148+
149+
renderGhosttySnapshot({
150+
...layout,
151+
context,
152+
snapshot: snapshot(false),
153+
forceFull: true,
154+
cursorOn: false,
155+
});
156+
expect(isDarkAt(context, x.flat, 4.5)).toBe(true);
157+
expect(isDarkAt(context, x.flat, 20.5)).toBe(true);
158+
expect(isDarkAt(context, x.round, 12.5)).toBe(true);
159+
expect(isDarkAt(context, x.round, 4.5)).toBe(false);
160+
161+
renderGhosttySnapshot({
162+
...layout,
163+
context,
164+
snapshot: snapshot(true),
165+
forceFull: true,
166+
cursorOn: true,
167+
});
168+
expect(isDarkAt(context, x.outside, 12.5)).toBe(true);
169+
expect(isDarkAt(context, x.flat, 12.5)).toBe(false);
170+
});
117171
});

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

Lines changed: 0 additions & 116 deletions
This file was deleted.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function fillSolidPowerlineSeparator(
5656
top: number,
5757
width: number,
5858
height: number,
59+
seamOverlap: number,
5960
): void {
6061
const right = left + width;
6162
const middle = top + height / 2;
@@ -65,9 +66,9 @@ function fillSolidPowerlineSeparator(
6566
context.beginPath();
6667
context.ellipse(base, middle, width, height / 2, 0, start, start + Math.PI);
6768
context.closePath();
68-
// Overlap the segment this cap closes by a pixel so anti-aliasing at a
69-
// fractional cell edge does not leave a hairline seam.
70-
context.rect(Math.min(base, base + inward), top, 1, height);
69+
// Overlap the segment this cap closes so anti-aliasing at a fractional
70+
// cell edge does not leave a hairline seam.
71+
context.rect(Math.min(base, base + inward * seamOverlap), top, seamOverlap, height);
7172
context.fill();
7273
return;
7374
}
@@ -254,6 +255,7 @@ export function renderGhosttySnapshot(options: {
254255
top,
255256
metrics.width,
256257
height,
258+
1,
257259
);
258260
}
259261
runStart += 1;
@@ -331,7 +333,7 @@ export function renderGhosttySnapshot(options: {
331333
if (cell?.text) {
332334
context.fillStyle = cssColor(snapshot.background);
333335
if (isSolidPowerlineSeparator(cell.text)) {
334-
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height);
336+
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height, 0);
335337
} else {
336338
context.font = fontForCell(cell, fontSize, fontFamily);
337339
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);

0 commit comments

Comments
 (0)