summaryrefslogtreecommitdiff
path: root/src/pdf.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/pdf.zig')
-rw-r--r--src/pdf.zig95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/pdf.zig b/src/pdf.zig
index 4a671315..84d6a4e5 100644
--- a/src/pdf.zig
+++ b/src/pdf.zig
@@ -28,6 +28,8 @@ pub const max_owned_raster_bytes: usize = @as(usize, absolute_max_render_dimensi
/// MuPDF's per-document eviction cache ceiling. The owned RGBA render buffer
/// is allocated separately by Pardes and does not count against this store.
pub const store_limit_bytes: usize = c.PARDES_PDF_STORE_LIMIT_BYTES;
+/// Maximum native outline rows accepted atomically from one document.
+pub const max_outline_items: usize = c.PARDES_PDF_MAX_OUTLINE_ITEMS;
test "MuPDF document cache has an explicit conservative ceiling" {
try std.testing.expectEqual(@as(usize, 64 * 1024 * 1024), store_limit_bytes);
@@ -996,6 +998,43 @@ fn makeTooDeepOutlinePdf(gpa: std.mem.Allocator) ![]u8 {
return finishGeneratedPdf(&bytes, gpa, offsets);
}
+fn makeWideOutlinePdf(gpa: std.mem.Allocator, items: usize) ![]u8 {
+ if (items == 0) return error.EmptyOutlineFixture;
+ const first_outline_item = 5;
+ const object_count = try std.math.add(usize, first_outline_item, items);
+ const offsets = try gpa.alloc(usize, object_count);
+ defer gpa.free(offsets);
+ @memset(offsets, 0);
+ var bytes: std.ArrayList(u8) = .empty;
+ errdefer bytes.deinit(gpa);
+
+ try bytes.appendSlice(gpa, "%PDF-1.7\n%\xE2\xE3\xCF\xD3\n");
+ try beginPdfObject(&bytes, gpa, offsets, 1);
+ try bytes.appendSlice(gpa, "<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>\nendobj\n");
+ try beginPdfObject(&bytes, gpa, offsets, 2);
+ try bytes.appendSlice(gpa, "<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n");
+ try beginPdfObject(&bytes, gpa, offsets, 3);
+ try bytes.appendSlice(gpa, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> >>\nendobj\n");
+ try beginPdfObject(&bytes, gpa, offsets, 4);
+ try bytes.print(gpa, "<< /Type /Outlines /First 5 0 R /Last {d} 0 R /Count {d} >>\nendobj\n", .{
+ first_outline_item + items - 1,
+ items,
+ });
+
+ for (0..items) |ordinal| {
+ const number = first_outline_item + ordinal;
+ try beginPdfObject(&bytes, gpa, offsets, number);
+ try bytes.print(gpa, "<< /Title (Section {d}) /Parent 4 0 R /Dest [3 0 R /Fit]", .{ordinal});
+ if (ordinal != 0)
+ try bytes.print(gpa, " /Prev {d} 0 R", .{number - 1});
+ if (ordinal + 1 < items)
+ try bytes.print(gpa, " /Next {d} 0 R", .{number + 1});
+ try bytes.appendSlice(gpa, " >>\nendobj\n");
+ }
+
+ return finishGeneratedPdf(&bytes, gpa, offsets);
+}
+
test "PDF outline is a stable owned DFS view with native destinations" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
@@ -1061,6 +1100,62 @@ test "PDF outline is a stable owned DFS view with native destinations" {
}
}
+test "PDF outline accepts manual-sized trees and rejects item overflow atomically" {
+ const manual_items = 4105;
+ try std.testing.expect(max_outline_items >= manual_items);
+
+ var manual_tmp = std.testing.tmpDir(.{});
+ defer manual_tmp.cleanup();
+ const manual_fixture = try makeWideOutlinePdf(std.testing.allocator, manual_items);
+ defer std.testing.allocator.free(manual_fixture);
+ try manual_tmp.dir.writeFile(std.testing.io, .{
+ .sub_path = "manual-outline.pdf",
+ .data = manual_fixture,
+ });
+ var manual_path_buffer: [256]u8 = undefined;
+ const manual_path = try std.fmt.bufPrint(
+ &manual_path_buffer,
+ ".zig-cache/tmp/{s}/manual-outline.pdf",
+ .{manual_tmp.sub_path},
+ );
+ var manual_document = try Document.open(manual_path);
+ defer manual_document.deinit();
+ var outline = try manual_document.outline(std.testing.allocator);
+ defer outline.deinit(std.testing.allocator);
+ try std.testing.expectEqual(@as(usize, manual_items), outline.entries.len);
+ try std.testing.expectEqualStrings("Section 0", outline.entries[0].title.?);
+ try std.testing.expectEqualStrings("Section 4104", outline.entries[manual_items - 1].title.?);
+ try std.testing.expectEqual(@as(usize, 0), outline.entries[manual_items - 1].destination.internal.page);
+
+ var overflow_tmp = std.testing.tmpDir(.{});
+ defer overflow_tmp.cleanup();
+ const overflow_fixture = try makeWideOutlinePdf(std.testing.allocator, max_outline_items + 1);
+ defer std.testing.allocator.free(overflow_fixture);
+ try overflow_tmp.dir.writeFile(std.testing.io, .{
+ .sub_path = "overflow-outline.pdf",
+ .data = overflow_fixture,
+ });
+ var overflow_path_buffer: [256]u8 = undefined;
+ const overflow_path = try std.fmt.bufPrint(
+ &overflow_path_buffer,
+ ".zig-cache/tmp/{s}/overflow-outline.pdf",
+ .{overflow_tmp.sub_path},
+ );
+ var overflow_document = try Document.open(overflow_path);
+ defer overflow_document.deinit();
+ var raw: c.pardes_pdf_outline_result = undefined;
+ @memset(std.mem.asBytes(&raw), 0xa5);
+ try std.testing.expectEqual(
+ c.PARDES_PDF_LIMIT_EXCEEDED,
+ c.pardes_pdf_load_outline(overflow_document.handle, &raw),
+ );
+ try std.testing.expect(raw.handle == null);
+ try std.testing.expect(raw.items == null);
+ try std.testing.expectEqual(@as(usize, 0), raw.item_count);
+ try std.testing.expect(raw.bytes == null);
+ try std.testing.expectEqual(@as(usize, 0), raw.bytes_len);
+}
+
test "PDF outline absence and hostile depth are atomic and repeatable" {
var plain_tmp = std.testing.tmpDir(.{});
defer plain_tmp.cleanup();