Skip to content

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

fix(vector): preserve scaled live-line thickness during endpoint edits (#3184)

Nigel Ihza Firdausy21326ded434e
diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rs
index 80bae6302..bcf980469 100644
--- a/crates/engine/src/vector_cmds.rs
+++ b/crates/engine/src/vector_cmds.rs
@@ -511,7 +511,17 @@ pub fn transform_shape(sh: &mut ShapeLayer, a: &Affine) {
}
LiveShape::Ellipse { rect } => LiveShape::Ellipse { rect: r(rect) },
LiveShape::Polygon { rect, sides, star_ratio } => LiveShape::Polygon { rect: r(rect), sides, star_ratio },
- LiveShape::Line { from, to, weight } => LiveShape::Line { from: pt(from), to: pt(to), weight },
+ LiveShape::Line { from, to, weight } => {
+ // Live-shape edits regenerate the line's filled bar from its parameters.
+ // Keep the weight in sync with a positive scale of the path, or an
+ // unchanged endpoint edit unexpectedly restores the old thickness (#3063).
+ // A line's width scales perpendicular to its direction.
+ let (dx, dy) = (to[0] - from[0], to[1] - from[1]);
+ let old_length = dx.hypot(dy);
+ let new_length = (dx * m0).hypot(dy * m3);
+ let width_scale = if old_length > 0.0 && new_length > 0.0 { m0 * m3 * old_length / new_length } else { m0.min(m3) };
+ LiveShape::Line { from: pt(from), to: pt(to), weight: weight * width_scale }
+ }
})
}
// Flips and 90° turns keep rectangles, ellipses and lines live (Image Rotation): the new
diff --git a/crates/engine/tests/live_line_transform.rs b/crates/engine/tests/live_line_transform.rs
new file mode 100644
index 000000000..b16b51bc0
--- /dev/null
+++ b/crates/engine/tests/live_line_transform.rs
@@ -0,0 +1,49 @@
+use photocraft_engine::Session;
+use serde_json::json;
+
+fn live_line_info(s: &mut Session, layer: u64) -> serde_json::Value {
+ s.execute("shape.info", json!({"layer": layer})).unwrap()
+}
+
+#[test]
+fn live_line_keeps_scaled_thickness_after_endpoint_edit() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 128, "height": 128, "background": "transparent"})).unwrap();
+ let layer = s.execute("shape.create", json!({"kind": "line", "from": [10,20], "to": [30,20], "weight": 4})).unwrap()["layer"].as_u64().unwrap();
+
+ s.execute("shape.edit", json!({"transform": [2,0,0,2,0,0]})).unwrap();
+ let after_scale = live_line_info(&mut s, layer);
+ assert_eq!(after_scale["bounds"], json!([20, 36, 40, 8]));
+ assert_eq!(after_scale["live"]["weight"], json!(8.0));
+
+ s.execute("shape.edit", json!({"to": [60,40]})).unwrap();
+ let after_edit = live_line_info(&mut s, layer);
+ assert_eq!(after_edit["bounds"], after_scale["bounds"]);
+ assert_eq!(after_edit["path"], after_scale["path"]);
+ assert_eq!(after_edit["live"]["weight"], json!(8.0));
+}
+
+#[test]
+fn translating_live_lines_leaves_their_weight_unchanged() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 128, "height": 128, "background": "transparent"})).unwrap();
+ let layer = s.execute("shape.create", json!({"kind": "line", "from": [10,20], "to": [30,20], "weight": 4})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("shape.edit", json!({"move": [10,10]})).unwrap();
+ let after_move = live_line_info(&mut s, layer);
+ s.execute("shape.edit", json!({"to": [40,30]})).unwrap();
+ let after_edit = live_line_info(&mut s, layer);
+ assert_eq!(after_edit["path"], after_move["path"]);
+ assert_eq!(after_edit["live"]["weight"], json!(4.0));
+}
+
+#[test]
+fn nonuniform_scaling_uses_the_direction_perpendicular_to_the_line() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 128, "height": 128, "background": "transparent"})).unwrap();
+ let horizontal = s.execute("shape.create", json!({"kind": "line", "from": [10,20], "to": [30,20], "weight": 4})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("shape.edit", json!({"transform": [2,0,0,1,0,0]})).unwrap();
+ assert_eq!(live_line_info(&mut s, horizontal)["live"]["weight"], json!(4.0));
+ let vertical = s.execute("shape.create", json!({"kind": "line", "from": [50,10], "to": [50,30], "weight": 4})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("shape.edit", json!({"transform": [2,0,0,1,0,0]})).unwrap();
+ assert_eq!(live_line_info(&mut s, vertical)["live"]["weight"], json!(8.0));
+}