Skip to content

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

Canvas Size: growing the canvas re-renders PSD fill layers (#7362) (#7363)

David Mannfb6f8b4fa6ca
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rs
index 3e870aa68..4559ca49a 100644
--- a/crates/engine/src/image_cmds.rs
+++ b/crates/engine/src/image_cmds.rs
@@ -54,6 +54,18 @@ pub(crate) fn for_each_surface(layers: &mut [Layer], masks: bool, f: &mut dyn Fn
}
}
+/// Drops every fill cache of a layer tree. A fill layer covers the whole canvas, but its cache
+/// (the pixels a PSD stored) only the old one: after the canvas grows the fill must re-render
+/// from its model, or the added area stays transparent (#7362).
+fn drop_fill_caches(layers: &mut [Layer]) {
+ for l in layers {
+ l.fill_cache = None;
+ if let LayerContent::Group(g) = &mut l.content {
+ drop_fill_caches(&mut g.children);
+ }
+ }
+}
+
/// Converts every pixel surface of a layer tree (masks included) to `depth`.
pub(crate) fn convert_layers_depth(layers: &mut [Layer], depth: SampleType) {
for_each_surface(layers, true, &mut |surf, _| {
@@ -337,6 +349,9 @@ fn crop_doc(doc: &mut Document, cmd: &str, r: Rect, delete_pixels: bool, extensi
surf.write_interleaved(keep, &saved);
}
}
+ if extends {
+ drop_fill_caches(&mut doc.layers);
+ }
crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::Shapes);
Ok(())
}
@@ -397,6 +412,9 @@ fn canvas_size(s: &mut Session, p: &Value) -> Result<Value> {
s.edit("Canvas Size", |doc, _| {
translate_doc(doc, "image.canvasSize", dx, dy)?;
doc.size = Size::new(nw, nh);
+ if nw > ow as u32 || nh > oh as u32 {
+ drop_fill_caches(&mut doc.layers);
+ }
crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::Shapes);
let canvas = doc.bounds();
let old = Rect::from_xywh(dx, dy, ow as u32, oh as u32);
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 04063d307..0849cd941 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -702,6 +702,38 @@ fn fill_layer_pixel_rewrites_refresh_the_effect_maps() {
assert_eq!(after, fresh, "the effect maps track the cache surface, not the Fill spec alone");
}
+#[test]
+fn growing_the_canvas_re_renders_a_cached_fill_layer() {
+ // A PSD fill layer keeps its stored pixels as a fill cache covering only the old canvas.
+ // Canvas Size, an extending Crop and Reveal All must re-render the fill over the new area,
+ // as Photoshop does, instead of leaving the added bands transparent (#7362).
+ let grow: [(&str, Value); 2] = [
+ ("image.canvasSize", json!({"width": 100, "anchor": "left"})),
+ ("image.crop", json!({"x": 0, "y": 0, "width": 100, "height": 48, "deleteCroppedPixels": false})),
+ ];
+ for (cmd, params) in grow {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48, "background": "transparent"})).unwrap();
+ s.execute("layer.newFillLayer.solidColor", json!({"color": "#ff0000"})).unwrap();
+ s.edit("cache", |doc, active| {
+ let fmt = doc.pixel_format();
+ let l = doc.layer_mut(active.unwrap()).unwrap();
+ let photocraft_doc::LayerContent::Fill(f) = &l.content else { return Err(EngineError::Other("not a fill layer".into())) };
+ let fill = f.clone();
+ let mut surface = photocraft_raster::Surface::new(fmt);
+ surface.fill_rect(photocraft_geom::Rect::new(0, 0, 64, 48), &[1.0, 0.0, 0.0, 1.0]);
+ l.fill_cache = Some(photocraft_doc::FillCache { fill, surface });
+ Ok(())
+ })
+ .unwrap();
+ s.execute(cmd, params).unwrap();
+ let d = &s.active().unwrap().doc;
+ assert_eq!(d.size.width, 100, "{cmd} grows the canvas");
+ let px = photocraft_compose::render(d, d.bounds()).get(90, 20);
+ assert!(px[3] > 0.99 && px[0] > 0.99 && px[1] < 0.01, "{cmd}: the fill covers the added area, got {px:?}");
+ }
+}
+
#[test]
fn group_effect_maps_follow_child_visibility_and_opacity() {
// A styled group's maps come from its children's composite: hiding a child or changing its