Skip to content

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

Free Transform frames the whole type layer; Distort/Perspective greyed for type (#2630) (#2701)

Fabio Lussi46d170b19b0a
diff --git a/crates/engine/src/transform_cmds.rs b/crates/engine/src/transform_cmds.rs
index d11cf3cc1..88939fc7c 100644
--- a/crates/engine/src/transform_cmds.rs
+++ b/crates/engine/src/transform_cmds.rs
@@ -18,7 +18,15 @@ fn bad(msg: impl Into<String>) -> EngineError {
EngineError::BadParams { cmd: "edit.transform".into(), msg: msg.into() }
}
-/// Document-space bounds a transform of `layer` starts from (what Free Transform frames).
+/// Layers a transform moves as a whole, even with a selection: groups, and the kinds that keep the
+/// transform instead of pixels (type, shapes, smart objects). Only pixel layers move just the
+/// selected pixels.
+pub fn moves_whole(layer: &Layer) -> bool {
+ matches!(layer.content, LayerContent::Group(_) | LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Smart(_))
+}
+
+/// Document-space bounds a transform of `layer` starts from (what Free Transform frames): the
+/// content within the selection, or all of it for a layer that [`moves_whole`] (#2630).
/// Content scans are cached per tile: snapping asks for every layer's bounds per Move drag.
pub fn transform_bounds(doc: &Document, layer: &Layer) -> Rect {
let content = match &layer.content {
@@ -28,7 +36,7 @@ pub fn transform_bounds(doc: &Document, layer: &Layer) -> Rect {
let content =
if content.is_empty() { layer.mask.as_ref().map_or(Rect::EMPTY, |m| photocraft_compose::bounds::content_bounds(&m.surface)) } else { content };
match &doc.selection {
- Some(sel) if !layer.is_group() => content.intersect(&sel.content_bounds()),
+ Some(sel) if !moves_whole(layer) => content.intersect(&sel.content_bounds()),
_ => content,
}
}
@@ -226,7 +234,7 @@ pub(crate) fn transform_layer(doc_sel: Option<&Surface>, group: Locks, l: &mut L
}
// With a selection only the selected pixels move, and so only the same region of a linked
// mask (#205). Groups, type, shapes and smart objects move whole.
- let mask_sel = doc_sel.filter(|_| !matches!(l.content, LayerContent::Group(_) | LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Smart(_)));
+ let mask_sel = doc_sel.filter(|_| !moves_whole(l));
match &mut l.content {
LayerContent::Group(g) => {
for c in g.children.iter_mut() {
@@ -768,4 +776,32 @@ mod tests {
sel.fill_rect(Rect::new(0, 0, 4, 4), &[1.0]);
assert!(matches!(split_gray_selected(&s, &sel), Ok(None)));
}
+
+ /// #2630: type can't be partly transformed, so a selection doesn't narrow the frame: Free
+ /// Transform (and the frame-based presets) act on the whole type layer.
+ #[test]
+ fn a_selection_does_not_narrow_a_type_layers_frame() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 200, "height": 120})).unwrap();
+ s.execute("type.create", json!({"x": 20, "y": 60, "text": "Hello", "size": 36})).unwrap();
+ let whole = active_bounds(&s);
+ s.execute("select.rect", json!({"x": whole.x0, "y": whole.y0, "width": whole.width() / 2, "height": whole.height()})).unwrap();
+ let st = s.active().unwrap();
+ let l = st.doc.layer(st.active_layer.unwrap()).unwrap();
+ assert_eq!(transform_bounds(&st.doc, l), whole, "the frame is the whole type layer");
+ // Corners of the frame moved 10 px right: a pure translation of the whole text.
+ let q = [[whole.x0 + 10, whole.y0], [whole.x1 + 10, whole.y0], [whole.x1 + 10, whole.y1], [whole.x0 + 10, whole.y1]];
+ s.execute("edit.transform", json!({"quad": q})).unwrap();
+ let moved = active_bounds(&s);
+ assert!(moved.x0.abs_diff(whole.x0 + 10) <= 1 && moved.width().abs_diff(whole.width()) <= 1, "{whole:?} -> {moved:?}");
+ // Rotate 180° turns it about its own centre, not the selection's.
+ s.execute("edit.transform.rotate180", json!({})).unwrap();
+ let turned = active_bounds(&s);
+ assert!(turned.x0.abs_diff(moved.x0) <= 2 && turned.x1.abs_diff(moved.x1) <= 2, "{moved:?} -> {turned:?}");
+ // A pixel layer still frames only the selected pixels.
+ let mut s = session();
+ s.execute("select.rect", json!({"x": 10, "y": 10, "width": 5, "height": 10})).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!(transform_bounds(&st.doc, st.doc.layer(st.active_layer.unwrap()).unwrap()), Rect::new(10, 10, 15, 20));
+ }
}
diff --git a/crates/ui-egui/src/enable_rules.rs b/crates/ui-egui/src/enable_rules.rs
index 88f2236ed..593589051 100644
--- a/crates/ui-egui/src/enable_rules.rs
+++ b/crates/ui-egui/src/enable_rules.rs
@@ -116,6 +116,16 @@ pub fn disabled(app: &crate::PhotocraftApp, id: &str) -> bool {
if transform && crate::vector_ui::free_transform_path(app).is_some() {
return false;
}
+ // Editable type can be scaled, rotated and skewed but not distorted (#2630): Photoshop greys
+ // Distort and Perspective until the type is rasterized. A targeted unlinked mask or channel of
+ // the type layer is pixels, and can be.
+ if matches!(id, "edit.transform.distort" | "edit.transform.perspective")
+ && active.is_some_and(|l| matches!(l.content, LayerContent::Text(_)))
+ && photocraft_engine::transform_cmds::lone_target(&st.doc, st.active_layer, &serde_json::json!({ "target": crate::canvas::paint_target(app) }))
+ .is_ok_and(|t| t.is_none())
+ {
+ return true;
+ }
// Only these commands depend on the selection; skip walking the layers for the rest.
if selection_targets(id).is_some() {
return disabled_for_selection(&st.doc, &st.selected_layers(), active, id);
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 760aaec8c..96bd33dcb 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1992,6 +1992,9 @@ mod pencil_tests;
#[cfg(test)]
mod transform_undo_tests;
+#[cfg(test)]
+mod transform_type_tests;
+
#[cfg(test)]
mod save_identity_tests;
diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rs
index a0386aaa2..180884f94 100644
--- a/crates/ui-egui/src/transform_tool.rs
+++ b/crates/ui-egui/src/transform_tool.rs
@@ -190,7 +190,8 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>
// Preview document: the moving pixels removed (layer hidden, or the selection lifted out).
let mut pd = (*doc).clone();
let mut lifted = None;
- if let Some(sel) = doc.selection.clone().filter(|_| !layer.is_group() && layer.surface().is_some()) {
+ // Type, shapes and smart objects move whole, selection or not (#2630): only pixels are lifted.
+ if let Some(sel) = doc.selection.clone().filter(|_| !photocraft_engine::transform_cmds::moves_whole(layer) && layer.surface().is_some()) {
if let Some(surf) = pd.layer_mut(id).and_then(|l| l.surface_mut()) {
let (l, rest) = photocraft_engine::transform_cmds::split_selected(surf, &sel).map_err(|e| e.to_string())?;
*surf = rest;
diff --git a/crates/ui-egui/src/transform_type_tests.rs b/crates/ui-egui/src/transform_type_tests.rs
new file mode 100644
index 000000000..edc4c19ab
--- /dev/null
+++ b/crates/ui-egui/src/transform_type_tests.rs
@@ -0,0 +1,87 @@
+//! Free Transform on type layers (#2630): the Edit menu offers it, a selection doesn't narrow it
+//! (type can't be partly transformed, so the whole layer moves), and Distort / Perspective are
+//! greyed as in Photoshop until the type is rasterized.
+
+use serde_json::json;
+
+use crate::PhotocraftApp;
+use crate::state::Tool;
+
+/// A 200 × 120 document with a type layer "Hello" active and the Rectangular Marquee chosen.
+fn app() -> PhotocraftApp {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 120})).unwrap();
+ app.sync_views();
+ app.run("type.create", json!({"x": 20, "y": 60, "text": "Hello", "size": 36})).unwrap();
+ app.ui.tool = Tool::RectMarquee;
+ app
+}
+
+fn type_bounds(app: &PhotocraftApp) -> photocraft_geom::Rect {
+ let st = app.session.active().unwrap();
+ st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds()
+}
+
+fn select_left_half(app: &mut PhotocraftApp) {
+ let b = type_bounds(app);
+ app.run("select.rect", json!({"x": b.x0, "y": b.y0, "width": b.width() / 2, "height": b.height()})).unwrap();
+}
+
+#[test]
+fn type_layers_offer_free_transform_but_not_distort_or_perspective() {
+ let mut app = app();
+ for selection in [false, true] {
+ if selection {
+ select_left_half(&mut app);
+ }
+ for id in ["edit.freeTransform", "edit.transform.scale", "edit.transform.rotate", "edit.transform.skew"] {
+ assert!(crate::menus::is_enabled(&app, id), "{id} (selection: {selection})");
+ }
+ for id in ["edit.transform.distort", "edit.transform.perspective"] {
+ assert!(!crate::menus::is_enabled(&app, id), "{id} is greyed for editable type (selection: {selection})");
+ }
+ }
+ // The same rows in the marquee's canvas context menu.
+ let menu = crate::canvas_tool_menu::CanvasToolMenu {
+ pos: [10.0, 10.0],
+ tool: Tool::RectMarquee,
+ has_selection: true,
+ has_path: false,
+ path_name: None,
+ transform: false,
+ pasteboard: false,
+ };
+ assert!(crate::canvas_tool_menu::available(&app, &menu, "edit.freeTransform"));
+ assert!(!crate::canvas_tool_menu::available(&app, &menu, "edit.transform.distort"));
+ assert!(!crate::canvas_tool_menu::available(&app, &menu, "edit.transform.perspective"));
+ // Rasterized, the type is pixels and can be distorted.
+ app.run("layer.rasterize.type", json!({})).unwrap();
+ assert!(crate::menus::is_enabled(&app, "edit.transform.distort") && crate::menus::is_enabled(&app, "edit.transform.perspective"));
+}
+
+#[test]
+fn free_transform_with_a_selection_frames_and_moves_the_whole_type_layer() {
+ let mut app = app();
+ let whole = type_bounds(&app);
+ select_left_half(&mut app);
+ let ctx = egui::Context::default();
+ crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})).unwrap();
+ let t = app.ui.transform.as_ref().unwrap();
+ assert_eq!(t.rect, [whole.x0 as f64, whole.y0 as f64, whole.x1 as f64, whole.y1 as f64], "the box frames the whole text");
+ // The preview moves the whole layer (it is hidden under the box), not half of its pixels.
+ let pv = app.transform_preview.as_ref().unwrap();
+ let id = app.session.active().unwrap().active_layer.unwrap();
+ let under = pv.doc.layer(id).unwrap();
+ assert!(!under.visible, "the type layer is hidden while its preview moves");
+ assert_eq!(under.surface().unwrap().content_bounds(), whole, "nothing lifted out of the type");
+ // Move the box 10 px right: the text moves 10 px, unscaled.
+ if let Some(t) = app.ui.transform.as_mut() {
+ t.quad = t.quad.map(|q| [q[0] + 10.0, q[1]]);
+ }
+ crate::transform_tool::commit(&mut app);
+ assert_eq!(app.ui.status, "");
+ let moved = type_bounds(&app);
+ assert!(moved.x0.abs_diff(whole.x0 + 10) <= 1 && moved.width().abs_diff(whole.width()) <= 1, "{whole:?} -> {moved:?}");
+ let st = app.session.active().unwrap();
+ assert!(matches!(st.doc.layer(id).unwrap().content, photocraft_doc::LayerContent::Text(_)), "still editable type");
+}