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): monochrome glyph atlas

enes01aca568eb59
diff --git a/src/renderer/glyphgrid/atlas.test.ts b/src/renderer/glyphgrid/atlas.test.ts
new file mode 100644
index 000000000..5d49fdb8c
--- /dev/null
+++ b/src/renderer/glyphgrid/atlas.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from 'vitest'
+import { GlyphAtlas, type GlyphRasterizer } from './atlas'
+
+function fakeRasterizer(cellW = 10, cellH = 20): GlyphRasterizer & { calls: string[] } {
+ const calls: string[] = []
+ return {
+ cellW,
+ cellH,
+ calls,
+ source: null,
+ draw(code, bold, italic, x, y) {
+ calls.push(`${code}|${bold ? 'b' : ''}${italic ? 'i' : ''}@${x},${y}`)
+ }
+ }
+}
+
+describe('GlyphAtlas', () => {
+ it('slot 0 is blank and never rasterized; first real glyph gets slot 1', () => {
+ const r = fakeRasterizer()
+ const atlas = new GlyphAtlas(r, 100)
+ expect(atlas.glyphFor(0x20, false, false)).toBe(0) // space = blank slot, no draw
+ expect(r.calls).toEqual([])
+ expect(atlas.glyphFor(0x41, false, false)).toBe(1) // 'A'
+ expect(r.calls).toHaveLength(1)
+ })
+
+ it('same key is cached (one rasterization, stable slot); styles are distinct keys', () => {
+ const r = fakeRasterizer()
+ const atlas = new GlyphAtlas(r, 100)
+ const a = atlas.glyphFor(0x41, false, false)
+ expect(atlas.glyphFor(0x41, false, false)).toBe(a)
+ expect(atlas.glyphFor(0x41, true, false)).not.toBe(a)
+ expect(r.calls).toHaveLength(2)
+ })
+
+ it('lays slots out row-major within the page and reports correct uv rects', () => {
+ const r = fakeRasterizer(10, 20) // 100px page → 10 cols × 5 rows
+ const atlas = new GlyphAtlas(r, 100)
+ for (let i = 0; i < 11; i++) atlas.glyphFor(0x30 + i, false, false) // '0'..':' fills into row 2
+ // The i-th glyph lands in slot i+1 (slot 0 is blank), so calls[9] is '9' (0x39) in slot 10.
+ // slot 10 = row 1, col 0 → x=0, y=20; slot 11 = row 1, col 1 → x=10, y=20.
+ expect(r.calls[8]).toBe(`${0x38}|@90,0`) // slot 9 = last cell of row 0
+ expect(r.calls[9]).toBe(`${0x39}|@0,20`)
+ expect(r.calls[10]).toBe(`${0x3a}|@10,20`)
+ const rect = atlas.slotRect(1)
+ expect(rect).toEqual({ u0: 0.1, v0: 0, u1: 0.2, v1: 0.2 })
+ })
+
+ it('sets dirty on new glyphs and clears on demand', () => {
+ const atlas = new GlyphAtlas(fakeRasterizer(), 100)
+ expect(atlas.dirty).toBe(false)
+ atlas.glyphFor(0x41, false, false)
+ expect(atlas.dirty).toBe(true)
+ atlas.clearDirty()
+ expect(atlas.dirty).toBe(false)
+ })
+
+ it('a full page falls back to the blank slot instead of overflowing', () => {
+ const r = fakeRasterizer(10, 20)
+ const atlas = new GlyphAtlas(r, 20) // 2 cols × 1 row = capacity 2 (slot 0 blank + 1 real)
+ expect(atlas.capacity).toBe(2)
+ expect(atlas.glyphFor(0x41, false, false)).toBe(1)
+ expect(atlas.glyphFor(0x42, false, false)).toBe(0) // full → blank, never throws
+ })
+})
diff --git a/src/renderer/glyphgrid/atlas.ts b/src/renderer/glyphgrid/atlas.ts
new file mode 100644
index 000000000..ae7dbd6c6
--- /dev/null
+++ b/src/renderer/glyphgrid/atlas.ts
@@ -0,0 +1,65 @@
+/** Monochrome glyph atlas BOOKKEEPING. Rasterization is injected (GlyphRasterizer) so this
+ * module is pure and unit-testable; the real rasterizer (raster.ts) draws white glyphs onto an
+ * OffscreenCanvas that gl-webgl2.ts uploads as the atlas texture. Colors are per-cell instance
+ * data tinted in the shader — that is what keeps the key space to code|bold|italic. */
+export interface GlyphRasterizer {
+ cellW: number
+ cellH: number
+ draw(code: number, bold: boolean, italic: boolean, x: number, y: number): void
+ readonly source: TexImageSource | null
+}
+
+export class GlyphAtlas {
+ private slots = new Map<string, number>()
+ private nextSlot = 1 // slot 0 is permanently blank
+ private dirtyFlag = false
+
+ constructor(
+ private rasterizer: GlyphRasterizer,
+ private sizePx = 1024
+ ) {}
+
+ get capacity(): number {
+ const cols = Math.floor(this.sizePx / this.rasterizer.cellW)
+ const rows = Math.floor(this.sizePx / this.rasterizer.cellH)
+ return cols * rows
+ }
+
+ glyphFor(code: number, bold: boolean, italic: boolean): number {
+ if (code === 0x20 || code === 0) return 0
+ const key = `${code}|${bold ? 1 : 0}${italic ? 1 : 0}`
+ const hit = this.slots.get(key)
+ if (hit !== undefined) return hit
+ if (this.nextSlot >= this.capacity) return 0 // page full — degrade to blank, never throw
+ const slot = this.nextSlot++
+ const cols = Math.floor(this.sizePx / this.rasterizer.cellW)
+ const x = (slot % cols) * this.rasterizer.cellW
+ const y = Math.floor(slot / cols) * this.rasterizer.cellH
+ this.rasterizer.draw(code, bold, italic, x, y)
+ this.slots.set(key, slot)
+ this.dirtyFlag = true
+ return slot
+ }
+
+ slotRect(slot: number): { u0: number; v0: number; u1: number; v1: number } {
+ const cols = Math.floor(this.sizePx / this.rasterizer.cellW)
+ const x = (slot % cols) * this.rasterizer.cellW
+ const y = Math.floor(slot / cols) * this.rasterizer.cellH
+ return {
+ u0: x / this.sizePx,
+ v0: y / this.sizePx,
+ u1: (x + this.rasterizer.cellW) / this.sizePx,
+ v1: (y + this.rasterizer.cellH) / this.sizePx
+ }
+ }
+
+ get dirty(): boolean {
+ return this.dirtyFlag
+ }
+ clearDirty(): void {
+ this.dirtyFlag = false
+ }
+ get source(): TexImageSource | null {
+ return this.rasterizer.source
+ }
+}