From 7189295bc3b1ed95c76a067edcd2fc52ae9bbc99 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Fri, 20 Nov 2026 09:02:40 -0700 Subject: [PATCH] Stack r1: #1280: each IMAGE keeps its own IMAGEDEF when a repeated handle is reassigned --- crates/io/src/dxf_image.rs | 18 ++------ crates/io/src/dxf_read.rs | 4 +- crates/io/src/dxf_read/entities.rs | 7 ++++ .../tests/it/passthrough_image_roundtrip.rs | 42 +++++++++++++++++++ 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/crates/io/src/dxf_image.rs b/crates/io/src/dxf_image.rs index cc91c7e42..fa6a1d842 100644 --- a/crates/io/src/dxf_image.rs +++ b/crates/io/src/dxf_image.rs @@ -79,24 +79,12 @@ fn has_images(store: &EntityStore) -> bool { } /// Fill in each image's definition (path, name, pixel size) from the IMAGEDEF it points to. -pub(crate) fn resolve(secs: &[SectionRef<'_>], d: &mut Drawing) { +/// `refs`: each IMAGE entity's handle in the drawing → its IMAGEDEF handle (upper case), noted +/// as the entity was read, so an image whose repeated handle was reassigned keeps its own. +pub(crate) fn resolve(secs: &[SectionRef<'_>], d: &mut Drawing, refs: &HashMap) { if !has_images(&d.model) && !d.layouts.iter().any(|l| has_images(&l.entities)) && !d.blocks.values().any(|b| has_images(&b.entities)) { return; } - // IMAGE handle → IMAGEDEF handle. - let mut refs: HashMap = HashMap::new(); - for s in secs.iter().filter(|s| s.name == "ENTITIES" || s.name == "BLOCKS") { - each_record(s.tags, |kind, tags| { - if kind != "IMAGE" { - return; - } - let h = tags.iter().find(|t| t.code == 5).and_then(|t| Handle::parse_hex(&t.str())); - let def = tags.iter().find(|t| t.code == 340).map(|t| key(&t.str())); - if let (Some(h), Some(def)) = (h, def) { - refs.insert(h, def); - } - }); - } // IMAGEDEF objects, and the names dictionaries give them. let mut defs: HashMap = HashMap::new(); let mut names: HashMap = HashMap::new(); diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 3f2bd34da..7130ab000 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -71,6 +71,8 @@ struct Rx { block: Option, /// The points HATCH boundary curves may still be sampled into. curves: hatch::CurvePoints, + /// IMAGE entity handle, as read (a repeated one reassigned) → its IMAGEDEF handle (upper case). + image_defs: HashMap, } /// Read a DXF file into a drawing. @@ -186,7 +188,7 @@ fn read_budgeted(bytes: &[u8], mut table_cells_left: usize, curve_points: usize) number_unknown_viewport_ids(&mut d, &dropped_paper_views(&rx, &paper_block_for, &first_layout)); apply_sortents(&mut d, &objs.sortents, &rx.brs, &paper_block_for, &first_layout); objs.apply(&mut d, &rx); - crate::dxf_image::resolve(&secs, &mut d); + crate::dxf_image::resolve(&secs, &mut d, &rx.image_defs); crate::dxf_mline::resolve(&secs, &mut d); // Keep handles unique against the header's seed. let seed = d.header.str("HANDSEED", ""); diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index 1801a125d..899b20430 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -515,6 +515,13 @@ pub(super) fn entity_extras(kind: &str, tags: &[Tag], h: Handle, k: &mut EntityK rx.table_style_fix.push((h, t.s(342).map(|sh| sh.trim().to_ascii_uppercase()).unwrap_or_default())); } } + // Keyed by the handle the entity was given, so an IMAGE whose repeated handle was + // reassigned still finds its own IMAGEDEF. + ("IMAGE", EntityKind::Image(_)) => { + if let Some(def) = tags.iter().find(|t| t.code == 340) { + rx.image_defs.insert(h, def.str().trim().to_ascii_uppercase()); + } + } ("INSERT", EntityKind::Insert(ins)) => { rx.inserted.insert(ins.block.to_ascii_uppercase()); } diff --git a/crates/io/tests/it/passthrough_image_roundtrip.rs b/crates/io/tests/it/passthrough_image_roundtrip.rs index 285af34db..f78b884d6 100644 --- a/crates/io/tests/it/passthrough_image_roundtrip.rs +++ b/crates/io/tests/it/passthrough_image_roundtrip.rs @@ -204,3 +204,45 @@ fn image_survives_dwg_save_and_reopen() { // The DWG bridge keeps everything but the definition's resolution unit. assert_eq!(Image { resolution_units: img.resolution_units, ..b }, img); } + +#[test] +fn images_repeating_a_handle_each_keep_their_own_definition() { + // Two IMAGE records share handle 31 and point at different IMAGEDEFs. The second is given a + // fresh handle on reading; each must still show its own file. + let image = |def: &str| -> String { + let groups: &[(i32, &str)] = &[ + (0, "IMAGE"), + (5, "31"), + (100, "AcDbEntity"), + (8, "0"), + (100, "AcDbRasterImage"), + (10, "0"), + (20, "0"), + (13, "10"), + (23, "10"), + (340, def), + ]; + groups.iter().map(|(c, v)| format!("{c}\n{v}\n")).collect() + }; + let imagedef = |h: &str, path: &str| format!("0\nIMAGEDEF\n5\n{h}\n100\nAcDbRasterImageDef\n1\n{path}\n"); + let dxf = format!( + "0\nSECTION\n2\nENTITIES\n{}{}0\nENDSEC\n0\nSECTION\n2\nOBJECTS\n{}{}0\nENDSEC\n0\nEOF\n", + image("40"), + image("43"), + imagedef("40", "first.png"), + imagedef("43", "second.png") + ); + let d = cadcraft_io::read_dxf(dxf.as_bytes()).unwrap(); + let mut paths: Vec<(u64, String)> = d + .model + .iter() + .filter_map(|e| match &e.kind { + EntityKind::Image(i) => Some((e.handle.0, i.path.clone())), + _ => None, + }) + .collect(); + paths.sort(); + assert_eq!(paths.len(), 2, "{paths:?}"); + assert_eq!(paths.first(), Some(&(0x31, "first.png".to_string())), "{paths:?}"); + assert_eq!(paths.get(1).map(|p| p.1.as_str()), Some("second.png"), "{paths:?}"); +}