Skip to content

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

feat(type): release area type to box text; warn when PSD can't keep it

PanPDX19261c5f12ca
diff --git a/crates/engine/src/type_area_cmds/tests.rs b/crates/engine/src/type_area_cmds/tests.rs
index 083b01ffc..e8e72953b 100644
--- a/crates/engine/src/type_area_cmds/tests.rs
+++ b/crates/engine/src/type_area_cmds/tests.rs
@@ -113,3 +113,37 @@ fn fill_path_rejects_bad_paths_without_history() {
s.execute("layer.setProps", json!({"layer": id.0, "locked": true})).unwrap();
assert!(s.execute("type.fillPath", json!({})).is_err());
}
+
+#[test]
+fn release_turns_area_type_into_box_text_over_the_path() {
+ let mut s = session(8);
+ let id = text_layer(&mut s, WORDS);
+ set_work_path(&mut s, Path::new(vec![Subpath::polygon(&[(100.0, 100.0), (300.0, 100.0), (300.0, 250.0)])]));
+ s.execute("type.fillPath", json!({})).unwrap();
+ assert!(s.is_enabled("type.detachFromPath"));
+ assert!(!s.is_enabled("type.convertToPointText") && !s.is_enabled("type.convertToParagraphText"));
+ s.execute("type.detachFromPath", json!({})).unwrap();
+ assert_eq!(text(&s, id).shape, TextShape::Box { x: 0.0, y: 0.0, width: 200.0, height: 150.0 });
+ assert!(s.undo());
+ assert!(matches!(text(&s, id).shape, TextShape::Area { .. }));
+}
+
+#[test]
+fn area_type_saves_to_psd_as_paragraph_text_with_a_warning() {
+ let mut s = session(8);
+ let id = text_layer(&mut s, WORDS);
+ set_work_path(&mut s, Path::new(vec![Subpath::polygon(&[(100.0, 100.0), (300.0, 100.0), (300.0, 250.0), (100.0, 250.0)])]));
+ s.execute("type.fillPath", json!({})).unwrap();
+ let doc = s.active().unwrap().doc.clone();
+ let r = photocraft_io::export(&doc, "x.psd", &photocraft_io::ExportOptions::default()).unwrap();
+ assert!(r.warnings.iter().any(|w| w.contains("area type")), "{:?}", r.warnings);
+ let back = photocraft_io::import("x.psd", &r.bytes).unwrap().document;
+ let t = back.walk().into_iter().find_map(|(_, _, l)| match &l.content {
+ LayerContent::Text(t) => Some(t.clone()),
+ _ => None,
+ });
+ let t = t.expect("still a type layer");
+ assert_eq!(t.text, WORDS);
+ assert!(matches!(t.shape, TextShape::Box { width, height, .. } if (width - 200.0).abs() < 0.5 && (height - 150.0).abs() < 0.5), "{:?}", t.shape);
+ let _ = id;
+}
diff --git a/crates/engine/src/type_extra_cmds.rs b/crates/engine/src/type_extra_cmds.rs
index 907b991c4..533958c64 100644
--- a/crates/engine/src/type_extra_cmds.rs
+++ b/crates/engine/src/type_extra_cmds.rs
@@ -393,7 +393,7 @@ fn attach_to_path(s: &mut Session, p: &Value) -> Result<Value> {
fn is_path_text(s: &Session) -> std::result::Result<(), String> {
match active_text(s)?.shape {
- TextShape::Path { .. } => Ok(()),
+ TextShape::Path { .. } | TextShape::Area { .. } => Ok(()),
_ => Err("the type layer is not on a path".into()),
}
}
@@ -401,13 +401,20 @@ fn is_path_text(s: &Session) -> std::result::Result<(), String> {
fn detach_from_path(s: &mut Session, p: &Value) -> Result<Value> {
let id = layer_param(s, p)?;
with_text(s, p, "Release Type from Path", |t, _| {
- if !matches!(t.shape, TextShape::Path { .. }) {
- return Err(EngineError::Other("the type layer is not on a path".into()));
+ match &t.shape {
+ TextShape::Path { .. } => {
+ // Point text starts where the type started on the path.
+ let origin = photocraft_text::warp::Warp::on_path(&t.shape).map_or((0.0, 0.0), |w| w.apply(0.0, 0.0));
+ t.shape = TextShape::Point;
+ t.transform = then_translate(&t.transform, origin.0, origin.1);
+ }
+ // Area type keeps wrapping, in a text box over the path's bounds.
+ TextShape::Area { path } => {
+ let (x0, y0, x1, y1) = path.control_bounds().ok_or_else(|| EngineError::Other("the area type's path is empty".into()))?;
+ t.shape = TextShape::Box { x: x0 as f32, y: y0 as f32, width: ((x1 - x0) as f32).max(1.0), height: ((y1 - y0) as f32).max(1.0) };
+ }
+ _ => return Err(EngineError::Other("the type layer is not on a path".into())),
}
- // Point text starts where the type started on the path.
- let origin = photocraft_text::warp::Warp::on_path(&t.shape).map_or((0.0, 0.0), |w| w.apply(0.0, 0.0));
- t.shape = TextShape::Point;
- t.transform = then_translate(&t.transform, origin.0, origin.1);
Ok(())
})?;
Ok(json!({"layer": id.0}))
diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs
index 8ba30d83c..7265c0ccb 100644
--- a/crates/io/src/psd_export.rs
+++ b/crates/io/src/psd_export.rs
@@ -625,6 +625,10 @@ impl Ex {
crate::vector_map::shape_blocks(sh, &mut raw, w, h, self.dpi);
}
LayerContent::Text(t) => {
+ if matches!(t.shape, photocraft_doc::text::TextShape::Area { .. }) {
+ self.warnings
+ .push(format!("layer \"{}\": area type is saved as paragraph text over its path's bounds (PSD area type is not written yet)", l.name));
+ }
// Text layers without PSD data (created here) get a generated TySh.
let generated = t.psd_raw.is_none().then(|| std::sync::Arc::new(photocraft_text::psd::build_tysh(t, self.dpi, None)));
let src = t.psd_raw.as_ref().or(generated.as_ref());