Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(glyphgrid): give the rasterizer a backdrop — opaque atlas page, coverage off the red channel

enesc0a1586398cb
diff --git a/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md b/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md
index 0b2e0ff94..9ddeea586 100644
--- a/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md
+++ b/docs/superpowers/plans/2026-08-03-phase1b-device-checklist.md
@@ -94,6 +94,16 @@ Auto after you switched builds, that is why: re-select Shared and carry on.
RASTER itself (the canvas rasterizer's antialiasing / baseline rounding, or L14's
first-terminal cell latch), not at the sampler. Also zoom OUT past 1 and confirm the text
degrades smoothly rather than speckling.
+ **Round 6 addressed exactly that raster.** The atlas is no longer drawn onto transparency:
+ the page is opaque BLACK, the ink is WHITE, and the shader reads coverage off the RED
+ channel — the same backdrop xterm's own `TextureAtlas._drawToCache` hands the platform
+ rasterizer before every `fillText`, and the reason its glyphs come out at full weight on
+ macOS. Judge plain text (a paragraph of prose, `man bash`, a source file) against the
+ per-terminal GPU renderer at dpr 2 and dpr 1. **If a weight/softness gap still remains after
+ this, stop tuning the rasterizer**: the next step is xterm's full approach — a COLOR atlas
+ keyed by `(code, style, fg, bg)` with per-glyph ink-box cropping — which reworks `atlas.ts`,
+ `raster.ts` and the cell/uv contract together and is therefore a **Phase 2** item, not a
+ round-7 patch. Report it as "2.7 raster gap → Phase 2 color atlas".
- [ ] **2.8 Selection visual.** Drag-select inside a terminal: the selection band covers exactly
the selected cells, with correct fg/bg inversion, and matches what the DOM renderer draws.
- [ ] **2.9 Cursor.** A focused terminal shows a solid block cursor at the right cell. It is
diff --git a/src/renderer/glyphgrid/atlas.ts b/src/renderer/glyphgrid/atlas.ts
index 1b5773145..b50892ea2 100644
--- a/src/renderer/glyphgrid/atlas.ts
+++ b/src/renderer/glyphgrid/atlas.ts
@@ -13,9 +13,10 @@ export class GlyphAtlas {
private slots = new Map<string, number>()
/** Slot 0 is permanently blank and is never handed to the rasterizer: every space, every
* unknown code point and every cell of a not-yet-uploaded GPU buffer samples it. Its
- * BLANKNESS is a contract on raster.ts — the page starts fully transparent and nothing may
- * ever draw at (0,0) — not something this file can enforce, so the two must change together.
- * If ink ever lands there, every space on the canvas grows a glyph. */
+ * BLANKNESS is a contract on raster.ts — the page is opaque BLACK (= coverage 0; ink is white
+ * and the shader reads the luminance) and nothing may ever draw at (0,0), so slot 0 simply
+ * stays black — not something this file can enforce, so the two must change together. If ink
+ * ever lands there, every space on the canvas grows a glyph. */
private nextSlot = 1
private dirtyFlag = false
diff --git a/src/renderer/glyphgrid/box-glyphs.ts b/src/renderer/glyphgrid/box-glyphs.ts
index a9c87e035..090ebe056 100644
--- a/src/renderer/glyphgrid/box-glyphs.ts
+++ b/src/renderer/glyphgrid/box-glyphs.ts
@@ -52,6 +52,10 @@
* shade blocks ░▒▓ — is a dither pattern, not a tint (see `SHADES`), and an alpha field would
* make it possible to "simplify" them back into flat washes that no longer match the renderer
* beside them. Everything this module draws is ink or nothing.
+ *
+ * "Opaque" is the atlas's ink colour, not an alpha value: `raster.ts` fills these rects WHITE onto
+ * its opaque BLACK page, so a set pixel is coverage 1 and everything else is coverage 0 — the same
+ * two states the fillText path produces, and the reason this module never touches `globalAlpha`.
*/
export interface PaintOp {
x: number
diff --git a/src/renderer/glyphgrid/gl-webgl2.ts b/src/renderer/glyphgrid/gl-webgl2.ts
index 8c8eb6e72..2e40a9205 100644
--- a/src/renderer/glyphgrid/gl-webgl2.ts
+++ b/src/renderer/glyphgrid/gl-webgl2.ts
@@ -46,7 +46,13 @@ vec4 rgba8(uint c) {
void main() {
vec4 bg = rgba8(vCell.z);
vec4 fg = rgba8(vCell.y);
- float glyph = texture(uAtlas, vUv).a;
+ // COVERAGE COMES OFF THE RED CHANNEL, not alpha. raster.ts paints the atlas page opaque BLACK
+ // and the ink WHITE: giving the platform rasterizer a real backdrop is what gets full-weight
+ // glyphs out of CoreText on macOS (text drawn onto transparency comes out thin and soft), and it
+ // is exactly what xterm's own TextureAtlas does: _drawToCache fills the tile with the
+ // background color before every fillText. So the page's LUMINANCE is the coverage; its alpha is
+ // 1 everywhere now and carries no information.
+ float glyph = texture(uAtlas, vUv).r;
outColor = mix(bg, vec4(fg.rgb, 1.0), glyph * fg.a);
}`
diff --git a/src/renderer/glyphgrid/raster.test.ts b/src/renderer/glyphgrid/raster.test.ts
new file mode 100644
index 000000000..2752c521f
--- /dev/null
+++ b/src/renderer/glyphgrid/raster.test.ts
@@ -0,0 +1,130 @@
+import { afterEach, describe, expect, it } from 'vitest'
+import { GlyphAtlas } from './atlas'
+import { createCanvasRasterizer } from './raster'
+
+/**
+ * These tests exist for ONE reason: the atlas page is opaque black with white ink, and the shader
+ * reads coverage off the RED channel (`gl-webgl2.ts`). If this file ever goes back to rasterizing
+ * onto transparency — which is what made plain text render thinner than the per-terminal
+ * WebglAddon on macOS — the shader reads 0 everywhere and the terminal goes blank. Node has no
+ * OffscreenCanvas, so the contract is checked against a recording stub rather than real pixels.
+ */
+
+interface Op {
+ kind: 'fillRect' | 'fillText' | 'clip'
+ fill?: string
+ args: number[]
+ text?: string
+}
+
+function stubCanvas(): { ops: Op[]; restore: () => void } {
+ const ops: Op[] = []
+ const ctx = {
+ font: '',
+ textBaseline: '',
+ fillStyle: '',
+ save() {},
+ restore() {},
+ beginPath() {},
+ rect(x: number, y: number, w: number, h: number) {
+ ops.push({ kind: 'clip', args: [x, y, w, h] })
+ },
+ clip() {},
+ fillRect(x: number, y: number, w: number, h: number) {
+ ops.push({ kind: 'fillRect', fill: ctx.fillStyle, args: [x, y, w, h] })
+ },
+ fillText(text: string, x: number, y: number) {
+ ops.push({ kind: 'fillText', fill: ctx.fillStyle, args: [x, y], text })
+ },
+ // A face whose line box exactly fills a 20px cell, so the baseline math is not the subject.
+ measureText: () => ({ fontBoundingBoxAscent: 16, fontBoundingBoxDescent: 4 })
+ }
+ const prev = (globalThis as Record<string, unknown>).OffscreenCanvas
+ ;(globalThis as Record<string, unknown>).OffscreenCanvas = class {
+ constructor(
+ public width: number,
+ public height: number
+ ) {}
+ getContext(): unknown {
+ return ctx
+ }
+ }
+ return {
+ ops,
+ restore() {
+ ;(globalThis as Record<string, unknown>).OffscreenCanvas = prev
+ }
+ }
+}
+
+const FONT = { family: 'monospace', sizePx: 16, cellW: 10, cellH: 20 }
+
+let active: { restore: () => void } | null = null
+afterEach(() => {
+ active?.restore()
+ active = null
+})
+
+describe('createCanvasRasterizer', () => {
+ it('degrades to null (caller keeps the DOM renderer) when OffscreenCanvas is unavailable', () => {
+ const prev = (globalThis as Record<string, unknown>).OffscreenCanvas
+ delete (globalThis as Record<string, unknown>).OffscreenCanvas
+ expect(createCanvasRasterizer(FONT, 256)).toBeNull()
+ ;(globalThis as Record<string, unknown>).OffscreenCanvas = prev
+ })
+
+ it('paints the WHOLE page opaque black once at creation — the backdrop coverage 0 means', () => {
+ const stub = stubCanvas()
+ active = stub
+ createCanvasRasterizer(FONT, 256)
+ expect(stub.ops).toEqual([{ kind: 'fillRect', fill: '#000000', args: [0, 0, 256, 256] }])
+ })
+
+ it('re-blacks a slot inside its clip, then draws WHITE ink over that backdrop', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ stub.ops.length = 0
+ r.draw(0x41, false, false, 10, 20) // 'A' into the slot at (10,20)
+
+ expect(stub.ops[0]).toEqual({ kind: 'clip', args: [10, 20, 10, 20] })
+ // The black fill is the slot's own rect — never the page, and the clip above keeps it off the
+ // neighbouring slots even if a future cell size disagreed.
+ expect(stub.ops[1]).toEqual({ kind: 'fillRect', fill: '#000000', args: [10, 20, 10, 20] })
+ const ink = stub.ops[2]
+ expect(ink.kind).toBe('fillText')
+ expect(ink.text).toBe('A')
+ expect(ink.fill).toBe('#ffffff')
+ // Baseline: half-leading of a line box that exactly fills the cell → the ascent, on a WHOLE
+ // device pixel (a fractional baseline would give each glyph a different NEAREST cut).
+ expect(ink.args[1]).toBe(20 + 16)
+ expect(Number.isInteger(ink.args[1])).toBe(true)
+ })
+
+ it('draws the geometric box/block glyphs as WHITE rects (coverage 1), never with the font', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ stub.ops.length = 0
+ r.draw(0x2588, false, false, 0, 0) // █ full block
+
+ expect(stub.ops.some((o) => o.kind === 'fillText')).toBe(false)
+ const fills = stub.ops.filter((o) => o.kind === 'fillRect')
+ expect(fills[0].fill).toBe('#000000') // the slot's backdrop
+ expect(fills.slice(1).every((o) => o.fill === '#ffffff')).toBe(true)
+ expect(fills.length).toBeGreaterThan(1)
+ })
+
+ it('never draws slot 0, so the blank slot every space samples stays black', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ const atlas = new GlyphAtlas(r, 256)
+ stub.ops.length = 0
+ expect(atlas.glyphFor(0x20, false, false)).toBe(0)
+ expect(stub.ops).toEqual([])
+ // The first real glyph starts at slot 1 — i.e. past the origin cell.
+ atlas.glyphFor(0x41, false, false)
+ expect(stub.ops.some((o) => o.args[0] === 0 && o.args[1] === 0)).toBe(false)
+ })
+})
diff --git a/src/renderer/glyphgrid/raster.ts b/src/renderer/glyphgrid/raster.ts
index d5744788e..99a0d8a81 100644
--- a/src/renderer/glyphgrid/raster.ts
+++ b/src/renderer/glyphgrid/raster.ts
@@ -12,6 +12,11 @@ export interface RasterFont {
cellH: number
}
+/** The two states a texel of this atlas can be in. Coverage is the LUMINANCE of the page, read off
+ * the red channel in the shader — so "no ink" must be opaque BLACK, not transparency. */
+const INK_OFF = '#000000'
+const INK_ON = '#ffffff'
+
/**
* Where the alphabetic baseline sits inside the cell, in device px.
*
@@ -28,6 +33,23 @@ export interface RasterFont {
* Clamped into the cell: a font whose line box is much taller than the cell would otherwise put
* the baseline outside the slot, and every draw is clipped to it (see below) — the glyph would
* simply vanish.
+ *
+ * WHY NOT xterm's CONVENTION (checked in round 6, deliberately left alone). The WebglAddon's
+ * `_drawToCache` sets `textBaseline = TEXT_BASELINE` (`'ideographic'`, `'bottom'` on Firefox) and
+ * draws at `y = padding + deviceCharHeight` — i.e. the bottom of the char box is pinned to the
+ * char height and any extra `lineHeight` leading falls entirely BELOW the glyph. Two reasons ours
+ * stays as it is:
+ * - At the default `lineHeight: 1` the two agree to within rounding. `deviceCharHeight` is the
+ * measured line box (`ceil(charSize.height * dpr)`, DomRenderer `_updateDimensions`), so
+ * `cellH ≈ asc + desc`, the half-leading term collapses to ~0 and our baseline lands on `asc`
+ * — the same row xterm's ideographic-at-cell-bottom resolves to.
+ * - Both land on a WHOLE device pixel: xterm's `deviceCharHeight` is `ceil`'d, ours is
+ * `Math.round`'d. Sharpness is what a fractional baseline would cost (the atlas is sampled 1:1
+ * with NEAREST, so a half-texel of vertical ink offset is a different cut per glyph); an
+ * integer baseline has no such cost, and any residual disagreement is ≤0.5px of PLACEMENT, not
+ * of crispness.
+ * Where they genuinely differ (`lineHeight != 1`) the half-leading answer is the one that matches
+ * the DOM renderer these terminals fall back to, so it is the one worth keeping.
*/
function baselineIn(ctx: OffscreenCanvasRenderingContext2D, font: RasterFont): number {
ctx.font = `${font.sizePx}px ${font.family}`
@@ -40,17 +62,35 @@ function baselineIn(ctx: OffscreenCanvasRenderingContext2D, font: RasterFont): n
return Math.max(1, Math.min(Math.ceil(font.cellH), baseline))
}
-/** Draws WHITE glyphs (the shader tints); baseline-centered in the cell like xterm's renderers.
- * Returns null when OffscreenCanvas 2D is unavailable — caller keeps the DOM renderer.
+/** Draws WHITE glyphs on an OPAQUE BLACK page (the shader tints); baseline-centered in the cell
+ * like xterm's renderers. Returns null when OffscreenCanvas 2D is unavailable — caller keeps the
+ * DOM renderer.
+ *
+ * WHY THE PAGE IS OPAQUE (round 6 — "plain text is still softer than the WebglAddon"): macOS
+ * rasterizes text drawn onto a TRANSPARENT backdrop thinner and softer than the same text drawn
+ * over an opaque one. xterm's own TextureAtlas never rasterizes onto transparency — read
+ * `_drawToCache` in @xterm/addon-webgl: before every `fillText` it does
+ * `globalCompositeOperation='copy'; fillStyle=backgroundColor.css; fillRect(0,0,w,h)`, i.e. it
+ * hands the platform a fully painted backdrop, and only AFTERWARDS (`clearColor`, on the read-back
+ * ImageData) does it punch the background pixels' alpha to 0. Our atlas is monochrome + tinted in
+ * the shader rather than color-keyed, so we get the same input to the rasterizer the cheap way:
+ * the page is black, the ink is white, and COVERAGE IS THE LUMINANCE — read off the RED channel
+ * by the fragment shader (`gl-webgl2.ts`). The alpha channel is now 1 everywhere and unused.
+ *
+ * The context is deliberately still `alpha: true` — exactly like xterm's tmp canvas, which is a
+ * plain `document.createElement('canvas')`. The opacity that matters is the PAINTED backdrop, not
+ * the backing store's format, and an alpha-less canvas would additionally let Chromium turn on
+ * LCD/subpixel antialiasing, whose per-channel coverage would make sampling one channel wrong.
*
- * Two invariants this file must not break:
- * 1. The canvas is left fully TRANSPARENT except where a glyph is drawn — the atlas's slot 0
- * (the cell at 0,0) is permanently blank and is what every space/unknown glyph samples. So
- * never clear/fill the WHOLE canvas here: GlyphAtlas never asks for slot 0, and a
- * full-surface fill would put ink under it. A slot-granular clear (evicting one glyph to
- * reuse its cell) is a legitimate future need and does not violate this.
- * 2. Every draw is CLIPPED to its cell rect, so a glyph wider than cellW (a CJK cell, an
- * overhanging italic) cannot bleed into the neighbouring slot's texels.
+ * Three invariants this file must not break:
+ * 1. The page is opaque BLACK where no glyph has been drawn — coverage 0. The atlas's slot 0
+ * (the cell at 0,0) is permanently blank and is what every space/unknown glyph samples;
+ * `GlyphAtlas` never asks for slot 0, so slot 0 is never drawn and simply stays black.
+ * 2. A slot is re-blacked (not cleared to transparency) before it is drawn, so evicting and
+ * reusing a cell can never leave a previous glyph's ink behind as coverage.
+ * 3. Every draw is CLIPPED to its cell rect, so a glyph wider than cellW (a CJK cell, an
+ * overhanging italic) cannot bleed into the neighbouring slot's texels — and the per-slot
+ * black fill above is clipped by the same rect, so it can never erase a neighbour.
*
* And one thing it must not START doing: draw the box-drawing / block-element ranges with the
* FONT. `boxGlyphOps` gets first refusal on every code point (see box-glyphs.ts for why — fonts
@@ -65,7 +105,10 @@ export function createCanvasRasterizer(
const ctx = canvas.getContext('2d', { alpha: true })
if (!ctx) return null
ctx.textBaseline = 'alphabetic'
- ctx.fillStyle = '#ffffff'
+ // The backdrop, once, for the whole page: every texel starts at coverage 0 AND opaque, which is
+ // what gives the platform rasterizer something to draw over (see the header).
+ ctx.fillStyle = INK_OFF
+ ctx.fillRect(0, 0, atlasSizePx, atlasSizePx)
const baseline = baselineIn(ctx, font)
return {
cellW: font.cellW,
@@ -78,13 +121,18 @@ export function createCanvasRasterizer(
ctx.beginPath()
ctx.rect(x, y, font.cellW, font.cellH)
ctx.clip()
+ // Re-black this slot only (the clip keeps it off the neighbours): drawing over a reused cell
+ // must start from coverage 0, and 'source-over' white ink cannot subtract old ink.
+ ctx.fillStyle = INK_OFF
+ ctx.fillRect(x, y, font.cellW, font.cellH)
+ ctx.fillStyle = INK_ON
// Geometry first: these ranges are DEFINED as fractions of the cell, so drawing them is
// both more correct and cheaper than trusting the face. The ops are already snapped to
// device px (interior edges) and to the exact cell bounds (outer edges), so no seam can
// appear between two adjacent cells and no rect lands on a half pixel.
// The ops are opaque white by construction — including the shade blocks, which are DITHER
// patterns rather than tints, so `globalAlpha` is never touched here and the atlas keeps
- // exactly two states per texel: ink or transparent.
+ // exactly two states per texel: white ink (coverage 1) or the black backdrop (coverage 0).
const geometry = boxGlyphOps(code, font.cellW, font.cellH)
if (geometry) {
for (const op of geometry) ctx.fillRect(x + op.x, y + op.y, op.w, op.h)