diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index d29d9742c4..e28bdbb021 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -580,12 +580,19 @@ pub fn offer_place(app: &mut VectorcraftApp, p: &Value) -> bool { offer(app, &name, &bytes, path, Some(p)) } +/// Whether the `file.place` params `p` give a password. `file.place` reads an empty or null +/// `password` as none; a value that isn't text counts as given, for `file.place` to refuse. +fn gives_password(p: &Value) -> bool { + p.get("password").is_some_and(|v| !v.is_null() && v.as_str() != Some("")) +} + /// [`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 -/// `page`) and crop box once it is unlocked; any other PDF is placed that way without asking. +/// encrypted PDF given no password (see [`gives_password`]), 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 gives_password(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 }; @@ -626,12 +633,13 @@ pub(crate) fn with_preferred_text_as(app: &VectorcraftApp, name: &str, bytes: &[ /// 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 /// `textAs`. A linked page shows the file's own text, and a `.ai` or `.ait` file is placed as it -/// says. +/// says. An empty `password` is none, so a place from a path with Link on and an empty +/// `password` stays linked. 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) && !gives_password(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 249c52a3f9..30a1aa9d65 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -492,3 +492,51 @@ 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` reads an empty or null `password` as none: a place from a path with Link on stays +/// linked with the PDF Text preference at Outlines, and only a password that is given embeds it. +#[test] +fn an_empty_password_keeps_a_place_linked() { + let mut app = app(); + set_pdf_text(&mut app, "outlines"); + let dir = vectorcraft_testkit::TempDir::new("import-pdf-empty-password"); + let path = dir.join("one.pdf").to_string_lossy().into_owned(); + std::fs::write(&path, text_pages(1)).unwrap(); + for password in [json!(""), json!(null)] { + let mut p = json!({"path": path, "password": password}); + import_pdf::place_with_preferred_text_as(&app, &mut p); + assert_eq!(p, json!({"path": path, "password": password}), "no textAs with password {password}"); + } + let mut p = json!({"path": path, "password": "pw"}); + import_pdf::place_with_preferred_text_as(&app, &mut p); + assert_eq!(p["textAs"], "outlines", "a password embeds the page"); + + crate::place::run_for_user(&mut app, &json!({"path": path, "password": ""})).unwrap(); + opened(&mut app); + 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); +} + +/// With Show Import Options off, an encrypted PDF placed with an empty `password` asks for its +/// password, as one placed without `password` does; a password that is given goes to `file.place`. +#[test] +fn an_empty_password_still_asks_for_the_password() { + 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")); + let data = vectorcraft_format::base64_encode(&bytes); + let close = |app: &VectorcraftApp| { + let id = dialog_id(app).expect("an Import PDF dialog"); + assert!(app.dialogs().unwrap().with(|host| host.cancel_by_id(&ctx(app), id))); + assert!(!is_open(app)); + }; + for password in [json!(""), json!(null)] { + let p = json!({"name": "locked.pdf", "dataBase64": data, "password": password}); + assert!(import_pdf::offer_place_password(&mut app, &p), "asks with password {password}"); + assert_eq!((field(&app, "__locked"), field(&app, "__passwordOnly")), (json!(true), json!(true))); + close(&app); + } + let p = json!({"name": "locked.pdf", "dataBase64": data, "password": "pw"}); + assert!(!import_pdf::offer_place_password(&mut app, &p), "a given password is placed with"); + assert!(!is_open(&app)); +} diff --git a/docs/mcp.md b/docs/mcp.md index 75eeab902e..abf7f9bcb3 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9247,7 +9247,7 @@ unless `link: false`; a Photoshop document as its merged image), an SVG as one g and swatches it uses. A VectorCraft document read from `path` is a placed document: one locked object showing its artboard `page` (or, with `crop: "bounding"`, its art's bounds), linked to the file and read again when it changes (see Linked images), and vectors in every output; with `link: false` (or from `dataBase64`) it is an editable copy -of its art, as one clipped group. A PDF, `.ai` or `.ait` page read from `path` (with neither `password` nor +of its art, as one clipped group. A PDF, `.ai` or `.ait` page read from `path` (with neither a non-empty `password` nor `textAs: "outlines"`) is a placed document the same way, showing page `page` clipped to its `crop` box (or its art's bounds); with `link: false` it is placed as one clipped group. An EPS file, a PostScript file or a PostScript `.ai` read from `path` is a placed document too, showing the art `link: false` places (clipped to its bounding box, or its art's bounds with `crop: "bounding"`); @@ -9320,7 +9320,7 @@ slices (Place PSD). `showImportOptions: false` places such a file without asking its dialog: the text options, the DXF fit, lineweight, centering and layer choices with the drawing at 1:1 in its own unit, and the PSD conversion, hidden-layer and Import Slices choices. A PDF is placed on page 1 (or `page`) and its crop box, and an encrypted PDF given no -`password` still asks for the password. An encrypted PDF given a `page` and no `password` asks for the password with +`password` (or an empty one) still asks for the password. An encrypted PDF given a `page` and no `password` asks for the password with Show Import Options on too, and places that page. A `showImportOptions` that is not true or false is refused, and nothing is placed. The Place dialog (File › Place, or `file.place {paths}`) has a Show Import Options checkbox after Link, Template and Replace ([Place multiple files in a document](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-other-file-types/place-multiple-files-in-a-document.html)).