summaryrefslogtreecommitdiff
path: root/src/TerminalImages.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 22:21:45 -0300
committerGabriel Schneider <[email protected]>2026-11-05 23:08:36 -0300
commit71e5a0f4a8685b385fac762995d45575b638179f (patch)
treebf643875b0567b45cf659c1b3ad4b872f311a774 /src/TerminalImages.zig
parentd8e5fc18f5923c4213de1cb4105a677e725200af (diff)
downloadpardes-71e5a0f4a8685b385fac762995d45575b638179f.tar.gz
pardes-71e5a0f4a8685b385fac762995d45575b638179f.zip
kinds #1, the paint contract: a picture over the cells is one Placement (raster: ptr, w, h, stride; src in pixels; dst in the frame's cells; at, where in them in pixels, or none for the frontend to fit; key, the pixels' identity by pane serial, page and revision; a terminal program's kitty z; dirty, the rows changed since an older key; art, the pane's Petscii, Palette and Ascii), where ImagePlace carried native.{revision, page, fit, pan_x, pan_y, geometry, pixel_offset_y, paper_bg, exact}, cell rects and an ImageCacheKey of three shapes by build; a PDF strip's scroll is its body region's scroll_px, whose fraction the window draws as pixel_offset_y was; glyph art leaves the core for src/Art.zig, the frontends' (the matcher, both palettes and a cache by key, ex image.zig's GlyphArt and TerminalImages' glyph cache): the core always hands an image pane, a PDF's pages and a terminal's kitty images as placements, whatever the frontend shows, and the tty draws them as glyph art without kitty graphics or when the pane asks, the window when the pane asks; the four words set the same pane bytes as before and the frontend reads them off the placement, so config.zig, builtins.zig and exec.zig are untouched; /screen lists a frame's placements as images (only a frame with pictures has the field); no golden changes: the snapshot goldens are the tty's, which draws the same art (images.snap and tagbottomimage.snap pass unchanged, and the tty's bytes for an image, its Palette and its Ascii are byte-identical to the base's), no /screen golden holds a picture, and the window's goldens hash the same; tests: kitty.zig's glyph-art case becomes its core half (a placement over the 8x4 cells, the cells the pane's) with the art in tui.zig's own test, terminal.zig's glyph-art case the same, surface.zig's two ImageCacheKey cases one keyOf case, the GlyphArt cases move to Art.zig with a frame case; gates: core-test 959/959, behaviour-test 244/244, unit-test 1486/1487 tui and 1530/1531 gui, pane-test 171/172, snap all ok, process-test 0 of 37 failed on both, hxdiff 19 and hxparity 6 mismatches by the base's names, replicas, gui-golden, monkey-gui, kitty-test 44/44, kitty-harness and image-harness on both, daemon.py main and window-clients
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/TerminalImages.zig')
-rw-r--r--src/TerminalImages.zig99
1 files changed, 21 insertions, 78 deletions
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);
- }
}