Skip to content

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

Scale shape strokes when resampling documents (#8341)

bommertse2a238737dc7
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rs
index ab2a5b960..8fc73d52b 100644
--- a/crates/engine/src/image_cmds.rs
+++ b/crates/engine/src/image_cmds.rs
@@ -288,7 +288,16 @@ pub(crate) fn resample_doc(doc: &mut Document, nw: u32, nh: u32, filter: Resampl
*surf = resize_surface_in_canvas(surf, sx, sy, if is_mask { Resample::Bilinear } else { filter }, canvas);
});
let k = ((sx + sy) / 2.0) as f32;
- for_each_layer(&mut doc.layers, &mut |l| scale_effects(&mut l.effects, k));
+ for_each_layer(&mut doc.layers, &mut |l| {
+ scale_effects(&mut l.effects, k);
+ // Canvas resampling scales the whole appearance, including shape strokes (#8341).
+ // Dash lengths and offsets are already expressed in multiples of this width.
+ if let LayerContent::Shape(sh) = &mut l.content
+ && let Some(stroke) = &mut sh.stroke
+ {
+ stroke.width *= k;
+ }
+ });
for ch in doc.channels.iter_mut().chain(doc.quick_mask.as_mut()) {
ch.surface = resize_surface_in_canvas(&ch.surface, sx, sy, Resample::Bilinear, canvas);
}
diff --git a/crates/engine/tests/export_shape_strokes.rs b/crates/engine/tests/export_shape_strokes.rs
new file mode 100644
index 000000000..0875f4bfc
--- /dev/null
+++ b/crates/engine/tests/export_shape_strokes.rs
@@ -0,0 +1,105 @@
+//! Regression coverage for #8341: canvas resizing must scale shape strokes too.
+use photocraft_doc::{LayerContent, LayerId};
+use photocraft_engine::{
+ Session,
+ web_cmds::{WebSettings, web_document},
+};
+use serde_json::json;
+
+fn shape_session(depth: u32) -> (Session, LayerId) {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 1024, "height": 1024, "depth": depth, "background": "transparent"})).unwrap();
+ let id = s.execute("shape.create", json!({"rect": [256, 256, 512, 512], "fill": null, "stroke": {"width": 32, "color": "#000000"}})).unwrap()["layer"]
+ .as_u64()
+ .unwrap();
+ (s, LayerId(id))
+}
+
+#[test]
+fn web_downscale_keeps_stroke_only_shape_proportions_at_every_depth() {
+ for depth in [8, 16, 32] {
+ let (s, id) = shape_session(depth);
+ let original = &s.active().unwrap().doc;
+ for params in [json!({"percent": 25}), json!({"width": 256, "height": 256})] {
+ let (scaled, sx, sy) = web_document(original, &params, &WebSettings::default()).unwrap();
+ assert_eq!((scaled.size.width, scaled.size.height), (256, 256));
+ assert_eq!((sx, sy), (0.25, 0.25));
+ let LayerContent::Shape(sh) = &scaled.layer(id).unwrap().content else {
+ panic!("shape lost");
+ };
+ assert_eq!(sh.stroke.as_ref().unwrap().width, 8.0, "{depth}-bit stroke");
+ let pixels = photocraft_compose::flatten(&scaled);
+ let alpha = |x: usize| pixels.px[128 * 256 + x][3];
+ assert!(alpha(62) > 0.99, "scaled stroke is visible");
+ assert!(alpha(52) < 0.01, "stroke must not retain its original width");
+ assert!(alpha(128) < 0.01, "no fill is introduced");
+ }
+ let LayerContent::Shape(sh) = &original.layer(id).unwrap().content else {
+ panic!("shape lost");
+ };
+ assert_eq!(sh.stroke.as_ref().unwrap().width, 32.0, "export leaves the source untouched");
+ }
+}
+
+#[test]
+fn image_size_scales_strokes_once_and_undo_restores_them() {
+ let (mut s, id) = shape_session(8);
+ s.execute("shape.edit", json!({"layer": id.0, "stroke": {"dashes": [2, 1], "dashOffset": 0.5}})).unwrap();
+ let before = s.active().unwrap().doc.layer(id).unwrap().clone();
+ s.execute("image.imageSize", json!({"width": 512, "height": 512})).unwrap();
+ let LayerContent::Shape(sh) = &s.active().unwrap().doc.layer(id).unwrap().content else {
+ panic!("shape lost");
+ };
+ assert_eq!(sh.stroke.as_ref().unwrap().width, 16.0);
+ assert_eq!(sh.stroke.as_ref().unwrap().dashes, vec![2.0, 1.0], "dash lengths stay in stroke-width units");
+ assert_eq!(sh.stroke.as_ref().unwrap().dash_offset, 0.5);
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(s.active().unwrap().doc.layer(id).unwrap(), &before);
+ s.execute("edit.redo", json!({})).unwrap();
+ let LayerContent::Shape(sh) = &s.active().unwrap().doc.layer(id).unwrap().content else {
+ panic!("shape lost");
+ };
+ assert_eq!(sh.stroke.as_ref().unwrap().width, 16.0);
+}
+
+#[test]
+fn grouped_strokes_scale_up_and_unchanged_size_is_a_noop() {
+ let (mut s, id) = shape_session(8);
+ s.edit("Group", |doc, _| {
+ let children = std::mem::take(&mut doc.layers);
+ doc.layers.push(photocraft_doc::Layer::group("Outer", vec![photocraft_doc::Layer::group("Inner", children)]));
+ Ok(())
+ })
+ .unwrap();
+ let original = &s.active().unwrap().doc;
+ for (width, expected) in [(1024, 32.0), (1280, 40.0), (256, 8.0)] {
+ let (scaled, _, _) = web_document(original, &json!({"width": width}), &WebSettings::default()).unwrap();
+ let LayerContent::Shape(sh) = &scaled.layer(id).unwrap().content else {
+ panic!("shape lost");
+ };
+ assert_eq!(sh.stroke.as_ref().unwrap().width, expected);
+ }
+}
+
+#[test]
+fn png_export_contains_the_scaled_stroke_and_keeps_the_source_document() {
+ let (mut s, _) = shape_session(8);
+ let before = s.active().unwrap().doc.clone();
+ let mut bytes = Vec::new();
+ photocraft_engine::web_cmds::save_for_web_with_writer(
+ &mut s,
+ &json!({"format": "png24", "path": "stroke.png", "width": 256, "height": 256}),
+ &mut |_, encoded| {
+ bytes = encoded.to_vec();
+ Ok(())
+ },
+ )
+ .unwrap();
+ let image = photocraft_codecs::decode(&bytes).unwrap().convert(photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8);
+ assert_eq!(image.dimensions(), (256, 256));
+ let alpha = |x: usize| image.data()[(128 * 256 + x) * 4 + 3];
+ assert_eq!(alpha(62), 255);
+ assert_eq!(alpha(52), 0);
+ assert_eq!(alpha(128), 0);
+ assert_eq!(s.active().unwrap().doc, before);
+}
diff --git a/crates/ui-egui/src/export_dialog.rs b/crates/ui-egui/src/export_dialog.rs
index 103b36a06..907c2cb0b 100644
--- a/crates/ui-egui/src/export_dialog.rs
+++ b/crates/ui-egui/src/export_dialog.rs
@@ -1229,6 +1229,36 @@ mod tests {
}
}
+ /// #8341: Export As and its reduced preview carry the stroke width with the path.
+ #[test]
+ fn export_as_downscales_stroke_only_shapes_and_preview() {
+ for depth in [8, 16, 32] {
+ let mut session = photocraft_engine::Session::new();
+ session.execute("file.new", json!({"width": 1024, "height": 1024, "depth": depth, "background": "transparent"})).unwrap();
+ let id = session.execute("shape.create", json!({"rect": [256, 256, 512, 512], "fill": null, "stroke": {"width": 32}})).unwrap()["layer"]
+ .as_u64()
+ .unwrap();
+ let doc = session.active().unwrap().doc.clone();
+ let fields = Map::from_iter([("format".into(), json!("png")), ("scale".into(), json!(25))]);
+ for limit in [None, Some(128)] {
+ let out = export_document(&doc, &fields, limit).unwrap();
+ let side = if limit.is_some() { 128 } else { 256 };
+ assert_eq!((out.size.width, out.size.height), (side, side));
+ let shape = out.layer(photocraft_doc::LayerId(id)).unwrap();
+ let photocraft_doc::LayerContent::Shape(shape) = &shape.content else {
+ panic!("shape lost");
+ };
+ assert_eq!(shape.stroke.as_ref().unwrap().width, side as f32 / 32.0);
+ let pixels = photocraft_compose::flatten(&out);
+ let row = (side / 2 * side) as usize;
+ assert!(pixels.px[row + (side / 4) as usize][3] > 0.99);
+ assert!(pixels.px[row + (side / 4 - side / 32) as usize][3] < 0.01);
+ assert!(pixels.px[row + (side / 2) as usize][3] < 0.01);
+ }
+ assert_eq!(session.active().unwrap().doc, doc, "export leaves the source unchanged");
+ }
+ }
+
#[test]
fn export_document_scales_and_flattens() {
let doc = Document::with_background(