From 6e8a6a63d45b2caef7b649d358f836e6cc4134d6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 22:50:15 -0300 Subject: anim 11: a PDF's strip moves in the text's motion system: the integrator is animation.Follow (the Glide's, moved out), here the strip's lag behind document_scroll_y in pixels; whatever moves the core's position (a notch, a spin, PageDown, Ctrl-d/f, ge/gg, a Look at a page, Back, the rail) kicks it, a touchpad's fraction and a relayout move it directly; the pages a glide crosses render in its input's frame, a far jump's only where it comes to rest; a slow stream eases onto its mark (it ran into it at speed and passed it); pictures neither scroll nor pan --- src/pdf_view.zig | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 168 insertions(+), 12 deletions(-) (limited to 'src/pdf_view.zig') diff --git a/src/pdf_view.zig b/src/pdf_view.zig index 2a89ef01..37e4b216 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -197,6 +197,81 @@ test "PDF selections retain their owning page across page focus" { try std.testing.expectEqual(@as(usize, 0), state.selection_text.len); } +test "a PDF strip glides a notch and a page to their marks, eased and never past them, and follows a touchpad's fraction at once" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + const view: Viewport = .{ .pixel_w = 800, .pixel_h = 600 }; + const motion = pardes.animation.Motion.of(.smooth); + ensureLayout(&state, view); + var now: u64 = std.time.ns_per_s; + try std.testing.expectEqual(state.document_scroll_y, stripAt(&state, view, motion, now)); + for ([_]f64{ 20, 600 }) |step| { + const from = state.document_scroll_y; + _ = scrollDocument(&state, view, step); + const mark = state.document_scroll_y; + // The input's frame still shows where it was; then it eases in. + var shown = stripAt(&state, view, motion, now); + try std.testing.expectEqual(from, shown); + var last_step: f64 = 0; + var peaked = false; + for (0..60) |_| { + now += pardes.animation.frame_ns; + const next = stripAt(&state, view, motion, now); + const moved = next - shown; + try std.testing.expect(moved >= 0 and next <= mark); + // Past its fastest, each frame's step is no longer than the last. + if (moved < last_step) peaked = true; + if (peaked) try std.testing.expect(moved <= last_step + 1e-9); + last_step = moved; + shown = next; + } + try std.testing.expectEqual(mark, shown); + try std.testing.expect(!state.follow.moving()); + try std.testing.expect(last_step <= 1); + } + // A touchpad's fraction: drawn where the core is, the same frame. + const from = state.document_scroll_y; + _ = scrollDocument(&state, view, 0.25); + state.direct_y += state.document_scroll_y - from; + try std.testing.expectEqual(from + 0.25, stripAt(&state, view, motion, now)); +} + +test "a PDF glide renders the pages it crosses in its input's frame, but a far jump's only where it comes to rest" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const view: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + const motion = pardes.animation.Motion.of(.smooth); + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + ensureLayout(&state, view); + const frame = struct { + fn f(st: *State, a: *std.heap.ArenaAllocator, m: pardes.animation.Motion, k: TintKey, h: HighlightInput) void { + const v: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + const shown = stripAt(st, v, m, std.time.ns_per_s); + st.document_scroll_y = shown; + _ = renderFrame(st, std.testing.allocator, a.allocator(), v, pardes.pdf_raster_policy, k, h, st.drawn_y - shown, st.far); + st.document_scroll_y = st.drawn_y; + } + }.f; + frame(&state, &arena, motion, key, none); + // A body down, onto page one's lower part: what it crosses is drawn. + _ = scrollDocument(&state, view, 300); + frame(&state, &arena, motion, key, none); + try std.testing.expect(!state.far and rasterForPage(&state, 0) != null); + // Far, to page three: only it; the slide comes in over page two's place. + state.document_scroll_y = @floatFromInt(state.page_starts[3]); + frame(&state, &arena, motion, key, none); + try std.testing.expect(state.far); + try std.testing.expect(rasterForPage(&state, 3) != null); + try std.testing.expect(rasterForPage(&state, 2) == null); +} + pub const Raster = if (enabled) struct { valid: bool = false, page: usize = 0, @@ -249,6 +324,15 @@ pub const State = if (enabled) struct { layout_fit: FitMode = .width, layout_valid: bool = false, document_scroll_y: f64 = 0, + /// The strip as drawn lags `document_scroll_y` by `follow` (anim 11, + /// the text's motion system, in pixels): `drawn_y` is where the core + /// stood at the last frame, `direct_y` what moved it since that the + /// picture takes as it comes (a touchpad, a relayout). + follow: pardes.animation.Follow = .{}, + drawn_y: f64 = 0, + direct_y: f64 = 0, + /// The last kick was a jump past a body (renderFrame's `far`). + far: bool = false, scroll_to_page_pending: bool = true, rasters: [raster_max]Raster = undefined, rasters_len: usize = 0, @@ -869,23 +953,28 @@ fn pageAtOffset(state: *const State, offset: f64) usize { return if (y >= end and lo + 1 < state.page_count) lo + 1 else lo; } -fn pageVisible(state: *const State, page: usize, viewport: Viewport) bool { +fn pageVisible(state: *const State, page: usize, y: f64, h: f64) bool { if (comptime !enabled) return false; - const top = @as(f64, @floatFromInt(state.page_starts[page])) - state.document_scroll_y; + const top = @as(f64, @floatFromInt(state.page_starts[page])) - y; const bottom = top + @as(f64, @floatFromInt(state.page_heights[page])); - return bottom > 0 and top < @as(f64, @floatFromInt(viewport.pixel_h)); + return bottom > 0 and top < h; } pub fn visiblePages(state: *const State, viewport: Viewport) VisiblePages { + return pagesOver(state, state.document_scroll_y, @floatFromInt(viewport.pixel_h)); +} + +/// The pages over the strip from `y`, `h` pixels down. +fn pagesOver(state: *const State, y: f64, h: f64) VisiblePages { if (comptime !enabled) return .{}; var out: VisiblePages = .{}; - var page = pageAtOffset(state, state.document_scroll_y); - if (page > 0 and pageVisible(state, page - 1, viewport)) page -= 1; + var page = pageAtOffset(state, y); + if (page > 0 and pageVisible(state, page - 1, y, h)) page -= 1; out.first = page; while (page < state.page_count) : (page += 1) { - const top = @as(f64, @floatFromInt(state.page_starts[page])) - state.document_scroll_y; - if (top >= @as(f64, @floatFromInt(viewport.pixel_h))) break; - if (pageVisible(state, page, viewport)) out.len += 1; + const top = @as(f64, @floatFromInt(state.page_starts[page])) - y; + if (top >= h) break; + if (pageVisible(state, page, y, h)) out.len += 1; } return out; } @@ -1496,18 +1585,31 @@ pub fn renderFrame( policy: RasterPolicy, tint_key: TintKey, highlight_input: HighlightInput, + path: f64, + far: bool, ) VisiblePages { if (comptime !enabled) return .{}; ensureLayout(state, viewport); state.resolveSearch(gpa); const highlights = buildHighlights(state, arena, highlight_input) catch Highlights{}; const request = renderRequest(viewport, policy, state.fit); - var visible = visiblePages(state, viewport); + // ponytail: every page the glide crosses to its mark (`path` pixels + // on) renders now, in the input's frame, so no frame of the glide + // waits on MuPDF (it renders on the loop). A far jump's slide (`far`) + // comes in over what is there, none of the page above its mark, which + // would cost the input's frame a page; a render thread would draw it. + const span = struct { + fn f(st: *const State, view: Viewport, along: f64, from_rest: bool) VisiblePages { + const h: f64 = @floatFromInt(view.pixel_h); + return if (from_rest) pagesOver(st, st.document_scroll_y + along, h) else pagesOver(st, st.document_scroll_y + @min(0, along), h + @abs(along)); + } + }.f; + var visible = span(state, viewport, path, far); reconcile(state, gpa, request, tint_key, highlights, visible, viewport); rearmSearchReveal(state, viewport); revealSearch(state, viewport, activeGeometry(state, viewport)); - visible = visiblePages(state, viewport); + visible = span(state, viewport, path, far); reconcile(state, gpa, request, tint_key, highlights, visible, viewport); return visible; } @@ -1898,6 +2000,7 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { if (comptime !enabled) return; const tz = tracy.zone(@src(), "pdf.ensure_layout"); defer tz.end(); + var relaid = false; if (state.layout_valid and !state.scroll_to_page_pending and !state.layout_anchor_pending and (state.layout_viewport_w != viewport.pixel_w or @@ -1919,6 +2022,7 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { state.layout_viewport_h = viewport.pixel_h; state.layout_fit = state.fit; state.layout_valid = true; + relaid = true; if (state.scroll_to_page_pending) { state.document_scroll_y = @floatFromInt(state.page_starts[state.page]); state.scroll_to_page_pending = false; @@ -1933,6 +2037,12 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { consumeOutlineReveal(state, viewport); const max_scroll = @as(f64, @floatFromInt(state.document_height -| viewport.pixel_h)); state.document_scroll_y = std.math.clamp(state.document_scroll_y, 0, max_scroll); + // A new layout is not a scroll: the picture is drawn there at once. + if (relaid) { + state.follow = .{}; + state.drawn_y = state.document_scroll_y; + state.direct_y = 0; + } } pub const WordProbe = if (enabled) struct { @@ -2688,23 +2798,55 @@ pub fn panPane( return panPixels(state, placed, axis, direction, display_pixels); } -pub fn scrollPane(core: *pardes.Pardes, pane: *pardes.Pane, delta_pixels: f64) bool { +/// `glide`: a notch, which the strip glides; else a touchpad's fraction, +/// which it follows as it comes. +pub fn scrollPane(core: *pardes.Pardes, pane: *pardes.Pane, delta_pixels: f64, glide: bool) bool { if (comptime !enabled) return false; const tz = tracy.zone(@src(), "pdf.scroll_notch"); defer tz.end(); const state = &(pane.pdf orelse return false); const view = ensurePaneLayout(core, pane) orelse return false; + const from = state.document_scroll_y; const result = scrollDocument(state, view, delta_pixels) orelse return false; + if (!glide) state.direct_y += state.document_scroll_y - from; if (result.active_page != state.page) activatePage(core, pane, result.active_page, false); return true; } +/// Where the strip is drawn this frame (anim 11): what moved the core's +/// position since the last frame kicks its follow, as a scroll kicks the +/// text's, all but what moved it directly; the follow then moves on to now. +fn stripAt(state: *State, view: Viewport, motion: pardes.animation.Motion, now_ns: u64) f64 { + const pace = pardes.animation.Follow.pace(motion, @floatFromInt(view.pixel_h), 0.1); + const moved = state.document_scroll_y - state.drawn_y - state.direct_y; + state.drawn_y = state.document_scroll_y; + state.direct_y = 0; + if (motion.instant) { + state.follow = .{}; + } else if (moved != 0) { + // A move past a body slides one, as a text's far jump does (G6): + // the pages between are rendered for the slide, not crossed. + const body: f64 = @floatFromInt(view.pixel_h); + state.far = @abs(moved) > body; + state.follow.kick(@floatCast(std.math.clamp(moved, -body, body)), now_ns, pace); + } else state.follow.advance(now_ns, pace); + const max_scroll = @as(f64, @floatFromInt(state.document_height -| view.pixel_h)); + return std.math.clamp(state.drawn_y + state.follow.offset, 0, max_scroll); +} + +/// A PDF strip is still on its way to where the core stands. +pub fn moving(core: *const pardes.Pardes) bool { + if (comptime !enabled) return false; + for (core.panes) |slot| if (slot) |pane| if (pane.pdf) |state| if (state.follow.moving()) return true; + return false; +} + pub fn verticalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { if (comptime !enabled) return; if (!core.native_images) return stepPanePage(core, pane, direction); const rows: u32 = @intCast(@max(1, config.wheel_rows)); const pixels = scaledStep(core.cell_pixels.h, rows); - _ = scrollPane(core, pane, @as(f64, @floatFromInt(pixels)) * direction); + _ = scrollPane(core, pane, @as(f64, @floatFromInt(pixels)) * direction, true); } pub fn horizontalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { @@ -2730,6 +2872,14 @@ pub fn draw( const state = &(pane.pdf orelse return false); const view = paneViewport(core, pane) orelse return false; const key = TintKey{ .mode = state.tint, .colors = tintColors(core) }; + ensureLayout(state, view); + // Drawn where the strip's follow is; the core's position comes back + // after (`drawn_y`), unless a search's reveal moved it as it drew, + // which is then drawn there at once. + const shown = stripAt(state, view, pardes.animation.Motion.of(core.settings.motion), core.now_ns); + state.document_scroll_y = shown; + defer state.document_scroll_y = state.drawn_y; + if (pane_id == core.active) core.frames.scroll = @floatCast(shown); const visible = renderFrame( state, core.pdf_gpa, @@ -2738,7 +2888,13 @@ pub fn draw( pardes.pdf_raster_policy, key, highlightInput(core, pane_id, pane), + state.drawn_y - shown, + state.far, ); + if (state.document_scroll_y != shown) { + state.follow = .{}; + state.drawn_y = state.document_scroll_y; + } var placed_any = false; var page = visible.first; const visible_end = visible.first + visible.len; -- cgit v1.3