From 71e5a0f4a8685b385fac762995d45575b638179f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 22:21:45 -0300 Subject: kinds #1, the paint contract: a picture over the cells is one Placement (raster: ptr, w, h, stride; src in pixels; dst in the frame's cells; at, where in them in pixels, or none for the frontend to fit; key, the pixels' identity by pane serial, page and revision; a terminal program's kitty z; dirty, the rows changed since an older key; art, the pane's Petscii, Palette and Ascii), where ImagePlace carried native.{revision, page, fit, pan_x, pan_y, geometry, pixel_offset_y, paper_bg, exact}, cell rects and an ImageCacheKey of three shapes by build; a PDF strip's scroll is its body region's scroll_px, whose fraction the window draws as pixel_offset_y was; glyph art leaves the core for src/Art.zig, the frontends' (the matcher, both palettes and a cache by key, ex image.zig's GlyphArt and TerminalImages' glyph cache): the core always hands an image pane, a PDF's pages and a terminal's kitty images as placements, whatever the frontend shows, and the tty draws them as glyph art without kitty graphics or when the pane asks, the window when the pane asks; the four words set the same pane bytes as before and the frontend reads them off the placement, so config.zig, builtins.zig and exec.zig are untouched; /screen lists a frame's placements as images (only a frame with pictures has the field); no golden changes: the snapshot goldens are the tty's, which draws the same art (images.snap and tagbottomimage.snap pass unchanged, and the tty's bytes for an image, its Palette and its Ascii are byte-identical to the base's), no /screen golden holds a picture, and the window's goldens hash the same; tests: kitty.zig's glyph-art case becomes its core half (a placement over the 8x4 cells, the cells the pane's) with the art in tui.zig's own test, terminal.zig's glyph-art case the same, surface.zig's two ImageCacheKey cases one keyOf case, the GlyphArt cases move to Art.zig with a frame case; gates: core-test 959/959, behaviour-test 244/244, unit-test 1486/1487 tui and 1530/1531 gui, pane-test 171/172, snap all ok, process-test 0 of 37 failed on both, hxdiff 19 and hxparity 6 mismatches by the base's names, replicas, gui-golden, monkey-gui, kitty-test 44/44, kitty-harness and image-harness on both, daemon.py main and window-clients Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Art.zig | 618 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Core.zig | 8 +- src/TerminalImages.zig | 99 ++------ src/draw.zig | 4 +- src/gui/gui.zig | 196 ++++++++-------- src/image.zig | 567 ++------------------------------------------- src/macos.zig | 78 +++---- src/ninep/screen.zig | 23 +- src/pardes.zig | 8 +- src/pdf_view.zig | 78 ++----- src/surface.zig | 180 ++++++-------- src/terminal.zig | 72 +++--- src/tui/tui.zig | 279 +++++++++++----------- 13 files changed, 1081 insertions(+), 1129 deletions(-) create mode 100644 src/Art.zig (limited to 'src') diff --git a/src/Art.zig b/src/Art.zig new file mode 100644 index 00000000..6a938f68 --- /dev/null +++ b/src/Art.zig @@ -0,0 +1,618 @@ +//! Glyph art: a picture drawn in a frame's cells with block, line and +//! letter glyphs in sixteen colours (the matcher is adapted from +//! caioluders/petsciinator). It is the frontends' (tui.zig always where the +//! terminal shows no pixels, gui.zig where a pane asks): the core hands a +//! picture as a `Placement` and never draws it, so its grid, `/screen`, is +//! the same on every client. One of these a frontend keeps the art of the +//! pictures it shows, while it shows them. +const std = @import("std"); +const ghostty_vt = @import("ghostty-vt"); +const pardes = @import("pardes.zig"); + +const Art = @This(); +const Surface = pardes.Surface; +const Placement = pardes.Placement; +const Raster = pardes.Raster; +const RectPx = pardes.RectPx; + +grids: std.AutoHashMapUnmanaged(Key, Kept) = .empty, + +/// What a grid was made from: the picture, the part of it, the cells, and +/// how (fitted when the placement leaves its place to the frontend). +const Key = struct { key: u64, src: RectPx, cols: u16, rows: u16, fitted: bool, palette: Placement.Art.Palette, ascii: bool }; +const Kept = struct { grid: Grid, used: bool = true }; + +pub fn deinit(art: *Art, gpa: std.mem.Allocator) void { + var it = art.grids.valueIterator(); + while (it.next()) |kept| gpa.free(kept.grid.cells); + art.grids.deinit(gpa); +} + +/// `s` as this frontend draws it: every placement it shows as glyph art +/// (each one where it shows no `pixels`, else those whose pane asks for +/// art) painted into a copy of the frame made in `arena`; `s` itself when +/// there is none. `cell_h` is a cell's height in the frontend's pixels: a +/// sliver of a row (under half of it) has no art. +pub fn frame(art: *Art, gpa: std.mem.Allocator, arena: std.mem.Allocator, s: *const Surface, pixels: bool, cell_h: u32) !*const Surface { + var it = art.grids.valueIterator(); + while (it.next()) |kept| kept.used = false; + defer art.prune(gpa); + var out: ?*Surface = null; + for (s.placements.items) |place| { + if (pixels and !place.art.glyph) continue; + const grid = art.gridOf(gpa, place, cell_h) orelse continue; + const drawn = out orelse copy: { + const copy = try arena.create(Surface); + copy.* = s.*; + copy.cells = try arena.dupe(pardes.Cell, s.cells); + out = copy; + break :copy copy; + }; + // Fitted, the art is centred in its cells; else it fills them. + const offx = if (place.at == null) (place.dst.w - grid.cols) / 2 else 0; + const offy = if (place.at == null) (place.dst.h - grid.rows) / 2 else 0; + for (0..grid.rows) |cy| for (0..grid.cols) |cx| { + const cell = &grid.cells[cy * grid.cols + cx]; + const sx = place.dst.x + offx + cx; + const sy = place.dst.y + offy + cy; + if (sx >= drawn.cols or sy >= drawn.rows) continue; + drawn.set(@intCast(sx), @intCast(sy), cell.glyph[0..cell.glyph_len], switch (place.art.palette) { + .commodore => .{ .fg = .{ .rgb = commodore[cell.fg] }, .bg = .{ .rgb = commodore[cell.bg] } }, + .terminal => .{ .fg = .{ .index = cell.fg }, .bg = .{ .index = cell.bg } }, + }); + }; + } + return out orelse s; +} + +/// The placement's art, made the first frame it is drawn. +fn gridOf(art: *Art, gpa: std.mem.Allocator, place: Placement, cell_h: u32) ?Grid { + if (place.dst.w == 0 or place.dst.h == 0) return null; + if (place.at) |at| if (at.h * 2 < @as(u32, place.dst.h) * cell_h) return null; + const key: Key = .{ .key = place.key, .src = place.src, .cols = place.dst.w, .rows = place.dst.h, .fitted = place.at == null, .palette = place.art.palette, .ascii = place.art.ascii }; + if (art.grids.getPtr(key)) |hit| { + hit.used = true; + return hit.grid; + } + const colours = switch (place.art.palette) { + .commodore => commodore, + .terminal => terminal_palette, + }; + const grid = (if (place.at == null) + fitted(gpa, place.raster, place.src, place.dst.w, place.dst.h, colours, place.art.ascii) + else + renderRect(gpa, place.raster, place.src, place.dst.w, place.dst.h, colours, place.art.ascii)) catch return null; + if (grid.cells.len == 0) { + gpa.free(grid.cells); + return null; + } + art.grids.put(gpa, key, .{ .grid = grid }) catch { + gpa.free(grid.cells); + return null; + }; + return grid; +} + +/// Drop what this frame did not draw. +fn prune(art: *Art, gpa: std.mem.Allocator) void { + while (true) { + var it = art.grids.iterator(); + const stale = while (it.next()) |entry| { + if (!entry.value_ptr.used) break entry.key_ptr.*; + } else break; + gpa.free(art.grids.get(stale).?.grid.cells); + _ = art.grids.remove(stale); + } +} + +pub const commodore = [16][3]u8{ + .{ 0x00, 0x00, 0x00 }, // 0 black + .{ 0xff, 0xff, 0xff }, // 1 white + .{ 0x68, 0x37, 0x2b }, // 2 red + .{ 0x70, 0xa4, 0xb2 }, // 3 cyan + .{ 0x6f, 0x3d, 0x86 }, // 4 purple + .{ 0x58, 0x8d, 0x43 }, // 5 green + .{ 0x35, 0x28, 0x79 }, // 6 blue + .{ 0xb8, 0xc7, 0x6f }, // 7 yellow + .{ 0x6f, 0x4f, 0x25 }, // 8 orange + .{ 0x43, 0x39, 0x00 }, // 9 brown + .{ 0x9a, 0x67, 0x59 }, // 10 light red + .{ 0x44, 0x44, 0x44 }, // 11 dark grey + .{ 0x6c, 0x6c, 0x6c }, // 12 grey + .{ 0x9a, 0xd2, 0x84 }, // 13 light green + .{ 0x6c, 0x5e, 0xb5 }, // 14 light blue + .{ 0x95, 0x95, 0x95 }, // 15 light grey +}; + +const Colours = [16][3]u8; + +pub const Cell = struct { + glyph: [4]u8 = .{ ' ', 0, 0, 0 }, + glyph_len: u3 = 1, + fg: u4 = 1, + bg: u4 = 0, +}; + +pub const Grid = struct { cells: []Cell, cols: usize, rows: usize }; + +fn dist2(a: [3]u8, b: [3]u8) u32 { + const dr = @as(i32, a[0]) - b[0]; + const dg = @as(i32, a[1]) - b[1]; + const db = @as(i32, a[2]) - b[2]; + return @intCast(dr * dr + dg * dg + db * db); +} + +fn nearest(px: [3]u8, pal: Colours) u4 { + var best: u4 = 0; + var bestd: u32 = std.math.maxInt(u32); + for (pal, 0..) |c, i| { + const d = dist2(px, c); + if (d < bestd) { + bestd = d; + best = @intCast(i); + } + } + return best; +} + +// ---- glyph set: each is a codepoint + an 8x8 ink bitmap (bit y*8+x set = fg) ---- +const Glyph = struct { cp: u21, bits: u64 }; + +fn sextantCp(p: u6) u21 { + return switch (p) { + 0 => ' ', + 21 => 0x258C, // left half ▌ + 42 => 0x2590, // right half ▐ + 63 => 0x2588, // full block █ + else => blk: { + var off: u21 = @as(u21, p) - 1; + if (p > 21) off -= 1; + if (p > 42) off -= 1; + break :blk 0x1FB00 + off; // Symbols for Legacy Computing sextants + }, + }; +} + +// the 8x8 ink bitmap for a sextant pattern (2 cols x 3 rows of subcells). +fn sextantBits(p: u6) u64 { + var bits: u64 = 0; + var y: usize = 0; + while (y < 8) : (y += 1) { + const band = (y * 3) / 8; // 0,0,0,1,1,1,2,2 + var x: usize = 0; + while (x < 8) : (x += 1) { + const col: usize = if (x < 4) 0 else 1; + const sub: u6 = @intCast(band * 2 + col); + if ((p >> sub) & 1 != 0) bits |= @as(u64, 1) << @intCast(y * 8 + x); + } + } + return bits; +} + +// pack 8 row-bytes (bit x set, x=0 leftmost) into the 8x8 bitmap. +fn glyphMask(r: [8]u8) u64 { + var b: u64 = 0; + for (r, 0..) |row, y| b |= @as(u64, row) << @intCast(y * 8); + return b; +} + +// the horizontal half blocks and the ten 2x2 quadrants — sextants are 2x3, so they +// can't express an exact 4-row half or a quarter; these fill that gap. +const block_glyphs = [_]Glyph{ + .{ .cp = 0x2580, .bits = glyphMask(.{ 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0 }) }, // ▀ top half + .{ .cp = 0x2584, .bits = glyphMask(.{ 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff }) }, // ▄ bottom half + .{ .cp = 0x2598, .bits = glyphMask(.{ 0x0f, 0x0f, 0x0f, 0x0f, 0, 0, 0, 0 }) }, // ▘ TL + .{ .cp = 0x259d, .bits = glyphMask(.{ 0xf0, 0xf0, 0xf0, 0xf0, 0, 0, 0, 0 }) }, // ▝ TR + .{ .cp = 0x2596, .bits = glyphMask(.{ 0, 0, 0, 0, 0x0f, 0x0f, 0x0f, 0x0f }) }, // ▖ BL + .{ .cp = 0x2597, .bits = glyphMask(.{ 0, 0, 0, 0, 0xf0, 0xf0, 0xf0, 0xf0 }) }, // ▗ BR + .{ .cp = 0x259a, .bits = glyphMask(.{ 0x0f, 0x0f, 0x0f, 0x0f, 0xf0, 0xf0, 0xf0, 0xf0 }) }, // ▚ TL+BR + .{ .cp = 0x259e, .bits = glyphMask(.{ 0xf0, 0xf0, 0xf0, 0xf0, 0x0f, 0x0f, 0x0f, 0x0f }) }, // ▞ TR+BL + .{ .cp = 0x259b, .bits = glyphMask(.{ 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f }) }, // ▛ ¬BR + .{ .cp = 0x259c, .bits = glyphMask(.{ 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0 }) }, // ▜ ¬BL + .{ .cp = 0x2599, .bits = glyphMask(.{ 0x0f, 0x0f, 0x0f, 0x0f, 0xff, 0xff, 0xff, 0xff }) }, // ▙ ¬TR + .{ .cp = 0x259f, .bits = glyphMask(.{ 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff }) }, // ▟ ¬TL +}; + +// line/diagonal glyphs add the characteristic PETSCII "drawn" look on edges. +const line_glyphs = [_]Glyph{ + .{ .cp = 0x2500, .bits = glyphMask(.{ 0, 0, 0, 0xff, 0xff, 0, 0, 0 }) }, // ─ + .{ .cp = 0x2502, .bits = glyphMask(.{ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }) }, // │ + .{ .cp = 0x253c, .bits = glyphMask(.{ 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18 }) }, // ┼ + .{ .cp = 0x2572, .bits = glyphMask(.{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }) }, // ╲ + .{ .cp = 0x2571, .bits = glyphMask(.{ 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }) }, // ╱ + .{ .cp = 0x2573, .bits = glyphMask(.{ 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }) }, // ╳ +}; + +const ascii_glyphs = [_]Glyph{ + .{ .cp = 0x21, .bits = 0x00180018183c3c18 }, // ! + .{ .cp = 0x22, .bits = 0x0000000000246666 }, // " + .{ .cp = 0x23, .bits = 0x0036367f367f3636 }, // # + .{ .cp = 0x24, .bits = 0x00183e603c067c18 }, // $ + .{ .cp = 0x25, .bits = 0x0063660c18336300 }, // % + .{ .cp = 0x26, .bits = 0x006e333b6e1c361c }, // & + .{ .cp = 0x27, .bits = 0x00000000000c1818 }, // ' + .{ .cp = 0x28, .bits = 0x0030180c0c0c1830 }, // ( + .{ .cp = 0x29, .bits = 0x000c18303030180c }, // ) + .{ .cp = 0x2a, .bits = 0x0000663cff3c6600 }, // * + .{ .cp = 0x2b, .bits = 0x000018187e181800 }, // + + .{ .cp = 0x2c, .bits = 0x0c18180000000000 }, // , + .{ .cp = 0x2d, .bits = 0x000000007e000000 }, // - + .{ .cp = 0x2e, .bits = 0x0018180000000000 }, // . + .{ .cp = 0x2f, .bits = 0x000103060c183060 }, // / + .{ .cp = 0x30, .bits = 0x001c36636b63361c }, // 0 + .{ .cp = 0x31, .bits = 0x007e181818181c18 }, // 1 + .{ .cp = 0x32, .bits = 0x007f660c3860633e }, // 2 + .{ .cp = 0x33, .bits = 0x003e63603c60633e }, // 3 + .{ .cp = 0x34, .bits = 0x0078307f33363c38 }, // 4 + .{ .cp = 0x35, .bits = 0x003e63603f03037f }, // 5 + .{ .cp = 0x36, .bits = 0x003e63633f03061c }, // 6 + .{ .cp = 0x37, .bits = 0x000c0c0c1830637f }, // 7 + .{ .cp = 0x38, .bits = 0x003e63633e63633e }, // 8 + .{ .cp = 0x39, .bits = 0x001e30607e63633e }, // 9 + .{ .cp = 0x3a, .bits = 0x0018180000181800 }, // : + .{ .cp = 0x3b, .bits = 0x0c18180000181800 }, // ; + .{ .cp = 0x3c, .bits = 0x006030180c183060 }, // < + .{ .cp = 0x3d, .bits = 0x00007e00007e0000 }, // = + .{ .cp = 0x3e, .bits = 0x00060c1830180c06 }, // > + .{ .cp = 0x3f, .bits = 0x001800181830633e }, // ? + .{ .cp = 0x40, .bits = 0x001e037b7b7b633e }, // @ + .{ .cp = 0x41, .bits = 0x006363637f63361c }, // A + .{ .cp = 0x42, .bits = 0x003f66663e66663f }, // B + .{ .cp = 0x43, .bits = 0x003c66030303663c }, // C + .{ .cp = 0x44, .bits = 0x001f36666666361f }, // D + .{ .cp = 0x45, .bits = 0x007f46161e16467f }, // E + .{ .cp = 0x46, .bits = 0x000f06161e16467f }, // F + .{ .cp = 0x47, .bits = 0x005c66730303663c }, // G + .{ .cp = 0x48, .bits = 0x006363637f636363 }, // H + .{ .cp = 0x49, .bits = 0x003c18181818183c }, // I + .{ .cp = 0x4a, .bits = 0x001e333330303078 }, // J + .{ .cp = 0x4b, .bits = 0x006766361e366667 }, // K + .{ .cp = 0x4c, .bits = 0x007f66460606060f }, // L + .{ .cp = 0x4d, .bits = 0x0063636b7f7f7763 }, // M + .{ .cp = 0x4e, .bits = 0x006363737b6f6763 }, // N + .{ .cp = 0x4f, .bits = 0x003e63636363633e }, // O + .{ .cp = 0x50, .bits = 0x000f06063e66663f }, // P + .{ .cp = 0x51, .bits = 0x703e73636363633e }, // Q + .{ .cp = 0x52, .bits = 0x006766363e66663f }, // R + .{ .cp = 0x53, .bits = 0x003c6630180c663c }, // S + .{ .cp = 0x54, .bits = 0x003c1818185a7e7e }, // T + .{ .cp = 0x55, .bits = 0x003e636363636363 }, // U + .{ .cp = 0x56, .bits = 0x001c366363636363 }, // V + .{ .cp = 0x57, .bits = 0x00367f6b6b636363 }, // W + .{ .cp = 0x58, .bits = 0x006363361c366363 }, // X + .{ .cp = 0x59, .bits = 0x003c18183c666666 }, // Y + .{ .cp = 0x5a, .bits = 0x007f664c1831637f }, // Z + .{ .cp = 0x5b, .bits = 0x003c0c0c0c0c0c3c }, // [ + .{ .cp = 0x5c, .bits = 0x00406030180c0603 }, // \\ + .{ .cp = 0x5d, .bits = 0x003c30303030303c }, // ] + .{ .cp = 0x5e, .bits = 0x0000000063361c08 }, // ^ + .{ .cp = 0x5f, .bits = 0xff00000000000000 }, // _ + .{ .cp = 0x60, .bits = 0x000000000030180c }, // ` + .{ .cp = 0x61, .bits = 0x006e333e301e0000 }, // a + .{ .cp = 0x62, .bits = 0x003b6666663e0607 }, // b + .{ .cp = 0x63, .bits = 0x003e6303633e0000 }, // c + .{ .cp = 0x64, .bits = 0x006e3333333e3038 }, // d + .{ .cp = 0x65, .bits = 0x003e037f633e0000 }, // e + .{ .cp = 0x66, .bits = 0x000f06061f06663c }, // f + .{ .cp = 0x67, .bits = 0x1f303e33336e0000 }, // g + .{ .cp = 0x68, .bits = 0x006766666e360607 }, // h + .{ .cp = 0x69, .bits = 0x003c1818181c0018 }, // i + .{ .cp = 0x6a, .bits = 0x3c66666060600060 }, // j + .{ .cp = 0x6b, .bits = 0x0067361e36660607 }, // k + .{ .cp = 0x6c, .bits = 0x003c18181818181c }, // l + .{ .cp = 0x6d, .bits = 0x006b6b6b7f370000 }, // m + .{ .cp = 0x6e, .bits = 0x00666666663b0000 }, // n + .{ .cp = 0x6f, .bits = 0x003e6363633e0000 }, // o + .{ .cp = 0x70, .bits = 0x0f063e66663b0000 }, // p + .{ .cp = 0x71, .bits = 0x78303e33336e0000 }, // q + .{ .cp = 0x72, .bits = 0x000f06066e3b0000 }, // r + .{ .cp = 0x73, .bits = 0x003f603e037e0000 }, // s + .{ .cp = 0x74, .bits = 0x00386c0c0c3f0c0c }, // t + .{ .cp = 0x75, .bits = 0x006e333333330000 }, // u + .{ .cp = 0x76, .bits = 0x001c366363630000 }, // v + .{ .cp = 0x77, .bits = 0x00367f6b6b630000 }, // w + .{ .cp = 0x78, .bits = 0x0063361c36630000 }, // x + .{ .cp = 0x79, .bits = 0x3f607e6363630000 }, // y + .{ .cp = 0x7a, .bits = 0x007e4c18327e0000 }, // z + .{ .cp = 0x7b, .bits = 0x007018180e181870 }, // { + .{ .cp = 0x7c, .bits = 0x0018181818181818 }, // | + .{ .cp = 0x7d, .bits = 0x000e18187018180e }, // } + .{ .cp = 0x7e, .bits = 0x0000000000003b6e }, // ~ +}; + +// the block glyph table (sextants + half/quadrant blocks + line glyphs), built at +// comptime, and the same set extended with the ASCII glyphs. `render(ascii=…)` picks. +const glyphs = blk: { + @setEvalBranchQuota(100000); + var list: [64 + block_glyphs.len + line_glyphs.len]Glyph = undefined; + var p: usize = 0; + while (p < 64) : (p += 1) list[p] = .{ .cp = sextantCp(@intCast(p)), .bits = sextantBits(@intCast(p)) }; + for (block_glyphs, 0..) |bg, i| list[64 + i] = bg; + for (line_glyphs, 0..) |lg, i| list[64 + block_glyphs.len + i] = lg; + break :blk list; +}; +const glyphs_ascii = glyphs ++ ascii_glyphs; + +fn matchCell(cell: *const [64][3]u8, pal: Colours, gset: []const Glyph) Cell { + var counts = @as([16]u16, @splat(0)); + for (cell) |px| counts[nearest(px, pal)] += 1; + var cand: [4]u4 = undefined; + var ncand: usize = 0; + var used = @as([16]bool, @splat(false)); + while (ncand < 4) : (ncand += 1) { + var best: ?usize = null; + for (counts, 0..) |c, i| { + if (used[i] or c == 0) continue; + if (best == null or c > counts[best.?]) best = i; + } + if (best) |bi| { + cand[ncand] = @intCast(bi); + used[bi] = true; + } else break; + } + if (ncand == 0) return .{}; // can't happen (64 pixels), but keep it total + if (ncand == 1) return encode(' ', cand[0], cand[0]); // solid color + + var best_cost: i64 = std.math.maxInt(i64); + var best = encode(' ', cand[0], cand[0]); + var fi: usize = 0; + while (fi < ncand) : (fi += 1) { + var bi: usize = 0; + while (bi < ncand) : (bi += 1) { + if (fi == bi) continue; + const fg = cand[fi]; + const bg = cand[bi]; + var dfg: [64]u32 = undefined; + var dbg: [64]u32 = undefined; + var base: i64 = 0; + for (cell, 0..) |px, p| { + dfg[p] = dist2(px, pal[fg]); + dbg[p] = dist2(px, pal[bg]); + base += dbg[p]; + } + for (gset) |g| { + var delta: i64 = 0; + var bits = g.bits; + while (bits != 0) : (bits &= bits - 1) { + const p: usize = @ctz(bits); + delta += @as(i64, dfg[p]) - @as(i64, dbg[p]); + } + const cost = base + delta; + if (cost < best_cost) { + best_cost = cost; + best = encode(g.cp, fg, bg); + } + } + } + } + return best; +} + +fn encode(cp: u21, fg: u4, bg: u4) Cell { + var c = Cell{ .fg = fg, .bg = bg }; + const n = std.unicode.utf8Encode(cp, &c.glyph) catch 1; + c.glyph_len = @intCast(n); + return c; +} + +/// `src` of `raster` fitted into `max_cols` x `max_rows` cells, its shape +/// kept: cells are about twice as tall as wide, so a row spans two widths. +fn fitted(gpa: std.mem.Allocator, raster: Raster, src: RectPx, max_cols: usize, max_rows: usize, pal: Colours, ascii: bool) !Grid { + if (src.w == 0 or src.h == 0 or max_cols == 0 or max_rows == 0) return .{ .cells = try gpa.alloc(Cell, 0), .cols = 0, .rows = 0 }; + var cols = max_cols; + var rows = (max_cols * src.h) / (2 * src.w); + if (rows > max_rows) { + rows = max_rows; + cols = (max_rows * 2 * src.w) / src.h; + } + cols = std.math.clamp(cols, 1, max_cols); + rows = std.math.clamp(rows, 1, max_rows); + return renderRect(gpa, raster, src, cols, rows, pal, ascii); +} + +/// The pixels of `src` laid over exactly `cols` x `rows` cells, whatever +/// their shape: a terminal's image fragment is already the size its cells +/// are. +fn renderRect(gpa: std.mem.Allocator, raster: Raster, src: RectPx, cols: usize, rows: usize, pal: Colours, ascii: bool) !Grid { + const iw: usize = raster.w; + const ih: usize = raster.h; + const rgba = raster.bytes(); + const x0: usize = @min(src.x, iw); + const y0: usize = @min(src.y, ih); + const sw: usize = @min(src.w, iw - x0); + const sh: usize = @min(src.h, ih - y0); + if (sw == 0 or sh == 0 or cols == 0 or rows == 0) return .{ .cells = try gpa.alloc(Cell, 0), .cols = 0, .rows = 0 }; + const gset: []const Glyph = if (ascii) &glyphs_ascii else &glyphs; + const cells = try gpa.alloc(Cell, cols * rows); + var cy: usize = 0; + while (cy < rows) : (cy += 1) { + const ry0 = y0 + cy * sh / rows; + const ry1 = @max(ry0 + 1, y0 + (cy + 1) * sh / rows); + var cx: usize = 0; + while (cx < cols) : (cx += 1) { + const rx0 = x0 + cx * sw / cols; + const rx1 = @max(rx0 + 1, x0 + (cx + 1) * sw / cols); + var cell: [64][3]u8 = undefined; + var sy: usize = 0; + while (sy < 8) : (sy += 1) { + const py0 = ry0 + sy * (ry1 - ry0) / 8; + const py1 = @max(py0 + 1, ry0 + (sy + 1) * (ry1 - ry0) / 8); + var sx: usize = 0; + while (sx < 8) : (sx += 1) { + const px0 = rx0 + sx * (rx1 - rx0) / 8; + const px1 = @max(px0 + 1, rx0 + (sx + 1) * (rx1 - rx0) / 8); + var rs: usize = 0; + var gs: usize = 0; + var bs: usize = 0; + var n: usize = 0; + var yy = py0; + while (yy < py1 and yy < ih) : (yy += 1) { + var xx = px0; + while (xx < px1 and xx < iw) : (xx += 1) { + const i = yy * raster.stride + xx * 4; + rs += rgba[i]; + gs += rgba[i + 1]; + bs += rgba[i + 2]; + n += 1; + } + } + if (n == 0) n = 1; + cell[sy * 8 + sx] = .{ @intCast(rs / n), @intCast(gs / n), @intCast(bs / n) }; + } + } + cells[cy * cols + cx] = matchCell(&cell, pal, gset); + } + } + return .{ .cells = cells, .cols = cols, .rows = rows }; +} + +test "sextant codepoints: blocks + endpoints" { + try std.testing.expectEqual(@as(u21, ' '), sextantCp(0)); + try std.testing.expectEqual(@as(u21, 0x2588), sextantCp(63)); + try std.testing.expectEqual(@as(u21, 0x258C), sextantCp(21)); + try std.testing.expectEqual(@as(u21, 0x2590), sextantCp(42)); + try std.testing.expectEqual(@as(u21, 0x1FB00), sextantCp(1)); // first sextant + try std.testing.expectEqual(@as(u21, 0x1FB3B), sextantCp(62)); // last sextant +} + +test "matchCell: solid color -> space on that bg" { + var cell: [64][3]u8 = undefined; + for (&cell) |*p| p.* = commodore[5]; // all green + const m = matchCell(&cell, commodore, &glyphs); + try std.testing.expectEqual(@as(u4, 5), m.bg); + try std.testing.expectEqual(@as(u8, ' '), m.glyph[0]); +} + +test "matchCell: clean top/bottom split picks the two colors" { + var cell: [64][3]u8 = undefined; + for (0..64) |p| cell[p] = if (p < 32) commodore[1] else commodore[6]; // white over blue + const m = matchCell(&cell, commodore, &glyphs); + // both palette colors must be chosen (in some fg/bg order) + const a = @as(u4, @min(m.fg, m.bg)); + const b = @as(u4, @max(m.fg, m.bg)); + try std.testing.expectEqual(@as(u4, 1), a); + try std.testing.expectEqual(@as(u4, 6), b); + // a clean top/bottom split must resolve to a real block glyph (the top-4-rows + // half block ▀), never a blank cell. + const cp = std.unicode.utf8Decode(m.glyph[0..m.glyph_len]) catch 0; + try std.testing.expectEqual(@as(u21, 0x2580), cp); +} + +test "ascii option only enables the ascii glyphs" { + // the ascii glyph codepoints must be reachable exactly when ascii is on. + var seen_block = false; + var seen_ascii = false; + for (glyphs) |g| if (g.cp == '#') { + seen_block = true; + }; + for (glyphs_ascii) |g| if (g.cp == '#') { + seen_ascii = true; + }; + try std.testing.expect(!seen_block); // '#' is an ascii-only glyph + try std.testing.expect(seen_ascii); + try std.testing.expectEqual(glyphs.len + ascii_glyphs.len, glyphs_ascii.len); +} + +test "ascii glyph is chosen when a cell has its exact shape" { + // a cell shaped exactly like the font 'S' (ink=white on black) must match 'S' + // with ascii on (cost 0), and fall back to some block glyph with ascii off. + const s_bits: u64 = 0x003c6630180c663c; + var cell: [64][3]u8 = undefined; + for (0..64) |p| cell[p] = if ((s_bits >> @intCast(p)) & 1 != 0) commodore[1] else commodore[0]; + const on = matchCell(&cell, commodore, &glyphs_ascii); + try std.testing.expectEqual(@as(u21, 'S'), std.unicode.utf8Decode(on.glyph[0..on.glyph_len]) catch 0); + const off = matchCell(&cell, commodore, &glyphs); + try std.testing.expect((std.unicode.utf8Decode(off.glyph[0..off.glyph_len]) catch 0) != 'S'); +} + +test "a tiny image fitted into cells makes a grid within bounds" { + const a = std.testing.allocator; + // 2x2 checker, RGBA + var img = @as([2 * 2 * 4]u8, @splat(0)); + img[0] = 255; + img[1] = 255; + img[2] = 255; + img[3] = 255; // (0,0) white + img[(3) * 4 + 0] = 255; + img[(3) * 4 + 1] = 255; + img[(3) * 4 + 2] = 255; + img[(3) * 4 + 3] = 255; // (1,1) white + const g = try fitted(a, .of(&img, 2, 2), .{ .w = 2, .h = 2 }, 10, 10, commodore, true); + defer a.free(g.cells); + try std.testing.expect(g.cols >= 1 and g.cols <= 10); + try std.testing.expect(g.rows >= 1 and g.rows <= 10); + try std.testing.expectEqual(g.cols * g.rows, g.cells.len); +} +pub const terminal_palette = [16][3]u8{ + .{ 0x1D, 0x1F, 0x21 }, // black + .{ 0xCC, 0x66, 0x66 }, // red + .{ 0xB5, 0xBD, 0x68 }, // green + .{ 0xF0, 0xC6, 0x74 }, // yellow + .{ 0x81, 0xA2, 0xBE }, // blue + .{ 0xB2, 0x94, 0xBB }, // magenta + .{ 0x8A, 0xBE, 0xB7 }, // cyan + .{ 0xC5, 0xC8, 0xC6 }, // white + .{ 0x66, 0x66, 0x66 }, // bright black + .{ 0xD5, 0x4E, 0x53 }, // bright red + .{ 0xB9, 0xCA, 0x4A }, // bright green + .{ 0xE7, 0xC5, 0x47 }, // bright yellow + .{ 0x7A, 0xA6, 0xDA }, // bright blue + .{ 0xC3, 0x97, 0xD8 }, // bright magenta + .{ 0x70, 0xC0, 0xB1 }, // bright cyan + .{ 0xEA, 0xEA, 0xEA }, // bright white +}; + +comptime { + for (terminal_palette, 0..) |rgb, i| { + const c = ghostty_vt.color.default[i]; + if (rgb[0] != c.r or rgb[1] != c.g or rgb[2] != c.b) @compileError( + "terminal palette differs from the emulator", + ); + } +} + +test "a frontend draws a placement as glyph art where it shows no pixels, or where its pane asks, and else leaves the frame be" { + const gpa = std.testing.allocator; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var art: Art = .{}; + defer art.deinit(gpa); + // 8x8 pixels, white over blue, over a 20x6 cell body at (2, 1). + var px: [8 * 8 * 4]u8 = undefined; + for (0..64) |i| px[i * 4 ..][0..4].* = if (i < 32) .{ 255, 255, 255, 255 } else .{ 0x35, 0x28, 0x79, 255 }; + var cells: [30 * 8]pardes.Cell = @splat(.{}); + var s: Surface = .{ .cols = 30, .rows = 8, .cells = &cells }; + defer s.placements.deinit(gpa); + try s.placements.append(gpa, .{ .pane = 0, .raster = .of(&px, 8, 8), .src = .{ .w = 8, .h = 8 }, .dst = .{ .x = 2, .y = 1, .w = 20, .h = 6 }, .key = Placement.keyOf(1, 0, 0) }); + + try std.testing.expectEqual(@as(*const Surface, &s), try art.frame(gpa, arena.allocator(), &s, true, 16)); + const drawn = try art.frame(gpa, arena.allocator(), &s, false, 16); + try std.testing.expect(drawn != &s); + try std.testing.expect(s.at(2, 1).default); + // Fitted at its shape (a square is half as many rows as columns) and + // centred: 12x6 cells from column 6. + try std.testing.expect(@constCast(drawn).at(5, 1).default); + try std.testing.expectEqual(pardes.Color{ .rgb = commodore[1] }, @constCast(drawn).at(6, 1).style.bg); + try std.testing.expect(!@constCast(drawn).at(17, 6).default); + try std.testing.expect(@constCast(drawn).at(18, 6).default); + try std.testing.expectEqual(@as(u32, 1), art.grids.count()); + + // The pane asks for art in the terminal's sixteen: indices, not rgb. + s.placements.items[0].art = .{ .glyph = true, .palette = .terminal }; + const asked = try art.frame(gpa, arena.allocator(), &s, true, 16); + try std.testing.expect(@constCast(asked).at(6, 1).style.bg == .index); + // What a frame did not draw is let go. + try std.testing.expectEqual(@as(u32, 1), art.grids.count()); + s.placements.clearRetainingCapacity(); + try std.testing.expectEqual(@as(*const Surface, &s), try art.frame(gpa, arena.allocator(), &s, false, 16)); + try std.testing.expectEqual(@as(u32, 0), art.grids.count()); + + // Placed exactly (a terminal's image row), it fills its cells, but not + // a sliver of a row. + try s.placements.append(gpa, .{ .pane = 0, .raster = .of(&px, 8, 8), .src = .{ .w = 8, .h = 4 }, .dst = .{ .x = 0, .y = 7, .w = 4, .h = 1 }, .at = .{ .w = 40, .h = 16 }, .key = Placement.keyOf(1, 2, 0) }); + const row = try art.frame(gpa, arena.allocator(), &s, false, 16); + for (0..4) |x| try std.testing.expect(!@constCast(row).at(@intCast(x), 7).default); + try std.testing.expect(@constCast(row).at(4, 7).default); + s.placements.items[0].at.?.h = 7; + try std.testing.expectEqual(@as(*const Surface, &s), try art.frame(gpa, arena.allocator(), &s, false, 16)); +} diff --git a/src/Core.zig b/src/Core.zig index 0efe447b..eb3f0562 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -21,7 +21,7 @@ const FontRole = pardes.FontRole; const FrameStats = pardes.FrameStats; const Handover = pardes.Handover; const Host = pardes.Host; -const ImagePlace = pardes.ImagePlace; +const Placement = pardes.Placement; const Jump = pardes.Jump; const Frontend = @import("Event.zig").Frontend; const Ask = pardes.lsp.Ask; @@ -776,7 +776,7 @@ pub fn deinit(p: *Pardes) void { gpa.free(p.surface.cells); for (&p.surface.body_layers) |*layer| gpa.free(layer.cells); for (&p.surface.tag_layers) |*layer| gpa.free(layer.cells); - p.surface.images.deinit(gpa); + p.surface.placements.deinit(gpa); gpa.destroy(p); } @@ -2049,8 +2049,8 @@ pub fn takeHaptic(p: *Pardes) Haptic { pub const render = draw.render; -pub fn appendImagePlace(p: *Pardes, place: ImagePlace) bool { - p.surface.images.append(p.gpa, place) catch return false; +pub fn appendPlacement(p: *Pardes, place: Placement) bool { + p.surface.placements.append(p.gpa, place) catch return false; return true; } diff --git a/src/TerminalImages.zig b/src/TerminalImages.zig index 0131568d..86c20d2c 100644 --- a/src/TerminalImages.zig +++ b/src/TerminalImages.zig @@ -2,8 +2,8 @@ //! previews, `kitten icat`) drawn where it placed them. ghostty-vt keeps //! the images and placements; this reads them each frame, cuts each one //! into the cell rows it covers, clipped to the rows the pane shows, and -//! hands them on: as pixels (`ImagePlace`, exact geometry) to a shell that -//! draws them, else as glyph art in the cells, as an image pane's Petscii. +//! hands them on as placements, each at its exact pixels: a frontend draws +//! them, or draws them as glyph art (Art.zig), as an image pane's. //! Both kinds of placement are drawn: one on a cell (`a=T`/`a=p`), which //! moves with the text it was put on, and a virtual one (`U=1`) shown //! through the U+10EEEE placeholder cells the program prints. @@ -14,28 +14,23 @@ const image = @import("image.zig"); const TerminalImages = @This(); const gfx = ghostty_vt.kitty.graphics; -const GlyphArt = image.GlyphArt; const PixelRect = image.PixelRect; /// The bytes ghostty-vt may hold for one screen's images (it evicts the /// oldest past this): a 4K picture in RGBA, with room for a second. pub const storage_limit = 64 * 1024 * 1024; -/// This pane draws its images as glyph art even where pixels could be +/// This pane asks for its images as glyph art even where pixels could be /// drawn (the Petscii builtin; TermImages sets it for a new pane). petscii: bool = false, /// Pixels in RGBA for images ghostty-vt holds in another format (RGB, /// grey): only for images on screen, dropped the frame they are not. converted: std.AutoHashMapUnmanaged(u32, Converted) = .empty, -/// Glyph art for the fragments drawn as cells, kept while on screen. -glyphs: std.AutoHashMapUnmanaged(GlyphKey, Glyphs) = .empty, /// When the next frame of a playing animation (a=a) is due, in ms on the /// core's clock (`Pardes.stepped_ns`): the core wakes for it (nextWake). animation_due_ms: ?u64 = null, const Converted = struct { generation: u64, rgba: []u8, used: bool = true }; -const Glyphs = struct { cells: []GlyphArt.Cell, used: bool = true }; -const GlyphKey = struct { id: u32, generation: u64, src: PixelRect, cols: u16 }; /// One image's rectangle in the pane, in pixels from the viewport's top /// left corner (above or left of it is negative). @@ -111,9 +106,6 @@ pub fn deinit(ti: *TerminalImages, gpa: std.mem.Allocator) void { var conv = ti.converted.valueIterator(); while (conv.next()) |c| gpa.free(c.rgba); ti.converted.deinit(gpa); - var gl = ti.glyphs.valueIterator(); - while (gl.next()) |g| gpa.free(g.cells); - ti.glyphs.deinit(gpa); } /// Accept PNG (`f=100`) as well as raw pixels: ghostty-vt built as a @@ -144,8 +136,6 @@ pub fn draw( ) void { var conv = ti.converted.valueIterator(); while (conv.next()) |c| c.used = false; - var gl = ti.glyphs.valueIterator(); - while (gl.next()) |g| g.used = false; defer ti.prune(p.gpa); const screen = vt.screens.active; @@ -160,13 +150,12 @@ pub fn draw( const bottom = pages.getBottomRight(.viewport) orelse return; const cw: u32 = @max(1, p.cell_pixels.w); const ch: u32 = @max(1, p.cell_pixels.h); - const pixels = p.frontend.native_images and !ti.petscii; - const ctx: Ctx = .{ .ti = ti, .p = p, .s = s, .pane = pane_id, .serial = serial, .tx = tx, .body_y = body_y, .cols = cols, .rows_of = rows_of, .cw = cw, .ch = ch, .pixels = pixels }; + const ctx: Ctx = .{ .ti = ti, .p = p, .pane = pane_id, .serial = serial, .tx = tx, .body_y = body_y, .cols = cols, .rows_of = rows_of, .cw = cw, .ch = ch }; // Handed on lowest z first (the storage's order is a hash map's), the // order a shell draws them in; equal z keeps the order they came in. - const first = s.images.items.len; - defer std.sort.insertion(pardes.ImagePlace, s.images.items[first..], {}, struct { - fn lessThan(_: void, a: pardes.ImagePlace, b: pardes.ImagePlace) bool { + const first = s.placements.items.len; + defer std.sort.insertion(pardes.Placement, s.placements.items[first..], {}, struct { + fn lessThan(_: void, a: pardes.Placement, b: pardes.Placement) bool { return a.z < b.z; } }.lessThan); @@ -243,7 +232,6 @@ pub fn draw( const Ctx = struct { ti: *TerminalImages, p: *pardes.Pardes, - s: *pardes.Surface, pane: u8, serial: u32, tx: u16, @@ -252,7 +240,6 @@ const Ctx = struct { rows_of: []const ?u16, cw: u32, ch: u32, - pixels: bool, /// Draw `src` of `img` over `dst`, one cell row of it at a time: a row /// is shown, or not, or shown elsewhere, as the body lays it out. @@ -280,38 +267,20 @@ const Ctx = struct { }; const c0: u16 = @intCast(@divFloor(x0, cw)); const c1: u16 = @intCast(@divFloor(x1 + cw - 1, cw)); - if (ctx.pixels) { - _ = ctx.p.appendImagePlace(.{ - .pane = ctx.pane, - .serial = ctx.serial, - .native = .{ - .page = img.id, - .revision = @truncate(img.generation), - .exact = true, - .geometry = .{ .src = piece, .dst = .{ - .x = @intCast(x0 - @as(i64, c0) * cw), - .y = @intCast(y0 - v * ch), - .w = @intCast(x1 - x0), - .h = @intCast(y1 - y0), - } }, - }, - .x = ctx.tx + c0, - .y = ctx.body_y + row, - .w = c1 - c0, - .h = 1, - .rgba = rgba, - .iw = img.width, - .ih = img.height, - .z = z, - }); - continue; - } - // Glyph art fills whole cells: a sliver of a row is left out. - if ((y1 - y0) * 2 < ch) continue; - const cells = ctx.ti.glyphsOf(ctx.p.gpa, img, rgba, piece, c1 - c0) orelse continue; - for (cells, c0..) |*cell, x| ctx.s.set(ctx.tx + @as(u16, @intCast(x)), ctx.body_y + row, cell.glyph[0..cell.glyph_len], .{ - .fg = .{ .rgb = GlyphArt.commodore[cell.fg] }, - .bg = .{ .rgb = GlyphArt.commodore[cell.bg] }, + _ = ctx.p.appendPlacement(.{ + .pane = ctx.pane, + .raster = .of(rgba, img.width, img.height), + .src = piece, + .dst = .{ .x = ctx.tx + c0, .y = ctx.body_y + row, .w = c1 - c0, .h = 1 }, + .at = .{ + .x = @intCast(x0 - @as(i64, c0) * cw), + .y = @intCast(y0 - v * ch), + .w = @intCast(x1 - x0), + .h = @intCast(y1 - y0), + }, + .key = pardes.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)), + .z = z, + .art = .{ .glyph = ctx.ti.petscii }, }); } } @@ -362,24 +331,6 @@ fn convert(gpa: std.mem.Allocator, img: gfx.Image, n: usize) ?[]u8 { return out; } -fn glyphsOf(ti: *TerminalImages, gpa: std.mem.Allocator, img: gfx.Image, rgba: []const u8, piece: PixelRect, cols: u16) ?[]const GlyphArt.Cell { - const key: GlyphKey = .{ .id = img.id, .generation = img.generation, .src = piece, .cols = cols }; - if (ti.glyphs.getPtr(key)) |hit| { - hit.used = true; - return hit.cells; - } - const grid = GlyphArt.renderRect(gpa, rgba, img.width, img.height, piece, cols, 1, GlyphArt.commodore, true) catch return null; - if (grid.cells.len != cols) { - gpa.free(grid.cells); - return null; - } - ti.glyphs.put(gpa, key, .{ .cells = grid.cells }) catch { - gpa.free(grid.cells); - return null; - }; - return grid.cells; -} - /// Drop what this frame did not draw: the caches hold only what is shown. fn prune(ti: *TerminalImages, gpa: std.mem.Allocator) void { while (true) { @@ -390,12 +341,4 @@ fn prune(ti: *TerminalImages, gpa: std.mem.Allocator) void { gpa.free(ti.converted.get(stale).?.rgba); _ = ti.converted.remove(stale); } - while (true) { - var gl = ti.glyphs.iterator(); - const stale = while (gl.next()) |entry| { - if (!entry.value_ptr.used) break entry.key_ptr.*; - } else break; - gpa.free(ti.glyphs.get(stale).?.cells); - _ = ti.glyphs.remove(stale); - } } diff --git a/src/draw.zig b/src/draw.zig index 830f10df..ac13fc7d 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -196,7 +196,7 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { if (p.drag == .move or p.drag == .column_move) s.pointer_shape = .box; for (&s.body_layers) |*layer| layer.rows = 0; for (&s.tag_layers) |*layer| layer.rows = 0; - s.images.clearRetainingCapacity(); + s.placements.clearRetainingCapacity(); const chrome = p.chromeTheme(); s.chrome = .{ .page = p.theme().bg, @@ -442,7 +442,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: // painted out. Text gets the overlay; a picture gives up the rows. const shown = pane.notices.span(); const image_h = (r.h -| pane.tag_rows) -| shown; - panes.Image.draw(p, s, iv, @intCast(id), pane.serial, tx, body_y + shown, tw, image_h); + panes.Image.draw(p, iv, @intCast(id), pane.serial, tx, body_y + shown, tw, image_h); // thumbless, but the same one column as the real scrollbar below — // that is the whole point of drawing it, and like that one it runs // past the notice bands so the gutter has no notch in it diff --git a/src/gui/gui.zig b/src/gui/gui.zig index d6efa27c..f9b757f8 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -15,6 +15,7 @@ const file_watch = @import("../file_watch.zig"); const deck = @import("deck.zig"); const pet = @import("pet.zig"); const JumpTrail = @import("../jump_trail.zig"); +const Art = @import("../Art.zig"); const fonts = @import("../fonts.zig"); // the fallback faces const selection_pipe = @import("../selection_pipe.zig"); const c_heap = @import("c_heap"); @@ -460,7 +461,7 @@ const ImageInstance = extern struct { const opaque_bg_bit: u32 = 0x2000_0000; const PreparedImage = struct { - place: pardes.ImagePlace, + place: pardes.Placement, texture: *c.SDL_GPUTexture, }; @@ -958,8 +959,13 @@ const Gui = struct { image_vbuf: ?*c.SDL_GPUBuffer = null, image_vxfer: ?*c.SDL_GPUTransferBuffer = null, image_capacity: u32 = 0, - native_images: std.AutoHashMapUnmanaged(pardes.ImageCacheKey, *c.SDL_GPUTexture) = .empty, + /// The textures of the placements shown, by key. + native_images: std.AutoHashMapUnmanaged(u64, *c.SDL_GPUTexture) = .empty, prepared_images: std.ArrayListUnmanaged(PreparedImage) = .empty, + /// The glyph art of the placements whose panes ask for it (Petscii + /// on), and the frame copy it is drawn into. + art: Art = .{}, + art_arena: ?std.heap.ArenaAllocator = null, /// renderFrame's cover of the grid (coverFrame). cover: std.ArrayListUnmanaged(Cover) = .empty, /// renderFrame's decor instances, group by group (buildDecor). @@ -1658,6 +1664,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di clearNativeImages(&g); g.native_images.deinit(gpa); g.prepared_images.deinit(gpa); + g.art.deinit(gpa); + if (g.art_arena) |*arena| arena.deinit(); g.cover.deinit(gpa); g.decor.deinit(gpa); } @@ -5159,7 +5167,7 @@ fn scrollScissor(g: *const Gui, layout: CellLayout, sw: u32, sh: u32) c.SDL_Rect return .{ .x = x0, .y = y0, .w = x1 - x0, .h = y1 - y0 }; } -fn releaseNativeImage(g: *Gui, key: pardes.ImageCacheKey) void { +fn releaseNativeImage(g: *Gui, key: u64) void { if (g.native_images.fetchRemove(key)) |removed| c.SDL_ReleaseGPUTexture(g.device, removed.value); } @@ -5170,79 +5178,62 @@ fn clearNativeImages(g: *Gui) void { g.native_images.clearRetainingCapacity(); } -fn surfaceHasNativeKey(surface: *const pardes.Surface, key: pardes.ImageCacheKey) bool { - for (surface.images.items) |place| { - if (validImageBytes(place) != null and place.cacheKey().eql(key)) return true; +/// A placement the window shows as a texture: pixels, not glyph art. +fn texturePlacement(place: pardes.Placement) bool { + return !place.art.glyph and validImageBytes(place) != null; +} + +fn surfaceHasNativeKey(surface: *const pardes.Surface, key: u64) bool { + for (surface.placements.items) |place| { + if (texturePlacement(place) and place.key == key) return true; } return false; } -fn validImageBytes(place: pardes.ImagePlace) ?u32 { - if (place.iw == 0 or place.ih == 0 or - place.iw > std.math.maxInt(u32) or place.ih > std.math.maxInt(u32)) return null; - const pixels = std.math.mul(usize, place.iw, place.ih) catch return null; - const bytes = std.math.mul(usize, pixels, 4) catch return null; - if (place.rgba.len != bytes or bytes > std.math.maxInt(u32)) return null; +fn validImageBytes(place: pardes.Placement) ?u32 { + const r = place.raster; + // Rows packed: a texture upload takes no stride. + if (r.w == 0 or r.h == 0 or r.stride != r.w * 4) return null; + const bytes = std.math.mul(usize, r.stride, r.h) catch return null; + if (bytes > std.math.maxInt(u32)) return null; return @intCast(bytes); } -const NativeBounds = struct { w: u32, h: u32 }; - -fn nativeBounds(g: *const Gui, place: pardes.ImagePlace) ?NativeBounds { - const bound_w = std.math.mul(u32, place.w, g.cell_w) catch return null; - const bound_h = std.math.mul(u32, place.h, g.cell_h) catch return null; - return .{ .w = bound_w, .h = bound_h }; -} - -fn nativePlaceGeometry(g: *const Gui, place: pardes.ImagePlace) ?pardes.image.NativeGeometry { - if (comptime pardes.pdf_enabled) { - if (place.native.geometry) |geometry| return geometry; - const bounds = nativeBounds(g, place) orelse return null; - return pardes.image.nativeGeometry( - place.iw, - place.ih, - bounds.w, - bounds.h, - place.native.fit, - place.native.pan_x, - place.native.pan_y, - ); - } - return null; -} - -fn nativePlaceDrawable(g: *const Gui, place: pardes.ImagePlace) bool { - if (comptime pardes.pdf_enabled) return nativePlaceGeometry(g, place) != null; - const bounds = nativeBounds(g, place) orelse return false; - return pardes.image.containGeometry(place.iw, place.ih, bounds.w, bounds.h) != null; +/// Where the placement's pixels go in its cells: where it says, or fitted +/// there (no larger than it is, at the top left). +fn nativePlaceGeometry(g: *const Gui, place: pardes.Placement) ?pardes.image.NativeGeometry { + if (place.at) |at| return .{ .src = place.src, .dst = at }; + const bound_w = std.math.mul(u32, place.dst.w, g.cell_w) catch return null; + const bound_h = std.math.mul(u32, place.dst.h, g.cell_h) catch return null; + const fit = pardes.image.containGeometry(place.src.w, place.src.h, bound_w, bound_h) orelse return null; + return .{ .src = place.src, .dst = fit.dst }; } fn uploadNativeTexture( g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, - place: pardes.ImagePlace, + place: pardes.Placement, ) !void { const byte_len = validImageBytes(place) orelse return error.BadImage; - const key = place.cacheKey(); + const key = place.key; if (g.native_images.contains(key)) return; - if (comptime pardes.pdf_enabled) if (place.patch) |patch| patch: { - // The core changed only these rows since a revision we still hold: + const r = place.raster; + const row = r.stride; + if (place.dirty) |dirty| dirty: { + // The core changed only these rows since a picture we still hold: // write them into that texture rather than sending the whole page. - var base = key; - base.revision = patch.from; - if (patch.y + patch.h > place.ih) break :patch; - const texture = g.native_images.get(base) orelse break :patch; - const row = place.iw * 4; - try uploadTextureRows(g, cmd, texture, place.rgba[patch.y * row ..][0 .. patch.h * row], place.iw, patch.y, patch.h); + if (dirty.y + dirty.h > r.h) break :dirty; + const texture = g.native_images.get(dirty.from) orelse break :dirty; + try uploadTextureRows(g, cmd, texture, r.bytes()[dirty.y * row ..][0 .. dirty.h * row], r.w, dirty.y, dirty.h); try g.native_images.put(gpa, key, texture); - _ = g.native_images.remove(base); + _ = g.native_images.remove(dirty.from); return; - }; + } - const texture = texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, @intCast(place.iw), @intCast(place.ih)) orelse return error.GpuCreate; + const texture = texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, r.w, r.h) orelse return error.GpuCreate; errdefer c.SDL_ReleaseGPUTexture(g.device, texture); - try uploadTextureRows(g, cmd, texture, place.rgba[0..byte_len], place.iw, 0, place.ih); + try uploadTextureRows(g, cmd, texture, r.bytes()[0..byte_len], r.w, 0, r.h); try g.native_images.put(gpa, key, texture); } @@ -5289,54 +5280,46 @@ fn uploadTextureRows( c.SDL_EndGPUCopyPass(copy); } -fn appendPreparedImage(g: *Gui, gpa: std.mem.Allocator, place: pardes.ImagePlace) void { - const texture = g.native_images.get(place.cacheKey()) orelse return; - if (!nativePlaceDrawable(g, place)) return; +fn appendPreparedImage(g: *Gui, gpa: std.mem.Allocator, place: pardes.Placement) void { + const texture = g.native_images.get(place.key) orelse return; + if (nativePlaceGeometry(g, place) == null) return; g.prepared_images.append(gpa, .{ .place = place, .texture = texture }) catch {}; } fn emitPreparedImage( g: *Gui, + surface: *const pardes.Surface, instance: *ImageInstance, prepared: PreparedImage, win_w: f32, win_h: f32, ) bool { const place = prepared.place; - const body_x = @as(u32, place.x) * g.cell_w; - const body_y = @as(u32, place.y) * g.cell_h; - if (comptime pardes.pdf_enabled) { - const geometry = nativePlaceGeometry(g, place) orelse return false; - const px0 = @as(f32, @floatFromInt(body_x + geometry.dst.x)); - const py0 = @as(f32, @floatFromInt(body_y + geometry.dst.y)) + place.native.pixel_offset_y; - const px1 = px0 + @as(f32, @floatFromInt(geometry.dst.w)); - const py1 = py0 + @as(f32, @floatFromInt(geometry.dst.h)); - const image_w: f32 = @floatFromInt(place.iw); - const image_h: f32 = @floatFromInt(place.ih); - instance.* = .{ - .x0 = (px0 / win_w) * 2.0 - 1.0, - .y0 = 1.0 - (py0 / win_h) * 2.0, - .x1 = (px1 / win_w) * 2.0 - 1.0, - .y1 = 1.0 - (py1 / win_h) * 2.0, - .u0 = @as(f32, @floatFromInt(geometry.src.x)) / image_w, - .v0 = @as(f32, @floatFromInt(geometry.src.y)) / image_h, - .u1 = @as(f32, @floatFromInt(geometry.src.x + geometry.src.w)) / image_w, - .v1 = @as(f32, @floatFromInt(geometry.src.y + geometry.src.h)) / image_h, - }; - } else { - const bounds = nativeBounds(g, place) orelse return false; - const fit = (pardes.image.containGeometry(place.iw, place.ih, bounds.w, bounds.h) orelse return false).dst; - instance.* = .{ - .x0 = (@as(f32, @floatFromInt(body_x)) / win_w) * 2.0 - 1.0, - .y0 = 1.0 - (@as(f32, @floatFromInt(body_y)) / win_h) * 2.0, - .x1 = (@as(f32, @floatFromInt(body_x + fit.w)) / win_w) * 2.0 - 1.0, - .y1 = 1.0 - (@as(f32, @floatFromInt(body_y + fit.h)) / win_h) * 2.0, - .u0 = 0, - .v0 = 0, - .u1 = 1, - .v1 = 1, - }; - } + const body_x = @as(u32, place.dst.x) * g.cell_w; + const body_y = @as(u32, place.dst.y) * g.cell_h; + const geometry = nativePlaceGeometry(g, place) orelse return false; + // A strip's pictures lie at its whole pixel: up by the fraction left. + var scroll: f64 = 0; + for (surface.regionList()) |r| if (r.kind == .body and r.owner == place.pane) { + scroll = r.scroll_px; + break; + }; + const px0 = @as(f32, @floatFromInt(body_x + geometry.dst.x)); + const py0 = @as(f32, @floatFromInt(body_y + geometry.dst.y)) - @as(f32, @floatCast(scroll - @floor(scroll))); + const px1 = px0 + @as(f32, @floatFromInt(geometry.dst.w)); + const py1 = py0 + @as(f32, @floatFromInt(geometry.dst.h)); + const image_w: f32 = @floatFromInt(place.raster.w); + const image_h: f32 = @floatFromInt(place.raster.h); + instance.* = .{ + .x0 = (px0 / win_w) * 2.0 - 1.0, + .y0 = 1.0 - (py0 / win_h) * 2.0, + .x1 = (px1 / win_w) * 2.0 - 1.0, + .y1 = 1.0 - (py1 / win_h) * 2.0, + .u0 = @as(f32, @floatFromInt(geometry.src.x)) / image_w, + .v0 = @as(f32, @floatFromInt(geometry.src.y)) / image_h, + .u1 = @as(f32, @floatFromInt(geometry.src.x + geometry.src.w)) / image_w, + .v1 = @as(f32, @floatFromInt(geometry.src.y + geometry.src.h)) / image_h, + }; return true; } @@ -5349,13 +5332,13 @@ fn prepareNativeImages( sw: u32, sh: u32, ) bool { - for (surface.images.items) |place| { - if (validImageBytes(place) == null) continue; + for (surface.placements.items) |place| { + if (!texturePlacement(place)) continue; uploadNativeTexture(g, gpa, cmd, place) catch continue; } while (true) { - var stale: [pardes.MAX_PANES]pardes.ImageCacheKey = undefined; + var stale: [pardes.MAX_PANES]u64 = undefined; var stale_len: usize = 0; var iterator = g.native_images.iterator(); while (iterator.next()) |entry| { @@ -5370,8 +5353,8 @@ fn prepareNativeImages( g.prepared_images.clearRetainingCapacity(); // The pictures are the page's: the other groups float over them. - for (surface.images.items) |place| - if (validImageBytes(place) != null) appendPreparedImage(g, gpa, place); + for (surface.placements.items) |place| + if (texturePlacement(place)) appendPreparedImage(g, gpa, place); groups.items[0].image_start = 0; groups.items[0].image_count = std.math.cast(u32, g.prepared_images.items.len) orelse return false; @@ -5385,7 +5368,7 @@ fn prepareNativeImages( const win_h: f32 = @floatFromInt(sh); var idx: u32 = 0; for (g.prepared_images.items) |prepared| { - if (!emitPreparedImage(g, &instances[idx], prepared, win_w, win_h)) continue; + if (!emitPreparedImage(g, surface, &instances[idx], prepared, win_w, win_h)) continue; idx += 1; } c.SDL_UnmapGPUTransferBuffer(g.device, g.image_vxfer.?); @@ -5399,12 +5382,12 @@ fn prepareNativeImages( return true; } -fn imageScissor(g: *const Gui, place: pardes.ImagePlace) c.SDL_Rect { +fn imageScissor(g: *const Gui, place: pardes.Placement) c.SDL_Rect { return .{ - .x = @intCast(@as(u32, place.x) * g.cell_w), - .y = @intCast(@as(u32, place.y) * g.cell_h), - .w = @intCast(@as(u32, place.w) * g.cell_w), - .h = @intCast(@as(u32, place.h) * g.cell_h), + .x = @intCast(@as(u32, place.dst.x) * g.cell_w), + .y = @intCast(@as(u32, place.dst.y) * g.cell_h), + .w = @intCast(@as(u32, place.dst.w) * g.cell_w), + .h = @intCast(@as(u32, place.dst.h) * g.cell_h), }; } @@ -5440,8 +5423,13 @@ fn renderFrame( g: *Gui, gpa: std.mem.Allocator, core: *pardes.Pardes, - surface: *const pardes.Surface, + canonical: *const pardes.Surface, ) !bool { + // The pictures a pane asks to see as glyph art, drawn into a copy of + // the frame (Art.zig); the rest are textures. + if (g.art_arena == null) g.art_arena = .init(gpa); + _ = g.art_arena.?.reset(.retain_capacity); + const surface = g.art.frame(gpa, g.art_arena.?.allocator(), canonical, true, g.cell_h) catch canonical; if (surface.pointer_shape != g.pointer_shape) { g.pointer_shape = surface.pointer_shape; const cursor = switch (g.pointer_shape) { diff --git a/src/image.zig b/src/image.zig index 417cc20e..215bfc25 100644 --- a/src/image.zig +++ b/src/image.zig @@ -1,485 +1,16 @@ -//! Image decoding, pixel placement, and glyph approximation. +//! Image decoding, pixel placement, and the image pane. const std = @import("std"); const builtin = @import("builtin"); const zstbi = @import("zstbi"); -/// The 16-colour ANSI table below is the only thing this file ever wanted from -/// the emulator. -const ghostty_vt = @import("ghostty-vt"); const pdf_enabled = @import("pardes_config").mupdf; const pardes = @import("pardes.zig"); const config = @import("config.zig"); const filesystem = @import("fs.zig"); -// Glyph matching adapted from caioluders/petsciinator. -pub const GlyphArt = struct { - pub const commodore = [16][3]u8{ - .{ 0x00, 0x00, 0x00 }, // 0 black - .{ 0xff, 0xff, 0xff }, // 1 white - .{ 0x68, 0x37, 0x2b }, // 2 red - .{ 0x70, 0xa4, 0xb2 }, // 3 cyan - .{ 0x6f, 0x3d, 0x86 }, // 4 purple - .{ 0x58, 0x8d, 0x43 }, // 5 green - .{ 0x35, 0x28, 0x79 }, // 6 blue - .{ 0xb8, 0xc7, 0x6f }, // 7 yellow - .{ 0x6f, 0x4f, 0x25 }, // 8 orange - .{ 0x43, 0x39, 0x00 }, // 9 brown - .{ 0x9a, 0x67, 0x59 }, // 10 light red - .{ 0x44, 0x44, 0x44 }, // 11 dark grey - .{ 0x6c, 0x6c, 0x6c }, // 12 grey - .{ 0x9a, 0xd2, 0x84 }, // 13 light green - .{ 0x6c, 0x5e, 0xb5 }, // 14 light blue - .{ 0x95, 0x95, 0x95 }, // 15 light grey - }; - - const Palette = [16][3]u8; - - pub const Cell = struct { - glyph: [4]u8 = .{ ' ', 0, 0, 0 }, - glyph_len: u3 = 1, - fg: u4 = 1, - bg: u4 = 0, - }; - - pub const Grid = struct { cells: []Cell, cols: usize, rows: usize }; - - fn dist2(a: [3]u8, b: [3]u8) u32 { - const dr = @as(i32, a[0]) - b[0]; - const dg = @as(i32, a[1]) - b[1]; - const db = @as(i32, a[2]) - b[2]; - return @intCast(dr * dr + dg * dg + db * db); - } - - fn nearest(px: [3]u8, pal: Palette) u4 { - var best: u4 = 0; - var bestd: u32 = std.math.maxInt(u32); - for (pal, 0..) |c, i| { - const d = dist2(px, c); - if (d < bestd) { - bestd = d; - best = @intCast(i); - } - } - return best; - } - - // ---- glyph set: each is a codepoint + an 8x8 ink bitmap (bit y*8+x set = fg) ---- - const Glyph = struct { cp: u21, bits: u64 }; - - fn sextantCp(p: u6) u21 { - return switch (p) { - 0 => ' ', - 21 => 0x258C, // left half ▌ - 42 => 0x2590, // right half ▐ - 63 => 0x2588, // full block █ - else => blk: { - var off: u21 = @as(u21, p) - 1; - if (p > 21) off -= 1; - if (p > 42) off -= 1; - break :blk 0x1FB00 + off; // Symbols for Legacy Computing sextants - }, - }; - } - - // the 8x8 ink bitmap for a sextant pattern (2 cols x 3 rows of subcells). - fn sextantBits(p: u6) u64 { - var bits: u64 = 0; - var y: usize = 0; - while (y < 8) : (y += 1) { - const band = (y * 3) / 8; // 0,0,0,1,1,1,2,2 - var x: usize = 0; - while (x < 8) : (x += 1) { - const col: usize = if (x < 4) 0 else 1; - const sub: u6 = @intCast(band * 2 + col); - if ((p >> sub) & 1 != 0) bits |= @as(u64, 1) << @intCast(y * 8 + x); - } - } - return bits; - } - - // pack 8 row-bytes (bit x set, x=0 leftmost) into the 8x8 bitmap. - fn glyphMask(r: [8]u8) u64 { - var b: u64 = 0; - for (r, 0..) |row, y| b |= @as(u64, row) << @intCast(y * 8); - return b; - } - - // the horizontal half blocks and the ten 2x2 quadrants — sextants are 2x3, so they - // can't express an exact 4-row half or a quarter; these fill that gap. - const block_glyphs = [_]Glyph{ - .{ .cp = 0x2580, .bits = glyphMask(.{ 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0 }) }, // ▀ top half - .{ .cp = 0x2584, .bits = glyphMask(.{ 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff }) }, // ▄ bottom half - .{ .cp = 0x2598, .bits = glyphMask(.{ 0x0f, 0x0f, 0x0f, 0x0f, 0, 0, 0, 0 }) }, // ▘ TL - .{ .cp = 0x259d, .bits = glyphMask(.{ 0xf0, 0xf0, 0xf0, 0xf0, 0, 0, 0, 0 }) }, // ▝ TR - .{ .cp = 0x2596, .bits = glyphMask(.{ 0, 0, 0, 0, 0x0f, 0x0f, 0x0f, 0x0f }) }, // ▖ BL - .{ .cp = 0x2597, .bits = glyphMask(.{ 0, 0, 0, 0, 0xf0, 0xf0, 0xf0, 0xf0 }) }, // ▗ BR - .{ .cp = 0x259a, .bits = glyphMask(.{ 0x0f, 0x0f, 0x0f, 0x0f, 0xf0, 0xf0, 0xf0, 0xf0 }) }, // ▚ TL+BR - .{ .cp = 0x259e, .bits = glyphMask(.{ 0xf0, 0xf0, 0xf0, 0xf0, 0x0f, 0x0f, 0x0f, 0x0f }) }, // ▞ TR+BL - .{ .cp = 0x259b, .bits = glyphMask(.{ 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f }) }, // ▛ ¬BR - .{ .cp = 0x259c, .bits = glyphMask(.{ 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0 }) }, // ▜ ¬BL - .{ .cp = 0x2599, .bits = glyphMask(.{ 0x0f, 0x0f, 0x0f, 0x0f, 0xff, 0xff, 0xff, 0xff }) }, // ▙ ¬TR - .{ .cp = 0x259f, .bits = glyphMask(.{ 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff }) }, // ▟ ¬TL - }; - - // line/diagonal glyphs add the characteristic PETSCII "drawn" look on edges. - const line_glyphs = [_]Glyph{ - .{ .cp = 0x2500, .bits = glyphMask(.{ 0, 0, 0, 0xff, 0xff, 0, 0, 0 }) }, // ─ - .{ .cp = 0x2502, .bits = glyphMask(.{ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }) }, // │ - .{ .cp = 0x253c, .bits = glyphMask(.{ 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18 }) }, // ┼ - .{ .cp = 0x2572, .bits = glyphMask(.{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }) }, // ╲ - .{ .cp = 0x2571, .bits = glyphMask(.{ 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }) }, // ╱ - .{ .cp = 0x2573, .bits = glyphMask(.{ 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }) }, // ╳ - }; - - const ascii_glyphs = [_]Glyph{ - .{ .cp = 0x21, .bits = 0x00180018183c3c18 }, // ! - .{ .cp = 0x22, .bits = 0x0000000000246666 }, // " - .{ .cp = 0x23, .bits = 0x0036367f367f3636 }, // # - .{ .cp = 0x24, .bits = 0x00183e603c067c18 }, // $ - .{ .cp = 0x25, .bits = 0x0063660c18336300 }, // % - .{ .cp = 0x26, .bits = 0x006e333b6e1c361c }, // & - .{ .cp = 0x27, .bits = 0x00000000000c1818 }, // ' - .{ .cp = 0x28, .bits = 0x0030180c0c0c1830 }, // ( - .{ .cp = 0x29, .bits = 0x000c18303030180c }, // ) - .{ .cp = 0x2a, .bits = 0x0000663cff3c6600 }, // * - .{ .cp = 0x2b, .bits = 0x000018187e181800 }, // + - .{ .cp = 0x2c, .bits = 0x0c18180000000000 }, // , - .{ .cp = 0x2d, .bits = 0x000000007e000000 }, // - - .{ .cp = 0x2e, .bits = 0x0018180000000000 }, // . - .{ .cp = 0x2f, .bits = 0x000103060c183060 }, // / - .{ .cp = 0x30, .bits = 0x001c36636b63361c }, // 0 - .{ .cp = 0x31, .bits = 0x007e181818181c18 }, // 1 - .{ .cp = 0x32, .bits = 0x007f660c3860633e }, // 2 - .{ .cp = 0x33, .bits = 0x003e63603c60633e }, // 3 - .{ .cp = 0x34, .bits = 0x0078307f33363c38 }, // 4 - .{ .cp = 0x35, .bits = 0x003e63603f03037f }, // 5 - .{ .cp = 0x36, .bits = 0x003e63633f03061c }, // 6 - .{ .cp = 0x37, .bits = 0x000c0c0c1830637f }, // 7 - .{ .cp = 0x38, .bits = 0x003e63633e63633e }, // 8 - .{ .cp = 0x39, .bits = 0x001e30607e63633e }, // 9 - .{ .cp = 0x3a, .bits = 0x0018180000181800 }, // : - .{ .cp = 0x3b, .bits = 0x0c18180000181800 }, // ; - .{ .cp = 0x3c, .bits = 0x006030180c183060 }, // < - .{ .cp = 0x3d, .bits = 0x00007e00007e0000 }, // = - .{ .cp = 0x3e, .bits = 0x00060c1830180c06 }, // > - .{ .cp = 0x3f, .bits = 0x001800181830633e }, // ? - .{ .cp = 0x40, .bits = 0x001e037b7b7b633e }, // @ - .{ .cp = 0x41, .bits = 0x006363637f63361c }, // A - .{ .cp = 0x42, .bits = 0x003f66663e66663f }, // B - .{ .cp = 0x43, .bits = 0x003c66030303663c }, // C - .{ .cp = 0x44, .bits = 0x001f36666666361f }, // D - .{ .cp = 0x45, .bits = 0x007f46161e16467f }, // E - .{ .cp = 0x46, .bits = 0x000f06161e16467f }, // F - .{ .cp = 0x47, .bits = 0x005c66730303663c }, // G - .{ .cp = 0x48, .bits = 0x006363637f636363 }, // H - .{ .cp = 0x49, .bits = 0x003c18181818183c }, // I - .{ .cp = 0x4a, .bits = 0x001e333330303078 }, // J - .{ .cp = 0x4b, .bits = 0x006766361e366667 }, // K - .{ .cp = 0x4c, .bits = 0x007f66460606060f }, // L - .{ .cp = 0x4d, .bits = 0x0063636b7f7f7763 }, // M - .{ .cp = 0x4e, .bits = 0x006363737b6f6763 }, // N - .{ .cp = 0x4f, .bits = 0x003e63636363633e }, // O - .{ .cp = 0x50, .bits = 0x000f06063e66663f }, // P - .{ .cp = 0x51, .bits = 0x703e73636363633e }, // Q - .{ .cp = 0x52, .bits = 0x006766363e66663f }, // R - .{ .cp = 0x53, .bits = 0x003c6630180c663c }, // S - .{ .cp = 0x54, .bits = 0x003c1818185a7e7e }, // T - .{ .cp = 0x55, .bits = 0x003e636363636363 }, // U - .{ .cp = 0x56, .bits = 0x001c366363636363 }, // V - .{ .cp = 0x57, .bits = 0x00367f6b6b636363 }, // W - .{ .cp = 0x58, .bits = 0x006363361c366363 }, // X - .{ .cp = 0x59, .bits = 0x003c18183c666666 }, // Y - .{ .cp = 0x5a, .bits = 0x007f664c1831637f }, // Z - .{ .cp = 0x5b, .bits = 0x003c0c0c0c0c0c3c }, // [ - .{ .cp = 0x5c, .bits = 0x00406030180c0603 }, // \\ - .{ .cp = 0x5d, .bits = 0x003c30303030303c }, // ] - .{ .cp = 0x5e, .bits = 0x0000000063361c08 }, // ^ - .{ .cp = 0x5f, .bits = 0xff00000000000000 }, // _ - .{ .cp = 0x60, .bits = 0x000000000030180c }, // ` - .{ .cp = 0x61, .bits = 0x006e333e301e0000 }, // a - .{ .cp = 0x62, .bits = 0x003b6666663e0607 }, // b - .{ .cp = 0x63, .bits = 0x003e6303633e0000 }, // c - .{ .cp = 0x64, .bits = 0x006e3333333e3038 }, // d - .{ .cp = 0x65, .bits = 0x003e037f633e0000 }, // e - .{ .cp = 0x66, .bits = 0x000f06061f06663c }, // f - .{ .cp = 0x67, .bits = 0x1f303e33336e0000 }, // g - .{ .cp = 0x68, .bits = 0x006766666e360607 }, // h - .{ .cp = 0x69, .bits = 0x003c1818181c0018 }, // i - .{ .cp = 0x6a, .bits = 0x3c66666060600060 }, // j - .{ .cp = 0x6b, .bits = 0x0067361e36660607 }, // k - .{ .cp = 0x6c, .bits = 0x003c18181818181c }, // l - .{ .cp = 0x6d, .bits = 0x006b6b6b7f370000 }, // m - .{ .cp = 0x6e, .bits = 0x00666666663b0000 }, // n - .{ .cp = 0x6f, .bits = 0x003e6363633e0000 }, // o - .{ .cp = 0x70, .bits = 0x0f063e66663b0000 }, // p - .{ .cp = 0x71, .bits = 0x78303e33336e0000 }, // q - .{ .cp = 0x72, .bits = 0x000f06066e3b0000 }, // r - .{ .cp = 0x73, .bits = 0x003f603e037e0000 }, // s - .{ .cp = 0x74, .bits = 0x00386c0c0c3f0c0c }, // t - .{ .cp = 0x75, .bits = 0x006e333333330000 }, // u - .{ .cp = 0x76, .bits = 0x001c366363630000 }, // v - .{ .cp = 0x77, .bits = 0x00367f6b6b630000 }, // w - .{ .cp = 0x78, .bits = 0x0063361c36630000 }, // x - .{ .cp = 0x79, .bits = 0x3f607e6363630000 }, // y - .{ .cp = 0x7a, .bits = 0x007e4c18327e0000 }, // z - .{ .cp = 0x7b, .bits = 0x007018180e181870 }, // { - .{ .cp = 0x7c, .bits = 0x0018181818181818 }, // | - .{ .cp = 0x7d, .bits = 0x000e18187018180e }, // } - .{ .cp = 0x7e, .bits = 0x0000000000003b6e }, // ~ - }; - - // the block glyph table (sextants + half/quadrant blocks + line glyphs), built at - // comptime, and the same set extended with the ASCII glyphs. `render(ascii=…)` picks. - const glyphs = blk: { - @setEvalBranchQuota(100000); - var list: [64 + block_glyphs.len + line_glyphs.len]Glyph = undefined; - var p: usize = 0; - while (p < 64) : (p += 1) list[p] = .{ .cp = sextantCp(@intCast(p)), .bits = sextantBits(@intCast(p)) }; - for (block_glyphs, 0..) |bg, i| list[64 + i] = bg; - for (line_glyphs, 0..) |lg, i| list[64 + block_glyphs.len + i] = lg; - break :blk list; - }; - const glyphs_ascii = glyphs ++ ascii_glyphs; - - fn matchCell(cell: *const [64][3]u8, pal: Palette, gset: []const Glyph) Cell { - var counts = @as([16]u16, @splat(0)); - for (cell) |px| counts[nearest(px, pal)] += 1; - var cand: [4]u4 = undefined; - var ncand: usize = 0; - var used = @as([16]bool, @splat(false)); - while (ncand < 4) : (ncand += 1) { - var best: ?usize = null; - for (counts, 0..) |c, i| { - if (used[i] or c == 0) continue; - if (best == null or c > counts[best.?]) best = i; - } - if (best) |bi| { - cand[ncand] = @intCast(bi); - used[bi] = true; - } else break; - } - if (ncand == 0) return .{}; // can't happen (64 pixels), but keep it total - if (ncand == 1) return encode(' ', cand[0], cand[0]); // solid color - - var best_cost: i64 = std.math.maxInt(i64); - var best = encode(' ', cand[0], cand[0]); - var fi: usize = 0; - while (fi < ncand) : (fi += 1) { - var bi: usize = 0; - while (bi < ncand) : (bi += 1) { - if (fi == bi) continue; - const fg = cand[fi]; - const bg = cand[bi]; - var dfg: [64]u32 = undefined; - var dbg: [64]u32 = undefined; - var base: i64 = 0; - for (cell, 0..) |px, p| { - dfg[p] = dist2(px, pal[fg]); - dbg[p] = dist2(px, pal[bg]); - base += dbg[p]; - } - for (gset) |g| { - var delta: i64 = 0; - var bits = g.bits; - while (bits != 0) : (bits &= bits - 1) { - const p: usize = @ctz(bits); - delta += @as(i64, dfg[p]) - @as(i64, dbg[p]); - } - const cost = base + delta; - if (cost < best_cost) { - best_cost = cost; - best = encode(g.cp, fg, bg); - } - } - } - } - return best; - } - - fn encode(cp: u21, fg: u4, bg: u4) Cell { - var c = Cell{ .fg = fg, .bg = bg }; - const n = std.unicode.utf8Encode(cp, &c.glyph) catch 1; - c.glyph_len = @intCast(n); - return c; - } - - pub fn render(gpa: std.mem.Allocator, rgba: []const u8, iw: usize, ih: usize, max_cols: usize, max_rows: usize, pal: Palette, ascii: bool) !Grid { - if (iw == 0 or ih == 0 or max_cols == 0 or max_rows == 0) return .{ .cells = try gpa.alloc(Cell, 0), .cols = 0, .rows = 0 }; - // contain-fit; cells are ~twice as tall as wide, so a row spans 2 width-units. - var cols = max_cols; - var rows = (max_cols * ih) / (2 * iw); - if (rows > max_rows) { - rows = max_rows; - cols = (max_rows * 2 * iw) / ih; - } - cols = std.math.clamp(cols, 1, max_cols); - rows = std.math.clamp(rows, 1, max_rows); - return renderRect(gpa, rgba, iw, ih, .{ .w = @intCast(iw), .h = @intCast(ih) }, cols, rows, pal, ascii); - } - - /// The pixels of `src` (a rectangle of the `iw` x `ih` image) laid - /// over exactly `cols` x `rows` cells, whatever their shape: a - /// terminal's image fragment is already the size its cells are. - pub fn renderRect(gpa: std.mem.Allocator, rgba: []const u8, iw: usize, ih: usize, src: PixelRect, cols: usize, rows: usize, pal: Palette, ascii: bool) !Grid { - const x0: usize = @min(src.x, iw); - const y0: usize = @min(src.y, ih); - const sw: usize = @min(src.w, iw - x0); - const sh: usize = @min(src.h, ih - y0); - if (sw == 0 or sh == 0 or cols == 0 or rows == 0) return .{ .cells = try gpa.alloc(Cell, 0), .cols = 0, .rows = 0 }; - const gset: []const Glyph = if (ascii) &glyphs_ascii else &glyphs; - const cells = try gpa.alloc(Cell, cols * rows); - var cy: usize = 0; - while (cy < rows) : (cy += 1) { - const ry0 = y0 + cy * sh / rows; - const ry1 = @max(ry0 + 1, y0 + (cy + 1) * sh / rows); - var cx: usize = 0; - while (cx < cols) : (cx += 1) { - const rx0 = x0 + cx * sw / cols; - const rx1 = @max(rx0 + 1, x0 + (cx + 1) * sw / cols); - var cell: [64][3]u8 = undefined; - var sy: usize = 0; - while (sy < 8) : (sy += 1) { - const py0 = ry0 + sy * (ry1 - ry0) / 8; - const py1 = @max(py0 + 1, ry0 + (sy + 1) * (ry1 - ry0) / 8); - var sx: usize = 0; - while (sx < 8) : (sx += 1) { - const px0 = rx0 + sx * (rx1 - rx0) / 8; - const px1 = @max(px0 + 1, rx0 + (sx + 1) * (rx1 - rx0) / 8); - var rs: usize = 0; - var gs: usize = 0; - var bs: usize = 0; - var n: usize = 0; - var yy = py0; - while (yy < py1 and yy < ih) : (yy += 1) { - var xx = px0; - while (xx < px1 and xx < iw) : (xx += 1) { - const i = (yy * iw + xx) * 4; - rs += rgba[i]; - gs += rgba[i + 1]; - bs += rgba[i + 2]; - n += 1; - } - } - if (n == 0) n = 1; - cell[sy * 8 + sx] = .{ @intCast(rs / n), @intCast(gs / n), @intCast(bs / n) }; - } - } - cells[cy * cols + cx] = matchCell(&cell, pal, gset); - } - } - return .{ .cells = cells, .cols = cols, .rows = rows }; - } - - test "sextant codepoints: blocks + endpoints" { - try std.testing.expectEqual(@as(u21, ' '), sextantCp(0)); - try std.testing.expectEqual(@as(u21, 0x2588), sextantCp(63)); - try std.testing.expectEqual(@as(u21, 0x258C), sextantCp(21)); - try std.testing.expectEqual(@as(u21, 0x2590), sextantCp(42)); - try std.testing.expectEqual(@as(u21, 0x1FB00), sextantCp(1)); // first sextant - try std.testing.expectEqual(@as(u21, 0x1FB3B), sextantCp(62)); // last sextant - } - - test "matchCell: solid color -> space on that bg" { - var cell: [64][3]u8 = undefined; - for (&cell) |*p| p.* = commodore[5]; // all green - const m = matchCell(&cell, commodore, &glyphs); - try std.testing.expectEqual(@as(u4, 5), m.bg); - try std.testing.expectEqual(@as(u8, ' '), m.glyph[0]); - } - - test "matchCell: clean top/bottom split picks the two colors" { - var cell: [64][3]u8 = undefined; - for (0..64) |p| cell[p] = if (p < 32) commodore[1] else commodore[6]; // white over blue - const m = matchCell(&cell, commodore, &glyphs); - // both palette colors must be chosen (in some fg/bg order) - const a = @as(u4, @min(m.fg, m.bg)); - const b = @as(u4, @max(m.fg, m.bg)); - try std.testing.expectEqual(@as(u4, 1), a); - try std.testing.expectEqual(@as(u4, 6), b); - // a clean top/bottom split must resolve to a real block glyph (the top-4-rows - // half block ▀), never a blank cell. - const cp = std.unicode.utf8Decode(m.glyph[0..m.glyph_len]) catch 0; - try std.testing.expectEqual(@as(u21, 0x2580), cp); - } - - test "ascii option only enables the ascii glyphs" { - // the ascii glyph codepoints must be reachable exactly when ascii is on. - var seen_block = false; - var seen_ascii = false; - for (glyphs) |g| if (g.cp == '#') { - seen_block = true; - }; - for (glyphs_ascii) |g| if (g.cp == '#') { - seen_ascii = true; - }; - try std.testing.expect(!seen_block); // '#' is an ascii-only glyph - try std.testing.expect(seen_ascii); - try std.testing.expectEqual(glyphs.len + ascii_glyphs.len, glyphs_ascii.len); - } - - test "ascii glyph is chosen when a cell has its exact shape" { - // a cell shaped exactly like the font 'S' (ink=white on black) must match 'S' - // with ascii on (cost 0), and fall back to some block glyph with ascii off. - const s_bits: u64 = 0x003c6630180c663c; - var cell: [64][3]u8 = undefined; - for (0..64) |p| cell[p] = if ((s_bits >> @intCast(p)) & 1 != 0) commodore[1] else commodore[0]; - const on = matchCell(&cell, commodore, &glyphs_ascii); - try std.testing.expectEqual(@as(u21, 'S'), std.unicode.utf8Decode(on.glyph[0..on.glyph_len]) catch 0); - const off = matchCell(&cell, commodore, &glyphs); - try std.testing.expect((std.unicode.utf8Decode(off.glyph[0..off.glyph_len]) catch 0) != 'S'); - } - - test "render: tiny image produces a grid within bounds" { - const a = std.testing.allocator; - // 2x2 checker, RGBA - var img = @as([2 * 2 * 4]u8, @splat(0)); - img[0] = 255; - img[1] = 255; - img[2] = 255; - img[3] = 255; // (0,0) white - img[(3) * 4 + 0] = 255; - img[(3) * 4 + 1] = 255; - img[(3) * 4 + 2] = 255; - img[(3) * 4 + 3] = 255; // (1,1) white - const g = try render(a, &img, 2, 2, 10, 10, commodore, true); - defer a.free(g.cells); - try std.testing.expect(g.cols >= 1 and g.cols <= 10); - try std.testing.expect(g.rows >= 1 and g.rows <= 10); - try std.testing.expectEqual(g.cols * g.rows, g.cells.len); - } -}; - -pub const terminal_palette = [16][3]u8{ - .{ 0x1D, 0x1F, 0x21 }, // black - .{ 0xCC, 0x66, 0x66 }, // red - .{ 0xB5, 0xBD, 0x68 }, // green - .{ 0xF0, 0xC6, 0x74 }, // yellow - .{ 0x81, 0xA2, 0xBE }, // blue - .{ 0xB2, 0x94, 0xBB }, // magenta - .{ 0x8A, 0xBE, 0xB7 }, // cyan - .{ 0xC5, 0xC8, 0xC6 }, // white - .{ 0x66, 0x66, 0x66 }, // bright black - .{ 0xD5, 0x4E, 0x53 }, // bright red - .{ 0xB9, 0xCA, 0x4A }, // bright green - .{ 0xE7, 0xC5, 0x47 }, // bright yellow - .{ 0x7A, 0xA6, 0xDA }, // bright blue - .{ 0xC3, 0x97, 0xD8 }, // bright magenta - .{ 0x70, 0xC0, 0xB1 }, // bright cyan - .{ 0xEA, 0xEA, 0xEA }, // bright white -}; - -comptime { - for (terminal_palette, 0..) |rgb, i| { - const c = ghostty_vt.color.default[i]; - if (rgb[0] != c.r or rgb[1] != c.g or rgb[2] != c.b) @compileError( - "terminal palette differs from the emulator", - ); - } -} - /// cap the longest side before keeping/transmitting: a pane is at most a /// screenful of cells, multi-thousand-pixel photos waste decode/transmit time const MAX_DIM: u32 = 1280; -pub const PaletteMode = enum { commodore, terminal }; +pub const PaletteMode = pardes.Placement.Art.Palette; /// How a native pixel attachment is placed in its pane body. `contain` is the /// historical image-pane policy: shrink only when necessary and keep the @@ -898,19 +429,13 @@ test "PNG and JPEG decode through wuffs, sized from their headers first; other f // ---- the image pane ---- -const GridKey = struct { - cols: u16 = 0, - rows: u16 = 0, - palette: PaletteMode = .commodore, - ascii: bool = true, -}; - pub const State = struct { /// What the replay digest leaves out (src/walk.zig): the picture as - /// decoded from its bytes, off the loop as the render first wants it, - /// and the glyph art drawn from that. - pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "grid", "grid_key", "decoder", "decoded", "answer" } }; + /// decoded from its bytes, off the loop as the render first wants it. + pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "decoder", "decoded", "answer" } }; path: []u8, + /// Petscii, Palette and Ascii: how a frontend draws the picture as + /// glyph art (`Placement.Art`), which the core never does. glyph_art: bool = false, pmode: PaletteMode = .commodore, ascii: bool = true, @@ -920,8 +445,6 @@ pub const State = struct { ih: usize = 0, /// Dump-loaded bytes, decoded lazily by the same path as a disk image. raw: []u8 = &.{}, - grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 }, - grid_key: GridKey = .{}, /// The decode under way off the loop (ensureDecoded), and its answer. decoder: ?std.Thread = null, decoded: std.atomic.Value(bool) = .init(false), @@ -933,7 +456,6 @@ pub const State = struct { gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); if (state.raw.len > 0) gpa.free(state.raw); - if (state.grid.cells.len > 0) gpa.free(state.grid.cells); state.* = undefined; } }; @@ -1020,72 +542,17 @@ fn ensureDecoded(p: *pardes.Pardes, state: *State) void { ensureDecoded(p, state); } -fn ensureGrid(p: *pardes.Pardes, state: *State, cols: u16, rows: u16) void { - const wanted = GridKey{ .cols = cols, .rows = rows, .palette = state.pmode, .ascii = state.ascii }; - if (state.grid.cells.len > 0 and std.meta.eql(state.grid_key, wanted)) return; - if (state.grid.cells.len > 0) p.image_gpa.free(state.grid.cells); - const palette = switch (state.pmode) { - .commodore => GlyphArt.commodore, - .terminal => terminal_palette, - }; - state.grid = GlyphArt.render( - p.image_gpa, - state.rgba, - state.iw, - state.ih, - cols, - rows, - palette, - state.ascii, - ) catch .{ .cells = &.{}, .cols = 0, .rows = 0 }; - state.grid_key = wanted; -} - -pub fn draw( - p: *pardes.Pardes, - s: *pardes.Surface, - state: *State, - pane_id: u8, - serial: u32, - x: u16, - y: u16, - cols: u16, - rows: u16, -) void { +/// The picture over the body's cells, left for the frontend to fit (or +/// to draw as glyph art). +pub fn draw(p: *pardes.Pardes, state: *State, pane_id: u8, serial: u32, x: u16, y: u16, cols: u16, rows: u16) void { ensureDecoded(p, state); if (state.rgba.len == 0 or cols == 0 or rows == 0) return; - if (!state.glyph_art and p.frontend.native_images) { - _ = p.appendImagePlace(.{ - .pane = pane_id, - .serial = serial, - .x = x, - .y = y, - .w = cols, - .h = rows, - .rgba = state.rgba, - .iw = state.iw, - .ih = state.ih, - }); - return; - } - - ensureGrid(p, state, cols, rows); - if (state.grid.cells.len == 0) return; - const offx = if (cols > state.grid.cols) (@as(usize, cols) - state.grid.cols) / 2 else 0; - const offy = if (rows > state.grid.rows) (@as(usize, rows) - state.grid.rows) / 2 else 0; - for (0..state.grid.rows) |cy| for (0..state.grid.cols) |cx| { - const cell = &state.grid.cells[cy * state.grid.cols + cx]; - const fg: pardes.Color = switch (state.pmode) { - .commodore => .{ .rgb = GlyphArt.commodore[cell.fg] }, - .terminal => .{ .index = cell.fg }, - }; - const bg: pardes.Color = switch (state.pmode) { - .commodore => .{ .rgb = GlyphArt.commodore[cell.bg] }, - .terminal => .{ .index = cell.bg }, - }; - const sx = x + @as(u16, @intCast(offx + cx)); - const sy = y + @as(u16, @intCast(offy + cy)); - if (sx < s.cols and sy < s.rows) - s.set(sx, sy, cell.glyph[0..cell.glyph_len], .{ .fg = fg, .bg = bg }); - }; + _ = p.appendPlacement(.{ + .pane = pane_id, + .raster = .of(state.rgba, state.iw, state.ih), + .src = .{ .w = @intCast(state.iw), .h = @intCast(state.ih) }, + .dst = .{ .x = x, .y = y, .w = cols, .h = rows }, + .key = pardes.Placement.keyOf(serial, 0, 0), + .art = .{ .glyph = state.glyph_art, .palette = state.pmode, .ascii = state.ascii }, + }); } diff --git a/src/macos.zig b/src/macos.zig index d46f6cd4..0d79f96b 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -1755,52 +1755,50 @@ fn encodeChanged(diff: pardes.PanelCellDiff) u8 { } fn collectImages(st: *State, surface: *const pardes.Surface) void { - if (comptime !pardes.pdf_enabled) return; - if (surface.images.items.len == 0) return; - if (st.images.len < surface.images.items.len) { + const placements = surface.placements.items; + if (placements.len == 0) return; + if (st.images.len < placements.len) { const resized = if (st.images.len == 0) - st.gpa.alloc(Image, surface.images.items.len) + st.gpa.alloc(Image, placements.len) else - st.gpa.realloc(st.images, surface.images.items.len); + st.gpa.realloc(st.images, placements.len); st.images = resized catch return; } - for (surface.images.items) |place| { - if (place.iw == 0 or place.ih == 0 or place.rgba.len == 0) continue; - const geometry = place.native.geometry orelse image.NativeGeometry{ - .src = .{ .x = 0, .y = 0, .w = @intCast(place.iw), .h = @intCast(place.ih) }, - .dst = .{ - .x = 0, - .y = 0, - .w = @as(u32, place.w) * st.core.cell_pixels.w, - .h = @as(u32, place.h) * st.core.cell_pixels.h, - }, + for (placements) |place| { + if (place.raster.w == 0 or place.raster.h == 0) continue; + const dst = place.at orelse pardes.RectPx{ + .w = @as(u32, place.dst.w) * st.core.cell_pixels.w, + .h = @as(u32, place.dst.h) * st.core.cell_pixels.h, + }; + if (dst.w == 0 or dst.h == 0 or place.src.w == 0 or place.src.h == 0) continue; + var scroll: f64 = 0; + for (surface.regionList()) |r| if (r.kind == .body and r.owner == place.pane) { + scroll = r.scroll_px; + break; }; - if (geometry.dst.w == 0 or geometry.dst.h == 0) continue; - if (geometry.src.w == 0 or geometry.src.h == 0) continue; st.images[st.images_len] = .{ - .serial = place.serial, - .page = place.native.page, - .revision = place.native.revision, - .cell_x = place.x, - .cell_y = place.y, - .cell_w = place.w, - .cell_h = place.h, - .dst_x = geometry.dst.x, - .dst_y = geometry.dst.y, - .dst_w = geometry.dst.w, - .dst_h = geometry.dst.h, - .src_x = geometry.src.x, - .src_y = geometry.src.y, - .src_w = geometry.src.w, - .src_h = geometry.src.h, - .offset_y = place.native.pixel_offset_y, - .iw = @intCast(place.iw), - .ih = @intCast(place.ih), - .rgba = place.rgba.ptr, - .paper_bg = if (place.native.paper_bg) |bg| - @as(u32, bg[0]) << 16 | @as(u32, bg[1]) << 8 | bg[2] - else - color_default, + // The shell keys its textures by these three: the placement's + // key in two halves keeps them one picture each. + .serial = if (st.core.panes[place.pane]) |pane| pane.serial else 0, + .page = @truncate(place.key), + .revision = @truncate(place.key >> 32), + .cell_x = place.dst.x, + .cell_y = place.dst.y, + .cell_w = place.dst.w, + .cell_h = place.dst.h, + .dst_x = dst.x, + .dst_y = dst.y, + .dst_w = dst.w, + .dst_h = dst.h, + .src_x = place.src.x, + .src_y = place.src.y, + .src_w = place.src.w, + .src_h = place.src.h, + .offset_y = -@as(f32, @floatCast(scroll - @floor(scroll))), + .iw = place.raster.w, + .ih = place.raster.h, + .rgba = place.raster.ptr, + .paper_bg = color_default, }; st.images_len += 1; } diff --git a/src/ninep/screen.zig b/src/ninep/screen.zig index 71b656d2..8d8b6e33 100644 --- a/src/ninep/screen.zig +++ b/src/ninep/screen.zig @@ -39,7 +39,24 @@ pub fn render(p: *Pardes) ![]u8 { if (i != 0) try writer.writeByte(','); try std.json.Stringify.value(.{ if (cell.default) " " else cell.grapheme(), index }, .{}, writer); } - try writer.writeAll("]}\n"); + try writer.writeByte(']'); + // The pictures over the cells, which every frontend realises its own + // way (pixels, or glyph art in `rect`'s cells): a frame without any + // has no `images`. + if (surface.placements.items.len > 0) { + try writer.writeAll(",\"images\":["); + for (surface.placements.items, 0..) |place, i| { + if (i != 0) try writer.writeByte(','); + const serial = if (p.panes[place.pane]) |pane| pane.serial else 0; + try writer.print("{{\"serial\":{d},\"rect\":[{d},{d},{d},{d}],\"src\":[{d},{d},{d},{d}],\"key\":\"{x:0>16}\",\"petscii\":{}}}", .{ + serial, place.dst.x, place.dst.y, place.dst.w, place.dst.h, + place.src.x, place.src.y, place.src.w, place.src.h, place.key, + place.art.glyph, + }); + } + try writer.writeByte(']'); + } + try writer.writeAll("}\n"); return out.toOwnedSlice(); } @@ -266,8 +283,8 @@ test "screen inspection leaves the next real frame as it would have been" { try testing.expectEqual(expected.rows, actual.rows); try testing.expectEqualDeep(expected.cursor, actual.cursor); try testing.expectEqual(expected.cells.len, actual.cells.len); - try testing.expectEqual(@as(usize, 0), expected.images.items.len); - try testing.expectEqual(@as(usize, 0), actual.images.items.len); + try testing.expectEqual(@as(usize, 0), expected.placements.items.len); + try testing.expectEqual(@as(usize, 0), actual.placements.items.len); for (expected.cells, actual.cells) |*left, *right| try testing.expect(left.visuallyEqual(right)); control.presented(); inspected.presented(); diff --git a/src/pardes.zig b/src/pardes.zig index dee1db2d..22f4b7de 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -40,10 +40,10 @@ pub const Color = @import("surface.zig").Color; pub const FontRole = @import("surface.zig").FontRole; pub const CellStyle = @import("surface.zig").CellStyle; pub const Cell = @import("surface.zig").Cell; -pub const ImageCacheKey = @import("surface.zig").ImageCacheKey; -pub const NativePlacement = @import("surface.zig").NativePlacement; -pub const ImagePatch = @import("surface.zig").ImagePatch; -pub const ImagePlace = @import("surface.zig").ImagePlace; +pub const Placement = @import("surface.zig").Placement; +pub const Raster = @import("surface.zig").Raster; +pub const RectPx = @import("surface.zig").RectPx; +pub const RectCell = @import("surface.zig").RectCell; pub const Surface = @import("surface.zig").Surface; pub const Region = @import("surface.zig").Region; pub const Chrome = @import("surface.zig").Chrome; diff --git a/src/pdf_view.zig b/src/pdf_view.zig index bb960139..4da382fd 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -3338,6 +3338,15 @@ pub fn draw( state.far, ); if (state.document_scroll_y != shown) state.drawn_y = state.document_scroll_y; + // Below the notice chips, like an image: a placed page is drawn after + // the cells and would paint a chip out. + const body: pardes.RectCell = .{ + .x = text_x, + .y = core.bodyTop(pane, rect) + pane.notices.span(), + .w = text_width, + .h = (rect.h -| pane.tag_rows) -| pane.notices.span(), + }; + if (s.region(.body, pane_id)) |region| region.scroll_px = state.document_scroll_y; var placed_any = false; var page = visible.first; const visible_end = visible.first + visible.len; @@ -3352,73 +3361,36 @@ pub fn draw( if (workers == null or pardes.platform == .macos) continue; const size = state.page_sizes[page]; const rows = visibleRows(state, view, page, @intFromFloat(@max(1, @round(size.width * 8))), @intFromFloat(@max(1, @round(size.height * 8)))) orelse continue; - if (!core.appendImagePlace(.{ + if (!core.appendPlacement(.{ .pane = @intCast(pane_id), - .serial = pane.serial, - .native = .{ - .revision = 0, - .page = @intCast(page), - .fit = switch (state.fit) { - .width => .width, - .height => .height, - }, - .pan_x = state.pan_x, - .geometry = .{ - .src = .{ .x = 0, .y = 0, .w = 1, .h = 1 }, - .dst = .{ .x = rows.base.dst.x, .y = rows.dst_y, .w = rows.base.dst.w, .h = rows.dst_h }, - }, - .pixel_offset_y = rows.pixel_offset_y, - }, - .x = text_x, - .y = core.bodyTop(pane, rect) + pane.notices.span(), - .w = text_width, - .h = (rect.h -| pane.tag_rows) -| pane.notices.span(), - .rgba = &state.blank, - .iw = 1, - .ih = 1, + .raster = .of(&state.blank, 1, 1), + .src = .{ .w = 1, .h = 1 }, + .dst = body, + .at = .{ .x = rows.base.dst.x, .y = rows.dst_y, .w = rows.base.dst.w, .h = rows.dst_h }, + .key = pardes.Placement.keyOf(pane.serial, @intCast(page), 0), })) break; placed_any = true; continue; }; const slot = rasterForPage(state, page).?; - const patch: ?pardes.ImagePatch = if (slot.patch_from != 0 and + const dirty: ?pardes.Placement.Dirty = if (slot.patch_from != 0 and slot.patch_rows.start < slot.patch_rows.end) .{ - .from = slot.patch_from, + .from = pardes.Placement.keyOf(pane.serial, @intCast(placed.page), slot.patch_from), .y = @intCast(slot.patch_rows.start - slot.band_y), .h = @intCast(slot.patch_rows.end - slot.patch_rows.start), } else null; - if (!core.appendImagePlace(.{ + if (!core.appendPlacement(.{ .pane = @intCast(pane_id), - .serial = pane.serial, - .native = .{ - .revision = placed.revision, - .page = @intCast(placed.page), - .fit = switch (placed.fit) { - .width => .width, - .height => .height, - }, - .pan_x = placed.pan_x, - .geometry = placed.geometry, - .pixel_offset_y = placed.pixel_offset_y, - .paper_bg = if (pardes.platform == .macos) - (if (key.mode == .disabled) .{ 255, 255, 255 } else key.colors.background) - else - null, - }, - .x = text_x, - // Below the notice chips, like an image: a placed page is - // drawn after the cells and would paint a chip out. - .y = core.bodyTop(pane, rect) + pane.notices.span(), - .w = text_width, - .h = (rect.h -| pane.tag_rows) -| pane.notices.span(), - .rgba = placed.rgba, - .iw = placed.width, - // The texture contains the retained band, not the full page. - .ih = placed.band_height, - .patch = patch, + // The texture is the retained band, not the full page. + .raster = .of(placed.rgba, placed.width, placed.band_height), + .src = placed.geometry.src, + .dst = body, + .at = placed.geometry.dst, + .key = pardes.Placement.keyOf(pane.serial, @intCast(placed.page), placed.revision), + .dirty = dirty, })) break; // A shell now holds (or will fetch whole) this revision. slot.patch_from = slot.revision; diff --git a/src/surface.zig b/src/surface.zig index 3716df4f..ef1679a5 100644 --- a/src/surface.zig +++ b/src/surface.zig @@ -7,12 +7,10 @@ const uucode = @import("uucode"); const vaxis = @import("vaxis"); const panes = @import("panes.zig"); const config = @import("config.zig"); -const pdf_enabled = panes.Pdf.enabled; const image = @import("image.zig"); const MAX_PANES = pardes.MAX_PANES; const Layer = @import("Layer.zig"); const MAX_TAG_LAYERS = pardes.MAX_TAG_LAYERS; -const CellPixels = pardes.CellPixels; pub const Color = union(enum) { default, index: u8, rgb: [3]u8 }; @@ -72,123 +70,83 @@ test "cell visual equality ignores dead grapheme tail bytes" { try std.testing.expect(a.visuallyEqual(&b)); } -pub const ImageCacheKey = if (pdf_enabled) struct { - serial: u32, - page: u32, - revision: u32, +/// A rectangle in pixels. +pub const RectPx = image.PixelRect; - pub fn eql(a: @This(), b: @This()) bool { - return a.serial == b.serial and a.page == b.page and - a.revision == b.revision; +/// A rectangle in the frame's cells. +pub const RectCell = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; + +/// The pixels a placement shows: RGBA, straight alpha, `stride` bytes a +/// row. They are the pane's, and stay put until its next frame. +pub const Raster = struct { + ptr: [*]const u8, + w: u32, + h: u32, + stride: u32, + + pub fn of(rgba: []const u8, w: usize, h: usize) Raster { + return .{ .ptr = rgba.ptr, .w = @intCast(w), .h = @intCast(h), .stride = @intCast(w * 4) }; } -} else struct { - serial: u32, - pub fn eql(a: @This(), b: @This()) bool { - return a.serial == b.serial; + pub fn bytes(r: Raster) []const u8 { + return r.ptr[0 .. @as(usize, r.stride) * r.h]; } }; -/// Dynamic placement exists only for native PDF pages. Static image panes need -/// only their pane identity, so feature-off builds carry a zero-bit payload. -pub const NativePlacement = if (pdf_enabled) struct { - revision: u32 = 0, - page: u32 = 0, - fit: image.NativeFit = .contain, - pan_x: u16 = 0, - pan_y: u16 = 0, - /// Continuous-document pages supply their already-clipped geometry. - /// Static images and legacy callers leave this null and derive fit/pan. - geometry: ?image.NativeGeometry = null, - /// Subpixel vertical displacement retained from proportional SDL wheel dy. - pixel_offset_y: f32 = 0, - /// Separate paper behind straight-alpha PDF content; null for other images. - paper_bg: ?[3]u8 = null, - /// `geometry` is the whole of it: `src` drawn into `dst`, scaled as - /// it says. A terminal pane's kitty graphics come this way, one cell - /// row at a time, already clipped (TerminalImages.zig). - exact: bool = false, -} else struct {}; - -pub const ImagePatch = struct { from: u32, y: u32, h: u32 }; - -pub const ImagePlace = struct { +/// A picture over the frame's cells, which a frontend realises: the window +/// as a texture kept by `key`, the tty as a kitty image, or either as glyph +/// art in `dst`'s cells (Art.zig, the frontends' own) where it cannot show +/// pixels or the pane asks for art. The frame lists them a pane at a time, +/// lowest first. +pub const Placement = struct { pane: u8, - /// Pane slots are reused. This identity makes a cached GPU texture or - /// kitty image unambiguously belong to the pane which supplied the bytes. - serial: u32, - native: NativePlacement = .{}, - x: u16, - y: u16, - w: u16, - h: u16, - rgba: []const u8, - iw: usize, - ih: usize, - /// A terminal's kitty graphics stacking order (`z=`): the surface lists - /// a pane's images lowest first, so a shell drawing them in order puts - /// a higher one over a lower; below 0 the program means it under the - /// text, which the tty passes on to the outer terminal (the GUI draws - /// every image over the cells still). 0 for every other image. + raster: Raster, + /// The part of `raster` shown. + src: RectPx, + /// The cells it lies over, which clip it. + dst: RectCell, + /// Where it goes in `dst`, in pixels from `dst`'s top left corner; + /// null: the frontend fits `src` there, no larger than it is, at the + /// top left (as glyph art, centred). + at: ?RectPx = null, + /// The pixels' identity (`keyOf`): one key is one picture, so a + /// frontend keeps what it uploaded under it. + key: u64, + /// A terminal program's kitty `z=`: below 0 it is under the text, + /// which a tty passes on to the terminal outside. z: i32 = 0, - /// Rows [y, y+h) of `rgba` are the only ones that differ from the - /// same pane page at revision `from`: a shell still holding that one may - /// update just these rows instead of taking the whole image again. - patch: ?ImagePatch = null, - - pub fn cacheKey(place: ImagePlace) ImageCacheKey { - if (comptime pdf_enabled) - return .{ - .serial = place.serial, - .page = place.native.page, - .revision = place.native.revision, - }; - return .{ .serial = place.serial }; - } -}; + /// Rows of `raster` that alone differ from the picture under the key + /// `from`: a frontend still holding that one updates those rows. + dirty: ?Dirty = null, + art: Art = .{}, + + pub const Dirty = struct { from: u64, y: u32, h: u32 }; + + /// How the pane wants it drawn as glyph art: the Petscii, Palette and + /// Ascii words on an image pane, Petscii and TermImages on a terminal. + pub const Art = struct { + /// Glyph art even where pixels could be shown (Petscii on). + glyph: bool = false, + palette: Palette = .commodore, + /// Letters and signs among the glyphs, not only blocks and lines. + ascii: bool = true, + + pub const Palette = enum { commodore, terminal }; + }; -test "pixel attachment cache key follows both pane lifetime and rendered revision" { - if (comptime pdf_enabled) { - const first = ImageCacheKey{ .serial = 41, .page = 7, .revision = 3 }; - try std.testing.expect(first.eql(.{ .serial = 41, .page = 7, .revision = 3 })); - try std.testing.expect(!first.eql(.{ .serial = 41, .page = 8, .revision = 3 })); - try std.testing.expect(!first.eql(.{ .serial = 41, .page = 7, .revision = 4 })); - try std.testing.expect(!first.eql(.{ .serial = 42, .page = 7, .revision = 3 })); - } else { - const first = ImageCacheKey{ .serial = 41 }; - try std.testing.expect(first.eql(.{ .serial = 41 })); - try std.testing.expect(!first.eql(.{ .serial = 42 })); + /// A pane's picture `page` (a PDF's page, a terminal's image id; 0 for + /// an image) as of `revision`. + pub fn keyOf(serial: u32, page: u32, revision: u32) u64 { + return std.hash.Wyhash.hash(0, std.mem.asBytes(&[3]u32{ serial, page, revision })); } - try std.testing.expectEqual(pdf_enabled, @hasField(ImageCacheKey, "revision")); - try std.testing.expectEqual(pdf_enabled, @hasField(NativePlacement, "fit")); - try std.testing.expectEqual(pdf_enabled, @hasField(CellPixels, "w")); - if (comptime !pdf_enabled) { - try std.testing.expectEqual(@as(usize, 4), @sizeOf(ImageCacheKey)); - try std.testing.expectEqual(@as(usize, 0), @sizeOf(NativePlacement)); - try std.testing.expectEqual(@as(usize, 0), @sizeOf(CellPixels)); - try std.testing.expectEqual(@as(usize, 0), @sizeOf(panes.Pdf.PointerDrag)); - } -} +}; -test "pixel attachment fit and pan do not invalidate cached pixels" { - if (comptime !pdf_enabled) return; - var place = ImagePlace{ - .pane = 0, - .serial = 41, - .native = .{ .revision = 3 }, - .x = 0, - .y = 0, - .w = 1, - .h = 1, - .rgba = &.{ 0, 0, 0, 255 }, - .iw = 1, - .ih = 1, - }; - const key = place.cacheKey(); - place.native.fit = .width; - place.native.pan_x = 1234; - place.native.pan_y = 65535; - try std.testing.expect(key.eql(place.cacheKey())); +test "a placement's key follows its pane, its page and the revision of its pixels, and nothing of where it goes" { + const first = Placement.keyOf(41, 7, 3); + try std.testing.expectEqual(first, Placement.keyOf(41, 7, 3)); + try std.testing.expect(first != Placement.keyOf(41, 8, 3)); + try std.testing.expect(first != Placement.keyOf(41, 7, 4)); + try std.testing.expect(first != Placement.keyOf(42, 7, 3)); } /// One part of the frame and where it is, placed once by `draw.place` @@ -250,6 +208,10 @@ pub const Region = struct { /// 0 for a terminal or a picture), which a frontend's glide follows. line: i32 = 0, doc: u64 = 0, + /// A body drawn as a strip of pictures (a PDF): how far down the strip + /// it shows, in pixels. Its placements are laid at the whole pixel; a + /// pixel frontend draws them the fraction higher. + scroll_px: f64 = 0, /// Cells the column-move rail takes off the right edge while it runs /// there: layers and pixel shells stop short of them, the grid does not. clip: u16 = 0, @@ -322,7 +284,7 @@ pub const Surface = struct { cells: []Cell = &.{}, /// bar: draw an insert-style thin cursor instead of the block cursor: ?struct { x: u16, y: u16, bar: bool = false } = null, - images: std.ArrayList(ImagePlace) = .empty, + placements: std.ArrayList(Placement) = .empty, pub fn tagLayers(s: *const Surface) []const Layer { return &s.tag_layers; diff --git a/src/terminal.zig b/src/terminal.zig index 6f9e2071..a285d1f8 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -3142,7 +3142,7 @@ pub fn drawImages(p: *Pardes, s: *Surface, pane: *Pane, id: usize, tx: u16, tw: } // Nothing to draw, no placeholder to blank and nothing cached to let // go: the common case costs the rows' flags. - if (!any_placeholder and screen.kitty_images.placements.count() == 0 and state.images.converted.count() == 0 and state.images.glyphs.count() == 0) return; + if (!any_placeholder and screen.kitty_images.placements.count() == 0 and state.images.converted.count() == 0) return; state.images.draw(p, s, &state.vt, @intCast(id), pane.serial, tx, body_y, @min(tw, pane.cols), rows_of[0..vp_rows]); } @@ -3210,8 +3210,8 @@ inline fn kittyTransmit(comptime extra: []const u8) []const u8 { }; } -fn imagePlaces(s: *const Surface) []const pardes.ImagePlace { - return s.images.items; +fn imagePlaces(s: *const Surface) []const pardes.Placement { + return s.placements.items; } test "a terminal's kitty graphics image is drawn where its program placed it, and a delete takes it away" { @@ -3232,25 +3232,20 @@ test "a terminal's kitty graphics image is drawn where its program placed it, an try std.testing.expectEqual(@as(usize, 1), places.len); const place = places[0]; const r = p.rects[0]; - try std.testing.expect(place.native.exact); - try std.testing.expectEqual(@as(u32, 7), place.native.page); - try std.testing.expectEqual(pane.serial, place.serial); - try std.testing.expectEqual(r.x + config.GUTTER + 4, place.x); - try std.testing.expectEqual(p.bodyTop(pane, r) + 2, place.y); - try std.testing.expectEqual(@as(u16, 2), place.w); - try std.testing.expectEqual(@as(u16, 1), place.h); - try std.testing.expectEqual(@as(usize, 4), place.iw); - try std.testing.expectEqual(@as(usize, 2), place.ih); - try std.testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, place.rgba[0..4]); - const g = place.native.geometry.?; - try std.testing.expectEqual(pardes.image.PixelRect{ .x = 0, .y = 0, .w = 4, .h = 2 }, g.src); - try std.testing.expectEqual(pardes.image.PixelRect{ .x = 0, .y = 0, .w = 20, .h = 20 }, g.dst); + const img = pane.terminal.?.vt.screens.active.kitty_images.imageById(7).?; + try std.testing.expectEqual(pardes.Placement.keyOf(pane.serial, 7, @truncate(img.generation)), place.key); + try std.testing.expectEqual(pardes.RectCell{ .x = r.x + config.GUTTER + 4, .y = p.bodyTop(pane, r) + 2, .w = 2, .h = 1 }, place.dst); + try std.testing.expectEqual(@as(u32, 4), place.raster.w); + try std.testing.expectEqual(@as(u32, 2), place.raster.h); + try std.testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, place.raster.bytes()[0..4]); + try std.testing.expectEqual(pardes.RectPx{ .x = 0, .y = 0, .w = 4, .h = 2 }, place.src); + try std.testing.expectEqual(pardes.RectPx{ .x = 0, .y = 0, .w = 20, .h = 20 }, place.at.?); // Scrolled up by the shell's output, it goes up with the text. p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[24;1H\n\n" } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); - try std.testing.expectEqual(p.bodyTop(pane, r), imagePlaces(s)[0].y); + try std.testing.expectEqual(p.bodyTop(pane, r), imagePlaces(s)[0].dst.y); p.update(.{ .output = .{ .pane = 0, .bytes = "\n" } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); @@ -3362,15 +3357,13 @@ test "a virtual kitty placement shows through its placeholder cells, which draw const r = p.rects[0]; const tx = r.x + config.GUTTER; const y = p.bodyTop(pane, r) + 1; - try std.testing.expectEqual(tx + 2, places[0].x); - try std.testing.expectEqual(y, places[0].y); - try std.testing.expectEqual(@as(u16, 2), places[0].w); + try std.testing.expectEqual(pardes.RectCell{ .x = tx + 2, .y = y, .w = 2, .h = 1 }, places[0].dst); try std.testing.expectEqualStrings(" ", s.at(tx + 2, y).grapheme()); try std.testing.expectEqualStrings(" ", s.at(tx + 3, y).grapheme()); try std.testing.expectEqualStrings("X", s.at(tx + 4, y).grapheme()); } -test "without pixels, or by the Petscii builtin, a terminal's image is glyph art in its cells" { +test "a terminal's image is a placement whatever the frontend draws, and the Petscii builtin asks for it as glyph art" { if (comptime !images_enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); @@ -3384,40 +3377,27 @@ test "without pixels, or by the Petscii builtin, a terminal's image is glyph art const r = p.rects[0]; const tx = r.x + config.GUTTER; const y = p.bodyTop(pane, r); - const commodore = pardes.image.GlyphArt.commodore; - const painted = struct { - fn check(cell: *const pardes.Cell) bool { - const bg = switch (cell.style.bg) { - .rgb => |rgb| rgb, - else => return false, - }; - for (commodore) |c| if (std.mem.eql(u8, &c, &bg)) return true; - return false; - } - }.check; - // A shell that draws no pixels (a tty under no kitty graphics): forced. + // A frontend that draws no pixels (a tty under no kitty graphics) is + // handed the picture all the same, and the cells under it are its. p.frontend.native_images = false; var s = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); - try std.testing.expect(painted(s.at(tx, y)) and painted(s.at(tx + 1, y))); - try std.testing.expect(!painted(s.at(tx + 2, y))); - // The tag says what is drawn, glyph art, as an image pane's does. + try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); + try std.testing.expect(!imagePlaces(s)[0].art.glyph); + try std.testing.expectEqualStrings(" ", s.at(tx, y).grapheme()); + try std.testing.expectEqualStrings(" ", s.at(tx + 1, y).grapheme()); + // The tag says what that frontend draws, glyph art, as an image pane's does. try std.testing.expect(std.mem.indexOf(u8, try tagline.tagPrefix(p, pane), " petscii:on") != null); - // Pixels where they can be drawn; the builtin flips the pane to glyphs - // and back. + // The builtin flips the pane to glyph art and back. p.frontend.native_images = true; - _ = frame.reset(.retain_capacity); - s = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); try std.testing.expect(p.executeBuiltinLine(0, "Petscii")); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); - try std.testing.expect(painted(s.at(tx, y))); + try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); + try std.testing.expect(imagePlaces(s)[0].art.glyph); try std.testing.expect(std.mem.indexOf(u8, try tagline.tagPrefix(p, pane), " petscii:on") != null); - // Dumped and restored, it is still glyph art. + // Dumped and restored, it still asks for glyph art. try pardes.dump.dumpState(p); const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); defer restored.deinit(); @@ -3425,7 +3405,7 @@ test "without pixels, or by the Petscii builtin, a terminal's image is glyph art try std.testing.expect(p.executeBuiltinLine(0, "Petscii")); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); + try std.testing.expect(!imagePlaces(s)[0].art.glyph); } test "TermImages is real by default, and petscii makes new terminals draw glyph art" { diff --git a/src/tui/tui.zig b/src/tui/tui.zig index e8a5c44c..68b97ba2 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -6,6 +6,7 @@ const libc = std.c; const vaxis = @import("vaxis"); const pardes = @import("../pardes.zig"); const JumpTrail = @import("../jump_trail.zig"); +const Art = @import("../Art.zig"); const vaxis_input = @import("../vaxis_input.zig"); const tracy = @import("../tracy.zig"); const look = @import("../look.zig"); @@ -774,21 +775,20 @@ fn terminalSize(cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) @FieldType(par }; } -/// A terminal pane's image fragment (`native.exact`): its `src` into its -/// `dst`, inside one cell row. Kitty scales a source rectangle to whole +/// A terminal pane's image fragment (one cell row): its `src` into its +/// `at`, inside that row. Kitty scales a source rectangle to whole /// cells, so the source is trimmed to the cells' scale rather than the /// picture stretched: a fragment loses under a cell at its right edge. /// A full row is declared one row high; a part of one (an image's top or /// bottom edge) only its columns, its height then following the source. fn exactKittyPlacement( - place: pardes.ImagePlace, + place: pardes.Placement, screen_cols: u16, screen_rows: u16, screen_pixel_w: u16, screen_pixel_h: u16, ) ?KittyPlacement { - if (comptime !pardes.pdf_enabled) return null; - const g = place.native.geometry orelse return null; + const g: pardes.image.NativeGeometry = .{ .src = place.src, .dst = place.at orelse return null }; const cell_w = terminalCellPixels(screen_pixel_w, screen_cols, 8); const cell_h = terminalCellPixels(screen_pixel_h, screen_rows, 16); if (g.dst.w == 0 or g.dst.h == 0 or g.src.w == 0 or g.src.h == 0) return null; @@ -796,7 +796,7 @@ fn exactKittyPlacement( const pixel_x = g.dst.x % cell_w; const pixel_y = @min(g.dst.y, cell_h - 1); const cols: u32 = @max(1, (g.dst.w + cell_w / 2) / cell_w); - if (cell_x >= place.w) return null; + if (cell_x >= place.dst.w) return null; const declared_w = cols * cell_w; // At the cells' scale, the source that fills `cols` cells. var src_w = g.src.w; @@ -813,7 +813,7 @@ fn exactKittyPlacement( return .{ .cell_x = std.math.cast(u16, cell_x) orelse return null, .cell_y = 0, - .cell_cols = std.math.cast(u16, @min(@as(u32, place.w) - cell_x, (pixel_x + declared_w + cell_w - 1) / cell_w)) orelse return null, + .cell_cols = std.math.cast(u16, @min(@as(u32, place.dst.w) - cell_x, (pixel_x + declared_w + cell_w - 1) / cell_w)) orelse return null, .cell_rows = 1, .options = .{ .clip_region = .{ @@ -833,28 +833,20 @@ fn exactKittyPlacement( }; } +/// A PDF's page over its body: declared to kitty by its columns where it +/// is as wide as the body (fit to width), else by its rows, and its source +/// cut so the declared cells never run past its clipped bottom. fn kittyPlacement( - place: pardes.ImagePlace, + place: pardes.Placement, screen_cols: u16, screen_rows: u16, screen_pixel_w: u16, screen_pixel_h: u16, ) ?KittyPlacement { - if (comptime !pardes.pdf_enabled) return null; - if (place.native.fit == .contain) return null; const cell_w = terminalCellPixels(screen_pixel_w, screen_cols, 8); const cell_h = terminalCellPixels(screen_pixel_h, screen_rows, 16); - const body_w = std.math.mul(u32, place.w, cell_w) catch return null; - const body_h = std.math.mul(u32, place.h, cell_h) catch return null; - const geometry = place.native.geometry orelse pardes.image.nativeGeometry( - place.iw, - place.ih, - body_w, - body_h, - place.native.fit, - place.native.pan_x, - place.native.pan_y, - ) orelse return null; + const geometry: pardes.image.NativeGeometry = .{ .src = place.src, .dst = place.at orelse return null }; + const fit: enum { width, height } = if (geometry.dst.w >= @as(u32, place.dst.w) * cell_w) .width else .height; const safe_rows_u32 = geometry.dst.h / cell_h; if (safe_rows_u32 == 0) return null; @@ -862,7 +854,7 @@ fn kittyPlacement( var safe_src_h: u32 = geometry.src.h; var declared_cols: u32 = 0; var declared_rows: u32 = 0; - switch (place.native.fit) { + switch (fit) { .width => { declared_cols = @intCast(@max( @as(u64, 1), @@ -896,7 +888,6 @@ fn kittyPlacement( (@as(u64, aspect_pixel_w) + cell_w - 1) / cell_w, ); }, - .contain => unreachable, } if (declared_rows == 0 or declared_rows > safe_rows_u32) return null; @@ -906,15 +897,15 @@ fn kittyPlacement( const src_h = std.math.cast(u16, safe_src_h) orelse return null; const cell_x = std.math.cast(u16, geometry.dst.x / cell_w) orelse return null; const cell_y = std.math.cast(u16, geometry.dst.y / cell_h) orelse return null; - if (cell_x >= place.w or cell_y >= place.h) return null; + if (cell_x >= place.dst.w or cell_y >= place.dst.h) return null; const pixel_x = std.math.cast(u16, geometry.dst.x % cell_w) orelse return null; const pixel_y = std.math.cast(u16, geometry.dst.y % cell_h) orelse return null; const cell_cols = std.math.cast(u16, @min( - @as(u32, place.w - cell_x), + @as(u32, place.dst.w - cell_x), (@as(u64, pixel_x) + @as(u64, declared_cols) * cell_w + cell_w - 1) / cell_w, )) orelse return null; const cell_rows = std.math.cast(u16, @min( - @as(u32, place.h - cell_y), + @as(u32, place.dst.h - cell_y), (@as(u64, pixel_y) + declared_rows * cell_h + cell_h - 1) / cell_h, )) orelse return null; if (cell_cols == 0 or cell_rows == 0) return null; @@ -927,31 +918,22 @@ fn kittyPlacement( .options = .{ .clip_region = .{ .x = src_x, .y = src_y, .width = src_w, .height = src_h }, .pixel_offset = if (pixel_x != 0 or pixel_y != 0) .{ .x = pixel_x, .y = pixel_y } else null, - .size = switch (place.native.fit) { + .size = switch (fit) { .width => .{ .cols = std.math.cast(u16, declared_cols) orelse return null }, .height => .{ .rows = std.math.cast(u16, declared_rows) orelse return null }, - .contain => unreachable, }, }, }; } test "a terminal's image fragment goes to kitty cropped to its cells' scale, a full row one row high" { - if (comptime !pardes.pdf_enabled) return; - var place = pardes.ImagePlace{ + var place = pardes.Placement{ .pane = 0, - .serial = 3, - .native = .{ .page = 7, .exact = true, .geometry = .{ - .src = .{ .x = 0, .y = 40, .w = 240, .h = 20 }, - .dst = .{ .x = 0, .y = 0, .w = 240, .h = 20 }, - } }, - .x = 99, - .y = 4, - .w = 24, - .h = 1, - .rgba = &.{}, - .iw = 240, - .ih = 160, + .raster = .{ .ptr = undefined, .w = 240, .h = 160, .stride = 960 }, + .src = .{ .x = 0, .y = 40, .w = 240, .h = 20 }, + .dst = .{ .x = 99, .y = 4, .w = 24, .h = 1 }, + .at = .{ .x = 0, .y = 0, .w = 240, .h = 20 }, + .key = pardes.Placement.keyOf(3, 7, 0), }; // 10x20 pixel cells: a whole row, one to one. const full = exactKittyPlacement(place, 100, 30, 1000, 600).?; @@ -966,10 +948,8 @@ test "a terminal's image fragment goes to kitty cropped to its cells' scale, a f // tall, 35 pixels in, 205 wide, which rounds to 21 cells. Its columns // only, its source no taller than the 13 pixels left in the row at // that scale. - place.native.geometry = .{ - .src = .{ .x = 0, .y = 0, .w = 205, .h = 13 }, - .dst = .{ .x = 35, .y = 7, .w = 205, .h = 13 }, - }; + place.src = .{ .x = 0, .y = 0, .w = 205, .h = 13 }; + place.at = .{ .x = 35, .y = 7, .w = 205, .h = 13 }; const top = exactKittyPlacement(place, 100, 30, 1000, 600).?; try std.testing.expectEqual(@as(u16, 3), top.cell_x); try std.testing.expectEqual(@as(u16, 5), top.options.pixel_offset.?.x); @@ -982,37 +962,22 @@ test "a terminal's image fragment goes to kitty cropped to its cells' scale, a f try std.testing.expect(h > 0 and @as(u32, h) * 210 / w <= 13); // Narrower than its cells round to: the source is cut to their scale. - place.native.geometry = .{ - .src = .{ .x = 0, .y = 0, .w = 400, .h = 40 }, - .dst = .{ .x = 0, .y = 0, .w = 204, .h = 20 }, - }; + place.src = .{ .x = 0, .y = 0, .w = 400, .h = 40 }; + place.at = .{ .x = 0, .y = 0, .w = 204, .h = 20 }; const cut = exactKittyPlacement(place, 100, 30, 1000, 600).?; try std.testing.expectEqual(@as(?u16, 20), cut.options.size.?.cols); try std.testing.expectEqual(@as(?u16, 392), cut.options.clip_region.?.width); } test "Kitty PDF fragments never declare pixels beyond their clipped bottom" { - if (comptime !pardes.pdf_enabled) return; - - const base = pardes.ImagePlace{ + // As wide as the body: fit to width. + const base = pardes.Placement{ .pane = 0, - .serial = 1, - .native = .{ - .revision = 1, - .page = 0, - .fit = .width, - .geometry = .{ - .src = .{ .w = 96, .h = 10 }, - .dst = .{ .y = 195, .w = 304, .h = 29 }, - }, - }, - .x = 0, - .y = 0, - .w = 38, - .h = 14, - .rgba = &.{}, - .iw = 96, - .ih = 64, + .raster = .{ .ptr = undefined, .w = 96, .h = 64, .stride = 384 }, + .src = .{ .w = 96, .h = 10 }, + .dst = .{ .x = 0, .y = 0, .w = 38, .h = 14 }, + .at = .{ .y = 195, .w = 304, .h = 29 }, + .key = pardes.Placement.keyOf(1, 0, 1), }; const width = kittyPlacement(base, 80, 16, 640, 256) orelse return error.MissingSafeKittyWidthFragment; @@ -1024,21 +989,62 @@ test "Kitty PDF fragments never declare pixels beyond their clipped bottom" { const aspect_pixels = (@as(u32, width_size.cols.?) * 8 * width_clip.height.? + width_clip.width.? - 1) / width_clip.width.?; const inferred_rows = (aspect_pixels + 15) / 16; - try std.testing.expect(inferred_rows * 16 <= base.native.geometry.?.dst.h); + try std.testing.expect(inferred_rows * 16 <= base.at.?.h); + // Narrower than the body: fit to height. var height = base; - height.native.fit = .height; - height.native.geometry.?.src.h = 9; + height.at.?.w = 300; + height.src.h = 9; const height_fragment = kittyPlacement(height, 80, 16, 640, 256) orelse return error.MissingSafeKittyHeightFragment; try std.testing.expectEqual(@as(u16, 1), height_fragment.options.size.?.rows.?); try std.testing.expect(@as(u32, height_fragment.options.size.?.rows.?) * 16 <= - height.native.geometry.?.dst.h); + height.at.?.h); - height.native.geometry.?.dst.h = 13; + height.at.?.h = 13; try std.testing.expect(kittyPlacement(height, 80, 16, 640, 256) == null); } +test "on a terminal with no kitty graphics a program's 64x64 image over 8x4 cells is glyph art in every cell, and pixels stay put where it has them" { + if (comptime !pardes.pdf_enabled) return; + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .cols = 120, .rows = 40, .shells = 1 }); + defer core.deinit(); + core.update(.{ .resize = .{ .cols = 120, .rows = 40, .cell_pixels = .{ .w = 8, .h = 16 } } }); + const pane = core.panes[0].?; + pane.body.mode = .tty; + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const a = frame.allocator(); + const px = try a.alloc(u8, 64 * 64 * 3); + for (0..64 * 64) |i| px[i * 3 ..][0..3].* = .{ 13, 77, 231 }; + const enc = try a.alloc(u8, std.base64.standard.Encoder.calcSize(px.len)); + _ = std.base64.standard.Encoder.encode(enc, px); + core.update(.{ .output = .{ .pane = 0, .bytes = try std.fmt.allocPrint(a, "\x1b[H\x1b[2J\r\nANCHOR>\x1b_Ga=T,i=42,f=24,s=64,v=64,c=8,r=4;{s}\x1b\\\r\n", .{enc}) } }); + while (core.nextEffect()) |_| {} + const s = try core.render(a); + try std.testing.expectEqual(@as(usize, 4), s.placements.items.len); + var art: Art = .{}; + defer art.deinit(gpa); + try std.testing.expectEqual(@as(*const pardes.Surface, s), try art.frame(gpa, a, s, true, 16)); + const shown = try art.frame(gpa, a, s, false, 16); + const r = core.rects[0]; + const x0 = r.x + pardes.config.GUTTER + 7; + const y0 = core.bodyTop(pane, r) + 1; + var painted: usize = 0; + for (0..4) |dy| for (0..8) |dx| { + const cell = @constCast(shown).at(x0 + @as(u16, @intCast(dx)), y0 + @as(u16, @intCast(dy))); + const bg = switch (cell.style.bg) { + .rgb => |rgb| rgb, + else => continue, + }; + for (Art.commodore) |c| { + if (std.mem.eql(u8, &c, &bg)) painted += 1; + } + }; + try std.testing.expectEqual(@as(usize, 32), painted); +} + test "host watch closes initial race and reloads rename-over PDF while idle" { if (comptime !file_watch.supported or !pardes.pdf_enabled) return; const io = std.testing.io; @@ -1094,19 +1100,20 @@ test "host watch closes initial race and reloads rename-over PDF while idle" { var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const surface = try core.render(frame.allocator()); - try std.testing.expect(surface.images.items.len > 0); - try std.testing.expectEqual(@as(u32, 0), surface.images.items[0].native.page); + try std.testing.expect(surface.placements.items.len > 0); + const shown = surface.placements.items[0]; + try std.testing.expectEqual(pardes.Placement.keyOf(pane.serial, 0, pardes.panes.Pdf.rasterForPage(pane.pdf.?, 0).?.revision), shown.key); } -fn kittyImageRepresentable(place: pardes.ImagePlace) bool { - return place.iw > 0 and place.ih > 0 and - place.iw <= std.math.maxInt(u16) and place.ih <= std.math.maxInt(u16); +fn kittyImageRepresentable(place: pardes.Placement) bool { + return place.raster.w > 0 and place.raster.h > 0 and + place.raster.w <= std.math.maxInt(u16) and place.raster.h <= std.math.maxInt(u16); } -fn surfaceHasKittyKey(surface: *const pardes.Surface, key: pardes.ImageCacheKey) bool { - for (surface.images.items) |place| { - if (comptime pardes.pdf_enabled) if (!kittyImageRepresentable(place)) continue; - if (place.cacheKey().eql(key)) return true; +fn surfaceHasKittyKey(surface: *const pardes.Surface, key: u64) bool { + for (surface.placements.items) |place| { + if (place.art.glyph or !kittyImageRepresentable(place)) continue; + if (place.key == key) return true; } return false; } @@ -1265,6 +1272,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se try screen.enter(try vaxis.Tty.init(io, &tty_buf)); defer screen.leave(); defer screen.glide.deinit(gpa); + defer screen.art.deinit(gpa); var paste_buf: std.Io.Writer.Allocating = .init(gpa); defer paste_buf.deinit(); var loop: Loop = .init(io, &screen.tty, &screen.vx); @@ -1404,7 +1412,11 @@ const Screen = struct { killed: ?usize = null, feed: Feed = .{}, thread: ?std.Io.Future(void) = null, - kitty: std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), + /// The kitty images it holds, by placement key. + kitty: std.AutoHashMap(u64, vaxis.Image), + /// The glyph art of the pictures it shows so (all of them without + /// kitty graphics). + art: Art = .{}, caps_pending: bool = true, /// Its size as last taken (`Shell.resized`). size: ?vaxis.Winsize = null, @@ -1511,6 +1523,7 @@ const Screen = struct { fn destroy(sc: *Screen) void { sc.glide.deinit(sc.gpa); + sc.art.deinit(sc.gpa); sc.kitty.deinit(); sc.gpa.free(sc.buf); sc.gpa.destroy(sc); @@ -1619,6 +1632,7 @@ fn localSession( pardes.lsp.setStatusSink(&sh, lspStatusSink); host_io.setExitWake(loop, wakeForExit); defer screen.glide.deinit(gpa); + defer screen.art.deinit(gpa); // The terminals of the other views end with the session: each put back, // its stub told. defer for (&sh.terms) |*slot| if (slot.*) |sc| if (!sc.primary) { @@ -1770,7 +1784,7 @@ fn localSession( } fn clearNativeImages( - kitty_handles: *std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), + kitty_handles: *std.AutoHashMap(u64, vaxis.Image), vx: *vaxis.Vaxis, tty: *vaxis.Tty, ) void { @@ -2622,21 +2636,24 @@ const Shell = struct { const win = vx.window(); // The tty's own motions, on its own clock, from the frame alone. const now: u64 = s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); - sc.glide.frame(s.gpa, canonical, now, null, null); + // The pictures it shows as glyph art, in a copy of the frame: all + // of them where the terminal has no kitty graphics. + const cell_h = terminalCellPixels(vx.screen.height_pix, vx.screen.height, 16); + const arted = sc.art.frame(s.gpa, s.frame.allocator(), canonical, vx.caps.kitty_graphics, cell_h) catch canonical; + sc.glide.frame(s.gpa, arted, now, null, null); s.core.frames.scroll = @round(sc.glide.follow.offset); - const glided = glideRows(s.frame.allocator(), canonical, &sc.glide) catch canonical; + const glided = glideRows(s.frame.allocator(), arted, &sc.glide) catch arted; const truecolor = vx.caps.rgb or colortermTruecolor(); const trail_on = s.core.settings.jump_trail and truecolor; const shown = trailCells(s.frame.allocator(), glided, &sc.trail, trail_on, now) catch glided; paintCells(win, shown.cells, shown.cols, shown.rows); if (s.remote) |c| if (c.link.notice_len > 0) noticeLine(win, c.link.notice(), shown.cols, shown.rows); tz_cells.end(); - for (canonical.images.items) |place| { - if (comptime pardes.pdf_enabled) { - if (!kittyImageRepresentable(place)) continue; - } - const key = place.cacheKey(); - if (!sc.kitty.contains(key) and vx.caps.kitty_graphics and sc.feed.shm.load(.acquire)) shm: { + if (vx.caps.kitty_graphics) for (canonical.placements.items) |place| { + if (place.art.glyph or !kittyImageRepresentable(place)) continue; + const key = place.key; + const rgba = place.raster.bytes(); + if (!sc.kitty.contains(key) and sc.feed.shm.load(.acquire)) shm: { var buf: [64]u8 = undefined; kitty_shm_serial +%= 1; // Bounded: a segment this many transmissions old was never @@ -2647,61 +2664,51 @@ const Shell = struct { if (kittyShmName(&old_buf, kitty_shm_serial - kitty_shm_window)) |old| _ = libc.shm_unlink(old) else |_| {} } const name = kittyShmName(&buf, kitty_shm_serial) catch break :shm; - if (!kittyShmWrite(name, place.rgba)) break :shm; - if (vx.transmitLocalImagePath(s.gpa, sc.tty.writer(), name, @intCast(place.iw), @intCast(place.ih), .shared_mem, .rgba) catch null) |handle| + if (!kittyShmWrite(name, rgba)) break :shm; + if (vx.transmitLocalImagePath(s.gpa, sc.tty.writer(), name, @intCast(place.raster.w), @intCast(place.raster.h), .shared_mem, .rgba) catch null) |handle| sc.kitty.put(key, handle) catch vx.freeImage(sc.tty.writer(), handle.id) else _ = libc.shm_unlink(name); } - if (!sc.kitty.contains(key) and vx.caps.kitty_graphics) { + if (!sc.kitty.contains(key)) { const enc = std.base64.standard.Encoder; - if (s.gpa.alloc(u8, enc.calcSize(place.rgba.len))) |b64| { + if (s.gpa.alloc(u8, enc.calcSize(rgba.len))) |b64| { defer s.gpa.free(b64); - _ = enc.encode(b64, place.rgba); - if (vx.transmitPreEncodedImage(sc.tty.writer(), b64, @intCast(place.iw), @intCast(place.ih), .rgba) catch null) |handle| + _ = enc.encode(b64, rgba); + if (vx.transmitPreEncodedImage(sc.tty.writer(), b64, @intCast(place.raster.w), @intCast(place.raster.h), .rgba) catch null) |handle| sc.kitty.put(key, handle) catch vx.freeImage(sc.tty.writer(), handle.id); } else |_| {} } - if (sc.kitty.get(key)) |cached| { - if (comptime !pardes.pdf_enabled) { - const child = win.child(.{ .x_off = place.x, .y_off = place.y, .width = place.w, .height = place.h }); - cached.draw(child, .{ .scale = .contain }) catch {}; - } else { - if (place.native.exact) { - if (exactKittyPlacement(place, vx.screen.width, vx.screen.height, vx.screen.width_pix, vx.screen.height_pix)) |placement| { - // The cell keeps its own glyph and colours - // (Image.draw would reset them): a fragment - // short of the cell shows the pane under it. - const x = place.x + placement.cell_x; - if (win.readCell(x, place.y)) |under| { - var cell = under; - cell.image = .{ .img_id = cached.id, .options = placement.options }; - win.writeCell(x, place.y, cell); - } - } - } else if (place.native.fit == .contain) { - const child = win.child(.{ .x_off = place.x, .y_off = place.y, .width = place.w, .height = place.h }); - cached.draw(child, .{ .scale = .contain }) catch {}; - } else if (kittyPlacement( - place, - vx.screen.width, - vx.screen.height, - vx.screen.width_pix, - vx.screen.height_pix, - )) |placement| { - const child = win.child(.{ - .x_off = @as(i17, place.x) + placement.cell_x, - .y_off = @as(i17, place.y) + placement.cell_y, - .width = placement.cell_cols, - .height = placement.cell_rows, - }); - cached.draw(child, placement.options) catch {}; + const cached = sc.kitty.get(key) orelse continue; + const d = place.dst; + if (place.at == null) { + // Fitted: kitty's own contain, at the top left. + const child = win.child(.{ .x_off = d.x, .y_off = d.y, .width = d.w, .height = d.h }); + cached.draw(child, .{ .scale = .contain }) catch {}; + } else if (d.h == 1) { + if (exactKittyPlacement(place, vx.screen.width, vx.screen.height, vx.screen.width_pix, vx.screen.height_pix)) |placement| { + // The cell keeps its own glyph and colours + // (Image.draw would reset them): a fragment + // short of the cell shows the pane under it. + const x = d.x + placement.cell_x; + if (win.readCell(x, d.y)) |under| { + var cell = under; + cell.image = .{ .img_id = cached.id, .options = placement.options }; + win.writeCell(x, d.y, cell); } } + } else if (kittyPlacement(place, vx.screen.width, vx.screen.height, vx.screen.width_pix, vx.screen.height_pix)) |placement| { + const child = win.child(.{ + .x_off = @as(i17, d.x) + placement.cell_x, + .y_off = @as(i17, d.y) + placement.cell_y, + .width = placement.cell_cols, + .height = placement.cell_rows, + }); + cached.draw(child, placement.options) catch {}; } - } + }; while (true) { - var stale: [pardes.MAX_PANES]pardes.ImageCacheKey = undefined; + var stale: [pardes.MAX_PANES]u64 = undefined; var stale_len: usize = 0; var image_iterator = sc.kitty.iterator(); while (image_iterator.next()) |entry| { -- cgit v1.3