PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Moving type keeps its rendering: Artboards to Files no longer drops PSD text (#3218)
Fabian Ghenof14733b522385
diff --git a/crates/engine/src/artboard_cmds/tests.rs b/crates/engine/src/artboard_cmds/tests.rsindex 6f3de6b11..793d4ca0d 100644--- a/crates/engine/src/artboard_cmds/tests.rs+++ b/crates/engine/src/artboard_cmds/tests.rs@@ -427,6 +427,82 @@ fn moving_an_artboard_past_the_canvas_keeps_its_shapes_whole() {}}+/// A type layer as a PSD import leaves it: Photoshop's rendering as the cache (deliberately not+/// what PhotoCraft would draw, a solid block, so a re-render shows) and Photoshop's `TySh`.+fn add_imported_type(s: &mut Session, block: Rect) -> LayerId {+ s.edit("type", |doc, active| {+ let mut t = photocraft_doc::TextLayer { text: "Hello!".into(), font_family: "No Such Font".into(), size_pt: 26.0, ..Default::default() };+ t.transform = photocraft_geom::Affine::translate(f64::from(block.x0), f64::from(block.y1));+ t.psd_raw = Some(std::sync::Arc::new(photocraft_text::psd::build_tysh(&t, 72.0, None)));+ let fmt = doc.pixel_format();+ let mut cache = photocraft_raster::Surface::new(fmt);+ cache.fill_rect(block, &photocraft_raster::from_rgba(&fmt, [0.4, 0.2, 0.6, 1.0]));+ t.cache = Some(cache);+ let id = doc.insert_above(None, Layer::new("Hello!", LayerContent::Text(t)));+ *active = Some(id);+ Ok(id)+ })+ .unwrap()+}++fn type_layer(doc: &Document, id: LayerId) -> photocraft_doc::TextLayer {+ match &doc.layer(id).unwrap().content {+ LayerContent::Text(t) => t.clone(),+ other => panic!("not a type layer: {}", other.kind_name()),+ }+}++#[test]+fn artboard_export_keeps_imported_type_as_rendered() {+ for depth in [8, 16, 32] {+ let mut s = session(depth);+ let text = add_imported_type(&mut s, Rect::new(30, 20, 60, 30));+ s.execute("layer.new.artboard", json!({"rect": [20, 10, 50, 40]})).unwrap();+ let board = doc(&s).artboards()[0].0;+ s.edit("into board", |doc, _| {+ let l = doc.remove(text).unwrap();+ let Some(LayerContent::Group(g)) = doc.layer_mut(board).map(|b| &mut b.content) else { panic!("artboard group") };+ g.children.push(l);+ Ok(())+ })+ .unwrap();+ let one = artboard_document(doc(&s), board).unwrap().unwrap();+ let t = type_layer(&one, text);+ // The same pixels, moved to the board's origin: not re-rendered.+ let c = t.cache.as_ref().unwrap();+ assert_eq!(c.content_bounds(), Rect::new(10, 10, 40, 20), "depth {depth}");+ let px = photocraft_raster::to_rgba(&c.format(), &c.pixel(15, 15));+ assert!((px[0] - 0.4).abs() < 0.01 && (px[2] - 0.6).abs() < 0.01, "depth {depth}: {px:?}");+ let tysh = photocraft_text::psd::parse_tysh(t.psd_raw.as_deref().unwrap()).unwrap();+ assert_eq!((tysh.transform.m[4], tysh.transform.m[5]), (10.0, 20.0), "the PSD position moves with it");+ }+}++#[test]+fn moving_imported_type_shifts_it_without_re_rendering() {+ let mut s = session(8);+ let text = add_imported_type(&mut s, Rect::new(30, 20, 60, 30));+ let before = type_layer(doc(&s), text);+ s.execute("layer.translate", json!({"layer": text.0, "dx": 5, "dy": -3})).unwrap();+ let after = type_layer(doc(&s), text);+ assert_eq!(after.cache.as_ref().unwrap().content_bounds(), Rect::new(35, 17, 65, 27));+ assert_eq!(after.transform.m[4..], [35.0, 27.0]);+ let (old, new) = (before.psd_raw.unwrap(), after.psd_raw.unwrap());+ assert_eq!((&new[..34], &new[50..]), (&old[..34], &old[50..]), "only the TySh translation changes");+ s.undo();+ assert_eq!(type_layer(doc(&s), text).cache, before.cache);+ // Type with nothing rendered yet is rendered after the move, as before.+ s.edit("drop cache", |doc, _| {+ if let Some(LayerContent::Text(t)) = doc.layer_mut(text).map(|l| &mut l.content) {+ t.cache = None;+ }+ Ok(())+ })+ .unwrap();+ s.execute("layer.translate", json!({"layer": text.0, "dx": 1, "dy": 0})).unwrap();+ assert!(type_layer(doc(&s), text).cache.is_some());+}+#[test]fn moving_an_artboard_left_of_or_above_the_origin_grows_the_canvas_there() {// Photoshop grows an artboard document in every direction; the canvas only grew right anddiff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 70b1385bb..e71aad6ea 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -1346,7 +1346,18 @@ pub(crate) fn translate_layer(doc: &Document, l: &mut Layer, dx: i32, dy: i32) {LayerContent::Raster(s) => *s = translate_surface(s, dx, dy),LayerContent::Text(t) => {t.transform = photocraft_geom::Affine::translate(dx as f64, dy as f64).mul(&t.transform);- crate::type_cmds::refresh(doc, t);+ // A whole-pixel move shifts the rendered text and its PSD position as they are.+ // Re-rendering would replace Photoshop's rendering of imported type, and drop the+ // text when its font is missing (artboard export, moving type with the Move tool).+ let raw = t.psd_raw.as_deref().map(|r| photocraft_text::psd::translate_tysh(r, f64::from(dx), f64::from(dy)));+ match (&mut t.cache, raw) {+ (Some(c), Some(Some(r))) => {+ *c = translate_surface(c, dx, dy);+ t.psd_raw = Some(std::sync::Arc::new(r));+ }+ (Some(c), None) => *c = translate_surface(c, dx, dy),+ _ => crate::type_cmds::refresh(doc, t),+ }}LayerContent::Group(g) => {// Moving an artboard moves the board with its contents.diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rsindex 39255b79f..85ce2f6a0 100644--- a/crates/text/src/psd.rs+++ b/crates/text/src/psd.rs@@ -51,6 +51,20 @@ pub fn parse_tysh(data: &[u8]) -> Option<TySh> {Some(TySh { transform: Affine { m }, text: text.descriptor, warp, bounds })}+/// `tysh` moved by (`dx`, `dy`): only the transform's translation changes. The text, its styles,+/// the warp and the trailing bounds (which are in text space) stay byte for byte, so a moved+/// Photoshop type layer keeps everything Photoshop wrote. `None` when the block is too short.+pub fn translate_tysh(tysh: &[u8], dx: f64, dy: f64) -> Option<Vec<u8>> {+ let mut out = tysh.to_vec();+ // Version (2 bytes), then xx, xy, yx, yy, tx, ty as big-endian f64: tx at 34, ty at 42.+ for (at, d) in [(34, dx), (42, dy)] {+ let b = out.get_mut(at..at + 8)?;+ let v = f64::from_be_bytes(b.try_into().ok()?) + d;+ b.copy_from_slice(&v.to_be_bytes());+ }+ Some(out)+}+/// Serializes a `TySh` block.pub fn write_tysh(t: &TySh) -> Vec<u8> {let mut v = 1u16.to_be_bytes().to_vec();diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rsindex e966cf686..ea63b8551 100644--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@ -1419,3 +1419,18 @@ fn a_served_script_fallback_joins_the_fallback_stack_once_it_arrives() {db.register_font_data(cairo.bytes.to_vec());assert!(db.fallback_stack().any(|f| f == "Cairo"), "{:?}", db.fallback_stack().collect::<Vec<_>>());}++#[test]+fn translate_tysh_moves_only_the_translation() {+ let mut t = point("Hello!", 26.0);+ t.transform = Affine { m: [1.5, 0.0, 0.0, 1.5, 80.0, 340.0] };+ let tysh = crate::psd::build_tysh(&t, 72.0, Some([0.0, -20.0, 300.0, 4.0]));+ let moved = crate::psd::translate_tysh(&tysh, -8.0, -279.0).unwrap();+ let back = crate::psd::parse_tysh(&moved).unwrap();+ assert_eq!(back.transform.m, [1.5, 0.0, 0.0, 1.5, 72.0, 61.0]);+ // Everything but the 16 translation bytes is unchanged.+ assert_eq!(moved.len(), tysh.len());+ assert_eq!((&moved[..34], &moved[50..]), (&tysh[..34], &tysh[50..]));+ assert_eq!(crate::psd::parse_tysh(&tysh).unwrap().bounds, back.bounds, "bounds are in text space");+ assert!(crate::psd::translate_tysh(&tysh[..40], 1.0, 1.0).is_none(), "a truncated block is refused");+}