Skip to content

Lightpanda: the headless browser designed for AI and automation

Add PDF support

Karl Seguin67927b9bdee4
diff --git a/README.md b/README.md
index 3e54b2d6c..2fee3353b 100644
--- a/README.md
+++ b/README.md
@@ -106,7 +106,8 @@ docker run -d --name lightpanda -p 127.0.0.1:9222:9222 lightpanda/browser:nightl
```
You can use `--dump markdown` to convert directly into markdown, or
-`--dump png > page.png` for a text-only rendering of the page.
+`--dump png > page.png` or `--dump pdf > page.pdf` for a text-only rendering
+of the page.
`--wait-until`, `--wait-ms`, `--wait-selector` and `--wait-script` are
available to adjust waiting time before dump.
diff --git a/src/Config.zig b/src/Config.zig
index f23c5127b..0536c814f 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -871,6 +871,7 @@ pub const DumpFormat = enum {
html,
markdown,
png,
+ pdf,
wpt,
semantic_tree,
semantic_tree_text,
diff --git a/src/browser/pdf.zig b/src/browser/pdf.zig
new file mode 100644
index 000000000..da0522895
--- /dev/null
+++ b/src/browser/pdf.zig
@@ -0,0 +1,1137 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// PDF output for the block model, behind `Page.printToPDF` and `--dump pdf`.
+// The screenshot's DOM walk and parley layout (exported by the Rust side as
+// positioned glyph runs), paginated at line boundaries and written as real
+// text: glyph ids through a Type0/Identity-H font over an embedded subset of
+// the bundled DejaVu faces, a ToUnicode map so the text selects, copies and
+// searches, and a link annotation over every <a href>. The file is written
+// by hand: the objects below are all a text-only page needs.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const Base64Writer = @import("../Base64Writer.zig");
+
+const Frame = @import("Frame.zig");
+const screenshot = @import("screenshot.zig");
+
+const Node = @import("webapi/Node.zig");
+
+const log = lp.log;
+const flate = std.compress.flate;
+const Allocator = std.mem.Allocator;
+
+/// Print options. Lengths are CSS px (96 per inch); the defaults are
+/// Chrome's: US Letter with 0.4in margins.
+pub const Opts = struct {
+ paper_width: f32 = 8.5 * 96,
+ paper_height: f32 = 11 * 96,
+ margin_top: f32 = 0.4 * 96,
+ margin_right: f32 = 0.4 * 96,
+ margin_bottom: f32 = 0.4 * 96,
+ margin_left: f32 = 0.4 * 96,
+ /// Content scale, 0.1 to 2.
+ scale: f32 = 1.0,
+ print_background: bool = false,
+ /// Pages to print; empty prints everything. Pages come out in document
+ /// order, once each; pages past the end are ignored, and a selection
+ /// that ends up empty is an error. `parsePageRanges` reads CDP's text
+ /// form.
+ page_ranges: []const PageRange = &.{},
+};
+
+/// 1-based, inclusive. `to = maxInt(u32)` for an open end ("5-").
+pub const PageRange = struct {
+ from: u32,
+ to: u32,
+};
+
+/// CDP's `pageRanges` grammar ("1-5, 8, 11-13"): comma-separated pages or
+/// spans, whitespace ignored around each number, either end of a span may
+/// be left open. Same two failure classes as Chrome: unparsable text, or a
+/// range that is empty by construction (page 0, start past end).
+pub fn parsePageRanges(arena: Allocator, text: []const u8) error{ InvalidPageRangeSyntax, InvalidPageRange, OutOfMemory }![]const PageRange {
+ var out: std.ArrayList(PageRange) = .empty;
+ var it = std.mem.splitScalar(u8, text, ',');
+ while (it.next()) |raw| {
+ const part = std.mem.trim(u8, raw, &std.ascii.whitespace);
+ if (part.len == 0) continue;
+ var range: PageRange = undefined;
+ if (std.mem.indexOfScalar(u8, part, '-')) |dash| {
+ if (std.mem.indexOfScalarPos(u8, part, dash + 1, '-') != null) return error.InvalidPageRangeSyntax;
+ const a = std.mem.trim(u8, part[0..dash], &std.ascii.whitespace);
+ const b = std.mem.trim(u8, part[dash + 1 ..], &std.ascii.whitespace);
+ range.from = if (a.len == 0) 1 else parsePageNumber(a) orelse return error.InvalidPageRangeSyntax;
+ range.to = if (b.len == 0) std.math.maxInt(u32) else parsePageNumber(b) orelse return error.InvalidPageRangeSyntax;
+ } else {
+ range.from = parsePageNumber(part) orelse return error.InvalidPageRangeSyntax;
+ range.to = range.from;
+ }
+ if (range.from == 0 or range.from > range.to) return error.InvalidPageRange;
+ try out.append(arena, range);
+ }
+ return out.items;
+}
+
+fn parsePageNumber(s: []const u8) ?u32 {
+ return std.fmt.parseInt(u32, s, 10) catch null;
+}
+
+/// Prints `node` as a paginated, text PDF.
+pub fn print(arena: Allocator, node: *Node, opts: Opts, writer: *std.Io.Writer, frame: *Frame) !void {
+ const prepared = try prepare(arena, node, opts, frame);
+ return prepared.write(writer);
+}
+
+/// The PDF counterpart of `screenshot.preparePng`: same DOM walk, same block
+/// model, so a print and a screenshot agree on the page's text. Everything
+/// that can fail does so here, before any output: options, and a page
+/// selection that hits no page (that one costs a layout pass, only paid
+/// when ranges are given).
+pub fn prepare(arena: Allocator, node: *Node, opts: Opts, frame: *Frame) !Prepared {
+ const content_w = opts.paper_width - opts.margin_left - opts.margin_right;
+ const content_h = opts.paper_height - opts.margin_top - opts.margin_bottom;
+ if (!(opts.paper_width > 0 and opts.paper_height > 0) or
+ !(opts.margin_top >= 0 and opts.margin_right >= 0 and opts.margin_bottom >= 0 and opts.margin_left >= 0) or
+ !(content_w >= 1 and content_h >= 1) or
+ !(opts.scale >= 0.1 and opts.scale <= 2))
+ {
+ return error.InvalidPdfOptions;
+ }
+ for (opts.page_ranges) |r| {
+ if (r.from == 0 or r.from > r.to) return error.InvalidPdfOptions;
+ }
+ const prepared: Prepared = .{
+ .arena = arena,
+ .opts = opts,
+ .blocks = try screenshot.collect(arena, node, frame),
+ .renderer = try screenshot.rendererFor(frame),
+ };
+ if (opts.page_ranges.len > 0) {
+ const count = try prepared.pageCount();
+ for (opts.page_ranges) |r| {
+ if (r.from <= count) break;
+ } else return error.PageRangeExceedsPageCount;
+ }
+ return prepared;
+}
+
+pub const Prepared = struct {
+ arena: Allocator,
+ opts: Opts,
+ blocks: []const screenshot.LpBlock,
+ renderer: *screenshot.Renderer,
+
+ pub fn write(self: *const Prepared, writer: *std.Io.Writer) std.Io.Writer.Error!void {
+ _ = try self.render(writer, false);
+ }
+
+ /// How many pages the document paginates to, without writing any.
+ pub fn pageCount(self: *const Prepared) std.Io.Writer.Error!u32 {
+ var discard: std.Io.Writer.Discarding = .init(&.{});
+ return self.render(&discard.writer, true);
+ }
+
+ // Serializes as a base64 string, streamed, like `screenshot.Prepared`.
+ pub fn jsonStringify(self: *const Prepared, jws: *std.json.Stringify) std.Io.Writer.Error!void {
+ try jws.beginWriteRaw();
+ try jws.writer.writeByte('"');
+ var b64 = Base64Writer.init(jws.writer, .standard);
+ try self.write(&b64.writer);
+ try b64.finish();
+ try jws.writer.writeByte('"');
+ jws.endWriteRaw();
+ }
+
+ // WriteFailed is the only error jsonStringify's signature can carry,
+ // hence the log line for everything else.
+ fn render(self: *const Prepared, writer: *std.Io.Writer, measure_only: bool) std.Io.Writer.Error!u32 {
+ return self.renderInner(writer, measure_only) catch |err| switch (err) {
+ error.WriteFailed => return error.WriteFailed,
+ else => {
+ log.err(.browser, "pdf render", .{ .err = err });
+ return error.WriteFailed;
+ },
+ };
+ }
+
+ fn renderInner(self: *const Prepared, writer: *std.Io.Writer, measure_only: bool) !u32 {
+ const opts = self.opts;
+ const content_w = opts.paper_width - opts.margin_left - opts.margin_right;
+ const content_h = opts.paper_height - opts.margin_top - opts.margin_bottom;
+
+ // Layout in content px: CSS px over the print scale, so the page
+ // transform is one uniform scale and the text metrics stay the
+ // screenshot's.
+ var layout = try screenshot.layout(self.renderer, self.blocks, content_w / opts.scale, 0);
+ defer layout.deinit();
+
+ var doc: Document = try .init(self.arena, &layout, self.blocks, opts, content_h / opts.scale);
+ const page_count: u32 = @intCast(doc.breaks.len);
+ if (measure_only) {
+ return page_count;
+ }
+ if (std.mem.indexOfScalar(bool, doc.selected, true) == null) {
+ return error.NoPagesSelected;
+ }
+ try doc.draw();
+ try doc.writeFile(writer);
+ return page_count;
+ }
+};
+
+/// CSS px to PDF points.
+const PT: f32 = 0.75;
+/// Prefix marking an embedded font as a subset.
+const SUBSET_TAG = "LPSUBS";
+
+const Document = struct {
+ arena: Allocator,
+ layout: *const screenshot.Layout,
+ blocks: []const screenshot.LpBlock,
+ opts: Opts,
+ /// Column width in layout px.
+ column_w: f32,
+ /// Absolute y where each page starts.
+ breaks: []const f32,
+ selected: []const bool,
+ /// Per page: content stream body and link annotation dictionaries.
+ pages: []std.Io.Writer.Allocating,
+ annots: []std.ArrayList([]const u8),
+ /// One per layout font, filled in as glyphs are drawn.
+ fonts: []FontUse,
+ /// Per block: its span texts concatenated (what the clusters index).
+ texts: [][]const u8,
+
+ const Link = struct {
+ start: u32,
+ end: u32,
+ href: []const u8,
+ };
+
+ fn init(arena: Allocator, layout: *const screenshot.Layout, blocks: []const screenshot.LpBlock, opts: Opts, page_h: f32) !Document {
+ const breaks = try paginate(arena, layout, page_h);
+
+ // Pages come out in document order, once each, whatever order or
+ // overlap the ranges have; anything past the end is ignored.
+ const selected = try arena.alloc(bool, breaks.len);
+ for (selected, 1..) |*sel, n| {
+ sel.* = opts.page_ranges.len == 0;
+ for (opts.page_ranges) |r| {
+ if (n >= r.from and n <= r.to) sel.* = true;
+ }
+ }
+
+ const pages = try arena.alloc(std.Io.Writer.Allocating, breaks.len);
+ const annots = try arena.alloc(std.ArrayList([]const u8), breaks.len);
+ for (pages, annots) |*p, *a| {
+ p.* = .init(arena);
+ a.* = .empty;
+ }
+
+ const fonts = try arena.alloc(FontUse, layout.fonts().len);
+ for (fonts, layout.fonts()) |*f, info| {
+ f.* = try .init(info);
+ }
+
+ const texts = try arena.alloc([]const u8, blocks.len);
+ for (texts, blocks) |*t, b| {
+ var text: std.ArrayList(u8) = .empty;
+ for (b.spans[0..b.spans_len]) |s| {
+ try text.appendSlice(arena, s.text[0..s.len]);
+ }
+ t.* = text.items;
+ }
+
+ return .{
+ .arena = arena,
+ .layout = layout,
+ .blocks = blocks,
+ .opts = opts,
+ .column_w = (opts.paper_width - opts.margin_left - opts.margin_right) / opts.scale,
+ .breaks = breaks,
+ .selected = selected,
+ .pages = pages,
+ .annots = annots,
+ .fonts = fonts,
+ .texts = texts,
+ };
+ }
+
+ /// The page an absolute y lands on, and that y relative to the page.
+ fn pageOf(self: *const Document, y: f32) struct { usize, f32 } {
+ var lo: usize = 0;
+ var hi: usize = self.breaks.len;
+ while (hi - lo > 1) {
+ const mid = lo + (hi - lo) / 2;
+ if (self.breaks[mid] <= y) lo = mid else hi = mid;
+ }
+ return .{ lo, y - self.breaks[lo] };
+ }
+
+ fn draw(self: *Document) !void {
+ const layout = self.layout;
+ const lines = layout.lines();
+ for (layout.blocks(), 0..) |b, bi| {
+ if (b.kind == @intFromEnum(screenshot.LpBlock.Kind.rule)) {
+ const pg, const py = self.pageOf(b.y);
+ if (self.selected[pg]) {
+ try rect(&self.pages[pg].writer, layout.raw.rule_color, b.x, py, self.column_w - b.x, 1);
+ }
+ continue;
+ }
+ const block_lines = lines[b.lines..][0..b.lines_len];
+ const boxes = try self.lineBoxes(b, block_lines);
+
+ // Backgrounds and quote bars: one rect per page a block lands
+ // on, so the lines it spans there never show a seam.
+ var i: usize = 0;
+ while (i < boxes.len) {
+ const pg, const top = self.pageOf(boxes[i].top);
+ var j = i;
+ while (j + 1 < boxes.len and self.pageOf(boxes[j + 1].top)[0] == pg) j += 1;
+ const h = boxes[j].bottom - boxes[i].top;
+ if (self.selected[pg]) {
+ const w = &self.pages[pg].writer;
+ if (b.kind == @intFromEnum(screenshot.LpBlock.Kind.pre) and self.opts.print_background) {
+ const x0 = b.x - layout.raw.pre_pad;
+ try rect(w, layout.raw.pre_bg, x0, top, self.column_w - x0, h);
+ }
+ for (0..b.quote_bars) |q| {
+ const bx = b.quote_x + @as(f32, @floatFromInt(q)) * layout.raw.quote_indent;
+ try rect(w, layout.raw.rule_color, bx, top, 3, h);
+ }
+ }
+ i = j + 1;
+ }
+
+ const links = try self.linksOf(bi);
+ for (block_lines, boxes, 0..) |line, box, li| {
+ const pg, _ = self.pageOf(box.top);
+ if (!self.selected[pg]) continue;
+ const shift = self.breaks[pg];
+ const w = &self.pages[pg].writer;
+ if (li == 0 and b.marker_line != screenshot.LAYOUT_NONE) {
+ try self.drawLine(w, lines[b.marker_line], shift);
+ }
+ try self.drawLine(w, line, shift);
+ if (links.len > 0) {
+ try self.linkAnnotations(pg, line, shift, links);
+ }
+ try self.mapUnicode(line, self.texts[bi]);
+ }
+ if (b.marker_line != screenshot.LAYOUT_NONE) {
+ // The marker's own layout was built from the marker string.
+ const block = self.blocks[bi];
+ try self.mapUnicode(lines[b.marker_line], block.marker[0..block.marker_len]);
+ }
+ }
+ }
+
+ const Box = struct { top: f32, bottom: f32 };
+
+ /// Each line's vertical extent. A <pre> grows its first and last line by
+ /// the padding so the background stays with the text across a break.
+ fn lineBoxes(self: *const Document, b: screenshot.LpLayoutBlock, block_lines: []const screenshot.LpLine) ![]const Box {
+ const pad: f32 = if (b.kind == @intFromEnum(screenshot.LpBlock.Kind.pre)) self.layout.raw.pre_pad else 0;
+ const boxes = try self.arena.alloc(Box, block_lines.len);
+ for (boxes, block_lines, 0..) |*box, line, i| {
+ box.* = .{
+ .top = line.top - if (i == 0) pad else 0,
+ .bottom = line.bottom + if (i + 1 == block_lines.len) pad else 0,
+ };
+ }
+ return boxes;
+ }
+
+ /// Byte ranges of the block's text that are links, contiguous spans of
+ /// the same href merged.
+ fn linksOf(self: *const Document, bi: usize) ![]const Link {
+ var links: std.ArrayList(Link) = .empty;
+ var offset: u32 = 0;
+ const block = self.blocks[bi];
+ for (block.spans[0..block.spans_len]) |s| {
+ const end = offset + @as(u32, @intCast(s.len));
+ if (s.href_len > 0) {
+ const href = s.href[0..s.href_len];
+ if (links.items.len > 0 and links.items[links.items.len - 1].end == offset and std.mem.eql(u8, links.items[links.items.len - 1].href, href)) {
+ links.items[links.items.len - 1].end = end;
+ } else {
+ try links.append(self.arena, .{ .start = offset, .end = end, .href = href });
+ }
+ }
+ offset = end;
+ }
+ return links.items;
+ }
+
+ fn drawLine(self: *Document, w: *std.Io.Writer, line: screenshot.LpLine, shift: f32) !void {
+ for (self.layout.runs()[line.runs..][0..line.runs_len]) |run| {
+ try self.drawRun(w, run, line.x, shift);
+ }
+ }
+
+ /// One glyph run as a TJ array: font size 1 with the size (and any
+ /// synthetic italic skew) in the text matrix, glyph ids as 2-byte CIDs,
+ /// and an adjustment wherever the layout's pen differs from the font's
+ /// advance (kerning, marks, visual order). Same transform as the raster.
+ fn drawRun(self: *Document, w: *std.Io.Writer, run: screenshot.LpRun, line_x: f32, shift: f32) !void {
+ const font = &self.fonts[run.font];
+ const size = run.size;
+ const baseline = run.baseline - shift;
+ try w.print("BT /F{d} 1 Tf ", .{run.font});
+ try rgb(w, run.color);
+ try w.writeAll(" rg\n");
+ var pen = line_x + run.offset;
+ var cur_y: ?f32 = null;
+ var expected: f32 = 0;
+ for (self.layout.glyphs()[run.glyphs..][0..run.glyphs_len]) |g| {
+ const gx = pen + g.x;
+ const gy = baseline - g.y;
+ pen += g.advance;
+ if (cur_y != gy) {
+ if (cur_y != null) try w.writeAll(">] TJ\n");
+ try w.print("{d:.3} 0 {d:.3} {d:.3} {d:.3} {d:.3} Tm [<", .{ size, run.skew * size, -size, gx, gy });
+ cur_y = gy;
+ } else {
+ // Positive moves the pen left, in thousandths of the text
+ // space unit (= 1/size px here).
+ const adj = (expected - gx) * 1000 / size;
+ if (@abs(adj) >= 0.5) try w.print("> {d:.1} <", .{adj});
+ }
+ const gid: u16 = @intCast(g.id);
+ const width = try font.use(self.arena, gid);
+ try w.print("{X:0>4}", .{gid});
+ expected = gx + width * size / 1000;
+ }
+ if (cur_y != null) try w.writeAll(">] TJ\n");
+ try w.writeAll("ET\n");
+
+ const x0 = line_x + run.offset;
+ for ([_]screenshot.LpDecoration{ run.underline, run.strike }) |d| {
+ if (d.enabled == 0) continue;
+ try rect(w, d.color, x0, baseline - d.offset, run.advance, d.size);
+ }
+ }
+
+ /// One URI annotation per line per link: clusters in visual order,
+ /// contiguous runs of the same link merged into a box.
+ fn linkAnnotations(self: *Document, pg: usize, line: screenshot.LpLine, shift: f32, links: []const Link) !void {
+ const top = line.top - shift;
+ const bottom = line.bottom - shift;
+ var open: ?struct { link: usize, x0: f32, x1: f32 } = null;
+ for (self.layout.clusters()[line.clusters..][0..line.clusters_len]) |c| {
+ const x = line.x + c.x;
+ var hit: ?usize = null;
+ for (links, 0..) |l, i| {
+ if (c.text_start >= l.start and c.text_start < l.end) hit = i;
+ }
+ if (open != null and hit == open.?.link and @abs(open.?.x1 - x) < 0.01) {
+ open.?.x1 = x + c.advance;
+ continue;
+ }
+ if (open) |o| try self.annotate(pg, o.x0, top, o.x1, bottom, links[o.link].href);
+ open = if (hit) |i| .{ .link = i, .x0 = x, .x1 = x + c.advance } else null;
+ }
+ if (open) |o| try self.annotate(pg, o.x0, top, o.x1, bottom, links[o.link].href);
+ }
+
+ /// A URI link annotation over a page-relative box (layout px, y down).
+ /// The URL goes in as a hex string: nothing to escape.
+ fn annotate(self: *Document, pg: usize, x0: f32, top: f32, x1: f32, bottom: f32, url: []const u8) !void {
+ const opts = self.opts;
+ const s = opts.scale;
+ var aw: std.Io.Writer.Allocating = .init(self.arena);
+ try aw.writer.print("<< /Type /Annot /Subtype /Link /Rect [{d:.3} {d:.3} {d:.3} {d:.3}] /Border [0 0 0] /A << /S /URI /URI <", .{
+ (opts.margin_left + x0 * s) * PT,
+ (opts.paper_height - opts.margin_top - bottom * s) * PT,
+ (opts.margin_left + x1 * s) * PT,
+ (opts.paper_height - opts.margin_top - top * s) * PT,
+ });
+ for (url) |b| try aw.writer.print("{X:0>2}", .{b});
+ try aw.writer.writeAll("> >> >>\n");
+ try self.annots[pg].append(self.arena, aw.written());
+ }
+
+ /// Maps each cluster's glyph to the text it came from: a base+mark
+ /// cluster to its composed character, a ligature glyph to every letter
+ /// it absorbed (glyphless continuation clusters follow the one carrying
+ /// the glyph). First occurrence wins.
+ fn mapUnicode(self: *Document, line: screenshot.LpLine, text: []const u8) !void {
+ var ligature: ?struct { font: u32, gid: u16, text: std.ArrayList(u8) } = null;
+ for (self.layout.clusters()[line.clusters..][0..line.clusters_len]) |c| {
+ const t = if (c.text_start + c.text_len <= text.len) text[c.text_start..][0..c.text_len] else "";
+ if (c.flags & screenshot.CLUSTER_LIGATURE_CONT != 0) {
+ if (ligature) |*l| try l.text.appendSlice(self.arena, t);
+ continue;
+ }
+ if (ligature) |l| {
+ try self.fonts[l.font].mapText(self.arena, l.gid, l.text.items);
+ ligature = null;
+ }
+ if (c.glyph == screenshot.LAYOUT_NONE or t.len == 0) continue;
+ const gid: u16 = @intCast(c.glyph);
+ if (c.flags & screenshot.CLUSTER_LIGATURE_START != 0) {
+ var buf: std.ArrayList(u8) = .empty;
+ try buf.appendSlice(self.arena, t);
+ ligature = .{ .font = c.font, .gid = gid, .text = buf };
+ } else {
+ try self.fonts[c.font].mapText(self.arena, gid, t);
+ }
+ }
+ if (ligature) |l| try self.fonts[l.font].mapText(self.arena, l.gid, l.text.items);
+ }
+
+ // File structure: header, catalog, then each page's annotations,
+ // content and page object, then the fonts (subset by what the pages
+ // used), the shared resources and the page tree, and finally the xref.
+ fn writeFile(self: *Document, writer: *std.Io.Writer) !void {
+ const opts = self.opts;
+ var out: Out = .{ .writer = writer, .arena = self.arena };
+ try out.write("%PDF-1.4\n%\xE2\xE3\xCF\xD3\n");
+ const catalog = try out.alloc();
+ const pages_id = try out.alloc();
+ const resources = try out.alloc();
+ try out.begin(catalog);
+ try out.write("<< /Type /Catalog /Pages 2 0 R >>\n");
+ try out.end();
+
+ var kids: std.Io.Writer.Allocating = .init(self.arena);
+ var count: usize = 0;
+ for (self.pages, self.annots, 0..) |*page, annots, i| {
+ if (!self.selected[i]) continue;
+ var content: std.Io.Writer.Allocating = .init(self.arena);
+ try content.writer.print("q {d:.4} 0 0 {d:.4} {d:.3} {d:.3} cm\n", .{
+ PT * opts.scale,
+ -PT * opts.scale,
+ opts.margin_left * PT,
+ (opts.paper_height - opts.margin_top) * PT,
+ });
+ try content.writer.writeAll(page.written());
+ try content.writer.writeAll("Q\n");
+ const contents = try out.stream("", content.written());
+
+ var annot_refs: std.Io.Writer.Allocating = .init(self.arena);
+ for (annots.items) |a| {
+ const id = try out.alloc();
+ try out.begin(id);
+ try out.write(a);
+ try out.end();
+ try annot_refs.writer.print("{d} 0 R ", .{id});
+ }
+ const page_id = try out.alloc();
+ try out.begin(page_id);
+ try out.print("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {d:.3} {d:.3}] /Resources 3 0 R /Contents {d} 0 R", .{
+ opts.paper_width * PT,
+ opts.paper_height * PT,
+ contents,
+ });
+ if (annot_refs.written().len > 0) {
+ try out.print(" /Annots [ {s}]", .{annot_refs.written()});
+ }
+ try out.write(" >>\n");
+ try out.end();
+ try kids.writer.print("{d} 0 R ", .{page_id});
+ count += 1;
+ }
+
+ var font_refs: std.Io.Writer.Allocating = .init(self.arena);
+ for (self.fonts, 0..) |*font, i| {
+ if (font.used.count() == 0) continue;
+ const id = try font.writeObjects(self.arena, &out);
+ try font_refs.writer.print("/F{d} {d} 0 R ", .{ i, id });
+ }
+ try out.begin(resources);
+ try out.print("<< /Font << {s}>> >>\n", .{font_refs.written()});
+ try out.end();
+ try out.begin(pages_id);
+ try out.print("<< /Type /Pages /Count {d} /Kids [ {s}] >>\n", .{ count, kids.written() });
+ try out.end();
+
+ const xref_at = out.offset;
+ try out.print("xref\n0 {d}\n0000000000 65535 f \n", .{out.xref.items.len + 1});
+ for (out.xref.items) |off| {
+ try out.print("{d:0>10} 00000 n \n", .{off});
+ }
+ try out.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ out.xref.items.len + 1, xref_at });
+ }
+};
+
+/// Absolute y (layout px) where each page starts. A line that would cross
+/// the bottom of the page starts the next one; a line taller than a page
+/// overflows it. Rules are one 1px line; a <pre>'s padding rides with its
+/// first and last line.
+fn paginate(arena: Allocator, layout: *const screenshot.Layout, page_h: f32) ![]const f32 {
+ var breaks: std.ArrayList(f32) = .empty;
+ try breaks.append(arena, 0);
+ const lines = layout.lines();
+ for (layout.blocks()) |b| {
+ if (b.kind == @intFromEnum(screenshot.LpBlock.Kind.rule)) {
+ try fit(arena, &breaks, b.y, b.y + 1, page_h);
+ continue;
+ }
+ const pad: f32 = if (b.kind == @intFromEnum(screenshot.LpBlock.Kind.pre)) layout.raw.pre_pad else 0;
+ for (lines[b.lines..][0..b.lines_len], 0..) |line, i| {
+ const top = line.top - if (i == 0) pad else 0;
+ const bottom = line.bottom + if (i + 1 == b.lines_len) pad else 0;
+ try fit(arena, &breaks, top, bottom, page_h);
+ }
+ }
+ return breaks.items;
+}
+
+fn fit(arena: Allocator, breaks: *std.ArrayList(f32), top: f32, bottom: f32, page_h: f32) !void {
+ const start = breaks.items[breaks.items.len - 1];
+ if (bottom - start > page_h and top > start) {
+ try breaks.append(arena, top);
+ }
+}
+
+// Content-stream helpers. User space is the content box in layout px, y
+// down (the `cm` prefix in writeFile flips it), so positions are used as
+// laid out.
+
+fn rect(w: *std.Io.Writer, color: u32, x: f32, y: f32, width: f32, height: f32) !void {
+ if (!(width > 0 and height > 0)) return;
+ try rgb(w, color);
+ try w.print(" rg {d:.3} {d:.3} {d:.3} {d:.3} re f\n", .{ x, y, width, height });
+}
+
+/// "r g b" in 0..1 for a 0xRRGGBB.
+fn rgb(w: *std.Io.Writer, color: u32) !void {
+ try w.print("{d:.3} {d:.3} {d:.3}", .{
+ @as(f32, @floatFromInt((color >> 16) & 0xff)) / 255,
+ @as(f32, @floatFromInt((color >> 8) & 0xff)) / 255,
+ @as(f32, @floatFromInt(color & 0xff)) / 255,
+ });
+}
+
+// ---------------------------------------------------------------------------
+// Fonts: a face the layout used, the glyphs the pages drew with it (for the
+// subset and the widths) and what text each glyph stood for (ToUnicode).
+
+const FontUse = struct {
+ info: screenshot.LpFont,
+ ttf: TrueType,
+ /// gid → advance in 1/1000 em, for every glyph drawn.
+ used: std.AutoHashMapUnmanaged(u16, f32) = .empty,
+ /// gid → the text it was shaped from.
+ unicode: std.AutoHashMapUnmanaged(u16, []const u8) = .empty,
+
+ fn init(info: screenshot.LpFont) !FontUse {
+ return .{
+ .info = info,
+ .ttf = try TrueType.parse(info.data[0..info.data_len]),
+ };
+ }
+
+ /// Records a drawn glyph and returns its advance in 1/1000 em.
+ fn use(self: *FontUse, arena: Allocator, gid: u16) !f32 {
+ const gop = try self.used.getOrPut(arena, gid);
+ if (!gop.found_existing) {
+ gop.value_ptr.* = @as(f32, @floatFromInt(self.ttf.advance(gid))) * 1000 / @as(f32, @floatFromInt(self.ttf.upem));
+ }
+ return gop.value_ptr.*;
+ }
+
+ fn mapText(self: *FontUse, arena: Allocator, gid: u16, text: []const u8) !void {
+ const gop = try self.unicode.getOrPut(arena, gid);
+ if (!gop.found_existing) {
+ gop.value_ptr.* = try arena.dupe(u8, text);
+ }
+ }
+
+ fn sortedGids(self: *const FontUse, arena: Allocator) ![]u16 {
+ const gids = try arena.alloc(u16, self.used.count());
+ var it = self.used.keyIterator();
+ var i: usize = 0;
+ while (it.next()) |k| : (i += 1) gids[i] = k.*;
+ std.mem.sort(u16, gids, {}, std.sort.asc(u16));
+ return gids;
+ }
+
+ /// Writes the five objects of one embedded font and returns the Type0 id.
+ fn writeObjects(self: *const FontUse, arena: Allocator, out: *Out) !usize {
+ const ttf = &self.ttf;
+ const k = 1000 / @as(f32, @floatFromInt(ttf.upem));
+ const gids = try self.sortedGids(arena);
+ const name = try std.fmt.allocPrint(arena, "{s}+{s}", .{ SUBSET_TAG, self.info.name[0..self.info.name_len] });
+
+ const file = ttf.subset(arena, gids) catch ttf.data;
+ const file_id = try out.stream(try std.fmt.allocPrint(arena, "/Length1 {d}", .{file.len}), file);
+
+ const descriptor = try out.alloc();
+ try out.begin(descriptor);
+ try out.print("<< /Type /FontDescriptor /FontName /{s} /Flags {d} /FontBBox [{d:.1} {d:.1} {d:.1} {d:.1}] /ItalicAngle 0 /Ascent {d:.1} /Descent {d:.1} /CapHeight {d:.1} /StemV 80 /FontFile2 {d} 0 R >>\n", .{
+ name,
+ @as(u32, 32) | @as(u32, if (self.info.mono != 0) 1 else 0),
+ @as(f32, @floatFromInt(ttf.bbox[0])) * k,
+ @as(f32, @floatFromInt(ttf.bbox[1])) * k,
+ @as(f32, @floatFromInt(ttf.bbox[2])) * k,
+ @as(f32, @floatFromInt(ttf.bbox[3])) * k,
+ @as(f32, @floatFromInt(ttf.ascent)) * k,
+ -@abs(@as(f32, @floatFromInt(ttf.descent))) * k,
+ @as(f32, @floatFromInt(ttf.cap_height)) * k,
+ file_id,
+ });
+ try out.end();
+
+ // W: runs of consecutive gids as `first [w w w]`.
+ var widths: std.Io.Writer.Allocating = .init(arena);
+ var i: usize = 0;
+ while (i < gids.len) {
+ try widths.writer.print("{d} [{d:.1}", .{ gids[i], self.used.get(gids[i]).? });
+ while (i + 1 < gids.len and gids[i + 1] == gids[i] + 1) : (i += 1) {
+ try widths.writer.print(" {d:.1}", .{self.used.get(gids[i + 1]).?});
+ }
+ try widths.writer.writeAll("] ");
+ i += 1;
+ }
+ const cid = try out.alloc();
+ try out.begin(cid);
+ try out.print("<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{s} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor {d} 0 R /DW 1000 /W [ {s}] /CIDToGIDMap /Identity >>\n", .{ name, descriptor, widths.written() });
+ try out.end();
+
+ const to_unicode = try out.stream("", try self.toUnicodeCMap(arena, gids));
+ const type0 = try out.alloc();
+ try out.begin(type0);
+ try out.print("<< /Type /Font /Subtype /Type0 /BaseFont /{s} /Encoding /Identity-H /DescendantFonts [{d} 0 R] /ToUnicode {d} 0 R >>\n", .{ name, cid, to_unicode });
+ try out.end();
+ return type0;
+ }
+
+ fn toUnicodeCMap(self: *const FontUse, arena: Allocator, gids: []const u16) ![]const u8 {
+ var aw: std.Io.Writer.Allocating = .init(arena);
+ const w = &aw.writer;
+ try w.writeAll(
+ \\/CIDInit /ProcSet findresource begin
+ \\12 dict begin
+ \\begincmap
+ \\/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
+ \\/CMapName /Adobe-Identity-UCS def
+ \\/CMapType 2 def
+ \\1 begincodespacerange
+ \\<0000> <FFFF>
+ \\endcodespacerange
+ \\
+ );
+ var mapped: std.ArrayList(u16) = .empty;
+ for (gids) |gid| {
+ if (self.unicode.contains(gid)) try mapped.append(arena, gid);
+ }
+ var rest = mapped.items;
+ while (rest.len > 0) {
+ const chunk = rest[0..@min(rest.len, 100)];
+ rest = rest[chunk.len..];
+ try w.print("{d} beginbfchar\n", .{chunk.len});
+ for (chunk) |gid| {
+ try w.print("<{X:0>4}> <", .{gid});
+ // UTF-16BE.
+ var it = std.unicode.Utf8View.initUnchecked(self.unicode.get(gid).?).iterator();
+ while (it.nextCodepoint()) |cp| {
+ if (cp < 0x10000) {
+ try w.print("{X:0>4}", .{@as(u16, @intCast(cp))});
+ } else {
+ const c = cp - 0x10000;
+ try w.print("{X:0>4}{X:0>4}", .{ @as(u16, @intCast(0xD800 + (c >> 10))), @as(u16, @intCast(0xDC00 + (c & 0x3FF))) });
+ }
+ }
+ try w.writeAll(">\n");
+ }
+ try w.writeAll("endbfchar\n");
+ }
+ try w.writeAll("endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n");
+ return aw.written();
+ }
+};
+
+// ---------------------------------------------------------------------------
+// The TrueType tables the PDF needs: metrics for the descriptor, advances
+// for the W array and TJ, and a sparse subset for embedding.
+
+const TrueType = struct {
+ data: []const u8,
+ upem: u16,
+ num_glyphs: u16,
+ num_h_metrics: u16,
+ ascent: i16,
+ descent: i16,
+ cap_height: i16,
+ bbox: [4]i16,
+ hmtx: []const u8,
+
+ fn parse(data: []const u8) !TrueType {
+ if (data.len < 12 or be32(data, 0) != 0x00010000) return error.NotTrueType;
+ const head = table(data, "head") orelse return error.NotTrueType;
+ const hhea = table(data, "hhea") orelse return error.NotTrueType;
+ const maxp = table(data, "maxp") orelse return error.NotTrueType;
+ const hmtx = table(data, "hmtx") orelse return error.NotTrueType;
+ if (head.len < 54 or hhea.len < 36 or maxp.len < 6) return error.NotTrueType;
+ const ascent: i16 = @bitCast(be16(hhea, 4));
+ var cap_height = ascent;
+ if (table(data, "OS/2")) |os2| {
+ if (os2.len >= 90 and be16(os2, 0) >= 2) cap_height = @bitCast(be16(os2, 88));
+ }
+ return .{
+ .data = data,
+ .upem = be16(head, 18),
+ .num_glyphs = be16(maxp, 4),
+ .num_h_metrics = be16(hhea, 34),
+ .ascent = ascent,
+ .descent = @bitCast(be16(hhea, 6)),
+ .cap_height = cap_height,
+ .bbox = .{ @bitCast(be16(head, 36)), @bitCast(be16(head, 38)), @bitCast(be16(head, 40)), @bitCast(be16(head, 42)) },
+ .hmtx = hmtx,
+ };
+ }
+
+ fn table(data: []const u8, tag: *const [4]u8) ?[]const u8 {
+ const count = be16(data, 4);
+ for (0..count) |i| {
+ const rec = 12 + i * 16;
+ if (rec + 16 > data.len) return null;
+ if (!std.mem.eql(u8, data[rec..][0..4], tag)) continue;
+ const off = be32(data, rec + 8);
+ const len = be32(data, rec + 12);
+ if (off + len > data.len) return null;
+ return data[off..][0..len];
+ }
+ return null;
+ }
+
+ fn advance(self: *const TrueType, gid: u16) u16 {
+ const row = @min(gid, self.num_h_metrics -| 1);
+ const o = @as(usize, row) * 4;
+ if (o + 2 > self.hmtx.len) return 0;
+ return be16(self.hmtx, o);
+ }
+
+ /// Rebuilds the font keeping only the outlines of `gids` (plus the
+ /// components they reference). Glyph ids are unchanged: unused glyphs
+ /// keep their slot with no outline, so nothing else needs remapping.
+ fn subset(self: *const TrueType, arena: Allocator, gids: []const u16) ![]const u8 {
+ const data = self.data;
+ const head = table(data, "head") orelse return error.NotTrueType;
+ const loca = table(data, "loca") orelse return error.NotTrueType;
+ const glyf = table(data, "glyf") orelse return error.NotTrueType;
+ const n: usize = self.num_glyphs;
+ const long_loca = be16(head, 50) != 0;
+ const offsets = try arena.alloc(usize, n + 1);
+ for (offsets, 0..) |*o, i| {
+ if (long_loca) {
+ if (i * 4 + 4 > loca.len) return error.NotTrueType;
+ o.* = be32(loca, i * 4);
+ } else {
+ if (i * 2 + 2 > loca.len) return error.NotTrueType;
+ o.* = @as(usize, be16(loca, i * 2)) * 2;
+ }
+ if (o.* > glyf.len) return error.NotTrueType;
+ }
+
+ // Closure over composite components. .notdef always stays.
+ const keep = try arena.alloc(bool, n);
+ @memset(keep, false);
+ var stack: std.ArrayList(usize) = .empty;
+ try stack.append(arena, 0);
+ for (gids) |g| {
+ if (g < n) try stack.append(arena, g);
+ }
+ while (stack.pop()) |g| {
+ if (keep[g]) continue;
+ keep[g] = true;
+ const start = offsets[g];
+ const end = offsets[g + 1];
+ if (end < start + 10) continue;
+ const glyph = glyf[start..end];
+ if (@as(i16, @bitCast(be16(glyph, 0))) >= 0) continue;
+ var o: usize = 10;
+ while (o + 4 <= glyph.len) {
+ const flags = be16(glyph, o);
+ const component = be16(glyph, o + 2);
+ o += 4;
+ if (component < n) try stack.append(arena, component);
+ o += if (flags & 0x01 != 0) 4 else 2;
+ o += if (flags & 0x08 != 0) @as(usize, 2) else if (flags & 0x40 != 0) 4 else if (flags & 0x80 != 0) 8 else 0;
+ if (flags & 0x20 == 0) break;
+ }
+ }
+
+ var new_glyf: std.ArrayList(u8) = .empty;
+ var new_loca: std.ArrayList(u8) = .empty;
+ for (0..n) |g| {
+ try new_loca.appendSlice(arena, &std.mem.toBytes(std.mem.nativeToBig(u32, @intCast(new_glyf.items.len))));
+ if (keep[g]) {
+ try new_glyf.appendSlice(arena, glyf[offsets[g]..offsets[g + 1]]);
+ while (new_glyf.items.len % 4 != 0) try new_glyf.append(arena, 0);
+ }
+ }
+ try new_loca.appendSlice(arena, &std.mem.toBytes(std.mem.nativeToBig(u32, @intCast(new_glyf.items.len))));
+
+ // Unused advances zeroed for compression; the last full row stays
+ // because every glyph past it shares its advance.
+ const new_hmtx = try arena.dupe(u8, self.hmtx);
+ const num_h: usize = self.num_h_metrics;
+ for (0..n) |g| {
+ if (keep[g]) continue;
+ const range: ?struct { usize, usize } = if (g + 1 < num_h) .{ g * 4, g * 4 + 4 } else if (g >= num_h) .{ num_h * 4 + (g - num_h) * 2, num_h * 4 + (g - num_h) * 2 + 2 } else null;
+ if (range) |r| {
+ if (r[1] <= new_hmtx.len) @memset(new_hmtx[r[0]..r[1]], 0);
+ }
+ }
+
+ const new_head = try arena.dupe(u8, head);
+ std.mem.writeInt(u16, new_head[50..52], 1, .big);
+ @memset(new_head[8..12], 0);
+
+ const Table = struct {
+ tag: [4]u8,
+ data: []const u8,
+
+ fn lessThan(_: void, a: @This(), b: @This()) bool {
+ return std.mem.order(u8, &a.tag, &b.tag) == .lt;
+ }
+ };
+ var tables: std.ArrayList(Table) = .empty;
+ for ([_]*const [4]u8{ "cvt ", "fpgm", "prep", "hhea", "maxp" }) |tag| {
+ if (table(data, tag)) |t| try tables.append(arena, .{ .tag = tag.*, .data = t });
+ }
+ try tables.append(arena, .{ .tag = "glyf".*, .data = new_glyf.items });
+ try tables.append(arena, .{ .tag = "head".*, .data = new_head });
+ try tables.append(arena, .{ .tag = "hmtx".*, .data = new_hmtx });
+ try tables.append(arena, .{ .tag = "loca".*, .data = new_loca.items });
+ std.mem.sort(Table, tables.items, {}, Table.lessThan);
+
+ const count = tables.items.len;
+ const entry_selector: u4 = @intCast(std.math.log2_int(usize, count));
+ const search_range: u16 = @as(u16, 1) << entry_selector;
+ var out: std.Io.Writer.Allocating = .init(arena);
+ const w = &out.writer;
+ try w.writeInt(u32, 0x00010000, .big);
+ try w.writeInt(u16, @intCast(count), .big);
+ try w.writeInt(u16, search_range * 16, .big);
+ try w.writeInt(u16, entry_selector, .big);
+ try w.writeInt(u16, @as(u16, @intCast(count)) * 16 - search_range * 16, .big);
+
+ const dir_len = 12 + 16 * count;
+ var body: std.Io.Writer.Allocating = .init(arena);
+ var head_at: usize = 0;
+ for (tables.items) |t| {
+ const at = dir_len + body.written().len;
+ if (std.mem.eql(u8, &t.tag, "head")) head_at = at;
+ try w.writeAll(&t.tag);
+ try w.writeInt(u32, checksum(t.data), .big);
+ try w.writeInt(u32, @intCast(at), .big);
+ try w.writeInt(u32, @intCast(t.data.len), .big);
+ try body.writer.writeAll(t.data);
+ while (body.written().len % 4 != 0) try body.writer.writeByte(0);
+ }
+ try w.writeAll(body.written());
+ const file = out.written();
+ const adjust = 0xB1B0AFBA -% checksum(file);
+ std.mem.writeInt(u32, file[head_at + 8 ..][0..4], adjust, .big);
+ return file;
+ }
+};
+
+fn be16(d: []const u8, o: usize) u16 {
+ return std.mem.readInt(u16, d[o..][0..2], .big);
+}
+
+fn be32(d: []const u8, o: usize) u32 {
+ return std.mem.readInt(u32, d[o..][0..4], .big);
+}
+
+fn checksum(d: []const u8) u32 {
+ var sum: u32 = 0;
+ var i: usize = 0;
+ while (i < d.len) : (i += 4) {
+ var word: [4]u8 = .{ 0, 0, 0, 0 };
+ const n = @min(4, d.len - i);
+ @memcpy(word[0..n], d[i..][0..n]);
+ sum +%= std.mem.readInt(u32, &word, .big);
+ }
+ return sum;
+}
+
+// ---------------------------------------------------------------------------
+// The file itself: objects written in order with their offsets kept for the
+// xref. Ids come from `alloc` so an object can be referenced before it is
+// written (catalog, pages, resources).
+
+const Out = struct {
+ writer: *std.Io.Writer,
+ arena: Allocator,
+ offset: usize = 0,
+ xref: std.ArrayList(usize) = .empty,
+
+ fn write(self: *Out, bytes: []const u8) !void {
+ try self.writer.writeAll(bytes);
+ self.offset += bytes.len;
+ }
+
+ fn print(self: *Out, comptime fmt: []const u8, args: anytype) !void {
+ try self.write(try std.fmt.allocPrint(self.arena, fmt, args));
+ }
+
+ fn alloc(self: *Out) !usize {
+ try self.xref.append(self.arena, 0);
+ return self.xref.items.len;
+ }
+
+ fn begin(self: *Out, id: usize) !void {
+ self.xref.items[id - 1] = self.offset;
+ try self.print("{d} 0 obj\n", .{id});
+ }
+
+ fn end(self: *Out) !void {
+ try self.write("endobj\n");
+ }
+
+ /// A FlateDecode stream object; `extra` goes into its dictionary.
+ fn stream(self: *Out, extra: []const u8, data: []const u8) !usize {
+ const z = try deflate(self.arena, data);
+ const id = try self.alloc();
+ try self.begin(id);
+ try self.print("<< /Length {d} /Filter /FlateDecode {s} >>\nstream\n", .{ z.len, extra });
+ try self.write(z);
+ try self.write("\nendstream\n");
+ try self.end();
+ return id;
+ }
+};
+
+fn deflate(arena: Allocator, data: []const u8) ![]const u8 {
+ var aw: std.Io.Writer.Allocating = .init(arena);
+ try aw.ensureUnusedCapacity(256);
+ const window = try arena.alloc(u8, flate.max_window_len);
+ var c = try flate.Compress.init(&aw.writer, window, .zlib, .default);
+ try c.writer.writeAll(data);
+ try c.finish();
+ return aw.written();
+}
+
+const testing = @import("../testing.zig");
+test "browser.pdf: structure, pagination and links" {
+ defer testing.test_session.closeAllPages();
+ const frame = try testing.createFrame();
+ frame.url = "http://localhost/";
+ const doc = frame.window._document;
+ const div = try doc.createElement("div", null, frame);
+ try Frame.parse.htmlAsChildren(frame, div.asNode(), "<h1>Title</h1><p>Hello <b>world</b> <a href='/x'>link</a></p><pre>code</pre>");
+
+ var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
+ try print(testing.arena_allocator, div.asNode(), .{}, &aw.writer, frame);
+ const out = aw.written();
+ try testing.expectEqual("%PDF-1.4\n", out[0..9]);
+ try testing.expectEqual("%%EOF\n", out[out.len - 6 ..]);
+ try testing.expectEqual(1, std.mem.count(u8, out, "/Type /Page "));
+ // Letter in points, and the three faces the text used, each embedded once.
+ try testing.expectEqual(true, std.mem.indexOf(u8, out, "/MediaBox [0 0 612.000 792.000]") != null);
+ try testing.expectEqual(3, std.mem.count(u8, out, "/Subtype /Type0"));
+ try testing.expectEqual(3, std.mem.count(u8, out, "/FontFile2"));
+ try testing.expectEqual(true, std.mem.indexOf(u8, out, "/Encoding /Identity-H") != null);
+ // Well under the 2MB of bundled fonts: they were subset.
+ try testing.expectEqual(true, out.len < 120_000);
+ // The one link is a URI annotation on the page, resolved to absolute.
+ try testing.expectEqual(1, std.mem.count(u8, out, "/Subtype /Link"));
+ try testing.expectEqual(1, std.mem.count(u8, out, "/Annots ["));
+ const uri = "/URI <" ++ std.fmt.bytesToHex("http://localhost/x", .upper) ++ ">";
+ try testing.expectEqual(true, std.mem.indexOf(u8, out, uri) != null);
+
+ // 200 paragraphs don't fit one Letter page; a range then picks pages.
+ const long = try doc.createElement("div", null, frame);
+ var html: std.ArrayList(u8) = .empty;
+ for (0..200) |_| try html.appendSlice(testing.arena_allocator, "<p>A paragraph of text that takes up a line.</p>");
+ try Frame.parse.htmlAsChildren(frame, long.asNode(), html.items);
+
+ aw.clearRetainingCapacity();
+ try print(testing.arena_allocator, long.asNode(), .{}, &aw.writer, frame);
+ const pages = std.mem.count(u8, aw.written(), "/Type /Page ");
+ try testing.expectEqual(true, pages >= 5 and pages <= 8);
+
+ aw.clearRetainingCapacity();
+ try print(testing.arena_allocator, long.asNode(), .{ .page_ranges = try parsePageRanges(testing.arena_allocator, "2-3, 5") }, &aw.writer, frame);
+ try testing.expectEqual(3, std.mem.count(u8, aw.written(), "/Type /Page "));
+
+ // Document order, once each, the tail past the end ignored.
+ aw.clearRetainingCapacity();
+ try print(testing.arena_allocator, long.asNode(), .{ .page_ranges = try parsePageRanges(testing.arena_allocator, "5, 2-3, 3, 4000-") }, &aw.writer, frame);
+ try testing.expectEqual(3, std.mem.count(u8, aw.written(), "/Type /Page "));
+
+ // Halving the scale roughly halves the pages; landscape is the caller's
+ // swap, and margins shrink the content box.
+ aw.clearRetainingCapacity();
+ try print(testing.arena_allocator, long.asNode(), .{ .scale = 0.5 }, &aw.writer, frame);
+ const half = std.mem.count(u8, aw.written(), "/Type /Page ");
+ try testing.expectEqual(true, half < pages and half >= pages / 3);
+
+ aw.clearRetainingCapacity();
+ try print(testing.arena_allocator, long.asNode(), .{ .paper_width = 1056, .paper_height = 816, .margin_top = 300, .margin_bottom = 300 }, &aw.writer, frame);
+ try testing.expectEqual(true, std.mem.indexOf(u8, aw.written(), "/MediaBox [0 0 792.000 612.000]") != null);
+ try testing.expectEqual(true, std.mem.count(u8, aw.written(), "/Type /Page ") > pages);
+}
+
+test "browser.pdf: rejects bad options" {
+ defer testing.test_session.closeAllPages();
+ testing.silenceLog(&.{.browser});
+ const frame = try testing.createFrame();
+ frame.url = "http://localhost/";
+ const doc = frame.window._document;
+ const div = try doc.createElement("div", null, frame);
+ try Frame.parse.htmlAsChildren(frame, div.asNode(), "<p>hello</p>");
+
+ var discard: std.Io.Writer.Discarding = .init(&.{});
+ const w = &discard.writer;
+ const a = testing.arena_allocator;
+ try testing.expectError(error.InvalidPdfOptions, print(a, div.asNode(), .{ .scale = 3 }, w, frame));
+ try testing.expectError(error.InvalidPdfOptions, print(a, div.asNode(), .{ .paper_width = 0 }, w, frame));
+ try testing.expectError(error.InvalidPdfOptions, print(a, div.asNode(), .{ .margin_left = 500, .margin_right = 500 }, w, frame));
+ try testing.expectError(error.InvalidPdfOptions, print(a, div.asNode(), .{ .page_ranges = &.{.{ .from = 3, .to = 1 }} }, w, frame));
+ // Well-formed, but this is a one-page document.
+ try testing.expectError(error.PageRangeExceedsPageCount, print(a, div.asNode(), .{ .page_ranges = &.{.{ .from = 7, .to = 9 }} }, w, frame));
+
+ // Far too small for the file, so the sink refuses partway through.
+ var buf: [64]u8 = undefined;
+ var fixed = std.Io.Writer.fixed(&buf);
+ try testing.expectError(error.WriteFailed, print(a, div.asNode(), .{}, &fixed, frame));
+}
+
+test "browser.pdf: parsePageRanges follows the CDP grammar" {
+ const a = testing.arena_allocator;
+ const r = try parsePageRanges(a, "1-5, 8 ,11-13,,");
+ try testing.expectEqual(3, r.len);
+ try testing.expectEqual(PageRange{ .from = 1, .to = 5 }, r[0]);
+ try testing.expectEqual(PageRange{ .from = 8, .to = 8 }, r[1]);
+ try testing.expectEqual(PageRange{ .from = 11, .to = 13 }, r[2]);
+
+ try testing.expectEqual(0, (try parsePageRanges(a, "")).len);
+ try testing.expectEqual(0, (try parsePageRanges(a, " , ")).len);
+ try testing.expectEqual(PageRange{ .from = 1, .to = 5 }, (try parsePageRanges(a, "1 - 5"))[0]);
+ try testing.expectEqual(PageRange{ .from = 1, .to = 5 }, (try parsePageRanges(a, "-5"))[0]);
+ try testing.expectEqual(PageRange{ .from = 5, .to = std.math.maxInt(u32) }, (try parsePageRanges(a, "5-"))[0]);
+ try testing.expectEqual(PageRange{ .from = 1, .to = std.math.maxInt(u32) }, (try parsePageRanges(a, "-"))[0]);
+
+ try testing.expectError(error.InvalidPageRangeSyntax, parsePageRanges(a, "x"));
+ try testing.expectError(error.InvalidPageRangeSyntax, parsePageRanges(a, "1-2-3"));
+ try testing.expectError(error.InvalidPageRangeSyntax, parsePageRanges(a, "1.5"));
+ try testing.expectError(error.InvalidPageRangeSyntax, parsePageRanges(a, "99999999999"));
+ try testing.expectError(error.InvalidPageRange, parsePageRanges(a, "0"));
+ try testing.expectError(error.InvalidPageRange, parsePageRanges(a, "3-1"));
+}
diff --git a/src/browser/screenshot.zig b/src/browser/screenshot.zig
index 831373401..af0777c55 100644
--- a/src/browser/screenshot.zig
+++ b/src/browser/screenshot.zig
@@ -22,6 +22,7 @@ const lp = @import("lightpanda");
const Base64Writer = @import("../Base64Writer.zig");
const isAllWhitespace = @import("../string.zig").isAllWhitespace;
+const URL = @import("URL.zig");
const Frame = @import("Frame.zig");
const Viewport = @import("Viewport.zig");
const markdown = @import("markdown.zig");
@@ -64,7 +65,47 @@ pub const Renderer = opaque {
}
};
-fn rendererFor(frame: *Frame) !*Renderer {
+/// The block model laid out by the Rust side at scale 1, as flat arrays
+/// (see `LpLayout`). Lives on the Rust heap until `deinit`.
+pub const Layout = struct {
+ handle: *LayoutHandle,
+ raw: LpLayout,
+
+ pub fn deinit(self: *Layout) void {
+ lp_layout_free(self.handle);
+ }
+
+ pub fn blocks(self: *const Layout) []const LpLayoutBlock {
+ return self.raw.blocks[0..self.raw.blocks_len];
+ }
+ pub fn lines(self: *const Layout) []const LpLine {
+ return self.raw.lines[0..self.raw.lines_len];
+ }
+ pub fn runs(self: *const Layout) []const LpRun {
+ return self.raw.runs[0..self.raw.runs_len];
+ }
+ pub fn glyphs(self: *const Layout) []const LpGlyph {
+ return self.raw.glyphs[0..self.raw.glyphs_len];
+ }
+ pub fn clusters(self: *const Layout) []const LpCluster {
+ return self.raw.clusters[0..self.raw.clusters_len];
+ }
+ pub fn fonts(self: *const Layout) []const LpFont {
+ return self.raw.fonts[0..self.raw.fonts_len];
+ }
+};
+
+const LayoutHandle = opaque {};
+
+/// Flows `blocks` into a column `width` layout px wide with `margin` on
+/// every side.
+pub fn layout(renderer: *Renderer, blocks: []const LpBlock, width: f32, margin: f32) error{LayoutFailed}!Layout {
+ var raw: LpLayout = undefined;
+ const handle = lp_layout_new(renderer, blocks.ptr, blocks.len, width, margin, &raw) orelse return error.LayoutFailed;
+ return .{ .handle = handle, .raw = raw };
+}
+
+pub fn rendererFor(frame: *Frame) !*Renderer {
const browser = frame._session.browser;
if (browser.renderer) |r| {
return r;
@@ -75,34 +116,53 @@ fn rendererFor(frame: *Frame) !*Renderer {
}
pub fn png(arena: Allocator, node: *Node, opts: Opts, writer: *std.Io.Writer, frame: *Frame) !u32 {
- const prepared = try prepare(arena, node, opts, frame);
+ const prepared = try preparePng(arena, node, opts, frame);
return prepared.write(writer);
}
/// The height a render at `width` would have.
pub fn contentHeight(arena: Allocator, node: *Node, width: u32, frame: *Frame) !u32 {
- const prepared = try prepare(arena, node, .{ .width = width }, frame);
+ const prepared = try preparePng(arena, node, .{ .width = width }, frame);
return prepared.measure();
}
// The DOM walk, done up front so it can fail (allocation) before any output
// starts. Rasterizing is then a pure write: `Prepared` can be embedded in a
// std.json value and streams itself as a base64 string.
-pub fn prepare(arena: Allocator, node: *Node, opts: Opts, frame: *Frame) !Prepared {
+pub fn preparePng(arena: Allocator, node: *Node, opts: Opts, frame: *Frame) !Prepared {
if (opts.width == 0 or !(opts.scale > 0 and opts.scale <= 8)) {
return error.InvalidScreenshotOptions;
}
+ return .{
+ .opts = opts,
+ .blocks = try collect(arena, node, frame),
+ .renderer = try rendererFor(frame),
+ };
+}
+
+pub fn collect(arena: Allocator, node: *Node, frame: *Frame) ![]const LpBlock {
var builder: Builder = .{
.frame = frame,
.arena = arena,
};
try builder.render(node);
try builder.closeBlock();
- return .{
- .opts = opts,
- .blocks = builder.blocks.items,
- .renderer = try rendererFor(frame),
- };
+ return builder.blocks.items;
+}
+
+// Any non-zero rc means nothing usable was written, so the caller has to fail
+// rather than hand back a truncated file. WriteFailed is the only error
+// jsonStringify's signature can carry, hence the log line.
+fn renderFailed(what: []const u8, rc: i32) error{WriteFailed} {
+ log.err(.browser, what, .{ .reason = switch (rc) {
+ RC_WRITE_REFUSED => "write refused",
+ RC_INVALID => "invalid options",
+ RC_NO_RASTER => "raster allocation failed",
+ RC_ENCODE_FAILED => "png encoding failed",
+ RC_PANIC => "renderer panicked",
+ else => "unknown",
+ }, .rc = rc });
+ return error.WriteFailed;
}
pub const Prepared = struct {
@@ -149,19 +209,7 @@ pub const Prepared = struct {
if (rc == RC_OK) {
return content_height;
}
-
- // Any non-zero rc means nothing usable was written, so this has to
- // fail rather than hand back a truncated PNG. WriteFailed is the only
- // error jsonStringify's signature can carry, hence the log line.
- log.err(.browser, "screenshot render", .{ .reason = switch (rc) {
- RC_WRITE_REFUSED => "write refused",
- RC_INVALID => "invalid options",
- RC_NO_RASTER => "raster allocation failed",
- RC_ENCODE_FAILED => "png encoding failed",
- RC_PANIC => "renderer panicked",
- else => "unknown",
- }, .rc = rc });
- return error.WriteFailed;
+ return renderFailed("screenshot render", rc);
}
// Serializes as a base64 string, streamed: no PNG or base64 buffer.
@@ -198,14 +246,16 @@ const Sink = struct {
};
// Mirrors the C ABI in src/rust/render/lib.rs.
-const LpSpan = extern struct {
+pub const LpSpan = extern struct {
text: [*]const u8,
len: usize,
flags: u32,
color: u32,
+ href: [*]const u8,
+ href_len: usize,
};
-const LpBlock = extern struct {
+pub const LpBlock = extern struct {
spans: [*]const LpSpan,
spans_len: usize,
marker: [*]const u8,
@@ -216,7 +266,7 @@ const LpBlock = extern struct {
quote_depth: u8,
flags: u8,
- const Kind = enum(u8) {
+ pub const Kind = enum(u8) {
paragraph = 0,
heading = 1,
pre = 2,
@@ -246,6 +296,102 @@ const BLOCK_TIGHT: u8 = 1 << 0;
const RENDER_MEASURE_ONLY: u32 = 1 << 0;
+// The laid-out document `lp_layout_new` exports, in layout px: blocks own a
+// range of lines, lines a range of runs and clusters, runs a range of
+// glyphs. Consumed by pdf.zig; see the Rust definitions for field docs.
+pub const LpLayout = extern struct {
+ height: f32,
+ pre_pad: f32,
+ quote_indent: f32,
+ rule_color: u32,
+ pre_bg: u32,
+ blocks: [*]const LpLayoutBlock,
+ blocks_len: usize,
+ lines: [*]const LpLine,
+ lines_len: usize,
+ runs: [*]const LpRun,
+ runs_len: usize,
+ glyphs: [*]const LpGlyph,
+ glyphs_len: usize,
+ clusters: [*]const LpCluster,
+ clusters_len: usize,
+ fonts: [*]const LpFont,
+ fonts_len: usize,
+};
+
+pub const LAYOUT_NONE: u32 = std.math.maxInt(u32);
+
+pub const LpLayoutBlock = extern struct {
+ x: f32,
+ y: f32,
+ quote_x: f32,
+ lines: u32,
+ lines_len: u32,
+ marker_line: u32,
+ kind: u8,
+ quote_bars: u8,
+};
+
+pub const LpLine = extern struct {
+ x: f32,
+ top: f32,
+ bottom: f32,
+ runs: u32,
+ runs_len: u32,
+ clusters: u32,
+ clusters_len: u32,
+};
+
+pub const LpRun = extern struct {
+ font: u32,
+ size: f32,
+ skew: f32,
+ color: u32,
+ offset: f32,
+ baseline: f32,
+ advance: f32,
+ glyphs: u32,
+ glyphs_len: u32,
+ underline: LpDecoration,
+ strike: LpDecoration,
+};
+
+pub const LpDecoration = extern struct {
+ enabled: u32,
+ offset: f32,
+ size: f32,
+ color: u32,
+};
+
+pub const LpGlyph = extern struct {
+ id: u32,
+ x: f32,
+ y: f32,
+ advance: f32,
+};
+
+pub const LpCluster = extern struct {
+ font: u32,
+ glyph: u32,
+ text_start: u32,
+ text_len: u32,
+ x: f32,
+ advance: f32,
+ flags: u32,
+};
+
+pub const CLUSTER_LIGATURE_START: u32 = 1 << 0;
+pub const CLUSTER_LIGATURE_CONT: u32 = 1 << 1;
+
+pub const LpFont = extern struct {
+ data: [*]const u8,
+ data_len: usize,
+ index: u32,
+ name: [*]const u8,
+ name_len: usize,
+ mono: u8,
+};
+
const RC_OK: i32 = 0;
const RC_WRITE_REFUSED: i32 = 1;
const RC_INVALID: i32 = 2;
@@ -263,6 +409,8 @@ const LpAbi = extern struct {
span_len: u32,
span_flags: u32,
span_color: u32,
+ span_href: u32,
+ span_href_len: u32,
block_size: u32,
block_align: u32,
@@ -287,6 +435,89 @@ const LpAbi = extern struct {
opts_scale: u32,
opts_flags: u32,
+ layout_sizeof: u32,
+ layout_alignof: u32,
+ layout_height: u32,
+ layout_pre_pad: u32,
+ layout_quote_indent: u32,
+ layout_rule_color: u32,
+ layout_pre_bg: u32,
+ layout_blocks: u32,
+ layout_blocks_len: u32,
+ layout_lines: u32,
+ layout_lines_len: u32,
+ layout_runs: u32,
+ layout_runs_len: u32,
+ layout_glyphs: u32,
+ layout_glyphs_len: u32,
+ layout_clusters: u32,
+ layout_clusters_len: u32,
+ layout_fonts: u32,
+ layout_fonts_len: u32,
+ lblock_sizeof: u32,
+ lblock_alignof: u32,
+ lblock_x: u32,
+ lblock_y: u32,
+ lblock_quote_x: u32,
+ lblock_lines: u32,
+ lblock_lines_len: u32,
+ lblock_marker_line: u32,
+ lblock_kind: u32,
+ lblock_quote_bars: u32,
+ line_sizeof: u32,
+ line_alignof: u32,
+ line_x: u32,
+ line_top: u32,
+ line_bottom: u32,
+ line_runs: u32,
+ line_runs_len: u32,
+ line_clusters: u32,
+ line_clusters_len: u32,
+ run_sizeof: u32,
+ run_alignof: u32,
+ run_font: u32,
+ run_size: u32,
+ run_skew: u32,
+ run_color: u32,
+ run_offset: u32,
+ run_baseline: u32,
+ run_advance: u32,
+ run_glyphs: u32,
+ run_glyphs_len: u32,
+ run_underline: u32,
+ run_strike: u32,
+ deco_sizeof: u32,
+ deco_alignof: u32,
+ deco_enabled: u32,
+ deco_offset: u32,
+ deco_size: u32,
+ deco_color: u32,
+ glyph_sizeof: u32,
+ glyph_alignof: u32,
+ glyph_id: u32,
+ glyph_x: u32,
+ glyph_y: u32,
+ glyph_advance: u32,
+ cluster_sizeof: u32,
+ cluster_alignof: u32,
+ cluster_font: u32,
+ cluster_glyph: u32,
+ cluster_text_start: u32,
+ cluster_text_len: u32,
+ cluster_x: u32,
+ cluster_advance: u32,
+ cluster_flags: u32,
+ font_sizeof: u32,
+ font_alignof: u32,
+ font_data: u32,
+ font_data_len: u32,
+ font_index: u32,
+ font_name: u32,
+ font_name_len: u32,
+ font_mono: u32,
+ layout_none: u32,
+ cluster_ligature_start: u32,
+ cluster_ligature_cont: u32,
span_bold: u32,
span_italic: u32,
span_underline: u32,
@@ -323,6 +554,15 @@ extern "c" fn lp_render_png(
ctx: *anyopaque,
write: *const fn (ctx: *anyopaque, data: [*]const u8, len: usize) callconv(.c) bool,
) i32;
+extern "c" fn lp_layout_new(
+ r: *Renderer,
+ blocks: [*]const LpBlock,
+ blocks_len: usize,
+ width: f32,
+ margin: f32,
+ out: *LpLayout,
+) ?*LayoutHandle;
+extern "c" fn lp_layout_free(h: *LayoutHandle) void;
extern "c" fn lp_render_abi(out: *LpAbi) void;
const Builder = struct {
@@ -341,6 +581,7 @@ const Builder = struct {
text: std.ArrayList(u8) = .empty,
text_flags: u32 = 0,
text_color: u32 = 0,
+ text_href: []const u8 = "",
has_content: bool = false,
pending_space: bool = false,
// Nothing appended since an <a> closed. Adjacent links with no whitespace
@@ -357,6 +598,8 @@ const Builder = struct {
mono: u8 = 0,
strike: u8 = 0,
link: u8 = 0,
+ // Resolved href of the nearest enclosing <a>; "" outside one.
+ href: []const u8 = "",
muted: u8 = 0,
// Nearest enclosing <pre>, for whitespace preservation.
pre_node: ?*Node = null,
@@ -434,6 +677,8 @@ const Builder = struct {
.len = copy.len,
.flags = self.text_flags,
.color = self.text_color,
+ .href = self.text_href.ptr,
+ .href_len = self.text_href.len,
});
self.text.clearRetainingCapacity();
}
@@ -471,11 +716,12 @@ const Builder = struct {
try self.ensureBlock();
const flags = self.currentFlags();
const color = self.currentColor();
- if (self.text.items.len > 0 and (flags != self.text_flags or color != self.text_color)) {
+ if (self.text.items.len > 0 and (flags != self.text_flags or color != self.text_color or !std.mem.eql(u8, self.href, self.text_href))) {
try self.flushSpan();
}
self.text_flags = flags;
self.text_color = color;
+ self.text_href = self.href;
try self.text.appendSlice(self.arena, text);
self.has_content = true;
self.after_anchor = false;
@@ -498,11 +744,15 @@ const Builder = struct {
flags &= ~SPAN_HAS_COLOR;
}
if (flags & SPAN_HAS_COLOR == 0) color = 0;
+ // Same for the link: the gap is part of it only when both sides are
+ // the same link, so a hit box never bridges two.
+ const href = if (std.mem.eql(u8, self.text_href, self.href)) self.text_href else "";
- if (flags != self.text_flags or color != self.text_color) {
+ if (flags != self.text_flags or color != self.text_color or !std.mem.eql(u8, href, self.text_href)) {
try self.flushSpan();
self.text_flags = flags;
self.text_color = color;
+ self.text_href = href;
}
try self.text.append(self.arena, ' ');
}
@@ -687,13 +937,22 @@ const Builder = struct {
self.tight += 1;
}
if (self.after_anchor) self.pending_space = true;
- if (href != null) self.link += 1;
+ const prev_href = self.href;
+ if (href) |h| {
+ self.link += 1;
+ // Absolute, like markdown: the PDF's link annotations
+ // point outside the document.
+ self.href = URL.resolve(self.arena, self.frame.base(), h, .{ .encoding = self.frame.charset }) catch h;
+ }
if (info.has_visible) {
try self.renderContent(el);
} else {
try self.renderText(label.?);
}
- if (href != null) self.link -= 1;
+ if (href != null) {
+ self.link -= 1;
+ self.href = prev_href;
+ }
if (standalone) {
try self.closeBlock();
self.tight -= 1;
@@ -773,6 +1032,8 @@ test "browser.screenshot: rust abi matches" {
.span_len = @offsetOf(LpSpan, "len"),
.span_flags = @offsetOf(LpSpan, "flags"),
.span_color = @offsetOf(LpSpan, "color"),
+ .span_href = @offsetOf(LpSpan, "href"),
+ .span_href_len = @offsetOf(LpSpan, "href_len"),
.block_size = @sizeOf(LpBlock),
.block_align = @alignOf(LpBlock),
@@ -797,6 +1058,89 @@ test "browser.screenshot: rust abi matches" {
.opts_scale = @offsetOf(LpRenderOpts, "scale"),
.opts_flags = @offsetOf(LpRenderOpts, "flags"),
+ .layout_sizeof = @sizeOf(LpLayout),
+ .layout_alignof = @alignOf(LpLayout),
+ .layout_height = @offsetOf(LpLayout, "height"),
+ .layout_pre_pad = @offsetOf(LpLayout, "pre_pad"),
+ .layout_quote_indent = @offsetOf(LpLayout, "quote_indent"),
+ .layout_rule_color = @offsetOf(LpLayout, "rule_color"),
+ .layout_pre_bg = @offsetOf(LpLayout, "pre_bg"),
+ .layout_blocks = @offsetOf(LpLayout, "blocks"),
+ .layout_blocks_len = @offsetOf(LpLayout, "blocks_len"),
+ .layout_lines = @offsetOf(LpLayout, "lines"),
+ .layout_lines_len = @offsetOf(LpLayout, "lines_len"),
+ .layout_runs = @offsetOf(LpLayout, "runs"),
+ .layout_runs_len = @offsetOf(LpLayout, "runs_len"),
+ .layout_glyphs = @offsetOf(LpLayout, "glyphs"),
+ .layout_glyphs_len = @offsetOf(LpLayout, "glyphs_len"),
+ .layout_clusters = @offsetOf(LpLayout, "clusters"),
+ .layout_clusters_len = @offsetOf(LpLayout, "clusters_len"),
+ .layout_fonts = @offsetOf(LpLayout, "fonts"),
+ .layout_fonts_len = @offsetOf(LpLayout, "fonts_len"),
+ .lblock_sizeof = @sizeOf(LpLayoutBlock),
+ .lblock_alignof = @alignOf(LpLayoutBlock),
+ .lblock_x = @offsetOf(LpLayoutBlock, "x"),
+ .lblock_y = @offsetOf(LpLayoutBlock, "y"),
+ .lblock_quote_x = @offsetOf(LpLayoutBlock, "quote_x"),
+ .lblock_lines = @offsetOf(LpLayoutBlock, "lines"),
+ .lblock_lines_len = @offsetOf(LpLayoutBlock, "lines_len"),
+ .lblock_marker_line = @offsetOf(LpLayoutBlock, "marker_line"),
+ .lblock_kind = @offsetOf(LpLayoutBlock, "kind"),
+ .lblock_quote_bars = @offsetOf(LpLayoutBlock, "quote_bars"),
+ .line_sizeof = @sizeOf(LpLine),
+ .line_alignof = @alignOf(LpLine),
+ .line_x = @offsetOf(LpLine, "x"),
+ .line_top = @offsetOf(LpLine, "top"),
+ .line_bottom = @offsetOf(LpLine, "bottom"),
+ .line_runs = @offsetOf(LpLine, "runs"),
+ .line_runs_len = @offsetOf(LpLine, "runs_len"),
+ .line_clusters = @offsetOf(LpLine, "clusters"),
+ .line_clusters_len = @offsetOf(LpLine, "clusters_len"),
+ .run_sizeof = @sizeOf(LpRun),
+ .run_alignof = @alignOf(LpRun),
+ .run_font = @offsetOf(LpRun, "font"),
+ .run_size = @offsetOf(LpRun, "size"),
+ .run_skew = @offsetOf(LpRun, "skew"),
+ .run_color = @offsetOf(LpRun, "color"),
+ .run_offset = @offsetOf(LpRun, "offset"),
+ .run_baseline = @offsetOf(LpRun, "baseline"),
+ .run_advance = @offsetOf(LpRun, "advance"),
+ .run_glyphs = @offsetOf(LpRun, "glyphs"),
+ .run_glyphs_len = @offsetOf(LpRun, "glyphs_len"),
+ .run_underline = @offsetOf(LpRun, "underline"),
+ .run_strike = @offsetOf(LpRun, "strike"),
+ .deco_sizeof = @sizeOf(LpDecoration),
+ .deco_alignof = @alignOf(LpDecoration),
+ .deco_enabled = @offsetOf(LpDecoration, "enabled"),
+ .deco_offset = @offsetOf(LpDecoration, "offset"),
+ .deco_size = @offsetOf(LpDecoration, "size"),
+ .deco_color = @offsetOf(LpDecoration, "color"),
+ .glyph_sizeof = @sizeOf(LpGlyph),
+ .glyph_alignof = @alignOf(LpGlyph),
+ .glyph_id = @offsetOf(LpGlyph, "id"),
+ .glyph_x = @offsetOf(LpGlyph, "x"),
+ .glyph_y = @offsetOf(LpGlyph, "y"),
+ .glyph_advance = @offsetOf(LpGlyph, "advance"),
+ .cluster_sizeof = @sizeOf(LpCluster),
+ .cluster_alignof = @alignOf(LpCluster),
+ .cluster_font = @offsetOf(LpCluster, "font"),
+ .cluster_glyph = @offsetOf(LpCluster, "glyph"),
+ .cluster_text_start = @offsetOf(LpCluster, "text_start"),
+ .cluster_text_len = @offsetOf(LpCluster, "text_len"),
+ .cluster_x = @offsetOf(LpCluster, "x"),
+ .cluster_advance = @offsetOf(LpCluster, "advance"),
+ .cluster_flags = @offsetOf(LpCluster, "flags"),
+ .font_sizeof = @sizeOf(LpFont),
+ .font_alignof = @alignOf(LpFont),
+ .font_data = @offsetOf(LpFont, "data"),
+ .font_data_len = @offsetOf(LpFont, "data_len"),
+ .font_index = @offsetOf(LpFont, "index"),
+ .font_name = @offsetOf(LpFont, "name"),
+ .font_name_len = @offsetOf(LpFont, "name_len"),
+ .font_mono = @offsetOf(LpFont, "mono"),
+ .layout_none = LAYOUT_NONE,
+ .cluster_ligature_start = CLUSTER_LIGATURE_START,
+ .cluster_ligature_cont = CLUSTER_LIGATURE_CONT,
.span_bold = SPAN_BOLD,
.span_italic = SPAN_ITALIC,
.span_underline = SPAN_UNDERLINE,
@@ -943,7 +1287,7 @@ test "browser.screenshot: json streams base64" {
const div = try doc.createElement("div", null, frame);
try Frame.parse.htmlAsChildren(frame, div.asNode(), "<p>hello</p>");
- const prepared = try prepare(testing.arena_allocator, div.asNode(), .{ .width = 200 }, frame);
+ const prepared = try preparePng(testing.arena_allocator, div.asNode(), .{ .width = 200 }, frame);
var raw: std.Io.Writer.Allocating = .init(testing.allocator);
defer raw.deinit();
@@ -1015,7 +1359,10 @@ test "browser.screenshot: block extraction" {
try testing.expectEqual(SPAN_UNDERLINE | SPAN_HAS_COLOR, blocks[1].spans[4].flags);
try testing.expectEqual(LINK_COLOR, blocks[1].spans[4].color);
try testing.expectEqual("a link", blocks[1].spans[4].text[0..blocks[1].spans[4].len]);
+ try testing.expectEqual("http://localhost/l", blocks[1].spans[4].href[0..blocks[1].spans[4].href_len]);
+ try testing.expectEqual(0, blocks[1].spans[3].href_len);
try testing.expectEqual(" tail", blocks[1].spans[5].text[0..blocks[1].spans[5].len]);
+ try testing.expectEqual(0, blocks[1].spans[5].href_len);
try testing.expectEqual("one", try S.text(blocks[2], arena));
try testing.expectEqual(1, blocks[2].list_depth);
diff --git a/src/browser/tools.zig b/src/browser/tools.zig
index 55b4444fe..8e812034f 100644
--- a/src/browser/tools.zig
+++ b/src/browser/tools.zig
@@ -1365,7 +1365,7 @@ fn execScreenshot(arena: std.mem.Allocator, session: *lp.Session, registry: *CDP
}
const page = try ensurePage(session, registry, args.url, args.timeout);
const node = try resolveScope(session, registry, page, args.selector, args.backendNodeId);
- var prepared = lp.screenshot.prepare(arena, node, .fromViewport(page._page.getViewport(), args.fullPage), page) catch
+ var prepared = lp.screenshot.preparePng(arena, node, .fromViewport(page._page.getViewport(), args.fullPage), page) catch
return ToolError.InternalError;
if (args.path) |path| {
diff --git a/src/cdp/CDP.zig b/src/cdp/CDP.zig
index 2e955cb78..759ebbf45 100644
--- a/src/cdp/CDP.zig
+++ b/src/cdp/CDP.zig
@@ -96,6 +96,9 @@ frame_arena: std.heap.ArenaAllocator,
// (or altogether eliminate) our use of this.
browser_context_arena: std.heap.ArenaAllocator,
+// Files handed out as IO stream handles (Page.printToPDF ReturnAsStream).
+streams: @import("domains/io.zig").Streams,
+
pub fn init(
self: *CDP,
app: *App,
@@ -115,6 +118,7 @@ pub fn init(
.message_arena = std.heap.ArenaAllocator.init(allocator),
.notification_arena = std.heap.ArenaAllocator.init(allocator),
.browser_context_arena = std.heap.ArenaAllocator.init(allocator),
+ .streams = .{ .allocator = allocator },
};
try self.browser.init(app, .{ .env = .{ .with_inspector = true } }, self);
@@ -140,6 +144,7 @@ pub fn deinit(self: *CDP) void {
self.message_arena.deinit();
self.notification_arena.deinit();
self.browser_context_arena.deinit();
+ self.streams.deinit();
self.conn.deinit();
}
// Called by the Server run loop when readable bytes arrive on the CDP
@@ -384,6 +389,7 @@ fn dispatchCommand(command: *Command, method: []const u8) !void {
switch (domain.len) {
2 => switch (@as(u16, @bitCast(domain[0..2].*))) {
asUint(u16, "LP") => return @import("domains/lp.zig").processMessage(command),
+ asUint(u16, "IO") => return @import("domains/io.zig").processMessage(command),
else => {},
},
3 => switch (@as(u24, @bitCast(domain[0..3].*))) {
diff --git a/src/cdp/domains/io.zig b/src/cdp/domains/io.zig
new file mode 100644
index 000000000..113efecb8
--- /dev/null
+++ b/src/cdp/domains/io.zig
@@ -0,0 +1,134 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// The IO domain: reading back a file a command produced with
+// `transferMode: "ReturnAsStream"` (Page.printToPDF). Drivers read until
+// `eof` and then close.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const CDP = @import("../CDP.zig");
+
+const log = lp.log;
+const Allocator = std.mem.Allocator;
+
+pub fn processMessage(cmd: *CDP.Command) !void {
+ const action = std.meta.stringToEnum(enum {
+ read,
+ close,
+ }, cmd.input.action) orelse return error.UnknownMethod;
+
+ switch (action) {
+ .read => return read(cmd),
+ .close => return close(cmd),
+ }
+}
+
+// Whole files held per connection, keyed by the handle handed to the client.
+pub const Streams = struct {
+ allocator: Allocator,
+ next_handle: u32 = 1,
+ map: std.AutoHashMapUnmanaged(u32, Stream) = .empty,
+
+ const Stream = struct {
+ data: []const u8,
+ position: usize = 0, // position of the next read
+ };
+
+ // Takes ownership of `data`, which must have come from `self.allocator`.
+ pub fn add(self: *Streams, data: []const u8) !u32 {
+ const handle = self.next_handle;
+ try self.map.put(self.allocator, handle, .{ .data = data });
+ self.next_handle = handle + 1;
+ return handle;
+ }
+
+ pub fn deinit(self: *Streams) void {
+ var it = self.map.valueIterator();
+ while (it.next()) |s| {
+ self.allocator.free(s.data);
+ }
+ self.map.deinit(self.allocator);
+ }
+};
+
+const DEFAULT_CHUNK: usize = 1048576;
+
+fn read(cmd: *CDP.Command) !void {
+ const params = (try cmd.params(struct {
+ handle: []const u8,
+ offset: ?usize = null,
+ size: ?usize = null,
+ })) orelse return error.InvalidParams;
+
+ const stream = lookup(cmd, params.handle) orelse {
+ return cmd.sendError(-32000, "Invalid stream handle", .{});
+ };
+ const start = @min(params.offset orelse stream.position, stream.data.len);
+ const end = @min(start + (params.size orelse DEFAULT_CHUNK), stream.data.len);
+ stream.position = end;
+
+ const chunk = stream.data[start..end];
+ const encoder = std.base64.standard.Encoder;
+ const encoded = try cmd.arena.alloc(u8, encoder.calcSize(chunk.len));
+ _ = encoder.encode(encoded, chunk);
+ return cmd.sendResult(.{
+ .data = encoded,
+ .base64Encoded = true,
+ .eof = end == stream.data.len,
+ }, .{});
+}
+
+fn close(cmd: *CDP.Command) !void {
+ const params = (try cmd.params(struct {
+ handle: []const u8,
+ })) orelse return error.InvalidParams;
+
+ const handle = parseHandle(params.handle) orelse {
+ return cmd.sendError(-32000, "Invalid stream handle", .{});
+ };
+ const streams = &cmd.cdp.streams;
+ const entry = streams.map.fetchRemove(handle) orelse {
+ return cmd.sendError(-32000, "Invalid stream handle", .{});
+ };
+ streams.allocator.free(entry.value.data);
+ return cmd.sendResult(null, .{});
+}
+
+fn lookup(cmd: *CDP.Command, handle: []const u8) ?*Streams.Stream {
+ const key = parseHandle(handle) orelse return null;
+ return cmd.cdp.streams.map.getPtr(key);
+}
+
+fn parseHandle(handle: []const u8) ?u32 {
+ return std.fmt.parseInt(u32, handle, 10) catch null;
+}
+
+// Exercised end to end by "cdp.frame: printToPDF" in page.zig.
+const testing = @import("../testing.zig");
+test "cdp.io: unknown handles" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ try ctx.processMessage(.{ .id = 1, .method = "IO.read", .params = .{ .handle = "42" } });
+ try ctx.expectSentError(-32000, "Invalid stream handle", .{ .id = 1 });
+
+ try ctx.processMessage(.{ .id = 2, .method = "IO.close", .params = .{ .handle = "nope" } });
+ try ctx.expectSentError(-32000, "Invalid stream handle", .{ .id = 2 });
+}
diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig
index 0dab93e82..98d52b245 100644
--- a/src/cdp/domains/page.zig
+++ b/src/cdp/domains/page.zig
@@ -20,8 +20,6 @@
const std = @import("std");
const lp = @import("lightpanda");
-const screenshot_pdf = @embedFile("screenshot.pdf");
-
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
@@ -962,13 +960,6 @@ const Viewport = struct {
scale: f64,
};
-fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSize(input.len)]u8 {
- const encoder = std.base64.standard.Encoder;
- var buf: [encoder.calcSize(input.len)]u8 = undefined;
- _ = encoder.encode(&buf, input);
- return buf;
-}
-
fn captureScreenshot(cmd: *CDP.Command) !void {
const Params = struct {
format: ?[]const u8 = "png",
@@ -1005,15 +996,69 @@ fn captureScreenshot(cmd: *CDP.Command) !void {
}
// Prepared streams itself as base64 straight into the outgoing message.
- const shot = try lp.screenshot.prepare(cmd.arena, frame.window._document.asNode(), opts, frame);
+ const shot = try lp.screenshot.preparePng(cmd.arena, frame.window._document.asNode(), opts, frame);
return cmd.sendResult(.{ .data = shot }, .{});
}
-// Return a fake pdf
fn printToPDF(cmd: *CDP.Command) !void {
- // Ignore all parameters.
+ // Lengths in inches; Chrome's defaults.
+ const Params = struct {
+ landscape: bool = false,
+ displayHeaderFooter: bool = false,
+ printBackground: bool = false,
+ scale: f32 = 1,
+ paperWidth: f32 = 8.5,
+ paperHeight: f32 = 11,
+ marginTop: f32 = 0.4,
+ marginBottom: f32 = 0.4,
+ marginLeft: f32 = 0.4,
+ marginRight: f32 = 0.4,
+ pageRanges: []const u8 = "",
+ transferMode: enum { ReturnAsBase64, ReturnAsStream } = .ReturnAsBase64,
+ };
+ const params = try cmd.params(Params) orelse Params{};
+ if (params.displayHeaderFooter) {
+ log.warn(.not_implemented, "Page.printToPDF params", .{ .displayHeaderFooter = true });
+ }
+
+ const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
+ const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
+
+ const paper_w = if (params.landscape) params.paperHeight else params.paperWidth;
+ const paper_h = if (params.landscape) params.paperWidth else params.paperHeight;
+ const opts: lp.pdf.Opts = .{
+ .paper_width = paper_w * 96,
+ .paper_height = paper_h * 96,
+ .margin_top = params.marginTop * 96,
+ .margin_right = params.marginRight * 96,
+ .margin_bottom = params.marginBottom * 96,
+ .margin_left = params.marginLeft * 96,
+ .scale = params.scale,
+ .print_background = params.printBackground,
+ .page_ranges = lp.pdf.parsePageRanges(cmd.arena, params.pageRanges) catch |err| switch (err) {
+ error.InvalidPageRangeSyntax => return cmd.sendError(-32000, "Invalid page range syntax", .{}),
+ error.InvalidPageRange => return cmd.sendError(-32000, "Invalid page range", .{}),
+ error.OutOfMemory => return error.OutOfMemory,
+ },
+ };
+ const prepared = lp.pdf.prepare(cmd.arena, frame.window._document.asNode(), opts, frame) catch |err| switch (err) {
+ error.InvalidPdfOptions => return cmd.sendError(-32602, "invalid print parameters", .{}),
+ error.PageRangeExceedsPageCount => return cmd.sendError(-32000, "Page range exceeds page count", .{}),
+ else => return err,
+ };
+
+ if (params.transferMode == .ReturnAsBase64) {
+ return cmd.sendResult(.{ .data = prepared }, .{});
+ }
+
+ // Read via IO.read
+ var aw: std.Io.Writer.Allocating = .init(cmd.cdp.allocator);
+ errdefer aw.deinit();
+ try prepared.write(&aw.writer);
+ const handle = try cmd.cdp.streams.add(try aw.toOwnedSlice());
return cmd.sendResult(.{
- .data = base64Encode(screenshot_pdf),
+ .data = "",
+ .stream = try std.fmt.allocPrint(cmd.arena, "{d}", .{handle}),
}, .{});
}
@@ -1360,12 +1405,94 @@ test "cdp.frame: printToPDF" {
var ctx = try testing.context();
defer ctx.deinit();
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
+
{
- try ctx.processMessage(.{ .id = 10, .method = "Page.printToPDF", .params = .{ .landscape = true } });
- try ctx.expectSentResult(.{
- .data = base64Encode(screenshot_pdf),
- }, .{ .id = 10 });
+ try ctx.processMessage(.{ .id = 10, .method = "Page.printToPDF", .params = .{ .scale = 5 } });
+ try ctx.expectSentError(-32602, "invalid print parameters", .{ .id = 10 });
+
+ // Chrome's three page-range errors.
+ try ctx.processMessage(.{ .id = 20, .method = "Page.printToPDF", .params = .{ .pageRanges = "x" } });
+ try ctx.expectSentError(-32000, "Invalid page range syntax", .{ .id = 20 });
+ try ctx.processMessage(.{ .id = 21, .method = "Page.printToPDF", .params = .{ .pageRanges = "3-1" } });
+ try ctx.expectSentError(-32000, "Invalid page range", .{ .id = 21 });
+ try ctx.processMessage(.{ .id = 22, .method = "Page.printToPDF", .params = .{ .pageRanges = "9" } });
+ try ctx.expectSentError(-32000, "Page range exceeds page count", .{ .id = 22 });
}
+
+ {
+ // Inline base64, landscape: Letter swapped, in points.
+ try ctx.processMessage(.{ .id = 11, .method = "Page.printToPDF", .params = .{ .landscape = true } });
+ const pdf = try pdfResult(&ctx, 11);
+ try testing.expectEqual(true, std.mem.indexOf(u8, pdf, "/MediaBox [0 0 792.000 612.000]") != null);
+ }
+
+ {
+ // The stream mode drivers use: a handle, then IO.read until eof.
+ try ctx.processMessage(.{ .id = 12, .method = "Page.printToPDF", .params = .{ .transferMode = "ReturnAsStream" } });
+ const handle = try streamHandle(&ctx, 12);
+
+ try ctx.processMessage(.{ .id = 13, .method = "IO.read", .params = .{ .handle = handle, .size = 16 } });
+ const head = try ioReadResult(&ctx, 13, false);
+ try testing.expectEqual("%PDF-1.4\n", head[0..9]);
+
+ try ctx.processMessage(.{ .id = 14, .method = "IO.read", .params = .{ .handle = handle } });
+ const rest = try ioReadResult(&ctx, 14, true);
+ try testing.expectEqual("%%EOF\n", rest[rest.len - 6 ..]);
+
+ try ctx.processMessage(.{ .id = 15, .method = "IO.close", .params = .{ .handle = handle } });
+ try ctx.expectSentResult(null, .{ .id = 15 });
+
+ try ctx.processMessage(.{ .id = 16, .method = "IO.read", .params = .{ .handle = handle } });
+ try ctx.expectSentError(-32000, "Invalid stream handle", .{ .id = 16 });
+ }
+}
+
+fn pdfResult(ctx: *testing.TestContext, msg_id: i64) ![]const u8 {
+ var i: usize = 0;
+ while (try ctx.getSentMessage(i)) |msg| : (i += 1) {
+ const obj = msg.object;
+ const got = obj.get("id") orelse continue;
+ if (got != .integer or got.integer != msg_id) continue;
+ const data = obj.get("result").?.object.get("data").?.string;
+ const decoder = std.base64.standard.Decoder;
+ const out = try testing.arena_allocator.alloc(u8, try decoder.calcSizeForSlice(data));
+ try decoder.decode(out, data);
+ try testing.expectEqual("%PDF-1.4\n", out[0..9]);
+ return out;
+ }
+ return error.ResultNotFound;
+}
+
+fn streamHandle(ctx: *testing.TestContext, msg_id: i64) ![]const u8 {
+ var i: usize = 0;
+ while (try ctx.getSentMessage(i)) |msg| : (i += 1) {
+ const obj = msg.object;
+ const got = obj.get("id") orelse continue;
+ if (got != .integer or got.integer != msg_id) continue;
+ const result = obj.get("result").?.object;
+ try testing.expectEqual("", result.get("data").?.string);
+ return try testing.arena_allocator.dupe(u8, result.get("stream").?.string);
+ }
+ return error.ResultNotFound;
+}
+
+fn ioReadResult(ctx: *testing.TestContext, msg_id: i64, expect_eof: bool) ![]const u8 {
+ var i: usize = 0;
+ while (try ctx.getSentMessage(i)) |msg| : (i += 1) {
+ const obj = msg.object;
+ const got = obj.get("id") orelse continue;
+ if (got != .integer or got.integer != msg_id) continue;
+ const result = obj.get("result").?.object;
+ try testing.expectEqual(true, result.get("base64Encoded").?.bool);
+ try testing.expectEqual(expect_eof, result.get("eof").?.bool);
+ const data = result.get("data").?.string;
+ const decoder = std.base64.standard.Decoder;
+ const out = try testing.arena_allocator.alloc(u8, try decoder.calcSizeForSlice(data));
+ try decoder.decode(out, data);
+ return out;
+ }
+ return error.ResultNotFound;
}
test "cdp.frame: getLayoutMetrics" {
diff --git a/src/cdp/domains/screenshot.pdf b/src/cdp/domains/screenshot.pdf
deleted file mode 100644
index c61fecbf3..000000000
--- a/src/cdp/domains/screenshot.pdf
+++ /dev/null
@@ -1,82 +0,0 @@
-%PDF-1.4
-%����
-1 0 obj
-<< /Type /Catalog /Pages 2 0 R >>
-endobj
-2 0 obj
-<< /Type /Pages /Kids [3 0 R] /Count 1 >>
-endobj
-3 0 obj
-<< /Type /Page /Parent 2 0 R /MediaBox [0 0 1920 1080] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>
-endobj
-4 0 obj
-<< /Length 6030 >>
-stream
-q
-4.5 0 0 -4.5 775 880 cm
-0 0 0 rg
-59.8175 53.1278 m 63.226 55.6474 66.8494 59.8264 68.5383 62.2538 c 68.9682 62.8684 68.4462 63.6673 67.7092 63.5444 c 61.6906 62.3768 41.6696 58.1364 24.6885 49.8092 c 24.32 49.6248 24.1358 49.2254 24.2586 48.8567 c 24.6271 47.5661 25.9168 44.9235 30.093 44.7392 c 30.7378 44.7084 31.1063 44.0017 30.7685 43.4793 c 29.878 42.0966 28.5269 39.9457 28.0049 38.2557 c 27.9435 38.0406 27.7899 37.8869 27.575 37.7947 c 26.7766 37.3953 24.4122 36.3506 19.9596 34.9985 c 14.3095 33.2778 12.1293 33.3393 11.0546 31.5878 c 9.9798 29.8363 9.9491 27.9927 12.4978 26.5485 c 15.0465 25.1043 25.9782 22.7075 29.7245 22.8612 c 29.7245 22.8612 30.9221 22.9226 30.8607 22.2159 c 30.7992 21.5092 30.2465 20.3415 30.2158 18.4364 c 30.1851 16.3162 32.5803 14.9642 34.7298 14.2882 c 36.7871 13.6736 39.3051 14.9027 41.1475 15.8246 c 41.3318 15.9168 41.5774 15.9475 41.7617 15.8553 c 42.7136 15.5173 45.815 14.5955 49.4999 15.1486 c 50.0526 15.2407 51.8029 12.3216 53.9831 12.5367 c 56.4704 12.7825 57.668 12.9055 58.5892 14.9027 c 59.3876 16.6542 59.2033 17.4839 58.7734 18.5286 c 58.6506 18.8051 58.712 19.1431 58.8963 19.3582 c 59.6332 20.2801 61.5371 22.8304 62.8882 26.3026 c 64.5464 30.5123 63.7787 33.708 63.0417 36.3506 c 62.9803 36.5964 63.011 36.8422 63.1645 37.0573 c 64.4235 38.7166 70.1658 46.5214 71.3633 50.9154 c 72.7145 55.8625 73.39 57.614 70.6264 58.0749 c 67.8627 58.5051 66.9108 59.7956 63.748 56.0162 c 60.9229 52.6054 57.1152 50.9154 57.8522 51.407 c 59.5718 53.0049 l 59.6025 52.9741 59.7868 53.0971 59.8175 53.1278 c
-f
-1 1 1 rg
-44.4024 16.6852 m 44.4024 16.6852 33.0407 19.5428 31.9353 26.6716 c 30.4613 36.1664 32.3652 42.0354 41.4238 44.8009 c 51.8028 47.9965 60.5236 40.7141 61.5062 35.8284 c 62.4889 30.9428 62.4275 24.9202 57.3301 19.7272 c 52.2327 14.5343 44.4024 16.6852 44.4024 16.6852 c
-f
-32.3346 49.2254 m 32.2118 49.1333 34.3613 53.896 35.0676 54.3262 c 35.7738 54.7257 38.5682 55.7089 39.3666 55.3709 c 39.3666 55.3709 37.8619 52.2675 39.6122 49.2562 c 39.6122 49.2562 36.9714 49.2254 36.1116 51.6529 c 36.1423 51.6222 33.8393 50.1165 32.3346 49.2254 c
-f
-0.9647 0.9608 0.9255 rg
-32.9181 50.6698 m 32.9181 50.6698 37.7699 54.5415 38.906 54.8181 c 38.906 54.8181 39.0903 55.617 38.9367 53.1895 c 38.7832 50.762 39.7658 48.1809 40.902 47.8429 c 42.0382 47.5049 36.9101 47.9044 36.603 49.9938 c 36.2959 52.0833 35.4975 49.8709 34.5763 48.7033 c 33.6858 47.5049 32.8567 45.9071 32.8874 47.2591 c 32.9181 48.6111 31.6899 49.1949 32.9181 50.6698 c
-f
-0.1569 0.1529 0.1529 rg
-50.6666 56.6614 m 51.5571 57.3988 58.6197 60.8403 60.2472 60.7481 c 61.9054 60.656 65.2832 61.1476 65.1296 60.7481 c 65.0068 60.3487 56.593 53.1277 53.6145 51.6221 c 50.9122 50.2701 50.206 49.6862 46.8282 49.1946 c 46.6439 49.1639 43.8189 48.7951 42.9591 49.4404 c 42.4678 49.8399 41.7001 51.2226 42.4064 51.7757 c 43.2662 52.4824 46.7975 53.4964 49.1312 55.7703 c 49.2233 55.8625 49.3462 55.9546 49.469 56.0161 c 50.6666 56.6614 l
-f
-61.6905 39.2389 m 61.6905 39.2389 58.6198 41.9736 59.4181 43.3871 c 60.2165 44.8006 64.0242 51.069 65.6517 53.3121 c 67.2792 55.5552 68.5382 56.4156 69.3058 56.139 c 70.0735 55.8625 68.6917 49.6555 66.1123 45.0157 c 63.5329 40.4065 62.3353 38.6243 61.6905 39.2389 c
-f
-14.1562 28.9145 m 14.2483 28.9453 19.9598 29.4369 22.2629 29.99 c 24.5659 30.5431 26.6233 31.3727 27.2374 29.3447 c 27.8516 27.3167 29.2948 25.9647 28.8342 25.5345 c 28.3736 25.1043 24.5659 25.1658 21.2495 25.8418 c 17.9332 26.5178 11.3925 27.9005 14.1562 28.9145 c
-f
-0 0 0 rg
-41.1782 33.2165 m 41.1782 33.2165 42.7749 34.6299 44.7402 34.4763 c 46.7054 34.3227 47.5652 33.0014 48.7628 31.3114 c 49.9604 29.6214 50.2982 27.9621 49.5919 26.3335 c 48.8856 24.7049 47.6574 23.568 44.3103 24.244 c 40.9632 24.92 39.7349 31.3114 41.1782 33.2165 c
-f
-57.4529 30.1128 m 57.4529 30.1128 56.4088 28.8222 55.4569 27.8697 c 54.505 26.9479 54.7199 25.1964 55.7333 24.3053 c 56.7466 23.4142 58.0977 23.2605 59.5409 26.1489 c 60.9842 29.0066 61.0456 32.1101 60.1244 34.261 c 60.1551 34.261 58.7118 31.3112 57.4529 30.1128 c
-f
-48.2101 36.1354 m 48.2101 36.1354 49.4077 37.7332 51.2194 38.2248 c 53.0005 38.7165 54.6586 38.0712 55.1192 37.5796 c 55.5491 37.1187 56.1326 36.1661 54.7508 35.4901 c 53.3689 34.8141 52.2021 33.9844 53.0926 33.1548 c 53.9831 32.3252 58.2207 32.3559 58.5891 33.0012 c 58.9576 33.6464 59.0497 34.1995 57.7908 35.2135 c 56.5318 36.2275 56.2247 36.8114 56.8388 37.5181 c 57.4837 38.2248 58.4356 37.7332 58.4356 37.7332 c 58.4356 37.7332 57.7908 38.8087 56.8081 38.6243 c 55.8562 38.4399 56.3782 37.979 55.8562 38.4399 c 55.3035 38.9008 54.4744 39.4539 53.3382 39.5154 c 52.2021 39.5768 50.3903 39.4539 48.9164 38.0712 c 47.4425 36.7192 47.7495 36.4426 47.8724 36.3197 c 47.9952 36.1968 48.0259 36.1661 48.2101 36.1354 c
-f
-0.4235 0.7804 0.8863 rg
-0 64.7428 m 0 64.7428 15.8756 52.083 46.3063 67.9999 c 46.3063 67.9999 20.4816 43.848 0 64.7428 c
-f
-0 0.4941 0.6667 rg
-4.6982 59.2118 m 4.6982 59.2118 15.6607 48.9488 32.089 57.1223 c 48.5173 65.2958 60.8308 75.651 82.5715 59.2118 c 82.5715 59.2118 64.0551 66.74 47.5654 57.2145 c 31.0757 47.689 17.1346 44.3704 4.6982 59.2118 c
-f
-1 1 1 rg
-47.6576 32.5403 m 48.4378 32.5403 49.0702 31.9075 49.0702 31.1268 c 49.0702 30.3462 48.4378 29.7134 47.6576 29.7134 c 46.8775 29.7134 46.2451 30.3462 46.2451 31.1268 c 46.2451 31.9075 46.8775 32.5403 47.6576 32.5403 c
-f
-58.0113 30.8234 m 58.7908 30.7915 59.3945 30.0782 59.3598 29.2304 c 59.3251 28.3826 58.665 27.7212 57.8855 27.7531 c 57.106 27.7851 56.5023 28.4983 56.537 29.3462 c 56.5717 30.194 57.2318 30.8554 58.0113 30.8234 c
-f
-0.9294 0.9294 0.9294 rg
-38.5068 19.0513 m 38.5068 19.0513 33.6858 27.0711 35.0062 32.9401 c 35.4361 34.8145 36.5108 43.7869 53.3076 44.1864 c 53.3076 44.1864 48.3945 46.8904 41.4547 44.8009 c 34.5149 42.7115 32.2733 38.9627 31.6591 34.4457 c 31.045 29.9288 31.9969 24.3057 34.0236 22.462 c 36.0502 20.6184 36.5109 20.0346 38.5068 19.0513 c
-f
-Q
-BT
-/F1 38 Tf
-0.1333 0.1333 0.1333 rg
-540 410 Td
-(No screenshot available,) Tj
-0 -48 Td
-(Lightpanda has no graphical rendering engine.) Tj
-ET
-endstream
-endobj
-5 0 obj
-<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>
-endobj
-xref
-0 6
-0000000000 65535 f
-0000000015 00000 n
-0000000064 00000 n
-0000000121 00000 n
-0000000249 00000 n
-0000006331 00000 n
-trailer
-<< /Size 6 /Root 1 0 R >>
-startxref
-6428
-%%EOF
diff --git a/src/lightpanda.zig b/src/lightpanda.zig
index a6930ded9..87f2300bf 100644
--- a/src/lightpanda.zig
+++ b/src/lightpanda.zig
@@ -39,6 +39,7 @@ pub const js = @import("browser/js/js.zig");
pub const dump = @import("browser/dump.zig");
pub const markdown = @import("browser/markdown.zig");
pub const screenshot = @import("browser/screenshot.zig");
+pub const pdf = @import("browser/pdf.zig");
pub const Base64Writer = @import("Base64Writer.zig");
const Selector = @import("browser/webapi/selector/Selector.zig");
const Node = @import("browser/webapi/Node.zig");
@@ -396,12 +397,14 @@ fn writeResults(app: *App, opts: FetchOpts, pages: []const Session.PageHandle, e
}
const frame = page.frame();
- if (opts.dump_mode == .png and frame != null) {
+ if ((opts.dump_mode == .png or opts.dump_mode == .pdf) and frame != null) {
+ // Binary formats stream themselves into the envelope as
+ // base64; nothing is buffered here.
const arena = try app.arena_pool.acquire(.large, "screenshot.dump");
defer arena.release();
- if (prepareShot(arena.allocator(), frame.?, opts)) |shot| {
- try writeJsonEnvelope(writer, frame, opts.dump_mode, shot, err.*);
+ if (prepareBinary(arena.allocator(), frame.?, opts)) |prepared| {
+ try writeJsonEnvelope(writer, frame, opts.dump_mode, prepared, err.*);
} else |e| {
if (err.* == null) err.* = e;
try writeJsonEnvelope(writer, frame, opts.dump_mode, "", err.*);
@@ -438,8 +441,26 @@ fn writeResults(app: *App, opts: FetchOpts, pages: []const Session.PageHandle, e
try writer.flush();
}
-fn prepareShot(arena: std.mem.Allocator, frame: *Frame, opts: FetchOpts) !screenshot.Prepared {
- return screenshot.prepare(arena, try dumpRoot(frame, opts.selector), .fromViewport(frame._page.getViewport(), true), frame);
+// A dump that can only fail before any output starts; it then streams into
+// the JSON envelope as base64.
+const Binary = union(enum) {
+ png: screenshot.Prepared,
+ pdf: pdf.Prepared,
+
+ pub fn jsonStringify(self: *const Binary, jws: *std.json.Stringify) std.Io.Writer.Error!void {
+ switch (self.*) {
+ inline else => |*prepared| try prepared.jsonStringify(jws),
+ }
+ }
+};
+
+fn prepareBinary(arena: std.mem.Allocator, frame: *Frame, opts: FetchOpts) !Binary {
+ const root = try dumpRoot(frame, opts.selector);
+ return switch (opts.dump_mode.?) {
+ .png => .{ .png = try screenshot.preparePng(arena, root, .fromViewport(frame._page.getViewport(), true), frame) },
+ .pdf => .{ .pdf = try pdf.prepare(arena, root, .{}, frame) },
+ else => unreachable,
+ };
}
fn dumpRoot(frame: *Frame, selector: ?[]const u8) !*Node {
@@ -462,6 +483,11 @@ fn dumpContent(app: *App, mode: Config.DumpFormat, opts: FetchOpts, frame: *Fram
defer arena.deinit();
_ = try screenshot.png(arena.allocator(), root, .fromViewport(frame._page.getViewport(), true), writer, frame);
},
+ .pdf => {
+ var arena: std.heap.ArenaAllocator = .init(app.allocator);
+ defer arena.deinit();
+ try pdf.print(arena.allocator(), root, .{}, writer, frame);
+ },
.semantic_tree, .semantic_tree_text => {
var registry = CDPNode.Registry.init(app.allocator);
defer registry.deinit();
diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rs
index daf6405d0..ce4eba1c3 100644
--- a/src/rust/render/lib.rs
+++ b/src/rust/render/lib.rs
@@ -67,6 +67,10 @@ pub struct LpSpan {
pub len: usize,
pub flags: u32,
pub color: u32,
+ /// Absolute URL of the enclosing link; empty for none. Not used here:
+ /// pdf.zig maps it onto the exported clusters.
+ pub href: *const u8,
+ pub href_len: usize,
}
#[repr(C)]
@@ -101,6 +105,140 @@ pub struct LpRenderOpts {
pub flags: u32,
}
+/// A laid-out document in layout px (scale 1), as flat arrays: blocks own a
+/// range of lines, lines a range of runs and clusters, runs a range of
+/// glyphs. Everything is owned by the handle `lp_layout_new` returns and is
+/// valid until `lp_layout_free`. The consumer is src/browser/pdf.zig.
+#[repr(C)]
+pub struct LpLayout {
+ /// Column height, margins included.
+ pub height: f32,
+ /// Style the consumer has to reproduce: <pre> padding, quote bar step,
+ /// rule/bar color and <pre> background (0xRRGGBB).
+ pub pre_pad: f32,
+ pub quote_indent: f32,
+ pub rule_color: u32,
+ pub pre_bg: u32,
+ pub blocks: *const LpLayoutBlock,
+ pub blocks_len: usize,
+ pub lines: *const LpLine,
+ pub lines_len: usize,
+ pub runs: *const LpRun,
+ pub runs_len: usize,
+ pub glyphs: *const LpGlyph,
+ pub glyphs_len: usize,
+ pub clusters: *const LpCluster,
+ pub clusters_len: usize,
+ pub fonts: *const LpFont,
+ pub fonts_len: usize,
+}
+
+/// "No index" for `marker_line` and `LpCluster.glyph`.
+pub const LAYOUT_NONE: u32 = u32::MAX;
+
+#[repr(C)]
+pub struct LpLayoutBlock {
+ /// Text origin: x is the left edge of the runs, y the top of the first
+ /// line box (inside the <pre> padding).
+ pub x: f32,
+ pub y: f32,
+ /// Where quote bars start; one every `quote_indent`.
+ pub quote_x: f32,
+ /// Text lines, in order. A rule has none.
+ pub lines: u32,
+ pub lines_len: u32,
+ /// The list marker's line (its own layout, positioned in the gutter),
+ /// or LAYOUT_NONE.
+ pub marker_line: u32,
+ /// BLOCK_* kind.
+ pub kind: u8,
+ pub quote_bars: u8,
+}
+
+#[repr(C)]
+pub struct LpLine {
+ /// Absolute origin of the runs on this line.
+ pub x: f32,
+ /// Absolute line box.
+ pub top: f32,
+ pub bottom: f32,
+ pub runs: u32,
+ pub runs_len: u32,
+ /// Every cluster on the line in visual order, for text mapping.
+ pub clusters: u32,
+ pub clusters_len: u32,
+}
+
+/// A run of glyphs in one font, size and style.
+#[repr(C)]
+pub struct LpRun {
+ pub font: u32,
+ pub size: f32,
+ /// tan of the synthetic italic angle; 0 for upright.
+ pub skew: f32,
+ /// 0xRRGGBB.
+ pub color: u32,
+ /// x from the line origin.
+ pub offset: f32,
+ /// Absolute y.
+ pub baseline: f32,
+ pub advance: f32,
+ pub glyphs: u32,
+ pub glyphs_len: u32,
+ pub underline: LpDecoration,
+ pub strike: LpDecoration,
+}
+
+/// A line drawn `offset` above the baseline, `size` thick, over the run.
+#[repr(C)]
+pub struct LpDecoration {
+ pub enabled: u32,
+ pub offset: f32,
+ pub size: f32,
+ pub color: u32,
+}
+
+/// Positioned relative to the pen: x/y offsets, then the pen advances.
+#[repr(C)]
+pub struct LpGlyph {
+ pub id: u32,
+ pub x: f32,
+ pub y: f32,
+ pub advance: f32,
+}
+
+/// What a piece of the block's text became: the (first) glyph it produced,
+/// and where on the line it sits. `text_start` indexes the concatenation
+/// of the block's span texts.
+#[repr(C)]
+pub struct LpCluster {
+ pub font: u32,
+ /// Glyph id, or LAYOUT_NONE for a cluster that produced none (a
+ /// ligature continuation).
+ pub glyph: u32,
+ pub text_start: u32,
+ pub text_len: u32,
+ /// x from the line origin.
+ pub x: f32,
+ pub advance: f32,
+ pub flags: u32,
+}
+
+pub const CLUSTER_LIGATURE_START: u32 = 1 << 0;
+pub const CLUSTER_LIGATURE_CONT: u32 = 1 << 1;
+
+/// A face the layout used: the raw font file (static, outlives the handle)
+/// and how to name it.
+#[repr(C)]
+pub struct LpFont {
+ pub data: *const u8,
+ pub data_len: usize,
+ pub index: u32,
+ pub name: *const u8,
+ pub name_len: usize,
+ pub mono: u8,
+}
+
pub type LpWriteFn = extern "C" fn(ctx: *mut c_void, data: *const u8, len: usize) -> bool;
/// Return codes. Anything but OK means nothing usable reached `write`, so a
@@ -126,6 +264,8 @@ pub struct LpAbi {
pub span_len: u32,
pub span_flags: u32,
pub span_color: u32,
+ pub span_href: u32,
+ pub span_href_len: u32,
pub block_size: u32,
pub block_align: u32,
@@ -150,6 +290,89 @@ pub struct LpAbi {
pub opts_scale: u32,
pub opts_flags: u32,
+ pub layout_sizeof: u32,
+ pub layout_alignof: u32,
+ pub layout_height: u32,
+ pub layout_pre_pad: u32,
+ pub layout_quote_indent: u32,
+ pub layout_rule_color: u32,
+ pub layout_pre_bg: u32,
+ pub layout_blocks: u32,
+ pub layout_blocks_len: u32,
+ pub layout_lines: u32,
+ pub layout_lines_len: u32,
+ pub layout_runs: u32,
+ pub layout_runs_len: u32,
+ pub layout_glyphs: u32,
+ pub layout_glyphs_len: u32,
+ pub layout_clusters: u32,
+ pub layout_clusters_len: u32,
+ pub layout_fonts: u32,
+ pub layout_fonts_len: u32,
+ pub lblock_sizeof: u32,
+ pub lblock_alignof: u32,
+ pub lblock_x: u32,
+ pub lblock_y: u32,
+ pub lblock_quote_x: u32,
+ pub lblock_lines: u32,
+ pub lblock_lines_len: u32,
+ pub lblock_marker_line: u32,
+ pub lblock_kind: u32,
+ pub lblock_quote_bars: u32,
+ pub line_sizeof: u32,
+ pub line_alignof: u32,
+ pub line_x: u32,
+ pub line_top: u32,
+ pub line_bottom: u32,
+ pub line_runs: u32,
+ pub line_runs_len: u32,
+ pub line_clusters: u32,
+ pub line_clusters_len: u32,
+ pub run_sizeof: u32,
+ pub run_alignof: u32,
+ pub run_font: u32,
+ pub run_size: u32,
+ pub run_skew: u32,
+ pub run_color: u32,
+ pub run_offset: u32,
+ pub run_baseline: u32,
+ pub run_advance: u32,
+ pub run_glyphs: u32,
+ pub run_glyphs_len: u32,
+ pub run_underline: u32,
+ pub run_strike: u32,
+ pub deco_sizeof: u32,
+ pub deco_alignof: u32,
+ pub deco_enabled: u32,
+ pub deco_offset: u32,
+ pub deco_size: u32,
+ pub deco_color: u32,
+ pub glyph_sizeof: u32,
+ pub glyph_alignof: u32,
+ pub glyph_id: u32,
+ pub glyph_x: u32,
+ pub glyph_y: u32,
+ pub glyph_advance: u32,
+ pub cluster_sizeof: u32,
+ pub cluster_alignof: u32,
+ pub cluster_font: u32,
+ pub cluster_glyph: u32,
+ pub cluster_text_start: u32,
+ pub cluster_text_len: u32,
+ pub cluster_x: u32,
+ pub cluster_advance: u32,
+ pub cluster_flags: u32,
+ pub font_sizeof: u32,
+ pub font_alignof: u32,
+ pub font_data: u32,
+ pub font_data_len: u32,
+ pub font_index: u32,
+ pub font_name: u32,
+ pub font_name_len: u32,
+ pub font_mono: u32,
+ pub layout_none: u32,
+ pub cluster_ligature_start: u32,
+ pub cluster_ligature_cont: u32,
pub span_bold: u32,
pub span_italic: u32,
pub span_underline: u32,
@@ -184,6 +407,8 @@ pub unsafe extern "C" fn lp_render_abi(out: *mut LpAbi) {
span_len: offset_of!(LpSpan, len) as u32,
span_flags: offset_of!(LpSpan, flags) as u32,
span_color: offset_of!(LpSpan, color) as u32,
+ span_href: offset_of!(LpSpan, href) as u32,
+ span_href_len: offset_of!(LpSpan, href_len) as u32,
block_size: size_of::<LpBlock>() as u32,
block_align: align_of::<LpBlock>() as u32,
@@ -208,6 +433,89 @@ pub unsafe extern "C" fn lp_render_abi(out: *mut LpAbi) {
opts_scale: offset_of!(LpRenderOpts, scale) as u32,
opts_flags: offset_of!(LpRenderOpts, flags) as u32,
+ layout_sizeof: size_of::<LpLayout>() as u32,
+ layout_alignof: align_of::<LpLayout>() as u32,
+ layout_height: offset_of!(LpLayout, height) as u32,
+ layout_pre_pad: offset_of!(LpLayout, pre_pad) as u32,
+ layout_quote_indent: offset_of!(LpLayout, quote_indent) as u32,
+ layout_rule_color: offset_of!(LpLayout, rule_color) as u32,
+ layout_pre_bg: offset_of!(LpLayout, pre_bg) as u32,
+ layout_blocks: offset_of!(LpLayout, blocks) as u32,
+ layout_blocks_len: offset_of!(LpLayout, blocks_len) as u32,
+ layout_lines: offset_of!(LpLayout, lines) as u32,
+ layout_lines_len: offset_of!(LpLayout, lines_len) as u32,
+ layout_runs: offset_of!(LpLayout, runs) as u32,
+ layout_runs_len: offset_of!(LpLayout, runs_len) as u32,
+ layout_glyphs: offset_of!(LpLayout, glyphs) as u32,
+ layout_glyphs_len: offset_of!(LpLayout, glyphs_len) as u32,
+ layout_clusters: offset_of!(LpLayout, clusters) as u32,
+ layout_clusters_len: offset_of!(LpLayout, clusters_len) as u32,
+ layout_fonts: offset_of!(LpLayout, fonts) as u32,
+ layout_fonts_len: offset_of!(LpLayout, fonts_len) as u32,
+ lblock_sizeof: size_of::<LpLayoutBlock>() as u32,
+ lblock_alignof: align_of::<LpLayoutBlock>() as u32,
+ lblock_x: offset_of!(LpLayoutBlock, x) as u32,
+ lblock_y: offset_of!(LpLayoutBlock, y) as u32,
+ lblock_quote_x: offset_of!(LpLayoutBlock, quote_x) as u32,
+ lblock_lines: offset_of!(LpLayoutBlock, lines) as u32,
+ lblock_lines_len: offset_of!(LpLayoutBlock, lines_len) as u32,
+ lblock_marker_line: offset_of!(LpLayoutBlock, marker_line) as u32,
+ lblock_kind: offset_of!(LpLayoutBlock, kind) as u32,
+ lblock_quote_bars: offset_of!(LpLayoutBlock, quote_bars) as u32,
+ line_sizeof: size_of::<LpLine>() as u32,
+ line_alignof: align_of::<LpLine>() as u32,
+ line_x: offset_of!(LpLine, x) as u32,
+ line_top: offset_of!(LpLine, top) as u32,
+ line_bottom: offset_of!(LpLine, bottom) as u32,
+ line_runs: offset_of!(LpLine, runs) as u32,
+ line_runs_len: offset_of!(LpLine, runs_len) as u32,
+ line_clusters: offset_of!(LpLine, clusters) as u32,
+ line_clusters_len: offset_of!(LpLine, clusters_len) as u32,
+ run_sizeof: size_of::<LpRun>() as u32,
+ run_alignof: align_of::<LpRun>() as u32,
+ run_font: offset_of!(LpRun, font) as u32,
+ run_size: offset_of!(LpRun, size) as u32,
+ run_skew: offset_of!(LpRun, skew) as u32,
+ run_color: offset_of!(LpRun, color) as u32,
+ run_offset: offset_of!(LpRun, offset) as u32,
+ run_baseline: offset_of!(LpRun, baseline) as u32,
+ run_advance: offset_of!(LpRun, advance) as u32,
+ run_glyphs: offset_of!(LpRun, glyphs) as u32,
+ run_glyphs_len: offset_of!(LpRun, glyphs_len) as u32,
+ run_underline: offset_of!(LpRun, underline) as u32,
+ run_strike: offset_of!(LpRun, strike) as u32,
+ deco_sizeof: size_of::<LpDecoration>() as u32,
+ deco_alignof: align_of::<LpDecoration>() as u32,
+ deco_enabled: offset_of!(LpDecoration, enabled) as u32,
+ deco_offset: offset_of!(LpDecoration, offset) as u32,
+ deco_size: offset_of!(LpDecoration, size) as u32,
+ deco_color: offset_of!(LpDecoration, color) as u32,
+ glyph_sizeof: size_of::<LpGlyph>() as u32,
+ glyph_alignof: align_of::<LpGlyph>() as u32,
+ glyph_id: offset_of!(LpGlyph, id) as u32,
+ glyph_x: offset_of!(LpGlyph, x) as u32,
+ glyph_y: offset_of!(LpGlyph, y) as u32,
+ glyph_advance: offset_of!(LpGlyph, advance) as u32,
+ cluster_sizeof: size_of::<LpCluster>() as u32,
+ cluster_alignof: align_of::<LpCluster>() as u32,
+ cluster_font: offset_of!(LpCluster, font) as u32,
+ cluster_glyph: offset_of!(LpCluster, glyph) as u32,
+ cluster_text_start: offset_of!(LpCluster, text_start) as u32,
+ cluster_text_len: offset_of!(LpCluster, text_len) as u32,
+ cluster_x: offset_of!(LpCluster, x) as u32,
+ cluster_advance: offset_of!(LpCluster, advance) as u32,
+ cluster_flags: offset_of!(LpCluster, flags) as u32,
+ font_sizeof: size_of::<LpFont>() as u32,
+ font_alignof: align_of::<LpFont>() as u32,
+ font_data: offset_of!(LpFont, data) as u32,
+ font_data_len: offset_of!(LpFont, data_len) as u32,
+ font_index: offset_of!(LpFont, index) as u32,
+ font_name: offset_of!(LpFont, name) as u32,
+ font_name_len: offset_of!(LpFont, name_len) as u32,
+ font_mono: offset_of!(LpFont, mono) as u32,
+ layout_none: LAYOUT_NONE,
+ cluster_ligature_start: CLUSTER_LIGATURE_START,
+ cluster_ligature_cont: CLUSTER_LIGATURE_CONT,
span_bold: SPAN_BOLD,
span_italic: SPAN_ITALIC,
span_underline: SPAN_UNDERLINE,
@@ -288,6 +596,262 @@ unsafe fn render_png(
if opts.width == 0 || !(opts.scale > 0.0) || opts.scale > 8.0 {
return RC_INVALID;
}
+ let Some(doc) = blocks_from_ffi(blocks, blocks_len) else {
+ return RC_INVALID;
+ };
+ let mut sink = Sink {
+ ctx,
+ write,
+ failed: false,
+ };
+ let (h, rc) = r.render(&doc, opts, &mut sink);
+ if !content_height.is_null() {
+ *content_height = h;
+ }
+ // A refused write surfaces as an encode failure too; report the cause.
+ if sink.failed {
+ RC_WRITE_REFUSED
+ } else {
+ rc
+ }
+}
+
+/// Lays `blocks` out in a column `width` layout px wide with `margin` on
+/// every side and exports the result into `out`. Null on invalid input or
+/// panic; the handle must be freed with `lp_layout_free`.
+#[no_mangle]
+pub unsafe extern "C" fn lp_layout_new(
+ r: *mut Renderer,
+ blocks: *const LpBlock,
+ blocks_len: usize,
+ width: f32,
+ margin: f32,
+ out: *mut LpLayout,
+) -> *mut LayoutHandle {
+ let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ if !(width >= 1.0) || !width.is_finite() || !(margin >= 0.0) || !margin.is_finite() {
+ return std::ptr::null_mut();
+ }
+ let Some(doc) = blocks_from_ffi(blocks, blocks_len) else {
+ return std::ptr::null_mut();
+ };
+ let (placed, height) = (*r).layout(&doc, width, margin, 1.0);
+ let handle = Box::new(LayoutHandle::export(&placed, height));
+ *out = handle.view();
+ Box::into_raw(handle)
+ }));
+ caught.unwrap_or(std::ptr::null_mut())
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn lp_layout_free(h: *mut LayoutHandle) {
+ drop(Box::from_raw(h));
+}
+
+/// The arrays behind an `LpLayout`.
+pub struct LayoutHandle {
+ height: f32,
+ blocks: Vec<LpLayoutBlock>,
+ lines: Vec<LpLine>,
+ runs: Vec<LpRun>,
+ glyphs: Vec<LpGlyph>,
+ clusters: Vec<LpCluster>,
+ fonts: Vec<LpFont>,
+}
+
+impl LayoutHandle {
+ fn view(&self) -> LpLayout {
+ LpLayout {
+ height: self.height,
+ pre_pad: PRE_PAD,
+ quote_indent: QUOTE_INDENT,
+ rule_color: RULE_COLOR.packed(),
+ pre_bg: PRE_BG.packed(),
+ blocks: self.blocks.as_ptr(),
+ blocks_len: self.blocks.len(),
+ lines: self.lines.as_ptr(),
+ lines_len: self.lines.len(),
+ runs: self.runs.as_ptr(),
+ runs_len: self.runs.len(),
+ glyphs: self.glyphs.as_ptr(),
+ glyphs_len: self.glyphs.len(),
+ clusters: self.clusters.as_ptr(),
+ clusters_len: self.clusters.len(),
+ fonts: self.fonts.as_ptr(),
+ fonts_len: self.fonts.len(),
+ }
+ }
+
+ fn export(placed: &[Placed], height: f32) -> LayoutHandle {
+ let mut h = LayoutHandle {
+ height,
+ blocks: Vec::with_capacity(placed.len()),
+ lines: Vec::new(),
+ runs: Vec::new(),
+ glyphs: Vec::new(),
+ clusters: Vec::new(),
+ fonts: Vec::new(),
+ };
+ for p in placed {
+ let lines = h.lines.len() as u32;
+ if p.kind != BLOCK_RULE {
+ h.export_layout(&p.layout, p.x, p.y);
+ }
+ let lines_len = h.lines.len() as u32 - lines;
+ let marker_line = match &p.marker {
+ Some(m) => {
+ let at = h.lines.len() as u32;
+ h.export_layout(&m.layout, m.x, p.y);
+ at
+ }
+ None => LAYOUT_NONE,
+ };
+ h.blocks.push(LpLayoutBlock {
+ x: p.x,
+ y: p.y,
+ quote_x: p.quote_x,
+ lines,
+ lines_len,
+ marker_line,
+ kind: p.kind,
+ quote_bars: p.quote_bars,
+ });
+ }
+ h
+ }
+
+ fn export_layout(&mut self, layout: &Layout<Rgb>, ox: f32, oy: f32) {
+ for line in layout.lines() {
+ let m = line.metrics();
+ let runs = self.runs.len() as u32;
+ for item in line.items() {
+ if let PositionedLayoutItem::GlyphRun(gr) = item {
+ self.export_run(&gr, oy);
+ }
+ }
+ let runs_len = self.runs.len() as u32 - runs;
+
+ // Clusters in visual order, positioned by accumulating advances
+ // (parley's Cluster::visual_offset is linear per call).
+ let clusters = self.clusters.len() as u32;
+ let mut x = m.offset;
+ for run in line.runs() {
+ let font = self.font_index(run.font());
+ for cluster in run.visual_clusters() {
+ let range = cluster.text_range();
+ let mut flags = 0;
+ if cluster.is_ligature_start() {
+ flags |= CLUSTER_LIGATURE_START;
+ }
+ if cluster.is_ligature_continuation() {
+ flags |= CLUSTER_LIGATURE_CONT;
+ }
+ self.clusters.push(LpCluster {
+ font,
+ glyph: cluster.glyphs().next().map_or(LAYOUT_NONE, |g| g.id),
+ text_start: range.start as u32,
+ text_len: range.len() as u32,
+ x,
+ advance: cluster.advance(),
+ flags,
+ });
+ x += cluster.advance();
+ }
+ }
+ let clusters_len = self.clusters.len() as u32 - clusters;
+
+ self.lines.push(LpLine {
+ x: ox,
+ top: oy + m.block_min_coord,
+ bottom: oy + m.block_max_coord,
+ runs,
+ runs_len,
+ clusters,
+ clusters_len,
+ });
+ }
+ }
+
+ fn export_run(&mut self, gr: &GlyphRun<Rgb>, oy: f32) {
+ let run = gr.run();
+ let style = gr.style();
+ let m = run.metrics();
+ let glyphs = self.glyphs.len() as u32;
+ for g in gr.glyphs() {
+ self.glyphs.push(LpGlyph {
+ id: g.id,
+ x: g.x,
+ y: g.y,
+ advance: g.advance,
+ });
+ }
+ let deco = |d: &Option<parley::layout::Decoration<Rgb>>, off: f32, size: f32| match d {
+ Some(d) => LpDecoration {
+ enabled: 1,
+ offset: d.offset.unwrap_or(off),
+ size: d.size.unwrap_or(size).max(1.0),
+ color: d.brush.packed(),
+ },
+ None => LpDecoration {
+ enabled: 0,
+ offset: 0.0,
+ size: 0.0,
+ color: 0,
+ },
+ };
+ let font = self.font_index(run.font());
+ let glyphs_len = self.glyphs.len() as u32 - glyphs;
+ self.runs.push(LpRun {
+ font,
+ size: run.font_size(),
+ skew: run
+ .synthesis()
+ .skew()
+ .map(|deg| deg.to_radians().tan())
+ .unwrap_or(0.0),
+ color: style.brush.packed(),
+ offset: gr.offset(),
+ baseline: oy + gr.baseline(),
+ advance: gr.advance(),
+ glyphs,
+ glyphs_len,
+ underline: deco(&style.underline, m.underline_offset, m.underline_size),
+ strike: deco(
+ &style.strikethrough,
+ m.strikethrough_offset,
+ m.strikethrough_size,
+ ),
+ });
+ }
+
+ fn font_index(&mut self, font: &parley::FontData) -> u32 {
+ let bytes = font.data.as_ref();
+ if let Some(i) = self
+ .fonts
+ .iter()
+ .position(|f| f.data == bytes.as_ptr() && f.index == font.index)
+ {
+ return i as u32;
+ }
+ let (name, mono) = FONTS
+ .iter()
+ .find(|f| f.0.as_ptr() == bytes.as_ptr())
+ .map(|f| (f.1, f.2))
+ .unwrap_or(("DejaVuSans", false));
+ self.fonts.push(LpFont {
+ data: bytes.as_ptr(),
+ data_len: bytes.len(),
+ index: font.index,
+ name: name.as_ptr(),
+ name_len: name.len(),
+ mono: mono as u8,
+ });
+ (self.fonts.len() - 1) as u32
+ }
+}
+
+/// None if any span isn't UTF-8.
+unsafe fn blocks_from_ffi<'a>(blocks: *const LpBlock, blocks_len: usize) -> Option<Vec<Block<'a>>> {
let blocks = if blocks_len == 0 {
&[][..]
} else {
@@ -303,9 +867,7 @@ unsafe fn render_png(
let mut out_spans = Vec::with_capacity(spans.len());
for s in spans {
let bytes = std::slice::from_raw_parts(s.text, s.len);
- let Ok(text) = std::str::from_utf8(bytes) else {
- return RC_INVALID;
- };
+ let text = std::str::from_utf8(bytes).ok()?;
out_spans.push(Span {
text,
flags: s.flags,
@@ -324,25 +886,10 @@ unsafe fn render_png(
spans: out_spans,
});
}
-
- let mut sink = Sink {
- ctx,
- write,
- failed: false,
- };
- let (h, rc) = r.render(&doc, opts, &mut sink);
- if !content_height.is_null() {
- *content_height = h;
- }
- // A refused write surfaces as an encode failure too; report the cause.
- if sink.failed {
- RC_WRITE_REFUSED
- } else {
- rc
- }
+ Some(doc)
}
-struct Sink {
+pub(crate) struct Sink {
ctx: *mut c_void,
write: LpWriteFn,
failed: bool,
@@ -382,6 +929,12 @@ struct Block<'a> {
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
struct Rgb(u8, u8, u8);
+impl Rgb {
+ fn packed(self) -> u32 {
+ (self.0 as u32) << 16 | (self.1 as u32) << 8 | self.2 as u32
+ }
+}
+
const BASE_SIZE: f32 = 16.0;
const PAGE_MARGIN: f32 = 16.0;
const LIST_INDENT: f32 = 24.0;
@@ -405,16 +958,47 @@ pub struct Renderer {
glyph_cache: HashMap<GlyphKey, Option<tiny_skia::Path>>,
}
-struct Placed {
+/// A laid-out block. Positions are layout px from the top-left of the
+/// column; `layout` itself is in device px (layout px times the scale it
+/// was built with).
+pub(crate) struct Placed {
layout: Layout<Rgb>,
x: f32,
y: f32,
kind: u8,
- marker: Option<(Layout<Rgb>, f32)>,
+ marker: Option<Marker>,
quote_bars: u8,
quote_x: f32,
}
+pub(crate) struct Marker {
+ layout: Layout<Rgb>,
+ /// Device px; already right-aligned into the gutter.
+ x: f32,
+}
+
+/// The bundled faces: (file, PostScript-ish name, monospace). parley hands
+/// back the same `&'static [u8]` it was registered with, so a face is
+/// identified by its data pointer.
+pub(crate) static FONTS: [(&[u8], &str, bool); 4] = [
+ (include_bytes!("fonts/DejaVuSans.ttf"), "DejaVuSans", false),
+ (
+ include_bytes!("fonts/DejaVuSans-Bold.ttf"),
+ "DejaVuSans-Bold",
+ false,
+ ),
+ (
+ include_bytes!("fonts/DejaVuSansMono.ttf"),
+ "DejaVuSansMono",
+ true,
+ ),
+ (
+ include_bytes!("fonts/DejaVuSansMono-Bold.ttf"),
+ "DejaVuSansMono-Bold",
+ true,
+ ),
+];
+
/// (font_size, weight, space_before, space_after, line_height)
fn block_metrics(b: &Block) -> (f32, FontWeight, f32, f32, f32) {
match b.kind {
@@ -444,13 +1028,10 @@ impl Renderer {
shared: false,
system_fonts: false,
});
- let sans = Self::register(&mut collection, include_bytes!("fonts/DejaVuSans.ttf"));
- Self::register(&mut collection, include_bytes!("fonts/DejaVuSans-Bold.ttf"));
- let mono = Self::register(&mut collection, include_bytes!("fonts/DejaVuSansMono.ttf"));
- Self::register(
- &mut collection,
- include_bytes!("fonts/DejaVuSansMono-Bold.ttf"),
- );
+ let sans = Self::register(&mut collection, FONTS[0].0);
+ Self::register(&mut collection, FONTS[1].0);
+ let mono = Self::register(&mut collection, FONTS[2].0);
+ Self::register(&mut collection, FONTS[3].0);
for generic in [
GenericFamily::SansSerif,
GenericFamily::Serif,
@@ -476,12 +1057,18 @@ impl Renderer {
fams[0].0
}
- /// Returns the content height in CSS px, and RC_OK only if a complete
- /// PNG reached `sink`.
- fn render(&mut self, blocks: &[Block], opts: LpRenderOpts, sink: &mut Sink) -> (u32, i32) {
- let scale = opts.scale;
- let content_w = (opts.width as f32 - 2.0 * PAGE_MARGIN).max(1.0);
- let mut y = PAGE_MARGIN;
+ /// Flows `blocks` into a column `width` layout px wide with `margin` on
+ /// every side, shaping at `scale` device px per layout px. Returns the
+ /// placed blocks and the column height (margins included), in layout px.
+ pub(crate) fn layout(
+ &mut self,
+ blocks: &[Block],
+ width: f32,
+ margin: f32,
+ scale: f32,
+ ) -> (Vec<Placed>, f32) {
+ let content_w = (width - 2.0 * margin).max(1.0);
+ let mut y = margin;
let mut placed: Vec<Placed> = Vec::with_capacity(blocks.len());
let mut prev_after: Option<f32> = None;
@@ -495,12 +1082,12 @@ impl Renderer {
let indent =
block.list_depth as f32 * LIST_INDENT + block.quote_depth as f32 * QUOTE_INDENT;
- let quote_x = PAGE_MARGIN + block.list_depth as f32 * LIST_INDENT;
+ let quote_x = margin + block.list_depth as f32 * LIST_INDENT;
if block.kind == BLOCK_RULE {
placed.push(Placed {
layout: Layout::new(),
- x: PAGE_MARGIN + indent,
+ x: margin + indent,
y,
kind: BLOCK_RULE,
marker: None,
@@ -579,14 +1166,14 @@ impl Renderer {
b.push_default(StyleProperty::Brush(TEXT_COLOR));
let mut ml = b.build(block.marker);
ml.break_all_lines(None);
- let mx = (PAGE_MARGIN + indent) * scale - ml.width() - 6.0 * scale;
- Some((ml, mx))
+ let mx = (margin + indent) * scale - ml.width() - 6.0 * scale;
+ Some(Marker { layout: ml, x: mx })
};
let h = layout.height() / scale;
placed.push(Placed {
layout,
- x: PAGE_MARGIN + indent + pad,
+ x: margin + indent + pad,
y: y + pad,
kind: block.kind,
marker,
@@ -595,7 +1182,14 @@ impl Renderer {
});
y += h + 2.0 * pad;
}
- y += PAGE_MARGIN;
+ (placed, y + margin)
+ }
+
+ /// Returns the content height in CSS px, and RC_OK only if a complete
+ /// PNG reached `sink`.
+ fn render(&mut self, blocks: &[Block], opts: LpRenderOpts, sink: &mut Sink) -> (u32, i32) {
+ let scale = opts.scale;
+ let (placed, y) = self.layout(blocks, opts.width as f32, PAGE_MARGIN, scale);
let content_h = y.ceil().max(1.0) as u32;
if opts.flags & RENDER_MEASURE_ONLY != 0 {
return (content_h, RC_OK);
@@ -662,8 +1256,8 @@ impl Renderer {
pixmap.fill_rect(r, &paint, Transform::identity(), None);
}
}
- if let Some((ml, mx)) = &p.marker {
- self.draw_layout(&mut pixmap, ml, *mx, oy);
+ if let Some(m) = &p.marker {
+ self.draw_layout(&mut pixmap, &m.layout, m.x, oy);
}
self.draw_layout(&mut pixmap, &p.layout, p.x * scale, oy);
}