Skip to content

Lightpanda: the headless browser designed for AI and automation

cap PNG size

Karl Seguin2c7237f259f5
diff --git a/src/browser/screenshot.zig b/src/browser/screenshot.zig
index 86498b4f8..182105117 100644
--- a/src/browser/screenshot.zig
+++ b/src/browser/screenshot.zig
@@ -678,6 +678,27 @@ test "browser.screenshot: fixed height, clip and scale" {
try testing.expectEqual(40, std.mem.readInt(u32, aw.written()[20..24], .big));
}
+test "browser.screenshot: raster is bounded" {
+ 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>");
+
+ var aw: std.Io.Writer.Allocating = .init(testing.allocator);
+ defer aw.deinit();
+
+ _ = try png(div.asNode(), .{ .width = 300000, .height = 8 }, &aw.writer, frame);
+ try testing.expectEqual(16384, std.mem.readInt(u32, aw.written()[16..20], .big));
+ try testing.expectEqual(8, std.mem.readInt(u32, aw.written()[20..24], .big));
+
+ aw.clearRetainingCapacity();
+ _ = try png(div.asNode(), .{ .width = 8, .height = 100000 }, &aw.writer, frame);
+ try testing.expectEqual(8, std.mem.readInt(u32, aw.written()[16..20], .big));
+ try testing.expectEqual(16384, 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();
diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rs
index c5fa5eed7..d9faf34e0 100644
--- a/src/rust/render/lib.rs
+++ b/src/rust/render/lib.rs
@@ -218,8 +218,11 @@ 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;
+/// Device pixels per side. Chrome's own full-page captures top out around here.
+const MAX_RASTER_DIM: u64 = 16384;
+/// Device pixels in total, 4 bytes each: a 256MB ceiling on the pixmap. A 4K
+/// viewport captured full-page at 2x still fits.
+const MAX_RASTER_PIXELS: u64 = 64 << 20;
type GlyphKey = (usize, u32, u32);
@@ -421,9 +424,14 @@ impl Renderer {
// 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).
+ // Width and height are both client-controlled (viewport, clip scale),
+ // so bound the buffer.
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 pw = ((opts.width as f64 * scale as f64).ceil() as u64).clamp(1, MAX_RASTER_DIM);
+ let ph = ((out_h as f64 * scale as f64).ceil() as u64)
+ .clamp(1, MAX_RASTER_DIM)
+ .min((MAX_RASTER_PIXELS / pw).max(1));
+ let (pw, ph) = (pw as u32, ph as u32);
let Some(mut pixmap) = Pixmap::new(pw, ph) else {
return content_h;
};