diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs index 47b4bbd34a..bf78919740 100644 --- a/apps/photocraft/src/services.rs +++ b/apps/photocraft/src/services.rs @@ -543,6 +543,61 @@ fn image_from_offers(offers: &[String], mut fetch: impl FnMut(&str) -> Option Option<(u32, u32, Vec)> { + if u64::try_from(text.len()).ok()? > MAX_CLIPBOARD_SVG_BYTES || !text.trim_start_matches('\u{feff}').trim_start().starts_with('<') { + return None; + } + crate::crash_guard::guard("Paste", || photocraft_io::svg::rasterize_rgba8(text.as_bytes()).map_err(|e| e.to_string())).ok() +} + +/// What a paste reads from the OS clipboard, in this order: the first copied file that is an +/// image (Finder also puts the file's icon on the clipboard as an image, which would otherwise +/// paste instead of the file), the clipboard's image, then a drawing copied as SVG markup. Each +/// read runs only when the ones before it found nothing. A read that fails, such as one of a busy +/// clipboard, returns its error, and the reads after it don't run. +fn clipboard_image( + clipboard: &mut C, + files: impl FnOnce(&mut C) -> Result>, String>, + image: impl FnOnce(&mut C) -> Result)>, String>, + text: impl FnOnce(&mut C) -> Result, String>, +) -> Result)>, String> { + if let Some(img) = files(clipboard)?.and_then(|paths| image_from_files(&paths)) { + return Ok(Some(img)); + } + if let Some(img) = image(clipboard)? { + return Ok(Some(img)); + } + Ok(text(clipboard)?.and_then(|text| image_from_svg_text(&text))) +} + +/// One OS clipboard read as [`clipboard_image`] takes it: no content in the format asked for is +/// `None`, and any other failure, such as a busy clipboard or a broken pipe on Wayland, is the +/// read's error. +fn clipboard_read(read: Result) -> Result, String> { + match read { + Ok(value) => Ok(Some(value)), + Err(arboard::Error::ContentNotAvailable) => Ok(None), + Err(error) => Err(error.to_string()), + } +} + +/// Clipboard text as [`clipboard_image`] takes it: like [`clipboard_read`], except that text that +/// isn't UTF-8 is `None`, like other text that isn't SVG markup. +fn clipboard_text(read: Result) -> Result, String> { + match read { + Err(arboard::Error::ConversionFailure) => Ok(None), + read => clipboard_read(read), + } +} + /// The shell command that starts this install under XWayland, where winit delivers file drops /// (winit 0.30 has none on Wayland, #386). A Flatpak gets the X11 socket only without the Wayland /// one, an AppImage relaunches by its own path, and anything else is `photocraft` on the PATH. @@ -660,30 +715,24 @@ pub fn native(automation: Option) -> slot.insert(opened.map_err(|e| e.to_string())?) } }; - // Copied files first: Finder also puts the file's icon on the clipboard as an - // image, which would otherwise paste instead of the file. - match cb.get().file_list() { - Ok(paths) => { - if let Some(img) = image_from_files(&paths) { - return Ok(Some(img)); + clipboard_image( + cb, + |cb| clipboard_read(cb.get().file_list()), + |cb| match cb.get_image() { + Ok(img) => Ok(Some((img.width as u32, img.height as u32, img.bytes.into_owned()))), + Err(error) => { + #[cfg(target_os = "linux")] + if let Some(image) = wayland_clipboard_image() { + return Ok(Some(image)); + } + match error { + arboard::Error::ContentNotAvailable => Ok(None), + _ => Err(error.to_string()), + } } - } - Err(error @ arboard::Error::ClipboardOccupied) => return Err(error.to_string()), - Err(_) => {} - } - match cb.get_image() { - Ok(img) => Ok(Some((img.width as u32, img.height as u32, img.bytes.into_owned()))), - Err(error) => { - #[cfg(target_os = "linux")] - if let Some(image) = wayland_clipboard_image() { - return Ok(Some(image)); - } - match error { - arboard::Error::ContentNotAvailable => Ok(None), - _ => Err(error.to_string()), - } - } - } + }, + |cb| clipboard_text(cb.get_text()), + ) }) }), clipboard_unreadable: Some(Box::new(move || clip_unreadable.get())), diff --git a/apps/photocraft/src/services/tests.rs b/apps/photocraft/src/services/tests.rs index f9bce2a367..c04b8171df 100644 --- a/apps/photocraft/src/services/tests.rs +++ b/apps/photocraft/src/services/tests.rs @@ -660,6 +660,175 @@ fn tga_is_trusted_only_with_its_extension() { assert!(image_from_files(&[bin]).is_none()); } +/// A 121 × 81 white rectangle with a 1-unit black stroke, written as markup the way a vector +/// app's Copy puts it on the clipboard as text. +const COPIED_RECTANGLE: &str = r##" + + + +"##; + +/// A 40 × 20 drawing whose left half is a red square; the right half has no art. +const HALF_RED: &str = r##""##; + +/// The clipboard as `clipboard_image` reads it when it holds only `text`. +fn from_text(text: &str) -> Option<(u32, u32, Vec)> { + clipboard_image(&mut (), |_| Ok(None), |_| Ok(None), |_| Ok(Some(text.to_string()))).expect("no read fails") +} + +fn pixel(img: &(u32, u32, Vec), x: u32, y: u32) -> [u8; 4] { + let at = ((y * img.0 + x) * 4) as usize; + img.2[at..at + 4].try_into().unwrap() +} + +#[test] +fn svg_markup_copied_as_text_pastes_its_pixels() { + let img = from_text(COPIED_RECTANGLE).expect("the drawing's pixels"); + assert_eq!((img.0, img.1, img.2.len()), (121, 81, 121 * 81 * 4), "one pixel per SVG unit"); + assert_eq!(pixel(&img, 60, 40), [255, 255, 255, 255], "the white fill"); + for (x, y) in [(0, 40), (120, 40), (60, 0), (60, 80)] { + let [r, g, b, a] = pixel(&img, x, y); + assert!(a == 255 && r < 8 && g < 8 && b < 8, "the black stroke at {x},{y}: {:?}", [r, g, b, a]); + } + let img = from_text(HALF_RED).expect("the drawing's pixels"); + assert_eq!((img.0, img.1), (40, 20)); + assert_eq!(pixel(&img, 5, 10), [255, 0, 0, 255]); + assert_eq!(pixel(&img, 30, 10), [0, 0, 0, 0], "transparent where there is no art"); +} + +#[test] +fn text_that_is_not_a_drawing_pastes_nothing() { + for text in ["", "hello", "

not a drawing

", ""] { + assert!(from_text(text).is_none(), "{text:?}"); + } + // Markup that doesn't parse, and markup past the read limit, paste nothing either. + assert!(from_text("").is_none()); + let mut huge = String::from(""); + huge.push_str(&" ".repeat(usize::try_from(MAX_CLIPBOARD_SVG_BYTES).unwrap())); + huge.push_str(""); + assert!(from_text(&huge).is_none(), "markup past the limit isn't parsed"); +} + +#[test] +fn svg_clipboard_text_accepts_a_long_xml_preamble() { + let padding = "x".repeat(8192); + for prefix in [format!(""), format!("\n"), format!("")] { + let text = format!("{prefix}{HALF_RED}"); + let img = from_text(&text).expect("SVG after the XML preamble"); + assert_eq!((img.0, img.1), (40, 20)); + assert_eq!(pixel(&img, 5, 10), [255, 0, 0, 255]); + assert_eq!(pixel(&img, 30, 10), [0, 0, 0, 0]); + } + assert!(from_text(&format!("")).is_none()); + assert!(from_text(&format!("")).is_none()); +} + +#[test] +fn a_drawing_past_the_pixel_limit_pastes_scaled_down() { + let wide = r##""##; + let img = from_text(wide).expect("scaled down, not dropped"); + let pixels = u64::from(img.0) * u64::from(img.1); + assert!(pixels <= 24_000_000 && pixels > 12_000_000, "{} × {}", img.0, img.1); + assert!(img.0.abs_diff(img.1 * 2) <= 2, "the proportions are kept: {} × {}", img.0, img.1); + assert_eq!(pixel(&img, img.0 / 2, img.1 / 2), [0, 0, 255, 255]); +} + +/// Copied files and an image come before SVG text, which is read only when neither is there. +#[test] +fn copied_files_and_images_come_before_svg_text() { + let dir = temp("svg-order"); + let png = dir.join("red.png"); + std::fs::write(&png, red_png(3, 2)).unwrap(); + let blue = (2, 2, [0u8, 0, 255, 255].repeat(4)); + let mut text_read = false; + let svg_text = |read: &mut bool| { + *read = true; + Ok(Some(COPIED_RECTANGLE.to_string())) + }; + let got = clipboard_image(&mut text_read, |_| Ok(Some(vec![png.clone()])), |_| Ok(Some(blue.clone())), svg_text); + assert_eq!(got.unwrap().map(|(w, h, _)| (w, h)), Some((3, 2)), "the copied file"); + let got = clipboard_image(&mut text_read, |_| Ok(None), |_| Ok(Some(blue.clone())), svg_text); + assert_eq!(got.unwrap().map(|(w, h, _)| (w, h)), Some((2, 2)), "the image, not the SVG text"); + assert!(!text_read, "the text isn't read while there is a file or an image"); + // A copied file that isn't an image (an .svg) falls through to the image, then the text. + let svg = dir.join("drawing.svg"); + std::fs::write(&svg, COPIED_RECTANGLE).unwrap(); + let got = clipboard_image(&mut (), |_| Ok(Some(vec![svg.clone()])), |_| Ok(None), |_| Ok(Some(COPIED_RECTANGLE.to_string()))); + assert_eq!(got.unwrap().map(|(w, h, _)| (w, h)), Some((121, 81))); +} + +/// A failed read, such as one of a busy clipboard, is the paste's error: the reads after it, +/// the SVG text included, don't run. +#[test] +fn a_failed_clipboard_read_is_an_error_and_the_text_is_not_read() { + fn busy() -> Result { + Err("the clipboard is busy".to_string()) + } + let mut text_read = false; + let svg_text = |read: &mut bool| { + *read = true; + Ok(Some(COPIED_RECTANGLE.to_string())) + }; + let got = clipboard_image(&mut text_read, |_| busy(), |_| Ok(None), svg_text); + assert_eq!(got, Err("the clipboard is busy".to_string()), "the file list's error"); + let got = clipboard_image(&mut text_read, |_| Ok(None), |_| busy(), svg_text); + assert_eq!(got, Err("the clipboard is busy".to_string()), "the image's error"); + assert!(!text_read, "the text isn't read after a failed read"); + let got = clipboard_image(&mut (), |_| Ok(None), |_| Ok(None), |_| busy()); + assert_eq!(got, Err("the clipboard is busy".to_string()), "the text's error"); +} + +/// Only a clipboard with nothing in the format asked for reads as nothing to paste. A read +/// that fails in any other way, such as a broken pipe on Wayland, is the paste's error, for +/// the copied files and the text alike. Text that isn't UTF-8 reads as no SVG markup. +#[test] +fn only_missing_content_reads_as_nothing_to_paste() { + use arboard::Error; + let failures = || [Error::ClipboardOccupied, Error::ClipboardNotSupported, Error::Unknown { description: "broken pipe".to_string() }]; + assert_eq!(clipboard_read(Ok::<_, Error>(vec![PathBuf::from("a.png")])), Ok(Some(vec![PathBuf::from("a.png")]))); + assert_eq!(clipboard_read::>(Err(Error::ContentNotAvailable)), Ok(None)); + for error in failures().into_iter().chain([Error::ConversionFailure]) { + let message = error.to_string(); + assert_eq!(clipboard_read::>(Err(error)), Err(message.clone()), "{message}"); + } + assert_eq!(clipboard_text(Ok("words".to_string())), Ok(Some("words".to_string()))); + assert_eq!(clipboard_text(Err(Error::ContentNotAvailable)), Ok(None)); + assert_eq!(clipboard_text(Err(Error::ConversionFailure)), Ok(None), "text that isn't UTF-8"); + for error in failures() { + let message = error.to_string(); + assert_eq!(clipboard_text(Err(error)), Err(message.clone()), "{message}"); + } +} + +/// Edit › Paste with SVG markup as the clipboard's only content adds a new layer that holds the +/// drawing's art; other text is still a quiet "nothing to paste". +#[test] +fn paste_turns_svg_text_on_the_clipboard_into_a_layer() { + let text: Rc> = Rc::new(RefCell::new(HALF_RED.to_string())); + let clip = text.clone(); + let services = Services { + clipboard_get_image: Some(Box::new(move || clipboard_image(&mut (), |_| Ok(None), |_| Ok(None), |_| Ok(Some(clip.borrow().clone()))))), + ..Default::default() + }; + let mut app = PhotocraftApp::new(Session::new(), services); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + let layers = app.session.active().unwrap().doc.layers.len(); + let r = photocraft_ui_egui::menus::invoke(&mut app, &ctx, "edit.paste", json!({})).unwrap(); + assert_ne!(r["pasted"], json!(false), "{r}"); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layers.len(), layers + 1); + let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + let bounds = surface.content_bounds(); + assert_eq!((bounds.width(), bounds.height()), (20, 20), "the red half; the transparent half adds nothing"); + *text.borrow_mut() = "just words".to_string(); + let r = photocraft_ui_egui::menus::invoke(&mut app, &ctx, "edit.paste", json!({})).unwrap(); + assert_eq!(r["pasted"], json!(false)); + assert_eq!(app.ui.status, "Nothing to paste: the clipboard holds no image"); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers + 1); +} + /// #7582: a panic while reading the OS clipboard (in arboard, or decoding a copied file) is /// the paste's error in the status bar, not a crash: the document stays and the next paste /// works. The same for a panic while writing it. diff --git a/crates/automation/src/rpc/tests.rs b/crates/automation/src/rpc/tests.rs index b7e22b3ffa..5136b0363b 100644 --- a/crates/automation/src/rpc/tests.rs +++ b/crates/automation/src/rpc/tests.rs @@ -377,6 +377,44 @@ fn doc_place_reads_through_the_read_root_and_never_outside_it() { std::fs::remove_dir_all(base).unwrap(); } +/// A save or render never turns a file PhotoCraft doesn't write (source, settings, notes) +/// into image bytes, whatever `format` says; images and new files are written as before (#9439). +#[test] +fn workspace_writes_refuse_to_replace_a_file_that_is_not_an_image() { + let root = std::env::temp_dir().join(format!("pc-no-replace-foreign-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(root.join("src")).unwrap(); + let foreign = [("src/main.rs", "fn main() {}\n"), ("Cargo.toml", "[package]\n"), (".env", "TOKEN=x\n"), ("Makefile", "all:\n")]; + for (name, text) in foreign { + std::fs::write(root.join(name), text).unwrap(); + } + std::fs::write(root.join("old.png"), b"not yet a png").unwrap(); + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap(); + for (name, text) in foreign { + let saved = h.handle("doc.save", json!({"path": name, "format": "png"})).unwrap_err().to_string(); + assert!(saved.contains("already exists"), "doc.save over {name}: {saved}"); + let rendered = h.handle("doc.render", json!({"path": name})).unwrap_err().to_string(); + assert!(rendered.contains("already exists"), "doc.render over {name}: {rendered}"); + assert_eq!(std::fs::read_to_string(root.join(name)).unwrap(), text, "{name} is untouched"); + } + // An existing image is replaced, and a new file of any name is created. + h.handle("doc.save", json!({"path": "old.png"})).unwrap(); + assert!(std::fs::read(root.join("old.png")).unwrap().starts_with(b"\x89PNG")); + h.handle("doc.save", json!({"path": "fresh.txt", "format": "png"})).unwrap(); + assert!(std::fs::read(root.join("fresh.txt")).unwrap().starts_with(b"\x89PNG")); + h.handle("doc.render", json!({"path": "old.png"})).unwrap(); + std::fs::write(root.join("OLD.TIF"), b"x").unwrap(); + h.handle("doc.save", json!({"path": "OLD.TIF"})).unwrap(); + // An image saved under a name without an image extension is recognised by its bytes. + h.handle("doc.save", json!({"path": "out", "format": "png"})).unwrap(); + h.handle("doc.save", json!({"path": "out", "format": "png"})).unwrap(); + h.handle("doc.render", json!({"path": "fresh.txt"})).unwrap(); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(root).unwrap(); +} + #[test] fn doc_replace_smart_object_reads_through_the_read_root_and_never_outside_it() { let base = std::env::temp_dir().join(format!("pc-doc-replace-so-{}", std::process::id())); diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index 13cbbb2169..4dd1a5df05 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -88,6 +88,22 @@ impl AuthorizedWorkspace { let relative = request_path(path, self.write.as_ref())?; let root = self.write.as_ref().ok_or_else(|| AutomationError::BadRequest(format!("{DENIED}: write authority is absent")))?; let leaf = relative.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + // The bytes are an image or document whatever the name says (`format` picks them), so + // only a file of a kind PhotoCraft writes is replaced: never source, settings or notes + // that share the root (`src/main.rs`, `.env`; #9439). A new file may have any name, and + // an image saved under one (`out` with `format: png`) can be saved over again. + let exists = root.dir.symlink_metadata(&relative).is_ok(); + // Only a regular file is ever opened below (its head here, its attributes on Unix): a + // FIFO would block that open until a writer appears, and with it every other request. + // Links are followed for this check; a dangling one falls through to the name rule. + if exists && root.dir.metadata(&relative).is_ok_and(|m| !m.is_file()) { + return Err(AutomationError::BadRequest(format!("automation write refused: `{path}` already exists and is not a regular file"))); + } + if exists && !written_kind(&leaf) && !holds_image(&root.dir, &relative) { + return Err(AutomationError::BadRequest(format!( + "automation write refused: `{path}` already exists and is not an image or document file PhotoCraft writes; save to a new name" + ))); + } let parent = relative.parent().map(Path::to_path_buf).unwrap_or_default(); let tmp = parent.join(photocraft_format::atomic::temp_name(&leaf)); let mut options = OpenOptions::new(); @@ -124,6 +140,23 @@ impl AuthorizedWorkspace { } } +/// Whether `name`'s extension is one of the formats PhotoCraft saves or exports. +fn written_kind(name: &str) -> bool { + let Some((_, ext)) = name.rsplit_once('.') else { return false }; + let ext = ext.to_ascii_lowercase(); + ["psd", "psb", "ora", "pdf", photocraft_format::EXTENSION].contains(&ext.as_str()) || photocraft_codecs::from_extension(&ext).is_some() +} + +/// Whether the file at `relative` starts like an image PhotoCraft reads (PSD/PSB or a codec's +/// magic). Containers that also hold other things (zip, PDF) are judged by name only. +fn holds_image(dir: &Dir, relative: &Path) -> bool { + let mut head = [0u8; 64]; + let Ok(mut file) = dir.open(relative) else { return false }; + let Ok(n) = file.read(&mut head) else { return false }; + let head = &head[..n]; + head.starts_with(b"8BPS") || photocraft_codecs::detect(head).is_some() +} + fn read_to_observed_length(reader: &mut impl Read, observed_length: u64) -> Result, AutomationError> { // Do not reserve from a potentially stale or attacker-controlled metadata length. Read in // chunks, and reject growth beyond the length observed on the opened file before appending. @@ -221,11 +254,14 @@ fn relative_path(raw: &str) -> Result { Ok(PathBuf::from(raw)) } +/// At least every name cap-std refuses to open on Windows (`COM0`–`COM9`, `COM¹`–`COM³`, the same +/// for `LPT`, spaces before the extension ignored), so nothing written is unreadable (#9424). fn is_windows_device_name(component: &str) -> bool { - let stem = component.split('.').next().unwrap_or(component).to_ascii_uppercase(); + let stem = component.split('.').next().unwrap_or(component).trim_end().to_ascii_uppercase(); + let port = |n: &str| matches!(n, "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"); matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$") - || stem.strip_prefix("COM").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) - || stem.strip_prefix("LPT").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) + || stem.strip_prefix("COM").is_some_and(port) + || stem.strip_prefix("LPT").is_some_and(port) } fn path_error(path: &str, reason: &str) -> AutomationError { diff --git a/crates/automation/src/workspace/tests.rs b/crates/automation/src/workspace/tests.rs index 2a94de15f2..63a5b0b8c7 100644 --- a/crates/automation/src/workspace/tests.rs +++ b/crates/automation/src/workspace/tests.rs @@ -122,6 +122,46 @@ fn write_replaces_atomically_and_leaves_no_temp_file() { assert!(temp_files(&inside).is_empty()); } +/// A directory at the target is refused before anything is opened, even under an image name. +#[test] +fn write_refuses_a_directory_target_before_opening_it() { + let (inside, _, workspace) = roots("dir-target"); + for name in ["adir", "adir.png"] { + std::fs::create_dir_all(inside.join(name)).unwrap(); + let error = workspace.write(name, b"x").unwrap_err().to_string(); + assert!(error.contains("is not a regular file"), "{name}: {error}"); + assert!(inside.join(name).is_dir()); + } + assert!(temp_files(&inside).is_empty()); +} + +/// A FIFO at the target is refused at once: opening it to sniff its head or copy its +/// attributes would block until a writer appears, stalling every other request. +#[cfg(unix)] +#[test] +fn write_refuses_a_fifo_target_without_blocking() { + let (inside, _, workspace) = roots("fifo-target"); + for name in ["pipe", "out.png"] { + let made = std::process::Command::new("mkfifo").arg(inside.join(name)).status(); + if !made.is_ok_and(|s| s.success()) { + return; // no mkfifo on this system: nothing to assert + } + } + std::os::unix::fs::symlink("pipe", inside.join("link")).unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + for name in ["pipe", "out.png", "link"] { + let _ = tx.send((name, workspace.write(name, b"x").map_err(|e| e.to_string()))); + } + }); + for _ in 0..3 { + let (name, result) = rx.recv_timeout(std::time::Duration::from_secs(5)).expect("write blocked on a FIFO"); + let error = result.unwrap_err(); + assert!(error.contains("is not a regular file"), "{name}: {error}"); + } + assert!(temp_files(&inside).is_empty()); +} + /// #7095: replacing a file through the automation path keeps the old file's extended /// attributes, the same as a save through `atomic_write` does. #[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] @@ -177,6 +217,32 @@ fn rejects_absolute_traversal_mixed_prefix_and_malformed_paths_without_panicking } } +#[test] +fn every_device_name_cap_std_refuses_to_open_is_refused_before_any_write() { + // cap-std refuses these when it opens a file on Windows, so a file written under one of + // them could not be read back (#9424). + let (inside, _, workspace) = roots("devices"); + std::fs::create_dir(inside.join("sub")).unwrap(); + for path in ["COM0.txt", "LPT0", "COM¹.png", "com².png", "COM³", "LPT¹.txt", "LPT².txt", "lpt³", "NUL .png", "Aux .txt", "sub/CON .psd", "COM¹/frame.png"] + { + let error = workspace.write(path, b"x").unwrap_err().to_string(); + assert!(error.contains("reserved device names"), "write {path:?}: {error}"); + let error = workspace.read(path).unwrap_err().to_string(); + assert!(error.contains("reserved device names"), "read {path:?}: {error}"); + } + for dir in ["COM¹", "LPT0", "sub/PRN .x"] { + let error = workspace.export_sequence(dir).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(error.contains("reserved device names"), "export {dir:?}: {error}"); + } + assert_eq!(std::fs::read_dir(&inside).unwrap().count(), 1, "only `sub` may exist"); + assert_eq!(std::fs::read_dir(inside.join("sub")).unwrap().count(), 0); + // Look-alikes are ordinary names. + for path in ["COM10.txt", "COM¹0.txt", "LPT00", "console.txt", "NULL.txt", "nul_.txt", " NUL.txt"] { + workspace.write(path, b"x").unwrap_or_else(|e| panic!("write {path:?}: {e}")); + assert_eq!(workspace.read(path).unwrap(), b"x", "{path:?}"); + } +} + #[test] fn read_and_write_authority_are_separate() { let (inside, _, _) = roots("separate"); diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index 6a85f53908..3feeb3a97d 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -87,16 +87,50 @@ impl LutFile { } } +/// A LUT file format. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Format { + Cube, + ThreeDl, + Look, +} + +impl Format { + /// The format [`parse`] reads `bytes` as: from `file_name`'s extension, else sniffed + /// (XML is `.look`, anything else `.cube`). + pub fn detect(file_name: &str, bytes: &[u8]) -> Self { + let lower = file_name.to_ascii_lowercase(); + if lower.ends_with(".3dl") { + Format::ThreeDl + } else if lower.ends_with(".look") || decode_text(bytes).trim_start().starts_with('<') { + Format::Look + } else { + Format::Cube + } + } + + /// The file extension, without the dot. + pub fn extension(self) -> &'static str { + match self { + Format::Cube => "cube", + Format::ThreeDl => "3dl", + Format::Look => "look", + } + } +} + /// Parses a LUT, picking the format from `file_name`'s extension (falls back to sniffing). pub fn parse(file_name: &str, bytes: &[u8]) -> Result { - let lower = file_name.to_ascii_lowercase(); + parse_as(Format::detect(file_name, bytes), bytes) +} + +/// Parses a LUT in a known format. +pub fn parse_as(format: Format, bytes: &[u8]) -> Result { let text = decode_text(bytes); - if lower.ends_with(".3dl") { - parse_3dl(&text) - } else if lower.ends_with(".look") || text.trim_start().starts_with('<') { - parse_look(&text) - } else { - parse_cube(&text) + match format { + Format::Cube => parse_cube(&text), + Format::ThreeDl => parse_3dl(&text), + Format::Look => parse_look(&text), } } @@ -518,6 +552,9 @@ DOMAIN_MAX 0.5 1 1 let be: Vec = [0xFE, 0xFF].into_iter().chain(xml.encode_utf16().flat_map(u16::to_be_bytes)).collect(); assert_eq!(parse("a.look", &le).unwrap().data, id.data); assert_eq!(parse("a.look", &be).unwrap().data, id.data); + // Sniffed as XML through the byte-order mark too, so the recorded format is the one parsed. + assert_eq!(Format::detect("a.lut", &le), Format::Look); + assert_eq!(parse_as(Format::Look, &be).unwrap().data, id.data); } #[test] @@ -570,6 +607,17 @@ DOMAIN_MAX 0.5 1 1 assert!(parse_look(&text).is_err()); } + #[test] + fn format_detection_matches_parse() { + assert_eq!(Format::detect("a.3DL", b""), Format::ThreeDl); + assert_eq!(Format::detect("a.Look", b""), Format::Look); + assert_eq!(Format::detect("a.cube", b" "), Format::Look, "XML is sniffed as .look"); + assert_eq!(Format::detect("a.txt", b"LUT_3D_SIZE 2"), Format::Cube); + assert_eq!(Format::ThreeDl.extension(), "3dl"); + let text = write_cube(&LutFile::identity(2)); + assert_eq!(parse_as(Format::Cube, text.as_bytes()), parse("a.cube", text.as_bytes())); + } + #[test] fn malformed_rejected() { assert!(parse_cube("LUT_3D_SIZE 2\n0 0 0\n").is_err()); diff --git a/crates/compose/proptest-regressions/cmyk_tests.txt b/crates/compose/proptest-regressions/cmyk_tests.txt index 2f3a9c3fb8..2498f3013c 100644 --- a/crates/compose/proptest-regressions/cmyk_tests.txt +++ b/crates/compose/proptest-regressions/cmyk_tests.txt @@ -7,3 +7,4 @@ cc a795914e4492bef1e662315563d48d54f09030c48e046df95329465c86dde4b4 # shrinks to s = Spec { w: 4, h: 6, layers: [Group { children: [Pixels { kind: Raster, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [1.0, 0.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: false, visible: true, mask: None }], pass_through: false, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }, Pixels { kind: Adjust(Levels { channel: 0, levels: [0.0, 0.6, 1.0105422, 0.0, 0.7] }), blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 0.0])], opacity: 0.0, fill: 0.0, clipped: false, visible: true, mask: None }] } cc e14b02709a87770da2637c28678c4fa761b5628f402e0c6d60fe66f1aa4d4bfa # shrinks to s = Spec { w: 3, h: 2, layers: [Group { children: [Pixels { kind: Raster, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: false, visible: true, mask: None }], pass_through: false, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }, Group { children: [Pixels { kind: Adjust(Levels { channel: 0, levels: [0.0, 0.6, 0.3, 0.0, 0.7] }), blend: Dissolve, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 0.0])], opacity: 0.8972109, fill: 1.0, clipped: false, visible: true, mask: None }], pass_through: true, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }] } cc 4eeba1f8db987228717b932b82d043d61e82fd897a33ea9e2ce68d6c61d7f491 # shrinks to s = Spec { w: 3, h: 5, layers: [Group { children: [Pixels { kind: Raster, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 0.026626235, clipped: false, visible: true, mask: None }, Pixels { kind: Adjust(Invert), blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 0.0])], opacity: 0.0, fill: 0.0, clipped: false, visible: true, mask: None }], pass_through: true, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }] } +cc 33fb5c528e97f31669a82a4634930230f903c97e58ce59fc88fd733ac097089c # shrinks to s = Spec { w: 6, h: 4, layers: [Group { children: [Pixels { kind: Raster, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 5, y1: 3 }, [0.0, 1.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: false, visible: true, mask: None }, Group { children: [Pixels { kind: Solid, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 1.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: false, visible: true, mask: None }], pass_through: false, blend: VividLight, opacity: 1.0, fill: 1.0, mask: None }, Pixels { kind: Text, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: true, visible: true, mask: None }], pass_through: false, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }] } diff --git a/crates/compose/src/adjust/tone_tests.rs b/crates/compose/src/adjust/tone_tests.rs index fbd91bf14b..7134d5288f 100644 --- a/crates/compose/src/adjust/tone_tests.rs +++ b/crates/compose/src/adjust/tone_tests.rs @@ -198,7 +198,8 @@ fn color_lookup_with_an_overflowing_stored_size_is_the_identity() { // A stored document's size is untrusted: (2²²)³ × 3 overflows, which once wrapped past // the table-length guard and indexed out of bounds. for size in [1 << 22, u32::MAX] { - let a = Adjustment::ColorLookup { name: "x".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size, tetrahedral: false, dither: false }; + let a = + Adjustment::ColorLookup { name: "x".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size, tetrahedral: false, dither: false, source: None }; let mut b = ramp(); apply(&a, &mut b); assert_eq!(b.px, ramp().px, "size {size}"); diff --git a/crates/compose/src/advanced.rs b/crates/compose/src/advanced.rs index 6809c9e865..8ea5a86c6d 100644 --- a/crates/compose/src/advanced.rs +++ b/crates/compose/src/advanced.rs @@ -12,7 +12,9 @@ //! composites onto the result in its own mode, kept inside the base's shape (its alpha with //! masks, times its opacity). //! - **Transparency Shapes Layer off.** The layer's effects and knockout cover the whole layer -//! (its masks still apply) instead of following its transparency. +//! (its masks still apply) instead of following its transparency. In the special eight +//! ([`crate::psblend::fill_is_special`]) the layer's own transparency then blends as Fill does +//! (`blend_layer_into` in `composite.rs`; measured in Photoshop, #5692). //! - **Layer / Vector Mask Hides Effects.** The effects are built from the layer without that //! mask, and the mask is applied to the finished layer and effects. //! @@ -75,7 +77,13 @@ pub fn advanced_active(layer: &Layer, clipped: &[Layer]) -> bool { return false; } let fx = effects::has_effects(layer); - a.knockout != Knockout::None || clips_individually(layer, clipped) || (a.blend_interior && fx) || (!a.transparency_shapes && fx) || hides_effects(layer) + // Transparency Shapes Layer off changes the special eight's blend itself (#5692). + let shapeless_blend = crate::psblend::fill_is_special(layer.blend) && !matches!(layer.content, LayerContent::Adjustment(_)); + a.knockout != Knockout::None + || clips_individually(layer, clipped) + || (a.blend_interior && fx) + || (!a.transparency_shapes && (fx || shapeless_blend)) + || hides_effects(layer) } /// Whether the layer's effects (or knockout) cover the whole layer: Transparency Shapes Layer diff --git a/crates/compose/src/advanced_tests.rs b/crates/compose/src/advanced_tests.rs index a0fdf52cb0..608c6188e2 100644 --- a/crates/compose/src/advanced_tests.rs +++ b/crates/compose/src/advanced_tests.rs @@ -555,3 +555,157 @@ fn deep_knockout_reveals_a_localized_background() { check(&d, 2, 2, [0.0; 4], "an unlocked layer named Background"); } } + +/// Transparency Shapes Layer off in the special eight: the layer's own transparency blends as +/// Fill does (#5692). Measured in Photoshop: Background (90, 110, 130), a layer (200, 150, 80) at +/// alpha 30 % (77) and 70 % (179) in columns 0–5 and 6–11, read from the flattened 8-bit PNG. +#[test] +fn transparency_shapes_off_blends_alpha_as_fill() { + const A30: f32 = 77.0 / 255.0; + const A70: f32 = 179.0 / 255.0; + let c8 = |c: [u8; 3]| [f32::from(c[0]) / 255.0, f32::from(c[1]) / 255.0, f32::from(c[2]) / 255.0]; + let px = |c: [u8; 3], a: f32| { + let c = c8(c); + [c[0], c[1], c[2], a] + }; + let make = |depth: SampleType, mode: BlendMode, opacity: f32, fill: f32, tsl: bool| { + let b = c8([90, 110, 130]); + let mut d = Document::with_background("t", Size::new(12, 8), ColorMode::Rgb, depth, Color::rgb(b[0], b[1], b[2])); + let mut l = solid(&d, "a30", Rect::new(0, 0, 6, 8), px([200, 150, 80], A30)); + l.surface_mut().unwrap().fill_rect(Rect::new(6, 0, 12, 8), &photocraft_raster::from_rgba(&d.pixel_format(), px([200, 150, 80], A70))); + l.blend = mode; + l.opacity = opacity; + l.fill_opacity = fill; + l.advanced.transparency_shapes = tsl; + d.layers.push(l); + d + }; + let (o53, f50) = (135.0 / 255.0, 128.0 / 255.0); + // (mode, opacity, fill, off at 30 %, off at 70 %, on at 30 %, on at 70 %) + type Rgb = [u8; 3]; + let measured: [(BlendMode, f32, f32, Rgb, Rgb, Rgb, Rgb); 7] = [ + (BlendMode::ColorDodge, o53, 1.0, [105, 123, 137], [148, 151, 150], [117, 133, 139], [151, 164, 152]), + (BlendMode::ColorBurn, o53, 1.0, [84, 99, 113], [74, 79, 69], [83, 94, 109], [73, 72, 82]), + (BlendMode::LinearDodge, o53, 1.0, [122, 134, 143], [164, 166, 160], [117, 133, 143], [151, 164, 160]), + (BlendMode::HardMix, o53, 1.0, [98, 111, 119], [133, 117, 75], [117, 133, 109], [151, 164, 82]), + (BlendMode::Difference, o53, 1.0, [58, 86, 117], [69, 54, 100], [93, 99, 117], [97, 84, 100]), + (BlendMode::ColorDodge, 1.0, f50, [102, 121, 136], [125, 139, 146], [108, 124, 137], [131, 142, 147]), + (BlendMode::Difference, 1.0, f50, [59, 87, 118], [19, 57, 102], [66, 87, 118], [34, 57, 102]), + ]; + for depth in DEPTHS { + for (mode, opacity, fill, off30, off70, on30, on70) in measured { + // A 32-bit document's Linear Dodge doesn't clip at 1 (psblend::HDR). + if depth == SampleType::F32 && mode == BlendMode::LinearDodge { + continue; + } + let what = format!("{mode:?} opacity {opacity} fill {fill}"); + let off = make(depth, mode, opacity, fill, false); + check(&off, 2, 4, px(off30, 1.0), &format!("{what}, off, alpha 30 %")); + check(&off, 9, 4, px(off70, 1.0), &format!("{what}, off, alpha 70 %")); + let on = make(depth, mode, opacity, fill, true); + check(&on, 2, 4, px(on30, 1.0), &format!("{what}, on, alpha 30 %")); + check(&on, 9, 4, px(on70, 1.0), &format!("{what}, on, alpha 70 %")); + assert!(advanced_active(&off.layers[1], &[]), "{what}: the GPU leaves it to the CPU"); + } + // Modes outside the special eight look the same either way, and stay on the plain path. + for mode in [BlendMode::Normal, BlendMode::Multiply, BlendMode::Screen, BlendMode::Overlay] { + let (off, on) = (make(depth, mode, o53, f50, false), make(depth, mode, o53, f50, true)); + assert_eq!(render(&off, off.bounds()).px, render(&on, on.bounds()).px, "{mode:?}"); + assert!(!advanced_active(&off.layers[1], &[]), "{mode:?}"); + } + } +} + +/// A mask is not the layer's transparency: Photoshop draws an opaque layer under a 30 % mask the +/// same with the option on or off (measured: Color Dodge, Hard Mix, Difference and the rest of +/// the special eight, every Opacity and Fill tried). Over a half-transparent backdrop and for an +/// isolated group the alpha still blends as Fill (measured: Color Dodge at 53 %). +#[test] +fn transparency_shapes_off_ignores_masks_and_reaches_groups() { + let c8 = |c: [u8; 3]| [f32::from(c[0]) / 255.0, f32::from(c[1]) / 255.0, f32::from(c[2]) / 255.0]; + let px = |c: [u8; 3], a: f32| { + let c = c8(c); + [c[0], c[1], c[2], a] + }; + let o53 = 135.0 / 255.0; + for depth in DEPTHS { + for mode in [BlendMode::ColorDodge, BlendMode::HardMix, BlendMode::Difference] { + let render_with = |tsl: bool| { + let b = c8([90, 110, 130]); + let mut d = Document::with_background("t", Size::new(12, 8), ColorMode::Rgb, depth, Color::rgb(b[0], b[1], b[2])); + let mut l = solid(&d, "l", full(&d), px([200, 150, 80], 1.0)); + l.blend = mode; + l.opacity = o53; + l.mask = Some(LayerMask { surface: photocraft_raster::Surface::with_default(PixelFormat::GRAY8, &[77.0 / 255.0]), ..LayerMask::reveal_all() }); + l.advanced.transparency_shapes = tsl; + d.layers.push(l); + render(&d, d.bounds()).px + }; + let (off, on) = (render_with(false), render_with(true)); + assert!(off.iter().zip(&on).all(|(a, b)| close(*a, *b)), "{mode:?} ({depth:?}): a mask isn't the layer's transparency"); + } + // Color Dodge at 53 % over a backdrop of alpha 50 % (128): (118, 127, 129, 148) at + // alpha 30 %, (162, 151, 131, 175) at 70 %. + let mut d = doc(depth, false); + d.layers.push(solid(&d, "under", full(&d), px([90, 110, 130], 128.0 / 255.0))); + let mut l = solid(&d, "a30", Rect::new(0, 0, 6, 8), px([200, 150, 80], 77.0 / 255.0)); + l.surface_mut().unwrap().fill_rect(Rect::new(6, 0, 12, 8), &photocraft_raster::from_rgba(&d.pixel_format(), px([200, 150, 80], 179.0 / 255.0))); + l.blend = BlendMode::ColorDodge; + l.opacity = o53; + l.advanced.transparency_shapes = false; + d.layers.push(l.clone()); + check(&d, 2, 4, px([118, 127, 129], 148.0 / 255.0), "half-transparent backdrop, alpha 30 %"); + check(&d, 9, 4, px([162, 151, 131], 175.0 / 255.0), "half-transparent backdrop, alpha 70 %"); + // An isolated Color Dodge group at 53 % holding the Normal layer: the group's alpha blends + // as Fill, as a layer's does. + let b = c8([90, 110, 130]); + let mut d = Document::with_background("t", Size::new(12, 8), ColorMode::Rgb, depth, Color::rgb(b[0], b[1], b[2])); + l.blend = BlendMode::Normal; + l.opacity = 1.0; + l.advanced.transparency_shapes = true; + let mut g = Layer::group("g", vec![l]); + g.blend = BlendMode::ColorDodge; + g.opacity = o53; + g.advanced.transparency_shapes = false; + d.layers.push(g); + check(&d, 2, 4, px([105, 123, 137], 1.0), "group, alpha 30 %"); + check(&d, 9, 4, px([148, 151, 150], 1.0), "group, alpha 70 %"); + } +} + +/// A clipped knockout in a Transparency Shapes Layer off base of the special eight: the knockout +/// stops at the base's own content (Blend Clipped Layers as Group), so it gives back the base's +/// colour without touching its alpha, and the base's transparency still blends as Fill under a +/// feathered vector mask. Where the fill-0 knocker covers an earlier clipped layer the composite +/// is the base's alone. +#[test] +fn transparency_shapes_off_keeps_the_base_alpha_under_a_clipped_knockout() { + let square = photocraft_doc::vector::Path::new(vec![photocraft_doc::vector::Subpath::polygon(&[(2.0, 1.0), (10.0, 1.0), (10.0, 7.0), (2.0, 7.0)])]); + for depth in DEPTHS { + for k in [Knockout::Shallow, Knockout::Deep] { + let mut d = Document::with_background("t", Size::new(12, 8), ColorMode::Rgb, depth, Color::rgb(90.0 / 255.0, 110.0 / 255.0, 130.0 / 255.0)); + let mut base = solid(&d, "base", Rect::new(0, 0, 3, 8), [0.78, 0.59, 0.31, 0.3]); + base.surface_mut().unwrap().fill_rect(Rect::new(3, 0, 12, 8), &photocraft_raster::from_rgba(&d.pixel_format(), [0.78, 0.59, 0.31, 0.7])); + base.blend = BlendMode::ColorDodge; + base.opacity = 0.53; + base.advanced.transparency_shapes = false; + base.vector_mask = Some(photocraft_doc::vector::VectorMask { feather: 1.5, ..photocraft_doc::vector::VectorMask::new(square.clone()) }); + d.layers.push(base); + let alone = render(&d, d.bounds()).px; + let mut red = solid(&d, "red", full(&d), [1.0, 0.0, 0.0, 0.5]); + red.clipped = true; + let mut knock = knocker(&d, k); + knock.clipped = true; + d.layers.extend([red, knock]); + let got = render(&d, d.bounds()).px; + let what = format!("{k:?} ({depth:?})"); + for (i, (g, a)) in got.iter().zip(&alone).enumerate() { + if i % 12 < 6 { + assert!(close(*g, *a), "{what}: knocked out at {i}: {g:?}, the base alone {a:?}"); + } + } + assert!(!close(got[6 * 12 + 8], alone[6 * 12 + 8]), "{what}: the red shows outside the knocker"); + check(&d, 4, 3, alone[3 * 12 + 4], &what); + } + } +} diff --git a/crates/compose/src/cmyk/tests.rs b/crates/compose/src/cmyk/tests.rs index 3e28fccd7e..44b265c8dd 100644 --- a/crates/compose/src/cmyk/tests.rs +++ b/crates/compose/src/cmyk/tests.rs @@ -105,8 +105,10 @@ fn over_text(b: Cmyka, s: Cmyka, opacity: f32) -> Cmyka { let dec = |v: f32| 1.0 - photocraft_color::convert::linear_to_srgb(v.max(0.0).powf(gamma)); let mut o = [0.0; 5]; for i in 0..4 { - // Mixing an ink with itself (or with nothing) leaves it exactly as it is. - o[i] = if b[4] <= 0.0 || b[i] == s[i] { s[i] } else { dec((1.0 - sa) * b[4] / a * enc(b[i]) + sa / a * enc(s[i])) }; + // Mixing an ink with itself, with nothing, or over a backdrop it covers fully leaves it + // exactly as it is (the round trip through the gamma space can move it by a rounding + // step, which a mode that jumps at no ink or full ink turns into a whole ink). + o[i] = if b[4] <= 0.0 || sa >= 1.0 || b[i] == s[i] { s[i] } else { dec((1.0 - sa) * b[4] / a * enc(b[i]) + sa / a * enc(s[i])) }; } o[4] = a; o @@ -394,6 +396,64 @@ fn isolated_groups_keep_the_special_fill_rule() { } } +/// White type clipped onto an isolated Vivid Light group over full magenta (#9688): the glyph +/// covers its pixel, so the group shows exactly no ink there, and Vivid Light with no ink (full +/// brightness) gives no ink whatever the backdrop. Mixing the covered pixel through the type +/// gamma would leave a rounding step of ink, which Vivid Light turns into full magenta. +#[test] +fn type_clipped_to_a_vivid_light_group_covers_exactly() { + let mut inner = Layer::new("f", LayerContent::Fill(photocraft_doc::Fill::Solid(cmyk_color([0.0, 1.0, 0.0, 0.0], 1.0)))); + inner.blend = BlendMode::Normal; + let mut vivid = Layer::group("vivid", vec![inner]); + vivid.blend = BlendMode::VividLight; + let mut glyph = text_layer(&[(Rect::new(0, 0, 1, 1), [0.0, 0.0, 0.0, 0.0, 1.0])]); + glyph.clipped = true; + let outer = Layer::group("outer", vec![layer(&[(Rect::new(0, 0, 5, 3), [0.0, 1.0, 0.0, 0.0, 1.0])]), vivid, glyph]); + let mut d = doc(6, 4); + d.layers.push(outer); + assert!(supports(&d)); + let got = render_cmyk(&d, d.bounds()).unwrap_or_else(|| panic!("supported")); + assert_eq!(got.px[0], [0.0, 0.0, 0.0, 0.0, 1.0], "white glyph in Vivid Light"); + // Magenta in Vivid Light onto magenta stays magenta. + assert_eq!(got.px[1], [0.0, 1.0, 0.0, 0.0, 1.0], "beside the glyph"); + assert_matches_reference(&d); +} + +/// The same structure in the neighbouring modes, with type or pixels clipped (fully covering and +/// at a partly covered edge), onto inks that sit at the jumps of the modes that have them. +#[test] +fn layers_clipped_to_isolated_groups_match_the_reference_in_every_mode() { + use BlendMode as B; + for mode in [B::VividLight, B::LinearLight, B::PinLight, B::HardMix, B::ColorDodge, B::ColorBurn, B::Normal, B::Multiply, B::Screen] { + for text in [false, true] { + for (backdrop, ink) in [([0.0, 1.0, 0.0, 0.0], [0.0; 4]), ([1.0, 0.0, 1.0, 1.0], [1.0; 4]), ([0.0, 1.0, 0.0, 0.0], [0.0, 0.3, 1.0, 0.0])] { + let mut inner = Layer::new("f", LayerContent::Fill(photocraft_doc::Fill::Solid(cmyk_color(backdrop, 1.0)))); + inner.blend = BlendMode::Normal; + let mut group = Layer::group("g", vec![inner]); + group.blend = mode; + let [c, m, y, k] = ink; + let rects = [(Rect::new(0, 0, 2, 2), [c, m, y, k, 1.0]), (Rect::new(2, 0, 3, 2), [c, m, y, k, 0.5])]; + let mut clip = if text { text_layer(&rects) } else { layer(&rects) }; + clip.clipped = true; + let [bc, bm, by, bk] = backdrop; + let outer = Layer::group("outer", vec![layer(&[(Rect::new(0, 0, 4, 2), [bc, bm, by, bk, 1.0])]), group, clip]); + let mut d = doc(4, 3); + d.layers.push(outer); + assert!(supports(&d), "{mode:?}"); + assert_matches_reference(&d); + } + } + } + // Onto a pass-through group, clipped layers take the RGB compositor's isolated rendering. + let mut pass = Layer::group("pass", vec![layer(&[(Rect::new(0, 0, 2, 2), [0.0, 1.0, 0.0, 0.0, 1.0])])]); + pass.blend = BlendMode::PassThrough; + let mut clip = text_layer(&[(Rect::new(0, 0, 1, 1), [0.0; 5])]); + clip.clipped = true; + let mut d = doc(2, 2); + d.layers.extend([pass, clip]); + assert!(!supports(&d)); +} + #[test] fn unsupported_documents_fall_back() { let simple = || { diff --git a/crates/compose/src/composite.rs b/crates/compose/src/composite.rs index dccaed8c4b..ab5be3e67c 100644 --- a/crates/compose/src/composite.rs +++ b/crates/compose/src/composite.rs @@ -364,6 +364,10 @@ pub(crate) fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop: }; } } + // A Frame's stroke over the frame's content, at the group's opacity. + if let Some(stroke) = shape_split::frame_stroke(layer, cx.canvas, cx.depth) { + blend_into(backdrop, &surface_to_buffer(&stroke, rect), BlendMode::Normal, opacity); + } // Layers clipped to a pass-through group sit atop the group's // isolated rendering; their effect (isolated result with vs. without // them, each placed over the original backdrop) is added to the @@ -443,7 +447,7 @@ pub(crate) fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop: *p = psblend::composite(BlendMode::Normal, *p, *s, 1.0); p[3] *= mask_k(&mask, i); } - blend_into_fill(backdrop, &content, layer.blend, layer.opacity, layer.fill_opacity, 1.0); + blend_layer_into(backdrop, &content, layer, cx, 1.0); return; } if !matches!(layer.content, LayerContent::Group(_) | LayerContent::Fill(_) | LayerContent::Adjustment(_)) @@ -467,14 +471,17 @@ pub(crate) fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop: } return; } + // The row-at-a-time fast path blends like `blend_into_fill`; Transparency Shapes Layer off + // with one of the special eight needs `blend_layer_into`'s per-pixel mask split. if clipped.is_empty() + && (layer.advanced.transparency_shapes || !psblend::fill_is_special(layer.blend)) && let Some(surf) = pixel_surface(layer) { return composite_pixel_layer(layer, surf, backdrop, cx); } let Some(mut content) = render_content(layer, rect, cx) else { return }; advanced::composite_clipped(clipped, &mut content, cx); - blend_into_fill(backdrop, &content, layer.blend, layer.opacity, layer.fill_opacity, text_gamma(layer)); + blend_layer_into(backdrop, &content, layer, cx, text_gamma(layer)); recycle_buffer(content); } @@ -626,10 +633,34 @@ fn composite_atop_any(layer: &Layer, base: &mut Buffer, cx: &Ctx) { } /// Blend an isolated layer buffer into the backdrop. -fn blend_into(backdrop: &mut Buffer, src: &Buffer, mode: BlendMode, opacity: f32) { +pub(crate) fn blend_into(backdrop: &mut Buffer, src: &Buffer, mode: BlendMode, opacity: f32) { blend_into_g(backdrop, src, mode, opacity, 1.0); } +/// `layer`'s masked content `src` blended into the backdrop with its own mode, Opacity and Fill +/// ([`blend_into_fill`]). With Transparency Shapes Layer off, the special eight +/// ([`psblend::fill_is_special`]) take the layer's own transparency (its content's alpha, not its +/// masks) inside the blend, as Fill: Photoshop composites a pixel of alpha `a` at Fill `f` as +/// an opaque one at Fill `a × f`, covering the backdrop with Opacity × masks only. Other modes +/// look the same either way (#5692). +fn blend_layer_into(backdrop: &mut Buffer, src: &Buffer, layer: &Layer, cx: &Ctx, gamma: f32) { + let (mode, opacity, fill) = (layer.blend, layer.opacity, layer.fill_opacity); + if layer.advanced.transparency_shapes || !psblend::fill_is_special(mode) { + return blend_into_fill(backdrop, src, mode, opacity, fill, gamma); + } + let mv = mask_vals(layer, backdrop.rect, cx); + let fill = fill.clamp(0.0, 1.0); + for (i, (b, s)) in backdrop.px.iter_mut().zip(&src.px).enumerate() { + // `src` carries alpha × masks; split them again. + let m = mv.as_ref().map_or(1.0, |v| v.get(i).copied().unwrap_or(1.0)); + if s[3] <= 0.0 || m <= 0.0 { + continue; + } + let alpha = (s[3] / m).clamp(0.0, 1.0); + *b = psblend::composite_fill(mode, *b, [s[0], s[1], s[2], m], opacity, fill * alpha, gamma); + } +} + /// [`blend_into_g`] for a layer's own `opacity` and `fill`: Fill is part of the blend for /// Photoshop's special eight ([`psblend::composite_fill`]), plain coverage otherwise. pub(crate) fn blend_into_fill(backdrop: &mut Buffer, src: &Buffer, mode: BlendMode, opacity: f32, fill: f32, gamma: f32) { diff --git a/crates/compose/src/content.rs b/crates/compose/src/content.rs index 88ec2751a9..4f9999fe6e 100644 --- a/crates/compose/src/content.rs +++ b/crates/compose/src/content.rs @@ -1,4 +1,4 @@ -use super::composite::{adjustment_quantum, composite_stack}; +use super::composite::{adjustment_quantum, blend_into, composite_stack}; use super::ctx::Ctx; use super::extents::{effect_shape, effect_source_bounds, mask_vals, shapeless_stroke_bounds, transparent_outside}; use super::fx_cache::effect_maps; @@ -94,6 +94,10 @@ pub(crate) fn render_content(layer: &Layer, rect: Rect, cx: &Ctx) -> Option Rect { } match &layer.content { LayerContent::Group(g) if g.artboard.is_some() => g.artboard.as_ref().map_or(Rect::EMPTY, |a| a.rect), - LayerContent::Group(g) => g.children.iter().filter(|c| c.visible).fold(Rect::EMPTY, |acc, c| { + LayerContent::Group(g) => g.children.iter().filter(|c| c.visible).fold(frame_stroke_bounds(layer, canvas), |acc, c| { let b = layer_bounds(c, canvas); if b.is_empty() { acc @@ -47,23 +47,36 @@ pub fn layer_bounds(layer: &Layer, canvas: Rect) -> Rect { } } +/// Where a Photoshop Frame's stroke ([`shape_split::frame_shape`]) draws, within `canvas`; empty +/// for other layers. Taken from the compiled stroke the renderer draws, not the path grown by the +/// stroke width: a mitered corner reaches up to `miter_limit` widths past the path, and a frame +/// has no pixels of its own that would cover it. +fn frame_stroke_bounds(layer: &Layer, canvas: Rect) -> Rect { + shape_split::frame_shape(layer) + .and_then(|sh| photocraft_vector::CompiledShape::new(&sh, photocraft_vector::DEFAULT_TOLERANCE, canvas).bounds()) + .map_or(Rect::EMPTY, |r| r.intersect(&canvas)) +} + /// Where a layer's effects take their shape from: [`layer_bounds`], except that a group's /// children count with their own effects (a child's outside stroke shapes the group's drop /// shadow and must not be cut off at the child's pixels, #3274). pub fn effect_source_bounds(layer: &Layer, canvas: Rect) -> Rect { match &layer.content { - LayerContent::Group(g) if g.artboard.is_none() && !advanced::shapeless(layer) => g.children.iter().filter(|c| c.visible).fold(Rect::EMPTY, |acc, c| { - let m = if effects::has_effects(c) { effects::margin(c) } else { 0 }; - let b = effect_source_bounds(c, canvas); - let b = if b.is_empty() { b } else { b.inflate(m) }; - if b.is_empty() { - acc - } else if acc.is_empty() { - b - } else { - acc.union(&b) - } - }), + // A Frame's stroke is part of the group's content its effects are built from. + LayerContent::Group(g) if g.artboard.is_none() && !advanced::shapeless(layer) => { + g.children.iter().filter(|c| c.visible).fold(frame_stroke_bounds(layer, canvas), |acc, c| { + let m = if effects::has_effects(c) { effects::margin(c) } else { 0 }; + let b = effect_source_bounds(c, canvas); + let b = if b.is_empty() { b } else { b.inflate(m) }; + if b.is_empty() { + acc + } else if acc.is_empty() { + b + } else { + acc.union(&b) + } + }) + } _ => layer_bounds(layer, canvas), } } @@ -343,7 +356,7 @@ pub fn composite_bounds(layer: &Layer, canvas: Rect) -> Option { } let own = match &layer.content { LayerContent::Group(g) if g.artboard.is_none() => { - let mut acc = Rect::EMPTY; + let mut acc = frame_stroke_bounds(layer, canvas); for c in g.children.iter().filter(|c| c.visible) { let b = composite_bounds(c, canvas)?; if !b.is_empty() { diff --git a/crates/compose/src/fx_cache.rs b/crates/compose/src/fx_cache.rs index cd08eaff7c..1907a565f1 100644 --- a/crates/compose/src/fx_cache.rs +++ b/crates/compose/src/fx_cache.rs @@ -44,7 +44,7 @@ pub(crate) fn layer_identity(layer: &Layer, h: &mut std::collections::hash_map:: format!("{:?}", c.blend).hash(h); layer_identity(c, h, pin); } - format!("{:?}", g.artboard).hash(h); + format!("{:?}{:?}", g.artboard, g.frame_stroke).hash(h); } LayerContent::Fill(f) => { format!("{f:?}").hash(h); diff --git a/crates/compose/src/shape_split.rs b/crates/compose/src/shape_split.rs index b2fd28116e..c6e76fb0b2 100644 --- a/crates/compose/src/shape_split.rs +++ b/crates/compose/src/shape_split.rs @@ -8,6 +8,7 @@ use std::sync::{Mutex, OnceLock}; use photocraft_color::{PixelFormat, SampleType}; use photocraft_doc::vector::ShapeLayer; +use photocraft_doc::{Layer, LayerContent}; use photocraft_geom::{Rect, TileCoord}; use photocraft_raster::Surface; @@ -97,6 +98,22 @@ pub fn split(sh: &ShapeLayer, canvas: Rect, depth: SampleType) -> Option<(Surfac Some(parts) } +/// The stroke-only shape a Photoshop Frame's stroke draws: `Group::frame_stroke` along the +/// group's enabled vector mask (the frame outline). `None` for other layers and for a frame +/// without a visible stroke or a vector mask. +pub fn frame_shape(layer: &Layer) -> Option { + let LayerContent::Group(g) = &layer.content else { return None }; + let stroke = g.frame_stroke.as_ref().filter(|s| s.width.is_finite() && s.width > 0.0 && s.opacity > 0.0)?; + let vm = layer.vector_mask.as_ref().filter(|m| m.enabled && !m.path.is_empty())?; + Some(ShapeLayer { path: vm.path.clone(), fill: None, stroke: Some(stroke.clone()), ..ShapeLayer::default() }) +} + +/// A Photoshop Frame's stroke ([`frame_shape`]) rendered over `canvas` in RGBA at `depth`, from +/// the same cache as the shape strokes. It goes above the frame's (masked) content. +pub fn frame_stroke(layer: &Layer, canvas: Rect, depth: SampleType) -> Option { + split(&frame_shape(layer)?, canvas, depth).map(|(_, stroke)| stroke) +} + /// Fits the stroke part to the shape's own pixels: where they reach past the fill, the stroke /// covers what the fill doesn't (`(pixels - fill) / (1 - fill)`), in the pixels' colour where we /// drew no stroke, and elsewhere in the colour that, drawn over the fill, gives back the pixels' diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 76ba8dc72b..7f939c84a5 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -1358,6 +1358,46 @@ fn group_effects_reach_past_children_effects() { assert!(close4(px(&d, 24, 43), [1.0; 4]), "nothing past the shadow: {:?}", px(&d, 24, 43)); } +/// An empty Photoshop Frame with an outside stroke along `points` (#4381). +fn stroked_frame(points: &[(f64, f64)], width: f32) -> Layer { + use photocraft_doc::vector::{Path, ShapeStroke, StrokeAlign, Subpath, VectorMask}; + let mut l = Layer::group("Frame 1", vec![]); + l.vector_mask = Some(VectorMask::new(Path::new(vec![Subpath::polygon(points)]))); + if let LayerContent::Group(g) = &mut l.content { + g.frame_stroke = Some(ShapeStroke { width, align: StrokeAlign::Outside, paint: Fill::Solid(Color::rgb(0.0, 0.0, 1.0)), ..Default::default() }); + } + l +} + +#[test] +fn frame_stroke_bounds_hold_its_mitered_corners() { + // A flat triangle's sharp corners miter far past the path (Photoshop's miter limit is 100): + // more than the stroke width, which is all the path's bounds grow by. + let canvas = Rect::new(0, 0, 64, 64); + let frame = stroked_frame(&[(20.0, 32.0), (44.0, 32.0), (32.0, 29.0)], 2.0); + let drawn = crate::bounds::content_bounds(&shape_split::frame_stroke(&frame, canvas, SampleType::U8).unwrap()); + assert!(drawn.x0 < 20 - 2 && drawn.x1 > 44 + 2, "the miters reach past the width: {drawn:?}"); + for b in [layer_bounds(&frame, canvas), composite_bounds(&frame, canvas).unwrap(), effect_source_bounds(&frame, canvas)] { + assert_eq!(b.union(&drawn), b, "{b:?} holds the stroke {drawn:?}"); + } +} + +#[test] +fn an_empty_frames_effects_follow_its_stroke() { + // No children: the frame's drop shadow is shaped by its stroke alone. + let mut d = doc_white(48, 48); + let mut frame = stroked_frame(&[(14.0, 14.0), (34.0, 14.0), (34.0, 34.0), (14.0, 34.0)], 4.0); + frame.effects.items = vec![Effect::DropShadow(shadow(3.0, 90.0))]; + d.layers.push(frame); + let canvas = d.bounds(); + let drawn = crate::bounds::content_bounds(&shape_split::frame_stroke(&d.layers[1], canvas, SampleType::U8).unwrap()); + let b = effect_source_bounds(&d.layers[1], canvas); + assert!(!drawn.is_empty() && b.union(&drawn) == b, "{b:?} holds the stroke {drawn:?}"); + assert!(close4(px(&d, 24, 36), [0.0, 0.0, 1.0, 1.0]), "stroke below the frame: {:?}", px(&d, 24, 36)); + assert!(close4(px(&d, 24, 39), [0.0, 0.0, 0.0, 1.0]), "shadow below the stroke: {:?}", px(&d, 24, 39)); + assert!(close4(px(&d, 24, 24), [1.0; 4]), "the frame's inside is empty: {:?}", px(&d, 24, 24)); +} + #[test] fn drop_shadow_uses_global_light_and_knockout() { let mut d = fx_doc(vec![Effect::DropShadow(Shadow { use_global_light: true, ..shadow(5.0, 0.0) })]); @@ -3291,23 +3331,28 @@ fn effect_maps_with_nested_rayon_work_do_not_deadlock() { // #276: a tile building a layer's effect maps inside a once-init ran Rayon work; waiting on // it, its worker stole another tile of the same layer, which then blocked on the init held // further up its own stack. Eight threads (two rarely steal across that wait) and 64 tiles make that stealing likely; the cache is - // purged every round so the maps are rebuilt. A watchdog turns a hang into a failure. + // purged every round so the maps are rebuilt. A watchdog turns a hang into a failure: it + // fails when 120 s pass without a render finishing, however long all 101 renders take. + const ROUNDS: usize = 100; let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { let Ok(pool) = rayon::ThreadPoolBuilder::new().num_threads(8).build() else { return }; let d = nested_parallel_fx_doc(); let first = pool.install(|| render_tiled(&d, d.bounds(), 64)); - for _ in 0..100 { + let _ = tx.send(()); + for _ in 0..ROUNDS { purge_effect_cache(); let out = pool.install(|| render_tiled(&d, d.bounds(), 64)); assert!(out.px == first.px, "renders differ between rounds"); + let _ = tx.send(()); } - let _ = tx.send(()); }); - match rx.recv_timeout(std::time::Duration::from_secs(120)) { - Ok(()) => {} - Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!("effect-map rendering deadlocked (#276)"), - Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => panic!("render thread panicked"), + for _ in 0..=ROUNDS { + match rx.recv_timeout(std::time::Duration::from_secs(120)) { + Ok(()) => {} + Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!("effect-map rendering deadlocked (#276)"), + Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => panic!("render thread panicked"), + } } } diff --git a/crates/doc/src/adjust.rs b/crates/doc/src/adjust.rs index e197236a4a..056235ed42 100644 --- a/crates/doc/src/adjust.rs +++ b/crates/doc/src/adjust.rs @@ -17,6 +17,24 @@ pub fn is_identity_curve(points: &[CurvePoint]) -> bool { } } +/// A 3D LUT file format (Photoshop's `LUTFormat`). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum LutFormat { + #[serde(rename = "cube")] + Cube, + #[serde(rename = "3dl")] + ThreeDl, + #[serde(rename = "look")] + Look, +} + +/// A Color Lookup's LUT file as loaded: its format and bytes. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct LutSource { + pub format: LutFormat, + pub bytes: Arc>, +} + /// Entries in a pencil-drawn Curves map (one per 8-bit level, as in PSD `curv` maps). pub const CURVE_MAP_LEN: usize = 256; @@ -260,6 +278,11 @@ pub enum Adjustment { /// Ordered dither of ±½ an 8-bit step to hide banding (Photoshop's "Dither"). #[serde(default)] dither: bool, + /// The LUT file `lut` was read from, embedded unchanged on PSD save as Photoshop does. + /// None for a table made in PhotoCraft (a built-in look); a save also ignores it when + /// it no longer parses to `lut`. + #[serde(default, skip_serializing_if = "Option::is_none")] + source: Option, }, Invert, Posterize { diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 6ec9bc67e3..33589e731e 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -318,6 +318,10 @@ pub struct Group { pub expanded: bool, /// Set when this (top-level) group is an artboard: children are clipped to its rect. pub artboard: Option, + /// A Photoshop Frame's stroke (Properties › Stroke; PSD `vstk` on the frame group): drawn + /// along the group's enabled vector mask (the frame outline), above the framed content and + /// not clipped by the frame. Ignored without a vector mask. + pub frame_stroke: Option, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -703,7 +707,7 @@ impl Layer { Self::new(name, LayerContent::Raster(Surface::new(format))) } pub fn group(name: impl Into, children: Vec) -> Self { - let mut l = Self::new(name, LayerContent::Group(Group { children, expanded: true, artboard: None })); + let mut l = Self::new(name, LayerContent::Group(Group { children, expanded: true, artboard: None, frame_stroke: None })); l.blend = BlendMode::PassThrough; l } @@ -1509,7 +1513,7 @@ mod tests { assert_eq!(LayerContent::Adjustment(Adjustment::Invert).article(), "an"); assert_eq!(LayerContent::Adjustment(Adjustment::Invert).kind_name(), "Adjustment"); assert_eq!(LayerContent::Raster(Surface::new(PixelFormat::RGBA8)).article(), "a"); - assert_eq!(LayerContent::Group(Group { children: Vec::new(), expanded: true, artboard: None }).article(), "a"); + assert_eq!(LayerContent::Group(Group { children: Vec::new(), expanded: true, artboard: None, frame_stroke: None }).article(), "a"); } } diff --git a/crates/engine/src/channel_clip.rs b/crates/engine/src/channel_clip.rs index 47677e21ff..7823fdb18a 100644 --- a/crates/engine/src/channel_clip.rs +++ b/crates/engine/src/channel_clip.rs @@ -36,17 +36,18 @@ pub(crate) fn copy(s: &mut Session) -> Result> { let Some(d) = s.active() else { return Ok(None) }; let doc = &d.doc; let canvas = doc.bounds(); - // The surface the values come from, and which of its samples is the channel. - let (src, k) = match d.channel_view.target { + // The surface the values come from, which of its samples is the channel, and whether it stores + // ink (CMYK, a spot channel) rather than tone. + let (src, k, ink) = match d.channel_view.target { ChannelTarget::Composite | ChannelTarget::LayerMask | ChannelTarget::VectorMask => return Ok(None), ChannelTarget::Color(k) => { let id = d.active_layer.ok_or_else(|| EngineError::Other("no active layer".into()))?; let surf = doc.layer(id).and_then(Layer::surface).ok_or_else(|| EngineError::Other("the active layer has no pixels".into()))?; - (surf, k) + (surf, k, surf.format().mode == ColorMode::Cmyk) } ChannelTarget::Alpha(i) => { let c = doc.channels.get(i).ok_or_else(|| EngineError::Other(format!("no alpha channel {i} (document has {})", doc.channels.len())))?; - (&c.surface, 0) + (&c.surface, 0, c.spot.is_some()) } }; let n = src.format().channels(); @@ -64,7 +65,8 @@ pub(crate) fn copy(s: &mut Session) -> Result> { let len = (values.len() / n).checked_mul(2).ok_or_else(|| EngineError::Other("the channel is too large to copy".into()))?; let mut px = crate::allocation::capacity(len, "copying a channel")?; for (i, p) in values.chunks_exact(n).enumerate() { - px.push(p.get(k).copied().unwrap_or(0.0)); + // A CMYK or spot channel copies as its tone (#5787). + px.push(p.get(k).map_or(0.0, |&v| if ink { 1.0 - v } else { v })); px.push(cover.as_ref().map_or(1.0, |(m, mk)| m.get(i * mk).copied().unwrap_or(0.0))); } let mut surface = Surface::new(fmt); @@ -91,7 +93,7 @@ pub(crate) fn paste(s: &mut Session, p: &Value, k: usize, in_place: bool, limit: let area = src.content_bounds().intersect(&canvas); s.edit(label, |doc, _| { let surf = crate::commands::paint_surface(doc, id, &Value::Null)?; - let n = surf.format().channels(); + let (n, mode) = (surf.format().channels(), surf.format().mode); if k >= n { return Err(EngineError::Other(format!("no colour channel {k} in this layer"))); } @@ -105,7 +107,7 @@ pub(crate) fn paste(s: &mut Session, p: &Value, k: usize, in_place: bool, limit: let mut px = crate::allocation::read_region(surf, area, "pasting into a channel")?; for (i, (dst, s)) in px.chunks_exact_mut(n).zip(&rgba).enumerate() { let w = s[3] * weight.as_ref().map_or(1.0, |(m, mk)| m.get(i * mk).copied().unwrap_or(0.0)); - let v = photocraft_color::convert::rgb_to_gray([s[0], s[1], s[2]]); + let v = crate::channel_cmds::tone(mode, photocraft_color::convert::rgb_to_gray([s[0], s[1], s[2]])); if let Some(c) = dst.get_mut(k) { *c += (v - *c) * w.clamp(0.0, 1.0); } diff --git a/crates/engine/src/channel_clip/tests.rs b/crates/engine/src/channel_clip/tests.rs index 3d89c8f647..a5e687b297 100644 --- a/crates/engine/src/channel_clip/tests.rs +++ b/crates/engine/src/channel_clip/tests.rs @@ -72,6 +72,60 @@ fn alpha_channels_copy_and_paste_into_colour_channels() { assert!(near(&px(&s, 30, 15), &[0.0, 0.0, 0.0, 1.0])); } +#[test] +fn cmyk_channels_copy_and_paste_as_tone() { + // #5787: copying a CMYK channel gives its tone (white where there is no ink), and pasting a + // tone into a CMYK channel writes it back as ink. + for depth in [8, 16] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": W, "height": H, "mode": "cmyk", "depth": depth, "background": "white"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": W / 2, "height": H})).unwrap(); + s.execute("edit.fill", json!({"color": "#00ffff"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + let (left, right) = (px(&s, 5, 5), px(&s, 30, 5)); + s.execute("channel.target", json!({"channel": "cyan"})).unwrap(); + s.execute("edit.copy", json!({})).unwrap(); + let clip = &s.clipboard.as_ref().unwrap().surface; + assert!((clip.pixel(30, 5)[0] - 1.0).abs() < 1e-3, "depth {depth}: no ink copies as white"); + assert!((clip.pixel(5, 5)[0] - (1.0 - left[0])).abs() < 1e-3, "depth {depth}: ink copies dark"); + // Cyan pasted into Black: black gets cyan's ink, and white stays white. + s.execute("channel.target", json!({"channel": "black"})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + assert!(near(&px(&s, 30, 5), &right), "depth {depth}: no ink stays no ink, {:?}", px(&s, 30, 5)); + let p = px(&s, 5, 5); + assert!((p[3] - left[0]).abs() < 2e-3 && near(&p[..3], &left[..3]), "depth {depth}: black = old cyan, {p:?} vs {left:?}"); + } +} + +#[test] +fn spot_channels_copy_and_paste_as_tone() { + // #5787: a spot channel stores ink but copies as its tone, and a tone pasted into it goes back + // to ink: spot -> Black prints the spot's ink in black, Cyan -> spot gives the spot Cyan's ink. + for depth in [8, 16] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": W, "height": H, "mode": "cmyk", "depth": depth, "background": "white"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": W / 2, "height": H})).unwrap(); + s.execute("edit.fill", json!({"color": "#00ffff"})).unwrap(); + let cyan = px(&s, 5, 5)[0]; + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": H})).unwrap(); + let spot = s.execute("channel.newSpot", json!({"color": "#ff00ff", "solidity": 100})).unwrap()["channel"].as_u64().unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.execute("channel.target", json!({"channel": spot})).unwrap(); + s.execute("edit.copy", json!({})).unwrap(); + let clip = &s.clipboard.as_ref().unwrap().surface; + assert!(clip.pixel(5, 5)[0] < 1e-3 && (clip.pixel(30, 5)[0] - 1.0).abs() < 1e-3, "depth {depth}: ink copies dark"); + s.execute("channel.target", json!({"channel": "black"})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + assert!((px(&s, 5, 5)[3] - 1.0).abs() < 2e-3 && px(&s, 30, 5)[3] < 2e-3, "depth {depth}: black = the spot's ink"); + s.execute("channel.target", json!({"channel": "cyan"})).unwrap(); + s.execute("edit.copy", json!({})).unwrap(); + s.execute("channel.target", json!({"channel": spot})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + let ink = |x| s.active().unwrap().doc.channels[spot as usize].surface.sample_channel(x, 5, 0); + assert!(near(&[ink(5), ink(15), ink(30)], &[cyan, cyan, 0.0]), "depth {depth}: spot = Cyan's ink, {:?}", [ink(5), ink(15), ink(30)]); + } +} + #[test] fn copy_an_alpha_channel_without_a_pixel_layer() { let mut s = session(8); diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 5b5f37b4d6..dde9acc16f 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -90,6 +90,7 @@ pub mod gradient_fill_cmds; pub mod group_view_cmds; pub mod history_cmds; pub mod history_menu_cmds; +pub mod history_options_cmds; pub mod history_select_cmds; pub mod history_source_cmds; pub mod image_cmds; @@ -592,7 +593,11 @@ fn build() -> Vec { let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); // Clipping and channel changes reach the layers around it: recomposite everything. let local = p.get("clipped").is_none() && p.get("channels").is_none(); - s.edit(label, |doc, _| { + // The eye: a history state only with History Options › Make Layer Visibility + // Changes Undoable. + let only_visibility = changed.as_slice() == ["visible"]; + let record = !only_visibility || s.visibility_undoable(); + s.edit_with(label, record, |doc, _| { // The Background has no opacity, fill or blend mode: like Photoshop, setting // one that changes its look turns it into a normal layer ("Layer 0") first. let num = |k: &str| p.get(k).and_then(Value::as_f64).is_some_and(|v| v < 1.0); @@ -1273,6 +1278,7 @@ fn build() -> Vec { v.extend(crate::analysis_cmds::specs()); v.extend(crate::notes_cmds::specs()); v.extend(crate::history_cmds::specs()); + v.extend(crate::history_options_cmds::specs()); v.extend(crate::history_menu_cmds::specs()); v.extend(crate::history_source_cmds::specs()); v.extend(crate::history_select_cmds::specs()); diff --git a/crates/engine/src/cmd/adjust_cmds.rs b/crates/engine/src/cmd/adjust_cmds.rs index 830d601b5f..5bbd7df961 100644 --- a/crates/engine/src/cmd/adjust_cmds.rs +++ b/crates/engine/src/cmd/adjust_cmds.rs @@ -10,6 +10,7 @@ //! result through the selection like the other Image › Adjustments commands. use photocraft_algo::tone::{self, HdrToning, MatchColor, ShadowsHighlights}; +use photocraft_doc::adjust::{LutFormat, LutSource}; use photocraft_doc::{Adjustment, Document, Layer, LayerContent, Rect}; use photocraft_raster::{Surface, to_rgba}; use serde_json::{Value, json}; @@ -104,34 +105,38 @@ pub fn selective_from_params(p: &Value, base: Option<&Adjustment>) -> Result) -> Result { const CMD: &str = "colorLookup"; - let (mut name, mut lut, mut size, mut tetrahedral, mut dither) = match base { - Some(Adjustment::ColorLookup { name, lut, size, tetrahedral, dither }) => (name.clone(), lut.clone(), *size, *tetrahedral, *dither), - _ => (String::new(), None, 0, false, false), + let (mut name, mut lut, mut size, mut tetrahedral, mut dither, mut source) = match base { + Some(Adjustment::ColorLookup { name, lut, size, tetrahedral, dither, source }) => { + (name.clone(), lut.clone(), *size, *tetrahedral, *dither, source.clone()) + } + _ => (String::new(), None, 0, false, false, None), }; - let mut loaded: Option<(photocraft_cms::lutfile::LutFile, String)> = None; + // The parsed table, its label and (for a file) the file as loaded, which a PSD save embeds. + let mut loaded: Option<(photocraft_cms::lutfile::LutFile, String, Option)> = None; if let Some(id) = opt_str(CMD, p, "lut")? { if id == "none" || id.is_empty() { name.clear(); lut = None; size = 0; + source = None; } else { let f = photocraft_cms::lutfile::builtin(id).ok_or_else(|| bad(CMD, format!("unknown look `{id}` (built-ins: {})", builtin_ids())))?; let label = f.title.clone(); - loaded = Some((f, label)); + loaded = Some((f, label, None)); } } let file_name = opt_str(CMD, p, "fileName")?; if let Some(text) = opt_str(CMD, p, "data")? { let file_name = file_name.unwrap_or("lut.cube"); let f = photocraft_cms::lutfile::parse(file_name, text.as_bytes()).map_err(|e| bad(CMD, e.0))?; - loaded = Some((f, base_name(file_name))); + loaded = Some((f, base_name(file_name), Some(lut_source(file_name, text.as_bytes().to_vec())))); } if let Some(path) = opt_str(CMD, p, "file")?.filter(|s| !s.is_empty()) { let bytes = read_file(path).ok_or_else(|| bad(CMD, format!("can't read {path}")))?; let f = photocraft_cms::lutfile::parse(path, &bytes).map_err(|e| bad(CMD, format!("{path}: {}", e.0)))?; - loaded = Some((f, base_name(path))); + loaded = Some((f, base_name(path), Some(lut_source(path, bytes)))); } - if let Some((f, label)) = loaded { + if let Some((f, label, src)) = loaded { if !f.domain_is_default() { return Err(bad( CMD, @@ -144,6 +149,7 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result {} @@ -157,7 +163,17 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result) -> LutSource { + let format = match photocraft_cms::lutfile::Format::detect(file_name, &bytes) { + photocraft_cms::lutfile::Format::Cube => LutFormat::Cube, + photocraft_cms::lutfile::Format::ThreeDl => LutFormat::ThreeDl, + photocraft_cms::lutfile::Format::Look => LutFormat::Look, + }; + LutSource { format, bytes: std::sync::Arc::new(bytes) } } fn builtin_ids() -> String { diff --git a/crates/engine/src/cmd/adjust_cmds/tests.rs b/crates/engine/src/cmd/adjust_cmds/tests.rs index 312caedc32..e18e37f1ec 100644 --- a/crates/engine/src/cmd/adjust_cmds/tests.rs +++ b/crates/engine/src/cmd/adjust_cmds/tests.rs @@ -170,7 +170,7 @@ fn color_lookup_builtin_data_and_errors() { // Switching interpolation keeps the table. s.execute("layer.setAdjustment", json!({"layer": id.0, "interpolation": "tetrahedral", "dither": true})).unwrap(); match &doc(&s).layer(id).unwrap().content { - LayerContent::Adjustment(Adjustment::ColorLookup { lut: Some(_), size: 33, tetrahedral: true, dither: true, name }) => { + LayerContent::Adjustment(Adjustment::ColorLookup { lut: Some(_), size: 33, tetrahedral: true, dither: true, name, .. }) => { assert_eq!(name, "Day for Night") } other => panic!("{other:?}"), @@ -220,6 +220,53 @@ LUT_3D_SIZE 2 } /// The kind's parameters, read back from the model (Color Lookup with its table name and size). +#[test] +fn color_lookup_file_round_trips_psd_in_its_own_format() { + use photocraft_doc::adjust::{LutFormat, LutSource}; + // #6544: a .3dl / .look file is kept as loaded and embedded unchanged in a PSD. + let mut threedl = String::from("0 1023\n"); + for r in 0..2 { + for g in 0..2 { + for b in 0..2 { + threedl.push_str(&format!("{} {} {}\n", (1 - r) * 4095, (1 - g) * 4095, (1 - b) * 4095)); + } + } + } + let hex: String = photocraft_cms::lutfile::LutFile::identity(2).data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + let look = format!("2{hex}"); + for (text, file_name, format, psd_format) in + [(threedl.as_str(), "Invert.3dl", LutFormat::ThreeDl, "LUTFormat3DL"), (look.as_str(), "Grade.look", LutFormat::Look, "LUTFormatLOOK")] + { + let mut s = session(8, "rgb"); + let r = s.execute("layer.newAdjustmentLayer.colorLookup", json!({"data": text, "fileName": file_name})).unwrap(); + let id = photocraft_doc::LayerId(r["layer"].as_u64().unwrap()); + let want = LutSource { format, bytes: std::sync::Arc::new(text.as_bytes().to_vec()) }; + let source_of = |s: &Session| match adjustment_of(s, id) { + Adjustment::ColorLookup { source, .. } => source, + other => panic!("{other:?}"), + }; + assert_eq!(source_of(&s), Some(want.clone()), "{file_name}"); + // Settings that don't touch the table keep the file. + s.execute("layer.setAdjustment", json!({"layer": id.0, "interpolation": "tetrahedral", "dither": true})).unwrap(); + assert_eq!(source_of(&s), Some(want.clone()), "{file_name}"); + // Saved as PSD and reopened: same format tag, same bytes, same name. + let psd = photocraft_io::export(doc(&s), "psd", &photocraft_io::ExportOptions::default()).unwrap().bytes; + let back = photocraft_io::import("x.psd", &psd).unwrap().document; + let layer = back.walk().into_iter().map(|(_, _, l)| l).find(|l| matches!(l.content, LayerContent::Adjustment(Adjustment::ColorLookup { .. }))).unwrap(); + let LayerContent::Adjustment(Adjustment::ColorLookup { name, source, .. }) = &layer.content else { panic!("not a Color Lookup") }; + assert_eq!((name.as_str(), source.as_ref()), (file_name, Some(&want)), "{psd_format}"); + // Choosing a built-in look replaces the table, so the file goes too. + s.execute("layer.setAdjustment", json!({"layer": id.0, "lut": "dayForNight"})).unwrap(); + assert_eq!(source_of(&s), None); + } + // "none" drops it as well. + let mut s = session(8, "rgb"); + let r = s.execute("layer.newAdjustmentLayer.colorLookup", json!({"data": threedl, "fileName": "Invert.3dl"})).unwrap(); + let id = photocraft_doc::LayerId(r["layer"].as_u64().unwrap()); + s.execute("layer.setAdjustment", json!({"layer": id.0, "lut": "none"})).unwrap(); + assert!(matches!(adjustment_of(&s, id), Adjustment::ColorLookup { source: None, lut: None, .. })); +} + fn adjustment_of(s: &Session, id: photocraft_doc::LayerId) -> Adjustment { match &doc(s).layer(id).unwrap().content { LayerContent::Adjustment(a) => a.clone(), diff --git a/crates/engine/src/cmd/analysis_cmds.rs b/crates/engine/src/cmd/analysis_cmds.rs index 8932295d52..defaa834d4 100644 --- a/crates/engine/src/cmd/analysis_cmds.rs +++ b/crates/engine/src/cmd/analysis_cmds.rs @@ -197,7 +197,8 @@ pub(crate) fn compound(s: &mut Session, label: &str, f: impl FnOnce(&mut Sess Ok(v) => { // As in `Session::edit`: undo targets what was selected just before the step (#1356). st.history.set_current_layers(prior); - st.history.record(label, before, st.layer_target()); + st.history.record(label, before.clone(), st.layer_target()); + st.history.add_current_visibility(&crate::visibility_changes(&before, &st.doc)); st.trim_history(); st.coalesce = None; Ok(v) diff --git a/crates/engine/src/cmd/atn_cmds.rs b/crates/engine/src/cmd/atn_cmds.rs index 32e5405d1a..7c717701bf 100644 --- a/crates/engine/src/cmd/atn_cmds.rs +++ b/crates/engine/src/cmd/atn_cmds.rs @@ -38,7 +38,7 @@ mod steps_make; mod tables; #[cfg(test)] -use adjustments::selective_from; +use adjustments::{levels_from, selective_from}; pub use convert::{Imported, Skipped, Unsupported, export, function_key_name, import, parse_function_key}; #[cfg(test)] use convert::{MAX_NAME_CHARS, export_item, normalise}; @@ -56,7 +56,7 @@ use files::{load, save}; #[cfg(test)] use steps_doc::crop_from; #[cfg(test)] -use steps_image::transform_to; +use steps_image::{apply_image_from, transform_to}; #[cfg(test)] use steps_layer::quick_mask_ref; #[cfg(test)] diff --git a/crates/engine/src/cmd/atn_cmds/steps_image.rs b/crates/engine/src/cmd/atn_cmds/steps_image.rs index bb8e1c82fd..8ef108f70e 100644 --- a/crates/engine/src/cmd/atn_cmds/steps_image.rs +++ b/crates/engine/src/cmd/atn_cmds/steps_image.rs @@ -4,7 +4,7 @@ use super::descriptors::{ }; use super::events::pixel_target; use super::fields::{F, Kind, Mapped, fields_from, fields_to, step}; -use super::tables::{CHANNELS, INTERPOLATION, RADIAL_METHOD, RADIAL_QUALITY, lookup, lookup_back}; +use super::tables::{CHANNELS, INTERPOLATION, RADIAL_METHOD, RADIAL_QUALITY, channel_of, lookup, lookup_back}; use super::*; /// `transform`: Photoshop records a Free Transform relative to the frame (offset of its centre, @@ -143,7 +143,7 @@ pub(super) fn apply_image_from(d: &Descriptor) -> Mapped { match item { ReferenceItem::Enumerated { class, value, .. } if class.class_id.is("Chnl") => { let v = trimmed(value); - let Some(channel) = lookup(SOURCE_CHANNELS, &v).or_else(|| lookup(CHANNELS, &v)) else { return unsupported }; + let Some(channel) = lookup(SOURCE_CHANNELS, &v).or_else(|| channel_of(&v)) else { return unsupported }; source.insert("channel".into(), channel.into()); } // A named (alpha) channel. diff --git a/crates/engine/src/cmd/atn_cmds/tables.rs b/crates/engine/src/cmd/atn_cmds/tables.rs index 13b4bb9e8d..0432f26ac3 100644 --- a/crates/engine/src/cmd/atn_cmds/tables.rs +++ b/crates/engine/src/cmd/atn_cmds/tables.rs @@ -78,17 +78,24 @@ pub(super) const CHANNELS: &[(&str, &str)] = &[ ("Mgnt", "magenta"), ("Yllw", "yellow"), ("Blck", "black"), - ("Lmnc", "lightness"), + // Lab Lightness: Photoshop rejects `Lmnc` ("luminance") on a Lab document (#9384). + ("Lght", "lightness"), ("A", "a"), ("B", "b"), ]; +/// The channel key of a `Chnl` enumeration. `Lmnc` is what PhotoCraft wrote for Lightness before +/// #9384; reading it keeps those actions loading. +pub(super) fn channel_of(code: &str) -> Option<&'static str> { + lookup(CHANNELS, code).or_else(|| (code.trim_end() == "Lmnc").then_some("lightness")) +} + /// The channel an adjustment entry's `Chnl` reference names: `None` = the composite. pub(super) fn channel_key(d: &Descriptor) -> Option> { let r = get_ref(d, "Chnl")?; Some(match target(r) { Target::Ordinal(o) if o == "Trgt" || o == "Cmps" => None, - Target::Ordinal(o) => Some(lookup(CHANNELS, &o)?), + Target::Ordinal(o) => Some(channel_of(&o)?), _ => return None, }) } diff --git a/crates/engine/src/cmd/atn_cmds/tests.rs b/crates/engine/src/cmd/atn_cmds/tests.rs index afd3895393..08286b5329 100644 --- a/crates/engine/src/cmd/atn_cmds/tests.rs +++ b/crates/engine/src/cmd/atn_cmds/tests.rs @@ -2644,6 +2644,44 @@ fn levels_and_curves_name_the_composite_channel_as_photoshop_records_it() { assert_eq!(channels(&curves), vec![composite, red]); } +/// A Lab document's Lightness channel is `Chnl.Lght` (#9384): Photoshop plays a Levels or Apply +/// Image step naming it and rejects `Chnl.Lmnc` ("luminance"), which PhotoCraft wrote before. +/// Steps PhotoCraft saved with `Lmnc` still load. +#[test] +fn lab_lightness_channel_is_written_and_read_as_photoshop_names_it() { + let exported = |id: &str, p: Value| export_item(id, &p, "S").and_then(|i| i.descriptor).unwrap_or_else(|| panic!("{id} does not export")); + let entry_codes = |d: &Descriptor| -> Vec { + get_list(d, "Adjs") + .unwrap_or_default() + .iter() + .filter_map(|e| match e { + D::Descriptor(e) => match get_ref(e, "Chnl")?.first()? { + ReferenceItem::Enumerated { value, .. } => Some(trimmed(value)), + _ => None, + }, + _ => None, + }) + .collect() + }; + let levels = exported("image.adjustments.levels", json!({"lightness": {"gamma": 1.5}, "a": {"inBlack": 3}})); + assert_eq!(entry_codes(&levels), ["Cmps", "Lght", "A"]); + let curves = exported("image.adjustments.curves", json!({"lightness": [[0, 10], [255, 245]]})); + assert_eq!(entry_codes(&curves), ["Lght"]); + let apply = exported("image.applyImage", json!({"sourceLayer": "merged", "sourceChannel": "Lightness"})); + let source = get_ref(get_obj(&apply, "With").unwrap(), "T ").unwrap(); + assert!(matches!(&source[0], ReferenceItem::Enumerated { value, .. } if trimmed(value) == "Lght"), "{source:?}"); + + // What Photoshop records, and what PhotoCraft wrote before, both read as Lightness. + let chnl = |c: &str| D::Reference(vec![ReferenceItem::Enumerated { class: class("Chnl"), type_id: code("Chnl"), value: code(c) }]); + for c in ["Lght", "Lmnc"] { + let lvls = desc("Lvls").with("Adjs", D::List(vec![D::Descriptor(desc("LvlA").with("Chnl", chnl(c)).with("Gmm ", D::Double(1.5)))])); + assert_eq!(levels_from(&lvls), Some(json!({"lightness": {"gamma": 1.5}})), "{c}"); + let with = desc("Clcl").with("T ", chnl(c)); + let Mapped::Steps(steps) = apply_image_from(&desc("null").with("With", D::Descriptor(with))) else { panic!("{c}: Apply Image does not import") }; + assert_eq!(steps[0].1["source"]["channel"], "lightness", "{c}"); + } +} + /// Photoshop sets one colour per step: a step that sets both doesn't export rather than drop one. #[test] fn set_colors_exports_only_a_single_colour() { diff --git a/crates/engine/src/cmd/channel_cmds.rs b/crates/engine/src/cmd/channel_cmds.rs index 6beb64542e..a676c05d6f 100644 --- a/crates/engine/src/cmd/channel_cmds.rs +++ b/crates/engine/src/cmd/channel_cmds.rs @@ -113,6 +113,7 @@ mod view; use calc::{apply_image, calculations}; use manage::{delete_channel, duplicate_channel, move_channel, new_channel, new_spot, options, rename_channel, set_visible}; use planes::edit_mode; +pub(crate) use planes::tone; pub use planes::{ GraySurface, IsolatedCurveChannel, channel_format, color_count, filter_mask_curve_channel, isolated_curve_channel, isolated_curve_channel_for, next_alpha_name, @@ -123,8 +124,9 @@ use selection::{load_selection, quick_mask, save_selection}; use spot::{merge, merge_spot, mergeable, split}; use targeting::{SLOT_KEY, target_cmd, target_slot}; pub(crate) use targeting::{ - Target, channel_surface_for_filter, clear_channel, clear_color_channel, color_restriction, edits_channel, filter_mask_layer, inject_target, - is_channel_target, mask_target_enabled, read_target_surface, restrict_to_color, routed, target_of, target_surface, targeted_channel, targeted_color, + Target, channel_surface_for_filter, clear_channel, clear_color_channel, color_restriction, edits_channel, filter_mask_layer, inject_target, invert_region, + is_channel_target, is_spot_target, mask_target_enabled, read_target_surface, restrict_to_color, routed, target_of, target_surface, targeted_channel, + targeted_color, }; pub use targeting::{follows_filter_mask, follows_target}; pub(crate) use view::fix_view; diff --git a/crates/engine/src/cmd/channel_cmds/calc.rs b/crates/engine/src/cmd/channel_cmds/calc.rs index 57e71cb360..3f45b40c53 100644 --- a/crates/engine/src/cmd/channel_cmds/calc.rs +++ b/crates/engine/src/cmd/channel_cmds/calc.rs @@ -1,7 +1,7 @@ use super::manage::gray_document; -use super::planes::{channel_format, next_alpha_name, plane_surface, read_plane}; -use super::refs::{Blending, bad, mask_plane, source, source_spec}; -use super::targeting::{Target, is_channel_target, target_of, target_surface}; +use super::planes::{channel_format, edit_mode, next_alpha_name, plane_surface, read_plane, tone}; +use super::refs::{Blending, bad, doc_index, mask_plane, source, source_spec}; +use super::targeting::{Target, is_channel_target, is_spot_target, target_of, target_surface}; use super::*; // --------------------------------------------------------------------------------------------- @@ -13,7 +13,11 @@ pub(super) fn apply_image(s: &mut Session, p: &Value) -> Result { let opacity = p.get("opacity").and_then(Value::as_f64).unwrap_or(100.0).clamp(0.0, 100.0) as f32 / 100.0; let preserve = p.get("preserveTransparency").and_then(Value::as_bool).unwrap_or(false); let src_spec = source_spec(p, "source", "source").unwrap_or_else(|| json!({})); - let src = source(s, &src_spec, cmd, false)?; + // A composite's planes are in its own document's mode, which may not be the target's. + let src_mode = s.documents().get(doc_index(s, &src_spec, "document")?).map(|d| edit_mode(&d.doc)).ok_or(EngineError::NoDocument)?; + // A CMYK composite reaches a channel or a layer mask as its luminosity, as Calculations reads + // it: its planes are ink, and an average of their tones would leave Black alone light gray. + let src = source(s, &src_spec, cmd, src_mode == ColorMode::Cmyk && (is_channel_target(p) || target_of(p) == Target::Mask))?; let mask = mask_plane(s, p, cmd)?; let layer = s.active().and_then(|d| d.active_layer); s.edit("Apply Image", |doc, _| { @@ -22,12 +26,22 @@ pub(super) fn apply_image(s: &mut Session, p: &Value) -> Result { let weight = |i: usize| opacity * mask.as_ref().map_or(1.0, |m| m[i]) * selection.as_ref().map_or(1.0, |m| m[i]); let mask_target = target_of(p) == Target::Mask; if is_channel_target(p) || mask_target { - // A grayscale target gets the luminosity of a colour source (one plane). + // A grayscale target gets one plane: a CMYK composite's luminosity (above), the average of + // another composite's planes. let srcp = if src.len() == 1 { src[0].clone() } else { (0..src[0].len()).map(|i| src.iter().map(|pl| pl[i]).sum::() / src.len() as f32).collect() }; + let spot = is_spot_target(doc, p); + let ink = |v: f32| if spot { 1.0 - v } else { v }; let (surf, _) = target_surface(doc, layer, p)?; let base = read_plane(surf, area); - let out: Vec = base.iter().enumerate().map(|(i, &b)| b + (blending.apply(b, srcp[i]) - b) * weight(i)).collect(); + let out: Vec = base + .iter() + .enumerate() + .map(|(i, &b)| { + let b = ink(b); + ink(b + (blending.apply(b, srcp[i]) - b) * weight(i)) + }) + .collect(); if mask_target { // A layer mask keeps its default (reveal or hide) outside the canvas. if surf.channels() != 1 { @@ -64,9 +78,15 @@ pub(super) fn apply_image(s: &mut Session, p: &Value) -> Result { // source-over, so applying to an empty layer copies the source in any mode. let out_a = if preserve { base_a } else { w + base_a * (1.0 - w) }; for (c, b) in px[..colors].iter_mut().enumerate() { - let top = src.get(c).unwrap_or(&src[0])[i]; - let blended = top + (blending.apply(*b, top) - top) * base_a; - *b = if preserve { *b + (blended - *b) * w } else { (w * blended + base_a * (1.0 - w) * *b) / out_a }; + // Blended as tone, as Photoshop does: a CMYK layer's ink turns into tone and back + // (Multiply darkens, so it adds ink). One plane (a channel, a mask) is a tone already. + // A CMYK composite onto another mode darkens each tone by its Black, so Black + // alone stays black. + let k = if src_mode == ColorMode::Cmyk && sf.mode != ColorMode::Cmyk { src.get(3).map_or(1.0, |pl| tone(src_mode, pl[i])) } else { 1.0 }; + let top = if src.len() > 1 { tone(src_mode, src.get(c).unwrap_or(&src[0])[i]) * k } else { src[0][i] }; + let base = tone(sf.mode, *b); + let blended = top + (blending.apply(base, top) - top) * base_a; + *b = tone(sf.mode, if preserve { base + (blended - base) * w } else { (w * blended + base_a * (1.0 - w) * base) / out_a }); } if sf.alpha { px[colors] = out_a; diff --git a/crates/engine/src/cmd/channel_cmds/planes.rs b/crates/engine/src/cmd/channel_cmds/planes.rs index f434a2fa7b..923b90da54 100644 --- a/crates/engine/src/cmd/channel_cmds/planes.rs +++ b/crates/engine/src/cmd/channel_cmds/planes.rs @@ -133,6 +133,13 @@ pub(super) fn materialize(s: &mut Surface, r: Rect) { s.write_region(r, &v); } +/// A colour channel sample as the Channels panel shows it: CMYK stores ink (1 = full ink) but a +/// CMYK channel reads as its tone (white = no ink), as in Photoshop. Its own inverse, so it also +/// turns a tone back into ink. Other modes are unchanged. +pub(crate) fn tone(mode: ColorMode, v: f32) -> f32 { + if mode == ColorMode::Cmyk { 1.0 - v } else { v } +} + pub(super) fn luminosity(rgba: [f32; 4]) -> f32 { 0.299 * rgba[0] + 0.587 * rgba[1] + 0.114 * rgba[2] } diff --git a/crates/engine/src/cmd/channel_cmds/refs.rs b/crates/engine/src/cmd/channel_cmds/refs.rs index 876fa0b49b..d18630e95d 100644 --- a/crates/engine/src/cmd/channel_cmds/refs.rs +++ b/crates/engine/src/cmd/channel_cmds/refs.rs @@ -1,4 +1,4 @@ -use super::planes::{color_count, edit_mode, luminosity, read_plane}; +use super::planes::{color_count, edit_mode, luminosity, read_plane, tone}; use super::view::{color_names, composite_name}; use super::*; @@ -127,11 +127,19 @@ fn ref_planes(doc: &Document, layer: Option, active: Option, r let (px, n) = native_pixels(doc, layer)?; let planes: Vec> = (0..colors).map(|c| px.chunks_exact(n).map(|p| p[c]).collect()).collect(); match r { - ChanRef::Color(k) => vec![planes.get(k).cloned().ok_or_else(|| EngineError::Other(format!("no colour channel {k}")))?], + ChanRef::Color(k) => { + let plane = planes.get(k).ok_or_else(|| EngineError::Other(format!("no colour channel {k}")))?; + vec![plane.iter().map(|&v| tone(edit_mode(doc), v)).collect()] + } _ => planes, } } - ChanRef::Alpha(i) => vec![read_plane(&doc.channels.get(i).ok_or_else(|| EngineError::Other(format!("no alpha channel {i}")))?.surface, area)], + ChanRef::Alpha(i) => { + let ch = doc.channels.get(i).ok_or_else(|| EngineError::Other(format!("no alpha channel {i}")))?; + let plane = read_plane(&ch.surface, area); + // A spot channel stores ink (1 = solid) but reads as its tone, as in Photoshop. + vec![if ch.spot.is_some() { plane.into_iter().map(|v| 1.0 - v).collect() } else { plane }] + } ChanRef::QuickMask => vec![read_plane(&doc.quick_mask.as_ref().ok_or_else(|| EngineError::Other("not in Quick Mask mode".into()))?.surface, area)], ChanRef::Selection => vec![sel::mask_from_surface(doc.selection.as_ref(), area)], ChanRef::Transparency => { diff --git a/crates/engine/src/cmd/channel_cmds/spot.rs b/crates/engine/src/cmd/channel_cmds/spot.rs index a3fefee98a..b1395a3199 100644 --- a/crates/engine/src/cmd/channel_cmds/spot.rs +++ b/crates/engine/src/cmd/channel_cmds/spot.rs @@ -1,5 +1,5 @@ use super::manage::{fix_view_now, gray_document}; -use super::planes::{channel_format, edit_mode, plane_surface, read_plane}; +use super::planes::{channel_format, edit_mode, plane_surface, read_plane, tone}; use super::refs::{ChanRef, alpha_index, bad, gray_plane, native_pixels}; use super::view::{ChannelTarget, ChannelView, color_names}; use super::*; @@ -66,7 +66,7 @@ pub(super) fn split(s: &mut Session, p: &Value) -> Result { let short = |name: &str| name.chars().next().map(|c| c.to_ascii_uppercase().to_string()).unwrap_or_default(); let mut docs: Vec = Vec::new(); for (c, name) in names.iter().enumerate() { - let plane: Vec = px.chunks_exact(n).map(|p| p[c]).collect(); + let plane: Vec = px.chunks_exact(n).map(|p| tone(mode, p[c])).collect(); docs.push(gray_document(&format!("{}_{}", doc.name, short(name)), &doc, &plane)); } for ch in &doc.channels { @@ -133,7 +133,7 @@ pub(super) fn merge(s: &mut Session, p: &Value) -> Result { let mut data = Vec::with_capacity(npx * fmt.channels()); for i in 0..npx { for pl in &planes[..cc] { - data.push(pl[i].clamp(0.0, 1.0)); + data.push(tone(mode, pl[i]).clamp(0.0, 1.0)); } data.push(1.0); } diff --git a/crates/engine/src/cmd/channel_cmds/targeting.rs b/crates/engine/src/cmd/channel_cmds/targeting.rs index ce131cd43a..d4ca22c5e5 100644 --- a/crates/engine/src/cmd/channel_cmds/targeting.rs +++ b/crates/engine/src/cmd/channel_cmds/targeting.rs @@ -37,6 +37,19 @@ pub(crate) fn is_channel_target(p: &Value) -> bool { matches!(target_of(p), Target::Alpha(_) | Target::QuickMask) } +/// The target is a spot channel: it stores ink but takes tone, as it reads. +pub(crate) fn is_spot_target(doc: &Document, p: &Value) -> bool { + matches!(target_of(p), Target::Alpha(i) if doc.channels.get(i).is_some_and(|c| c.spot.is_some())) +} + +/// Inverts `r` of a channel surface (ink to tone and back). +pub(crate) fn invert_region(s: &mut Surface, r: Rect) -> Result<()> { + let mut v = crate::allocation::read_region(s, r, "inverting a channel")?; + v.iter_mut().for_each(|x| *x = 1.0 - *x); + s.try_write_region(r, &v)?; + Ok(()) +} + /// The surface a pixel command writes to and whether the layer's transparency is locked. pub(crate) fn target_surface<'a>(doc: &'a mut Document, layer: Option, p: &Value) -> Result<(&'a mut Surface, bool)> { match target_of(p) { diff --git a/crates/engine/src/cmd/channel_cmds/tests.rs b/crates/engine/src/cmd/channel_cmds/tests.rs index 2ae1bd94b0..079b2e4c86 100644 --- a/crates/engine/src/cmd/channel_cmds/tests.rs +++ b/crates/engine/src/cmd/channel_cmds/tests.rs @@ -120,6 +120,38 @@ fn load_transparency_mask_and_composite() { assert_eq!(sel_at(&s, 10, 5), 1.0); } +/// A white CMYK document (no ink anywhere) whose left half carries some cyan ink. +fn cmyk_session(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 20, "mode": "cmyk", "depth": depth, "background": "white"})).unwrap(); + rect(&mut s, 0, 0, 20, 20, "replace"); + s.execute("edit.fill", json!({"color": "#00ffff"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s +} + +#[test] +fn cmyk_channels_duplicate_and_load_as_tone_not_ink() { + // #5787: a CMYK channel is its tone, as the Channels panel draws it: white where there is no ink. + // (The merged image goes through RGB, so inked pixels may drift a few levels.) + for depth in [8, 16] { + let mut s = cmyk_session(depth); + let ink = px(&s, 5, 5)[0]; + assert!(ink > 0.5 && px(&s, 30, 5)[0] == 0.0, "depth {depth}: cyan ink on the left only, {ink}"); + s.execute("channel.duplicate", json!({"channel": "cyan"})).unwrap(); + assert_eq!(doc(&s).channels[0].name, "Cyan copy"); + assert!((chan(&s, 0, 30, 5) - 1.0).abs() < 1e-3, "depth {depth}: no ink duplicates as white"); + assert!((chan(&s, 0, 5, 5) - (1.0 - ink)).abs() < 0.02, "depth {depth}: ink duplicates dark"); + s.execute("select.loadSelection", json!({"channel": "cyan"})).unwrap(); + assert!((sel_at(&s, 30, 5) - 1.0).abs() < 1e-3, "depth {depth}: no ink is selected"); + assert!((sel_at(&s, 5, 5) - (1.0 - ink)).abs() < 0.02, "depth {depth}"); + // Calculations read the same tone. + let r = s.execute("image.calculations", json!({"source1": {"channel": "cyan"}, "source2": {"channel": "cyan"}, "blending": "normal"})).unwrap(); + let i = r["channel"].as_u64().unwrap() as usize; + assert!((chan(&s, i, 30, 5) - 1.0).abs() < 1e-3, "depth {depth}"); + } +} + #[test] fn channel_management_and_view_state() { let mut s = session(); @@ -627,6 +659,166 @@ fn apply_image_into_alpha_channel() { assert!(chan(&s, 0, 3, 3) < 0.01); } +#[test] +fn cmyk_apply_image_of_a_colour_channel_keeps_ink_and_tone_apart() { + // #5787: the Cyan channel is read as tone and written back as ink, so applying it onto its own + // CMYK layer leaves cyan as it was, and into an alpha channel it lands as tone. (The merged + // image goes through RGB, so a few levels of drift are allowed.) + for depth in [8, 16] { + let mut s = cmyk_session(depth); + let before = (px(&s, 5, 5), px(&s, 30, 5)); + s.execute("image.applyImage", json!({"source": {"channel": "cyan"}, "blending": "normal"})).unwrap(); + let after = px(&s, 5, 5); + assert!((after[0] - before.0[0]).abs() < 0.02, "depth {depth}: cyan unchanged, {after:?} vs {:?}", before.0); + assert!(px(&s, 30, 5).iter().zip(&before.1).all(|(a, b)| (a - b).abs() < 1e-3), "depth {depth}: no ink stays no ink"); + s.undo(); + s.execute("image.applyImage", json!({"blending": "normal"})).unwrap(); + assert!(px(&s, 5, 5).iter().zip(&before.0).all(|(a, b)| (a - b).abs() < 0.02), "depth {depth}: CMYK onto itself is a no-op"); + s.execute("channel.new", json!({"fill": "black"})).unwrap(); + s.execute("image.applyImage", json!({"source": {"channel": "cyan"}, "blending": "normal"})).unwrap(); + assert!((chan(&s, 0, 30, 5) - 1.0).abs() < 1e-3, "depth {depth}: tone into an alpha channel"); + } +} + +#[test] +fn cmyk_apply_image_blends_as_tone() { + // Photoshop blends a CMYK layer as tone: Multiply (the default) with a white source changes + // nothing, and Multiply with Cyan multiplies every channel's tone by Cyan's, so it adds ink. + // A spot channel reads as tone too: its solid ink applies as full ink and isn't selected. + for depth in [8, 16] { + let mut s = cmyk_session(depth); + let (left, right) = (px(&s, 5, 5), px(&s, 30, 5)); + let near = |a: &[f32], b: &[f32]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.01); + s.execute("channel.new", json!({"fill": "white"})).unwrap(); + s.execute("channel.target", json!({"channel": "composite"})).unwrap(); + s.execute("image.applyImage", json!({"source": {"channel": 0}})).unwrap(); + assert!(near(&px(&s, 5, 5), &left), "depth {depth}: white multiplies to no change, {:?} vs {left:?}", px(&s, 5, 5)); + s.undo(); + s.execute("image.applyImage", json!({"source": {"channel": "cyan"}, "blending": "multiply"})).unwrap(); + let want: Vec = left[..4].iter().map(|&ink| 1.0 - (1.0 - ink) * (1.0 - left[0])).chain([1.0]).collect(); + assert!(near(&px(&s, 5, 5), &want), "depth {depth}: {:?} vs {want:?}", px(&s, 5, 5)); + assert!(near(&px(&s, 30, 5), &right), "depth {depth}: no ink stays no ink"); + s.undo(); + rect(&mut s, 0, 0, 20, 20, "replace"); + let r = s.execute("channel.newSpot", json!({"color": "#ff00ff", "solidity": 100})).unwrap(); + let spot = r["channel"].as_u64().unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.execute("channel.target", json!({"channel": "composite"})).unwrap(); + s.execute("image.applyImage", json!({"source": {"channel": spot}, "blending": "normal"})).unwrap(); + assert!(near(&px(&s, 5, 5), &[1.0; 5]) && near(&px(&s, 30, 5), &right), "depth {depth}: spot ink applies as ink, {:?}", px(&s, 5, 5)); + s.execute("select.loadSelection", json!({"channel": spot})).unwrap(); + assert!(sel_at(&s, 5, 5) < 1e-3 && (sel_at(&s, 30, 5) - 1.0).abs() < 1e-3, "depth {depth}: the spot's unprinted area loads"); + } +} + +#[test] +fn apply_image_onto_a_spot_channel_blends_as_tone() { + let near = |a: &[f32], b: &[f32]| a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.01); + // #5787: a spot channel stores ink but reads and takes tone, so the spot applied onto itself + // changes nothing (Normal or Multiply), and Cyan applied onto it gives it Cyan's ink. + for depth in [8, 16] { + let mut s = cmyk_session(depth); + let cyan = px(&s, 5, 5)[0]; + rect(&mut s, 0, 0, 10, 20, "replace"); + let spot = s.execute("channel.newSpot", json!({"color": "#ff00ff", "solidity": 100})).unwrap()["channel"].as_u64().unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + let ink = |s: &Session| [5, 15, 30].map(|x| chan(s, spot as usize, x, 5)); + let before = ink(&s); + assert!(near(&before, &[1.0, 0.0, 0.0]), "depth {depth}: {before:?}"); + for blending in ["normal", "multiply"] { + s.execute("image.applyImage", json!({"source": {"channel": spot}, "blending": blending, "target": {"channel": spot}})).unwrap(); + assert!(near(&ink(&s), &before), "depth {depth}: {blending} onto itself, {:?}", ink(&s)); + } + s.execute("image.applyImage", json!({"source": {"channel": "cyan"}, "blending": "normal", "target": {"channel": spot}})).unwrap(); + assert!(near(&ink(&s), &[cyan, cyan, 0.0]), "depth {depth}: Cyan's ink, {:?}", ink(&s)); + } +} + +#[test] +fn apply_image_from_an_rgb_document_onto_cmyk_reads_it_in_its_own_mode() { + let near = |a: &[f32], b: &[f32]| a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.01); + // #5787: an RGB composite is tone in RGB terms: white multiplied (the default) onto a CMYK + // layer changes nothing. + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 20, "background": "white"})).unwrap(); + s.execute("file.new", json!({"width": 40, "height": 20, "mode": "cmyk", "background": "white"})).unwrap(); + rect(&mut s, 0, 0, 20, 20, "replace"); + s.execute("edit.fill", json!({"color": "#00ffff"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + let before = (px(&s, 5, 5), px(&s, 30, 5)); + s.execute("image.applyImage", json!({"source": {"document": 0}})).unwrap(); + assert!(near(&px(&s, 5, 5), &before.0) && near(&px(&s, 30, 5), &before.1), "{:?} vs {before:?}", (px(&s, 5, 5), px(&s, 30, 5))); +} + +#[test] +fn apply_image_of_a_cmyk_composite_onto_a_spot_channel_blends_as_tone() { + // #5787: the CMYK composite stores ink, so its white has none: applied in Normal onto an empty + // spot channel, the spot stays empty on the white and takes ink on the Cyan. + for depth in [8, 16] { + let mut s = cmyk_session(depth); + let spot = s.execute("channel.newSpot", json!({"color": "#ff00ff", "solidity": 100})).unwrap()["channel"].as_u64().unwrap() as usize; + s.execute("image.applyImage", json!({"blending": "normal", "target": {"channel": spot}})).unwrap(); + let (cyan, white) = (chan(&s, spot, 5, 5), chan(&s, spot, 30, 5)); + assert!(white < 0.01 && cyan > 0.1 && cyan < 0.9, "depth {depth}: cyan {cyan}, white {white}"); + } +} + +#[test] +fn apply_image_of_a_cmyk_composite_onto_rgb_keeps_black() { + // #5787: Black alone (no C, M or Y) applied from a CMYK document onto RGB stays black, and + // the CMYK white stays white (the merged image goes through RGB, so a few levels of drift). + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 20, "mode": "cmyk", "background": "white"})).unwrap(); + { + let d = s.active_mut().unwrap(); + let id = d.active_layer.unwrap(); + let surf = std::sync::Arc::make_mut(&mut d.doc).layer_mut(id).unwrap().surface_mut().unwrap(); + let n = surf.format().channels(); + let mut k = vec![0.0; 20 * 20 * n]; + k.chunks_exact_mut(n).for_each(|p| p[3] = 1.0); + surf.write_region(Rect::new(0, 0, 20, 20), &k); + } + s.execute("file.new", json!({"width": 40, "height": 20, "background": "white"})).unwrap(); + s.execute("image.applyImage", json!({"source": {"document": 0}, "blending": "normal"})).unwrap(); + let (black, white) = (px(&s, 5, 5), px(&s, 30, 5)); + assert!(black[..3].iter().all(|&v| v < 0.05), "{black:?}"); + assert!(white[..3].iter().all(|&v| v > 0.95), "{white:?}"); +} + +#[test] +fn apply_image_of_a_cmyk_composite_onto_an_alpha_channel_takes_its_luminosity() { + // #5787: a CMYK composite reaches a channel as its luminosity, as Calculations reads it, so + // Black alone stays dark and white stays white. + for depth in [8, 16] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 20, "mode": "cmyk", "depth": depth, "background": "white"})).unwrap(); + { + let d = s.active_mut().unwrap(); + let id = d.active_layer.unwrap(); + let surf = std::sync::Arc::make_mut(&mut d.doc).layer_mut(id).unwrap().surface_mut().unwrap(); + let n = surf.format().channels(); + let mut k = vec![1.0; 20 * 20 * n]; + k.chunks_exact_mut(n).for_each(|p| p[..3].fill(0.0)); + surf.write_region(Rect::new(0, 0, 20, 20), &k); + } + let a = s.execute("channel.new", json!({})).unwrap()["channel"].as_u64().unwrap() as usize; + s.execute("image.applyImage", json!({"blending": "normal", "target": {"channel": a}})).unwrap(); + let b = s.execute("image.calculations", json!({"source1": {}, "source2": {}, "blending": "normal"})).unwrap()["channel"].as_u64().unwrap() as usize; + let (black, white) = (chan(&s, a, 5, 5), chan(&s, a, 30, 5)); + assert!(black < 0.25 && (white - 1.0).abs() < 1e-3, "depth {depth}: black {black}, white {white}"); + assert!((black - chan(&s, b, 5, 5)).abs() < 1e-3, "depth {depth}: as Calculations reads it"); + // A targeted layer mask is a grayscale target too: it takes the same luminosity. + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("channel.target", json!({"channel": "mask"})).unwrap(); + s.execute("image.applyImage", json!({"blending": "normal"})).unwrap(); + let d = s.active().unwrap(); + let m = &d.doc.layer(d.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface; + let (mb, mw) = (m.sample_channel(5, 5, 0), m.sample_channel(30, 5, 0)); + assert!((mb - black).abs() < 1e-3 && (mw - 1.0).abs() < 1e-3, "depth {depth}: mask black {mb}, white {mw}"); + } +} + #[test] fn apply_image_into_targeted_layer_mask_leaves_the_pixels() { let mut s = session(); @@ -706,6 +898,30 @@ fn split_and_merge_channels() { assert!(s.execute("channel.merge", json!({"mode": "cmyk"})).is_err()); } +#[test] +fn cmyk_split_and_merge_use_tone() { + // #5787: Split Channels of a white CMYK image gives white grayscale documents, and merging + // them back gives the same CMYK pixels (within the few levels the merged image's trip + // through RGB costs). + for depth in [8, 16] { + let mut s = cmyk_session(depth); + let (left, right) = (px(&s, 5, 5), px(&s, 30, 5)); + s.execute("channel.split", json!({})).unwrap(); + assert_eq!(s.documents().len(), 4); + let v = |s: &Session, i: usize, x: i32| s.documents()[i].doc.layers[0].surface().unwrap().pixel(x, 5)[0]; + for i in 0..4 { + assert!((v(&s, i, 30) - 1.0).abs() < 1e-3, "depth {depth}: channel {i} of white is white"); + } + assert!((v(&s, 0, 5) - (1.0 - left[0])).abs() < 0.02, "depth {depth}: cyan ink splits dark"); + s.set_active(0); + s.execute("channel.merge", json!({"mode": "cmyk", "documents": [0, 1, 2, 3]})).unwrap(); + assert_eq!(doc(&s).mode, ColorMode::Cmyk); + let near = |a: &[f32], b: &[f32]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.02); + assert!(near(&px(&s, 5, 5), &left), "depth {depth}: round trip {:?} vs {left:?}", px(&s, 5, 5)); + assert!(near(&px(&s, 30, 5), &right), "depth {depth}: white stays white {:?}", px(&s, 30, 5)); + } +} + #[test] fn merge_rejects_a_repeated_source_and_keeps_unrelated_documents() { // #934: `documents: [0, 0, 1]` passed validation, then the close loop ran `close(1)`, diff --git a/crates/engine/src/cmd/edit_cmds.rs b/crates/engine/src/cmd/edit_cmds.rs index eb77590999..fc3eb3226d 100644 --- a/crates/engine/src/cmd/edit_cmds.rs +++ b/crates/engine/src/cmd/edit_cmds.rs @@ -506,8 +506,16 @@ pub(crate) fn paste_to_target(s: &mut Session, p: &Value, in_place: bool, limit: let src = shifted(&clip.surface, dx, dy); let area = src.content_bounds().intersect(&canvas); s.edit(label, |doc, _| { + // A spot channel stores ink: the paste goes over its tone, then back to ink (#5787). + let spot = crate::channel_cmds::is_spot_target(doc, p); let (surf, _) = crate::channel_cmds::target_surface(doc, layer, p)?; + if spot { + crate::channel_cmds::invert_region(surf, area)?; + } crate::fill_cmds::composite_over(surf, &src, area, limit)?; + if spot { + crate::channel_cmds::invert_region(surf, area)?; + } surf.prune(); doc.selection = None; Ok(()) @@ -761,11 +769,8 @@ fn transform_again_copy(s: &mut Session) -> Result { Err(e) => { // The copy (e.g. of a layer whose position is locked) couldn't be transformed: take // it back, leaving nothing to redo and no Duplicate step to record. - s.undo(); + s.rollback(); s.journal.truncate(journal); - if let Some(st) = s.active_mut() { - st.history.clear_redo(); - } Err(e) } } diff --git a/crates/engine/src/cmd/edit_cmds/tests.rs b/crates/engine/src/cmd/edit_cmds/tests.rs index e632d9fb71..ec9a9fa8b7 100644 --- a/crates/engine/src/cmd/edit_cmds/tests.rs +++ b/crates/engine/src/cmd/edit_cmds/tests.rs @@ -1138,6 +1138,31 @@ fn transform_again_on_a_copy_steps_and_repeats() { assert!(!s.active().unwrap().history.can_redo(), "nothing to redo"); } +/// History Options › Allow Non-Linear History: the Duplicate step of a refused Transform +/// Again on a copy moved the later states in front of it; taking it back must restore the +/// document before the command and the History list as it was, not land on a later state. +#[test] +fn a_refused_transform_again_on_a_copy_rolls_back_exactly_in_non_linear_history() { + let mut s = session(); + s.execute("history.options", json!({"allowNonLinearHistory": true})).unwrap(); + s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 10, 0]})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.setProps", json!({"layer": id.0, "locks": {"position": true}})).unwrap(); + s.execute("layer.setProps", json!({"layer": id.0, "opacity": 0.5})).unwrap(); + s.execute("layer.setProps", json!({"layer": id.0, "opacity": 0.25})).unwrap(); + s.undo(); + s.undo(); + let st = s.active().unwrap(); + let (doc, entries) = (st.doc.clone(), st.history.entries()); + let redo: Vec = st.history.redo_labels().map(str::to_string).collect(); + assert_eq!(redo.len(), 2); + assert!(s.execute("edit.transform.againCopy", json!({})).unwrap_err().to_string().contains("locked")); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&st.doc, &doc), "the document before the command"); + assert_eq!(st.history.entries(), entries); + assert_eq!(st.history.redo_labels().map(str::to_string).collect::>(), redo); +} + #[test] fn toggle_last_state_and_transform_again() { let mut s = session(); diff --git a/crates/engine/src/cmd/extra_cmds.rs b/crates/engine/src/cmd/extra_cmds.rs index 4a3190ec72..cdefb64cba 100644 --- a/crates/engine/src/cmd/extra_cmds.rs +++ b/crates/engine/src/cmd/extra_cmds.rs @@ -574,7 +574,7 @@ fn set_visible(s: &mut Session, p: &Value, visible: bool) -> Result { return crate::layer_multi_cmds::set_visible_selected(s, visible); } let id = layer_param(s, p)?; - s.edit(if visible { "Show Layer" } else { "Hide Layer" }, |doc, _| { + s.edit_visibility(if visible { "Show Layer" } else { "Hide Layer" }, |doc, _| { doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.visible = visible; Ok(()) })?; @@ -640,7 +640,7 @@ fn show_only(s: &mut Session, p: &Value) -> Result { let rows = walk.iter().map(|(p, _, l)| (p.clone(), before.as_ref().is_none_or(|b| b.get(&l.id).copied().unwrap_or(l.visible)))).collect(); ("Show Layers", rows, None) }; - s.edit(label, |doc, _| { + s.edit_visibility(label, |doc, _| { for (p, visible) in &rows { if let Some(l) = doc.layer_at_mut(p) { l.visible = *visible; diff --git a/crates/engine/src/cmd/frame_cmds.rs b/crates/engine/src/cmd/frame_cmds.rs index 2ea953609f..c006b730cd 100644 --- a/crates/engine/src/cmd/frame_cmds.rs +++ b/crates/engine/src/cmd/frame_cmds.rs @@ -54,11 +54,8 @@ fn frame_from_layers(s: &mut Session, p: &Value) -> Result { Err(e) => { // The group couldn't be framed: take the grouping back, leaving nothing to redo and // no grouping step to record. - s.undo(); + s.rollback(); s.journal.truncate(journal); - if let Some(st) = s.active_mut() { - st.history.clear_redo(); - } Err(e) } } diff --git a/crates/engine/src/cmd/history_cmds.rs b/crates/engine/src/cmd/history_cmds.rs index 0596bbb12d..d8bea8607b 100644 --- a/crates/engine/src/cmd/history_cmds.rs +++ b/crates/engine/src/cmd/history_cmds.rs @@ -39,27 +39,41 @@ fn bad(cmd: &str, msg: impl Into) -> EngineError { /// The `state` param as an index into the active document's history entries, defaulting to the /// current state. fn state_index(s: &Session, cmd: &str, p: &Value) -> Result<(usize, usize)> { + state_index_upto(s, cmd, p, false) +} + +/// [`state_index`], also accepting the later (dimmed, redo) states with `later`. +fn state_index_upto(s: &Session, cmd: &str, p: &Value, later: bool) -> Result<(usize, usize)> { let st = s.active().ok_or(EngineError::NoDocument)?; let current = st.history.past_len(); + let last = if later { current.saturating_add(st.history.redo_labels().count()) } else { current }; let i = match p.get("state") { None | Some(Value::Null) => current, Some(_) => { let v = int(p, "state").ok_or_else(|| bad(cmd, "`state` must be a number"))?; - usize::try_from(v).ok().filter(|i| *i <= current).ok_or_else(|| bad(cmd, format!("`state` = {v} is not a state (0..={current})")))? + usize::try_from(v).ok().filter(|i| *i <= last).ok_or_else(|| bad(cmd, format!("`state` = {v} is not a state (0..={last})")))? } }; Ok((i, current)) } /// History panel › Delete: drop the state and every state after it, as Photoshop does in linear -/// history. The document returns to the state before it, and the deleted steps can't be redone. -/// The oldest state (the document as opened) can't be deleted. +/// history (in non-linear history only that state, also a later one: +/// `history_options_cmds::delete_state`). The document returns to the state before it, and the +/// deleted steps can't be redone. The oldest state (the document as opened) can't be deleted. fn delete_state(s: &mut Session, p: &Value) -> Result { const CMD: &str = "history.deleteState"; - let (i, current) = state_index(s, CMD, p)?; + // History Options › Allow Non-Linear History: only that state goes, a later (dimmed) one too. + let non_linear = s.active().is_some_and(|st| st.history.non_linear); + let (i, current) = state_index_upto(s, CMD, p, non_linear)?; if i == 0 { return Err(bad(CMD, "the oldest state can't be deleted")); } + if non_linear { + crate::history_options_cmds::delete_state(s, i, current)?; + let entries = s.active().ok_or(EngineError::NoDocument)?.history.entries(); + return Ok(json!({ "states": entries.len(), "current": entries.last() })); + } // Step back to the state before `i`; the steps from `i` on are then redo states. for _ in i..=current { if !s.undo() { @@ -199,8 +213,13 @@ fn delete_snapshot(s: &mut Session, p: &Value) -> Result { Ok(json!({ "snapshot": snap.id, "snapshots": st.snapshots.len() })) } +/// A state other than the oldest exists to delete: a past or the current one, and with Allow +/// Non-Linear History a later (dimmed) one too, also while the oldest state is current. fn can_delete(s: &Session) -> std::result::Result<(), String> { - s.active().filter(|d| d.history.past_len() > 0).map(|_| ()).ok_or_else(|| "the oldest state can't be deleted".into()) + s.active() + .filter(|d| d.history.past_len() > 0 || (d.history.non_linear && d.history.can_redo())) + .map(|_| ()) + .ok_or_else(|| "the oldest state can't be deleted".into()) } fn has_doc(s: &Session) -> std::result::Result<(), String> { @@ -245,7 +264,7 @@ pub fn specs() -> Vec { label: "Delete State", menu: &[], shortcut: None, - params: r##"{"state":index?} (History entry, 0 = oldest; default the current state) → {states,current}: removes the state and every later one"##, + params: r##"{"state":index?} (History entry, 0 = oldest; default the current state) → {states,current}: removes the state and every later one (with Allow Non-Linear History only that state, a later dimmed one too)"##, enabled: can_delete, run: delete_state, journal: true, diff --git a/crates/engine/src/cmd/history_options_cmds.rs b/crates/engine/src/cmd/history_options_cmds.rs new file mode 100644 index 0000000000..17341b5ae1 --- /dev/null +++ b/crates/engine/src/cmd/history_options_cmds.rs @@ -0,0 +1,561 @@ +//! History panel › History Options… (#2966): Allow Non-Linear History and Make Layer Visibility +//! Changes Undoable. The values are preferences (`historyOptions.*`, also `prefs.set`), applied +//! by [`Session::apply_prefs`] and [`Session::edit_visibility`]. Without params the command +//! reports them; the GUI opens its dialog instead and runs it with the dialog's values on OK. +//! +//! Photoshop's three snapshot options (Automatically Create First Snapshot, Automatically +//! Create New Snapshot When Saving, Show New Snapshot Dialog by Default) come with #3634, which +//! makes the snapshots act on them. + +use serde_json::{Map, Value}; + +use crate::commands::CommandSpec; +use crate::{EngineError, Result, Session}; + +const CMD: &str = "history.options"; + +/// The option keys, as in `prefs.get historyOptions` and the command's params. +pub const OPTIONS: [&str; 2] = ["allowNonLinearHistory", "makeLayerVisibilityChangesUndoable"]; + +fn bad(msg: impl Into) -> EngineError { + EngineError::BadParams { cmd: CMD.into(), msg: msg.into() } +} + +/// The current History Options as `{allowNonLinearHistory, makeLayerVisibilityChangesUndoable}`. +pub fn current(s: &Session) -> Value { + let o = &s.prefs().history_options; + serde_json::json!({ + "allowNonLinearHistory": o.allow_non_linear_history, + "makeLayerVisibilityChangesUndoable": o.make_layer_visibility_changes_undoable, + }) +} + +fn options(s: &mut Session, p: &Value) -> Result { + let given: Map = match p { + Value::Null => Map::new(), + Value::Object(m) => m.clone(), + _ => return Err(bad("params must be an object")), + }; + // Validate everything before changing anything. + let mut set = Vec::new(); + for (k, v) in &given { + if !OPTIONS.contains(&k.as_str()) { + return Err(bad(format!("unknown option `{k}` (expected {})", OPTIONS.join(", ")))); + } + let b = v.as_bool().ok_or_else(|| bad(format!("`{k}` must be true or false")))?; + set.push((k.clone(), b)); + } + if !set.is_empty() { + s.edit_prefs(|prefs| { + for (k, b) in &set { + match k.as_str() { + "allowNonLinearHistory" => prefs.history_options.allow_non_linear_history = *b, + _ => prefs.history_options.make_layer_visibility_changes_undoable = *b, + } + } + }); + } + Ok(current(s)) +} + +/// History panel › Delete in non-linear history: delete state `i` (numbered as the History panel +/// lists them, `current` = the current state's index, the later dimmed states after it) and +/// nothing else. Deleting the current state steps back to the one before it; the later states +/// stay. +pub(crate) fn delete_state(s: &mut Session, i: usize, current: usize) -> Result<()> { + if i == 0 { + return Err(EngineError::BadParams { cmd: "history.deleteState".into(), msg: "the oldest state can't be deleted".into() }); + } + // A background job is computing from the current state: it must not move under it. + if s.active_job().is_some() { + return Err(EngineError::Other("the history can't step back right now".into())); + } + let keep_visibility = !s.visibility_undoable(); + let st = s.active_mut().ok_or(EngineError::NoDocument)?; + st.coalesce = None; + if i != current { + let removed = match i.checked_sub(current.saturating_add(1)) { + Some(j) => st.history.remove_later(j), + None => st.history.remove(i), + }; + if !removed { + return Err(EngineError::Other(format!("state {i} is not held"))); + } + // The document is unchanged, but the History panel's rows are not: a view-only change + // that keeps a clean document clean. + let clean = st.saved_revision == st.revision; + st.revision += 1; + st.view_revision += 1; + if clean { + st.saved_revision = st.revision; + } + st.last_damage = Some(photocraft_geom::Rect::EMPTY); + return Ok(()); + } + // The step that made the current state: the layers whose visibility it changed get it back. + let changed = st.history.current_visibility().to_vec(); + let (mut d, layers) = st.history.drop_current().ok_or_else(|| EngineError::Other("the history can't step back right now".into()))?; + // As Undo does: Save As changes file identity outside history. + if d.name != st.doc.name { + std::sync::Arc::make_mut(&mut d).name.clone_from(&st.doc.name); + } + if keep_visibility { + crate::carry_visibility(&st.doc, &mut d, &changed); + } + let damage = crate::layer_multi_cmds::step_damage(&d, &st.doc); + st.doc = d; + crate::restore_target(st, layers); + st.revision += 1; + st.last_damage = damage; + Ok(()) +} + +fn always(_: &Session) -> std::result::Result<(), String> { + Ok(()) +} + +pub fn specs() -> Vec { + vec![CommandSpec { + id: CMD, + label: "History Options…", + menu: &[], + shortcut: None, + params: r##"{"allowNonLinearHistory":bool?,"makeLayerVisibilityChangesUndoable":bool?} → both values (no params: report them). Allow Non-Linear History: an edit after stepping back keeps the later states and is appended at the end; deleting a state deletes only it (default off). Make Layer Visibility Changes Undoable: showing or hiding a layer is a history state (default on). Also `prefs.set historyOptions.*`"##, + enabled: always, + run: options, + journal: false, + }] +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn entries(s: &Session) -> Vec { + s.active().unwrap().history.entries() + } + + /// A document with a layer above the background: entries Open, New Layer. + fn doc() -> (Session, u64) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap().0; + (s, id) + } + + #[test] + fn defaults_match_photoshop_and_round_trip_through_prefs() { + let mut s = Session::new(); + assert_eq!(s.execute(CMD, json!({})).unwrap(), json!({"allowNonLinearHistory": false, "makeLayerVisibilityChangesUndoable": true})); + assert_eq!(s.execute("prefs.get", json!({"path": "historyOptions"})).unwrap(), current(&s)); + let r = s.execute(CMD, json!({"allowNonLinearHistory": true, "makeLayerVisibilityChangesUndoable": false})).unwrap(); + assert_eq!(r, json!({"allowNonLinearHistory": true, "makeLayerVisibilityChangesUndoable": false})); + assert_eq!(s.execute("prefs.get", json!({"path": "historyOptions.allowNonLinearHistory"})).unwrap(), json!(true)); + // Saved and restored with the other preferences. + let saved = s.prefs_to_json(); + let mut t = Session::new(); + t.load_prefs_json(&saved).unwrap(); + assert_eq!(current(&t), r); + // prefs.set and prefs.reset reach them too. + t.execute("prefs.set", json!({"path": "historyOptions.allowNonLinearHistory", "value": false})).unwrap(); + assert_eq!(t.execute(CMD, Value::Null).unwrap()["allowNonLinearHistory"], json!(false)); + t.execute("prefs.reset", json!({"path": "historyOptions"})).unwrap(); + assert_eq!(current(&t), current(&Session::new())); + // Older preference files without the section load with the defaults. + t.load_prefs_json("{}").unwrap(); + assert_eq!(current(&t), current(&Session::new())); + } + + #[test] + fn bad_params_are_refused_and_change_nothing() { + let mut s = Session::new(); + let before = current(&s); + for p in [ + json!({"allowNonLinearHistory": "yes"}), + json!({"allowNonLinearHistory": 1}), + json!({"makeLayerVisibilityChangesUndoable": null}), + json!({"allowNonLinearHistory": true, "snapshots": true}), + json!([true]), + json!("x"), + json!(3), + ] { + assert!(s.execute(CMD, p.clone()).is_err(), "{p}"); + assert_eq!(current(&s), before, "{p} changed nothing"); + } + assert!(s.execute("prefs.set", json!({"path": "historyOptions.allowNonLinearHistory", "value": "on"})).is_err()); + assert_eq!(current(&s), before); + } + + #[test] + fn layer_visibility_is_a_history_state_only_when_undoable() { + // On (the default): the eye, Hide/Show Layers and ⌥-click each add a state. + let (mut s, id) = doc(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + assert_eq!(entries(&s).last().map(String::as_str), Some("Layer Visibility")); + s.execute("edit.undo", json!({})).unwrap(); + assert!(s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().visible); + + // Off: the document changes (and is modified) but History doesn't. + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + let before = entries(&s); + let rev = s.active().unwrap().revision; + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + s.execute("layer.showLayers", json!({"layer": id})).unwrap(); + s.execute("layer.hideLayers", json!({"layer": id})).unwrap(); + s.execute("layer.showOnly", json!({"layer": id})).unwrap(); + s.execute("layer.showOnly", json!({"layer": id})).unwrap(); + // Hide Layers toggles (⌘,), so show the layer first for it to hide. + s.execute("layer.showLayers", json!({"layer": id})).unwrap(); + s.execute("layer.hideLayers", json!({"layer": id})).unwrap(); + assert_eq!(entries(&s), before, "no visibility state was recorded"); + let st = s.active().unwrap(); + assert!(!st.doc.layer(photocraft_doc::LayerId(id)).unwrap().visible, "the change still applies"); + assert!(st.revision > rev && st.is_dirty()); + // Other properties still record a state. + s.execute("layer.setProps", json!({"layer": id, "opacity": 0.5})).unwrap(); + assert_eq!(entries(&s).len(), before.len() + 1); + // An unrecorded change leaves the redo states alone. + s.execute("edit.undo", json!({})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": true})).unwrap(); + assert!(s.active().unwrap().history.can_redo()); + } + + fn visible(s: &Session, id: u64) -> bool { + s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().visible + } + + fn red(s: &Session, id: u64) -> bool { + let st = s.active().unwrap(); + st.doc.layer(photocraft_doc::LayerId(id)).unwrap().surface().unwrap().pixel(1, 1)[0] > 0.5 + } + + /// Off, showing or hiding a layer is not a state, so stepping through history (Undo, Redo, a + /// History row) leaves the visibility as the user set it, as Photoshop does; history states + /// hold whole documents and would otherwise bring the old visibility back. + #[test] + fn stepping_through_history_keeps_unrecorded_visibility() { + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + assert!(s.execute("edit.undo", json!({})).unwrap().as_bool().unwrap()); + assert!(!red(&s, id), "the fill is undone"); + assert!(!visible(&s, id), "the layer stays hidden"); + s.execute("layer.setProps", json!({"layer": id, "visible": true})).unwrap(); + s.execute("edit.redo", json!({})).unwrap(); + assert!(red(&s, id) && visible(&s, id), "redo brings the fill back, not the hidden layer"); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + // History rows: New, New Layer, Fill. + s.execute("history.selectState", json!({"state": 1})).unwrap(); + assert!(!red(&s, id) && !visible(&s, id)); + s.execute("history.selectState", json!({"state": 2})).unwrap(); + assert!(red(&s, id) && !visible(&s, id)); + + // On (the default): Undo takes the hide back first, then the fill. + let (mut s, id) = doc(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + assert!(red(&s, id) && visible(&s, id)); + s.execute("edit.undo", json!({})).unwrap(); + assert!(!red(&s, id) && visible(&s, id)); + s.execute("history.selectState", json!({"state": 3})).unwrap(); + assert!(red(&s, id) && !visible(&s, id), "the hide is a state of its own"); + } + + /// Off, only the unrecorded visibility changes carry across history: a recorded step that + /// changed visibility (Apply Layer Comp) gives it back when undone and sets it again when + /// redone, whether by Undo / Redo, a History row or deleting the current state. + #[test] + fn stepping_over_a_recorded_visibility_change_restores_it() { + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + // A comp with the layer hidden, then the eye shows it again (no state) and a fill. + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + s.execute("layerComp.new", json!({"name": "Hidden"})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": true})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("layerComp.apply", json!({"comp": "Hidden"})).unwrap(); + assert!(red(&s, id) && !visible(&s, id), "the comp hides the layer"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(red(&s, id) && visible(&s, id), "undoing Apply Layer Comp shows the layer again"); + // The eye hides it (no state): undoing the fill keeps that. + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + assert!(!red(&s, id) && !visible(&s, id), "the unrecorded hide carries across the fill's undo"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(red(&s, id) && !visible(&s, id)); + s.execute("layer.setProps", json!({"layer": id, "visible": true})).unwrap(); + s.execute("edit.redo", json!({})).unwrap(); + assert!(red(&s, id) && !visible(&s, id), "redoing the comp hides the layer again"); + + // History rows: New, New Layer, New Layer Comp, Fill, Apply Layer Comp. + assert_eq!(entries(&s).len(), 5); + s.execute("history.selectState", json!({"state": 2})).unwrap(); + assert!(!red(&s, id) && visible(&s, id), "jumping back over the comp shows the layer again"); + s.execute("history.selectState", json!({"state": 4})).unwrap(); + assert!(red(&s, id) && !visible(&s, id), "jumping forward over the comp hides it again"); + + // Non-linear Delete of the current state (the comp) steps back and shows the layer too. + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + s.execute("history.deleteState", json!({})).unwrap(); + assert!(red(&s, id) && visible(&s, id), "deleting the comp's state shows the layer again"); + + // Non-linear Delete of a past state: the step after it now starts from the state before + // it, so undoing that step also gives back the visibility the deleted step changed. + s.execute("layerComp.apply", json!({"comp": "Hidden"})).unwrap(); + s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + let comp = entries(&s).len() - 2; + s.execute("history.deleteState", json!({"state": comp})).unwrap(); + assert!(!visible(&s, id)); + s.execute("edit.undo", json!({})).unwrap(); + assert!(red(&s, id) && visible(&s, id), "back at the red fill, where the layer was shown"); + } + + #[test] + fn non_linear_history_keeps_later_states_and_delete_removes_only_one() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + for c in ["#ff0000", "#00ff00", "#0000ff"] { + s.execute("edit.fill", json!({"color": c})).unwrap(); + } + let filled = entries(&s); + assert_eq!(filled.len(), 4); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("image.adjustments.invert", json!({})).unwrap(); + let after = entries(&s); + assert_eq!(after.len(), 5, "the two later fills are kept: {after:?}"); + assert_eq!(after[..4], filled[..]); + // Delete the second fill only: the states after it stay. + s.execute("history.deleteState", json!({"state": 2})).unwrap(); + let mut expected = after.clone(); + expected.remove(2); + assert_eq!(entries(&s), expected); + // Deleting the current state steps back to the one before; nothing else goes. + let doc_before = s.active().unwrap().history.state(expected.len() - 2).unwrap(); + s.execute("history.deleteState", json!({})).unwrap(); + assert_eq!(entries(&s), expected[..expected.len() - 1].to_vec()); + assert!(std::sync::Arc::ptr_eq(&s.active().unwrap().doc, &doc_before)); + // After stepping back, deleting the current state keeps the later (redo) states. + s.execute("edit.undo", json!({})).unwrap(); + let redo: Vec = s.active().unwrap().history.redo_labels().map(str::to_string).collect(); + s.execute("history.deleteState", json!({})).unwrap(); + assert_eq!(s.active().unwrap().history.redo_labels().map(str::to_string).collect::>(), redo); + + // Turning it off applies to open documents: the next edit after stepping back discards. + s.execute(CMD, json!({"allowNonLinearHistory": false})).unwrap(); + assert!(!s.active().unwrap().history.non_linear); + s.execute("edit.fill", json!({"color": "#808080"})).unwrap(); + assert!(!s.active().unwrap().history.can_redo()); + // New documents pick the option up too. + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + assert!(s.active().unwrap().history.non_linear); + } + + /// With the oldest state current, non-linear Delete is still enabled for a later (dimmed) + /// state (the History panel's trash and its drop target ask for it); linear history has + /// nothing to delete there. + #[test] + fn non_linear_delete_is_enabled_while_the_oldest_state_is_current() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + for c in ["#ff0000", "#00ff00"] { + s.execute("edit.fill", json!({"color": c})).unwrap(); + } + s.execute("edit.undo", json!({})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + assert!(!s.is_enabled("history.deleteState"), "linear: only the oldest state is left to delete"); + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + assert!(s.is_enabled("history.deleteState")); + let doc = s.active().unwrap().doc.clone(); + s.execute("history.deleteState", json!({"state": 1})).unwrap(); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&st.doc, &doc), "the document is unchanged"); + assert_eq!(st.history.redo_labels().count(), 1, "only the first fill went"); + assert!(s.execute("history.deleteState", json!({})).is_err(), "the oldest state itself stays"); + s.execute("history.deleteState", json!({"state": 1})).unwrap(); + assert!(!s.is_enabled("history.deleteState"), "nothing left but the oldest state"); + } + + /// Non-linear Delete of a past or later state leaves the document as it is, so a saved + /// (clean) document stays clean; deleting the current state steps back and modifies it. + #[test] + fn non_linear_delete_of_another_state_keeps_a_clean_document_clean() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + for c in ["#ff0000", "#00ff00", "#0000ff", "#808080"] { + s.execute("edit.fill", json!({"color": c})).unwrap(); + } + s.execute("edit.undo", json!({})).unwrap(); + // States: New, Fill 1-3 (3 current), the dimmed Fill 4. As if just saved. + let st = s.active_mut().unwrap(); + st.saved_revision = st.revision; + let rev = st.revision; + s.execute("history.deleteState", json!({"state": 1})).unwrap(); + let st = s.active().unwrap(); + assert!(!st.is_dirty(), "deleting a past state left the saved document unmodified"); + assert!(st.revision > rev, "the History panel still sees the change"); + s.execute("history.deleteState", json!({"state": 3})).unwrap(); + assert!(!s.active().unwrap().is_dirty(), "deleting a later state left it unmodified too"); + assert_eq!(s.active().unwrap().history.redo_labels().count(), 0); + // A modified document stays modified. + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + s.execute("history.deleteState", json!({"state": 1})).unwrap(); + assert!(s.active().unwrap().is_dirty()); + // The current state: the document steps back, which is a change. + let st = s.active_mut().unwrap(); + st.saved_revision = st.revision; + s.execute("history.deleteState", json!({})).unwrap(); + assert!(s.active().unwrap().is_dirty()); + } + + /// With the option off, a recorded step that changes visibility along with other properties + /// (layer.setProps visible + opacity is one Layer Properties state) is undone and redone as a + /// whole: the carried-over visibility skips the layers the step itself changed. + #[test] + fn undoing_a_mixed_visibility_and_opacity_change_undoes_both() { + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + let n = entries(&s).len(); + s.execute("layer.setProps", json!({"layer": id, "visible": false, "opacity": 0.5})).unwrap(); + assert_eq!(entries(&s).len(), n + 1, "the mixed change is a state"); + let opacity = |s: &Session| s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().opacity; + s.execute("edit.undo", json!({})).unwrap(); + assert!(visible(&s, id) && (opacity(&s) - 1.0).abs() < 1e-6, "undo shows the layer again and restores its opacity"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(!visible(&s, id) && (opacity(&s) - 0.5).abs() < 1e-6, "redo hides it again"); + } + + /// Non-linear Delete reaches a later (dimmed) state too, and deletes only it; linear history + /// still refuses one past the current state. + #[test] + fn non_linear_delete_removes_a_later_state() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + for c in ["#ff0000", "#00ff00", "#0000ff"] { + s.execute("edit.fill", json!({"color": c})).unwrap(); + } + s.execute("edit.undo", json!({})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + assert!(s.execute("history.deleteState", json!({"state": 2})).is_err(), "linear: not a deletable state"); + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + let doc = s.active().unwrap().doc.clone(); + let mut expected = entries(&s); + let mut redo: Vec = s.active().unwrap().history.redo_labels().map(str::to_string).collect(); + // Entries: New, Fill (current), then the dimmed Fill 2 (state 2) and Fill 3 (state 3). + s.execute("history.deleteState", json!({"state": 2})).unwrap(); + redo.remove(0); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&st.doc, &doc), "the document is unchanged"); + assert_eq!(st.history.redo_labels().map(str::to_string).collect::>(), redo); + assert_eq!(entries(&s), expected); + assert!(s.execute("history.deleteState", json!({"state": 4})).is_err(), "past the last state"); + s.execute("edit.redo", json!({})).unwrap(); + expected.push(redo[0].clone()); + assert_eq!(entries(&s), expected); + let px = s.active().unwrap().doc.layers[0].surface().unwrap().pixel(1, 1); + assert!(px[2] > 0.5 && px[1] < 0.5, "redo reaches the blue fill: {px:?}"); + } + + /// Off, taking a step back with `Session::rollback` (a cancelled Free Transform of a made + /// layer, an abandoned Alt-drag copy, a compound command whose later part failed) keeps the + /// unrecorded visibility as Undo does, and gives back the visibility the step itself changed, + /// whether it takes the step back exactly, falls back to Undo or lands on the oldest state. + #[test] + fn rollback_keeps_unrecorded_visibility() { + // Exactly: the fill is taken back, the eye's hide stays. + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + assert!(s.rollback()); + assert!(!red(&s, id), "the fill is taken back"); + assert!(!visible(&s, id), "the unrecorded hide stays"); + assert!(!s.active().unwrap().history.can_redo(), "nothing to redo"); + + // Landing on the oldest state (the New Layer step taken back): the hidden background stays + // hidden. + let (mut s, _) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + let bg = s.active().unwrap().doc.layers[0].id.0; + s.execute("layer.setProps", json!({"layer": bg, "visible": false})).unwrap(); + assert!(s.rollback()); + assert_eq!(s.active().unwrap().doc.layers.len(), 1, "the new layer is taken back"); + assert!(!visible(&s, bg), "the unrecorded hide stays on the oldest state"); + assert!(!s.rollback(), "nothing left to take back"); + assert!(!visible(&s, bg)); + + // A step that changed visibility (Apply Layer Comp) gives it back. + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + s.execute("layerComp.new", json!({"name": "Hidden"})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": true})).unwrap(); + s.execute("layerComp.apply", json!({"comp": "Hidden"})).unwrap(); + assert!(!visible(&s, id)); + assert!(s.rollback()); + assert!(visible(&s, id), "taking back Apply Layer Comp shows the layer again"); + + // Falling back to Undo (the history changed since the fill: undo, redo). + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("edit.redo", json!({})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + assert!(s.rollback()); + assert!(!red(&s, id) && !visible(&s, id), "the fallback Undo keeps the hide too"); + assert!(!s.active().unwrap().history.can_redo()); + + // Non-linear: a step recorded after stepping back, taken back exactly, keeps the hide and + // gives the later state back to redo. + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false, "allowNonLinearHistory": true})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("image.adjustments.invert", json!({})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + assert!(s.rollback()); + assert!(!red(&s, id) && !visible(&s, id), "back before the invert, still hidden"); + assert!(s.active().unwrap().history.can_redo(), "the fill is redoable again"); + + // On (the default) the eye records a state of its own, which a rollback takes back. + let (mut s, id) = doc(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + assert!(s.rollback()); + assert!(red(&s, id) && visible(&s, id), "the hide is taken back, the fill stays"); + } + + /// Non-linear, a step that moves more later states than the History States limit leaves room + /// for is still taken back exactly by a rollback (a compound command whose later part failed): + /// the document before it comes back with the full list, not an Undo onto the last later + /// state that also drops the rest. + #[test] + fn rollback_past_the_state_limit_restores_the_list() { + let (mut s, id) = doc(); + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("image.adjustments.invert", json!({})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + let before = entries(&s); + let redo: Vec = s.active().unwrap().history.redo_labels().map(str::to_string).collect(); + assert_eq!(redo.len(), 2); + s.active_mut().unwrap().history.max_states = 2; + s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + assert!(s.rollback()); + assert_eq!(entries(&s), before); + assert_eq!(s.active().unwrap().history.redo_labels().map(str::to_string).collect::>(), redo); + assert!(!red(&s, id), "back on the New Layer state, not on the inverted fill"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(red(&s, id), "the red fill is still redoable"); + } +} diff --git a/crates/engine/src/cmd/history_select_cmds.rs b/crates/engine/src/cmd/history_select_cmds.rs index 2409722121..3b04fe0221 100644 --- a/crates/engine/src/cmd/history_select_cmds.rs +++ b/crates/engine/src/cmd/history_select_cmds.rs @@ -32,6 +32,7 @@ fn select_state(s: &mut Session, p: &Value) -> Result { if s.active_job().is_some() { return Err(EngineError::Other("the history can't move while a command is still running on this document".into())); } + let keep_visibility = !s.visibility_undoable(); let st = s.active_mut().ok_or(EngineError::NoDocument)?; if target == past { return Ok(json!({ "state": target, "changed": false })); @@ -44,8 +45,19 @@ fn select_state(s: &mut Session, p: &Value) -> Result { let mut layers = None; let mut at = past; while at != target { - let step = if target < at { st.history.undo(doc.clone()) } else { st.history.redo(doc.clone()) }; - let Some((d, l)) = step else { break }; + // History Options: visibility changes that are not history states stay as they are, + // step by step as Undo / Redo keep them, except on the layers whose visibility the step + // passed over changed. + let undoing = target < at; + let mut changed = if undoing { st.history.current_visibility().to_vec() } else { Vec::new() }; + let step = if undoing { st.history.undo(doc.clone()) } else { st.history.redo(doc.clone()) }; + let Some((mut d, l)) = step else { break }; + if keep_visibility { + if !undoing { + changed = st.history.current_visibility().to_vec(); + } + crate::carry_visibility(&doc, &mut d, &changed); + } doc = d; layers = Some(l); at = if target < at { at - 1 } else { at + 1 }; diff --git a/crates/engine/src/cmd/layer_menu_cmds/tests.rs b/crates/engine/src/cmd/layer_menu_cmds/tests.rs index b0cceb72a8..702d65891b 100644 --- a/crates/engine/src/cmd/layer_menu_cmds/tests.rs +++ b/crates/engine/src/cmd/layer_menu_cmds/tests.rs @@ -432,7 +432,7 @@ fn stack_modes_render_from_the_nested_layers() { surf.write_region(Rect::new(3, 3, 4, 4), &photocraft_raster::from_rgba(&fmt, [v, v, v, 1.0])); kids.push(l); } - let g = Layer::new("stack", LayerContent::Group(photocraft_doc::Group { children: kids, expanded: true, artboard: None })); + let g = Layer::new("stack", LayerContent::Group(photocraft_doc::Group { children: kids, expanded: true, artboard: None, frame_stroke: None })); let smart = crate::smart_cmds::layer_to_smart(doc, &g)?; let id = smart.id; doc.layers = vec![smart]; diff --git a/crates/engine/src/cmd/layer_multi_cmds/structure.rs b/crates/engine/src/cmd/layer_multi_cmds/structure.rs index 7800db630f..c8146901bc 100644 --- a/crates/engine/src/cmd/layer_multi_cmds/structure.rs +++ b/crates/engine/src/cmd/layer_multi_cmds/structure.rs @@ -278,7 +278,7 @@ pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Resul /// Show/hide every selected layer in one step. pub fn set_visible_selected(s: &mut Session, visible: bool) -> Result { let sel = selected(s); - s.edit(if visible { "Show Layers" } else { "Hide Layers" }, |doc, _| { + s.edit_visibility(if visible { "Show Layers" } else { "Hide Layers" }, |doc, _| { for id in &sel { doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?.visible = visible; } diff --git a/crates/engine/src/cmd/layer_xmp_cmds.rs b/crates/engine/src/cmd/layer_xmp_cmds.rs index 4fb4eda32f..742e0adf8d 100644 --- a/crates/engine/src/cmd/layer_xmp_cmds.rs +++ b/crates/engine/src/cmd/layer_xmp_cmds.rs @@ -33,7 +33,7 @@ fn check_packet(raw: &str) -> Result<()> { if raw.contains('\0') { return Err(bad("`rawData` can't contain NUL characters")); } - let xml = roxmltree::Document::parse(raw).map_err(|e| bad(format!("`rawData` isn't well-formed XML: {e}")))?; + let xml = photocraft_io::xml::parse(raw, roxmltree::ParsingOptions::default()).map_err(|e| bad(format!("`rawData` isn't well-formed XML: {e}")))?; let root = xml.root_element().tag_name(); let xmp = matches!((root.namespace(), root.name()), (Some("adobe:ns:meta/"), "xmpmeta") | (Some("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), "RDF")); if !xmp { diff --git a/crates/engine/src/cmd/layer_xmp_cmds/tests.rs b/crates/engine/src/cmd/layer_xmp_cmds/tests.rs index 015f739399..5fb67ad13c 100644 --- a/crates/engine/src/cmd/layer_xmp_cmds/tests.rs +++ b/crates/engine/src/cmd/layer_xmp_cmds/tests.rs @@ -61,6 +61,27 @@ fn sets_reads_and_removes_as_one_undo_step_each() { } } +/// A `rawData` packet nested 20 000 deep overflowed the XML parser's recursion (an abort nothing +/// can catch, from one command param); it is a param error now. The small stack makes a +/// regression abort instead of passing. +#[test] +fn deeply_nested_packets_are_refused_not_a_stack_overflow() { + let (s, a, _) = fixture(8); + let raw = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let (s, e) = std::thread::Builder::new() + .stack_size(1024 * 1024) + .spawn(move || { + let mut s = s; + let e = s.execute(ID, json!({"rawData": raw})).err(); + (s, e) + }) + .unwrap() + .join() + .unwrap(); + assert!(matches!(&e, Some(EngineError::BadParams { msg, .. }) if msg.contains("nested")), "{e:?}"); + assert_eq!(xmp(&s, a), None); +} + #[test] fn duplicating_a_layer_copies_its_packet() { let (mut s, a, _) = fixture(8); diff --git a/crates/engine/src/cmd/print_cmds.rs b/crates/engine/src/cmd/print_cmds.rs index d115566a7c..9b26b637c0 100644 --- a/crates/engine/src/cmd/print_cmds.rs +++ b/crates/engine/src/cmd/print_cmds.rs @@ -71,6 +71,176 @@ pub struct Spooler { pub run: SpoolFn, } +/// A stand-in for the system's printers, installed on `Session::printer_source` (tests): +/// `file.printers`, `file.print.printers` and the Print dialog list these and do not ask the spooler. +/// `file.print.printers` and the dialog's driver Printer menu list them on Windows only, where the +/// GDI route is; elsewhere they answer as that route does there. In the browser `file.printers` +/// lists none of them, as it lists no printers there without a stand-in. +pub type PrinterSource = std::sync::Arc Vec + Send + Sync>; + +/// Held while a thread of this process lists printers or calls into a printer driver. +static SPOOLER: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +/// Runs `f`, which lists printers or calls into a printer driver, while no other thread of this +/// process does either. On Windows, two threads that open printers' PrintTicket providers for the +/// first time at once can crash the process inside the spooler client (`winspool.drv`). A panic in +/// an earlier caller does not block later ones. A call made inside `f` on the same thread runs at +/// once. +pub fn with_spooler(f: impl FnOnce() -> T) -> T { + if HOLDING.get() { + return f(); + } + let _held = SPOOLER.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + holding(f) +} + +thread_local! { + /// Whether this thread holds `SPOOLER`. + static HOLDING: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Runs `f` with `HOLDING` set; called with `SPOOLER` held. +fn holding(f: impl FnOnce() -> T) -> T { + struct Release; + impl Drop for Release { + fn drop(&mut self) { + HOLDING.set(false); + } + } + HOLDING.set(true); + let _release = Release; + f() +} + +/// Counts the times this process asked the system for its printers. +static SYSTEM_LISTINGS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// How many times this process has asked the system for its printers. A session with a +/// `printer_source` does not ask. +pub fn system_printer_listings() -> usize { + SYSTEM_LISTINGS.load(std::sync::atomic::Ordering::Relaxed) +} + +/// Runs `f` as `with_spooler` does, or returns `None` at once while another thread of this +/// process holds the lock (the UI thread, which must not wait for a print job or a driver). +pub fn try_with_spooler(f: impl FnOnce() -> T) -> Option { + if HOLDING.get() { + return Some(f()); + } + let _held = match SPOOLER.try_lock() { + Ok(held) => held, + Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(), + Err(std::sync::TryLockError::WouldBlock) => return None, + }; + Some(holding(f)) +} + +/// What the Print dialog shows when `try_with_spooler` finds the lock held. +pub const SPOOLER_BUSY: &str = "the printer driver is busy with another print; try again when it finishes"; + +/// The GDI route's printers, or why they could not be listed. +pub type RgbPrinters = std::result::Result, craft_print::rgb::PrintError>; + +/// A list kept with the session's `printer_source` it came from (`None` for the system's printers). +type Kept = std::sync::Mutex, T)>>; + +/// The last answers `system_printer_list` and `rgb_printers` got in this process. +static LAST_PRINTERS: Kept> = std::sync::Mutex::new(None); +static LAST_RGB_PRINTERS: Kept = std::sync::Mutex::new(None); + +/// The kept list when it came from `source`; a list from other printers is not returned. +fn last(kept: &Kept, source: Option<&PrinterSource>) -> Option { + let kept = kept.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let (from, list) = kept.as_ref()?; + let same = match (from, source) { + (None, None) => true, + (Some(from), Some(source)) => std::sync::Arc::ptr_eq(from, source), + _ => false, + }; + same.then(|| list.clone()) +} + +fn keep(kept: &Kept, source: Option<&PrinterSource>, value: T) { + *kept.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some((source.cloned(), value)); +} + +/// Lists the printers; called with the spooler lock held. +fn list_system_printers(s: &Session) -> Vec { + let list = match stand_in_printers(s.printer_source.as_ref(), cfg!(target_arch = "wasm32")) { + Some(source) => source(), + None => { + SYSTEM_LISTINGS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + craft_print::printers() + } + }; + keep(&LAST_PRINTERS, s.printer_source.as_ref(), list.clone()); + list +} + +/// `file.printers`' stand-in: the session's outside the browser, and `None` in the browser, where +/// `file.printers` lists no printers. +fn stand_in_printers(source: Option<&PrinterSource>, browser: bool) -> Option<&PrinterSource> { + source.filter(|_| !browser) +} + +/// Lists the GDI route's printers; called with the spooler lock held. +fn list_rgb_printers(s: &Session) -> RgbPrinters { + let list = match &s.printer_source { + Some(source) => stand_in_rgb_printers(source, cfg!(windows)), + None => { + SYSTEM_LISTINGS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + craft_print::rgb::printers() + } + }; + keep(&LAST_RGB_PRINTERS, s.printer_source.as_ref(), list.clone()); + list +} + +/// The GDI route's answer from a stand-in: its printers on Windows, and `Unsupported` elsewhere, +/// as `craft_print::rgb::printers` answers there. +fn stand_in_rgb_printers(source: &PrinterSource, windows: bool) -> RgbPrinters { + if !windows { + return Err(craft_print::rgb::PrintError::Unsupported); + } + Ok(source().into_iter().map(|p| craft_print::rgb::Printer { name: p.name, default: p.default }).collect()) +} + +/// The printers `craft-print` spools to, or the session's stand-in for them. +fn system_printer_list(s: &Session) -> Vec { + with_spooler(|| list_system_printers(s)) +} + +/// The printers the GDI route prints to (`craft_print::rgb`), or on Windows the session's stand-in +/// for them. +pub fn rgb_printers(s: &Session) -> RgbPrinters { + with_spooler(|| list_rgb_printers(s)) +} + +/// Lists printers when the spooler is available. While it is busy, returns the cached list for +/// the same source, or `Unsupported` if that source has no cached list. +pub fn rgb_printers_without_waiting(s: &Session) -> RgbPrinters { + try_with_spooler(|| list_rgb_printers(s)) + .or_else(|| last(&LAST_RGB_PRINTERS, s.printer_source.as_ref())) + .unwrap_or(Err(craft_print::rgb::PrintError::Unsupported)) +} + +/// Lists printers for the Print dialog. While the system spooler is busy, returns the cached +/// list for the same source, or an empty list if that source has no cached list. +pub fn printers_without_waiting(s: &mut Session) -> Value { + if stand_in_printers(s.printer_source.as_ref(), cfg!(target_arch = "wasm32")).is_none() && system_spooler().is_none() { + return system_printers(s, &json!({})).unwrap_or_default(); + } + let list = try_with_spooler(|| list_system_printers(s)).or_else(|| last(&LAST_PRINTERS, s.printer_source.as_ref())).unwrap_or_default(); + printers_answer(list) +} + +/// `file.printers`' answer for a list from `craft-print`. +fn printers_answer(list: Vec) -> Value { + let default = list.iter().find(|p| p.default).map(|p| p.name.clone()); + let printers: Vec = list.into_iter().map(|p| json!({"name": p.name, "default": p.default})).collect(); + json!({"server": Value::Null, "default": default, "printers": printers}) +} + /// Why the IPP route could not spool: no route at all (the app's spooler may have one), or a /// failure on a route that exists (a wrong printer name, a rejected job). enum SpoolError { @@ -91,7 +261,7 @@ fn spool_job(spooler: Option<&Spooler>, job: &PrintJob, lp: &craft_print::Job) - } if let Some(name) = system_spooler() { require_windows_pdf()?; - return craft_print::submit(&job.pdf, lp).map(|m| (m, name.to_string())).map_err(other); + return with_spooler(|| craft_print::submit(&job.pdf, lp)).map(|m| (m, name.to_string())).map_err(other); } } match cups_spool(job) { @@ -190,13 +360,10 @@ fn cups_spool(job: &PrintJob) -> std::result::Result { /// `file.printers`: the printers the system's (or `printServer`'s) print server knows, with the default. From /// `craft-print` (`lpstat`, the Windows spooler) where that is the route; over IPP for a named /// server and where there is no `lp` (a sandbox). -fn system_printers(_s: &mut Session, p: &Value) -> Result { +fn system_printers(s: &mut Session, p: &Value) -> Result { let named = p.get("printServer").and_then(Value::as_str).filter(|v| !v.is_empty()); - if named.is_none() && system_spooler().is_some() { - let list = craft_print::printers(); - let default = list.iter().find(|p| p.default).map(|p| p.name.clone()); - let printers: Vec = list.into_iter().map(|p| json!({"name": p.name, "default": p.default})).collect(); - return Ok(json!({"server": Value::Null, "default": default, "printers": printers})); + if named.is_none() && (stand_in_printers(s.printer_source.as_ref(), cfg!(target_arch = "wasm32")).is_some() || system_spooler().is_some()) { + return Ok(printers_answer(system_printer_list(s))); } #[cfg(all(not(target_arch = "wasm32"), not(target_os = "windows")))] { @@ -736,7 +903,8 @@ fn with_driver_printable(p: &Value) -> Value { fn driver_printable(p: &Value) -> Option { let printer = printer_name(p).ok()?; - let pg = craft_print::rgb::page(&printer, printer_settings(p).ok()?.as_ref()).ok()?; + let settings = printer_settings(p).ok()?; + let pg = with_spooler(|| craft_print::rgb::page(&printer, settings.as_ref())).ok()?; // Only for the paper the driver is set to: a print laid out on another paper keeps the // fixed margin (and `print_windows` refuses it), as before. let (pw, ph) = paper(p, "file.print").ok()?; @@ -905,7 +1073,7 @@ fn printer_name(p: &Value) -> Result { if let Some(name) = p.get("printer").and_then(Value::as_str).filter(|v| !v.trim().is_empty()) { return Ok(name.to_string()); } - craft_print::rgb::default_printer().map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set")) + with_spooler(craft_print::rgb::default_printer).map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set")) } /// The driver settings chosen with Print Settings… (`printerSettings`, hex), when any. @@ -958,16 +1126,16 @@ fn settings_for_layout( settings: Option, paper: (f64, f64), ) -> Result<(Option, craft_print::rgb::Page)> { - let page = craft_print::rgb::page(printer, settings.as_ref()).map_err(other)?; + let page = with_spooler(|| craft_print::rgb::page(printer, settings.as_ref())).map_err(other)?; if !turned_paper(page.paper, paper) { return Ok((settings, page)); } let base = match settings { Some(s) => s, - None => craft_print::rgb::default_settings(printer).map_err(other)?, + None => with_spooler(|| craft_print::rgb::default_settings(printer)).map_err(other)?, }; let turned = turned_settings(&base, paper.0 > paper.1)?; - let page = craft_print::rgb::page(printer, Some(&turned)).map_err(other)?; + let page = with_spooler(|| craft_print::rgb::page(printer, Some(&turned))).map_err(other)?; Ok((Some(turned), page)) } @@ -1027,14 +1195,14 @@ fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Resul color_policy, primitives: &primitives, }; - let done = craft_print::rgb::print(&job, &image, rect).map_err(other)?; + let done = with_spooler(|| craft_print::rgb::print(&job, &image, rect)).map_err(other)?; Ok( json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3]}), ) } -fn printers(_: &mut Session, _: &Value) -> Result { - match craft_print::rgb::printers() { +fn printers(s: &mut Session, _: &Value) -> Result { + match rgb_printers(s) { Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::>()})), Err(craft_print::rgb::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})), Err(e) => Err(other(e)), @@ -1056,11 +1224,11 @@ fn printer_page(_: &mut Session, p: &Value) -> Result { if let Some(landscape) = landscape { let base = match settings { Some(s) => s, - None => craft_print::rgb::default_settings(&printer).map_err(other)?, + None => with_spooler(|| craft_print::rgb::default_settings(&printer)).map_err(other)?, }; settings = Some(turned_settings(&base, landscape)?); } - let pg = craft_print::rgb::page(&printer, settings.as_ref()).map_err(other)?; + let pg = with_spooler(|| craft_print::rgb::page(&printer, settings.as_ref())).map_err(other)?; let mut out = json!({"printer": printer, "paper": [pg.paper.0, pg.paper.1], "printable": [pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3], "dpi": [pg.dpi.0, pg.dpi.1]}); // The turned settings, to pass on as `printerSettings` (the Print dialog's Orientation does). if let (Some(_), Some(s), Some(o)) = (landscape, settings, out.as_object_mut()) { diff --git a/crates/engine/src/cmd/print_cmds/tests.rs b/crates/engine/src/cmd/print_cmds/tests.rs index ab2e104a48..6208b53ec5 100644 --- a/crates/engine/src/cmd/print_cmds/tests.rs +++ b/crates/engine/src/cmd/print_cmds/tests.rs @@ -258,17 +258,50 @@ fn native_rgb_route_preserves_pdf_only_color_and_page_content() { assert!(native_rgb_compatible(&page_of(1, vec![0; 2], false), "noColorManagement")); } +/// Two printers, the second the default, in place of the system spooler's. +fn test_printers() -> PrinterSource { + std::sync::Arc::new(|| { + vec![ + craft_print::Printer { name: "Studio".into(), default: false, two_sided: None, description: String::new() }, + craft_print::Printer { name: "Office".into(), default: true, two_sided: Some(true), description: String::new() }, + ] + }) +} + #[test] fn printer_commands_answer_without_a_document_and_reject_bad_settings() { let mut s = Session::new(); + s.printer_source = Some(test_printers()); let r = s.execute("file.print.printers", json!({})).unwrap(); assert_eq!(r["supported"], cfg!(windows)); - assert!(r["printers"].is_array()); + let listed = if cfg!(windows) { json!([{"name": "Studio", "default": false}, {"name": "Office", "default": true}]) } else { json!([]) }; + assert_eq!(r["printers"], listed); assert!(s.execute("file.print.page", json!({"printer": "Anything", "printerSettings": "zz"})).is_err()); let mut s = session("rgb", 8); assert!(s.execute("file.print", json!({"printer": "Anything", "printerSettings": "0102", "send": true})).is_err()); } +/// The GDI route lists a stand-in's printers only on Windows. Elsewhere it answers `Unsupported` +/// as the real route does, so the Print dialog shows the system's printers and paper controls. +#[test] +fn stand_in_printers_reach_the_gdi_route_on_windows_only() { + let source = test_printers(); + assert_eq!(stand_in_rgb_printers(&source, false), Err(craft_print::rgb::PrintError::Unsupported)); + let listed = stand_in_rgb_printers(&source, true).unwrap(); + assert_eq!(listed.iter().map(|p| (p.name.as_str(), p.default)).collect::>(), [("Studio", false), ("Office", true)]); +} + +/// `file.printers` lists a stand-in's printers everywhere but the browser. In the browser it lists +/// none, as the real command does there. +#[test] +fn stand_in_printers_reach_file_printers_outside_the_browser_only() { + let source = test_printers(); + assert!(stand_in_printers(Some(&source), true).is_none()); + let listed = stand_in_printers(Some(&source), false).map(|source| source()); + assert_eq!(listed.unwrap_or_default().iter().map(|p| (p.name.as_str(), p.default)).collect::>(), [("Studio", false), ("Office", true)]); + assert!(stand_in_printers(None, false).is_none()); +} + /// Synthetic driver settings: a DEVMODEW of `size` bytes plus `extra` private driver bytes, every /// byte a pattern so a stray write shows, with `dmFields` saying only the paper size is set. fn devmode(size: u16, extra: u16) -> Vec { @@ -676,13 +709,189 @@ fn disabled_printing_never_spools() { #[test] fn printers_lists_the_spooler_destinations_without_a_document() { let mut s = Session::new(); + s.printer_source = Some(test_printers()); let r = s.execute("file.printers", json!({})).unwrap(); - let list = r["printers"].as_array().unwrap(); - assert!(list.iter().all(|p| p["name"].is_string() && p["default"].is_boolean())); - match r["default"].as_str() { - Some(d) => assert!(list.iter().any(|p| p["name"] == d && p["default"] == true)), - None => assert!(list.iter().all(|p| p["default"] == false)), + assert_eq!(r["printers"], json!([{"name": "Studio", "default": false}, {"name": "Office", "default": true}])); + assert_eq!(r["default"], "Office"); + s.printer_source = Some(std::sync::Arc::new(Vec::new)); + let r = s.execute("file.printers", json!({})).unwrap(); + assert_eq!((r["printers"].clone(), r["default"].clone()), (json!([]), Value::Null)); +} + +/// Threads that list printers at the same time enter the spooler one at a time: the stand-in +/// printer list records how many threads were inside it at once. +#[test] +fn printer_listings_enter_the_spooler_one_thread_at_a_time() { + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::{Arc, Barrier}; + const THREADS: usize = 8; + let inside = Arc::new(AtomicUsize::new(0)); + let most = Arc::new(AtomicUsize::new(0)); + let start = Arc::new(Barrier::new(THREADS)); + let threads: Vec<_> = (0..THREADS) + .map(|i| { + let (inside, most, start) = (Arc::clone(&inside), Arc::clone(&most), Arc::clone(&start)); + std::thread::spawn(move || { + let mut s = Session::new(); + s.printer_source = Some(Arc::new(move || { + let now = inside.fetch_add(1, Ordering::SeqCst) + 1; + most.fetch_max(now, Ordering::SeqCst); + std::thread::sleep(std::time::Duration::from_millis(5)); + inside.fetch_sub(1, Ordering::SeqCst); + vec![craft_print::Printer { name: "Office".into(), default: true, two_sided: None, description: String::new() }] + })); + start.wait(); + for _ in 0..3 { + // `file.print.printers` lists the stand-in on Windows only. + let id = if i % 2 == 0 || !cfg!(windows) { "file.printers" } else { "file.print.printers" }; + assert_eq!(s.execute(id, json!({})).unwrap()["printers"][0]["name"], "Office"); + } + }) + }) + .collect(); + for t in threads { + t.join().unwrap(); } + assert_eq!(most.load(Ordering::SeqCst), 1, "two threads were in the printer listing at once"); +} + +/// A caller that panics inside the spooler leaves it usable for later callers. +#[test] +fn a_panic_inside_the_spooler_does_not_block_later_callers() { + let panicked = std::thread::spawn(|| with_spooler::<()>(|| panic!("a driver call panicked"))).join(); + assert!(panicked.is_err()); + assert_eq!(with_spooler(|| 7), 7); + let mut s = Session::new(); + s.printer_source = Some(test_printers()); + assert_eq!(s.execute("file.printers", json!({})).unwrap()["default"], "Office"); +} + +/// A driver call made while this thread already holds the spooler (a command run from the Print +/// dialog's `try_with_spooler`) runs at once. +#[test] +fn a_thread_holding_the_spooler_does_not_wait_for_itself() { + let (done, finished) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let mut s = Session::new(); + s.printer_source = Some(test_printers()); + // Inside one outer hold, so no other test takes the lock between the calls. + let answers = with_spooler(|| { + let nested = with_spooler(|| with_spooler(|| 7)); + let tried = try_with_spooler(|| with_spooler(|| try_with_spooler(|| 8))); + let listed = try_with_spooler(|| s.execute("file.printers", json!({})).unwrap()["default"].clone()); + (nested, tried, listed) + }); + done.send(answers).unwrap(); + }); + // Bounded, so a thread that waits for itself fails the test rather than hanging it. + let answers = finished.recv_timeout(std::time::Duration::from_secs(30)).expect("a thread holding the spooler waited for itself"); + assert_eq!(answers, (7, Some(Some(8)), Some(json!("Office")))); +} + +/// While another thread is in the spooler (a print job being handed over, Print Settings…), the +/// Print dialog's listings return at once with the last lists this process got. +#[test] +fn listings_without_waiting_return_the_last_lists_while_the_spooler_is_busy() { + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + use std::sync::{Arc, mpsc}; + let asked = Arc::new(AtomicUsize::new(0)); + let counted = Arc::clone(&asked); + let mut s = Session::new(); + s.printer_source = Some(Arc::new(move || { + counted.fetch_add(1, Ordering::SeqCst); + vec![craft_print::Printer { name: "Office".into(), default: true, two_sided: None, description: String::new() }] + })); + let (held_tx, held) = mpsc::channel(); + let (release, released_rx) = mpsc::channel::<()>(); + let released = Arc::new(AtomicBool::new(false)); + let holder = { + let released = Arc::clone(&released); + let mut lister = Session::new(); + lister.printer_source = s.printer_source.clone(); + std::thread::spawn(move || { + with_spooler(|| { + // Lists this stand-in's printers once with the lock held, so another test's listing + // cannot replace the last lists before the listings below. + held_tx.send((list_system_printers(&lister), list_rgb_printers(&lister))).unwrap(); + // Bounded, so a listing that waits fails the test rather than hanging it. + let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); + released.store(true, Ordering::SeqCst); + }); + }) + }; + let (kept, kept_rgb) = held.recv().unwrap(); + let before = asked.load(Ordering::SeqCst); + let listed = printers_without_waiting(&mut s); + let listed_rgb = rgb_printers_without_waiting(&s); + assert!(!released.load(Ordering::SeqCst), "the listing waited for the spooler"); + release.send(()).unwrap(); + holder.join().unwrap(); + assert_eq!(asked.load(Ordering::SeqCst), before, "a printer list was asked for while the spooler was busy"); + assert_eq!(listed, printers_answer(kept)); + assert_eq!(listed_rgb, kept_rgb); +} + +#[test] +fn listings_without_waiting_skip_the_lists_of_another_stand_in() { + uncached_listings_do_not_wait(true); +} + +#[test] +fn listings_without_waiting_return_empty_when_no_list_was_cached() { + uncached_listings_do_not_wait(false); +} + +fn uncached_listings_do_not_wait(cache_other: bool) { + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + use std::sync::{Arc, mpsc}; + let asked = Arc::new(AtomicUsize::new(0)); + let counted = Arc::clone(&asked); + let mut s = Session::new(); + s.printer_source = Some(Arc::new(move || { + counted.fetch_add(1, Ordering::SeqCst); + vec![craft_print::Printer { name: "Proofing".into(), default: true, two_sided: None, description: String::new() }] + })); + let (held_tx, held) = mpsc::channel(); + let (release, released_rx) = mpsc::channel(); + let released = Arc::new(AtomicBool::new(false)); + let done = Arc::clone(&released); + let holder = std::thread::spawn(move || { + with_spooler(|| { + if cache_other { + let mut other = Session::new(); + other.printer_source = Some(test_printers()); + list_system_printers(&other); + let _ = list_rgb_printers(&other); + } else { + *LAST_PRINTERS.lock().unwrap() = None; + *LAST_RGB_PRINTERS.lock().unwrap() = None; + } + held_tx.send(()).unwrap(); + let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); + done.store(true, Ordering::SeqCst); + }); + }); + held.recv().unwrap(); + let listed = printers_without_waiting(&mut s); + let listed_rgb = rgb_printers_without_waiting(&s); + let waited = released.load(Ordering::SeqCst); + let _ = release.send(()); + holder.join().unwrap(); + assert!(!waited, "an uncached listing waited for the spooler"); + assert_eq!(asked.load(Ordering::SeqCst), 0); + assert_eq!(listed, printers_answer(Vec::new())); + assert_eq!(listed_rgb, Err(craft_print::rgb::PrintError::Unsupported)); + with_spooler(|| { + let listed = printers_without_waiting(&mut s); + let listed_rgb = rgb_printers_without_waiting(&s); + assert_eq!(listed["printers"], json!([{"name": "Proofing", "default": true}])); + let names = listed_rgb.map(|list| list.into_iter().map(|p| p.name).collect::>()); + if cfg!(windows) { + assert_eq!(names, Ok(vec!["Proofing".to_string()])); + } else { + assert_eq!(names, Err(craft_print::rgb::PrintError::Unsupported)); + } + }); } /// Spooling tests run in a child process: `disable_printing` is process-wide and a test above diff --git a/crates/engine/src/cmd/smart_cmds/unpack.rs b/crates/engine/src/cmd/smart_cmds/unpack.rs index 0fdf3ac3fa..af7510da8d 100644 --- a/crates/engine/src/cmd/smart_cmds/unpack.rs +++ b/crates/engine/src/cmd/smart_cmds/unpack.rs @@ -161,7 +161,7 @@ fn unpacked(so: &Layer, mut layers: Vec) -> Layer { g.id = LayerId::fresh(); g.psd_id = None; g.blend = blend; - g.content = LayerContent::Group(Group { children: layers, expanded: true, artboard: None }); + g.content = LayerContent::Group(Group { children: layers, expanded: true, artboard: None, frame_stroke: None }); detach_psd(&mut g); g } diff --git a/crates/engine/src/cmd/web_cmds.rs b/crates/engine/src/cmd/web_cmds.rs index b28e7a94fb..470edd3067 100644 --- a/crates/engine/src/cmd/web_cmds.rs +++ b/crates/engine/src/cmd/web_cmds.rs @@ -65,7 +65,7 @@ pub fn specs() -> Vec { s.active().map(|_| ()).ok_or_else(|| "no document open".into()) } - /// Generate ▸ Image Assets needs Preferences ▸ Plug-ins ▸ Generator enabled, as in + /// Generate ▸ Image Assets needs Preferences ▸ Plug-ins ▸ Enable Generator, as in /// Photoshop (where "Enable Generator" is unchecked until turned on). fn generator_enabled(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; diff --git a/crates/engine/src/cmd/web_cmds/tests.rs b/crates/engine/src/cmd/web_cmds/tests.rs index 38fa82c188..cdc7d576e7 100644 --- a/crates/engine/src/cmd/web_cmds/tests.rs +++ b/crates/engine/src/cmd/web_cmds/tests.rs @@ -637,7 +637,7 @@ fn image_assets_needs_the_generator_preference() { fn image_assets_are_generated_now_and_on_save() { let dir = tmp("assets"); let mut s = session(8); - // Preferences ▸ Plug-ins ▸ Generator enabled: the feature is opt-in. + // Preferences ▸ Plug-ins ▸ Enable Generator: the feature is opt-in. s.execute("prefs.set", json!({"path": "plugIns.generatorEnabled", "value": true})).unwrap(); let id = s.active().unwrap().active_layer.unwrap(); s.execute("layer.renameLayer", json!({"layer": id.0, "name": "logo.png, 50% small/logo.gif, logo.jpg80%"})).unwrap(); @@ -661,7 +661,10 @@ fn image_assets_are_generated_now_and_on_save() { let s2 = session(8); let doc = s2.active().unwrap().doc.clone(); let mut d = (*doc).clone(); - d.layers.push(Layer::new("default 200% @2x", LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None }))); + d.layers.push(Layer::new( + "default 200% @2x", + LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None, frame_stroke: None }), + )); let lid = d.layers[1].id; d.layer_mut(lid).unwrap().name = "icon.png".into(); let (files, errors) = generate_assets(&d, &dir); @@ -684,7 +687,7 @@ fn image_assets_keep_full_artboards() { logo.surface_mut().unwrap().fill_rect(Rect::new(12, 10, 20, 16), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); let board = photocraft_doc::Artboard { rect: Rect::new(4, 6, 44, 36), background: photocraft_doc::ArtboardBackground::Transparent, preset: String::new() }; - let group = photocraft_doc::Group { children: vec![logo], expanded: true, artboard: Some(board) }; + let group = photocraft_doc::Group { children: vec![logo], expanded: true, artboard: Some(board), frame_stroke: None }; doc.layers = vec![Layer::new("board.png", LayerContent::Group(group))]; Ok(()) }) @@ -722,8 +725,8 @@ fn image_assets_refuse_empty_artboards() { background: photocraft_doc::ArtboardBackground::Transparent, preset: String::new(), }; - let empty = photocraft_doc::Group { children: vec![], expanded: true, artboard: Some(board(0)) }; - let off = photocraft_doc::Group { children: vec![outside], expanded: true, artboard: Some(board(24)) }; + let empty = photocraft_doc::Group { children: vec![], expanded: true, artboard: Some(board(0)), frame_stroke: None }; + let off = photocraft_doc::Group { children: vec![outside], expanded: true, artboard: Some(board(24)), frame_stroke: None }; doc.layers = vec![Layer::new("empty.png", LayerContent::Group(empty)), Layer::new("off.png", LayerContent::Group(off))]; Ok(()) }) @@ -770,8 +773,10 @@ fn image_assets_never_write_one_file_twice() { let dir = tmp("asset-variant-collisions"); d.layer_mut(top).unwrap().name = "icon.png".into(); d.layer_mut(logo).unwrap().name = "Logo".into(); - d.layers - .push(Layer::new("default 50% + 200% + 200% @2x", LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None }))); + d.layers.push(Layer::new( + "default 50% + 200% + 200% @2x", + LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None, frame_stroke: None }), + )); let (files, errors) = generate_assets(&d, &dir); let names: Vec<_> = files.iter().map(|p| std::path::Path::new(p).file_name().unwrap().to_string_lossy().into_owned()).collect(); assert_eq!(names, ["icon.png", "icon@2x.png"]); @@ -799,7 +804,7 @@ fn image_assets_reject_traversal_from_names_and_defaults() { d.layer_mut(id).unwrap().name = "safe.png".into(); d.layers.push(Layer::new( "default ../ + /outside/ + ..\\bad/ + C:\\drive/ + 50% nested/", - LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None }), + LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None, frame_stroke: None }), )); let (files, errors) = generate_assets(&d, &dir); assert_eq!( diff --git a/crates/engine/src/inspect.rs b/crates/engine/src/inspect.rs index e4c93c7cbc..dc681b7d8e 100644 --- a/crates/engine/src/inspect.rs +++ b/crates/engine/src/inspect.rs @@ -21,6 +21,8 @@ pub fn session(s: &Session) -> Value { })).collect::>(), "foreground": s.tools.foreground, "background": s.tools.background, + // History panel › History Options… (`history.options`). + "historyOptions": crate::history_options_cmds::current(s), }) } diff --git a/crates/engine/src/kys.rs b/crates/engine/src/kys.rs index 6a0ff99c84..643ed46057 100644 --- a/crates/engine/src/kys.rs +++ b/crates/engine/src/kys.rs @@ -56,7 +56,7 @@ pub fn parse(text: &str) -> Result { return Err("not a keyboard shortcut set (.kys): larger than 4 MiB".into()); } let text = text.trim_start_matches('\u{feff}'); - let doc = roxmltree::Document::parse(text).map_err(|e| format!("not a keyboard shortcut set (.kys): {e}"))?; + let doc = photocraft_io::xml::parse(text, roxmltree::ParsingOptions::default()).map_err(|e| format!("not a keyboard shortcut set (.kys): {e}"))?; let root = doc.root_element(); if root.tag_name().name() != "photoshop-keyboard-shortcuts" { return Err("not a keyboard shortcut set (.kys): no root".into()); @@ -342,6 +342,15 @@ mod tests { assert_eq!(p.alternate_keys.get("image.levels"), Some(&Vec::new())); } + /// A set nested 20 000 deep overflowed the XML parser's recursion (an abort nothing can + /// catch); it is an error now. The small stack makes a regression abort instead of passing. + #[test] + fn deeply_nested_sets_are_an_error_not_a_stack_overflow() { + let xml = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || parse(&xml).err()).unwrap().join().unwrap(); + assert!(e.as_deref().is_some_and(|m| m.contains("nested")), "{e:?}"); + } + #[test] fn a_row_with_the_default_and_a_function_key_keeps_both() { let xml = diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 93e91922b6..e94e5fcd90 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -96,6 +96,7 @@ pub use cmd::gradient_fill_cmds; pub use cmd::group_view_cmds; pub use cmd::history_cmds; pub use cmd::history_menu_cmds; +pub use cmd::history_options_cmds; pub use cmd::history_select_cmds; pub use cmd::history_source_cmds; pub use cmd::image_cmds; @@ -160,9 +161,9 @@ pub use cmd::web_cmds; use cmd::wia_cmds; pub use doc_state::{CopiedStyle, DocState, PRECHECKED, ToolState}; pub use error::{EngineError, Result}; -pub(crate) use session::fix_selection; pub use session::move_item; use session::restore_target; +pub(crate) use session::{carry_visibility, fix_selection, visibility_changes}; /// The pixels of a raster layer (typically one a command just created), as an error instead /// of a panic if the layer has none. @@ -241,6 +242,9 @@ pub struct Session { /// A further route for File › Print when neither `lp` nor a CUPS server over IPP is /// reachable (the XDG Print portal in a Flatpak); see `print_cmds::Spooler`. pub print_spooler: Option, + /// The printers `file.printers`, `file.print.printers` and the Print dialog list in place of + /// the system's (tests); see `print_cmds::PrinterSource`. `None` asks the spooler. + pub printer_source: Option, /// Background jobs (see [`jobs`]). jobs: jobs::Jobs, /// Color Sampler points dropped by the last canvas-size change (crop, Canvas Size, Image Size, diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 24cba73d4d..22f82002b7 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -12,8 +12,9 @@ //! OS config directory; the web shell: browser storage) and restore it with //! [`Session::load_prefs_json`]. //! -//! Engine-side values are applied by [`Session::apply_prefs`]: the history state limit (every -//! open document), the effect-cache memory budget, and units for commands that report lengths. +//! Engine-side values are applied by [`Session::apply_prefs`]: the history state limit and +//! History Options › Allow Non-Linear History (every open document), the effect-cache memory +//! budget, and units for commands that report lengths. //! Colour settings (`colorSettings`) are stored with the colour state //! ([`crate::color_cmds::ColorSettings`]) and persisted in the same document. @@ -998,6 +999,24 @@ impl Default for Integrations { } } +/// The History panel's History Options… (not part of Edit › Preferences in Photoshop either). +/// Photoshop's snapshot options come with #3634. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct HistoryOptions { + /// Allow Non-Linear History: editing after selecting an earlier state keeps the later + /// states (the new step is appended at the end), and deleting a state deletes only it. + pub allow_non_linear_history: bool, + /// Make Layer Visibility Changes Undoable: showing or hiding a layer is a history state. + pub make_layer_visibility_changes_undoable: bool, +} + +impl Default for HistoryOptions { + fn default() -> Self { + Self { allow_non_linear_history: false, make_layer_visibility_changes_undoable: true } + } +} + /// Edit › Menus: hidden items and item colours, by command id. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] @@ -1083,6 +1102,8 @@ pub struct Preferences { pub enhanced_controls: EnhancedControls, pub raw_defaults: RawDefaults, pub integrations: Integrations, + /// History panel › History Options… (`history.options`). + pub history_options: HistoryOptions, /// Edit › Keyboard Shortcuts: command id → shortcut (`Cmd+Shift+N` notation); an empty /// string removes the default shortcut. pub shortcuts: BTreeMap, @@ -1268,15 +1289,18 @@ impl Session { r } - /// Push engine-relevant preferences into live state: every document's history limit, the - /// transform and layer-effect cache budgets and the text engine's missing-glyph protection. + /// Push engine-relevant preferences into live state: every document's history limit and + /// linearity, the transform and layer-effect cache budgets and the text engine's + /// missing-glyph protection. pub fn apply_prefs(&mut self) { let n = self.prefs().performance.history_states.max(1) as usize; let bytes = self.prefs().performance.history_budget_bytes(); let budget = self.prefs().performance.effect_cache_mb as usize; + let non_linear = self.prefs().history_options.allow_non_linear_history; for st in &mut self.docs { st.history.max_states = n; st.history.max_bytes = bytes; + st.history.non_linear = non_linear; st.trim_history(); } photocraft_compose::set_effect_cache_budget(budget << 20); diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs index 18eb3dc80a..90b7883f0b 100644 --- a/crates/engine/src/session.rs +++ b/crates/engine/src/session.rs @@ -8,4 +8,4 @@ mod history; mod layers; pub use documents::move_item; -pub(crate) use history::{fix_selection, restore_target}; +pub(crate) use history::{carry_visibility, fix_selection, restore_target, visibility_changes}; diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index ea20944fbc..668bb36e07 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -109,6 +109,7 @@ impl Session { let mut st = DocState::new(doc, path); st.history.max_states = self.prefs.get().performance.history_states.max(1) as usize; st.history.max_bytes = self.prefs.get().performance.history_budget_bytes(); + st.history.non_linear = self.prefs.get().history_options.allow_non_linear_history; self.docs.push(st); let i = self.docs.len() - 1; self.active = Some(i); diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs index c3822673bb..25f0b9f7f7 100644 --- a/crates/engine/src/session/execute.rs +++ b/crates/engine/src/session/execute.rs @@ -163,6 +163,25 @@ impl Session { /// Apply an undoable edit to the active document. pub fn edit(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { + self.edit_with(label, true, f) + } + + /// Apply a change of layer visibility (and nothing else): a history state when History + /// Options › Make Layer Visibility Changes Undoable is on (the default, as in Photoshop), + /// otherwise the document changes without one, so the History panel and Undo skip it. + pub fn edit_visibility(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { + let record = self.visibility_undoable(); + self.edit_with(label, record, f) + } + + /// History Options › Make Layer Visibility Changes Undoable. + pub fn visibility_undoable(&self) -> bool { + self.prefs().history_options.make_layer_visibility_changes_undoable + } + + /// [`Self::edit`], recording a history state only when `record` is set. An unrecorded change + /// still marks the document modified and leaves the redo states alone. + pub(crate) fn edit_with(&mut self, label: &str, record: bool, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { // Every mutation path (linked-file refresh, transform, place) re-renders smart objects // through this entry; the requesting session's authority must reach those renders. let _authorization = crate::jobs::render_authorization_scope(self.authorize); @@ -223,14 +242,20 @@ impl Session { let key = self.coalesce_request.clone(); let st = self.active_mut().ok_or(EngineError::NoDocument)?; let layers = st.layer_target(); - if key.is_none() || st.coalesce != key || !st.history.can_undo() { + if !record { + // Nothing to coalesce with: the next recorded step starts a state of its own. + st.coalesce = None; + } else if key.is_none() || st.coalesce != key || !st.history.can_undo() { st.history.set_current_layers(prior); - st.history.record(label, before, layers); + st.history.record(label, before.clone(), layers); + st.history.add_current_visibility(&visibility_changes(&before, &st.doc)); st.trim_history(); + st.coalesce = key; } else { st.history.set_current_layers(layers); + st.history.add_current_visibility(&visibility_changes(&before, &st.doc)); + st.coalesce = key; } - st.coalesce = key; st.bump_revision(keep_clean); st.last_damage = damage; self.samplers_removed += samplers_removed; diff --git a/crates/engine/src/session/history.rs b/crates/engine/src/session/history.rs index 0d619f47e9..a4e91eaf6a 100644 --- a/crates/engine/src/session/history.rs +++ b/crates/engine/src/session/history.rs @@ -7,8 +7,11 @@ impl Session { if self.active_job().is_some() { return false; } + let keep_visibility = !self.visibility_undoable(); let Some(st) = self.active_mut() else { return false }; st.coalesce = None; + // The step being undone: the layers whose visibility it changed get it back. + let changed = st.history.current_visibility().to_vec(); match st.history.undo(st.doc.clone()) { // Undo Clear History: the list comes back, the document is the same one. Not an // edit: keep a clean document clean (the revision still moves for the panels). @@ -27,6 +30,9 @@ impl Session { if d.name != st.doc.name { Arc::make_mut(&mut d).name.clone_from(&st.doc.name); } + if keep_visibility { + carry_visibility(&st.doc, &mut d, &changed); + } // Pixels this step can have touched, so the canvas recomposites only that // (it recomposited everything before). let damage = layer_multi_cmds::step_damage(&d, &st.doc); @@ -45,6 +51,7 @@ impl Session { if self.active_job().is_some() { return false; } + let keep_visibility = !self.visibility_undoable(); let Some(st) = self.active_mut() else { return false }; st.coalesce = None; match st.history.redo(st.doc.clone()) { @@ -52,6 +59,11 @@ impl Session { if d.name != st.doc.name { Arc::make_mut(&mut d).name.clone_from(&st.doc.name); } + if keep_visibility { + // The step being redone sets the visibility it changed again. + let changed = st.history.current_visibility().to_vec(); + carry_visibility(&st.doc, &mut d, &changed); + } let damage = layer_multi_cmds::step_damage(&st.doc, &d); let before = std::mem::replace(&mut st.doc, d); let keep_clean = st.keeps_clean(&before); @@ -65,6 +77,79 @@ impl Session { } } +impl Session { + /// Take back the step a compound command just recorded, after its later part failed: the + /// document before the command is current again, nothing is left to redo, and in non-linear + /// history the later states that step moved stay where they were (an undo would land on the + /// last of them). Falls back to Undo plus forgetting the redo states when the history + /// changed since that step. Returns whether a step was taken back. + pub fn rollback(&mut self) -> bool { + if self.active_job().is_some() { + return false; + } + let keep_visibility = !self.visibility_undoable(); + let Some(st) = self.active_mut() else { return false }; + st.coalesce = None; + // The step taken back (and the later states it moved, in non-linear history): the layers + // whose visibility it changed get it back, the unrecorded changes stay, as for Undo. + let changed = st.history.current_visibility().to_vec(); + let Some((mut d, layers)) = st.history.rollback() else { + let undone = self.undo(); + if undone && let Some(st) = self.active_mut() { + st.history.clear_redo(); + } + return undone; + }; + if d.name != st.doc.name { + Arc::make_mut(&mut d).name.clone_from(&st.doc.name); + } + if keep_visibility { + carry_visibility(&st.doc, &mut d, &changed); + } + let damage = layer_multi_cmds::step_damage(&d, &st.doc); + let before = std::mem::replace(&mut st.doc, d); + // Taking back a selection-only step keeps a clean document clean, as Undo does (#6865). + let keep_clean = st.keeps_clean(&before); + restore_target(st, layers); + st.bump_revision(keep_clean); + st.last_damage = damage; + true + } +} + +/// History Options › Make Layer Visibility Changes Undoable off: showing or hiding a layer is not +/// a history state, so stepping through history must not bring the old visibility back either +/// (history states hold whole documents). Gives every layer of `to` that `from` also has the +/// visibility it has in `from`, except the `stepped` layers: a recorded step between the two +/// states changed their visibility (Apply Layer Comp, a snapshot, a visibility change recorded +/// while the option was on), so they keep the visibility `to` holds. Layers only one of them has +/// keep theirs. Copies `to` only when something differs. +pub(crate) fn carry_visibility(from: &Document, to: &mut Arc, stepped: &[LayerId]) { + let visible: std::collections::HashMap = from.walk().into_iter().map(|(_, _, l)| (l.id, l.visible)).collect(); + let changes: Vec<(LayerId, bool)> = to + .walk() + .into_iter() + .filter(|(_, _, l)| !stepped.contains(&l.id)) + .filter_map(|(_, _, l)| visible.get(&l.id).filter(|v| **v != l.visible).map(|v| (l.id, *v))) + .collect(); + if changes.is_empty() { + return; + } + let d = Arc::make_mut(to); + for (id, v) in changes { + if let Some(l) = d.layer_mut(id) { + l.visible = v; + } + } +} + +/// The layers (in both documents) whose visibility differs between `before` and `after`: what a +/// recorded step changed, for [`photocraft_ops::History::add_current_visibility`]. +pub(crate) fn visibility_changes(before: &Document, after: &Document) -> Vec { + let visible: std::collections::HashMap = before.walk().into_iter().map(|(_, _, l)| (l.id, l.visible)).collect(); + after.walk().into_iter().filter(|(_, _, l)| visible.get(&l.id).is_some_and(|v| *v != l.visible)).map(|(_, _, l)| l.id).collect() +} + /// Undo / redo: target the layers the restored state targeted, as far as they still exist. pub(crate) fn restore_target(st: &mut DocState, layers: LayerTarget) { // Deselect Layers is a valid history target, not a missing layer to replace with the top diff --git a/crates/engine/tests/panic_hunt.rs b/crates/engine/tests/panic_hunt.rs index 91f42be306..c4d8359e18 100644 --- a/crates/engine/tests/panic_hunt.rs +++ b/crates/engine/tests/panic_hunt.rs @@ -13,6 +13,8 @@ fn fresh() -> Session { // Belt and braces with the EXTERNAL skip below: nothing in this process may reach a printer. photocraft_engine::print_cmds::disable_printing(); let mut s = Session::new(); + // `file.printers` and `file.print.printers` list no printers and do not ask the spooler. + s.printer_source = Some(std::sync::Arc::new(Vec::new)); s.execute("file.new", json!({"width": 24, "height": 16})).unwrap(); s.execute("layer.new.layer", json!({})).unwrap(); s.execute("select.rect", json!({"x": 1, "y": 1, "width": 6, "height": 5})).unwrap(); diff --git a/crates/format/src/autosave.rs b/crates/format/src/autosave.rs index c5ba725edc..7b379451dd 100644 --- a/crates/format/src/autosave.rs +++ b/crates/format/src/autosave.rs @@ -180,38 +180,52 @@ fn worker(rx: Receiver, bundle: PathBuf, sidecar: PathBuf, last: Arc.json` does not exist: a listing in between took the entry for a sidecar-less +/// bundle and listed it twice, or not at all (#9034). A rename swaps the name in one step, and +/// recovery data is private to the app, so there is no metadata to keep. fn write_sidecar(path: &Path, bytes: &[u8]) -> Result<()> { - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt; - struct PrivateFs; - impl crate::atomic::AtomicFs for PrivateFs { - fn write_new(&self, tmp: &Path, bytes: &[u8]) -> std::io::Result<()> { + struct SidecarFs; + impl crate::atomic::AtomicFs for SidecarFs { + // As before: Windows refuses a read-only sidecar up front, Unix replaces it. + #[cfg(not(unix))] + fn is_readonly(&self, path: &Path) -> bool { + crate::atomic::RealFs.is_readonly(path) + } + fn write_new(&self, tmp: &Path, bytes: &[u8]) -> std::io::Result<()> { + #[cfg(unix)] + { use std::io::Write; + use std::os::unix::fs::OpenOptionsExt; let mut f = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(tmp)?; f.write_all(bytes)?; crate::atomic::sync_file(&f) } - fn rename(&self, from: &Path, to: &Path) -> std::io::Result<()> { - crate::atomic::RealFs.rename(from, to) - } - fn remove(&self, path: &Path) -> std::io::Result<()> { - crate::atomic::RealFs.remove(path) - } - fn sync_dir(&self, dir: &Path) -> std::io::Result<()> { - crate::atomic::RealFs.sync_dir(dir) - } - fn copy_metadata(&self, from: &Path, to: &Path) -> std::io::Result<()> { - crate::atomic::RealFs.copy_metadata(from, to) + #[cfg(not(unix))] + { + crate::atomic::RealFs.write_new(tmp, bytes) } } - crate::atomic::atomic_write_with(&PrivateFs, crate::atomic::RenameRetry::platform(), path, bytes)?; - Ok(()) - } - #[cfg(not(unix))] - { - crate::store::write_atomic(path, bytes) + fn rename(&self, from: &Path, to: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.rename(from, to) + } + // `replace_existing` keeps the trait's default: this same one-step rename. + fn remove(&self, path: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.remove(path) + } + fn sync_dir(&self, dir: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.sync_dir(dir) + } + fn copy_metadata(&self, from: &Path, to: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.copy_metadata(from, to) + } } + crate::atomic::atomic_write_with(&SidecarFs, crate::atomic::RenameRetry::platform(), path, bytes)?; + Ok(()) } fn read_sidecar_bounded(path: &Path, max: usize) -> Option> { diff --git a/crates/format/src/convert.rs b/crates/format/src/convert.rs index 65f06fd8e2..8b26f17940 100644 --- a/crates/format/src/convert.rs +++ b/crates/format/src/convert.rs @@ -125,9 +125,12 @@ fn video_m(v: &photocraft_doc::VideoData, sink: &mut dyn Sink) -> crate::manifes fn layer_m(l: &Layer, sink: &mut dyn Sink) -> LayerM { let content = match &l.content { LayerContent::Raster(s) => ContentM::Raster { surface: surface_m(s, sink) }, - LayerContent::Group(g) => { - ContentM::Group { children: g.children.iter().map(|c| layer_m(c, sink)).collect(), expanded: g.expanded, artboard: g.artboard.clone() } - } + LayerContent::Group(g) => ContentM::Group { + children: g.children.iter().map(|c| layer_m(c, sink)).collect(), + expanded: g.expanded, + artboard: g.artboard.clone(), + frame_stroke: g.frame_stroke.clone(), + }, LayerContent::Adjustment(a) => ContentM::Adjustment { adjustment: a.clone() }, LayerContent::Fill(f) => ContentM::Fill { fill: f.clone() }, LayerContent::Text(t) => ContentM::Text { @@ -449,7 +452,9 @@ impl Loader<'_> { fn layer_with(&mut self, m: &LayerM, children: Vec) -> Result { let content = match &m.content { ContentM::Raster { surface } => LayerContent::Raster(self.surface(surface)?), - ContentM::Group { expanded, artboard, .. } => LayerContent::Group(Group { artboard: artboard.clone(), children, expanded: *expanded }), + ContentM::Group { expanded, artboard, frame_stroke, .. } => { + LayerContent::Group(Group { artboard: artboard.clone(), children, expanded: *expanded, frame_stroke: frame_stroke.clone() }) + } ContentM::Adjustment { adjustment } => LayerContent::Adjustment(adjustment.clone()), ContentM::Fill { fill } => LayerContent::Fill(fill.clone()), ContentM::Text { text, font_family, size_pt, color, transform, cache, psd_raw, runs, paragraphs, shape, orientation, antialias, warp } => { diff --git a/crates/format/src/manifest.rs b/crates/format/src/manifest.rs index 72192c79f6..0b1e08b706 100644 --- a/crates/format/src/manifest.rs +++ b/crates/format/src/manifest.rs @@ -259,6 +259,9 @@ pub enum ContentM { /// Artboard (Layer › New › Artboard). #[serde(default)] artboard: Option, + /// A Photoshop Frame's stroke, drawn along the group's vector mask. + #[serde(default, skip_serializing_if = "Option::is_none")] + frame_stroke: Option, }, Adjustment { adjustment: Adjustment, diff --git a/crates/format/tests/it/autosave.rs b/crates/format/tests/it/autosave.rs index 24d61d50d3..26be1c9d76 100644 --- a/crates/format/tests/it/autosave.rs +++ b/crates/format/tests/it/autosave.rs @@ -304,3 +304,54 @@ fn failed_cleanup_after_commit_does_not_fail_the_save() { assert_eq!(load_path(&dir).unwrap(), doc); std::fs::remove_dir_all(dir.parent().unwrap()).unwrap(); } + +/// #9034: while a document's autosave replaces its entry, a listing (another launch's recovery +/// prompt, or the launch's own check after closing a second document) sees that entry exactly +/// once. On Windows the sidecar used to be replaced through `ReplaceFileW`, which moves the old +/// sidecar to a backup name before moving the new one in: a listing in between found the bundle +/// without a sidecar and listed it a second time (or, the other way round, not at all). +#[test] +fn a_listing_during_an_autosave_sees_the_entry_exactly_once() { + let dir = temp_dir("listing-during-autosave"); + let doc = Document::new("Busy", photocraft_doc::Size { width: 4, height: 4 }, ColorMode::Rgb, SampleType::U8); + save_once(&dir, "busy", doc.clone()); + let done = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let writer = { + let (dir, done) = (dir.clone(), done.clone()); + std::thread::spawn(move || { + // Sets `done` however the thread ends, so a failing writer stops the listing loop + // and `join` reports its panic instead of the test hanging. + struct SetOnDrop(Arc); + impl Drop for SetOnDrop { + fn drop(&mut self) { + self.0.store(true, std::sync::atomic::Ordering::SeqCst); + } + } + let _done = SetOnDrop(done); + let saver = Autosaver::new(&dir, "busy"); + for revision in 2..=200 { + saver.request(Arc::new(doc.clone()), revision, None, SaveOptions::default()); + // Wait for this write, so every revision replaces the sidecar once. + let completed = loop { + let completed = saver.take_completed(); + if !completed.is_empty() { + break completed; + } + std::thread::yield_now(); + }; + assert!(completed.iter().all(|(_, result)| result.is_ok()), "revision {revision}: {completed:?}"); + } + saver.flush() + }) + }; + let mut listings = 0u32; + while !done.load(std::sync::atomic::Ordering::SeqCst) { + let entries = list_recovery(&dir); + let keys: Vec<&str> = entries.iter().map(|e| e.info.key.as_str()).collect(); + assert_eq!(keys, ["busy"], "listing {listings} during an autosave: {entries:?}"); + listings += 1; + } + assert!(writer.join().unwrap().is_some_and(|r| r.is_ok())); + assert!(listings > 0); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/format/tests/it/frame_stroke.rs b/crates/format/tests/it/frame_stroke.rs new file mode 100644 index 0000000000..89567ee7f0 --- /dev/null +++ b/crates/format/tests/it/frame_stroke.rs @@ -0,0 +1,37 @@ +//! A Photoshop Frame's stroke (`Group::frame_stroke`) survives a `.pcraft` round trip; older +//! manifests, written before it existed, load without one (#4381). + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Fill, Layer, LayerContent, ShapeStroke, Size, StrokeAlign}; +use photocraft_format::*; + +fn stroke_of(d: &Document) -> Option { + match &d.layers.first()?.content { + LayerContent::Group(g) => g.frame_stroke.clone(), + _ => None, + } +} + +#[test] +fn frame_stroke_roundtrips_and_defaults_when_absent() { + let mut doc = Document::new("a", Size::new(40, 30), ColorMode::Rgb, SampleType::U8); + let stroke = + ShapeStroke { width: 8.0, align: StrokeAlign::Inside, paint: Fill::Solid(Color::rgb(1.0, 0.0, 0.0)), dashes: vec![2.0, 1.0], ..ShapeStroke::default() }; + let mut g = Layer::group("Frame 1", vec![]); + if let LayerContent::Group(gr) = &mut g.content { + gr.frame_stroke = Some(stroke.clone()); + } + doc.layers.push(g); + let bytes = save_to_bytes(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(stroke_of(&load_from_bytes(&bytes).unwrap()), Some(stroke)); + + // A manifest without the key (older files) loads with no stroke. + let m = read_manifest(&bytes).unwrap(); + let mut v = serde_json::to_value(&m.document).unwrap(); + let content = &mut v["layers"][0]["content"]; + assert!(content.get("frame_stroke").is_some()); + content.as_object_mut().unwrap().remove("frame_stroke"); + let old: photocraft_format::manifest::DocM = serde_json::from_value(v).unwrap(); + let photocraft_format::manifest::ContentM::Group { frame_stroke, .. } = &old.layers[0].content else { panic!("not a group") }; + assert!(frame_stroke.is_none()); +} diff --git a/crates/format/tests/it/main.rs b/crates/format/tests/it/main.rs index 6c822f4282..71eae66ddd 100644 --- a/crates/format/tests/it/main.rs +++ b/crates/format/tests/it/main.rs @@ -6,6 +6,7 @@ mod common; mod corruption; mod directory_symlinks; mod float_roundtrip; +mod frame_stroke; mod layer_labels; mod roundtrip; mod smart_contents; diff --git a/crates/format/tests/it/roundtrip.rs b/crates/format/tests/it/roundtrip.rs index 06d3928fd1..31f2561619 100644 --- a/crates/format/tests/it/roundtrip.rs +++ b/crates/format/tests/it/roundtrip.rs @@ -884,6 +884,25 @@ fn variables_follow_layer_id_remapping() { assert!(back.walk().into_iter().all(|(_, _, l)| l.id != gone), "dangling target must not address a live layer"); } +#[test] +fn color_lookup_lut_file_round_trips_and_old_files_load() { + use photocraft_doc::Adjustment; + use photocraft_doc::adjust::{LutFormat, LutSource}; + let lut = Some(std::sync::Arc::new(vec![0.5f32; 24])); + let source = Some(LutSource { format: LutFormat::ThreeDl, bytes: std::sync::Arc::new(b"0 1023\n0 0 0\n".to_vec()) }); + let adj = Adjustment::ColorLookup { name: "Film.3dl".into(), lut, size: 2, tetrahedral: false, dither: false, source }; + let mut doc = Document::new("d", photocraft_doc::Size::new(4, 4), ColorMode::Rgb, SampleType::U8); + doc.insert_above(None, Layer::new("lookup", LayerContent::Adjustment(adj.clone()))); + let back = load_from_bytes(&save_to_bytes(&doc, &SaveOptions::default()).unwrap()).unwrap(); + assert_eq!(back, doc, "the LUT file survives a .pcraft round trip (#6544)"); + // A .pcraft written before the field existed has no `source`: it loads as None. + let mut v = serde_json::to_value(&adj).unwrap(); + let removed = v.as_object_mut().and_then(|o| o.values_mut().next()).and_then(|f| f.as_object_mut()).and_then(|f| f.remove("source")); + assert!(removed.is_some(), "{v}"); + let Adjustment::ColorLookup { source: None, name, .. } = serde_json::from_value(v).unwrap() else { panic!("old lookup must load") }; + assert_eq!(name, "Film.3dl"); +} + /// Removing previews is post-commit housekeeping, not part of publishing the new document. #[test] fn directory_save_without_previews_removes_stale_files_after_commit() { diff --git a/crates/gpu/src/plan/planner.rs b/crates/gpu/src/plan/planner.rs index 577237e975..27d7bf589a 100644 --- a/crates/gpu/src/plan/planner.rs +++ b/crates/gpu/src/plan/planner.rs @@ -374,6 +374,15 @@ impl<'a> Planner<'a> { p.mask = self.mask_use(layer); after = self.emit(p); } + // A Frame's stroke over the frame's content, at the group's opacity (compose). + if let Some(stroke) = photocraft_compose::shape_split::frame_stroke(layer, self.cx.canvas, self.cx.depth) { + let stroke = self.shape_part(layer, Role::Stroke, stroke); + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(after); + p.b = Some(stroke); + p.opacity = opacity; + after = self.emit(p); + } if visible_clipped.is_empty() { self.release(before); return Ok(after); @@ -554,12 +563,20 @@ impl<'a> Planner<'a> { match &layer.content { LayerContent::Group(g) => { let empty = self.clear(); - let s = self.stack(&g.children, empty)?; + let mut s = self.stack(&g.children, empty)?; if layer.mask.is_some() || layer.vector_mask.is_some() { let mut p = Pass::new(Kernel::Mask, 0); p.a = Some(s); p.mask = self.mask_use(layer); - return Ok(self.emit(p)); + s = self.emit(p); + } + // A Frame's stroke over its masked content (compose::render_content). + if let Some(stroke) = photocraft_compose::shape_split::frame_stroke(layer, self.cx.canvas, self.cx.depth) { + let stroke = self.shape_part(layer, Role::Stroke, stroke); + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(s); + p.b = Some(stroke); + s = self.emit(p); } Ok(s) } diff --git a/crates/gpu/src/plan/tests.rs b/crates/gpu/src/plan/tests.rs index 283fe034de..83f286bf2b 100644 --- a/crates/gpu/src/plan/tests.rs +++ b/crates/gpu/src/plan/tests.rs @@ -108,7 +108,8 @@ fn outer_glow_plan_knocks_out_see_through_fill() { #[test] fn color_lookup_with_an_overflowing_stored_size_uploads_no_table() { for size in [1 << 22, u32::MAX] { - let a = Adjustment::ColorLookup { name: "x".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size, tetrahedral: false, dither: false }; + let a = + Adjustment::ColorLookup { name: "x".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size, tetrahedral: false, dither: false, source: None }; let (kind, _, rows) = adjustment_program(&a, Transfer::Srgb, SampleType::U8, photocraft_color::ColorMode::Rgb); assert_eq!((kind, rows.is_none()), (0, true), "size {size}"); } diff --git a/crates/gpu/tests/it/parity.rs b/crates/gpu/tests/it/parity.rs index 14a3e62243..901a21365b 100644 --- a/crates/gpu/tests/it/parity.rs +++ b/crates/gpu/tests/it/parity.rs @@ -715,7 +715,7 @@ fn lookup(n: usize, tetrahedral: bool, dither: bool) -> Adjustment { } } } - Adjustment::ColorLookup { name: "test".into(), lut: Some(std::sync::Arc::new(t)), size: n as u32, tetrahedral, dither } + Adjustment::ColorLookup { name: "test".into(), lut: Some(std::sync::Arc::new(t)), size: n as u32, tetrahedral, dither, source: None } } /// 32-bit Hue/Saturation on colours at and above HSL lightness 1 and with a negative channel, @@ -2482,6 +2482,36 @@ fn stroked_shapes_with_clipped_layers() { } } +/// A Photoshop Frame's stroke (#4381): along the group's vector mask, above its masked content, +/// for an empty frame and one holding a layer, pass-through and isolated. +#[test] +fn frame_strokes() { + let Some(mut g) = gpu() else { return }; + use photocraft_doc::vector::{Path, ShapeStroke, StrokeAlign, Subpath, VectorMask}; + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for (blend, align, content) in [ + (BlendMode::PassThrough, StrokeAlign::Inside, false), + (BlendMode::PassThrough, StrokeAlign::Outside, true), + (BlendMode::Multiply, StrokeAlign::Center, true), + ] { + let mut d = Document::new("t", Size::new(80, 64), ColorMode::Rgb, depth); + d.layers.push(noise_layer("bg", d.pixel_format(), Rect::from_xywh(0, 0, 80, 64), 1, 0.3)); + let kids = if content { vec![noise_layer("c", d.pixel_format(), Rect::from_xywh(0, 0, 80, 64), 7, 0.4)] } else { vec![] }; + let mut l = Layer::group("Frame 1", kids); + l.blend = blend; + l.opacity = 0.8; + l.vector_mask = Some(VectorMask::new(Path::new(vec![Subpath::polygon(&[(10.3, 8.6), (66.2, 12.1), (58.7, 54.4), (16.9, 48.2)])]))); + if let LayerContent::Group(gr) = &mut l.content { + gr.frame_stroke = Some(ShapeStroke { width: 5.0, align, paint: Fill::Solid(Color::rgb(0.9, 0.1, 0.1)), ..Default::default() }); + } + d.layers.push(l); + let st = photocraft_gpu::render_to_vec_stats(&mut g.comp, &g.device, &g.queue, &d, d.bounds()); + assert!(st.is_ok(), "planned on the GPU"); + check(&mut g, &d, &format!("{depth:?} frame stroke {blend:?} {align:?} content {content}")); + } + } +} + #[test] fn artboards() { let Some(mut g) = gpu() else { return }; @@ -3055,6 +3085,25 @@ fn pass_through_clipping_base_with_an_adjustment() { check(&mut g, &d, "pass-through clipping base with an adjustment"); } +/// Transparency Shapes Layer off changes the special eight's blend (the layer's alpha blends as +/// Fill, #5692); the GPU has no pass for it and leaves such documents to the CPU. Other modes look +/// the same either way and stay on the GPU. +#[test] +fn transparency_shapes_off_special_blend_falls_back_to_the_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(64, 48); + let mut l = noise_layer("top", PixelFormat::RGBA8, Rect::new(4, 3, 60, 45), 2, 0.0); + l.blend = BlendMode::ColorDodge; + l.opacity = 0.53; + l.advanced.transparency_shapes = false; + d.layers.push(l); + assert!(render_to_vec(&mut g.comp, &g.device, &g.queue, &d, d.bounds()).is_err(), "Color Dodge with Transparency Shapes Layer off"); + if let Some(l) = d.layers.last_mut() { + l.blend = BlendMode::Multiply; + } + check(&mut g, &d, "Multiply with Transparency Shapes Layer off"); +} + /// #5846: a shape's Density and Feather soften its fill (its own rendered pixels), with and /// without a vector stroke over clipped layers and layer effects: the GPU matches the CPU. #[test] diff --git a/crates/io/src/adjust_map.rs b/crates/io/src/adjust_map.rs index 2cac91c4a0..ecde1d7a2c 100644 --- a/crates/io/src/adjust_map.rs +++ b/crates/io/src/adjust_map.rs @@ -11,7 +11,7 @@ //! stay [`Adjustment::Unsupported`] and are written back verbatim. use photocraft_doc::Adjustment; -use photocraft_doc::adjust::{CURVE_MAP_LEN, CurvePoint, HueRange, LevelsChannel, ToneSpace, curve_map, curve_map_outputs}; +use photocraft_doc::adjust::{CURVE_MAP_LEN, CurvePoint, HueRange, LevelsChannel, LutFormat, LutSource, ToneSpace, curve_map, curve_map_outputs}; use photocraft_psd::descriptor::{Descriptor, Value, VersionedDescriptor}; /// All PSD adjustment keys recognized as adjustment layers. @@ -459,12 +459,12 @@ fn read_lookup(d: &[u8]) -> Result { Some(_) => return Err("its LUT data is empty".into()), None => return Err("it uses a colour profile (abstract or device link), not a LUT file, which isn't supported yet".into()), }; - let (ext, format) = match desc_enum(&desc, "LUTFormat") { - Some(b"LUTFormat3DL") => ("x.3dl", "3DL"), - Some(b"LUTFormatLOOK") => ("x.look", "LOOK"), - _ => ("x.cube", "CUBE"), + let format = match desc_enum(&desc, "LUTFormat") { + Some(b"LUTFormat3DL") => LutFormat::ThreeDl, + Some(b"LUTFormatLOOK") => LutFormat::Look, + _ => LutFormat::Cube, }; - let lut = photocraft_cms::lutfile::parse(ext, bytes).map_err(|e| format!("its {format} LUT can't be read ({e})"))?; + let lut = parse_lut_source(format, bytes).map_err(|e| format!("its {} LUT can't be read ({e})", psd_lut_format(format).trim_start_matches("LUTFormat")))?; if !lut.domain_is_default() { return Err(format!( "its LUT has a non-default input domain (DOMAIN_MIN {:?}, DOMAIN_MAX {:?}), which is not supported yet", @@ -478,9 +478,47 @@ fn read_lookup(d: &[u8]) -> Result { lut: Some(std::sync::Arc::new(lut.data)), tetrahedral: false, dither: desc_bool(&desc, "Dthr").unwrap_or(false), + // Kept so a save embeds the file as loaded (Photoshop never converts it). + source: Some(LutSource { format, bytes: std::sync::Arc::new(bytes.clone()) }), }) } +/// A LUT file's table, read the way a PSD read does (an XML file tagged `.cube` is sniffed as `.look`). +fn parse_lut_source(format: LutFormat, bytes: &[u8]) -> Result { + let format = match format { + LutFormat::Cube => photocraft_cms::lutfile::Format::Cube, + LutFormat::ThreeDl => photocraft_cms::lutfile::Format::ThreeDl, + LutFormat::Look => photocraft_cms::lutfile::Format::Look, + }; + photocraft_cms::lutfile::parse(&format!("x.{}", format.extension()), bytes) +} + +/// Photoshop's `LUTFormatType` value for a format. +fn psd_lut_format(format: LutFormat) -> &'static str { + match format { + LutFormat::Cube => "LUTFormatCUBE", + LutFormat::ThreeDl => "LUTFormat3DL", + LutFormat::Look => "LUTFormatLOOK", + } +} + +/// The source file of a Color Lookup to embed as is: only while it still parses to the +/// layer's table, so an edited table is never saved with stale bytes. +fn lut_source_for<'a>(source: Option<&'a LutSource>, table: &[f32], size: usize) -> Option<&'a LutSource> { + let source = source?; + let file = parse_lut_source(source.format, &source.bytes).ok()?; + (file.size == size && file.data == table).then_some(source) +} + +/// `name` with a `.3dl` / `.look` extension swapped for `.cube`, for a table saved as `.cube`. +fn cube_file_name(name: &str) -> String { + let lower = name.to_ascii_lowercase(); + match [".3dl", ".look"].iter().find(|ext| lower.ends_with(*ext)) { + Some(ext) => format!("{}.cube", name.get(..name.len() - ext.len()).unwrap_or(name)), + None => name.to_string(), + } +} + /// The descriptor of a `vibA` / `blwh` block: a 4-byte descriptor version (16), then the descriptor. fn versioned(d: &[u8]) -> Option { VersionedDescriptor::parse_prefix(d).ok().map(|(v, _)| v.descriptor) @@ -735,31 +773,40 @@ pub fn write(adj: &Adjustment) -> Vec<([u8; 4], Vec)> { } return vec![(*b"selc", v)]; } - Adjustment::ColorLookup { name, lut, size, dither, .. } => { + Adjustment::ColorLookup { name, lut, size, dither, source, .. } => { // A lookup without a usable table (none chosen yet, or a bad size) renders as the // identity, so it is written as a 2³ identity cube rather than dropped. let n = *size as usize; let cube = lut.as_deref().zip(photocraft_doc::adjust::lut3d_len(*size)).and_then(|(table, len)| table.get(..len)).filter(|_| n <= 256); - let (file, dither) = match cube { - Some(cube) => ( - photocraft_cms::lutfile::LutFile { title: String::new(), size: n, data: cube.to_vec(), domain_min: [0.0; 3], domain_max: [1.0; 3] }, - *dither, - ), - _ => (photocraft_cms::lutfile::LutFile { title: String::new(), ..photocraft_cms::lutfile::LutFile::identity(2) }, false), + // Like Photoshop, embed the LUT file as loaded, in its own format; a table made or + // changed in PhotoCraft is written as `.cube`, named to match. + let (format, data, name, dither) = match cube { + Some(cube) => match lut_source_for(source.as_ref(), cube, n) { + Some(src) => (src.format, src.bytes.to_vec(), name.clone(), *dither), + None => { + let file = + photocraft_cms::lutfile::LutFile { title: String::new(), size: n, data: cube.to_vec(), domain_min: [0.0; 3], domain_max: [1.0; 3] }; + (LutFormat::Cube, photocraft_cms::lutfile::write_cube(&file).into_bytes(), cube_file_name(name), *dither) + } + }, + _ => { + let file = photocraft_cms::lutfile::LutFile { title: String::new(), ..photocraft_cms::lutfile::LutFile::identity(2) }; + (LutFormat::Cube, photocraft_cms::lutfile::write_cube(&file).into_bytes(), cube_file_name(name), false) + } }; let en = |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; let text = |t: &str| Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul(t)); let d = Descriptor::new("null") .with("lookupType", en("colorLookupType", "3DLUT")) - .with("NM ", text(name)) + .with("NM ", text(&name)) .with("Dthr", Value::Boolean(dither)) .with("profile", Value::RawData(Vec::new())) - .with("LUTFormat", en("LUTFormatType", "LUTFormatCUBE")) + .with("LUTFormat", en("LUTFormatType", psd_lut_format(format))) .with("dataOrder", en("colorLookupOrder", "rgbOrder")) .with("tableOrder", en("colorLookupOrder", "bgrOrder")) - .with("LUT3DFileData", Value::RawData(photocraft_cms::lutfile::write_cube(&file).into_bytes())) - .with("LUT3DFileName", text(name)); + .with("LUT3DFileData", Value::RawData(data)) + .with("LUT3DFileName", text(&name)); put16(&mut v, 1); v.extend_from_slice(&VersionedDescriptor::new(d).to_bytes()); return vec![(*b"clrL", v)]; diff --git a/crates/io/src/adjust_map/tests.rs b/crates/io/src/adjust_map/tests.rs index 70727fba4f..62236782d7 100644 --- a/crates/io/src/adjust_map/tests.rs +++ b/crates/io/src/adjust_map/tests.rs @@ -219,7 +219,9 @@ fn roundtrips() { rt(Adjustment::ChannelMixer { matrix: [[0.5, 0.3, 0.2, 0.0], [0.1, 0.8, 0.1, 0.05], [0.0, 0.2, 0.9, -0.05]], monochrome: false }); rt(Adjustment::ChannelMixer { matrix: [[0.4, 0.4, 0.2, 0.1], [0.0, 1.0, 0.0, 0.0], [-2.0, 0.0, 2.0, 0.0]], monochrome: true }); let id = photocraft_cms::lutfile::LutFile::identity(5); - rt(Adjustment::ColorLookup { name: "Look.cube".into(), lut: Some(std::sync::Arc::new(id.data)), size: 5, tetrahedral: false, dither: true }); + let cube = photocraft_cms::lutfile::write_cube(&id).into_bytes(); + let source = Some(LutSource { format: LutFormat::Cube, bytes: std::sync::Arc::new(cube) }); + rt(Adjustment::ColorLookup { name: "Look.cube".into(), lut: Some(std::sync::Arc::new(id.data)), size: 5, tetrahedral: false, dither: true, source }); } #[test] @@ -246,6 +248,105 @@ fn selective_color_synthetic_block() { assert_eq!(write(&a)[0].1, d); } +/// A 2³ Lustre `.3dl` (12-bit, blue fastest) that inverts every colour. +fn inverting_3dl() -> Vec { + let mut text = String::from("# made by hand\n0 1023\n"); + for r in 0..2 { + for g in 0..2 { + for b in 0..2 { + text.push_str(&format!("{} {} {}\n", (1 - r) * 4095, (1 - g) * 4095, (1 - b) * 4095)); + } + } + } + text.into_bytes() +} + +/// A 2³ SpeedGrade `.look` holding the identity. +fn identity_look() -> Vec { + let hex: String = photocraft_cms::lutfile::LutFile::identity(2).data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + format!("\n\"2\"\"{hex}\"\n").into_bytes() +} + +/// A `clrL` block as Photoshop writes one for a loaded LUT file. +fn clrl_block(format: &str, bytes: &[u8], name: &str) -> Vec { + let en = |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; + let text = |t: &str| Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul(t)); + let desc = Descriptor::new("null") + .with("lookupType", en("colorLookupType", "3DLUT")) + .with("NM ", text(name)) + .with("LUTFormat", en("LUTFormatType", format)) + .with("LUT3DFileData", Value::RawData(bytes.to_vec())) + .with("LUT3DFileName", text(name)); + let mut d = vec![0, 1]; + d.extend(VersionedDescriptor::new(desc).to_bytes()); + d +} + +/// The descriptor of the `clrL` block `a` is written as. +fn written_clrl(a: &Adjustment) -> Descriptor { + let b = write(a); + assert_eq!(&b[0].0, b"clrL"); + VersionedDescriptor::parse_prefix(&b[0].1[2..]).unwrap().0.descriptor +} + +fn raw<'a>(d: &'a Descriptor, key: &str) -> &'a [u8] { + let Some(Value::RawData(b)) = d.get(key) else { panic!("{key} missing") }; + b +} + +#[test] +fn color_lookup_keeps_a_3dl_or_look_file_as_loaded() { + // #6544: Photoshop embeds the LUT file as loaded and tags its format; it never converts it. + for (format, kind, bytes, name) in + [("LUTFormat3DL", LutFormat::ThreeDl, inverting_3dl(), "Invert.3DL"), ("LUTFormatLOOK", LutFormat::Look, identity_look(), "Grade.look")] + { + let a = parse(b"clrL", &clrl_block(format, &bytes, name), None, Channels::Rgb); + let Adjustment::ColorLookup { name: n, lut: Some(_), size: 2, source: Some(src), .. } = &a else { panic!("{a:?}") }; + assert_eq!((n.as_str(), src.format, src.bytes.as_slice()), (name, kind, bytes.as_slice())); + let d = written_clrl(&a); + assert_eq!(desc_enum(&d, "LUTFormat"), Some(format.as_bytes()), "{name}"); + assert_eq!(raw(&d, "LUT3DFileData"), bytes.as_slice(), "{name}: the file's bytes, unchanged"); + assert_eq!(desc_text(&d, "LUT3DFileName").as_deref(), Some(name)); + assert_eq!(desc_text(&d, "NM ").as_deref(), Some(name)); + // And it reads back as the same layer. + let b = write(&a); + assert_eq!(parse(&b[0].0, &b[0].1, None, Channels::Rgb), a); + } +} + +#[test] +fn color_lookup_table_without_matching_file_is_saved_as_cube_named_to_match() { + let a = parse(b"clrL", &clrl_block("LUTFormat3DL", &inverting_3dl(), "Invert.3DL"), None, Channels::Rgb); + let Adjustment::ColorLookup { name, size, tetrahedral, dither, source, .. } = a else { panic!("{a:?}") }; + // The table no longer matches the stored file (changed in PhotoCraft): the bytes are stale. + let id = photocraft_cms::lutfile::LutFile::identity(2).data; + let edited = Adjustment::ColorLookup { name, lut: Some(std::sync::Arc::new(id.clone())), size, tetrahedral, dither, source }; + let d = written_clrl(&edited); + assert_eq!(desc_enum(&d, "LUTFormat"), Some(&b"LUTFormatCUBE"[..])); + assert_eq!(desc_text(&d, "LUT3DFileName").as_deref(), Some("Invert.cube")); + assert_eq!(photocraft_cms::lutfile::parse("x.cube", raw(&d, "LUT3DFileData")).unwrap().data, id); + // A table made in PhotoCraft (no file) named like another format is also renamed. + let made = + Adjustment::ColorLookup { name: "Grade.look".into(), lut: Some(std::sync::Arc::new(id)), size: 2, tetrahedral: false, dither: false, source: None }; + let d = written_clrl(&made); + assert_eq!(desc_enum(&d, "LUTFormat"), Some(&b"LUTFormatCUBE"[..])); + assert_eq!(desc_text(&d, "LUT3DFileName").as_deref(), Some("Grade.cube")); + assert_eq!(desc_text(&d, "NM ").as_deref(), Some("Grade.cube")); + // An unreadable stored file falls back the same way instead of failing the save. + let junk = Adjustment::ColorLookup { + name: "Junk.3dl".into(), + lut: Some(std::sync::Arc::new(photocraft_cms::lutfile::LutFile::identity(2).data)), + size: 2, + tetrahedral: false, + dither: false, + source: Some(LutSource { format: LutFormat::ThreeDl, bytes: std::sync::Arc::new(b"\xFF not a lut".to_vec()) }), + }; + assert_eq!(desc_text(&written_clrl(&junk), "LUT3DFileName").as_deref(), Some("Junk.cube")); + assert_eq!(cube_file_name("a.b.3dl"), "a.b.cube"); + assert_eq!(cube_file_name("Day for Night"), "Day for Night"); + assert_eq!(cube_file_name("x.cube"), "x.cube"); +} + #[test] fn color_lookup_synthetic_block() { let cube = b"TITLE \"t\"\nLUT_3D_SIZE 2\n0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"; @@ -547,7 +648,7 @@ fn degenerate_adjustments_still_get_an_encoding() { } // A colour lookup without a (usable) table becomes an identity cube, without dither. for lut in [None, Some(std::sync::Arc::new(vec![0.5; 7]))] { - let b = write(&Adjustment::ColorLookup { name: "L".into(), lut, size: 3, tetrahedral: false, dither: true }); + let b = write(&Adjustment::ColorLookup { name: "L".into(), lut, size: 3, tetrahedral: false, dither: true, source: None }); let back = parse(&b[0].0, &b[0].1, None, Channels::Rgb); let Adjustment::ColorLookup { lut: Some(t), size: 2, dither: false, name, .. } = back else { panic!("{back:?}") }; assert_eq!(name, "L"); diff --git a/crates/io/src/affinity.rs b/crates/io/src/affinity.rs index f794e1be40..4e5ed90f86 100644 --- a/crates/io/src/affinity.rs +++ b/crates/io/src/affinity.rs @@ -149,7 +149,7 @@ impl Builder { let r = self.bbox(to_doc, page); let mut g = Layer::new( format!("Page {}", i + 1), - LayerContent::Group(Group { children: std::mem::take(&mut children), expanded: true, artboard: None }), + LayerContent::Group(Group { children: std::mem::take(&mut children), expanded: true, artboard: None, frame_stroke: None }), ); if let LayerContent::Group(gr) = &mut g.content { gr.artboard = Some(Artboard { rect: r, background: ArtboardBackground::White, preset: String::new() }); @@ -244,7 +244,7 @@ impl Builder { fn group(&mut self, n: &af::Node, children: Vec) -> Layer { // Pass-through and Normal (isolated) groups map one to one. - self.base(n, LayerContent::Group(Group { children, expanded: false, artboard: None })) + self.base(n, LayerContent::Group(Group { children, expanded: false, artboard: None, frame_stroke: None })) } /// One Affinity node and its subtree; `to_doc` maps Affinity document pixels to the canvas. diff --git a/crates/io/src/camera_raw_xmp.rs b/crates/io/src/camera_raw_xmp.rs index a2e63322d0..65df97e898 100644 --- a/crates/io/src/camera_raw_xmp.rs +++ b/crates/io/src/camera_raw_xmp.rs @@ -228,7 +228,7 @@ pub fn load(params: &mut CameraRaw, bytes: &[u8]) -> Result let text = std::str::from_utf8(bytes).map_err(|_| "not a Camera Raw settings file (not UTF-8 text)".to_string())?; // Some writers put a byte order mark or an `` wrapper first; roxmltree takes both. let text = text.trim_start_matches('\u{feff}'); - let doc = roxmltree::Document::parse(text).map_err(|e| format!("not a Camera Raw settings file ({e})"))?; + let doc = crate::xml::parse(text, roxmltree::ParsingOptions::default()).map_err(|e| format!("not a Camera Raw settings file ({e})"))?; let mut next = params.clone(); let mut report = LoadReport::default(); let mut found = false; @@ -389,6 +389,16 @@ mod tests { assert_eq!(p.calibration_hue[0], -5.0); } + /// A settings file nested 20 000 deep overflowed the XML parser's recursion (an abort nothing + /// can catch); it is an error now. The small stack makes a regression abort instead of passing. + #[test] + fn deeply_nested_settings_are_an_error_not_a_stack_overflow() { + let xml = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let e = + std::thread::Builder::new().stack_size(512 * 1024).spawn(move || load(&mut CameraRaw::default(), xml.as_bytes()).err()).unwrap().join().unwrap(); + assert!(e.as_deref().is_some_and(|m| m.contains("nested")), "{e:?}"); + } + #[test] fn save_then_load_round_trips() { let mut p = CameraRaw { diff --git a/crates/io/src/kra.rs b/crates/io/src/kra.rs index af9b238971..d8b3f9a36d 100644 --- a/crates/io/src/kra.rs +++ b/crates/io/src/kra.rs @@ -283,10 +283,10 @@ pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result Result { let v = number(image, a, 0.0)?; diff --git a/crates/io/src/kra/tests.rs b/crates/io/src/kra/tests.rs index 92e951cdb8..214ccdc9c6 100644 --- a/crates/io/src/kra/tests.rs +++ b/crates/io/src/kra/tests.rs @@ -200,6 +200,23 @@ fn export_is_refused() { } } +/// maindoc.xml nested 20 000 deep, directly or through an internal-subset entity the depth check +/// can't see, overflowed the XML parser's recursion (an abort nothing can catch). Both are errors +/// now; the small stack makes a regression abort instead of passing. +#[test] +fn deeply_nested_maindoc_is_an_error_not_a_stack_overflow() { + let (open, close) = ("".repeat(20_000), "".repeat(20_000)); + let direct = maindoc("RGBA", &format!("{open}{close}")); + let entity = format!("]>&e;"); + // A quoted `?>` in the XML declaration must not hide the DOCTYPE behind it. + let declared = format!("\"?>{entity}"); + for (xml, why) in [(direct, "nested"), (entity, "internal DTD"), (declared, "internal DTD")] { + let bytes = archive(&xml, &[]); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || crate::import("deep.kra", &bytes).err()).unwrap().join().unwrap(); + assert!(matches!(&e, Some(IoError::Kra(m)) if m.contains(why)), "{why}: {e:?}"); + } +} + #[test] fn lzf_decodes_literals_and_overlapping_references() { // "abc" then a 6-byte back-reference 3 back: abcabcabc. diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index 44beb59d47..76b39e9270 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -75,6 +75,7 @@ pub mod text_styles_map; pub mod tiff_layers; pub mod vector_map; mod xcf; +pub mod xml; use photocraft_codecs::{CodecError, EncodeOptions}; pub use photocraft_codecs::{DdsCompression, DdsOptions, DpxBits}; diff --git a/crates/io/src/multichannel_map.rs b/crates/io/src/multichannel_map.rs index e9437deb6c..e877276198 100644 --- a/crates/io/src/multichannel_map.rs +++ b/crates/io/src/multichannel_map.rs @@ -73,7 +73,9 @@ pub(crate) fn import(file: &PsdFile, all: &[u8], doc: &mut Document, names: &[St } /// Writes a Multichannel document as a mode-7 PSD (no layer section; every channel a plane). -pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec) { +/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps +/// the version whatever the size. +pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: Option) -> (PsdFile, Vec) { let mut warnings = Vec::new(); // Photoshop's Multichannel mode is 8 or 16 bits per channel. let sample = match doc.depth { @@ -106,12 +108,9 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< } } let big = doc.size.width > 30_000 || doc.size.height > 30_000; - let size_exceeds_limit = !force_psb && !big && crate::psd_export::psd_size_exceeds_limit(doc); - let version = crate::psd_export::psd_version(doc, force_psb, size_exceeds_limit); + let version = crate::psd_export::psd_version(doc, force_psb); if big && !force_psb { warnings.push("document exceeds 30000 px; written as PSB".into()); - } else if size_exceeds_limit && !force_psb { - warnings.push("estimated encoded size exceeds 2 GB; written as PSB".into()); } let header = Header::new(version, doc.size.width, doc.size.height, channels.len().max(1) as u16, psd_depth(sample), PsdMode::Multichannel); let image_data = ImageData::encode(Compression::Rle, &planes, &header) @@ -133,14 +132,16 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< // Alpha identifiers: one u32 per channel, only while they still match the channels. let keep = |id: u16, data: &[u8]| id != ALPHA_IDENTIFIERS || data.len() == channels.len().saturating_mul(4); let color_mode_data = photocraft_psd::hdr::color_mode_data_for_depth(header.depth); - let file = layerless_file(doc, header, image_data, color_mode_data, resources, keep, &mut warnings); + let file = layerless_file(doc, header, image_data, color_mode_data, resources, keep, size_limit, &mut warnings); (file, warnings) } /// A PSD without layers around `image_data`: `resources` (the mode's own) plus the paths, XMP, /// EXIF and every other imported resource `keep` accepts (except the ones that only describe /// layers and any already in `resources`), and the document-level tagged blocks. Shared by the -/// modes that have no layers in Photoshop: Multichannel, Indexed Color and Bitmap. +/// modes that have no layers in Photoshop: Multichannel, Indexed Color and Bitmap. A file past +/// `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps the version. +#[allow(clippy::too_many_arguments)] pub(crate) fn layerless_file( doc: &Document, header: Header, @@ -148,6 +149,7 @@ pub(crate) fn layerless_file( color_mode_data: Vec, mut resources: Vec, keep: impl Fn(u16, &[u8]) -> bool, + size_limit: Option, warnings: &mut Vec, ) -> PsdFile { resources.extend(crate::psd_export::path_resources(doc, warnings)); @@ -175,13 +177,14 @@ pub(crate) fn layerless_file( } resources.push(version_info_resource(true)); // Document-level tagged blocks (patterns, notes, ...) are valid without a layer section; a - // kept layer block would describe layers the document doesn't have. + // kept layer block would describe layers the document doesn't have. Their signatures follow + // the version the file is written as, known once its size is. let global_blocks: Vec = crate::annotations_map::export_blocks(doc, crate::pattern_map::export_global_blocks(doc)) .into_iter() .filter(|(_, key, _)| !matches!(key, b"Layr" | b"Lr16" | b"Lr32")) - .map(|(sig, key, data)| crate::psd_export::global_block(crate::psd_export::block_signature(sig, key, header.version), key, &data)) + .map(|(sig, key, data)| crate::psd_export::global_block(sig, key, &data)) .collect(); - PsdFile { + let mut file = PsdFile { header, color_mode_data, resources, @@ -191,5 +194,11 @@ pub(crate) fn layerless_file( global_blocks, layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, + }; + if let Some(limit) = size_limit { + warnings.extend(crate::psd_export::fit_psd_size(&mut file, limit)); } + crate::psd_export::sign_global_blocks(&mut file); + file } diff --git a/crates/io/src/ora.rs b/crates/io/src/ora.rs index 193680d31e..fb7968fbcc 100644 --- a/crates/io/src/ora.rs +++ b/crates/io/src/ora.rs @@ -414,7 +414,7 @@ pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result root")); diff --git a/crates/io/src/ora/tests.rs b/crates/io/src/ora/tests.rs index c5576ce0ef..e4797fc52d 100644 --- a/crates/io/src/ora/tests.rs +++ b/crates/io/src/ora/tests.rs @@ -195,6 +195,16 @@ fn archive(stack: &str, images: &[(&str, Vec)]) -> Vec { z.finish().unwrap() } +/// A stack.xml nested 20 000 deep overflowed the XML parser's recursion (an abort nothing can +/// catch); it is an error now. The small stack makes a regression abort instead of passing. +#[test] +fn deeply_nested_stack_xml_is_an_error_not_a_stack_overflow() { + let stack = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let bytes = archive(&stack, &[]); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || crate::import("deep.ora", &bytes).err()).unwrap().join().unwrap(); + assert!(matches!(&e, Some(IoError::Ora(m)) if m.contains("nested")), "{e:?}"); +} + fn solid_png(w: u32, h: u32, rgba: [u8; 4]) -> Vec { let data = rgba.iter().copied().cycle().take((w * h * 4) as usize).collect(); codecs::encode(&Image::from_raw(w, h, ChannelLayout::Rgba, CSample::U8, data).unwrap(), Format::Png, &Default::default()).unwrap() diff --git a/crates/io/src/palette_map.rs b/crates/io/src/palette_map.rs index 45b8168f1c..8a20d7477b 100644 --- a/crates/io/src/palette_map.rs +++ b/crates/io/src/palette_map.rs @@ -21,7 +21,9 @@ const CHANNEL_RESOURCES: [u16; 4] = [1006, 1045, 1053, crate::channel_map::DISPL /// opaque, or a Bitmap document; None for any other document (it is written like the others). A /// document with alpha channels or a Quick Mask is written like the others too: the importer does /// not read the extra channels of these modes back, so they would be lost. -pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> Option<(PsdFile, Vec)> { +/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps +/// the version whatever the size. +pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: Option) -> Option<(PsdFile, Vec)> { if !doc.channels.is_empty() || doc.quick_mask.is_some() { return None; } @@ -38,9 +40,7 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> Option<(PsdFil if !flattens_losslessly(doc) { warnings.push(format!("{:?} documents have no layers in Photoshop: written flattened", doc.mode)); } - let big = doc.size.width > 30_000 || doc.size.height > 30_000; - let size_exceeds_limit = !force_psb && !big && crate::psd_export::psd_size_exceeds_limit(doc); - let version = crate::psd_export::psd_version(doc, force_psb, size_exceeds_limit); + let version = crate::psd_export::psd_version(doc, force_psb); let depth = if mode == PsdMode::Bitmap { 1 } else { 8 }; let header = Header::new(version, doc.size.width, doc.size.height, 1, depth, mode); let image_data = ImageData::encode(Compression::Rle, &plane, &header) @@ -65,7 +65,7 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> Option<(PsdFil } // An imported transparency index that no longer applies is not copied either. let keep = |id: u16, _: &[u8]| !CHANNEL_RESOURCES.contains(&id) && id != TRANSPARENCY_INDEX && id != COLOR_TABLE_COUNT; - let file = crate::multichannel_map::layerless_file(doc, header, image_data, color_mode_data, resources, keep, &mut warnings); + let file = crate::multichannel_map::layerless_file(doc, header, image_data, color_mode_data, resources, keep, size_limit, &mut warnings); Some((file, warnings)) } @@ -162,7 +162,7 @@ mod tests { } fn round_trip(doc: &Document) -> (PsdFile, Document) { - let (file, _) = document_to_psd(doc, false).unwrap(); + let (file, _) = document_to_psd(doc, false, None).unwrap(); let bytes = file.to_bytes().unwrap(); let file = PsdFile::from_bytes(&bytes).unwrap(); let back = crate::import("test.psd", &bytes).unwrap().document; @@ -214,17 +214,17 @@ mod tests { assert_eq!(back.color_table, doc.color_table); for count in [0, 257] { doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0]; count], transparent: None }); - assert!(document_to_psd(&doc, false).is_none(), "{count}"); + assert!(document_to_psd(&doc, false, None).is_none(), "{count}"); } doc.color_table = None; - assert!(document_to_psd(&doc, false).is_none()); + assert!(document_to_psd(&doc, false, None).is_none()); } #[test] fn indexed_import_bounds_malformed_count_and_transparency_resources() { let mut doc = document(1, 1, SampleType::U8, ColorMode::Indexed, &[0.0, 0.0, 0.0, 1.0]); doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: None }); - let (original, _) = document_to_psd(&doc, false).unwrap(); + let (original, _) = document_to_psd(&doc, false, None).unwrap(); for (count, expected) in [(vec![0, 0], 1), (vec![1, 1], 256), (vec![255, 255], 256), (vec![0], 256)] { let mut file = original.clone(); file.resources.retain(|r| r.id != COLOR_TABLE_COUNT); diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 0c809273a9..20207e44eb 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -22,13 +22,11 @@ mod resources; mod smart; pub(crate) use document::document_to_psd_interruptible; +#[cfg(test)] +use document::document_to_psd_sized; pub use document::{document_to_psd, document_to_psd_with}; use document::{document_to_psd_nested, mask_parameters}; -#[cfg(test)] -pub(crate) use estimate::estimate_psd_size; -pub(crate) use estimate::{block_signature, psd_size_exceeds_limit, psd_version}; -#[cfg(test)] -use estimate::{estimate_encoded_size, exceeds_psd_size_limit}; +pub(crate) use estimate::{fit_psd_size, psd_version, sign_global_blocks}; pub(crate) use resources::{global_block, guides_resource, keep_imported_resource, legacy_name, pascal_names_resource, path_resources, unicode_names_resource}; use smart::{LINK_KEYS, SmartOut, Source, contents_uuid, effects_unchanged, masked, size_from_layer, smart_keys, upsert}; @@ -37,24 +35,25 @@ use smart::{LINK_KEYS, SmartOut, Source, contents_uuid, effects_unchanged, maske /// A raster layer never writes them, nor adjustment blocks. const RASTER_STALE_KEYS: [&[u8; 4]; 8] = [b"SoCo", b"GdFl", b"PtFl", b"SoLd", b"PlLd", b"SoLE", b"TySh", b"vscg"]; +/// The largest file written as PSD: past it, the document is saved as PSB (Large Document Format), +/// as Photoshop does. const PSD_MAX_FILE_SIZE: u64 = 2 * 1024 * 1024 * 1024; +/// Room kept under [`PSD_MAX_FILE_SIZE`] for what the save adds after the conversion (the image +/// preview resource, a few KB). +const PSD_SAVE_HEADROOM: u64 = 1024 * 1024; /// Most bytes of one layer's interleaved samples the exporter gathers. The interleaved bytes and /// the channel planes made from them (or the band parts and the joined buffer) are alive together, /// so the peak is about twice this; it is also what fits in a file of at most [`PSD_MAX_FILE_SIZE`]. const MAX_LAYER_GATHER_BYTES: u64 = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 256 << 20 }; -const PSD_ESTIMATE_FIXED_OVERHEAD: u64 = 1024 * 1024; -const PSD_ESTIMATE_LAYER_OVERHEAD: u64 = 64 * 1024; /// Options for [`document_to_psd_with`]. #[derive(Debug, Clone)] pub struct PsdExportOptions { /// Write PSB even when the document fits PSD limits. pub force_psb: bool, - /// Switch to PSB when the estimated encoded size exceeds PSD's 2 GB limit. The estimate - /// counts every layer plane uncompressed, so a document with many canvas-sized layers can - /// pass it while its real file is far smaller. A writer that can measure the real size - /// instead turns this off: the layered TIFF writes PSD-style layer data and falls back to - /// PSB only when a length really overflows 32 bits. + /// Switch to PSB when the encoded file exceeds PSD's 2 GB limit (`fit_psd_size`). A writer + /// that embeds the layer data elsewhere turns this off: the layered TIFF writes PSD-style + /// layer data and falls back to PSB only when a length really overflows 32 bits. pub size_limit_psb: bool, /// Matte the merged image against white where it is translucent, as Photoshop does /// (`false` keeps straight colour under the alpha: for containers that store the composite @@ -543,7 +542,23 @@ impl Ex { let masked = l.vector_mask.is_some(); raw.retain(|(k, _)| !(RASTER_STALE_KEYS.contains(&k) || adjust_map::ADJUSTMENT_KEYS.contains(&k) || k == b"CgEd" || (k == b"vstk" && masked))); } - LayerContent::Group(_) => {} + // A Frame's stroke (`vstk`), kept byte-identical while it still decodes to the model. + LayerContent::Group(g) => { + let pos = raw.iter().position(|(k, _)| k == b"vstk"); + let old = pos.and_then(|i| raw.get(i)).and_then(|(_, d)| crate::vector_map::parse_vstk(d, self.dpi)); + // One we can't decode stays as it was unless the model has a stroke to write. + let stale = match &old { + Some(v) => v.stroke != g.frame_stroke, + None => g.frame_stroke.is_some(), + }; + if stale { + let data = crate::vector_map::vstk_bytes(g.frame_stroke.as_ref(), old.is_none_or(|v| v.fill_enabled), self.dpi); + match pos.and_then(|i| raw.get_mut(i)) { + Some(e) => e.1 = data, + None => raw.push((*b"vstk", data)), + } + } + } } // Effects reference point: written from the field (in place, keeping block order). match l.effects.reference { diff --git a/crates/io/src/psd_export/document.rs b/crates/io/src/psd_export/document.rs index 0ad7188a58..50ec2088a0 100644 --- a/crates/io/src/psd_export/document.rs +++ b/crates/io/src/psd_export/document.rs @@ -1,4 +1,4 @@ -use super::estimate::{MERGED_RENDERED, MERGED_WEIGHT, block_signature, psd_size_exceeds_limit, psd_version}; +use super::estimate::{MERGED_RENDERED, MERGED_WEIGHT, fit_psd_size, psd_version, sign_global_blocks}; use super::merged::{cmyk_of, merged_planes, placeholder_planes}; use super::resources::{global_block, guides_resource, keep_imported_resource, legacy_name, pascal_names_resource, path_resources, unicode_names_resource}; use super::smart::{SmartOut, link_group_resource}; @@ -25,10 +25,17 @@ pub(crate) fn document_to_psd_interruptible(doc: &Document, opts: &PsdExportOpti /// [`document_to_psd_with`] for a document embedded `depth` smart objects deep. pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt) -> (PsdFile, Vec) { + document_to_psd_sized(doc, opts, depth, ctl, PSD_MAX_FILE_SIZE) +} + +/// [`document_to_psd_nested`] with the largest file kept as PSD (lowered by tests). +pub(super) fn document_to_psd_sized(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt, size_limit: u64) -> (PsdFile, Vec) { + // Without `size_limit_psb` the encoded size never changes the version, in every mode. + let layerless_limit = opts.size_limit_psb.then_some(size_limit); if doc.mode == ColorMode::Multichannel { - return crate::multichannel_map::document_to_psd(doc, opts.force_psb); + return crate::multichannel_map::document_to_psd(doc, opts.force_psb, layerless_limit); } - if let Some(file) = crate::palette_map::document_to_psd(doc, opts.force_psb) { + if let Some(file) = crate::palette_map::document_to_psd(doc, opts.force_psb, layerless_limit) { return file; } let fmt = doc.pixel_format(); @@ -43,8 +50,7 @@ pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, de let sample = fmt.sample; let cc = fmt.mode.color_channels(); let big = doc.size.width > 30_000 || doc.size.height > 30_000; - let size_exceeds_limit = opts.size_limit_psb && !opts.force_psb && !big && psd_size_exceeds_limit(doc); - let version = psd_version(doc, opts.force_psb, size_exceeds_limit); + let version = psd_version(doc, opts.force_psb); let mut ex = Ex { fmt, dpi: doc.resolution_dpi, @@ -70,8 +76,6 @@ pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, de }; if big && !opts.force_psb { ex.warnings.push("document exceeds 30000 px; written as PSB".into()); - } else if size_exceeds_limit && !opts.force_psb { - ex.warnings.push("estimated encoded size exceeds 2 GB; written as PSB".into()); } if fmt.mode != doc.mode && duotone_inks.is_none() { ex.warnings.push(format!("{:?} document written as {:?}", doc.mode, fmt.mode)); @@ -281,7 +285,8 @@ pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, de if !ex.records.is_empty() && matches!(&key, b"Lr16" | b"Lr32" | b"Layr") { continue; } - let block = global_block(block_signature(sig, key, version), key, &data); + // The signature follows the final version (see `sign_global_blocks`). + let block = global_block(sig, key, &data); // A kept merged-transparency marker describes the opened file's composite. Over an opaque // one it would make the first alpha channel read back as the transparency, and vanish. if !has_alpha && block.is_merged_transparency_marker() { @@ -362,7 +367,7 @@ pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, de Some(raw) => raw, None => photocraft_psd::hdr::color_mode_data_for_depth(header.depth), }; - let file = PsdFile { + let mut file = PsdFile { header, color_mode_data, resources, @@ -372,7 +377,12 @@ pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, de global_blocks, layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, }; + if opts.size_limit_psb { + ex.warnings.extend(fit_psd_size(&mut file, size_limit)); + } + sign_global_blocks(&mut file); (file, ex.warnings) } diff --git a/crates/io/src/psd_export/estimate.rs b/crates/io/src/psd_export/estimate.rs index 83c2388a1e..1f9250678b 100644 --- a/crates/io/src/psd_export/estimate.rs +++ b/crates/io/src/psd_export/estimate.rs @@ -8,109 +8,6 @@ pub(super) const MERGED_WEIGHT: f32 = 2.0; /// rest is compressing its planes. pub(super) const MERGED_RENDERED: f32 = 0.8; -/// Adds headroom for compressed channel streams and their per-row length tables. -pub(super) fn estimate_encoded_size(raw_bytes: u64, rows: u64, structural_bytes: u64) -> Option { - raw_bytes.checked_add(raw_bytes.checked_add(63)?.checked_div(64)?)?.checked_add(rows.checked_mul(8)?)?.checked_add(structural_bytes) -} - -fn add_plane_estimate(raw_bytes: &mut u64, rows: &mut u64, width: u64, height: u64, channels: u64, bytes_per_sample: u64) -> Option<()> { - let plane_bytes = width.checked_mul(height)?.checked_mul(channels)?.checked_mul(bytes_per_sample)?; - let plane_rows = height.checked_mul(channels)?; - *raw_bytes = raw_bytes.checked_add(plane_bytes)?; - *rows = rows.checked_add(plane_rows)?; - Some(()) -} - -pub(super) fn exceeds_psd_size_limit(estimated_size: Option) -> bool { - estimated_size.is_none_or(|size| size > PSD_MAX_FILE_SIZE) -} - -fn surface_dimensions(surface: &Surface) -> Option<(u64, u64)> { - let bounds = surface.content_bounds(); - Some((u64::from(bounds.width()), u64::from(bounds.height()))) -} - -pub(crate) fn estimate_psd_size(doc: &Document) -> Option { - let mut raw_bytes = 0; - let mut rows = 0; - let mut structural_bytes = PSD_ESTIMATE_FIXED_OVERHEAD; - - let width = u64::from(doc.size.width); - let height = u64::from(doc.size.height); - - if doc.mode == ColorMode::Multichannel { - let channels = u64::try_from(doc.channels.len().clamp(1, 56)).ok()?; - let sample_bytes = u64::try_from(if doc.depth == SampleType::F32 { SampleType::U16.bytes() } else { doc.depth.bytes() }).ok()?; - add_plane_estimate(&mut raw_bytes, &mut rows, width, height, channels, sample_bytes)?; - } else { - let format = doc.pixel_format(); - let color_channels = u64::try_from(format.mode.color_channels()).ok()?; - let bytes_per_sample = u64::try_from(format.sample.bytes()).ok()?; - let layer_channels = color_channels.checked_add(1)?; - - for (_, _, layer) in doc.walk() { - structural_bytes = structural_bytes.checked_add(PSD_ESTIMATE_LAYER_OVERHEAD)?.checked_add(u64::try_from(layer.name.len()).ok()?)?; - for (_, data) in &layer.psd_blocks { - structural_bytes = structural_bytes.checked_add(u64::try_from(data.len()).ok()?)?; - } - - let surface = match &layer.content { - LayerContent::Fill(fill) => { - let mut fill_width = width; - let mut fill_height = height; - if let Some(cache) = &layer.fill_cache - && cache.fill == *fill - { - let (cache_width, cache_height) = surface_dimensions(&cache.surface)?; - fill_width = fill_width.max(cache_width); - fill_height = fill_height.max(cache_height); - } - add_plane_estimate(&mut raw_bytes, &mut rows, fill_width, fill_height, layer_channels, bytes_per_sample)?; - None - } - _ => layer.surface(), - }; - if let Some(surface) = surface { - let (surface_width, surface_height) = surface_dimensions(surface)?; - add_plane_estimate(&mut raw_bytes, &mut rows, surface_width, surface_height, layer_channels, bytes_per_sample)?; - } - if let Some(mask) = &layer.mask { - let (mask_width, mask_height) = surface_dimensions(&mask.surface)?; - add_plane_estimate(&mut raw_bytes, &mut rows, mask_width, mask_height, 1, bytes_per_sample)?; - } - if let LayerContent::Smart(smart) = &layer.content - && let photocraft_doc::SmartSource::Embedded { bytes, .. } = &smart.source - { - structural_bytes = structural_bytes.checked_add(u64::try_from(bytes.len()).ok()?)?; - } - } - - let max_extra_channels = 56usize.saturating_sub(format.mode.color_channels() + 1); - let mut extra_channels = doc.channels.len().min(max_extra_channels); - if doc.quick_mask.is_some() && extra_channels < max_extra_channels { - extra_channels += 1; - } - let merged_channels = color_channels.checked_add(1)?.checked_add(u64::try_from(extra_channels).ok()?)?; - add_plane_estimate(&mut raw_bytes, &mut rows, width, height, merged_channels, bytes_per_sample)?; - for channel in doc.channels.iter().take(extra_channels) { - structural_bytes = structural_bytes.checked_add(u64::try_from(channel.name.len()).ok()?)?; - } - } - - structural_bytes = structural_bytes - .checked_add(u64::try_from(doc.icc_profile.as_ref().map_or(0, |profile| profile.len())).ok()?)? - .checked_add(u64::try_from(doc.metadata.xmp.as_deref().map_or(0, str::len)).ok()?)? - .checked_add(u64::try_from(doc.metadata.exif.as_ref().map_or(0, |data| data.len())).ok()?)?; - for (_, name, data) in &doc.metadata.psd_resources { - structural_bytes = structural_bytes.checked_add(u64::try_from(name.len()).ok()?)?.checked_add(u64::try_from(data.len()).ok()?)?; - } - for (_, _, data) in &doc.metadata.psd_global_blocks { - structural_bytes = structural_bytes.checked_add(u64::try_from(data.len()).ok()?)?; - } - - estimate_encoded_size(raw_bytes, rows, structural_bytes) -} - /// The signature a kept tagged block is written with. `8B64` exists only in PSB files: a PSD /// with an `8B64` block (kept from a PSB source, e.g. its global `FMsk` or `cinf`) is refused by /// Photoshop as "not compatible with this version" (measured in Photoshop 27.11). The other way @@ -125,11 +22,48 @@ pub(crate) fn block_signature(sig: [u8; 4], key: [u8; 4], version: Version) -> [ } } -pub(crate) fn psd_version(doc: &Document, force_psb: bool, size_exceeds_limit: bool) -> Version { +/// The version a document is encoded as: PSB when asked for or past PSD's 30000 px; otherwise +/// PSD, which [`fit_psd_size`] turns into PSB once the encoded file proves too large. +pub(crate) fn psd_version(doc: &Document, force_psb: bool) -> Version { let exceeds_dimensions = doc.size.width > 30_000 || doc.size.height > 30_000; - if force_psb || exceeds_dimensions || size_exceeds_limit { Version::Psb } else { Version::Psd } + if force_psb || exceeds_dimensions { Version::Psb } else { Version::Psd } } -pub(crate) fn psd_size_exceeds_limit(doc: &Document) -> bool { - exceeds_psd_size_limit(estimate_psd_size(doc)) +/// Keeps an encoded PSD whose file fits `limit` (less [`PSD_SAVE_HEADROOM`]) and turns a larger +/// one into PSB (#212, #5285). The decision is made on the exact length of the file as it will be +/// written, not on the raw pixel size: RLE usually packs the channels far below it. Promoting +/// re-encodes nothing (only the RLE row-count tables widen, see [`PsdFile::promote_to_psb`]). +/// Returns a warning when the version changed or could not be changed. +pub(crate) fn fit_psd_size(file: &mut PsdFile, limit: u64) -> Option { + if file.written_version().is_psb() { + return None; + } + // A length that cannot be computed (it overflows) cannot fit either. + if file.encoded_len().is_ok_and(|len| len.saturating_add(PSD_SAVE_HEADROOM) <= limit) { + return None; + } + match file.promote_to_psb() { + Ok(()) => { + // Photoshop pads the layer info to 4 bytes; the padding depends on the version. + let padded = file.layer_info.as_ref().is_some_and(|info| info.padding.is_some()); + if matches!(file.layer_info_placement, LayerInfoPlacement::Section) + && padded + && file.pad_layer_info_to(4).is_err() + && let Some(info) = &mut file.layer_info + { + info.padding = None; + } + Some("encoded size exceeds 2 GB; written as PSB".into()) + } + Err(e) => Some(format!("encoded size exceeds 2 GB, but the file could not be written as PSB: {e}")), + } +} + +/// Signs every global block for the version `file` is written as ([`block_signature`]): run +/// after [`fit_psd_size`], which may turn a PSD into a PSB. +pub(crate) fn sign_global_blocks(file: &mut PsdFile) { + let version = file.written_version(); + for block in &mut file.global_blocks { + block.signature = block_signature(block.signature, block.key, version); + } } diff --git a/crates/io/src/psd_export/tests.rs b/crates/io/src/psd_export/tests.rs index 9688dfeac2..af7c8a2241 100644 --- a/crates/io/src/psd_export/tests.rs +++ b/crates/io/src/psd_export/tests.rs @@ -337,33 +337,161 @@ fn light_adjustment_is_kept_verbatim_and_warned() { assert!(!crate::adjust_map::is_extended_brit(Some(&plain[1].1))); } -#[test] -fn size_limit_boundary_keeps_limit_and_switches_above_it() { - assert!(!exceeds_psd_size_limit(Some(PSD_MAX_FILE_SIZE))); - assert!(exceeds_psd_size_limit(Some(PSD_MAX_FILE_SIZE + 1))); - assert!(exceeds_psd_size_limit(None)); +/// A 1024² RGB document with one full-canvas layer: flat colour (packs to a few KB) or noise +/// (RLE can't pack it). Its raw planes (layer RGBA + merged RGB) are 7 MiB either way. +fn sized_document(noise: bool, depth: SampleType) -> Document { + const SIDE: u32 = 1024; + let mut doc = Document::new("size limit", photocraft_geom::Size::new(SIDE, SIDE), ColorMode::Rgb, depth); + let mut layer = Layer::raster("Layer 1", doc.pixel_format()); + let rect = photocraft_geom::Rect::new(0, 0, SIDE as i32, SIDE as i32); + let surface = layer.surface_mut().unwrap(); + if noise { + let mut state = 0x2545_f491_u32; + let data: Vec = (0..SIDE * SIDE * 4) + .map(|i| { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + if i % 4 == 3 { 1.0 } else { (state >> 24) as f32 / 255.0 } + }) + .collect(); + surface.write_region(rect, &data); + } else { + surface.fill_rect(rect, &[0.2, 0.4, 0.6, 1.0]); + } + doc.layers.push(layer); + doc } -#[test] -fn encoded_size_estimator_rejects_arithmetic_overflow() { - assert_eq!(estimate_encoded_size(u64::MAX, 0, 0), None); - assert_eq!(estimate_encoded_size(0, u64::MAX, 0), None); - assert_eq!(estimate_encoded_size(0, 0, u64::MAX), Some(u64::MAX)); - assert!(exceeds_psd_size_limit(estimate_psd_size(&document(u32::MAX, u32::MAX)))); +const SIZED_RAW_BYTES: u64 = 1024 * 1024 * (4 + 3); +/// Under the raw size, so only the encoded size can keep a document PSD. +const SIZED_LIMIT: u64 = 4 * 1024 * 1024; +const _: () = assert!(SIZED_RAW_BYTES > SIZED_LIMIT); + +fn export_sized(doc: &Document, limit: u64) -> (PsdFile, Vec) { + document_to_psd_sized(doc, &PsdExportOptions::default(), 0, &Interrupt::NONE, limit) } +/// #5285: a document whose raw pixels are over the PSD limit but whose encoded file fits is +/// saved as PSD (the raw-size estimate used to make it a PSB). #[test] -fn large_encoded_size_selects_psb_without_changing_explicit_choice() { - let large = document(30_000, 30_000); - assert!(estimate_psd_size(&large).is_some_and(|size| size > PSD_MAX_FILE_SIZE)); - let large_size_exceeds_limit = psd_size_exceeds_limit(&large); - assert_eq!(psd_version(&large, false, large_size_exceeds_limit), Version::Psb); +fn a_file_that_fits_after_compression_stays_psd() { + let doc = sized_document(false, SampleType::U8); + let (file, warnings) = export_sized(&doc, SIZED_LIMIT); + assert_eq!(file.written_version(), Version::Psd); + assert!(warnings.iter().all(|w| !w.contains("PSB")), "{warnings:?}"); + let bytes = file.to_bytes().unwrap(); + assert!(bytes.len() as u64 + PSD_SAVE_HEADROOM <= SIZED_LIMIT, "{} bytes", bytes.len()); + let (back, _) = crate::psd_to_document(&PsdFile::from_bytes(&bytes).unwrap()); + assert_eq!(back.layers[0].surface().unwrap().pixel(700, 300), doc.layers[0].surface().unwrap().pixel(700, 300)); +} +/// #5285: a document whose encoded file is over the limit becomes a PSB, byte for byte the +/// one a forced PSB save writes, and reads back. 8 bits keep the layers in the layer section, +/// 16 bits in a global `Lr16` block. +#[test] +fn a_file_over_the_limit_after_compression_is_psb() { + for depth in [SampleType::U8, SampleType::U16] { + let doc = sized_document(true, depth); + let (file, warnings) = export_sized(&doc, SIZED_LIMIT); + assert_eq!(file.written_version(), Version::Psb, "{depth:?}"); + assert!(warnings.iter().any(|w| w == "encoded size exceeds 2 GB; written as PSB"), "{warnings:?}"); + let bytes = file.to_bytes().unwrap(); + assert!(bytes.len() as u64 > SIZED_LIMIT); + let forced = document_to_psd_with(&doc, &PsdExportOptions { force_psb: true, ..Default::default() }).0; + let forced = forced.to_bytes().unwrap(); + assert!(bytes == forced, "{depth:?}: the promoted PSB differs from a PSB written directly"); + assert_eq!(PsdFile::from_bytes(&bytes).unwrap(), PsdFile::from_bytes(&forced).unwrap(), "{depth:?}"); + // The promoted model itself stays a consistent PSD for every reader. + assert_eq!(file.header.version, Version::Psd); + assert_eq!(crate::psd_to_document(&file).0.layers[0].surface().unwrap().pixel(511, 777), doc.layers[0].surface().unwrap().pixel(511, 777)); + let (back, _) = crate::psd_to_document(&PsdFile::from_bytes(&bytes).unwrap()); + for (x, y) in [(0, 0), (1023, 0), (511, 777), (1023, 1023)] { + assert_eq!(back.layers[0].surface().unwrap().pixel(x, y), doc.layers[0].surface().unwrap().pixel(x, y), "{depth:?} ({x}, {y})"); + } + } +} + +/// The limit is the exact written length plus the save's headroom: one byte less is a PSB. +#[test] +fn the_size_limit_is_the_exact_encoded_length() { + let doc = sized_document(false, SampleType::U8); + let len = export_sized(&doc, u64::MAX).0.to_bytes().unwrap().len() as u64; + assert_eq!(export_sized(&doc, len + PSD_SAVE_HEADROOM).0.written_version(), Version::Psd); + assert_eq!(export_sized(&doc, len + PSD_SAVE_HEADROOM - 1).0.written_version(), Version::Psb); + // The real limit: a small document stays PSD, a forced one is PSB. let small = document(1, 1); - let small_size_exceeds_limit = psd_size_exceeds_limit(&small); - assert_eq!(psd_version(&small, false, small_size_exceeds_limit), Version::Psd); - assert_eq!(psd_version(&small, true, small_size_exceeds_limit), Version::Psb); + assert_eq!(document_to_psd(&small).written_version(), Version::Psd); + assert_eq!(document_to_psd_with(&small, &PsdExportOptions { force_psb: true, ..Default::default() }).0.written_version(), Version::Psb); + assert_eq!(psd_version(&document(30_001, 1), false), Version::Psb); } + +/// A Multichannel document follows the same rule. +#[test] +fn a_multichannel_file_over_the_limit_is_psb() { + let mut doc = document(64, 64); + doc.mode = ColorMode::Multichannel; + doc.channels.push(photocraft_doc::AlphaChannel::new("Cyan", photocraft_raster::Surface::new(PixelFormat::GRAY8))); + let (file, _) = export_sized(&doc, u64::MAX); + assert_eq!(file.written_version(), Version::Psd); + let len = file.to_bytes().unwrap().len() as u64; + let (file, warnings) = export_sized(&doc, len + PSD_SAVE_HEADROOM - 1); + assert_eq!(file.written_version(), Version::Psb); + assert!(warnings.iter().any(|w| w.contains("written as PSB")), "{warnings:?}"); + let back = PsdFile::from_bytes(&file.to_bytes().unwrap()).unwrap(); + assert_eq!(back.header.version, Version::Psb); + assert_eq!(back.decode_merged().unwrap(), PsdFile::from_bytes(&export_sized(&doc, u64::MAX).0.to_bytes().unwrap()).unwrap().decode_merged().unwrap()); +} + +/// Indexed Color and Bitmap (written without layers, #4816), Multichannel and RGB follow the +/// encoded-size rule, and a file turned into PSB by its size writes its global blocks with +/// 8-byte lengths as `8B64` (#7937), whatever signature they were kept with; a file that fits +/// stays a PSD with `8BIM` throughout. +#[test] +fn every_mode_promoted_by_size_signs_long_blocks_8b64() { + let white = photocraft_color::Color::rgba(1.0, 1.0, 1.0, 1.0); + let mut docs = Vec::new(); + for (mode, psd_mode) in [(ColorMode::Indexed, PsdMode::Indexed), (ColorMode::Bitmap, PsdMode::Bitmap), (ColorMode::Rgb, PsdMode::Rgb)] { + let mut doc = Document::with_background("modes", photocraft_geom::Size::new(64, 64), ColorMode::Rgb, SampleType::U8, white); + doc.mode = mode; + doc.color_table = Some(photocraft_doc::ColorTable { colors: vec![[255, 255, 255], [0, 0, 0]], transparent: None }); + docs.push((doc, psd_mode)); + } + let mut multichannel = document(64, 64); + multichannel.mode = ColorMode::Multichannel; + multichannel.channels.push(photocraft_doc::AlphaChannel::new("Cyan", photocraft_raster::Surface::new(PixelFormat::GRAY8))); + docs.push((multichannel, PsdMode::Multichannel)); + for (mut doc, psd_mode) in docs { + doc.metadata.psd_global_blocks = vec![ + (*b"8BIM", *b"cinf", std::sync::Arc::new(vec![0, 0, 0, 16, 0, 0, 0, 0])), + (*b"8BIM", *b"pths", std::sync::Arc::new(vec![0, 0, 0, 16, 0, 0, 0, 0])), + (*b"8B64", *b"FMsk", std::sync::Arc::new(vec![0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50])), + (*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new())), + ]; + let written = |limit| { + let (file, warnings) = export_sized(&doc, limit); + let bytes = file.to_bytes().unwrap(); + let back = PsdFile::from_bytes(&bytes).unwrap(); + let blocks = back.global_blocks.iter().map(|b| (b.signature, b.key)).collect::>(); + (bytes.len() as u64, back, blocks, warnings) + }; + let (len, small, blocks, _) = written(u64::MAX); + assert_eq!((small.header.version, small.header.color_mode), (Version::Psd, psd_mode), "{psd_mode:?}"); + assert_eq!(blocks, vec![(*b"8BIM", *b"cinf"), (*b"8BIM", *b"pths"), (*b"8BIM", *b"FMsk"), (*b"8BIM", *b"Patt")], "{psd_mode:?}: a PSD keeps 8BIM"); + // One byte short of the encoded length: the same document is written as PSB. + let (_, big, blocks, warnings) = written(len + PSD_SAVE_HEADROOM - 1); + assert_eq!((big.header.version, big.header.color_mode), (Version::Psb, psd_mode), "{psd_mode:?}: {warnings:?}"); + assert!(warnings.iter().any(|w| w == "encoded size exceeds 2 GB; written as PSB"), "{psd_mode:?}: {warnings:?}"); + assert_eq!(blocks, vec![(*b"8B64", *b"cinf"), (*b"8B64", *b"pths"), (*b"8B64", *b"FMsk"), (*b"8BIM", *b"Patt")], "{psd_mode:?}: a PSB signs 8B64"); + assert_eq!(big.decode_merged().unwrap(), small.decode_merged().unwrap(), "{psd_mode:?}"); + // Without `size_limit_psb` no mode is promoted by its size, the layerless ones included. + let opts = PsdExportOptions { size_limit_psb: false, ..PsdExportOptions::default() }; + let (kept, warnings) = document_to_psd_sized(&doc, &opts, 0, &Interrupt::NONE, len + PSD_SAVE_HEADROOM - 1); + assert_eq!((kept.written_version(), kept.header.color_mode), (Version::Psd, psd_mode), "{psd_mode:?}: {warnings:?}"); + assert!(!warnings.iter().any(|w| w.contains("written as PSB")), "{psd_mode:?}: {warnings:?}"); + } +} + #[test] fn kept_psb_blocks_are_written_as_8bim_in_a_psd() { // Photoshop 27.11 refuses a PSD holding `8B64` blocks ("not compatible with this diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index 2a0f982514..f26574153f 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -633,7 +633,9 @@ impl Ctx<'_> { let sd = rec.section_divider(); let expanded = sd.is_none_or(|s| s.kind != photocraft_psd::SectionType::ClosedFolder); let artboard = crate::comps_map::ARTBOARD_KEYS.iter().find_map(|k| rec.block(k)).and_then(|b| crate::comps_map::parse_artboard(&b.data)); - let mut l = Layer::new(rec.name(), LayerContent::Group(Group { children, expanded, artboard })); + // A Photoshop Frame's stroke: `vstk` on the frame group, in a shape's layout. + let frame_stroke = rec.block(b"vstk").and_then(|b| crate::vector_map::parse_vstk(&b.data, self.dpi)).and_then(|v| v.stroke); + let mut l = Layer::new(rec.name(), LayerContent::Group(Group { children, expanded, artboard, frame_stroke })); l.psd_blocks = preserved_blocks(rec); self.apply_common(&mut l, rec, sd.and_then(|s| s.blend_mode)); self.apply_vector_mask(&mut l, rec); diff --git a/crates/io/src/smart_map/export.rs b/crates/io/src/smart_map/export.rs index c8159201e4..810d1f49d5 100644 --- a/crates/io/src/smart_map/export.rs +++ b/crates/io/src/smart_map/export.rs @@ -111,7 +111,8 @@ fn enum_out(p: &J, key: &str, default: &str, type_id: &str, table: &[(&str, &str Ok(enumv(type_id, psd)) } -/// `key` rounded to a `long` in `lo..=hi`. +/// `key` rounded to a `long` in `lo..=hi` (clamped, and a non-number is the default, as the +/// engine reads Extrude, Tiles, Trace Contour, Crystallize and Pointillize). fn pi_in(p: &J, key: &str, default: f64, lo: i32, hi: i32) -> Value { Value::Integer(pf(p, key, default).round().clamp(f64::from(lo), f64::from(hi)) as i32) } diff --git a/crates/io/src/smart_map/tests.rs b/crates/io/src/smart_map/tests.rs index 1bbd5bd094..22808b08a2 100644 --- a/crates/io/src/smart_map/tests.rs +++ b/crates/io/src/smart_map/tests.rs @@ -1470,7 +1470,8 @@ fn stylize_and_pixelate_filters_open_from_photoshop_shapes() { fltr("filter.stylize.diffuse", json!({"mode": "normal", "seed": 7})), Some(Value::Descriptor(Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Nrml")))) ); - // Defaults and out-of-range values are written in Photoshop's ranges. + // Defaults and out-of-range values are written as the engine renders them: clamped to + // Photoshop's ranges, and a non-number as the default. assert_eq!( fltr("filter.stylize.traceContour", json!({})), Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(128)).with("Edg ", enumv("CntE", "Lwr ")))) @@ -1478,17 +1479,28 @@ fn stylize_and_pixelate_filters_open_from_photoshop_shapes() { assert_eq!(fltr("filter.pixelate.crystallize", json!({})), Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(10))))); assert_eq!(fltr("filter.pixelate.pointillize", json!({})), Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(5))))); assert_eq!( - fltr("filter.pixelate.crystallize", json!({"cellSize": 1e9})), + fltr("filter.pixelate.crystallize", json!({"cellSize": 300})), Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(300)))) ); + assert_eq!(fltr("filter.pixelate.pointillize", json!({"cellSize": 2.6})), Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(3))))); assert_eq!( - fltr("filter.pixelate.pointillize", json!({"cellSize": "big"})), - Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(5)))) - ); - assert_eq!( - fltr("filter.stylize.traceContour", json!({"level": -40, "edge": "upper"})), + fltr("filter.stylize.traceContour", json!({"level": 0, "edge": "upper"})), Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(0)).with("Edg ", enumv("CntE", "Upr ")))) ); + assert_eq!( + fltr("filter.stylize.traceContour", json!({"level": 255.4})), + Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(255)).with("Edg ", enumv("CntE", "Lwr ")))) + ); + let crst = |v: i32| Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(v)))); + let pntl = |v: i32| Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(v)))); + let trcc = |v: i32, e: &str| Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(v)).with("Edg ", enumv("CntE", e)))); + assert_eq!(fltr("filter.pixelate.crystallize", json!({"cellSize": 1e9})), crst(300)); + assert_eq!(fltr("filter.pixelate.crystallize", json!({"cellSize": 2.4})), crst(3)); + assert_eq!(fltr("filter.pixelate.pointillize", json!({"cellSize": "big"})), pntl(5)); + assert_eq!(fltr("filter.pixelate.pointillize", json!({"cellSize": null})), pntl(5)); + assert_eq!(fltr("filter.stylize.traceContour", json!({"level": -40, "edge": "upper"})), trcc(0, "Upr ")); + assert_eq!(fltr("filter.stylize.traceContour", json!({"level": 255.5})), trcc(255, "Lwr ")); + assert_eq!(fltr("filter.stylize.traceContour", json!({"level": [128]})), trcc(128, "Lwr ")); } #[test] diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index 98161732de..5b3bf5e272 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -286,32 +286,40 @@ fn font_resolver() -> usvg::FontResolver<'static> { /// Parse options for an untrusted drawing. `` resolves only `data:` URLs: a file path /// or relative href is never read, so opening an SVG cannot pull local files (`/etc/...`, /// `~/.ssh/...`) into the document. Embedded SVG images get no `` resolution at all -/// (`usvg` already blanks it for sub-SVGs). -fn options(fontdb: Arc) -> usvg::Options<'static> { +/// (`usvg` already blanks it for sub-SVGs). An embedded SVG may nest `depth_left` levels: it is +/// parsed on top of the drawing's own recursion, so the two depths share [`MAX_XML_DEPTH`]. +fn options(fontdb: Arc, depth_left: usize) -> usvg::Options<'static> { let default_resolver = usvg::ImageHrefResolver::default_data_resolver(); let resolve_data: usvg::ImageHrefDataResolverFn<'static> = Box::new(move |mime, data, opts| { // Reject embedded images whose decoded RGBA storage would exceed the budget before // resvg (which allocates the full intrinsic image before any destination scaling) ever // sees them. Formats without a cheap header probe fall through unchanged; their decode // path is bounded elsewhere or accepted as-is, matching the previous behavior. - if let Some(probed) = embedded_image_dimensions(mime, &data) { + let probed = embedded_image_dimensions(mime, &data); + if let Some(probed) = probed { let (w, h) = probed?; let decoded = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4); if decoded > MAX_EMBEDDED_IMAGE_BYTES { return None; } } - // An embedded `image/svg+xml` value can itself be gzip-compressed (SVGZ-in-data-URL). - // The default resolver's sub-SVG loader checks for the gzip magic and decompresses - // again with no byte limit of its own. Apply our bounded inflate first: if the data is - // gzip-compressed, hand the default resolver the already-decompressed (and - // size-checked) bytes, which no longer start with the gzip magic, so it parses them - // directly instead of decompressing a second time. - if (mime == "image/svg+xml" || mime == "text/plain") && data.starts_with(&[0x1f, 0x8b]) { - return match inflate(&data) { - Ok(xml) if !xml.starts_with(&[0x1f, 0x8b]) => default_resolver("image/svg+xml", std::sync::Arc::new(xml.into_owned()), opts), - _ => None, + // An embedded SVG (`image/svg+xml`, or `text/plain` that isn't one of the raster + // formats probed above) is parsed by the default resolver with none of `parse_with`'s + // checks, so they run here. It can itself be gzip-compressed (SVGZ-in-data-URL), and the + // default resolver's sub-SVG loader would decompress again with no byte limit of its + // own: hand it our bounded inflate's output instead, which no longer starts with the + // gzip magic. Nesting deeper than `depth_left` drops the image rather than overflowing + // the parser's stack. + if mime == "image/svg+xml" || (mime == "text/plain" && probed.is_none()) { + let (mime, xml) = match inflate(&data) { + Ok(std::borrow::Cow::Borrowed(_)) => (mime, Arc::clone(&data)), + Ok(std::borrow::Cow::Owned(xml)) if !xml.starts_with(&[0x1f, 0x8b]) => ("image/svg+xml", Arc::new(xml)), + _ => return None, }; + if nesting_depth(&xml, depth_left) > depth_left { + return None; + } + return default_resolver(mime, xml, opts); } default_resolver(mime, data, opts) }); @@ -344,13 +352,14 @@ fn parse(bytes: &[u8]) -> Result { /// [`parse`] with the font database `fonts` supplies (the tests hand in an empty one). fn parse_with(bytes: &[u8], fonts: fn() -> Arc) -> Result { let xml = inflate(bytes)?; - if nesting_depth(&xml, MAX_XML_DEPTH) > MAX_XML_DEPTH { + let depth = nesting_depth(&xml, MAX_XML_DEPTH); + if depth > MAX_XML_DEPTH { return Err(IoError::Svg(format!("elements are nested more than {MAX_XML_DEPTH} levels deep"))); } let has_text = xml.windows(4).any(|w| w == b"text"); let fontdb = if has_text { fonts() } else { Arc::new(usvg::fontdb::Database::new()) }; let fontless_text = has_text && fontdb.faces().next().is_none(); - let opt = options(fontdb); + let opt = options(fontdb, MAX_XML_DEPTH.saturating_sub(depth)); // Parse the already-bounded-and-decoded text with from_str, not from_data: from_data // re-checks the gzip magic and would decompress a second time without our size limit if // the inflated content happened to start with a nested gzip stream. @@ -379,7 +388,7 @@ fn inflate(bytes: &[u8]) -> Result, IoError> { /// then `limit + 1`). Comments, CDATA sections, processing instructions and the DOCTYPE add /// nothing; quoted attribute values may hold `>` or `/>`. Malformed markup is only counted /// loosely: the parser rejects it afterwards. -fn nesting_depth(xml: &[u8], limit: usize) -> usize { +pub(crate) fn nesting_depth(xml: &[u8], limit: usize) -> usize { let after = |from: usize, pat: &[u8]| -> usize { xml.get(from..).and_then(|s| s.windows(pat.len()).position(|w| w == pat)).map_or(xml.len(), |p| from.saturating_add(p).saturating_add(pat.len())) }; @@ -498,14 +507,47 @@ fn import_parsed_with_budget(name: &str, parsed: Parsed, max_group_depth: usize, pub fn rasterize(bytes: &[u8], scale: f32) -> Result { let tree = parse(bytes)?.tree; let cv = canvas(&tree, &mut Vec::new()); - let k = cv.scale * scale.clamp(1.0 / 64.0, 64.0); + let pixmap = render(&tree, cv.scale * scale.clamp(1.0 / 64.0, 64.0))?; + let rect = Rect::new(0, 0, pixmap.width() as i32, pixmap.height() as i32); + let px = pixmap.pixels().iter().map(|p| rgba(p.demultiply())).collect(); + Ok(photocraft_compose::Buffer { rect, px }) +} + +/// The whole drawing at one pixel per SVG unit, scaled down to fit [`MAX_SIDE`] a side and +/// [`MAX_RASTER_PIXELS`] in all: its width, height and straight-alpha RGBA8 pixels. Paste uses it +/// for a drawing copied as SVG markup. +pub fn rasterize_rgba8(bytes: &[u8]) -> Result<(u32, u32, Vec), IoError> { + let tree = parse(bytes)?.tree; + let cv = canvas(&tree, &mut Vec::new()); + let (w, h) = (f64::from(tree.size().width() * cv.scale), f64::from(tree.size().height() * cv.scale)); + // The largest k ≤ 1 with (w·k + 2)(h·k + 2) ≤ the limit: `render` rounds each side up, and + // f32 rounding can add one more pixel. + let max = MAX_RASTER_PIXELS as f64; + let fit = if (w + 2.0) * (h + 2.0) <= max { + 1.0 + } else { + let (a, b) = (w * h, 2.0 * (w + h)); + (-b + (b * b - 4.0 * a * (4.0 - max)).sqrt()) / (2.0 * a) + }; + let pixmap = render(&tree, cv.scale * fit as f32)?; + let px = pixmap + .pixels() + .iter() + .flat_map(|p| { + let c = p.demultiply(); + [c.red(), c.green(), c.blue(), c.alpha()] + }) + .collect(); + Ok((pixmap.width(), pixmap.height(), px)) +} + +/// `tree` drawn at `k` pixels per SVG unit on a transparent pixmap whose sides round up. +fn render(tree: &usvg::Tree, k: f32) -> Result { let side = |v: f32| ((v * k).ceil() as u32).max(1); let (w, h) = (side(tree.size().width()), side(tree.size().height())); let mut pixmap = pixmap(w, h).ok_or_else(|| IoError::Svg(format!("{w} × {h} px is too large to rasterise")))?; - resvg::render(&tree, tiny_skia::Transform::from_scale(k, k), &mut pixmap.as_mut()); - let rect = Rect::new(0, 0, w as i32, h as i32); - let px = pixmap.pixels().iter().map(|p| rgba(p.demultiply())).collect(); - Ok(photocraft_compose::Buffer { rect, px }) + resvg::render(tree, tiny_skia::Transform::from_scale(k, k), &mut pixmap.as_mut()); + Ok(pixmap) } /// A transparent pixmap, or `None` past [`MAX_RASTER_PIXELS`]. @@ -672,7 +714,7 @@ impl Conv { } return; } - let mut l = Layer::new(self.name(g.id(), 1), LayerContent::Group(Group { children, expanded: false, artboard: None })); + let mut l = Layer::new(self.name(g.id(), 1), LayerContent::Group(Group { children, expanded: false, artboard: None, frame_stroke: None })); l.opacity = g.opacity().get(); l.blend = blend(g.blend_mode()); l.vector_mask = clip; @@ -699,7 +741,7 @@ impl Conv { } else { t.id().to_string() }; - out.push(Layer::new(name, LayerContent::Group(Group { children, expanded: false, artboard: None }))); + out.push(Layer::new(name, LayerContent::Group(Group { children, expanded: false, artboard: None, frame_stroke: None }))); } } } diff --git a/crates/io/src/svg/tests.rs b/crates/io/src/svg/tests.rs index 7127738daa..426962fa9c 100644 --- a/crates/io/src/svg/tests.rs +++ b/crates/io/src/svg/tests.rs @@ -414,9 +414,31 @@ fn huge_drawings_scale_to_the_side_limit() { assert_eq!(w.len(), 1); } +/// A pasted drawing is one pixel per SVG unit, and a drawing past the pixel limit is scaled +/// down to fit it, keeping its proportions, instead of failing. +#[test] +fn rgba8_renders_at_its_own_size_within_the_pixel_limit() { + let svg = br##""##; + let (w, h, px) = rasterize_rgba8(svg).unwrap(); + assert_eq!((w, h, px.len()), (40, 20, 40 * 20 * 4)); + assert_eq!(px[..4], [255, 0, 0, 255], "the rectangle"); + assert_eq!(px[(10 * 40 + 30) * 4..][..4], [0, 0, 0, 0], "transparent beside it"); + for (side_w, side_h) in [(10_000u32, 5_000u32), (20_000, 20_000), (20_000, 2_000)] { + let svg = format!(""); + let (w, h, px) = rasterize_rgba8(svg.as_bytes()).unwrap(); + let pixels = u64::from(w) * u64::from(h); + assert!(pixels <= MAX_RASTER_PIXELS && pixels > MAX_RASTER_PIXELS / 2, "{side_w} × {side_h} became {w} × {h}"); + assert!(w <= MAX_SIDE && h <= MAX_SIDE, "{w} × {h}"); + let (aspect, want) = (f64::from(w) / f64::from(h), f64::from(side_w) / f64::from(side_h)); + assert!((aspect / want - 1.0).abs() < 0.01, "{w} × {h} keeps {side_w} × {side_h}"); + assert_eq!(px.len() as u64, pixels * 4); + } + assert!(rasterize_rgba8(b""; let once = gz(svg); diff --git a/crates/io/src/tiff_layers.rs b/crates/io/src/tiff_layers.rs index 2949a551cc..fc08d76452 100644 --- a/crates/io/src/tiff_layers.rs +++ b/crates/io/src/tiff_layers.rs @@ -233,10 +233,9 @@ pub(crate) fn export_layered(doc: &Document, opts: &ExportOptions, ctl: &photocr // TIFF already round-trips as. // Layer data is written PSD-style ("Adobe Photoshop Document Data Block", 4-byte lengths), as // Photoshop writes it: Photoshop does not read the PSB-style header ("... Data V0002") in a - // TIFF and opens only the composite, as one locked Background. The exporter's size estimate - // counts layer planes uncompressed, so a document with many canvas-sized layers passes 2 GB - // while its real layer data is a small fraction of that; it is not used here. PSB-style data - // is written only past 30000 px, when asked for, or when a length really overflows 32 bits. + // TIFF and opens only the composite, as one locked Background. The exporter's switch to PSB + // on the encoded file size is not used here: PSB-style data is written only past 30000 px, + // when asked for, or when a length really overflows 32 bits. let user_channels = !doc.channels.is_empty() || doc.quick_mask.is_some(); let force_psb = opts.force_psb || big; let psd_opts = PsdExportOptions { @@ -970,11 +969,11 @@ mod tests { use crate::{ExportOptions, export}; - /// Hidden solid fills on a 2048² canvas: the PSD size estimate counts each one as a - /// canvas-sized plane (16 MiB at 8 bits), so 140 of them pass 2 GB, while their real pixels - /// (the cached rendering, pruned to nothing) take almost no space. A layered TIFF of such a - /// document must still carry PSD-style layer data: Photoshop does not read the PSB-style - /// header in a TIFF and opens only the composite, as one locked Background. + /// Hidden solid fills on a 2048² canvas: counted as canvas-sized planes (16 MiB each at 8 + /// bits), 140 of them pass 2 GB, while their real pixels (the cached rendering, pruned to + /// nothing) take almost no space. A layered TIFF of such a document must still carry + /// PSD-style layer data: Photoshop does not read the PSB-style header in a TIFF and opens only + /// the composite, as one locked Background. fn many_cheap_fills() -> Document { let mut doc = Document::new("many fills", photocraft_geom::Size::new(2048, 2048), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); for i in 0..140 { @@ -990,7 +989,6 @@ mod tests { #[test] fn layered_tiff_ignores_the_uncompressed_size_estimate() { let doc = many_cheap_fills(); - assert!(crate::psd_export::psd_size_exceeds_limit(&doc), "the fixture must pass the 2 GB estimate"); let r = export(&doc, "x.tif", &ExportOptions { tiff_layers: true, ..Default::default() }).unwrap(); let (_, layers) = photocraft_codecs::tiff_photoshop_tags(&r.bytes); @@ -1000,9 +998,9 @@ mod tests { let back = crate::import("x.tif", &r.bytes).unwrap().document; assert_eq!(back.layers.len(), doc.layers.len()); - // A .psd save still switches to PSB on the estimate, as before. + // A .psd save switches to PSB on the encoded size, which is small here: it stays PSD. let psd = export(&doc, "x.psd", &ExportOptions::default()).unwrap(); - assert_eq!(psd.bytes.get(4..6), Some(&[0u8, 2][..]), "PSD saves keep the size-estimate switch"); + assert_eq!(psd.bytes.get(4..6), Some(&[0u8, 1][..]), "PSD saves switch on the encoded size"); } #[test] diff --git a/crates/io/src/xml.rs b/crates/io/src/xml.rs new file mode 100644 index 0000000000..13e02004d1 --- /dev/null +++ b/crates/io/src/xml.rs @@ -0,0 +1,156 @@ +//! XML parsing with a nesting cap. roxmltree recurses once per element level, so a few thousand +//! nested tags (a 30 KB file or command param) overflow a 2 MB thread stack, an abort no guard +//! can catch. Every roxmltree caller that reads a file or a param goes through [`parse`]. + +use roxmltree::{Document, ParsingOptions}; + +/// Deepest element nesting accepted, the same as for SVG drawings: real files stay far below. +pub const MAX_DEPTH: usize = crate::svg::MAX_XML_DEPTH; + +/// Parses `text`, refusing nesting deeper than [`MAX_DEPTH`] before roxmltree sees it. +/// +/// With `opts.allow_dtd` an internal DTD subset is refused too: its entities can expand into +/// markup the depth check never sees. An external DOCTYPE (a public or system id) still parses. +pub fn parse<'i>(text: &'i str, opts: ParsingOptions<'i>) -> Result, String> { + if opts.allow_dtd && internal_subset(text.as_bytes()) { + return Err("internal DTD subsets aren't supported".into()); + } + if crate::svg::nesting_depth(text.as_bytes(), MAX_DEPTH) > MAX_DEPTH { + return Err(format!("elements are nested more than {MAX_DEPTH} levels deep")); + } + Document::parse_with_options(text, opts).map_err(|e| e.to_string()) +} + +/// Does a DOCTYPE have an internal subset (``)? Every `` in the XML +/// declaration must not hide it. One inside a comment may be refused needlessly; that fails safe. +/// +/// Each `` ends a DOCTYPE without one. Scans in +/// the same quote state go on alike, so one pass keeps just three flags (outside quotes, in `"`, +/// in `'`) and stays linear however many ` bool { + const DOCTYPE: &[u8] = b"')) || (double && b == b'"') || (single && b == b'\''), + (double && b != b'"') || (bare && b == b'"'), + (single && b != b'\'') || (bare && b == b'\''), + ); + } + false +} + +#[cfg(test)] +mod tests { + use super::*; + + fn nested(levels: usize) -> String { + format!("{}{}", "".repeat(levels), "".repeat(levels)) + } + + fn dtd<'a>() -> ParsingOptions<'a> { + ParsingOptions { allow_dtd: true, ..Default::default() } + } + + #[test] + fn nesting_past_the_cap_is_an_error_not_a_stack_overflow() { + let deep = nested(20_000); + std::thread::Builder::new() + .stack_size(512 * 1024) + .spawn(move || { + for opts in [ParsingOptions::default(), dtd()] { + let e = parse(&deep, opts).err().unwrap_or_else(|| panic!("parsed")); + assert!(e.contains("nested") && e.contains(&MAX_DEPTH.to_string()), "{e}"); + } + }) + .unwrap() + .join() + .unwrap(); + // The root plus MAX_DEPTH - 1 levels is exactly MAX_DEPTH deep. + assert!(parse(&nested(MAX_DEPTH - 1), ParsingOptions::default()).is_ok()); + assert!(parse(&nested(MAX_DEPTH), ParsingOptions::default()).is_err()); + } + + #[test] + fn markup_in_comments_cdata_pis_and_attributes_adds_no_depth() { + let g = "".repeat(2 * MAX_DEPTH); + let xml = format!("\"> {g} "); + assert!(parse(&xml, ParsingOptions::default()).is_ok()); + } + + /// The scan per ` bool { + const DOCTYPE: &[u8] = b" quote = None, + Some(_) => {} + None if b == b'"' || b == b'\'' => quote = Some(b), + None if b == b'[' => return true, + None if b == b'>' => return false, + None => {} + } + } + false + }) + } + + #[test] + fn the_one_pass_doctype_scan_agrees_with_a_scan_per_doctype() { + let parts: [&[u8]; 7] = [b"", b"a", b"", "\n\n"; + assert!(parse(krita, dtd()).is_ok()); + let g = ("".repeat(20_000), "".repeat(20_000)); + for xml in [ + "]>&e;".to_string(), + "\u{feff} ]>".to_string(), + format!("]>&e;", g.0, g.1), + // roxmltree reads the declaration's values as quoted attributes, so they may hold `?>`. + format!("\"?>]>&e;", g.0, g.1), + format!("\"?>]>{}{}", g.0, g.1), + ] { + let e = parse(&xml, dtd()).err().unwrap_or_else(|| panic!("parsed {xml:.60}")); + assert!(e.contains("internal DTD"), "{e}"); + } + // Without allow_dtd roxmltree refuses any DOCTYPE itself. + assert!(parse(krita, ParsingOptions::default()).is_err()); + } +} diff --git a/crates/io/tests/it/adjust_blend_roundtrip.rs b/crates/io/tests/it/adjust_blend_roundtrip.rs index 1159836047..bab904f58c 100644 --- a/crates/io/tests/it/adjust_blend_roundtrip.rs +++ b/crates/io/tests/it/adjust_blend_roundtrip.rs @@ -80,9 +80,20 @@ fn adjustments() -> Vec<(Adjustment, bool)> { (Adjustment::PhotoFilter { color: [q16(0), q16(39321), q16(65535)], density: 0.3, preserve_luminosity: false }, true), (Adjustment::ChannelMixer { matrix: [[0.5, 0.3, 0.2, 0.0], [0.1, 0.8, 0.1, 0.05], [0.0, 0.2, 0.9, -0.05]], monochrome: false }, true), (Adjustment::ChannelMixer { matrix: [[0.4, 0.4, 0.2, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], monochrome: true }, true), - (Adjustment::ColorLookup { name: "Look.cube".into(), lut: Some(Arc::new(lut.data)), size: 5, tetrahedral: false, dither: false }, true), + // The LUT file is embedded as loaded (here a .cube), so it reads back with its source. + ( + Adjustment::ColorLookup { + name: "Look.cube".into(), + source: Some(adjust::LutSource { format: adjust::LutFormat::Cube, bytes: Arc::new(photocraft_cms::lutfile::write_cube(&lut).into_bytes()) }), + lut: Some(Arc::new(lut.data)), + size: 5, + tetrahedral: false, + dither: false, + }, + true, + ), // No table chosen: written as an identity cube (renders the same, comes back with a table). - (Adjustment::ColorLookup { name: String::new(), lut: None, size: 0, tetrahedral: false, dither: false }, false), + (Adjustment::ColorLookup { name: String::new(), lut: None, size: 0, tetrahedral: false, dither: false, source: None }, false), (Adjustment::Invert, true), (Adjustment::Posterize { levels: 4 }, true), (Adjustment::Threshold { level: g(140) }, true), @@ -207,7 +218,8 @@ fn every_adjustment_kind_round_trips_16bit() { fn color_lookup_tetrahedral_warns_on_psd_save() { let lut = photocraft_cms::lutfile::LutFile::from_fn("t", 2, |c| if c == [1.0; 3] { [1.0; 3] } else { [0.0; 3] }); for tetrahedral in [true, false] { - let adj = Adjustment::ColorLookup { name: "test.cube".into(), lut: Some(Arc::new(lut.data.clone())), size: 2, tetrahedral, dither: false }; + let adj = + Adjustment::ColorLookup { name: "test.cube".into(), lut: Some(Arc::new(lut.data.clone())), size: 2, tetrahedral, dither: false, source: None }; let mut d = Document::new("lut", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8); let fmt = d.pixel_format(); d.layers.push(raster("Background", fmt, d.bounds(), 1, false)); diff --git a/crates/io/tests/it/adjust_warnings.rs b/crates/io/tests/it/adjust_warnings.rs index c9199835d2..0e4e822527 100644 --- a/crates/io/tests/it/adjust_warnings.rs +++ b/crates/io/tests/it/adjust_warnings.rs @@ -36,7 +36,8 @@ fn unreadable_adjustments_warn_on_open() { #[test] fn readable_adjustments_open_without_warnings() { let lut: Vec = (0..8).flat_map(|i| [(i & 1) as f32, ((i >> 1) & 1) as f32, ((i >> 2) & 1) as f32]).collect(); - let lookup = Adjustment::ColorLookup { name: "id.cube".into(), size: 2, lut: Some(std::sync::Arc::new(lut)), tetrahedral: false, dither: false }; + let lookup = + Adjustment::ColorLookup { name: "id.cube".into(), size: 2, lut: Some(std::sync::Arc::new(lut)), tetrahedral: false, dither: false, source: None }; for adj in [lookup, Adjustment::Invert] { let (doc, warnings) = reopen(adj, "x.psd", &ExportOptions::default()); assert!(!matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::Unsupported { .. }))); @@ -114,10 +115,15 @@ fn photoshop_layout_color_lookup_round_trips_through_psb() { let opts = ExportOptions { force_psb: true, ..Default::default() }; let (doc, warnings) = reopen(Adjustment::Unsupported { psd_key: "clrL".into(), raw: raw.as_slice().into() }, "x.psb", &opts); assert!(warnings.iter().all(|w| !w.contains("Teal Look")), "{warnings:?}"); - let LayerContent::Adjustment(Adjustment::ColorLookup { name, size, lut: Some(table), tetrahedral: false, dither: false }) = &doc.layers[0].content else { + let LayerContent::Adjustment(Adjustment::ColorLookup { name, size, lut: Some(table), tetrahedral: false, dither: false, source: Some(source) }) = + &doc.layers[0].content + else { panic!("{:?}", doc.layers[0].content) }; assert_eq!(name, "frost.cube"); + // The loaded `.cube` file is kept as its source, so a later save can embed it unchanged. + assert_eq!(source.format, photocraft_doc::adjust::LutFormat::Cube); + assert_eq!(source.bytes.as_slice(), b"LUT_3D_SIZE 2\n0 0 0\n0 1 1\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"); assert_eq!(*size, 2); let want: Vec = [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0].into(); assert_eq!(table.as_slice(), want.as_slice()); @@ -139,18 +145,32 @@ fn photoshop_layout_color_lookup_round_trips_through_psb() { fn short_color_lookup_table_warns_on_open() { // A stored table shorter than size³×3 renders as the identity; opening must say so, naming // the layer (#1763). - let short = Adjustment::ColorLookup { name: "t.cube".into(), size: 3, lut: Some(std::sync::Arc::new(vec![0.5; 7])), tetrahedral: false, dither: false }; + let short = Adjustment::ColorLookup { + name: "t.cube".into(), + size: 3, + lut: Some(std::sync::Arc::new(vec![0.5; 7])), + tetrahedral: false, + dither: false, + source: None, + }; let (doc, warnings) = reopen(short, "x.pcraft", &ExportOptions::default()); assert!(matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::ColorLookup { .. })), "{:?}", doc.layers[0].content); let w: Vec<&String> = warnings.iter().filter(|w| w.contains("Teal Look")).collect(); assert_eq!(w.len(), 1, "{warnings:?}"); assert!(w[0].contains("Color Lookup") && w[0].contains("7 values") && w[0].contains("81"), "{}", w[0]); // A table long enough for its size opens without the note. - let ok = Adjustment::ColorLookup { name: "t.cube".into(), size: 2, lut: Some(std::sync::Arc::new(vec![0.5; 24])), tetrahedral: false, dither: false }; + let ok = Adjustment::ColorLookup { + name: "t.cube".into(), + size: 2, + lut: Some(std::sync::Arc::new(vec![0.5; 24])), + tetrahedral: false, + dither: false, + source: None, + }; let (_, warnings) = reopen(ok, "x.pcraft", &ExportOptions::default()); assert!(warnings.iter().all(|w| !w.contains("Teal Look")), "{warnings:?}"); // A missing table renders as the identity too. - let none = Adjustment::ColorLookup { name: "t.cube".into(), size: 2, lut: None, tetrahedral: false, dither: false }; + let none = Adjustment::ColorLookup { name: "t.cube".into(), size: 2, lut: None, tetrahedral: false, dither: false, source: None }; let (_, warnings) = reopen(none, "x.pcraft", &ExportOptions::default()); assert!(warnings.iter().any(|w| w.contains("Teal Look") && w.contains("no usable table")), "{warnings:?}"); } diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 94f4ba715d..27b7b9b4ef 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -50,6 +50,7 @@ mod psd_block_layout; mod psd_damaged_merged; mod psd_empty_channels; mod psd_font_identity; +mod psd_frame_stroke; mod psd_kerning; mod psd_layer_block_length; mod psd_maximize_compatibility; diff --git a/crates/io/tests/it/psd_frame_stroke.rs b/crates/io/tests/it/psd_frame_stroke.rs new file mode 100644 index 0000000000..3ae2284543 --- /dev/null +++ b/crates/io/tests/it/psd_frame_stroke.rs @@ -0,0 +1,117 @@ +//! A Photoshop Frame (Frame tool, K) is a layer group whose record carries the frame outline as a +//! vector mask (`vsms`) and its Properties › Stroke as a shape-style `vstk`. The stroke draws +//! along the outline even when nothing is placed in the frame (#4381). + +use photocraft_color::{Color, ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{Document, Fill, Group, Layer, LayerContent, Path, ShapeStroke, Size, StrokeAlign, Subpath, VectorMask}; +use photocraft_io::vector_map::{parse_vstk, vstk_bytes}; +use photocraft_io::*; +use photocraft_psd::{PsdFile, SectionType, TaggedBlock}; + +fn red_inside(width: f32) -> ShapeStroke { + ShapeStroke { width, align: StrokeAlign::Inside, paint: Fill::Solid(Color::rgb(1.0, 0.0, 0.0)), ..ShapeStroke::default() } +} + +/// An 80 × 60 white document with an empty rectangular frame over (20, 10)–(60, 50). +fn frame_doc(depth: SampleType, stroke: Option) -> Document { + let mut d = Document::with_background("f", Size::new(80, 60), ColorMode::Rgb, depth, Color::WHITE); + let mut g = Layer::group("Frame 1", vec![]); + if let LayerContent::Group(Group { frame_stroke, .. }) = &mut g.content { + *frame_stroke = stroke; + } + g.vector_mask = Some(VectorMask::new(Path::new(vec![Subpath::polygon(&[(20.0, 10.0), (60.0, 10.0), (60.0, 50.0), (20.0, 50.0)])]))); + d.layers.push(g); + d +} + +fn frame_stroke(d: &Document) -> Option { + d.layers.iter().find_map(|l| match &l.content { + LayerContent::Group(g) => Some(g.frame_stroke.clone()), + _ => None, + })? +} + +fn px(d: &Document, x: i32, y: i32) -> [f32; 4] { + photocraft_compose::flatten_to_surface(d, PixelFormat::RGBA8.with_sample(SampleType::F32), None).rgba(x, y) +} + +/// The group's own record (its top, `lsct` 1 or 2) in a PSD file. +fn frame_record(f: &mut PsdFile) -> &mut photocraft_psd::LayerRecord { + f.layers_mut() + .iter_mut() + .find(|r| r.section_divider().is_some_and(|s| matches!(s.kind, SectionType::OpenFolder | SectionType::ClosedFolder))) + .expect("the frame's group record") +} + +#[test] +fn a_photoshop_frames_vstk_draws_its_stroke_along_the_outline() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + // A file shaped like Photoshop's: the group record carries `vsms` and a `vstk`. + let mut f = document_to_psd(&frame_doc(depth, None)); + frame_record(&mut f).blocks.push(TaggedBlock::new(*b"vstk", vstk_bytes(Some(&red_inside(4.0)), true, 72.0))); + let (d, _) = psd_to_document(&f); + assert_eq!(frame_stroke(&d), Some(red_inside(4.0)), "{depth:?}"); + // Inside the outline, 4 px wide: red on the frame's edge band, white in the middle and + // outside it. + let close = |a: [f32; 4], b: [f32; 3]| (0..3).all(|i| (a[i] - b[i]).abs() < 0.02); + for (x, y) in [(21, 30), (58, 30), (40, 11), (40, 48)] { + assert!(close(px(&d, x, y), [1.0, 0.0, 0.0]), "{depth:?} ({x}, {y}): {:?}", px(&d, x, y)); + } + for (x, y) in [(40, 30), (18, 30), (62, 30), (40, 8)] { + assert!(close(px(&d, x, y), [1.0, 1.0, 1.0]), "{depth:?} ({x}, {y}): {:?}", px(&d, x, y)); + } + } +} + +#[test] +fn a_frame_stroke_round_trips_through_psd() { + let stroke = ShapeStroke { dashes: vec![2.0, 1.0], opacity: 0.5, align: StrokeAlign::Outside, ..red_inside(3.0) }; + let d = frame_doc(SampleType::U8, Some(stroke.clone())); + let bytes = export(&d, "f.psd", &ExportOptions::default()).unwrap().bytes; + let back = import("f.psd", &bytes).unwrap().document; + assert_eq!(frame_stroke(&back), Some(stroke.clone())); + // Outside the outline at half opacity: pink just outside the edge. + let p = px(&back, 18, 30); + assert!((p[1] - 0.5).abs() < 0.05 && p[0] > 0.98, "{p:?}"); + + // Saved again unchanged, the imported `vstk` stays byte for byte. + let mut f = PsdFile::from_bytes(&bytes).unwrap(); + let first = frame_record(&mut f).block(b"vstk").unwrap().data.clone(); + let again = export(&back, "f.psd", &ExportOptions::default()).unwrap().bytes; + let mut f2 = PsdFile::from_bytes(&again).unwrap(); + assert_eq!(frame_record(&mut f2).block(b"vstk").unwrap().data, first); + + // Removing the stroke writes it disabled; no stroke at all writes no `vstk`. + let mut cleared = back.clone(); + if let Some(LayerContent::Group(g)) = cleared.layers.iter_mut().map(|l| &mut l.content).find(|c| matches!(c, LayerContent::Group(_))) { + g.frame_stroke = None; + } + let out = export(&cleared, "f.psd", &ExportOptions::default()).unwrap().bytes; + let mut f3 = PsdFile::from_bytes(&out).unwrap(); + assert_eq!(parse_vstk(&frame_record(&mut f3).block(b"vstk").unwrap().data, 72.0).unwrap().stroke, None); + let plain = export(&frame_doc(SampleType::U8, None), "f.psd", &ExportOptions::default()).unwrap().bytes; + let mut f4 = PsdFile::from_bytes(&plain).unwrap(); + assert!(frame_record(&mut f4).block(b"vstk").is_none()); +} + +#[test] +fn a_frame_stroke_without_a_vector_mask_or_width_draws_nothing() { + let mut d = frame_doc(SampleType::U8, Some(red_inside(4.0))); + d.layers[1].vector_mask = None; + assert_eq!(px(&d, 21, 30), [1.0, 1.0, 1.0, 1.0]); + let d = frame_doc(SampleType::U8, Some(ShapeStroke { width: 0.0, ..red_inside(4.0) })); + assert_eq!(px(&d, 21, 30), [1.0, 1.0, 1.0, 1.0]); + // Hostile widths from a file render without panicking. + for width in [f32::NAN, f32::INFINITY, -5.0, 1e9] { + let _ = px(&frame_doc(SampleType::U8, Some(ShapeStroke { width, ..red_inside(4.0) })), 21, 30); + } +} + +#[test] +fn an_isolated_frame_draws_its_outside_stroke_past_the_outline() { + let mut d = frame_doc(SampleType::U8, Some(ShapeStroke { align: StrokeAlign::Outside, ..red_inside(3.0) })); + d.layers[1].blend = photocraft_color::BlendMode::Normal; + let p = px(&d, 18, 30); + assert!(p[0] > 0.98 && p[1] < 0.02, "{p:?}"); + assert!(px(&d, 22, 30)[1] > 0.98, "inside stays white"); +} diff --git a/crates/io/tests/it/svg.rs b/crates/io/tests/it/svg.rs index c71bb0ae4f..cd533321ec 100644 --- a/crates/io/tests/it/svg.rs +++ b/crates/io/tests/it/svg.rs @@ -328,6 +328,66 @@ fn gzip_compressed_embedded_svg_decompresses_once_with_a_size_cap() { assert!(r.document.layers.iter().all(|l| !matches!(l.content, LayerContent::Raster(_))), "{:?}", r.document.layers); } +/// usvg parses an SVG embedded as a `data:` image on its own, past the drawing's depth check: +/// 20 000 nested groups in it overflowed a 2 MB thread stack, an abort nothing can catch. The +/// embedded SVG gets the same check now, so the image is dropped and the drawing opens. +#[test] +fn deeply_nested_embedded_svg_is_dropped_not_a_stack_overflow() { + let deep = nested(20_000); + let percent: String = deep.bytes().map(|b| if b.is_ascii_alphanumeric() { char::from(b).to_string() } else { format!("%{b:02X}") }).collect(); + let urls = [ + format!("data:image/svg+xml,{percent}"), + base64_data_url("image/svg+xml", deep.as_bytes()), + base64_data_url("text/plain", deep.as_bytes()), + base64_data_url("image/svg+xml", &gzip(deep.as_bytes())), + base64_data_url("text/plain", &gzip(deep.as_bytes())), + ]; + let embed = |url: &str| format!(""); + std::thread::Builder::new() + .stack_size(2 << 20) + .spawn(move || { + for url in &urls { + let r = open(&embed(url)); + assert!(r.document.layers.iter().all(|l| !matches!(l.content, LayerContent::Raster(_))), "{}: {:?}", &url[..20], r.document.layers); + } + // Just inside the limit (the drawing's own `` takes one level) the embedded + // drawing still renders (its rect is black). + let r = open(&embed(&base64_data_url("image/svg+xml", nested(svg::MAX_XML_DEPTH - 2).as_bytes()))); + assert!(close(px(&r.document, 4, 4), [0.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 4, 4)); + }) + .unwrap() + .join() + .unwrap(); + // A PNG sent as `text/plain` is still an image, not markup to count. + let r = open(&format!( + "", + png_data_url().replace("image/png", "text/plain") + )); + assert!(close(px(&r.document, 1, 1), [1.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 1, 1)); +} + +/// The embedded SVG is parsed on top of the drawing's own recursion, so the two depths share +/// [`svg::MAX_XML_DEPTH`]: 511 + 511 groups, each fine alone, overflowed a 1 MB thread stack. +#[test] +fn an_embedded_svg_gets_only_the_nesting_depth_the_drawing_left() { + let embed = |outer: usize, inner: usize| { + let url = base64_data_url("image/svg+xml", nested(inner).as_bytes()); + format!("{}{}", "".repeat(outer), "".repeat(outer)) + }; + std::thread::Builder::new() + .stack_size(1 << 20) + .spawn(move || { + let r = open(&embed(svg::MAX_XML_DEPTH - 1, svg::MAX_XML_DEPTH - 1)); + assert!(px(&r.document, 4, 4)[3] < 0.01, "{:?}", px(&r.document, 4, 4)); + // Within the shared budget the embedded drawing renders (its rect is black). + let r = open(&embed(250, 250)); + assert!(close(px(&r.document, 4, 4), [0.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 4, 4)); + }) + .unwrap() + .join() + .unwrap(); +} + /// 8 000 nested groups (70 KB) overflowed the XML parser's recursion on a 2 MB thread stack, an /// abort nothing can catch. It is an error now, found before the parser runs; the thread here /// has a small stack so a regression aborts the test instead of passing by luck. diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs index 06d24e35ed..822af3804c 100644 --- a/crates/ops/src/lib.rs +++ b/crates/ops/src/lib.rs @@ -33,6 +33,9 @@ pub struct HistoryState { pub doc: Arc, /// The layers targeted when this state was created. pub layers: LayerTarget, + /// The layers whose visibility the step that created this state changed, as stepping to this + /// state from the one before it in the list must apply it; see [`History::current_visibility`]. + pub visibility: Vec, /// Stable while the state is held: indices shift when older states fall off, ids don't. pub id: u64, } @@ -52,6 +55,16 @@ pub struct History { current_label: String, /// The layers targeted when the current document was created (opened or edited). current_layers: LayerTarget, + /// [`HistoryState::visibility`] of the current state. + current_visibility: Vec, + /// History Options › Allow Non-Linear History: a step recorded while later states exist + /// (after stepping back) keeps them and goes to the end of the list, instead of discarding + /// them; see [`History::record`] and [`History::remove`]. + pub non_linear: bool, + /// The most recent [`History::record`], while it is still the latest change to the list; + /// `None` once anything else changed it. [`History::rollback`] needs it to take the step back + /// exactly. + last_record: Option, /// The states History panel › Clear History took off the list, kept so the next undo puts /// them back (Clear History is itself undoable). Any new step drops them. cleared: Option>, @@ -63,6 +76,21 @@ pub struct History { brush_source: Option, } +/// What [`History::rollback`] needs to take the most recent [`History::record`] back. +#[derive(Clone, Debug)] +struct LastRecord { + /// The document it replaced (the state a rollback makes current again). + before: Arc, + /// How many former redo states it moved onto the undo side (always 0 in linear history). + moved: usize, + /// The states it (and the [`History::trim`] after it) dropped for the state limit or the + /// byte budget, in the order they were taken off, each with its index in the undo list at + /// that moment (not always the front: the History Brush's source is never dropped). + /// Their tiles are no longer counted; they are only held until the next change to the + /// list, so a rollback can put them back where they were. + evicted: Vec<(usize, HistoryState)>, +} + /// The undo and redo states [`History::clear_states`] removed, and the History Brush source set /// before it. #[derive(Clone, Debug)] @@ -91,6 +119,9 @@ impl History { max_bytes: 0, current_label: "Open".into(), current_layers: LayerTarget::default(), + current_visibility: Vec::new(), + non_linear: false, + last_record: None, cleared: None, current_id: 0, next_id: 1, @@ -100,6 +131,11 @@ impl History { /// Record that `before` was replaced by a new current document via step `label`, which left /// `layers` targeted. + /// + /// Linear history (the default) discards the redo states, as Photoshop does when you edit + /// after selecting an earlier state. With [`History::non_linear`] they are kept: the list + /// reads `…, before, the former redo states (oldest first), the new step`, so the new step is + /// appended at the end and every earlier state can still be stepped to. pub fn record(&mut self, label: impl Into, before: Arc, layers: LayerTarget) { if self.max_bytes == 0 { self.tiles = None; @@ -113,19 +149,36 @@ impl History { } let prev_label = std::mem::replace(&mut self.current_label, label.into()); let prev_layers = std::mem::replace(&mut self.current_layers, layers); + let prev_visibility = std::mem::take(&mut self.current_visibility); let prev_id = std::mem::replace(&mut self.current_id, self.next_id); self.next_id = self.next_id.wrapping_add(1); - self.undo.push_back(HistoryState { label: prev_label, doc: before, layers: prev_layers, id: prev_id }); - self.clear_redo(); + self.undo.push_back(HistoryState { label: prev_label, doc: before.clone(), layers: prev_layers, visibility: prev_visibility, id: prev_id }); + let mut moved = 0; + if self.non_linear { + // `redo` holds the next state last; the list order is oldest first. Their tiles stay + // counted: they only move from the redo side to the undo side. + while let Some(state) = self.redo.pop() { + // The new step follows the last of them in the list but was made from `before`: + // stepping back to them walks back over their steps. + merge_ids(&mut self.current_visibility, &state.visibility); + self.undo.push_back(state); + moved += 1; + } + } else { + self.clear_redo(); + } + let mut evicted = Vec::new(); while self.undo.len() > self.max_states { // The History Brush's source doesn't fall off (as in Photoshop): the next oldest does. let pinned = self.max_states > 1 && self.undo.front().is_some_and(|s| Some(s.id) == self.brush_source); - if let Some(state) = self.undo.remove(usize::from(pinned)) - && let Some(tiles) = &mut self.tiles - { + let at = usize::from(pinned); + let Some(state) = self.undo.remove(at) else { break }; + if let Some(tiles) = &mut self.tiles { tiles.remove(&state.doc); } + evicted.push((at, state)); } + self.last_record = Some(LastRecord { before, moved, evicted }); } pub fn can_undo(&self) -> bool { @@ -154,6 +207,21 @@ impl History { self.current_layers = layers; } + /// The layers whose visibility the step that produced the current document changed, as the + /// session reported them with [`History::add_current_visibility`]. With History Options › + /// Make Layer Visibility Changes Undoable off, showing or hiding a layer is not a step, so + /// stepping through history keeps every other layer's visibility as it is; these layers take + /// the visibility of the state stepped to, since a recorded step changed them. + pub fn current_visibility(&self) -> &[LayerId] { + &self.current_visibility + } + + /// Note layers whose visibility the current step changed (after [`History::record`], and + /// again when a later change coalesces into the step). + pub fn add_current_visibility(&mut self, ids: &[LayerId]) { + merge_ids(&mut self.current_visibility, ids); + } + /// Undo: `current` becomes redoable; returns the document to make current and the layers it /// targeted when it was created. pub fn undo(&mut self, current: Arc) -> Option<(Arc, LayerTarget)> { @@ -167,28 +235,32 @@ impl History { return Some((current, self.current_layers.clone())); } let prev = self.undo.pop_back()?; + self.last_record = None; if let Some(tiles) = &mut self.tiles { tiles.remove(&prev.doc); tiles.add(¤t); } let label = std::mem::replace(&mut self.current_label, prev.label); let layers = std::mem::replace(&mut self.current_layers, prev.layers.clone()); + let visibility = std::mem::replace(&mut self.current_visibility, prev.visibility); let id = std::mem::replace(&mut self.current_id, prev.id); - self.redo.push(HistoryState { label, doc: current, layers, id }); + self.redo.push(HistoryState { label, doc: current, layers, visibility, id }); Some((prev.doc, prev.layers)) } /// Redo: the mirror of [`Self::undo`]. pub fn redo(&mut self, current: Arc) -> Option<(Arc, LayerTarget)> { let next = self.redo.pop()?; + self.last_record = None; if let Some(tiles) = &mut self.tiles { tiles.remove(&next.doc); tiles.add(¤t); } let label = std::mem::replace(&mut self.current_label, next.label); let layers = std::mem::replace(&mut self.current_layers, next.layers.clone()); + let visibility = std::mem::replace(&mut self.current_visibility, next.visibility); let id = std::mem::replace(&mut self.current_id, next.id); - self.undo.push_back(HistoryState { label, doc: current, layers, id }); + self.undo.push_back(HistoryState { label, doc: current, layers, visibility, id }); Some((next.doc, next.layers)) } @@ -270,6 +342,7 @@ impl History { /// last step can no longer be undone and its pixels are released. pub fn purge_last(&mut self) -> bool { self.clear_redo(); + self.last_record = None; if self.cleared.take().is_some() { self.tiles = None; return true; @@ -278,11 +351,118 @@ impl History { if let Some(tiles) = &mut self.tiles { tiles.remove(&state.doc); } + // The current step now starts where the purged one did. + merge_ids(&mut self.current_visibility, &state.visibility); true } + /// Delete the past state at entry `i` (indexed like [`History::entries`]) and nothing else, + /// as the History panel's Delete does in non-linear history. The current state is not held + /// here (the session steps back first) and the oldest state can't be removed this way + /// either, since every later state would lose its starting point in the list. Returns + /// whether a state was removed. + pub fn remove(&mut self, i: usize) -> bool { + if i == 0 { + return false; + } + let Some(state) = self.undo.remove(i) else { return false }; + self.last_record = None; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&state.doc); + } + // The state after it now follows the one before it. + match self.undo.get_mut(i) { + Some(next) => merge_ids(&mut next.visibility, &state.visibility), + None => merge_ids(&mut self.current_visibility, &state.visibility), + } + true + } + + /// Delete the later (redo) state `j` (0 = the next redo, as [`History::redo_labels`] lists + /// them) and nothing else: the History panel's Delete on a dimmed state in non-linear + /// history. Returns whether a state was removed. + pub fn remove_later(&mut self, j: usize) -> bool { + let Some(k) = self.redo.len().checked_sub(j.saturating_add(1)) else { return false }; + let state = self.redo.remove(k); + self.last_record = None; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&state.doc); + } + // The state after it in the list (the one before it in `redo`) now follows the state + // before it. + if let Some(next) = k.checked_sub(1).and_then(|n| self.redo.get_mut(n)) { + merge_ids(&mut next.visibility, &state.visibility); + } + true + } + + /// Step back like [`History::undo`] but drop the current document instead of making it + /// redoable: the current state is deleted while the redo states (later states in the + /// list) stay. Returns the document to make current and the layers it targeted. + pub fn drop_current(&mut self) -> Option<(Arc, LayerTarget)> { + let prev = self.undo.pop_back()?; + self.last_record = None; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&prev.doc); + } + self.current_label = prev.label; + self.current_layers = prev.layers.clone(); + self.current_id = prev.id; + let dropped = std::mem::replace(&mut self.current_visibility, prev.visibility); + // The next later state now follows `prev`. + if let Some(next) = self.redo.last_mut() { + merge_ids(&mut next.visibility, &dropped); + } + Some((prev.doc, prev.layers)) + } + + /// Take back the most recent [`History::record`] exactly, for a compound command whose later + /// part failed: the document it replaced becomes current again, the current document is + /// dropped (nothing to redo), and in non-linear history the states that record moved from + /// the redo side go back there, so the list reads as before the command, with the oldest + /// states the state limit or the byte budget dropped for it back too. Returns `None` + /// (changing nothing) when something else changed the list since that record, e.g. an undo; + /// the caller then falls back to [`History::undo`]. + pub fn rollback(&mut self) -> Option<(Arc, LayerTarget)> { + let held = self.last_record.as_ref()?; + if self.undo.len().saturating_add(held.evicted.len()) <= held.moved { + return None; + } + let LastRecord { moved, evicted, .. } = self.last_record.take()?; + // Put each back where it was taken off, the last one taken off first. + for (at, state) in evicted.into_iter().rev() { + if let Some(tiles) = &mut self.tiles { + tiles.add(&state.doc); + } + self.undo.insert(at.min(self.undo.len()), state); + } + for _ in 0..moved { + // Tiles stay counted: they only move back to the redo side. + let state = self.undo.pop_back()?; + self.redo.push(state); + } + let prev = self.undo.pop_back()?; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&prev.doc); + } + self.current_label = prev.label; + self.current_layers = prev.layers.clone(); + self.current_visibility = prev.visibility; + self.current_id = prev.id; + Some((prev.doc, prev.layers)) + } + + /// The document the most recent [`History::record`] replaced, while [`History::rollback`] + /// can still take that record back. + pub fn recorded_before(&self) -> Option> { + self.last_record.as_ref().map(|held| held.before.clone()) + } + /// Forget the redo states, e.g. after undoing half of a compound step that failed. pub fn clear_redo(&mut self) { + if !self.redo.is_empty() { + self.last_record = None; + } if let Some(tiles) = &mut self.tiles { for state in &self.redo { tiles.remove(&state.doc); @@ -295,6 +475,8 @@ impl History { /// goes back to the default, the oldest state held (now the current one): a source set on a /// state the purge removed would otherwise leave no source. pub fn clear(&mut self) { + self.last_record = None; + self.current_visibility.clear(); self.undo.clear(); self.redo.clear(); self.tiles = None; @@ -320,6 +502,7 @@ impl History { let redo = std::mem::take(&mut self.redo); let brush_source = self.brush_source.take(); self.cleared = Some(Box::new(Cleared { undo, redo, brush_source })); + self.last_record = None; self.tiles = None; true } @@ -462,6 +645,11 @@ impl History { } else { tiles.remove(&state.doc); dropped += 1; + // Held for a rollback of the step that pushed them out. Taking them off from the + // highest index down keeps each index valid, so a rollback can insert them back. + if let Some(held) = &mut self.last_record { + held.evicted.push((i, state)); + } } } *undo = kept; @@ -469,6 +657,15 @@ impl History { } } +/// Add the ids of `from` that `into` lacks. +fn merge_ids(into: &mut Vec, from: &[LayerId]) { + for id in from { + if !into.contains(id) { + into.push(*id); + } + } +} + /// Bytes of the pixel tiles of `doc` (layers, masks, fill caches, smart filter masks, alpha channels, selection) not already in `seen`. fn tile_bytes(doc: &Document, seen: &mut HashSet) -> usize { let mut n = 0usize; diff --git a/crates/ops/src/tests.rs b/crates/ops/src/tests.rs index 1ac12b2885..b71b2b9ec4 100644 --- a/crates/ops/src/tests.rs +++ b/crates/ops/src/tests.rs @@ -208,6 +208,321 @@ fn new_edit_clears_redo() { assert_eq!(h.entries(), vec!["Open".to_string(), "B".to_string()]); } +/// Open, A, B, C, then two steps back: A is current, B and C are redo states. +fn stepped_back(non_linear: bool) -> (History, Arc) { + let mut h = History { non_linear, ..History::default() }; + let mut cur = Arc::new(base()); + for n in ["A", "B", "C"] { + edit(&mut h, &mut cur, n, |d| d.name = n.to_lowercase()); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert_eq!(cur.name, "a"); + (h, cur) +} + +#[test] +fn non_linear_history_keeps_later_states_and_appends_the_new_step() { + let (mut h, mut cur) = stepped_back(true); + edit(&mut h, &mut cur, "D", |d| d.name = format!("{}+d", d.name)); + assert_eq!(h.entries(), ["Open", "A", "B", "C", "D"], "B and C are kept, D goes to the end"); + assert!(!h.can_redo()); + assert_eq!(cur.name, "a+d", "D was made from the state selected (A), not from C"); + // Stepping back walks the list: C, B, A, Open, each with its own document. + let mut names = Vec::new(); + while let Some((d, _)) = h.undo(cur.clone()) { + cur = d; + names.push(cur.name.clone()); + } + assert_eq!(names, ["c", "b", "a", "h"]); + // Linear history (the default) discards them. + let (mut h, mut cur) = stepped_back(false); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.entries(), ["Open", "A", "D"]); +} + +#[test] +fn non_linear_steps_respect_the_state_limit_and_the_tile_counter() { + let mut h = History::new(3); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for i in 0..4 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "branch", |d| d.name = "branch".into()); + assert_eq!(h.past_len(), 3, "the oldest states beyond the limit go"); + assert_eq!(h.entries(), ["paint 1", "paint 2", "paint 3", "branch"]); + assert_counter(&h, &cur); +} + +#[test] +fn remove_deletes_one_past_state_and_drop_current_keeps_the_redo_states() { + let (mut h, _) = stepped_back(true); + assert!(!h.remove(0), "the oldest state stays"); + assert!(!h.remove(1), "the current state (A) is not held"); + assert!(!h.remove(99)); + let (mut h, mut cur) = stepped_back(true); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert!(h.remove(1)); + assert_eq!(h.entries(), ["Open", "B", "C", "D"]); + assert_eq!(h.state(1).unwrap().name, "b", "each state keeps its own document"); + // Deleting the current state: the one before becomes current, the later ones stay. + let (mut h, _) = stepped_back(true); + let (d, _) = h.drop_current().unwrap(); + assert_eq!(d.name, "h"); + assert_eq!(h.entries(), ["Open"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert!(h.drop_current().is_none(), "nothing before Open"); + // The tile counter follows both. + let mut h = History::new(50); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for i in 0..4 { + edit(&mut h, &mut cur, "Paint", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + assert!(h.remove(2)); + assert_counter(&h, &cur); + cur = h.drop_current().unwrap().0; + assert_counter(&h, &cur); +} + +#[test] +fn remove_later_deletes_one_redo_state() { + let mut h = History::new(50); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for (i, n) in ["A", "B", "C"].into_iter().enumerate() { + edit(&mut h, &mut cur, n, |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert!(!h.remove_later(2) && !h.remove_later(usize::MAX)); + assert!(h.remove_later(0), "B"); + assert_eq!(h.redo_labels().collect::>(), ["C"]); + assert_counter(&h, &cur); + let (c, _) = h.redo(cur).unwrap(); + assert_eq!(h.entries(), ["Open", "A", "C"]); + assert_counter(&h, &c); +} + +/// Each state keeps the layers whose visibility its own step changed; when a state goes +/// (Purge, a non-linear Delete) or a step is appended after the kept later states, the step +/// that now follows covers the changes of the steps it now spans. +#[test] +fn step_visibility_follows_steps_and_spans_removed_ones() { + let vis = |h: &History| { + let mut v: Vec = h.current_visibility().iter().map(|l| l.0).collect(); + v.sort_unstable(); + v + }; + // Open, then steps A, B, C, D changing layers 1, 2, 3, 4. + let steps = |n: usize, non_linear: bool| { + let mut h = History { non_linear, ..History::default() }; + let mut cur = Arc::new(base()); + for (i, label) in ["A", "B", "C", "D"].into_iter().take(n).enumerate() { + edit(&mut h, &mut cur, label, |d| d.name = label.to_lowercase()); + h.add_current_visibility(&[LayerId(i as u64 + 1)]); + } + (h, cur) + }; + let (mut h, cur) = steps(3, false); + assert_eq!(vis(&h), [3]); + let (cur, _) = h.undo(cur).unwrap(); + assert_eq!(vis(&h), [2]); + h.redo(cur).unwrap(); + assert_eq!(vis(&h), [3]); + // Purge: C now starts from A. + h.purge_last(); + assert_eq!(vis(&h), [2, 3]); + // Non-linear Delete of B (between A and C): C now follows A. + let (mut h, cur) = steps(4, true); + assert!(h.remove(2)); + h.undo(cur).unwrap(); + assert_eq!(vis(&h), [2, 3]); + // Of the later B (A current): C follows A. + let (mut h, mut cur) = steps(3, true); + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert!(h.remove_later(0)); + h.redo(cur).unwrap(); + assert_eq!(vis(&h), [2, 3]); + // Of the current B (C later): A is current, C follows it. + let (mut h, cur) = steps(3, true); + h.undo(cur).unwrap(); + let cur = h.drop_current().unwrap().0; + assert_eq!(vis(&h), [1]); + h.redo(cur).unwrap(); + assert_eq!(vis(&h), [2, 3]); + // A step after the kept later states B and C (made from A) follows C in the list. + let (mut h, mut cur) = steps(3, true); + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + h.add_current_visibility(&[LayerId(4)]); + assert_eq!(vis(&h), [2, 3, 4]); + h.rollback().unwrap(); + assert_eq!(vis(&h), [1], "rollback restores A's"); + h.clear(); + assert_eq!(vis(&h), Vec::::new()); +} + +#[test] +fn rollback_takes_the_last_record_back_exactly_in_both_modes() { + // Non-linear, with redo states: the moved states go back to the redo side and the + // document the step replaced (A) is current again, not the last former redo state (C). + let (mut h, mut cur) = stepped_back(true); + let a = cur.clone(); + let entries = h.entries(); + let redo: Vec = h.redo_labels().map(str::to_string).collect(); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.recorded_before().map(|d| d.name.clone()).as_deref(), Some("a")); + let (d, _) = h.rollback().unwrap(); + assert!(Arc::ptr_eq(&d, &a)); + assert_eq!(h.entries(), entries); + assert_eq!(h.redo_labels().map(str::to_string).collect::>(), redo); + assert!(h.rollback().is_none(), "only once"); + // Redoing still walks the kept states. + let (b, _) = h.redo(d).unwrap(); + assert_eq!(b.name, "b"); + // Linear: the same as an undo whose redo state is dropped. + let (mut h, mut cur) = stepped_back(false); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.rollback().unwrap().0.name, "a"); + assert_eq!(h.entries(), ["Open", "A"]); + assert!(!h.can_redo()); + // Anything else since the record: no rollback (the caller undoes instead). + let (mut h, mut cur) = stepped_back(true); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + cur = h.undo(cur).unwrap().0; + assert!(h.recorded_before().is_none() && h.rollback().is_none()); + h.redo(cur).unwrap(); + assert!(h.rollback().is_none()); + // The tile counter follows. + let mut h = History::new(50); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for i in 0..3 { + edit(&mut h, &mut cur, "Paint", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "Paint", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[1.0, 0.0, 0.0, 1.0]); + }); + cur = h.rollback().unwrap().0; + assert_counter(&h, &cur); +} + +/// A failed compound command whose first step moved more later states than the state limit +/// leaves room for (or whose step the byte budget trimmed for) is still taken back exactly: +/// the states the limit dropped for it come back, as in the list before the command. +#[test] +fn rollback_brings_back_the_states_the_limit_dropped_for_the_step() { + for max in 1..=4 { + let (mut h, mut cur) = stepped_back(true); + h.max_states = max; + let a = cur.clone(); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert!(h.past_len() <= max); + assert_eq!(h.recorded_before().map(|d| d.name.clone()).as_deref(), Some("a"), "max {max}"); + let (d, _) = h.rollback().unwrap(); + assert!(Arc::ptr_eq(&d, &a), "max {max}: back on A, not on an Undo of C"); + assert_eq!(h.entries(), ["Open", "A"], "max {max}"); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"], "max {max}"); + assert!(h.rollback().is_none()); + } + // Once anything else changed the list, the dropped states stay dropped. + let (mut h, mut cur) = stepped_back(true); + h.max_states = 2; + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + cur = h.undo(cur).unwrap().0; + h.redo(cur).unwrap(); + assert!(h.rollback().is_none()); + assert_eq!(h.entries(), ["B", "C", "D"]); + // The byte budget: the trim after the step keeps only its newest state; the rollback + // brings back the rest and the tile counter follows. + let mut h = History { non_linear: true, ..History::default() }; + let mut cur = Arc::new(base()); + for (i, n) in ["A", "B", "C"].into_iter().enumerate() { + edit(&mut h, &mut cur, n, |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + h.max_bytes = 1; + edit(&mut h, &mut cur, "D", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[1.0, 0.0, 0.0, 1.0]); + }); + assert!(h.trim(&cur) > 0); + assert_eq!(h.entries(), ["C", "D"]); + assert_counter(&h, &cur); + cur = h.rollback().unwrap().0; + assert_eq!(h.entries(), ["Open", "A"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert_counter(&h, &cur); +} + +/// The History Brush's source never falls off, so the limit and the byte budget drop states +/// behind it, not at the front; a rollback still puts each back where it was. +#[test] +fn rollback_puts_states_back_behind_the_pinned_brush_source() { + let (mut h, mut cur) = stepped_back(true); + assert!(h.set_brush_source(0)); + h.max_states = 2; + let a = cur.clone(); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.entries(), ["Open", "C", "D"], "A and B go, the source (Open) stays"); + let (d, _) = h.rollback().unwrap(); + assert!(Arc::ptr_eq(&d, &a)); + assert_eq!(h.entries(), ["Open", "A"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert_eq!(h.brush_source(), Some(0)); + assert_eq!(h.state(0).unwrap().name, "h"); + // The byte budget, with the source on the oldest state. + let mut h = History { non_linear: true, ..History::default() }; + let mut cur = Arc::new(base()); + for (i, n) in ["A", "B", "C"].into_iter().enumerate() { + edit(&mut h, &mut cur, n, |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert!(h.set_brush_source(0)); + h.max_bytes = 1; + edit(&mut h, &mut cur, "D", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[1.0, 0.0, 0.0, 1.0]); + }); + assert!(h.trim(&cur) > 0); + assert_eq!(h.entries(), ["Open", "C", "D"]); + cur = h.rollback().unwrap().0; + assert_eq!(cur.name, "h"); + assert_eq!(h.entries(), ["Open", "A"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert_eq!(h.state(0).unwrap().name, "h"); + assert_counter(&h, &cur); +} + #[test] fn max_states_bound() { let mut h = History::new(3); diff --git a/crates/plugins/src/runtime.rs b/crates/plugins/src/runtime.rs index c82171a5ac..9f66b545ba 100644 --- a/crates/plugins/src/runtime.rs +++ b/crates/plugins/src/runtime.rs @@ -115,6 +115,42 @@ fn wasm_err(e: wasmi::Error) -> Error { if e.as_trap_code() == Some(wasmi::TrapCode::OutOfFuel) { Error::Limit("instruction budget".into()) } else { Error::Trap(e.to_string()) } } +/// Reads an unsigned LEB128 `u32` at the start of `bytes`: the value and its length. +fn leb_u32(bytes: &[u8]) -> Option<(u32, usize)> { + let mut value = 0u32; + for (i, &b) in bytes.iter().take(5).enumerate() { + value |= u32::from(b & 0x7f).checked_shl(7 * i as u32)?; + if b & 0x80 == 0 { + return Some((value, i + 1)); + } + } + None +} + +/// Refuses a code section that declares no functions but more bytes than that count. The spec +/// makes the count the whole section then; wasmi 2.0's strict limits divide the section size by +/// the function count before validating it, so such a module (crafted or truncated) panicked. +/// Every code section is checked, since a duplicate one also reaches that division before +/// validation refuses it. Anything else, malformed or not, is left to wasmi. +fn check_code_section(bytes: &[u8]) -> Result<()> { + if bytes.get(4..8) != Some(&[1, 0, 0, 0][..]) { + return Ok(()); + } + let mut pos = 8usize; + while let Some(&id) = bytes.get(pos) { + let Some((size, len)) = bytes.get(pos + 1..).and_then(leb_u32) else { return Ok(()) }; + let start = pos + 1 + len; + if id == 10 + && let Some((0, count_len)) = bytes.get(start..).and_then(leb_u32) + && size as usize > count_len + { + return Err(Error::Module(format!("the code section declares no functions but {size} bytes"))); + } + pos = start.saturating_add(size as usize); + } + Ok(()) +} + impl Plugin { /// Validates and loads a module: checks the ABI version and reads the manifest. pub fn load(bytes: &[u8], limits: Limits) -> Result { @@ -124,6 +160,7 @@ impl Plugin { if !bytes.starts_with(b"\0asm") { return Err(Error::Module("not a WebAssembly binary (missing \\0asm header)".into())); } + check_code_section(bytes)?; let mut config = Config::default(); config.consume_fuel(true).enforced_limits(EnforcedLimits::strict()).set_max_recursion_depth(limits.max_recursion_depth.max(16)); let engine = Engine::new(&config); diff --git a/crates/plugins/tests/abi.rs b/crates/plugins/tests/abi.rs index 61ee32791f..17e352982e 100644 --- a/crates/plugins/tests/abi.rs +++ b/crates/plugins/tests/abi.rs @@ -219,6 +219,25 @@ fn malformed_and_oversized_modules_are_refused() { assert!(matches!(e, Error::Abi(_)), "{e}"); } +#[test] +fn a_code_section_claiming_no_functions_but_many_bytes_is_refused() { + // Header, then a code section (id 10) declaring 2000 bytes whose function count is 0, cut off + // after the count (a truncated download) or padded to its full size. The spec makes the + // count's own byte the whole section; wasmi's strict limits divided the size by the count. + let truncated = b"\0asm\x01\0\0\0\x0a\xd0\x0f\x00".to_vec(); + let mut padded = truncated.clone(); + padded.resize(padded.len() + 1999, 0); + // The same section after a valid empty one: a duplicate reaches the division before validation. + let mut second = b"\0asm\x01\0\0\0\x0a\x01\x00".to_vec(); + second.extend_from_slice(&padded[8..]); + for (name, bytes) in [("truncated", truncated), ("padded", padded), ("second", second)] { + assert!(matches!(Plugin::load(&bytes, Limits::default()), Err(Error::Module(_))), "{name}"); + } + // An empty code section (count 0, nothing after it) is valid; it fails only for the missing ABI exports. + let empty = b"\0asm\x01\0\0\0\x0a\x01\x00"; + assert!(matches!(Plugin::load(empty, Limits::default()), Err(Error::Abi(_)))); +} + #[test] fn budgets_stop_runaway_plugins() { let spin = ok_module("(loop $l (br $l)) (i32.const 0)"); diff --git a/crates/psd/src/builder.rs b/crates/psd/src/builder.rs index d419f8edac..6606bdb351 100644 --- a/crates/psd/src/builder.rs +++ b/crates/psd/src/builder.rs @@ -460,6 +460,7 @@ impl PsdBuilder { global_blocks: Vec::new(), layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, }) } diff --git a/crates/psd/src/compression.rs b/crates/psd/src/compression.rs index 3ec7d64bb8..eb5d05d609 100644 --- a/crates/psd/src/compression.rs +++ b/crates/psd/src/compression.rs @@ -225,6 +225,28 @@ pub fn decode_planes(compression: Compression, data: &[u8], layout: &PlaneLayout } } +/// Bytes a PSD RLE stream (two-byte row counts) of `rows` rows grows by when written as PSB +/// (four-byte row counts); an error when `data` is too short to hold the count table. +pub(crate) fn rle_widening(data: &[u8], rows: usize) -> Result { + let table = rows.checked_mul(2).ok_or(PsdError::LimitExceeded("row count overflow"))?; + if data.len() < table { + return Err(PsdError::UnexpectedEof { offset: data.len(), needed: table - data.len() }); + } + Ok(table as u64) +} + +/// Writes a PSD RLE stream (two-byte row counts) as the PSB one (four-byte row counts): the packed +/// rows are the same in both versions, only the count table widens. +pub(crate) fn put_widened_rle(out: &mut Vec, data: &[u8], rows: usize) -> Result<()> { + let table_len = usize::try_from(rle_widening(data, rows)?).map_err(|_| PsdError::LimitExceeded("row count overflow"))?; + let (table, packed) = data.split_at_checked(table_len).ok_or_else(|| PsdError::invalid("RLE row-count table out of range"))?; + for count in table.as_chunks::<2>().0 { + out.extend_from_slice(&[0, 0, count[0], count[1]]); + } + out.extend_from_slice(packed); + Ok(()) +} + /// Encodes planar data (excluding the 2-byte compression field). pub fn encode_planes(compression: Compression, decoded: &[u8], layout: &PlaneLayout) -> Result> { let total = layout.decoded_len()?; diff --git a/crates/psd/src/file.rs b/crates/psd/src/file.rs index eec73ed072..ccc1e7ba0d 100644 --- a/crates/psd/src/file.rs +++ b/crates/psd/src/file.rs @@ -1,6 +1,6 @@ //! The top-level [`PsdFile`] model, parser and writer. -use crate::compression::{Compression, MAX_DECODED_BYTES, decode_planes, validate_planes}; +use crate::compression::{Compression, MAX_DECODED_BYTES, decode_planes, put_widened_rle, rle_widening, validate_planes}; use crate::error::{PsdError, Result}; use crate::header::{ColorMode, Header, Version}; use crate::image_data::ImageData; @@ -71,7 +71,8 @@ impl GlobalLayerMask { /// A complete PSD/PSB file. /// /// `PsdFile::from_bytes(b)?.to_bytes()? == b` for unmodified well-formed -/// files, and `from_bytes(to_bytes(f)) == f` for any model `f`. +/// files, and `from_bytes(to_bytes(f)) == f` for any model `f` not marked +/// [`Self::write_as_psb`] (a marked one reads back as the PSB it was written as). #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct PsdFile { @@ -94,6 +95,12 @@ pub struct PsdFile { pub layer_mask_trailing: Vec, /// Merged image data. pub image_data: ImageData, + /// Write this PSD model as PSB (set by [`Self::promote_to_psb`]). The model itself stays a + /// consistent PSD: its channel data keeps two-byte RLE row counts, so everything that reads + /// it with `header.version` decodes it as before. Only [`Self::to_bytes`] and + /// [`Self::encoded_len`] write version 2, long length fields and four-byte row counts. + #[cfg_attr(feature = "serde", serde(default))] + pub write_as_psb: bool, } impl PsdFile { @@ -183,8 +190,18 @@ impl PsdFile { } } let image_data = ImageData { compression: Compression::Raw, data: Vec::new() }; - let mut file = - PsdFile { header, color_mode_data, resources, layer_info, layer_info_placement, global_layer_mask, global_blocks, layer_mask_trailing, image_data }; + let mut file = PsdFile { + header, + color_mode_data, + resources, + layer_info, + layer_info_placement, + global_layer_mask, + global_blocks, + layer_mask_trailing, + image_data, + write_as_psb: false, + }; let mut encoded = r.clone(); let merged = if want(&file) { file.read_merged_decoded(&mut r).map(Some) @@ -239,14 +256,24 @@ impl PsdFile { /// Serializes the file. pub fn to_bytes(&self) -> Result> { - let v = self.header.version; + let (v, widen) = self.write_form(); let psb = v.is_psb(); // Reserve the pixel data up front: appending it to an empty Vec reallocates and copies // repeatedly. Everything else (headers, resources, records) is small by comparison. - let hint = self.layer_info.as_ref().map_or(0, LayerInfo::channel_data_len).saturating_add(self.image_data.data.len()); + let widened = if widen { self.rle_widening()? } else { 0 }; + let hint = self + .layer_info + .as_ref() + .map_or(0, LayerInfo::channel_data_len) + .saturating_add(self.image_data.data.len()) + .saturating_add(usize::try_from(widened).unwrap_or(usize::MAX)); let mut out = Vec::new(); out.try_reserve(hint).map_err(|_| PsdError::LimitExceeded("not enough memory to serialize the file"))?; - self.header.write(&mut out); + if widen { + Header { version: v, ..self.header.clone() }.write(&mut out); + } else { + self.header.write(&mut out); + } out.put_len(self.color_mode_data.len() as u64, false)?; out.put(&self.color_mode_data); resources::write_section(&mut out, &self.resources)?; @@ -261,7 +288,7 @@ impl PsdFile { if !section_empty { let li = out.begin_len(psb); if let (Some(info), false) = (&self.layer_info, in_block) { - info.write_body(&mut out, v)?; + info.write_body_as(&mut out, v, widen)?; } out.end_len(li, psb)?; let need_mask_field = self.global_layer_mask.is_some() || !self.global_blocks.is_empty() || (in_block && self.layer_info.is_some()); @@ -281,7 +308,7 @@ impl PsdFile { let long = uses_long_length(v, key); let at = out.begin_len(long); let start = out.len(); - info.write_body(&mut out, v)?; + info.write_body_as(&mut out, v, widen)?; let len = out.len() - start; out.end_len(at, long)?; match padding { @@ -297,10 +324,121 @@ impl PsdFile { out.end_len(at, psb)?; out.put_u16(self.image_data.compression.as_u16()); - out.put(&self.image_data.data); + match self.merged_widened_rows(widen)? { + 0 => out.put(&self.image_data.data), + rows => put_widened_rle(&mut out, &self.image_data.data, rows)?, + } Ok(out) } + /// The version the file is written as, and whether its RLE count tables widen on the way + /// (a PSD model marked [`Self::write_as_psb`]). + fn write_form(&self) -> (Version, bool) { + let widen = self.write_as_psb && !self.header.version.is_psb(); + (if widen { Version::Psb } else { self.header.version }, widen) + } + + /// The version [`Self::to_bytes`] writes: PSB for a PSD model marked [`Self::write_as_psb`], + /// else `header.version`. + pub fn written_version(&self) -> Version { + self.write_form().0 + } + + /// Rows of the merged image's RLE count table that widen on write (0 when nothing widens). + fn merged_widened_rows(&self, widen: bool) -> Result { + if widen && self.image_data.compression == Compression::Rle { ImageData::layout(&self.header).rows() } else { Ok(0) } + } + + /// Bytes every RLE count table of this PSD model grows by when written as PSB; an error + /// when a table is too short for its rows. + fn rle_widening(&self) -> Result { + let mut n = 0u64; + for layer in self.layers() { + for c in &layer.channels { + match layer.widened_rows(c, true)? { + 0 => {} + rows => n = n.saturating_add(rle_widening(&c.data, rows)?), + } + } + } + match self.merged_widened_rows(true)? { + 0 => Ok(n), + rows => Ok(n.saturating_add(rle_widening(&self.image_data.data, rows)?)), + } + } + + /// The number of bytes [`Self::to_bytes`] writes, computed without serializing the channel + /// data (only the layer records, which are small, are serialized to measure them). Lets a + /// writer check a file against the 2 GB PSD limit before choosing the version. + pub fn encoded_len(&self) -> Result { + let (v, widen) = self.write_form(); + let psb = v.is_psb(); + let len_field = |long: bool| if long { 8u64 } else { 4 }; + let blocks_len = |blocks: &[TaggedBlock]| blocks.iter().fold(0u64, |n, b| n.saturating_add(b.encoded_len_aligned(v, 4) as u64)); + let body_len = |info: &LayerInfo| -> Result { + let unpadded = info.unpadded_len_as(v, widen)?; + let padding = info.padding.as_ref().map_or(unpadded % 2, |p| p.len() as u64); + Ok(unpadded.saturating_add(padding)) + }; + // Header, colour mode data, resources. + let mut n = 26u64.saturating_add(4).saturating_add(self.color_mode_data.len() as u64).saturating_add(resources::section_len(&self.resources)); + + let in_block = matches!(self.layer_info_placement, LayerInfoPlacement::GlobalBlock { .. }); + let section_empty = + self.layer_info.is_none() && self.global_layer_mask.is_none() && self.global_blocks.is_empty() && self.layer_mask_trailing.is_empty(); + n = n.saturating_add(len_field(psb)); + if !section_empty { + n = n.saturating_add(len_field(psb)); + if let (Some(info), false) = (&self.layer_info, in_block) { + n = n.saturating_add(body_len(info)?); + } + let need_mask_field = self.global_layer_mask.is_some() || !self.global_blocks.is_empty() || (in_block && self.layer_info.is_some()); + if need_mask_field { + n = n.saturating_add(4).saturating_add(self.global_layer_mask.as_ref().map_or(0, |g| g.data.len() as u64)); + } + n = n.saturating_add(blocks_len(&self.global_blocks)); + if let (Some(info), LayerInfoPlacement::GlobalBlock { key, padding, .. }) = (&self.layer_info, &self.layer_info_placement) { + let body = body_len(info)?; + let pad = padding.as_ref().map_or((4 - body % 4) % 4, |p| p.len() as u64); + n = n.saturating_add(8).saturating_add(len_field(uses_long_length(v, key))).saturating_add(body).saturating_add(pad); + } + n = n.saturating_add(self.layer_mask_trailing.len() as u64); + } + let merged = match self.merged_widened_rows(widen)? { + 0 => 0, + rows => rle_widening(&self.image_data.data, rows)?, + }; + Ok(n.saturating_add(2).saturating_add(self.image_data.data.len() as u64).saturating_add(merged)) + } + + /// Marks a PSD model to be written as the equivalent PSB ([`Self::write_as_psb`]) without + /// touching or re-encoding any pixels: the packed RLE rows are the same in both versions, so + /// [`Self::to_bytes`] only widens the row-count tables of the layer channels and the merged + /// image (two to four bytes per row) and the length fields while it copies them out. Raw and + /// ZIP channel data is the same in both. A PSB model is left as is. + /// + /// Every RLE count table is checked first: an error leaves the model unmarked, and the write + /// cannot fail on a table later. The layer info padding is kept; a writer that pads to an + /// alignment re-pads with [`Self::pad_layer_info_to`]. + pub fn promote_to_psb(&mut self) -> Result<()> { + if self.header.version.is_psb() || self.write_as_psb { + return Ok(()); + } + self.rle_widening()?; + self.write_as_psb = true; + Ok(()) + } + + /// Pads the layer info body to a multiple of `align` bytes as it will be written (see + /// [`LayerInfo::pad_to`]), RLE count tables widened when [`Self::write_as_psb`] is set. + pub fn pad_layer_info_to(&mut self, align: u64) -> Result<()> { + let (v, widen) = self.write_form(); + if let Some(info) = &mut self.layer_info { + info.padding = Some(vec![0; info.pad_len_as(v, widen, align)?]); + } + Ok(()) + } + /// Layer records in file order (bottom-most first); empty if none. pub fn layers(&self) -> &[LayerRecord] { self.layer_info.as_ref().map_or(&[], |i| &i.layers) @@ -440,6 +578,89 @@ mod write_tests { use super::*; use crate::{Compression, testgen}; + /// `encoded_len` is exactly what `to_bytes` writes, for every generated file and both layer + /// info placements, with and without padding (#5285). + #[test] + fn encoded_len_matches_the_written_length() { + for case in testgen::all_cases() { + assert_eq!(case.file.encoded_len().unwrap(), case.file.to_bytes().unwrap().len() as u64, "{}", case.name); + } + for version in [Version::Psd, Version::Psb] { + for key in [*b"Lr16", *b"Layr"] { + for padding in [None, Some(vec![]), Some(vec![1, 2, 3])] { + for body_padding in [None, Some(vec![]), Some(vec![9])] { + let mut file = testgen::small(version, Compression::Rle); + file.global_blocks = vec![TaggedBlock::new(*b"zz01", vec![11]), TaggedBlock::new(*b"lnk2", vec![1; 5])]; + file.layer_info_placement = LayerInfoPlacement::GlobalBlock { index: 1, signature: *b"8BIM", key, padding: padding.clone() }; + file.layer_info.as_mut().unwrap().padding = body_padding; + file.layer_mask_trailing = vec![0; 3]; + file.resources.push(ImageResource { name: b"odd".to_vec(), ..ImageResource::new(4000, vec![1, 2, 3]) }); + assert_eq!(file.encoded_len().unwrap(), file.to_bytes().unwrap().len() as u64, "{version:?} {key:?}"); + } + } + } + } + let mut empty = testgen::small(Version::Psd, Compression::Raw); + empty.layer_info = None; + empty.global_layer_mask = None; + empty.global_blocks.clear(); + assert_eq!(empty.encoded_len().unwrap(), empty.to_bytes().unwrap().len() as u64); + } + + /// A promoted PSD writes exactly the PSB written directly from the same pixels, and reads + /// back as that PSB; the model itself stays a PSD that every reader decodes as before (#5285). + #[test] + fn promote_to_psb_writes_the_psb_written_directly() { + let cases = testgen::all_cases(); + let (psd, psb) = cases.split_at(cases.len() / 2); + for (psd, psb) in psd.iter().zip(psb) { + assert_eq!(psd.file.header.version, Version::Psd, "{}", psd.name); + let mut promoted = psd.file.clone(); + promoted.promote_to_psb().unwrap(); + assert!(promoted.write_as_psb); + assert_eq!(promoted.written_version(), Version::Psb); + let bytes = promoted.to_bytes().unwrap(); + assert!(bytes == psb.file.to_bytes().unwrap(), "{}", psd.name); + assert_eq!(promoted.encoded_len().unwrap(), bytes.len() as u64, "{}", psd.name); + assert_eq!(PsdFile::from_bytes(&bytes).unwrap(), psb.file, "{}", psd.name); + // Unchanged as a model: same header, same (two-byte) channel data. + assert_eq!(PsdFile { write_as_psb: false, ..promoted.clone() }, psd.file, "{}", psd.name); + assert_eq!(promoted.decode_merged().ok(), psd.file.decode_merged().ok(), "{}", psd.name); + } + } + + /// Re-padding a promoted model pads what is written, as `pad_to` does for a PSB. + #[test] + fn pad_layer_info_to_pads_the_written_body() { + let mut promoted = testgen::layered(Version::Psd, ColorMode::Rgb, 8, Compression::Rle); + promoted.promote_to_psb().unwrap(); + promoted.pad_layer_info_to(4).unwrap(); + let mut direct = testgen::layered(Version::Psb, ColorMode::Rgb, 8, Compression::Rle); + direct.layer_info.as_mut().unwrap().pad_to(Version::Psb, 4).unwrap(); + assert!(promoted.to_bytes().unwrap() == direct.to_bytes().unwrap()); + } + + /// A damaged RLE table is refused before anything changes: the file stays a valid PSD. + #[test] + fn promote_to_psb_is_atomic() { + let mut file = testgen::small(Version::Psd, Compression::Rle); + // The last layer with pixels ("b", two rows): its red channel loses its row-count table. + let layers = &mut file.layer_info.as_mut().unwrap().layers; + let red = layers[2].channels.iter_mut().find(|c| c.id == 0).unwrap(); + assert_eq!(red.compression, Some(Compression::Rle)); + red.data.truncate(1); + let before = file.clone(); + assert!(file.promote_to_psb().is_err()); + assert_eq!(file, before); + assert!(!file.write_as_psb); + // A table damaged after promotion fails the write instead of panicking. + let mut file = testgen::small(Version::Psd, Compression::Rle); + file.promote_to_psb().unwrap(); + file.image_data.data.truncate(3); + assert!(file.to_bytes().is_err()); + assert!(file.encoded_len().is_err()); + } + #[test] fn global_blocks_default_to_four_byte_padding() { for version in [Version::Psd, Version::Psb] { diff --git a/crates/psd/src/layer.rs b/crates/psd/src/layer.rs index 1b5c241191..7756be4a6c 100644 --- a/crates/psd/src/layer.rs +++ b/crates/psd/src/layer.rs @@ -1,7 +1,7 @@ //! Layer records, layer masks, blending ranges and per-layer channel data. use crate::blend::BlendMode; -use crate::compression::{Compression, PlaneLayout, decode_planes, encode_planes}; +use crate::compression::{Compression, PlaneLayout, decode_planes, encode_planes, put_widened_rle, rle_widening}; use crate::error::{PsdError, Result}; use crate::header::{MAX_DIMENSION, Version}; use crate::io::{Reader, WriteExt, decode_legacy_name, read_pascal, write_pascal}; @@ -598,6 +598,12 @@ impl LayerRecord { Ok((LayerRecord { rect, channels, blend_mode, opacity, clipping, flags, filler, mask, blending_ranges, name, blocks, extra_trailing }, lens)) } + /// Rows of a channel's RLE count table that grow when a PSD model is written as PSB + /// (`widen`, see [`crate::PsdFile::promote_to_psb`]); 0 for other channels. + pub(crate) fn widened_rows(&self, c: &ChannelData, widen: bool) -> Result { + if widen && c.compression == Some(Compression::Rle) { Ok(self.channel_rect(c.id).size()?.1) } else { Ok(0) } + } + /// Bytes [`Self::write`] produces, computed without serializing. Errors where `write` would. fn encoded_len(&self, version: Version) -> Result { let too_big = || PsdError::LimitExceeded("section too large for 32-bit length (use PSB)"); @@ -628,13 +634,15 @@ impl LayerRecord { Ok((16 + 2 + self.channels.len() as u64 * (2 + channel_len) + 8 + 4 + 4).saturating_add(extra)) } - fn write(&self, out: &mut Vec, version: Version) -> Result<()> { + fn write(&self, out: &mut Vec, version: Version, widen: bool) -> Result<()> { self.rect.write(out); let nch = u16::try_from(self.channels.len()).map_err(|_| PsdError::LimitExceeded("too many channels"))?; out.put_u16(nch); for c in &self.channels { out.put_i16(c.id); - out.put_len(c.stored_len(), version.is_psb())?; + let rows = self.widened_rows(c, widen)?; + let grown = if rows == 0 { 0 } else { rle_widening(&c.data, rows)? }; + out.put_len(c.stored_len().saturating_add(grown), version.is_psb())?; } out.put(b"8BIM"); out.put(&self.blend_mode.key()); @@ -709,14 +717,24 @@ impl LayerInfo { /// Length of the body (layer count, records and channel data) without /// its trailing padding. pub fn unpadded_len(&self, version: Version) -> Result { + self.unpadded_len_as(version, false) + } + + /// [`Self::unpadded_len`] of the body written with its RLE count tables widened (`widen`). + pub(crate) fn unpadded_len_as(&self, version: Version, widen: bool) -> Result { // Computed from the field sizes: serializing the records just to measure them would copy - // every tagged block. + // every tagged block. Widening changes channel data only: the record's length fields keep + // their width. let mut n = 2u64; for l in &self.layers { n = n.saturating_add(l.encoded_len(version)?); } - for c in self.layers.iter().flat_map(|l| &l.channels).filter(|c| c.compression.is_some()) { - n = n.saturating_add(2).saturating_add(c.data.len() as u64); + for l in &self.layers { + for c in l.channels.iter().filter(|c| c.compression.is_some()) { + let rows = l.widened_rows(c, widen)?; + let grown = if rows == 0 { 0 } else { rle_widening(&c.data, rows)? }; + n = n.saturating_add(2).saturating_add(c.data.len() as u64).saturating_add(grown); + } } Ok(n) } @@ -732,8 +750,13 @@ impl LayerInfo { /// Number of zero bytes [`Self::pad_to`] would set, without touching `self`. pub(crate) fn pad_len(&self, version: Version, align: u64) -> Result { + self.pad_len_as(version, false, align) + } + + /// [`Self::pad_len`] for the body written with its RLE count tables widened (`widen`). + pub(crate) fn pad_len_as(&self, version: Version, widen: bool, align: u64) -> Result { let align = align.max(1); - let len = self.unpadded_len(version)?; + let len = self.unpadded_len_as(version, widen)?; Ok(((align - len % align) % align) as usize) } @@ -745,16 +768,25 @@ impl LayerInfo { /// Serializes the body without any trailing padding (the padding field is ignored). pub(crate) fn write_unpadded(&self, out: &mut Vec, version: Version) -> Result<()> { + self.write_unpadded_as(out, version, false) + } + + /// [`Self::write_unpadded`], widening the RLE count tables of a PSD model written as PSB + /// (`widen`) on the way out. + fn write_unpadded_as(&self, out: &mut Vec, version: Version, widen: bool) -> Result<()> { let n = i16::try_from(self.layers.len()).map_err(|_| PsdError::LimitExceeded("more than 32767 layers"))?; out.put_i16(if self.merged_alpha { -n } else { n }); for l in &self.layers { - l.write(out, version)?; + l.write(out, version, widen)?; } for l in &self.layers { for c in &l.channels { if let Some(comp) = c.compression { out.put_u16(comp.as_u16()); - out.put(&c.data); + match l.widened_rows(c, widen)? { + 0 => out.put(&c.data), + rows => put_widened_rle(out, &c.data, rows)?, + } } } } @@ -762,9 +794,15 @@ impl LayerInfo { } /// Serializes the body (without a length field). + #[cfg(test)] pub(crate) fn write_body(&self, out: &mut Vec, version: Version) -> Result<()> { + self.write_body_as(out, version, false) + } + + /// [`Self::write_body`], widening RLE count tables when `widen` (see [`Self::write_unpadded_as`]). + pub(crate) fn write_body_as(&self, out: &mut Vec, version: Version, widen: bool) -> Result<()> { let start = out.len(); - self.write_unpadded(out, version)?; + self.write_unpadded_as(out, version, widen)?; match &self.padding { Some(p) => out.put(p), None => { diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index 4d78a062be..33ce147101 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -246,6 +246,16 @@ pub(crate) fn read_section(r: &mut Reader<'_>) -> Result> { Ok(v) } +/// Serialized size of the resource section [`write_section`] writes, its length field included. +pub(crate) fn section_len(res: &[ImageResource]) -> u64 { + res.iter().fold(4u64, |n, r| { + let name = 1 + r.name.len().min(255); + let name = name + name % 2; + let data = r.data.len().saturating_add(r.data.len() % 2); + n.saturating_add((4 + 2 + name + 4) as u64).saturating_add(data as u64) + }) +} + pub(crate) fn write_section(out: &mut Vec, res: &[ImageResource]) -> Result<()> { let at = out.begin_len(false); for r in res { diff --git a/crates/psd/src/tagged.rs b/crates/psd/src/tagged.rs index b16e158565..098b720a22 100644 --- a/crates/psd/src/tagged.rs +++ b/crates/psd/src/tagged.rs @@ -360,9 +360,14 @@ impl TaggedBlock { /// Serialized size in bytes as a layer block (two-byte alignment). pub fn encoded_len(&self, version: Version) -> usize { + self.encoded_len_aligned(version, 2) + } + + /// Serialized size in bytes as [`Self::write`] writes it with `alignment`. + pub(crate) fn encoded_len_aligned(&self, version: Version, alignment: usize) -> usize { let l = if uses_long_length(version, &self.key) { 8 } else { 4 }; - let p = self.padding.as_ref().map_or(Self::default_padding(self.data.len(), 2), Vec::len); - 8 + l + self.data.len() + p + let p = self.padding.as_ref().map_or(Self::default_padding(self.data.len(), alignment), Vec::len); + (8 + l + p).saturating_add(self.data.len()) } } diff --git a/crates/psd/src/testgen.rs b/crates/psd/src/testgen.rs index d2d4784cb9..818c33bae0 100644 --- a/crates/psd/src/testgen.rs +++ b/crates/psd/src/testgen.rs @@ -124,6 +124,7 @@ pub fn merged_only(version: Version, mode: ColorMode, depth: u16, compression: C global_blocks: Vec::new(), layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, } } @@ -316,6 +317,7 @@ pub fn layered(version: Version, mode: ColorMode, depth: u16, compression: Compr global_blocks: blocks, layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, } } @@ -351,6 +353,7 @@ pub fn small(version: Version, compression: Compression) -> PsdFile { global_blocks: vec![TaggedBlock::new(*b"Patt", vec![1, 2])], layer_mask_trailing: Vec::new(), image_data: ImageData::encode(compression, &planes, &header).expect("encode"), + write_as_psb: false, } } diff --git a/crates/psd/src/tiff.rs b/crates/psd/src/tiff.rs index 86373f2ed3..b669fcc637 100644 --- a/crates/psd/src/tiff.rs +++ b/crates/psd/src/tiff.rs @@ -217,6 +217,7 @@ impl ImageSourceData { global_blocks: self.global_blocks, layer_mask_trailing: self.trailing, image_data, + write_as_psb: false, } } } diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index dd9b635e64..d2a49241d5 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -1041,6 +1041,10 @@ fn pen_dialog(app: &mut PhotocraftApp, command: &str, label: &str, name: &str) { } else { fields.insert("name".into(), json!(name)); } + if command == "path.toSelection" { + // OK deselects and hides the path, as ⌘↩ does (#6079). + fields.insert("__deselectPath".into(), json!(true)); + } if command == "path.fill" { let fg = app.session.tools.foreground; let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs index eb9147a7a5..6f8d0a3aa8 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs @@ -712,6 +712,16 @@ fn completed_pen_path_right_click_makes_selection() { crate::dialogs::confirm(h.state_mut(), dialog).unwrap(); assert!(h.state().session.active().unwrap().doc.selection.is_some()); assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("path.toSelection")); + // Like ⌘↩, it deselects and hides the work path, which stays in the document (#6079). + assert_eq!(h.state().ui.selected_path, None); + assert!(h.state().session.active().unwrap().doc.work_path.is_some()); + assert!(!crate::vector_ui::work_path_shown(h.state())); + // The hidden work path is no longer the active path: a second right-click offers no path + // commands, so Delete Path can't remove it unseen. + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.tool == Tool::Pen && !m.has_path && m.path_name.is_none())); + choose(h.state_mut(), &ctx, "path.delete"); + assert!(h.state().session.active().unwrap().doc.work_path.is_some(), "the hidden work path survives Delete Path"); } #[test] @@ -751,8 +761,12 @@ fn pen_options_bar_makes_a_selection_from_the_path() { crate::dialogs::confirm(&mut app, dialog).unwrap(); let st = app.session.active().unwrap(); assert!(st.doc.work_path.is_some() && st.doc.selection.is_some()); - // The active (Work) path again, with no pen path in progress. + // That hid the Work Path (#6079): it is not the active path until it is selected in the + // Paths panel again, and then it is, with no pen path in progress. app.run("select.deselect", json!({})).unwrap(); + assert!(make_selection(&mut app).is_err(), "the hidden Work Path is not the active path"); + assert!(app.ui.dialogs.is_empty()); + app.ui.selected_path = Some("work".into()); make_selection(&mut app).unwrap(); let dialog = app.ui.dialogs.last().map(|d| d.id).unwrap(); crate::dialogs::confirm(&mut app, dialog).unwrap(); diff --git a/crates/ui-egui/src/color_panel_tests.rs b/crates/ui-egui/src/color_panel_tests.rs index 6f7cc49729..5dd64cb77b 100644 --- a/crates/ui-egui/src/color_panel_tests.rs +++ b/crates/ui-egui/src/color_panel_tests.rs @@ -412,6 +412,34 @@ fn color_wheel_ignores_clicks_beside_it() { assert_eq!(bytes(h.state().session.tools.foreground), [0, 0, 255]); } +/// The Color Wheel shrinks to the Color group at its default height in every theme, as the +/// slider modes' ramp does, instead of running past the group's bottom into its scroll area. +#[test] +fn color_wheel_fits_the_default_color_group() { + for kind in ThemeKind::ALL { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.ui.color_panel.mode = ColorPanelMode::ColorWheel; + app.ui.theme = kind; + app.ui.dock_tabs.color = Group::Color.tabs(Tokens::for_kind(kind).pro).iter().position(|name| *name == "Color").unwrap(); + let mut h = Harness::builder().with_size(vec2(1440.0, 1200.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + crate::panels::dock_area(app, ui); + egui::CentralPanel::default().show(ui, |_| {}); + } + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, kind); + h.run_steps(4); + let group = crate::panel_docking::probe::native_group_rect(h.state(), &h.ctx, Group::Color).unwrap(); + assert!((group.height() - Group::Color.default_height()).abs() < 0.5, "{kind:?}: Color group {group:?}"); + let wheel = h.query_by_label("Color Wheel").unwrap_or_else(|| panic!("{kind:?}: no Color Wheel")).rect(); + // The group's scroll area ends at least 8 pt above its bottom edge (the card's bottom margin). + assert!(wheel.bottom() <= group.bottom() - 8.0, "{kind:?}: wheel {wheel:?} runs past the Color group {group:?}"); + } +} + /// The Color tab in `mode` with a document holding one selected black rectangle, the Rectangle tool /// in hand, foreground blue and background red, editing the background chip if `background`. fn color_tab_with_a_shape(mode: ColorPanelMode, background: bool) -> Harness<'static, PhotocraftApp> { diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index c9d6f46d68..b752dbd2d6 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -907,6 +907,13 @@ pub fn confirm(app: &mut PhotocraftApp, id: u64) -> Result { Some(p) if result.is_ok() && running => p.committing = true, _ => app.filter_preview = None, } + if result.is_ok() + && cmd == "path.toSelection" + && d.fields.contains_key("__deselectPath") + && let Some(name) = params.get("name").and_then(Value::as_str) + { + crate::vector_ui::deselect_loaded_path(app, name); + } if result.is_ok() && cmd == "view.newGuideLayout" { app.ui.view.guide_layout = params; } @@ -1014,6 +1021,7 @@ pub(super) mod filter_preview_ui; pub(super) mod filter_preview_worker; pub mod gallery_ui; pub mod guide_layout_ui; +pub mod history_options_ui; pub mod jpeg_options_ui; pub mod layer_picker; pub mod layer_style; diff --git a/crates/ui-egui/src/dialogs/file_ui/print.rs b/crates/ui-egui/src/dialogs/file_ui/print.rs index 8ed54e6644..0a596a307b 100644 --- a/crates/ui-egui/src/dialogs/file_ui/print.rs +++ b/crates/ui-egui/src/dialogs/file_ui/print.rs @@ -31,9 +31,6 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 { let mut f = Map::new(); f.insert("__print".into(), json!(true)); f.insert("__label".into(), json!("PhotoCraft Print Settings")); - // Asked once per opening: the Printer menu lists what the spooler knows now. - let printers = app.session.execute("file.printers", json!({})).unwrap_or_default(); - f.insert("__systemPrinters".into(), printers["printers"].clone()); // The settings of the last print, also from an earlier session (#7410). if let Some(Value::Object(m)) = photocraft_engine::print_cmds::last_print(&app.session) { f.extend(m); @@ -63,17 +60,31 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 { ] { f.entry(k.to_string()).or_insert(v); } - // Windows: the system's printers, and the paper from the chosen printer's driver settings. - if let Ok(list) = craft_print::rgb::printers() { + refresh_printers(app, &mut f); + app.ui.open_dialog(DialogKind::Command, f) +} + +fn refresh_printers(app: &mut PhotocraftApp, f: &mut Map) { + if photocraft_engine::print_cmds::try_with_spooler(|| take_printer_lists(app, f)).is_some() { + f.remove("__printerListingPending"); + } else { + take_printer_lists(app, f); + f.insert("__printerListingPending".into(), json!(true)); + } +} + +fn take_printer_lists(app: &mut PhotocraftApp, f: &mut Map) { + let printers = photocraft_engine::print_cmds::printers_without_waiting(&mut app.session); + f.insert("__systemPrinters".into(), printers["printers"].clone()); + if let Ok(list) = photocraft_engine::print_cmds::rgb_printers_without_waiting(&app.session) { f.insert("__printers".into(), json!(list.iter().map(|p| p.name.clone()).collect::>())); let known = |n: &str| list.iter().any(|p| p.name == n); - if !known(&s(&f, "printer", "")) { + if !list.is_empty() && !known(&s(f, "printer", "")) { let fallback = list.iter().find(|p| p.default).or(list.first()).map(|p| p.name.clone()).unwrap_or_default(); f.insert("printer".into(), json!(fallback)); f.remove("printerSettings"); } } - app.ui.open_dialog(DialogKind::Command, f) } /// Whether the driver's paper, as `driver_page` took it, is wider than tall: the Orientation @@ -91,11 +102,14 @@ pub(super) fn turn_driver(app: &mut PhotocraftApp, f: &mut Map, l if let (Some(h), Some(o)) = (f.get("printerSettings").cloned(), q.as_object_mut()) { o.insert("printerSettings".into(), h); } - match app.session.execute("file.print.page", q) { - Ok(r) => take_turned_page(f, &r), - Err(e) => { + match photocraft_engine::print_cmds::try_with_spooler(|| app.session.execute("file.print.page", q)) { + Some(Ok(r)) => take_turned_page(f, &r), + Some(Err(e)) => { f.insert("__pageError".into(), json!(e.to_string())); } + None => { + f.insert("__pageError".into(), json!(photocraft_engine::print_cmds::SPOOLER_BUSY)); + } } } @@ -115,21 +129,27 @@ pub(super) fn take_turned_page(f: &mut Map, r: &Value) { } /// Takes the paper from the printer's driver settings (Windows), as Photoshop does. -fn driver_page(f: &mut Map) { +pub(super) fn driver_page(f: &mut Map) -> bool { let printer = s(f, "printer", ""); let settings = f.get("printerSettings").and_then(Value::as_str).and_then(|h| craft_print::rgb::DevMode::from_hex(h).ok()); - match craft_print::rgb::page(&printer, settings.as_ref()) { - Ok(pg) => { + match photocraft_engine::print_cmds::try_with_spooler(|| craft_print::rgb::page(&printer, settings.as_ref())) { + Some(Ok(pg)) => { f.insert("paper".into(), json!([pg.paper.0, pg.paper.1])); f.insert("printable".into(), json!([pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3])); f.insert("orientation".into(), json!("portrait")); f.remove("__pageError"); } - Err(e) => { + Some(Err(e)) => { f.remove("printable"); f.insert("__pageError".into(), json!(e.to_string())); } + None => { + f.insert("__pageError".into(), json!(photocraft_engine::print_cmds::SPOOLER_BUSY)); + f.remove("__driverPageInitialized"); + return false; + } } + true } /// Points per unit for the Position and Size fields. @@ -280,6 +300,12 @@ pub(super) fn print_preview_image(l: photocraft_engine::print_cmds::PrintLayout, /// Dragging the visible image updates dialog offsets live from a fixed gesture origin; /// the position row stays present while centered so switching to manual cannot move the page. pub(super) fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, id: u64) { + if b(f, "__printerListingPending", false) { + refresh_printers(app, f); + if b(f, "__printerListingPending", false) { + ui.ctx().request_repaint_after(std::time::Duration::from_millis(100)); + } + } let t = Tokens::get(ui.ctx()); let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return }; // PDF exports and previews retain editable paper on Windows too. Only a real native @@ -292,8 +318,11 @@ pub(super) fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map }; if printers.is_some() { if !b(f, "__driverPageInitialized", false) { - driver_page(f); - f.insert("__driverPageInitialized".into(), json!(true)); + if driver_page(f) { + f.insert("__driverPageInitialized".into(), json!(true)); + } else { + ui.ctx().request_repaint_after(std::time::Duration::from_millis(100)); + } } } else { // PDF output and previews have no driver page: Scale to Fit Media uses the paper. @@ -416,15 +445,18 @@ pub(super) fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map if ui.button(tl!("Print Settings…")).clicked() { let printer = s(f, "printer", ""); let current = f.get("printerSettings").and_then(Value::as_str).and_then(|h| craft_print::rgb::DevMode::from_hex(h).ok()); - match craft_print::rgb::settings_dialog(&printer, current.as_ref(), None) { - Ok(Some(dm)) => { + match photocraft_engine::print_cmds::try_with_spooler(|| craft_print::rgb::settings_dialog(&printer, current.as_ref(), None)) { + Some(Ok(Some(dm))) => { f.insert("printerSettings".into(), json!(dm.to_hex())); driver_page(f); } - Ok(None) => {} - Err(e) => { + Some(Ok(None)) => {} + Some(Err(e)) => { f.insert("__pageError".into(), json!(e.to_string())); } + None => { + f.insert("__pageError".into(), json!(photocraft_engine::print_cmds::SPOOLER_BUSY)); + } } } }); diff --git a/crates/ui-egui/src/dialogs/file_ui/tests.rs b/crates/ui-egui/src/dialogs/file_ui/tests.rs index 1bc5273cad..19f7b1ceff 100644 --- a/crates/ui-egui/src/dialogs/file_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/file_ui/tests.rs @@ -1,5 +1,164 @@ use super::*; +#[test] +fn busy_driver_page_requests_a_retry_without_input() { + let (mut app, ctx) = app(); + let mut fields = json!({"printer": "Test printer", "__printers": ["Test printer"], "paper": "letter"}).as_object().unwrap().clone(); + let (held_tx, held) = std::sync::mpsc::channel(); + let (release, released_rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + photocraft_engine::print_cmds::with_spooler(|| { + held_tx.send(()).unwrap(); + let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); + }); + }); + held.recv().unwrap(); + let mut delay = std::time::Duration::MAX; + for _ in 0..6 { + let mut output = ctx.run_ui(Default::default(), |ui| print_body(&mut app, ui, &mut fields, 1)); + delay = output.viewport_output[&egui::ViewportId::ROOT].repaint_delay; + output.textures_delta.clear(); + } + let busy = fields.get("__pageError").cloned(); + let initialized = fields.get("__driverPageInitialized").cloned(); + let _ = release.send(()); + holder.join().unwrap(); + assert_eq!(busy, Some(json!(photocraft_engine::print_cmds::SPOOLER_BUSY))); + assert_ne!(initialized, Some(json!(true))); + assert!(delay <= std::time::Duration::from_millis(100), "busy driver repaint delay: {delay:?}"); + photocraft_engine::print_cmds::with_spooler(|| { + ctx.run_ui(Default::default(), |ui| print_body(&mut app, ui, &mut fields, 1)).textures_delta.clear(); + assert_eq!(fields.get("__driverPageInitialized"), Some(&json!(true))); + assert_ne!(fields.get("__pageError"), Some(&json!(photocraft_engine::print_cmds::SPOOLER_BUSY))); + }); +} + +#[cfg(windows)] +#[test] +fn print_dialog_keeps_saved_settings_when_the_cached_printer_list_is_empty() { + use std::sync::atomic::{AtomicBool, Ordering}; + let (mut app, ctx) = app(); + let available = Arc::new(AtomicBool::new(false)); + let listed = Arc::clone(&available); + app.session.printer_source = Some(Arc::new(move || { + if listed.load(Ordering::SeqCst) { + vec![craft_print::Printer { name: "Saved printer".into(), default: true, two_sided: None, description: String::new() }] + } else { + Vec::new() + } + })); + app.session.prefs.edit(|p| { + p.dialogs.insert(photocraft_engine::print_cmds::REMEMBERED.into(), json!({"printer": "Saved printer", "printerSettings": "0011"})); + }); + let mut lister = photocraft_engine::Session::new(); + lister.printer_source = app.session.printer_source.clone(); + let (held_tx, held) = std::sync::mpsc::channel(); + let (release, released_rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + photocraft_engine::print_cmds::with_spooler(|| { + assert!(photocraft_engine::print_cmds::rgb_printers(&lister).unwrap().is_empty()); + available.store(true, Ordering::SeqCst); + held_tx.send(()).unwrap(); + let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); + }); + }); + held.recv().unwrap(); + let id = open_print(&mut app); + let mut fields = app.ui.dialog_mut(id).unwrap().fields.clone(); + let before_retry = (fields.get("printer").cloned(), fields.get("printerSettings").cloned()); + let pending = fields.get("__printerListingPending").cloned(); + let _ = release.send(()); + holder.join().unwrap(); + assert_eq!(pending, Some(json!(true))); + assert_eq!(before_retry, (Some(json!("Saved printer")), Some(json!("0011")))); + fields.insert("dryRun".into(), json!(true)); + photocraft_engine::print_cmds::with_spooler(|| { + ctx.run_ui(Default::default(), |ui| print_body(&mut app, ui, &mut fields, id)).textures_delta.clear(); + assert_eq!(fields["printer"], "Saved printer"); + assert_eq!(fields["printerSettings"], "0011"); + assert!(!fields.contains_key("__printerListingPending")); + }); +} + +#[test] +fn print_dialog_retries_an_uncached_printer_list_after_the_spooler_is_released() { + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + let (mut app, ctx) = app(); + let asked = Arc::new(AtomicUsize::new(0)); + let counted = Arc::clone(&asked); + app.session.printer_source = Some(Arc::new(move || { + counted.fetch_add(1, Ordering::SeqCst); + vec![craft_print::Printer { name: "Retry printer".into(), default: true, two_sided: None, description: String::new() }] + })); + let (held_tx, held) = std::sync::mpsc::channel(); + let (release, released_rx) = std::sync::mpsc::channel::<()>(); + let released = Arc::new(AtomicBool::new(false)); + let done = Arc::clone(&released); + let holder = std::thread::spawn(move || { + photocraft_engine::print_cmds::with_spooler(|| { + held_tx.send(()).unwrap(); + let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); + done.store(true, Ordering::SeqCst); + }); + }); + held.recv().unwrap(); + let id = open_print(&mut app); + let mut fields = app.ui.dialog_mut(id).unwrap().fields.clone(); + fields.insert("dryRun".into(), json!(true)); + ctx.run_ui(Default::default(), |ui| print_body(&mut app, ui, &mut fields, id)).textures_delta.clear(); + let waited = released.load(Ordering::SeqCst); + let calls_while_busy = asked.load(Ordering::SeqCst); + let _ = release.send(()); + holder.join().unwrap(); + assert!(!waited, "opening or drawing Print waited for the spooler"); + assert_eq!(calls_while_busy, 0); + assert_eq!(fields["__systemPrinters"], json!([])); + photocraft_engine::print_cmds::with_spooler(|| { + ctx.run_ui(Default::default(), |ui| print_body(&mut app, ui, &mut fields, id)).textures_delta.clear(); + assert_eq!(fields["__systemPrinters"], json!([{"name": "Retry printer", "default": true}])); + assert_eq!(fields.get("__printers"), cfg!(windows).then(|| json!(["Retry printer"])).as_ref()); + assert!(!fields.contains_key("__printerListingPending")); + assert!(asked.load(Ordering::SeqCst) > 0); + }); +} + +#[test] +fn driver_calls_do_not_wait_while_the_spooler_is_busy() { + use std::sync::atomic::{AtomicBool, Ordering}; + let (mut app, _) = app(); + let (held_tx, held) = std::sync::mpsc::channel(); + let (release, released_rx) = std::sync::mpsc::channel::<()>(); + let released = Arc::new(AtomicBool::new(false)); + let holder = { + let released = Arc::clone(&released); + std::thread::spawn(move || { + photocraft_engine::print_cmds::with_spooler(|| { + held_tx.send(()).unwrap(); + let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); + released.store(true, Ordering::SeqCst); + }); + }) + }; + held.recv().unwrap(); + let mut page = Map::new(); + page.insert("printer".into(), json!("Test printer")); + page.insert("__driverPageInitialized".into(), json!(true)); + let answered = print::driver_page(&mut page); + let mut turn = Map::new(); + turn.insert("printer".into(), json!("Test printer")); + turn.insert("paper".into(), json!([595.28, 841.89])); + turn_driver(&mut app, &mut turn, true); + let waited = released.load(Ordering::SeqCst); + let _ = release.send(()); + holder.join().unwrap(); + assert!(!waited, "a driver call waited for the spooler"); + assert!(!answered); + assert!(page.get("__driverPageInitialized").is_none()); + assert_eq!(page["__pageError"], photocraft_engine::print_cmds::SPOOLER_BUSY); + assert_eq!(turn["__pageError"], photocraft_engine::print_cmds::SPOOLER_BUSY); + assert_eq!((turn.get("paper"), turn.get("printerSettings")), (Some(&json!([595.28, 841.89])), None)); +} + /// Checks no-jump centering and nonaccumulating screen displacement against shared layout /// across fit, percentage scaling, orientation and mirroring combinations. #[test] @@ -59,6 +218,8 @@ fn print_drag_rejects_nonfinite_input_and_bounds_offsets() { fn app() -> (PhotocraftApp, egui::Context) { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.printer_source = + Some(Arc::new(|| vec![craft_print::Printer { name: "Test printer".into(), default: true, two_sided: None, description: String::new() }])); app.run("file.new", json!({"width": 120, "height": 80, "name": "web"})).unwrap(); (app, egui::Context::default()) } @@ -432,11 +593,16 @@ fn printer_menu_lists_the_spooler_printers_after_the_default() { assert_eq!(printer_options(None, "").len(), 1); // The dialog asks the engine, and the list never reaches file.print's params. let (mut app, ctx) = app(); - let id = crate::menus::invoke(&mut app, &ctx, "file.print", json!({})).unwrap()["dialog"].as_u64().unwrap(); + let id = photocraft_engine::print_cmds::with_spooler(|| crate::menus::invoke(&mut app, &ctx, "file.print", json!({})).unwrap()["dialog"].as_u64().unwrap()); let f = &app.ui.dialog_mut(id).unwrap().fields; - assert!(f.get("__systemPrinters").is_some_and(Value::is_array)); + assert_eq!(f["__systemPrinters"], json!([{"name": "Test printer", "default": true}])); + assert_eq!(f.get("__printers"), cfg!(windows).then(|| json!(["Test printer"])).as_ref()); + if cfg!(windows) { + assert_eq!(f["printer"], "Test printer"); + } assert!(params(f).get("__systemPrinters").is_none()); assert!(params(f).get("__printers").is_none()); + assert_eq!(photocraft_engine::print_cmds::system_printer_listings(), 0, "a test asked the system spooler for its printers"); } /// An output path or a preview typed and confirmed in one frame doesn't take the driver's area. @@ -478,7 +644,10 @@ fn save_as_pdf_browse_fills_the_output_from_a_save_dialog() { let (show, asked) = crate::file_dialog::fake(vec![Some(FileDialogAnswer::SaveTo(out.clone()))]); let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| { PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); - PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services { file_dialog: Some(show), ..Default::default() }) + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services { file_dialog: Some(show), ..Default::default() }); + app.session.printer_source = + Some(Arc::new(|| vec![craft_print::Printer { name: "Test printer".into(), default: true, two_sided: None, description: String::new() }])); + app }); h.state_mut().run("file.new", json!({"width": 120, "height": 80, "name": "poster"})).unwrap(); let id = open_print(h.state_mut()); diff --git a/crates/ui-egui/src/dialogs/history_options_ui.rs b/crates/ui-egui/src/dialogs/history_options_ui.rs new file mode 100644 index 0000000000..2d4756543b --- /dev/null +++ b/crates/ui-egui/src/dialogs/history_options_ui.rs @@ -0,0 +1,98 @@ +//! The History Options dialog (History panel menu › History Options…, #2966): a form over the +//! engine's `history.options` with its two checkboxes, filled with the current values. OK runs +//! the command with the dialog's values (the generic command-dialog confirm), Cancel changes +//! nothing. Agents can also run `history.options` with params directly, or `prefs.set +//! historyOptions.*`; a call with params skips the dialog. + +use serde_json::{Map, Value, json}; + +use crate::PhotocraftApp; +use crate::state::DialogKind; + +pub const COMMAND: &str = "history.options"; + +/// Opens the dialog for a parameterless `history.options`; anything else is not handled here. +pub fn menu(app: &mut PhotocraftApp, id: &str, params: &Value) -> Option> { + if id != COMMAND || params.as_object().is_some_and(|m| m.keys().any(|k| !k.starts_with("__"))) { + return None; + } + Some(Ok(json!({ "dialog": open(app) }))) +} + +/// Open the dialog on the current options; returns its id. +pub fn open(app: &mut PhotocraftApp) -> u64 { + let current = photocraft_engine::history_options_cmds::current(&app.session); + let mut f = Map::new(); + f.insert("__command".into(), json!(COMMAND)); + f.insert("__label".into(), json!("History Options…")); + f.insert("__form".into(), json!(true)); + f.insert("__choices".into(), json!({})); + // Photoshop's capitalised labels, in the UI language (the form would sentence-case the keys). + f.insert( + "__labels".into(), + json!({ + "allowNonLinearHistory": tl!("Allow Non-Linear History"), + "makeLayerVisibilityChangesUndoable": tl!("Make Layer Visibility Changes Undoable"), + }), + ); + for key in photocraft_engine::history_options_cmds::OPTIONS { + f.insert(key.into(), current.get(key).cloned().unwrap_or(Value::Bool(false))); + } + app.ui.open_dialog(DialogKind::Command, f) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn app() -> PhotocraftApp { + PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()) + } + + #[test] + fn the_command_opens_the_dialog_on_the_current_values_and_ok_applies_them() { + let mut app = app(); + let ctx = egui::Context::default(); + let r = crate::menus::invoke(&mut app, &ctx, COMMAND, json!({})).unwrap(); + let id = r["dialog"].as_u64().unwrap(); + let d = app.ui.dialogs.iter().find(|d| d.id == id).unwrap(); + assert_eq!(d.fields["allowNonLinearHistory"], json!(false)); + assert_eq!(d.fields["makeLayerVisibilityChangesUndoable"], json!(true)); + assert_eq!(crate::dialogs::title(d), "History Options"); + // Cancel changes nothing. + app.ui.dialog_mut(id).unwrap().fields.insert("allowNonLinearHistory".into(), json!(true)); + crate::dialogs::cancel(&mut app, id).unwrap(); + assert!(!app.session.prefs().history_options.allow_non_linear_history); + // OK applies both checkboxes. + let id = open(&mut app); + let fields = &mut app.ui.dialog_mut(id).unwrap().fields; + fields.insert("allowNonLinearHistory".into(), json!(true)); + fields.insert("makeLayerVisibilityChangesUndoable".into(), json!(false)); + crate::dialogs::confirm(&mut app, id).unwrap(); + let o = &app.session.prefs().history_options; + assert!(o.allow_non_linear_history && !o.make_layer_visibility_changes_undoable); + assert!(app.ui.dialogs.is_empty()); + // Reopened, it shows what was chosen; with params the command runs without a dialog. + let id = open(&mut app); + assert_eq!(app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields["allowNonLinearHistory"], json!(true)); + crate::dialogs::cancel(&mut app, id).unwrap(); + let r = crate::menus::invoke(&mut app, &ctx, COMMAND, json!({"allowNonLinearHistory": false})).unwrap(); + assert_eq!(r["allowNonLinearHistory"], json!(false)); + assert!(app.ui.dialogs.is_empty()); + assert!(crate::menus::invoke(&mut app, &ctx, COMMAND, json!({"allowNonLinearHistory": "no"})).is_err()); + } + + #[test] + fn the_dialog_draws_both_checkboxes_with_photoshops_labels() { + use egui_kittest::{Harness, kittest::Queryable}; + let mut h = Harness::builder().with_size(egui::vec2(700.0, 400.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app()); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + open(h.state_mut()); + h.run_steps(3); + h.get_by_label("History Options"); + h.get_by_label("Allow Non-Linear History"); + h.get_by_label("Make Layer Visibility Changes Undoable"); + h.get_by_label("OK"); + h.get_by_label("Cancel"); + } +} diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv index 925d19568b..3d40d04d18 100644 --- a/crates/ui-egui/src/i18n/cs.tsv +++ b/crates/ui-egui/src/i18n/cs.tsv @@ -1966,7 +1966,7 @@ Full size tip Hrot v plné velikosti Gamut warning color Barva upozornění na gamut Gamut warning opacity Krytí upozornění na gamut - Generator enabled Generátor zapnut + Enable Generator Zapnout generátor Gif GIF Glossy Lesklý Grid color Barva mřížky @@ -3618,6 +3618,9 @@ curves Smooth Vyhladit Clear History Vymazat historii New Snapshot… Nový snímek… History Options… Volby historie… +# History panel › History Options… (#2966) + Allow Non-Linear History Povolit nelineární historii + Make Layer Visibility Changes Undoable Umožnit vrácení změn viditelnosti vrstev Drag (lasso) or click points (polygonal) · {add} add · {sub} subtract Táhněte (laso) nebo klikejte na body (mnohoúhelník) · {add} přidat · {sub} odečíst The current gradient (pick one in Window › Gradients) Aktuální přechod (vyberte v Okno › Přechody) # Canvas context menus, Channels panel menu, Layer Style blending page (labels rendered through a variable). diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv index 6558daa51d..418125859b 100644 --- a/crates/ui-egui/src/i18n/de.tsv +++ b/crates/ui-egui/src/i18n/de.tsv @@ -1941,7 +1941,7 @@ Full size tip Spitze in voller Größe Gamut warning color Farbumfang-Warnfarbe Gamut warning opacity Deckkraft der Farbumfang-Warnung - Generator enabled Generator aktiviert + Enable Generator Generator aktivieren Gif GIF Glossy Glänzend Grid color Rasterfarbe @@ -2814,6 +2814,9 @@ curves Smooth Glätten Clear History Protokoll löschen New Snapshot… Neuer Schnappschuss… History Options… Protokolloptionen… +# History panel › History Options… (#2966) + Allow Non-Linear History Nicht-lineare Protokolle zulassen + Make Layer Visibility Changes Undoable Änderungen der Ebenensichtbarkeit widerrufbar machen # Print dialog (#2697) Emulsion Down Schichtseite unten Functions Funktionen diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv index 5f1a3d3538..3e7e7390fa 100644 --- a/crates/ui-egui/src/i18n/el.tsv +++ b/crates/ui-egui/src/i18n/el.tsv @@ -1714,7 +1714,7 @@ Full size tip Άκρη πλήρους μεγέθους Gamut warning color Χρώμα προειδοποίησης χρωματικής γκάμας Gamut warning opacity Αδιαφάνεια προειδοποίησης χρωματικής γκάμας - Generator enabled Ενεργοποίηση γεννήτριας + Enable Generator Ενεργοποίηση γεννήτριας Click the first point or press ↵ to close · Esc cancels Κάντε κλικ στο πρώτο σημείο ή πατήστε ↵ για κλείσιμο · Esc για ακύρωση Click to add a point · drag off or ⌘/Ctrl-click or Delete to remove Κλικ για προσθήκη σημείου · σύρσιμο εκτός ή ⌘/Ctrl-κλικ ή Delete για αφαίρεση Click to apply, ⌥-click to clear overrides, double-click for Style Options Κλικ για εφαρμογή, ⌥-κλικ για εκκαθάριση παρακάμψεων, διπλό κλικ για επιλογές στυλ @@ -3860,6 +3860,9 @@ curves Smooth Εξομάλυνση # History panel menu (#2966) New Snapshot… Νέο στιγμιότυπο… History Options… Επιλογές ιστορικού… +# History panel › History Options… (#2966) + Allow Non-Linear History Να επιτρέπεται μη γραμμικό ιστορικό + Make Layer Visibility Changes Undoable Οι αλλαγές ορατότητας επιπέδων να αναιρούνται # Print dialog (#2697) Emulsion Down Γαλάκτωμα προς τα κάτω Functions Λειτουργίες diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv index 879572c9f4..acacb0402a 100644 --- a/crates/ui-egui/src/i18n/es.tsv +++ b/crates/ui-egui/src/i18n/es.tsv @@ -1954,7 +1954,7 @@ Full size tip Punta a tamaño real Gamut warning color Color de advertencia de gama Gamut warning opacity Opacidad de advertencia de gama - Generator enabled Generador activado + Enable Generator Activar generador Gif GIF Glossy Brillante Grid color Color de la cuadrícula @@ -2848,6 +2848,9 @@ curves Smooth Suavizar # History panel menu (#2966) New Snapshot… Nueva instantánea… History Options… Opciones de historial… +# History panel › History Options… (#2966) + Allow Non-Linear History Permitir historia no lineal + Make Layer Visibility Changes Undoable Permitir deshacer los cambios de visibilidad de capas # Print dialog (#2697) Emulsion Down Emulsión hacia abajo Functions Funciones diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv index 9692437211..4b481366ed 100644 --- a/crates/ui-egui/src/i18n/fr.tsv +++ b/crates/ui-egui/src/i18n/fr.tsv @@ -1922,7 +1922,7 @@ Full size tip Pointe en taille réelle Gamut warning color Couleur de l'alerte de gamut Gamut warning opacity Opacité de l'alerte de gamut - Generator enabled Générateur activé + Enable Generator Activer le générateur Gif GIF Glossy Brillant Grid color Couleur de la grille @@ -3184,6 +3184,9 @@ curves Smooth Lisser # History panel menu (#2966) New Snapshot… Nouvel instantané… History Options… Options d'historique… +# History panel › History Options… (#2966) + Allow Non-Linear History Autoriser l'historique non linéaire + Make Layer Visibility Changes Undoable Permettre d'annuler les changements de visibilité des calques # Print dialog (#2697) Emulsion Down Émulsion verso Functions Fonctions diff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsv index 1931aaa2b0..9fb897d482 100644 --- a/crates/ui-egui/src/i18n/hu.tsv +++ b/crates/ui-egui/src/i18n/hu.tsv @@ -611,6 +611,8 @@ High Pass… Felüláteresztő… Histogram Hisztogram History Előzmények + Allow Non-Linear History Nem lineáris előzmények engedélyezése + Make Layer Visibility Changes Undoable A rétegláthatósági változások visszavonhatók Horizontal Vízszintes HSB/HSL HSB/HSL Huge Óriási @@ -3278,7 +3280,7 @@ cameraRaw Yellow Sárga Full size tip Teljes méretű hegy Fullscreen Teljes képernyő General… Általános… - Generator enabled Generátor bekapcsolva + Enable Generator Generátor bekapcsolása Generators Generátorok Generic Általános GitHub GitHub diff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsv index eb890e9bce..0fa31a8ebd 100644 --- a/crates/ui-egui/src/i18n/id.tsv +++ b/crates/ui-egui/src/i18n/id.tsv @@ -1932,7 +1932,7 @@ Full size tip Ujung ukuran penuh Gamut warning color Warna peringatan gamut Gamut warning opacity Opasitas peringatan gamut - Generator enabled Generator aktif + Enable Generator Aktifkan Generator Gif GIF Glossy Mengilap Grid color Warna grid @@ -3187,6 +3187,9 @@ curves Smooth Haluskan # History panel menu (#2966) New Snapshot… Snapshot Baru… History Options… Opsi Riwayat… +# History panel › History Options… (#2966) + Allow Non-Linear History Izinkan Riwayat Non-Linier + Make Layer Visibility Changes Undoable Perubahan Visibilitas Layer Dapat Diurungkan # Print dialog (#2697) Emulsion Down Emulsi di Bawah Functions Fungsi diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv index 661192d7b6..ea51deccb5 100644 --- a/crates/ui-egui/src/i18n/it.tsv +++ b/crates/ui-egui/src/i18n/it.tsv @@ -1948,7 +1948,7 @@ Full size tip Punta dimensione reale Gamut warning color Colore avviso gamma Gamut warning opacity Opacità avviso gamma - Generator enabled Generatore abilitato + Enable Generator Abilita generatore Gif GIF Glossy Lucido Grid color Colore griglia @@ -2833,6 +2833,9 @@ curves Smooth Leviga # History panel menu (#2966) New Snapshot… Nuova istantanea… History Options… Opzioni cronologia… +# History panel › History Options… (#2966) + Allow Non-Linear History Consenti cronologia non lineare + Make Layer Visibility Changes Undoable Rendi annullabili le modifiche di visibilità dei livelli # Print dialog (#2697) Emulsion Down Emulsione sotto Functions Funzioni diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv index c5c0b02fc0..6d96eeaa2d 100644 --- a/crates/ui-egui/src/i18n/ja.tsv +++ b/crates/ui-egui/src/i18n/ja.tsv @@ -1950,7 +1950,7 @@ Full size tip 実寸の先端 Gamut warning color 色域外警告の色 Gamut warning opacity 色域外警告の不透明度 - Generator enabled 自動生成を有効にする + Enable Generator 自動生成を有効にする Gif GIF Glossy 光沢 Grid color グリッドの色 @@ -3774,6 +3774,9 @@ curves Smooth スムーズ # History panel menu (#2966) New Snapshot… 新規スナップショット… History Options… ヒストリーオプション… +# History panel › History Options… (#2966) + Allow Non-Linear History 非線形のヒストリーを許可 + Make Layer Visibility Changes Undoable レイヤーの表示/非表示の変更を取り消し可能にする # Print dialog (#2697) Emulsion Down 乳剤面を下 Functions 機能 diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv index 2c730c1877..85abdc30ec 100644 --- a/crates/ui-egui/src/i18n/ko.tsv +++ b/crates/ui-egui/src/i18n/ko.tsv @@ -1938,7 +1938,7 @@ Full size tip 전체 크기 브러시 끝 Gamut warning color 색상 범위 경고 색상 Gamut warning opacity 색상 범위 경고 불투명도 - Generator enabled 생성기 활성화 + Enable Generator 생성기 활성화 Gif GIF Glossy 광택 Grid color 격자 색상 @@ -3379,6 +3379,9 @@ curves Smooth 매끄럽게 Clear History 작업 내역 지우기 New Snapshot… 새 스냅샷… History Options… 작업 내역 옵션… +# History panel › History Options… (#2966) + Allow Non-Linear History 비선형 작업 내역 허용 + Make Layer Visibility Changes Undoable 레이어 표시 여부 변경을 실행 취소할 수 있게 하기 # Print dialog (#2697) Emulsion Down 감광면 아래로 Functions 기능 diff --git a/crates/ui-egui/src/i18n/nl.tsv b/crates/ui-egui/src/i18n/nl.tsv index a8539cd159..3fb8ee4399 100644 --- a/crates/ui-egui/src/i18n/nl.tsv +++ b/crates/ui-egui/src/i18n/nl.tsv @@ -1922,7 +1922,7 @@ Full size tip Punt op volledige grootte Gamut warning color Kleur waarschuwing kleurbereik Gamut warning opacity Dekking waarschuwing kleurbereik - Generator enabled Generator ingeschakeld + Enable Generator Generator inschakelen Gif GIF Glossy Glanzend Grid color Rasterkleur @@ -3922,6 +3922,9 @@ curves Smooth Vloeiend maken # History panel menu (#2966) New Snapshot… Nieuwe momentopname… History Options… Geschiedenisopties… +# History panel › History Options… (#2966) + Allow Non-Linear History Niet-lineaire geschiedenis toestaan + Make Layer Visibility Changes Undoable Wijzigingen in laagzichtbaarheid ongedaan kunnen maken # Print dialog (#2697) Emulsion Down Emulsie omlaag Functions Functies diff --git a/crates/ui-egui/src/i18n/pl.tsv b/crates/ui-egui/src/i18n/pl.tsv index 6b2ad286a0..fdd468523a 100644 --- a/crates/ui-egui/src/i18n/pl.tsv +++ b/crates/ui-egui/src/i18n/pl.tsv @@ -1960,7 +1960,7 @@ Full size tip Końcówka w pełnym rozmiarze Gamut warning color Kolor ostrzeżenia o gamucie Gamut warning opacity Krycie ostrzeżenia o gamucie - Generator enabled Generator włączony + Enable Generator Włącz generator Gif GIF Glossy Błyszczący Grid color Kolor siatki @@ -3792,6 +3792,9 @@ curves Smooth Wygładź # History panel menu (#2966) New Snapshot… Nowa migawka… History Options… Opcje historii… +# History panel › History Options… (#2966) + Allow Non-Linear History Zezwalaj na nieliniową historię + Make Layer Visibility Changes Undoable Pozwalaj cofać zmiany widoczności warstw # Print dialog (#2697) Emulsion Down Emulsja w dół Functions Funkcje diff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsv index 098ca1f8a6..f3e38e1e27 100644 --- a/crates/ui-egui/src/i18n/pt-br.tsv +++ b/crates/ui-egui/src/i18n/pt-br.tsv @@ -1916,7 +1916,7 @@ Full size tip Ponta em tamanho real Gamut warning color Cor do aviso de gama Gamut warning opacity Opacidade do aviso de gama - Generator enabled Gerador ativado + Enable Generator Ativar gerador Gif GIF Glossy Brilhante Grid color Cor da grade @@ -3952,6 +3952,9 @@ layerLabel No Color Sem cor # History panel menu (#2966) New Snapshot… Novo instantâneo… History Options… Opções de histórico… +# History panel › History Options… (#2966) + Allow Non-Linear History Permitir histórico não linear + Make Layer Visibility Changes Undoable Permitir desfazer alterações de visibilidade das camadas # Print dialog (#2697) Emulsion Down Emulsão para baixo Functions Funções diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv index d3e6033306..8d86a06979 100644 --- a/crates/ui-egui/src/i18n/ru.tsv +++ b/crates/ui-egui/src/i18n/ru.tsv @@ -2376,7 +2376,7 @@ Gamut warning color Цвет предупреждения об охвате Gamut warning opacity Непрозрачность предупреждения об охвате General Основные - Generator enabled Генератор включён + Enable Generator Включить генератор Gif GIF Glossy Глянцевая Grid color Цвет сетки @@ -3115,6 +3115,9 @@ curves Smooth Сгладить # History panel menu (#2966) New Snapshot… Новый снимок… History Options… Параметры истории… +# History panel › History Options… (#2966) + Allow Non-Linear History Разрешить нелинейную историю + Make Layer Visibility Changes Undoable Разрешить отмену изменений видимости слоёв # Print dialog (#2697) Emulsion Down Эмульсией вниз Functions Функции diff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsv index 6a6dfb425f..3a62b455ab 100644 --- a/crates/ui-egui/src/i18n/sr.tsv +++ b/crates/ui-egui/src/i18n/sr.tsv @@ -1895,7 +1895,7 @@ Full size tip Vrh pune veličine Gamut warning color Boja upozorenja o gamutu Gamut warning opacity Neprozirnost upozorenja o gamutu - Generator enabled Generator je omogućen + Enable Generator Omogući generator Gif GIF Glossy Sjajno Grid color Boja mreže @@ -2304,6 +2304,8 @@ Automatically Create New Snapshot When Saving Automatski napravi novi snimak stanja pri čuvanju Show New Snapshot Dialog by Default Podrazumevano prikaži dijalog za novi snimak stanja Clear History Obriši istoriju + Allow Non-Linear History Dozvoli nelinearnu istoriju + Make Layer Visibility Changes Undoable Omogući opozivanje promena vidljivosti slojeva Panel Options… Opcije panela… View menu Meni prikaza Keep view in sync Drži prikaz usklađenim diff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsv index d725b8c9ac..b9938796b4 100644 --- a/crates/ui-egui/src/i18n/tr.tsv +++ b/crates/ui-egui/src/i18n/tr.tsv @@ -749,6 +749,8 @@ Gradients Degradeler Histogram Histogram History Geçmiş + Allow Non-Linear History Doğrusal Olmayan Geçmişe İzin Ver + Make Layer Visibility Changes Undoable Katman Görünürlüğü Değişikliklerini Geri Alınabilir Yap Info Bilgi Measurement Log Ölçüm Günlüğü Modifier Keys Değiştirici Tuşlar @@ -1894,7 +1896,7 @@ Full size tip Tam boyutlu uç Gamut warning color Gamut uyarı rengi Gamut warning opacity Gamut uyarı opaklığı - Generator enabled Oluşturucu etkin + Enable Generator Oluşturucuyu etkinleştir Gif GIF Glossy parlak Grid color Izgara rengi diff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsv index 777f03f60a..8db5ac0dcf 100644 --- a/crates/ui-egui/src/i18n/uk.tsv +++ b/crates/ui-egui/src/i18n/uk.tsv @@ -2347,7 +2347,7 @@ Gamut warning color Колір попередження про колірне охоплення Gamut warning opacity Непрозорість попередження про колірне охоплення General Загальні - Generator enabled Генератор увімкнено + Enable Generator Увімкнути генератор Gif GIF Glossy Глянцевий Grid color Колір сітки @@ -3862,6 +3862,9 @@ curves Smooth Згладити # History panel menu (#2966) New Snapshot… Новий знімок стану… History Options… Параметри історії… +# History panel › History Options… (#2966) + Allow Non-Linear History Дозволити нелінійну історію + Make Layer Visibility Changes Undoable Дозволити скасування змін видимості шарів # Print dialog (#2697) Emulsion Down Емульсією донизу Functions Функції diff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsv index 397b02d38d..1b14d41a05 100644 --- a/crates/ui-egui/src/i18n/vi.tsv +++ b/crates/ui-egui/src/i18n/vi.tsv @@ -2719,7 +2719,7 @@ shape Appearance Diện mạo Extra large Rất lớn Fill new type layers with placeholder Tô lớp chữ mới bằng nội dung tạm Font preview Xem trước phông chữ - Generator enabled Đã bật bộ tạo + Enable Generator Bật bộ tạo Glossy Bóng Gutter Lề Ignore exif profile tag Bỏ qua thẻ hồ sơ exif @@ -3697,6 +3697,9 @@ curves Smooth Làm mượt Clear History Xóa lịch sử History Options… Tùy chọn lịch sử… Background (Default) Nền (mặc định) +# History panel › History Options… (#2966) + Allow Non-Linear History Cho phép lịch sử không tuyến tính + Make Layer Visibility Changes Undoable Cho phép hoàn tác thay đổi hiển thị lớp Presentation Trình bày # Batch / Image Processor Include All Subfolders Bao gồm tất cả thư mục con diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv index e6b9080ae6..c45d9c4431 100644 --- a/crates/ui-egui/src/i18n/zh-hans.tsv +++ b/crates/ui-egui/src/i18n/zh-hans.tsv @@ -1992,7 +1992,7 @@ Full size tip 全尺寸笔尖 Gamut warning color 色域警告颜色 Gamut warning opacity 色域警告不透明度 - Generator enabled 启用生成器 + Enable Generator 启用生成器 Gif GIF Glossy 光面 Grid color 网格颜色 @@ -2826,6 +2826,9 @@ curves Smooth 平滑 Clear History 清除历史记录 New Snapshot… 新建快照… History Options… 历史记录选项… +# History panel › History Options… (#2966) + Allow Non-Linear History 允许非线性历史记录 + Make Layer Visibility Changes Undoable 允许撤销图层可见性更改 # Print dialog (#2697) Emulsion Down 药膜朝下 Functions 功能 diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv index 1586d47bf8..e002fb3a6b 100644 --- a/crates/ui-egui/src/i18n/zh-hant.tsv +++ b/crates/ui-egui/src/i18n/zh-hant.tsv @@ -1964,7 +1964,7 @@ Full size tip 完整大小筆尖 Gamut warning color 色域警告顏色 Gamut warning opacity 色域警告不透明度 - Generator enabled 啟用 Generator + Enable Generator 啟用 Generator Gif GIF Glossy 光亮 Grid color 格點顏色 @@ -2775,6 +2775,9 @@ curves Smooth 平滑 Clear History 清除步驟記錄 New Snapshot… 新增快照… History Options… 步驟記錄選項… +# History panel › History Options… (#2966) + Allow Non-Linear History 允許非線性步驟記錄 + Make Layer Visibility Changes Undoable 允許復原圖層可見度變更 # Print dialog (#2697) Emulsion Down 感光乳劑面朝下 Functions 功能 diff --git a/crates/ui-egui/src/jobs_ui/tests.rs b/crates/ui-egui/src/jobs_ui/tests.rs index 6b58786a85..4377074ed5 100644 --- a/crates/ui-egui/src/jobs_ui/tests.rs +++ b/crates/ui-egui/src/jobs_ui/tests.rs @@ -808,6 +808,8 @@ fn printing_keeps_drawing_and_reports_only_the_final_spooler_result() { assert!(h.state().ui.status_error); } } + // The job and its status do not ask the system spooler for its printers. + assert_eq!(photocraft_engine::print_cmds::system_printer_listings(), 0, "a test asked the system spooler for its printers"); } fn layer_count(app: &PhotocraftApp) -> usize { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 5a64b551f4..ef7269dbaa 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -132,6 +132,7 @@ use dialogs::filter_preview_ui; use dialogs::filter_preview_worker; pub use dialogs::gallery_ui; pub use dialogs::guide_layout_ui; +pub use dialogs::history_options_ui; pub use dialogs::jpeg_options_ui; pub use dialogs::layer_picker; pub use dialogs::layer_style; diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 09640f6e27..e1f05a28bd 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -122,6 +122,10 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id: if let Some(r) = crate::plugin_ui::menu(app, id, ¶ms) { return r; } + // History panel menu › History Options… (without params: its dialog). + if let Some(r) = crate::history_options_ui::menu(app, id, ¶ms) { + return r; + } if id == "window.panel.brushSettings" { app.ui.brush_tab = 0; } diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 78812d8c0c..366145f381 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -861,6 +861,49 @@ mod snapshot_row_tests { assert_eq!(st.snapshots.len(), 2, "the snapshot is kept"); } + /// With Allow Non-Linear History and only the oldest state current, a click on the trash has + /// nothing to delete (the oldest state stays, as in Photoshop), so the trash is disabled; a + /// later (dimmed) state dragged onto it is still deleted, and once a past state exists again + /// the click works. + #[test] + fn non_linear_trash_click_never_targets_the_oldest_state() { + let mut h = pro_history(); + h.state_mut().run("history.options", json!({"allowNonLinearHistory": true})).unwrap(); + for _ in 0..3 { + h.state_mut().run("edit.undo", json!({})).unwrap(); + } + h.run_steps(3); + let history = |h: &Harness<'static, PhotocraftApp>| { + let st = h.state().session.active().unwrap(); + (st.history.past_len(), st.history.redo_labels().count()) + }; + assert_eq!(history(&h), (0, 3), "Open current, New Layer, Fill, Fill dimmed"); + assert!(h.state().session.is_enabled("history.deleteState"), "the engine still offers a later state to delete"); + let enabled = |h: &Harness<'static, PhotocraftApp>| { + let p = trash(h); + h.ctx.viewport(|v| { + v.prev_pass.widgets.layers().flat_map(|(_, w)| w.iter()).find(|w| w.rect.size() == vec2(26.0, 26.0) && w.rect.center() == p).map(|w| w.enabled) + }) + }; + assert_eq!(enabled(&h), Some(false), "a click would only target the oldest state"); + let p = trash(&h); + click(&mut h, p); + assert_eq!(history(&h), (0, 3), "the click deletes nothing"); + assert!(!h.state().ui.status_error, "and reports no error: {}", h.state().ui.status); + + // The last dimmed Fill dragged onto the trash: only it goes. + let row = h.get_all_by_label("Fill").last().expect("the dimmed Fill row").rect().center(); + drag(&mut h, row, p); + assert_eq!(history(&h), (0, 2), "the dragged later state is deleted"); + + // A past state again: the click deletes the current one. + h.state_mut().run("edit.redo", json!({})).unwrap(); + h.run_steps(3); + assert_eq!(enabled(&h), Some(true)); + click(&mut h, p); + assert_eq!(history(&h).0, 0, "the click deleted the current state"); + } + /// Snapshot rows scroll with the states: many snapshots in a short dock leave the footer and /// its camera button in view. #[test] diff --git a/crates/ui-egui/src/panels/adjust_ui.rs b/crates/ui-egui/src/panels/adjust_ui.rs index ae6722e574..2d70592c52 100644 --- a/crates/ui-egui/src/panels/adjust_ui.rs +++ b/crates/ui-egui/src/panels/adjust_ui.rs @@ -174,7 +174,7 @@ fn lookup_params(key: &str) -> Value { pub fn color_lookup_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: LayerId, adj: &Adjustment) { let t = Tokens::get(ui.ctx()); - let Adjustment::ColorLookup { name, lut, size, tetrahedral, dither } = adj else { + let Adjustment::ColorLookup { name, lut, size, tetrahedral, dither, .. } = adj else { return; }; let builtins = photocraft_engine::adjust_cmds::LOOKS; diff --git a/crates/ui-egui/src/panels/color_panel_ui.rs b/crates/ui-egui/src/panels/color_panel_ui.rs index 19f223b8a2..952176e61a 100644 --- a/crates/ui-egui/src/panels/color_panel_ui.rs +++ b/crates/ui-egui/src/panels/color_panel_ui.rs @@ -537,6 +537,7 @@ fn rgb3(c: [f32; 4]) -> [f32; 3] { /// other corners; the add-to-swatches button sits under it. fn wheel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let t = Tokens::get(ui.ctx()); + let top = ui.cursor().top(); let background = app.ui.color_panel.background; let rgb = active(app); let v = current_values(ui, Model::Hsb, background, rgb); @@ -556,9 +557,12 @@ fn wheel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let mut changed = edited.is_some(); ui.add_space(6.0); let fill = ui.data(|d| d.get_temp::(color_field_fill_id())); - let gaps = 2.0 * ui.spacing().item_spacing.y + 6.0; let w = ui.available_width(); - let side = fill.filter(|f| f.is_finite()).map_or(w.min(220.0), |f| (f - top_h - gaps).min(w)).max(80.0); + // The wheel takes what the dock group has left under the sliders as drawn (they stand taller + // than `ROW_H`), down to 28 pt as the slider modes' ramp: an estimate and an 80 pt floor ran + // it past the bottom of the Color group at its default height. + let used = ui.cursor().top() - top; + let side = fill.filter(|f| f.is_finite()).map_or(w.min(220.0), |f| (f - used).min(w)).max(28.0); let (area, resp) = ui.allocate_exact_size(vec2(w, side), Sense::click_and_drag()); resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Unknown, ui.is_enabled(), "Color Wheel")); let (center, inner, outer) = wheel_geometry(area); diff --git a/crates/ui-egui/src/panels/history.rs b/crates/ui-egui/src/panels/history.rs index 9f8d6033cb..7af5b3e3c1 100644 --- a/crates/ui-egui/src/panels/history.rs +++ b/crates/ui-egui/src/panels/history.rs @@ -10,6 +10,7 @@ pub(super) fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let entries = st.history.entries(); let current = entries.len() - 1; let redo: Vec = st.history.redo_labels().map(str::to_string).collect(); + let non_linear = st.history.non_linear; let doc_id = st.doc.id; let source = photocraft_engine::history_source_cmds::source(st); let selected_snap = selected_snapshot(ui.ctx(), st); @@ -45,8 +46,9 @@ pub(super) fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) { for (i, (e, is_redo)) in all.enumerate() { // The current state shows selected unless a snapshot is (Photoshop highlights one row). let selected = i == current && selected_snap.is_none(); - // Past states can be dragged onto the trash; the oldest one can't be deleted. - let sense = if is_redo || i == 0 { Sense::click() } else { Sense::click_and_drag() }; + // Past states can be dragged onto the trash; the oldest one can't be deleted. With + // Allow Non-Linear History a later (dimmed) state can be deleted too. + let sense = if (is_redo && !non_linear) || i == 0 { Sense::click() } else { Sense::click_and_drag() }; let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 24.0), sense); resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, selected, &e)); if resp.drag_started() { @@ -105,15 +107,24 @@ pub(super) fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) { { let label = match drag { HistoryDrag::Snapshot(id) => app.session.active().and_then(|st| st.snapshots.iter().find(|s| s.id == id)).map(|s| s.name.clone()), - HistoryDrag::State(i) => entries.get(i).cloned(), + // Rows number the past states, then the later (dimmed) ones a non-linear drag can lift. + HistoryDrag::State(i) => entries.get(i).or_else(|| i.checked_sub(entries.len()).and_then(|j| redo.get(j))).cloned(), }; crate::layer_transfer::ghost(&ctx, p, &label.unwrap_or_default()); } let mut footer_cmd: Option<(&str, Value)> = None; let mut snapshot_dialog = false; if t.pro { - // With a snapshot selected the trash deletes it, as in Photoshop (#5324). - let can_delete = selected_snap.is_some() || app.session.is_enabled("history.deleteState"); + // With a snapshot selected the trash deletes it, as in Photoshop (#5324). A click deletes + // the current state, never the oldest; a drop can delete a later (dimmed) one, which + // Allow Non-Linear History offers even while the oldest state is current. + let can_click = selected_snap.is_some() || app.session.active().is_some_and(|st| st.history.past_len() > 0); + let can_drop = match drag { + Some(HistoryDrag::Snapshot(_)) => true, + Some(HistoryDrag::State(_)) => app.session.is_enabled("history.deleteState"), + None => false, + }; + let can_delete = can_click || can_drop; let can_new = app.session.is_enabled("history.newDocument"); widgets::panel_footer(ui, |ui| { let trash = ui.add_enabled_ui(can_delete, |ui| icons::button(ui, "trash", 26.0, false, tl!("Delete current state"))).inner; diff --git a/crates/ui-egui/src/panels/history_menu_ui.rs b/crates/ui-egui/src/panels/history_menu_ui.rs index 6e3e197662..245fa986d8 100644 --- a/crates/ui-egui/src/panels/history_menu_ui.rs +++ b/crates/ui-egui/src/panels/history_menu_ui.rs @@ -19,8 +19,8 @@ pub const ITEMS: &[(&str, Option<&str>)] = &[ ("Clear History", Some("history.clear")), ("New Document", Some("history.newDocument")), ("---", None), - // The History Options dialog is not built yet. - ("History Options…", None), + // Without params `history.options` opens the History Options dialog (#3535). + ("History Options…", Some("history.options")), ("---", None), ("Close", Some("window.panel.close")), ]; @@ -63,7 +63,9 @@ pub fn panel_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let Some(cmd) = clicked else { return }; ui.close(); let (cmd, params) = resolve(cmd, snapshot); - if let Err(e) = app.run(cmd, params) { + // History Options… opens its dialog, as the menu bar's command does. + let result = crate::history_options_ui::menu(app, cmd, ¶ms).unwrap_or_else(|| app.run(cmd, params)); + if let Err(e) = result { app.ui.status = e; app.ui.status_error = true; } @@ -109,7 +111,7 @@ mod tests { let mut h = harness(app()); assert_eq!(states(&h), (4, 0)); assert!(is_disabled(&h, "Step Forward") && !is_disabled(&h, "Step Backward")); - assert!(is_disabled(&h, "History Options…"), "not built yet: greyed"); + assert!(!is_disabled(&h, "History Options…")); assert!(!is_disabled(&h, "New Snapshot…")); click(&mut h, "Step Backward"); assert_eq!(states(&h), (3, 1)); @@ -126,6 +128,9 @@ mod tests { click(&mut h, "New Document"); assert_eq!(h.state().session.documents().len(), 2); assert!(!h.state().ui.status_error); + click(&mut h, "History Options…"); + assert_eq!(h.state().ui.dialogs.len(), 1, "History Options… opens its dialog"); + assert!(!h.state().ui.status_error, "{}", h.state().ui.status); } #[test] @@ -188,10 +193,12 @@ mod tests { } #[test] - fn without_a_document_only_close_is_enabled() { + fn without_a_document_only_close_and_history_options_are_enabled() { let h = harness(PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default())); for &(label, cmd) in ITEMS.iter().filter(|(l, _)| *l != "---") { - assert_eq!(is_disabled(&h, label), cmd != Some("window.panel.close"), "{label}"); + // History Options are preferences, not part of a document. + let on = matches!(cmd, Some("window.panel.close" | "history.options")); + assert_eq!(is_disabled(&h, label), !on, "{label}"); } } } diff --git a/crates/ui-egui/src/panels/history_panel_select_tests.rs b/crates/ui-egui/src/panels/history_panel_select_tests.rs index 85be286d6a..071e343d69 100644 --- a/crates/ui-egui/src/panels/history_panel_select_tests.rs +++ b/crates/ui-egui/src/panels/history_panel_select_tests.rs @@ -59,6 +59,44 @@ fn clicking_a_state_jumps_there_with_one_command() { assert_eq!(state(&h), (4, 0, rev + 2)); } +/// The ghost label that follows a History row being dragged to the trash (the text painted at +/// the pointer, `layer_transfer::ghost`), if one is drawn. +fn ghost_label(h: &Harness<'_, PhotocraftApp>, p: egui::Pos2) -> Option { + let at = p + vec2(20.0, -6.0); + h.output().shapes.iter().find_map(|c| match &c.shape { + egui::Shape::Text(t) if (t.pos - at).length() < 1.0 => Some(t.galley.job.text.clone()), + _ => None, + }) +} + +/// Allow Non-Linear History lets a later (dimmed) state be dragged onto the trash: its row's +/// label follows the pointer, as a past state's does. +#[test] +fn dragging_a_later_state_shows_its_label_at_the_pointer() { + let mut h = harness(); + h.state_mut().run("history.options", json!({"allowNonLinearHistory": true})).unwrap(); + h.state_mut().run("edit.undo", json!({})).unwrap(); + h.run_steps(2); + // Rows: Open, New Layer, Fill, New Layer (current), the dimmed Fill. + for (label, row) in [("New Layer", 1), ("Fill", 1)] { + let from = h.get_all_by_label(label).nth(row).expect("the row").rect().center(); + let to = from + vec2(-60.0, 40.0); + h.hover_at(from); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + for i in 1..=4 { + h.hover_at(from + (to - from) * (i as f32 / 4.0)); + h.run_steps(1); + } + assert_eq!(ghost_label(&h, to).as_deref(), Some(label), "the dragged {label} row's label follows the pointer"); + // Released off the trash: nothing is deleted. + h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); + h.run_steps(2); + } + assert_eq!(h.state().session.active().unwrap().history.entries().len() + h.state().session.active().unwrap().history.redo_labels().count(), 5); +} + #[test] fn new_history_steps_scroll_the_current_state_into_view() { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); diff --git a/crates/ui-egui/src/pen_variants_tests.rs b/crates/ui-egui/src/pen_variants_tests.rs index cee8e37bbd..330f2bc6f3 100644 --- a/crates/ui-egui/src/pen_variants_tests.rs +++ b/crates/ui-egui/src/pen_variants_tests.rs @@ -273,3 +273,39 @@ fn undo_in_a_resumed_draft_stops_at_the_paths_own_anchors() { assert!(app.ui.direct_selection.anchors.is_empty()); assert_eq!(work_path(&app), before, "the path is as it was"); } + +/// #6079 review: Make Selection hides the Work Path. A Pen press on its old endpoint then starts a +/// new Work Path that replaces it, as Photoshop does with no path selected, instead of carrying on +/// the hidden one. A shown Work Path and a selected saved path still carry on. +#[test] +fn the_pen_does_not_carry_on_a_work_path_hidden_by_make_selection() { + let mut app = app(Tool::Pen); + let anchors = |p: &Path| -> Vec> { p.subpaths.iter().map(|s| s.knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect()).collect() }; + click(&mut app, 20.0, 20.0); + click(&mut app, 80.0, 20.0); + crate::vector_ui::pen_commit(&mut app, false); + crate::vector_ui::path_to_selection(&mut app, json!({})).unwrap(); + assert!(!crate::vector_ui::work_path_shown(&app), "Make Selection hid the Work Path"); + click(&mut app, 80.0, 20.0); + click(&mut app, 80.0, 80.0); + crate::vector_ui::pen_commit(&mut app, false); + assert_eq!(anchors(&work_path(&app)), [vec![[80.0, 20.0], [80.0, 80.0]]], "a new Work Path, not the hidden one carried on"); + // The new Work Path is shown and selected: it carries on as before. + click(&mut app, 80.0, 80.0); + click(&mut app, 140.0, 80.0); + crate::vector_ui::pen_commit(&mut app, false); + assert_eq!(anchors(&work_path(&app)), [vec![[80.0, 20.0], [80.0, 80.0], [140.0, 80.0]]]); + // A saved path selected in the Paths panel carries on while the Work Path is hidden. + let open = json!({"subpaths": [{"closed": false, "knots": [{"anchor": [150.0, 150.0]}, {"anchor": [200.0, 150.0]}]}]}); + app.run("path.set", json!({"name": "Saved", "path": open})).unwrap(); + crate::vector_ui::path_to_selection(&mut app, json!({})).unwrap(); + assert!(!crate::vector_ui::work_path_shown(&app)); + app.ui.selected_path = Some("Saved".into()); + click(&mut app, 200.0, 150.0); + click(&mut app, 200.0, 190.0); + crate::vector_ui::pen_commit(&mut app, false); + let st = app.session.active().unwrap(); + let saved = st.doc.paths.iter().find(|p| p.name == "Saved").unwrap(); + assert_eq!(anchors(&saved.path), [vec![[150.0, 150.0], [200.0, 150.0], [200.0, 190.0]]], "the saved path carried on"); + assert_eq!(anchors(&work_path(&app)).len(), 1, "the hidden Work Path untouched"); +} diff --git a/crates/ui-egui/src/prefs/prefs_ui/fields.rs b/crates/ui-egui/src/prefs/prefs_ui/fields.rs index f4141464d0..88e383ce9b 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/fields.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/fields.rs @@ -63,6 +63,10 @@ pub(super) fn humanize(key: &str) -> String { if key == "doNotAppendCopy" { return "Do not append \"copy\" to filename when saving a copy".into(); } + // Plug-ins: Photoshop's wording (#9392). + if key == "generatorEnabled" { + return "Enable Generator".into(); + } if key == "lowLatencyDrawing" { return "Low-Latency Drawing (vsync off, applies at next launch)".into(); } diff --git a/crates/ui-egui/src/prefs/prefs_ui/tests.rs b/crates/ui-egui/src/prefs/prefs_ui/tests.rs index 55e3c6b6e2..84112c95cc 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/tests.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/tests.rs @@ -14,6 +14,24 @@ fn memory_usage_has_a_dedicated_explanation_and_keeps_its_value() { assert_eq!(h.state()["memoryUsageMb"], 50000); } +#[test] +fn the_generator_checkbox_reads_enable_generator() { + // #9392: the reference application's Plug-ins page says "Enable Generator", not "Generator enabled". + use egui_kittest::{Harness, kittest::Queryable}; + let values = serde_json::to_value(photocraft_engine::prefs::PlugIns::default()).unwrap().as_object().unwrap().clone(); + let keys: Vec = values.keys().cloned().collect(); + let mut h = Harness::builder() + .with_size(vec2(680.0, 350.0)) + .build_ui_state(move |ui, values| section_fields(ui, "plugIns", values, &keys, crate::i18n::Lang::EN, None, ""), values); + h.run_steps(2); + assert!(h.query_by_label("Generator enabled").is_none()); + h.get_by_label("Enable Generator").click(); + h.run_steps(2); + assert_eq!(h.state()["generatorEnabled"], true); + let de = crate::i18n::Lang::from_code("de").unwrap(); + assert_eq!(crate::i18n::tr(de, "Enable Generator"), "Generator aktivieren"); +} + #[test] fn graphics_dropdowns_are_named_for_their_settings() { use egui::accesskit::Role; diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index ef587063cb..8fb058524a 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -314,6 +314,11 @@ pub struct UiState { /// Selected row in the Paths panel ("work" or a saved path name). #[serde(default)] pub selected_path: Option, + /// The document and Work Path deselected by making a selection from it with the Pen or Path + /// Selection tool (#6079): the canvas hides that path until a path is selected again or the + /// Work Path changes. + #[serde(default)] + pub hidden_work_path: Option<(u64, photocraft_doc::Path)>, /// Layers panel kind filter ("pixel", "adjustment", "type", "shape", "smart"); empty = all. #[serde(default)] pub layer_filter: Vec, @@ -540,6 +545,7 @@ impl Default for UiState { direct_selection: Default::default(), corner_radius: None, selected_path: None, + hidden_work_path: None, panels: Panels::default(), views: Vec::new(), dialogs: Vec::new(), diff --git a/crates/ui-egui/src/tools/crop_ui.rs b/crates/ui-egui/src/tools/crop_ui.rs index dc852d47c1..e909bd8656 100644 --- a/crates/ui-egui/src/tools/crop_ui.rs +++ b/crates/ui-egui/src/tools/crop_ui.rs @@ -727,10 +727,11 @@ pub fn swap_orientation(app: &mut PhotocraftApp) -> bool { { app.ui.tool_options.crop_ratio = k; } + // The frame turns first: fitting the swapped ratio inside the old frame would shrink it. + keyed(app, swapped(r)); if preset_ratio(app).is_some() { apply_ratio(app); } else { - keyed(app, swapped(r)); crate::crop_mode::sync_frame(app); crate::crop_mode::auto_center(app); } diff --git a/crates/ui-egui/src/tools/crop_ui/tests.rs b/crates/ui-egui/src/tools/crop_ui/tests.rs index ffe9914527..6fba4e57d4 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -1080,6 +1080,17 @@ fn x_swaps_the_frame_orientation_not_the_colours() { app.ui.tool_options.crop_ratio = String::new(); assert!(swap_orientation(&mut app)); assert_eq!(app.ui.tool_options.crop_ratio, "", "no ratio stays none"); + // With a ratio preset X turns the frame too, keeping its size, and X again brings it back. + let near = |a: Option<[f64; 4]>, b: [f64; 4]| a.is_some_and(|a| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9)); + app.ui.tool_options.crop_ratio = "16:9".into(); + app.ui.crop_rect = Some([60.0, 27.5, 140.0, 72.5]); + key(&mut app, Key::X, NONE); + assert_eq!(app.ui.tool_options.crop_ratio, "9:16"); + assert!(near(app.ui.crop_rect, [77.5, 10.0, 122.5, 90.0]), "45 x 80, not shrunk: {:?}", app.ui.crop_rect); + key(&mut app, Key::X, NONE); + assert_eq!(app.ui.tool_options.crop_ratio, "16:9"); + assert!(near(app.ui.crop_rect, [60.0, 27.5, 140.0, 72.5]), "and back: {:?}", app.ui.crop_rect); + app.ui.tool_options.crop_ratio = String::new(); assert_eq!(swapped_ratio("1.414:1", 1.0, 1.0).as_deref(), Some("1:1.414"), "decimals survive the swap (#6487)"); assert_eq!(swapped_ratio("0:5", 1.0, 1.0), None); assert_eq!(swapped_ratio("nonsense", 1.0, 1.0), None); diff --git a/crates/ui-egui/src/tools/direct_select.rs b/crates/ui-egui/src/tools/direct_select.rs index 6f8ede6a1b..c8f441ac33 100644 --- a/crates/ui-egui/src/tools/direct_select.rs +++ b/crates/ui-egui/src/tools/direct_select.rs @@ -167,7 +167,7 @@ pub fn shows(app: &PhotocraftApp, mods: Modifiers) -> bool { /// the work path. The Pen's Auto Add/Delete edits these (`anchor_tools::pen_auto`). pub(crate) fn active_paths(app: &PhotocraftApp) -> Vec<(PathRef, Path)> { let layer = crate::vector_ui::targeted_vector_mask(app).or_else(|| crate::vector_ui::active_shape_path(app)); - let work = app.session.active().and_then(|st| st.doc.work_path.clone()); + let work = app.session.active().and_then(|st| st.doc.work_path.clone()).filter(|_| crate::vector_ui::work_path_shown(app)); layer.map(|(id, p)| (PathRef::Layer(id), p)).into_iter().chain(work.map(|p| (PathRef::Work, p))).collect() } diff --git a/crates/ui-egui/src/tools/move_mods.rs b/crates/ui-egui/src/tools/move_mods.rs index ffd302add0..84bf2c085f 100644 --- a/crates/ui-egui/src/tools/move_mods.rs +++ b/crates/ui-egui/src/tools/move_mods.rs @@ -90,7 +90,9 @@ fn duplicate(app: &mut PhotocraftApp) -> Option { let before = past_len(app)?; // In place: the copy follows the pointer from the original, artboards too (#1531). match app.run("layer.duplicate", json!({"inPlace": true})) { - Ok(_) => Some(before), + // The state just before the copy's: in non-linear history the copy's step also moved + // the later states in front of it (`History::record`), and those must stay. + Ok(_) => Some(past_len(app).and_then(|n| n.checked_sub(1)).unwrap_or(before).max(before)), Err(e) => { app.ui.status = e; None @@ -186,7 +188,8 @@ pub fn finish(app: &mut PhotocraftApp) { /// ⌥-drag's copy is taken back. pub fn abandon(app: &mut PhotocraftApp) { if std::mem::take(&mut app.move_mods).dup_from.is_some() { - app.session.undo(); + // Exactly, also in non-linear history (where an undo would land on a later state). + app.session.rollback(); } } diff --git a/crates/ui-egui/src/tools/move_mods/tests.rs b/crates/ui-egui/src/tools/move_mods/tests.rs index 27118357fe..bb8b39f0d8 100644 --- a/crates/ui-egui/src/tools/move_mods/tests.rs +++ b/crates/ui-egui/src/tools/move_mods/tests.rs @@ -104,6 +104,22 @@ fn alt_drag_duplicates_and_moves_the_copy_in_one_step() { assert_eq!(bounds(&app, st.active_layer.unwrap()), photocraft_geom::Rect::new(22, 8, 38, 24)); } +/// Allow Non-Linear History: the copy's step moves the later states in front of it, and +/// folding the copy and the move into one step must keep them. +#[test] +fn alt_drag_keeps_the_later_states_in_non_linear_history() { + let mut app = app_with_layer(); + app.session.execute("history.options", json!({"allowNonLinearHistory": true})).unwrap(); + let id = app.session.active().unwrap().active_layer.unwrap(); + app.session.execute("layer.setProps", json!({"layer": id.0, "opacity": 0.5})).unwrap(); + assert!(app.session.undo()); + let mut expected = app.session.active().unwrap().history.entries(); + expected.extend(app.session.active().unwrap().history.redo_labels().map(str::to_string)); + expected.push(DUPLICATE_MOVE_LABEL.into()); + drag(&mut app, &[[16.0, 16.0], [20.0, 16.0], [26.0, 21.0]], egui::Modifiers::ALT); + assert_eq!(app.session.active().unwrap().history.entries(), expected, "the later state stays"); +} + #[test] fn alt_drag_duplicates_every_selected_layer() { let mut app = app_with_layer(); diff --git a/crates/ui-egui/src/tools/transform_tool/begin.rs b/crates/ui-egui/src/tools/transform_tool/begin.rs index e4ec2b8691..b3bba8d8f1 100644 --- a/crates/ui-egui/src/tools/transform_tool/begin.rs +++ b/crates/ui-egui/src/tools/transform_tool/begin.rs @@ -208,17 +208,18 @@ pub(super) fn take_back_made(app: &mut PhotocraftApp, document: DocId, layer: La let Some(index) = app.session.documents().iter().position(|st| { st.doc.id == document && st.doc.layer(layer).is_some() - && st.history.past_len().checked_sub(1).and_then(|i| st.history.state(i)).is_some_and(|before| before.layer(layer).is_none()) + && st + .history + .recorded_before() + .or_else(|| st.history.past_len().checked_sub(1).and_then(|i| st.history.state(i))) + .is_some_and(|before| before.layer(layer).is_none()) }) else { return; }; let active = app.session.active_index(); app.session.set_active(index); - if app.session.undo() - && let Some(st) = app.session.active_mut() - { - st.history.clear_redo(); - } + // Exactly, also in non-linear history (where an undo would land on a later state). + app.session.rollback(); if let Some(active) = active { app.session.set_active(active); } diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index 60ee51c676..eab436fd07 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -36,8 +36,12 @@ use overlay::{MAX_CURVE_PIECES, bezier}; #[cfg(test)] pub(crate) use overlay::{curve_points, path_lines, selected_saved_path, shows_anchors}; pub(crate) use overlay::{draw_anchor, draw_handle, draw_outline}; +#[cfg(test)] +use path_ops::target_path; pub use path_ops::{active_path_name, path_selection_finish, path_to_selection, pick_shape_at, selected_document_path, takes_path_mask, with_active_path}; -pub(crate) use path_ops::{active_shape_path, delete_key_deletes_work_path, free_transform_path, targeted_vector_mask}; +pub(crate) use path_ops::{ + active_shape_path, delete_key_deletes_work_path, deselect_loaded_path, free_transform_path, targeted_vector_mask, work_path_shown, work_path_shown_in, +}; #[cfg(test)] use paths_panel::path_context_actions; pub use paths_panel::{PathEntry, PathRow, path_rows, paths_footer, paths_panel}; diff --git a/crates/ui-egui/src/tools/vector_ui/overlay.rs b/crates/ui-egui/src/tools/vector_ui/overlay.rs index b0bdbe3221..6f394c6828 100644 --- a/crates/ui-egui/src/tools/vector_ui/overlay.rs +++ b/crates/ui-egui/src/tools/vector_ui/overlay.rs @@ -147,7 +147,7 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform // a path tool is active, as in Photoshop: a Work Path left unselected (one a PSD arrived // with, or after Ctrl-click Load as Selection) would otherwise cover the marching ants (#6551). let anchors = shows_anchors(tool); - if let Some(wp) = &doc.work_path + if let Some(wp) = doc.work_path.as_ref().filter(|_| work_path_shown_in(app, doc)) && (anchors || app.ui.selected_path.as_deref() == Some("work")) { draw_path(wp, anchors); diff --git a/crates/ui-egui/src/tools/vector_ui/path_ops.rs b/crates/ui-egui/src/tools/vector_ui/path_ops.rs index db9847aeca..30feea8dc2 100644 --- a/crates/ui-egui/src/tools/vector_ui/path_ops.rs +++ b/crates/ui-egui/src/tools/vector_ui/path_ops.rs @@ -3,7 +3,8 @@ use super::pen::pen_commit; use super::*; /// The path that path commands act on without a name: the one selected in the Paths panel, else -/// the work path, else the selected layer's shape path or vector mask; `None` when there's none. +/// the work path while it is shown (not after Make Selection hid it, #6079), else the selected +/// layer's shape path or vector mask; `None` when there's none. pub fn active_path_name(app: &PhotocraftApp) -> Option { let st = app.session.active()?; let rows = path_rows(&st.doc, st.active_layer); @@ -13,7 +14,8 @@ pub fn active_path_name(app: &PhotocraftApp) -> Option { PathRow::Saved => r.name.clone(), }; let selected = app.ui.selected_path.as_deref().filter(|s| rows.iter().any(|r| key(r) == *s)).map(str::to_string); - selected.or_else(|| rows.iter().find(|r| r.kind == PathRow::Work).map(key)).or_else(|| rows.iter().find(|r| r.kind == PathRow::Layer).map(key)) + let work = rows.iter().find(|r| r.kind == PathRow::Work).filter(|_| work_path_shown(app)).map(key); + selected.or(work).or_else(|| rows.iter().find(|r| r.kind == PathRow::Layer).map(key)) } /// The commands that create a fill or adjustment layer and take a `"path"` as its vector mask. @@ -65,14 +67,51 @@ pub fn path_to_selection(app: &mut PhotocraftApp, params: Value) -> Result