diff options
| -rw-r--r-- | docs/render-pipeline.md | 18 | ||||
| -rw-r--r-- | docs/typ/reference.typ | 12 | ||||
| -rw-r--r-- | src/Art.zig | 618 | ||||
| -rw-r--r-- | src/Core.zig | 8 | ||||
| -rw-r--r-- | src/TerminalImages.zig | 99 | ||||
| -rw-r--r-- | src/draw.zig | 4 | ||||
| -rw-r--r-- | src/gui/gui.zig | 196 | ||||
| -rw-r--r-- | src/image.zig | 567 | ||||
| -rw-r--r-- | src/macos.zig | 78 | ||||
| -rw-r--r-- | src/ninep/screen.zig | 23 | ||||
| -rw-r--r-- | src/pardes.zig | 8 | ||||
| -rw-r--r-- | src/pdf_view.zig | 78 | ||||
| -rw-r--r-- | src/surface.zig | 178 | ||||
| -rw-r--r-- | src/terminal.zig | 72 | ||||
| -rw-r--r-- | src/tui/tui.zig | 279 | ||||
| -rw-r--r-- | test/kitty.zig | 127 | ||||
| -rw-r--r-- | test/pdf.zig | 279 | ||||
| -rw-r--r-- | test/pdf_pointer_bench.zig | 10 | ||||
| -rw-r--r-- | test/pdf_scroll_bench.zig | 70 |
19 files changed, 1384 insertions, 1340 deletions
diff --git a/docs/render-pipeline.md b/docs/render-pipeline.md index 49180ad3..64fa4e65 100644 --- a/docs/render-pipeline.md +++ b/docs/render-pipeline.md @@ -23,8 +23,21 @@ frontend keeps the little it draws on its own. the snapshot goldens, the tty), the regions (each part's rectangle, owner and focus; a body's first line and the text it shows, `Region.line` and `Region.doc`), the layers a pixel shell draws at the - tagline's pitch and with compact context rows, the images, the cursor - and the palette (`Chrome`). Nothing on it moves. + tagline's pitch and with compact context rows, the placements, the + cursor and the palette (`Chrome`). Nothing on it moves. +- A placement (surface.zig's `Placement`) is a picture over the cells, a + pane's at a time and lowest first: its pixels (`raster`: RGBA, straight + alpha), the part shown (`src`), the cells it lies over and is clipped to + (`dst`), where in them in pixels (`at`; none for an image pane, which + the frontend fits there), a `key` that names those pixels (pane serial, + page, revision) so a frontend keeps what it uploaded under it, a + terminal program's kitty `z`, the rows that alone changed since an + older key (`dirty`, a PDF's highlight), and how the pane wants it as + glyph art (`art`: Petscii, Palette, Ascii). A PDF's body region carries + its strip's scroll (`scroll_px`): its pictures lie at the whole pixel + and a pixel frontend draws them the fraction higher. The core never + draws a picture in the cells, so `/screen` (whose `images` lists the + placements) is the same on every client. ## The frontends' own @@ -40,6 +53,7 @@ frames itself while they move (the last frame again: the tty's | a jump's trail (`JumpTrail`, jump_trail.zig) | the ground behind the cursor, truecolor terminals | drawn over it, in the overlay | the cursor between two frames | | `InactiveDim` | (kept, not drawn) | the unfocused panes' ink faded, never below its contrast floor | the regions' focus | | `Pet` | (unsupported) | a sprite in the workspace tag's blank end (gui/pet.zig) | the grid's first row; SDL's ticks | +| a placement | a kitty image placed over its cells, or glyph art in them (Art.zig) without kitty graphics or when its pane asks | a texture by key, scissored to its cells, or glyph art when its pane asks | the frame's placements | The settings these read are plain bytes in the core's settings, dumped and replicated as any other; nothing a frontend keeps goes back to the diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index cb2df2a8..b515e1b2 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -572,7 +572,9 @@ drawn: glyph art (`on`) or pixels (`off`). With no graphics it is always colours the glyph art uses, the C64's (`commodore`) or the terminal theme's own 16 (#word("Palette") toggles it); `ascii` is whether the printable ASCII bitmaps join the glyph set the matcher picks from -(#word("Ascii") toggles it). #word("Palette") and ascii only change the glyph art. +(#word("Ascii") toggles it). #word("Palette") and ascii only change the glyph art, +which the frontend draws: a tui without graphics always, a window when +#word("Petscii") is on. #word("Petscii") and #word("Ascii") (`on|off`) and #word("Palette") (`commodore|terminal`) take the value their tag word shows and set it, as a PDF's #word("PdfFit") (`width|height`) and #word("PdfTint") @@ -931,7 +933,13 @@ at 200, ending in `…`. Control characters become spaces. row 5. Cells are #file("/screen")'s. - #file("/screen"): JSON `cols`, `rows`, `cursor`, `styles`, and row-major `cells` of `[grapheme, style_index]`; one frame per open. Compare a - cell's style through `styles`, not the index. + cell's style through `styles`, not the index. A frame with pictures + (an image, a PDF's pages, a terminal program's kitty images) has + `images` too, a `{serial, rect, src, key, petscii}` each: the pane, the + cells it lies over (`[x, y, w, h]`), the part of its pixels shown, a + key naming those pixels, and whether its pane asks for glyph art. The + cells under a picture are the pane's ground: the glyph art a terminal + without graphics shows there is drawn by the frontend, not the core. - #file("/commands"): `Word [arg] root|pane|both [values] -- sentence`, one a line, generated from the builtin registry (`both`: #word("Edit"), at the active pane from the root). 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, + /// 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 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 }; - } -}; + pub const Dirty = struct { from: u64, y: u32, h: u32 }; -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 })); - } - 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)); - } -} + /// 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, -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, + pub const Palette = enum { commodore, terminal }; }; - 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())); + + /// 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 })); + } +}; + +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| { diff --git a/test/kitty.zig b/test/kitty.zig index daa01060..f222f9cf 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -9,7 +9,7 @@ //! and what the emulator stores is drawn where the program put it. Each case //! feeds a pane's output the way a pty read does and checks all three: the //! bytes the program would read back, ghostty's image storage, and the -//! `ImagePlace`s a frame hands the shell (which a tty re-emits to the outer +//! placements a frame hands the shell (which a tty re-emits to the outer //! terminal and the GUI draws; test/kitty_harness.zig follows them there). //! //! A case pardes does not pass yet is a `knownGap`: its check runs, the test @@ -125,10 +125,18 @@ const Rig = struct { return &r.vt(id).screens.active.kitty_images; } - fn places(r: *Rig) ![]const pardes.ImagePlace { + fn places(r: *Rig) ![]const pardes.Placement { _ = r.frame.reset(.retain_capacity); const s = try r.p.render(r.frame.allocator()); - return s.images.items; + return s.placements.items; + } + + /// The id its program gave the image a place draws. + fn idOf(r: *Rig, place: pardes.Placement) ?u32 { + const shown = r.p.panes[place.pane] orelse return null; + var it = shown.terminal.?.vt.screens.active.kitty_images.images.iterator(); + while (it.next()) |e| if (pardes.Placement.keyOf(shown.serial, e.key_ptr.*, @truncate(e.value_ptr.generation)) == place.key) return e.key_ptr.*; + return null; } /// The top-left cell of pane `id`'s body, in screen cells. @@ -138,9 +146,9 @@ const Rig = struct { } /// The places drawn for the image a program called `image_id`. - fn placesOf(r: *Rig, image_id: u32) ![]const pardes.ImagePlace { - var out: std.ArrayList(pardes.ImagePlace) = .empty; - for (try r.places()) |place| if (place.native.page == image_id) + fn placesOf(r: *Rig, image_id: u32) ![]const pardes.Placement { + var out: std.ArrayList(pardes.Placement) = .empty; + for (try r.places()) |place| if (r.idOf(place) == image_id) try out.append(r.frame.allocator(), place); return out.items; } @@ -155,8 +163,8 @@ const Rig = struct { }; /// The pixel at `(x, y)` of a drawn place's whole image, as RGBA. -fn pixel(place: pardes.ImagePlace, x: usize, y: usize) [4]u8 { - return place.rgba[(y * place.iw + x) * 4 ..][0..4].*; +fn pixel(place: pardes.Placement, x: usize, y: usize) [4]u8 { + return place.raster.bytes()[y * place.raster.stride + x * 4 ..][0..4].*; } test "kitty: a support query is answered on the asking pane's pty and stores nothing" { @@ -221,7 +229,7 @@ test "kitty: RGB, RGBA, PNG and zlib-compressed RGB all draw the same pixels" { try std.testing.expectEqual(@as(u32, 2), img.height); const drawn = try r.placesOf(1); try std.testing.expectEqual(@as(usize, 1), drawn.len); - try std.testing.expectEqual(@as(usize, 4), drawn[0].iw); + try std.testing.expectEqual(@as(usize, 4), drawn[0].raster.w); for ([_][2]usize{ .{ 0, 0 }, .{ 1, 1 } }) |at| try std.testing.expectEqual(red, pixel(drawn[0], at[0], at[1])); for ([_][2]usize{ .{ 2, 0 }, .{ 3, 1 } }) |at| try std.testing.expectEqual(blue, pixel(drawn[0], at[0], at[1])); } @@ -361,10 +369,10 @@ test "kitty: a=T draws at the cursor and moves the cursor past the image; C=1 le // A cell row at a time: two rows. try std.testing.expectEqual(@as(usize, 2), drawn.len); const o = r.origin(0); - try std.testing.expectEqual(o.x + 4, drawn[0].x); - try std.testing.expectEqual(o.y + 2, drawn[0].y); - try std.testing.expectEqual(o.y + 3, drawn[1].y); - for (drawn) |place| try std.testing.expectEqual(@as(u16, 2), place.w); + try std.testing.expectEqual(o.x + 4, drawn[0].dst.x); + try std.testing.expectEqual(o.y + 2, drawn[0].dst.y); + try std.testing.expectEqual(o.y + 3, drawn[1].dst.y); + for (drawn) |place| try std.testing.expectEqual(@as(u16, 2), place.dst.w); } } @@ -383,14 +391,14 @@ test "kitty: a=p places one image twice by placement id, a source rect crops it, const o = r.origin(0); var drawn = try r.placesOf(7); try std.testing.expectEqual(@as(usize, 2), drawn.len); - var cropped: ?pardes.ImagePlace = null; - for (drawn) |place| if (place.y == o.y + 5) { + var cropped: ?pardes.Placement = null; + for (drawn) |place| if (place.dst.y == o.y + 5) { cropped = place; }; const crop = cropped orelse return error.CroppedPlacementMissing; - try std.testing.expectEqual(o.x + 9, crop.x); - try std.testing.expectEqual(@as(u16, 1), crop.w); - const g = crop.native.geometry.?; + try std.testing.expectEqual(o.x + 9, crop.dst.x); + try std.testing.expectEqual(@as(u16, 1), crop.dst.w); + const g: pardes.image.NativeGeometry = .{ .src = crop.src, .dst = crop.at.? }; try std.testing.expectEqual(@as(u32, 2), g.src.x); try std.testing.expectEqual(@as(u32, 2), g.src.w); // Put again under the same id, placement 1 moves; it is not doubled. @@ -400,8 +408,8 @@ test "kitty: a=p places one image twice by placement id, a source rect crops it, var at_ten = false; var at_one = false; for (drawn) |place| { - if (place.y == o.y + 9) at_ten = true; - if (place.y == o.y) at_one = true; + if (place.dst.y == o.y + 9) at_ten = true; + if (place.dst.y == o.y) at_one = true; } try std.testing.expect(at_ten and !at_one); } @@ -441,7 +449,7 @@ test "kitty: z-index travels with the image, and images are handed on lowest z f var ids: [4]u32 = undefined; for (drawn, 0..) |place, i| { zs[i] = place.z; - ids[i] = place.native.page; + ids[i] = r.idOf(place).?; } try std.testing.expectEqualSlices(i32, &.{ -7, 0, 3, 5 }, &zs); try std.testing.expectEqualSlices(u32, &.{ 9, 10, 11, 8 }, &ids); @@ -547,13 +555,13 @@ test "kitty: Unicode placeholders draw a virtual placement over two rows, with i const drawn = try r.placesOf(7); const o = r.origin(0); try std.testing.expectEqual(@as(usize, 2), drawn.len); - try std.testing.expectEqual(o.x + 2, drawn[0].x); - try std.testing.expectEqual(o.y + 1, drawn[0].y); - try std.testing.expectEqual(o.y + 2, drawn[1].y); - for (drawn) |place| try std.testing.expectEqual(@as(u16, 2), place.w); + try std.testing.expectEqual(o.x + 2, drawn[0].dst.x); + try std.testing.expectEqual(o.y + 1, drawn[0].dst.y); + try std.testing.expectEqual(o.y + 2, drawn[1].dst.y); + for (drawn) |place| try std.testing.expectEqual(@as(u16, 2), place.dst.w); // The top row shows the image's top half, the bottom its bottom half. - try std.testing.expectEqual(@as(u32, 0), drawn[0].native.geometry.?.src.y); - try std.testing.expectEqual(@as(u32, 1), drawn[1].native.geometry.?.src.y); + try std.testing.expectEqual(@as(u32, 0), drawn[0].src.y); + try std.testing.expectEqual(@as(u32, 1), drawn[1].src.y); } test "kitty: a placeholder naming its placement in the underline colour picks that placement" { @@ -566,7 +574,7 @@ test "kitty: a placeholder naming its placement in the underline colour picks th const drawn = try r.placesOf(7); try std.testing.expectEqual(@as(usize, 1), drawn.len); // Placement 2 spans two cells, so each cell is half the image. - try std.testing.expectEqual(@as(u16, 2), drawn[0].w); + try std.testing.expectEqual(@as(u16, 2), drawn[0].dst.w); } test "kitty: an image follows its text when the shell scrolls, resizes and reflows" { @@ -574,10 +582,10 @@ test "kitty: an image follows its text when the shell scrolls, resizes and reflo defer r.deinit(); r.feed(0, "\x1b[5;1Hmarker" ++ apc("a=T,i=10,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); const o = r.origin(0); - try std.testing.expectEqual(o.y + 4, (try r.placesOf(10))[0].y); + try std.testing.expectEqual(o.y + 4, (try r.placesOf(10))[0].dst.y); // Two lines of output at the bottom scroll it up two rows. r.feed(0, "\x1b[40;1H\n\n"); - try std.testing.expectEqual(o.y + 2, (try r.placesOf(10))[0].y); + try std.testing.expectEqual(o.y + 2, (try r.placesOf(10))[0].dst.y); // A narrower pane reflows the text; the image stays on its row. const pane = r.pane(0); terminal.resizeGrid(pane, 40, pane.rows); @@ -588,7 +596,7 @@ test "kitty: an image follows its text when the shell scrolls, resizes and reflo const drawn = try r.placesOf(10); try std.testing.expectEqual(@as(usize, 1), drawn.len); const viewport_top: usize = @intCast(terminal.gridOffset(pane)); - try std.testing.expectEqual(o.y + @as(u16, @intCast(row - viewport_top)), drawn[0].y); + try std.testing.expectEqual(o.y + @as(u16, @intCast(row - viewport_top)), drawn[0].dst.y); } test "kitty: the alternate screen has its own images, and the primary's come back after it" { @@ -777,7 +785,7 @@ test "kitty: a placement relative to another (P=, Q=) is drawn at its parent's c try std.testing.expectEqual(@as(usize, 3), drawn.len); const o = r.origin(0); var cells: [3][2]u16 = undefined; - for (drawn, 0..) |place, i| cells[i] = .{ place.x - o.x, place.y - o.y }; + for (drawn, 0..) |place, i| cells[i] = .{ place.dst.x - o.x, place.dst.y - o.y }; std.mem.sort([2]u16, &cells, {}, struct { fn lt(_: void, a: [2]u16, b: [2]u16) bool { return a[0] < b[0]; @@ -1167,8 +1175,8 @@ test "kitty: scrolled back by pardes, an image shows on its row; scrolled forwar const o = r.origin(0); const drawn = try r.placesOf(20); try std.testing.expectEqual(@as(usize, 1), drawn.len); - try std.testing.expectEqual(o.y, drawn[0].y); - try std.testing.expectEqual(o.x + 6, drawn[0].x); + try std.testing.expectEqual(o.y, drawn[0].dst.y); + try std.testing.expectEqual(o.x + 6, drawn[0].dst.x); terminal.followOutput(pane); try std.testing.expectEqual(@as(usize, 0), (try r.placesOf(20)).len); } @@ -1179,13 +1187,13 @@ test "kitty: an edited copy taller than the rows it covers pushes the images bel const pane = r.pane(0); r.feed(0, "\x1b[1;1Hone\r\ntwo\r\n\x1b[6;1H" ++ apc("a=T,i=21,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); const o = r.origin(0); - try std.testing.expectEqual(o.y + 5, (try r.placesOf(21))[0].y); + try std.testing.expectEqual(o.y + 5, (try r.placesOf(21))[0].dst.y); // Row 1 ("two") edited into three lines. pane.body.mode = .normal; pane.ovl = .{ .row = 1, .rows = 1, .text = try gpa.dupe(u8, "two\nthree\nfour") }; const drawn = try r.placesOf(21); try std.testing.expectEqual(@as(usize, 1), drawn.len); - try std.testing.expectEqual(o.y + 7, drawn[0].y); + try std.testing.expectEqual(o.y + 7, drawn[0].dst.y); } test "kitty: an image wider than its pane is cut at the pane's edge, never drawn over the next one" { @@ -1201,11 +1209,11 @@ test "kitty: an image wider than its pane is cut at the pane's edge, never drawn r.feed(0, apc("a=T,i=22,f=24,s=4,v=2,c=4,r=1,q=2", rgb_b64)); const drawn = try r.placesOf(22); try std.testing.expectEqual(@as(usize, 1), drawn.len); - try std.testing.expect(drawn[0].x + drawn[0].w <= o.x + r.pane(0).cols); - try std.testing.expect(drawn[0].x + drawn[0].w <= right); + try std.testing.expect(drawn[0].dst.x + drawn[0].dst.w <= o.x + r.pane(0).cols); + try std.testing.expect(drawn[0].dst.x + drawn[0].dst.w <= right); // And the pixels it takes are only that cell's. - const g = drawn[0].native.geometry.?; - try std.testing.expect(g.dst.w <= @as(u32, drawn[0].w) * 10); + const g: pardes.image.NativeGeometry = .{ .src = drawn[0].src, .dst = drawn[0].at.? }; + try std.testing.expect(g.dst.w <= @as(u32, drawn[0].dst.w) * 10); } test "kitty: a collapsed pane draws no image" { @@ -1224,16 +1232,16 @@ test "kitty: a new cell size (font zoom) redraws an image at the same cells, sca defer r.deinit(); r.feed(0, "\x1b[2;3H" ++ apc("a=T,i=24,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); var drawn = try r.placesOf(24); - try std.testing.expectEqual(@as(u32, 20), drawn[0].native.geometry.?.dst.w); + try std.testing.expectEqual(@as(u32, 20), drawn[0].at.?.w); r.p.update(.{ .resize = .{ .cols = 120, .rows = 40, .cell_pixels = .{ .w = 15, .h = 30 } } }); r.drain(); drawn = try r.placesOf(24); const o = r.origin(0); try std.testing.expectEqual(@as(usize, 1), drawn.len); - try std.testing.expectEqual(o.x + 2, drawn[0].x); - try std.testing.expectEqual(o.y + 1, drawn[0].y); - try std.testing.expectEqual(@as(u32, 30), drawn[0].native.geometry.?.dst.w); - try std.testing.expectEqual(@as(u32, 30), drawn[0].native.geometry.?.dst.h); + try std.testing.expectEqual(o.x + 2, drawn[0].dst.x); + try std.testing.expectEqual(o.y + 1, drawn[0].dst.y); + try std.testing.expectEqual(@as(u32, 30), drawn[0].at.?.w); + try std.testing.expectEqual(@as(u32, 30), drawn[0].at.?.h); } test "kitty: a dumped and restored terminal keeps its images" { @@ -1292,22 +1300,23 @@ test "kitty: recorded kitten icat and chafa traffic replays: answers whole, imag continue; }; const o = r.origin(0); - var top: ?pardes.ImagePlace = null; + var top: ?pardes.Placement = null; for (drawn) |place| { - if (top == null or place.y < top.?.y or (place.y == top.?.y and place.x < top.?.x)) top = place; + if (top == null or place.dst.y < top.?.dst.y or (place.dst.y == top.?.dst.y and place.dst.x < top.?.dst.x)) top = place; } const first = top orelse { std.debug.print("{s}: nothing drawn\n", .{rec.name}); return error.NothingDrawn; }; - std.testing.expectEqual([2]u16{ o.x + want[0], o.y + want[1] }, [2]u16{ first.x, first.y }) catch |err| { - std.debug.print("{s}: drawn at {d},{d}, live at {d},{d}\n", .{ rec.name, first.x - o.x, first.y - o.y, want[0], want[1] }); + std.testing.expectEqual([2]u16{ o.x + want[0], o.y + want[1] }, [2]u16{ first.dst.x, first.dst.y }) catch |err| { + std.debug.print("{s}: drawn at {d},{d}, live at {d},{d}\n", .{ rec.name, first.dst.x - o.x, first.dst.y - o.y, want[0], want[1] }); return err; }; } } -test "kitty: under a shell that draws no pixels, a 64x64 image over 8x4 cells is glyph art in every cell" { +test "kitty: under a shell that draws no pixels, a 64x64 image over 8x4 cells is still handed on over those cells, its cells left to the shell" { + // The glyph art a tty draws there is the tty's test (tui.zig). var r = try Rig.init(1); defer r.deinit(); r.p.frontend.native_images = false; @@ -1337,17 +1346,13 @@ test "kitty: under a shell that draws no pixels, a 64x64 image over 8x4 cells is } try cmds.appendSlice(arena, "\r\n"); r.feed(0, cmds.items); - _ = try r.places(); - const s = &r.p.surface; + const drawn = try r.places(); const o = r.origin(0); - var painted: usize = 0; - for (0..4) |dy| for (0..8) |dx| { - const cell = s.at(o.x + 7 + @as(u16, @intCast(dx)), o.y + 1 + @as(u16, @intCast(dy))); - if (cell.style.bg == .rgb and !cell.default) painted += 1; - }; - std.testing.expectEqual(@as(usize, 32), painted) catch |err| { - std.debug.print("helix theme {}: {d} cells painted\n", .{ helix, painted }); - return err; - }; + try std.testing.expectEqual(@as(usize, 4), drawn.len); + for (drawn, 0..) |place, dy| { + try std.testing.expectEqual(pardes.RectCell{ .x = o.x + 7, .y = o.y + 1 + @as(u16, @intCast(dy)), .w = 8, .h = 1 }, place.dst); + try std.testing.expect(!place.art.glyph); + try std.testing.expectEqualStrings(" ", r.p.surface.at(o.x + 7, place.dst.y).grapheme()); + } } } diff --git a/test/pdf.zig b/test/pdf.zig index b66db1df..1b2f2829 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -16,7 +16,69 @@ const Key = pardes.Key; const Event = pardes.Event; const Mode = panes.Text.Mode; const Surface = pardes.Surface; -const ImagePlace = pardes.ImagePlace; +const Placement = pardes.Placement; + +/// A placement as these cases read it: its page and revision (its key +/// among its pane's pages), where it goes and its pixels. +const Shown = struct { + page: u32, + revision: u32, + fit: image.NativeFit, + geometry: image.NativeGeometry, + pixel_offset_y: f32, + rgba: []const u8, + iw: usize, + ih: usize, +}; + +/// A frame of `p`, and its placements read so. +fn renderShown(p: *Pardes, arena: std.mem.Allocator) ![]Shown { + _ = try p.render(arena); + return shownAll(p, arena); +} + +/// `p`'s frame's placements, read so. +fn shownAll(p: *Pardes, arena: std.mem.Allocator) ![]Shown { + const s = &p.surface; + const out = try arena.alloc(Shown, s.placements.items.len); + for (s.placements.items, out) |place, *o| { + const pane = p.panes[place.pane].?; + const pv = pane.pdf.?; + var page: u32 = 0; + var revision: u32 = 0; + found: { + for (pv.rasters[0..pv.rasters_len]) |r| if (r.valid and Placement.keyOf(pane.serial, @intCast(r.page), r.revision) == place.key) { + page = @intCast(r.page); + revision = r.revision; + break :found; + }; + for (0..pv.page_count) |pg| if (Placement.keyOf(pane.serial, @intCast(pg), 0) == place.key) { + page = @intCast(pg); + break :found; + }; + return error.UnknownPlacement; + } + var scroll: f64 = 0; + for (s.regionList()) |r| if (r.kind == .body and r.owner == place.pane) { + scroll = r.scroll_px; + break; + }; + o.* = .{ + .page = page, + .revision = revision, + .fit = switch (pv.fit) { + .width => .width, + .height => .height, + }, + .geometry = .{ .src = place.src, .dst = place.at orelse return error.MissingPdfGeometry }, + .pixel_offset_y = -@as(f32, @floatCast(scroll - @floor(scroll))), + .rgba = place.raster.bytes(), + .iw = place.raster.w, + .ih = place.raster.h, + }; + } + return out; +} const PdfFitMode = panes.Pdf.FitMode; const PdfTintMode = panes.Pdf.TintMode; const pdf_enabled = panes.Pdf.enabled; @@ -159,14 +221,14 @@ const PdfLinkTests = struct { const pane = p.panes[0].?; var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); - const plain = try p.render(frame.allocator()); - const plain_revision = plain.images.items[0].native.revision; + _ = try p.render(frame.allocator()); + const plain_revision = (try shownAll(p, frame.allocator()))[0].revision; while (p.nextEffect()) |_| {} const blank = try cellAt(p, 0.3, 0.38); hover(p, blank); const hovered = try p.render(frame.allocator()); try std.testing.expectEqual(Surface.PointerShape.link, hovered.pointer_shape); - try std.testing.expect(hovered.images.items[0].native.revision != plain_revision); + try std.testing.expect((try shownAll(p, frame.allocator()))[0].revision != plain_revision); const highlights = panes.Pdf.highlightInput(p, 0, pane); try std.testing.expect(!std.meta.eql(highlights.hover_color, highlights.selection_color)); try std.testing.expect(pane.pdf.?.selection == null); @@ -563,10 +625,10 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const first = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 1), first.images.items.len); - const first_place = first.images.items[0]; + try std.testing.expectEqual(@as(usize, 1), first.placements.items.len); + const first_place = (try shownAll(p, frame.allocator()))[0]; try std.testing.expect(first_place.rgba.len == first_place.iw * first_place.ih * 4); - try std.testing.expectEqual(image.NativeFit.width, first_place.native.fit); + try std.testing.expectEqual(image.NativeFit.width, first_place.fit); const request = panes.Pdf.renderRequest( panes.Pdf.paneViewport(p, pane) orelse return error.MissingPdfViewport, pdf_raster_policy, @@ -597,9 +659,9 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { p.update(.{ .pdf_scroll = .{ .pane = 0, .delta_pixels = 0.25, .glide = false } }); try std.testing.expectEqual(@as(f64, 0.25), pane.pdf.?.document_scroll_y); _ = frame.reset(.retain_capacity); - const fractional = try p.render(frame.allocator()); - try std.testing.expectEqual(first_place.native.revision, fractional.images.items[0].native.revision); - try std.testing.expectEqual(@as(f32, -0.25), fractional.images.items[0].native.pixel_offset_y); + _ = try p.render(frame.allocator()); + try std.testing.expectEqual(first_place.revision, (try shownAll(p, frame.allocator()))[0].revision); + try std.testing.expectEqual(@as(f32, -0.25), (try shownAll(p, frame.allocator()))[0].pixel_offset_y); pane.pdf.?.document_scroll_y = 0; // The default reading view moves one exact display-cell distance without @@ -608,13 +670,13 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { try std.testing.expectEqual(@as(usize, 0), pane.pdf.?.page); try std.testing.expectEqual(@as(f64, p.cell_pixels.h), pane.pdf.?.document_scroll_y); try std.testing.expectEqual( - first_place.native.revision, + first_place.revision, panes.Pdf.rasterForPage(pane.pdf.?, pane.pdf.?.page).?.revision, ); _ = frame.reset(.retain_capacity); - const panned = try p.render(frame.allocator()); - try std.testing.expectEqual(first_place.native.revision, panned.images.items[0].native.revision); - try std.testing.expect(panned.images.items[0].native.geometry.?.src.y > first_place.native.geometry.?.src.y); + _ = try p.render(frame.allocator()); + try std.testing.expectEqual(first_place.revision, (try shownAll(p, frame.allocator()))[0].revision); + try std.testing.expect((try shownAll(p, frame.allocator()))[0].geometry.src.y > first_place.geometry.src.y); const row_scroll = pane.pdf.?.document_scroll_y; pane.pdf.?.document_scroll_y = 0; @@ -668,7 +730,7 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); try std.testing.expectEqual(@max(@as(f64, 0), counted_full - @as(f64, @floatFromInt(key_viewport.pixel_h * 2))), pane.pdf.?.document_scroll_y); try std.testing.expectEqual( - first_place.native.revision, + first_place.revision, panes.Pdf.rasterForPage(pane.pdf.?, pane.pdf.?.page).?.revision, ); @@ -687,22 +749,22 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { ); _ = frame.reset(.retain_capacity); const boundary = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 2), boundary.images.items.len); - try std.testing.expectEqual(@as(u32, 0), boundary.images.items[0].native.page); - try std.testing.expectEqual(@as(u32, 1), boundary.images.items[1].native.page); + try std.testing.expectEqual(@as(usize, 2), boundary.placements.items.len); + try std.testing.expectEqual(@as(u32, 0), (try shownAll(p, frame.allocator()))[0].page); + try std.testing.expectEqual(@as(u32, 1), (try shownAll(p, frame.allocator()))[1].page); const first_bottom = @as(f32, @floatFromInt( - boundary.images.items[0].native.geometry.?.dst.y + - boundary.images.items[0].native.geometry.?.dst.h, - )) + boundary.images.items[0].native.pixel_offset_y; + (try shownAll(p, frame.allocator()))[0].geometry.dst.y + + (try shownAll(p, frame.allocator()))[0].geometry.dst.h, + )) + (try shownAll(p, frame.allocator()))[0].pixel_offset_y; const second_top = @as(f32, @floatFromInt( - boundary.images.items[1].native.geometry.?.dst.y, - )) + boundary.images.items[1].native.pixel_offset_y; + (try shownAll(p, frame.allocator()))[1].geometry.dst.y, + )) + (try shownAll(p, frame.allocator()))[1].pixel_offset_y; const visible_gap = second_top - first_bottom; try std.testing.expect(visible_gap >= @as(f32, PDF_PAGE_GAP_PX)); try std.testing.expect(visible_gap <= @as(f32, PDF_PAGE_GAP_PX + 1)); const before_boundary_scroll = pane.pdf.?.document_scroll_y; - const page0_revision = boundary.images.items[0].native.revision; - const page1_revision = boundary.images.items[1].native.revision; + const page0_revision = (try shownAll(p, frame.allocator()))[0].revision; + const page1_revision = (try shownAll(p, frame.allocator()))[1].revision; // A scroll can activate the already-cached neighbor before the shell's // next draw. Every consumer resolves the active page's resident raster, @@ -725,9 +787,9 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { try std.testing.expectEqual(before_boundary_scroll + p.cell_pixels.h, pane.pdf.?.document_scroll_y); _ = frame.reset(.retain_capacity); const second = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 2), second.images.items.len); - try std.testing.expectEqual(page0_revision, second.images.items[0].native.revision); - try std.testing.expectEqual(page1_revision, second.images.items[1].native.revision); + try std.testing.expectEqual(@as(usize, 2), second.placements.items.len); + try std.testing.expectEqual(page0_revision, (try shownAll(p, frame.allocator()))[0].revision); + try std.testing.expectEqual(page1_revision, (try shownAll(p, frame.allocator()))[1].revision); p.update(.{ .key = .{ .cp = 'k' } }); try std.testing.expectEqual(before_boundary_scroll, pane.pdf.?.document_scroll_y); @@ -740,18 +802,18 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { panes.Pdf.activatePage(p, pane, 1, true); _ = frame.reset(.retain_capacity); const away = try p.render(frame.allocator()); - try std.testing.expect(away.images.items.len > 0); - for (away.images.items) |place| - try std.testing.expect(place.native.page != 0); + try std.testing.expect(away.placements.items.len > 0); + for (try shownAll(p, frame.allocator())) |place| + try std.testing.expect(place.page != 0); try std.testing.expect(panes.Pdf.rasterForPage(pane.pdf.?, 0) == null); try std.testing.expectEqual(@as(usize, 1), pane.pdf.?.spare_len); const retired = pane.pdf.?.spare[0]; panes.Pdf.activatePage(p, pane, 0, true); _ = frame.reset(.retain_capacity); - const returned = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(u32, 0), returned.images.items[0].native.page); - try std.testing.expectEqual(retired.ptr, returned.images.items[0].rgba.ptr); - try std.testing.expect(returned.images.items[0].native.revision != page0_revision); + _ = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(u32, 0), (try shownAll(p, frame.allocator()))[0].page); + try std.testing.expectEqual(retired.ptr, (try shownAll(p, frame.allocator()))[0].rgba.ptr); + try std.testing.expect((try shownAll(p, frame.allocator()))[0].revision != page0_revision); // PdfFit exists as a real builtin in this build. It resets placement but // preserves the current page pixels; fit-height j/k remains continuous in @@ -917,16 +979,16 @@ test "a fling's banded pages show the reader exactly what whole pages would" { try std.testing.expect(panes.Pdf.scrollPane(p, pane, landing, false)); _ = frame.reset(.retain_capacity); const flung = try p.render(frame.allocator()); - try std.testing.expect(flung.images.items.len >= 2); + try std.testing.expect(flung.placements.items.len >= 2); var banded = false; - for (flung.images.items) |place| { - const geometry = place.native.geometry orelse return error.MissingPdfGeometry; - const raster = panes.Pdf.rasterForPage(pv, place.native.page) orelse + for (try shownAll(p, frame.allocator())) |place| { + const geometry = place.geometry; + const raster = panes.Pdf.rasterForPage(pv, place.page) orelse return error.MissingPdfRaster; if (raster.band_h < raster.ih) banded = true; try std.testing.expectEqual(place.iw * raster.band_h * 4, place.rgba.len); shots[shots_len] = .{ - .page = place.native.page, + .page = place.page, .dst = geometry.dst, .pixels = try copySourceRect(gpa, place, geometry.src), }; @@ -940,13 +1002,13 @@ test "a fling's banded pages show the reader exactly what whole pages would" { // has to have matched. _ = frame.reset(.retain_capacity); const rested = try p.render(frame.allocator()); - try std.testing.expectEqual(shots_len, rested.images.items.len); - for (rested.images.items, shots[0..shots_len]) |place, shot| { - const geometry = place.native.geometry orelse return error.MissingPdfGeometry; - const raster = panes.Pdf.rasterForPage(pv, place.native.page) orelse + try std.testing.expectEqual(shots_len, rested.placements.items.len); + for (try shownAll(p, frame.allocator()), shots[0..shots_len]) |place, shot| { + const geometry = place.geometry; + const raster = panes.Pdf.rasterForPage(pv, place.page) orelse return error.MissingPdfRaster; try std.testing.expectEqual(raster.ih, raster.band_h); // promoted at rest - try std.testing.expectEqual(shot.page, place.native.page); + try std.testing.expectEqual(shot.page, place.page); try std.testing.expectEqual(shot.dst.x, geometry.dst.x); try std.testing.expectEqual(shot.dst.y, geometry.dst.y); try std.testing.expectEqual(shot.dst.w, geometry.dst.w); @@ -1035,9 +1097,9 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh pv.scroll_travel = 0; // read, not flung _ = frame.reset(.retain_capacity); const surface = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 1), surface.images.items.len); - const place = surface.images.items[0]; - const geometry = place.native.geometry orelse return error.MissingPdfGeometry; + try std.testing.expectEqual(@as(usize, 1), surface.placements.items.len); + const place = (try shownAll(p, frame.allocator()))[0]; + const geometry = place.geometry; const raster = panes.Pdf.rasterForPage(pv, 0) orelse return error.MissingPdfRaster; // Bounded: a band, never the page, and within one buffer's budget. try std.testing.expectEqual(shape.width, place.iw); @@ -1064,7 +1126,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh _ = panes.Pdf.scrollPane(p, pane, @round(max_scroll / 2) - pv.document_scroll_y, false); pv.scroll_travel = 0; _ = frame.reset(.retain_capacity); - const again = (try p.render(frame.allocator())).images.items[0]; + const again = (try renderShown(p, frame.allocator()))[0]; const again_y = panes.Pdf.rasterForPage(pv, 0).?.band_y; const stride = shape.stride; const common_top = @max(middle_y, again_y); @@ -1078,7 +1140,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh _ = panes.Pdf.scrollPane(p, pane, max_scroll - pv.document_scroll_y, false); pv.scroll_travel = 0; _ = frame.reset(.retain_capacity); - const foot = try gpa.dupe(u8, (try p.render(frame.allocator())).images.items[0].rgba); + const foot = try gpa.dupe(u8, (try renderShown(p, frame.allocator()))[0].rgba); defer gpa.free(foot); // At the foot of the page, a search hit: its highlight lands on exactly @@ -1086,8 +1148,8 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh // word finds the word. try pv.setSearchQuery(gpa, "bottommost"); _ = frame.reset(.retain_capacity); - const lit = try p.render(frame.allocator()); - const place = lit.images.items[0]; + _ = try p.render(frame.allocator()); + const place = (try shownAll(p, frame.allocator()))[0]; const raster = panes.Pdf.rasterForPage(pv, 0) orelse return error.MissingPdfRaster; const highlights = try panes.Pdf.buildHighlights(pv, frame.allocator(), panes.Pdf.highlightInput(p, 0, pane)); const marks = highlights.forPage(0); @@ -1127,7 +1189,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh /// that makes "same picture" mean something when the textures differ in shape. fn copySourceRect( gpa: std.mem.Allocator, - place: ImagePlace, + place: Shown, src: image.PixelRect, ) ![]u8 { const stride = place.iw * 4; @@ -1270,11 +1332,11 @@ test "PDF continuous strip renders every intersecting short page" { var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const surface = try p.render(frame.allocator()); - try std.testing.expectEqual(pv.page_count, surface.images.items.len); + try std.testing.expectEqual(pv.page_count, surface.placements.items.len); try std.testing.expectEqual(pv.page_count, pv.rasters_len); - for (surface.images.items, 0..) |place, page| { - try std.testing.expectEqual(@as(u32, @intCast(page)), place.native.page); - try std.testing.expectEqual(@as(u32, 1), place.native.geometry.?.dst.h); + for (try shownAll(p, frame.allocator()), 0..) |place, page| { + try std.testing.expectEqual(@as(u32, @intCast(page)), place.page); + try std.testing.expectEqual(@as(u32, 1), place.geometry.dst.h); } } @@ -1320,7 +1382,7 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { _ = frame.reset(.retain_capacity); const default_surface = try p.render(frame.allocator()); try std.testing.expectEqual(PdfTintMode.filtered, pv.tint); - try std.testing.expectEqual(@as(usize, 2), default_surface.images.items.len); + try std.testing.expectEqual(@as(usize, 2), default_surface.placements.items.len); try std.testing.expectEqual(@as(usize, 2), pv.rasters_len); const Snapshot = struct { @@ -1333,16 +1395,17 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { checksum: u64, }; const Capture = struct { - fn get(surface: *const Surface) ![2]Snapshot { - if (surface.images.items.len != 2) return error.UnexpectedVisiblePdfCount; + fn get(core: *Pardes, arena: std.mem.Allocator) ![2]Snapshot { + if (core.surface.placements.items.len != 2) return error.UnexpectedVisiblePdfCount; var out: [2]Snapshot = undefined; + const shown = try shownAll(core, arena); for (&out, 0..) |*snapshot, i| { - const place = surface.images.items[i]; + const place = shown[i]; snapshot.* = .{ - .page = place.native.page, - .revision = place.native.revision, - .geometry = place.native.geometry orelse return error.MissingPdfGeometry, - .pixel_offset_y = place.native.pixel_offset_y, + .page = place.page, + .revision = place.revision, + .geometry = place.geometry, + .pixel_offset_y = place.pixel_offset_y, .iw = place.iw, .ih = place.ih, .checksum = std.hash.Wyhash.hash(0x5044_4654_494e_5421, place.rgba), @@ -1361,7 +1424,7 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { } } }; - const default_filtered = try Capture.get(default_surface); + const default_filtered = try Capture.get(p, frame.allocator()); for (pv.rasters[0..pv.rasters_len]) |raster| try std.testing.expectEqual(PdfTintMode.filtered, raster.tint_key.?.mode); @@ -1371,8 +1434,8 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { pardes.colors.setThemeIndex(p, dark_index); for (pv.rasters[0..pv.rasters_len]) |raster| try std.testing.expect(!raster.tried); _ = frame.reset(.retain_capacity); - const themed_surface = try p.render(frame.allocator()); - const themed = try Capture.get(themed_surface); + _ = try p.render(frame.allocator()); + const themed = try Capture.get(p, frame.allocator()); try Capture.expectGeometry(default_filtered, themed); for (default_filtered, themed) |old, new| { try std.testing.expect(new.revision > old.revision); @@ -1382,8 +1445,8 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { panes.Pdf.toggleTint(pane); try std.testing.expectEqual(PdfTintMode.full, pv.tint); _ = frame.reset(.retain_capacity); - const full_surface = try p.render(frame.allocator()); - const full = try Capture.get(full_surface); + _ = try p.render(frame.allocator()); + const full = try Capture.get(p, frame.allocator()); try Capture.expectGeometry(themed, full); for (themed, full) |old, new| { try std.testing.expect(new.revision > old.revision); @@ -1394,8 +1457,8 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { panes.Pdf.toggleTint(pane); try std.testing.expectEqual(PdfTintMode.disabled, pv.tint); _ = frame.reset(.retain_capacity); - const disabled_surface = try p.render(frame.allocator()); - const disabled = try Capture.get(disabled_surface); + _ = try p.render(frame.allocator()); + const disabled = try Capture.get(p, frame.allocator()); try Capture.expectGeometry(full, disabled); for (full, disabled) |old, source| { try std.testing.expect(source.revision > old.revision); @@ -1411,8 +1474,8 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { // the per-raster key comparison asks MuPDF for replacement pixels. for (pv.rasters[0..pv.rasters_len]) |raster| try std.testing.expect(raster.tried); _ = frame.reset(.retain_capacity); - const unchanged_surface = try p.render(frame.allocator()); - const unchanged = try Capture.get(unchanged_surface); + _ = try p.render(frame.allocator()); + const unchanged = try Capture.get(p, frame.allocator()); try Capture.expectGeometry(disabled, unchanged); for (disabled, unchanged) |old, new| { try std.testing.expectEqual(old.revision, new.revision); @@ -1581,8 +1644,8 @@ test "PDF n/N addresses and reveals distinct MuPDF hits on one page" { try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(@as(usize, 0), pv.search_hit); try std.testing.expectEqual(@as(usize, 0), pane.search_row.?); - const first = try p.render(frame.allocator()); - const revision = first.images.items[0].native.revision; + _ = try p.render(frame.allocator()); + const revision = (try shownAll(p, frame.allocator()))[0].revision; const raster = panes.Pdf.rasterForPage(pv, pv.page).?; const first_scroll = pv.document_scroll_y; const first_results = pv.search_results orelse return error.MissingPdfPageSearch; @@ -1601,15 +1664,15 @@ test "PDF n/N addresses and reveals distinct MuPDF hits on one page" { try std.testing.expectEqual(@as(usize, 1), pv.search_hit); try std.testing.expectEqual(@as(usize, 2), pane.search_row.?); _ = frame.reset(.retain_capacity); - const second = try p.render(frame.allocator()); - try std.testing.expectEqual(revision, second.images.items[0].native.revision); + _ = try p.render(frame.allocator()); + try std.testing.expectEqual(revision, (try shownAll(p, frame.allocator()))[0].revision); try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(@as(usize, 2), pv.search_hit); try std.testing.expectEqual(@as(usize, 4), pane.search_row.?); _ = frame.reset(.retain_capacity); - const third = try p.render(frame.allocator()); - try std.testing.expectEqual(revision, third.images.items[0].native.revision); + _ = try p.render(frame.allocator()); + try std.testing.expectEqual(revision, (try shownAll(p, frame.allocator()))[0].revision); try std.testing.expect(pv.document_scroll_y != first_scroll); const third_results = pv.search_results orelse return error.MissingPdfPageSearch; const third_quad = for (third_results.quads) |item| { @@ -1641,7 +1704,7 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { // A host without Kitty/native pixels keeps the projected-text contract: // j/k can still change pages and a body drag remains a generic selection. const fallback = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 0), fallback.images.items.len); + try std.testing.expectEqual(@as(usize, 0), fallback.placements.items.len); p.update(.{ .key = .{ .cp = 'j' } }); try std.testing.expectEqual(@as(usize, 1), pane.pdf.?.page); p.update(.{ .key = .{ .cp = 'k' } }); @@ -1658,8 +1721,8 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { p.frontend.native_images = true; _ = frame.reset(.retain_capacity); - const plain = try p.render(frame.allocator()); - const plain_revision = plain.images.items[0].native.revision; + _ = try p.render(frame.allocator()); + const plain_revision = (try shownAll(p, frame.allocator()))[0].revision; var found = try pane.pdf.?.document.search(gpa, 0, "Pardes"); defer found.deinit(gpa); @@ -1726,8 +1789,8 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { ) != null); _ = frame.reset(.retain_capacity); - const selected_frame = try p.render(frame.allocator()); - const selected_revision = selected_frame.images.items[0].native.revision; + _ = try p.render(frame.allocator()); + const selected_revision = (try shownAll(p, frame.allocator()))[0].revision; try std.testing.expect(selected_revision != plain_revision); // Repeating an identical drag endpoint is a no-op: Kitty/SDL keep the @@ -1735,8 +1798,8 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { try std.testing.expect(panes.Pdf.beginDrag(p, pane, word_col, selected_row, true)); try std.testing.expect(raster.tried); _ = frame.reset(.retain_capacity); - const duplicate = try p.render(frame.allocator()); - try std.testing.expectEqual(selected_revision, duplicate.images.items[0].native.revision); + _ = try p.render(frame.allocator()); + try std.testing.expectEqual(selected_revision, (try shownAll(p, frame.allocator()))[0].revision); // Delayed native hover retains independent quads/text and changes only // presentation state. In particular it must not borrow the click path, @@ -1789,15 +1852,15 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { try std.testing.expectEqual(pdf_impl.HighlightKind.custom, page_highlights[0].kind); try std.testing.expectEqual(pdf_impl.HighlightKind.selection, page_highlights[page_highlights.len - 1].kind); _ = frame.reset(.retain_capacity); - const hovered_frame = try p.render(frame.allocator()); - const hovered_revision = hovered_frame.images.items[0].native.revision; + _ = try p.render(frame.allocator()); + const hovered_revision = (try shownAll(p, frame.allocator()))[0].revision; try std.testing.expect(hovered_revision != selected_revision); p.update(.pointer_leave); try std.testing.expect(p.pdf_hover_preview == null); _ = frame.reset(.retain_capacity); - const unhovered_frame = try p.render(frame.allocator()); - try std.testing.expect(unhovered_frame.images.items[0].native.revision != hovered_revision); + _ = try p.render(frame.allocator()); + try std.testing.expect((try shownAll(p, frame.allocator()))[0].revision != hovered_revision); try std.testing.expectEqual(active_before, p.active); try std.testing.expectEqual(page_before, pv.page); @@ -1850,14 +1913,14 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { try std.testing.expectEqual(max_document_scroll, pv.document_scroll_y); pv.document_scroll_y = revealed_scroll; _ = frame.reset(.retain_capacity); - const searched = try p.render(frame.allocator()); - try std.testing.expect(searched.images.items[0].native.revision != selected_revision); + _ = try p.render(frame.allocator()); + try std.testing.expect((try shownAll(p, frame.allocator()))[0].revision != selected_revision); // Fit and viewport changes can crop a hit which was already revealed. // Fit remains placement-only. SDL's physical-viewport quality request // changes with a resize and therefore replaces pixels; Kitty's fixed, // bandwidth-conscious request retains them. - const revision_before_geometry_change = searched.images.items[0].native.revision; + const revision_before_geometry_change = (try shownAll(p, frame.allocator()))[0].revision; pv.search_reveal_pending = false; panes.Pdf.toggleFit(pane); try std.testing.expect(pv.search_reveal_pending); @@ -1882,14 +1945,14 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { } }); try std.testing.expect(pv.search_reveal_pending); _ = frame.reset(.retain_capacity); - const geometry_changed = try p.render(frame.allocator()); + _ = try p.render(frame.allocator()); if (pdf_raster_policy.match_viewport) - try std.testing.expect(geometry_changed.images.items[0].native.revision != + try std.testing.expect((try shownAll(p, frame.allocator()))[0].revision != revision_before_geometry_change) else try std.testing.expectEqual( revision_before_geometry_change, - geometry_changed.images.items[0].native.revision, + (try shownAll(p, frame.allocator()))[0].revision, ); p.update(.{ .resize = .{ .cols = p.screen_w, @@ -1914,8 +1977,8 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { // Native drag events update MuPDF quads and copied text immediately, but // keep the already transmitted page generation stable until release. _ = frame.reset(.retain_capacity); - const before_drag = try p.render(frame.allocator()); - const before_drag_revision = before_drag.images.items[0].native.revision; + _ = try p.render(frame.allocator()); + const before_drag_revision = (try shownAll(p, frame.allocator()))[0].revision; const drag_col = @min(r.x + r.w - 1, word_col +| 12); const drag_row = @min(r.y + r.h - 1, selected_row +| 4); p.update(.{ .mouse = .{ @@ -1931,10 +1994,10 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { .row = @min(drag_row, selected_row +| 2), } }); _ = frame.reset(.retain_capacity); - const during_first_drag = try p.render(frame.allocator()); + _ = try p.render(frame.allocator()); try std.testing.expectEqual( before_drag_revision, - during_first_drag.images.items[0].native.revision, + (try shownAll(p, frame.allocator()))[0].revision, ); p.update(.{ .mouse = .{ .button = config.select_button, @@ -1944,10 +2007,10 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { } }); try std.testing.expect(p.drag.select.pdf.selection_changed); _ = frame.reset(.retain_capacity); - const during_second_drag = try p.render(frame.allocator()); + _ = try p.render(frame.allocator()); try std.testing.expectEqual( before_drag_revision, - during_second_drag.images.items[0].native.revision, + (try shownAll(p, frame.allocator()))[0].revision, ); p.update(.{ .mouse = .{ .button = config.select_button, @@ -1957,16 +2020,16 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { } }); try std.testing.expect(!raster.tried); _ = frame.reset(.retain_capacity); - const committed_drag = try p.render(frame.allocator()); + _ = try p.render(frame.allocator()); try std.testing.expectEqual( before_drag_revision + 1, - committed_drag.images.items[0].native.revision, + (try shownAll(p, frame.allocator()))[0].revision, ); _ = frame.reset(.retain_capacity); - const stable_drag = try p.render(frame.allocator()); + _ = try p.render(frame.allocator()); try std.testing.expectEqual( - committed_drag.images.items[0].native.revision, - stable_drag.images.items[0].native.revision, + (try shownAll(p, frame.allocator()))[0].revision, + (try shownAll(p, frame.allocator()))[0].revision, ); const saved_query = try gpa.dupe(u8, pv.search_query); diff --git a/test/pdf_pointer_bench.zig b/test/pdf_pointer_bench.zig index d6cf0e19..359d2fc1 100644 --- a/test/pdf_pointer_bench.zig +++ b/test/pdf_pointer_bench.zig @@ -111,10 +111,12 @@ const Run = struct { } fn observe(run: *Run, surface: *const pardes.Surface) void { - for (surface.images.items) |place| { - run.visual = mix(run.visual, [_]u64{ place.pane, place.x, place.y, place.w, place.h, place.native.page, place.iw, place.ih }); - if (place.native.geometry) |g| run.visual = mix(run.visual, [_]u64{ g.src.x, g.src.y, g.src.w, g.src.h, g.dst.x, g.dst.y, g.dst.w, g.dst.h }); - run.visual = std.hash.Wyhash.hash(run.visual, place.rgba); + for (surface.placements.items) |place| { + run.visual = mix(run.visual, [_]u64{ place.pane, place.dst.x, place.dst.y, place.dst.w, place.dst.h, place.raster.w, place.raster.h }); + const g = place.src; + run.visual = mix(run.visual, [_]u64{ g.x, g.y, g.w, g.h }); + if (place.at) |at| run.visual = mix(run.visual, [_]u64{ at.x, at.y, at.w, at.h }); + run.visual = std.hash.Wyhash.hash(run.visual, place.raster.bytes()); } } }; diff --git a/test/pdf_scroll_bench.zig b/test/pdf_scroll_bench.zig index 9ef11a1c..837e3acb 100644 --- a/test/pdf_scroll_bench.zig +++ b/test/pdf_scroll_bench.zig @@ -381,7 +381,7 @@ fn measure(config: Config, plan: Plan) !Result { drainEffects(core); const elapsed = nowNs() -| started; total += elapsed; - recorder.observe(frame, surface); + recorder.observe(frame, surface, pane); peak_raster_bytes = @max(peak_raster_bytes, rasterBytes(pv)); // The same frame boundary src/tui/tui.zig marks, so a Tracy capture // of this benchmark reads like a capture of the real shell. @@ -458,43 +458,42 @@ const Recorder = struct { /// and they live in the other checksum on purpose: fast scrolling is /// ALLOWED to change how pixels are carried and is never allowed to change /// which pixels arrive. - fn observe(self: *Recorder, frame: usize, surface: *const pardes.Surface) void { + fn observe(self: *Recorder, frame: usize, surface: *const pardes.Surface, pane: *const pardes.Pane) void { self.frames += 1; - self.visual = mix(self.visual, surface.images.items.len); - self.transport = mix(self.transport, surface.images.items.len); + self.visual = mix(self.visual, surface.placements.items.len); + self.transport = mix(self.transport, surface.placements.items.len); const full = frame % full_rgba_stride == 0; - for (surface.images.items) |place| { + for (surface.placements.items) |place| { self.images += 1; self.visual = mix(self.visual, place.pane); - self.visual = mix(self.visual, place.x); - self.visual = mix(self.visual, place.y); - self.visual = mix(self.visual, place.w); - self.visual = mix(self.visual, place.h); - self.visual = mix(self.visual, place.native.page); - self.visual = mix(self.visual, @backingInt(place.native.fit)); - self.visual = mix(self.visual, place.native.pan_x); - self.visual = mix(self.visual, place.native.pan_y); - self.visual = mix(self.visual, @as(u32, @bitCast(place.native.pixel_offset_y))); - if (place.native.geometry) |geometry| { - inline for (.{geometry.dst}) |rect| { - self.visual = mix(self.visual, rect.x); - self.visual = mix(self.visual, rect.y); - self.visual = mix(self.visual, rect.w); - self.visual = mix(self.visual, rect.h); - } - self.visual = self.hashSource(self.visual, place, geometry.src, full); + self.visual = mix(self.visual, place.dst.x); + self.visual = mix(self.visual, place.dst.y); + self.visual = mix(self.visual, place.dst.w); + self.visual = mix(self.visual, place.dst.h); + for (surface.regionList()) |r| if (r.kind == .body and r.owner == place.pane) { + self.visual = mix(self.visual, @as(u64, @bitCast(r.scroll_px - @floor(r.scroll_px)))); + }; + if (place.at) |rect| { + self.visual = mix(self.visual, rect.x); + self.visual = mix(self.visual, rect.y); + self.visual = mix(self.visual, rect.w); + self.visual = mix(self.visual, rect.h); + self.visual = self.hashSource(self.visual, place, place.src, full); } else { self.visual = mix(self.visual, std.math.maxInt(u64)); self.visual = if (full) - std.hash.Wyhash.hash(self.visual, place.rgba) + std.hash.Wyhash.hash(self.visual, place.raster.bytes()) else - stridedHash(self.visual, place.rgba); + stridedHash(self.visual, place.raster.bytes()); } - self.transport = mix(self.transport, place.native.page); - self.transport = mix(self.transport, place.iw); - self.transport = mix(self.transport, place.ih); - self.transport = mix(self.transport, place.rgba.len); - self.transport = mix(self.transport, place.native.revision -% self.base_revision); + // The revision the key is of, from the slot holding it (0: a + // page's paper before it lands). + const revision = for (pane.pdf.?.rasters[0..pane.pdf.?.rasters_len]) |r| { + if (r.valid and pardes.Placement.keyOf(pane.serial, @intCast(r.page), r.revision) == place.key) break r.revision; + } else 0; + self.transport = mix(self.transport, revision -% self.base_revision); + self.transport = mix(self.transport, place.raster.w); + self.transport = mix(self.transport, place.raster.h); } } @@ -504,21 +503,22 @@ const Recorder = struct { fn hashSource( self: *Recorder, seed: u64, - place: pardes.ImagePlace, + place: pardes.Placement, src: anytype, full: bool, ) u64 { _ = self; - const stride = place.iw * 4; - if (stride == 0 or place.rgba.len < stride) return mix(seed, std.math.maxInt(u64)); - const rows = @min(src.h, @as(u32, @intCast(place.rgba.len / stride)) -| src.y); + const rgba = place.raster.bytes(); + const stride: usize = place.raster.stride; + if (stride == 0 or rgba.len < stride) return mix(seed, std.math.maxInt(u64)); + const rows = @min(src.h, @as(u32, @intCast(rgba.len / stride)) -| src.y); var hash = mix(seed, rows); var row: u32 = 0; while (row < rows) : (row += if (full) 1 else 7) { const start = (@as(usize, src.y) + row) * stride + @as(usize, src.x) * 4; - const len = @min(@as(usize, src.w) * 4, place.rgba.len -| start); + const len = @min(@as(usize, src.w) * 4, rgba.len -| start); if (len == 0) break; - hash = std.hash.Wyhash.hash(hash, place.rgba[start..][0..len]); + hash = std.hash.Wyhash.hash(hash, rgba[start..][0..len]); } return hash; } |
