//! docs/typ/reference.typ, Addons and the PDF passages of Panes //! and Look and exec : a PDF is a pane of the pdf //! addon (addons/pdf/pdf.zig), driven here as a 9P client drives it. What //! test/pdf.zig asked of the core's PDF code before it left the core. const std = @import("std"); const Session = @import("session").Session; fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } /// The pictures /screen places for pane `serial`: their keys and the part /// of their pixels shown, `[x, y, w, h]`. const Shown = struct { key: []const u8, src: [4]i64 }; fn shown(s: *Session, serial: u32) ![]Shown { const a = s.arena.allocator(); const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); var out: std.ArrayList(Shown) = .empty; const images = json.object.get("images") orelse return out.items; for (images.array.items) |im| { if (im.object.get("serial").?.integer != serial) continue; var src: [4]i64 = undefined; for (im.object.get("src").?.array.items, &src) |v, *o| o.* = v.integer; const key = im.object.get("key").?; try out.append(a, .{ .key = if (key == .string) key.string else try std.fmt.allocPrint(a, "{d}", .{key.integer}), .src = src }); } return out.items; } fn ctlOf(s: *Session, n: u32) ![]const u8 { return s.read(s.at("/pane/{d}/ctl", .{n})); } test "addons: a PDF is the pdf addon's pane, its pages pictures, its page in its tag, its fit and tint in its ctl and stepped by its words, all of it back from a Dump" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); s.graphics(); const n = try s.lookFile("docs/design.pdf"); try std.testing.expect(has(try s.read("/index"), " pdf ")); try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "pdf 1 3.0 1 /")); const pages = (try s.pdfPage(n))[1]; try std.testing.expectEqual(@as(u32, 16), pages); // Its first page, a picture over its body. const first = try shown(&s, n); try std.testing.expect(first.len >= 1); // Its tag: its page, then the core's art words (it placed a picture), // its path and its kind's words. const tag = try s.read(s.at("/pane/{d}/tag", .{n})); try std.testing.expect(std.mem.startsWith(u8, tag, "[1/16] petscii:off palette:commodore ascii:on /")); try std.testing.expect(has(tag, "/docs/design.pdf Term PdfSections PdfTint PdfFit Collapse Del")); try std.testing.expect(has(try ctlOf(&s, n), " fit:width tint:filtered mode:normal ")); // The words: bare they step, given a value they take it, any other // value refused; another pane's is refused and leaves the PDF alone. const ctl = s.at("/pane/{d}/ctl", .{n}); try s.write(ctl, "PdfFit\n"); try std.testing.expect(has(try ctlOf(&s, n), " fit:height ")); try s.write(ctl, "PdfTint full\n"); try std.testing.expect(has(try ctlOf(&s, n), " tint:full ")); // A tint is a page drawn again: another picture. const tinted = try shown(&s, n); try std.testing.expect(tinted.len >= 1 and !std.mem.eql(u8, tinted[0].key, first[0].key)); _ = try s.refused(ctl, "PdfTint loud\n"); try std.testing.expect(has(s.said, "PdfTint takes disabled, filtered or full")); const text = std.mem.sliceTo(try s.read("/index"), ' '); _ = try s.refused(s.at("/pane/{s}/ctl", .{text}), "PdfFit\n"); try std.testing.expect(has(try ctlOf(&s, n), " fit:height tint:full ")); // Its last page, then a Dump: restored it is there, as it was. try s.keys("ge"); try std.testing.expectEqual(@as(u32, 16), (try s.pdfPage(n))[0]); try s.write("/ctl", "Dump\n"); const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' ')); try s.write(ctl, "Del\n"); try s.write("/ctl", s.at("Restore {s}\n", .{label})); // (A test's session steps only on its writes: a key that moves nothing // is the step after the Restore a host's wake makes.) try s.keys("0"); var back: u32 = 0; var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (lines.next()) |line| if (has(line, " pdf ")) { back = try std.fmt.parseInt(u32, std.mem.sliceTo(line, ' '), 10); }; try std.testing.expect(back != 0); try std.testing.expectEqual(@as(u32, 16), (try s.pdfPage(back))[0]); try std.testing.expect(has(try ctlOf(&s, back), " fit:height tint:full ")); try std.testing.expect((try shown(&s, back)).len >= 1); } test "addons: a PDF's / lists its hits as file.pdf:P:H rows in a +Search, n walks them, a Look of one turns to its page, Esc drops the search first and then leaves, and a hit not there is a miss" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("docs/design.pdf"); try s.keys("/pardes\r"); var rows: []const u8 = ""; var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (it.next()) |line| if (has(line, "+Search")) { rows = try s.read(s.at("/pane/{s}/body", .{std.mem.sliceTo(line, ' ')})); }; // (Each row its place, then the text, as a +Search lays its rows out.) try std.testing.expect(std.mem.startsWith(u8, rows, "design.pdf:1:1\n")); try std.testing.expect(std.mem.count(u8, rows, "design.pdf:") > 3); // At its first hit's page; from the PDF n walks its rows (the // keyboard on the next, selected), and a Look of a row turns there. try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); var search: u32 = 0; it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (it.next()) |line| if (has(line, "+Search")) { search = try std.fmt.parseInt(u32, std.mem.sliceTo(line, ' '), 10); }; try s.write("/focus", s.at("{d}\n", .{n})); try s.keys("n"); try std.testing.expectEqual(search, try s.focused()); var last: []const u8 = ""; var places = std.mem.tokenizeScalar(u8, rows, '\n'); while (places.next()) |row| if (std.mem.startsWith(u8, row, "design.pdf:")) { last = row; }; const page = try std.fmt.parseInt(u32, last["design.pdf:".len..std.mem.indexOfScalarPos(u8, last, "design.pdf:".len, ':').?], 10); try s.write(s.at("/pane/{d}/look", .{search}), s.at("{s}\n", .{last})); try std.testing.expectEqual(page, (try s.pdfPage(n))[0]); try std.testing.expectEqual(n, try s.focused()); // A hit there is a place; one not there a miss said with its path. const look = s.at("/pane/{d}/look", .{n}); try s.write(look, ":1:1\n"); _ = try s.refused(look, ":1:99\n"); try std.testing.expect(has(try s.read("/log"), "design.pdf has no search hit 99 on page 1")); // Esc: the search goes first (no hit is a place now), then the pane. try s.keys("\x1b"); try std.testing.expectEqual(n, try s.focused()); _ = try s.refused(look, ":1:1\n"); try s.keys("\x1b"); try std.testing.expect(try s.focused() != n); } test "addons: a PDF's f is its PdfSections, its outline as places a Look in it goes to (none once the PDF is gone), and a jump back over a far one is Ctrl-o's, Forward's on, two places on a page a window apart two" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); s.graphics(); const n = try s.lookFile("test/pdf-fixtures/outline.pdf"); try s.keys("f"); var list: u32 = 0; var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (it.next()) |line| if (has(line, "/+PdfSections")) { list = try std.fmt.parseInt(u32, std.mem.sliceTo(line, ' '), 10); }; try std.testing.expect(list != 0); const body = try s.read(s.at("/pane/{d}/body", .{list})); try std.testing.expect(std.mem.startsWith(u8, body, "outline.pdf:")); // The last row of a section in it: its page, where a Look lands. var last: []const u8 = ""; var rows = std.mem.tokenizeScalar(u8, body, '\n'); while (rows.next()) |row| if (std.mem.startsWith(u8, row, "outline.pdf:")) { last = row; }; const place = last[0..std.mem.indexOfScalar(u8, last, ' ').?]; var fields = std.mem.splitScalar(u8, place, ':'); _ = fields.next(); const page = try std.fmt.parseInt(u32, fields.next().?, 10); try s.write(s.at("/pane/{d}/look", .{list}), s.at("{s}\n", .{place})); try std.testing.expectEqual(page, (try s.pdfPage(n))[0]); // A jump far: Ctrl-o goes back, Ctrl-i on. try s.write(s.at("/pane/{d}/look", .{n}), ":1\n"); try s.keys("ge"); const end = (try s.pdfPage(n))[0]; try s.keys("\x0f"); try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); // (Ctrl-i is Tab without kitty's keyboard: its word, Forward.) try s.write("/ctl", "Forward\n"); try std.testing.expectEqual(end, (try s.pdfPage(n))[0]); // Two places on page 1 more than a window apart (the second its 0.8, // as its `where` says a place) are two visits: Ctrl-o from a far one // goes to the second, then the first, where they were. const look = s.at("/pane/{d}/look", .{n}); try s.write(look, ":1\n"); const top = (try shown(&s, n))[0].src[1]; try s.write(look, ":1:0.8\n"); try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); const down = (try shown(&s, n))[0].src[1]; try std.testing.expect(down > top); try s.write(look, ":3\n"); try s.keys("\x0f"); try std.testing.expectEqual(down, (try shown(&s, n))[0].src[1]); try s.keys("\x0f"); try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); try std.testing.expectEqual(top, (try shown(&s, n))[0].src[1]); // The PDF gone, its list's rows are no place: not another pane's. try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); _ = try s.refused(s.at("/pane/{d}/look", .{list}), s.at("{s}\n", .{place})); try std.testing.expect(has(try s.read("/log"), "the pane its list is of is gone")); } test "addons: a closed PDF is in Recent and its jumps, and a jump back to it opens it at its page; a tall page is shown a band of it at a time" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); s.graphics(); const n = try s.lookFile("docs/design.pdf"); try s.write(s.at("/pane/{d}/look", .{n}), ":5\n"); const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); // From a scratch, Back is the nearest place of any file: the PDF's, // closed meanwhile, opened again at its page. const scratch = try s.newPane(); try s.write("/focus", s.at("{d}\n", .{scratch})); try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}", .{name}))); try s.keys("\x0f"); const again = try s.focused(); try std.testing.expect(again != scratch); try std.testing.expectEqual(@as(u32, 5), (try s.pdfPage(again))[0]); // A page 9000 points tall: a band of its rows, never the whole. const tall = try s.lookFile("test/pdf-fixtures/tall.pdf"); const bands = try shown(&s, tall); try std.testing.expect(bands.len >= 1); try std.testing.expect(bands[0].src[3] <= 8192); } test "addons: a PDF's body is its page's text layer, read from any offset and never written; a truncated PDF is a refused Look saying why" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("test/pdf-fixtures/links.pdf"); const body = s.at("/pane/{d}/body", .{n}); const whole = try s.arena.allocator().dupe(u8, try s.read(body)); try std.testing.expect(has(whole, "target.txt")); // (Read from an offset, as a mount's chunked reads ask.) try std.testing.expectEqualStrings(whole[3..], try s.readAt(body, 3, 4096)); try std.testing.expect(try s.refused(body, "more\n") != 0); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); const bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", s.arena.allocator(), .unlimited); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "cut.pdf", .data = bytes[0..40] }); var buf: [4096]u8 = undefined; const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; _ = try s.refused("/look", s.at("{s}/cut.pdf\n", .{dir})); try std.testing.expect(has(s.said, "cut.pdf") and has(s.said, "not a PDF pardes can read")); } test "addons: a PDF whose file changes on disk shows its new pages, under new keys: a window keeps a texture by its key" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); s.graphics(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var buf: [4096]u8 = undefined; const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; const a = s.arena.allocator(); const links = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", a, .unlimited); const outline = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/outline.pdf", a, .unlimited); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = links }); const n = try s.lookFile(s.at("{s}/doc.pdf", .{dir})); const first = try shown(&s, n); try std.testing.expect(first.len >= 1); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = outline }); try std.testing.expect(try s.filesChanged()); // Its pages laid out at the step after (the host's next). try s.keys("0"); const now = try shown(&s, n); try std.testing.expect(now.len >= 1); for (now) |pic| for (first) |old| try std.testing.expect(!std.mem.eql(u8, pic.key, old.key)); }