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.

feat(glyphgrid): rasterize glyphs in their real colors over their real background

enes4745d4dfa860
diff --git a/src/renderer/glyphgrid/atlas.ts b/src/renderer/glyphgrid/atlas.ts
index 3363863bb..f3f29f70c 100644
--- a/src/renderer/glyphgrid/atlas.ts
+++ b/src/renderer/glyphgrid/atlas.ts
@@ -56,6 +56,17 @@ export interface GlyphRasterizer {
*/
export const GUTTER_PX = 2
+/** The slot PITCH for a cell extent, in whole texels — the ONE copy of that arithmetic.
+ *
+ * Exported because raster.ts needs the identical number: the atlas lays the page out on this
+ * pitch and the rasterizer fills exactly one pitch rect per slot with that slot's background.
+ * Two hand-written copies would drift the moment the gutter or the rounding changed, and the
+ * symptom is a background fill that either leaves a page-ground seam or overwrites a neighbour's
+ * gutter. See `GlyphAtlas.strideX` for why the pitch is rounded and the sampled extent is not. */
+export function slotPitch(cell: number): number {
+ return Math.max(1, Math.ceil(cell)) + 2 * GUTTER_PX
+}
+
/** One slot allocation, reported to an optional debug tap. See `GlyphAtlas`'s constructor.
*
* This exists for ONE open device bug: a single letter (`ç` in round 5, lowercase `x` in round 7)
@@ -148,10 +159,10 @@ export class GlyphAtlas {
* box, and `capacity`/`cellXY` would count slots that cannot hold a pixel.
*/
get strideX(): number {
- return Math.max(1, Math.ceil(this.rasterizer.cellW)) + 2 * GUTTER_PX
+ return slotPitch(this.rasterizer.cellW)
}
get strideY(): number {
- return Math.max(1, Math.ceil(this.rasterizer.cellH)) + 2 * GUTTER_PX
+ return slotPitch(this.rasterizer.cellH)
}
get capacity(): number {
diff --git a/src/renderer/glyphgrid/raster.test.ts b/src/renderer/glyphgrid/raster.test.ts
index 6645c048e..c28bfe940 100644
--- a/src/renderer/glyphgrid/raster.test.ts
+++ b/src/renderer/glyphgrid/raster.test.ts
@@ -1,17 +1,21 @@
import { afterEach, describe, expect, it } from 'vitest'
-import { GlyphAtlas } from './atlas'
+import { GlyphAtlas, GUTTER_PX } from './atlas'
+import { packColor } from './cells'
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.
+ * These tests pin the two rects a colored draw uses, because the whole mip story rests on them:
+ * the slot's own BACKGROUND covers the FULL PITCH rect (gutter included — that is what makes a
+ * level-1/2 texel blend this slot's background with itself rather than with the page ground),
+ * while the INK is clipped to the CELL rect and may never touch the gutter (the MAX_SAFE_LOD = 2
+ * derivation in `atlas.ts` counts on 2*GUTTER_PX clean texels between two slots' inks).
+ *
+ * Node has no OffscreenCanvas, so the contract is checked against a recording stub rather than
+ * real pixels.
*/
interface Op {
- kind: 'fillRect' | 'fillText' | 'clip'
+ kind: 'fillRect' | 'fillText' | 'clip' | 'clearRect'
fill?: string
args: number[]
text?: string
@@ -33,6 +37,9 @@ function stubCanvas(): { ops: Op[]; restore: () => void } {
fillRect(x: number, y: number, w: number, h: number) {
ops.push({ kind: 'fillRect', fill: ctx.fillStyle, args: [x, y, w, h] })
},
+ clearRect(x: number, y: number, w: number, h: number) {
+ ops.push({ kind: 'clearRect', args: [x, y, w, h] })
+ },
fillText(text: string, x: number, y: number) {
ops.push({ kind: 'fillText', fill: ctx.fillStyle, args: [x, y], text })
},
@@ -58,11 +65,20 @@ function stubCanvas(): { ops: Op[]; restore: () => void } {
}
const FONT = { family: 'monospace', sizePx: 16, cellW: 10, cellH: 20 }
+/** The pitch this font lays out on: ceil(cell) + a gutter on each side. */
+const PITCH_W = 10 + 2 * GUTTER_PX
+const PITCH_H = 20 + 2 * GUTTER_PX
+
+/** A realistic INK origin — the corner of a pitch cell plus one gutter on each axis, which is
+ * exactly what `GlyphAtlas.cellXY` hands the rasterizer. */
+const INK_X = PITCH_W + GUTTER_PX
+const INK_Y = PITCH_H + GUTTER_PX
-/** Packed colour lanes. T2 is what makes them observable in the ops below; for now they only have
- * to be threaded through the signature. */
-const FG = 0xffffffff
-const BG = 0xff000000
+/** Final packed colour lanes, deliberately not black/white so a swapped fg/bg is visible. */
+const FG = packColor(255, 160, 0, 255)
+const BG = packColor(16, 32, 48, 255)
+const FG_CSS = 'rgb(255,160,0)'
+const BG_CSS = 'rgb(16,32,48)'
let active: { restore: () => void } | null = null
afterEach(() => {
@@ -78,49 +94,127 @@ describe('createCanvasRasterizer', () => {
;(globalThis as Record<string, unknown>).OffscreenCanvas = prev
})
- it('paints the WHOLE page opaque black once at creation — the backdrop coverage 0 means', () => {
+ it('paints NOTHING at creation — the page ground stays transparent-black', () => {
const stub = stubCanvas()
active = stub
createCanvasRasterizer(FONT, 256)
- expect(stub.ops).toEqual([{ kind: 'fillRect', fill: '#000000', args: [0, 0, 256, 256] }])
+ // The old opaque-black page fill is GONE: the backdrop is now per-slot (the bg fill in
+ // `draw`), and a page-wide fill would put an opaque colour in slot 0, which every space
+ // samples.
+ expect(stub.ops).toEqual([])
})
- it('re-blacks a slot inside its clip, then draws WHITE ink over that backdrop', () => {
+ it('fills the FULL PITCH rect with bg, then clips the ink to the CELL rect and draws it in fg', () => {
const stub = stubCanvas()
active = stub
const r = createCanvasRasterizer(FONT, 256)!
stub.ops.length = 0
- r.draw(0x41, false, false, 10, 20, FG, BG) // 'A' into the slot at (10,20)
+ r.draw(0x41, false, false, INK_X, INK_Y, FG, BG)
- 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] })
+ // 1. The background covers the pitch cell — its origin is the INK origin minus one gutter on
+ // each axis, and its extent is the whole pitch (gutters included).
+ expect(stub.ops[0]).toEqual({
+ kind: 'fillRect',
+ fill: BG_CSS,
+ args: [INK_X - GUTTER_PX, INK_Y - GUTTER_PX, PITCH_W, PITCH_H]
+ })
+ // 2. The ink is clipped to the CELL rect, which starts at the ink origin.
+ expect(stub.ops[1]).toEqual({ kind: 'clip', args: [INK_X, INK_Y, FONT.cellW, FONT.cellH] })
+ // 3. The glyph itself, in the foreground colour.
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(ink.fill).toBe(FG_CSS)
+ // Baseline: unchanged half-leading convention, now measured from the ink origin (i.e. inside
+ // the cell rect, one gutter into the pitch cell), on a WHOLE device pixel.
+ expect(ink.args[0]).toBe(INK_X)
+ expect(ink.args[1]).toBe(INK_Y + 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', () => {
+ it('keeps every ink op inside the CELL rect — the gutter must stay clean for the mip chain', () => {
const stub = stubCanvas()
active = stub
const r = createCanvasRasterizer(FONT, 256)!
stub.ops.length = 0
- r.draw(0x2588, false, false, 0, 0, FG, BG) // █ full block
+ r.draw(0x2588, false, false, INK_X, INK_Y, FG, BG) // █ — the glyph that fills the whole cell
+
+ const ink = stub.ops.filter((o) => o.kind === 'fillRect' && o.fill === FG_CSS)
+ expect(ink.length).toBeGreaterThan(0)
+ for (const op of ink) {
+ const [x, y, w, h] = op.args
+ expect(x).toBeGreaterThanOrEqual(INK_X)
+ expect(y).toBeGreaterThanOrEqual(INK_Y)
+ expect(x + w).toBeLessThanOrEqual(INK_X + FONT.cellW)
+ expect(y + h).toBeLessThanOrEqual(INK_Y + FONT.cellH)
+ }
+ // And the only fill that is allowed outside the cell is the background one, which is exactly
+ // the pitch rect.
+ const outside = stub.ops.filter(
+ (o) =>
+ o.kind === 'fillRect' &&
+ (o.args[0] < INK_X ||
+ o.args[1] < INK_Y ||
+ o.args[0] + o.args[2] > INK_X + FONT.cellW ||
+ o.args[1] + o.args[3] > INK_Y + FONT.cellH)
+ )
+ expect(outside).toEqual([
+ {
+ kind: 'fillRect',
+ fill: BG_CSS,
+ args: [INK_X - GUTTER_PX, INK_Y - GUTTER_PX, PITCH_W, PITCH_H]
+ }
+ ])
+ })
+
+ it('draws the geometric box/block glyphs in the FOREGROUND over the bg fill, never with the font', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ stub.ops.length = 0
+ r.draw(0x2591, false, false, INK_X, INK_Y, FG, BG) // ░ — a stipple, fg pixels over bg
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[0].fill).toBe(BG_CSS) // the slot's own backdrop, first
+ expect(fills.slice(1).every((o) => o.fill === FG_CSS)).toBe(true)
expect(fills.length).toBeGreaterThan(1)
})
- it('never draws slot 0, so the blank slot every space samples stays black', () => {
+ it('converts both packed lanes to css rgb(), reading the RGB lanes in packColor order', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ stub.ops.length = 0
+ // A colour whose channels are all different, so a swapped r/b lane cannot pass.
+ r.draw(0x41, false, false, INK_X, INK_Y, packColor(1, 2, 3, 255), packColor(4, 5, 6, 255))
+ expect(stub.ops[0].fill).toBe('rgb(4,5,6)')
+ expect(stub.ops.find((o) => o.kind === 'fillText')!.fill).toBe('rgb(1,2,3)')
+ })
+
+ it('keys off the UNSIGNED lanes — a negative packed colour is the same colour', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ stub.ops.length = 0
+ // `0xff << 24` is negative in JS; an arithmetic path can hand us that spelling.
+ r.draw(0x41, false, false, INK_X, INK_Y, (0xff << 24) | 0x0000ff, (0xff << 24) | 0x00ff00)
+ expect(stub.ops[0].fill).toBe('rgb(0,255,0)')
+ expect(stub.ops.find((o) => o.kind === 'fillText')!.fill).toBe('rgb(255,0,0)')
+ })
+
+ it('clearPage() blanks the whole page back to transparent-black', () => {
+ const stub = stubCanvas()
+ active = stub
+ const r = createCanvasRasterizer(FONT, 256)!
+ stub.ops.length = 0
+ r.clearPage()
+ // clearRect, NOT a fill: the page ground has to go back to transparent-black so slot 0 stays
+ // the blank slot every space samples.
+ expect(stub.ops).toEqual([{ kind: 'clearRect', args: [0, 0, 256, 256] }])
+ })
+
+ it('never draws slot 0, so the blank slot every space samples stays transparent', () => {
const stub = stubCanvas()
active = stub
const r = createCanvasRasterizer(FONT, 256)!
@@ -128,8 +222,9 @@ describe('createCanvasRasterizer', () => {
stub.ops.length = 0
expect(atlas.glyphFor(0x20, false, false, FG, BG)).toBe(0)
expect(stub.ops).toEqual([])
- // The first real glyph starts at slot 1 — i.e. past the origin cell.
+ // The first real glyph starts at slot 1 — i.e. past the origin cell. Not even its background
+ // fill may reach slot 0's pitch rect.
atlas.glyphFor(0x41, false, false, FG, BG)
- expect(stub.ops.some((o) => o.args[0] === 0 && o.args[1] === 0)).toBe(false)
+ expect(stub.ops.some((o) => o.args[0] < PITCH_W && o.args[1] < PITCH_H)).toBe(false)
})
})
diff --git a/src/renderer/glyphgrid/raster.ts b/src/renderer/glyphgrid/raster.ts
index f7e800b08..dbc20cca8 100644
--- a/src/renderer/glyphgrid/raster.ts
+++ b/src/renderer/glyphgrid/raster.ts
@@ -1,5 +1,6 @@
-import type { GlyphRasterizer } from './atlas'
+import { GUTTER_PX, slotPitch, type GlyphRasterizer } from './atlas'
import { boxGlyphOps } from './box-glyphs'
+import { unpackColor } from './cells'
export interface RasterFont {
family: string
@@ -12,10 +13,27 @@ 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'
+/**
+ * A packed colour lane (`packColor`) as a css colour.
+ *
+ * ALPHA IS DROPPED, deliberately. Every lane that reaches this file has been resolved by the feed
+ * into a concrete on-screen colour (default/palette/RGB → inverse → dim → selection/cursor), and
+ * those all carry alpha 255 — xterm's own colour manager packs opaque colours for exactly the same
+ * reason: the terminal's cells are opaque. Emitting `rgba(...)` off a lane would only matter if a
+ * translucent colour ever appeared, and a translucent slot would break the invariant this file
+ * exists to hold (an OPAQUE per-slot backdrop for the platform rasterizer to draw over — see the
+ * header). So an unexpected alpha is ignored rather than honoured.
+ *
+ * `>>> 0` first: `0xff << 24` is NEGATIVE in JS, so a lane can arrive as -16777216 from an
+ * arithmetic path and as 4278190080 off a cell lane. `unpackColor`'s masks happen to give the same
+ * answer for both spellings, so this is not a fix for a live bug — it is the same normalization
+ * `GlyphAtlas.glyphFor` applies to its KEY, kept here so the two can never disagree about what
+ * "the same colour" means.
+ */
+function cssColor(packed: number): string {
+ const { r, g, b } = unpackColor(packed >>> 0)
+ return `rgb(${r},${g},${b})`
+}
/**
* Where the alphabetic baseline sits inside the cell, in device px.
@@ -62,35 +80,44 @@ function baselineIn(ctx: OffscreenCanvasRenderingContext2D, font: RasterFont): n
return Math.max(1, Math.min(Math.ceil(font.cellH), baseline))
}
-/** 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.
+/** Draws each glyph in its REAL foreground colour over its REAL background — the colour-keyed
+ * atlas xterm's own TextureAtlas builds — 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 BACKDROP, AND WHY THIS IS STILL THE ROUND-6 PROPERTY. macOS rasterizes text drawn onto a
+ * TRANSPARENT backdrop thinner and softer than the same text drawn over an opaque one, which is
+ * what made plain text look softer than the per-terminal WebglAddon. xterm never rasterizes onto
+ * transparency either — read `_drawToCache` in @xterm/addon-webgl: before every `fillText` it does
+ * `globalCompositeOperation='copy'; fillStyle=backgroundColor.css; fillRect(...)`, i.e. it hands
+ * the platform a fully painted backdrop. Round 6 approximated that with a page-wide opaque BLACK
+ * fill plus a white-ink/coverage-in-the-red-channel encoding that the shader re-mixed; that
+ * encoding is GONE (the re-mix had no correct gamma — see atlas.ts's header). The property it was
+ * protecting is not: every slot's PITCH RECT is filled with that slot's own opaque background
+ * before any ink lands, so CoreText still draws over an opaque backdrop. The page-wide black fill
+ * must NOT come back — it would put opaque black in slot 0, which every space samples.
*
* 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.
+ * LCD/subpixel antialiasing, whose per-channel coverage the RGBA blit would bake in per channel.
*
- * 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.
+ * THE PAGE INVARIANT, in one line: the backdrop is per-slot bg over the FULL PITCH rect; the
+ * inter-slot page ground is transparent-black and is never sampled at LOD <= MAX_SAFE_LOD.
+ * Unpacked, the four things this file must not break:
+ * 1. The page ground starts (and `clearPage` returns to) TRANSPARENT-black. The atlas's slot 0 —
+ * the pitch cell at 0,0 — is permanently blank and is what every space/unknown code point
+ * samples; `GlyphAtlas` never asks for slot 0, so nothing is ever drawn there and it stays
+ * transparent, which is what lets the shader leave the plate's own pixels alone.
+ * 2. A slot's whole pitch rect is REPAINTED with its background before it is inked, so reusing a
+ * cell after a reset can never leave a previous glyph's ink or a previous background behind.
+ * 3. INK NEVER ENTERS THE GUTTER. Every glyph is clipped to the CELL rect — one gutter inside the
+ * pitch rect on each axis — so two slots' inks stay 2*GUTTER_PX texels apart, which is exactly
+ * the separation `MAX_SAFE_LOD = 2` is derived from (see GUTTER_PX in atlas.ts). A glyph wider
+ * than the cell (a CJK cell, an overhanging italic) is CUT, not allowed to overhang: a soft
+ * edge is a cosmetic loss on one glyph, a ghost glyph in a neighbour's mip is a rendering bug.
+ * 4. The background fill covers the pitch EXACTLY — not more (it would erase a neighbour's
+ * gutter, and with it that neighbour's mip skirt) and not less (a strip of page ground inside
+ * the mip neighbourhood would blend transparency into the slot at LOD 1/2).
*
* 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
@@ -105,45 +132,57 @@ export function createCanvasRasterizer(
const ctx = canvas.getContext('2d', { alpha: true })
if (!ctx) return null
ctx.textBaseline = 'alphabetic'
- // 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)
+ // NOTHING is painted here. A fresh OffscreenCanvas is already transparent-black, which is the
+ // page ground this atlas wants; the backdrop the platform rasterizer needs is per-slot and is
+ // painted in `draw`. (Round 6's page-wide opaque black fill lived here — see the header for why
+ // it must not come back.)
const baseline = baselineIn(ctx, font)
+ // The slot pitch, from the SAME helper the atlas lays the page out with — the background fill
+ // below has to cover exactly one pitch cell.
+ const pitchW = slotPitch(font.cellW)
+ const pitchH = slotPitch(font.cellH)
return {
cellW: font.cellW,
cellH: font.cellH,
get source() {
return canvas
},
- /** T2 fills this in: blank the page back to the state `createCanvasRasterizer` leaves it in.
- * Kept mechanical here — the atlas's reset path needs the member to exist, and the colour
- * contract (transparent-black page, per-slot backdrops) is Task 2's to land. */
+ /** Blank the page back to the state `createCanvasRasterizer` leaves it in: TRANSPARENT-black,
+ * everywhere. `clearRect`, not a fill — a fill of any colour would give slot 0 (and every
+ * never-allocated pitch cell) an opaque colour, and slot 0 is what every space samples. */
clearPage() {
- ctx.fillStyle = INK_OFF
- ctx.fillRect(0, 0, atlasSizePx, atlasSizePx)
+ ctx.clearRect(0, 0, atlasSizePx, atlasSizePx)
},
- // fg/bg are the FINAL packed colour lanes for this slot. T2 fills this in: fill the whole
- // PITCH rect (gutter included) with `bg` and paint the glyph/box ops in `fg`, so the atlas
- // holds CoreText's own coloured pixels. Until then the draw stays monochrome — the signature
- // is threaded now because the atlas keys on the colours from this task on.
- draw(code, bold, italic, x, y, _fg, _bg) {
+ /** `x, y` is the INK origin the atlas hands us — already one gutter inside the pitch cell on
+ * each axis (`GlyphAtlas.cellXY`) — and `fg`/`bg` are the FINAL packed colour lanes for this
+ * slot. Two DIFFERENT rects are involved; see the header's invariants 3 and 4. */
+ draw(code, bold, italic, x, y, fg, bg) {
+ // 1. THE BACKGROUND, over the whole PITCH rect — gutters included. The atlas passes the INK
+ // origin, so the pitch cell's corner is one gutter back on each axis; its extent is the
+ // pitch, which is `ceil(cell) + 2*GUTTER_PX`. Both are whole texels and consecutive pitch
+ // cells are exactly `pitch` apart, so this fill tiles its own slot and touches no other.
+ // Unclipped on purpose: the rect is already exact, and the fill is what has to reach the
+ // gutter (a gutter carrying this slot's own background is what makes mip bleed invisible).
+ // It also repaints everything a previous tenant of this slot left behind.
+ ctx.fillStyle = cssColor(bg)
+ ctx.fillRect(x - GUTTER_PX, y - GUTTER_PX, pitchW, pitchH)
+ // 2. THE INK, clipped to the CELL rect — never the pitch rect. The cell always fits inside
+ // the pitch (the pitch rounds the cell UP and adds a gutter on each side), so this clip
+ // leaves at least GUTTER_PX ink-free texels on every side, which is the separation
+ // MAX_SAFE_LOD = 2 is derived from. A glyph that overflows the cell is CUT here.
ctx.save()
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
+ ctx.fillStyle = cssColor(fg)
// 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: white ink (coverage 1) or the black backdrop (coverage 0).
+ // `PaintOp` is alpha-free by construction (x/y/w/h only) — including the shade blocks, which
+ // are DITHER patterns rather than tints. So `globalAlpha` is never touched here and a stipple
+ // reads as foreground PIXELS over the background fill, which is exactly what xterm's own
+ // patterns are.
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)