Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions crates/grida-canvas/src/export/export_as_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ pub fn export_node_as_image(
rect: Rectangle,
format: ExportAsImage,
) -> Option<Exported> {
// Guard: Skia cannot create a raster surface with zero or negative dimensions.
let pixel_w = size.width as i32;
let pixel_h = size.height as i32;
if pixel_w <= 0 || pixel_h <= 0 {
return None;
}

let skfmt: EncodedImageFormat = format.clone().into();

// Create camera with original bounds to determine world-space view
Expand All @@ -39,7 +46,11 @@ pub fn export_node_as_image(
// Scale the camera size to target resolution and adjust zoom to maintain same world-space view
// When we increase the viewport size and zoom IN proportionally, we see the same world-space rect
// but at higher resolution (scale = 2 means 2x zoom, 2x pixels, same world-space view)
let scale = size.width / rect.width;
let scale = if rect.width > 0.0 {
size.width / rect.width
} else {
1.0
};
camera.set_size(Size {
width: size.width,
height: size.height,
Expand All @@ -48,7 +59,7 @@ pub fn export_node_as_image(

let store = fonts.store();
let mut r = Renderer::new_with_store(
Backend::new_from_raster(size.width as i32, size.height as i32),
Backend::new_from_raster(pixel_w, pixel_h),
None,
camera,
store,
Expand Down
5 changes: 5 additions & 0 deletions crates/grida-canvas/src/export/export_as_pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub fn export_node_as_pdf(
let width = rect.width;
let height = rect.height;

// Guard: Skia cannot create a raster surface with zero or negative dimensions.
if width as i32 <= 0 || height as i32 <= 0 {
return None;
}

// Begin a new page
let mut page = doc.begin_page(SkSize::new(width, height), None);
let canvas = page.canvas();
Expand Down
9 changes: 8 additions & 1 deletion crates/grida-canvas/src/export/export_as_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ pub fn export_node_as_svg(
let width = rect.width;
let height = rect.height;

// Guard: Skia cannot create a raster surface with zero or negative dimensions.
let pixel_w = width as i32;
let pixel_h = height as i32;
if pixel_w <= 0 || pixel_h <= 0 {
return None;
}

// Create SVG canvas
let bounds = SkRect::from_wh(width, height);
let canvas = svg::Canvas::new(bounds, None);
Expand All @@ -31,7 +38,7 @@ pub fn export_node_as_svg(
// Temporary renderer using raster backend sharing the ByteStore
let store = fonts.store();
let mut renderer = Renderer::new_with_store(
Backend::new_from_raster(width as i32, height as i32),
Backend::new_from_raster(pixel_w, pixel_h),
None,
camera,
store,
Expand Down
Loading
Loading