PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Integration: merge main, resolve conflicts (#6658)
David Mann07911e77f48f
diff --cc crates/engine/src/jobs.rsindex 02f3c2c26,4c3d63297..b96293163--- a/crates/engine/src/jobs.rs+++ b/crates/engine/src/jobs.rs@@@ -542,10 -536,19 +547,19 @@@ impl Sessione => EngineError::Other(e.to_string()),})?;ctx.progress(0.95, "Preparing layers");- warm_layer_bounds(&imported.document, || ctx.check())?;+ warm_layer_bounds(&imported.0.document, || ctx.check())?;+ // Type layers need the shared text engine, whose first use waits for the+ // installed-font scan started at launch (minutes on some machines). Wait here,+ // so the shell's missing-font check after the open finds it ready instead of+ // freezing the window (#7332).- if crate::type_extra_cmds::has_type_layers(&imported.document) {++ if crate::type_extra_cmds::has_type_layers(&imported.0.document) {+ ctx.progress(0.97, "Loading fonts");+ let _ = photocraft_text::shared();+ ctx.check()?;+ }Ok(imported)},- move |s, mut r: photocraft_io::ImportResult| {+ move |s, (mut r, damaged): (photocraft_io::ImportResult, bool)| {// File Handling's ACEScg option, before the colour prompt looks at the profile.crate::file_cmds::tag_exr_colorspace(s, &name_a, &mut r.document);// Ask When Opening: held back until the prompt is answered (`take_held_open`). Adiff --cc crates/io/src/lib.rsindex 37227fff8,d71a5d716..809b19e7a--- a/crates/io/src/lib.rs+++ b/crates/io/src/lib.rs@@@ -302,54 -318,41 +318,60 @@@ pub fn import_with_svg_group_depth_and_max_group_depth: usize,ctl: &photocraft_raster::Interrupt,) -> Result<ImportResult, IoError> {+ import_for_open_with(name, bytes, max_group_depth, ctl).map(|(result, _)| result)+}++/// The ordinary import result plus whether PSD/PSB decoding encountered damaged data.+/// Interactive callers can ask whether to continue before adding the document. This is+/// information, not a save policy: accepting it opens an ordinary, writable document.+/// Non-interactive callers keep using [`import`], which still returns readable content+/// and warnings without requiring confirmation.+///+/// The flag is independent of the wording of `warnings`. An ordinary compatibility+/// warning does not set it. No file is decoded a second time to obtain this information.+pub fn import_for_open(name: &str, bytes: &[u8], max_group_depth: usize) -> Result<(ImportResult, bool), IoError> {+ import_for_open_with(name, bytes, max_group_depth, &photocraft_raster::Interrupt::NONE)+}++/// [`import_for_open`] with cancellation and progress, shared with [`import_with`].+pub fn import_for_open_with(name: &str, bytes: &[u8], max_group_depth: usize, ctl: &photocraft_raster::Interrupt) -> Result<(ImportResult, bool), IoError> {ctl.check().map_err(|_| IoError::Cancelled)?;- let r = import_stages(name, bytes, ctl, max_group_depth)?;+ let mut damaged = false;+ let result = import_stages(name, bytes, ctl, max_group_depth, &mut damaged)?;ctl.check().map_err(|_| IoError::Cancelled)?;ctl.progress(1.0);- Ok(r)+ Ok((result, damaged))}-fn import_psd(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt) -> Result<ImportResult, IoError> {++fn import_psd(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, damaged: &mut bool) -> Result<ImportResult, IoError> {+ // A merged image the import reads is decoded with the parse: a ZIP one is inflated once.+ let (file, merged) = PsdFile::from_bytes_and_merged_recovering(+ bytes,+ |f| psd_import::check_group_depth(f).is_ok() && psd_import::merged_needed(f),+ psd_import::can_recover_layers,+ )?;+ // Nesting past the document model's cap could never be saved; reject the file with the+ // actionable limit.+ psd_import::check_group_depth(&file)?;+ ctl.check().map_err(|_| IoError::Cancelled)?;+ ctl.progress(0.05);- let (mut document, warnings) = psd_import::psd_to_document_decoded(&file, ctl, merged).ok_or(IoError::Cancelled)?;++ let (mut document, warnings, psd_damaged) = psd_import::psd_to_document_decoded_integrity(&file, ctl, merged).ok_or(IoError::Cancelled)?;++ *damaged = psd_damaged;+ document.name = name.to_string();+ text_import::prepare(&mut document);+ Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false })+ }+-fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, max_group_depth: usize) -> Result<ImportResult, IoError> {+fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, max_group_depth: usize, damaged: &mut bool) -> Result<ImportResult, IoError> {// A declared native extension must reach its loader so malformed bundles retain format errors.if has_extension(name, photocraft_format::EXTENSION) || photocraft_format::is_pcraft(bytes) {let document = photocraft_format::load_from_bytes(bytes)?;return Ok(ImportResult { warnings: open_warnings(&document), document, source_read_only: false, preview_only: false });}+ // Magic first: an Affinity file renamed .psd is still Affinity (#5258 corpus+ // preview_never_saves_back_to_its_native_or_renamed_source).if is_psd(bytes) {- // A merged image the import reads is decoded with the parse: a ZIP one is inflated once.- let (file, merged) = PsdFile::from_bytes_and_merged_recovering(- bytes,- |f| psd_import::check_group_depth(f).is_ok() && psd_import::merged_needed(f),- psd_import::can_recover_layers,- )?;- // Nesting past the document model's cap could never be saved; reject the file with the- // actionable limit.- psd_import::check_group_depth(&file)?;- ctl.check().map_err(|_| IoError::Cancelled)?;- ctl.progress(0.05);- let (mut document, warnings, psd_damaged) = psd_import::psd_to_document_decoded_integrity(&file, ctl, merged).ok_or(IoError::Cancelled)?;- *damaged = psd_damaged;- document.name = name.to_string();- text_import::prepare(&mut document);- return Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false });- return import_psd(name, bytes, ctl);++ return import_psd(name, bytes, ctl, damaged);}if affinity::is_affinity(bytes) || affinity::has_extension(name) {return affinity::import(name, bytes);@@@ -379,6 -382,11 +401,11 @@@if is_svg_input(name, bytes) {return svg::import_svg_with_group_depth(name, bytes, max_group_depth);}+ // A `.psd`/`.psb`/`.psdt` with no raster signature keeps the PSD error instead of+ // `codec: unrecognized image format` (#5258). Magic already returned above.+ if is_psd_input(name, bytes) {- return import_psd(name, bytes, ctl);++ return import_psd(name, bytes, ctl, damaged);+ }flat::import_flat(name, bytes)}@@@ -604,40 -616,7 +635,40 @@@ pub fn merged_composite(file: &PsdFile#[cfg(test)]mod tests {- use super::{import, import_for_open, is_svg_input};- use super::{import, is_psd_input, is_svg_input};++ use super::{import, import_for_open, is_psd_input, is_svg_input};+ use photocraft_psd::{Compression, Version, testgen};++ #[test]+ fn valid_psd_can_still_be_edited() {+ let bytes = testgen::small(Version::Psd, Compression::Raw).to_bytes().unwrap();+ let imported = import("valid.psd", &bytes).unwrap();+ assert!(!imported.source_read_only);+ }++ #[test]+ fn unreadable_psd_channel_keeps_best_effort_import_and_reports_damage() {+ let mut file = testgen::small(Version::Psd, Compression::Raw);+ let channel = file.layers_mut()[0].channels.iter_mut().find(|c| c.id == 0).unwrap();+ channel.data.clear();+ let bytes = file.to_bytes().unwrap();+ let imported = import("damaged.psd", &bytes).unwrap();+ assert!(!imported.source_read_only);+ assert!(imported.warnings.iter().any(|w| w.contains("could not be decoded")));+ let (opened, damaged) = import_for_open("damaged.psd", &bytes, photocraft_doc::MAX_GROUP_DEPTH).unwrap();+ assert!(damaged);+ assert!(!opened.source_read_only);+ assert!(!opened.preview_only);+ assert_eq!(opened.warnings, imported.warnings);+ assert_eq!(opened.document.layers.len(), imported.document.layers.len());+ }++ #[test]+ fn valid_psd_does_not_request_damage_confirmation() {+ let bytes = testgen::small(Version::Psd, Compression::Raw).to_bytes().unwrap();+ let (result, damaged) = import_for_open("valid.psd", &bytes, photocraft_doc::MAX_GROUP_DEPTH).unwrap();+ assert!(!damaged);+ assert!(!result.source_read_only);+ }#[test]fn svg_extension_does_not_override_another_format_signature() {