DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge asset-dedupe: Assets: one asset per image, and saves leave out unused ones
aionggcfd57cb3ca59
diff --cc crates/doc/src/assets.rsindex 000000000,73f7ac98d..1b6520f16mode 000000,100644..100644--- a/crates/doc/src/assets.rs+++ b/crates/doc/src/assets.rs@@@ -1,0 -1,342 +1,343 @@@+ //! Placed files (assets): one asset per distinct file, the assets a document uses, and the+ //! compaction a save applies (duplicates merged, unused assets left out).+ //!+ //! An asset holds what is shared by every placement of a file (bytes, link, type, the PDF page+ //! shown); each placement's frame data (transform, crop, fitting) lives on its [`Graphic`].++ use std::collections::{BTreeMap, BTreeSet, HashMap};+ use std::sync::Arc;++ use crate::story::Story;+ use crate::{Asset, AssetId, Content, Document, Item};++ /// Nesting limit for groups, anchored objects, table cells and notes. A document nested deeper+ /// is treated as using every asset, so nothing it references is merged away or pruned.+ const MAX_DEPTH: usize = 64;++ impl Asset {+ /// Whether `other` holds the same file: the same type, page and bytes. Assets without bytes+ /// (a link that couldn't be read) are the same only when they link to the same path.+ pub fn same_file(&self, other: &Asset) -> bool {+ if self.mime != other.mime || self.page != other.page {+ return false;+ }+ match (self.data.is_empty(), other.data.is_empty()) {+ (false, false) => Arc::ptr_eq(&self.data, &other.data) || (self.data.len() == other.data.len() && *self.data == *other.data),+ (true, true) => self.link.is_some() && self.link == other.link,+ _ => false,+ }+ }+ }++ /// The asset in `assets` holding the same file as `asset`.+ pub fn find_same_asset(assets: &BTreeMap<AssetId, Arc<Asset>>, asset: &Asset) -> Option<AssetId> {+ assets.values().find(|a| a.same_file(asset)).map(|a| a.id)+ }++ /// Add `asset` to `assets` unless they already hold the same file; returns the id placements+ /// should reference. A reused asset without a link takes the new asset's link.+ pub fn insert_asset(assets: &mut BTreeMap<AssetId, Arc<Asset>>, asset: Asset) -> AssetId {+ if let Some(id) = find_same_asset(assets, &asset) {+ if let Some(a) = assets.get_mut(&id)+ && a.link.is_none()+ && asset.link.is_some()+ {+ Arc::make_mut(a).link = asset.link;+ }+ return id;+ }+ let id = asset.id;+ assets.insert(id, Arc::new(asset));+ id+ }++ /// Calls `f` with every asset reference in `it` and its children; false when nesting exceeded+ /// [`MAX_DEPTH`] (some references weren't visited).+ fn item_refs(it: &Item, f: &mut impl FnMut(AssetId), depth: usize) -> bool {+ if depth > MAX_DEPTH {+ return false;+ }+ if let Content::Graphic(g) = &it.content {+ f(g.asset);+ }+ if let Some(p) = it.media.as_ref().and_then(|m| m.poster) {+ f(p);+ }+ it.children().iter().all(|c| item_refs(c, f, depth + 1))+ }++ /// Asset references in a story's anchored objects, graphic cells and notes (recursively).+ fn story_refs(st: &Story, f: &mut impl FnMut(AssetId), depth: usize) -> bool {+ if depth > MAX_DEPTH {+ return false;+ }+ let mut complete = st.objects.iter().all(|o| item_refs(&o.item, f, depth + 1));+ for t in st.tables.values() {+ for c in &t.cells {+ if let Some(g) = &c.graphic {+ f(g.asset);+ }+ complete &= story_refs(&c.text, f, depth + 1);+ }+ }+ for n in st.notes.iter().chain(&st.endnotes) {+ complete &= story_refs(&n.text, f, depth + 1);+ }+ complete+ }++ /// Every asset reference in a story (its anchored objects, graphic cells and notes).+ pub fn story_asset_refs(st: &Story) -> Vec<AssetId> {+ let mut out = Vec::new();+ story_refs(st, &mut |a| out.push(a), 0);+ out+ }++ /// Point a story's references to a key of `map` at its value.+ pub fn remap_story_assets(st: &mut Story, map: &HashMap<AssetId, AssetId>) {+ if !map.is_empty() {+ remap_story(st, map, 0);+ }+ }++ fn item_uses(it: &Item, map: &HashMap<AssetId, AssetId>) -> bool {+ let mut hit = false;+ item_refs(it, &mut |a| hit |= map.contains_key(&a), 0);+ hit+ }++ fn story_uses(st: &Story, map: &HashMap<AssetId, AssetId>) -> bool {+ let mut hit = false;+ story_refs(st, &mut |a| hit |= map.contains_key(&a), 0);+ hit+ }++ fn remap_id(id: &mut AssetId, map: &HashMap<AssetId, AssetId>) {+ if let Some(n) = map.get(id) {+ *id = *n;+ }+ }++ /// Rewrites the references in `it`, copying only the shared items that change.+ fn remap_item(it: &mut Arc<Item>, map: &HashMap<AssetId, AssetId>, depth: usize) {+ if depth > MAX_DEPTH || !item_uses(it, map) {+ return;+ }+ remap_item_in(Arc::make_mut(it), map, depth);+ }++ fn remap_item_in(it: &mut Item, map: &HashMap<AssetId, AssetId>, depth: usize) {+ if let Content::Graphic(g) = &mut it.content {+ remap_id(&mut g.asset, map);+ }+ if let Some(p) = it.media.as_mut().and_then(|m| m.poster.as_mut()) {+ remap_id(p, map);+ }+ if let Some(kids) = it.children_mut() {+ for k in kids {+ remap_item(k, map, depth + 1);+ }+ }+ }++ fn remap_story(st: &mut Story, map: &HashMap<AssetId, AssetId>, depth: usize) {+ if depth > MAX_DEPTH {+ return;+ }+ for o in &mut st.objects {+ if item_uses(&o.item, map) {+ remap_item_in(&mut Arc::make_mut(o).item, map, depth + 1);+ }+ }+ for t in st.tables.values_mut() {+ if !t.cells.iter().any(|c| c.graphic.as_ref().is_some_and(|g| map.contains_key(&g.asset)) || story_uses(&c.text, map)) {+ continue;+ }+ for c in &mut Arc::make_mut(t).cells {+ if let Some(g) = &mut c.graphic {+ remap_id(&mut g.asset, map);+ }+ remap_story(&mut c.text, map, depth + 1);+ }+ }+ for n in st.notes.iter_mut().chain(st.endnotes.iter_mut()) {+ if story_uses(&n.text, map) {+ remap_story(&mut Arc::make_mut(n).text, map, depth + 1);+ }+ }+ }++ impl Document {+ /// Add a placed file, reusing the asset of an identical file already in the document;+ /// returns the id to reference.+ pub fn add_asset(&mut self, asset: Asset) -> AssetId {+ insert_asset(&mut self.assets, asset)+ }++ /// Copy asset `id` of `src` into this document (or reuse the identical asset it already+ /// has); returns the id to reference here, `None` when `src` has no such asset.+ pub fn adopt_asset(&mut self, src: &Document, id: AssetId) -> Option<AssetId> {+ let a = src.assets.get(&id)?;+ if let Some(same) = find_same_asset(&self.assets, a) {+ return Some(same);+ }+ let fresh = AssetId(self.alloc());+ Some(self.add_asset(Asset { id: fresh, ..(**a).clone() }))+ }++ /// Calls `f` with every asset reference: graphics on document and parent spreads (in+ /// groups and multi-state objects too), media posters, anchored and inline objects, graphic+ /// table cells and notes. False when nesting exceeded the depth limit.+ fn asset_refs(&self, f: &mut impl FnMut(AssetId)) -> bool {+ let mut complete = true;+ for sp in self.spreads.iter().chain(&self.parents) {+ for it in &sp.items {+ complete &= item_refs(it, f, 0);+ }+ }+ for st in self.stories.values() {+ complete &= story_refs(st, f, 0);+ }+ complete+ }++ /// The assets something in the document references. When the document nests deeper than+ /// the traversal limit, every asset counts as used.+ pub fn used_assets(&self) -> BTreeSet<AssetId> {+ let mut used = BTreeSet::new();+ if !self.asset_refs(&mut |a| {+ used.insert(a);+ }) {+ return self.assets.keys().copied().collect();+ }+ used+ }++ /// Point every reference to a key of `map` at its value.+ pub fn remap_assets(&mut self, map: &HashMap<AssetId, AssetId>) {+ if map.is_empty() {+ return;+ }+ for sp in self.spreads.iter_mut().chain(self.parents.iter_mut()) {+ if sp.items.iter().any(|it| item_uses(it, map)) {+ for it in &mut Arc::make_mut(sp).items {+ remap_item(it, map, 0);+ }+ }+ }+ for st in self.stories.values_mut() {+ if story_uses(st, map) {+ remap_story(Arc::make_mut(st), map, 0);+ }+ }+ }++ /// Merge assets holding the same file into the first of them (lowest id), rewriting the+ /// references; returns how many assets were removed.+ pub fn merge_duplicate_assets(&mut self) -> usize {+ if !self.asset_refs(&mut |_| {}) {+ return 0;+ }+ let mut reps: HashMap<(usize, &str, u32), Vec<&Arc<Asset>>> = HashMap::new();+ let mut map = HashMap::new();+ let mut links = Vec::new();+ for a in self.assets.values() {+ let bucket = reps.entry((a.data.len(), a.mime.as_str(), a.page)).or_default();+ match bucket.iter().find(|r| r.same_file(a)) {+ Some(r) => {+ map.insert(a.id, r.id);+ if r.link.is_none()+ && let Some(l) = &a.link+ {+ links.push((r.id, l.clone()));+ }+ }+ None => bucket.push(a),+ }+ }+ for (id, link) in links {+ if let Some(a) = self.assets.get_mut(&id)+ && a.link.is_none()+ {+ Arc::make_mut(a).link = Some(link);+ }+ }+ self.remap_assets(&map);+ self.assets.retain(|id, _| !map.contains_key(id));+ map.len()+ }++ /// Merge duplicate assets and drop the ones nothing references (what a saved file holds).+ pub fn compact_assets(&mut self) {+ self.merge_duplicate_assets();+ let used = self.used_assets();+ self.assets.retain(|id, _| used.contains(id));+ }+ }++ #[cfg(test)]+ mod tests {+ use super::*;+ use crate::{Graphic, ItemId, LayerId, Shape, StoryId};+ use designcraft_geom::{Affine, Rect, shapes};++ fn asset(id: u64, data: &[u8]) -> Asset {- Asset { id: AssetId(id), name: "p.png".into(), mime: "image/png".into(), link: None, data: Arc::new(data.to_vec()), pixels: None, page: 0 }++ Asset { id: AssetId(id), name: "p.png".into(), mime: "image/png".into(), data: Arc::new(data.to_vec()), ..Default::default() }+ }++ fn graphic_item(id: u64, asset: u64, crop: f64) -> Item {+ let mut it = Item::new(ItemId(id), LayerId(1), Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)));+ it.content = Content::Graphic(Graphic {+ asset: AssetId(asset),+ size: (10.0, 10.0),+ xf: Affine::translate((crop, 0.0)),+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [crop; 4],++ wrap: Default::default(),+ });+ it+ }++ #[test]+ fn identical_files_share_one_asset() {+ let mut d = Document::new(&Default::default());+ let a = d.add_asset(asset(1, b"same"));+ let b = d.add_asset(Asset { link: Some("/x/p.png".into()), ..asset(2, b"same") });+ let c = d.add_asset(asset(3, b"diff"));+ let p = d.add_asset(Asset { page: 1, ..asset(4, b"same") });+ assert_eq!(a, b);+ assert_ne!(a, c);+ assert_ne!(a, p);+ assert_eq!(d.assets.len(), 3);+ assert_eq!(d.assets[&a].link.as_deref(), Some("/x/p.png"));+ // Unreadable links match by path only.+ let m1 = d.add_asset(Asset { link: Some("/m/1.png".into()), ..asset(5, b"") });+ let m2 = d.add_asset(Asset { link: Some("/m/2.png".into()), ..asset(6, b"") });+ let m3 = d.add_asset(asset(7, b""));+ let m4 = d.add_asset(asset(8, b""));+ assert_ne!(m1, m2);+ assert_ne!(m3, m4);+ }++ #[test]+ fn compaction_merges_duplicates_and_drops_unused_assets() {+ let mut d = Document::new(&Default::default());+ for (id, data) in [(1, b"one"), (2, b"one"), (3, b"two"), (4, b"old")] {+ d.assets.insert(AssetId(id), Arc::new(asset(id, data)));+ }+ let sp = Arc::make_mut(d.spreads.first_mut().unwrap());+ sp.items.push(Arc::new(graphic_item(10, 1, 1.0)));+ sp.items.push(Arc::new(graphic_item(11, 2, 2.0)));+ // A graphic cell and an anchored object in a story.+ let mut st = Story::new(StoryId(20));+ st.objects.push(Arc::new(crate::AnchoredObject::new(graphic_item(12, 3, 3.0), Default::default())));+ d.stories.insert(StoryId(20), Arc::new(st));+ assert_eq!(d.used_assets(), [1, 2, 3].map(AssetId).into_iter().collect());++ d.compact_assets();+ assert_eq!(d.assets.keys().copied().collect::<Vec<_>>(), vec![AssetId(1), AssetId(3)]);+ let crops: Vec<(AssetId, f64)> = d.spreads[0].items.iter().filter_map(|i| i.graphic().map(|g| (g.asset, g.crop[0]))).collect();+ assert_eq!(crops, vec![(AssetId(1), 1.0), (AssetId(1), 2.0)]);+ }+ }diff --cc crates/engine/src/cmd/datamerge/fill.rsindex 47087937b,89dd0c74e..c76b5a2f0--- a/crates/engine/src/cmd/datamerge/fill.rs+++ b/crates/engine/src/cmd/datamerge/fill.rs@@@ -355,30 -369,19 +369,27 @@@ fn place_image};let (nw, nh) = (f64::from(pw), f64::from(ph));let mode = fitting_of(&options.fitting);- let aid = AssetId(doc.alloc());+ let fresh = AssetId(doc.alloc());let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| field.to_string());let link = options.link_images.then(|| path.to_string_lossy().into_owned());- doc.assets.insert(- aid,- Arc::new(Asset {- id: aid,- name,- mime: designcraft_render::image_mime(&bytes).to_string(),- link,- data: Arc::new(bytes),- pixels: Some((pw, ph)),- ..Default::default()- }),- );+ let aid = doc.add_asset(Asset {+ id: fresh,+ name,+ mime: designcraft_render::image_mime(&bytes).to_string(),+ link,+ data: Arc::new(bytes),+ pixels: Some((pw, ph)),- page: 0,++ ..Default::default()+ });- let mut g = Graphic { asset: aid, size: (nw, nh), xf: Affine::translate((inner.x0, inner.y0)), auto_fit: mode, fit_align: 0, crop: [0.0; 4] };+ let mut g = Graphic {+ asset: aid,+ size: (nw, nh),+ xf: Affine::translate((inner.x0, inner.y0)),+ auto_fit: mode,+ fit_align: 0,+ crop: [0.0; 4],+ wrap: Default::default(),+ };if let Some(xf) = g.fitted(inner, mode) {g.xf = xf;}diff --cc crates/engine/src/cmd/file.rsindex 8003d182c,e7bc7e4ec..c1d5da72b--- a/crates/engine/src/cmd/file.rs+++ b/crates/engine/src/cmd/file.rs@@@ -452,8 -336,9 +452,8 @@@ fn file_place(s: &mut Session, p: &Valulet py = p.get("y").and_then(Value::as_f64);let want_w = p.get("width").and_then(Value::as_f64);s.edit(|d, sel| {- let aid = AssetId(d.alloc());- d.assets.insert(aid, Arc::new(Asset { id: aid, ..asset }));+ let fresh = AssetId(d.alloc());- let mime = designcraft_render::image_mime(&bytes).to_string();- let aid = d.add_asset(Asset { page: pdf_page, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)) });++ let aid = d.add_asset(Asset { id: fresh, ..asset });// The width the whole page (or image) gets; the height follows the proportions.let w = match want_w {Some(w) => w,@@@ -570,21 -441,9 +570,9 @@@ fn place_load(s: &mut Session, p: &Valulet (bytes, name, link) = read_source(p)?;let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad("place.load", "unsupported or corrupt image"))?;let aid = s.edit(|d, _| {- let aid = AssetId(d.alloc());+ let fresh = AssetId(d.alloc());let mime = designcraft_render::image_mime(&bytes).to_string();- d.assets.insert(- aid,- Arc::new(Asset {- id: aid,- name: name.clone(),- mime,- link: link.clone(),- data: Arc::new(bytes),- pixels: Some((pw, ph)),- ..Default::default()- }),- );- Ok(aid)- Ok(d.add_asset(Asset { page: 0, id: fresh, name: name.clone(), mime, link: link.clone(), data: Arc::new(bytes), pixels: Some((pw, ph)) }))++ Ok(d.add_asset(Asset { id: fresh, name: name.clone(), mime, link: link.clone(), data: Arc::new(bytes), pixels: Some((pw, ph)), ..Default::default() }))})?;s.loaded = Some((aid, (pw as f64, ph as f64)));s.set_tool("placeGun");diff --cc crates/engine/src/cmd/links.rsindex 5512ab44c,687cd7b1a..ba0c974a7--- a/crates/engine/src/cmd/links.rs+++ b/crates/engine/src/cmd/links.rs@@@ -315,3 -313,86 +317,85 @@@ mod relink_folder_testslet _ = std::fs::remove_dir_all(&dir);}}++ #[cfg(test)]+ mod shared_asset_tests {+ use super::*;++ fn png(w: u32, h: u32) -> Vec<u8> {+ designcraft_render::Rendered { width: w, height: h, pixels: vec![200u8; (w * h * 4) as usize] }.to_png()+ }++ #[test]+ fn placements_of_one_file_share_an_asset_and_saves_drop_replaced_ones() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let a = super::super::file::base64_encode(&png(10, 10));+ s.execute("file.place", &json!({"base64": a, "name": "a.png", "x": 72, "y": 72})).unwrap();+ s.execute("edit.deselectAll", &json!({})).unwrap();+ s.execute("file.place", &json!({"base64": a, "name": "a.png", "x": 200, "y": 72})).unwrap();+ let l = s.execute("links.list", &json!({})).unwrap();+ assert_eq!(l.as_array().unwrap().len(), 1);+ assert_eq!(l[0]["uses"].as_array().unwrap().len(), 2);+ assert_eq!(s.doc().unwrap().doc.assets.len(), 1);++ // A new image into a frame: the old asset stays in memory (undo) but not in the file.+ let f = s.execute("frame.create", &json!({"rect": [100, 300, 200, 400]})).unwrap()["id"].as_u64().unwrap();+ let b = super::super::file::base64_encode(&png(20, 10));+ let c = super::super::file::base64_encode(&png(30, 10));+ s.execute("file.place", &json!({"base64": b, "name": "b.png", "frame": f})).unwrap();+ s.execute("file.place", &json!({"base64": c, "name": "c.png", "frame": f})).unwrap();+ let d = &s.doc().unwrap().doc;+ assert_eq!(d.assets.len(), 3);+ assert_eq!(s.execute("links.list", &json!({})).unwrap().as_array().unwrap().len(), 2);+ let back = designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();+ let mut names: Vec<&str> = back.assets.values().map(|a| a.name.as_str()).collect();+ names.sort();+ assert_eq!(names, ["a.png", "c.png"]);+ }++ #[test]+ fn pasted_graphics_reuse_the_identical_asset_of_the_target() {+ use designcraft_doc::build::NewDocument;+ use designcraft_doc::{Asset, Graphic, Item, ItemId, Shape, SpreadRef};+ let asset = |id: u64, data: Vec<u8>| Asset {+ id: AssetId(id),+ name: "p.png".into(),+ mime: "image/png".into(),- link: None,+ data: Arc::new(data),- pixels: None,- page: 0,++ ..Default::default()+ };+ let mut src = Document::new(&NewDocument::default());+ let mut dst = Document::new(&NewDocument::default());+ let (pa, pb) = (png(4, 4), png(8, 4));+ // The source's asset id is taken in the target by a different file.+ let sid = AssetId(src.alloc());+ src.assets.insert(sid, Arc::new(asset(sid.0, pa.clone())));+ dst.assets.insert(sid, Arc::new(asset(sid.0, pb)));+ dst.alloc();+ let same = AssetId(dst.alloc());+ assert_ne!(same, sid);+ dst.assets.insert(same, Arc::new(asset(same.0, pa)));+ let iid = ItemId(src.alloc());+ let mut it = Item::new(+ iid,+ src.default_layer(),+ Shape::Rectangle,+ designcraft_geom::shapes::rectangle(designcraft_geom::Rect::new(0.0, 0.0, 9.0, 9.0)),+ );+ it.content = Content::Graphic(Graphic {+ asset: sid,+ size: (4.0, 4.0),+ xf: designcraft_geom::Affine::IDENTITY,+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [1.0; 4],++ wrap: Default::default(),+ });+ src.insert_item(SpreadRef::Doc(0), it, None).unwrap();+ let ids = super::super::object::duplicate_from(&mut dst, &src, &[iid], SpreadRef::Doc(0), designcraft_geom::Vec2::ZERO).unwrap();+ let g = dst.item(ids[0]).and_then(|i| i.graphic()).unwrap();+ assert_eq!((g.asset, g.crop), (same, [1.0; 4]));+ assert_eq!(dst.assets.len(), 2);+ }+ }diff --cc crates/engine/src/cmd/mathexpr.rsindex 9dd4df71c,ec90d52e3..dc1b6ed76--- a/crates/engine/src/cmd/mathexpr.rs+++ b/crates/engine/src/cmd/mathexpr.rs@@@ -13,9 -13,16 +13,8 @@@ use crate::{Result, Session}fn math_item(d: &mut designcraft_doc::Document, latex: &str, size: f64, lid: designcraft_doc::LayerId, at: (f64, f64)) -> Item {let (svg, (w, h)) = crate::math::to_svg(latex, size);- let aid = AssetId(d.alloc());- d.assets- .insert(aid, Arc::new(Asset { id: aid, name: "math.svg".into(), mime: "image/svg+xml".into(), data: Arc::new(svg), ..Default::default() }));+ let fresh = AssetId(d.alloc());- let aid = d.add_asset(Asset {- page: 0,- id: fresh,- name: "math.svg".into(),- mime: "image/svg+xml".into(),- link: None,- data: Arc::new(svg),- pixels: None,- });++ let aid = d.add_asset(Asset { id: fresh, name: "math.svg".into(), mime: "image/svg+xml".into(), data: Arc::new(svg), ..Default::default() });let id = ItemId(d.alloc());let r = Rect::new(at.0, at.1, at.0 + w, at.1 + h);let mut it = Item::new(id, lid, Shape::Rectangle, shapes::rectangle(r));diff --cc crates/engine/src/cmd/media.rsindex 87d292b4e,b6e4cd4b8..640345f41--- a/crates/engine/src/cmd/media.rs+++ b/crates/engine/src/cmd/media.rs@@@ -25,8 -25,8 +25,8 @@@ pub(crate) fn place_media(s: &mut Sessilet (px, py) = (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64));let mime = mime.to_string();s.edit(|d, sel| {- let aid = AssetId(d.alloc());- d.assets.insert(aid, Arc::new(Asset { id: aid, name, mime, link, data: Arc::new(bytes), ..Default::default() }));+ let fresh = AssetId(d.alloc());- let aid = d.add_asset(Asset { page: 0, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: None });++ let aid = d.add_asset(Asset { id: fresh, name, mime, link, data: Arc::new(bytes), ..Default::default() });let id = match target {Some(fid) => {let it = d.item_mut(fid).ok_or(designcraft_doc::DocError::NoItem(fid))?;@@@ -92,13 -90,9 +92,9 @@@ pub fn specs() -> Vec<CommandSpec>s.edit(|d, _| {let poster_id = match poster {Some(Some((bytes, name, link, px))) => {- let aid = AssetId(d.alloc());+ let fresh = AssetId(d.alloc());let mime = designcraft_render::image_mime(&bytes).to_string();- d.assets.insert(- aid,- Arc::new(Asset { id: aid, name, mime, link, data: Arc::new(bytes), pixels: Some(px), ..Default::default() }),- );- Some(Some(aid))- Some(Some(d.add_asset(Asset { page: 0, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: Some(px) })))++ Some(Some(d.add_asset(Asset { id: fresh, name, mime, link, data: Arc::new(bytes), pixels: Some(px), ..Default::default() })))}Some(None) => Some(None),None => None,diff --cc crates/engine/src/cmd/table.rsindex c7bd83a19,fafece0df..eb67156a7--- a/crates/engine/src/cmd/table.rs+++ b/crates/engine/src/cmd/table.rs@@@ -1612,20 -1402,10 +1612,17 @@@ fn place_graphic(s: &mut Session, p: &Vlet (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad(ID, "unsupported or corrupt image"))?;let fill = p.get("fit").and_then(Value::as_str) == Some("fill");s.edit(|d, sel| {- let aid = designcraft_doc::AssetId(d.alloc());+ let fresh = designcraft_doc::AssetId(d.alloc());let mime = designcraft_render::image_mime(&bytes).to_string();- d.assets.insert(- aid,- std::sync::Arc::new(designcraft_doc::Asset {- id: aid,- name,- mime,- link,- data: std::sync::Arc::new(bytes),- pixels: Some((pw, ph)),- ..Default::default()- }),- );- let aid =- d.add_asset(designcraft_doc::Asset { page: 0, id: fresh, name, mime, link, data: std::sync::Arc::new(bytes), pixels: Some((pw, ph)) });++ let aid = d.add_asset(designcraft_doc::Asset {++ id: fresh,++ name,++ mime,++ link,++ data: std::sync::Arc::new(bytes),++ pixels: Some((pw, ph)),++ ..Default::default()++ });let st = d.story_mut(g.story).ok_or_else(|| bad(ID, "no story"))?;let t = st.table_mut(g.table).ok_or_else(|| bad(ID, "no table"))?;let (r, c) = t.owner(g.range.r0, g.range.c0);diff --cc crates/format/src/lib.rsindex 9353ea68e,2de45402d..628ce7c5c--- a/crates/format/src/lib.rs+++ b/crates/format/src/lib.rs@@@ -2,11 -2,11 +2,12 @@@//!//! - `mimetype` (stored first, uncompressed): `application/vnd.designcraft+zip`//! - `document.json`: the [`Document`] (pretty JSON, documented by the serde model in `designcraft-doc`)- //! - `assets/<id>.<ext>`: embedded placed files, byte-for-byte+ //! - `assets/<id>.<ext>`: embedded placed files, byte-for-byte: one per distinct file, and only+ //! the ones the document references (see [`Document::compact_assets`])//! - `meta.json`: format version and generator//!-//! Older single-file JSON documents (assets as base64 in `assetData`) still open.+//! Older single-file JSON documents (assets as base64 in `assetData`) still open. Documents+//! from older format versions are upgraded on load so they lay out as they did.#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]#![forbid(unsafe_code)]@@@ -107,17 -102,29 +112,30 @@@ fn load_zip(bytes: &[u8]) -> Result<Doc}let json = read(&mut z, "document.json").ok_or_else(|| FormatError::NotOurs("missing document.json".into()))?;let mut v: Value = serde_json::from_slice(&json).map_err(|e| FormatError::NotOurs(e.to_string()))?;- upgrade(&mut v);+ upgrade_json(&mut v);let mut doc: Document = serde_json::from_value(v).map_err(|e| FormatError::NotOurs(e.to_string()))?;let names: Vec<String> = z.file_names().map(str::to_string).collect();+ // Files saved with one copy per placement: identical copies share one buffer as they are+ // read, so memory holds each distinct file once.+ let mut loaded: HashMap<usize, Vec<Arc<Vec<u8>>>> = HashMap::new();for name in names {let Some(stem) = name.strip_prefix("assets/") else { continue };let Some(id) = stem.split('.').next().and_then(|s| s.parse::<u64>().ok()) else { continue };if let (Some(data), Some(a)) = (read(&mut z, &name), doc.assets.get_mut(&AssetId(id))) {- Arc::make_mut(a).data = Arc::new(data);+ let same = loaded.entry(data.len()).or_default();+ let data = match same.iter().find(|d| ***d == data) {+ Some(d) => d.clone(),+ None => {+ let d = Arc::new(data);+ same.push(d.clone());+ d+ }+ };+ Arc::make_mut(a).data = data;}}+ upgrade(&mut doc, version);+ doc.merge_duplicate_assets();doc.check().map_err(|e| FormatError::NotOurs(e.to_string()))?;Ok(doc)}@@@ -182,7 -142,7 +200,8 @@@ fn load_legacy_json(bytes: &[u8]) -> Re}}}+ upgrade(&mut doc, 1);+ doc.merge_duplicate_assets();doc.check().map_err(|e| FormatError::NotOurs(e.to_string()))?;Ok(doc)}@@@ -264,6 -224,60 +283,60 @@@ mod testsuse designcraft_doc::geom::Rect;use designcraft_doc::{Asset, ParaFormat, SpreadRef};+ fn png_asset(d: &mut Document, data: &[u8]) -> AssetId {+ let aid = AssetId(d.alloc());+ let a = Asset {- page: 0,+ id: aid,+ name: "x.png".into(),+ mime: "image/png".into(),- link: None,+ data: Arc::new(data.to_vec()),+ pixels: Some((1, 1)),++ ..Default::default()+ };+ d.assets.insert(aid, Arc::new(a));+ aid+ }++ fn place(d: &mut Document, asset: AssetId, crop: f64) -> designcraft_doc::ItemId {+ use designcraft_doc::geom::{Affine, shapes};+ use designcraft_doc::{Content, Graphic, Item, ItemId, Shape};+ let id = ItemId(d.alloc());+ let mut it = Item::new(id, d.default_layer(), Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 50.0, 50.0)));+ it.content = Content::Graphic(Graphic {+ asset,+ size: (1.0, 1.0),+ xf: Affine::translate((crop, crop)),+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [crop; 4],++ wrap: Default::default(),+ });+ d.insert_item(SpreadRef::Doc(0), it, None).unwrap();+ id+ }++ fn asset_entries(bytes: &[u8]) -> usize {+ let z = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();+ z.file_names().filter(|n| n.starts_with("assets/")).count()+ }++ /// Zip `d` the way files were saved with one asset per placement (nothing merged or pruned).+ fn save_uncompacted(d: &Document) -> Vec<u8> {+ let mut buf = Cursor::new(Vec::new());+ let mut z = zip::ZipWriter::new(&mut buf);+ let opts = zip::write::SimpleFileOptions::default();+ z.start_file("mimetype", opts).unwrap();+ z.write_all(MIME.as_bytes()).unwrap();+ z.start_file("document.json", opts).unwrap();+ z.write_all(&serde_json::to_vec(d).unwrap()).unwrap();+ for (id, a) in &d.assets {+ z.start_file(format!("assets/{}.png", id.0), opts).unwrap();+ z.write_all(&a.data).unwrap();+ }+ z.finish().unwrap();+ buf.into_inner()+ }+#[test]fn roundtrip_with_assets() {let mut d = Document::new(&NewDocument { pages: 3, ..Default::default() });@@@ -289,122 -293,42 +352,158 @@@assert_eq!(back.stories, d.stories);}+ /// `doc` as format 1 wrote it.+ fn save_v1(doc: &Document) -> Vec<u8> {+ let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));+ z.start_file("meta.json", zip::write::SimpleFileOptions::default()).unwrap();+ z.write_all(br#"{"format": "designcraft", "version": 1}"#).unwrap();+ z.start_file("document.json", zip::write::SimpleFileOptions::default()).unwrap();+ z.write_all(&serde_json::to_vec(doc).unwrap()).unwrap();+ z.finish().unwrap().into_inner()+ }++ fn para_style(name: &str, based_on: Option<&str>, keep_all_lines: Option<bool>) -> designcraft_doc::ParagraphStyle {+ designcraft_doc::ParagraphStyle {+ name: name.into(),+ based_on: based_on.map(str::to_string),+ next_style: None,+ para: designcraft_doc::ParaAttrs { keep_lines_together: Some(true), keep_all_lines, ..Default::default() },+ chars: Default::default(),+ shortcut: String::new(),+ }+ }++ /// Format 1 resolved an unset Keep Lines Together mode as all lines: those documents still+ /// do after loading, wherever the mode comes from. New documents keep lines at start/end.+ #[test]+ fn format_1_keep_lines_without_mode_stays_all_lines() {+ let mut d = Document::new(&NewDocument::default());+ let st = d.styles_mut();+ st.paragraph.push(para_style("Root", None, None));+ st.paragraph.push(para_style("Child", Some("Root"), None));+ st.paragraph.push(para_style("Explicit", None, Some(false)));+ st.paragraph.push(para_style("Under explicit", Some("Explicit"), None));+ st.paragraph.push(para_style("Loop A", Some("Loop B"), None));+ st.paragraph.push(para_style("Loop B", Some("Loop A"), None));+ let lid = d.default_layer();+ let names = [designcraft_doc::story::BASIC_PARAGRAPH, "Root", "Child", "Explicit", "Under explicit", "Loop A", "Loop B", "Missing"];+ let text = names.join("\n");+ let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 200.0, 200.0), lid, &text, ParaFormat::default()).unwrap();+ {+ let s = d.story_mut(sid).unwrap();+ for (p, name) in s.paras.iter_mut().zip(names) {+ p.style = name.into();+ p.para.keep_lines_together = Some(true);+ }+ s.insert_note(1, "Note", ParaFormat { style: "Missing".into(), ..Default::default() });+ }+ let back = load(&save_v1(&d)).unwrap();+ let resolve = |f: &ParaFormat| back.styles.resolve_para(f).0;+ let story = back.story(sid).unwrap();+ for (p, name) in story.paras.iter().zip(names) {+ let pp = resolve(p);+ assert!(pp.keep_lines_together);+ assert_eq!(pp.keep_all_lines, !name.contains("xplicit"), "{name}");+ }+ assert!(resolve(&story.notes[0].text.paras[0]).keep_all_lines, "footnote with a missing style");+ assert!(back.styles.resolve_para_style("Loop A").0.keep_all_lines && back.styles.resolve_para_style("Loop B").0.keep_all_lines);+ // Pinned where the chains start; explicit and inheriting styles are left as they were.+ assert_eq!(back.styles.para("Root").unwrap().para.keep_all_lines, Some(true));+ assert_eq!(back.styles.para("Child").unwrap().para.keep_all_lines, None);+ assert_eq!(back.styles.para("Under explicit").unwrap().para.keep_all_lines, None);+ // The current format keeps the new default: at start/end of paragraph.+ let now = load(&save(&d).unwrap()).unwrap();+ let p = &now.story(sid).unwrap().paras[0];+ assert!(!now.styles.resolve_para(p).0.keep_all_lines);+ assert!(!designcraft_doc::ParaProps::default().keep_all_lines);+ }++ /// Format 1 wrote paragraph rules, text frame options and numbered lists in full, so their+ /// old defaults (black rules, auto-size from the top centre, lists continuing across stories)+ /// load as saved. New documents take InDesign's.+ #[test]+ fn format_1_rules_frames_and_lists_load_as_saved() {+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ let old_rule = designcraft_doc::Rule { on: true, color: designcraft_doc::color::swatch::BLACK.into(), ..Default::default() };+ let (fid, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 200.0, 200.0), lid, "Ruled", ParaFormat::default()).unwrap();+ d.story_mut(sid).unwrap().paras[0].para.rule_above = Some(old_rule.clone());+ d.item_mut(fid).unwrap().text_frame_mut().unwrap().options.auto_size_ref = 1;+ d.settings.lists.push(designcraft_doc::NumberedList { name: "Steps".into(), continue_across_stories: true });+ let back = load(&save_v1(&d)).unwrap();+ assert_eq!(back.story(sid).unwrap().paras[0].para.rule_above, Some(old_rule));+ assert_eq!(back.item(fid).unwrap().text_frame().unwrap().options.auto_size_ref, 1);+ assert!(back.settings.lists[0].continue_across_stories);+ assert_eq!(designcraft_doc::Rule::default().color, designcraft_doc::TEXT_COLOR);+ assert_eq!(designcraft_doc::TextFrameOptions::default().auto_size_ref, 4);+ assert!(!designcraft_doc::NumberedList::default().continue_across_stories);+ }++ /// Format 1 set drop caps without Align Left Edge: those documents still do where nothing set+ /// it. New documents align the drop cap's left edge.+ #[test]+ fn format_1_drop_caps_stay_unaligned() {+ let mut d = Document::new(&NewDocument::default());+ let st = d.styles_mut();+ st.paragraph.push(para_style("Opener", None, None));+ st.paragraph.push(designcraft_doc::ParagraphStyle {+ para: designcraft_doc::ParaAttrs { drop_cap_align_left: Some(true), ..Default::default() },+ ..para_style("Aligned", None, None)+ });+ let lid = d.default_layer();+ let (_, sid) =+ d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 200.0, 200.0), lid, "One\nTwo\nThree", ParaFormat::default()).unwrap();+ {+ let s = d.story_mut(sid).unwrap();+ for (p, name) in s.paras.iter_mut().zip(["Opener", "Aligned", "Missing"]) {+ p.style = name.into();+ p.para.drop_cap_lines = Some(2);+ p.para.drop_cap_chars = Some(1);+ }+ }+ let back = load(&save_v1(&d)).unwrap();+ let aligned: Vec<bool> = back.story(sid).unwrap().paras.iter().map(|p| back.styles.resolve_para(p).0.drop_cap_align_left).collect();+ assert_eq!(aligned, [false, true, false]);+ let now = load(&save(&d).unwrap()).unwrap();+ assert!(now.styles.resolve_para(&now.story(sid).unwrap().paras[0]).0.drop_cap_align_left);+ }++ #[test]+ fn saves_hold_each_used_file_once() {+ let mut d = Document::new(&NewDocument::default());+ let first = png_asset(&mut d, &[7; 64]);+ let copy = png_asset(&mut d, &[7; 64]);+ let replaced = png_asset(&mut d, &[9; 64]);+ let a = place(&mut d, first, 1.0);+ let b = place(&mut d, copy, 2.0);+ assert_eq!(d.assets.len(), 3);++ // Duplicates merge into the first asset, the unreferenced one is left out, and every+ // placement keeps its own crop and transform.+ let bytes = save(&d).unwrap();+ assert_eq!(asset_entries(&bytes), 1);+ assert_eq!(d.assets.len(), 3, "the open document keeps its assets");+ let back = load(&bytes).unwrap();+ assert_eq!(back.assets.keys().copied().collect::<Vec<_>>(), vec![first]);+ assert!(!back.assets.contains_key(&replaced));+ for (id, crop) in [(a, 1.0), (b, 2.0)] {+ let g = back.item(id).and_then(|i| i.graphic()).unwrap();+ assert_eq!((g.asset, g.crop, g.xf), (first, [crop; 4], d.item(id).unwrap().graphic().unwrap().xf));+ }+ }++ #[test]+ fn older_files_with_one_asset_per_placement_open_with_one() {+ let mut d = Document::new(&NewDocument::default());+ let ids: Vec<AssetId> = (0..3).map(|_| png_asset(&mut d, &[5; 32])).collect();+ for (k, id) in ids.iter().enumerate() {+ place(&mut d, *id, k as f64);+ }+ let back = load(&save_uncompacted(&d)).unwrap();+ assert_eq!(back.assets.len(), 1);+ assert_eq!(asset_entries(&save(&back).unwrap()), 1);+ }+/// The object of the item `id` in a document's JSON.fn item_json(v: &mut Value, id: u64) -> Option<&mut Value> {if v.get("id").and_then(Value::as_u64) == Some(id) && v.get("content").is_some() {diff --cc crates/idml/src/import.rsindex f35baae85,138775268..424870fae--- a/crates/idml/src/import.rs+++ b/crates/idml/src/import.rs@@@ -244,6 -132,8 +244,9 @@@ struct Importer<'r>page_index: HashMap<String, usize>,item_ids: HashMap<String, ItemId>,assets: BTreeMap<AssetId, Arc<Asset>>,- /// Assets read from a link path, so later placements of that path reuse them unread.- linked_assets: HashMap<String, AssetId>,++ /// Assets read from a link path (with the PDF page and crop box shown), so later placements++ /// of that path reuse them unread.++ linked_assets: HashMap<(String, u32, designcraft_doc::PdfCrop), AssetId>,ctx: ItemCtx,sections: Vec<Section>,footnote_options: designcraft_doc::FootnoteOptions,@@@ -2602,16 -2206,31 +2606,39 @@@ impl<'r> Importer<'r>None => (0.0, 0.0, 0.0, 0.0),};let size = ((r - l).abs(), (b - t).abs());+ let wrap = if g.get("AppliedObjectStyle").is_some() { self.with_style(g, "TextWrapPreference") } else { g.find("TextWrapPreference").cloned() };+ let wrap = wrap.map(|w| text_wrap(&w)).unwrap_or_default();let link = g.find("Link");let uri = link.and_then(|k| k.get("LinkResourceURI")).map(uri_to_path);++ let page = pdf_page_in(g);++ // Where the box sits on the page needs the PDF parsed: the engine finds it after import.++ let pdf_crop = g.find("PDFAttribute").and_then(|a| a.get("PDFCrop")).map_or(designcraft_doc::PdfCrop::Crop, names::pdf_crop_in);+ let graphic = |asset| {+ Content::Graphic(designcraft_doc::Graphic {+ asset,+ size,+ xf: gxf * Affine::translate((l, t)),+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [0.0; 4],++ // A wrap set on the placed graphic (Direct Selection) is stored on the graphic element,++ // over the graphic's own object style when it names one.++ wrap,+ })+ };let mut data = g.prop_el("Contents").map(|c| base64_decode(&c.text_content())).unwrap_or_default();+ let mut read_from = None;if data.is_empty()&& let Some(p) = &uri- && let Some(d) = (self.read_link)(p){- data = d;+ // Every placement of a linked file shares one asset; read the file once.- if let Some(id) = self.linked_assets.get(p) {++ if let Some(id) = self.linked_assets.get(&(p.clone(), page, pdf_crop)) {+ return graphic(*id);+ }+ if let Some(d) = (self.read_link)(p) {+ data = d;+ read_from = Some(p.clone());+ }}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("") {@@@ -2627,24 -2246,14 +2654,16 @@@});let name =uri.as_deref().and_then(|p| p.rsplit(['/', '\\']).next()).filter(|n| !n.is_empty()).map(str::to_string).unwrap_or_else(|| "image".into());- let id = AssetId(self.alloc());+ let fresh = AssetId(self.alloc());let link_path = uri.filter(|p| p.contains('/') || p.contains('\\'));- let page = pdf_page_in(g);- // Where the box sits on the page needs the PDF parsed: the engine finds it after import.- let pdf_crop = g.find("PDFAttribute").and_then(|a| a.get("PDFCrop")).map_or(designcraft_doc::PdfCrop::Crop, names::pdf_crop_in);- self.assets- .insert(id, Arc::new(Asset { page, id, name, mime, link: link_path, data: Arc::new(data), pixels, pdf_crop, ..Default::default() }));- Content::Graphic(designcraft_doc::Graphic {- asset: id,- size,- xf: gxf * Affine::translate((l, t)),- auto_fit: Default::default(),- fit_align: 4,- crop: [0.0; 4],- // A wrap set on the placed graphic (Direct Selection) is stored on the graphic element, over- // the graphic's own object style when it names one.- wrap,- })- let id =- designcraft_doc::insert_asset(&mut self.assets, Asset { page: 0, id: fresh, name, mime, link: link_path, data: Arc::new(data), pixels });++ let id = designcraft_doc::insert_asset(++ &mut self.assets,++ Asset { page, id: fresh, name, mime, link: link_path, data: Arc::new(data), pixels, pdf_crop, ..Default::default() },++ );+ if let Some(p) = read_from {- self.linked_assets.insert(p, id);++ self.linked_assets.insert((p, page, pdf_crop), id);+ }+ graphic(id)}fn link_threads(&mut self) {diff --cc crates/idml/src/tests.rsindex f7c4bcc10,4c7817009..f03682f98--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@@ -7,11 -7,8 +7,12 @@@ use designcraft_geom::{Rect, shapes}use super::*;+ mod assets;mod decorations;+mod rtl_binding;+mod variables;+mod wrap;+mod object_styles;fn zip_files(files: &[(&str, &str)]) -> Vec<u8> {use zip::write::SimpleFileOptions;