Stack r1: #1280: each IMAGE keeps its own IMAGEDEF when a repeated handle is reassigned

This commit is contained in:
Tanish Manku
2026-11-20 09:06:55 -07:00
parent 0191a5fa61
commit 7189295bc3
4 changed files with 55 additions and 16 deletions
+3 -15
View File
@@ -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<Handle, String>) {
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<Handle, String> = 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<String, Def> = HashMap::new();
let mut names: HashMap<String, String> = HashMap::new();
+3 -1
View File
@@ -71,6 +71,8 @@ struct Rx {
block: Option<String>,
/// 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<Handle, String>,
}
/// 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", "");
+7
View File
@@ -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());
}
@@ -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:?}");
}