From c8553032ef84aec74cbb5d07d07f934eecf3d4a9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 05:19:21 -0300 Subject: kinds #9, the core's image code gone: src/image.zig (558 lines) and the static table go, so no kind is compiled in (Registry.init, Kind.image, compiledIn, `static` in /addons and "is compiled in" with them) and a test of the core's side of a kind's pane opens Kind.test_kind (a picture of one pixel, no library); a placement's fit (NativeGeometry, containGeometry) moves to surface.zig and a PDF's fit modes to pdf_view.zig; a terminal's kitty PNG decodes in TerminalImages.zig through wuffs as before, its downscale through stb_image_resize, the one part of zstbi the core keeps (stb_image patched out of it, the wuffs_images option, tag_image and image_exts gone); the image cases load the built image.so by path: test/panes.zig's and fs_namespace's through Addon.perform, T1 through Addon load (a new behaviour case: PNG, JPEG and PPM through 9P, the art words, a refused corrupt PNG, a dump's placeholder and its picture back, which replaces the decode unit test and ctl.zig's corrupt-image case), the snapshot, process and image harnesses by linking it into each session's data directory; images.snap unchanged Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/surface.zig | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) (limited to 'src/surface.zig') diff --git a/src/surface.zig b/src/surface.zig index df9abac1..c058e101 100644 --- a/src/surface.zig +++ b/src/surface.zig @@ -7,7 +7,6 @@ const uucode = @import("uucode"); const vaxis = @import("vaxis"); const panes = @import("panes.zig"); const config = @import("config.zig"); -const image = @import("image.zig"); const MAX_PANES = pardes.MAX_PANES; const Layer = @import("Layer.zig"); const MAX_TAG_LAYERS = pardes.MAX_TAG_LAYERS; @@ -71,7 +70,77 @@ test "cell visual equality ignores dead grapheme tail bytes" { } /// A rectangle in pixels. -pub const RectPx = image.PixelRect; +pub const RectPx = struct { + x: u32 = 0, + y: u32 = 0, + w: u32 = 0, + h: u32 = 0, +}; + +pub const PixelPoint = struct { x: u32, y: u32 }; + +/// Source pixels and their destination inside a pane body's pixel rectangle. +/// Both rectangles are half-open. Rounding is always toward the inside, so a +/// backend can draw this result without a backend-specific overflow clip. +pub const NativeGeometry = struct { + src: RectPx, + dst: RectPx, + + /// Map a destination pixel back into the retained source crop. This is the + /// same integer transform the texture rectangle describes; points in a + /// letterbox return null. + pub fn sourcePoint(self: NativeGeometry, x: u32, y: u32) ?PixelPoint { + if (self.src.w == 0 or self.src.h == 0 or self.dst.w == 0 or self.dst.h == 0) return null; + if (x < self.dst.x or y < self.dst.y) return null; + const dx = x - self.dst.x; + const dy = y - self.dst.y; + if (dx >= self.dst.w or dy >= self.dst.h) return null; + return .{ + .x = self.src.x + @as(u32, @intCast(@as(u64, dx) * self.src.w / self.dst.w)), + .y = self.src.y + @as(u32, @intCast(@as(u64, dy) * self.src.h / self.dst.h)), + }; + } +}; + +/// Where a fitted placement (`at` null) goes: shrunk only when it must be, +/// at the top left. The scale ratios are compared without floating point: +/// the tighter bound is exact, the other rounded inward. +pub fn containGeometry(iw: usize, ih: usize, max_w: u32, max_h: u32) ?NativeGeometry { + if (iw == 0 or ih == 0 or max_w == 0 or max_h == 0 or + iw > std.math.maxInt(u32) or ih > std.math.maxInt(u32)) return null; + + const src_w: u32 = @intCast(iw); + const src_h: u32 = @intCast(ih); + if (src_w <= max_w and src_h <= max_h) return .{ + .src = .{ .w = src_w, .h = src_h }, + .dst = .{ .w = src_w, .h = src_h }, + }; + const dst: RectPx = if (@as(u64, max_w) * src_h <= @as(u64, max_h) * src_w) + .{ + .w = max_w, + .h = @intCast(@max(1, @as(u64, src_h) * max_w / src_w)), + } + else + .{ + .w = @intCast(@max(1, @as(u64, src_w) * max_h / src_h)), + .h = max_h, + }; + return .{ .src = .{ .w = src_w, .h = src_h }, .dst = dst }; +} + +test "native image contain keeps aspect, bounds, top-left, and small-image size" { + try std.testing.expectEqual(RectPx{ .w = 40, .h = 20 }, containGeometry(400, 200, 40, 40).?.dst); + try std.testing.expectEqual(RectPx{ .w = 20, .h = 40 }, containGeometry(200, 400, 40, 40).?.dst); + try std.testing.expectEqual(RectPx{ .w = 17, .h = 9 }, containGeometry(17, 9, 40, 40).?.dst); + try std.testing.expect(containGeometry(17, 9, 0, 40) == null); + + const odd = containGeometry(403, 211, 101, 47).?; + try std.testing.expectEqual(RectPx{ .w = 89, .h = 47 }, odd.dst); + try std.testing.expectEqual(RectPx{ .w = 403, .h = 211 }, odd.src); + // Nothing its backend contract cannot represent. + try std.testing.expect(containGeometry(0, 10, 20, 20) == null); + if (@bitSizeOf(usize) > 32) try std.testing.expect(containGeometry(@as(usize, std.math.maxInt(u32)) + 1, 10, 20, 20) == null); +} /// A rectangle in the frame's cells. pub const RectCell = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; -- cgit v1.3