mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 08:26:28 +00:00
Place: the Place dialog's Link tooltip offers an EPS file as one locked object
With Link, File > Place now places an EPS file, a PostScript file and a PostScript .ai as a linked placed document, but the Place dialog still counted them as other files, so Link's tooltip did not say they become one locked object, and the dialog test expected a PostScript .ai to be embedded. - `is_vectorcraft_file` counts an EPS, EPSF or PostScript file on disk, and a PostScript .ai, as a document, and leaves out a DCS file, which is placed as a linked image. - Where the platform reads part of a file, it reads only the file's start and tells a DCS file from it with `place::head_is_dcs` (the check Links uses), binary EPS headers included. - The dialog test now checks the .ai, .eps and .ps cases, the bounded read, DCS files plain and behind a binary header, and a file named .eps that isn't PostScript.
This commit is contained in:
@@ -159,6 +159,16 @@ struct Loaded {
|
||||
/// of its art, and their options.
|
||||
type Slices = Vec<(Rect, SliceOptions)>;
|
||||
|
||||
/// The most bytes [`head_is_dcs`] reads from the start of a file.
|
||||
pub const DCS_HEAD_BYTES: usize = dcs::HEAD_BYTES;
|
||||
|
||||
/// Does `head`, the start of a file `size` bytes long (at most [`DCS_HEAD_BYTES`] of it, or all of
|
||||
/// it), name DCS plates? `file.place` places such a file as a linked image, never as a placed
|
||||
/// document. `None` when `head` can't tell.
|
||||
pub fn head_is_dcs(head: &[u8], size: u64) -> Option<bool> {
|
||||
dcs::head_is_dcs(head, size)
|
||||
}
|
||||
|
||||
/// Points per pixel of a raster image at the resolution it declares (72 ppi when none).
|
||||
pub(crate) fn pt_per_px(ppi: Option<(f64, f64)>) -> (f64, f64) {
|
||||
let (x, y) = ppi.unwrap_or((72.0, 72.0));
|
||||
|
||||
@@ -305,14 +305,36 @@ fn open_dialog(app: &mut VectorcraftApp, files: Vec<Value>) {
|
||||
}
|
||||
|
||||
/// Is dialog file entry `file` a VectorCraft document on disk (what Link places as one locked
|
||||
/// object), or a PDF or `.ai` file on disk? A `.ai` saved without PDF compatibility is PostScript,
|
||||
/// which `file.place` embeds whether Link is on or not.
|
||||
/// object), or a PDF, `.ai`, EPS or PostScript file on disk? A DCS file (an EPS file naming its
|
||||
/// plates) is placed as a linked image instead. Where the platform reads part of a file, only the
|
||||
/// file's start is read; a file longer than that is taken to be at least as long as its binary EPS
|
||||
/// header says, and one whose header can't be told from it counts as a document.
|
||||
fn is_vectorcraft_file(app: &VectorcraftApp, file: &Value) -> bool {
|
||||
use vectorcraft_engine::cmd::place::{DCS_HEAD_BYTES, head_is_dcs};
|
||||
let Some(path) = file.get("path").and_then(Value::as_str) else { return false };
|
||||
let native = ["vectorcraft", vectorcraft_format::LEGACY_EXTENSION, "vctemplate"];
|
||||
// The file's start, and the file's length when that is all of it.
|
||||
let bytes = || match (&app.services.read_head, &app.services.read) {
|
||||
(Some(read_head), _) => read_head(path, DCS_HEAD_BYTES + 1).ok().map(|mut b| {
|
||||
let whole = b.len() <= DCS_HEAD_BYTES;
|
||||
b.truncate(DCS_HEAD_BYTES);
|
||||
let size = if whole { b.len() as u64 } else { u64::MAX };
|
||||
(b, size)
|
||||
}),
|
||||
(None, Some(read)) => read(path).ok().map(|b| {
|
||||
let size = b.len() as u64;
|
||||
(b, size)
|
||||
}),
|
||||
(None, None) => None,
|
||||
};
|
||||
let dcs = |(b, size): &(Vec<u8>, u64)| {
|
||||
let head = b.get(..DCS_HEAD_BYTES).unwrap_or(b);
|
||||
vectorcraft_pdf::is_postscript(head) && head_is_dcs(head, *size).unwrap_or(false)
|
||||
};
|
||||
match fileio::extension(path).as_str() {
|
||||
e if native.contains(&e) => true,
|
||||
"pdf" | "ai" | "ait" => app.services.read.as_ref().and_then(|r| r(path).ok()).is_none_or(|b| !vectorcraft_pdf::is_postscript(&b)),
|
||||
"pdf" | "ai" | "ait" => bytes().is_none_or(|b| !dcs(&b)),
|
||||
"eps" | "epsf" | "ps" => bytes().is_some_and(|b| vectorcraft_pdf::is_postscript(&b.0) && !dcs(&b)),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -756,21 +756,61 @@ fn the_place_dialog_links_a_pdf_page_and_embeds_it_with_link_off() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_place_dialog_does_not_offer_a_postscript_ai_as_a_locked_object() {
|
||||
fn the_place_dialog_offers_a_postscript_ai_and_an_eps_file_as_a_locked_object() {
|
||||
let mut app = app();
|
||||
let ps = b"%!PS-Adobe-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n1 0 0 setrgbcolor 10 10 50 50 rectfill\nshowpage\n%%EOF\n";
|
||||
let dir = Folder::new("ui-place-postscript");
|
||||
let path = dir.file("legacy.ai", ps);
|
||||
// file.place embeds a .ai saved without PDF compatibility, even with Link on.
|
||||
let r = app.session.execute("file.place", &json!({"path": path, "link": true})).unwrap();
|
||||
assert_eq!(r["linked"], json!(false), "{r}");
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(!matches!(st.doc.node(st.selection.objects[0]).unwrap().kind, NodeKind::PlacedDocument(_)));
|
||||
// So Link's tooltip does not promise one locked object for it.
|
||||
for name in ["legacy.ai", "legacy.eps", "legacy.ps"] {
|
||||
let path = dir.file(name, ps);
|
||||
// With Link, file.place places a .ai saved without PDF compatibility, an EPS file and a
|
||||
// PostScript file as one locked placed document.
|
||||
let r = app.session.execute("file.place", &json!({"path": path, "link": true})).unwrap();
|
||||
assert_eq!(r["linked"], json!(true), "{name}: {r}");
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(matches!(st.doc.node(st.selection.objects[0]).unwrap().kind, NodeKind::PlacedDocument(_)), "{name}");
|
||||
// So Link's tooltip promises one locked object for it.
|
||||
place_picked(&mut app, &path);
|
||||
let d = app.ui.dialog.as_ref().expect("the Place dialog");
|
||||
assert!(d.bool("__documents") && !d.bool("__others"), "{name}: {:?}", d.fields);
|
||||
assert!(crate::dialogs::place::link_tip(d).contains("locked object"), "{name}");
|
||||
app.ui.dialog = None;
|
||||
}
|
||||
// Where the platform reads part of a file, only the file's start is read to tell.
|
||||
let asked = std::rc::Rc::new(std::cell::Cell::new(0));
|
||||
let seen = asked.clone();
|
||||
app.services.read_head = Some(Box::new(move |p: &str, max: usize| {
|
||||
seen.set(max);
|
||||
std::fs::read(p).map(|b| b[..b.len().min(max)].to_vec()).map_err(|e| e.to_string())
|
||||
}));
|
||||
place_picked(&mut app, &dir.file("headed.eps", ps));
|
||||
assert!(app.ui.dialog.as_ref().expect("the Place dialog").bool("__documents"));
|
||||
assert_eq!(asked.get(), vectorcraft_engine::cmd::place::DCS_HEAD_BYTES + 1);
|
||||
app.ui.dialog = None;
|
||||
// A DCS file is placed as a linked image, from its start alone too: past the start the
|
||||
// PostScript goes on longer than what is read.
|
||||
let mut dcs = b"%!PS-Adobe-2.0 EPSF-1.2\n%%BoundingBox: 0 0 20 10\n%%CyanPlate: pic.C\n%%EndComments\n".to_vec();
|
||||
dcs.resize(vectorcraft_engine::cmd::place::DCS_HEAD_BYTES + 100, b'\n');
|
||||
// The same PostScript behind a binary EPS header (no preview).
|
||||
let mut binary = vec![0xC5, 0xD0, 0xD3, 0xC6];
|
||||
binary.extend(30u32.to_le_bytes());
|
||||
binary.extend((dcs.len() as u32).to_le_bytes());
|
||||
binary.resize(30, 0);
|
||||
binary.extend(&dcs);
|
||||
let files = [("plates.eps", &dcs, true), ("plates.ai", &dcs, true), ("binary.eps", &binary, true), ("binary.eps", &binary, false)];
|
||||
for (name, bytes, head) in files {
|
||||
if !head {
|
||||
app.services.read_head = None;
|
||||
}
|
||||
place_picked(&mut app, &dir.file(name, bytes));
|
||||
let d = app.ui.dialog.as_ref().expect("the Place dialog");
|
||||
assert!(!d.bool("__documents") && d.bool("__others"), "{name}, head {head}: {:?}", d.fields);
|
||||
app.ui.dialog = None;
|
||||
}
|
||||
// A file named .eps that isn't PostScript gets the tooltip for other files.
|
||||
let path = dir.file("notes.eps", b"not PostScript");
|
||||
place_picked(&mut app, &path);
|
||||
let d = app.ui.dialog.as_ref().expect("the Place dialog");
|
||||
assert!(!d.bool("__documents") && d.bool("__others"), "{:?}", d.fields);
|
||||
assert!(!crate::dialogs::place::link_tip(d).contains("locked object"));
|
||||
}
|
||||
|
||||
/// Tool key `key` as the Control bar's Apply (Enter) and Cancel (Escape) send it.
|
||||
|
||||
Reference in New Issue
Block a user