diff options
Diffstat (limited to 'src/pdf_view.zig')
| -rw-r--r-- | src/pdf_view.zig | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig index a0a48178..654284f4 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -2759,7 +2759,9 @@ pub fn openPane( // pane meanwhile. core.reserved_slots[id] = true; defer core.reserved_slots[id] = false; - var state = if (filesystem.localPath(path) != null) + const state = try core.pdf_gpa.create(State); + errdefer core.pdf_gpa.destroy(state); + 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); @@ -2779,7 +2781,7 @@ pub fn openPane( /// 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; + 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); @@ -2792,13 +2794,15 @@ pub fn deinitPane(core: *pardes.Pardes, pane: *pardes.Pane, state: *State) void if (comptime !enabled) return; if (core.paneIdOf(pane)) |id| core.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); state.deinit(core.pdf_gpa); + core.pdf_gpa.destroy(state); + pane.pdf = null; } /// A PDF pane loaded from a dump given its document again (MuPDF's, so /// never in the dump) from its file; one changed since is laid out anew. pub fn reopen(core: *pardes.Pardes, pane: *pardes.Pane) !void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; const local = filesystem.localPath(state.path) orelse return error.NonLocalPath; const bytes = try filesystem.readFile(core.pdf_gpa, local); defer core.pdf_gpa.free(bytes); @@ -2858,7 +2862,7 @@ pub fn reloadWatched( pane: *pardes.Pane, ) !void { if (comptime !enabled) return error.PdfDisabled; - const state = &(pane.pdf orelse return error.MissingPdfState); + const state = pane.pdf orelse return error.MissingPdfState; try state.reload(core.pdf_gpa); resetPageChrome(pane); refreshCleanSections(core, state); @@ -2895,7 +2899,7 @@ fn rearmCleanSections( 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); + 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. @@ -2945,7 +2949,7 @@ fn sectionsOwner(core: *pardes.Pardes, output_id: usize) ?SectionsOwner { 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 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) @@ -3017,7 +3021,7 @@ pub fn activatePage( reveal: bool, ) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (state.activatePage(core.pdf_gpa, page, reveal)) resetPageChrome(pane); } @@ -3028,7 +3032,7 @@ pub fn revealOutlineDestination( ) void { if (comptime !enabled) return; activatePage(core, pane, destination.page, false); - const state = &pane.pdf.?; + 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. @@ -3044,7 +3048,7 @@ 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; + 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]); @@ -3056,7 +3060,7 @@ pub fn place(pane: *const pardes.Pane) ?Place { /// 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; + 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])) + @@ -3069,7 +3073,7 @@ pub fn placeY(pane: *const pardes.Pane, at: Place) ?f64 { /// 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 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; @@ -3087,13 +3091,13 @@ pub fn landAt(core: *pardes.Pardes, pane: *pardes.Pane, at: Place) void { pub fn toggleFit(pane: *pardes.Pane) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse 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); + const state = pane.pdf orelse return; state.toggleTint(); } @@ -3111,7 +3115,7 @@ pub fn paneViewport(core: *const pardes.Pardes, pane: *const pardes.Pane) ?Viewp pub fn ensurePaneLayout(core: *pardes.Pardes, pane: *pardes.Pane) ?Viewport { if (comptime !enabled) return null; - const state = &(pane.pdf orelse return null); + const state = pane.pdf orelse return null; const view = paneViewport(core, pane) orelse return null; ensureLayout(state, view); return view; @@ -3128,7 +3132,7 @@ pub fn tintColors(core: *const pardes.Pardes) TintColors { 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 state = if (pane.pdf) |view| view else return null; const view = paneViewport(core, pane) orelse return null; return activeGeometry(state, view); } @@ -3136,7 +3140,7 @@ pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?image /// The page under grid row `row`, from the middle of its cell. 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 state = pane.pdf orelse return null; const rect = core.rects[core.paneIdOf(pane) orelse return null]; const body_y = core.bodyTop(pane, rect); if (!state.layout_valid or row < body_y or row >= body_y + (rect.h -| pane.tag_rows)) return null; @@ -3154,7 +3158,7 @@ pub fn pageAtGridRow(core: *const pardes.Pardes, pane: *const pardes.Pane, row: 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 state = if (pane.pdf) |view| view else return false; const view = paneViewport(core, pane) orelse return false; for (state.rasters[0..state.rasters_len]) |*raster| { if (raster.valid and raster.rgba.len > 0 and @@ -3170,7 +3174,7 @@ pub fn nativePageAtGridRow( ) ?usize { if (comptime !enabled) return null; if (!core.native_images) return null; - const state = if (pane.pdf) |*view| view else 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; @@ -3188,7 +3192,7 @@ pub fn panePointAtPage( clamp_to_page: bool, ) ?Point { if (comptime !enabled) return null; - const state = if (pane.pdf) |*view| view else return null; + const state = if (pane.pdf) |view| view else return null; const view = paneViewport(core, pane) orelse return null; const raster = rasterForPageConst(state, page) orelse return null; if (raster.rgba.len == 0) return null; @@ -3210,7 +3214,7 @@ pub fn probeAt( 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); + 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) { @@ -3241,7 +3245,7 @@ pub fn beginDrag( 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.?; + const state = pane.pdf.?; state.clearDrag(); const point = panePointAtPage(core, pane, state.page, col, row, false) orelse return false; state.drag_anchor = point; @@ -3265,7 +3269,7 @@ pub fn updateSelection( invalidate_raster: bool, ) SelectionUpdate { if (comptime !enabled) return .failed; - const state = &(pane.pdf orelse return .failed); + const state = pane.pdf orelse return .failed; const anchor = state.drag_anchor orelse return .failed; const point = panePointAtPage(core, pane, state.page, col, row, true) orelse return .failed; if (state.drag_head) |head| if (head.x == point.x and head.y == point.y) return .stationary; @@ -3335,7 +3339,7 @@ pub fn pointerUpdate( pub fn pointerCancel(pane: *pardes.Pane, drag: PointerDrag) void { if (comptime !enabled) return; - if (drag.native) if (pane.pdf) |*state| { + if (drag.native) if (pane.pdf) |state| { if (drag.selection_changed) state.invalidateRaster(state.page); state.clearDrag(); }; @@ -3349,7 +3353,7 @@ pub fn pointerRelease( chorded: bool, ) PointerRelease { if (comptime !enabled) return .{}; - const state = &(pane.pdf orelse 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 @@ -3427,7 +3431,7 @@ pub fn scrollPane(core: *pardes.Pardes, pane: *pardes.Pane, delta_pixels: f64, g if (comptime !enabled) return false; const tz = tracy.zone(@src(), "pdf.scroll_notch"); defer tz.end(); - const state = &(pane.pdf orelse return false); + 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; @@ -3461,7 +3465,7 @@ fn stripAt(state: *State, view: Viewport, motion: pardes.animation.Motion, now_n /// 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; + for (core.panes) |slot| if (slot) |pane| if (pane.pdf) |state| if (state.follow.moving()) return true; return false; } @@ -3469,7 +3473,7 @@ pub fn verticalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) vo if (comptime !enabled) return; if (!core.native_images) { // The text layer: a page a notch. - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (stepPage(state, core.pdf_gpa, direction)) resetPageChrome(pane); return; } @@ -3480,7 +3484,7 @@ pub fn verticalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) vo pub fn horizontalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (!core.native_images or state.fit != .height) return; const placed = paneGeometry(core, pane) orelse return; const cols: u32 = @intCast(@max(1, config.wheel_cols)); @@ -3499,7 +3503,7 @@ pub fn draw( ) 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 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); |
