diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index 8bf7e37874..d128aba2e4 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -9,8 +9,10 @@ //! without a password; `null` until it is checked or cleared, when it follows the pages picked: on //! for All, off for a range; picking the other of All and Range sets it back to `null`; `linksOn` //! is the effective flag, `__linkable` is false for a file `linkPages` does not take, such as one -//! over its size limit), `cropTo`, `textAs` (`text` | `outlines`; it starts from the `pdfTextAs` -//! preference for a PDF (`__pdf`; Editable for a `.ai` or `.ait` file), or from the `textAs` a place +//! over its size limit), `cropTo` (when opening, it starts on `trim` for a file with editing data, +//! else `crop`; OK names the box only when it differs from the box it started on, so a file with +//! the starting box opens as it does without the dialog), `textAs` (`text` | `outlines`; it starts +//! from the `pdfTextAs` preference for a PDF (`__pdf`; Editable for a `.ai` or `.ait` file), or from the `textAs` a place //! gives (`__textAsGiven`), and OK keeps a PDF's choice there; a linked place shows the file's own //! text, so placing linked leaves it as it is, and Outlines clears Import PDF Pages as Links) and //! `password`. `__locked` is set until the password is accepted: OK then unlocks the file, and with @@ -146,6 +148,9 @@ struct State { pages: Vec, link_pages: Option, crop: CropTo, + /// The box Crop To started on ([`opening_crop`] when opening). OK names the box only when + /// it differs from this box. + start_crop: CropTo, text_as: TextAs, pdf: bool, password: String, @@ -212,10 +217,17 @@ impl State { /// Reads the file with the typed password. `Ok(true)` when the file has pages to pick; `Ok(false)` when OK /// goes straight on to open or place it. fn unlock(&mut self) -> Result { - let info = vectorcraft_pdf::info(&self.file.bytes, self.password().as_deref()).map_err(|e| e.to_string())?; + let opened = vectorcraft_pdf::open_pdf(&self.file.bytes, self.password().as_deref()).map_err(|e| e.to_string())?; self.locked = false; - self.page_count = info.pages.len(); - Ok(info.pages.len() > 1 && !self.password_only) + self.page_count = opened.page_count(); + if self.place.is_none() { + let start_crop = opening_crop(Some(&opened)); + if self.crop == self.start_crop { + self.crop = start_crop; + } + self.start_crop = start_crop; + } + Ok(self.page_count > 1 && !self.password_only) } /// The Text choice the preview renders with (a linked place: as editable text). @@ -506,6 +518,12 @@ fn pick_pages(ui: &mut egui::Ui, s: &mut State, count: usize, current: usize) { } } +/// The box Crop To starts on when opening the file `opened`: Trim Box for a file with editing +/// data, which opens on its trim boxes when no box is named, else Crop Box. +fn opening_crop(opened: Option<&vectorcraft_pdf::Opened>) -> CropTo { + if opened.is_some_and(vectorcraft_pdf::Opened::has_illustrator_data) { CropTo::Trim } else { CropTo::default() } +} + /// Open `bytes` (or place them with the `file.place` params `place`) through the dialog when the /// file is a PDF (or `.ai`/`.ait`) with several pages or a password; false when there's nothing to /// ask (the caller goes on), including, when opening, a file that carries its VectorCraft document @@ -528,6 +546,7 @@ fn offer_with(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option 1 => (count, false), Err(PdfError::NeedsPassword) => (0, true), @@ -544,7 +563,8 @@ fn offer_with(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option Result<(), String> { } crate::place::run_for_user(app, &p).map(|_| ()) } else { - let mut p = json!({"cropTo": s.crop.id(), "password": s.password(), "textAs": s.text_as.id()}); + let mut p = json!({"password": s.password(), "textAs": s.text_as.id()}); + // A box left as it starts ([`opening_crop`]) is not named: a file with editing data then + // opens on its trim boxes, from its editing layers where it can, as without the dialog. + if s.crop != s.start_crop { + p["cropTo"] = json!(s.crop.id()); + } // An empty range under Range opens every page. if !s.all_pages && !s.pages.is_empty() { p["pages"] = json!(format_pages(&s.pages)); diff --git a/crates/ui-egui/src/dialogs/tests_import_pdf.rs b/crates/ui-egui/src/dialogs/tests_import_pdf.rs index c9383d0f44..1d1d434f2d 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -409,6 +409,52 @@ fn opening_an_ai_with_editing_data_reads_it_without_asking() { assert!(!is_open(&app)); } +/// A PDF with editing data opens on its trim boxes when no box is named. Opening one, Crop To starts +/// on Trim Box, also once a password unlocks it, and OK with it left so names no box: the pages open +/// on their trim boxes, as without the dialog. Picking Crop Box opens the crop boxes, and a place +/// starts on Crop Box. +#[test] +fn opening_a_pdf_with_editing_data_keeps_its_trim_boxes_unless_a_box_is_picked() { + let trimmed = |password: Option<&str>| { + let mut first = PdfPage::new(100.0, 100.0, "0 g 0 0 10 10 re f"); + first.entries = "/PieceInfo << /Illustrator << /Private << /AIPrivateData1 7 /NumBlock 1 >> >> >>".into(); + first.trim = Some([10.0, 10.0, 90.0, 90.0]); + let mut second = PdfPage::new(200.0, 100.0, "1 0 0 rg 0 0 30 30 re f"); + second.trim = Some([10.0, 10.0, 190.0, 90.0]); + pdf(&[first, second], password) + }; + let mut app = app(); + assert!(import_pdf::offer(&mut app, "two.pdf", &trimmed(None), None, None)); + assert_eq!(field(&app, "cropTo"), "trim"); + confirm(&mut app).unwrap(); + assert_eq!(artboard_widths(&app), [80.0, 180.0], "the trim boxes"); + + let mut app = self::app(); + assert!(import_pdf::offer(&mut app, "two.pdf", &trimmed(None), None, None)); + set(&mut app, "cropTo", json!("crop")); + confirm(&mut app).unwrap(); + assert_eq!(artboard_widths(&app), [100.0, 200.0], "an explicit crop box"); + + let mut app = self::app(); + assert!(import_pdf::offer(&mut app, "locked.pdf", &trimmed(Some("pw")), None, None)); + set(&mut app, "password", json!("pw")); + confirm(&mut app).unwrap(); + assert_eq!((field(&app, "__locked"), field(&app, "cropTo")), (json!(false), json!("trim")), "unlocked"); + + let mut app = self::app(); + assert!(import_pdf::offer(&mut app, "locked.pdf", &trimmed(Some("pw")), None, None)); + set(&mut app, "cropTo", json!("media")); + set(&mut app, "password", json!("pw")); + confirm(&mut app).unwrap(); + set(&mut app, "cropTo", json!("crop")); + confirm(&mut app).unwrap(); + assert_eq!(artboard_widths(&app), [100.0, 200.0]); + + let mut app = self::app(); + assert!(import_pdf::offer(&mut app, "two.pdf", &trimmed(None), None, Some(&json!({})))); + assert_eq!(field(&app, "cropTo"), "crop", "a place"); +} + /// A `.ai` whose editing data can't be read asks nothing either, and every page opens from the PDF /// part. Asking at that point is #6134. #[test]