diff options
| -rw-r--r-- | src/linux/v9fs.zig | 36 | ||||
| -rw-r--r-- | src/lsp/lsp.zig | 34 | ||||
| -rw-r--r-- | src/lsp/lsp_client.zig | 133 | ||||
| -rw-r--r-- | src/pdf.zig | 392 | ||||
| -rw-r--r-- | src/pdf_bridge.c | 28 | ||||
| -rw-r--r-- | src/pdf_bridge.h | 1 | ||||
| -rw-r--r-- | test/panes.zig | 4 | ||||
| -rw-r--r-- | test/pdf.zig | 8 | ||||
| -rw-r--r-- | test/pdf_bench.zig | 26 | ||||
| -rw-r--r-- | test/pdf_sections_bench.zig | 8 |
10 files changed, 231 insertions, 439 deletions
diff --git a/src/linux/v9fs.zig b/src/linux/v9fs.zig index 0d1e2e33..47be298b 100644 --- a/src/linux/v9fs.zig +++ b/src/linux/v9fs.zig @@ -5,23 +5,13 @@ const builtin = @import("builtin"); extern "c" fn unshare(flags: c_int) c_int; extern "c" fn mount(source: ?[*:0]const u8, target: [*:0]const u8, filesystemtype: ?[*:0]const u8, flags: c_ulong, data: ?*const anyopaque) c_int; -extern "c" fn getuid() c_uint; -extern "c" fn geteuid() c_uint; extern "c" fn setgroups(size: usize, list: ?[*]const c_uint) c_int; extern "c" fn initgroups(user: [*:0]const u8, group: c_uint) c_int; -extern "c" fn setresgid(real: c_uint, effective: c_uint, saved: c_uint) c_int; -extern "c" fn setresuid(real: c_uint, effective: c_uint, saved: c_uint) c_int; extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn execvp(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; -extern "c" fn readlink(path: [*:0]const u8, buf: [*]u8, size: usize) isize; extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; -extern "c" fn rmdir(path: [*:0]const u8) c_int; extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; extern "c" fn unsetenv(name: [*:0]const u8) c_int; -extern "c" fn fork() c_int; -extern "c" fn waitpid(pid: c_int, status: *c_int, flags: c_int) c_int; -extern "c" fn kill(pid: c_int, sig: c_int) c_int; -extern "c" fn _exit(status: c_int) noreturn; pub const executable = "pardes-v9fs"; @@ -51,7 +41,7 @@ fn quote(writer: *std.Io.Writer, value: []const u8) !void { } fn selfPath(buf: []u8) ![:0]u8 { - const n = readlink("/proc/self/exe", buf.ptr, buf.len - 1); + const n = std.c.readlink("/proc/self/exe", buf.ptr, buf.len - 1); if (n < 0 or n >= buf.len - 1) return error.ExecutablePathUnavailable; const len: usize = @intCast(n); buf[len] = 0; @@ -85,22 +75,22 @@ fn usage() void { , .{}); } -var sudo_pid: std.atomic.Value(c_int) = .init(-1); +var sudo_pid: std.atomic.Value(std.c.pid_t) = .init(-1); fn forwardSignal(sig: std.posix.SIG) callconv(.c) void { const pid = sudo_pid.load(.monotonic); - if (pid > 0) _ = kill(pid, @intCast(@backingInt(sig))); + if (pid > 0) _ = std.c.kill(pid, sig); } fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 { if (args.len < 5 or !std.mem.eql(u8, args[3], "--")) return error.InvalidArguments; - if (geteuid() == 0 or getuid() != geteuid()) return error.UnprivilegedLaunchRequired; + if (std.c.geteuid() == 0 or std.c.getuid() != std.c.geteuid()) return error.UnprivilegedLaunchRequired; if (!std.fs.path.isAbsolute(args[2]) or !std.fs.path.isAbsolute(args[4])) return error.AbsolutePathRequired; var own: [4096]u8 = undefined; const helper = try selfPath(&own); var target = "/tmp/pardes-v9fs-XXXXXX".*; if (mkdtemp(&target) == null) return error.MountDirectoryFailed; - defer _ = rmdir(&target); + defer _ = std.c.rmdir(&target); try check(setenv("PARDES_MOUNT", &target, 1), "export mount path"); inline for (.{ "PATH", "HOME", "USER", "LOGNAME", "SHELL" }) |name| { const saved = "PARDES_V9FS_" ++ name; @@ -108,14 +98,14 @@ fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 { try check(setenv(saved, value, 1), "preserve shell environment"); } else _ = unsetenv(saved); } - const uid = try std.fmt.allocPrintSentinel(arena, "{d}", .{getuid()}, 0); + const uid = try std.fmt.allocPrintSentinel(arena, "{d}", .{std.c.getuid()}, 0); const gid = try std.fmt.allocPrintSentinel(arena, "{d}", .{std.c.getgid()}, 0); const prefix = [_]?[*:0]const u8{ "sudo", "-E", "--", helper.ptr, args[2].ptr, &target, uid.ptr, gid.ptr, "--" }; const argv = try arena.allocSentinel(?[*:0]const u8, prefix.len + args.len - 4, null); @memcpy(argv[0..prefix.len], &prefix); for (args[4..], prefix.len..) |arg, i| argv[i] = arg.ptr; std.debug.print("Mounting Pardes at {s}\n", .{target}); - const pid = fork(); + const pid = std.c.fork(); if (pid < 0) return error.ForkFailed; if (pid == 0) { // host_io's resetChildSignals, here in its own module: every signal @@ -129,13 +119,13 @@ fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 { const none = std.posix.sigemptyset(); std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, null); _ = execvp("sudo", argv.ptr); - _exit(127); + std.c._exit(127); } sudo_pid.store(pid, .monotonic); const action: std.posix.Sigaction = .{ .handler = .{ .handler = forwardSignal }, .mask = std.posix.sigemptyset(), .flags = 0 }; for ([_]std.posix.SIG{ .HUP, .INT, .TERM }) |sig| std.posix.sigaction(sig, &action, null); var status: c_int = 0; - while (waitpid(pid, &status, 0) < 0) { + while (std.c.waitpid(pid, &status, 0) < 0) { if (std.posix.errno(-1) != .INTR) return error.WaitFailed; } sudo_pid.store(-1, .monotonic); @@ -176,8 +166,8 @@ pub fn main(init: std.process.Init) !void { } const uid = try userId(args[3]); const gid = try userId(args[4]); - if (getuid() != geteuid()) return error.SetuidInstallationUnsupported; - if (geteuid() != 0) { + if (std.c.getuid() != std.c.geteuid()) return error.SetuidInstallationUnsupported; + if (std.c.geteuid() != 0) { std.debug.print("v9fs: native 9P mounts need CAP_SYS_ADMIN in the initial user namespace; use --launch to ask sudo in this terminal.\n", .{}); return error.MountPrivilegeRequired; } @@ -194,8 +184,8 @@ pub fn main(init: std.process.Init) !void { try check(setgroups(0, null), "clear supplementary groups"); const account = std.c.getpwuid(uid) orelse return error.UserAccountNotFound; try check(initgroups(account.name orelse return error.UserAccountNotFound, gid), "restore user groups"); - try check(setresgid(gid, gid, gid), "drop group privileges"); - try check(setresuid(uid, uid, uid), "drop user privileges"); + try check(std.c.setresgid(gid, gid, gid), "drop group privileges"); + try check(std.c.setresuid(uid, uid, uid), "drop user privileges"); // sudo can replace identity variables and PATH even with -E. Restore // those values only after dropping privileges. inline for (.{ "PATH", "HOME", "USER", "LOGNAME", "SHELL" }) |name| { diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig index 1c1b57a2..1263b583 100644 --- a/src/lsp/lsp.zig +++ b/src/lsp/lsp.zig @@ -91,39 +91,31 @@ test "an explain request narrates the query named in its arg, a definition by de /// Every language, Zig's (zls) included, through the protocol client, which /// runs its server as a child process. None on the web or the board. -const backends = if (@import("pardes_config").platform == .web or @import("pardes_config").platform == .esp32p4) - .{} -else - .{@import("lsp_client.zig")}; +const has_client = switch (@import("pardes_config").platform) { + .web, .esp32p4 => false, + else => true, +}; +const client = if (has_client) @import("lsp_client.zig") else void; pub const backend_name = "lsp-client"; -pub const supports: std.EnumSet(Kind) = blk: { - var s: std.EnumSet(Kind) = .empty; - for (0..backends.len) |i| s.setUnion(backends[i].supports); - break :blk s; -}; +pub const supports: std.EnumSet(Kind) = if (has_client) client.supports else .empty; pub fn speaks(path: []const u8) bool { - inline for (backends) |b| if (b.speaks(path)) return true; - return false; + return has_client and client.speaks(path); } pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { if (req.kind == .status) { try out.print("backend: {s}\n", .{backend_name}); - inline for (backends) |b| try b.query(io, gpa, arena, req, out); + if (has_client) try client.query(io, gpa, arena, req, out); return; } - inline for (backends) |b| { - if (b.speaks(req.path) and b.supports.contains(req.kind)) - return b.query(io, gpa, arena, req, out); - } - if (req.kind == .explain and backends.len > 0) - return backends[0].query(io, gpa, arena, req, out); + if (has_client and (req.kind == .explain or (client.speaks(req.path) and client.supports.contains(req.kind)))) + return client.query(io, gpa, arena, req, out); // Nothing asked: said why, where an empty answer would read as nothing // found. - inline for (backends) |b| if (b.speaks(req.path)) + if (speaks(req.path)) return none(out, "the language server for this file does not answer it", .{}); try none(out, "no language server for this file", .{}); } @@ -136,7 +128,7 @@ pub fn none(out: *std.Io.Writer, comptime fmt: []const u8, args: anytype) std.Io // Called on server reader threads; the sink must copy text before returning. pub fn setStatusSink(ctx: ?*anyopaque, cb: ?*const fn (ctx: ?*anyopaque, text: []const u8) void) void { - inline for (backends) |b| b.setStatusSink(ctx, cb); + if (has_client) client.setStatusSink(ctx, cb); } pub fn rel(base: []const u8, path: []const u8) []const u8 { @@ -218,7 +210,7 @@ test "LSP encoders report every insufficient output capacity" { } test "a question no backend answers for a file says why rather than find nothing" { - if (backends.len == 0) return; + if (!has_client) return; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); var buf: [256]u8 = undefined; diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig index e356c979..5baf7129 100644 --- a/src/lsp/lsp_client.zig +++ b/src/lsp/lsp_client.zig @@ -52,26 +52,15 @@ const libc = std.c; const lsp = @import("lsp.zig"); extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; -extern "c" fn chdir(path: [*:0]const u8) c_int; -extern "c" fn _exit(status: c_int) noreturn; -extern "c" fn setsid() libc.pid_t; -extern "c" fn access(path: [*:0]const u8, mode: c_int) c_int; -/// std.posix.getenv is gone in 0.16 and std.process.Environ wants an Io -extern "c" fn getenv(name: [*:0]const u8) ?[*:0]const u8; extern "c" fn usleep(usec: c_uint) c_int; extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int; // test-only libc (the seam's own tests build a real directory tree) extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; -extern "c" fn mkdir(path: [*:0]const u8, mode: libc.mode_t) c_int; extern "c" fn system(cmd: [*:0]const u8) c_int; -const X_OK: c_int = 1; -const POLLIN: i16 = 0x001; -const POLLOUT: i16 = 0x004; -/// linux MSG_NOSIGNAL; darwin has no such flag and gets SO_NOSIGPIPE on the -/// socket instead (0x1022), set right after socketpair. Both numbers are ABI. -const msg_nosignal: c_int = if (builtin.os.tag.isDarwin()) 0 else 0x4000; -const so_nosigpipe: c_int = 0x1022; +/// darwin has no MSG_NOSIGNAL and gets SO_NOSIGPIPE on the socket instead, +/// set right after socketpair. +const msg_nosignal = if (builtin.os.tag.isDarwin()) 0 else libc.MSG.NOSIGNAL; /// One language server this client knows how to run. Adding a language is /// adding a row; nothing below the table branches on a language. @@ -167,7 +156,7 @@ fn specFor(path: []const u8) ?usize { for (&specs, 0..) |*s, i| { for (s.exts) |e| { if (!std.mem.endsWith(u8, path, e)) continue; - if (getenv(s.env)) |v| if (v[0] == 0) return null; // "" disables + if (libc.getenv(s.env)) |v| if (v[0] == 0) return null; // "" disables return i; } } @@ -602,10 +591,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, try out.writeByte('\n'); }, .document_symbols => { - var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "\"textDocument\":{\"uri\":"); - try jstr(&b, arena, uri.items); - try app(&b, arena, "}"); + const b = try docParams(arena, uri.items); const result = (try call(c, io, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return; try walkSymbols(&cx, c.caps.enc, uri.items, result, 0); }, @@ -636,18 +622,14 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, try renameEdits(&cx, c.caps.enc, uri.items, result, req.source, old, also.items); }, .format => { - var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "\"textDocument\":{\"uri\":"); - try jstr(&b, arena, uri.items); - try app(&b, arena, "},\"options\":{\"tabSize\":4,\"insertSpaces\":true}"); + var b = try docParams(arena, uri.items); + try app(&b, arena, ",\"options\":{\"tabSize\":4,\"insertSpaces\":true}"); const result = (try call(c, io, arena, "textDocument/formatting", b.items, deadline)) orelse return; try formatEdits(&cx, c.caps.enc, result, req.source); }, .code_action => { - var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "\"textDocument\":{\"uri\":"); - try jstr(&b, arena, uri.items); - try b.print(arena, "}},\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch }); + var b = try docParams(arena, uri.items); + try b.print(arena, ",\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch }); const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return; for (items(result)) |ca| { const title = str(get(ca, "title")) orelse continue; @@ -726,10 +708,7 @@ fn walkSymbols(cx: *Cx, enc: Enc, uri: []const u8, node: std.json.Value, depth: /// sent, so `]d` right after an edit sees the new truth, not the old one. fn diagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, deadline: i64) Err!void { if (c.caps.pull) { - var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "\"textDocument\":{\"uri\":"); - try jstr(&b, arena, uri); - try app(&b, arena, "}"); + const b = try docParams(arena, uri); const result = (try call(c, io, arena, "textDocument/diagnostic", b.items, deadline)) orelse return; for (items(get(result, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena); return; @@ -826,6 +805,8 @@ fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, var edits: std.ArrayList(PutEdit) = .empty; var foreign = false; + // (uri, edits) per file, from either shape of a WorkspaceEdit + var files: std.ArrayList(struct { []const u8, ?std.json.Value }) = .empty; if (get(result, "documentChanges")) |dcs| { for (items(dcs)) |dc| { if (get(dc, "kind") != null) { @@ -833,32 +814,25 @@ fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, continue; } const u = str(get(get(dc, "textDocument"), "uri")) orelse continue; - const in_self = std.mem.eql(u8, u, self_uri); - if (!in_self) foreign = true; - for (items(get(dc, "edits"))) |ed| { - if (in_self) { - // one malformed edit poisons the WHOLE mutating response: - // applying the valid remainder would be a partial edit set - const span = byteSpan(src, get(ed, "range"), enc) orelse return; - const text = str(get(ed, "newText")) orelse return; - edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }) catch return error.OutOfMemory; - } else try emitRange(cx, enc, u, get(ed, "range"), flat(cx.arena, str(get(ed, "newText")) orelse "")); - } + files.append(cx.arena, .{ u, get(dc, "edits") }) catch return error.OutOfMemory; } } else if (get(result, "changes")) |ch| if (ch == .object) { var it = ch.object.iterator(); - while (it.next()) |e| { - const in_self = std.mem.eql(u8, e.key_ptr.*, self_uri); - if (!in_self) foreign = true; - for (items(e.value_ptr.*)) |ed| { - if (in_self) { - const span = byteSpan(src, get(ed, "range"), enc) orelse return; - const text = str(get(ed, "newText")) orelse return; - edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }) catch return error.OutOfMemory; - } else try emitRange(cx, enc, e.key_ptr.*, get(ed, "range"), flat(cx.arena, str(get(ed, "newText")) orelse "")); - } - } + while (it.next()) |e| files.append(cx.arena, .{ e.key_ptr.*, e.value_ptr.* }) catch return error.OutOfMemory; }; + for (files.items) |f| { + const in_self = std.mem.eql(u8, f[0], self_uri); + if (!in_self) foreign = true; + for (items(f[1])) |ed| { + if (in_self) { + // one malformed edit poisons the WHOLE mutating response: + // applying the valid remainder would be a partial edit set + const span = byteSpan(src, get(ed, "range"), enc) orelse return; + const text = str(get(ed, "newText")) orelse return; + edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }) catch return error.OutOfMemory; + } else try emitRange(cx, enc, f[0], get(ed, "range"), flat(cx.arena, str(get(ed, "newText")) orelse "")); + } + } // Answered for this file alone while other open files say the old // name: previewed, never applied, each such file named in a row. @@ -1492,7 +1466,7 @@ fn transportPair(sv: *[2]libc.fd_t) bool { // number itself, which the child's 3..1024 sweep closes anyway. setCloexec(sv[1]); const one: c_int = 1; - _ = libc.setsockopt(sv[0], libc.SOL.SOCKET, so_nosigpipe, @ptrCast(&one), @sizeOf(c_int)); + _ = libc.setsockopt(sv[0], libc.SOL.SOCKET, libc.SO.NOSIGPIPE, @ptrCast(&one), @sizeOf(c_int)); } return true; } @@ -1560,6 +1534,8 @@ fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Re return error.NoServer; } if (!exit_hook.swap(true, .acq_rel)) _ = atexit(killAllAtExit); + // ponytail: hand-rolled, not std.process.spawn: 0.17's has no setsid + // (only setpgid), no signal mask/disposition reset and no fd sweep. const pid = libc.fork(); if (pid < 0) { _ = libc.close(sv[0]); @@ -1572,7 +1548,7 @@ fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Re // Between fork and exec, in a process with threads: only async- // signal-safe calls, no allocation, no locks. (Same rule as tty.zig's // forkShell.) - _ = setsid(); // Ctrl-C in pardes's terminal is not the server's business + _ = libc.setsid(); // Ctrl-C in pardes's terminal is not the server's business @import("../host_io.zig").resetChildSignals(); _ = libc.dup2(sv[1], 0); _ = libc.dup2(sv[1], 1); @@ -1580,7 +1556,7 @@ fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Re if (devnull >= 0) _ = libc.dup2(devnull, 2); var fd: c_int = 3; while (fd < 1024) : (fd += 1) _ = libc.close(fd); - _ = chdir(root_z.ptr); + _ = libc.chdir(root_z.ptr); var argv: [8:null]?[*:0]const u8 = @splat(null); argv[0] = exe.ptr; // args are comptime literals; the buffers live until execv @@ -1591,7 +1567,7 @@ fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Re argv[1 + i] = z.ptr; } _ = execv(exe.ptr, &argv); - _exit(127); + libc._exit(127); } _ = libc.close(sv[1]); @@ -1937,7 +1913,7 @@ fn reader(io: std.Io, si: usize, g: u32, sock: c_int) void { const bigger = sa.realloc(buf, buf.len * 2) catch break :outer; buf = bigger; } - var pfd = [_]libc.pollfd{.{ .fd = sock, .events = POLLIN, .revents = 0 }}; + var pfd = [_]libc.pollfd{.{ .fd = sock, .events = libc.POLL.IN, .revents = 0 }}; const pr = libc.poll(&pfd, 1, 1000); if (pr < 0) { if (libc.errno(pr) == .INTR) continue; @@ -2225,7 +2201,7 @@ fn status(io: std.Io, req: lsp.Req, out: *std.Io.Writer) !void { const chier = c.caps.call_hier; const thier = c.caps.type_hier; c.mu.unlock(io); - const env_v = if (getenv(s.env)) |v| std.mem.span(v) else null; + const env_v = if (libc.getenv(s.env)) |v| std.mem.span(v) else null; // Never spawned is not off: the first query of its files starts it. try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (its first query starts it)" else @tagName(state) }); if (state == .ready or state == .starting) { @@ -2270,14 +2246,21 @@ fn app(b: *std.ArrayList(u8), arena: std.mem.Allocator, s: []const u8) Err!void b.appendSlice(arena, s) catch return error.OutOfMemory; } -/// `"textDocument":{"uri":U},"position":{...}` — the params shared by every -/// position request. -fn atPos(arena: std.mem.Allocator, uri: []const u8, pos: Pos) Err!std.ArrayList(u8) { +/// `"textDocument":{"uri":U}` — the params every request about a file +/// starts with. +fn docParams(arena: std.mem.Allocator, uri: []const u8) Err!std.ArrayList(u8) { var b: std.ArrayList(u8) = .empty; try app(&b, arena, "\"textDocument\":{\"uri\":"); try jstr(&b, arena, uri); - b.print(arena, "}},\"position\":{{\"line\":{d},\"character\":{d}}}", .{ pos.line, pos.ch }) catch return error.OutOfMemory; - // the extra `}` above closed textDocument; nothing to fix up + try app(&b, arena, "}"); + return b; +} + +/// `"textDocument":{"uri":U},"position":{...}` — the params shared by every +/// position request. +fn atPos(arena: std.mem.Allocator, uri: []const u8, pos: Pos) Err!std.ArrayList(u8) { + var b = try docParams(arena, uri); + b.print(arena, ",\"position\":{{\"line\":{d},\"character\":{d}}}", .{ pos.line, pos.ch }) catch return error.OutOfMemory; return b; } @@ -2353,7 +2336,7 @@ fn sockWrite(io: ?std.Io, sock: c_int, bytes: []const u8, deadline: i64, started if (deadline - nowMs() <= 0) return error.Timeout; if (io) |cancel_io| cancel_io.checkCancel() catch return error.Canceled; } else if (nowMs() - deadline > wedge_ms) return error.Dead; - var pfd = [_]libc.pollfd{.{ .fd = sock, .events = POLLOUT, .revents = 0 }}; + var pfd = [_]libc.pollfd{.{ .fd = sock, .events = libc.POLL.OUT, .revents = 0 }}; const pr = libc.poll(&pfd, 1, 500); if (pr < 0) { if (libc.errno(pr) == .INTR) continue; @@ -2387,17 +2370,17 @@ fn nowUs() u64 { /// The spec's binary: the env override when set, else `bin`, PATH-searched /// unless it names a path. fn binOf(spec: *const Spec, buf: *[4096:0]u8) ?[:0]const u8 { - const name = if (getenv(spec.env)) |v| std.mem.span(v) else spec.bin; + const name = if (libc.getenv(spec.env)) |v| std.mem.span(v) else spec.bin; if (name.len == 0) return null; if (std.mem.indexOfScalar(u8, name, '/') != null) { const z = std.fmt.bufPrintSentinel(buf, "{s}", .{name}, 0) catch return null; - return if (access(z.ptr, X_OK) == 0) z else null; + return if (libc.access(z.ptr, libc.X_OK) == 0) z else null; } - const path = if (getenv("PATH")) |p| std.mem.span(p) else "/usr/local/bin:/usr/bin:/bin"; + const path = if (libc.getenv("PATH")) |p| std.mem.span(p) else "/usr/local/bin:/usr/bin:/bin"; var it = std.mem.tokenizeScalar(u8, path, ':'); while (it.next()) |dir| { const cand = std.fmt.bufPrintSentinel(buf, "{s}/{s}", .{ dir, name }, 0) catch continue; - if (access(cand.ptr, X_OK) == 0) return cand; + if (libc.access(cand.ptr, libc.X_OK) == 0) return cand; } return null; } @@ -2415,14 +2398,14 @@ fn rootOf(dir: []const u8, markers: []const []const u8) []const u8 { while (true) { for (markers) |marker| { const p = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d, marker }, 0) catch continue; - if (access(p.ptr, 0) == 0) { + if (libc.access(p.ptr, libc.F_OK) == 0) { top_marker = d; break; } } if (git == null) { if (std.fmt.bufPrintSentinel(&buf, "{s}/.git", .{d}, 0)) |p| { - if (access(p.ptr, 0) == 0) git = d; + if (libc.access(p.ptr, libc.F_OK) == 0) git = d; } else |_| {} } d = std.fs.path.dirname(d) orelse break; @@ -2795,7 +2778,7 @@ test "rootOf takes the top-most marker and falls back to git then dir" { var pb: [128:0]u8 = undefined; for ([_][]const u8{ "/ws", "/ws/member", "/ws/member/src" }) |d| { const z = try std.fmt.bufPrintSentinel(&pb, "{s}{s}", .{ base, d }, 0); - try std.testing.expect(mkdir(z.ptr, 0o700) == 0); + try std.testing.expect(libc.mkdir(z.ptr, 0o700) == 0); } for ([_][]const u8{ "/ws/Cargo.toml", "/ws/member/Cargo.toml" }) |f| { const z = try std.fmt.bufPrintSentinel(&pb, "{s}{s}", .{ base, f }, 0); @@ -2816,7 +2799,7 @@ test "specFor routes extensions and honours the disable env" { try std.testing.expect(specFor("/x/main.rs") != null); try std.testing.expect(specFor("/x/README.md") == null); try std.testing.expectEqualStrings("rust-analyzer", specs[specFor("/x/main.rs").?].name); - if (getenv("PARDES_LSP_ZIG") == null) try std.testing.expectEqualStrings("zls", specs[specFor("/x/main.zig").?].name); + if (libc.getenv("PARDES_LSP_ZIG") == null) try std.testing.expectEqualStrings("zls", specs[specFor("/x/main.zig").?].name); } test "the transport this host actually gives us is a pair, and both ends are close-on-exec" { @@ -2894,7 +2877,7 @@ test "Lspinfo before any query says a server is not started yet, not off" { } test "a server that failed a moment ago is said to have failed, and when it is tried again, not to be missing" { - if (getenv("PARDES_LSP_ZIG") != null) return error.SkipZigTest; + if (libc.getenv("PARDES_LSP_ZIG") != null) return error.SkipZigTest; const io = std.testing.io; const si = specFor("/x/a.zig").?; const c = &conns[si]; @@ -2923,7 +2906,7 @@ test "reaping a server takes what it started with it: its process group goes, it const pid = libc.fork(); if (pid < 0) return error.SkipZigTest; if (pid == 0) { - _ = setsid(); + _ = libc.setsid(); const child = libc.fork(); if (child == 0) { while (true) _ = usleep(100_000); 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; diff --git a/src/pdf_bridge.c b/src/pdf_bridge.c index 594db0f9..b1537107 100644 --- a/src/pdf_bridge.c +++ b/src/pdf_bridge.c @@ -321,8 +321,8 @@ pardes_pdf_grown_capacity(size_t current, size_t needed, size_t maximum) return capacity; } -static pardes_pdf_document * -pardes_pdf_open_source(const char *path, const unsigned char *bytes, size_t length, int *page_count) +pardes_pdf_document * +pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count) { pardes_pdf_document *state; fz_context *ctx; @@ -331,7 +331,7 @@ pardes_pdf_open_source(const char *path, const unsigned char *bytes, size_t leng fz_stream *stream = NULL; int pages = 0; - if ((path == NULL && bytes == NULL) || page_count == NULL) + if (bytes == NULL || page_count == NULL) return NULL; state = pardes_pdf_allocate_document(); @@ -356,13 +356,9 @@ pardes_pdf_open_source(const char *path, const unsigned char *bytes, size_t leng fz_try(ctx) { fz_register_document_handlers(ctx); - if (path != NULL) { - doc = fz_open_document(ctx, path); - } else { - buffer = fz_new_buffer_from_copied_data(ctx, bytes, length); - stream = fz_open_buffer(ctx, buffer); - doc = fz_open_document_with_stream(ctx, "application/pdf", stream); - } + buffer = fz_new_buffer_from_copied_data(ctx, bytes, length); + stream = fz_open_buffer(ctx, buffer); + doc = fz_open_document_with_stream(ctx, "application/pdf", stream); pages = fz_count_pages(ctx, doc); if (pages < 1) fz_throw(ctx, FZ_ERROR_FORMAT, "PDF has no pages"); @@ -389,18 +385,6 @@ pardes_pdf_open_source(const char *path, const unsigned char *bytes, size_t leng return state; } -pardes_pdf_document * -pardes_pdf_open(const char *path, int *page_count) -{ - return pardes_pdf_open_source(path, NULL, 0, page_count); -} - -pardes_pdf_document * -pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count) -{ - return pardes_pdf_open_source(NULL, bytes, length, page_count); -} - void pardes_pdf_close(pardes_pdf_document *document) { diff --git a/src/pdf_bridge.h b/src/pdf_bridge.h index 29d87bff..776843de 100644 --- a/src/pdf_bridge.h +++ b/src/pdf_bridge.h @@ -189,7 +189,6 @@ enum { PARDES_PDF_MAX_RASTER_ROWS = 1 << 20 }; -pardes_pdf_document *pardes_pdf_open(const char *path, int *page_count); pardes_pdf_document *pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count); void pardes_pdf_close(pardes_pdf_document *document); diff --git a/test/panes.zig b/test/panes.zig index 1460b355..1b1b3eea 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -3421,7 +3421,9 @@ const PdfTests = struct { test "PDF word probe owns quads and text without a selection object" { if (comptime !enabled) return; - var document = try Document.open("docs/design.pdf"); + const bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "docs/design.pdf", std.testing.allocator, .unlimited); + defer std.testing.allocator.free(bytes); + var document = try Document.openBytes(bytes); defer document.deinit(); var found = try document.search(std.testing.allocator, 0, "Pardes"); defer found.deinit(std.testing.allocator); diff --git a/test/pdf.zig b/test/pdf.zig index b8a768c8..cc4a258d 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -212,11 +212,13 @@ test "PDF mounted bytes own their source after the caller frees it" { gpa.free(source); defer document.deinit(); - var original = try pdf_impl.Document.open("docs/design.pdf"); + const again = try pardes.filesystem.readFile(gpa, "docs/design.pdf"); + defer gpa.free(again); + var original = try pdf_impl.Document.openBytes(again); defer original.deinit(); try std.testing.expectEqual(original.pages, document.pages); try std.testing.expectEqual(try original.pageSize(0), try document.pageSize(0)); - const raster = try document.render(gpa, 0); + const raster = try document.render(gpa, 0, .{}, &.{}); defer gpa.free(raster.rgba); try std.testing.expect(raster.rgba.len > 0); } @@ -1249,7 +1251,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh // tint, and tinted as the pane tints. const plain = try gpa.alloc(u8, shape.len); defer gpa.free(plain); - try pv.document.renderIntoAt(0, request, shape, shape.wholePage(), &.{}, plain); + try pv.document.renderIntoAtWithPaper(0, request, shape, shape.wholePage(), &.{}, plain, false); const whole = try gpa.dupe(u8, plain); defer gpa.free(whole); try pdf_impl.tintRgba(whole, pv.tint, panes.Pdf.tintColors(p)); diff --git a/test/pdf_bench.zig b/test/pdf_bench.zig index 23565106..9054698b 100644 --- a/test/pdf_bench.zig +++ b/test/pdf_bench.zig @@ -9,9 +9,9 @@ //! ReleaseFast. Timed regions include MuPDF rasterization directly into the //! allocator-owned RGBA output; tint scenarios additionally include the exact //! in-place PDF transform used by the core. Checksum/free/reporting stay -//! outside. "cold" means a fresh Document and MuPDF page cache (the OS -//! filesystem cache is deliberately uncontrolled), while warm scenarios -//! retain one Document. +//! outside. "cold" means a fresh Document and MuPDF page cache, opened from +//! the file's bytes (read once, untimed, as the view reads a file whole), +//! while warm scenarios retain one Document. const std = @import("std"); const bench_util = @import("bench_util.zig"); const nowNs = bench_util.nowNs; @@ -28,6 +28,8 @@ const checksum_seed: u64 = 0x7061_7264_6573_5044; const Config = struct { path: []const u8 = "docs/design.pdf", + /// the file at `path`, read whole before anything is timed + bytes: []const u8 = "", page: usize = 0, reps: usize = default_reps, warmup: usize = default_warmup, @@ -140,9 +142,11 @@ const Result = struct { pub fn main(init: std.process.Init) !void { const args = try init.minimal.args.toSlice(init.arena.allocator()); - const config = parseArgs(args); + var config = parseArgs(args); + config.bytes = std.Io.Dir.cwd().readFileAlloc(init.io, config.path, gpa, .unlimited) catch |err| + fatal("cannot read {s}: {t}", .{ config.path, err }); - var probe = pdf.Document.open(config.path) catch |err| + var probe = pdf.Document.openBytes(config.bytes) catch |err| fatal("cannot open {s}: {t}", .{ config.path, err }); const document_pages = probe.pages; probe.deinit(); @@ -239,7 +243,7 @@ fn measure( if (scenario.lifecycle == .cold_document_page) { for (0..rounds) |round| { const timed = try timeCold( - config.path, + config.bytes, config.page, profile.request, scenario.decoration, @@ -253,7 +257,7 @@ fn measure( } } } else { - var document = try pdf.Document.open(config.path); + var document = try pdf.Document.openBytes(config.bytes); defer document.deinit(); for (0..rounds) |round| { const timed = switch (scenario.lifecycle) { @@ -298,14 +302,14 @@ fn measure( } fn timeCold( - path: []const u8, + bytes: []const u8, page: usize, request: pdf.RenderRequest, decoration: Decoration, tint: pdf.TintMode, ) !Timed { const started = nowNs(); - var document = try pdf.Document.open(path); + var document = try pdf.Document.openBytes(bytes); errdefer document.deinit(); const rendered = try renderPage(&document, page, request, decoration, tint); const elapsed = @max(@as(u64, 1), nowNs() -| started); @@ -364,8 +368,8 @@ fn renderPage( tint: pdf.TintMode, ) !pdf.Render { const rendered = switch (decoration) { - .plain => document.renderAt(gpa, page, request), - .highlighted => document.renderWithHighlightsAt(gpa, page, request, &highlights), + .plain => document.render(gpa, page, request, &.{}), + .highlighted => document.render(gpa, page, request, &highlights), }; const fresh = try rendered; errdefer gpa.free(fresh.rgba); diff --git a/test/pdf_sections_bench.zig b/test/pdf_sections_bench.zig index ee26e83d..ce548924 100644 --- a/test/pdf_sections_bench.zig +++ b/test/pdf_sections_bench.zig @@ -72,7 +72,7 @@ pub fn main(init: std.process.Init) !void { try writeFile(path, fixture); defer unlink(path); - var document = try pdf.Document.open(path); + var document = try pdf.Document.openBytes(fixture); defer document.deinit(); var outline = try document.outline(gpa); defer outline.deinit(gpa); @@ -81,7 +81,7 @@ pub fn main(init: std.process.Init) !void { defer gpa.free(synthetic); const results = [_]Result{ - try measureOutline(path, config), + try measureOutline(fixture, config), try measureMaterialize(path, outline.entries, config), try measureCachedControl(path, config), try measureCachedReopen(path, config), @@ -112,14 +112,14 @@ fn parseArgs(args: []const []const u8) Config { return config; } -fn measureOutline(path: []const u8, config: Config) !Result { +fn measureOutline(fixture: []const u8, config: Config) !Result { const samples = try gpa.alloc(u64, config.reps); defer gpa.free(samples); var expected: ?Identity = null; for (0..config.warmup + config.reps) |round| { // Document open/close is setup: this row isolates MuPDF outline load, // bridge flattening, and the Zig-owned two-allocation copy. - var document = try pdf.Document.open(path); + var document = try pdf.Document.openBytes(fixture); defer document.deinit(); const started = nowNs(); var outline = try document.outline(gpa); |
