From a73dd2ad29c0b2ee5363441faf71861dbc95abbf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 09:39:44 -0800 Subject: [PATCH] Import PDF: a PDF with editing data opens on its trim boxes unless a box is picked Opening a PDF with editing data without naming a box gives its trim boxes, but the Import PDF dialog always sent cropTo, so OK with Crop To left as it started opened the crop boxes instead. Opening such a file, Crop To now starts on Trim Box, also once a password unlocks it, and OK names the box only when one was picked. Picking Crop Box still opens the crop boxes, and a place still starts on Crop Box. --- crates/ui-egui/src/dialogs/import_pdf.rs | 36 +++++++++++++++--- .../ui-egui/src/dialogs/tests_import_pdf.rs | 37 +++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index 8bf7e37874..fd141eb74b 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -9,7 +9,9 @@ //! 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` +//! 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 was picked, 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 @@ -146,6 +148,9 @@ struct State { pages: Vec, link_pages: Option, crop: CropTo, + /// Whether Crop To was picked. Opening, a box left as it starts ([`opening_crop`]) is not + /// named, so a file with editing data opens on its trim boxes, as it opens without the dialog. + crop_chosen: bool, text_as: TextAs, pdf: bool, password: String, @@ -212,10 +217,13 @@ 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.crop_chosen && self.place.is_none() { + self.crop = opening_crop(Some(&opened)); + } + Ok(self.page_count > 1 && !self.password_only) } /// The Text choice the preview renders with (a linked place: as editable text). @@ -270,6 +278,7 @@ impl State { let text = text()?; let i = CropTo::IDS.iter().position(|id| *id == text).ok_or("cropTo is not a crop box")?; self.crop = CropTo::ALL.get(i).copied().unwrap_or_default(); + self.crop_chosen = true; } "textAs" => { let id = text()?; @@ -459,7 +468,9 @@ fn pages_body(ui: &mut egui::Ui, s: &mut State, preview: Option<&(Revision, Arc< }); Stack::new().show(ui, |ui, _| { let crops: Vec> = CropTo::ALL.iter().zip(CropTo::LABELS).map(|(crop, label)| Entry::new(*crop, *label)).collect(); + let before = s.crop; Field::new(tl!("Crop To")).show(ui, |ui, _| Select::new(tl!("Crop To"), Bind::bound(&mut s.crop), &crops).show(ui)); + s.crop_chosen |= s.crop != before; let linked = s.places_linked(); let texts = [Entry::new(TextAs::Text, tl!("Editable")), Entry::new(TextAs::Outlines, tl!("Outlines"))]; Field::new(tl!("Text")).show(ui, |ui, _| Select::new(tl!("Text"), Bind::bound(&mut s.text_as), &texts).enabled(!linked).show(ui)); @@ -506,6 +517,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 +545,7 @@ fn offer_with(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option 1 => (count, false), Err(PdfError::NeedsPassword) => (0, true), @@ -544,7 +562,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_chosen { + 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..8b0bedd950 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -409,6 +409,43 @@ 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, "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]