From 5d4cf19621ab11d52d59ca5feb6e5a601c7fca1a Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Sat, 14 Nov 2026 00:03:09 +0000 Subject: [PATCH 1/2] fix(dwg): custom table cell styles survive a DWG save (#3132) acadrust keeps a TABLESTYLE's xdata from DXF only in its raw groups, which its DWG writer ignores, and writes a table style read from DWG to DXF without its object xdata. The bridge now makes the CADCRAFT xdata strings the style's object xdata before writing DWG, and adds them back after the style's groups in the DXF made from a DWG. --- crates/dwg/src/lib.rs | 123 +++++++++++++++++++++++- crates/io/tests/dwg_table_cellstyles.rs | 60 ++++++++++++ 2 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 crates/io/tests/dwg_table_cellstyles.rs diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 4d1eee2790..11bfb18e60 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -124,7 +124,8 @@ mod native { let mut out = CappedWriter { buf: Vec::new(), limit: limits.max_dxf_bytes, exceeded: false }; let written = std::panic::catch_unwind(AssertUnwindSafe(|| { acadrust::DxfWriter::new(&doc).write_to_writer(&mut out)?; - mtext_contents(&doc, &mut out) + mtext_contents(&doc, &mut out)?; + table_style_xdata_to_dxf(&doc, &mut out) })) .map_err(|_| "the DWG→DXF conversion failed on this file".to_string())?; if out.exceeded { @@ -139,6 +140,125 @@ mod native { Ok((add_header_vars(out.buf, &doc.header), notes)) } + /// The xdata application CADCraft keeps its own data under (DXF APPID `CADCRAFT`). + const CADCRAFT_APP: &str = "CADCRAFT"; + + /// CADCraft's xdata strings on table styles (custom cell styles, #3132). acadrust 0.6.6's DXF + /// reader keeps a TABLESTYLE's xdata only in its raw groups, which its DWG writer ignores, so + /// before a DWG is written the strings after `1001 CADCRAFT` are made the style's object xdata. + fn table_style_xdata_from_dxf(doc: &mut acadrust::CadDocument) { + use acadrust::objects::ObjectType; + use acadrust::xdata::{ExtendedDataRecord, XDataValue}; + let mut records = Vec::new(); + for o in doc.objects.values() { + let ObjectType::TableStyle(s) = o else { continue }; + let Some(codes) = &s.raw_dxf_codes else { continue }; + let mut ours = false; + let mut record = ExtendedDataRecord::new(CADCRAFT_APP); + for (code, value) in codes { + match code { + 1001 => ours = value.trim() == CADCRAFT_APP, + 1000 if ours => record.add_value(XDataValue::String(value.clone())), + _ => {} + } + } + if !record.is_empty() { + records.push((s.handle, record)); + } + } + for (handle, record) in records { + doc.set_object_xdata_record(handle, record); + } + } + + /// The other half of [`table_style_xdata_from_dxf`]: acadrust 0.6.6 reads a TABLESTYLE's + /// xdata from DWG into the object xdata, but its DXF writer writes a table style's xdata only + /// from raw groups, which a style read from DWG has none of. The CADCraft strings are added + /// after the style's groups in the DXF it wrote; every other group stays byte-for-byte. + fn table_style_xdata_to_dxf(doc: &acadrust::CadDocument, out: &mut CappedWriter) -> acadrust::Result<()> { + use acadrust::io::dxf::{DxfStreamWriter, DxfTextWriter}; + use acadrust::objects::ObjectType; + use acadrust::xdata::XDataValue; + let strings: std::collections::HashMap> = doc + .objects + .iter() + .filter(|(_, o)| matches!(o, ObjectType::TableStyle(_))) + .filter_map(|(h, _)| { + let record = doc.object_xdata.get(h)?.get_record(CADCRAFT_APP)?; + let values: Vec<&str> = record + .values + .iter() + .filter_map(|v| match v { + XDataValue::String(s) => Some(s.as_str()), + _ => None, + }) + .collect(); + (!values.is_empty()).then_some((h.value(), values)) + }) + .collect(); + if strings.is_empty() { + return Ok(()); + } + let dxf = std::mem::take(&mut out.buf); + let mut lines = dxf.split_inclusive(|b| *b == b'\n'); + let (mut style, mut pending) = (false, None); + while let Some(code) = lines.next() { + let value = lines.next().ok_or_else(|| std::io::Error::other("incomplete DWG bridge DXF pair"))?; + match code.trim_ascii() { + b"0" => { + if let Some(values) = pending.take() { + let mut writer = DxfTextWriter::new(&mut *out); + if doc.version < acadrust::DxfVersion::AC1021 + && let Some(encoding) = acadrust::io::dxf::code_page::encoding_from_code_page(&doc.header.code_page) + { + writer.set_encoding(encoding); + } + writer.write_string(1001, CADCRAFT_APP)?; + for &v in values { + writer.write_xrecord_string(1000, v)?; + } + } + style = value.trim_ascii() == b"TABLESTYLE"; + } + b"5" if style => { + pending = + std::str::from_utf8(value.trim_ascii()).ok().and_then(|s| u64::from_str_radix(s, 16).ok()).and_then(|h| strings.get(&h)); + style = false; + } + _ => {} + } + out.write_all(code)?; + out.write_all(value)?; + } + Ok(()) + } + + #[cfg(test)] + #[test] + fn table_style_xdata_goes_after_the_styles_groups_only() -> acadrust::Result<()> { + use acadrust::objects::{ObjectType, TableStyle}; + use acadrust::xdata::{ExtendedDataRecord, XDataValue}; + let mut doc = acadrust::CadDocument::new(); + let mut style = TableStyle::new("Costs"); + style.handle = acadrust::Handle::new(0x2A); + doc.objects.insert(style.handle, ObjectType::TableStyle(style)); + let mut record = ExtendedDataRecord::new(CADCRAFT_APP); + record.add_value(XDataValue::String("CELLSTYLES".into())); + record.add_value(XDataValue::String("{\"v\":1}".into())); + doc.set_object_xdata_record(acadrust::Handle::new(0x2A), record); + // Another style (5 2B) and the style's own groups are left as they are. + let style = b" 0\r\nTABLESTYLE\r\n 5\r\n2A\r\n100\r\nAcDbTableStyle\r\n"; + let rest = b" 0\r\nTABLESTYLE\r\n 5\r\n2B\r\n 0\r\nENDSEC\r\n"; + let mut out = CappedWriter { buf: [style.as_slice(), rest].concat(), limit: 4096, exceeded: false }; + table_style_xdata_to_dxf(&doc, &mut out)?; + let xdata = b"1001\r\nCADCRAFT\r\n1000\r\nCELLSTYLES\r\n1000\r\n{\"v\":1}\r\n"; + assert_eq!(out.buf, [style.as_slice(), xdata, rest].concat()); + let mut limited = CappedWriter { buf: [style.as_slice(), rest].concat(), limit: 1, exceeded: false }; + assert!(table_style_xdata_to_dxf(&doc, &mut limited).is_err()); + assert!(limited.exceeded); + Ok(()) + } + /// acadrust 0.6.6's ASCII writer treats a caret followed by a literal space as already /// escaped, and turns line feeds into paragraph codes. Serialize affected MTEXT contents /// from the native values instead, using its lossless string writer. All other groups, @@ -1226,6 +1346,7 @@ mod native { }; other_layout_owners(&mut doc); table_style_rows(&mut doc, true); + table_style_xdata_from_dxf(&mut doc); dimension_flags(&mut doc); let mut unwritten = drop_unwritable(&mut doc); for (kind, n) in proxies { diff --git a/crates/io/tests/dwg_table_cellstyles.rs b/crates/io/tests/dwg_table_cellstyles.rs new file mode 100644 index 0000000000..2ce88f4bcd --- /dev/null +++ b/crates/io/tests/dwg_table_cellstyles.rs @@ -0,0 +1,60 @@ +//! A table style's custom cell styles survive a DWG save as they do a DXF one (#3132). +#![cfg(not(target_arch = "wasm32"))] +// FreeBSD ARM64 has no native DWG bridge (#2526). +#![cfg(not(all(target_os = "freebsd", target_arch = "aarch64")))] + +use cadcraft_doc::{CellStyle, Common, Drawing, EntityKind, Space, Table, TableCell, TableStyle}; +use cadcraft_io::{SaveOptions, Version}; + +fn drawing() -> Drawing { + let mut d = Drawing::new_imperial(); + let mut st = TableStyle { name: "Costs".into(), text_height: 0.2, ..TableStyle::default() }; + let mut mine = st.cell_style(CellStyle::DATA); + mine.name = "MINE".into(); + mine.text_height = 0.05; + mine.align = 4; + st.set_cell_style(mine); + d.table_styles.push(st); + let cell = |t: &str| TableCell { text: t.into(), ..TableCell::default() }; + let mut cells = vec![vec![cell("a"), cell("b")], vec![cell("c"), cell("d")]]; + cells[1][0].style = "MINE".into(); + let t = Table { + style: "Costs".into(), + text_height: 0.2, + title: false, + header: false, + row_heights: vec![0.4, 0.4], + col_widths: vec![2.0, 2.0], + cells, + ..Table::default() + }; + d.add(&Space::Model, Common::default(), EntityKind::Table(t)).unwrap(); + d +} + +fn save(d: &Drawing, v: Version) -> Drawing { + let bytes = cadcraft_io::write_with(d, "t.dwg", &SaveOptions { version: Some(v), binary: false }).unwrap(); + cadcraft_io::read(&bytes, "t.dwg").unwrap() +} + +#[test] +fn custom_cell_styles_roundtrip_through_dwg() { + let d = drawing(); + let want = d.table_styles.iter().find(|s| s.name == "Costs").unwrap().clone(); + for v in [Version::R2004, Version::R2007, Version::R2010, Version::R2013, Version::R2018] { + let back = save(&d, v); + let st = back.table_styles.iter().find(|s| s.name == "Costs").expect("table style kept"); + assert!(st.has_cell_style("MINE"), "{v:?}: cell styles after DWG: {:?}", st.cell_styles); + assert_eq!(st.cell_styles, want.cell_styles, "{v:?}"); + // And a second save keeps them. + let again = save(&back, v); + let st = again.table_styles.iter().find(|s| s.name == "Costs").unwrap(); + assert_eq!(st.cell_styles, want.cell_styles, "{v:?}: second save"); + // Plain styles stay plain. + let std = again.table_styles.iter().find(|s| s.name == "Standard").unwrap(); + assert!(std.cell_styles.is_empty(), "{v:?}"); + // The table's cell still names the style. + let t = again.model.iter().find_map(|e| if let EntityKind::Table(t) = &e.kind { Some(t) } else { None }).unwrap(); + assert_eq!(t.cells[1][0].style, "MINE", "{v:?}"); + } +} From 82fe7d58096d1c8a4834df39086fa5c612e45e7d Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Sat, 14 Nov 2026 01:01:37 +0000 Subject: [PATCH 2/2] Address review: table style xdata fits the DWG string and record limits (#3132) Re-split each CADCRAFT xdata string by its encoded length before R2007, and refuse a save whose table style xdata acadrust would not write, instead of dropping it. --- crates/dwg/src/lib.rs | 95 +++++++++++++++++++++++-- crates/io/tests/dwg_table_cellstyles.rs | 20 ++++++ 2 files changed, 110 insertions(+), 5 deletions(-) diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 11bfb18e60..d4815a1ea9 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -146,22 +146,58 @@ mod native { /// CADCraft's xdata strings on table styles (custom cell styles, #3132). acadrust 0.6.6's DXF /// reader keeps a TABLESTYLE's xdata only in its raw groups, which its DWG writer ignores, so /// before a DWG is written the strings after `1001 CADCRAFT` are made the style's object xdata. - fn table_style_xdata_from_dxf(doc: &mut acadrust::CadDocument) { + /// + /// A DWG's EED string is at most 255 bytes in its code page before R2007, and acadrust writes + /// an application's block only when it is under 32 767 bytes, silently leaving it out + /// otherwise. So each string is re-split by its encoded length (the strings are joined when + /// read, so the split points don't matter), and a block that would not be written is an error. + fn table_style_xdata_from_dxf(doc: &mut acadrust::CadDocument) -> Result<(), String> { + use acadrust::io::dxf::code_page::{dwg_code_page_index, encode_legacy_string, encoding_from_dwg_code_page}; use acadrust::objects::ObjectType; use acadrust::xdata::{ExtendedDataRecord, XDataValue}; + let wide = doc.version >= acadrust::DxfVersion::AC1021; + let encoding = encoding_from_dwg_code_page(dwg_code_page_index(&doc.header.code_page)); let mut records = Vec::new(); for o in doc.objects.values() { let ObjectType::TableStyle(s) = o else { continue }; let Some(codes) = &s.raw_dxf_codes else { continue }; let mut ours = false; let mut record = ExtendedDataRecord::new(CADCRAFT_APP); + let mut size = 0usize; for (code, value) in codes { match code { 1001 => ours = value.trim() == CADCRAFT_APP, - 1000 if ours => record.add_value(XDataValue::String(value.clone())), + 1000 if ours => { + let mut rest = value.as_str(); + while !rest.is_empty() { + let mut end = 0; + let mut bytes = 0; + for ch in rest.chars() { + let n = if wide { 0 } else { encode_legacy_string(ch.encode_utf8(&mut [0u8; 4]), encoding).len() }; + if !wide && bytes + n > usize::from(u8::MAX) { + break; + } + bytes += n; + end += ch.len_utf8(); + } + // A character that alone is over the limit cannot happen (an escape is 7 bytes). + let (chunk, tail) = rest.split_at(end.max(rest.chars().next().map_or(0, char::len_utf8))); + size += if wide { 3 + 2 * chunk.encode_utf16().count() } else { 4 + bytes }; + record.add_value(XDataValue::String(chunk.to_string())); + rest = tail; + } + } _ => {} } } + if size >= usize::from(i16::MAX.unsigned_abs()) { + return Err(format!( + "DWG: the custom cell styles of table style \"{}\" take {size} bytes, more than the {} a DWG can keep for one object; \ + remove some cell styles or shorten their names", + s.name, + i16::MAX + )); + } if !record.is_empty() { records.push((s.handle, record)); } @@ -169,6 +205,7 @@ mod native { for (handle, record) in records { doc.set_object_xdata_record(handle, record); } + Ok(()) } /// The other half of [`table_style_xdata_from_dxf`]: acadrust 0.6.6 reads a TABLESTYLE's @@ -259,6 +296,54 @@ mod native { Ok(()) } + #[cfg(test)] + fn doc_with_cadcraft_strings(version: acadrust::DxfVersion, strings: &[String]) -> acadrust::CadDocument { + use acadrust::objects::{ObjectType, TableStyle}; + let mut doc = acadrust::CadDocument::new(); + doc.version = version; + let mut style = TableStyle::new("Costs"); + style.handle = acadrust::Handle::new(0x2A); + let mut codes = vec![(1001, CADCRAFT_APP.to_string())]; + codes.extend(strings.iter().map(|s| (1000, s.clone()))); + style.raw_dxf_codes = Some(codes); + doc.objects.insert(style.handle, ObjectType::TableStyle(style)); + doc + } + + #[cfg(test)] + #[test] + fn table_style_xdata_strings_fit_the_legacy_string_limit() -> Result<(), String> { + use acadrust::io::dxf::code_page::{dwg_code_page_index, encode_legacy_string, encoding_from_dwg_code_page}; + use acadrust::xdata::XDataValue; + // 80 characters the code page lacks: each is a 7-byte `\U+XXXX` escape, 560 bytes in all. + let long = "中".repeat(80); + let mut doc = doc_with_cadcraft_strings(acadrust::DxfVersion::AC1018, std::slice::from_ref(&long)); + table_style_xdata_from_dxf(&mut doc)?; + let record = doc.object_xdata.get(&acadrust::Handle::new(0x2A)).and_then(|x| x.get_record(CADCRAFT_APP)).ok_or("no record")?; + let encoding = encoding_from_dwg_code_page(dwg_code_page_index(&doc.header.code_page)); + let mut joined = String::new(); + for v in &record.values { + let XDataValue::String(s) = v else { return Err("not a string".into()) }; + assert!(encode_legacy_string(s, encoding).len() <= 255, "chunk of {} bytes", encode_legacy_string(s, encoding).len()); + joined.push_str(s); + } + assert!(record.values.len() > 1); + assert_eq!(joined, long); + Ok(()) + } + + #[cfg(test)] + #[test] + fn table_style_xdata_too_big_for_a_dwg_is_an_error() { + let strings = vec!["x".repeat(255); 140]; + for version in [acadrust::DxfVersion::AC1018, acadrust::DxfVersion::AC1032] { + let mut doc = doc_with_cadcraft_strings(version, &strings); + let err = table_style_xdata_from_dxf(&mut doc).err().unwrap_or_default(); + assert!(err.contains("Costs"), "{version:?}: {err:?}"); + assert!(doc.object_xdata.is_empty(), "{version:?}: nothing is stored for a style that cannot be written"); + } + } + /// acadrust 0.6.6's ASCII writer treats a caret followed by a literal space as already /// escaped, and turns line feeds into paragraph codes. Serialize affected MTEXT contents /// from the native values instead, using its lossless string writer. All other groups, @@ -1346,7 +1431,7 @@ mod native { }; other_layout_owners(&mut doc); table_style_rows(&mut doc, true); - table_style_xdata_from_dxf(&mut doc); + table_style_xdata_from_dxf(&mut doc)?; dimension_flags(&mut doc); let mut unwritten = drop_unwritable(&mut doc); for (kind, n) in proxies { @@ -1357,9 +1442,9 @@ mod native { add_variable_dictionary(&mut doc); current_text_style(&mut doc); current_layer(&mut doc); - unwritten + Ok::<_, String>(unwritten) })) - .map_err(|_| "the DXF to DWG conversion failed on this file".to_string())?; + .map_err(|_| "the DXF to DWG conversion failed on this file".to_string())??; let dwg = std::panic::catch_unwind(AssertUnwindSafe(|| acadrust::DwgWriter::write_to_vec(&doc))) .map_err(|_| "the DWG writer failed on this drawing".to_string())? .map_err(|e| format!("DWG write: {e}"))?; diff --git a/crates/io/tests/dwg_table_cellstyles.rs b/crates/io/tests/dwg_table_cellstyles.rs index 2ce88f4bcd..687966a5e6 100644 --- a/crates/io/tests/dwg_table_cellstyles.rs +++ b/crates/io/tests/dwg_table_cellstyles.rs @@ -37,6 +37,26 @@ fn save(d: &Drawing, v: Version) -> Drawing { cadcraft_io::read(&bytes, "t.dwg").unwrap() } +/// A style whose name is too long for one pre-R2007 EED string once its characters are escaped. +#[test] +fn long_non_latin_style_name_roundtrips_through_dwg() { + let mut d = drawing(); + let long = "中".repeat(80); + if let Some(st) = d.table_styles.iter_mut().find(|s| s.name == "Costs") { + let mut other = st.cell_style(CellStyle::DATA); + other.name = long.clone(); + other.align = 5; + st.set_cell_style(other); + } + let want = d.table_styles.iter().find(|s| s.name == "Costs").unwrap().clone(); + for v in [Version::R2004, Version::R2007, Version::R2018] { + let back = save(&d, v); + let st = back.table_styles.iter().find(|s| s.name == "Costs").unwrap(); + assert!(st.has_cell_style(&long), "{v:?}: cell styles after DWG: {:?}", st.cell_styles); + assert_eq!(st.cell_styles, want.cell_styles, "{v:?}"); + } +} + #[test] fn custom_cell_styles_roundtrip_through_dwg() { let d = drawing();