diff options
| -rw-r--r-- | src/Output.zig | 7 | ||||
| -rw-r--r-- | src/pardes.zig | 43 | ||||
| -rw-r--r-- | src/pdf_view.zig | 50 | ||||
| -rw-r--r-- | test/pdf.zig | 110 |
4 files changed, 201 insertions, 9 deletions
diff --git a/src/Output.zig b/src/Output.zig index 01e3e0f1..a17b0459 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -701,6 +701,11 @@ pub fn openJumps(p: *Pardes, id: usize) !void { // A closed file's entry: marked, and a look at it opens it again. if (j.serial == 0) { const e = p.recentByHash(j.path_hash) orelse continue; + // A PDF's `col` is how far down its page, no Look's column. + if (j.pdf) { + try out.writer.print("{s}:{d} (closed)\n", .{ e.path, @max(1, j.line) }); + continue; + } try out.writer.print("{s}:{d}:{d} (closed)\n", .{ e.path, @max(1, j.line), @max(1, j.col) }); continue; } @@ -724,6 +729,8 @@ pub fn openJumps(p: *Pardes, id: usize) !void { while (cut > 0 and cut < what.len and what[cut] & 0xc0 == 0x80) cut -= 1; if (j.line == 0) try out.writer.print("{s} {s}\n", .{ loc, what[0..cut] }) + else if (j.pdf) + try out.writer.print("{s}:{d} {s}\n", .{ loc, j.line, what[0..cut] }) else try out.writer.print("{s}:{d}:{d} {s}\n", .{ loc, j.line, j.col, what[0..cut] }); } diff --git a/src/pardes.zig b/src/pardes.zig index 26b167d5..a23039ef 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -462,6 +462,9 @@ pub const Loc = struct { col: u32, /// The file it is in (Wyhash of the path), 0 for a pane with none. path_hash: u64 = 0, + /// A PDF's place: `line` its page (one-based), `col` how far down it + /// (pdf_view.Place.at, a fraction of the page in 2^-32). + pdf: bool = false, }; test { @@ -4924,6 +4927,12 @@ pub const Pardes = struct { return null; } + /// The file a jump in `pane` is in: a text's, a PDF's. + fn jumpPath(pane: *const Pane) ?[]const u8 { + if (recentDot(pane)) |d| return d.path; + return pane.pdfPath(); + } + fn recentDot(pane: *const Pane) ?struct { path: []const u8, line: u32, col: u32, aline: u32 = 0, acol: u32 = 0 } { const f = pane.file orelse return null; if (f.output != null) return null; @@ -7355,6 +7364,14 @@ pub const Pardes = struct { fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool { if (a.pane != b.pane or a.serial != b.serial) return false; + // A PDF's places by where they stand on its strip, as a text's by + // lines: within a body of each other, one visit. + if (a.pdf and b.pdf) if (p.panes[b.pane]) |pane| { + const view = panes.Pdf.paneViewport(p, pane) orelse return a.line == b.line; + const ya = panes.Pdf.placeY(pane, .{ .page = a.line - 1, .at = a.col }) orelse return a.line == b.line; + const yb = panes.Pdf.placeY(pane, .{ .page = b.line - 1, .at = b.col }) orelse return a.line == b.line; + return @abs(ya - yb) <= @as(f64, @floatFromInt(view.pixel_h)); + }; if (a.line == 0 or b.line == 0) return true; const body = @max(1, p.rects[b.pane].h -| BOX_H); return @max(a.line, b.line) - @min(a.line, b.line) <= body; @@ -7374,8 +7391,8 @@ pub const Pardes = struct { kept.serial = 0; // Open again in some pane (a look, Recent): the entry is that // pane's, and closed only while no pane has the file. - for (p.panes, 0..) |slot, id| if (slot) |pane| if (recentDot(pane)) |d| { - if (std.hash.Wyhash.hash(0, d.path) != j.path_hash) continue; + for (p.panes, 0..) |slot, id| if (slot) |pane| if (jumpPath(pane)) |path| { + if (std.hash.Wyhash.hash(0, path) != j.path_hash) continue; kept.pane = @intCast(id); kept.serial = pane.serial; break; @@ -7399,12 +7416,14 @@ pub const Pardes = struct { if (pane.query) |q| if (!q.frozen) return; const force = p.jump_force; p.jump_force = false; + const pdf_place = panes.Pdf.place(pane); const now: Loc = .{ .pane = @intCast(p.active), .serial = pane.serial, - .line = if (pane.pdfPage()) |page| @intCast(page + 1) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0, - .col = if (pane.pdfPage() != null) 0 else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_col) + 1) else 0, - .path_hash = if (recentDot(pane)) |d| std.hash.Wyhash.hash(0, d.path) else 0, + .line = if (pdf_place) |at| @intCast(at.page + 1) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0, + .col = if (pdf_place) |at| at.at else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_col) + 1) else 0, + .path_hash = if (jumpPath(pane)) |path| std.hash.Wyhash.hash(0, path) else 0, + .pdf = pdf_place != null, }; if (!force and p.njumps > 0 and p.samePlace(p.jumps[p.jcur], now)) { p.jumps[p.jcur] = now; // same visit; the entry IS where you are @@ -7447,7 +7466,7 @@ pub const Pardes = struct { var next = if (older) p.jcur - 1 else p.jcur + 1; // A terminal or a scratch has no file to keep to: the step is global. if (p.settings.jump_scope == .file) if (p.panes[p.active]) |here| if (recentPlace(here)) |place| if (place.path.len > 0) { - const hash = if (recentDot(here)) |d| std.hash.Wyhash.hash(0, d.path) else 0; + const hash = if (jumpPath(here)) |path| std.hash.Wyhash.hash(0, path) else 0; // The nearest of this pane's own wins over a nearer one of this // file in another pane: the step does not move the keyboard. var pick: ?usize = null; @@ -7492,10 +7511,11 @@ pub const Pardes = struct { return p.jumpBy(delta); } const from = p.active; - look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = @max(1, j.col) }); + look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.pdf) 0 else @max(1, j.col) }); const opened = p.panes[p.active] orelse return; - const f = opened.file orelse return; - if (!std.mem.eql(u8, f.path, e.path)) return; // it did not open + const opened_path = jumpPath(opened) orelse return; + if (!std.mem.eql(u8, opened_path, e.path)) return; // it did not open + if (j.pdf) panes.Pdf.landAt(p, opened, .{ .page = j.line -| 1, .at = j.col }); // Every entry of that file is the reopened pane from here on, so // a step onto another of them moves in it rather than reopening. for (p.jumps[0..p.njumps]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { @@ -7510,6 +7530,11 @@ pub const Pardes = struct { if (p.panes[p.active]) |source| look.clearNavigationSelection(p, source); if (p.active != j.pane) look.clearNavigationSelection(p, pane); p.jcur = next; + if (pane.pdfPath() != null) { + look.focusPaneLine(p, j.pane, .{ .line = 0, .col = 0 }, .keep); + panes.Pdf.landAt(p, pane, .{ .page = j.line -| 1, .at = if (j.pdf) j.col else 0 }); + return; + } look.focusPaneLine(p, j.pane, .{ .line = j.line, .col = j.col }, .center); } diff --git a/src/pdf_view.zig b/src/pdf_view.zig index bcca11b4..f6d26eac 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -3070,6 +3070,56 @@ pub fn revealOutlineDestination( _ = ensurePaneLayout(core, pane); } +/// A PDF's place, as the jumplist keeps it: the page at the strip's top and +/// how far down that page it stands, a fraction in 2^-32 of the page. A +/// fraction, not pixels: a zoom, a fit change or a resize scales every page, +/// and a fraction of one lands on the same line of print after it. +pub const Place = struct { page: usize, at: u32 }; +const place_scale: f64 = std.math.maxInt(u32); + +pub fn place(pane: *const pardes.Pane) ?Place { + if (comptime !enabled) return null; + const state = if (pane.pdf) |*pv| pv else return null; + if (!state.layout_valid or state.document_height == 0) return .{ .page = state.page, .at = 0 }; + const page = pageAtOffset(state, state.document_scroll_y); + const start: f64 = @floatFromInt(state.page_starts[page]); + const height: f64 = @floatFromInt(@max(1, state.page_heights[page])); + const fraction = std.math.clamp((state.document_scroll_y - start) / height, 0, 1); + return .{ .page = page, .at = @intFromFloat(@round(fraction * place_scale)) }; +} + +/// Where `at` stands on the strip as laid out now, in document pixels. +pub fn placeY(pane: *const pardes.Pane, at: Place) ?f64 { + if (comptime !enabled) return null; + const state = if (pane.pdf) |*pv| pv else return null; + if (!state.layout_valid) return null; + const page = @min(at.page, state.page_count -| 1); + return @as(f64, @floatFromInt(state.page_starts[page])) + + @as(f64, @floatFromInt(at.at)) / place_scale * @as(f64, @floatFromInt(state.page_heights[page])); +} + +/// A jump to `at` (Back, Forward): the core is there at once and the strip +/// follows through the motion system, a far one as one body's slide, the +/// page rendered where it comes to rest. Not laid out yet (a pane just +/// reopened), it is anchored there for the first layout. +pub fn landAt(core: *pardes.Pardes, pane: *pardes.Pane, at: Place) void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + const page = @min(at.page, state.page_count -| 1); + activatePage(core, pane, page, false); + state.scroll_to_page_pending = false; + if (placeY(pane, .{ .page = page, .at = at.at })) |y| { + state.layout_anchor_pending = false; + // Where it stands already (half a pixel is the place's rounding): + // not moved, not kicked. + if (@abs(y - state.document_scroll_y) >= 0.5) state.document_scroll_y = y; + } else { + state.layout_anchor_pending = true; + state.layout_anchor_page = page; + state.layout_anchor_fraction = @as(f64, @floatFromInt(at.at)) / place_scale; + } +} + pub fn setPage(core: *pardes.Pardes, pane: *pardes.Pane, page: usize) void { activatePage(core, pane, page, true); } diff --git a/test/pdf.zig b/test/pdf.zig index 5f7ebc52..bbb0da66 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -2448,3 +2448,113 @@ test "a PDF's fit and tint, and Collapse, read in its ctl and come back from a D try std.testing.expectEqualStrings("height", @tagName(pv.fit)); try std.testing.expectEqualStrings("full", @tagName(pv.tint)); } + +/// The jumplist's PDF (pdfrender): the Intel SDM (5363 pages, the author's +/// stress file) when this machine has it, else the docs' PDF; the same +/// checks either way. +fn jumpPdf() []const u8 { + const sdm = "/home/goblin/00-projects/kvmctf/harnesses/contained-official/visor-uefi/intel-ia32-manual.pdf"; + std.Io.Dir.accessAbsolute(std.testing.io, sdm, .{}) catch return "docs/design.pdf"; + return sdm; +} + +/// Lays the PDF pane out (a frame drawn) and syncs, so the jumplist sees it. +fn jumpFrame(p: *Pardes) !void { + var frame: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer frame.deinit(); + _ = try p.render(frame.allocator()); + p.sync(); +} + +fn stripAtPlace(p: *Pardes, pane: *pardes.Pane, page: usize, fraction: f64) !f64 { + const pv = &pane.pdf.?; + pv.document_scroll_y = @as(f64, @floatFromInt(pv.page_starts[page])) + fraction * @as(f64, @floatFromInt(pv.page_heights[page])); + try jumpFrame(p); + return pv.document_scroll_y; +} + +test "a PDF's jumps: Back after a far jump returns to the exact offset, two places on one page are two entries" { + if (!pdf_enabled or platform == .web) return error.SkipZigTest; + const p = try Pardes.init(std.testing.allocator, .{ .file = jumpPdf(), .cols = 80, .rows = 28 }); + defer p.deinit(); + p.native_images = true; + p.presentation.enabled = false; + const id = p.active; + const pane = p.panes[id].?; + const pv = &pane.pdf.?; + try jumpFrame(p); + try std.testing.expect(pv.page_count > 3); + // Two places on page 1, more than a body apart: two entries. + const before = p.njumps; + _ = try stripAtPlace(p, pane, 1, 0.05); + const near = try stripAtPlace(p, pane, 1, 0.8); + try std.testing.expectEqual(before + 2, p.njumps); + try std.testing.expect(p.jumps[p.njumps - 1].pdf); + // A far jump (a Look at a page), then Back: the exact offset, not the + // page's top, and its page. + look.lookAt(p, id, if (pv.page_count > 3000) ":3000" else ":4"); + try jumpFrame(p); + try std.testing.expect(pv.page >= 3); + p.jumpBy(-1); + try jumpFrame(p); + try std.testing.expectEqual(@as(usize, 1), pv.page); + try std.testing.expectApproxEqAbs(near, pv.document_scroll_y, 0.5); + // A wheel's small steps are the same visit: the entry follows them. + const n = p.njumps; + for (0..4) |_| { + _ = panes.Pdf.scrollPane(p, pane, 30, true); + try jumpFrame(p); + } + try std.testing.expectEqual(n, p.njumps); +} + +test "a PDF's jumps: JumpScope file keeps to the PDF, and a closed PDF reopens at its place" { + if (!pdf_enabled or platform == .web) return error.SkipZigTest; + // Recent keeps a file by the path it was opened as: a real one here, + // as a Look opens one. + var cwd_buf: [4096]u8 = undefined; + const cwd = cwd_buf[0..try std.Io.Dir.cwd().realPath(std.testing.io, &cwd_buf)]; + var path_buf: [4200]u8 = undefined; + const path = try std.fmt.bufPrint(&path_buf, "{s}/docs/design.pdf", .{cwd}); + const p = try Pardes.init(std.testing.allocator, .{ .file = path, .cols = 80, .rows = 28 }); + defer p.deinit(); + p.native_images = true; + p.presentation.enabled = false; + try std.testing.expectEqual(config.Runtime.JumpScope.file, p.settings.jump_scope); + const doc = p.active; + const pane = p.panes[doc].?; + try jumpFrame(p); + const first = try stripAtPlace(p, pane, 1, 0.3); + // A text between, then a far place in the PDF. + look.lookAt(p, doc, "../build.zig:10"); + try jumpFrame(p); + try std.testing.expect(p.active != doc); + p.active = doc; + _ = try stripAtPlace(p, pane, 3, 0.5); + // Back from the PDF keeps to it: its own earlier place, not the text. + p.jumpBy(-1); + try jumpFrame(p); + try std.testing.expectEqual(doc, p.active); + try std.testing.expectApproxEqAbs(first, pane.pdf.?.document_scroll_y, 0.5); + // Closed, its entries stay; Back from the text reopens it at the place. + p.jumpBy(1); + try jumpFrame(p); + try p.removePane(doc, null); + try jumpFrame(p); + var closed = false; + for (p.jumps[0..p.njumps]) |j| closed = closed or (j.serial == 0 and j.pdf); + try std.testing.expect(closed); + var tries: usize = 0; + while (tries < 4) : (tries += 1) { + p.jumpBy(-1); + try jumpFrame(p); + if (p.panes[p.active].?.pdf != null) break; + } + const reopened = p.panes[p.active].?; + try std.testing.expect(reopened.pdf != null); + const pv = &reopened.pdf.?; + // The newest PDF place before the text: page 4, half way down. + try std.testing.expectEqual(@as(usize, 3), pv.page); + const want = @as(f64, @floatFromInt(pv.page_starts[3])) + 0.5 * @as(f64, @floatFromInt(pv.page_heights[3])); + try std.testing.expectApproxEqAbs(want, pv.document_scroll_y, 0.5); +} |
