From a1c46996c213a6198fb338f9df65bf5a44067e0e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 20:46:59 -0800 Subject: [PATCH 1/3] Place: file.place without the dialog applies the PDF Text preference to an embedded PDF file.place run as a command (MCP, scripts in the app) applied the pdfTextAs preference only to places the user started, so an embedded PDF placed with showImportOptions: false and no textAs kept its text as point type even with PDF Text set to Outlines. docs/mcp.md says such a place takes the preference. The command path now applies it the same way: a textAs given with the place, a linked place and a .ai file keep their own text. --- crates/ui-egui/src/dialogs/import_pdf.rs | 4 +-- .../ui-egui/src/dialogs/tests_import_pdf.rs | 34 +++++++++++++++++++ crates/ui-egui/src/place.rs | 6 ++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index b14cba49cd..c28f5694f6 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -264,8 +264,8 @@ pub(crate) fn with_preferred_text_as(app: &VectorcraftApp, name: &str, bytes: &[ p } -/// The `file.place` params `p` of a PDF the user places embedded (Link off, from bytes or with a -/// password) get the `pdfTextAs` preference as their `textAs` when it is Outlines and they have no +/// The `file.place` params `p` of a PDF placed embedded (Link off, from bytes or with a password), +/// by the user or as a command, get the `pdfTextAs` preference as their `textAs` when it is Outlines and they have no /// `textAs`. A linked page shows the file's own text, and a `.ai` or `.ait` file is placed as it /// says. pub(crate) fn place_with_preferred_text_as(app: &VectorcraftApp, p: &mut Value) { diff --git a/crates/ui-egui/src/dialogs/tests_import_pdf.rs b/crates/ui-egui/src/dialogs/tests_import_pdf.rs index 06bdf5e1b7..a1e9a350a4 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -491,3 +491,37 @@ fn the_text_choice_applies_to_embedded_places_only() { assert!(!matches!(placed.kind, NodeKind::PlacedDocument(_)), "embedded: {:?}", placed.kind); assert_eq!(type_objects(&app), before, "placed as outlines"); } + +/// `file.place` run as a command (MCP, scripts) without the dialog takes the PDF Text preference for +/// an embedded PDF with no `textAs`, as a place by the user does; a `textAs` given and a linked +/// place keep their own text. +#[test] +fn file_place_without_the_dialog_takes_the_pdf_text_preference() { + let mut app = app(); + set_pdf_text(&mut app, "outlines"); + let dir = vectorcraft_testkit::TempDir::new("import-pdf-text-at-once"); + let path = dir.join("two.pdf").to_string_lossy().into_owned(); + std::fs::write(&path, text_pages(2)).unwrap(); + let embedded = json!({"path": path, "link": false, "showImportOptions": false}); + crate::place::run(&mut app, &embedded).unwrap(); + assert!(app.ui.dialog.is_none(), "no dialog"); + assert_eq!(type_objects(&app), 0, "the preference: outlines"); + + let one = json!({"name": "one.pdf", "dataBase64": vectorcraft_format::base64_encode(&text_pages(1))}); + crate::place::run(&mut app, &one).unwrap(); + assert_eq!(type_objects(&app), 0, "one page from bytes: outlines"); + + let mut given = embedded.clone(); + given["textAs"] = json!("text"); + crate::place::run(&mut app, &given).unwrap(); + assert_eq!(type_objects(&app), 1, "a textAs given is kept"); + + crate::place::run(&mut app, &json!({"path": path, "showImportOptions": false})).unwrap(); + let st = app.session.active().unwrap(); + let placed = st.doc.node(st.selection.objects[0]).unwrap(); + assert!(matches!(placed.kind, NodeKind::PlacedDocument(_)), "linked: {:?}", placed.kind); + + set_pdf_text(&mut app, "text"); + crate::place::run(&mut app, &embedded).unwrap(); + assert_eq!(type_objects(&app), 2, "Editable: page 1's type object"); +} diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rs index a36d85f8a2..4e8107cc12 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -262,9 +262,9 @@ fn place_file_in(app: &mut VectorcraftApp, p: &Value, read: Read) -> Result { let r = crate::placing::start(app, &p, dropped); From 35ee5de2d480bb4909a7705ab50a188deb183e99 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 03:24:32 -0800 Subject: [PATCH 2/3] Place: an empty password counts as none, as file.place reads it file.place reads an empty password as no password, but the PDF Text preference treated one as given: a PDF placed from a path with Link on and password "" got textAs outlines and was embedded instead of linked. With Show Import Options off, an encrypted PDF placed with an empty password was not asked for its password and the place failed. Both checks now use the same test as the engine and the dialog's own password field. Tests cover the linked place with an empty password and the password prompt for an encrypted PDF. --- crates/ui-egui/src/dialogs/import_pdf.rs | 13 +++++++---- .../ui-egui/src/dialogs/tests_import_pdf.rs | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index c28f5694f6..fe4603ccf0 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -190,11 +190,11 @@ pub fn offer_place(app: &mut VectorcraftApp, p: &Value) -> bool { } /// [`offer_place`] when Show Import Options is off: asks (true) only for the password of an -/// encrypted PDF given no `password`, which nothing else opens, and places its first page (or +/// encrypted PDF given no `password` (or an empty one), which nothing else opens, and places its first page (or /// `page`) and crop box once it is unlocked; any other PDF is placed that way without asking. pub fn offer_place_password(app: &mut VectorcraftApp, p: &Value) -> bool { // A `page` that isn't a page number is left for `file.place` to refuse. - if p.get("password").is_some() || p.get("page").is_some_and(|v| v.as_u64().is_none_or(|n| n == 0)) { + if password_given(p) || p.get("page").is_some_and(|v| v.as_u64().is_none_or(|n| n == 0)) { return false; } let Some((name, bytes, path)) = place_source(app, p, |ext| matches!(ext, "pdf" | "ai" | "ait")) else { return false }; @@ -233,6 +233,11 @@ fn password(d: &Dialog) -> Option { Some(d.str("password")).filter(|p| !p.is_empty()) } +/// Do the `file.place` params `p` give a password: `file.place` reads an empty one as none. +fn password_given(p: &Value) -> bool { + p.get("password").is_some_and(|v| !v.is_null() && v.as_str() != Some("")) +} + /// The labels of the Text choices, in the order of [`TextAs::ALL`]. const TEXT_AS_LABELS: [&str; 2] = ["Editable", "Outlines"]; @@ -264,7 +269,7 @@ pub(crate) fn with_preferred_text_as(app: &VectorcraftApp, name: &str, bytes: &[ p } -/// The `file.place` params `p` of a PDF placed embedded (Link off, from bytes or with a password), +/// The `file.place` params `p` of a PDF placed embedded (Link off, from bytes or with a non-empty password), /// by the user or as a command, get the `pdfTextAs` preference as their `textAs` when it is Outlines and they have no /// `textAs`. A linked page shows the file's own text, and a `.ai` or `.ait` file is placed as it /// says. @@ -272,7 +277,7 @@ pub(crate) fn place_with_preferred_text_as(app: &VectorcraftApp, p: &mut Value) let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); let path = p.get("path").and_then(Value::as_str); let name = path.or_else(|| p.get("name").and_then(Value::as_str)).unwrap_or_default(); - let linked = path.is_some() && p.get("link").and_then(Value::as_bool) != Some(false) && !given("password"); + let linked = path.is_some() && p.get("link").and_then(Value::as_bool) != Some(false) && !password_given(p); if given("textAs") || linked || preferred_text_as(app) == TextAs::Text || fileio::extension(name) != "pdf" { return; } diff --git a/crates/ui-egui/src/dialogs/tests_import_pdf.rs b/crates/ui-egui/src/dialogs/tests_import_pdf.rs index a1e9a350a4..6e234bbd0c 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -521,7 +521,30 @@ fn file_place_without_the_dialog_takes_the_pdf_text_preference() { let placed = st.doc.node(st.selection.objects[0]).unwrap(); assert!(matches!(placed.kind, NodeKind::PlacedDocument(_)), "linked: {:?}", placed.kind); + // `file.place` reads an empty password as none, so the page stays linked. + crate::place::run(&mut app, &json!({"path": path, "password": "", "showImportOptions": false})).unwrap(); + let st = app.session.active().unwrap(); + let placed = st.doc.node(st.selection.objects[0]).unwrap(); + assert!(matches!(placed.kind, NodeKind::PlacedDocument(_)), "an empty password: linked: {:?}", placed.kind); + set_pdf_text(&mut app, "text"); crate::place::run(&mut app, &embedded).unwrap(); assert_eq!(type_objects(&app), 2, "Editable: page 1's type object"); } + +/// With Show Import Options off, an encrypted PDF placed with an empty `password` asks for its +/// password, as one placed without a `password` does. +#[test] +fn an_empty_password_asks_for_the_password_of_an_encrypted_pdf() { + let mut app = app(); + let bytes = pdf(&[PdfPage::new(120.0, 60.0, "1 0 0 rg 0 0 20 20 re f")], Some("pw")); + place(&mut app, &bytes, json!({"password": "", "showImportOptions": false})).unwrap(); + assert_eq!((field(&app, "__locked"), field(&app, "__passwordOnly")), (json!(true), json!(true))); + frame(&mut app); + set(&mut app, "password", json!("pw")); + confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none()); + let st = app.session.active().unwrap(); + let b = st.doc.node(st.selection.objects[0]).and_then(|n| n.geometric_bounds()).unwrap(); + assert!((b.width() - 120.0).abs() < 0.01, "placed once unlocked: {}", b.width()); +} From cfb9f825683bfcd832a3c168cbf7d049a40685c2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 04:14:26 -0800 Subject: [PATCH 3/3] Place: PDF bytes without a .pdf name take the PDF Text preference The PDF Text preference decided whether a place was a PDF by the extension of its path or name, so a file.place with dataBase64 and no name, or a name with another extension, kept its type editable with Outlines set. file.place reads such bytes as a PDF by their content. Bytes now count as a PDF when they start with %PDF, as file.place detects them; only the first bytes are decoded for the check. A .ai or .ait name is still placed as the file says, and a path is still checked by its extension. A test covers unnamed bytes, another extension and a .ai name. --- crates/ui-egui/src/dialogs/import_pdf.rs | 21 ++++++++++++++++--- .../ui-egui/src/dialogs/tests_import_pdf.rs | 19 +++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index fe4603ccf0..5f97ea75ec 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -271,14 +271,14 @@ pub(crate) fn with_preferred_text_as(app: &VectorcraftApp, name: &str, bytes: &[ /// The `file.place` params `p` of a PDF placed embedded (Link off, from bytes or with a non-empty password), /// by the user or as a command, get the `pdfTextAs` preference as their `textAs` when it is Outlines and they have no -/// `textAs`. A linked page shows the file's own text, and a `.ai` or `.ait` file is placed as it -/// says. +/// `textAs`. Bytes are a PDF by their content ([`places_pdf`]). A linked page shows the file's own +/// text, and a `.ai` or `.ait` file is placed as it says. pub(crate) fn place_with_preferred_text_as(app: &VectorcraftApp, p: &mut Value) { let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); let path = p.get("path").and_then(Value::as_str); let name = path.or_else(|| p.get("name").and_then(Value::as_str)).unwrap_or_default(); let linked = path.is_some() && p.get("link").and_then(Value::as_bool) != Some(false) && !password_given(p); - if given("textAs") || linked || preferred_text_as(app) == TextAs::Text || fileio::extension(name) != "pdf" { + if given("textAs") || linked || preferred_text_as(app) == TextAs::Text || !places_pdf(p, path, name) { return; } if let Some(o) = p.as_object_mut() { @@ -286,6 +286,21 @@ pub(crate) fn place_with_preferred_text_as(app: &VectorcraftApp, p: &mut Value) } } +/// Do the `file.place` params `p` place a PDF (not a `.ai` or `.ait` file, which is PDF inside): +/// a `path` by its extension, as [`place_source`] picks one, and `dataBase64` by its content, as +/// `file.place` reads it ([`fileio::detect`]: bytes that start with `%PDF`), so bytes without a +/// `name` or with another extension count too. Only the first bytes are decoded. +fn places_pdf(p: &Value, path: Option<&str>, name: &str) -> bool { + let ext = fileio::extension(name); + match (path, p.get("dataBase64").and_then(Value::as_str)) { + (None, Some(b64)) => { + let head: String = b64.chars().filter(|c| !c.is_ascii_whitespace()).take(8).collect(); + !matches!(ext.as_str(), "ai" | "ait") && vectorcraft_format::base64_decode(&head).is_some_and(|b| b.starts_with(b"%PDF")) + } + _ => ext == "pdf", + } +} + fn text_as(d: &Dialog) -> TextAs { text_as_of(&d.str("textAs")) } diff --git a/crates/ui-egui/src/dialogs/tests_import_pdf.rs b/crates/ui-egui/src/dialogs/tests_import_pdf.rs index 6e234bbd0c..5d60c04b36 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -532,6 +532,25 @@ fn file_place_without_the_dialog_takes_the_pdf_text_preference() { assert_eq!(type_objects(&app), 2, "Editable: page 1's type object"); } +/// PDF bytes placed without a `name`, or under another extension, are a PDF by their content, as +/// `file.place` reads them, and take the PDF Text preference; under a `.ai` name they are placed as +/// the file says. +#[test] +fn pdf_bytes_without_a_pdf_name_take_the_pdf_text_preference() { + let mut app = app(); + set_pdf_text(&mut app, "outlines"); + let data = vectorcraft_format::base64_encode(&text_pages(1)); + crate::place::run(&mut app, &json!({"dataBase64": data, "showImportOptions": false})).unwrap(); + assert!(app.ui.dialog.is_none(), "no dialog"); + assert_eq!(type_objects(&app), 0, "unnamed bytes: outlines"); + + crate::place::run(&mut app, &json!({"name": "page.bin", "dataBase64": data, "showImportOptions": false})).unwrap(); + assert_eq!(type_objects(&app), 0, "another extension: outlines"); + + crate::place::run(&mut app, &json!({"name": "page.ai", "dataBase64": data, "showImportOptions": false})).unwrap(); + assert_eq!(type_objects(&app), 1, "a .ai file: its own text"); +} + /// With Show Import Options off, an encrypted PDF placed with an empty `password` asks for its /// password, as one placed without a `password` does. #[test]