Lightpanda: the headless browser designed for AI and automation
wip: screenshot
Karl Seguinff5b791b7e43
diff --git a/.github/actions/install/action.yml b/.github/actions/install/action.ymlindex b12b18963..96fc529a4 100644--- a/.github/actions/install/action.yml+++ b/.github/actions/install/action.yml@@ -49,13 +49,13 @@ runs:shell: bashrun: mkdir -p "${ZIG_GLOBAL_CACHE_DIR:-$HOME/.cache/zig}/tmp"- # Rust Toolchain for html5ever+ # Rust Toolchain for src/rust (html5ever bridge, renderer)- uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # masterwith:toolchain: stable- uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1with:- workspaces: src/html5ever+ workspaces: src/rust- name: Cache v8id: cache-v8diff --git a/.gitignore b/.gitignoreindex edae6eca5..67b898da3 100644--- a/.gitignore+++ b/.gitignore@@ -6,5 +6,5 @@/zig-pkg/zig-outlightpanda.id-/src/html5ever/target/+/src/rust/target/src/snapshot.bindiff --git a/Makefile b/Makefileindex dadfa59fa..ac014e5e1 100644--- a/Makefile+++ b/Makefile@@ -144,7 +144,7 @@ test-agent:## Remove build artifacts (keeps .lp-cache/ and zig-pkg/ — slow to re-fetch)clean:rm -rf zig-out .zig-cache src/snapshot.bin- cd src/html5ever && cargo clean+ cd src/rust && cargo clean# Install and build required dependencies commands# ------------diff --git a/README.md b/README.mdindex 88ababc44..d188cb454 100644--- a/README.md+++ b/README.md@@ -105,7 +105,8 @@ docker run -d --name lightpanda -p 127.0.0.1:9222:9222 lightpanda/browser:nightl./lightpanda fetch --obey-robots --dump html --log-format pretty --log-level info https://demo-browser.lightpanda.io/campfire-commerce/```-You can use `--dump markdown` to convert directly into markdown.+You can use `--dump markdown` to convert directly into markdown, or+`--dump png > page.png` for a text-only rendering of the page.`--wait-until`, `--wait-ms`, `--wait-selector` and `--wait-script` areavailable to adjust waiting time before dump.diff --git a/build.zig b/build.zigindex 7ccff15f7..d53e610ba 100644--- a/build.zig+++ b/build.zig@@ -115,7 +115,7 @@ pub fn build(b: *Build) !void {linkV8(b, lightpanda_module, enable_asan, enable_tsan, prebuilt_v8_path, shared_v8);linkCurl(b, lightpanda_module, enable_tsan);- linkHtml5Ever(b, lightpanda_module);+ linkRust(b, lightpanda_module);linkZenai(b, lightpanda_module);linkIsocline(b, lightpanda_module);linkSqlite(b, lightpanda_module, enable_csan, enable_tsan);@@ -323,37 +323,47 @@ fn linkV8(mod.addImport("v8", dep.module("v8"));}-fn linkHtml5Ever(b: *Build, mod: *Build.Module) void {+fn linkRust(b: *Build, mod: *Build.Module) void {const is_debug = mod.optimize.? == .Debug;+ // One cargo workspace, one staticlib (src/rust/Cargo.toml explains why).const exec_cargo = b.addSystemCommand(&.{"cargo", "build","--profile", if (is_debug) "dev" else "release","--features", if (is_debug) "memstats" else "",- "--manifest-path", "src/html5ever/Cargo.toml",+ "--manifest-path", "src/rust/ffi/Cargo.toml",});// Track Rust sources so edits invalidate the cargo step's cache.// Without this, Zig keys the step on argv only and won't re-run cargo- // when lib.rs/Cargo.toml change.+ // when the sources change.for ([_][]const u8{- "src/html5ever/Cargo.toml",- "src/html5ever/Cargo.lock",- "src/html5ever/lib.rs",- "src/html5ever/sink.rs",- "src/html5ever/types.rs",- "src/html5ever/url.rs",+ "src/rust/Cargo.toml",+ "src/rust/Cargo.lock",+ "src/rust/ffi/Cargo.toml",+ "src/rust/ffi/lib.rs",+ "src/rust/html5ever/Cargo.toml",+ "src/rust/html5ever/lib.rs",+ "src/rust/html5ever/prescan.rs",+ "src/rust/html5ever/sink.rs",+ "src/rust/html5ever/types.rs",+ "src/rust/html5ever/url.rs",+ "src/rust/render/Cargo.toml",+ "src/rust/render/lib.rs",+ "src/rust/render/fonts/DejaVuSans.ttf",+ "src/rust/render/fonts/DejaVuSans-Bold.ttf",+ "src/rust/render/fonts/DejaVuSansMono.ttf",}) |path| {exec_cargo.addFileInput(b.path(path));}// TODO: We can prefer `--artifact-dir` once it become stable.- const out_dir = exec_cargo.addPrefixedOutputDirectoryArg("--target-dir=", "html5ever");+ const out_dir = exec_cargo.addPrefixedOutputDirectoryArg("--target-dir=", "rust");- const html5ever_step = b.step("html5ever", "Install html5ever dependency (requires cargo)");- html5ever_step.dependOn(&exec_cargo.step);+ const rust_step = b.step("rust", "Build the Rust staticlib (requires cargo)");+ rust_step.dependOn(&exec_cargo.step);- const obj = out_dir.path(b, if (is_debug) "debug" else "release").path(b, "liblitefetch_html5ever.a");+ const obj = out_dir.path(b, if (is_debug) "debug" else "release").path(b, "liblightpanda_ffi.a");mod.addObjectFile(obj);}diff --git a/src/Base64Writer.zig b/src/Base64Writer.zignew file mode 100644index 000000000..a3bf6e476--- /dev/null+++ b/src/Base64Writer.zig@@ -0,0 +1,162 @@+// 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/>.++// var b64 = Base64Writer.init(&out.writer, .standard);+// try producer.write(&b64.writer);+// try b64.finish();++const std = @import("std");+const Writer = std.Io.Writer;++const Base64Writer = @This();++inner: *Writer,+writer: Writer,+pending_len: u2 = 0,+pending: [3]u8 = undefined,+codec: *const std.base64.Base64Encoder,++pub const Codec = enum {+ standard,+ standard_no_pad,+ url_safe,+ url_safe_no_pad,++ fn encoder(self: Codec) *const std.base64.Base64Encoder {+ return switch (self) {+ .standard => &std.base64.standard.Encoder,+ .standard_no_pad => &std.base64.standard_no_pad.Encoder,+ .url_safe => &std.base64.url_safe.Encoder,+ .url_safe_no_pad => &std.base64.url_safe_no_pad.Encoder,+ };+ }+};++pub fn init(inner: *Writer, codec: Codec) Base64Writer {+ return .{+ .inner = inner,+ .codec = codec.encoder(),+ .writer = .{+ .vtable = &vtable,+ .buffer = &.{},+ },+ };+}++const vtable = Writer.VTable{ .drain = drain };++fn drain(w: *Writer, data: []const []const u8, splat: usize) Writer.Error!usize {+ const self: *Base64Writer = @alignCast(@fieldParentPtr("writer", w));+ var total: usize = 0;+ for (data[0 .. data.len - 1]) |slice| {+ try self.feed(slice);+ total += slice.len;+ }+ const pattern = data[data.len - 1];+ for (0..splat) |_| {+ try self.feed(pattern);+ total += pattern.len;+ }+ return total;+}++fn feed(self: *Base64Writer, bytes: []const u8) Writer.Error!void {+ var src = bytes;++ if (self.pending_len > 0) {+ while (self.pending_len < 3 and src.len > 0) {+ self.pending[self.pending_len] = src[0];+ self.pending_len += 1;+ src = src[1..];+ }+ if (self.pending_len < 3) return;+ var group: [4]u8 = undefined;+ try self.inner.writeAll(self.codec.encode(&group, &self.pending));+ self.pending_len = 0;+ }++ // Encode whole groups in bulk through a stack buffer, then keep the+ // remainder for the next write.+ const full = src.len - src.len % 3;+ var out: [4096]u8 = undefined;+ const in_step = out.len / 4 * 3;+ var i: usize = 0;+ while (i < full) {+ const n = @min(in_step, full - i);+ try self.inner.writeAll(self.codec.encode(&out, src[i .. i + n]));+ i += n;+ }++ const rem = src[full..];+ @memcpy(self.pending[0..rem.len], rem);+ self.pending_len = @intCast(rem.len);+}++// Encodes the trailing partial group (with padding, per codec). Call once,+// after the last write.+pub fn finish(self: *Base64Writer) Writer.Error!void {+ if (self.pending_len == 0) return;+ var group: [4]u8 = undefined;+ try self.inner.writeAll(self.codec.encode(&group, self.pending[0..self.pending_len]));+ self.pending_len = 0;+}++const testing = @import("testing.zig");+test "Base64Writer: matches std for every chunking" {+ var input: [1000]u8 = undefined;+ for (&input, 0..) |*b, i| b.* = @truncate(i *% 31 +% 7);++ inline for (.{ Codec.standard, Codec.standard_no_pad, Codec.url_safe, Codec.url_safe_no_pad }) |codec| {+ const enc = codec.encoder();+ for ([_]usize{ 0, 1, 2, 3, 4, 5, 6, 7, 100, 999, 1000 }) |len| {+ const expected = try testing.allocator.alloc(u8, enc.calcSize(len));+ defer testing.allocator.free(expected);+ _ = enc.encode(expected, input[0..len]);++ for ([_]usize{ 1, 2, 3, 4, 5, 7, 64, 3071, 3072, 3073, 4096 }) |chunk| {+ const got = try testEncode(codec, input[0..len], chunk);+ defer testing.allocator.free(got);+ try testing.expectEqual(expected, got);+ }+ }+ }+}++test "Base64Writer: writer helpers" {+ var aw: Writer.Allocating = .init(testing.allocator);+ defer aw.deinit();++ var b64 = Base64Writer.init(&aw.writer, .standard);+ try b64.writer.print("{s}-{d}", .{ "hello", 42 });+ try b64.writer.splatByteAll('!', 5);+ try b64.finish();+ try testing.expectEqual("aGVsbG8tNDIhISEhIQ==", aw.written());+}++fn testEncode(codec: Codec, input: []const u8, chunk: usize) ![]const u8 {+ var aw: Writer.Allocating = .init(testing.allocator);+ errdefer aw.deinit();++ var b64 = Base64Writer.init(&aw.writer, codec);+ var i: usize = 0;+ while (i < input.len) : (i += chunk) {+ try b64.writer.writeAll(input[i..@min(input.len, i + chunk)]);+ }+ try b64.finish();+ return aw.toOwnedSlice();+}diff --git a/src/Config.zig b/src/Config.zigindex b9da8cfa3..ecde1b943 100644--- a/src/Config.zig+++ b/src/Config.zig@@ -847,6 +847,7 @@ pub fn customCertStore(self: *const Config) ?*crypto.X509_STORE {pub const DumpFormat = enum {html,markdown,+ png,wpt,semantic_tree,semantic_tree_text,diff --git a/src/browser/markdown.zig b/src/browser/markdown.zigindex 3a68e41db..f8934cd58 100644--- a/src/browser/markdown.zig+++ b/src/browser/markdown.zig@@ -114,7 +114,7 @@ fn isLayoutBlock(tag: Element.Tag) bool {};}-fn isStandaloneAnchor(el: *Element) bool {+pub fn isStandaloneAnchor(el: *Element) bool {const node = el.asNode();const parent = node.parentNode() orelse return false;const parent_el = parent.is(Element) orelse return false;@@ -154,12 +154,12 @@ fn getAnchorLabel(el: *Element) ?[]const u8 {return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title"));}-const ContentInfo = struct {+pub const ContentInfo = struct {has_visible: bool,has_block: bool,};-fn analyzeContent(root: *Node) ContentInfo {+pub fn analyzeContent(root: *Node) ContentInfo {var result: ContentInfo = .{ .has_visible = false, .has_block = false };var tw = TreeWalker.FullExcludeSelf.init(root, .{});while (tw.next()) |node| {diff --git a/src/browser/screenshot.zig b/src/browser/screenshot.zignew file mode 100644index 000000000..86498b4f8--- /dev/null+++ b/src/browser/screenshot.zig@@ -0,0 +1,852 @@+// 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/>.++const std = @import("std");+const lp = @import("lightpanda");++const Base64Writer = @import("../Base64Writer.zig");+const isAllWhitespace = @import("../string.zig").isAllWhitespace;++const Frame = @import("Frame.zig");+const markdown = @import("markdown.zig");++const Node = @import("webapi/Node.zig");+const Element = @import("webapi/Element.zig");+const Slot = @import("webapi/element/html/Slot.zig");++const log = lp.log;+const Allocator = std.mem.Allocator;++pub const Opts = struct {+ width: u32,+ height: u32 = 0,+ clip: ?Clip = null,+ scale: f32 = 1.0,++ pub const Clip = struct {+ x: f32,+ y: f32,+ width: f32,+ height: f32,+ };+};++pub fn png(node: *Node, opts: Opts, writer: *std.Io.Writer, frame: *Frame) !u32 {+ const prepared = try prepare(node, opts, frame);+ return prepared.write(writer);+}++// get the height of the PNG if we were to render it.+pub fn contentHeight(node: *Node, width: u32, frame: *Frame) !u32 {+ const prepared = try prepare(node, .{ .width = width }, frame);+ var discard: std.Io.Writer.Discarding = .init(&.{});+ return prepared.render(&discard.writer, RENDER_MEASURE_ONLY);+}++// 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(node: *Node, opts: Opts, frame: *Frame) !Prepared {+ if (opts.width == 0 or !(opts.scale > 0 and opts.scale <= 8)) {+ return error.InvalidScreenshotOptions;+ }+ var builder: Builder = .{+ .frame = frame,+ .arena = frame.local_arena,+ };+ try builder.render(node);+ try builder.closeBlock();+ return .{ .blocks = builder.blocks.items, .opts = opts };+}++pub const Prepared = struct {+ opts: Opts,+ blocks: []const LpBlock,++ pub fn write(self: *const Prepared, writer: *std.Io.Writer) std.Io.Writer.Error!u32 {+ return self.render(writer, 0);+ }++ fn render(self: *const Prepared, writer: *std.Io.Writer, flags: u32) std.Io.Writer.Error!u32 {+ const opts = self.opts;+ const clip = opts.clip orelse Opts.Clip{ .x = 0, .y = 0, .width = 0, .height = 0 };+ var content_height: u32 = 0;+ var sink: Sink = .{ .writer = writer };+ const rc = lp_render_png(self.blocks.ptr, self.blocks.len, .{+ .width = opts.width,+ .height = opts.height,+ .clip_x = clip.x,+ .clip_y = clip.y,+ .clip_w = clip.width,+ .clip_h = clip.height,+ .scale = opts.scale,+ .flags = flags,+ }, &content_height, &sink, Sink.write);++ if (rc == 0) {+ return content_height;+ }++ log.err(.browser, "screenshot render", .{ .rc = rc });+ return error.WriteFailed;+ }++ // Serializes as a base64 string, streamed: no PNG or base64 buffer.+ 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();+ }+};++const Sink = struct {+ writer: *std.Io.Writer,++ fn write(ctx: *anyopaque, data: [*]const u8, len: usize) callconv(.c) bool {+ const self: *Sink = @ptrCast(@alignCast(ctx));+ self.writer.writeAll(data[0..len]) catch return false;+ return true;+ }+};++// Mirrors the C ABI in src/rust/render/lib.rs.+const LpSpan = extern struct {+ text: [*]const u8,+ len: usize,+ flags: u32,+ color: u32,+};++const LpBlock = extern struct {+ spans: [*]const LpSpan,+ spans_len: usize,+ marker: [*]const u8,+ marker_len: usize,+ kind: Kind,+ level: u8,+ list_depth: u8,+ quote_depth: u8,+ flags: u8,++ const Kind = enum(u8) {+ paragraph = 0,+ heading = 1,+ pre = 2,+ rule = 3,+ };+};++const LpRenderOpts = extern struct {+ width: u32,+ height: u32,+ clip_x: f32,+ clip_y: f32,+ clip_w: f32,+ clip_h: f32,+ scale: f32,+ flags: u32,+};++const SPAN_BOLD: u32 = 1 << 0;+const SPAN_ITALIC: u32 = 1 << 1;+const SPAN_UNDERLINE: u32 = 1 << 2;+const SPAN_MONO: u32 = 1 << 3;+const SPAN_STRIKE: u32 = 1 << 4;+const SPAN_HAS_COLOR: u32 = 1 << 5;++const BLOCK_TIGHT: u8 = 1 << 0;++const RENDER_MEASURE_ONLY: u32 = 1 << 0;++const LINK_COLOR: u32 = 0x1a0dab;+const MUTED_COLOR: u32 = 0x6b6b6b;++extern "c" fn lp_render_png(+ blocks: [*]const LpBlock,+ blocks_len: usize,+ opts: LpRenderOpts,+ content_height: *u32,+ ctx: *anyopaque,+ write: *const fn (ctx: *anyopaque, data: [*]const u8, len: usize) callconv(.c) bool,+) i32;++const Builder = struct {+ frame: *Frame,+ arena: Allocator,++ blocks: std.ArrayList(LpBlock) = .empty,++ // The block being built.+ block_open: bool = false,+ kind: LpBlock.Kind = .paragraph,+ level: u8 = 0,+ marker: []const u8 = "",+ spans: std.ArrayList(LpSpan) = .empty,+ // Text of the span being built, and the flags it was started with.+ text: std.ArrayList(u8) = .empty,+ text_flags: u32 = 0,+ text_color: u32 = 0,+ has_content: bool = false,+ pending_space: bool = false,+ // Nothing appended since an <a> closed. Adjacent links with no whitespace+ // between them (nav bars) would otherwise fuse into one word.+ after_anchor: bool = false,++ // Set by <li>; consumed by the first block it produces.+ pending_marker: []const u8 = "",++ // Inline style nesting.+ bold: u8 = 0,+ italic: u8 = 0,+ underline: u8 = 0,+ mono: u8 = 0,+ strike: u8 = 0,+ link: u8 = 0,+ muted: u8 = 0,+ // Nearest enclosing <pre>, for whitespace preservation.+ pre_node: ?*Node = null,++ quote_depth: u8 = 0,+ list_depth: u8 = 0,+ list_stack: [16]ListState = undefined,+ // Blocks closed while > 0 get list-like vertical spacing.+ tight: u8 = 0,++ // When there's a slot-attribute, we skip rendering, unless this flag has+ // been set to true.+ force_slot: bool = false,++ const ListState = struct {+ ordered: bool,+ index: u32,+ };++ const Error = Allocator.Error;++ fn currentFlags(self: *const Builder) u32 {+ var flags: u32 = 0;+ if (self.bold > 0) {+ flags |= SPAN_BOLD;+ }+ if (self.italic > 0) {+ flags |= SPAN_ITALIC;+ }+ if (self.underline > 0 or self.link > 0) {+ flags |= SPAN_UNDERLINE;+ }+ if (self.mono > 0) {+ flags |= SPAN_MONO;+ }+ if (self.strike > 0) {+ flags |= SPAN_STRIKE;+ }+ if (self.link > 0 or self.muted > 0) {+ flags |= SPAN_HAS_COLOR;+ }+ return flags;+ }++ fn currentColor(self: *const Builder) u32 {+ if (self.link > 0) {+ return LINK_COLOR;+ }+ if (self.muted > 0) {+ return MUTED_COLOR;+ }+ return 0;+ }++ fn openBlock(self: *Builder, kind: LpBlock.Kind, level: u8) Error!void {+ try self.closeBlock();+ self.block_open = true;+ self.kind = kind;+ self.level = level;+ self.marker = self.pending_marker;+ self.pending_marker = "";+ }++ fn ensureBlock(self: *Builder) Error!void {+ if (!self.block_open) {+ try self.openBlock(.paragraph, 0);+ }+ }++ fn flushSpan(self: *Builder) Error!void {+ if (self.text.items.len == 0) return;+ const copy = try self.arena.dupe(u8, self.text.items);+ try self.spans.append(self.arena, .{+ .text = copy.ptr,+ .len = copy.len,+ .flags = self.text_flags,+ .color = self.text_color,+ });+ self.text.clearRetainingCapacity();+ }++ fn closeBlock(self: *Builder) Error!void {+ if (!self.block_open) return;+ try self.flushSpan();+ if (self.spans.items.len > 0 or self.kind == .rule) {+ const spans = try self.arena.dupe(LpSpan, self.spans.items);+ try self.blocks.append(self.arena, .{+ .spans = spans.ptr,+ .spans_len = spans.len,+ .marker = self.marker.ptr,+ .marker_len = self.marker.len,+ .kind = self.kind,+ .level = self.level,+ .list_depth = self.list_depth,+ .quote_depth = self.quote_depth,+ .flags = if (self.tight > 0) BLOCK_TIGHT else 0,+ });+ } else if (self.marker.len > 0) {+ // Empty <li>: keep the marker for whatever block comes next.+ self.pending_marker = self.marker;+ }+ self.block_open = false;+ self.marker = "";+ self.spans.clearRetainingCapacity();+ self.has_content = false;+ self.pending_space = false;+ self.after_anchor = false;+ }++ // Appends already-collapsed text in the current inline style.+ fn append(self: *Builder, text: []const u8) Error!void {+ 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)) {+ try self.flushSpan();+ }+ self.text_flags = flags;+ self.text_color = color;+ try self.text.appendSlice(self.arena, text);+ self.has_content = true;+ self.after_anchor = false;+ }++ fn appendWord(self: *Builder, word: []const u8) Error!void {+ if (self.pending_space and self.has_content) {+ try self.appendSpace();+ }+ self.pending_space = false;+ try self.append(word);+ }++ // A collapsed space between two runs takes only the styling both sides+ // share, so an underline never spills onto the gap before a link.+ fn appendSpace(self: *Builder) Error!void {+ var flags = self.text_flags & self.currentFlags();+ var color = self.text_color;+ if (flags & SPAN_HAS_COLOR != 0 and color != self.currentColor()) {+ flags &= ~SPAN_HAS_COLOR;+ }+ if (flags & SPAN_HAS_COLOR == 0) color = 0;++ if (flags != self.text_flags or color != self.text_color) {+ try self.flushSpan();+ self.text_flags = flags;+ self.text_color = color;+ }+ try self.text.append(self.arena, ' ');+ }++ fn render(self: *Builder, node: *Node) Error!void {+ switch (node._type) {+ .document, .document_fragment => try self.renderChildren(node),+ .element => try self.renderElement(node.subtype(Node.Element)),+ .cdata => {+ if (node.is(Node.CData.Text)) |_| {+ var text = node.subtype(Node.CData).getData().str();+ if (self.pre_node) |pre| {+ if (node.parentNode() == pre and node.nextSibling() == null) {+ text = std.mem.trimEnd(u8, text, " \t\r\n");+ }+ }+ try self.renderText(text);+ }+ },+ else => {},+ }+ }++ fn renderChildren(self: *Builder, parent: *Node) Error!void {+ var it = parent.childrenIterator();+ while (it.next()) |child| {+ try self.render(child);+ }+ }++ fn renderSlotContent(self: *Builder, slot: *Slot) Error!void {+ const assigned = slot.assignedNodes(null, self.frame) catch return;+ if (assigned.len == 0) {+ return self.renderChildren(slot.asNode());+ }+ for (assigned) |node| {+ self.force_slot = true;+ try self.render(node);+ }+ self.force_slot = false;+ }++ fn renderText(self: *Builder, text: []const u8) Error!void {+ if (text.len == 0) return;++ if (self.pre_node != null) {+ return self.append(text);+ }++ if (isAllWhitespace(text)) {+ self.pending_space = true;+ return;+ }++ if (std.ascii.isWhitespace(text[0])) {+ self.pending_space = true;+ }+ var it = std.mem.tokenizeAny(u8, text, " \t\n\r");+ while (it.next()) |word| {+ try self.appendWord(word);+ self.pending_space = true;+ }+ self.pending_space = std.ascii.isWhitespace(text[text.len - 1]);+ }++ fn renderElement(self: *Builder, el: *Element) Error!void {+ const force_slot = self.force_slot;+ self.force_slot = false;++ const tag = el.getTag();+ if (tag.isMetadata() or tag == .svg) {+ return;+ }++ if (!force_slot and el.getAttributeSafe(comptime .wrap("slot")) != null) {+ return;+ }++ switch (tag) {+ .h1, .h2, .h3, .h4, .h5, .h6 => {+ const level: u8 = switch (tag) {+ .h1 => 1,+ .h2 => 2,+ .h3 => 3,+ .h4 => 4,+ .h5 => 5,+ else => 6,+ };+ try self.openBlock(.heading, level);+ try self.renderContent(el);+ return self.closeBlock();+ },+ .pre => {+ try self.openBlock(.pre, 0);+ const prev = self.pre_node;+ self.pre_node = el.asNode();+ try self.renderContent(el);+ self.pre_node = prev;+ return self.closeBlock();+ },+ .hr => {+ try self.openBlock(.rule, 0);+ return self.closeBlock();+ },+ .br => {+ if (self.pre_node != null) return self.append("\n");+ if (!self.has_content) return;+ // A hard break within the block: parley honors '\n'.+ try self.append("\n");+ self.pending_space = false;+ return;+ },+ .ul, .ol => {+ try self.closeBlock();+ const pushed = self.list_depth < self.list_stack.len;+ if (pushed) {+ self.list_stack[self.list_depth] = .{ .ordered = tag == .ol, .index = 1 };+ self.list_depth += 1;+ }+ try self.renderContent(el);+ try self.closeBlock();+ if (pushed) self.list_depth -= 1;+ return;+ },+ .li => {+ try self.closeBlock();+ // A stray <li> outside any list still gets a bullet.+ const stray = self.list_depth == 0;+ if (stray) self.list_depth = 1;+ if (!stray and self.list_stack[self.list_depth - 1].ordered) {+ const state = &self.list_stack[self.list_depth - 1];+ self.pending_marker = try std.fmt.allocPrint(self.arena, "{d}.", .{state.index});+ state.index += 1;+ } else {+ self.pending_marker = "•";+ }+ try self.renderContent(el);+ try self.closeBlock();+ self.pending_marker = "";+ if (stray) self.list_depth = 0;+ return;+ },+ .blockquote => {+ try self.closeBlock();+ self.quote_depth +|= 1;+ try self.renderContent(el);+ try self.closeBlock();+ self.quote_depth -= 1;+ return;+ },+ .img => {+ const alt = el.getAttributeSafe(comptime .wrap("alt")) orelse return;+ if (isAllWhitespace(alt)) return;+ self.italic += 1;+ self.muted += 1;+ try self.renderText(alt);+ self.muted -= 1;+ self.italic -= 1;+ return;+ },+ .input => {+ const type_attr = el.getAttributeSafe(comptime .wrap("type")) orelse return;+ if (std.ascii.eqlIgnoreCase(type_attr, "checkbox")) {+ const checked = el.getAttributeSafe(comptime .wrap("checked")) != null;+ try self.appendWord(if (checked) "☑" else "☐");+ self.pending_space = true;+ }+ return;+ },+ .anchor => {+ const href = el.getAttributeSafe(comptime .wrap("href"));+ const label = el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title"));+ const info = markdown.analyzeContent(el.asNode());+ if (!info.has_visible and label == null) return;++ // Same split as markdown: an anchor wrapping blocks, or one+ // sitting among element-only siblings (nav bars, post lists),+ // gets its own tight block instead of flowing inline.+ const standalone = info.has_block or markdown.isStandaloneAnchor(el);+ if (standalone) {+ try self.closeBlock();+ self.tight += 1;+ }+ if (self.after_anchor) self.pending_space = true;+ if (href != null) self.link += 1;+ if (info.has_visible) {+ try self.renderContent(el);+ } else {+ try self.renderText(label.?);+ }+ if (href != null) self.link -= 1;+ if (standalone) {+ try self.closeBlock();+ self.tight -= 1;+ } else {+ self.after_anchor = true;+ }+ return;+ },+ .slot => return self.renderSlotContent(el.as(Slot)),+ .td, .th => {+ if (self.has_content) {+ self.pending_space = true;+ self.muted += 1;+ try self.appendWord("|");+ self.muted -= 1;+ self.pending_space = true;+ }+ if (tag == .th) self.bold += 1;+ try self.renderContent(el);+ if (tag == .th) self.bold -= 1;+ self.pending_space = true;+ return;+ },+ else => {},+ }++ const block = tag.isBlock() or switch (tag) {+ .tr, .dt, .dd, .details, .summary, .caption, .legend, .option, .textarea => true,+ else => false,+ };+ if (block) try self.closeBlock();++ switch (tag) {+ .b, .strong => self.bold += 1,+ .i, .em, .dfn => self.italic += 1,+ .ins => self.underline += 1,+ .s, .del => self.strike += 1,+ .code => self.mono += 1,+ else => {},+ }+ try self.renderContent(el);+ switch (tag) {+ .b, .strong => self.bold -= 1,+ .i, .em, .dfn => self.italic -= 1,+ .ins => self.underline -= 1,+ .s, .del => self.strike -= 1,+ .code => self.mono -= 1,+ else => {},+ }++ if (block) {+ try self.closeBlock();+ }+ }++ // Composed tree: a shadow host renders its shadow tree in place of its+ // light-DOM children (visible only through <slot>).+ fn renderContent(self: *Builder, el: *Element) Error!void {+ if (el.hostedShadowRoot(self.frame)) |shadow| {+ return self.renderChildren(shadow.asNode());+ }+ return self.renderChildren(el.asNode());+ }+};++const testing = @import("../testing.zig");+fn testPng(html: []const u8, width: u32) ![]const u8 {+ 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(), html);++ var aw: std.Io.Writer.Allocating = .init(testing.allocator);+ errdefer aw.deinit();+ _ = try png(div.asNode(), .{ .width = width }, &aw.writer, frame);+ return aw.toOwnedSlice();+}++test "browser.screenshot: png signature and dimensions" {+ defer testing.test_session.closeAllPages();+ const out = try testPng("<h1>Title</h1><p>Hello <b>world</b> <a href='/x'>link</a></p>", 640);+ defer testing.allocator.free(out);++ try testing.expectEqual(true, out.len > 100);+ try testing.expectEqual("\x89PNG\r\n\x1a\n", out[0..8]);+ // IHDR width/height are big-endian at offsets 16 and 20.+ try testing.expectEqual(640, std.mem.readInt(u32, out[16..20], .big));+ const height = std.mem.readInt(u32, out[20..24], .big);+ // Two blocks plus margins.+ try testing.expectEqual(true, height > 60 and height < 200);+}++test "browser.screenshot: fixed height, clip and scale" {+ 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(), "<p>one</p><p>two</p><p>three</p>");++ var aw: std.Io.Writer.Allocating = .init(testing.allocator);+ defer aw.deinit();+ const content_height = try png(div.asNode(), .{ .width = 300, .height = 50, .scale = 2.0 }, &aw.writer, frame);+ try testing.expectEqual(true, content_height > 50);+ try testing.expectEqual(600, std.mem.readInt(u32, aw.written()[16..20], .big));+ try testing.expectEqual(100, std.mem.readInt(u32, aw.written()[20..24], .big));++ try testing.expectEqual(content_height, try contentHeight(div.asNode(), 300, frame));++ aw.clearRetainingCapacity();+ _ = try png(div.asNode(), .{+ .width = 300,+ .clip = .{ .x = 10, .y = 10, .width = 100, .height = 40 },+ }, &aw.writer, frame);+ try testing.expectEqual(100, std.mem.readInt(u32, aw.written()[16..20], .big));+ try testing.expectEqual(40, std.mem.readInt(u32, aw.written()[20..24], .big));+}++test "browser.screenshot: json streams base64" {+ 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(), "<p>hello</p>");++ const prepared = try prepare(div.asNode(), .{ .width = 200 }, frame);++ var raw: std.Io.Writer.Allocating = .init(testing.allocator);+ defer raw.deinit();+ _ = try prepared.write(&raw.writer);++ var json: std.Io.Writer.Allocating = .init(testing.allocator);+ defer json.deinit();+ try std.json.Stringify.value(.{ .data = prepared }, .{}, &json.writer);++ const enc = std.base64.standard.Encoder;+ const expected = try testing.allocator.alloc(u8, enc.calcSize(raw.written().len) + 11);+ defer testing.allocator.free(expected);+ @memcpy(expected[0..9], "{\"data\":\"");+ _ = enc.encode(expected[9 .. expected.len - 2], raw.written());+ @memcpy(expected[expected.len - 2 ..], "\"}");+ try testing.expectString(expected, json.written());+}++test "browser.screenshot: block extraction" {+ 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(),+ \\<h2> Head ing </h2>+ \\<p>Some <b>bold <i>both</i></b> text <a href="/l">a link</a><span> tail</span></p>+ \\<ul><li>one</li><li><p>two</p><ol><li>nested</li></ol></li></ul>+ \\<blockquote>quote</blockquote>+ \\<pre> keep+ \\ this</pre>+ \\<hr>+ \\<div><img alt="a picture"><span></span></div>+ \\<table><tr><th>A</th><td>B</td></tr><tr><td>C</td></tr></table>+ \\<div> </div>+ );++ var builder: Builder = .{ .arena = frame.call_arena, .frame = frame };+ try builder.render(div.asNode());+ try builder.closeBlock();++ const blocks = builder.blocks.items;+ try testing.expectEqual(11, blocks.len);++ const S = struct {+ fn text(b: LpBlock, arena: Allocator) ![]const u8 {+ var out: std.ArrayList(u8) = .empty;+ for (b.spans[0..b.spans_len]) |s| try out.appendSlice(arena, s.text[0..s.len]);+ return out.items;+ }+ };+ const arena = frame.call_arena;++ try testing.expectEqual(.heading, blocks[0].kind);+ try testing.expectEqual(2, blocks[0].level);+ try testing.expectEqual("Head ing", try S.text(blocks[0], arena));++ try testing.expectEqual(.paragraph, blocks[1].kind);+ try testing.expectEqual("Some bold both text a link tail", try S.text(blocks[1], arena));+ try testing.expectEqual(6, blocks[1].spans_len);+ try testing.expectEqual("Some ", blocks[1].spans[0].text[0..blocks[1].spans[0].len]);+ try testing.expectEqual(SPAN_BOLD, blocks[1].spans[1].flags);+ try testing.expectEqual("bold ", blocks[1].spans[1].text[0..blocks[1].spans[1].len]);+ try testing.expectEqual(SPAN_BOLD | SPAN_ITALIC, blocks[1].spans[2].flags);+ try testing.expectEqual("both", blocks[1].spans[2].text[0..blocks[1].spans[2].len]);+ try testing.expectEqual(0, blocks[1].spans[3].flags);+ try testing.expectEqual(" text ", blocks[1].spans[3].text[0..blocks[1].spans[3].len]);+ 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(" tail", blocks[1].spans[5].text[0..blocks[1].spans[5].len]);++ try testing.expectEqual("one", try S.text(blocks[2], arena));+ try testing.expectEqual(1, blocks[2].list_depth);+ try testing.expectEqual("•", blocks[2].marker[0..blocks[2].marker_len]);+ try testing.expectEqual("two", try S.text(blocks[3], arena));+ try testing.expectEqual("•", blocks[3].marker[0..blocks[3].marker_len]);+ try testing.expectEqual("nested", try S.text(blocks[4], arena));+ try testing.expectEqual(2, blocks[4].list_depth);+ try testing.expectEqual("1.", blocks[4].marker[0..blocks[4].marker_len]);++ try testing.expectEqual("quote", try S.text(blocks[5], arena));+ try testing.expectEqual(1, blocks[5].quote_depth);+ try testing.expectEqual(0, blocks[5].list_depth);++ try testing.expectEqual(.pre, blocks[6].kind);+ try testing.expectEqual(" keep\n this", try S.text(blocks[6], arena));+ try testing.expectEqual(.rule, blocks[7].kind);+ try testing.expectEqual("a picture", try S.text(blocks[8], arena));+ try testing.expectEqual(SPAN_ITALIC | SPAN_HAS_COLOR, blocks[8].spans[0].flags);+ try testing.expectEqual("A | B", try S.text(blocks[9], arena));+ try testing.expectEqual(SPAN_BOLD, blocks[9].spans[0].flags);+ try testing.expectEqual("C", try S.text(blocks[10], arena));+}++test "browser.screenshot: adjacent anchors" {+ 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);+ // Inside <p> (not a layout block) so markdown's standalone rule doesn't+ // apply and the anchors flow inline.+ try Frame.parse.htmlAsChildren(frame, div.asNode(),+ \\<p><a href="/a">Log In</a><a href="/b">Sign Up</a><b>!</b> see <a href="/c">this</a>.</p>+ );++ var builder: Builder = .{ .arena = frame.call_arena, .frame = frame };+ try builder.render(div.asNode());+ try builder.closeBlock();+ const blocks = builder.blocks.items;+ try testing.expectEqual(1, blocks.len);+ var out: std.ArrayList(u8) = .empty;+ for (blocks[0].spans[0..blocks[0].spans_len]) |sp| try out.appendSlice(frame.call_arena, sp.text[0..sp.len]);+ try testing.expectString("Log In Sign Up! see this.", out.items);+}++test "browser.screenshot: standalone anchors get their own block" {+ 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(),+ \\<nav><a href="/a">bsky</a><a href="/b">rss</a></nav>+ \\<div><a href="/p"><h3>Post title</h3><span>Aug 04</span></a></div>+ \\<p>inline <a href="/x">link</a> here</p>+ );++ var builder: Builder = .{ .arena = frame.call_arena, .frame = frame };+ try builder.render(div.asNode());+ try builder.closeBlock();+ const blocks = builder.blocks.items;+ try testing.expectEqual(5, blocks.len);+ try testing.expectString("bsky", blocks[0].spans[0].text[0..blocks[0].spans[0].len]);+ try testing.expectEqual(BLOCK_TIGHT, blocks[0].flags);+ try testing.expectString("rss", blocks[1].spans[0].text[0..blocks[1].spans[0].len]);+ try testing.expectEqual(.heading, blocks[2].kind);+ try testing.expectString("Post title", blocks[2].spans[0].text[0..blocks[2].spans[0].len]);+ try testing.expectEqual(SPAN_UNDERLINE | SPAN_HAS_COLOR, blocks[2].spans[0].flags);+ try testing.expectString("Aug 04", blocks[3].spans[0].text[0..blocks[3].spans[0].len]);+ try testing.expectEqual(3, blocks[4].spans_len);+ try testing.expectEqual(0, blocks[4].flags);+}++test "browser.screenshot: shadow dom and slots" {+ 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 div.setHTMLUnsafe(+ \\<x-host><template shadowrootmode="open"><p>shadow <slot></slot></p></template>light</x-host>+ , frame);++ var builder: Builder = .{ .arena = frame.call_arena, .frame = frame };+ try builder.render(div.asNode());+ try builder.closeBlock();+ const blocks = builder.blocks.items;+ try testing.expectEqual(1, blocks.len);+ try testing.expectEqual(1, blocks[0].spans_len);+ try testing.expectEqual("shadow light", blocks[0].spans[0].text[0..blocks[0].spans[0].len]);+}diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zigindex 15ae70a0a..c77881705 100644--- a/src/cdp/domains/page.zig+++ b/src/cdp/domains/page.zig@@ -20,7 +20,6 @@const std = @import("std");const lp = @import("lightpanda");-const screenshot_png = @embedFile("screenshot.png");const screenshot_pdf = @embedFile("screenshot.pdf");const id = @import("../id.zig");@@ -970,7 +969,6 @@ fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSizreturn buf;}-// Return a fake screenshotfn captureScreenshot(cmd: *CDP.Command) !void {const Params = struct {format: ?[]const u8 = "png",@@ -983,7 +981,6 @@ fn captureScreenshot(cmd: *CDP.Command) !void {const params = try cmd.params(Params) orelse Params{};const format = params.format orelse "png";-if (!std.mem.eql(u8, format, "png")) {log.warn(.not_implemented, "Page.captureScreenshot params", .{ .format = format });return cmd.sendError(-32000, "unsupported screenshot format.", .{});@@ -991,20 +988,28 @@ fn captureScreenshot(cmd: *CDP.Command) !void {if (params.quality != null) {log.warn(.not_implemented, "Page.captureScreenshot params", .{ .quality = params.quality });}- if (params.clip != null) {- log.warn(.not_implemented, "Page.captureScreenshot params", .{ .clip = params.clip });- }- if (params.fromSurface orelse false or params.captureBeyondViewport orelse false or params.optimizeForSpeed orelse false) {- log.warn(.not_implemented, "Page.captureScreenshot params", .{- .fromSurface = params.fromSurface,- .captureBeyondViewport = params.captureBeyondViewport,- .optimizeForSpeed = params.optimizeForSpeed,- });++ const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;+ const frame = bc.mainFrame() orelse return error.FrameNotLoaded;+ const viewport = cmd.cdp.browser.getViewport();++ var opts: lp.screenshot.Opts = .{+ .width = viewport.width,+ .height = if (params.captureBeyondViewport orelse false) 0 else viewport.height,+ };+ if (params.clip) |clip| {+ opts.clip = .{+ .x = @floatCast(clip.x),+ .y = @floatCast(clip.y),+ .width = @floatCast(clip.width),+ .height = @floatCast(clip.height),+ };+ opts.scale = @floatCast(clip.scale);}- return cmd.sendResult(.{- .data = base64Encode(screenshot_png),- }, .{});+ // Prepared streams itself as base64 straight into the outgoing message.+ const shot = try lp.screenshot.prepare(frame.window._document.asNode(), opts, frame);+ return cmd.sendResult(.{ .data = shot }, .{});}// Return a fake pdf@@ -1022,6 +1027,15 @@ fn getLayoutMetrics(cmd: *CDP.Command) !void {const width = viewport.width;const height = viewport.height;+ // Full-page height as our text rendering lays it out, so a fullPage+ // screenshot (getLayoutMetrics → captureScreenshot) is consistent.+ const content_height: u32 = blk: {+ const bc = cmd.browser_context orelse break :blk height;+ const frame = bc.mainFrame() orelse break :blk height;+ const h = lp.screenshot.contentHeight(frame.window._document.asNode(), width, frame) catch break :blk height;+ break :blk @max(height, h);+ };+return cmd.sendResult(.{.layoutViewport = .{.pageX = 0,@@ -1043,7 +1057,7 @@ fn getLayoutMetrics(cmd: *CDP.Command) !void {.x = 0,.y = 0,.width = width,- .height = height,+ .height = content_height,},.cssLayoutViewport = .{.pageX = 0,@@ -1065,7 +1079,7 @@ fn getLayoutMetrics(cmd: *CDP.Command) !void {.x = 0,.y = 0,.width = width,- .height = height,+ .height = content_height,},}, .{});}@@ -1266,14 +1280,46 @@ test "cdp.frame: captureScreenshot" {try ctx.expectSentError(-32000, "unsupported screenshot format.", .{ .id = 10 });}+ _ = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });++ // Keep the payload small: the test transport buffers 32KB frames.+ try ctx.processMessage(.{ .id = 10, .method = "Emulation.setDeviceMetricsOverride", .params = .{ .width = 400, .height = 300 } });+{+ // Viewport-sized: the PNG header carries the size.try ctx.processMessage(.{ .id = 11, .method = "Page.captureScreenshot" });- try ctx.expectSentResult(.{- .data = base64Encode(screenshot_png),- }, .{ .id = 11 });+ const png = try screenshotResult(&ctx, 11);+ try testing.expectEqual(400, std.mem.readInt(u32, png[16..20], .big));+ try testing.expectEqual(300, std.mem.readInt(u32, png[20..24], .big));+ }++ {+ // clip + scale crops after layout and scales the raster.+ try ctx.processMessage(.{ .id = 12, .method = "Page.captureScreenshot", .params = .{+ .clip = .{ .x = 0, .y = 0, .width = 100, .height = 40, .scale = 2 },+ } });+ const png = try screenshotResult(&ctx, 12);+ try testing.expectEqual(200, std.mem.readInt(u32, png[16..20], .big));+ try testing.expectEqual(80, std.mem.readInt(u32, png[20..24], .big));}}+fn screenshotResult(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("\x89PNG\r\n\x1a\n", out[0..8]);+ return out;+ }+ return error.ResultNotFound;+}+test "cdp.frame: printToPDF" {testing.silenceLog(&.{.not_implemented});diff --git a/src/cdp/domains/screenshot.png b/src/cdp/domains/screenshot.pngdeleted file mode 100644index 9ad87e120..000000000Binary files a/src/cdp/domains/screenshot.png and /dev/null differdiff --git a/src/cdp/testing.zig b/src/cdp/testing.zigindex 8133f32bb..bd05569e2 100644--- a/src/cdp/testing.zig+++ b/src/cdp/testing.zig@@ -36,8 +36,9 @@ pub const pageTest = base.pageTest;pub const newString = base.newString;pub const expectLog = base.expectLog;pub const silenceLog = base.silenceLog;+pub const arena_allocator = base.arena_allocator;-const TestContext = struct {+pub const TestContext = struct {read_at: usize = 0,read_buf: [1024 * 32]u8 = undefined,cdp_: CDP = undefined,diff --git a/src/help.zon b/src/help.zonindex 4675d1318..d1f06d80e 100644--- a/src/help.zon+++ b/src/help.zon@@ -68,6 +68,8 @@\\ Allowed values:\\ html Serialized HTML of the DOM.\\ markdown Converts content to Markdown.+ \\ png Text-only rendering of the page as a+ \\ PNG image (base64 with --json).\\ semantic_tree JSON-serialized semantic tree.\\ semantic_tree_text Pruned plain-text semantic tree.\\ --inject-script <EXPR>diff --git a/src/lightpanda.zig b/src/lightpanda.zigindex f6fcd03c8..d0c7f1406 100644--- a/src/lightpanda.zig+++ b/src/lightpanda.zig@@ -38,6 +38,8 @@ pub const Session = @import("browser/Session.zig");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 Base64Writer = @import("Base64Writer.zig");pub const SemanticTree = @import("SemanticTree.zig");pub const CDPNode = @import("cdp/Node.zig");pub const interactive = @import("browser/interactive.zig");@@ -340,15 +342,23 @@ pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: Fettry writer.writeByte(',');}+ const frame = page.frame();+ if (opts.dump_mode == .png and frame != null) {+ // Binary payload: streams itself into the envelope as base64.+ const shot = try screenshot.prepare(frame.?.window._document.asNode(), .{+ .width = frame.?._page.getViewport().width,+ }, frame.?);+ try writeJsonEnvelope(writer, frame, opts.dump_mode, shot);+ continue;+ }+var aw: std.Io.Writer.Allocating = .init(app.allocator);defer aw.deinit();- if (opts.dump_mode) |mode| blk: {- const frame = page.frame() orelse break :blk;- try dumpContent(app, mode, opts.dump, frame, &aw.writer);+ if (opts.dump_mode) |mode| {+ if (frame) |f| try dumpContent(app, mode, opts.dump, f, &aw.writer);}-- try writeJsonEnvelope(writer, page.frame(), opts.dump_mode, aw.written());+ try writeJsonEnvelope(writer, frame, opts.dump_mode, aw.written());}if (wrap) {try writer.writeAll("]}");@@ -372,6 +382,9 @@ fn dumpContent(app: *App, mode: Config.DumpFormat, dump_opts: dump.Opts, frame:switch (mode) {.html => try dump.root(frame.window._document, dump_opts, writer, frame),.markdown => try markdown.dump(frame.window._document.asNode(), .{}, writer, frame),+ .png => _ = try screenshot.png(frame.window._document.asNode(), .{+ .width = frame._page.getViewport().width,+ }, writer, frame),.semantic_tree, .semantic_tree_text => {var registry = CDPNode.Registry.init(app.allocator);defer registry.deinit();@@ -407,7 +420,7 @@ pub fn checkVersion(allocator: std.mem.Allocator, config: *const Config) !void {// Writes a single page's result object. Framing (the enclosing array and any// separators / trailing newline) is the caller's responsibility.-fn writeJsonEnvelope(writer: *std.Io.Writer, frame: ?*Frame, dump_mode: ?Config.DumpFormat, content: []const u8) !void {+fn writeJsonEnvelope(writer: *std.Io.Writer, frame: ?*Frame, dump_mode: ?Config.DumpFormat, content: anytype) !void {const meta: ?Frame.HttpMetadata = if (frame) |f| f.httpMetadata() else null;try std.json.Stringify.value(.{.url = if (meta) |m| m.url else "",diff --git a/src/html5ever/Cargo.lock b/src/rust/Cargo.locksimilarity index 67%rename from src/html5ever/Cargo.lockrename to src/rust/Cargo.lockindex 72445a7c7..455440f4f 100644--- a/src/html5ever/Cargo.lock+++ b/src/rust/Cargo.lock@@ -2,6 +2,24 @@# It is not intended for manual editing.version = 4+[[package]]+name = "adler2"+version = "2.0.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"++[[package]]+name = "arrayref"+version = "0.3.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"++[[package]]+name = "arrayvec"+version = "0.7.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56"+[[package]]name = "autocfg"version = "1.4.0"@@ -14,6 +32,26 @@ version = "2.9.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"+[[package]]+name = "bytemuck"+version = "1.25.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797"+dependencies = [+ "bytemuck_derive",+]++[[package]]+name = "bytemuck_derive"+version = "1.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fc0e56a716f1e132ff6bf4bdac1c944a3fcdc1cae65f70a4a2a1ac3b401d2d1f"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 3.0.3",+]+[[package]]name = "cc"version = "1.2.39"@@ -30,6 +68,15 @@ version = "1.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"+[[package]]+name = "crc32fast"+version = "1.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"+dependencies = [+ "cfg-if",+]+[[package]]name = "displaydoc"version = "0.2.6"@@ -38,7 +85,7 @@ checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f"dependencies = ["proc-macro2","quote",- "syn",+ "syn 2.0.101",][[package]]@@ -56,12 +103,60 @@ version = "2.4.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6"+[[package]]+name = "fdeflate"+version = "0.3.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"+dependencies = [+ "simd-adler32",+]+[[package]]name = "find-msvc-tools"version = "0.1.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1ced73b1dacfc750a6db6c0a0c3a3853c8b41997e2e2c563dc90804ae6867959"+[[package]]+name = "flate2"+version = "1.1.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"+dependencies = [+ "crc32fast",+ "miniz_oxide",+]++[[package]]+name = "foldhash"+version = "0.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"++[[package]]+name = "font-types"+version = "0.12.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "75382bc7392ef10aad10935f92fc3db36d2d4dad0e5d96d8d65e04f89a07ec39"+dependencies = [+ "bytemuck",+]++[[package]]+name = "fontique"+version = "0.11.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6688bc1294fe7117d788937b6c53480169b29c566954af490830d4c09da9516a"+dependencies = [+ "hashbrown",+ "linebender_resource_handle",+ "memmap2",+ "parlance",+ "read-fonts",+ "smallvec",+]+[[package]]name = "form_urlencoded"version = "1.2.2"@@ -71,6 +166,27 @@ dependencies = ["percent-encoding",]+[[package]]+name = "harfrust"+version = "0.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c03d949a14aa089bbb282f7dd76a498a7f684428e4257202efc119ec010376f9"+dependencies = [+ "bitflags",+ "bytemuck",+ "read-fonts",+ "smallvec",+]++[[package]]+name = "hashbrown"+version = "0.17.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"+dependencies = [+ "foldhash",+]+[[package]]name = "html5ever"version = "0.39.0"@@ -95,6 +211,21 @@ dependencies = ["zerovec",]+[[package]]+name = "icu_locale"+version = "2.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d5a396343c7208121dc86e35623d3dfe19814a7613cfd14964994cdc9c9a2e26"+dependencies = [+ "icu_collections",+ "icu_locale_core",+ "icu_locale_data",+ "icu_provider",+ "potential_utf",+ "tinystr",+ "zerovec",+]+[[package]]name = "icu_locale_core"version = "2.2.0"@@ -103,11 +234,18 @@ checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29"dependencies = ["displaydoc","litemap",+ "serde","tinystr","writeable","zerovec",]+[[package]]+name = "icu_locale_data"+version = "2.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d5fdcc9ac77c6d74ff5cf6e65ef3181d6af32003b16fce3a77fb451d2f695993"+[[package]]name = "icu_normalizer"version = "2.2.0"@@ -156,6 +294,9 @@ checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421"dependencies = ["displaydoc","icu_locale_core",+ "log",+ "serde",+ "stable_deref_trait","writeable","yoke","zerofrom",@@ -163,6 +304,27 @@ dependencies = ["zerovec",]+[[package]]+name = "icu_segmenter"+version = "2.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5c0794db0b1a86193ac9c48768d0e6c52c54448e0870ad87907d456ee0dac964"+dependencies = [+ "icu_collections",+ "icu_locale",+ "icu_provider",+ "icu_segmenter_data",+ "potential_utf",+ "utf8_iter",+ "zerovec",+]++[[package]]+name = "icu_segmenter_data"+version = "2.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e4a2c462a4d927d512f5f882a033ddd62f33a05bb9f230d98f736ac3dc85938f"+[[package]]name = "idna"version = "1.1.0"@@ -191,20 +353,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"[[package]]-name = "litefetch-html5ever"+name = "lightpanda-ffi"+version = "0.1.0"+dependencies = [+ "lightpanda-html5ever",+ "lightpanda-render",+ "tikv-jemalloc-ctl",+ "tikv-jemallocator",+]++[[package]]+name = "lightpanda-html5ever"version = "0.1.0"dependencies = ["encoding_rs","html5ever","idna","string_cache",- "tikv-jemalloc-ctl",- "tikv-jemallocator","typed-arena","url","xml5ever",]+[[package]]+name = "lightpanda-render"+version = "0.1.0"+dependencies = [+ "fontique",+ "icu_provider",+ "parley",+ "png",+ "skrifa",+ "tiny-skia",+]++[[package]]+name = "linebender_resource_handle"+version = "0.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d4a5ff6bcca6c4867b1c4fd4ef63e4db7436ef363e0ad7531d1558856bae64f4"+[[package]]name = "litemap"version = "0.8.2"@@ -238,12 +426,37 @@ dependencies = ["web_atoms",]+[[package]]+name = "memmap2"+version = "0.9.11"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d1219ed1b7f229ee7104d281dd01d6802fe28bb6e95d292942c4daacdeb798c0"+dependencies = [+ "libc",+]++[[package]]+name = "miniz_oxide"+version = "0.8.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"+dependencies = [+ "adler2",+ "simd-adler32",+]+[[package]]name = "new_debug_unreachable"version = "1.0.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"+[[package]]+name = "once_cell"+version = "1.21.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"+[[package]]name = "parking_lot"version = "0.12.4"@@ -267,6 +480,39 @@ dependencies = ["windows-targets",]+[[package]]+name = "parlance"+version = "0.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4b6937eda350acc1a5d05872c3cbf99fe78619c269096e2be3d4a350058639d5"++[[package]]+name = "parley"+version = "0.11.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "22d2ff88bd3f7d68d1d9b09c7e6209f9a8e8c05088295140a2bcf2e9b17038c5"+dependencies = [+ "fontique",+ "harfrust",+ "hashbrown",+ "icu_normalizer",+ "icu_properties",+ "icu_segmenter",+ "linebender_resource_handle",+ "parlance",+ "parley_data",+ "skrifa",+]++[[package]]+name = "parley_data"+version = "0.11.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1567535334d6ba2d3cde19221ba9a7bd0fabb3cbd99046ddfb10ae061cfcc889"+dependencies = [+ "icu_properties",+]+[[package]]name = "paste"version = "1.0.15"@@ -318,12 +564,27 @@ dependencies = ["siphasher",]+[[package]]+name = "png"+version = "0.18.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61"+dependencies = [+ "bitflags",+ "crc32fast",+ "fdeflate",+ "flate2",+ "miniz_oxide",+]+[[package]]name = "potential_utf"version = "0.1.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564"dependencies = [+ "serde_core",+ "writeable","zerovec",]@@ -351,6 +612,17 @@ dependencies = ["proc-macro2",]+[[package]]+name = "read-fonts"+version = "0.41.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "046a7d674daf459825b32f5062056d6882db0d2f5a479fbd76ccfc870ac18709"+dependencies = [+ "bytemuck",+ "font-types",+ "once_cell",+]+[[package]]name = "redox_syscall"version = "0.5.12"@@ -368,22 +640,32 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"[[package]]name = "serde"-version = "1.0.219"+version = "1.0.229"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"+checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"+dependencies = [+ "serde_core",+ "serde_derive",+]++[[package]]+name = "serde_core"+version = "1.0.229"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"dependencies = ["serde_derive",][[package]]name = "serde_derive"-version = "1.0.219"+version = "1.0.229"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"+checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"dependencies = ["proc-macro2","quote",- "syn",+ "syn 3.0.3",][[package]]@@ -392,12 +674,28 @@ version = "1.3.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"+[[package]]+name = "simd-adler32"+version = "0.3.10"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"+[[package]]name = "siphasher"version = "1.0.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"+[[package]]+name = "skrifa"+version = "0.44.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "819ab7d62b1d3e72d9d9dea5650bac30424f9111364bb94928dbf5ecad1baa68"+dependencies = [+ "bytemuck",+ "read-fonts",+]+[[package]]name = "smallvec"version = "1.15.1"@@ -410,6 +708,12 @@ version = "1.2.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"+[[package]]+name = "strict-num"+version = "0.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"+[[package]]name = "string_cache"version = "0.9.0"@@ -446,6 +750,17 @@ dependencies = ["unicode-ident",]+[[package]]+name = "syn"+version = "3.0.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"+dependencies = [+ "proc-macro2",+ "quote",+ "unicode-ident",+]+[[package]]name = "synstructure"version = "0.13.2"@@ -454,7 +769,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"dependencies = ["proc-macro2","quote",- "syn",+ "syn 2.0.101",][[package]]@@ -498,6 +813,32 @@ dependencies = ["tikv-jemalloc-sys",]+[[package]]+name = "tiny-skia"+version = "0.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "47ffee5eaaf5527f630fb0e356b90ebdec84d5d18d937c5e440350f88c5a91ea"+dependencies = [+ "arrayref",+ "arrayvec",+ "bytemuck",+ "cfg-if",+ "log",+ "png",+ "tiny-skia-path",+]++[[package]]+name = "tiny-skia-path"+version = "0.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "edca365c3faccca67d06593c5980fa6c57687de727a03131735bb85f01fdeeb9"+dependencies = [+ "arrayref",+ "bytemuck",+ "strict-num",+]+[[package]]name = "tinystr"version = "0.8.3"@@ -505,6 +846,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d"dependencies = ["displaydoc",+ "serde_core","zerovec",]@@ -655,7 +997,7 @@ checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e"dependencies = ["proc-macro2","quote",- "syn",+ "syn 2.0.101","synstructure",]@@ -676,7 +1018,7 @@ checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1"dependencies = ["proc-macro2","quote",- "syn",+ "syn 2.0.101","synstructure",]@@ -689,6 +1031,7 @@ dependencies = ["displaydoc","yoke","zerofrom",+ "zerovec",][[package]]@@ -697,6 +1040,7 @@ version = "0.11.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239"dependencies = [+ "serde","yoke","zerofrom","zerovec-derive",@@ -710,5 +1054,5 @@ checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555"dependencies = ["proc-macro2","quote",- "syn",+ "syn 2.0.101",]diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.tomlnew file mode 100644index 000000000..3ee777c22--- /dev/null+++ b/src/rust/Cargo.toml@@ -0,0 +1,18 @@+# One workspace, one staticlib: Rust bundles libstd into every staticlib, so+# two of them in the same binary collide (duplicate symbols, two allocators).+# Domain crates are rlibs; `ffi` links them all into the single archive Zig+# consumes.+[workspace]+resolver = "2"+members = ["ffi", "html5ever", "render"]++# Debug builds keep our own crates unoptimized but optimize dependencies+# (parley/tiny-skia/png are 10x slower when not optimized).+[profile.dev.package."*"]+opt-level = 3+debug-assertions = false+overflow-checks = false++[profile.release]+lto = true+codegen-units = 1diff --git a/src/html5ever/Cargo.toml b/src/rust/ffi/Cargo.tomlsimilarity index 59%rename from src/html5ever/Cargo.tomlrename to src/rust/ffi/Cargo.tomlindex f3818f822..11d8282d1 100644--- a/src/html5ever/Cargo.toml+++ b/src/rust/ffi/Cargo.toml@@ -1,27 +1,18 @@[package]-name = "litefetch-html5ever"+name = "lightpanda-ffi"version = "0.1.0"edition = "2021"[lib]-name = "litefetch_html5ever"+name = "lightpanda_ffi"path = "lib.rs"crate-type = ["cdylib", "staticlib"][dependencies]-html5ever = "0.39.0"-string_cache = "0.9.0"-typed-arena = "2.0.2"+lightpanda-html5ever = { path = "../html5ever" }+lightpanda-render = { path = "../render" }tikv-jemallocator = { version = "0.6.1", features = ["stats"], optional = true }tikv-jemalloc-ctl = { version = "0.6.1", features = ["stats"], optional = true }-xml5ever = "0.39.0"-encoding_rs = "0.8"-idna = "1.1.0"-url = "2.5.8"--[profile.release]-lto = true-codegen-units = 1[features]memstats = ["dep:tikv-jemallocator", "dep:tikv-jemalloc-ctl"]diff --git a/src/rust/ffi/lib.rs b/src/rust/ffi/lib.rsnew file mode 100644index 000000000..ebb8b9961--- /dev/null+++ b/src/rust/ffi/lib.rs@@ -0,0 +1,49 @@+// 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 one staticlib Zig links. Domain crates export their `extern "C"`+//! entry points; re-exporting them here keeps their symbols in the archive.+//! Process-wide concerns (the allocator) live here, not in a domain crate.++pub use lightpanda_render::*;+pub use lightpanda_html5ever::*;++#[cfg(feature = "memstats")]+#[global_allocator]+static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;++#[cfg(feature = "memstats")]+#[repr(C)]+pub struct Memory {+ pub resident: usize,+ pub allocated: usize,+}++#[cfg(feature = "memstats")]+#[no_mangle]+pub extern "C" fn html5ever_get_memory_usage() -> Memory {+ use tikv_jemalloc_ctl::{epoch, stats};++ // many statistics are cached and only updated when the epoch is advanced.+ let _ = epoch::advance();++ Memory {+ resident: stats::resident::read().unwrap_or(0),+ allocated: stats::allocated::read().unwrap_or(0),+ }+}diff --git a/src/rust/html5ever/Cargo.toml b/src/rust/html5ever/Cargo.tomlnew file mode 100644index 000000000..c64e1dff1--- /dev/null+++ b/src/rust/html5ever/Cargo.toml@@ -0,0 +1,17 @@+[package]+name = "lightpanda-html5ever"+version = "0.1.0"+edition = "2021"++[lib]+name = "lightpanda_html5ever"+path = "lib.rs"++[dependencies]+html5ever = "0.39.0"+string_cache = "0.9.0"+typed-arena = "2.0.2"+xml5ever = "0.39.0"+encoding_rs = "0.8"+idna = "1.1.0"+url = "2.5.8"diff --git a/src/html5ever/lib.rs b/src/rust/html5ever/lib.rssimilarity index 98%rename from src/html5ever/lib.rsrename to src/rust/html5ever/lib.rsindex d69ffe7c3..e5db80fd2 100644--- a/src/html5ever/lib.rs+++ b/src/rust/html5ever/lib.rs@@ -21,10 +21,6 @@ mod sink;mod types;mod url;-#[cfg(feature = "memstats")]-#[global_allocator]-static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;-use std::cell::Cell;use std::os::raw::{c_uchar, c_void};use types::*;@@ -594,27 +590,6 @@ pub extern "C" fn html5ever_attribute_iterator_count(c_iter: *const c_void) -> ureturn iter.vec.len();}-#[cfg(feature = "memstats")]-#[repr(C)]-pub struct Memory {- pub resident: usize,- pub allocated: usize,-}--#[cfg(feature = "memstats")]-#[no_mangle]-pub extern "C" fn html5ever_get_memory_usage() -> Memory {- use tikv_jemalloc_ctl::{epoch, stats};-- // many statistics are cached and only updated when the epoch is advanced.- let _ = epoch::advance();-- Memory {- resident: stats::resident::read().unwrap_or(0),- allocated: stats::allocated::read().unwrap_or(0),- }-}-// Streaming parser API// The Parser type from html5ever implements TendrilSink and supports streamingpub struct StreamingParser {diff --git a/src/html5ever/prescan.rs b/src/rust/html5ever/prescan.rssimilarity index 100%rename from src/html5ever/prescan.rsrename to src/rust/html5ever/prescan.rsdiff --git a/src/html5ever/sink.rs b/src/rust/html5ever/sink.rssimilarity index 100%rename from src/html5ever/sink.rsrename to src/rust/html5ever/sink.rsdiff --git a/src/html5ever/types.rs b/src/rust/html5ever/types.rssimilarity index 100%rename from src/html5ever/types.rsrename to src/rust/html5ever/types.rsdiff --git a/src/html5ever/url.rs b/src/rust/html5ever/url.rssimilarity index 100%rename from src/html5ever/url.rsrename to src/rust/html5ever/url.rsdiff --git a/src/rust/render/Cargo.toml b/src/rust/render/Cargo.tomlnew file mode 100644index 000000000..76ea51a49--- /dev/null+++ b/src/rust/render/Cargo.toml@@ -0,0 +1,18 @@+[package]+name = "lightpanda-render"+version = "0.1.0"+edition = "2021"++[lib]+name = "lightpanda_render"+path = "lib.rs"++[dependencies]+parley = { version = "0.11.1", default-features = false, features = ["std"] }+fontique = { version = "0.11.1", default-features = false, features = ["std"] }+skrifa = "0.44"+tiny-skia = "0.12"+png = "0.18"+# Only to select `logging`: without it icu_provider prints missing-data notes+# (e.g. no CJK line-break model) to stderr in debug builds.+icu_provider = { version = "2", default-features = false, features = ["logging"] }diff --git a/src/rust/render/fonts/DejaVuSans-Bold.ttf b/src/rust/render/fonts/DejaVuSans-Bold.ttfnew file mode 100644index 000000000..7d6b76271Binary files /dev/null and b/src/rust/render/fonts/DejaVuSans-Bold.ttf differdiff --git a/src/rust/render/fonts/DejaVuSans.ttf b/src/rust/render/fonts/DejaVuSans.ttfnew file mode 100644index 000000000..0692d93f0Binary files /dev/null and b/src/rust/render/fonts/DejaVuSans.ttf differdiff --git a/src/rust/render/fonts/DejaVuSansMono.ttf b/src/rust/render/fonts/DejaVuSansMono.ttfnew file mode 100644index 000000000..bcf640e23Binary files /dev/null and b/src/rust/render/fonts/DejaVuSansMono.ttf differdiff --git a/src/rust/render/fonts/LICENSE b/src/rust/render/fonts/LICENSEnew file mode 100644index 000000000..df52c1709--- /dev/null+++ b/src/rust/render/fonts/LICENSE@@ -0,0 +1,187 @@+Fonts are (c) Bitstream (see below). DejaVu changes are in public domain.+Glyphs imported from Arev fonts are (c) Tavmjong Bah (see below)+++Bitstream Vera Fonts Copyright+------------------------------++Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is+a trademark of Bitstream, Inc.++Permission is hereby granted, free of charge, to any person obtaining a copy+of the fonts accompanying this license ("Fonts") and associated+documentation files (the "Font Software"), to reproduce and distribute the+Font Software, including without limitation the rights to use, copy, merge,+publish, distribute, and/or sell copies of the Font Software, and to permit+persons to whom the Font Software is furnished to do so, subject to the+following conditions:++The above copyright and trademark notices and this permission notice shall+be included in all copies of one or more of the Font Software typefaces.++The Font Software may be modified, altered, or added to, and in particular+the designs of glyphs or characters in the Fonts may be modified and+additional glyphs or characters may be added to the Fonts, only if the fonts+are renamed to names not containing either the words "Bitstream" or the word+"Vera".++This License becomes null and void to the extent applicable to Fonts or Font+Software that has been modified and is distributed under the "Bitstream+Vera" names.++The Font Software may be sold as part of a larger software package but no+copy of one or more of the Font Software typefaces may be sold by itself.++THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS+OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,+TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME+FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING+ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF+THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE+FONT SOFTWARE.++Except as contained in this notice, the names of Gnome, the Gnome+Foundation, and Bitstream Inc., shall not be used in advertising or+otherwise to promote the sale, use or other dealings in this Font Software+without prior written authorization from the Gnome Foundation or Bitstream+Inc., respectively. For further information, contact: fonts at gnome dot+org.++Arev Fonts Copyright+------------------------------++Copyright (c) 2006 by Tavmjong Bah. All Rights Reserved.++Permission is hereby granted, free of charge, to any person obtaining+a copy of the fonts accompanying this license ("Fonts") and+associated documentation files (the "Font Software"), to reproduce+and distribute the modifications to the Bitstream Vera Font Software,+including without limitation the rights to use, copy, merge, publish,+distribute, and/or sell copies of the Font Software, and to permit+persons to whom the Font Software is furnished to do so, subject to+the following conditions:++The above copyright and trademark notices and this permission notice+shall be included in all copies of one or more of the Font Software+typefaces.++The Font Software may be modified, altered, or added to, and in+particular the designs of glyphs or characters in the Fonts may be+modified and additional glyphs or characters may be added to the+Fonts, only if the fonts are renamed to names not containing either+the words "Tavmjong Bah" or the word "Arev".++This License becomes null and void to the extent applicable to Fonts+or Font Software that has been modified and is distributed under the+"Tavmjong Bah Arev" names.++The Font Software may be sold as part of a larger software package but+no copy of one or more of the Font Software typefaces may be sold by+itself.++THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL+TAVMJONG BAH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM+OTHER DEALINGS IN THE FONT SOFTWARE.++Except as contained in this notice, the name of Tavmjong Bah shall not+be used in advertising or otherwise to promote the sale, use or other+dealings in this Font Software without prior written authorization+from Tavmjong Bah. For further information, contact: tavmjong @ free+. fr.++TeX Gyre DJV Math+-----------------+Fonts are (c) Bitstream (see below). DejaVu changes are in public domain.++Math extensions done by B. Jackowski, P. Strzelczyk and P. Pianowski+(on behalf of TeX users groups) are in public domain.++Letters imported from Euler Fraktur from AMSfonts are (c) American+Mathematical Society (see below).+Bitstream Vera Fonts Copyright+Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera+is a trademark of Bitstream, Inc.++Permission is hereby granted, free of charge, to any person obtaining a copy+of the fonts accompanying this license (“Fonts”) and associated+documentation+files (the “Font Software”), to reproduce and distribute the Font Software,+including without limitation the rights to use, copy, merge, publish,+distribute,+and/or sell copies of the Font Software, and to permit persons to whom+the Font Software is furnished to do so, subject to the following+conditions:++The above copyright and trademark notices and this permission notice+shall be+included in all copies of one or more of the Font Software typefaces.++The Font Software may be modified, altered, or added to, and in particular+the designs of glyphs or characters in the Fonts may be modified and+additional+glyphs or characters may be added to the Fonts, only if the fonts are+renamed+to names not containing either the words “Bitstream” or the word “Vera”.++This License becomes null and void to the extent applicable to Fonts or+Font Software+that has been modified and is distributed under the “Bitstream Vera”+names.++The Font Software may be sold as part of a larger software package but+no copy+of one or more of the Font Software typefaces may be sold by itself.++THE FONT SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS+OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,+TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME+FOUNDATION+BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL,+SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN+ACTION+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR+INABILITY TO USE+THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.+Except as contained in this notice, the names of GNOME, the GNOME+Foundation,+and Bitstream Inc., shall not be used in advertising or otherwise to promote+the sale, use or other dealings in this Font Software without prior written+authorization from the GNOME Foundation or Bitstream Inc., respectively.+For further information, contact: fonts at gnome dot org.++AMSFonts (v. 2.2) copyright++The PostScript Type 1 implementation of the AMSFonts produced by and+previously distributed by Blue Sky Research and Y&Y, Inc. are now freely+available for general use. This has been accomplished through the+cooperation+of a consortium of scientific publishers with Blue Sky Research and Y&Y.+Members of this consortium include:++Elsevier Science IBM Corporation Society for Industrial and Applied+Mathematics (SIAM) Springer-Verlag American Mathematical Society (AMS)++In order to assure the authenticity of these fonts, copyright will be+held by+the American Mathematical Society. This is not meant to restrict in any way+the legitimate use of the fonts, such as (but not limited to) electronic+distribution of documents containing these fonts, inclusion of these fonts+into other public domain or commercial font collections or computer+applications, use of the outline data to create derivative fonts and/or+faces, etc. However, the AMS does require that the AMS copyright notice be+removed from any derivative versions of the fonts which have been altered in+any way. In addition, to ensure the fidelity of TeX documents using Computer+Modern fonts, Professor Donald Knuth, creator of the Computer Modern faces,+has requested that any alterations which yield different font metrics be+given a different name.++$Id$diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rsnew file mode 100644index 000000000..c5fa5eed7--- /dev/null+++ b/src/rust/render/lib.rs@@ -0,0 +1,610 @@+// 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/>.++//! Text-only page rasterizer behind `Page.captureScreenshot` and+//! `--dump png`. Lightpanda has no layout engine, so a "screenshot" is the+//! page's text content flowed into blocks — the same content the markdown+//! dump produces — set with parley (line breaking, shaping, bidi), outlined+//! with skrifa and filled by tiny-skia. Fonts are bundled so output doesn't+//! depend on the host.++use std::borrow::Cow;+use std::collections::HashMap;+use std::os::raw::c_void;+use std::sync::{Mutex, OnceLock};++use fontique::{Blob, Collection, CollectionOptions, GenericFamily, SourceCache};+use parley::{+ Alignment, AlignmentOptions, FontContext, FontFamily, FontStyle, FontWeight, GlyphRun,+ Layout, LayoutContext, LineHeight, OverflowWrap, PositionedLayoutItem, StyleProperty,+ TextStyle, WhiteSpaceCollapse,+};+use skrifa::instance::{LocationRef, Size};+use skrifa::outline::{DrawSettings, OutlinePen};+use skrifa::{GlyphId, MetadataProvider};+use tiny_skia::{Color, FillRule, IntRect, Paint, PathBuilder, Pixmap, Rect, Stroke, Transform};++// ---------------------------------------------------------------------------+// C ABI — mirrored by src/browser/screenshot.zig.++pub const SPAN_BOLD: u32 = 1 << 0;+pub const SPAN_ITALIC: u32 = 1 << 1;+pub const SPAN_UNDERLINE: u32 = 1 << 2;+pub const SPAN_MONO: u32 = 1 << 3;+pub const SPAN_STRIKE: u32 = 1 << 4;+/// `color` holds 0xRRGGBB when set.+pub const SPAN_HAS_COLOR: u32 = 1 << 5;++/// Block kinds; 0 is a paragraph. `level` is 1..=6 for headings.+pub const BLOCK_HEADING: u8 = 1;+/// Whitespace preserved, monospace.+pub const BLOCK_PRE: u8 = 2;+/// Horizontal rule; no spans.+pub const BLOCK_RULE: u8 = 3;++/// List-like vertical spacing (standalone links, nav bars).+pub const BLOCK_TIGHT: u8 = 1 << 0;++pub const RENDER_MEASURE_ONLY: u32 = 1 << 0;++#[repr(C)]+pub struct LpSpan {+ pub text: *const u8,+ pub len: usize,+ pub flags: u32,+ pub color: u32,+}++#[repr(C)]+pub struct LpBlock {+ pub spans: *const LpSpan,+ pub spans_len: usize,+ /// List marker ("•", "3.") drawn in the gutter of the first line; empty+ /// for none.+ pub marker: *const u8,+ pub marker_len: usize,+ pub kind: u8,+ pub level: u8,+ pub list_depth: u8,+ pub quote_depth: u8,+ pub flags: u8,+}++#[repr(C)]+#[derive(Clone, Copy)]+pub struct LpRenderOpts {+ /// Layout width in CSS px.+ pub width: u32,+ /// Output height in CSS px; 0 = content height (full page).+ pub height: u32,+ /// Crop rectangle in CSS px; clip_w == 0 means no crop.+ pub clip_x: f32,+ pub clip_y: f32,+ pub clip_w: f32,+ pub clip_h: f32,+ /// Device pixel ratio.+ pub scale: f32,+ pub flags: u32,+}++pub type LpWriteFn = extern "C" fn(ctx: *mut c_void, data: *const u8, len: usize) -> bool;++/// Renders `blocks` to PNG, streaming the encoded bytes to `write`.+/// `content_height` receives the full-page height in CSS px. Returns 0 on+/// success, 1 if `write` refused data, 2 on invalid input.+#[no_mangle]+pub unsafe extern "C" fn lp_render_png(+ blocks: *const LpBlock,+ blocks_len: usize,+ opts: LpRenderOpts,+ content_height: *mut u32,+ ctx: *mut c_void,+ write: LpWriteFn,+) -> i32 {+ if opts.width == 0 || !(opts.scale > 0.0) || opts.scale > 8.0 {+ return 2;+ }+ let blocks = if blocks_len == 0 {+ &[][..]+ } else {+ std::slice::from_raw_parts(blocks, blocks_len)+ };+ let mut doc: Vec<Block> = Vec::with_capacity(blocks_len);+ for b in blocks {+ let spans = if b.spans_len == 0 {+ &[][..]+ } else {+ std::slice::from_raw_parts(b.spans, b.spans_len)+ };+ 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 2;+ };+ out_spans.push(Span { text, flags: s.flags, color: s.color });+ }+ let marker = std::str::from_utf8(std::slice::from_raw_parts(b.marker, b.marker_len))+ .unwrap_or("");+ doc.push(Block {+ kind: b.kind,+ level: b.level,+ list_depth: b.list_depth,+ quote_depth: b.quote_depth,+ flags: b.flags,+ marker,+ spans: out_spans,+ });+ }++ static RENDERER: OnceLock<Mutex<Renderer>> = OnceLock::new();+ let mut r = RENDERER+ .get_or_init(|| Mutex::new(Renderer::new()))+ .lock()+ .unwrap_or_else(|e| e.into_inner());++ let mut sink = Sink { ctx, write, failed: false };+ let h = r.render(&doc, opts, &mut sink);+ if !content_height.is_null() {+ *content_height = h;+ }+ if sink.failed {+ 1+ } else {+ 0+ }+}++struct Sink {+ ctx: *mut c_void,+ write: LpWriteFn,+ failed: bool,+}++impl std::io::Write for Sink {+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {+ if self.failed || !(self.write)(self.ctx, buf.as_ptr(), buf.len()) {+ self.failed = true;+ return Err(std::io::ErrorKind::Other.into());+ }+ Ok(buf.len())+ }+ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+}++// ---------------------------------------------------------------------------++struct Span<'a> {+ text: &'a str,+ flags: u32,+ color: u32,+}++struct Block<'a> {+ kind: u8,+ level: u8,+ list_depth: u8,+ quote_depth: u8,+ flags: u8,+ marker: &'a str,+ spans: Vec<Span<'a>>,+}++#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]+struct Rgb(u8, u8, u8);++const BASE_SIZE: f32 = 16.0;+const PAGE_MARGIN: f32 = 16.0;+const LIST_INDENT: f32 = 24.0;+const QUOTE_INDENT: f32 = 20.0;+const PRE_PAD: f32 = 8.0;+const TEXT_COLOR: Rgb = Rgb(0x1f, 0x1f, 0x1f);+const RULE_COLOR: Rgb = Rgb(0xcc, 0xcc, 0xcc);+const PRE_BG: Rgb = Rgb(0xf4, 0xf4, 0xf4);+/// Device pixels. Chrome's own full-page captures top out around here.+const MAX_RASTER_HEIGHT: u32 = 16384;++type GlyphKey = (usize, u32, u32);++struct Renderer {+ fcx: FontContext,+ lcx: LayoutContext<Rgb>,+ glyph_cache: HashMap<GlyphKey, Option<tiny_skia::Path>>,+}++struct Placed {+ layout: Layout<Rgb>,+ x: f32,+ y: f32,+ kind: u8,+ marker: Option<(Layout<Rgb>, f32)>,+ quote_bars: u8,+ quote_x: f32,+}++/// (font_size, weight, space_before, space_after, line_height)+fn block_metrics(b: &Block) -> (f32, FontWeight, f32, f32, f32) {+ match b.kind {+ BLOCK_HEADING => {+ let s = match b.level {+ 1 => 2.0,+ 2 => 1.5,+ 3 => 1.25,+ 4 => 1.1,+ _ => 1.0,+ };+ (BASE_SIZE * s, FontWeight::BOLD, 20.0, 10.0, 1.25)+ }+ BLOCK_PRE => (BASE_SIZE * 0.875, FontWeight::NORMAL, 12.0, 12.0, 1.4),+ BLOCK_RULE => (0.0, FontWeight::NORMAL, 12.0, 12.0, 1.0),+ _ => {+ let tight = b.list_depth > 0 || b.flags & BLOCK_TIGHT != 0;+ let (before, after) = if tight { (4.0, 4.0) } else { (12.0, 12.0) };+ (BASE_SIZE, FontWeight::NORMAL, before, after, 1.4)+ }+ }+}++impl Renderer {+ fn new() -> Self {+ let mut collection = Collection::new(CollectionOptions {+ 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"));+ for generic in [+ GenericFamily::SansSerif,+ GenericFamily::Serif,+ GenericFamily::SystemUi,+ GenericFamily::Cursive,+ GenericFamily::Fantasy,+ ] {+ collection.set_generic_families(generic, [sans].into_iter());+ }+ collection.set_generic_families(GenericFamily::Monospace, [mono].into_iter());+ Self {+ fcx: FontContext {+ collection,+ source_cache: SourceCache::default(),+ },+ lcx: LayoutContext::new(),+ glyph_cache: HashMap::new(),+ }+ }++ fn register(collection: &mut Collection, bytes: &'static [u8]) -> fontique::FamilyId {+ let fams = collection.register_fonts(Blob::new(std::sync::Arc::new(bytes)), None);+ fams[0].0+ }++ /// Returns the content height in CSS px.+ fn render(&mut self, blocks: &[Block], opts: LpRenderOpts, sink: &mut Sink) -> u32 {+ let scale = opts.scale;+ let content_w = (opts.width as f32 - 2.0 * PAGE_MARGIN).max(1.0);+ let mut y = PAGE_MARGIN;+ let mut placed: Vec<Placed> = Vec::with_capacity(blocks.len());+ let mut prev_after: Option<f32> = None;++ for block in blocks {+ let (size, weight, before, after, line_h) = block_metrics(block);+ y += match prev_after {+ None => 0.0,+ Some(pa) => pa.max(before),+ };+ prev_after = Some(after);++ 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;++ if block.kind == BLOCK_RULE {+ placed.push(Placed {+ layout: Layout::new(),+ x: PAGE_MARGIN + indent,+ y,+ kind: BLOCK_RULE,+ marker: None,+ quote_bars: 0,+ quote_x,+ });+ y += 1.0;+ continue;+ }++ let is_pre = block.kind == BLOCK_PRE;+ let root = TextStyle {+ font_family: FontFamily::Source(Cow::Borrowed(if is_pre {+ "monospace"+ } else {+ "sans-serif"+ })),+ font_size: size,+ font_weight: weight,+ brush: TEXT_COLOR,+ line_height: LineHeight::FontSizeRelative(line_h),+ overflow_wrap: OverflowWrap::Anywhere,+ ..Default::default()+ };+ let mut builder = self.lcx.tree_builder(&mut self.fcx, scale, true, &root);+ // The Zig walker already collapsed whitespace; preserving lets+ // "\n" (from <br> and <pre>) act as a hard break.+ builder.set_white_space_mode(WhiteSpaceCollapse::Preserve);+ let mut props: Vec<StyleProperty<Rgb>> = Vec::with_capacity(6);+ for span in &block.spans {+ props.clear();+ if span.flags & SPAN_BOLD != 0 {+ props.push(StyleProperty::FontWeight(FontWeight::BOLD));+ }+ if span.flags & SPAN_ITALIC != 0 {+ props.push(StyleProperty::FontStyle(FontStyle::Italic));+ }+ if span.flags & SPAN_UNDERLINE != 0 {+ props.push(StyleProperty::Underline(true));+ }+ if span.flags & SPAN_STRIKE != 0 {+ props.push(StyleProperty::Strikethrough(true));+ }+ if span.flags & SPAN_MONO != 0 && !is_pre {+ props.push(StyleProperty::FontFamily(FontFamily::Source(Cow::Borrowed(+ "monospace",+ ))));+ props.push(StyleProperty::FontSize(size * 0.9));+ }+ if span.flags & SPAN_HAS_COLOR != 0 {+ let c = Rgb(+ (span.color >> 16) as u8,+ (span.color >> 8) as u8,+ span.color as u8,+ );+ props.push(StyleProperty::Brush(c));+ props.push(StyleProperty::UnderlineBrush(Some(c)));+ props.push(StyleProperty::StrikethroughBrush(Some(c)));+ }+ builder.push_style_modification_span(&props);+ builder.push_text(span.text);+ builder.pop_style_span();+ }+ let (mut layout, _) = builder.build();+ let pad = if is_pre { PRE_PAD } else { 0.0 };+ layout.break_all_lines(Some(((content_w - indent - 2.0 * pad) * scale).max(1.0)));+ layout.align(Alignment::Start, AlignmentOptions::default());++ let marker = if block.marker.is_empty() {+ None+ } else {+ let mut b = self.lcx.ranged_builder(&mut self.fcx, block.marker, scale, true);+ b.push_default(StyleProperty::FontSize(BASE_SIZE));+ 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 h = layout.height() / scale;+ placed.push(Placed {+ layout,+ x: PAGE_MARGIN + indent + pad,+ y: y + pad,+ kind: block.kind,+ marker,+ quote_bars: block.quote_depth,+ quote_x,+ });+ y += h + 2.0 * pad;+ }+ y += PAGE_MARGIN;+ let content_h = y.ceil().max(1.0) as u32;+ if opts.flags & RENDER_MEASURE_ONLY != 0 {+ return content_h;+ }++ // Rasterize the requested strip (viewport or full page), then crop.+ // A clip never extends the strip: Chrome intersects it with the page+ // too, and callers probe with absurd heights (Playwright's fullPage+ // clip is 1e8 tall).+ let out_h = if opts.height == 0 { content_h } else { opts.height };+ let pw = ((opts.width as f32 * scale).ceil() as u32).max(1);+ let ph = ((out_h as f32 * scale).ceil() as u32).clamp(1, MAX_RASTER_HEIGHT);+ let Some(mut pixmap) = Pixmap::new(pw, ph) else {+ return content_h;+ };+ pixmap.fill(Color::WHITE);++ let mut paint = Paint::default();+ paint.anti_alias = true;+ for p in &placed {+ let oy = p.y * scale;+ if oy > ph as f32 {+ break;+ }+ match p.kind {+ BLOCK_RULE => {+ paint.set_color_rgba8(RULE_COLOR.0, RULE_COLOR.1, RULE_COLOR.2, 255);+ let w = (opts.width as f32 - PAGE_MARGIN) * scale - p.x * scale;+ if let Some(r) = Rect::from_xywh(p.x * scale, oy, w, 1.0 * scale) {+ pixmap.fill_rect(r, &paint, Transform::identity(), None);+ }+ continue;+ }+ BLOCK_PRE => {+ paint.set_color_rgba8(PRE_BG.0, PRE_BG.1, PRE_BG.2, 255);+ let x0 = (p.x - PRE_PAD) * scale;+ let w = (opts.width as f32 - PAGE_MARGIN) * scale - x0;+ if let Some(r) = Rect::from_xywh(+ x0,+ oy - PRE_PAD * scale,+ w,+ p.layout.height() + 2.0 * PRE_PAD * scale,+ ) {+ pixmap.fill_rect(r, &paint, Transform::identity(), None);+ }+ }+ _ => {}+ }+ for i in 0..p.quote_bars {+ paint.set_color_rgba8(RULE_COLOR.0, RULE_COLOR.1, RULE_COLOR.2, 255);+ let bx = (p.quote_x + i as f32 * QUOTE_INDENT) * scale;+ if let Some(r) = Rect::from_xywh(bx, oy, 3.0 * scale, p.layout.height()) {+ pixmap.fill_rect(r, &paint, Transform::identity(), None);+ }+ }+ if let Some((ml, mx)) = &p.marker {+ self.draw_layout(&mut pixmap, ml, *mx, oy);+ }+ self.draw_layout(&mut pixmap, &p.layout, p.x * scale, oy);+ }++ let cropped;+ let view = if opts.clip_w > 0.0 {+ let x = ((opts.clip_x * scale).floor().max(0.0) as u32).min(pw - 1);+ let y = ((opts.clip_y * scale).floor().max(0.0) as u32).min(ph - 1);+ let w = ((opts.clip_w * scale).ceil().max(1.0) as u32).min(pw - x);+ let h = ((opts.clip_h * scale).ceil().max(1.0) as u32).min(ph - y);+ match IntRect::from_xywh(x as i32, y as i32, w, h).and_then(|r| pixmap.clone_rect(r)) {+ Some(c) => {+ cropped = c;+ &cropped+ }+ None => &pixmap,+ }+ } else {+ &pixmap+ };++ // Everything is opaque on an opaque background, so the premultiplied+ // buffer is already straight RGBA (and RGB would save only ~3%).+ let mut enc = png::Encoder::new(&mut *sink, view.width(), view.height());+ enc.set_color(png::ColorType::Rgba);+ enc.set_depth(png::BitDepth::Eight);+ // Measured on text pages: fdeflate "Fast" is 3-8x larger than this,+ // level 6 is only ~5% smaller and 4x slower. Output is mostly+ // base64'd over CDP, so size matters.+ enc.set_deflate_compression(png::DeflateCompression::Level(2));+ enc.set_filter(png::Filter::Adaptive);+ if let Ok(mut w) = enc.write_header() {+ let _ = w.write_image_data(view.data());+ let _ = w.finish();+ }+ content_h+ }++ fn draw_layout(&mut self, pixmap: &mut Pixmap, layout: &Layout<Rgb>, ox: f32, oy: f32) {+ for line in layout.lines() {+ for item in line.items() {+ if let PositionedLayoutItem::GlyphRun(gr) = item {+ self.draw_glyph_run(pixmap, &gr, ox, oy);+ }+ }+ }+ }++ fn draw_glyph_run(&mut self, pixmap: &mut Pixmap, gr: &GlyphRun<Rgb>, ox: f32, oy: f32) {+ let run = gr.run();+ let font = run.font();+ let font_size = run.font_size();+ let synth = run.synthesis();+ let Ok(font_ref) = skrifa::FontRef::from_index(font.data.as_ref(), font.index) else {+ return;+ };+ let outlines = font_ref.outline_glyphs();+ let font_key = font.data.as_ref().as_ptr() as usize ^ (font.index as usize);+ let size_key = font_size.to_bits();++ let style = gr.style();+ let mut paint = Paint::default();+ paint.anti_alias = true;+ paint.set_color_rgba8(style.brush.0, style.brush.1, style.brush.2, 255);++ let baseline = oy + gr.baseline();+ let mut x = ox + gr.offset();+ let skew = synth+ .skew()+ .map(|deg| (-deg.to_radians()).tan())+ .unwrap_or(0.0);+ let stroke = synth.embolden().then(|| Stroke {+ width: font_size / 24.0,+ ..Default::default()+ });+ for glyph in gr.glyphs() {+ let gx = x + glyph.x;+ let gy = baseline - glyph.y;+ x += glyph.advance;+ let path = self+ .glyph_cache+ .entry((font_key, glyph.id, size_key))+ .or_insert_with(|| {+ let og = outlines.get(GlyphId::new(glyph.id))?;+ let mut pen = TinyPen(PathBuilder::new());+ og.draw(+ DrawSettings::unhinted(Size::new(font_size), LocationRef::default()),+ &mut pen,+ )+ .ok()?;+ pen.0.finish()+ });+ if let Some(path) = path {+ // Glyph outlines are y-up; flip onto the pixmap.+ let t = Transform::from_row(1.0, 0.0, skew, -1.0, gx, gy);+ pixmap.fill_path(path, &paint, FillRule::Winding, t, None);+ if let Some(stroke) = &stroke {+ pixmap.stroke_path(path, &paint, stroke, t, None);+ }+ }+ }++ let m = run.metrics();+ let x0 = ox + gr.offset();+ for (deco, default_off, default_size) in [+ (&style.underline, m.underline_offset, m.underline_size),+ (&style.strikethrough, m.strikethrough_offset, m.strikethrough_size),+ ] {+ let Some(d) = deco else { continue };+ let off = d.offset.unwrap_or(default_off);+ let sz = d.size.unwrap_or(default_size).max(1.0);+ let mut p2 = Paint::default();+ p2.set_color_rgba8(d.brush.0, d.brush.1, d.brush.2, 255);+ if let Some(r) = Rect::from_xywh(x0, (baseline - off).round(), gr.advance(), sz.round()) {+ pixmap.fill_rect(r, &p2, Transform::identity(), None);+ }+ }+ }+}++struct TinyPen(PathBuilder);++impl OutlinePen for TinyPen {+ fn move_to(&mut self, x: f32, y: f32) {+ self.0.move_to(x, y);+ }+ fn line_to(&mut self, x: f32, y: f32) {+ self.0.line_to(x, y);+ }+ fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {+ self.0.quad_to(cx0, cy0, x, y);+ }+ fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {+ self.0.cubic_to(cx0, cy0, cx1, cy1, x, y);+ }+ fn close(&mut self) {+ self.0.close();+ }+}diff --git a/src/sys/idna.zig b/src/sys/idna.zigindex 39e8c65fd..8938219e3 100644--- a/src/sys/idna.zig+++ b/src/sys/idna.zig@@ -17,7 +17,7 @@ const std = @import("std");const Allocator = std.mem.Allocator;-// WHATWG "domain to ASCII" lives in the rust-url FFI (src/html5ever/url.rs),+// WHATWG "domain to ASCII" lives in the rust-url FFI (src/rust/html5ever/url.rs),// which uses the UTS#46-conformant `idna` crate — the same engine rust-url// itself uses.extern "c" fn lpurl_domain_to_ascii(diff --git a/src/sys/url.zig b/src/sys/url.zigindex 9e4169c46..e8ea18a43 100644--- a/src/sys/url.zig+++ b/src/sys/url.zig@@ -14,7 +14,7 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.//! Bindings for Servo's rust-url (https://github.com/servo/rust-url).-//! Check @src/html5ever/url.rs for Rust-side of the bindings.+//! Check @src/rust/html5ever/url.rs for Rust-side of the bindings.pub const Url = anyopaque;