From f10558a0bfb23c0d4b19771f863faf9ebf150f18 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:44:31 +0100 Subject: [PATCH 1/2] fix(web): terminal box-drawing and block glyphs meet across rows Fonts draw box-drawing (U+2500-257F) and block elements (U+2580-259F) to their own line height, which is shorter than terminal rows, so vertical borders and stacked scrollbar blocks (lazygit) showed a gap at every row. Draw those glyphs in their own runs, stretched so the font's full block ink fills the row, measured by drawing it once since measureText bounds overstate it. Text, spacing and Powerline separators are unchanged. Closes #36394 Co-authored-by: Claude Opus 5.5 Co-authored-by: GPT 6.1 Sol --- .../terminal/ghostty/renderer.browser.test.ts | 2 +- apps/web/src/terminal/ghostty/renderer.ts | 76 ++++++++++++++++++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/apps/web/src/terminal/ghostty/renderer.browser.test.ts b/apps/web/src/terminal/ghostty/renderer.browser.test.ts index 3d3aec210d30e..6a637b4f8f1f5 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 4eef1be37174f..cc3b87a06a056 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,57 @@ 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 to their own line height, shorter than our rows, so +// stretch the full block's ink past the row by `seamOverlap` on each side (the +// caller clips to the row) and the rest of the glyph follows. Returns the +// baseline to draw at; the caller saves and restores the context. +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, @@ -167,10 +222,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, }; } @@ -222,6 +280,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]; @@ -310,7 +369,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) @@ -334,7 +398,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(); @@ -388,15 +452,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(); } } } From 75aaf3fed8b032d92f8da09fe8f46bced9c699e1 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:54:28 +0100 Subject: [PATCH 2/2] refactor(web): trim terminal block-glyph fit and refresh on block metric changes Shorten the fitGlyphRow comment, re-apply font metrics when only the full-block ink changes, and give the surface test canvas getImageData so measureGhosttyCell's block sampling runs there. Co-authored-by: GPT 6.1 Sol Co-authored-by: Claude Opus 5.5 --- apps/web/src/terminal/ghostty/renderer.ts | 6 ++---- apps/web/src/terminal/ghostty/surface.test.ts | 3 +++ apps/web/src/terminal/ghostty/surface.ts | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/web/src/terminal/ghostty/renderer.ts b/apps/web/src/terminal/ghostty/renderer.ts index cc3b87a06a056..906eb965d33a2 100644 --- a/apps/web/src/terminal/ghostty/renderer.ts +++ b/apps/web/src/terminal/ghostty/renderer.ts @@ -92,10 +92,8 @@ function isCellFillingGlyph(text: string): boolean { return text.length === 1 && code >= 0x2500 && code <= 0x259f; } -// Fonts draw these glyphs to their own line height, shorter than our rows, so -// stretch the full block's ink past the row by `seamOverlap` on each side (the -// caller clips to the row) and the rest of the glyph follows. Returns the -// baseline to draw at; the caller saves and restores the context. +// 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, diff --git a/apps/web/src/terminal/ghostty/surface.test.ts b/apps/web/src/terminal/ghostty/surface.test.ts index 32721b740d099..64574dd5f5931 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 537f7bb524e59..a28109d719b37 100644 --- a/apps/web/src/terminal/ghostty/surface.ts +++ b/apps/web/src/terminal/ghostty/surface.ts @@ -856,7 +856,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; }