mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 09:35:43 +00:00
Import PDF: linked pages refuse a .ai saved without PDF compatibility
linkPages refused other files but let a .ai whose page is only a placeholder through, and each such page then showed the file's editing data. A page read from editing data is now refused with the existing error. The file is also hashed once for all its linked pages instead of once per page.
This commit is contained in:
@@ -253,10 +253,17 @@ pub(super) fn linked_pages(name: &str, bytes: &[u8], path: Option<&str>, o: &Loa
|
||||
let gap = ImportOptions::default().artboard_gap;
|
||||
// Every page's object keeps the same one copy of the file.
|
||||
let shared = Arc::new(bytes.to_vec());
|
||||
// The file is hashed once for all its pages.
|
||||
let hash = link.hash.clone().unwrap_or_else(|| vectorcraft_doc::links::hash_bytes(bytes));
|
||||
let (mut x, mut warnings, mut cmyk_pages) = (0.0, Vec::<String>::new(), 0);
|
||||
for (i, &index) in picked.iter().enumerate() {
|
||||
let page = index as u32 + 1;
|
||||
let s = document::shared_source(name, &shared, Some(path), page, bounding, document::page_box(o.crop), C)?;
|
||||
let s = document::shared_source(name, (&shared, &hash), Some(path), page, bounding, document::page_box(o.crop), C)?;
|
||||
// A page that is only a placeholder (a `.ai` saved without PDF compatibility) shows the
|
||||
// file's editing data, not a page of the file.
|
||||
if s.editing_data {
|
||||
return Err(bad(C, format!("linkPages: `{file}` isn't a PDF or a PDF-compatible .ai file")));
|
||||
}
|
||||
cmyk_pages += usize::from(s.color_mode == ColorMode::Cmyk);
|
||||
let mut node = document::node(&mut doc, &s, LinkInfo { page: Some(page), ..link.clone() }, bounding);
|
||||
if let NodeKind::PlacedDocument(p) = &mut node.kind {
|
||||
|
||||
@@ -1160,6 +1160,27 @@ fn an_ai_file_saved_without_its_pdf_part_places_from_its_editing_data() {
|
||||
assert!(e.contains("placeholder"), "{e}");
|
||||
}
|
||||
|
||||
/// `document.open` with `linkPages` refuses a `.ai` saved without PDF compatibility, whose page is
|
||||
/// only a placeholder, and opens a PDF-compatible one.
|
||||
#[test]
|
||||
fn link_pages_refuses_an_ai_file_saved_without_its_pdf_part() {
|
||||
let d = std::env::temp_dir().join(format!("vc-ailinkpages-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
let file = |name: &str, page: &str| {
|
||||
let path = d.join(name);
|
||||
std::fs::write(&path, ai::ai(&compressed(&ai::sample_data()), page)).unwrap();
|
||||
path.to_string_lossy().to_string()
|
||||
};
|
||||
let (plain, compatible) = (file("plain.ai", PLACEHOLDER), file("compatible.ai", "0 g 10 10 40 40 re f"));
|
||||
let mut s = Session::new();
|
||||
let e = s.execute("document.open", &json!({"path": plain, "linkPages": true})).unwrap_err().to_string();
|
||||
assert!(e.contains("isn't a PDF or a PDF-compatible .ai file"), "{e}");
|
||||
assert!(s.documents().is_empty(), "nothing opened");
|
||||
s.execute("document.open", &json!({"path": compatible, "linkPages": true})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.has_placed());
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
/// A page of text alone, as text-only art has in its PDF part: no path is painted.
|
||||
const TEXT_ART: &str = "BT /F1 36 Tf 10 40 Td (Text only) Tj ET";
|
||||
|
||||
|
||||
@@ -348,7 +348,7 @@ pub(crate) fn pdf_art(
|
||||
let clip = (!bounding).then_some(board);
|
||||
let frame = clip.or_else(|| shown_bounds(&symbols, &nodes));
|
||||
let frame = frame.filter(|r| r.width() > 0.0 || r.height() > 0.0).ok_or_else(|| bad(cmd, format!("`{name}` has no art to place")))?;
|
||||
Ok(document::DocumentArt { doc, nodes, clip, frame, warnings })
|
||||
Ok(document::DocumentArt { doc, nodes, clip, frame, warnings, editing_data: alone })
|
||||
}
|
||||
|
||||
/// The bounds of what `nodes` draw, as an export measures art
|
||||
|
||||
@@ -100,6 +100,9 @@ pub(crate) struct DocumentArt {
|
||||
/// What is shown: the artboard, or the art's bounds.
|
||||
pub frame: Rect,
|
||||
pub warnings: Vec<String>,
|
||||
/// Whether a PDF or `.ai` page was read from the file's editing data (its page only a
|
||||
/// placeholder, or its PDF part unreadable) instead of from the page.
|
||||
pub editing_data: bool,
|
||||
}
|
||||
|
||||
/// Artboard `page` (1-based) of the VectorCraft document `name` (`bytes`, read from `path` when
|
||||
@@ -145,7 +148,7 @@ pub(crate) fn document_art(
|
||||
let nodes = super::on_artboard(&symbols, &layers, board);
|
||||
let frame = if bounding { super::shown_bounds(&symbols, &nodes) } else { Some(board) };
|
||||
let frame = frame.filter(|r| r.width() > 0.0 || r.height() > 0.0).ok_or_else(|| bad(cmd, format!("`{file}` has no art to place")))?;
|
||||
Ok(DocumentArt { doc: l.doc, nodes, clip: (!bounding).then_some(board), frame, warnings: l.warnings })
|
||||
Ok(DocumentArt { doc: l.doc, nodes, clip: (!bounding).then_some(board), frame, warnings: l.warnings, editing_data: false })
|
||||
}
|
||||
|
||||
/// The most placed documents read inside one another while their links are resolved.
|
||||
@@ -223,6 +226,8 @@ pub(crate) struct Source {
|
||||
pub warnings: Vec<String>,
|
||||
/// The colour mode the file's art was read in (a PDF page painted mostly in CMYK reads in CMYK).
|
||||
pub color_mode: ColorMode,
|
||||
/// [`DocumentArt::editing_data`].
|
||||
pub editing_data: bool,
|
||||
}
|
||||
|
||||
/// The VectorCraft document `name` (`bytes`, from `path` when there is one) read for showing
|
||||
@@ -233,32 +238,36 @@ pub(crate) fn source(name: &str, bytes: &[u8], path: Option<&str>, page: u32, bo
|
||||
}
|
||||
|
||||
/// [`source`] of the file whose bytes `bytes` holds, kept by the blob without a copy of its own:
|
||||
/// several pages of one file read this way share one copy of it.
|
||||
/// several pages of one file read this way share one copy of it, with its hash ([`hash_bytes`])
|
||||
/// worked out once for them all: `file` is the bytes and that hash.
|
||||
pub(crate) fn shared_source(
|
||||
name: &str,
|
||||
bytes: &Arc<Vec<u8>>,
|
||||
file: (&Arc<Vec<u8>>, &str),
|
||||
path: Option<&str>,
|
||||
page: u32,
|
||||
bounding: bool,
|
||||
page_box: PageBox,
|
||||
cmd: &str,
|
||||
) -> Result<Source> {
|
||||
read_source(name, bytes, Some(bytes), path, (page, bounding, page_box), cmd)
|
||||
read_source(name, file.0, Some(file), path, (page, bounding, page_box), cmd)
|
||||
}
|
||||
|
||||
/// [`source`], the blob keeping `shared` when given (else a copy of `bytes`); `shown` is the page
|
||||
/// (or artboard), whether the art's bounds are shown, and the page's box.
|
||||
/// [`source`], the blob keeping `shared`'s bytes when given (else a copy of `bytes`), and its hash
|
||||
/// used for the key (else `bytes` hashed); `shown` is the page (or artboard), whether the art's
|
||||
/// bounds are shown, and the page's box.
|
||||
fn read_source(
|
||||
name: &str,
|
||||
bytes: &[u8],
|
||||
shared: Option<&Arc<Vec<u8>>>,
|
||||
shared: Option<(&Arc<Vec<u8>>, &str)>,
|
||||
path: Option<&str>,
|
||||
shown: (u32, bool, PageBox),
|
||||
cmd: &str,
|
||||
) -> Result<Source> {
|
||||
let (page, bounding, page_box) = shown;
|
||||
let hash = shared.map_or_else(|| hash_bytes(bytes), |(_, h)| h.to_string());
|
||||
let shared = shared.map(|(b, _)| b);
|
||||
// The same document placed the same way is read once (placing it again is free).
|
||||
let key = key_for(&hash_bytes(bytes), page, bounding, page_box);
|
||||
let key = key_for(&hash, page, bounding, page_box);
|
||||
// Resolving its links depends on where the file is and on what the caller may read, so a hit
|
||||
// needs the same path, and nothing is shared with or from a confined (automation) caller.
|
||||
let confined = crate::file_access::current().is_some();
|
||||
@@ -284,10 +293,10 @@ fn read_source(
|
||||
};
|
||||
let art = group(a.nodes, a.clip);
|
||||
blob.proxy = preview(&a.doc, &art, a.frame).map(Arc::new);
|
||||
let color_mode = a.doc.color_mode;
|
||||
let (color_mode, editing_data) = (a.doc.color_mode, a.editing_data);
|
||||
// Drawing it needn't read it again.
|
||||
vectorcraft_doc::placed_document::prime(&shown, blob.bytes.clone(), (a.doc, art, a.frame));
|
||||
let s = Source { blob, key, page_box, frame: a.frame, warnings: a.warnings, color_mode };
|
||||
let s = Source { blob, key, page_box, frame: a.frame, warnings: a.warnings, color_mode, editing_data };
|
||||
if !confined {
|
||||
let mut m = lock_memo();
|
||||
if m.len() >= MEMO_SIZE {
|
||||
|
||||
Reference in New Issue
Block a user