Merge pull request #8940 from dotCooCoo/fix/place-at-once-pdf-text-as

Place: file.place without the dialog applies the PDF Text preference to an embedded PDF
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 05:10:21 +07:00
commit 2e02c4c8f4
3 files changed
+99 -9

No files matched your search

+21 -6
View File
@@ -629,17 +629,17 @@ 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
/// `textAs`. A linked page shows the file's own text, and a `.ai` or `.ait` file is placed as it
/// says. An empty `password` is none, so a place from a path with Link on and an empty
/// `password` stays linked.
/// 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`. 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. 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) && !gives_password(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() {
@@ -647,6 +647,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",
}
}
/// Keeps the Text choice OK confirmed in the `pdfTextAs` preference.
fn keep_text_as(app: &mut VectorcraftApp, text_as: TextAs) {
if preferred_text_as(app) != text_as {
@@ -556,6 +556,81 @@ fn the_text_choice_applies_to_embedded_places_only() {
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!(!is_open(&app), "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);
// `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");
}
/// 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!(!is_open(&app), "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]
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)));
set(&mut app, "password", json!("pw"));
confirm(&mut app).unwrap();
assert!(!is_open(&app));
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());
}
/// `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]
+3 -3
View File
@@ -304,9 +304,9 @@ fn place_file_in(app: &mut VectorcraftApp, p: &Value, read: Read) -> Result<Valu
o.remove(ASKED);
o.remove(DROPPED);
}
if matches!(read, Read::ForUser { .. }) {
crate::dialogs::import_pdf::place_with_preferred_text_as(app, &mut p);
}
// An embedded PDF without `textAs` takes the `pdfTextAs` preference, placed by the user or run
// as a command.
crate::dialogs::import_pdf::place_with_preferred_text_as(app, &mut p);
match read {
Read::ForUser { dropped } if crate::placing::available(app) => {
let r = crate::placing::start(app, &p, dropped);