mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
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.
This commit is contained in:
1 parent
35ee5de2d4
commit
cfb9f82568
2 files changed
+37
-3
No files matched your search
@@ -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"))
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user