Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge PR #963

Brandon Thomas999bb848ccb4
diff --cc crates/idml/src/import.rs
index 36d8ed355,c72237964..517e3ee80
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@@ -2534,19 -2392,16 +2545,20 @@@ impl<'r> Importer<'r>
if data.is_empty()
&& let Some(p) = &uri
{
- // Every placement of a linked file shares one asset; read the file once.
- if let Some(id) = self.linked_assets.get(p) {
+ // Every placement of a linked file at one page shares one asset; read the file once.
+ let key = (p.clone(), page);
+ if let Some(id) = self.linked_assets.get(&key) {
- return graphic(*id);
+ let size = self.assets.get(id).and_then(|a| svg_viewbox_size(&a.data)).unwrap_or(size);
+ return graphic(*id, size);
}
if let Some(d) = (self.read_link)(p) {
data = d;
- read_from = Some(p.clone());
+ read_from = Some(key);
}
}
+ // InDesign draws a viewBox-only SVG at one unit to the point: its GraphicBounds are the
+ // art's bounds, which can overhang the viewBox, so they must not size the viewBox.
+ let size = svg_viewbox_size(&data).unwrap_or(size);
let (mime, px) = sniff_image(&data);
let mime = mime.map(str::to_string).unwrap_or_else(|| match link.and_then(|k| k.get("LinkResourceFormat")).unwrap_or("") {
f if f.contains("JPEG") => "image/jpeg".into(),
@@@ -2564,11 -2419,11 +2576,11 @@@
let fresh = AssetId(self.alloc());
let link_path = uri.filter(|p| p.contains('/') || p.contains('\\'));
let id =
- designcraft_doc::insert_asset(&mut self.assets, Asset { page: 0, id: fresh, name, mime, link: link_path, data: Arc::new(data), pixels });
- if let Some(p) = read_from {
- self.linked_assets.insert(p, id);
+ designcraft_doc::insert_asset(&mut self.assets, Asset { page, id: fresh, name, mime, link: link_path, data: Arc::new(data), pixels });
+ if let Some(key) = read_from {
+ self.linked_assets.insert(key, id);
}
- graphic(id)
+ graphic(id, size)
}
fn link_threads(&mut self) {
diff --cc crates/idml/src/tests/assets.rs
index 8c1ea2ee0,d4d1501f2..6817208cd
--- a/crates/idml/src/tests/assets.rs
+++ b/crates/idml/src/tests/assets.rs
@@@ -109,45 -125,26 +125,69 @@@ fn one_file_placed_several_times_import
}
}
+/// An `<SVG>` placement linking `uri`, whose GraphicBounds are `bounds` (left top right bottom).
+fn svg(n: usize, uri: &str, bounds: [f64; 4]) -> String {
+ let [l, t, r, b] = bounds;
+ format!(
+ r#"<SVG Self="sv{n}" ItemTransform="1 0 0 1 0 0">
+ <Properties><GraphicBounds Left="{l}" Top="{t}" Right="{r}" Bottom="{b}"/></Properties>
+ <Link Self="lk{n}" LinkResourceURI="{uri}" LinkResourceFormat="$ID/SVG" StoredState="Normal"/>
+ </SVG>"#
+ )
+}
+
+#[test]
+fn placed_svg_with_only_a_viewbox_keeps_its_viewbox_size() {
+ const OVERHANG: &str =
+ r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100"><line x1="-10" y1="50" x2="0" y2="50" stroke="#000"/></svg>"##;
+ const SIZED: &str =
+ r#"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100"><rect width="200" height="100"/></svg>"#;
+ let reader = |p: &str| match p {
+ "/art/overhang.svg" => Some(OVERHANG.as_bytes().to_vec()),
+ "/art/sized.svg" => Some(SIZED.as_bytes().to_vec()),
+ _ => None,
+ };
+ // GraphicBounds are the art's bounds: 211 wide for the overhanging one, placed twice (the second
+ // reuses the first's asset); a sized SVG is still stretched onto its bounds.
+ let frames = vec![
+ frame(1, 0.0, 0.0, &svg(1, "file:/art/overhang.svg", [-11.0, 0.0, 200.0, 100.0])),
+ frame(2, 100.0, 0.0, &svg(2, "file:/art/overhang.svg", [-11.0, 0.0, 200.0, 100.0])),
+ frame(3, 200.0, 0.0, &svg(3, "file:/art/sized.svg", [-10.0, 0.0, 200.0, 100.0])),
+ ];
+ let d = import_idml_with(&package(&frames), &reader).unwrap();
+ let mut gs: Vec<_> = d.spreads[0]
+ .items
+ .iter()
+ .filter_map(|i| match &i.content {
+ Content::Graphic(g) => Some((i.xf.translation().x, g.size, g.xf.translation().x)),
+ _ => None,
+ })
+ .collect();
+ gs.sort_by(|a, b| a.0.total_cmp(&b.0));
+ let got: Vec<_> = gs.into_iter().map(|(_, size, x)| (size, x)).collect();
+ assert_eq!(got, vec![((200.0, 100.0), -11.0), ((200.0, 100.0), -11.0), ((210.0, 100.0), -10.0)]);
+}
++
+ /// `PDFAttribute/@PageNumber` (1-based) is read into `Asset.page` (0-based), and the same file at
+ /// different pages stays apart (#905).
+ #[test]
+ fn a_pdf_placement_reads_its_page_number() {
+ let bytes = b"%PDF-1.7\n% fixture".to_vec();
+ let reader = |p: &str| p.ends_with("insert.pdf").then(|| bytes.clone());
+ let frames = vec![pdf_frame(1, 0.0, 2), pdf_frame(2, 100.0, 1), pdf_frame(3, 200.0, 2)];
+ let d = import_idml_with(&package(&frames), &reader).unwrap();
+ let p = placements(&d);
+ let page = |id| d.assets.get(&id).map(|a| a.page);
+ assert_eq!(page(p[0].0), Some(1), "PageNumber=\"2\" is the second page, stored 0-based");
+ assert_eq!(page(p[1].0), Some(0), "PageNumber=\"1\" is the first page");
+ assert_eq!(p[0].0, p[2].0, "the same file at the same page shares one asset");
+ assert_ne!(p[0].0, p[1].0, "the same file at another page is another asset");
+
+ // Exported (linked or embedded), the page survives the round trip.
+ for embed_images in [false, true] {
+ let out = export_idml_with(&d, &ExportOptions { embed_images });
+ let back = import_idml_with(&out, &reader).unwrap();
+ let pages: Vec<_> = placements(&back).iter().map(|(id, _, _)| back.assets.get(id).map(|a| a.page)).collect();
+ assert_eq!(pages, vec![Some(1), Some(0), Some(1)], "embed_images: {embed_images}");
+ }
+ }