diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pdf_view.zig | 164 |
1 files changed, 41 insertions, 123 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig index 0745a6fc..ddc9c896 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -1472,55 +1472,11 @@ pub fn activeGeometry(state: *const State, viewport: Viewport) ?image.NativeGeom 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 }; @@ -2842,9 +2798,7 @@ pub fn sameBytes(core: *pardes.Pardes, pane: *const pardes.Pane) bool { /// 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 } }); - } + if (core.paneIdOf(pane)) |id| core.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); state.deinit(core.pdf_gpa); } @@ -2862,16 +2816,6 @@ pub fn reopen(core: *pardes.Pardes, pane: *pardes.Pane) !void { } } -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; @@ -2921,8 +2865,11 @@ pub fn reloadWatched( core: *pardes.Pardes, pane: *pardes.Pane, ) !void { - try reloadPane(core, pane); - refreshCleanSections(core, &pane.pdf.?); + if (comptime !enabled) return error.PdfDisabled; + const state = &(pane.pdf orelse return error.MissingPdfState); + try state.reload(core.pdf_gpa); + resetPageChrome(pane); + refreshCleanSections(core, state); } fn rearmCleanSections( @@ -3146,10 +3093,6 @@ pub fn landAt(core: *pardes.Pardes, pane: *pardes.Pane, at: Place) void { } } -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); @@ -3162,17 +3105,9 @@ pub fn toggleTint(pane: *pardes.Pane) void { 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 rect = core.rects[core.paneIdOf(pane) orelse return null]; const cols = rect.w -| config.GUTTER; const rows = rect.h -| pane.tag_rows; if (cols == 0 or rows == 0) return null; @@ -3206,26 +3141,34 @@ pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?image return activeGeometry(state, view); } +/// 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 rect = for (core.panes, 0..) |slot, id| { - if (slot == pane) break core.rects[id]; - } else 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 (row < body_y or row >= body_y + (rect.h -| pane.tag_rows)) return null; - const local_y = @as(f64, @floatFromInt( + if (!state.layout_valid or row < body_y or row >= body_y + (rect.h -| pane.tag_rows)) return null; + const document_y = state.document_scroll_y + @as(f64, @floatFromInt( @as(u32, row - body_y) * core.cell_pixels.h + core.cell_pixels.h / 2, )); - return pageAtViewportY(&state, 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; } +/// Some page of the pane is placed where it is drawn. 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); + for (state.rasters[0..state.rasters_len]) |*raster| { + if (raster.valid and raster.rgba.len > 0 and + placedGeometry(state, raster, view, raster.page) != null) return true; + } + return false; } pub fn nativePageAtGridRow( @@ -3242,18 +3185,8 @@ pub fn nativePageAtGridRow( 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); -} - +/// The point of `page` under the middle of grid cell `col`,`row`, where +/// that page is placed; `clamp_to_page` holds a point past it on its edge. pub fn panePointAtPage( core: *const pardes.Pardes, pane: *const pardes.Pane, @@ -3265,14 +3198,15 @@ pub fn panePointAtPage( 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 raster = rasterForPageConst(state, page) orelse return null; + if (raster.rgba.len == 0) return null; + const placed = placedGeometry(state, raster, view, page) orelse return null; + const rect = core.rects[core.paneIdOf(pane) orelse 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); + return pointAtGeometry(placed.geometry, placed.pixel_offset_y, raster.iw, raster.ih, raster.band_y, px, py, clamp_to_page); } pub fn probeAt( @@ -3317,7 +3251,7 @@ pub fn beginDrag( 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; + const point = panePointAtPage(core, pane, state.page, col, row, false) orelse return false; state.drag_anchor = point; state.drag_head = point; if (!snap_word) return true; @@ -3331,15 +3265,6 @@ pub fn beginDrag( 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, @@ -3350,7 +3275,7 @@ pub fn updateSelection( 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; + 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; const result = state.setSelection(core.pdf_gpa, anchor, point, invalidate_raster); if (result != .failed) state.drag_head = point; @@ -3504,19 +3429,6 @@ pub fn highlightInput( }; } -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 { @@ -3563,7 +3475,12 @@ pub fn moving(core: *const pardes.Pardes) bool { 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); + if (!core.native_images) { + // The text layer: a page a notch. + const state = &(pane.pdf orelse return); + if (stepPage(state, core.pdf_gpa, direction)) resetPageChrome(pane); + return; + } 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); @@ -3571,10 +3488,11 @@ 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)); - _ = panPane(core, pane, .horizontal, direction, scaledStep(core.cell_pixels.w, cols)); + _ = panPixels(state, placed, .horizontal, direction, scaledStep(core.cell_pixels.w, cols)); } pub fn draw( |
