From 96cabca7ebe287692ebdff0dba6bb2897eba9d3e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:57:01 -0300 Subject: pdf: the test PDFs are files under test/pdf-fixtures, byte for byte what the generators wrote, where pdf.zig wrote each object by object (139 lines fewer there): testFixture reads one, the two outlines sized by the bridge's limits stay generated, and the sections bench embeds its outline Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pdf.zig | 195 +++++++----------------------------------------------------- 1 file changed, 23 insertions(+), 172 deletions(-) (limited to 'src/pdf.zig') diff --git a/src/pdf.zig b/src/pdf.zig index a68bf48a..7cfb86d5 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -1252,6 +1252,18 @@ fn openTestFile(path: []const u8) !Document { return Document.openBytes(bytes); } +/// TEST-ONLY (here and in the core's tests, which run in the repository's +/// root): one of test/pdf-fixtures' PDFs, whole. `outline.pdf`, an outline +/// of every kind (a branch with neither title nor destination over a UTF-16 +/// child, an empty title, an external link); `offset-rotated.pdf`, one page +/// with an offset CropBox and /Rotate 90 and no outline; `links.pdf`, +/// external, internal, same-URI and link-only annotations; `tall.pdf`, one +/// `tall_test_page` labelled every 100 pt; `rotated-link*.pdf`, a link on +/// the rotated page by URI, empty URI, JavaScript, a missing destination. +pub fn testFixture(gpa: std.mem.Allocator, comptime name: []const u8) ![]u8 { + return std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/" ++ name, gpa, .unlimited); +} + fn allocateGeneratedPdf( gpa: std.mem.Allocator, offsets: []usize, @@ -1281,144 +1293,11 @@ fn finishGeneratedPdf(bytes: *std.Io.Writer, offsets: []const usize) !void { ); } -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.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 2); - try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 3); - 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); - try bytes.print("<< /Length {d} >>\nstream\n{s}endstream\nendobj\n", .{ stream.len, stream }); - try finishGeneratedPdf(bytes, offsets); -} - -/// 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); -} - -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.writeAll("<< /Type /Catalog /Pages 2 0 R /Outlines 7 0 R /PageMode /UseOutlines >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 2); - 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.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>\nendobj\n"); - } - try beginPdfObject(bytes, offsets, 6); - try bytes.writeAll("<< >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 7); - 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.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.writeAll("<< /Title /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.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.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); -} - -/// 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); -} - -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.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); - try beginPdfObject(bytes, offsets, 1); - try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 2); - try bytes.writeAll("<< /Type /Pages /Count 2 /Kids [3 0 R 4 0 R] >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 3); - 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.writeAll("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 5); - 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.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.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.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.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); -} - -/// TEST-ONLY: external, blank internal, identical-label URI, and link-only URI. -pub fn makeLinksTestPdf(gpa: std.mem.Allocator) ![]u8 { - var offsets: [11]usize = undefined; - return allocateGeneratedPdf(gpa, &offsets, {}, emitLinksPdf); -} - /// 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: *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; - var stream: std.Io.Writer = .fixed(&storage); - var row: usize = 0; - while (row * 100 + 160 < 9000) : (row += 1) { - const y = 9000 - 60 - row * 100; - stream.print( - "BT /F1 28 Tf 40 {d} Td (Row {d}) Tj ET\n0.2 0.3 0.9 RG 3 w 20 {d} m 580 {d} l S\n", - .{ y, row, y - 20, y - 80 }, - ) catch return error.GeneratedPdfTooLarge; - } - stream.writeAll("BT /F1 28 Tf 40 40 Td (bottommost) Tj ET\n") catch return error.GeneratedPdfTooLarge; - const content = stream.buffered(); - try bytes.writeAll("%PDF-1.7\n%\xE2\xE3\xCF\xD3\n"); - try beginPdfObject(bytes, offsets, 1); - try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 2); - try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 3); - 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.writeAll(content); - try bytes.writeAll("endstream\nendobj\n"); - try beginPdfObject(bytes, offsets, 5); - try bytes.writeAll("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"); - try finishGeneratedPdf(bytes, offsets); -} - -/// TEST-ONLY: one page fifteen times as tall as it is wide (`tall_test_page`), -/// "Row N" every 100 pt from the top and "bottommost" at its foot. -pub fn makeTallTestPdf(gpa: std.mem.Allocator) ![]u8 { - var offsets: [6]usize = undefined; - return allocateGeneratedPdf(gpa, &offsets, {}, emitTallPdf); -} - test "a tall page rasterizes at the size it is shown, however many max_dimensions tall" { - const bytes = try makeTallTestPdf(std.testing.allocator); - defer std.testing.allocator.free(bytes); - var document = try Document.openBytes(bytes); + var document = try openTestFile("test/pdf-fixtures/tall.pdf"); defer document.deinit(); const size = try document.pageSize(0); try std.testing.expectEqual(tall_test_page.width, size.width); @@ -1466,9 +1345,7 @@ test "PDF links own URIs across page eviction and preserve internal coordinates" const gpa = std.testing.allocator; start(gpa); defer stop(); - const fixture = try makeLinksTestPdf(gpa); - defer gpa.free(fixture); - var document = try Document.openBytes(fixture); + var document = try openTestFile("test/pdf-fixtures/links.pdf"); defer document.deinit(); var external = (try document.linkAt(gpa, 0, .{ .x = 0.3, .y = 0.18 })) orelse return error.NoLink; defer external.deinit(gpa); @@ -1501,34 +1378,16 @@ 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: *std.Io.Writer, offsets: []usize, action: []const u8) !void { - try bytes.writeAll("%PDF-1.7\n"); - try beginPdfObject(bytes, offsets, 1); - try bytes.writeAll("<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 2); - try bytes.writeAll("<< /Type /Pages /Count 1 /Kids [3 0 R] >>\nendobj\n"); - try beginPdfObject(bytes, offsets, 3); - 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); -} - test "PDF links normalize cropped rotated annotations and ignore unsupported actions" { const gpa = std.testing.allocator; start(gpa); defer stop(); - const actions = [_][]const u8{ - "/A << /S /URI /URI (https://example.com/rotated) >>", - "/A << /S /URI /URI () >>", - "/A << /S /JavaScript /JS (ignored) >>", - "/Dest /missing_destination", - }; - for (actions, 0..) |action, index| { - var offsets: [5]usize = undefined; - const fixture = try allocateGeneratedPdf(gpa, &offsets, action, emitRotatedLinkPdf); - defer gpa.free(fixture); - var document = try Document.openBytes(fixture); + // The page's link's action: a URI, an empty URI, JavaScript, a + // destination that is not there. + const fixtures = [_][]const u8{ "rotated-link.pdf", "rotated-link-empty-uri.pdf", "rotated-link-javascript.pdf", "rotated-link-missing-dest.pdf" }; + for (fixtures, 0..) |name, index| { + var path: [64]u8 = undefined; + var document = try openTestFile(try std.fmt.bufPrint(&path, "test/pdf-fixtures/{s}", .{name})); defer document.deinit(); var found = try document.linkAt(gpa, 0, .{ .x = 0.1, .y = 0.1 }); defer if (found) |*item| item.deinit(gpa); @@ -1621,10 +1480,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 makeOutlineTestPdf(std.testing.allocator); - defer std.testing.allocator.free(fixture); - - var document = try Document.openBytes(fixture); + var document = try openTestFile("test/pdf-fixtures/outline.pdf"); defer document.deinit(); try std.testing.expectEqual(@as(usize, 3), document.pages); for (0..2) |fail_index| { @@ -1702,9 +1558,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 makeNoOutlineTestPdf(std.testing.allocator); - defer std.testing.allocator.free(plain_fixture); - var plain_document = try Document.openBytes(plain_fixture); + var plain_document = try openTestFile("test/pdf-fixtures/offset-rotated.pdf"); defer plain_document.deinit(); var absent = try plain_document.outline(std.testing.allocator); defer absent.deinit(std.testing.allocator); @@ -1943,10 +1797,7 @@ test "highlighted render changes pixels while plain render stays stable" { } test "offset crop and rotation keep normalized highlights pixel-aligned" { - const fixture = try makeNoOutlineTestPdf(std.testing.allocator); - defer std.testing.allocator.free(fixture); - - var document = try Document.openBytes(fixture); + var document = try openTestFile("test/pdf-fixtures/offset-rotated.pdf"); defer document.deinit(); const plain = try document.render(std.testing.allocator, 0, .{}, &.{}); defer std.testing.allocator.free(plain.rgba); -- cgit v1.3