diff --git a/apps/web/src/terminal/ghostty/renderer.browser.test.ts b/apps/web/src/terminal/ghostty/renderer.browser.test.ts index eb48b04167748..3d3aec210d30e 100644 --- a/apps/web/src/terminal/ghostty/renderer.browser.test.ts +++ b/apps/web/src/terminal/ghostty/renderer.browser.test.ts @@ -29,6 +29,13 @@ function pixelsOffBackground(context: CanvasRenderingContext2D): number { return count; } +/** Whether the device pixel under a CSS pixel position is closer to black than white. */ +function isDarkAt(context: CanvasRenderingContext2D, x: number, y: number): boolean { + const { a, d } = context.getTransform(); + const { data } = context.getImageData(Math.floor(x * a), Math.floor(y * d), 1, 1); + return data[0]! < 128 && data[1]! < 128 && data[2]! < 128; +} + /** A 40 by 60 CSS pixel canvas drawn at the given device pixel ratio. */ function scaledContext(scale: number): CanvasRenderingContext2D { const canvas = document.createElement("canvas"); @@ -114,4 +121,51 @@ describe.each([1.25, 2.2])("renderGhosttySnapshot at a %sx device pixel ratio", renderGhosttySnapshot({ ...options, snapshot: firstRow(blank) }); expect(pixelsOffBackground(context)).toBe(0); }); + + // Cell 1 spans x 11.8 to 19.6 and row 0 spans y 4 to 21 in CSS pixels. + it.each([ + ["right", "\ue0b4", { flat: 12.3, round: 19.1, neighbour: 0, outside: 11.3 }], + ["left", "\ue0b6", { flat: 19.1, round: 12.3, neighbour: 2, outside: 20.1 }], + ])("fills the rounded %s cap to the cell edges, under a block cursor too", (_side, text, x) => { + const context = scaledContext(scale); + const segment = { ...blank, background: { r: 0, g: 0, b: 0 } }; + const cells = [blank, { ...blank, text }, blank]; + cells[x.neighbour] = segment; + const snapshot = (cursorVisible: boolean): GhosttySnapshot => ({ + cols: 3, + rows: 1, + foreground: { r: 0, g: 0, b: 0 }, + background, + cursor: { r: 0, g: 0, b: 0 }, + cursorX: 1, + cursorY: 0, + cursorVisible, + cursorBlinking: false, + cursorStyle: 1, + dirtyRows: new Set([0]), + rowData: [{ cells, text: "", isWrapContinuation: false, wrapsToNext: false }], + }); + + renderGhosttySnapshot({ + ...layout, + context, + snapshot: snapshot(false), + forceFull: true, + cursorOn: false, + }); + expect(isDarkAt(context, x.flat, 4.5)).toBe(true); + expect(isDarkAt(context, x.flat, 20.5)).toBe(true); + expect(isDarkAt(context, x.round, 12.5)).toBe(true); + expect(isDarkAt(context, x.round, 4.5)).toBe(false); + + renderGhosttySnapshot({ + ...layout, + context, + snapshot: snapshot(true), + forceFull: true, + cursorOn: true, + }); + expect(isDarkAt(context, x.outside, 12.5)).toBe(true); + expect(isDarkAt(context, x.flat, 12.5)).toBe(false); + }); }); diff --git a/apps/web/src/terminal/ghostty/renderer.ts b/apps/web/src/terminal/ghostty/renderer.ts index 98e3699ab314b..630745c4faf3e 100644 --- a/apps/web/src/terminal/ghostty/renderer.ts +++ b/apps/web/src/terminal/ghostty/renderer.ts @@ -20,6 +20,8 @@ export interface GhosttyCellRange { const DEFAULT_SELECTION_BACKGROUND = "rgba(72, 122, 191, 0.35)"; const POWERLINE_SOLID_RIGHT = "\ue0b0"; const POWERLINE_SOLID_LEFT = "\ue0b2"; +const POWERLINE_ROUND_RIGHT = "\ue0b4"; +const POWERLINE_ROUND_LEFT = "\ue0b6"; function cssColor(color: GhosttyColor): string { return `rgb(${color.r}, ${color.g}, ${color.b})`; @@ -36,12 +38,17 @@ function sameTextStyle(left: GhosttyCell, right: GhosttyCell): boolean { } function isSolidPowerlineSeparator(text: string): boolean { - return text === POWERLINE_SOLID_RIGHT || text === POWERLINE_SOLID_LEFT; + return ( + text === POWERLINE_SOLID_RIGHT || + text === POWERLINE_SOLID_LEFT || + text === POWERLINE_ROUND_RIGHT || + text === POWERLINE_ROUND_LEFT + ); } // Fallback fonts inset or scale these private-use glyphs, leaving a gap where -// the separator should meet the next cell. Fill the triangle from the cell's -// own edges instead, as native terminals do. +// the separator should meet the next cell. Fill the triangle or half-ellipse +// from the cell's own edges instead, as native terminals do. function fillSolidPowerlineSeparator( context: CanvasRenderingContext2D, text: string, @@ -49,12 +56,26 @@ function fillSolidPowerlineSeparator( top: number, width: number, height: number, + seamOverlap: number, ): void { const right = left + width; + const middle = top + height / 2; + if (text === POWERLINE_ROUND_RIGHT || text === POWERLINE_ROUND_LEFT) { + const [base, start, inward] = + text === POWERLINE_ROUND_RIGHT ? [left, -Math.PI / 2, -1] : [right, Math.PI / 2, 1]; + context.beginPath(); + context.ellipse(base, middle, width, height / 2, 0, start, start + Math.PI); + context.closePath(); + // Overlap the segment this cap closes so anti-aliasing at a fractional + // cell edge does not leave a hairline seam. + context.rect(Math.min(base, base + inward * seamOverlap), top, seamOverlap, height); + context.fill(); + return; + } const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left]; context.beginPath(); context.moveTo(base, top); - context.lineTo(point, top + height / 2); + context.lineTo(point, middle); context.lineTo(base, top + height); context.closePath(); context.fill(); @@ -234,6 +255,7 @@ export function renderGhosttySnapshot(options: { top, metrics.width, height, + 1, ); } runStart += 1; @@ -311,7 +333,7 @@ export function renderGhosttySnapshot(options: { if (cell?.text) { context.fillStyle = cssColor(snapshot.background); if (isSolidPowerlineSeparator(cell.text)) { - fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height); + fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height, 0); } else { context.font = fontForCell(cell, fontSize, fontFamily); context.fillText(cell.text, left, top + metrics.baseline, metrics.width);