diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
| commit | 6d8da8721141284cf33bb901168b26135893eb86 (patch) | |
| tree | 791d63401d4e85fbca1d597a8ce0326e70efd545 /src/pdf_view.zig | |
| parent | efd43a3a2cf03bb741366331158b368abf9ac583 (diff) | |
| parent | bf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff) | |
| download | pardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz pardes-6d8da8721141284cf33bb901168b26135893eb86.zip | |
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'src/pdf_view.zig')
| -rw-r--r-- | src/pdf_view.zig | 3871 |
1 files changed, 3871 insertions, 0 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig new file mode 100644 index 00000000..f6d26eac --- /dev/null +++ b/src/pdf_view.zig @@ -0,0 +1,3871 @@ +//! A PDF pane: the MuPDF document it views, page rasters and their placement, +//! scrolling, search, pointer selection, and the sections outline. +const panes = @import("panes.zig"); +const std = @import("std"); +const pardes = @import("pardes.zig"); +const exec = @import("exec.zig"); +const layout = @import("layout.zig"); +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 look = @import("look.zig"); +const File = panes.File; +const Output = panes.Output; + +pub const enabled = @import("pardes_config").mupdf; +pub const pdf = if (enabled) @import("mupdf") else struct { + pub const PageSize = struct { width: f32, height: f32 }; + pub const Raster = struct { + width: usize = 0, + height: usize = 0, + stride: usize = 0, + len: usize = 0, + pub const Band = struct { y: usize = 0, height: usize = 0, len: usize = 0 }; + pub fn wholePage(_: @This()) Band { + return .{}; + } + pub fn band(_: @This(), _: usize, _: usize) Band { + return .{}; + } + }; +}; +pub const Point = if (enabled) pdf.Point else void; +pub const Quad = if (enabled) pdf.Quad else void; +pub const Document = if (enabled) pdf.Document else opaque {}; +pub const OutlineInternalDestination = if (enabled) pdf.OutlineInternalDestination else void; +const raster_max = 256; +const raster_spare = 4; +pub const page_gap_px: u32 = 8; +const band_grain: usize = 64; +/// The tallest raster one buffer carries: within every GPU's texture size +/// and Kitty's 10000-pixel image limit (kitty and ghostty both). A page +/// taller than this, or than `pdf.max_owned_raster_bytes` allows, is BANDED: +/// it is held as the rows around what is on screen and rendered in chunks. +const max_band_rows: usize = 8192; +/// A banded page is rendered in chunks of this many rows, each from its own +/// render of the chunk and `chunk_overlap` rows of the page beyond both of +/// its edges, which are thrown away. A row of the page therefore always comes +/// from the same render, however the reader scrolled to it, and never from +/// the edge of one: MuPDF's rows at the edge of a clip can differ from the +/// same rows rendered whole (see `pardes_pdf_render_into`), which, butted +/// against the next chunk, would be a seam. +const chunk_rows: usize = 512; +const chunk_overlap: usize = 32; + +pub const FitMode = if (enabled) enum { width, height } else void; +pub const TintMode = if (enabled) pdf.TintMode else void; +pub const TintColors = if (enabled) pdf.TintColors else void; +pub const RenderRequest = if (enabled) pdf.RenderRequest else void; + +/// Dump records must remain recognizable as PDFs even in a build without +/// MuPDF, where Look deliberately treats them as ordinary files. +pub fn isPath(path: []const u8) bool { + return std.ascii.endsWithIgnoreCase(path, ".pdf"); +} + +pub const RasterPolicy = struct { + dpi: u16, + max_dimension: u16, + match_viewport: bool, +}; + +pub const TintKey = if (enabled) struct { + mode: TintMode, + colors: pdf.TintColors, + + pub fn eql(a: @This(), b: @This()) bool { + return a.mode == b.mode and + (a.mode == .disabled or std.meta.eql(a.colors, b.colors)); + } +} else void; + +pub const Highlight = if (enabled) pdf.Highlight else void; +pub const Highlights = if (enabled) struct { + const Page = struct { page: usize, items: []const Highlight }; + pages: [3]Page = undefined, + len: usize = 0, + /// Repaint whenever the set changes, not only when a page's raster was + /// invalidated (`HighlightInput.live`). + live: bool = false, + + pub fn forPage(highlights: @This(), page: usize) []const Highlight { + for (highlights.pages[0..highlights.len]) |group| + if (group.page == page) return group.items; + return &.{}; + } +} else void; + +pub const HighlightInput = if (enabled) struct { + hover_quads: []const Quad = &.{}, + hover_page: ?usize = null, + hover_color: [3]u8, + selection_color: [3]u8, + live: bool = false, +} else void; + +pub fn buildHighlights( + state: *const State, + arena: std.mem.Allocator, + input: HighlightInput, +) !Highlights { + if (comptime !enabled) return; + const search_len = if (state.search_results) |results| results.quads.len else 0; + const selection_len = if (state.selection) |selection| selection.quads.len else 0; + const items = try arena.alloc(Highlight, input.hover_quads.len + search_len + selection_len); + var result: Highlights = .{ .live = input.live }; + var n: usize = 0; + // Hover, search and selection can belong to three different pages. + // Group them once so raster rendering can borrow a contiguous slice. + for ([_]?usize{ input.hover_page, state.page, state.selection_page }) |candidate| { + const page = candidate orelse continue; + var seen = false; + for (result.pages[0..result.len]) |group| { + if (group.page == page) seen = true; + } + if (seen) continue; + const start = n; + if (input.hover_page == page) for (input.hover_quads) |quad| { + items[n] = pdf.Highlight.init(quad, .{ + input.hover_color[0], input.hover_color[1], input.hover_color[2], 0x2c, + }, .custom); + n += 1; + }; + if (state.page == page) if (state.search_results) |results| { + for (results.quads) |item| { + items[n] = pdf.Highlight.init(item.quad, .{ 0xff, 0xd5, 0x4f, 0x70 }, .search); + n += 1; + } + }; + if (state.selection_page == page) if (state.selection) |selection| { + for (selection.quads) |quad| { + items[n] = pdf.Highlight.init(quad, .{ + input.selection_color[0], input.selection_color[1], input.selection_color[2], 0x78, + }, .selection); + n += 1; + } + }; + result.pages[result.len] = .{ .page = page, .items = items[start..n] }; + result.len += 1; + } + return result; +} + +test "PDF selections retain their owning page across page focus" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + try std.testing.expect(state.page_count > 1); + var found = try state.document.search(gpa, 0, "Pardes"); + defer found.deinit(gpa); + const quad = found.quads[0].quad; + const point: Point = .{ + .x = (quad.ul.x + quad.ur.x + quad.ll.x + quad.lr.x) / 4, + .y = (quad.ul.y + quad.ur.y + quad.ll.y + quad.lr.y) / 4, + }; + try std.testing.expectEqual(.changed, state.setSelection(gpa, point, point, true)); + const selected = try gpa.dupe(u8, state.selection_text); + defer gpa.free(selected); + try std.testing.expect(selected.len > 0); + try std.testing.expect(state.focusLocation(gpa, 2, 0)); + try std.testing.expectEqual(@as(?usize, 0), state.selection_page); + try std.testing.expectEqualStrings(selected, state.selection_text); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const highlights = try buildHighlights(&state, arena.allocator(), .{ + .hover_quads = &.{quad}, + .hover_page = 1, + .hover_color = .{ 1, 2, 3 }, + .selection_color = .{ 4, 5, 6 }, + }); + try std.testing.expectEqual(state.selection.?.quads.len, highlights.forPage(0).len); + try std.testing.expectEqual(@as(usize, 1), highlights.forPage(1).len); + try std.testing.expectEqual(@as(usize, 0), highlights.forPage(2).len); + try std.testing.expect(state.focusLocation(gpa, 1, 0)); + try std.testing.expectEqualStrings(selected, state.selection_text); + try std.testing.expectEqual(.unchanged, state.setSelection(gpa, point, point, true)); + try std.testing.expect(state.focusLocation(gpa, 2, 0)); + state.rasters[0] = .{ .valid = true, .page = 0, .tried = true, .decorated = true }; + state.rasters_len = 1; + try std.testing.expectEqual(.changed, state.setSelection(gpa, point, point, false)); + try std.testing.expectEqual(@as(?usize, 1), state.selection_page); + try std.testing.expect(!state.rasters[0].tried); + state.clearSelection(gpa); + try std.testing.expect(state.selection_page == null); + 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); +} + +test "off the loop: pages land from lanes, a stale request's raster never lands, and a job nobody waits for is aborted" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + workers = std.testing.io; + defer workers = null; + defer dropKept(); + 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 key: TintKey = .{ .mode = .disabled, .colors = undefined }; + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + const policy = pardes.pdf_raster_policy; + const Run = struct { + /// Frames until every lane is idle; no slot ever holds a raster of + /// the `stale` request. + fn settle(st: *State, a: *std.heap.ArenaAllocator, v: Viewport, k: TintKey, h: HighlightInput, stale: ?RenderRequest) !void { + var frames: usize = 0; + while (true) : (frames += 1) { + _ = renderFrame(st, std.testing.allocator, a.allocator(), v, pardes.pdf_raster_policy, k, h, 0, false); + if (stale) |old| for (st.rasters[0..st.rasters_len]) |r| if (r.rgba.len > 0 and r.request_valid) + try std.testing.expect(!r.request.eql(old)); + if (!rendering(st)) break; + try std.testing.expect(frames < 100_000); + std.Thread.yield() catch {}; + } + } + }; + const small: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + ensureLayout(&state, small); + // The first frame asks; nothing has landed, so the loop drew nothing. + _ = renderFrame(&state, gpa, arena.allocator(), small, policy, key, none, 0, false); + try std.testing.expect(rendering(&state)); + try std.testing.expect(!pageReady(&state, small, 0)); + try Run.settle(&state, &arena, small, key, none, null); + try std.testing.expect(pageReady(&state, small, 0)); + // The page past the view was rendered ahead. + const shown = visiblePages(&state, small); + try std.testing.expect(rasterForPage(&state, shown.first + shown.len) != null); + // A new request while the old one's jobs are out, many times over: + // only the newest request's rasters are ever in a slot. + var round: u32 = 0; + while (round < 8) : (round += 1) { + const v: Viewport = .{ .pixel_w = 300 + 37 * round, .pixel_h = 300 }; + ensureLayout(&state, v); + _ = renderFrame(&state, gpa, arena.allocator(), v, policy, key, none, 0, false); + const next: Viewport = .{ .pixel_w = 301 + 37 * round, .pixel_h = 300 }; + ensureLayout(&state, next); + try Run.settle(&state, &arena, next, key, none, renderRequest(v, policy, state.fit)); + try std.testing.expect(pageReady(&state, next, state.page)); + try std.testing.expect(rasterForPage(&state, state.page).?.request.eql(renderRequest(next, policy, state.fit))); + } + // A job whose page left the strip is aborted through its cookie, and an + // aborted job is not a failure: the page is asked again when it returns. + state.invalidateAllRasters(); + const last: Viewport = .{ .pixel_w = 301 + 37 * 7, .pixel_h = 300 }; + _ = renderFrame(&state, gpa, arena.allocator(), last, policy, key, none, 0, false); + state.document_scroll_y = @floatFromInt(state.page_starts[state.page_count - 1]); + _ = renderFrame(&state, gpa, arena.allocator(), last, policy, key, none, 0, false); + for (state.lanes) |lane| if (lane.job) |job| if (job.page < state.page_count - 2) + try std.testing.expectEqual(@as(c_int, 1), @atomicLoad(c_int, &job.cookie.abort, .acquire)); + try Run.settle(&state, &arena, last, key, none, null); + for (state.rasters[0..state.rasters_len]) |r| try std.testing.expect(!r.failed); +} + +test "page sizes off the loop: the strip opens on the first page's size, and when a lane's sizes land it keeps its page and offset" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var exact = try State.open(gpa, "docs/design.pdf", 1); + defer exact.deinit(gpa); + workers = std.testing.io; + defer workers = null; + defer dropKept(); + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + try std.testing.expect(state.sizes_pending); + for (state.page_sizes) |size| try std.testing.expectEqual(exact.page_sizes[0], size); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const view: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + // Assumed wrong (half as tall) so that the landing lays out again. + for (state.page_sizes) |*size| size.height /= 2; + ensureLayout(&state, view); + state.document_scroll_y = @as(f64, @floatFromInt(state.page_starts[2])) + 0.25 * @as(f64, @floatFromInt(state.page_heights[2])); + state.drawn_y = state.document_scroll_y; + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + var frames: usize = 0; + while (state.sizes_pending) : (frames += 1) { + _ = renderFrame(&state, gpa, arena.allocator(), view, pardes.pdf_raster_policy, key, none, 0, false); + try std.testing.expect(frames < 100_000); + std.Thread.yield() catch {}; + } + applySizes(&state, gpa); + ensureLayout(&state, view); + try std.testing.expect(std.mem.eql(u8, std.mem.sliceAsBytes(exact.page_sizes), std.mem.sliceAsBytes(state.page_sizes))); + try std.testing.expectEqual(@as(usize, 2), pageAtOffset(&state, state.document_scroll_y)); + const offset = (state.document_scroll_y - @as(f64, @floatFromInt(state.page_starts[2]))) / @as(f64, @floatFromInt(state.page_heights[2])); + try std.testing.expectApproxEqAbs(@as(f64, 0.25), offset, 1e-9); + // The strip moved with its place: no kick, the motion carries on. + try std.testing.expectEqual(state.document_scroll_y, state.drawn_y); + try Wait.idle(&state, &arena, view); +} + +test "kept pages: a second open shows the page it left, bounded in bytes, the oldest going first" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + workers = std.testing.io; + defer workers = null; + defer dropKept(); + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + const request: RenderRequest = .{}; + // Five pages of 20 MiB kept: only what fits in the bound stays, newest. + { + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + for (0..5) |page| { + state.rasters[page] = .{ .valid = true, .page = page, .rgba = try gpa.alloc(u8, 20 << 20), .iw = 1, .ih = 1, .band_h = 1, .request = request, .request_valid = true, .tint_key = key }; + state.rasters_len += 1; + } + } + var total: usize = 0; + var pages: [5]bool = @splat(false); + for (kept) |k| if (k) |e| { + total += e.rgba.len; + pages[e.page] = true; + }; + try std.testing.expect(total <= kept_limit); + try std.testing.expect(pages[4] and !pages[0]); + // Opened again: page 4 is there before any render. + var again = try State.open(gpa, "docs/design.pdf", 1); + defer again.deinit(gpa); + again.rasters[0] = .{ .valid = true, .page = 4 }; + again.rasters_len = 1; + try std.testing.expect(unkeep(&again, &again.rasters[0], request, key)); + try std.testing.expectEqual(@as(usize, 20 << 20), again.rasters[0].rgba.len); + for (kept) |k| if (k) |e| try std.testing.expect(e.page != 4); +} + +/// TEST-ONLY: frames until every lane is idle. +const Wait = struct { + fn idle(st: *State, a: *std.heap.ArenaAllocator, v: Viewport) !void { + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + var frames: usize = 0; + while (rendering(st)) : (frames += 1) { + _ = renderFrame(st, std.testing.allocator, a.allocator(), v, pardes.pdf_raster_policy, key, none, 0, false); + try std.testing.expect(frames < 100_000); + std.Thread.yield() catch {}; + } + } +}; + +pub const Raster = if (enabled) struct { + valid: bool = false, + page: usize = 0, + rgba: []u8 = &.{}, + /// Full page shape; rgba contains only the band below. + iw: usize = 0, + ih: usize = 0, + band_y: usize = 0, + band_h: usize = 0, + request: pdf.RenderRequest = .{}, + request_valid: bool = false, + tried: bool = false, + decorated: bool = false, + tint_key: ?TintKey = null, + revision: u32 = 0, + /// `rgba` before its highlights and tint, kept once the page has been + /// highlighted: a highlight change repaints rows from it instead of + /// rendering the page again. + clean: []u8 = &.{}, + /// The highlights baked into `rgba` (gpa-owned), so a change repaints + /// only the rows of the quads that differ. + baked: []Highlight = &.{}, + /// `baked` is a true record (false after a render could not copy it). + baked_valid: bool = false, + /// Page rows changed since revision `patch_from`, the one last handed + /// to a shell; 0 when the pixels were replaced wholesale since. + patch_from: u32 = 0, + patch_rows: pdf.Document.Rows = .{ .start = 0, .end = 0 }, + /// The render job this slot waits for (its generation; 0: none). Only + /// that job's raster lands here: an older one, or one for a slot since + /// retired, is dropped when it comes (`land`). + pending: u32 = 0, + /// The last job for `request` failed (not aborted): not asked again + /// until the raster is invalidated or the request changes. + failed: bool = false, + /// A lane found the page too tall for one buffer: it is rendered on the + /// loop, in chunks, as before. + banded: bool = false, +} else void; + +/// Where page renders run off the loop: set by a shell that has threads +/// (gui, tty) to its Io. Null (tests, benches, hosts without threads): the +/// loop renders as it always did. +pub var workers: ?std.Io = null; + +/// Two lanes: the page in view and the next one, or the two a strip shows +/// at once; a page asked for while both are out waits a frame. +/// ponytail: two lanes, fixed; more only if a profile shows pages queuing +/// behind a slow one (a third costs another parse of the document). +const lane_count = 2; + +/// One page render, run on a lane's thread: what the loop asked for, and +/// what the lane leaves for `land` (rgba and clean are gpa-owned, handed +/// over). The lane's thread touches only this and the lane's document. +const Job = if (enabled) struct { + /// A page's raster, or every page's size (`submitSizes`). + kind: enum { render, sizes } = .render, + /// The sizes pass's answer, gpa-owned. + sizes: []pdf.PageSize = &.{}, + gen: u32, + page: usize, + request: RenderRequest, + /// Measured by the lane. + shape: pdf.Raster = .{ .width = 0, .height = 0, .stride = 0, .len = 0 }, + /// Too tall for one buffer: the loop renders it in bands (`slot.banded`). + banded: bool = false, + tint_key: TintKey, + /// The highlights baked in (gpa-owned): the slot's `baked` once it lands. + highlights: []Highlight, + keep_clean: bool, + /// A spare of the page's size, or empty: the lane allocates it. + rgba: []u8, + clean: []u8 = &.{}, + ok: bool = false, + /// Set from the loop when nothing waits for this page any more. + cookie: pdf.Cookie = std.mem.zeroes(pdf.Cookie), + done: std.atomic.Value(bool) = .init(false), +} else void; + +/// Pages kept past their pane: a second open of the same file, a Restore +/// (the old core's panes close, the new one's open), shows them on its +/// first frame instead of waiting for a lane. Only with workers (a test's +/// State leaves nothing behind). Bound: `kept_limit` bytes and +/// `kept.len` pages, the least recently kept evicted first. +/// ponytail: the file is known by its path, length and a hash of its first +/// and last 64 KiB (a PDF's trailer and xref are at its end); a change +/// that keeps all three shows the old page until it is next rendered. +const Kept = if (enabled) struct { + file: u64, + page: usize, + request: RenderRequest, + tint_key: TintKey, + rgba: []u8, + iw: usize, + ih: usize, + gpa: std.mem.Allocator, + age: u64, +} else void; +const kept_limit: usize = 64 << 20; +var kept: [16]?Kept = @splat(null); +var kept_age: u64 = 0; + +fn fileKey(path: []const u8, bytes: []const u8) u64 { + var h: std.hash.Wyhash = .init(bytes.len); + h.update(path); + h.update(bytes[0..@min(bytes.len, 64 << 10)]); + h.update(bytes[bytes.len -| (64 << 10)..]); + return h.final(); +} + +/// A slot's page as it stands (whole, unhighlighted) into the kept pages, +/// its buffer handed over; the oldest go until it fits. +fn keepPage(state: *State, gpa: std.mem.Allocator, slot: *Raster) void { + if (workers == null or slot.rgba.len == 0 or slot.baked.len != 0 or !slot.request_valid or + slot.tint_key == null or slot.band_y != 0 or slot.band_h != slot.ih or slot.rgba.len > kept_limit) return; + kept_age += 1; + for (&kept) |*k| if (k.*) |e| if (e.file == state.file and e.page == slot.page) { + e.gpa.free(e.rgba); + k.* = null; + }; + while (true) { + var total: usize = slot.rgba.len; + var oldest: ?*?Kept = null; + var free: ?*?Kept = null; + for (&kept) |*k| if (k.*) |e| { + total += e.rgba.len; + if (oldest == null or e.age < oldest.?.*.?.age) oldest = k; + } else if (free == null) { + free = k; + }; + if (total <= kept_limit) if (free) |f| { + f.* = .{ .file = state.file, .page = slot.page, .request = slot.request, .tint_key = slot.tint_key.?, .rgba = slot.rgba, .iw = slot.iw, .ih = slot.ih, .gpa = gpa, .age = kept_age }; + slot.rgba = &.{}; + return; + }; + const o = oldest.?; + o.*.?.gpa.free(o.*.?.rgba); + o.* = null; + } +} + +/// The kept page for this file, page, request and tint, into `slot`. +fn unkeep(state: *State, slot: *Raster, request: RenderRequest, tint_key: TintKey) bool { + for (&kept) |*k| if (k.*) |e| if (e.file == state.file and e.page == slot.page and e.request.eql(request) and e.tint_key.eql(tint_key)) { + k.* = null; + slot.rgba = e.rgba; + slot.iw = e.iw; + slot.ih = e.ih; + slot.band_y = 0; + slot.band_h = e.ih; + slot.request = request; + slot.request_valid = true; + slot.tint_key = tint_key; + slot.tried = true; + slot.baked_valid = true; + state.next_raster_revision +%= 1; + if (state.next_raster_revision == 0) state.next_raster_revision = 1; + slot.revision = state.next_raster_revision; + state.landed +|= 1; + return true; + }; + return false; +} + +/// Every kept page freed: a shell's end, before its allocators' leak check. +pub fn dropKept() void { + if (comptime !enabled) return; + for (&kept) |*k| if (k.*) |e| { + e.gpa.free(e.rgba); + k.* = null; + }; +} + +const Lane = if (enabled) struct { + /// The lane's cloned context and its own document (`Document.laneClone`): + /// made on the loop at its first job, opened by its first job, used by + /// one job at a time, closed with the State. + doc: ?Document = null, + job: ?*Job = null, + future: ?std.Io.Future(void) = null, + io: std.Io = undefined, + /// Its document is open and its fonts loaded by a job (`collect`). + warm: bool = false, +} else void; + +pub const SectionsOutput = if (enabled) struct { + pane: usize, + serial: u32, + revision: u32, +} else void; + +pub const SelectionUpdate = enum { failed, stationary, unchanged, changed }; + +pub const State = if (enabled) struct { + path: []u8, + document: Document, + page: usize = 0, + page_count: usize, + page_sizes: []pdf.PageSize, + page_starts: []u64, + page_heights: []u32, + document_height: u64 = 0, + layout_viewport_w: u32 = 0, + layout_viewport_h: u32 = 0, + 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, + /// Rasters installed since the last draw (the probe's `landed`). + landed: u16 = 0, + lanes: [lane_count]Lane = @splat(.{}), + render_gen: u32 = 0, + /// The paper a page shows until its raster lands, tinted (`draw`). + blank: [4]u8 = .{ 255, 255, 255, 255 }, + /// Which way the strip last went: the page past it is rendered ahead. + ahead: i2 = 1, + /// `page_sizes` past the first are assumed (the first's) until a + /// lane's pass lands, generation `sizes_gen` (0: not asked yet). + sizes_pending: bool = false, + sizes_gen: u32 = 0, + /// The pass's sizes, landed, for the next draw (gpa-owned). + sizes_ready: []pdf.PageSize = &.{}, + /// The next relayout keeps the strip's motion (a retarget, not a + /// reset): the sizes landed under it, nothing the user did. + relayout_keeps_motion: bool = false, + /// Which file this is, for the kept pages (`fileKey`). + file: u64 = 0, + spare: [raster_spare][]u8 = @splat(&.{}), + spare_len: usize = 0, + layout_anchor_pending: bool = false, + layout_anchor_page: usize = 0, + layout_anchor_fraction: f64 = 0, + next_raster_revision: u32 = 0, + scroll_travel: f64 = 0, + fit: FitMode = .width, + tint: TintMode = .filtered, + pan_x: u16 = 0, + pan_y: u16 = 0, + highlights_dirty: bool = false, + search_reveal_pending: bool = false, + search_results: ?pdf.SearchResults = null, + selection: ?pdf.Selection = null, + selection_page: ?usize = null, + selection_text: []u8 = &.{}, + selection_anchor: ?Point = null, + selection_head: ?Point = null, + drag_anchor: ?Point = null, + drag_head: ?Point = null, + text: []u8 = &.{}, + text_tried: bool = false, + text_scroll: usize = 0, + text_scroll_clamp_pending: bool = false, + search_query: []u8 = &.{}, + search_hit: usize = 0, + reveal_viewport_w: u32 = 0, + reveal_viewport_h: u32 = 0, + reveal_fit: FitMode = .width, + reveal_viewport_valid: bool = false, + outline: ?pdf.Outline = null, + outline_tried: bool = false, + sections_output: ?SectionsOutput = null, + outline_reveal_pending: ?pdf.OutlineInternalDestination = null, + + /// Read whole and opened from memory (the bridge copies), never from + /// the file itself: MuPDF reads a file lazily at every page, and the + /// path may be a mount this editor serves, which only a read that + /// gives the turn up can come back from (`filesystem.readFile`). + pub fn open(gpa: std.mem.Allocator, path: []const u8, page_one_based: usize) !@This() { + const local = filesystem.localPath(path) orelse return error.NonLocalPath; + const bytes = try filesystem.readFile(gpa, local); + defer gpa.free(bytes); + return openBytes(gpa, path, bytes, page_one_based); + } + + pub fn openBytes(gpa: std.mem.Allocator, path: []const u8, bytes: []const u8, page_one_based: usize) !@This() { + var state = try initDocument(gpa, path, try Document.openBytes(bytes), page_one_based); + state.file = fileKey(path, bytes); + return state; + } + + fn initDocument(gpa: std.mem.Allocator, path: []const u8, opened: Document, page_one_based: usize) !@This() { + var document = opened; + errdefer document.deinit(); + const page_sizes = try gpa.alloc(pdf.PageSize, document.pages); + errdefer gpa.free(page_sizes); + // With lanes, only the first page is measured here and the strip + // is laid out as if every page had its size, until a lane's pass + // over the rest lands (`submitSizes`): 5363 pages of the SDM took + // the open 700 ms in Debug, 26 in ReleaseSafe, on the loop. + const assumed = workers != null and document.pages > 1; + if (assumed) { + @memset(page_sizes, try document.pageSize(0)); + } else for (page_sizes, 0..) |*size, page| size.* = try document.pageSize(page); + const page_starts = try gpa.alloc(u64, document.pages); + errdefer gpa.free(page_starts); + const page_heights = try gpa.alloc(u32, document.pages); + errdefer gpa.free(page_heights); + const owned_path = try gpa.dupe(u8, path); + errdefer gpa.free(owned_path); + return .{ + .path = owned_path, + .document = document, + .sizes_pending = assumed, + .page = if (page_one_based > 0) + @min(page_one_based - 1, document.pages - 1) + else + 0, + .page_count = document.pages, + .page_sizes = page_sizes, + .page_starts = page_starts, + .page_heights = page_heights, + }; + } + + pub fn reload(state: *@This(), gpa: std.mem.Allocator) !void { + const preserve_anchor = !state.scroll_to_page_pending and + (state.layout_anchor_pending or + (state.layout_valid and state.page_count > 0 and state.document_height > 0)); + var anchor_page: usize = state.layout_anchor_page; + var anchor_fraction: f64 = state.layout_anchor_fraction; + if (preserve_anchor and !state.layout_anchor_pending) { + anchor_page = pageAtOffset(state, state.document_scroll_y); + const start: f64 = @floatFromInt(state.page_starts[anchor_page]); + const height: f64 = @floatFromInt(@max(@as(u32, 1), state.page_heights[anchor_page])); + anchor_fraction = std.math.clamp( + (state.document_scroll_y - start) / height, + 0, + 1, + ); + } + + var fresh = try @This().open(gpa, state.path, state.page + 1); + errdefer fresh.deinit(gpa); + if (state.search_query.len > 0) + fresh.search_query = try gpa.dupe(u8, state.search_query); + + fresh.fit = state.fit; + fresh.tint = state.tint; + fresh.pan_x = state.pan_x; + fresh.pan_y = state.pan_y; + fresh.text_scroll = state.text_scroll; + fresh.text_scroll_clamp_pending = true; + fresh.search_hit = state.search_hit; + fresh.highlights_dirty = fresh.search_query.len > 0; + fresh.search_reveal_pending = state.search_reveal_pending; + fresh.reveal_viewport_w = state.reveal_viewport_w; + fresh.reveal_viewport_h = state.reveal_viewport_h; + fresh.reveal_fit = state.reveal_fit; + fresh.reveal_viewport_valid = state.reveal_viewport_valid; + // Revisions are part of the backend texture key. Resetting this counter + // while the pane serial stays live can alias a cached pre-reload page. + fresh.next_raster_revision = state.next_raster_revision; + fresh.sections_output = state.sections_output; + if (preserve_anchor) { + fresh.scroll_to_page_pending = false; + fresh.layout_anchor_pending = true; + fresh.layout_anchor_page = anchor_page; + fresh.layout_anchor_fraction = anchor_fraction; + } + + var old = state.*; + state.* = fresh; + old.deinit(gpa); + } + + pub fn invalidateRaster(state: *@This(), page: usize) void { + if (rasterForPage(state, page)) |raster| raster.tried = false; + } + + pub fn invalidateAllRasters(state: *@This()) void { + for (state.rasters[0..state.rasters_len]) |*raster| { + if (raster.valid) raster.tried = false; + } + } + + fn retireRgba(state: *@This(), gpa: std.mem.Allocator, rgba: []u8) void { + if (rgba.len == 0) return; + if (state.spare_len == state.spare.len) return gpa.free(rgba); + state.spare[state.spare_len] = rgba; + state.spare_len += 1; + } + + fn retireRaster(state: *@This(), gpa: std.mem.Allocator, raster: *Raster) void { + if (raster.clean.len > 0) gpa.free(raster.clean); + gpa.free(raster.baked); + state.retireRgba(gpa, raster.rgba); + raster.* = .{}; + } + + fn claimRgba(state: *@This(), gpa: std.mem.Allocator, bytes: usize) ?[]u8 { + for (state.spare[0..state.spare_len], 0..) |candidate, index| { + if (candidate.len != bytes) continue; + state.spare_len -= 1; + state.spare[index] = state.spare[state.spare_len]; + return candidate; + } + return gpa.alloc(u8, bytes) catch null; + } + + fn trimSpares(state: *@This(), gpa: std.mem.Allocator) void { + while (state.spare_len > 1) { + state.spare_len -= 1; + gpa.free(state.spare[state.spare_len]); + } + } + + fn dropSearchResults(state: *@This(), gpa: std.mem.Allocator) void { + if (state.search_results) |*results| results.deinit(gpa); + state.search_results = null; + } + + fn dropSelection(state: *@This(), gpa: std.mem.Allocator) void { + if (state.selection) |*selection| selection.deinit(gpa); + state.selection = null; + state.selection_page = null; + if (state.selection_text.len > 0) gpa.free(state.selection_text); + state.selection_text = &.{}; + state.selection_anchor = null; + state.selection_head = null; + } + + pub fn setSelection( + state: *@This(), + gpa: std.mem.Allocator, + start: Point, + end: Point, + invalidate_raster: bool, + ) SelectionUpdate { + if (state.selection != null and state.selection_page == state.page and + state.selection_anchor != null and state.selection_head != null and + state.selection_anchor.?.x == start.x and state.selection_anchor.?.y == start.y and + state.selection_head.?.x == end.x and state.selection_head.?.y == end.y) return .unchanged; + var selection = state.document.select(gpa, state.page, start, end) catch return .failed; + const text = state.document.copySelection( + gpa, + state.page, + selection.start, + selection.end, + ) catch { + selection.deinit(gpa); + return .failed; + }; + + const old_page = state.selection_page; + state.dropSelection(gpa); + state.selection = selection; + state.selection_page = state.page; + state.selection_text = text; + state.selection_anchor = start; + state.selection_head = end; + if (old_page) |page| if (page != state.page) state.invalidateRaster(page); + if (invalidate_raster) state.invalidateRaster(state.page); + return .changed; + } + + pub fn clearDrag(state: *@This()) void { + state.drag_anchor = null; + state.drag_head = null; + } + + pub fn clearSelection(state: *@This(), gpa: std.mem.Allocator) void { + const page = state.selection_page; + state.dropSelection(gpa); + if (page) |selected_page| state.invalidateRaster(selected_page); + } + + pub fn cancelChrome(state: *@This(), gpa: std.mem.Allocator) void { + state.clearDrag(); + state.clearSelection(gpa); + if (state.search_query.len == 0) return; + state.dropSearchQuery(gpa); + state.invalidateRaster(state.page); + } + + fn invalidatePage(state: *@This(), gpa: std.mem.Allocator) void { + state.dropSearchResults(gpa); + state.clearDrag(); + if (state.text.len > 0) gpa.free(state.text); + state.text = &.{}; + state.text_tried = false; + state.text_scroll = 0; + state.text_scroll_clamp_pending = false; + state.highlights_dirty = state.search_query.len > 0; + state.search_reveal_pending = state.search_query.len > 0; + state.search_hit = 0; + } + + fn dropSearchQuery(state: *@This(), gpa: std.mem.Allocator) void { + if (state.search_query.len > 0) gpa.free(state.search_query); + state.search_query = &.{}; + state.search_hit = 0; + state.dropSearchResults(gpa); + state.highlights_dirty = false; + state.search_reveal_pending = false; + } + + pub fn setSearchQuery(state: *@This(), gpa: std.mem.Allocator, query: []const u8) !void { + if (std.mem.eql(u8, state.search_query, query)) return; + const owned = try gpa.dupe(u8, query); + state.dropSearchQuery(gpa); + state.search_query = owned; + state.highlights_dirty = query.len > 0; + state.search_reveal_pending = query.len > 0; + state.invalidateRaster(state.page); + } + + pub fn ensureText(state: *@This(), gpa: std.mem.Allocator) []const u8 { + if (!state.text_tried) { + state.text_tried = true; + state.text = state.document.pageText(gpa, state.page) catch &.{}; + } + if (state.text_scroll_clamp_pending) { + const lines = std.mem.count(u8, state.text, "\n") + 1; + state.text_scroll = @min(state.text_scroll, lines - 1); + state.text_scroll_clamp_pending = false; + } + return state.text; + } + + pub fn resolveSearch(state: *@This(), gpa: std.mem.Allocator) void { + if (!state.highlights_dirty) return; + state.highlights_dirty = false; + state.dropSearchResults(gpa); + if (state.search_query.len == 0) return; + const results = state.document.search(gpa, state.page, state.search_query) catch return; + state.search_hit = if (results.hit_count == 0) + 0 + else + @min(state.search_hit, results.hit_count - 1); + state.search_results = results; + } + + /// Whether page `page` (one-based) holds search hit `hit` (one-based) of + /// the pane's search: none asked, no hit is there. + pub fn hitExists(state: *@This(), gpa: std.mem.Allocator, page: usize, hit: usize) bool { + if (hit == 0) return true; + if (state.search_query.len == 0 or page == 0 or page > state.page_count) return false; + var results = state.document.search(gpa, page - 1, state.search_query) catch return false; + defer results.deinit(gpa); + return hit <= results.hit_count; + } + + pub fn ensureOutline(state: *@This(), gpa: std.mem.Allocator) ?*const pdf.Outline { + if (!state.outline_tried) { + state.outline_tried = true; + state.outline = state.document.outline(gpa) catch null; + } + return if (state.outline) |*outline| outline else null; + } + + pub fn renderSections( + state: *@This(), + pdf_gpa: std.mem.Allocator, + output_gpa: std.mem.Allocator, + ) ![]u8 { + const entries: []const pdf.OutlineEntry = if (state.ensureOutline(pdf_gpa)) |outline| + outline.entries + else + &.{}; + return SectionRows.render(output_gpa, state.path, entries); + } + + pub fn sectionDestination( + state: *@This(), + gpa: std.mem.Allocator, + ordinal: usize, + ) ?pdf.OutlineDestination { + const outline = state.ensureOutline(gpa) orelse return null; + return SectionRows.resolve(outline.entries, ordinal); + } + + /// Apply the one-based page/hit location encoded in a PDF search row. + /// Returns whether host pane cursor chrome must be reset. + pub fn focusLocation( + state: *@This(), + gpa: std.mem.Allocator, + line: usize, + column: usize, + ) bool { + const changed = line > 0 and state.activatePage(gpa, line - 1, true); + if (column > 0 and state.search_query.len > 0) { + state.search_hit = column - 1; + state.search_reveal_pending = true; + } + return changed; + } + + /// Change the document page while leaving pane cursor/selection chrome to + /// the UI adapter. Returns whether that pane-local chrome must be reset. + pub fn activatePage( + state: *@This(), + gpa: std.mem.Allocator, + page: usize, + reveal: bool, + ) bool { + state.outline_reveal_pending = null; + const next = @min(page, state.page_count -| 1); + const changed = next != state.page; + if (changed) { + state.invalidatePage(gpa); + state.page = next; + } + if (reveal) { + state.scroll_to_page_pending = true; + if (state.layout_valid) { + state.document_scroll_y = @floatFromInt(state.page_starts[next]); + state.scroll_to_page_pending = false; + } + } else { + state.search_reveal_pending = false; + } + return changed; + } + + pub fn toggleFit(state: *@This()) void { + state.fit = if (state.fit == .width) .height else .width; + state.pan_x = 0; + state.pan_y = 0; + state.layout_valid = false; + state.scroll_to_page_pending = true; + state.search_reveal_pending = state.search_query.len > 0; + } + + pub fn toggleTint(state: *@This()) void { + state.tint = state.tint.next(); + state.invalidateAllRasters(); + } + + pub fn queueOutlineReveal( + state: *@This(), + destination: pdf.OutlineInternalDestination, + ) void { + state.scroll_to_page_pending = false; + state.layout_anchor_pending = false; + state.outline_reveal_pending = destination; + } + + pub fn deinit(state: *@This(), gpa: std.mem.Allocator) void { + // Every lane stopped and joined, its job dropped, its document + // closed, before the document its context was cloned from. + for (&state.lanes) |*lane| { + if (lane.job) |job| { + @atomicStore(c_int, &job.cookie.abort, 1, .release); + pardes.turn.yield(); + lane.future.?.await(lane.io); + pardes.turn.back(); + job.gen = 0; + land(state, gpa, job); + } + if (lane.doc) |*doc| doc.deinit(); + lane.* = .{}; + } + gpa.free(state.path); + for (state.rasters[0..state.rasters_len]) |*raster| keepPage(state, gpa, raster); + for (state.rasters[0..state.rasters_len]) |raster| { + if (raster.rgba.len > 0) gpa.free(raster.rgba); + if (raster.clean.len > 0) gpa.free(raster.clean); + gpa.free(raster.baked); + } + for (state.spare[0..state.spare_len]) |rgba| gpa.free(rgba); + if (state.sizes_ready.len > 0) gpa.free(state.sizes_ready); + gpa.free(state.page_sizes); + gpa.free(state.page_starts); + gpa.free(state.page_heights); + if (state.text.len > 0) gpa.free(state.text); + if (state.search_query.len > 0) gpa.free(state.search_query); + if (state.search_results) |*results| results.deinit(gpa); + if (state.selection) |*selection| selection.deinit(gpa); + if (state.selection_text.len > 0) gpa.free(state.selection_text); + if (state.outline) |*outline| outline.deinit(gpa); + state.document.deinit(); + state.* = undefined; + } +} else void; + +pub const SearchOutput = struct { + bytes: usize = 0, + rows: usize = 0, + anchor: ?usize = null, +}; + +pub fn textLines( + state: *State, + gpa: std.mem.Allocator, + arena: std.mem.Allocator, +) ![]const []const u8 { + if (comptime !enabled) return &.{}; + const page_text = state.ensureText(gpa); + const lines = try arena.alloc([]const u8, std.mem.count(u8, page_text, "\n") + 1); + var it = std.mem.splitScalar(u8, page_text, '\n'); + var n: usize = 0; + while (it.next()) |line| : (n += 1) lines[n] = line; + return lines; +} + +pub fn visibleText( + state: *State, + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + max_rows: usize, +) ![]const u8 { + if (comptime !enabled) return ""; + const page_text = state.ensureText(gpa); + var start: usize = 0; + for (0..state.text_scroll) |_| { + const newline = std.mem.indexOfScalarPos(u8, page_text, start, '\n') orelse + return arena.dupe(u8, ""); + start = newline + 1; + } + if (max_rows == 0) return arena.dupe(u8, ""); + + var end = start; + var row: usize = 0; + while (row < max_rows) : (row += 1) { + const newline = std.mem.indexOfScalarPos(u8, page_text, end, '\n') orelse { + end = page_text.len; + break; + }; + if (row + 1 == max_rows) { + end = newline; + break; + } + end = newline + 1; + } + return arena.dupe(u8, page_text[start..end]); +} + +/// Materialize exact MuPDF logical hits as `path:PAGE:HIT query` rows. The +/// same hit numbering drives persistent highlights and later reveal actions. +pub fn searchRows( + state: *State, + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + pattern: []const u8, + from_cursor: bool, + out: []u8, +) !SearchOutput { + if (comptime !enabled) return .{}; + try state.setSearchQuery(gpa, pattern); + const shown = std.fs.path.basename(state.path); + var result: SearchOutput = .{}; + const max_hits = 512; + var snippet_len = @min(pattern.len, 200); + while (snippet_len > 0 and snippet_len < pattern.len and pattern[snippet_len] & 0xc0 == 0x80) + snippet_len -= 1; + const snippet = pattern[0..snippet_len]; + for (0..state.page_count) |page| { + if (result.rows >= max_hits) break; + var found = try state.document.search(gpa, page, pattern); + defer found.deinit(gpa); + + const cursor_hit: ?usize = if (from_cursor and page == state.page and state.selection_page == page) cursor: { + const selection = state.selection orelse break :cursor null; + for (found.quads) |item| { + const q = item.quad; + const center: Point = .{ + .x = (q.ul.x + q.ur.x + q.ll.x + q.lr.x) / 4, + .y = (q.ul.y + q.ur.y + q.ll.y + q.lr.y) / 4, + }; + if (selection.contains(center)) break :cursor item.hit; + } + break :cursor null; + } else null; + + for (0..found.hit_count) |hit_index| { + if (result.rows >= max_hits) break; + const line = try std.fmt.allocPrint(arena, "{s}:{d}:{d} {s}\n", .{ + shown, page + 1, hit_index + 1, snippet, + }); + if (line.len > out.len - result.bytes) return result; + if (from_cursor and + (page < state.page or + (page == state.page and cursor_hit != null and hit_index <= cursor_hit.?))) + result.anchor = result.rows; + @memcpy(out[result.bytes..][0..line.len], line); + result.bytes += line.len; + result.rows += 1; + } + } + return result; +} + +pub const Viewport = struct { pixel_w: u32, pixel_h: u32 }; +pub const VisiblePages = struct { first: usize = 0, len: usize = 0 }; +pub const PanAxis = enum { horizontal, vertical }; +pub const PanResult = enum { moved, edge, unavailable }; +pub const ScrollResult = struct { active_page: usize }; +pub const CellPixels = struct { w: u16, h: u16 }; +pub const NormalHost = enum { + none, + leader, + command_line, + search, + search_forward, + search_backward, +}; +pub const NormalResult = struct { + host: NormalHost = .none, + page_changed: bool = false, +}; + +const PlacedGeometry = struct { + geometry: image.NativeGeometry, + pixel_offset_y: f32, +}; + +pub const PlacedRaster = if (enabled) struct { + page: usize, + revision: u32, + fit: FitMode, + pan_x: u16, + geometry: image.NativeGeometry, + pixel_offset_y: f32, + rgba: []const u8, + width: usize, + band_height: usize, +} else void; + +const VisibleRows = struct { + base: image.NativeGeometry, + y0: u32, + y1: u32, + dst_y: u32, + dst_h: u32, + pixel_offset_y: f32, +}; + +/// The raster a pane asks for. The page is shown `viewport.pixel_w` wide +/// (fit width) or `pixel_h` tall (fit height), and the raster is never +/// smaller than that, however tall the page: see `pdf.RenderRequest`. +pub fn renderRequest(viewport: Viewport, policy: RasterPolicy, fit: FitMode) RenderRequest { + if (comptime !enabled) return; + return .{ + .dpi = policy.dpi, + .minimum_width = if (policy.match_viewport) viewport.pixel_w else 0, + .minimum_height = if (policy.match_viewport) viewport.pixel_h else 0, + .max_dimension = policy.max_dimension, + .display_width = if (fit == .width) viewport.pixel_w else 0, + .display_height = if (fit == .height) viewport.pixel_h else 0, + }; +} + +fn pageHeight(size: pdf.PageSize, viewport: Viewport, fit: FitMode) u32 { + if (comptime !enabled) return 0; + if (fit == .height) return viewport.pixel_h; + const scaled = @as(f64, @floatFromInt(viewport.pixel_w)) * + @as(f64, size.height) / @as(f64, size.width); + return @max(1, @as(u32, @intFromFloat(@min( + @as(f64, @floatFromInt(std.math.maxInt(u32))), + @round(scaled), + )))); +} + +fn pageAtOffset(state: *const State, offset: f64) usize { + if (comptime !enabled) return 0; + const y: u64 = @intFromFloat(std.math.clamp( + @floor(offset), + 0, + @as(f64, @floatFromInt(state.document_height -| 1)), + )); + var lo: usize = 0; + var hi: usize = state.page_count; + while (lo + 1 < hi) { + const mid = lo + (hi - lo) / 2; + if (state.page_starts[mid] <= y) lo = mid else hi = mid; + } + const end = state.page_starts[lo] + state.page_heights[lo]; + return if (y >= end and lo + 1 < state.page_count) lo + 1 else lo; +} + +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])) - y; + const bottom = top + @as(f64, @floatFromInt(state.page_heights[page])); + 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, 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])) - y; + if (top >= h) break; + if (pageVisible(state, page, y, h)) out.len += 1; + } + return out; +} + +fn visibleContains(visible: VisiblePages, page: usize) bool { + return page >= visible.first and page - visible.first < visible.len; +} + +pub fn rasterForPage(state: *State, page: usize) ?*Raster { + if (comptime !enabled) return null; + for (state.rasters[0..state.rasters_len]) |*raster| + if (raster.valid and raster.page == page) return raster; + return null; +} + +fn rasterForPageConst(state: *const State, page: usize) ?*const Raster { + if (comptime !enabled) return null; + for (state.rasters[0..state.rasters_len]) |*raster| + if (raster.valid and raster.page == page) return raster; + return null; +} + +fn visibleRows( + state: *const State, + viewport: Viewport, + page: usize, + iw: usize, + ih: usize, +) ?VisibleRows { + if (comptime !enabled) return null; + const page_h = state.page_heights[page]; + const base = image.nativeGeometry( + iw, + ih, + viewport.pixel_w, + page_h, + switch (state.fit) { + .width => .width, + .height => .height, + }, + state.pan_x, + 0, + ) orelse return null; + if (base.dst.h == 0 or base.src.h == 0) return null; + + const scroll_floor = @floor(state.document_scroll_y); + const fractional: f32 = @floatCast(state.document_scroll_y - scroll_floor); + const scroll_i: i64 = @intFromFloat(@min( + scroll_floor, + @as(f64, @floatFromInt(std.math.maxInt(i64))), + )); + const start_i: i64 = @intCast(@min( + state.page_starts[page], + @as(u64, std.math.maxInt(i64)), + )); + const full_y = start_i - scroll_i + @as(i64, base.dst.y); + const full_bottom = full_y + @as(i64, base.dst.h); + const visible_y = @max(@as(i64, 0), full_y); + const visible_bottom = @min(@as(i64, viewport.pixel_h), full_bottom); + if (visible_bottom <= visible_y) return null; + + const rel_y0: u64 = @intCast(visible_y - full_y); + const rel_y1: u64 = @intCast(visible_bottom - full_y); + const src_y0: u32 = base.src.y + @as(u32, @intCast( + rel_y0 * base.src.h / base.dst.h, + )); + const src_y1: u32 = base.src.y + @as(u32, @intCast(@min( + @as(u64, base.src.h), + (rel_y1 * base.src.h + base.dst.h - 1) / base.dst.h, + ))); + if (src_y1 <= src_y0) return null; + return .{ + .base = base, + .y0 = src_y0, + .y1 = src_y1, + .dst_y = @intCast(visible_y), + .dst_h = @intCast(visible_bottom - visible_y), + .pixel_offset_y = -fractional, + }; +} + +fn placedGeometry( + state: *const State, + raster: *const Raster, + viewport: Viewport, + page: usize, +) ?PlacedGeometry { + if (comptime !enabled) return null; + const rows = visibleRows(state, viewport, page, raster.iw, raster.ih) orelse return null; + const band_y: u32 = @intCast(raster.band_y); + const band_end: u32 = @intCast(raster.band_y + raster.band_h); + if (rows.y0 < band_y or rows.y1 > band_end) return null; + return .{ + .geometry = .{ + .src = .{ + .x = rows.base.src.x, + .y = rows.y0 - band_y, + .w = rows.base.src.w, + .h = rows.y1 - rows.y0, + }, + .dst = .{ + .x = rows.base.dst.x, + .y = rows.dst_y, + .w = rows.base.dst.w, + .h = rows.dst_h, + }, + }, + .pixel_offset_y = rows.pixel_offset_y, + }; +} + +pub fn placedRaster(state: *const State, viewport: Viewport, page: usize) ?PlacedRaster { + if (comptime !enabled) return null; + const raster = rasterForPageConst(state, page) orelse return null; + if (raster.rgba.len == 0) return null; + const placed = placedGeometry(state, raster, viewport, page) orelse return null; + return .{ + .page = page, + .revision = raster.revision, + .fit = state.fit, + .pan_x = state.pan_x, + .geometry = placed.geometry, + .pixel_offset_y = placed.pixel_offset_y, + .rgba = raster.rgba, + .width = raster.iw, + .band_height = raster.band_h, + }; +} + +pub fn activeGeometry(state: *const State, viewport: Viewport) ?image.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; + return placed.geometry; +} + +pub fn pageAtViewportY(state: *const State, local_y: f64) ?usize { + if (comptime !enabled) return null; + if (!state.layout_valid or !std.math.isFinite(local_y) or local_y < 0) return null; + const document_y = state.document_scroll_y + local_y; + const page = pageAtOffset(state, document_y); + const start: f64 = @floatFromInt(state.page_starts[page]); + if (document_y < start or + document_y >= start + @as(f64, @floatFromInt(state.page_heights[page]))) return null; + return page; +} + +pub fn pageReady(state: *const State, viewport: Viewport, page: usize) bool { + if (comptime !enabled) return false; + return placedRaster(state, viewport, page) != null; +} + +pub fn nativeReady(state: *const State, viewport: Viewport) bool { + if (comptime !enabled) return false; + for (state.rasters[0..state.rasters_len]) |*raster| { + if (raster.valid and raster.rgba.len > 0 and + placedGeometry(state, raster, viewport, raster.page) != null) return true; + } + return false; +} + +pub fn pointAtPage( + state: *const State, + viewport: Viewport, + page: usize, + px: i64, + py: i64, + clamp_to_page: bool, +) ?Point { + if (comptime !enabled) return null; + const raster = rasterForPageConst(state, page) orelse return null; + if (raster.rgba.len == 0) return null; + const placed = placedGeometry(state, raster, viewport, page) orelse return null; + return pointAtGeometry( + placed.geometry, + placed.pixel_offset_y, + raster.iw, + raster.ih, + raster.band_y, + px, + py, + clamp_to_page, + ); +} + +fn slotShape(slot: *const Raster) pdf.Raster { + const stride = slot.iw * 4; + return .{ .width = slot.iw, .height = slot.ih, .stride = stride, .len = stride * slot.ih }; +} + +fn flinging(state: *const State, viewport: Viewport) bool { + if (comptime !enabled) return false; + return state.scroll_travel >= @as(f64, @floatFromInt(viewport.pixel_h)); +} + +/// The most rows one buffer of this page holds. +fn bandCap(shape: pdf.Raster) usize { + return @min(max_band_rows, pdf.max_owned_raster_bytes / @max(shape.stride, 1)); +} + +/// A page too tall for one buffer: see `max_band_rows`. +pub fn isBanded(shape: pdf.Raster) bool { + if (comptime !enabled) return false; + return shape.height > bandCap(shape); +} + +/// What a banded page's buffer is asked to hold. `target` is what a render +/// covers: the rows on screen and a screenful either side, so scrolling finds +/// the next rows ready (none while flinging, which only wants this frame's). +/// `keep` is what the buffer must still cover to be kept: half a screenful +/// either side, so a render happens once per half screenful scrolled, not on +/// every frame. +const Reach = enum { target, keep }; + +fn wantedBand( + state: *const State, + viewport: Viewport, + page: usize, + shape: pdf.Raster, + is_flinging: bool, + reach: Reach, +) pdf.Raster.Band { + if (isBanded(shape)) return chunkBand(state, viewport, page, shape, is_flinging, reach); + if (!is_flinging) return shape.wholePage(); + const rows = visibleRows(state, viewport, page, shape.width, shape.height) orelse + return shape.wholePage(); + const first = (@as(usize, rows.y0) / band_grain) * band_grain; + const last = std.math.divCeil(usize, @as(usize, rows.y1), band_grain) catch + return shape.wholePage(); + return shape.band(first, last * band_grain - first); +} + +fn chunkBand( + state: *const State, + viewport: Viewport, + page: usize, + shape: pdf.Raster, + is_flinging: bool, + reach: Reach, +) pdf.Raster.Band { + const rows = visibleRows(state, viewport, page, shape.width, shape.height) orelse + return shape.band(0, chunk_rows); + const seen: usize = rows.y1 - rows.y0; + const margin: usize = if (is_flinging) 0 else switch (reach) { + .target => seen, + .keep => seen / 2, + }; + const top = @as(usize, rows.y0) -| margin; + const bottom = @min(shape.height, @as(usize, rows.y1) + margin); + if (reach == .keep) return shape.band(top, bottom - top); + var first = top / chunk_rows * chunk_rows; + var last = @min(shape.height, (bottom + chunk_rows - 1) / chunk_rows * chunk_rows); + const cap = bandCap(shape) / chunk_rows * chunk_rows; + if (last - first > cap) { + // The margins give way: what is on screen always fits. + first = @as(usize, rows.y0) / chunk_rows * chunk_rows; + last = @min(shape.height, first + cap); + } + return shape.band(first, last - first); +} + +/// Fill `fresh` with the rows `want` of a banded page. Chunks the slot +/// already holds, under this request and tint and with these highlights +/// (`reuse`), are copied; the rest are rendered (`renderChunk`), then +/// highlighted and tinted as `reconcile` does a whole page. `slot.clean` +/// becomes the new rows' clean copy when `keep_clean`. +fn fillBanded( + state: *State, + gpa: std.mem.Allocator, + slot: *const Raster, + request: RenderRequest, + shape: pdf.Raster, + want: pdf.Raster.Band, + highlights: []const Highlight, + tint_key: TintKey, + fresh: []u8, + keep_clean: bool, + reuse: bool, + clean_out: *[]u8, +) bool { + const tz = tracy.zone(@src(), "pdf.fill_banded"); + defer tz.end(); + const stride = shape.stride; + const clean: []u8 = if (keep_clean) gpa.alloc(u8, fresh.len) catch return false else &.{}; + var filled = false; + defer if (!filled and clean.len > 0) gpa.free(clean); + const held = reuse and slot.rgba.len == slot.band_h * stride and + (!keep_clean or slot.clean.len == slot.rgba.len); + var scratch: []u8 = &.{}; + defer if (scratch.len > 0) gpa.free(scratch); + const end = want.y + want.height; + var y = want.y; + while (y < end) { + const rows = @min(chunk_rows - y % chunk_rows, end - y); + const at = (y - want.y) * stride; + const out = fresh[at..][0 .. rows * stride]; + if (held and slot.band_y <= y and y + rows <= slot.band_y + slot.band_h) { + const from = (y - slot.band_y) * stride; + @memcpy(out, slot.rgba[from..][0..out.len]); + if (keep_clean) @memcpy(clean[at..][0..out.len], slot.clean[from..][0..out.len]); + } else { + if (scratch.len == 0) + scratch = gpa.alloc(u8, (chunk_rows + 2 * chunk_overlap) * stride) catch return false; + const target = if (keep_clean) clean[at..][0..out.len] else out; + renderChunk(state, request, shape, slot.page, y, if (keep_clean) &.{} else highlights, target, scratch) catch + return false; + if (keep_clean) { + @memcpy(out, target); + state.document.paintHighlightsAt(slot.page, request, shape, shape.band(y, rows), highlights, out) catch + return false; + } + pdf.tintRgba(out, tint_key.mode, tint_key.colors) catch return false; + } + y += rows; + } + filled = true; + clean_out.* = clean; + return true; +} + +/// The `out.len / stride` rows of a banded page from row `y` (a chunk or +/// its end), cut from a render reaching `chunk_overlap` rows past both. +fn renderChunk( + state: *State, + request: RenderRequest, + shape: pdf.Raster, + page: usize, + y: usize, + highlights: []const Highlight, + out: []u8, + scratch: []u8, +) !void { + const tz = tracy.zone(@src(), "pdf.render_chunk"); + defer tz.end(); + const rows = out.len / shape.stride; + const top = y -| chunk_overlap; + const bottom = @min(shape.height, y + rows + chunk_overlap); + const band = shape.band(top, bottom - top); + const buf = scratch[0..band.len]; + try state.document.renderIntoAtWithPaper(page, request, shape, band, highlights, buf, pardes.platform == .macos); + @memcpy(out, buf[(y - top) * shape.stride ..][0..out.len]); +} + +fn reconcile( + state: *State, + gpa: std.mem.Allocator, + request: RenderRequest, + tint_key: TintKey, + highlights: Highlights, + visible: VisiblePages, + viewport: Viewport, +) void { + if (comptime !enabled) return; + const tz = tracy.zone(@src(), "pdf.reconcile"); + defer tz.end(); + collect(state, gpa); + defer if (workers != null) sweep(state); + if (state.sizes_pending and state.sizes_gen == 0) if (workers) |io| submitSizes(state, gpa, io); + + // Remove first so arriving pages can claim departing page buffers. Only + // the final visible set can be seen, so a fling skips crossed-over pages. + var index: usize = 0; + while (index < state.rasters_len) { + if (visibleContains(visible, state.rasters[index].page)) { + index += 1; + continue; + } + state.retireRaster(gpa, &state.rasters[index]); + state.rasters_len -= 1; + if (index != state.rasters_len) + state.rasters[index] = state.rasters[state.rasters_len]; + } + + const is_flinging = flinging(state, viewport); + var page = visible.first; + const end = visible.first + visible.len; + while (page < end) : (page += 1) { + var raster = rasterForPage(state, page); + if (raster == null) { + if (state.rasters_len == state.rasters.len) continue; + state.rasters[state.rasters_len] = .{ .valid = true, .page = page }; + state.rasters_len += 1; + raster = &state.rasters[state.rasters_len - 1]; + if (workers != null) _ = unkeep(state, raster.?, request, tint_key); + } + const slot = raster.?; + const page_highlights = highlights.forPage(page); + const decorated = page_highlights.len > 0; + const base_stale = !slot.request_valid or !slot.request.eql(request) or + slot.tint_key == null or !slot.tint_key.?.eql(tint_key) or + slot.rgba.len == 0 or slot.band_h == 0; + const uncovered = !base_stale and uncovered: { + const want = wantedBand(state, viewport, page, slotShape(slot), is_flinging, .keep); + break :uncovered slot.band_y > want.y or + slot.band_y + slot.band_h < want.y + want.height; + }; + // A banded page moving on keeps the rows it has, if they are exactly + // what a render would make now. + const reuse = !base_stale and slot.baked_valid and sameHighlights(slot.baked, page_highlights); + if (!base_stale and !uncovered) { + if (slot.tried and slot.decorated == decorated and + !(highlights.live and !sameHighlights(slot.baked, page_highlights))) continue; + // Invalidated, but it already shows exactly these highlights + // (a preview withdrawn that was never drawn, on every pointer + // move over a page): nothing to render. + if (slot.baked_valid and sameHighlights(slot.baked, page_highlights)) { + slot.tried = true; + continue; + } + // Only the highlights moved: repaint their rows in place. + if (repaintHighlights(state, gpa, slot, request, tint_key, page_highlights)) continue; + } + + // Off the loop: a lane renders the page whole and it lands on a + // later frame (`collect`); until then the slot shows what it has. + // A page too tall for one buffer is banded and stays on the loop. + if (workers) |io| { + if (pendingJob(state, slot)) |job| { + if (job.request.eql(request) and job.tint_key.eql(tint_key)) continue; + @atomicStore(c_int, &job.cookie.abort, 1, .release); + slot.pending = 0; + } + if (slot.failed and slot.tried and slot.request_valid and slot.request.eql(request)) continue; + const keep = pardes.platform != .macos and (decorated or slot.clean.len > 0); + switch (submit(state, gpa, io, slot, request, tint_key, page_highlights, keep)) { + .sent, .busy => continue, + .loop => {}, + } + } + + slot.tried = true; + slot.request = request; + slot.request_valid = true; + const shape_or_null = shape: { + const tz_measure = tracy.zone(@src(), "pdf.measure"); + defer tz_measure.end(); + break :shape state.document.measureRenderAt(page, request) catch null; + }; + const shape = shape_or_null orelse continue; + const want = wantedBand(state, viewport, page, shape, is_flinging, .target); + const fresh = state.claimRgba(gpa, want.len) orelse continue; + // A highlighted page keeps its clean rows for repaints. + const keep_clean = pardes.platform != .macos and (decorated or slot.clean.len > 0); + const filled = if (isBanded(shape)) banded: { + var clean: []u8 = &.{}; + if (!fillBanded(state, gpa, slot, request, shape, want, page_highlights, tint_key, fresh, keep_clean, reuse, &clean)) + break :banded false; + if (slot.clean.len > 0) gpa.free(slot.clean); + slot.clean = clean; + break :banded true; + } else filled: { + { + const tz_render = tracy.zone(@src(), "pdf.render_into"); + defer tz_render.end(); + state.document.renderIntoAtWithPaper( + page, + request, + shape, + want, + if (keep_clean) &.{} else page_highlights, + fresh, + pardes.platform == .macos, + ) catch break :filled false; + if (slot.clean.len != 0 and (!keep_clean or slot.clean.len != fresh.len)) { + gpa.free(slot.clean); + slot.clean = &.{}; + } + if (keep_clean) { + if (slot.clean.len == 0) slot.clean = gpa.alloc(u8, fresh.len) catch &.{}; + if (slot.clean.len == fresh.len) @memcpy(slot.clean, fresh); + state.document.paintHighlightsAt(page, request, shape, want, page_highlights, fresh) catch + break :filled false; + } + } + const tz_tint = tracy.zone(@src(), "pdf.tint"); + defer tz_tint.end(); + pdf.tintRgba(fresh, tint_key.mode, tint_key.colors) catch + break :filled false; + break :filled true; + }; + if (!filled) { + state.retireRgba(gpa, fresh); + // `clean` may already hold the band that failed. + if (slot.clean.len > 0) gpa.free(slot.clean); + slot.clean = &.{}; + continue; + } + + state.retireRgba(gpa, slot.rgba); + slot.rgba = fresh; + slot.iw = shape.width; + slot.ih = shape.height; + slot.band_y = want.y; + slot.band_h = want.height; + slot.decorated = decorated; + slot.tint_key = tint_key; + if (gpa.dupe(Highlight, page_highlights)) |copy| { + gpa.free(slot.baked); + slot.baked = copy; + slot.baked_valid = true; + } else |_| { + // Unrecorded highlights cannot be compared or diffed. + slot.baked_valid = false; + if (slot.clean.len > 0) gpa.free(slot.clean); + slot.clean = &.{}; + } + slot.patch_from = 0; + slot.patch_rows = .{ .start = 0, .end = 0 }; + state.next_raster_revision +%= 1; + if (state.next_raster_revision == 0) state.next_raster_revision = 1; + slot.revision = state.next_raster_revision; + state.landed +|= 1; + } + state.trimSpares(gpa); +} + +fn pendingJob(state: *State, slot: *const Raster) ?*Job { + if (slot.pending == 0) return null; + for (state.lanes) |lane| if (lane.job) |job| if (job.gen == slot.pending) return job; + return null; +} + +/// Hands `slot`'s page to a free lane: `busy` when both are out (asked +/// again next frame), `loop` when the page is banded or no lane can be had +/// (the loop renders it as before). +fn submit( + state: *State, + gpa: std.mem.Allocator, + io: std.Io, + slot: *Raster, + request: RenderRequest, + tint_key: TintKey, + page_highlights: []const Highlight, + keep_clean: bool, +) enum { sent, busy, loop } { + if (slot.banded) return .loop; + // A lane's first job parses the document again and loads its fonts: + // a second cold lane beside the first only slowed both (the SDM's + // first page 1.7 -> 2.5 s in Debug), so a lane starts once the one + // before it is warm. + const lane = for (&state.lanes, 0..) |*l, i| { + if (l.job == null and (l.doc != null or i == 0 or state.lanes[i - 1].warm)) break l; + } else return .busy; + if (lane.doc == null) lane.doc = state.document.laneClone() catch return .loop; + const highlights = gpa.dupe(Highlight, page_highlights) catch return .loop; + const job = gpa.create(Job) catch { + gpa.free(highlights); + return .loop; + }; + state.render_gen +%= 1; + if (state.render_gen == 0) state.render_gen = 1; + // A spare is handed over: the lane keeps it if it is the page's size + // (most pages of a document are), else frees it and allocates. + const spare: []u8 = if (state.spare_len > 0) spare: { + state.spare_len -= 1; + break :spare state.spare[state.spare_len]; + } else &.{}; + job.* = .{ + .gen = state.render_gen, + .page = slot.page, + .request = request, + .tint_key = tint_key, + .highlights = highlights, + .keep_clean = keep_clean, + .rgba = spare, + }; + lane.future = io.concurrent(renderJob, .{ lane.doc.?, job, gpa }) catch { + state.retireRgba(gpa, spare); + gpa.free(highlights); + gpa.destroy(job); + return .loop; + }; + lane.job = job; + lane.io = io; + slot.pending = job.gen; + return .sent; +} + +/// The sizes landed: laid out again where the view is, by page and offset +/// in it (a Look made meanwhile landed on the assumed layout and stays on +/// its page), and the strip's glide carries on through it (`ensureLayout`). +/// A document whose pages are all one size never gets here. +pub fn applySizes(state: *State, gpa: std.mem.Allocator) void { + if (comptime !enabled) return; + if (state.sizes_ready.len == 0) return; + if (state.layout_valid and !state.scroll_to_page_pending and !state.layout_anchor_pending) captureLayoutAnchor(state); + @memcpy(state.page_sizes, state.sizes_ready); + gpa.free(state.sizes_ready); + state.sizes_ready = &.{}; + state.layout_valid = false; + state.relayout_keeps_motion = true; +} + +/// The sizes of every page, measured on the last lane (its first job, so +/// its document's page tree is loaded when it starts rendering). +fn submitSizes(state: *State, gpa: std.mem.Allocator, io: std.Io) void { + const lane = &state.lanes[lane_count - 1]; + if (lane.job != null) return; + if (lane.doc == null) lane.doc = state.document.laneClone() catch return; + const job = gpa.create(Job) catch return; + state.render_gen +%= 1; + if (state.render_gen == 0) state.render_gen = 1; + job.* = .{ .kind = .sizes, .gen = state.render_gen, .page = 0, .request = .{}, .tint_key = .{ .mode = .disabled, .colors = undefined }, .highlights = &.{}, .keep_clean = false, .rgba = &.{} }; + lane.future = io.concurrent(renderJob, .{ lane.doc.?, job, gpa }) catch { + gpa.destroy(job); + // No lane: measured here after all. + for (state.page_sizes, 0..) |*size, page| size.* = state.document.pageSize(page) catch size.*; + state.sizes_pending = false; + state.layout_valid = false; + return; + }; + lane.job = job; + lane.io = io; + state.sizes_gen = job.gen; +} + +/// A lane's thread: the page rendered whole into the job's buffer, its +/// clean copy kept, highlighted and tinted, all as `reconcile` does on the +/// loop, under the job's cookie. It touches the job and the lane's own +/// document (`doc` is a copy of its handle), nothing of the State. +fn renderJob(lane_doc: Document, job: *Job, gpa: std.mem.Allocator) void { + const tz = tracy.zone(@src(), "pdf.lane_render"); + defer tz.end(); + var doc = lane_doc; + defer job.done.store(true, .release); + doc.laneOpen() catch return; + if (job.kind == .sizes) { + job.sizes = gpa.alloc(pdf.PageSize, doc.pages) catch return; + for (job.sizes, 0..) |*size, page| { + if (page % 64 == 0 and @atomicLoad(c_int, &job.cookie.abort, .acquire) != 0) return; + size.* = doc.pageSize(page) catch return; + } + job.ok = true; + return; + } + doc.setCookie(&job.cookie); + defer doc.setCookie(null); + // Measured here, on the lane's own document: loading a page (its links + // and annotations) cost the loop 6-7 ms a page of the SDM in Debug. + job.shape = doc.measureRenderAt(job.page, job.request) catch return; + if (isBanded(job.shape)) { + job.banded = true; + return; + } + if (job.rgba.len != job.shape.len) { + if (job.rgba.len > 0) gpa.free(job.rgba); + job.rgba = &.{}; + job.rgba = pardes.memory.unfilled(gpa, job.shape.len) catch return; + } + const band = job.shape.wholePage(); + doc.renderIntoAtWithPaper(job.page, job.request, job.shape, band, if (job.keep_clean) &.{} else job.highlights, job.rgba, pardes.platform == .macos) catch return; + if (job.keep_clean) { + job.clean = pardes.memory.dupeUnfilled(gpa, job.rgba) catch return; + doc.paintHighlightsAt(job.page, job.request, job.shape, band, job.highlights, job.rgba) catch return; + } + pdf.tintRgba(job.rgba, job.tint_key.mode, job.tint_key.colors) catch return; + job.ok = true; +} + +/// The jobs that are done, joined (a return away) and landed. +fn collect(state: *State, gpa: std.mem.Allocator) void { + for (&state.lanes) |*lane| { + const job = lane.job orelse continue; + if (!job.done.load(.acquire)) continue; + lane.future.?.await(lane.io); + lane.future = null; + lane.job = null; + lane.warm = true; + land(state, gpa, job); + } +} + +/// A job's raster into the slot waiting for its generation, swapped in +/// whole (no pixel is copied on the loop); a stale, failed or aborted one +/// is dropped. Frees the job. +fn land(state: *State, gpa: std.mem.Allocator, job: *Job) void { + const tz = tracy.zone(@src(), "pdf.land"); + defer tz.end(); + defer gpa.destroy(job); + if (job.kind == .sizes) { + defer if (job.sizes.len > 0) gpa.free(job.sizes); + if (job.gen == 0 or job.gen != state.sizes_gen) return; + state.sizes_pending = false; + if (!job.ok or job.sizes.len != state.page_count) return; // ponytail: the assumed sizes stay + if (std.mem.eql(u8, std.mem.sliceAsBytes(job.sizes), std.mem.sliceAsBytes(state.page_sizes))) return; + // Applied by the next draw, before it lays out (`applySizes`): here + // the strip may stand where it is drawn, not where the core is. + if (state.sizes_ready.len > 0) gpa.free(state.sizes_ready); + state.sizes_ready = job.sizes; + job.sizes = &.{}; + return; + } + const slot: ?*Raster = if (job.gen == 0) null else for (state.rasters[0..state.rasters_len]) |*r| { + if (r.valid and r.pending == job.gen) break r; + } else null; + if (slot == null or !job.ok) { + if (slot) |s| { + s.pending = 0; + s.banded = job.banded; + if (@atomicLoad(c_int, &job.cookie.abort, .acquire) == 0 and !job.banded) { + s.failed = true; + s.tried = true; + s.request = job.request; + s.request_valid = true; + } + } + state.retireRgba(gpa, job.rgba); + if (job.clean.len > 0) gpa.free(job.clean); + gpa.free(job.highlights); + return; + } + const s = slot.?; + s.pending = 0; + s.failed = false; + s.tried = true; + state.retireRgba(gpa, s.rgba); + s.rgba = job.rgba; + if (s.clean.len > 0) gpa.free(s.clean); + s.clean = job.clean; + s.iw = job.shape.width; + s.ih = job.shape.height; + s.band_y = 0; + s.band_h = job.shape.height; + s.request = job.request; + s.request_valid = true; + s.tint_key = job.tint_key; + s.decorated = job.highlights.len > 0; + gpa.free(s.baked); + s.baked = job.highlights; + s.baked_valid = true; + s.patch_from = 0; + s.patch_rows = .{ .start = 0, .end = 0 }; + state.next_raster_revision +%= 1; + if (state.next_raster_revision == 0) state.next_raster_revision = 1; + s.revision = state.next_raster_revision; + state.landed +|= 1; +} + +/// A job no slot waits for any more (its page left the strip, its request +/// changed) is told to stop: MuPDF sees the cookie at its next operator. +fn sweep(state: *State) void { + for (state.lanes) |lane| if (lane.job) |job| if (job.kind == .render) { + const wanted = for (state.rasters[0..state.rasters_len]) |r| { + if (r.valid and r.pending == job.gen) break true; + } else false; + if (!wanted) @atomicStore(c_int, &job.cookie.abort, 1, .release); + }; +} + +/// Some page of this State is being rendered on a lane (the frames after +/// look for it, as a picture's decode is looked for). +pub fn rendering(state: *const State) bool { + if (comptime !enabled) return false; + for (state.lanes) |lane| if (lane.job != null) return true; + return false; +} + +/// Repaint only the rows where the old and new highlights differ: the +/// clean rows kept from the page's last render, MuPDF's highlight pass +/// (the whole new set) over them, the per-pixel tint — the operations a +/// full render performs, in its order, on fewer rows and without running +/// the page again. False leaves the slot for the full path. +fn repaintHighlights( + state: *State, + gpa: std.mem.Allocator, + slot: *Raster, + request: RenderRequest, + tint_key: TintKey, + page_highlights: []const Highlight, +) bool { + const tz = tracy.zone(@src(), "pdf.repaint_highlights"); + defer tz.end(); + if (slot.clean.len != slot.rgba.len or !slot.baked_valid) return false; + if (sameHighlights(slot.baked, page_highlights)) { + slot.tried = true; + return true; + } + const baked = gpa.dupe(Highlight, page_highlights) catch return false; + const changed = changedHighlights(gpa, slot.baked, page_highlights) catch { + gpa.free(baked); + return false; + }; + defer gpa.free(changed); + var dirty = state.document.highlightRows(slot.page, request, changed) catch { + gpa.free(baked); + return false; + }; + slot.tried = true; + dirty.start = @max(dirty.start, slot.band_y); + dirty.end = @min(dirty.end, slot.band_y + slot.band_h); + if (dirty.start < dirty.end) { + const shape = slotShape(slot); + const band = shape.band(dirty.start, dirty.end - dirty.start); + const offset = (dirty.start - slot.band_y) * shape.stride; + const out = slot.rgba[offset..][0..band.len]; + @memcpy(out, slot.clean[offset..][0..band.len]); + // On failure the rows are half-made: let the full path redo them. + state.document.paintHighlightsAt(slot.page, request, shape, band, page_highlights, out) catch { + gpa.free(baked); + slot.tried = false; + return false; + }; + pdf.tintRgba(out, tint_key.mode, tint_key.colors) catch { + gpa.free(baked); + slot.tried = false; + return false; + }; + slot.patch_rows = unionRows(slot.patch_rows, dirty); + state.next_raster_revision +%= 1; + if (state.next_raster_revision == 0) state.next_raster_revision = 1; + slot.revision = state.next_raster_revision; + } + gpa.free(slot.baked); + slot.baked = baked; + slot.decorated = page_highlights.len > 0; + return true; +} + +fn sameHighlights(a: []const Highlight, b: []const Highlight) bool { + return std.mem.eql(u8, std.mem.sliceAsBytes(a), std.mem.sliceAsBytes(b)); +} + +/// The highlights in one set and not the other, as multisets (a quad +/// painted twice blends twice). When only the order changed, blending +/// order did, so everything counts. +// ponytail: quadratic match; past 64k comparisons every quad counts. +fn changedHighlights(gpa: std.mem.Allocator, old: []const Highlight, new: []const Highlight) ![]Highlight { + const out = try gpa.alloc(Highlight, old.len + new.len); + errdefer gpa.free(out); + var n: usize = 0; + if (old.len * new.len <= 1 << 16) { + const matched = try gpa.alloc(bool, new.len); + defer gpa.free(matched); + @memset(matched, false); + for (old) |h| { + for (new, matched) |candidate, *used| { + if (!used.* and std.mem.eql(u8, std.mem.asBytes(&h), std.mem.asBytes(&candidate))) { + used.* = true; + break; + } + } else { + out[n] = h; + n += 1; + } + } + for (new, matched) |h, used| if (!used) { + out[n] = h; + n += 1; + }; + } + if (n == 0) { + @memcpy(out[0..old.len], old); + @memcpy(out[old.len..], new); + return out; + } + // Freed by length: hand back exactly what the caller will free. + return gpa.realloc(out, n); +} + +fn unionRows(a: pdf.Document.Rows, b: pdf.Document.Rows) pdf.Document.Rows { + if (a.start >= a.end) return b; + if (b.start >= b.end) return a; + return .{ .start = @min(a.start, b.start), .end = @max(a.end, b.end) }; +} + +pub fn renderFrame( + state: *State, + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + viewport: Viewport, + 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); + // 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; + // Off the loop, the page past the strip the way it last went is asked + // for too, after the ones in view: rendered ahead, as paint-ahead does. + const ahead = struct { + fn f(st: *const State, v: VisiblePages) VisiblePages { + if (workers == null or v.len == 0) return v; + if (st.ahead > 0 and v.first + v.len < st.page_count) return .{ .first = v.first, .len = v.len + 1 }; + if (st.ahead < 0 and v.first > 0) return .{ .first = v.first - 1, .len = v.len + 1 }; + return v; + } + }.f; + var visible = span(state, viewport, path, far); + reconcile(state, gpa, request, tint_key, highlights, ahead(state, visible), viewport); + + rearmSearchReveal(state, viewport); + revealSearch(state, viewport, activeGeometry(state, viewport)); + visible = span(state, viewport, path, far); + reconcile(state, gpa, request, tint_key, highlights, ahead(state, visible), viewport); + return visible; +} + +pub fn normalizedPixel(value: f32, dimension: usize) u32 { + const scaled = std.math.clamp(value, 0, 1) * @as(f32, @floatFromInt(dimension)); + return @intCast(@min(dimension - 1, @as(usize, @intFromFloat(scaled)))); +} + +pub fn scaledStep(base: u32, count: u32) u32 { + return @intCast(@min( + @as(u64, std.math.maxInt(u32)), + @as(u64, base) * @max(@as(u64, 1), count), + )); +} + +pub fn scrollDocument(state: *State, viewport: Viewport, delta_pixels: f64) ?ScrollResult { + if (comptime !enabled) return null; + if (!std.math.isFinite(delta_pixels) or delta_pixels == 0) return null; + const max_scroll = @as(f64, @floatFromInt(state.document_height -| viewport.pixel_h)); + const next = std.math.clamp(state.document_scroll_y + delta_pixels, 0, max_scroll); + if (next == state.document_scroll_y) return null; + state.scroll_travel += @abs(next - state.document_scroll_y); + state.document_scroll_y = next; + return .{ .active_page = pageAtOffset(state, next) }; +} + +pub fn panPixels( + state: *State, + geometry: image.NativeGeometry, + axis: PanAxis, + direction: i8, + display_pixels: u32, +) PanResult { + if (comptime !enabled) return .unavailable; + const raster = rasterForPage(state, state.page) orelse return .unavailable; + const source_full: u32 = @intCast(switch (axis) { + .horizontal => raster.iw, + .vertical => raster.ih, + }); + const crop = switch (axis) { + .horizontal => geometry.src.w, + .vertical => geometry.src.h, + }; + const source_at = switch (axis) { + .horizontal => geometry.src.x, + .vertical => geometry.src.y, + }; + const displayed = @max(@as(u32, 1), switch (axis) { + .horizontal => geometry.dst.w, + .vertical => geometry.dst.h, + }); + const overflow = source_full -| crop; + if (overflow == 0 or + (direction < 0 and source_at == 0) or + (direction > 0 and source_at >= overflow)) return .edge; + + const source_step = @max( + @as(u64, 1), + (@as(u64, display_pixels) * @as(u64, crop) + displayed - 1) / displayed, + ); + const normalized_step: u32 = @intCast(@min( + @as(u64, std.math.maxInt(u16)), + @max( + @as(u64, 1), + (source_step * std.math.maxInt(u16) + overflow - 1) / overflow, + ), + )); + const position = switch (axis) { + .horizontal => &state.pan_x, + .vertical => &state.pan_y, + }; + if (direction > 0) { + position.* = @intCast(@min( + @as(u32, std.math.maxInt(u16)), + @as(u32, position.*) + normalized_step, + )); + } else { + position.* -|= @intCast(normalized_step); + } + return .moved; +} + +fn setHorizontalEdge(state: *State, geometry: image.NativeGeometry, end: bool) void { + if (comptime !enabled) return; + const raster = rasterForPage(state, state.page) orelse return; + if (raster.iw <= geometry.src.w) return; + state.pan_x = if (end) std.math.maxInt(u16) else 0; +} + +fn rearmSearchReveal(state: *State, viewport: Viewport) void { + if (comptime !enabled) return; + if (state.search_query.len == 0) return; + if (!state.reveal_viewport_valid or + state.reveal_viewport_w != viewport.pixel_w or + state.reveal_viewport_h != viewport.pixel_h or + state.reveal_fit != state.fit) + state.search_reveal_pending = true; +} + +pub fn revealSearch( + state: *State, + viewport: Viewport, + geometry: ?image.NativeGeometry, +) void { + if (comptime !enabled) return; + if (!state.search_reveal_pending) return; + state.reveal_viewport_w = viewport.pixel_w; + state.reveal_viewport_h = viewport.pixel_h; + state.reveal_fit = state.fit; + state.reveal_viewport_valid = true; + const results = state.search_results orelse { + state.search_reveal_pending = false; + return; + }; + if (results.hit_count == 0 or results.quads.len == 0) { + state.search_reveal_pending = false; + return; + } + state.search_hit = @min(state.search_hit, results.hit_count - 1); + const q = for (results.quads) |item| { + if (item.hit == state.search_hit) break item.quad; + } else { + state.search_reveal_pending = false; + return; + }; + state.search_reveal_pending = false; + const center_x = (q.ul.x + q.ur.x + q.ll.x + q.lr.x) / 4; + const center_y = (q.ul.y + q.ur.y + q.ll.y + q.lr.y) / 4; + if (state.fit == .width) { + ensureLayout(state, viewport); + const page_y = @as(f64, @floatFromInt(state.page_starts[state.page])) + + @as(f64, center_y) * @as(f64, @floatFromInt(state.page_heights[state.page])); + const wanted = page_y - @as(f64, @floatFromInt(viewport.pixel_h)) / 2; + const max_scroll = @as(f64, @floatFromInt(state.document_height -| viewport.pixel_h)); + state.document_scroll_y = std.math.clamp(wanted, 0, max_scroll); + return; + } + const placed = geometry orelse return; + const raster = rasterForPage(state, state.page) orelse return; + const full: u32 = @intCast(raster.iw); + const at = normalizedPixel(center_x, raster.iw); + if (at >= placed.src.x and at < placed.src.x + placed.src.w) return; + const overflow = full -| placed.src.w; + if (overflow == 0) return; + const wanted = @min(overflow, at -| placed.src.w / 2); + state.pan_x = @intCast( + (@as(u64, wanted) * std.math.maxInt(u16) + overflow / 2) / overflow, + ); +} + +pub fn stepPage(state: *State, gpa: std.mem.Allocator, delta: i64) bool { + const current: i64 = @intCast(state.page); + const last: i64 = @intCast(state.page_count -| 1); + return state.activatePage( + gpa, + @intCast(std.math.clamp(current + delta, 0, last)), + true, + ); +} + +fn scrollNormal( + state: *State, + gpa: std.mem.Allocator, + viewport: ?Viewport, + delta_pixels: f64, +) bool { + const view = viewport orelse return false; + ensureLayout(state, view); + const result = scrollDocument(state, view, delta_pixels) orelse return false; + return result.active_page != state.page and + state.activatePage(gpa, result.active_page, false); +} + +fn moveRows( + state: *State, + gpa: std.mem.Allocator, + native_images: bool, + viewport: ?Viewport, + cell_pixels: CellPixels, + direction: i8, + count: u32, +) bool { + if (!native_images) { + const pages: i64 = @intCast(@max(@as(u32, 1), count)); + return stepPage(state, gpa, if (direction > 0) pages else -pages); + } + return scrollNormal( + state, + gpa, + viewport, + @as(f64, @floatFromInt(scaledStep(cell_pixels.h, count))) * direction, + ); +} + +fn movePage( + state: *State, + gpa: std.mem.Allocator, + native_images: bool, + viewport: ?Viewport, + cell_pixels: CellPixels, + direction: i8, + kind: modal.Normal.Page, + count: u32, +) bool { + if (!native_images) { + const pages: i64 = @intCast(@max(@as(u32, 1), count)); + return stepPage(state, gpa, if (direction > 0) pages else -pages); + } + const base: u32 = switch (kind) { + .half_down, .half_up => if (viewport) |view| + @max(@as(u32, 1), view.pixel_h / 2) + else + cell_pixels.h, + .down, .up => if (viewport) |view| view.pixel_h else cell_pixels.h, + }; + return scrollNormal( + state, + gpa, + viewport, + @as(f64, @floatFromInt(scaledStep(base, count))) * direction, + ); +} + +pub fn applyNormal( + state: *State, + gpa: std.mem.Allocator, + semantic: modal.Normal.Action, + native_images: bool, + cell_pixels: CellPixels, + viewport: ?Viewport, + geometry: ?image.NativeGeometry, +) NormalResult { + if (comptime !enabled) return .{}; + var result: NormalResult = .{}; + switch (semantic) { + .escape => state.cancelChrome(gpa), + .move => |move| switch (move.motion) { + .down => result.page_changed = moveRows( + state, + gpa, + native_images, + viewport, + cell_pixels, + 1, + move.count, + ), + .up => result.page_changed = moveRows( + state, + gpa, + native_images, + viewport, + cell_pixels, + -1, + move.count, + ), + .left => if (native_images) if (geometry) |placed| { + _ = panPixels(state, placed, .horizontal, -1, scaledStep(cell_pixels.w, move.count)); + }, + .right => if (native_images) if (geometry) |placed| { + _ = panPixels(state, placed, .horizontal, 1, scaledStep(cell_pixels.w, move.count)); + }, + else => {}, + }, + .goto => |go| switch (go.target) { + .file_start => result.page_changed = state.activatePage( + gpa, + if (go.explicit_count) go.count -| 1 else 0, + true, + ), + .last_line => result.page_changed = state.activatePage(gpa, state.page_count -| 1, true), + .line_start, .first_nonws => if (native_images) if (geometry) |placed| + setHorizontalEdge(state, placed, false), + .line_end => if (native_images) if (geometry) |placed| + setHorizontalEdge(state, placed, true), + .line_down => result.page_changed = moveRows( + state, + gpa, + native_images, + viewport, + cell_pixels, + 1, + go.count, + ), + .line_up => result.page_changed = moveRows( + state, + gpa, + native_images, + viewport, + cell_pixels, + -1, + go.count, + ), + else => {}, + }, + .line => |line| switch (line) { + .start, .first_nonws => if (native_images) if (geometry) |placed| + setHorizontalEdge(state, placed, false), + .end => if (native_images) if (geometry) |placed| + setHorizontalEdge(state, placed, true), + }, + .goto_line => |go| { + if (go.explicit) + result.page_changed = state.activatePage(gpa, go.count -| 1, true); + }, + .page => |page| result.page_changed = switch (page.kind) { + .half_down, .down => movePage( + state, + gpa, + native_images, + viewport, + cell_pixels, + 1, + page.kind, + page.count, + ), + .half_up, .up => movePage( + state, + gpa, + native_images, + viewport, + cell_pixels, + -1, + page.kind, + page.count, + ), + }, + .view => |view| result.page_changed = switch (view) { + .scroll_down => moveRows(state, gpa, native_images, viewport, cell_pixels, 1, 1), + .scroll_up => moveRows(state, gpa, native_images, viewport, cell_pixels, -1, 1), + else => false, + }, + .leader => result.host = .leader, + .command_line => result.host = .command_line, + .search => result.host = .search, + .search_step => |step| result.host = if (step.direction == .forward) + .search_forward + else + .search_backward, + else => {}, + } + return result; +} + +pub fn captureLayoutAnchor(state: *State) void { + if (comptime !enabled) return; + if (!state.layout_valid or state.page_count == 0 or state.document_height == 0) return; + const page = pageAtOffset(state, state.document_scroll_y); + const start: f64 = @floatFromInt(state.page_starts[page]); + const height: f64 = @floatFromInt(@max(@as(u32, 1), state.page_heights[page])); + state.layout_anchor_page = page; + state.layout_anchor_fraction = std.math.clamp((state.document_scroll_y - start) / height, 0, 1); + state.layout_anchor_pending = true; +} + +fn consumeOutlineReveal(state: *State, viewport: Viewport) void { + const destination = state.outline_reveal_pending orelse return; + state.outline_reveal_pending = null; + const page = @min(destination.page, state.page_count -| 1); + const size = state.page_sizes[page]; + const raw_y = destination.y orelse 0; + const y = if (std.math.isFinite(raw_y)) std.math.clamp(raw_y, 0, size.height) else 0; + state.document_scroll_y = @as(f64, @floatFromInt(state.page_starts[page])) + + @as(f64, y) / @as(f64, size.height) * @as(f64, @floatFromInt(state.page_heights[page])); + + if (destination.x) |raw_x| if (state.fit == .height and std.math.isFinite(raw_x)) { + const display_width = @as(f64, @floatFromInt(viewport.pixel_h)) * + @as(f64, size.width) / @as(f64, size.height); + const viewport_width: f64 = @floatFromInt(viewport.pixel_w); + if (display_width > viewport_width) { + const x = std.math.clamp(raw_x, 0, size.width); + const target = @as(f64, x) / @as(f64, size.width) * display_width; + const overflow = display_width - viewport_width; + const wanted = std.math.clamp(target - viewport_width / 2, 0, overflow); + state.pan_x = @intFromFloat(@round( + wanted / overflow * @as(f64, std.math.maxInt(u16)), + )); + } + }; + state.search_reveal_pending = false; + state.reveal_viewport_w = viewport.pixel_w; + state.reveal_viewport_h = viewport.pixel_h; + state.reveal_fit = state.fit; + state.reveal_viewport_valid = true; +} + +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; + const before = state.document_scroll_y; + if (state.layout_valid and !state.scroll_to_page_pending and + !state.layout_anchor_pending and + (state.layout_viewport_w != viewport.pixel_w or + state.layout_viewport_h != viewport.pixel_h)) captureLayoutAnchor(state); + if (!state.layout_valid or state.layout_viewport_w != viewport.pixel_w or + state.layout_viewport_h != viewport.pixel_h or state.layout_fit != state.fit) + { + var at: u64 = 0; + for (state.page_sizes, 0..) |size, page| { + state.page_starts[page] = at; + const height = pageHeight(size, viewport, state.fit); + state.page_heights[page] = height; + at = std.math.add(u64, at, height) catch std.math.maxInt(u64); + if (page + 1 < state.page_count) + at = std.math.add(u64, at, page_gap_px) catch std.math.maxInt(u64); + } + state.document_height = at; + state.layout_viewport_w = viewport.pixel_w; + 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; + state.layout_anchor_pending = false; + } else if (state.layout_anchor_pending) { + const page = @min(state.layout_anchor_page, state.page_count -| 1); + state.document_scroll_y = @as(f64, @floatFromInt(state.page_starts[page])) + + state.layout_anchor_fraction * @as(f64, @floatFromInt(state.page_heights[page])); + state.layout_anchor_pending = false; + } + } + 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. + // Unless the sizes landed under it: then the strip, wherever it is in + // its glide, moves with the place it is heading for. + if (relaid and state.relayout_keeps_motion) { + state.drawn_y += state.document_scroll_y - before; + state.relayout_keeps_motion = false; + } else if (relaid) { + state.follow = .{}; + state.drawn_y = state.document_scroll_y; + state.direct_y = 0; + } +} + +pub const WordProbe = if (enabled) struct { + page: usize, + quads: []pdf.Quad, + text: []u8, + link: ?pdf.Link = null, + + pub fn deinit(probe: *@This(), gpa: std.mem.Allocator) void { + if (probe.link) |*link| link.deinit(gpa); + gpa.free(probe.quads); + gpa.free(probe.text); + probe.* = undefined; + } +} else void; + +pub const PointerDrag = if (enabled) struct { + native: bool = false, + /// Right-button Look probes this cell on release without borrowing or + /// replacing the pane's persistent MuPDF selection. + word_at: ?struct { col: u16, row: u16 } = null, + /// Drag updates keep selection geometry/text live, but their expensive + /// baked raster highlight is committed once on release. + selection_changed: bool = false, +} else struct {}; + +pub const PointerAction = enum { look, exec }; + +pub const PointerRelease = if (enabled) struct { + action: ?PointerAction = null, + text: []const u8 = &.{}, + probe: ?WordProbe = null, + + pub fn deinit(release: *@This(), gpa: std.mem.Allocator) void { + if (release.probe) |*probe| probe.deinit(gpa); + release.* = undefined; + } +} else struct { + pub fn deinit(_: *@This(), _: std.mem.Allocator) void {} +}; + +pub fn probeWord( + document: *Document, + gpa: std.mem.Allocator, + page: usize, + point: if (enabled) pdf.Point else void, +) !?WordProbe { + if (comptime !enabled) return null; + var selection = try document.select(gpa, page, point, point); + errdefer selection.deinit(gpa); + const text = try document.copySelection(gpa, page, selection.start, selection.end); + errdefer gpa.free(text); + if (selection.quads.len == 0 or text.len == 0) { + selection.deinit(gpa); + gpa.free(text); + return null; + } + // Transfer the quad allocation out of Selection. There is intentionally + // no Selection.deinit on this success path: WordProbe is now its owner. + return .{ .page = page, .quads = selection.quads, .text = text }; +} + +pub fn pointAtGeometry( + geometry: image.NativeGeometry, + pixel_offset_y: f32, + full_width: usize, + full_height: usize, + band_y: usize, + pixel_x: i64, + pixel_y: i64, + clamp_to_page: bool, +) ?Point { + if (comptime !enabled) return null; + if (full_width == 0 or full_height == 0 or + geometry.src.w == 0 or geometry.src.h == 0 or + geometry.dst.w == 0 or geometry.dst.h == 0) return null; + + const left: f64 = @floatFromInt(geometry.dst.x); + const top: f64 = @floatFromInt(geometry.dst.y); + const right = left + @as(f64, @floatFromInt(geometry.dst.w)); + const bottom = top + @as(f64, @floatFromInt(geometry.dst.h)); + var x: f64 = @floatFromInt(pixel_x); + var y: f64 = @as(f64, @floatFromInt(pixel_y)) - @as(f64, pixel_offset_y); + if (clamp_to_page) { + // Half-open rectangles: keep a clamped point infinitesimally inside + // the last presented pixel instead of letting it become `right`. + const epsilon = 1.0 / 1024.0; + x = std.math.clamp(x, left, @max(left, right - epsilon)); + y = std.math.clamp(y, top, @max(top, bottom - epsilon)); + } else if (x < left or x >= right or y < top or y >= bottom) { + return null; + } + + const source_x_f = @as(f64, @floatFromInt(geometry.src.x)) + + (x - left) * @as(f64, @floatFromInt(geometry.src.w)) / + @as(f64, @floatFromInt(geometry.dst.w)); + const source_y_f = @as(f64, @floatFromInt(geometry.src.y)) + + (y - top) * @as(f64, @floatFromInt(geometry.src.h)) / + @as(f64, @floatFromInt(geometry.dst.h)); + const source_x: usize = @min(full_width - 1, @as(usize, @intFromFloat(@floor(source_x_f)))); + const band_source_y: usize = @intFromFloat(@floor(source_y_f)); + const source_y = @min(full_height - 1, band_y + band_source_y); + return .{ + .x = (@as(f32, @floatFromInt(source_x)) + 0.5) / + @as(f32, @floatFromInt(full_width)), + .y = (@as(f32, @floatFromInt(source_y)) + 0.5) / + @as(f32, @floatFromInt(full_height)), + }; +} + +pub fn openPane( + core: *pardes.Pardes, + id: usize, + path: []const u8, + page_one_based: usize, + /// A page past the last: the last page (a restore, a start-up), or + /// refused with error.NoSuchPage (a look, which names a place). + past_end: enum { last, refuse }, +) !*pardes.Pane { + if (comptime !enabled) return error.PdfDisabled; + // The slot stays ours across the read: another request may make a + // pane meanwhile. + core.reserved_slots[id] = true; + defer core.reserved_slots[id] = false; + var state = if (filesystem.localPath(path) != null) + try State.open(core.pdf_gpa, path, page_one_based) + else virtual: { + const bytes = try filesystem.read(core, path); + defer core.gpa.free(bytes); + break :virtual try State.openBytes(core.pdf_gpa, path, bytes, page_one_based); + }; + errdefer state.deinit(core.pdf_gpa); + if (past_end == .refuse and page_one_based > state.page_count) return error.NoSuchPage; + const pane = try core.newDocPane(id); + pane.pdf = state; + pane.body.cur_pinned = true; + if (filesystem.localPath(path) != null) core.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + return pane; +} + +/// The file on disk is still the one the pane read (`fileKey`): a watch +/// armed over it has nothing to reload. +pub fn sameBytes(core: *pardes.Pardes, pane: *const pardes.Pane) bool { + if (comptime !enabled) return false; + const state = if (pane.pdf) |*pv| pv else return false; + if (state.file == 0) return false; + const bytes = filesystem.read(core, state.path) catch return false; + defer core.gpa.free(bytes); + return fileKey(state.path, bytes) == state.file; +} + +/// Release a PDF payload while its pane slot is still installed, so the host +/// can retire the corresponding directory watch before that id is reused. +pub fn deinitPane(core: *pardes.Pardes, pane: *pardes.Pane, state: *State) void { + if (comptime !enabled) return; + for (core.panes, 0..) |slot, id| { + if (slot == pane) core.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); + } + state.deinit(core.pdf_gpa); +} + +pub fn reloadPane( + core: *pardes.Pardes, + pane: *pardes.Pane, +) !void { + if (comptime !enabled) return error.PdfDisabled; + const state = &(pane.pdf orelse return error.MissingPdfState); + try state.reload(core.pdf_gpa); + resetPageChrome(pane); +} + +pub fn isSectionsOutput(pane: *const pardes.Pane) bool { + if (comptime !enabled) return false; + const file = pane.file orelse return false; + const output = file.output orelse return false; + return switch (output.from) { + .cmd => |builtin| builtin == .PdfSections, + else => false, + }; +} + +fn refreshCleanSections(core: *pardes.Pardes, state: *State) void { + if (comptime !enabled) return; + const remembered = state.sections_output orelse return; + if (remembered.pane >= core.panes.len) return; + const result = core.panes[remembered.pane] orelse return; + if (result.serial != remembered.serial or !isSectionsOutput(result)) return; + const file = &result.file.?; + if (file.revision != remembered.revision) return; + + const content = state.renderSections(core.pdf_gpa, core.gpa) catch { + state.sections_output = null; + return; + }; + if (std.mem.eql(u8, file.content, content)) { + core.gpa.free(content); + return; + } + look.invalidateLookHover(core, remembered.pane); + File.setContent(core, file, content); + const rows = File.lineCount(file.content); + file.scroll = @min(file.scroll, rows - 1); + const row = @min(@as(usize, @intCast(@max(0, result.body.cur_row))), rows - 1); + result.body.cur_row = @intCast(row); + result.body.cur_col = @intCast(@min( + @as(usize, @intCast(@max(0, result.body.cur_col))), + modal.lineSlice(file.content, row).len, + )); + result.body.msel.active = false; + result.body.vsel.active = false; + result.body.nsel = 0; + state.sections_output.?.revision = file.revision; +} + +/// One successful watched-path transaction, including every piece of derived +/// PDF output. The caller owns generic update bookkeeping and status text. +pub fn reloadWatched( + core: *pardes.Pardes, + pane: *pardes.Pane, +) !void { + try reloadPane(core, pane); + refreshCleanSections(core, &pane.pdf.?); +} + +fn rearmCleanSections( + core: *pardes.Pardes, + id: usize, + pane: *pardes.Pane, + state: *State, +) bool { + const remembered = state.sections_output orelse return false; + if (remembered.pane >= core.panes.len) return false; + const result = core.panes[remembered.pane] orelse return false; + if (result.serial != remembered.serial or !isSectionsOutput(result)) return false; + const file = &result.file.?; + if (file.revision != remembered.revision) return false; + + file.scroll = 0; + result.body.cur_row = 0; + result.body.cur_col = 0; + result.body.msel.active = false; + result.body.vsel.active = false; + result.body.nsel = 0; + pane.search_pane = remembered.pane; + pane.search_row = null; + look.armLookWalk(core, remembered.pane); + core.active = id; + return true; +} + +/// Lazily materialise this pane's cached outline as a location list. All +/// PDF-specific ownership stays here; Pardes contributes only placement. +pub fn openSections(core: *pardes.Pardes, id: usize) void { + if (comptime !enabled) return; + const pane = core.panes[id] orelse return; + const state = &(pane.pdf orelse return); + if (rearmCleanSections(core, id, pane, state)) return; + const content = state.renderSections(core.pdf_gpa, core.gpa) catch |err| return core.reportError(id, "PdfSections", err); + // No outline: said, not an empty pane to puzzle over. + if (content.len == 0) { + core.gpa.free(content); + return core.reportFailure(id, "PdfSections: this PDF has no outline"); + } + + const free = core.freeSlot() orelse { + core.gpa.free(content); + return core.reportError(id, "PdfSections", error.NoPaneSlots); + }; + const dir = std.fs.path.dirname(state.path) orelse "/"; + const result = Output.open( + core, + free, + dir, + .{ .cmd = .PdfSections }, + "", + content, + ) catch { + core.gpa.free(content); + return; + }; + state.sections_output = .{ + .pane = free, + .serial = result.serial, + .revision = result.file.?.revision, + }; + exec.placeNew(core, id, id, free, .doc); + layout.compute(core); + core.active = id; + pane.search_pane = free; + pane.search_row = null; + look.armLookWalk(core, free); +} + +const SectionsOwner = struct { + id: usize, + pane: *pardes.Pane, + state: *State, +}; + +fn sectionsOwner(core: *pardes.Pardes, output_id: usize) ?SectionsOwner { + const output = core.panes[output_id] orelse return null; + if (!isSectionsOutput(output)) return null; + const file = output.file.?; + for (core.panes, 0..) |slot, id| { + const pane = slot orelse continue; + const state = if (pane.pdf) |*pdf_state| pdf_state else continue; + const token = state.sections_output orelse continue; + if (token.pane == output_id and token.serial == output.serial and + token.revision == file.revision) + return .{ .id = id, .pane = pane, .state = state }; + } + return null; +} + +pub fn lookSection( + core: *pardes.Pardes, + output_id: usize, + path: []const u8, + at: look.Spot, +) bool { + if (comptime !enabled) return false; + const output = core.panes[output_id] orelse return false; + if (!isSectionsOutput(output)) return false; + if (at.line == 0 or at.col == 0 or at.end_line != 0 or + !look.isPdfPath(path)) return false; + const owner = sectionsOwner(core, output_id) orelse return true; + + var joined_path: [4096]u8 = undefined; + const output_dir = std.fs.path.dirname(output.file.?.path) orelse "/"; + const separator = if (std.mem.endsWith(u8, output_dir, "/")) "" else "/"; + const target_path = if (std.fs.path.isAbsolute(path)) + path + else + std.fmt.bufPrint(&joined_path, "{s}{s}{s}", .{ output_dir, separator, path }) catch return true; + if (!std.mem.eql(u8, owner.state.path, target_path)) return true; + + // A row is `file:PAGE:SECTION`, its first number the page as in every + // other location: a section there is none, or one on another page, is + // a miss said, never a silent jump. + var said: [160]u8 = undefined; + const destination = owner.state.sectionDestination(core.pdf_gpa, at.col - 1) orelse + return failSection(core, output_id, std.fmt.bufPrint(&said, "PdfSections: {s} has no section {d}", .{ std.fs.path.basename(owner.state.path), at.col }) catch "PdfSections: no such section"); + if (destination == .internal and destination.internal.page + 1 != at.line) + return failSection(core, output_id, std.fmt.bufPrint(&said, "PdfSections: section {d} of {s} is on page {d}, not {d}", .{ at.col, std.fs.path.basename(owner.state.path), destination.internal.page + 1, at.line }) catch "PdfSections: that section is on another page"); + switch (destination) { + .internal => |internal| { + look.clearNavigationSelection(core, owner.pane); + revealOutlineDestination(core, owner.pane, internal); + }, + .external => |uri| if (uri.len <= 256) core.emit(.{ .open_link = .from(uri) }), + .none => unreachable, + } + core.active = owner.id; + return true; +} + +fn failSection(core: *pardes.Pardes, id: usize, text: []const u8) bool { + core.look_missed = true; + core.reportFailure(id, text); + return true; +} + +pub fn resetPageChrome(pane: *pardes.Pane) void { + pane.body.cur_row = 0; + pane.body.cur_col = 0; + pane.body.vsel.active = false; + pane.body.msel.active = false; + pane.body.nsel = 0; +} + +pub fn activatePage( + core: *pardes.Pardes, + pane: *pardes.Pane, + page: usize, + reveal: bool, +) void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + if (state.activatePage(core.pdf_gpa, page, reveal)) resetPageChrome(pane); +} + +pub fn revealOutlineDestination( + core: *pardes.Pardes, + pane: *pardes.Pane, + destination: OutlineInternalDestination, +) void { + if (comptime !enabled) return; + activatePage(core, pane, destination.page, false); + const state = &pane.pdf.?; + state.queueOutlineReveal(destination); + // Consume immediately when geometry already exists; otherwise the PDF + // draw pass consumes the same value after the next layout transaction. + _ = 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); +} + +pub fn toggleFit(pane: *pardes.Pane) void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + state.toggleFit(); +} + +pub fn toggleTint(pane: *pardes.Pane) void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + state.toggleTint(); +} + +pub fn stepPanePage(core: *pardes.Pardes, pane: *pardes.Pane, delta: i64) void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + if (stepPage(state, core.pdf_gpa, delta)) resetPageChrome(pane); +} + +pub fn paneViewport(core: *const pardes.Pardes, pane: *const pardes.Pane) ?Viewport { + if (comptime !enabled) return null; + const rect = for (core.panes, 0..) |slot, id| { + if (slot == pane) break core.rects[id]; + } else return null; + const cols = rect.w -| config.GUTTER; + const rows = rect.h -| pane.tag_rows; + if (cols == 0 or rows == 0) return null; + return .{ + .pixel_w = @as(u32, cols) * @as(u32, core.cell_pixels.w), + .pixel_h = @as(u32, rows) * @as(u32, core.cell_pixels.h), + }; +} + +pub fn ensurePaneLayout(core: *pardes.Pardes, pane: *pardes.Pane) ?Viewport { + if (comptime !enabled) return null; + const state = &(pane.pdf orelse return null); + const view = paneViewport(core, pane) orelse return null; + ensureLayout(state, view); + return view; +} + +pub fn tintColors(core: *const pardes.Pardes) TintColors { + if (comptime !enabled) return; + const theme = core.theme(); + return .{ + .background = theme.bg orelse theme.tag_bg, + .foreground = theme.fg orelse theme.tag_fg, + }; +} + +pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?image.NativeGeometry { + if (comptime !enabled) return null; + const state = if (pane.pdf) |*view| view else return null; + const view = paneViewport(core, pane) orelse return null; + return activeGeometry(state, view); +} + +pub fn pageAtGridRow(core: *const pardes.Pardes, pane: *const pardes.Pane, row: u16) ?usize { + if (comptime !enabled) return null; + const state = pane.pdf orelse return null; + const rect = for (core.panes, 0..) |slot, id| { + if (slot == pane) break core.rects[id]; + } else return null; + const body_y = core.bodyTop(pane, rect); + if (row < body_y or row >= body_y + (rect.h -| pane.tag_rows)) return null; + const local_y = @as(f64, @floatFromInt( + @as(u32, row - body_y) * core.cell_pixels.h + core.cell_pixels.h / 2, + )); + return pageAtViewportY(&state, local_y); +} + +pub fn paneNativeReady(core: *const pardes.Pardes, pane: *const pardes.Pane) bool { + if (comptime !enabled) return false; + if (!core.native_images) return false; + const state = if (pane.pdf) |*view| view else return false; + const view = paneViewport(core, pane) orelse return false; + return nativeReady(state, view); +} + +pub fn nativePageAtGridRow( + core: *const pardes.Pardes, + pane: *const pardes.Pane, + row: u16, +) ?usize { + if (comptime !enabled) return null; + if (!core.native_images) return null; + const state = if (pane.pdf) |*view| view else return null; + const page = pageAtGridRow(core, pane, row) orelse return null; + const view = paneViewport(core, pane) orelse return null; + if (!pageReady(state, view, page)) return null; + return page; +} + +pub fn pointAt( + core: *const pardes.Pardes, + pane: *const pardes.Pane, + col: u16, + row: u16, + clamp_to_page: bool, +) ?Point { + if (comptime !enabled) return null; + const state = pane.pdf orelse return null; + return panePointAtPage(core, pane, state.page, col, row, clamp_to_page); +} + +pub fn panePointAtPage( + core: *const pardes.Pardes, + pane: *const pardes.Pane, + page: usize, + col: u16, + row: u16, + clamp_to_page: bool, +) ?Point { + if (comptime !enabled) return null; + const state = if (pane.pdf) |*view| view else return null; + const view = paneViewport(core, pane) orelse return null; + const rect = for (core.panes, 0..) |slot, id| { + if (slot == pane) break core.rects[id]; + } else return null; + const body_x = @as(i64, rect.x + config.GUTTER); + const body_y = @as(i64, core.bodyTop(pane, rect)); + const px = (@as(i64, col) - body_x) * core.cell_pixels.w + core.cell_pixels.w / 2; + const py = (@as(i64, row) - body_y) * core.cell_pixels.h + core.cell_pixels.h / 2; + return pointAtPage(state, view, page, px, py, clamp_to_page); +} + +pub fn probeAt( + core: *pardes.Pardes, + pane: *pardes.Pane, + col: u16, + row: u16, +) ?WordProbe { + if (comptime !enabled) return null; + const page = nativePageAtGridRow(core, pane, row) orelse return null; + const point = panePointAtPage(core, pane, page, col, row, false) orelse return null; + const state = &(pane.pdf orelse return null); + if (state.document.linkAt(core.pdf_gpa, page, point) catch null) |found| { + var link = found; + if (look.pdfLinkLocation(core, pane, link) != null) { + const quads = core.pdf_gpa.alloc(pdf.Quad, 1) catch { + link.deinit(core.pdf_gpa); + return null; + }; + const text = state.document.copyRectangle(core.pdf_gpa, page, link.quad) catch { + core.pdf_gpa.free(quads); + link.deinit(core.pdf_gpa); + return null; + }; + quads[0] = link.quad; + return .{ .page = page, .quads = quads, .text = text, .link = link }; + } + link.deinit(core.pdf_gpa); + } + return probeWord(&state.document, core.pdf_gpa, page, point) catch null; +} + +pub fn beginDrag( + core: *pardes.Pardes, + pane: *pardes.Pane, + col: u16, + row: u16, + snap_word: bool, +) bool { + if (comptime !enabled) return false; + const hit_page = pageAtGridRow(core, pane, row) orelse return false; + if (hit_page != pane.pdf.?.page) activatePage(core, pane, hit_page, false); + const state = &pane.pdf.?; + state.clearDrag(); + const point = pointAt(core, pane, col, row, false) orelse return false; + state.drag_anchor = point; + state.drag_head = point; + if (!snap_word) return true; + if (state.selection_page == state.page) if (state.selection) |selection| if (selection.contains(point)) return true; + if (state.setSelection(core.pdf_gpa, point, point, true) == .failed) { + // Preserve the old selection transactionally, but never let an + // outside click execute its stale text. + state.clearDrag(); + return false; + } + return true; +} + +pub fn beginSelection( + core: *pardes.Pardes, + pane: *pardes.Pane, + col: u16, + row: u16, +) bool { + return beginDrag(core, pane, col, row, true); +} + +pub fn updateSelection( + core: *pardes.Pardes, + pane: *pardes.Pane, + col: u16, + row: u16, + invalidate_raster: bool, +) SelectionUpdate { + if (comptime !enabled) return .failed; + const state = &(pane.pdf orelse return .failed); + const anchor = state.drag_anchor orelse return .failed; + const point = pointAt(core, pane, col, row, true) orelse return .failed; + if (state.drag_head) |head| if (head.x == point.x and head.y == point.y) return .stationary; + const result = state.setSelection(core.pdf_gpa, anchor, point, invalidate_raster); + if (result != .failed) state.drag_head = point; + return result; +} + +pub fn pointerStart( + core: *pardes.Pardes, + pane: *pardes.Pane, + button: pardes.Mouse.Button, + col: u16, + row: u16, + on_tag: bool, +) PointerDrag { + if (comptime !enabled) return .{}; + if (on_tag or !paneNativeReady(core, pane)) return .{}; + var drag: PointerDrag = .{ .native = true }; + if (button == config.look_button) { + drag.word_at = .{ .col = col, .row = row }; + } else if (button == config.select_button) { + _ = beginDrag(core, pane, col, row, false); + } else if (button == config.exec_button) { + _ = beginDrag(core, pane, col, row, true); + } + return drag; +} + +pub fn pointerUpdate( + drag: *PointerDrag, + core: *pardes.Pardes, + pane: *pardes.Pane, + col: u16, + row: u16, +) void { + if (comptime !enabled) return; + if (!drag.native) return; + if (drag.word_at) |at| { + if (col == at.col and row == at.row) return; + if (!beginDrag(core, pane, at.col, at.row, false)) { + drag.word_at = null; + return; + } + switch (updateSelection(core, pane, col, row, false)) { + .failed => { + drag.word_at = null; + pane.pdf.?.clearDrag(); + }, + // Adjacent grid cells can still address one raster pixel. Keep + // click mode and retry farther out. + .stationary => {}, + .unchanged => drag.word_at = null, + .changed => { + drag.word_at = null; + drag.selection_changed = true; + }, + } + return; + } + switch (updateSelection(core, pane, col, row, false)) { + .failed => pane.pdf.?.clearDrag(), + .stationary, .unchanged => {}, + .changed => drag.selection_changed = true, + } +} + +pub fn pointerCancel(pane: *pardes.Pane, drag: PointerDrag) void { + if (comptime !enabled) return; + if (drag.native) if (pane.pdf) |*state| { + if (drag.selection_changed) state.invalidateRaster(state.page); + state.clearDrag(); + }; +} + +pub fn pointerRelease( + core: *pardes.Pardes, + pane: *pardes.Pane, + drag: PointerDrag, + button: pardes.Mouse.Button, + chorded: bool, +) PointerRelease { + if (comptime !enabled) return .{}; + const state = &(pane.pdf orelse return .{}); + const slot = @backingInt(button); + const grid_selection = pane.sel[slot]; + pane.sel[slot].state = .none; // native quads, not projected text cells + + // Drag updates changed live geometry/text while leaving baked pixels + // stable. Commit exactly once before any chord/Exec/Look can clear state. + if (drag.selection_changed) state.invalidateRaster(state.page); + if (chorded) { + state.clearDrag(); + return .{}; + } + if (drag.word_at) |at| { + const maybe_probe = probeAt(core, pane, at.col, at.row); + state.clearDrag(); + const probe = maybe_probe orelse return .{}; + // A neighboring visible page becomes current before Look dispatch. + // WordProbe owns its text/quads independently of that state change. + if (probe.page != state.page) activatePage(core, pane, probe.page, false); + const text = probe.text; + return .{ .action = .look, .text = text, .probe = probe }; + } + if (button == config.select_button) { + const dragged = grid_selection.c0 != grid_selection.c1 or + grid_selection.r0 != grid_selection.r1; + if (!dragged) state.clearSelection(core.pdf_gpa); + pane.body.cur_pinned = true; + pane.body.mode = .normal; + pane.body.msel.active = false; + pane.body.vsel.active = false; + pane.body.normal.clear(); + pane.body.nsel = 0; + state.clearDrag(); + return .{}; + } + if (state.drag_anchor == null or state.selection_text.len == 0) { + state.clearDrag(); + return .{}; + } + const text = state.selection_text; + state.clearDrag(); + return .{ + .action = if (button == config.look_button) .look else .exec, + .text = text, + }; +} + +pub fn highlightInput( + core: *pardes.Pardes, + pane_id: usize, + pane: *const pardes.Pane, +) HighlightInput { + if (comptime !enabled) return; + const hover_quads: []const Quad = if (core.pdf_hover_preview) |preview| + if (preview.pane == pane_id and preview.serial == pane.serial) + preview.probe.quads + else + &.{} + else + &.{}; + const hover_page = if (hover_quads.len > 0) core.pdf_hover_preview.?.probe.page else null; + const link_hover = hover_quads.len > 0 and core.pdf_hover_preview.?.probe.link != null; + const selection_color = core.theme().sel_bg; + return .{ + .hover_quads = hover_quads, + .hover_page = hover_page, + .hover_color = if (link_hover) core.theme().box else selection_color, + .selection_color = selection_color, + .live = core.native_image_patches, + }; +} + +pub fn panPane( + core: *pardes.Pardes, + pane: *pardes.Pane, + axis: PanAxis, + direction: i8, + display_pixels: u32, +) PanResult { + if (comptime !enabled) return .unavailable; + const placed = paneGeometry(core, pane) orelse return .unavailable; + const state = &(pane.pdf orelse return .unavailable); + return panPixels(state, placed, axis, direction, display_pixels); +} + +/// `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.ahead = if (moved > 0) 1 else -1; + 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, true); +} + +pub fn horizontalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { + if (comptime !enabled) return; + const state = pane.pdf orelse return; + if (!core.native_images or state.fit != .height) return; + const cols: u32 = @intCast(@max(1, config.wheel_cols)); + _ = panPane(core, pane, .horizontal, direction, scaledStep(core.cell_pixels.w, cols)); +} + +pub fn draw( + core: *pardes.Pardes, + s: *pardes.Surface, + pane: *pardes.Pane, + rect: pardes.Rect, + pane_id: usize, + text_x: u16, + text_width: u16, + rail: *pardes.Region, +) bool { + if (comptime !enabled) return false; + if (!core.native_images or rect.h <= pane.tag_rows) return false; + const state = &(pane.pdf orelse return false); + const view = paneViewport(core, pane) orelse return false; + const key = TintKey{ .mode = state.tint, .colors = tintColors(core) }; + applySizes(state, core.pdf_gpa); + 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); + defer { + core.frames.landed +|= state.landed; + state.landed = 0; + } + const visible = renderFrame( + state, + core.pdf_gpa, + core.scratch.allocator(), + view, + 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; + state.blank = .{ 255, 255, 255, 255 }; + pdf.tintRgba(&state.blank, key.mode, key.colors) catch {}; + while (page < visible_end) : (page += 1) { + const placed = placedRaster(state, view, page) orelse { + // Not landed yet (a lane renders it): its paper, where it will + // be, one pixel drawn the page's size. Never a stall. + // ponytail: the blank's texture is keyed revision 0, so a tint + // change shows the old paper until the page lands. + if (workers == null or pardes.platform == .macos) continue; + const size = state.page_sizes[page]; + const rows = visibleRows(state, view, page, @intFromFloat(@max(1, @round(size.width * 8))), @intFromFloat(@max(1, @round(size.height * 8)))) orelse continue; + if (!core.appendImagePlace(.{ + .pane = @intCast(pane_id), + .serial = pane.serial, + .native = .{ + .revision = 0, + .page = @intCast(page), + .fit = switch (state.fit) { + .width => .width, + .height => .height, + }, + .pan_x = state.pan_x, + .geometry = .{ + .src = .{ .x = 0, .y = 0, .w = 1, .h = 1 }, + .dst = .{ .x = rows.base.dst.x, .y = rows.dst_y, .w = rows.base.dst.w, .h = rows.dst_h }, + }, + .pixel_offset_y = rows.pixel_offset_y, + }, + .x = text_x, + .y = core.bodyTop(pane, rect) + pane.notices.span(), + .w = text_width, + .h = (rect.h -| pane.tag_rows) -| pane.notices.span(), + .rgba = &state.blank, + .iw = 1, + .ih = 1, + })) break; + placed_any = true; + continue; + }; + const slot = rasterForPage(state, page).?; + const patch: ?pardes.ImagePatch = if (slot.patch_from != 0 and + slot.patch_rows.start < slot.patch_rows.end) + .{ + .from = slot.patch_from, + .y = @intCast(slot.patch_rows.start - slot.band_y), + .h = @intCast(slot.patch_rows.end - slot.patch_rows.start), + } + else + null; + if (!core.appendImagePlace(.{ + .pane = @intCast(pane_id), + .serial = pane.serial, + .native = .{ + .revision = placed.revision, + .page = @intCast(placed.page), + .fit = switch (placed.fit) { + .width => .width, + .height => .height, + }, + .pan_x = placed.pan_x, + .geometry = placed.geometry, + .pixel_offset_y = placed.pixel_offset_y, + .paper_bg = if (pardes.platform == .macos) + (if (key.mode == .disabled) .{ 255, 255, 255 } else key.colors.background) + else + null, + }, + .x = text_x, + // Below the notice chips, like an image: a placed page is + // drawn after the cells and would paint a chip out. + .y = core.bodyTop(pane, rect) + pane.notices.span(), + .w = text_width, + .h = (rect.h -| pane.tag_rows) -| pane.notices.span(), + .rgba = placed.rgba, + .iw = placed.width, + // The texture contains the retained band, not the full page. + .ih = placed.band_height, + .patch = patch, + })) break; + // A shell now holds (or will fetch whole) this revision. + slot.patch_from = slot.revision; + slot.patch_rows = .{ .start = 0, .end = 0 }; + placed_any = true; + } + // Nothing placed while pages are there (a far jump's first frame: its + // slide starts a body short of the pages it renders) is still the PDF's + // frame: falling back to the page's text extracted it on the loop + // (9 ms RS, 110 ms Debug on the SDM) to show it for one frame. + if (!placed_any and visible.len == 0) return false; + + // This frame has spent the motion used to choose its raster band. + state.scroll_travel = 0; + const chrome = core.chromeTheme(); + const theme = core.theme(); + const pane_bg: pardes.Color = if (theme.bg) |color| .{ .rgb = color } else .default; + // The rail runs past the notice bands; only the thumb tracks the text, + // and it is measured here, in document pixels, for `place`'s region. + const track_h: usize = rail.rect.h; + const total = @max(@as(u64, 1), state.document_height); + const len = @max( + @as(usize, 1), + @as(usize, @intCast(@min( + @as(u64, track_h), + @as(u64, track_h) * view.pixel_h / total, + ))), + ); + const offset: u64 = @intFromFloat(@floor(state.document_scroll_y)); + const pos: usize = @intCast(@min( + @as(u64, track_h -| 1), + @as(u64, track_h) * offset / total, + )); + rail.thumb_y = @intCast(pos); + rail.thumb_h = @intCast(@min(len, track_h -| pos)); + rail.thumb_at = @floatCast(state.document_scroll_y / @as(f64, @floatFromInt(total))); + rail.thumb_span = @as(f32, @floatFromInt(view.pixel_h)) / @as(f32, @floatFromInt(total)); + s.fill(rail.rect.x, rail.rect.y, 1, rail.rect.h, .{ .bg = .{ .rgb = chrome.scroll_track } }); + s.fill(rail.rect.x + 1, rail.rect.y, 1, rail.rect.h, .{ .bg = pane_bg }); + s.fill(rail.rect.x, rail.rect.y + rail.thumb_y, 1, rail.thumb_h, .{ .bg = .{ .rgb = chrome.scroll_thumb } }); + return true; +} + +pub const SectionRows = if (enabled) struct { + pub fn usableDestination(destination: pdf.OutlineDestination) ?pdf.OutlineDestination { + return switch (destination) { + .internal => destination, + .external => |uri| if (safeHttpUri(uri)) destination else null, + .none => null, + }; + } + + fn safeHttpUri(uri: []const u8) bool { + // Effect.open_link is inline and cannot carry a larger URL. Omitting + // it here is preferable to rendering a row which can never act. + if (uri.len > 256) return false; + var has_scheme = false; + for (config.url_schemes) |scheme| { + if (std.mem.startsWith(u8, uri, scheme)) { + has_scheme = true; + break; + } + } + if (!has_scheme) return false; + for (uri) |byte| if (byte <= 0x20 or byte == 0x7f) return false; + return true; + } + + pub fn resolve(entries: []const pdf.OutlineEntry, ordinal: usize) ?pdf.OutlineDestination { + if (ordinal >= entries.len) return null; + const entry = entries[ordinal]; + if (entry.destination != .none) return usableDestination(entry.destination); + var i = ordinal + 1; + while (i < entries.len and entries[i].depth > entry.depth) : (i += 1) + if (usableDestination(entries[i].destination)) |destination| return destination; + return null; + } + + pub fn resolveOrdinals( + gpa: std.mem.Allocator, + entries: []const pdf.OutlineEntry, + ) ![]usize { + const unresolved = std.math.maxInt(usize); + const ordinals = try gpa.alloc(usize, entries.len); + @memset(ordinals, unresolved); + + const Pending = struct { depth: u8, ordinal: usize }; + var pending: [256]Pending = undefined; + var pending_len: usize = 0; + for (entries, 0..) |entry, ordinal| { + while (pending_len > 0 and pending[pending_len - 1].depth >= entry.depth) + pending_len -= 1; + + if (usableDestination(entry.destination) != null) { + ordinals[ordinal] = ordinal; + for (pending[0..pending_len]) |ancestor| + ordinals[ancestor.ordinal] = ordinal; + pending_len = 0; + } else if (entry.destination == .none) { + pending[pending_len] = .{ .depth = entry.depth, .ordinal = ordinal }; + pending_len += 1; + } + } + return ordinals; + } + + fn cleanTitle(out: ?[]u8, title: ?[]const u8) usize { + const raw = title orelse { + if (out) |buf| @memcpy(buf[0..10], "[untitled]"); + return 10; + }; + var at: usize = 0; + var i: usize = 0; + var wrote = false; + var pending_space = false; + while (i < raw.len) { + const n: usize = std.unicode.utf8ByteSequenceLength(raw[i]) catch { + pending_space = wrote; + i += 1; + continue; + }; + if (i + n > raw.len) { + pending_space = wrote; + break; + } + const cp = std.unicode.utf8Decode(raw[i .. i + n]) catch { + pending_space = wrote; + i += n; + continue; + }; + const whitespace_or_control = cp <= 0x20 or cp == 0x7f or + (cp >= 0x80 and cp <= 0x9f) or cp == 0x2028 or cp == 0x2029; + if (whitespace_or_control) { + pending_space = wrote; + } else { + if (pending_space) { + if (out) |buf| buf[at] = ' '; + at += 1; + } + if (out) |buf| @memcpy(buf[at..][0..n], raw[i .. i + n]); + at += n; + wrote = true; + pending_space = false; + } + i += n; + } + if (!wrote) { + if (out) |buf| @memcpy(buf[0..13], "[empty title]"); + return 13; + } + return at; + } + + pub fn render(gpa: std.mem.Allocator, path: []const u8, entries: []const pdf.OutlineEntry) ![]u8 { + const target = std.fs.path.basename(path); + const ordinals = try resolveOrdinals(gpa, entries); + defer gpa.free(ordinals); + var total: usize = 0; + for (entries, 0..) |entry, ordinal| { + const resolved = ordinals[ordinal]; + if (resolved == std.math.maxInt(usize)) continue; + // unreachable: resolveOrdinals resolves only to usable destinations + const destination = usableDestination(entries[resolved].destination) orelse unreachable; + total += switch (destination) { + .internal => |internal| std.fmt.count("{s}:{d}:{d} ", .{ target, internal.page + 1, ordinal + 1 }), + .external => |uri| std.fmt.count("{s} ", .{uri}), + .none => unreachable, + }; + total += @as(usize, entry.depth) * 2 + cleanTitle(null, entry.title) + 1; + } + const out = try gpa.alloc(u8, total); + errdefer gpa.free(out); + var at: usize = 0; + for (entries, 0..) |entry, ordinal| { + const resolved = ordinals[ordinal]; + if (resolved == std.math.maxInt(usize)) continue; + // unreachable: resolveOrdinals resolves only to usable destinations + const destination = usableDestination(entries[resolved].destination) orelse unreachable; + const prefix = switch (destination) { + .internal => |internal| try std.fmt.bufPrint(out[at..], "{s}:{d}:{d} ", .{ target, internal.page + 1, ordinal + 1 }), + .external => |uri| try std.fmt.bufPrint(out[at..], "{s} ", .{uri}), + .none => unreachable, + }; + at += prefix.len; + const indent = @as(usize, entry.depth) * 2; + @memset(out[at..][0..indent], ' '); + at += indent; + at += cleanTitle(out[at..], entry.title); + out[at] = '\n'; + at += 1; + } + return out; + } +} else struct {}; |
