Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Integration: refuse empty artboard assets (#8254)

David Mann5c772cf5405a
diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rs
index bba243c22..fe6992b41 100644
--- a/crates/engine/src/web_cmds.rs
+++ b/crates/engine/src/web_cmds.rs
@@ -1440,6 +1440,9 @@ pub fn generate_assets(doc: &Document, dir: &str) -> (Vec<String>, Vec<Value>) {
return Err(other(format!("{rel}: another layer already writes this file; rename one of the layers")));
}
let mut one = match crate::artboard_cmds::artboard_document(doc, *id)? {
+ // Like Photoshop's Generator, a board with nothing on it is refused rather
+ // than written as a blank file.
+ Some(board) if !has_pixels(&board) => return Err(other("the layer is empty")),
Some(board) => board,
None => crate::layer_menu_cmds::layer_document(doc, *id)?,
};
@@ -1489,6 +1492,11 @@ pub fn generate_assets(doc: &Document, dir: &str) -> (Vec<String>, Vec<Value>) {
}
/// `<document folder>/<document name>-assets`.
+/// Whether any pixel of `doc` is not fully transparent.
+fn has_pixels(doc: &Document) -> bool {
+ photocraft_compose::render(doc, doc.bounds()).px.iter().any(|q| q[3] > 0.0)
+}
+
pub fn assets_dir(doc_path: &str) -> String {
let p = std::path::Path::new(doc_path);
let dir = p.parent().map(|d| d.to_string_lossy().into_owned()).unwrap_or_default();
diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rs
index 4de2d43b9..2c9b7b76f 100644
--- a/crates/engine/src/web_cmds/tests.rs
+++ b/crates/engine/src/web_cmds/tests.rs
@@ -679,6 +679,38 @@ fn image_assets_keep_full_artboards() {
assert_eq!(decode(&format!("{dir}/logo.png")).dimensions(), (8, 6), "a layer on the board is still trimmed");
}
+/// An artboard asset with nothing on the board (empty, or its contents all outside it) is
+/// refused like an empty layer, not written as a blank board: Photoshop's Generator refuses a
+/// zero-bounds pixmap before it pads to the board.
+#[test]
+fn image_assets_refuse_empty_artboards() {
+ let dir = tmp("asset-empty-artboards");
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48, "name": "boards.psd", "background": "transparent"})).unwrap();
+ s.edit("boards", |doc, _| {
+ let fmt = doc.pixel_format();
+ let mut outside = Layer::raster("outside", fmt);
+ outside.surface_mut().unwrap().fill_rect(Rect::new(50, 40, 60, 46), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0]));
+ let board = |x0| photocraft_doc::Artboard {
+ rect: Rect::new(x0, 0, x0 + 20, 20),
+ background: photocraft_doc::ArtboardBackground::Transparent,
+ preset: String::new(),
+ };
+ let empty = photocraft_doc::Group { children: vec![], expanded: true, artboard: Some(board(0)) };
+ let off = photocraft_doc::Group { children: vec![outside], expanded: true, artboard: Some(board(24)) };
+ doc.layers = vec![Layer::new("empty.png", LayerContent::Group(empty)), Layer::new("off.png", LayerContent::Group(off))];
+ Ok(())
+ })
+ .unwrap();
+ let r = s.execute("file.generate.imageAssets", json!({"on": true, "dir": dir})).unwrap();
+ assert_eq!(r["files"], json!([]), "{r}");
+ let errors = r["errors"].as_array().unwrap();
+ assert_eq!(errors.len(), 2, "{r}");
+ assert!(errors.iter().all(|e| e["error"].as_str().unwrap().contains("the layer is empty")), "{r}");
+ assert!(!std::path::Path::new(&format!("{dir}/empty.png")).exists());
+ assert!(!std::path::Path::new(&format!("{dir}/off.png")).exists());
+}
+
/// #6614: two layers, or two default variants, naming one file never overwrite each other: the
/// layer higher in the Layers panel keeps the file, whatever the case of its name, and the
/// others are reported.