Skip to content

fix(renderer): align font metrics to device pixel boundaries to prevent seams#146

Open
tommyme wants to merge 3 commits into
coder:mainfrom
tommyme:fix/renderer-dpr-pixel-seams
Open

fix(renderer): align font metrics to device pixel boundaries to prevent seams#146
tommyme wants to merge 3 commits into
coder:mainfrom
tommyme:fix/renderer-dpr-pixel-seams

Conversation

@tommyme
Copy link
Copy Markdown

@tommyme tommyme commented Mar 27, 2026

Problem

When devicePixelRatio is non-integer (e.g. 1.25, 1.5, 1.75 from browser zoom or high-DPI displays), rounding cell width/height to the nearest CSS pixel with Math.ceil() produces fractional physical pixel coordinates at cell edges.

The canvas rasterizer antialiases clearRect/fillRect calls at those sub-pixel boundaries. With alpha: true on the canvas, the resulting partially-transparent edge pixels composite against the page background and appear as thin black seams between rows and columns.

Fix

Round up to the nearest device pixel instead of CSS pixel:

const dpr = this.devicePixelRatio;
const width    = Math.ceil(widthMetrics.width       * dpr) / dpr;
const height   = Math.ceil((ascent + descent + 2)  * dpr) / dpr;
const baseline = Math.ceil((ascent + 1)            * dpr) / dpr;

This guarantees cell boundaries always fall on exact physical pixel boundaries regardless of zoom level or monitor DPR.

🤖 Generated with Claude Code

tommyme and others added 3 commits March 27, 2026 17:20
…nt seams

When devicePixelRatio is non-integer (e.g. 1.25/1.5/1.75 from browser zoom),
rounding cell width/height to the nearest CSS pixel produces fractional physical
pixel coordinates at cell edges. The canvas rasterizer antialiases
clearRect/fillRect at those sub-pixel boundaries, and with alpha:true the
resulting partially-transparent edge pixels composite against the page
background and appear as thin black seams between rows and columns.

Fix by rounding up to the nearest *device* pixel instead:
  Math.ceil(value * dpr) / dpr

This guarantees cell boundaries always fall on exact physical pixel boundaries
regardless of zoom level or monitor DPR.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The render loop ran requestAnimationFrame indefinitely (60fps) even when
idle, causing ~100% single-core CPU usage.

Replace startRenderLoop() with scheduleRender() that requests a single
frame and stops. All state-changing paths (write, scroll, selection,
resize, cursor blink) now call scheduleRender() to trigger rendering
on demand. Idle terminal uses 0 CPU.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- writeInternal: defer link invalidation and title check to render frame,
  so multiple writes within one rAF share a single flush
- rgbToCSS: cache color strings in a Map (terminals use ~20-30 distinct colors)
- ctx.font: only reassign when bold/italic flags change (most expensive Canvas op)
- link range comparison: direct field compare instead of JSON.stringify

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
diegosouzapw added a commit to diegosouzapw/ghostty-web that referenced this pull request May 23, 2026
…nt seams (#6)

When devicePixelRatio is non-integer (e.g. 1.25, 1.5, 1.75 from browser
zoom or HiDPI displays), rounding cell width/height to the nearest CSS
pixel with Math.ceil() produces fractional *physical* pixel coordinates
at cell edges.

The canvas rasterizer antialiases clearRect/fillRect calls at those
sub-pixel boundaries. With alpha:true on the canvas (enabled in coder#93 for
transparent backgrounds), the resulting partially-transparent edge
pixels composite against the page background and appear as thin black
seams between rows and columns.

Fix: round up to the nearest *device* pixel instead of CSS pixel. The
+2/+1 paddings for glyph overflow stay in CSS units before the DPR
multiplication so they scale correctly.

Ports only the font-metrics subset of upstream PR coder#146 — the rest of
that PR bundles a substantial render-loop refactor (startRenderLoop →
scheduleRender) and several perf caches whose risk/benefit needs
separate evaluation against our current architecture.


Inspired-by: coder#146

Co-authored-by: tommyme <chris.b.you@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant