Merge pull request #19139

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4
2026-12-02 14:21:57 +00:00
committed by autocraft-bot[bot]
3 changed files with 143 additions and 13 deletions
+33 -13
View File
@@ -946,28 +946,48 @@ fn last_transform(s: &Session) -> Result<&Value> {
fn can_transform_again(s: &Session) -> std::result::Result<(), String> {
has_doc(s)?;
last_transform(s).map(|_| ()).map_err(|error| error.to_string())
let last = last_transform(s).map_err(|error| error.to_string())?;
if let (Some(rect), Some(quad)) = (last.get("rect"), last.get("quad"))
&& crate::channel_cmds::targeted_channel(s).is_none()
&& let Ok(h) = transform_again_homography(rect, quad)
&& !h.is_identity()
&& (h.0[6].abs() >= 1e-12 || h.0[7].abs() >= 1e-12)
&& s.active().is_some_and(|st| {
let selected = crate::transform_cmds::selection_targets(st);
crate::transform_cmds::box_targets(&st.doc, &selected)
.into_iter()
.any(|id| st.doc.layer(id).is_some_and(|layer| matches!(layer.content, LayerContent::Text(_))))
})
{
return Err("Distort and Perspective need rasterized type (Type › Rasterize Type Layer)".into());
}
Ok(())
}
/// The map saved for a four-corner transform. A rectangle and quad can also describe an affine edit.
fn transform_again_homography(rect: &Value, quad: &Value) -> Result<photocraft_algo::transform::Homography> {
let coordinates = |value: &Value| -> Vec<f64> { value.as_array().map(|values| values.iter().filter_map(Value::as_f64).collect()).unwrap_or_default() };
let r = coordinates(rect);
let q: Vec<Vec<f64>> = quad.as_array().map(|rows| rows.iter().map(coordinates).collect()).unwrap_or_default();
if r.len() != 4 || q.len() != 4 || q.iter().any(|row| row.len() < 2) {
return Err(EngineError::Other("the last transform can't be repeated".into()));
}
photocraft_algo::transform::Homography::rect_to_quad(
[r[0], r[1], r[2], r[3]],
[[q[0][0], q[0][1]], [q[1][0], q[1][1]], [q[2][0], q[2][1]], [q[3][0], q[3][1]]],
)
.ok_or_else(|| EngineError::Other("the last transform can't be repeated".into()))
}
/// The `edit.transform` params that repeat the last transform on the layers `ids` (their frame
/// together is the box it starts from).
fn transform_again_params(s: &Session, ids: &[LayerId]) -> Result<Value> {
let last = last_transform(s)?;
let (rect, quad) = (last.get("rect").cloned(), last.get("quad").cloned());
let (rect, quad) = (last.get("rect"), last.get("quad"));
let mut p = json!({"interpolation": last.get("interpolation").cloned().unwrap_or(json!("bicubic"))});
match (rect, quad, last.get("matrix").cloned()) {
(Some(r), Some(q), _) => {
let v = |x: &Value| -> Vec<f64> { x.as_array().map(|a| a.iter().filter_map(Value::as_f64).collect()).unwrap_or_default() };
let r = v(&r);
let q: Vec<Vec<f64>> = q.as_array().map(|a| a.iter().map(v).collect()).unwrap_or_default();
if r.len() != 4 || q.len() != 4 || q.iter().any(|row| row.len() < 2) {
return Err(EngineError::Other("the last transform can't be repeated".into()));
}
let h = photocraft_algo::transform::Homography::rect_to_quad(
[r[0], r[1], r[2], r[3]],
[[q[0][0], q[0][1]], [q[1][0], q[1][1]], [q[2][0], q[2][1]], [q[3][0], q[3][1]]],
)
.ok_or(EngineError::Other("the last transform can't be repeated".into()))?;
let h = transform_again_homography(r, q)?;
let d = s.active().ok_or(EngineError::NoDocument)?;
d.doc.layer(active_id(s)?).ok_or(EngineError::Other("no active layer".into()))?;
let b = crate::transform_cmds::transform_bounds_of(&d.doc, ids);
+1
View File
@@ -200,6 +200,7 @@ mod smart_rerender_fallback;
mod style_and_align;
mod svg_smart_object;
mod tone_target_picker;
mod transform_again_enabled_type_tests;
mod transform_again_history_name_tests;
mod transform_budget_unclamped;
mod type_format_targets;
@@ -0,0 +1,109 @@
//! #17861: Transform Again disables a projective repeat when its targets include editable type.
use photocraft_doc::LayerId;
use photocraft_engine::{EngineError, Result, Session};
use photocraft_geom::Rect;
use serde_json::{Value, json};
const AGAIN: &str = "edit.transform.again";
const FRAME: [i32; 4] = [10, 10, 34, 28];
const DISTORT: [[i32; 2]; 4] = [[5, 12], [31, 15], [36, 31], [10, 32]];
const PERSPECTIVE: [[i32; 2]; 4] = [[14, 10], [30, 10], [35, 29], [10, 29]];
fn id_of(result: Value) -> Result<LayerId> {
result["layer"].as_u64().map(LayerId).ok_or_else(|| EngineError::Other("fixture did not create a layer".into()))
}
fn raster(s: &mut Session, rect: Rect) -> Result<LayerId> {
let id = id_of(s.execute("layer.new.layer", json!({}))?)?;
s.edit("Paint fixture", |doc, _| {
let layer = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
let surface = layer.surface_mut().ok_or_else(|| EngineError::Other("fixture layer has no pixels".into()))?;
surface.fill_rect(rect, &[1.0, 0.0, 0.0, 1.0]);
Ok(())
})?;
Ok(id)
}
fn document(transform: Value) -> Result<(Session, LayerId)> {
let mut s = Session::new();
s.execute("file.new", json!({"width": 160, "height": 120, "background": "transparent"}))?;
let pixels = raster(&mut s, Rect::new(10, 10, 34, 28))?;
s.execute("edit.transform", transform)?;
Ok((s, pixels))
}
fn select_both(s: &mut Session, first: LayerId, second: LayerId) -> Result<()> {
s.execute("layer.select", json!({"layer": first.0}))?;
s.execute("layer.select", json!({"layer": second.0, "mode": "add"}))?;
let st = s.active().ok_or(EngineError::NoDocument)?;
assert_eq!(st.selected_layers().len(), 2);
Ok(())
}
#[test]
fn a_projective_repeat_on_selected_type_is_disabled_and_does_not_edit() -> Result<()> {
for corners in [DISTORT, PERSPECTIVE] {
let (mut s, pixels) = document(json!({"rect": FRAME, "quad": corners}))?;
let text = id_of(s.execute("type.create", json!({"x": 55, "y": 48, "text": "Editable"}))?)?;
for (first, second) in [(pixels, text), (text, pixels)] {
select_both(&mut s, first, second)?;
assert!(!s.is_enabled(AGAIN), "the text layer blocks Again in either selection order");
let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
let steps = s.active().ok_or(EngineError::NoDocument)?.history.past_len();
let error = s.execute(AGAIN, json!({})).err().map(|e| e.to_string());
assert!(error.is_some_and(|e| e.contains("need rasterized type")));
let st = s.active().ok_or(EngineError::NoDocument)?;
assert_eq!(st.doc, before);
assert_eq!(st.history.past_len(), steps);
}
}
Ok(())
}
#[test]
fn a_projective_repeat_on_unselected_linked_type_is_disabled_and_does_not_edit() -> Result<()> {
let (mut s, pixels) = document(json!({"rect": FRAME, "quad": DISTORT}))?;
let text = id_of(s.execute("type.create", json!({"x": 55, "y": 48, "text": "Editable"}))?)?;
select_both(&mut s, text, pixels)?;
assert_eq!(s.execute("layer.linkLayers", json!({}))?["linked"], true);
s.execute("layer.select", json!({"layer": pixels.0}))?;
let st = s.active().ok_or(EngineError::NoDocument)?;
assert_eq!(st.selected_layers(), &[pixels]);
assert!(!s.is_enabled(AGAIN));
let before = st.doc.clone();
let steps = st.history.past_len();
let error = s.execute(AGAIN, json!({})).err().map(|e| e.to_string());
assert!(error.is_some_and(|e| e.contains("need rasterized type")));
let st = s.active().ok_or(EngineError::NoDocument)?;
assert_eq!(st.doc, before);
assert_eq!(st.history.past_len(), steps);
Ok(())
}
#[test]
fn an_affine_repeat_on_selected_type_stays_enabled_and_runs() -> Result<()> {
for transform in [json!({"matrix": [1, 0, 0, 1, 2, 3]}), json!({"rect": FRAME, "quad": [[12, 12], [36, 12], [39, 30], [15, 30]]})] {
let (mut s, pixels) = document(transform)?;
let text = id_of(s.execute("type.create", json!({"x": 55, "y": 48, "text": "Editable"}))?)?;
select_both(&mut s, text, pixels)?;
assert!(s.is_enabled(AGAIN));
s.execute(AGAIN, json!({}))?;
assert_eq!(s.active().ok_or(EngineError::NoDocument)?.history.undo_label(), Some("Transform Again"));
}
Ok(())
}
#[test]
fn a_projective_repeat_on_pixels_and_a_smart_object_stays_enabled_and_runs() -> Result<()> {
let (mut s, pixels) = document(json!({"rect": FRAME, "quad": DISTORT}))?;
raster(&mut s, Rect::new(45, 24, 75, 50))?;
s.execute("layer.smartObjects.convertToSmartObject", json!({}))?;
let smart = s.active().and_then(|st| st.active_layer).ok_or(EngineError::NoDocument)?;
select_both(&mut s, pixels, smart)?;
assert!(s.is_enabled(AGAIN));
s.execute(AGAIN, json!({}))?;
assert_eq!(s.active().ok_or(EngineError::NoDocument)?.history.undo_label(), Some("Transform Again"));
Ok(())
}