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.zig50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig
index bcca11b4..f6d26eac 100644
--- a/src/pdf_view.zig
+++ b/src/pdf_view.zig
@@ -3070,6 +3070,56 @@ pub fn revealOutlineDestination(
_ = ensurePaneLayout(core, pane);
}
+/// A PDF's place, as the jumplist keeps it: the page at the strip's top and
+/// how far down that page it stands, a fraction in 2^-32 of the page. A
+/// fraction, not pixels: a zoom, a fit change or a resize scales every page,
+/// and a fraction of one lands on the same line of print after it.
+pub const Place = struct { page: usize, at: u32 };
+const place_scale: f64 = std.math.maxInt(u32);
+
+pub fn place(pane: *const pardes.Pane) ?Place {
+ if (comptime !enabled) return null;
+ const state = if (pane.pdf) |*pv| pv else return null;
+ if (!state.layout_valid or state.document_height == 0) return .{ .page = state.page, .at = 0 };
+ const page = pageAtOffset(state, state.document_scroll_y);
+ const start: f64 = @floatFromInt(state.page_starts[page]);
+ const height: f64 = @floatFromInt(@max(1, state.page_heights[page]));
+ const fraction = std.math.clamp((state.document_scroll_y - start) / height, 0, 1);
+ return .{ .page = page, .at = @intFromFloat(@round(fraction * place_scale)) };
+}
+
+/// Where `at` stands on the strip as laid out now, in document pixels.
+pub fn placeY(pane: *const pardes.Pane, at: Place) ?f64 {
+ if (comptime !enabled) return null;
+ const state = if (pane.pdf) |*pv| pv else return null;
+ if (!state.layout_valid) return null;
+ const page = @min(at.page, state.page_count -| 1);
+ return @as(f64, @floatFromInt(state.page_starts[page])) +
+ @as(f64, @floatFromInt(at.at)) / place_scale * @as(f64, @floatFromInt(state.page_heights[page]));
+}
+
+/// A jump to `at` (Back, Forward): the core is there at once and the strip
+/// follows through the motion system, a far one as one body's slide, the
+/// page rendered where it comes to rest. Not laid out yet (a pane just
+/// reopened), it is anchored there for the first layout.
+pub fn landAt(core: *pardes.Pardes, pane: *pardes.Pane, at: Place) void {
+ if (comptime !enabled) return;
+ const state = &(pane.pdf orelse return);
+ const page = @min(at.page, state.page_count -| 1);
+ activatePage(core, pane, page, false);
+ state.scroll_to_page_pending = false;
+ if (placeY(pane, .{ .page = page, .at = at.at })) |y| {
+ state.layout_anchor_pending = false;
+ // Where it stands already (half a pixel is the place's rounding):
+ // not moved, not kicked.
+ if (@abs(y - state.document_scroll_y) >= 0.5) state.document_scroll_y = y;
+ } else {
+ state.layout_anchor_pending = true;
+ state.layout_anchor_page = page;
+ state.layout_anchor_fraction = @as(f64, @floatFromInt(at.at)) / place_scale;
+ }
+}
+
pub fn setPage(core: *pardes.Pardes, pane: *pardes.Pane, page: usize) void {
activatePage(core, pane, page, true);
}