summaryrefslogtreecommitdiff
path: root/src/pdf.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/pdf.zig')
-rw-r--r--src/pdf.zig62
1 files changed, 60 insertions, 2 deletions
diff --git a/src/pdf.zig b/src/pdf.zig
index 5aedcb50..18037bb7 100644
--- a/src/pdf.zig
+++ b/src/pdf.zig
@@ -63,6 +63,42 @@ test "custom allocator owns a complete PDF document lifecycle" {
try std.testing.expect(raster.rgba.len > 0);
}
+test "two lanes render two pages of one document at once, each as the base renders it, and a cookie aborts" {
+ const gpa = std.testing.allocator;
+ var base = try openTestFile("docs/design.pdf");
+ defer base.deinit();
+ try std.testing.expect(base.pages >= 2);
+ const Lane = struct {
+ doc: Document,
+ page: usize,
+ out: ?Render = null,
+ fn run(lane: *@This()) void {
+ lane.doc.laneOpen() catch return;
+ lane.out = lane.doc.render(std.testing.allocator, lane.page, .{}, &.{}) catch null;
+ }
+ };
+ // Cloned on the base's thread; each lane's document opened on its own.
+ var lanes = [2]Lane{ .{ .doc = try base.laneClone(), .page = 0 }, .{ .doc = try base.laneClone(), .page = 1 } };
+ defer for (&lanes) |*lane| {
+ if (lane.out) |r| gpa.free(r.rgba);
+ lane.doc.deinit();
+ };
+ var threads: [2]std.Thread = undefined;
+ for (&threads, &lanes) |*t, *lane| t.* = try std.Thread.spawn(.{}, Lane.run, .{lane});
+ for (threads) |t| t.join();
+ for (lanes) |lane| {
+ const want = try base.render(gpa, lane.page, .{}, &.{});
+ defer gpa.free(want.rgba);
+ try std.testing.expect(std.mem.eql(u8, want.rgba, lane.out.?.rgba));
+ }
+ // An abort set before the run stops it at its first operator.
+ var cookie = std.mem.zeroes(Cookie);
+ cookie.abort = 1;
+ lanes[0].doc.setCookie(&cookie);
+ try std.testing.expectError(error.Aborted, lanes[0].doc.render(gpa, 1, .{}, &.{}));
+ lanes[0].doc.setCookie(null);
+}
+
/// One raster-quality request. `minimum_*` asks MuPDF for enough source pixels
/// to avoid backend upscaling; `max_dimension` caps a raster row always, and
/// the page's height unless that would draw it below `display_*`, the size
@@ -102,6 +138,7 @@ test "MuPDF document cache has an explicit conservative ceiling" {
/// Page-space geometry is normalized to the page bounds, not a render. It
/// therefore survives DPI changes and can be shared by Kitty and SDL.
+pub const Cookie = c.pardes_pdf_cookie;
pub const Point = c.pardes_pdf_point;
pub const Quad = c.pardes_pdf_quad;
@@ -678,6 +715,25 @@ pub const Document = struct {
return .{ .handle = handle, .pages = @intCast(page_count) };
}
+ /// A render lane of this document (`pardes_pdf_lane_clone`): its own
+ /// cloned context, its own document opened (`laneOpen`) on the thread
+ /// that renders with it. Deinit every lane before `base`.
+ pub fn laneClone(base: *Document) !Document {
+ const handle = c.pardes_pdf_lane_clone(base.handle) orelse return error.OpenFailed;
+ return .{ .handle = handle, .pages = base.pages };
+ }
+
+ pub fn laneOpen(lane: *Document) !void {
+ if (c.pardes_pdf_lane_open(lane.handle) != c.PARDES_PDF_OK) return error.OpenFailed;
+ }
+
+ /// The cookie this document's renders run under until set again: set
+ /// its `abort` from any thread and a render stops at its next operator
+ /// (`error.Aborted`).
+ pub fn setCookie(document: *Document, cookie: ?*Cookie) void {
+ c.pardes_pdf_set_cookie(document.handle, cookie);
+ }
+
/// Link annotations are loaded only for the pointed page, separately from
/// text/raster work. Returned URIs survive cache eviction and page changes.
pub fn linkAt(document: *Document, gpa: std.mem.Allocator, page: usize, point: Point) !?Link {
@@ -897,7 +953,7 @@ pub const Document = struct {
if (highlights.len > c.PARDES_PDF_MAX_RESULT_QUADS) return error.RenderFailed;
const highlight_ptr: ?[*]const c.pardes_pdf_highlight =
if (highlights.len == 0) null else @ptrCast(highlights.ptr);
- if (c.pardes_pdf_render_into(
+ const status = c.pardes_pdf_render_into(
document.handle,
@intCast(page),
bounded.dpi,
@@ -916,7 +972,9 @@ pub const Document = struct {
@intCast(band.y),
@intCast(band.height),
@intFromBool(transparent_paper),
- ) != c.PARDES_PDF_OK) return error.RenderFailed;
+ );
+ if (status == c.PARDES_PDF_ABORTED) return error.Aborted;
+ if (status != c.PARDES_PDF_OK) return error.RenderFailed;
}
/// Only the highlight pass of `renderIntoAtWithPaper`, over `rgba`, which