diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pdf.zig | 70 |
1 files changed, 21 insertions, 49 deletions
diff --git a/src/pdf.zig b/src/pdf.zig index 1a983e2c..30828f92 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -117,25 +117,14 @@ pub const RenderRequest = struct { } }; -pub const default_render_request: RenderRequest = .{}; pub const absolute_max_render_dimension: u16 = 4096; /// The most one render buffer holds: a whole page, or one band of a page /// taller than this. pub const max_owned_raster_bytes: usize = @as(usize, absolute_max_render_dimension) * absolute_max_render_dimension * 4; -/// The tallest page raster any request produces (rows), whatever it is shown at. -pub const max_raster_rows: usize = c.PARDES_PDF_MAX_RASTER_ROWS; -/// 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); - try std.testing.expectEqual(@as(usize, 64 * 1024 * 1024), max_owned_raster_bytes); -} - /// 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; @@ -1049,21 +1038,14 @@ pub const Document = struct { page: usize, request: RenderRequest, ) !BoundedRequest { - if (page >= document.pages or page > std.math.maxInt(c_int)) - return error.PageOutOfRange; + _ = try document.checkedPage(page); if (request.dpi == 0 or request.max_dimension == 0 or request.max_dimension > absolute_max_render_dimension) return error.InvalidRenderRequest; return .{ .dpi = request.dpi, - .minimum_width = @intCast(@min( - request.minimum_width, - @as(u32, @intCast(std.math.maxInt(c_int))), - )), - .minimum_height = @intCast(@min( - request.minimum_height, - @as(u32, @intCast(std.math.maxInt(c_int))), - )), + .minimum_width = clampInt(request.minimum_width), + .minimum_height = clampInt(request.minimum_height), .max_dimension = request.max_dimension, .display_width = clampInt(request.display_width), .display_height = clampInt(request.display_height), @@ -1077,10 +1059,9 @@ pub const Document = struct { /// Plain UTF-8-ish text projection for one zero-based page. MuPDF owns the /// temporary buffer; callers receive an allocator-owned copy. pub fn pageText(document: *Document, gpa: std.mem.Allocator, page: usize) ![]u8 { - if (page >= document.pages or page > std.math.maxInt(c_int)) - return error.PageOutOfRange; + const page_number = try document.checkedPage(page); var text: c.pardes_pdf_text = std.mem.zeroes(c.pardes_pdf_text); - if (c.pardes_pdf_page_text(document.handle, @intCast(page), &text) != 0) + if (c.pardes_pdf_page_text(document.handle, page_number, &text) != 0) return error.TextFailed; defer c.pardes_pdf_drop_text(document.handle, text.handle); if (text.len == 0) return gpa.dupe(u8, ""); @@ -1342,7 +1323,8 @@ fn emitOffsetRotatedPdf(bytes: *std.Io.Writer, offsets: []usize, stream: []const try finishGeneratedPdf(bytes, offsets); } -fn makeOffsetRotatedPdf(gpa: std.mem.Allocator) ![]u8 { +/// TEST-ONLY (here and in the core's tests): pages, no outline tree. +pub fn makeNoOutlineTestPdf(gpa: std.mem.Allocator) ![]u8 { const stream = "q 1 0 0 rg 120 220 160 150 re f Q\n"; var offsets: [5]usize = undefined; return allocateGeneratedPdf(gpa, &offsets, stream, emitOffsetRotatedPdf); @@ -1379,23 +1361,12 @@ fn emitOutlinePdf(bytes: *std.Io.Writer, offsets: []usize, _: void) !void { try finishGeneratedPdf(bytes, offsets); } -fn makeOutlinePdf(gpa: std.mem.Allocator) ![]u8 { +/// TEST-ONLY (here and in the core's tests): an outline of every kind. +pub fn makeOutlineTestPdf(gpa: std.mem.Allocator) ![]u8 { var offsets: [12]usize = undefined; return allocateGeneratedPdf(gpa, &offsets, {}, emitOutlinePdf); } -/// TEST-ONLY generated fixture shared with the core integration tests. It is -/// public because `pardes.zig` imports this file as a module; production code -/// has no caller and the function is dead-stripped. -pub fn makeOutlineTestPdf(gpa: std.mem.Allocator) ![]u8 { - return makeOutlinePdf(gpa); -} - -/// TEST-ONLY generated PDF with pages but no outline tree. -pub fn makeNoOutlineTestPdf(gpa: std.mem.Allocator) ![]u8 { - return makeOffsetRotatedPdf(gpa); -} - fn emitLinksPdf(bytes: *std.Io.Writer, offsets: []usize, _: void) !void { const stream = "BT /F1 6 Tf 20 240 Td (target.txt) Tj ET\n" ++ "BT /F1 6 Tf 20 120 Td (https://example.com/same) Tj ET\n" ++ @@ -1677,7 +1648,7 @@ fn makeWideOutlinePdf(gpa: std.mem.Allocator, items: usize) ![]u8 { } test "PDF outline is a stable owned DFS view with native destinations" { - const fixture = try makeOutlinePdf(std.testing.allocator); + const fixture = try makeOutlineTestPdf(std.testing.allocator); defer std.testing.allocator.free(fixture); var document = try Document.openBytes(fixture); @@ -1758,7 +1729,7 @@ test "PDF outline accepts manual-sized trees and rejects item overflow atomicall } test "PDF outline absence and hostile depth are atomic and repeatable" { - const plain_fixture = try makeOffsetRotatedPdf(std.testing.allocator); + const plain_fixture = try makeNoOutlineTestPdf(std.testing.allocator); defer std.testing.allocator.free(plain_fixture); var plain_document = try Document.openBytes(plain_fixture); defer plain_document.deinit(); @@ -1823,14 +1794,15 @@ test "RGBA allocation and buffer-validation failures leave the document renderab document.render(failing.allocator(), 0, .{}, &.{}), ); + const request: RenderRequest = .{}; var raw_layout: c.pardes_pdf_raster_layout = std.mem.zeroes(c.pardes_pdf_raster_layout); try std.testing.expectEqual(c.PARDES_PDF_OK, c.pardes_pdf_measure_render( document.handle, 0, - default_render_request.dpi, - default_render_request.minimum_width, - default_render_request.minimum_height, - default_render_request.max_dimension, + request.dpi, + request.minimum_width, + request.minimum_height, + request.max_dimension, 0, 0, &raw_layout, @@ -1843,10 +1815,10 @@ test "RGBA allocation and buffer-validation failures leave the document renderab try std.testing.expectEqual(c.PARDES_PDF_ERROR, c.pardes_pdf_render_into( document.handle, 0, - default_render_request.dpi, - default_render_request.minimum_width, - default_render_request.minimum_height, - default_render_request.max_dimension, + request.dpi, + request.minimum_width, + request.minimum_height, + request.max_dimension, 0, 0, null, @@ -2013,7 +1985,7 @@ test "highlighted render changes pixels while plain render stays stable" { } test "offset crop and rotation keep normalized highlights pixel-aligned" { - const fixture = try makeOffsetRotatedPdf(std.testing.allocator); + const fixture = try makeNoOutlineTestPdf(std.testing.allocator); defer std.testing.allocator.free(fixture); var document = try Document.openBytes(fixture); |
