diff options
Diffstat (limited to 'src/surface.zig')
| -rw-r--r-- | src/surface.zig | 73 |
1 files changed, 71 insertions, 2 deletions
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 }; |
