Skip to content
Merged
313 changes: 278 additions & 35 deletions crates/dxf/src/codepage.rs

Large diffs are not rendered by default.

168 changes: 168 additions & 0 deletions crates/dxf/src/dos_tables.rs

Large diffs are not rendered by default.

436 changes: 339 additions & 97 deletions crates/dxf/src/lib.rs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions crates/dxf/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! The DXF/DWG file versions CADCraft writes (DXF Reference, HEADER `$ACADVER`).

/// A file version CADCraft can save: the AutoCAD release that introduced the format. Each format
/// also serves the releases after it (2018 is still AutoCAD's current format).
/// A file version CADCraft can save: the `AutoCAD` release that introduced the format. Each format
/// also serves the releases after it (2018 is still `AutoCAD`'s current format).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Version {
/// AutoCAD 2000/2002 (AC1015).
/// `AutoCAD` 2000/2002 (AC1015).
R2000,
/// AutoCAD 2004–2006 (AC1018).
/// `AutoCAD` 2004–2006 (AC1018).
R2004,
/// AutoCAD 2007–2009 (AC1021): the first version whose DXF text is UTF-8.
/// `AutoCAD` 2007–2009 (AC1021): the first version whose DXF text is UTF-8.
R2007,
/// AutoCAD 2010–2012 (AC1024).
/// `AutoCAD` 2010–2012 (AC1024).
R2010,
/// AutoCAD 2013–2017 (AC1027).
/// `AutoCAD` 2013–2017 (AC1027).
R2013,
/// AutoCAD 2018 and later (AC1032), the default.
/// `AutoCAD` 2018 and later (AC1032), the default.
#[default]
R2018,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/dxf/tests/ascii_bom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn utf8_bom_bytes_are_skipped() {
bytes.extend_from_slice(BODY.as_bytes());
let with_bom = craft_dxf::parse(&bytes);
assert!(with_bom.is_ok(), "{with_bom:?}");
assert_eq!(with_bom.map(|t| t.len()).unwrap_or(0), plain.len());
assert_eq!(with_bom.map_or(0, |t| t.len()), plain.len());
}

#[test]
Expand Down
139 changes: 139 additions & 0 deletions crates/dxf/tests/audit_followups.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//! Follow-ups of the crates/dxf audit (#1269): group code 255, the extra DOS and ISO code pages,
//! `format_real`, and the escape and integer writers that were rewritten without a change of
//! behaviour.

use craft_dxf::{Tag, Value, decode_code_page, format_real, parse, write_ascii, write_binary};

#[test]
fn group_code_255_is_an_ordinary_code_in_binary_dxf() {
// Code 255 used to be read as an R12 escape (`FF 00 <code>`), so a file with that code could
// not be read back.
let tags = vec![Tag::s(255, "x"), Tag::s(0, "EOF")];
let back = parse(&write_binary(&tags, true)).unwrap();
assert_eq!(back.len(), 2);
assert_eq!((back[0].code, back[0].str()), (255, "x".to_string()));
}

#[test]
fn more_dos_and_iso_code_pages_decode() {
// (code page, byte, the character the code page defines there)
let cases: [(&str, u8, char); 12] = [
("DOS720", 0x82, 'é'),
("DOS737", 0x80, 'Α'),
("DOS775", 0x80, 'Ć'),
("DOS855", 0x80, 'ђ'),
("DOS857", 0x8D, 'ı'),
("DOS860", 0x84, 'ã'),
("DOS861", 0x8B, 'Ð'),
("DOS863", 0x84, 'Â'),
("DOS864", 0x80, '°'),
("DOS865", 0x9B, 'ø'),
("DOS869", 0x86, 'Ά'),
("ISO8859_15", 0xA4, '€'),
];
for (name, byte, ch) in cases {
assert_eq!(decode_code_page(&[byte], name), Some(ch.to_string()), "{name}");
assert_eq!(decode_code_page(b"A", &name.to_lowercase()), Some("A".into()), "{name}");
}
// An unassigned byte is the replacement character, the rest decodes.
assert_eq!(decode_code_page(b"\xD5A", "DOS857").as_deref(), Some("\u{FFFD}A"));
}

#[test]
fn dos_code_pages_decode_known_characters() {
// Four bytes per table with the character the reference mapping gives there (code point in the
// comment); U+FFFD marks a byte the reference leaves undefined. DOS737, 775, 855, 857, 860, 861, 863,
// 864, 865 and 869 come from the Unicode Consortium files in
// https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/ (CP<number>.TXT). DOS720 has no such file:
// its reference is only Python's `cp720` codec, which maps the bytes below that we decode to U+FFFD
// (0x80, 0x81, 0x84, 0x86, 0x8D, 0x8E, 0x8F, 0x90) to the C1 controls U+0080..U+0090.
let tables: [(&str, [(u8, char); 4]); 11] = [
("DOS720", [(0x82, 'é'), (0x9B, 'ؤ'), (0xB3, '│'), (0xE3, 'ع')]), // U+00E9, U+0624, U+2502, U+0639
("DOS737", [(0x80, 'Α'), (0xAB, 'τ'), (0xB3, '│'), (0xF3, '≤')]), // U+0391, U+03C4, U+2502, U+2264
("DOS775", [(0x80, 'Ć'), (0xAB, '½'), (0xB3, '│'), (0xE3, 'Ń')]), // U+0106, U+00BD, U+2502, U+0143
("DOS855", [(0x80, 'ђ'), (0xA3, 'Б'), (0xB3, '│'), (0xFB, 'ч')]), // U+0452, U+0411, U+2502, U+0447
("DOS857", [(0x8D, 'ı'), (0xB3, '│'), (0xE3, 'Ò'), (0xD5, '\u{FFFD}')]), // U+0131, U+2502, U+00D2, undefined
("DOS860", [(0x84, 'ã'), (0x9B, '¢'), (0xB3, '│'), (0xE3, 'π')]), // U+00E3, U+00A2, U+2502, U+03C0
("DOS861", [(0x8B, 'Ð'), (0xAB, '½'), (0xB3, '│'), (0xFB, '√')]), // U+00D0, U+00BD, U+2502, U+221A
("DOS863", [(0x84, 'Â'), (0x9B, '¢'), (0xB3, '│'), (0xE3, 'π')]), // U+00C2, U+00A2, U+2502, U+03C0
("DOS864", [(0x80, '°'), (0xAB, 'ﺙ'), (0xB3, '٣'), (0xFF, '\u{FFFD}')]), // U+00B0, U+FE99, U+0663, undefined
("DOS865", [(0x9B, 'ø'), (0xAB, '½'), (0xB3, '│'), (0xE3, 'π')]), // U+00F8, U+00BD, U+2502, U+03C0
("DOS869", [(0x86, 'Ά'), (0x9B, 'ά'), (0xB3, '│'), (0x80, '\u{FFFD}')]), // U+0386, U+03AC, U+2502, undefined
];
for (name, cases) in tables {
for (byte, ch) in cases {
assert_eq!(decode_code_page(&[byte], name), Some(ch.to_string()), "{name} 0x{byte:02X}");
}
}
// DOS720: the 8 bytes the Python codec maps to C1 controls are unassigned here.
for byte in [0x80u8, 0x81, 0x84, 0x86, 0x8D, 0x8E, 0x8F, 0x90] {
assert_eq!(decode_code_page(&[byte], "DOS720").as_deref(), Some("\u{FFFD}"), "DOS720 0x{byte:02X}");
}
}

#[test]
fn ascii_file_in_a_dos_code_page_reads_its_text() {
let mut dxf = b"0\nSECTION\n2\nHEADER\n9\n$DWGCODEPAGE\n3\nDOS855\n0\nENDSEC\n0\nSECTION\n2\nTABLES\n8\n".to_vec();
dxf.extend_from_slice(b"\xA1\xA2\n0\nENDSEC\n0\nEOF\n");
let tags = parse(&dxf).unwrap();
let layer = tags.iter().find(|t| t.code == 8).unwrap();
assert_eq!(layer.str(), decode_code_page(b"\xA1\xA2", "DOS855").unwrap());
assert_eq!(layer.str(), "Аб");
}

#[test]
fn format_real_always_has_a_point() {
assert_eq!(format_real(5.0), "5.0");
assert_eq!(format_real(-0.0), "-0.0");
assert_eq!(format_real(0.1), "0.1");
assert_eq!(format_real(1e15), "1000000000000000.0");
assert_eq!(format_real(1e21), "1.0E+21");
assert_eq!(format_real(1e-7), "0.0000001");
assert_eq!(format_real(f64::MAX).parse::<f64>().unwrap(), f64::MAX);
assert_eq!(format_real(f64::NAN), "0.0");
assert_eq!(format_real(f64::INFINITY), "0.0");
assert_eq!(format_real(f64::NEG_INFINITY), "0.0");
}

#[test]
fn escapes_next_to_other_text_and_backslashes() {
let one = |s: &str| parse(format!("1\n{s}\n0\nEOF\n").as_bytes()).unwrap()[0].str();
assert_eq!(one("a\\U+00E9b"), "aéb");
assert_eq!(one("é\\U+0041\\ ü\\M+5BCFE\\U+D83D\\U+DE00 x\\"), "éA\\ ü件😀 x\\");
assert_eq!(one("\\\\U+0041"), "\\A");
assert_eq!(one("\\U+D83D\\U+0041 \\U+DE00"), "\\U+D83DA \\U+DE00");
assert_eq!(one("日本\\U+00"), "日本\\U+00");
// A long run of backslashes (hostile) is read in linear time and stays literal.
let slashes = "\\".repeat(100_000);
assert_eq!(one(&slashes), slashes);
// The same strings through the binary reader.
// Built by hand: the writer escapes a literal `\U+` (#2050), so it would not produce an escape.
let mut file = b"AutoCAD Binary DXF\r\n\x1a\0".to_vec();
file.extend_from_slice(&1i16.to_le_bytes());
file.extend_from_slice("é\\U+0041\\ ü".as_bytes());
file.push(0);
file.extend_from_slice(&0i16.to_le_bytes());
file.extend_from_slice(b"EOF\0");
assert_eq!(parse(&file).unwrap()[0].str(), "éA\\ ü");
// And the writer keeps the literal text literal.
let tags = [Tag::s(1, "é\\U+0041\\ ü"), Tag::s(0, "EOF")];
assert_eq!(parse(&write_binary(&tags, true)).unwrap()[0].str(), "é\\U+0041\\ ü");
}

#[test]
fn integers_are_written_in_full() {
let text = write_ascii(&[Tag::i(70, i64::MIN), Tag::i(90, -7), Tag::i(160, 0), Tag::new(290, Value::Bool(true))]);
assert_eq!(text, " 70\r\n-9223372036854775808\r\n 90\r\n-7\r\n160\r\n0\r\n290\r\n1\r\n");
}

#[test]
fn binary_hex_reads_every_byte_in_upper_case() {
let all: Vec<u8> = (0..=255).collect();
let mut file = b"AutoCAD Binary DXF\r\n\x1a\0".to_vec();
file.extend_from_slice(&[0x36, 0x01, 0xFF]);
file.extend_from_slice(&all[..255]);
file.extend_from_slice(&[0x00, 0x00, b'E', b'O', b'F', 0]);
let tags = parse(&file).unwrap();
let want: String = all[..255].iter().map(|b| format!("{b:02X}")).collect();
assert_eq!(tags[0].value, Value::Hex(want));
}
31 changes: 28 additions & 3 deletions crates/dxf/tests/code_page_write.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Pre-R2007 DXF text is written in the `$DWGCODEPAGE` code page: characters it has as their
//! bytes, the others as `\U+XXXX` escapes; every form reads back to the same text.

use craft_dxf::{Tag, parse, write_ascii, write_ascii_in, write_binary_in};
use craft_dxf::{Tag, parse, write_ascii, write_ascii_in, write_ascii_in_page, write_binary_in};

fn file(code_page: &str, text: &str) -> Vec<Tag> {
vec![
Expand All @@ -28,11 +28,15 @@ fn contains(bytes: &[u8], part: &[u8]) -> bool {
#[test]
fn text_is_written_in_the_code_page() {
// (code page, text, the bytes written for it)
let cases: [(&str, &str, &[u8]); 6] = [
let cases: [(&str, &str, &[u8]); 8] = [
("ANSI_1252", "Ação € 图", b"A\xE7\xE3o \x80 \\U+56FE"),
("ANSI_1251", "Слой é", b"\xD1\xEB\xEE\xE9 \\U+00E9"),
("ANSI_932", "表図面 é", b"\x95\x5C\x90\x7D\x96\xCA \\U+00E9"),
("DOS850", "Größe €", b"Gr\x94\xE1e \\U+20AC"),
// DOS 864: byte 0x25 is the Arabic percent sign, so a literal `%` is escaped.
("DOS864", "50% \u{066A}\u{00B0}", b"50\\U+0025 %\x80"),
// Only the percent sign: every byte is ASCII, and it still reads back as U+066A.
("DOS864", "50\u{066A}", b"50%"),
("ANSI_1252", "😀", b"\\U+D83D\\U+DE00"),
// A code page CADCraft doesn't know: everything non-ASCII is escaped.
("ANSI_1361", "Ação", b"A\\U+00E7\\U+00E3o"),
Expand Down Expand Up @@ -60,7 +64,7 @@ fn unknown_ascii_and_utf8_code_pages_are_pure_ascii() {
}
}

/// Code page bytes that happen to form valid UTF-8 (`СІ` in ANSI_1251 is D1 B2, which is U+0472
/// Code page bytes that happen to form valid UTF-8 (`СІ` in `ANSI_1251` is D1 B2, which is U+0472
/// in UTF-8) would be read back as UTF-8 from an ASCII file, so that file is written with
/// escapes. Binary DXF reads strings by the header's code page and keeps the bytes.
#[test]
Expand All @@ -77,6 +81,27 @@ fn code_page_bytes_that_are_valid_utf8_are_escaped_in_ascii() {
assert!(contains(&write_ascii_in(&file("ANSI_1251", "Слой"), "ANSI_1251"), b"\xD1\xEB\xEE\xE9"));
}

/// `write_ascii_in_page`, for a reader that decodes by `$DWGCODEPAGE` and never guesses UTF-8 (the
/// DWG bridge's acadrust), keeps the code page bytes even when they form valid UTF-8 (`é` in
/// ANSI_1252 is C3 A9, `é` in UTF-8), escapes only what the code page lacks, and escapes a
/// backslash that starts an escape. Otherwise it writes what `write_ascii_in` writes.
#[test]
fn page_writer_keeps_code_page_bytes_that_are_valid_utf8() {
let tags = file("ANSI_1252", "é Média Ω \\U+00E9");
let page = write_ascii_in_page(&tags, "ANSI_1252");
assert!(contains(&page, b"\xC3\xA9 M\xE9dia \\U+03A9 \\U+005CU+00E9"), "{:?}", String::from_utf8_lossy(&page));
// Text whose bytes are all valid UTF-8: `write_ascii_in` still falls back to escapes, the
// page writer keeps them.
let utf8_like = file("ANSI_1252", "é");
assert!(write_ascii_in(&utf8_like, "ANSI_1252").is_ascii());
assert!(contains(&write_ascii_in_page(&utf8_like, "ANSI_1252"), b"\r\n\xC3\xA9\r\n"));
let plain = file("ANSI_1252", "Ação");
assert_eq!(write_ascii_in_page(&plain, "ANSI_1252"), write_ascii_in(&plain, "ANSI_1252"));
for cp in ["EBCDIC", "ASCII", "UTF8"] {
assert_eq!(write_ascii_in_page(&plain, cp), write_ascii(&plain).into_bytes(), "{cp}");
}
}

/// A literal `\U+` or `\M+` in the text (#2050) reads back as written alongside code page bytes,
/// in ASCII and binary, including an ASCII file that falls back to escapes because its bytes
/// would read as UTF-8.
Expand Down
8 changes: 4 additions & 4 deletions crates/dxf/tests/conformance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Small independent byte fixtures for DXF tag types, text encoding, framing and failures.
//! Handwritten examples, not files from a CAD application or generated by our writer.
//! Background on the tag model and file structure:
//! https://ezdxf.readthedocs.io/en/stable/dxfinternals/dxftags.html
//! https://ezdxf.readthedocs.io/en/stable/dxfinternals/filestructure.html
//! <https://ezdxf.readthedocs.io/en/stable/dxfinternals/dxftags.html>
//! <https://ezdxf.readthedocs.io/en/stable/dxfinternals/filestructure.html>
//! ASCII spacing is our writer's documented convention; binary fixtures specify actual bytes.

use craft_dxf::{DxfError, Tag, Value, parse, parse_with_limit, write_ascii_as, write_binary};
Expand All @@ -16,8 +16,8 @@ fn fixtures() -> Vec<(Tag, &'static [u8], &'static [u8])> {
(Tag::s(105, "AB12"), b"105\r\nAB12\r\n", b"\x69\0AB12\0"),
(Tag::new(10, Value::Real(-3.25)), b" 10\r\n-3.25\r\n", b"\x0a\0\0\0\0\0\0\0\x0a\xc0"),
(Tag::i(70, -32768), b" 70\r\n-32768\r\n", b"\x46\0\0\x80"),
(Tag::i(90, -2147483648), b" 90\r\n-2147483648\r\n", b"\x5a\0\0\0\0\x80"),
(Tag::i(160, 9223372036854775807), b"160\r\n9223372036854775807\r\n", b"\xa0\0\xff\xff\xff\xff\xff\xff\xff\x7f"),
(Tag::i(90, -2_147_483_648), b" 90\r\n-2147483648\r\n", b"\x5a\0\0\0\0\x80"),
(Tag::i(160, 9_223_372_036_854_775_807), b"160\r\n9223372036854775807\r\n", b"\xa0\0\xff\xff\xff\xff\xff\xff\xff\x7f"),
(Tag::new(290, Value::Bool(false)), b"290\r\n0\r\n", b"\x22\x01\0"),
(Tag::new(299, Value::Bool(true)), b"299\r\n1\r\n", b"\x2b\x01\x01"),
(Tag::new(310, Value::Hex("00FF80".into())), b"310\r\n00FF80\r\n", b"\x36\x01\x03\0\xff\x80"),
Expand Down
11 changes: 8 additions & 3 deletions crates/dxf/tests/hex_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
//! code and following-record framing. Oversized binary read fixtures test tolerance only.
//! Pending writer regressions extend the chunk-size finding in issue #1025.
//! Public DXF Reference, Group Codes in Numerical Order and Binary DXF Files:
//! https://help.autodesk.com/cloudhelp/2025/ENU/AutoCAD-DXF/files/GUID-3F0380A5-1C15-464D-BC66-2C5F094BCFB9.htm
//! https://help.autodesk.com/cloudhelp/2025/ENU/AutoCAD-DXF/files/GUID-FC1C3C69-DBC2-49E4-893A-000D6538C0FE.htm
//! <https://help.autodesk.com/cloudhelp/2025/ENU/AutoCAD-DXF/files/GUID-3F0380A5-1C15-464D-BC66-2C5F094BCFB9.htm>
//! <https://help.autodesk.com/cloudhelp/2025/ENU/AutoCAD-DXF/files/GUID-FC1C3C69-DBC2-49E4-893A-000D6538C0FE.htm>

use std::fmt::Write as _;

use craft_dxf::{Tag, Value, parse, write_ascii_as, write_binary};

Expand All @@ -17,7 +19,10 @@ fn payload(length: usize) -> Vec<u8> {
}

fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02X}")).collect()
bytes.iter().fold(String::new(), |mut out, byte| {
let _ = write!(out, "{byte:02X}");
out
})
}

fn input(code: i32, bytes: &[u8]) -> [Tag; 2] {
Expand Down
6 changes: 5 additions & 1 deletion crates/dxf/tests/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ fn hex_chunks_keep_bytes_and_order() {
// Larger payloads need chunk-size/reassembly checks rather than exact tag equality;
// see hex_chunks.rs for the independent wire checks and pending writer regressions.
for length in [0, 1, 2, 126, 127] {
let hex: String = (0..length).map(|i| format!("{:02X}", (i * 37) % 256)).collect();
let hex: String = (0..length).fold(String::new(), |mut out, i| {
use std::fmt::Write as _;
let _ = write!(out, "{:02X}", (i * 37) % 256);
out
});
let tags: Vec<_> = (310..=319).chain([1004]).map(|code| Tag::new(code, Value::Hex(hex.clone()))).collect();
check(&format!("hex/bytes={length}"), &tags);
}
Expand Down
Loading