diff --git a/apps/web/src/terminal/ghostty/renderer.browser.test.ts b/apps/web/src/terminal/ghostty/renderer.browser.test.ts index c75299e2784a4..ed1dbb717ea08 100644 --- a/apps/web/src/terminal/ghostty/renderer.browser.test.ts +++ b/apps/web/src/terminal/ghostty/renderer.browser.test.ts @@ -48,7 +48,7 @@ function scaledContext(scale: number): CanvasRenderingContext2D { } const layout = { - metrics: { width: 7.8, height: 17, baseline: 13 }, + metrics: { width: 7.8, height: 17, baseline: 13, blockAscent: 13, blockHeight: 17 }, fontSize: 13, fontFamily: "monospace", padding: 4, diff --git a/apps/web/src/terminal/ghostty/renderer.ts b/apps/web/src/terminal/ghostty/renderer.ts index b35070ed72977..4b0c5643597fe 100644 --- a/apps/web/src/terminal/ghostty/renderer.ts +++ b/apps/web/src/terminal/ghostty/renderer.ts @@ -10,6 +10,9 @@ export interface GhosttyCellMetrics { readonly width: number; readonly height: number; readonly baseline: number; + // The font's full block (U+2588) ink, which box-drawing and block glyphs are drawn against. + readonly blockAscent: number; + readonly blockHeight: number; } export interface GhosttyCellRange { @@ -22,6 +25,7 @@ const POWERLINE_SOLID_RIGHT = "\ue0b0"; const POWERLINE_SOLID_LEFT = "\ue0b2"; const POWERLINE_ROUND_RIGHT = "\ue0b4"; const POWERLINE_ROUND_LEFT = "\ue0b6"; +const BLOCK_SAMPLE_FONT_SIZE = 100; function cssColor(color: GhosttyColor): string { return `rgb(${color.r}, ${color.g}, ${color.b})`; @@ -81,6 +85,55 @@ function fillSolidPowerlineSeparator( context.fill(); } +// Box drawing (U+2500-257F) and block elements (U+2580-259F) must meet the +// glyphs in the rows above and below, as native terminals draw them. +function isCellFillingGlyph(text: string): boolean { + const code = text.codePointAt(0) ?? 0; + return text.length === 1 && code >= 0x2500 && code <= 0x259f; +} + +// Fonts draw these glyphs shorter than our rows, so stretch the full block's ink one +// device pixel past each row edge. Returns the baseline; the caller saves, clips and restores. +function fitGlyphRow( + context: CanvasRenderingContext2D, + text: string, + top: number, + height: number, + metrics: GhosttyCellMetrics, + seamOverlap: number, +): number { + if (!isCellFillingGlyph(text)) return top + metrics.baseline; + context.translate(0, top - seamOverlap); + context.scale(1, (height + seamOverlap * 2) / metrics.blockHeight); + return metrics.blockAscent; +} + +// measureText overstates this glyph's ink in some fonts (DejaVu Sans Mono by +// 1.6% of the em), so draw it once and read where the ink lands, in ems. +function measureFullBlockInk(fontFamily: string): { ascent: number; height: number } | null { + const canvas = document.createElement("canvas"); + canvas.width = 1; + canvas.height = BLOCK_SAMPLE_FONT_SIZE * 2; + const context = canvas.getContext("2d", { willReadFrequently: true }); + if (!context) return null; + context.font = `normal 400 ${BLOCK_SAMPLE_FONT_SIZE}px ${fontFamily}`; + const baseline = BLOCK_SAMPLE_FONT_SIZE * 1.5; + context.fillText("█", -context.measureText("█").width / 2, baseline); + const pixels = context.getImageData(0, 0, 1, canvas.height).data; + let first = -1; + let last = -1; + for (let y = 0; y < canvas.height; y += 1) { + if ((pixels[y * 4 + 3] ?? 0) < 128) continue; + if (first < 0) first = y; + last = y; + } + if (first < 0) return null; + return { + ascent: (baseline - first) / BLOCK_SAMPLE_FONT_SIZE, + height: (last + 1 - first) / BLOCK_SAMPLE_FONT_SIZE, + }; +} + function ghosttyTextRunEnd( cells: readonly GhosttyCell[], start: number, @@ -184,10 +237,13 @@ export function measureGhosttyCell( const descent = verticalMeasurement.actualBoundingBoxDescent; const glyphHeight = ascent + descent; const height = Math.max(1, Math.round(fontSize * 1.35), Math.ceil(glyphHeight)); + const block = measureFullBlockInk(fontFamily); return { width: Math.max(1, widthMeasurement.width), height, baseline: Math.round((height - glyphHeight) / 2 + ascent), + blockAscent: block ? block.ascent * fontSize : ascent, + blockHeight: block ? block.height * fontSize : glyphHeight, }; } @@ -239,6 +295,7 @@ export function renderGhosttySnapshot(options: { const rowTop = (row: number) => Math.round((originY + row * metrics.height) * transform.d) / transform.d; const rowWidth = context.canvas.width / transform.a; + const devicePixel = 1 / transform.d; const rowsToDraw = forceFull ? Array.from({ length: snapshot.rows }, (_, index) => index) : [...snapshot.dirtyRows]; @@ -327,7 +384,12 @@ export function renderGhosttySnapshot(options: { runStart += 1; continue; } - const runEnd = ghosttyTextRunEnd(row.cells, runStart, (cell) => sameTextStyle(cell, first)); + const fillsCell = isCellFillingGlyph(first.text); + const runEnd = ghosttyTextRunEnd( + row.cells, + runStart, + (cell) => sameTextStyle(cell, first) && isCellFillingGlyph(cell.text) === fillsCell, + ); const text = row.cells .slice(runStart, runEnd) .map((cell) => cell.text) @@ -351,7 +413,7 @@ export function renderGhosttySnapshot(options: { runEnd, text, padding + runStart * metrics.width, - top + metrics.baseline, + fitGlyphRow(context, first.text, top, height, metrics, devicePixel), metrics.width, ); context.restore(); @@ -406,15 +468,19 @@ export function renderGhosttySnapshot(options: { if (isSolidPowerlineSeparator(cell.text)) { fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height, 0); } else { + context.save(); + context.beginPath(); + context.rect(left, top, width, height); + context.clip(); context.font = fontForCell(cell, fontSize, fontFamily); context.textAlign = wide ? "center" : "start"; context.fillText( cell.text, wide ? left + metrics.width : left, - top + metrics.baseline, + fitGlyphRow(context, cell.text, top, height, metrics, devicePixel), width, ); - context.textAlign = "start"; + context.restore(); } } if (cell && !cell.invisible) { diff --git a/apps/web/src/terminal/ghostty/surface.test.ts b/apps/web/src/terminal/ghostty/surface.test.ts index 7c8a61dda18e6..928e123507e94 100644 --- a/apps/web/src/terminal/ghostty/surface.test.ts +++ b/apps/web/src/terminal/ghostty/surface.test.ts @@ -139,6 +139,9 @@ describe("GhosttyTerminalSurface visibility", () => { fillRect: (...args: number[]) => paint("fillRect", args), strokeRect: (...args: number[]) => paint("strokeRect", args), fillText: (...args: [string, number, number, number?]) => paint("fillText", args), + getImageData: (_x: number, _y: number, width: number, height: number) => ({ + data: new Uint8ClampedArray(width * height * 4), + }), measureText: (text: string) => ({ width: text.length * 8, actualBoundingBoxAscent: 9, diff --git a/apps/web/src/terminal/ghostty/surface.ts b/apps/web/src/terminal/ghostty/surface.ts index 2e450cff91fb7..b6905f044a0ed 100644 --- a/apps/web/src/terminal/ghostty/surface.ts +++ b/apps/web/src/terminal/ghostty/surface.ts @@ -868,7 +868,9 @@ export class GhosttyTerminalSurface { if ( metrics.width === this.metrics.width && metrics.height === this.metrics.height && - metrics.baseline === this.metrics.baseline + metrics.baseline === this.metrics.baseline && + metrics.blockAscent === this.metrics.blockAscent && + metrics.blockHeight === this.metrics.blockHeight ) { return; }