diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-23 10:51:51 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-23 17:10:58 -0300 |
| commit | 7b743fcc7d568f13512a59089c5214bb6fa3282f (patch) | |
| tree | ab133f70ded7b9b415115345adfc6ff043117b03 /src/pdf.zig | |
| parent | b8f076b63d6fcbb01539d5541520a32bccc0e219 (diff) | |
| download | pardes-7b743fcc7d568f13512a59089c5214bb6fa3282f.tar.gz pardes-7b743fcc7d568f13512a59089c5214bb6fa3282f.zip | |
Audit (LSP, PDF): one client dispatch, docParams, renameEdits in one loop, std.c externs; one whole-page render, openBytes only
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/pdf.zig')
| -rw-r--r-- | src/pdf.zig | 392 |
1 files changed, 114 insertions, 278 deletions
diff --git a/src/pdf.zig b/src/pdf.zig index 5adc62d1..5aedcb50 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -56,9 +56,9 @@ test "custom allocator owns a complete PDF document lifecycle" { start(std.testing.allocator); defer stop(); - var document = try Document.open("docs/design.pdf"); + var document = try openTestFile("docs/design.pdf"); defer document.deinit(); - const raster = try document.render(std.testing.allocator, 0); + const raster = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(raster.rgba); try std.testing.expect(raster.rgba.len > 0); } @@ -666,20 +666,6 @@ pub const Document = struct { handle: *c.pardes_pdf_document, pages: usize, - pub fn open(path: []const u8) !Document { - var path_buf: [4096]u8 = undefined; - const path_z = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{path}, 0) catch - return error.PathTooLong; - var page_count: c_int = 0; - const handle = c.pardes_pdf_open(path_z.ptr, &page_count) orelse - return error.OpenFailed; - if (page_count < 1) { - c.pardes_pdf_close(handle); - return error.EmptyDocument; - } - return .{ .handle = handle, .pages = @intCast(page_count) }; - } - pub fn deinit(document: *Document) void { c.pardes_pdf_close(document.handle); document.* = undefined; @@ -843,44 +829,22 @@ pub const Document = struct { return .{ .width = size.width, .height = size.height }; } - /// Render one zero-based page directly into allocator-owned packed RGBA. - pub fn render(document: *Document, gpa: std.mem.Allocator, page: usize) !Render { - return document.renderAt(gpa, page, default_render_request); - } - - pub fn renderAt( - document: *Document, - gpa: std.mem.Allocator, - page: usize, - request: RenderRequest, - ) !Render { - return document.renderInternal(gpa, page, request, null); - } - - /// Render and bake normalized oriented highlights through MuPDF's draw - /// device. An empty list takes the exact same plain-render path as render. - pub fn renderWithHighlights( - document: *Document, - gpa: std.mem.Allocator, - page: usize, - highlights: []const Highlight, - ) !Render { - return document.renderWithHighlightsAt( - gpa, - page, - default_render_request, - highlights, - ); - } - - pub fn renderWithHighlightsAt( + /// Render one zero-based page whole into allocator-owned packed RGBA, + /// baking `highlights` in: the tests' and benches' render. The view + /// renders bands into buffers it owns (`renderIntoAtWithPaper`). + pub fn render( document: *Document, gpa: std.mem.Allocator, page: usize, request: RenderRequest, highlights: []const Highlight, ) !Render { - return document.renderInternal(gpa, page, request, highlights); + const raster = try document.measureRenderAt(page, request); + if (raster.len > max_owned_raster_bytes) return error.PixmapTooLarge; + const rgba = try gpa.alloc(u8, raster.len); + errdefer gpa.free(rgba); + try document.renderIntoAtWithPaper(page, request, raster, raster.wholePage(), highlights, rgba, false); + return .{ .rgba = rgba, .width = raster.width, .height = raster.height }; } /// Exactly the raster this request will produce, without producing it. @@ -913,20 +877,8 @@ pub const Document = struct { /// Rasterize `band` of a page into `rgba`, which MUST be exactly /// `band.len` bytes for a `band` taken from the same `measureRenderAt` /// shape and request. On any failure the buffer's contents are unspecified - /// and it still belongs to the caller. - pub fn renderIntoAt( - document: *Document, - page: usize, - request: RenderRequest, - raster: Raster, - band: Raster.Band, - highlights: []const Highlight, - rgba: []u8, - ) !void { - return document.renderIntoAtWithPaper(page, request, raster, band, highlights, rgba, false); - } - - /// Preserve the PDF's content coverage when the host paints paper separately. + /// and it still belongs to the caller. `transparent_paper` preserves the + /// PDF's content coverage when the host paints paper separately. pub fn renderIntoAtWithPaper( document: *Document, page: usize, @@ -1077,28 +1029,6 @@ pub const Document = struct { return @intCast(@min(value, @as(u32, @intCast(std.math.maxInt(c_int))))); } - fn renderInternal( - document: *Document, - gpa: std.mem.Allocator, - page: usize, - request: RenderRequest, - highlights: ?[]const Highlight, - ) !Render { - const raster = try document.measureRenderAt(page, request); - if (raster.len > max_owned_raster_bytes) return error.PixmapTooLarge; - const rgba = try gpa.alloc(u8, raster.len); - errdefer gpa.free(rgba); - try document.renderIntoAt( - page, - request, - raster, - raster.wholePage(), - highlights orelse &.{}, - rgba, - ); - return .{ .rgba = rgba, .width = raster.width, .height = raster.height }; - } - /// 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 { @@ -1257,7 +1187,7 @@ fn validQuad(quad: Quad) bool { } /// The exact shape of the raster a render request produces. `len` is the -/// packed RGBA byte count a FULL-page `renderIntoAt` demands, so a caller can +/// packed RGBA byte count a FULL-page `renderIntoAtWithPaper` demands, so a caller can /// match a retired buffer against it before deciding to allocate. pub const Raster = struct { width: usize, @@ -1315,29 +1245,13 @@ fn expectOpaque(rgba: []const u8) !void { try std.testing.expectEqual(@as(u8, 0xff), rgba[alpha]); } -const generated_pdf_fragment_max = 256; - -const PdfBytes = struct { - storage: ?[]u8 = null, - len: usize = 0, - - fn appendSlice(bytes: *PdfBytes, source: []const u8) !void { - const end = std.math.add(usize, bytes.len, source.len) catch - return error.GeneratedPdfTooLarge; - if (bytes.storage) |storage| { - if (end > storage.len) return error.GeneratedPdfTooLarge; - @memcpy(storage[bytes.len..end], source); - } - bytes.len = end; - } - - fn print(bytes: *PdfBytes, comptime format: []const u8, args: anytype) !void { - var fragment: [generated_pdf_fragment_max]u8 = undefined; - const rendered = std.fmt.bufPrint(&fragment, format, args) catch - return error.GeneratedPdfFragmentTooLong; - try bytes.appendSlice(rendered); - } -}; +/// TEST-ONLY: a PDF file read whole and opened from memory, as the view +/// opens one. +fn openTestFile(path: []const u8) !Document { + const bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, path, std.testing.allocator, .unlimited); + defer std.testing.allocator.free(bytes); + return Document.openBytes(bytes); +} fn allocateGeneratedPdf( gpa: std.mem.Allocator, @@ -1346,25 +1260,19 @@ fn allocateGeneratedPdf( comptime emit: anytype, ) ![]u8 { @memset(offsets, 0); - var counter: PdfBytes = .{}; - try emit(&counter, offsets, context); - - const result = try gpa.alloc(u8, counter.len); - errdefer gpa.free(result); - @memset(offsets, 0); - var bytes: PdfBytes = .{ .storage = result }; - try emit(&bytes, offsets, context); - std.debug.assert(bytes.len == result.len); - return result; + var bytes: std.Io.Writer.Allocating = .init(gpa); + defer bytes.deinit(); + try emit(&bytes.writer, offsets, context); + return bytes.toOwnedSlice(); } -fn beginPdfObject(bytes: *PdfBytes, offsets: []usize, number: usize) !void { - offsets[number] = bytes.len; +fn beginPdfObject(bytes: *std.Io.Writer, offsets: []usize, number: usize) !void { + offsets[number] = bytes.end; try bytes.print("{d} 0 obj\n", .{number}); } -fn finishGeneratedPdf(bytes: *PdfBytes, offsets: []const usize) !void { - const xref = bytes.len; +fn finishGeneratedPdf(bytes: *std.Io.Writer, offsets: []const usize) !void { + const xref = bytes.end; try bytes.print("xref\n0 {d}\n0000000000 65535 f \n", .{offsets.len}); for (offsets[1..]) |offset| try bytes.print("{d:0>10} 00000 n \n", .{offset}); @@ -1374,14 +1282,14 @@ fn finishGeneratedPdf(bytes: *PdfBytes, offsets: []const usize) !void { ); } -fn emitOffsetRotatedPdf(bytes: *PdfBytes, offsets: []usize, stream: []const u8) !void { - try bytes.appendSlice("%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"); +fn emitOffsetRotatedPdf(bytes: *std.Io.Writer, offsets: []usize, stream: []const u8) !void { + try bytes.writeAll("%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 3); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [100 200 300 400] " ++ + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [100 200 300 400] " ++ "/CropBox [120 220 280 370] /Rotate 90 /Resources << >> " ++ "/Contents 4 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 4); @@ -1395,33 +1303,33 @@ fn makeOffsetRotatedPdf(gpa: std.mem.Allocator) ![]u8 { return allocateGeneratedPdf(gpa, &offsets, stream, emitOffsetRotatedPdf); } -fn emitOutlinePdf(bytes: *PdfBytes, offsets: []usize, _: void) !void { - try bytes.appendSlice("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); +fn emitOutlinePdf(bytes: *std.Io.Writer, offsets: []usize, _: void) !void { + try bytes.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R /Outlines 7 0 R /PageMode /UseOutlines >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R /Outlines 7 0 R /PageMode /UseOutlines >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 3 /Kids [3 0 R 4 0 R 5 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 3 /Kids [3 0 R 4 0 R 5 0 R] >>\nendobj\n"); for (3..6) |page| { try beginPdfObject(bytes, offsets, page); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>\nendobj\n"); } try beginPdfObject(bytes, offsets, 6); - try bytes.appendSlice("<< >>\nendobj\n"); + try bytes.writeAll("<< >>\nendobj\n"); try beginPdfObject(bytes, offsets, 7); - try bytes.appendSlice("<< /Type /Outlines /First 8 0 R /Last 11 0 R /Count 4 >>\nendobj\n"); + try bytes.writeAll("<< /Type /Outlines /First 8 0 R /Last 11 0 R /Count 4 >>\nendobj\n"); // Destinationless branch with a deliberately missing /Title. try beginPdfObject(bytes, offsets, 8); - try bytes.appendSlice("<< /Parent 7 0 R /First 9 0 R /Last 9 0 R /Next 10 0 R /Count 1 >>\nendobj\n"); + try bytes.writeAll("<< /Parent 7 0 R /First 9 0 R /Last 9 0 R /Next 10 0 R /Count 1 >>\nendobj\n"); // UTF-16BE "Café 子", with bold/italic + color metadata. try beginPdfObject(bytes, offsets, 9); - try bytes.appendSlice("<< /Title <FEFF00430061006600E900205B50> /Parent 8 0 R " ++ + try bytes.writeAll("<< /Title <FEFF00430061006600E900205B50> /Parent 8 0 R " ++ "/Dest [4 0 R /XYZ 12 34 null] /F 3 /C [0.2 0.4 0.6] >>\nendobj\n"); // A present empty title and a destination with only one usable axis. try beginPdfObject(bytes, offsets, 10); - try bytes.appendSlice("<< /Title () /Parent 7 0 R /Prev 8 0 R /Next 11 0 R " ++ + try bytes.writeAll("<< /Title () /Parent 7 0 R /Prev 8 0 R /Next 11 0 R " ++ "/Dest [5 0 R /FitH 70] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 11); - try bytes.appendSlice("<< /Title (External) /Parent 7 0 R /Prev 10 0 R " ++ + try bytes.writeAll("<< /Title (External) /Parent 7 0 R /Prev 10 0 R " ++ "/A << /S /URI /URI (https://example.com/manual) >> >>\nendobj\n"); try finishGeneratedPdf(bytes, offsets); } @@ -1443,31 +1351,31 @@ pub fn makeNoOutlineTestPdf(gpa: std.mem.Allocator) ![]u8 { return makeOffsetRotatedPdf(gpa); } -fn emitLinksPdf(bytes: *PdfBytes, offsets: []usize, _: void) !void { +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" ++ "BT /F1 6 Tf 20 60 Td (label) Tj ET\n"; - try bytes.appendSlice("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); + try bytes.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 2 /Kids [3 0 R 4 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 2 /Kids [3 0 R 4 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 3); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << /Font << /F1 5 0 R >> >> /Contents 6 0 R /Annots [7 0 R 8 0 R 9 0 R 10 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << /Font << /F1 5 0 R >> >> /Contents 6 0 R /Annots [7 0 R 8 0 R 9 0 R 10 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 4); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>\nendobj\n"); try beginPdfObject(bytes, offsets, 5); - try bytes.appendSlice("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"); + try bytes.writeAll("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"); try beginPdfObject(bytes, offsets, 6); try bytes.print("<< /Length {d} >>\nstream\n{s}endstream\nendobj\n", .{ stream.len, stream }); try beginPdfObject(bytes, offsets, 7); - try bytes.appendSlice("<< /Type /Annot /Subtype /Link /Rect [20 230 120 260] /Border [0 0 0] /A << /S /URI /URI (https://example.com/embedded) >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Annot /Subtype /Link /Rect [20 230 120 260] /Border [0 0 0] /A << /S /URI /URI (https://example.com/embedded) >> >>\nendobj\n"); try beginPdfObject(bytes, offsets, 8); - try bytes.appendSlice("<< /Type /Annot /Subtype /Link /Rect [20 170 120 200] /Border [0 0 0] /Dest [4 0 R /XYZ 12 34 null] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Annot /Subtype /Link /Rect [20 170 120 200] /Border [0 0 0] /Dest [4 0 R /XYZ 12 34 null] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 9); - try bytes.appendSlice("<< /Type /Annot /Subtype /Link /Rect [20 110 190 140] /Border [0 0 0] /A << /S /URI /URI (https://example.com/same) >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Annot /Subtype /Link /Rect [20 110 190 140] /Border [0 0 0] /A << /S /URI /URI (https://example.com/same) >> >>\nendobj\n"); try beginPdfObject(bytes, offsets, 10); - try bytes.appendSlice("<< /Type /Annot /Subtype /Link /Rect [20 50 120 80] /Border [0 0 0] /A << /S /URI /URI (https://example.com/only) >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Annot /Subtype /Link /Rect [20 50 120 80] /Border [0 0 0] /A << /S /URI /URI (https://example.com/only) >> >>\nendobj\n"); try finishGeneratedPdf(bytes, offsets); } @@ -1480,7 +1388,7 @@ pub fn makeLinksTestPdf(gpa: std.mem.Allocator) ![]u8 { /// The tall test page: 600 x 9000 pt, fifteen A4 pages' height on one. pub const tall_test_page: PageSize = .{ .width = 600, .height = 9000 }; -fn emitTallPdf(bytes: *PdfBytes, offsets: []usize, _: void) !void { +fn emitTallPdf(bytes: *std.Io.Writer, offsets: []usize, _: void) !void { // A labelled row every 100 pt, each with a slanting rule, so a band // edge or a seam between chunks cuts through text and through lines. var storage: [16 * 1024]u8 = undefined; @@ -1495,19 +1403,19 @@ fn emitTallPdf(bytes: *PdfBytes, offsets: []usize, _: void) !void { } stream.writeAll("BT /F1 28 Tf 40 40 Td (bottommost) Tj ET\n") catch return error.GeneratedPdfTooLarge; const content = stream.buffered(); - try bytes.appendSlice("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); + try bytes.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 3); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 9000] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 9000] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 4); try bytes.print("<< /Length {d} >>\nstream\n", .{content.len}); - try bytes.appendSlice(content); - try bytes.appendSlice("endstream\nendobj\n"); + try bytes.writeAll(content); + try bytes.writeAll("endstream\nendobj\n"); try beginPdfObject(bytes, offsets, 5); - try bytes.appendSlice("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"); + try bytes.writeAll("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"); try finishGeneratedPdf(bytes, offsets); } @@ -1557,11 +1465,11 @@ test "a tall page rasterizes at the size it is shown, however many max_dimension const poster: RenderRequest = .{ .dpi = 72, .minimum_width = 4096, .max_dimension = 4096, .display_width = 4096 }; const huge = try document.measureRenderAt(0, poster); try std.testing.expect(huge.len > max_owned_raster_bytes); - try std.testing.expectError(error.PixmapTooLarge, document.renderAt(std.testing.allocator, 0, poster)); + try std.testing.expectError(error.PixmapTooLarge, document.render(std.testing.allocator, 0, poster, &.{})); const band = huge.band(huge.height - 100, 100); const rows = try std.testing.allocator.alloc(u8, band.len); defer std.testing.allocator.free(rows); - try document.renderIntoAt(0, poster, huge, band, &.{}, rows); + try document.renderIntoAtWithPaper(0, poster, huge, band, &.{}, rows, false); try expectOpaque(rows); } @@ -1604,14 +1512,14 @@ test "PDF links own URIs across page eviction and preserve internal coordinates" try std.testing.expect(try document.linkAt(gpa, 0, .{ .x = 0.99, .y = 0.99 }) == null); } -fn emitRotatedLinkPdf(bytes: *PdfBytes, offsets: []usize, action: []const u8) !void { - try bytes.appendSlice("%PDF-1.7\n"); +fn emitRotatedLinkPdf(bytes: *std.Io.Writer, offsets: []usize, action: []const u8) !void { + try bytes.writeAll("%PDF-1.7\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 3); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [100 200 300 400] /CropBox [120 220 280 370] /Rotate 90 /Resources << >> /Annots [4 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [100 200 300 400] /CropBox [120 220 280 370] /Rotate 90 /Resources << >> /Annots [4 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 4); try bytes.print("<< /Type /Annot /Subtype /Link /Rect [120 220 160 270] /Border [0 0 0] {s} >>\nendobj\n", .{action}); try finishGeneratedPdf(bytes, offsets); @@ -1648,15 +1556,15 @@ test "PDF links normalize cropped rotated annotations and ignore unsupported act } } -fn emitTooDeepOutlinePdf(bytes: *PdfBytes, offsets: []usize, levels: usize) !void { +fn emitTooDeepOutlinePdf(bytes: *std.Io.Writer, offsets: []usize, levels: usize) !void { const first_outline_item = 5; - try bytes.appendSlice("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); + try bytes.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 3); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> >>\nendobj\n"); try beginPdfObject(bytes, offsets, 4); try bytes.print("<< /Type /Outlines /First 5 0 R /Last 5 0 R /Count {d} >>\nendobj\n", .{levels}); @@ -1674,7 +1582,7 @@ fn emitTooDeepOutlinePdf(bytes: *PdfBytes, offsets: []usize, levels: usize) !voi levels - level - 1, }); } - try bytes.appendSlice(" >>\nendobj\n"); + try bytes.writeAll(" >>\nendobj\n"); } try finishGeneratedPdf(bytes, offsets); } @@ -1687,15 +1595,15 @@ fn makeTooDeepOutlinePdf(gpa: std.mem.Allocator) ![]u8 { return allocateGeneratedPdf(gpa, offsets, levels, emitTooDeepOutlinePdf); } -fn emitWideOutlinePdf(bytes: *PdfBytes, offsets: []usize, items: usize) !void { +fn emitWideOutlinePdf(bytes: *std.Io.Writer, offsets: []usize, items: usize) !void { const first_outline_item = 5; - try bytes.appendSlice("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); + try bytes.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); try beginPdfObject(bytes, offsets, 1); - try bytes.appendSlice("<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>\nendobj\n"); + try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R /Outlines 4 0 R >>\nendobj\n"); try beginPdfObject(bytes, offsets, 2); - try bytes.appendSlice("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); + try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); try beginPdfObject(bytes, offsets, 3); - try bytes.appendSlice("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> >>\nendobj\n"); + try bytes.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> >>\nendobj\n"); try beginPdfObject(bytes, offsets, 4); try bytes.print("<< /Type /Outlines /First 5 0 R /Last {d} 0 R /Count {d} >>\nendobj\n", .{ first_outline_item + items - 1, @@ -1710,7 +1618,7 @@ fn emitWideOutlinePdf(bytes: *PdfBytes, offsets: []usize, items: usize) !void { try bytes.print(" /Prev {d} 0 R", .{number - 1}); if (ordinal + 1 < items) try bytes.print(" /Next {d} 0 R", .{number + 1}); - try bytes.appendSlice(" >>\nendobj\n"); + try bytes.writeAll(" >>\nendobj\n"); } try finishGeneratedPdf(bytes, offsets); } @@ -1724,22 +1632,10 @@ fn makeWideOutlinePdf(gpa: std.mem.Allocator, items: usize) ![]u8 { } test "PDF outline is a stable owned DFS view with native destinations" { - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); const fixture = try makeOutlinePdf(std.testing.allocator); defer std.testing.allocator.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ - .sub_path = "outline.pdf", - .data = fixture, - }); - var path_buffer: [256]u8 = undefined; - const path = try std.fmt.bufPrint( - &path_buffer, - ".zig-cache/tmp/{s}/outline.pdf", - .{tmp.sub_path}, - ); - var document = try Document.open(path); + var document = try Document.openBytes(fixture); defer document.deinit(); try std.testing.expectEqual(@as(usize, 3), document.pages); for (0..2) |fail_index| { @@ -1792,21 +1688,9 @@ test "PDF outline accepts manual-sized trees and rejects item overflow atomicall 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); + var manual_document = try Document.openBytes(manual_fixture); defer manual_document.deinit(); var outline = try manual_document.outline(std.testing.allocator); defer outline.deinit(std.testing.allocator); @@ -1815,21 +1699,9 @@ test "PDF outline accepts manual-sized trees and rejects item overflow atomicall 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); + var overflow_document = try Document.openBytes(overflow_fixture); defer overflow_document.deinit(); var raw: c.pardes_pdf_outline_result = undefined; @memset(std.mem.asBytes(&raw), 0xa5); @@ -1845,42 +1717,18 @@ test "PDF outline accepts manual-sized trees and rejects item overflow atomicall } test "PDF outline absence and hostile depth are atomic and repeatable" { - var plain_tmp = std.testing.tmpDir(.{}); - defer plain_tmp.cleanup(); const plain_fixture = try makeOffsetRotatedPdf(std.testing.allocator); defer std.testing.allocator.free(plain_fixture); - try plain_tmp.dir.writeFile(std.testing.io, .{ - .sub_path = "no-outline.pdf", - .data = plain_fixture, - }); - var plain_path_buffer: [256]u8 = undefined; - const plain_path = try std.fmt.bufPrint( - &plain_path_buffer, - ".zig-cache/tmp/{s}/no-outline.pdf", - .{plain_tmp.sub_path}, - ); - var plain_document = try Document.open(plain_path); + var plain_document = try Document.openBytes(plain_fixture); defer plain_document.deinit(); var absent = try plain_document.outline(std.testing.allocator); defer absent.deinit(std.testing.allocator); try std.testing.expectEqual(@as(usize, 0), absent.entries.len); try std.testing.expectEqual(@as(usize, 0), absent.bytes.len); - var deep_tmp = std.testing.tmpDir(.{}); - defer deep_tmp.cleanup(); const deep_fixture = try makeTooDeepOutlinePdf(std.testing.allocator); defer std.testing.allocator.free(deep_fixture); - try deep_tmp.dir.writeFile(std.testing.io, .{ - .sub_path = "too-deep-outline.pdf", - .data = deep_fixture, - }); - var deep_path_buffer: [256]u8 = undefined; - const deep_path = try std.fmt.bufPrint( - &deep_path_buffer, - ".zig-cache/tmp/{s}/too-deep-outline.pdf", - .{deep_tmp.sub_path}, - ); - var deep_document = try Document.open(deep_path); + var deep_document = try Document.openBytes(deep_fixture); defer deep_document.deinit(); var raw: c.pardes_pdf_outline_result = undefined; @memset(std.mem.asBytes(&raw), 0xa5); @@ -1923,7 +1771,7 @@ test "caller-owned RGBA layout is packed and overflow checked" { } test "RGBA allocation and buffer-validation failures leave the document renderable" { - var document = try Document.open("docs/design.pdf"); + var document = try openTestFile("docs/design.pdf"); defer document.deinit(); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ @@ -1931,7 +1779,7 @@ test "RGBA allocation and buffer-validation failures leave the document renderab }); try std.testing.expectError( error.OutOfMemory, - document.render(failing.allocator(), 0), + document.render(failing.allocator(), 0, .{}, &.{}), ); var raw_layout: c.pardes_pdf_raster_layout = std.mem.zeroes(c.pardes_pdf_raster_layout); @@ -1975,9 +1823,9 @@ test "RGBA allocation and buffer-validation failures leave the document renderab try std.testing.expectEqual(@as(u8, 0xa5), byte); } - const after = try document.render(std.testing.allocator, 0); + const after = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(after.rgba); - const repeated = try document.render(std.testing.allocator, 0); + const repeated = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(repeated.rgba); try std.testing.expectEqualSlices(u8, after.rgba, repeated.rgba); try expectOpaque(after.rgba); @@ -1990,14 +1838,14 @@ test "a band's rows are the full page's rows, and highlights land in them" { // let the pixmap's bbox change antialiasing, hinting or subpixel placement, // a fling would show pixels a reader could tell apart from a rested view, // and this test is what refuses to let that happen quietly. - var document = try Document.open("docs/design.pdf"); + var document = try openTestFile("docs/design.pdf"); defer document.deinit(); const request: RenderRequest = .{ .dpi = 96, .max_dimension = 1200 }; const raster = try document.measureRenderAt(0, request); const whole = try std.testing.allocator.alloc(u8, raster.len); defer std.testing.allocator.free(whole); - try document.renderIntoAt(0, request, raster, raster.wholePage(), &.{}, whole); + try document.renderIntoAtWithPaper(0, request, raster, raster.wholePage(), &.{}, whole, false); // Odd offsets and an odd height on purpose: a band that starts on a row // MuPDF would never choose itself is exactly where an off-by-one in the @@ -2013,7 +1861,7 @@ test "a band's rows are the full page's rows, and highlights land in them" { const band = raster.band(case.y, case.h); const rows = try std.testing.allocator.alloc(u8, band.len); defer std.testing.allocator.free(rows); - try document.renderIntoAt(0, request, raster, band, &.{}, rows); + try document.renderIntoAtWithPaper(0, request, raster, band, &.{}, rows, false); const from = band.y * raster.stride; try std.testing.expectEqualSlices(u8, whole[from..][0..band.len], rows); try expectOpaque(rows); @@ -2033,11 +1881,11 @@ test "a band's rows are the full page's rows, and highlights land in them" { }}; const marked_whole = try std.testing.allocator.alloc(u8, raster.len); defer std.testing.allocator.free(marked_whole); - try document.renderIntoAt(0, request, raster, raster.wholePage(), &highlights, marked_whole); + try document.renderIntoAtWithPaper(0, request, raster, raster.wholePage(), &highlights, marked_whole, false); const marked_band = raster.band(raster.height / 3, raster.height / 3); const marked_rows = try std.testing.allocator.alloc(u8, marked_band.len); defer std.testing.allocator.free(marked_rows); - try document.renderIntoAt(0, request, raster, marked_band, &highlights, marked_rows); + try document.renderIntoAtWithPaper(0, request, raster, marked_band, &highlights, marked_rows, false); const marked_from = marked_band.y * raster.stride; try std.testing.expectEqualSlices( u8, @@ -2060,7 +1908,7 @@ test "a band's rows are the full page's rows, and highlights land in them" { } test "MuPDF search returns normalized oriented quads and word selection text" { - var document = try Document.open("docs/design.pdf"); + var document = try openTestFile("docs/design.pdf"); defer document.deinit(); const projected = try document.pageText(std.testing.allocator, 0); @@ -2095,10 +1943,10 @@ test "MuPDF search returns normalized oriented quads and word selection text" { } test "highlighted render changes pixels while plain render stays stable" { - var document = try Document.open("docs/design.pdf"); + var document = try openTestFile("docs/design.pdf"); defer document.deinit(); - const plain_before = try document.render(std.testing.allocator, 0); + const plain_before = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(plain_before.rgba); const whole_page = Quad{ .ul = .{ .x = 0, .y = 0 }, @@ -2109,9 +1957,9 @@ test "highlighted render changes pixels while plain render stays stable" { const highlights = [_]Highlight{ Highlight.init(whole_page, .{ 255, 0, 0, 128 }, .custom), }; - const marked = try document.renderWithHighlights(std.testing.allocator, 0, &highlights); + const marked = try document.render(std.testing.allocator, 0, .{}, &highlights); defer std.testing.allocator.free(marked.rgba); - const plain_after = try document.render(std.testing.allocator, 0); + const plain_after = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(plain_after.rgba); try expectOpaque(plain_before.rgba); @@ -2124,24 +1972,12 @@ test "highlighted render changes pixels while plain render stays stable" { } test "offset crop and rotation keep normalized highlights pixel-aligned" { - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); const fixture = try makeOffsetRotatedPdf(std.testing.allocator); defer std.testing.allocator.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ - .sub_path = "offset-rotated.pdf", - .data = fixture, - }); - var path_buffer: [256]u8 = undefined; - const path = try std.fmt.bufPrint( - &path_buffer, - ".zig-cache/tmp/{s}/offset-rotated.pdf", - .{tmp.sub_path}, - ); - var document = try Document.open(path); + var document = try Document.openBytes(fixture); defer document.deinit(); - const plain = try document.render(std.testing.allocator, 0); + const plain = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(plain.rgba); // CropBox is 160x150 points and /Rotate 90 swaps its displayed axes. try std.testing.expectEqual(@as(usize, 300), plain.width); @@ -2162,31 +1998,31 @@ test "offset crop and rotation keep normalized highlights pixel-aligned" { 0.001, ); - const kitty = try document.renderAt(std.testing.allocator, 0, .{ + const kitty = try document.render(std.testing.allocator, 0, .{ .dpi = 96, .max_dimension = 1200, - }); + }, &.{}); defer std.testing.allocator.free(kitty.rgba); try std.testing.expect(kitty.width < plain.width); try std.testing.expect(kitty.height < plain.height); - const sdl = try document.renderAt(std.testing.allocator, 0, .{ + const sdl = try document.render(std.testing.allocator, 0, .{ .dpi = 192, .minimum_width = 640, .minimum_height = 480, .max_dimension = 4096, - }); + }, &.{}); defer std.testing.allocator.free(sdl.rgba); try std.testing.expect(sdl.width >= 640); try std.testing.expect(sdl.height >= 480); try std.testing.expect(sdl.width * sdl.height >= kitty.width * kitty.height * 4); - const capped = try document.renderAt(std.testing.allocator, 0, .{ + const capped = try document.render(std.testing.allocator, 0, .{ .dpi = 192, .minimum_width = 4096, .minimum_height = 4096, .max_dimension = 512, - }); + }, &.{}); defer std.testing.allocator.free(capped.rgba); try std.testing.expectEqual(@as(usize, 512), @max(capped.width, capped.height)); @@ -2199,7 +2035,7 @@ test "offset crop and rotation keep normalized highlights pixel-aligned" { const highlights = [_]Highlight{ Highlight.init(left_half, .{ 0, 0, 255, 255 }, .custom), }; - const marked = try document.renderWithHighlights(std.testing.allocator, 0, &highlights); + const marked = try document.render(std.testing.allocator, 0, .{}, &highlights); defer std.testing.allocator.free(marked.rgba); try std.testing.expectEqual(plain.width, marked.width); try std.testing.expectEqual(plain.height, marked.height); @@ -2214,7 +2050,7 @@ test "offset crop and rotation keep normalized highlights pixel-aligned" { } test "transparent PDF paper preserves ink and band coverage" { - var document = try Document.open("docs/9p.pdf"); + var document = try openTestFile("docs/9p.pdf"); defer document.deinit(); const request: RenderRequest = .{ .dpi = 96, .max_dimension = 1200 }; const raster = try document.measureRenderAt(0, request); @@ -2222,7 +2058,7 @@ test "transparent PDF paper preserves ink and band coverage" { defer std.testing.allocator.free(flattened); const content = try std.testing.allocator.alloc(u8, raster.len); defer std.testing.allocator.free(content); - try document.renderIntoAt(0, request, raster, raster.wholePage(), &.{}, flattened); + try document.renderIntoAtWithPaper(0, request, raster, raster.wholePage(), &.{}, flattened, false); try document.renderIntoAtWithPaper(0, request, raster, raster.wholePage(), &.{}, content, true); var paper: usize = 0; var ink: usize = 0; |
