From 549cfa40e99c3d1937b742a0dba043281c687c83 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 29 Sep 2026 12:29:43 -0300 Subject: A PDF's body reads the text layer of the page it shows The body of a PDF pane read as empty. It now reads MuPDF's text extraction of the page shown, the text search and n/N already use, read-only (a write is refused, as round 22's body refusal made it). Co-Authored-By: Claude Opus 5.5 --- docs/fs.md | 3 +++ src/ninep/ctl.zig | 21 +++++++++++++++++++++ src/ninep/pane.zig | 17 ++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/fs.md b/docs/fs.md index 03cdd1ee..53082b11 100644 --- a/docs/fs.md +++ b/docs/fs.md @@ -1015,6 +1015,9 @@ never writes `follow`, so it sees nothing new: use the follow open instead. `/sc `cols`, `rows`, `cursor`, a `styles` table, and row-major `cells` of `[grapheme, style_index]`. Each open freezes one frame until close. A terminal `body` freezes its history on the first read of each open handle; +a PDF's `body` reads the text layer of the page it shows (MuPDF's +extraction; turn the page for another), and it, as an image's, takes no +write (`this pane has no text`, EINVAL); `pty/data` streams live output. An open that holds something between open and close -- a frozen screen, terminal body or log, a run, an `event` or `pty/data` open -- takes one of 64 records (lib9p's per-fid aux, acme's diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index db058ff2..be90bb6f 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -2115,6 +2115,27 @@ test "a language server question that finds nothing, cannot be answered, or is a try testing.expectEqual(E.INVAL, wr(p, Node.of(img.serial, .body), "text\n").errno()); } +test "a PDF's body reads its page's text layer, and takes no write" { + if (comptime !pardes.pdf_enabled) return; + const p = try withFile(testing.allocator, "x\n"); + defer p.deinit(); + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const bytes = try @import("mupdf").makeLinksTestPdf(testing.allocator); + defer testing.allocator.free(bytes); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "doc.pdf", .data = bytes }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; + var line: [4200]u8 = undefined; + _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/doc.pdf\n", .{dir})); + p.sync(); + const doc = p.panes[p.active].?; + try testing.expect(doc.hasPdf()); + const body = Node.of(doc.serial, .body); + try testing.expect(std.mem.indexOf(u8, rd(p, body, 0, 4096).bytes, "target.txt") != null); + try testing.expectEqual(E.INVAL, wr(p, body, "more\n").errno()); +} + test "every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 9e832df6..311a2429 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -257,7 +257,7 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 { const pf = &pane.fs; return switch (f) { // A terminal's body reads its screen and history, as text. - .body => if (pane.file == null and pane.isTerminal()) panes.Terminal.screenTextLen(pane) else bodyOf(pane).len, + .body => if (pane.file == null and pane.isTerminal()) panes.Terminal.screenTextLen(pane) else if (pdfText(p, pane)) |t| t.len else bodyOf(pane).len, .data, .xdata => bodyOf(pane).len, .tag => tagOf(p, pane).len, .name => nameOf(pane).len + 1, @@ -367,6 +367,14 @@ fn readFlag(p: *Pardes, req: Req, on: bool) Reply { return tree.stagedReply(p, req); } +/// The text layer of the page a PDF pane shows (MuPDF's extraction, kept +/// with the page), null for any other pane. +fn pdfText(p: *Pardes, pane: *Pane) ?[]const u8 { + if (comptime !pardes.pdf_enabled) return null; + const pv = &(pane.pdf orelse return null); + return pv.ensureText(p.pdf_gpa); +} + fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { if (pane.file != null) { const text = bodyOf(pane); @@ -379,6 +387,13 @@ fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { .len = clip(n), } } }; } + // A PDF's body is its text layer, the page shown's: read-only. + if (pdfText(p, pane)) |text| { + const off = @min(req.off, text.len); + const out = p.fs.stage(p.gpa); + out.appendSlice(p.gpa, text[off..][0..@min(text.len - off, req.size)]) catch return Reply.fail(req.tag, E.NOMEM); + return tree.stagedReply(p, req); + } if (req.handle != 0 and pane.isTerminal()) return screen.readSnapshot(p, req, pane); const text = panes.Terminal.screenTextAlloc(pane, p.gpa) catch return Reply.fail(req.tag, E.NOMEM); -- cgit v1.3