diff --git a/src/io/dwg/dwg_document_builder.rs b/src/io/dwg/dwg_document_builder.rs index dc9eb659..8587feb8 100644 --- a/src/io/dwg/dwg_document_builder.rs +++ b/src/io/dwg/dwg_document_builder.rs @@ -3856,6 +3856,12 @@ impl DwgDocumentBuilder { } document.header.handle_seed = document.header.handle_seed.max(next_handle); + // A seeded default the file has no record of - the Standard dimension + // style of a drawing that only uses ISO-25 - still names the seeded + // text style's handle, which the file may have given to a record of + // another table; point it at the file's text style of the same name. + crate::io::dxf::rewire_stale_text_style_references(document); + document.header.current_layer_name = document .layers .iter() diff --git a/src/io/dxf/mod.rs b/src/io/dxf/mod.rs index a0d197fe..6f5c16d5 100644 --- a/src/io/dxf/mod.rs +++ b/src/io/dxf/mod.rs @@ -29,8 +29,8 @@ mod writer; pub use dxf_code::DxfCode; pub use group_code_value::GroupCodeValueType; +pub(crate) use reader::{rewire_stale_text_style_references, DxfStreamReader, DxfTextReader}; pub use reader::{DxfReader, DxfReaderConfiguration}; -pub(crate) use reader::{DxfStreamReader, DxfTextReader}; pub use writer::{value_type_for_code, write_binary_dxf, write_dxf}; pub use writer::{ DxfBinaryWriter, DxfOmission, DxfStreamWriter, DxfStreamWriterExt, DxfTextWriter, DxfWriter, diff --git a/src/io/dxf/reader.rs b/src/io/dxf/reader.rs index 672e941e..926ef11e 100644 --- a/src/io/dxf/reader.rs +++ b/src/io/dxf/reader.rs @@ -1007,8 +1007,8 @@ fn rewire_dangling_layout_block_records(document: &mut CadDocument) { /// has since given to an unrelated record - e.g. the ByBlock linetype - and the /// writer emits a `340`/`342` that consumers resolve to the wrong table /// (issues #64 and #68). Re-point such stale handles at the file's text style -/// of the same name. -fn rewire_stale_text_style_references(document: &mut CadDocument) { +/// of the same name. The DWG reader seeds the same defaults and runs this too. +pub(crate) fn rewire_stale_text_style_references(document: &mut CadDocument) { let text_style_handles: std::collections::HashSet = document .text_styles .iter() diff --git a/tests/dwg_seeded_dimstyle_text_style.rs b/tests/dwg_seeded_dimstyle_text_style.rs new file mode 100644 index 00000000..454c716a --- /dev/null +++ b/tests/dwg_seeded_dimstyle_text_style.rs @@ -0,0 +1,97 @@ +//! The DWG reader seeds a Standard dimension style before it reads the file. +//! When the file has none of its own, that seeded style survives, and its text +//! style must be the file's Standard text style - not whatever record the file +//! keeps at the handle the seed was wired to (the DXF reader's fix for #64). + +use std::io::Cursor; + +use acadrust::tables::DimStyle; +use acadrust::types::{DxfVersion, Handle}; +use acadrust::{CadDocument, DwgReader, DwgWriter}; + +/// A drawing as other CAD programs save it: the Standard text style at #11, +/// the ByBlock linetype at #14 and ISO-25 as its only dimension style. +fn iso_only_dwg() -> Vec { + let mut doc = CadDocument::with_version(DxfVersion::AC1032); + doc.text_styles.get_mut("Standard").unwrap().handle = Handle::new(0x11); + doc.line_types.get_mut("ByBlock").unwrap().handle = Handle::new(0x14); + doc.dim_styles.remove("Standard").unwrap(); + let mut iso = DimStyle::new("ISO-25"); + iso.handle = doc.allocate_handle(); + iso.dimtxsty_handle = Handle::new(0x11); + iso.dimtxsty = "Standard".to_string(); + doc.header.current_dimstyle_handle = iso.handle; + doc.header.current_dimstyle_name = "ISO-25".to_string(); + doc.header.dim_text_style_handle = Handle::new(0x11); + doc.dim_styles.add(iso).unwrap(); + DwgWriter::write_to_vec(&doc).expect("write dwg") +} + +fn read(bytes: Vec) -> CadDocument { + DwgReader::from_stream(Cursor::new(bytes)) + .read() + .expect("read dwg") +} + +/// Every dimension style's DIMTXSTY names a text style of the document. +fn assert_dim_styles_name_text_styles(doc: &CadDocument) { + let text_styles: Vec = doc.text_styles.iter().map(|s| s.handle).collect(); + for style in doc.dim_styles.iter() { + assert!( + text_styles.contains(&style.dimtxsty_handle), + "dimension style {} names {:?} as its text style, which is no text style \ + (ByBlock linetype = {:?})", + style.name, + style.dimtxsty_handle, + doc.line_types.get("ByBlock").map(|l| l.handle), + ); + } +} + +#[test] +fn a_seeded_standard_dim_style_points_at_the_files_text_style() { + let loaded = read(iso_only_dwg()); + assert!( + loaded.dim_styles.get("Standard").is_some(), + "the reader keeps its seeded Standard dimension style" + ); + assert_dim_styles_name_text_styles(&loaded); + assert_eq!( + loaded.dim_styles.get("Standard").unwrap().dimtxsty_handle, + loaded.text_styles.get("Standard").unwrap().handle, + "the seeded style uses the file's Standard text style" + ); +} + +/// What a consumer sees: the drawing read and written again carries a +/// Standard dimension style whose DIMTXSTY is a text style. +#[test] +fn a_dwg_written_again_names_a_text_style_in_every_dim_style() { + let loaded = read(iso_only_dwg()); + let again = read(DwgWriter::write_to_vec(&loaded).expect("write dwg again")); + assert_dim_styles_name_text_styles(&again); + assert_eq!( + again.dim_styles.get("ISO-25").unwrap().dimtxsty_handle, + again.text_styles.get("Standard").unwrap().handle, + "the file's own style keeps its text style" + ); +} + +/// The repair only re-points what names no text style: a file's own Standard +/// dimension style keeps the text style it names, even one not called Standard. +#[test] +fn a_files_own_standard_dim_style_keeps_its_text_style() { + let mut doc = CadDocument::with_version(DxfVersion::AC1032); + let mut romans = acadrust::tables::TextStyle::new("ROMANS"); + romans.handle = doc.allocate_handle(); + let romans_handle = romans.handle; + doc.text_styles.add(romans).unwrap(); + let standard = doc.dim_styles.get_mut("Standard").unwrap(); + standard.dimtxsty_handle = romans_handle; + standard.dimtxsty = "ROMANS".to_string(); + let loaded = read(DwgWriter::write_to_vec(&doc).expect("write dwg")); + assert_eq!( + loaded.dim_styles.get("Standard").unwrap().dimtxsty_handle, + romans_handle + ); +}