Lightpanda: the headless browser designed for AI and automation
screenshot honors deviceScaleFactor
Karl Seguina4703f1eb3f3
diff --git a/src/browser/Viewport.zig b/src/browser/Viewport.zigindex d6c1b8e39..34e19ba28 100644--- a/src/browser/Viewport.zig+++ b/src/browser/Viewport.zig@@ -20,6 +20,7 @@ const Viewport = @This();width: u32,height: u32,+scale: f32 = 1.0, // for screenshot rasterpub const default = Viewport{.width = 1920,diff --git a/src/cdp/domains/emulation.zig b/src/cdp/domains/emulation.zigindex 56287d684..7f82b66f6 100644--- a/src/cdp/domains/emulation.zig+++ b/src/cdp/domains/emulation.zig@@ -78,13 +78,6 @@ fn setDeviceMetricsOverride(cmd: *CDP.Command) !void {// Not-yet-emulated parameters: accept them but warn so the caller knows// they are ignored.- if (params.deviceScaleFactor) |v| {- if (v != 0 and v != 1) log.warn(.not_implemented, "setDeviceMetricsOverride", .{- .cdp_cmd = "Emulation.setDeviceMetricsOverride",- .param = "deviceScaleFactor",- .value = v,- });- }if (params.mobile) |v| {if (v) log.warn(.not_implemented, "setDeviceMetricsOverride", .{.cdp_cmd = "Emulation.setDeviceMetricsOverride",@@ -121,9 +114,12 @@ fn setDeviceMetricsOverride(cmd: *CDP.Command) !void {// CDP convention: a 0 width/height means "don't override that dimension",// so keep the current value for any dimension passed as 0.const current = browser.getViewport();+ // Same convention for deviceScaleFactor: 0 means "don't override".+ const dsf: f32 = if (params.deviceScaleFactor) |v| @floatCast(v) else 0;browser.viewport_override = .{.width = if (params.width > 0) params.width else current.width,.height = if (params.height > 0) params.height else current.height,+ .scale = if (dsf > 0) dsf else current.scale,};return cmd.sendResult(null, .{});diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zigindex edd85f14c..d4a17d702 100644--- a/src/cdp/domains/page.zig+++ b/src/cdp/domains/page.zig@@ -994,6 +994,7 @@ fn captureScreenshot(cmd: *CDP.Command) !void {const viewport = cmd.cdp.browser.getViewport();var opts: lp.screenshot.Opts = .{+ .scale = viewport.scale,.width = viewport.width,.height = if (params.captureBeyondViewport orelse false) 0 else viewport.height,};@@ -1004,7 +1005,7 @@ fn captureScreenshot(cmd: *CDP.Command) !void {.width = @floatCast(clip.width),.height = @floatCast(clip.height),};- opts.scale = @floatCast(clip.scale);+ opts.scale *= @floatCast(clip.scale);}// Prepared streams itself as base64 straight into the outgoing message.@@ -1302,6 +1303,44 @@ test "cdp.frame: captureScreenshot" {try testing.expectEqual(200, std.mem.readInt(u32, png[16..20], .big));try testing.expectEqual(80, std.mem.readInt(u32, png[20..24], .big));}++ try ctx.processMessage(.{ .id = 13, .method = "Emulation.setDeviceMetricsOverride", .params = .{+ .width = 200,+ .height = 100,+ .deviceScaleFactor = 2,+ } });++ {+ // deviceScaleFactor alone rasters at 2x; the viewport stays CSS px.+ try ctx.processMessage(.{ .id = 14, .method = "Page.captureScreenshot" });+ const png = try screenshotResult(&ctx, 14);+ try testing.expectEqual(400, std.mem.readInt(u32, png[16..20], .big));+ try testing.expectEqual(200, std.mem.readInt(u32, png[20..24], .big));+ }++ {+ // clip.scale is a zoom on top of it, not a replacement: 2 * 2 = 4x.+ try ctx.processMessage(.{ .id = 15, .method = "Page.captureScreenshot", .params = .{+ .clip = .{ .x = 0, .y = 0, .width = 50, .height = 20, .scale = 2 },+ } });+ const png = try screenshotResult(&ctx, 15);+ try testing.expectEqual(200, std.mem.readInt(u32, png[16..20], .big));+ try testing.expectEqual(80, std.mem.readInt(u32, png[20..24], .big));+ }++ {+ // A width/height of 0 keeps the current values, and so does a+ // deviceScaleFactor of 0.+ try ctx.processMessage(.{ .id = 16, .method = "Emulation.setDeviceMetricsOverride", .params = .{+ .width = 0,+ .height = 0,+ .deviceScaleFactor = 0,+ } });+ try ctx.processMessage(.{ .id = 17, .method = "Page.captureScreenshot" });+ const png = try screenshotResult(&ctx, 17);+ try testing.expectEqual(400, std.mem.readInt(u32, png[16..20], .big));+ try testing.expectEqual(200, std.mem.readInt(u32, png[20..24], .big));+ }}fn screenshotResult(ctx: *testing.TestContext, msg_id: i64) ![]const u8 {