summaryrefslogtreecommitdiff
path: root/src/pdf_view.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/pdf_view.zig')
-rw-r--r--src/pdf_view.zig177
1 files changed, 166 insertions, 11 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig
index 8edb4902..93390b57 100644
--- a/src/pdf_view.zig
+++ b/src/pdf_view.zig
@@ -9,7 +9,7 @@ const config = @import("config.zig");
const modal = @import("modal.zig");
const filesystem = @import("fs.zig");
const tracy = @import("tracy.zig");
-const image = @import("image.zig");
+const surface = @import("surface.zig");
const look = @import("look.zig");
const tagline = @import("tagline.zig");
const File = panes.File;
@@ -1147,8 +1147,163 @@ pub const PanResult = enum { moved, edge, unavailable };
pub const ScrollResult = struct { active_page: usize };
pub const CellPixels = struct { w: u16, h: u16 };
+/// 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
+/// image at the top left. The axis-specific modes may enlarge, letterbox the
+/// unconstrained axis, or crop it when it overflows.
+pub const NativeFit = if (enabled) enum { contain, width, height } else enum { contain };
+
+fn panOffset(overflow: u32, pan: u16) u32 {
+ if (overflow == 0 or pan == 0) return 0;
+ if (pan == std.math.maxInt(u16)) return overflow;
+ // Round, rather than truncate, so the midpoint is the visual midpoint.
+ return @intCast((@as(u64, overflow) * pan + std.math.maxInt(u16) / 2) / std.math.maxInt(u16));
+}
+
+fn fitWidth(src_w: u32, src_h: u32, body_w: u32, body_h: u32, pan_y: u16) ?surface.NativeGeometry {
+ const scaled_h = @as(u64, src_h) * body_w;
+ if (scaled_h <= @as(u64, body_h) * src_w) {
+ const full_h: u32 = @intCast(@max(1, scaled_h / src_w));
+ return .{
+ .src = .{ .w = src_w, .h = src_h },
+ .dst = .{ .y = (body_h - full_h) / 2, .w = body_w, .h = full_h },
+ };
+ }
+
+ // Retain the largest whole source-pixel crop which cannot extend beyond
+ // the body at this scale. The sub-pixel remainder becomes at most a small
+ // centered letterbox instead of leaking into an adjacent cell in Kitty.
+ const crop_h_wide = @as(u64, body_h) * src_w / body_w;
+ if (crop_h_wide == 0) return null;
+ const crop_h: u32 = @min(src_h, @as(u32, @intCast(crop_h_wide)));
+ const dst_h: u32 = @min(body_h, @max(1, @as(u32, @intCast(@as(u64, crop_h) * body_w / src_w))));
+ return .{
+ .src = .{
+ .y = panOffset(src_h - crop_h, pan_y),
+ .w = src_w,
+ .h = crop_h,
+ },
+ .dst = .{ .y = (body_h - dst_h) / 2, .w = body_w, .h = dst_h },
+ };
+}
+
+fn fitHeight(src_w: u32, src_h: u32, body_w: u32, body_h: u32, pan_x: u16) ?surface.NativeGeometry {
+ const scaled_w = @as(u64, src_w) * body_h;
+ if (scaled_w <= @as(u64, body_w) * src_h) {
+ const full_w: u32 = @intCast(@max(1, scaled_w / src_h));
+ return .{
+ .src = .{ .w = src_w, .h = src_h },
+ .dst = .{ .x = (body_w - full_w) / 2, .w = full_w, .h = body_h },
+ };
+ }
+
+ const crop_w_wide = @as(u64, body_w) * src_h / body_h;
+ if (crop_w_wide == 0) return null;
+ const crop_w: u32 = @min(src_w, @as(u32, @intCast(crop_w_wide)));
+ const dst_w: u32 = @min(body_w, @max(1, @as(u32, @intCast(@as(u64, crop_w) * body_h / src_h))));
+ return .{
+ .src = .{
+ .x = panOffset(src_w - crop_w, pan_x),
+ .w = crop_w,
+ .h = src_h,
+ },
+ .dst = .{ .x = (body_w - dst_w) / 2, .w = dst_w, .h = body_h },
+ };
+}
+
+/// Compute native image placement without floating point. Source dimensions
+/// are limited to u32 because SDL's texture API and Kitty's crop parameters
+/// cannot describe anything larger. Kitty applies the stricter u16 check at
+/// its protocol boundary.
+pub fn nativeGeometry(
+ iw: usize,
+ ih: usize,
+ body_pixel_w: u32,
+ body_pixel_h: u32,
+ fit: NativeFit,
+ pan_x: u16,
+ pan_y: u16,
+) ?surface.NativeGeometry {
+ if (comptime !enabled) {
+ return surface.containGeometry(iw, ih, body_pixel_w, body_pixel_h);
+ }
+ if (iw == 0 or ih == 0 or body_pixel_w == 0 or body_pixel_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);
+ return switch (fit) {
+ .width => fitWidth(src_w, src_h, body_pixel_w, body_pixel_h, pan_y),
+ .height => fitHeight(src_w, src_h, body_pixel_w, body_pixel_h, pan_x),
+ // Pan is not contain's: it keeps the top-left placement.
+ .contain => surface.containGeometry(iw, ih, body_pixel_w, body_pixel_h),
+ };
+}
+
+test "native fit width handles portrait crop, pan extrema, and landscape letterbox" {
+ if (comptime !enabled) return;
+ const top = nativeGeometry(600, 1000, 800, 600, .width, 0, 0).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = 600, .h = 450 }, top.src);
+ try std.testing.expectEqual(surface.RectPx{ .w = 800, .h = 600 }, top.dst);
+
+ const middle = nativeGeometry(600, 1000, 800, 600, .width, 0, 32768).?;
+ try std.testing.expectEqual(@as(u32, 275), middle.src.y);
+ const bottom = nativeGeometry(600, 1000, 800, 600, .width, 65535, 65535).?;
+ try std.testing.expectEqual(@as(u32, 550), bottom.src.y);
+
+ const landscape = nativeGeometry(1000, 600, 800, 600, .width, 0, 65535).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = 1000, .h = 600 }, landscape.src);
+ try std.testing.expectEqual(surface.RectPx{ .y = 60, .w = 800, .h = 480 }, landscape.dst);
+}
+
+test "native fit height handles portrait letterbox and landscape crop" {
+ if (comptime !enabled) return;
+ const portrait = nativeGeometry(600, 1000, 800, 600, .height, 65535, 0).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = 600, .h = 1000 }, portrait.src);
+ try std.testing.expectEqual(surface.RectPx{ .x = 220, .w = 360, .h = 600 }, portrait.dst);
+
+ const left = nativeGeometry(1000, 600, 800, 600, .height, 0, 0).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = 800, .h = 600 }, left.src);
+ try std.testing.expectEqual(surface.RectPx{ .w = 800, .h = 600 }, left.dst);
+ const right = nativeGeometry(1000, 600, 800, 600, .height, 65535, 0).?;
+ try std.testing.expectEqual(@as(u32, 200), right.src.x);
+}
+
+test "native fit odd ratios round inward and map points through letterboxes and crops" {
+ if (comptime !enabled) return;
+ const width = nativeGeometry(403, 211, 101, 47, .width, 0, 32768).?;
+ try std.testing.expectEqual(surface.RectPx{ .y = 12, .w = 403, .h = 187 }, width.src);
+ try std.testing.expectEqual(surface.RectPx{ .w = 101, .h = 46 }, width.dst);
+ const mapped = width.sourcePoint(100, 45).?;
+ try std.testing.expect(mapped.x < width.src.x + width.src.w);
+ try std.testing.expect(mapped.y < width.src.y + width.src.h);
+ try std.testing.expect(width.sourcePoint(101, 46) == null);
+
+ const height = nativeGeometry(403, 211, 101, 47, .height, 32768, 0).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = 403, .h = 211 }, height.src);
+ try std.testing.expectEqual(surface.RectPx{ .x = 6, .w = 89, .h = 47 }, height.dst);
+ try std.testing.expect(height.sourcePoint(5, 20) == null);
+ try std.testing.expect(height.sourcePoint(6, 20) != null);
+}
+
+test "native fit modes reject what the backend contract cannot represent and are total at extreme aspect ratios" {
+ if (comptime !enabled) return;
+ try std.testing.expect(nativeGeometry(10, 10, 0, 20, .width, 0, 0) == null);
+ if (@bitSizeOf(usize) > 32)
+ try std.testing.expect(nativeGeometry(@as(usize, std.math.maxInt(u32)) + 1, 10, 20, 20, .height, 0, 0) == null);
+
+ const largest = std.math.maxInt(u32);
+ try std.testing.expect(nativeGeometry(1, largest, largest, 1, .width, 0, 65535) == null);
+ try std.testing.expect(nativeGeometry(largest, 1, 1, largest, .height, 65535, 0) == null);
+
+ const wide = nativeGeometry(largest, 1, largest, 1, .width, 0, 0).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = largest, .h = 1 }, wide.dst);
+ const tall = nativeGeometry(1, largest, 1, largest, .height, 0, 0).?;
+ try std.testing.expectEqual(surface.RectPx{ .w = 1, .h = largest }, tall.dst);
+}
+
const PlacedGeometry = struct {
- geometry: image.NativeGeometry,
+ geometry: surface.NativeGeometry,
pixel_offset_y: f32,
};
@@ -1157,7 +1312,7 @@ pub const PlacedRaster = if (enabled) struct {
revision: u32,
fit: FitMode,
pan_x: u16,
- geometry: image.NativeGeometry,
+ geometry: surface.NativeGeometry,
pixel_offset_y: f32,
rgba: []const u8,
width: usize,
@@ -1165,7 +1320,7 @@ pub const PlacedRaster = if (enabled) struct {
} else void;
const VisibleRows = struct {
- base: image.NativeGeometry,
+ base: surface.NativeGeometry,
y0: u32,
y1: u32,
dst_y: u32,
@@ -1269,7 +1424,7 @@ fn visibleRows(
) ?VisibleRows {
if (comptime !enabled) return null;
const page_h = state.page_heights[page];
- const base = image.nativeGeometry(
+ const base = nativeGeometry(
iw,
ih,
viewport.pixel_w,
@@ -1367,7 +1522,7 @@ pub fn placedRaster(state: *const State, viewport: Viewport, page: usize) ?Place
};
}
-pub fn activeGeometry(state: *const State, viewport: Viewport) ?image.NativeGeometry {
+pub fn activeGeometry(state: *const State, viewport: Viewport) ?surface.NativeGeometry {
if (comptime !enabled) return null;
const raster = rasterForPageConst(state, state.page) orelse return null;
const placed = placedGeometry(state, raster, viewport, state.page) orelse return null;
@@ -2131,7 +2286,7 @@ pub fn scrollDocument(state: *State, viewport: Viewport, delta_pixels: f64) ?Scr
pub fn panPixels(
state: *State,
- geometry: image.NativeGeometry,
+ geometry: surface.NativeGeometry,
axis: PanAxis,
direction: i8,
display_pixels: u32,
@@ -2185,7 +2340,7 @@ pub fn panPixels(
return .moved;
}
-fn setHorizontalEdge(state: *State, geometry: image.NativeGeometry, end: bool) void {
+fn setHorizontalEdge(state: *State, geometry: surface.NativeGeometry, end: bool) void {
if (comptime !enabled) return;
const raster = rasterForPage(state, state.page) orelse return;
if (raster.iw <= geometry.src.w) return;
@@ -2205,7 +2360,7 @@ fn rearmSearchReveal(state: *State, viewport: Viewport) void {
pub fn revealSearch(
state: *State,
viewport: Viewport,
- geometry: ?image.NativeGeometry,
+ geometry: ?surface.NativeGeometry,
) void {
if (comptime !enabled) return;
if (!state.search_reveal_pending) return;
@@ -2529,7 +2684,7 @@ pub fn probeWord(
}
pub fn pointAtGeometry(
- geometry: image.NativeGeometry,
+ geometry: surface.NativeGeometry,
pixel_offset_y: f32,
full_width: usize,
full_height: usize,
@@ -2958,7 +3113,7 @@ pub fn tintColors(core: *const pardes.Pardes) TintColors {
};
}
-pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?image.NativeGeometry {
+pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?surface.NativeGeometry {
if (comptime !enabled) return null;
const state = if (pane.pdf) |view| view else return null;
const view = paneViewport(core, pane) orelse return null;