ezdwg-core 0.22.0

Pure Rust DWG reader and native AC1015 writer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! AcDb:Header (HEADER VARIABLES) sequential decoder.
//!
//! The header section is one bit-coded stream with no random access, so the
//! variables are decoded in specification order and decoding stops right after
//! `XCLIPFRAME` on R2004+, and right after `INSUNITS` on older versions, which
//! have no `XCLIPFRAME`. If reading fails after `INSUNITS`, `xclipframe` is
//! `None` and the variables already read are kept. Layout differences per
//! version are handled as follows:
//!
//! - R2007+ (AC1021+): `TV` strings live in the separate string stream and
//!   `H` handles live in the separate handle stream, so neither consumes bits
//!   from the data stream here. The only exception is `HANDSEED`, which the
//!   specification keeps in the normal data stream.
//! - The prologue between the section size and the first data item varies with
//!   version and maintenance release (optional unknown `RL`, optional bit-size
//!   `RL`). Instead of trusting the maintenance byte, the decoder anchors on
//!   the first four data items (`BD` 412148564080.0 followed by three `BD`
//!   1.0), which form an unambiguous signature.
//! - `REQUIREDVERSIONS` (R2013+) uses a fixed 3-bit length prefix in real
//!   files, unlike the variable 1-3 bit form the specification suggests.
//! - R14 files have no `INSUNITS` header variable; decoding returns an empty
//!   result.

use crate::bit::{BitReader, Endian};
use crate::core::error::{DwgError, ErrorKind};
use crate::core::result::Result;
use crate::dwg::version::DwgVersion;

/// 16-byte sentinel that introduces the header variables data.
const SENTINEL: [u8; 16] = [
    0xCF, 0x7B, 0x1F, 0x23, 0xFD, 0xDE, 0x38, 0xA9, 0x5F, 0x7C, 0x68, 0xB8, 0x4E, 0x6D, 0x33, 0x5F,
];

/// First unknown BD in the data stream; used as the start-of-data signature.
const FIRST_BD_SIGNATURE: f64 = 412_148_564_080.0;

/// Decoded subset of the DWG header variables.
///
/// Every field is `None` when the variable is absent for the file version
/// (R14) or decoding did not reach it.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct HeaderVariables {
    /// $INSUNITS drawing units (0 = unitless, 1 = inches, 4 = millimeters, ...).
    pub insunits: Option<u16>,
    /// $LUNITS linear unit format.
    pub lunits: Option<u16>,
    /// $LUPREC linear unit precision.
    pub luprec: Option<u16>,
    /// $AUNITS angular unit format.
    pub aunits: Option<u16>,
    /// $AUPREC angular unit precision.
    pub auprec: Option<u16>,
    /// $LTSCALE global linetype scale.
    pub ltscale: Option<f64>,
    /// $TEXTSIZE default text height.
    pub textsize: Option<f64>,
    /// $EXTMIN model-space extents minimum.
    pub extmin: Option<(f64, f64, f64)>,
    /// $EXTMAX model-space extents maximum.
    pub extmax: Option<(f64, f64, f64)>,
    /// $LIMMIN model-space limits minimum.
    pub limmin: Option<(f64, f64)>,
    /// $LIMMAX model-space limits maximum.
    pub limmax: Option<(f64, f64)>,
    /// $XCLIPFRAME whether xref clip frames show and plot (0 = neither, 1 = both, 2 = shown, not plotted); R2004+.
    pub xclipframe: Option<u8>,
}

fn rank(version: &DwgVersion) -> Option<u8> {
    match version {
        DwgVersion::R13 | DwgVersion::R14 => Some(0),
        DwgVersion::R2000 => Some(1),
        DwgVersion::R2004 => Some(2),
        DwgVersion::R2007 => Some(3),
        DwgVersion::R2010 => Some(4),
        DwgVersion::R2013 => Some(5),
        DwgVersion::R2018 => Some(6),
        DwgVersion::Unknown(_) => None,
    }
}

struct HeaderCursor<'a> {
    reader: BitReader<'a>,
    r2004_plus: bool,
    r2007_plus: bool,
}

impl<'a> HeaderCursor<'a> {
    fn skip_b(&mut self, count: usize) -> Result<()> {
        for _ in 0..count {
            self.reader.read_b()?;
        }
        Ok(())
    }

    fn skip_bs(&mut self, count: usize) -> Result<()> {
        for _ in 0..count {
            self.reader.read_bs()?;
        }
        Ok(())
    }

    fn skip_bl(&mut self, count: usize) -> Result<()> {
        for _ in 0..count {
            self.reader.read_bl()?;
        }
        Ok(())
    }

    fn skip_bd(&mut self, count: usize) -> Result<()> {
        for _ in 0..count {
            self.reader.read_bd()?;
        }
        Ok(())
    }

    fn skip_3bd(&mut self, count: usize) -> Result<()> {
        for _ in 0..count {
            self.reader.read_3bd()?;
        }
        Ok(())
    }

    /// `TV`: inline text through R2004; string-stream (no data bits) for R2007+.
    fn skip_tv(&mut self) -> Result<()> {
        if !self.r2007_plus {
            self.reader.read_tv()?;
        }
        Ok(())
    }

    /// `H`: inline handle through R2004; handle-stream (no data bits) for R2007+.
    fn skip_h(&mut self) -> Result<()> {
        if !self.r2007_plus {
            self.reader.read_h()?;
        }
        Ok(())
    }

    /// `H` that stays in the data stream for every version (HANDSEED).
    fn skip_h_inline(&mut self) -> Result<()> {
        self.reader.read_h()?;
        Ok(())
    }

    /// `CMC`: bit-short index up to R2000; index + RGB + flag byte from R2004.
    /// Color/book names are inline `TV` for R2004 and move to the string
    /// stream for R2007+.
    fn skip_cmc(&mut self) -> Result<()> {
        self.reader.read_bs()?;
        if self.r2004_plus {
            self.reader.read_bl()?;
            let flags = self.reader.read_rc()?;
            if !self.r2007_plus {
                if flags & 0x01 != 0 {
                    self.reader.read_tv()?;
                }
                if flags & 0x02 != 0 {
                    self.reader.read_tv()?;
                }
            }
        }
        Ok(())
    }
}

/// Decode the header-variables subset from raw `AcDb:Header` section bytes.
///
/// `section` must contain the beginning sentinel; bytes before it (as returned
/// by some section loaders) are tolerated.
pub fn decode_header_variables(
    section: &[u8],
    version: &DwgVersion,
    codepage: Option<u16>,
) -> Result<HeaderVariables> {
    let Some(version_rank) = rank(version) else {
        return Err(DwgError::new(
            ErrorKind::Unsupported,
            format!("unsupported DWG version: {}", version.as_str()),
        ));
    };
    if version_rank == 0 {
        // R14: INSUNITS does not exist in the header variables.
        return Ok(HeaderVariables::default());
    }
    let r2004_plus = version_rank >= 2;
    let r2007_plus = version_rank >= 3;
    let r2010_plus = version_rank >= 4;
    let r2013_plus = version_rank >= 5;
    let is_r2000 = version_rank == 1;

    let sentinel_pos = section
        .windows(SENTINEL.len())
        .position(|window| window == SENTINEL)
        .ok_or_else(|| DwgError::new(ErrorKind::Format, "header variables sentinel not found"))?;

    let mut size_reader = BitReader::new(section);
    let data_start = sentinel_pos + SENTINEL.len();
    size_reader.set_pos(data_start, 0);
    size_reader.read_rl(Endian::Little)?;
    let after_size_bits = size_reader.tell_bits();

    // Candidate bit offsets between the size RL and the first data item:
    //   +0  : no extra prologue (R2000 / R2004)
    //   +32 : bit-size RL (R2007+), or unknown RL alone
    //   +64 : unknown RL (maintenance > 3 / R2018+) followed by bit-size RL
    let candidates: &[u64] = if r2010_plus {
        &[32, 64, 0]
    } else if r2007_plus {
        &[32]
    } else {
        &[0]
    };

    for offset in candidates {
        let start_bits = after_size_bits + offset;
        let mut reader = BitReader::new_with_codepage(section, codepage);
        reader.set_pos((start_bits / 8) as usize, (start_bits % 8) as u8);
        if r2013_plus {
            // REQUIREDVERSIONS: fixed 3-bit length prefix, then that many
            // bytes (observed in real files; differs from the 1-3 bit form in
            // the specification text).
            let length = reader.read_bits_msb(3)?;
            for _ in 0..length {
                reader.read_rc()?;
            }
        }
        let Ok(unknown1) = reader.read_bd() else {
            continue;
        };
        if (unknown1 - FIRST_BD_SIGNATURE).abs() >= 1.0 {
            continue;
        }
        let signature_ok = (0..3).try_fold(true, |ok, _| {
            reader.read_bd().map(|value| ok && value == 1.0)
        });
        if !matches!(signature_ok, Ok(true)) {
            continue;
        }

        let mut cursor = HeaderCursor {
            reader,
            r2004_plus,
            r2007_plus,
        };
        return decode_after_signature(&mut cursor, is_r2000, r2007_plus, r2010_plus, r2013_plus);
    }

    Err(DwgError::new(
        ErrorKind::Format,
        "header variables data start not recognized",
    ))
}

fn decode_after_signature(
    cursor: &mut HeaderCursor<'_>,
    is_r2000: bool,
    r2007_plus: bool,
    r2010_plus: bool,
    r2013_plus: bool,
) -> Result<HeaderVariables> {
    let r2004_plus = cursor.r2004_plus;
    let mut vars = HeaderVariables::default();

    for _ in 0..4 {
        cursor.skip_tv()?; // unknown text x4
    }
    cursor.skip_bl(2)?; // unknown long x2
    if is_r2000 {
        cursor.skip_h_inline()?; // pre-2004: current viewport entity header
    }
    cursor.skip_b(2)?; // DIMASO DIMSHO
    cursor.skip_b(7)?; // PLINEGEN..LIMCHECK
    if r2004_plus {
        cursor.skip_b(1)?; // undocumented
    }
    cursor.skip_b(4)?; // USRTIMER SKPOLY ANGDIR SPLFRAME
    cursor.skip_b(2)?; // MIRRTEXT WORLDVIEW
    cursor.skip_b(3)?; // TILEMODE PLIMCHECK VISRETAIN
    cursor.skip_b(2)?; // DISPSILH PELLIPSE
    cursor.skip_bs(1)?; // PROXYGRAPHICS
    cursor.skip_bs(1)?; // TREEDEPTH
    vars.lunits = Some(cursor.reader.read_bs()?);
    vars.luprec = Some(cursor.reader.read_bs()?);
    vars.aunits = Some(cursor.reader.read_bs()?);
    vars.auprec = Some(cursor.reader.read_bs()?);
    cursor.skip_bs(2)?; // ATTMODE PDMODE
    if r2004_plus {
        cursor.skip_bl(3)?; // unknown x3
    }
    cursor.skip_bs(5)?; // USERI1-5
    cursor.skip_bs(14)?; // SPLINESEGS..TEXTQLTY
    vars.ltscale = Some(cursor.reader.read_bd()?);
    vars.textsize = Some(cursor.reader.read_bd()?);
    cursor.skip_bd(7)?; // TRACEWID..PLINEWID
    cursor.skip_bd(5)?; // USERR1-5
    cursor.skip_bd(7)?; // CHAMFERA-D FACETRES CMLSCALE CELTSCALE
    if !r2007_plus {
        cursor.reader.read_tv()?; // MENUNAME (R13-R18)
    }
    cursor.skip_bl(4)?; // TDCREATE TDUPDATE (day/ms each)
    if r2004_plus {
        cursor.skip_bl(3)?; // unknown x3
    }
    cursor.skip_bl(4)?; // TDINDWG TDUSRTIMER (day/ms each)
    cursor.skip_cmc()?; // CECOLOR
    cursor.skip_h_inline()?; // HANDSEED (data stream for all versions)
    cursor.skip_h()?; // CLAYER
    cursor.skip_h()?; // TEXTSTYLE
    cursor.skip_h()?; // CELTYPE
    if r2007_plus {
        cursor.skip_h()?; // CMATERIAL
    }
    cursor.skip_h()?; // DIMSTYLE
    cursor.skip_h()?; // CMLSTYLE
    cursor.skip_bd(1)?; // PSVPSCALE (R2000+)

    // Paper-space block.
    cursor.skip_3bd(3)?; // INSBASE EXTMIN EXTMAX
    cursor.reader.read_rd(Endian::Little)?;
    cursor.reader.read_rd(Endian::Little)?; // LIMMIN
    cursor.reader.read_rd(Endian::Little)?;
    cursor.reader.read_rd(Endian::Little)?; // LIMMAX
    cursor.skip_bd(1)?; // ELEVATION
    cursor.skip_3bd(3)?; // UCSORG UCSXDIR UCSYDIR
    cursor.skip_h()?; // UCSNAME
    cursor.skip_h()?; // PUCSORTHOREF
    cursor.skip_bs(1)?; // PUCSORTHOVIEW
    cursor.skip_h()?; // PUCSBASE
    cursor.skip_3bd(6)?; // PUCSORGTOP..PUCSORGBACK

    // Model-space block.
    cursor.skip_3bd(1)?; // INSBASE
    vars.extmin = Some(cursor.reader.read_3bd()?);
    vars.extmax = Some(cursor.reader.read_3bd()?);
    let limmin = (
        cursor.reader.read_rd(Endian::Little)?,
        cursor.reader.read_rd(Endian::Little)?,
    );
    let limmax = (
        cursor.reader.read_rd(Endian::Little)?,
        cursor.reader.read_rd(Endian::Little)?,
    );
    vars.limmin = Some(limmin);
    vars.limmax = Some(limmax);
    cursor.skip_bd(1)?; // ELEVATION
    cursor.skip_3bd(3)?; // UCSORG UCSXDIR UCSYDIR
    cursor.skip_h()?; // UCSNAME
    cursor.skip_h()?; // UCSORTHOREF
    cursor.skip_bs(1)?; // UCSORTHOVIEW
    cursor.skip_h()?; // UCSBASE
    cursor.skip_3bd(6)?; // UCSORGTOP..UCSORGBACK

    cursor.skip_tv()?; // DIMPOST
    cursor.skip_tv()?; // DIMAPOST
    cursor.skip_bd(9)?; // DIMSCALE..DIMTM
    if r2007_plus {
        cursor.skip_bd(2)?; // DIMFXL DIMJOGANG
        cursor.skip_bs(1)?; // DIMTFILL
        cursor.skip_cmc()?; // DIMTFILLCLR
    }
    cursor.skip_b(6)?; // DIMTOL..DIMSE2
    cursor.skip_bs(3)?; // DIMTAD DIMZIN DIMAZIN
    if r2007_plus {
        cursor.skip_bs(1)?; // DIMARCSYM
    }
    cursor.skip_bd(8)?; // DIMTXT..DIMGAP
    cursor.skip_bd(1)?; // DIMALTRND
    cursor.skip_b(1)?; // DIMALT
    cursor.skip_bs(1)?; // DIMALTD
    cursor.skip_b(4)?; // DIMTOFL DIMSAH DIMTIX DIMSOXD
    cursor.skip_cmc()?; // DIMCLRD
    cursor.skip_cmc()?; // DIMCLRE
    cursor.skip_cmc()?; // DIMCLRT
    cursor.skip_bs(11)?; // DIMADEC..DIMJUST
    cursor.skip_b(2)?; // DIMSD1 DIMSD2
    cursor.skip_bs(4)?; // DIMTOLJ DIMTZIN DIMALTZ DIMALTTZ
    cursor.skip_b(1)?; // DIMUPT
    cursor.skip_bs(1)?; // DIMATFIT
    if r2007_plus {
        cursor.skip_b(1)?; // DIMFXLON
    }
    if r2010_plus {
        cursor.skip_b(1)?; // DIMTXTDIRECTION
        cursor.skip_bd(1)?; // DIMALTMZF
        cursor.skip_tv()?; // DIMALTMZS
        cursor.skip_bd(1)?; // DIMMZF
        cursor.skip_tv()?; // DIMMZS
    }
    for _ in 0..5 {
        cursor.skip_h()?; // DIMTXSTY DIMLDRBLK DIMBLK DIMBLK1 DIMBLK2
    }
    if r2007_plus {
        for _ in 0..3 {
            cursor.skip_h()?; // DIMLTYPE DIMLTEX1 DIMLTEX2
        }
    }
    cursor.skip_bs(2)?; // DIMLWD DIMLWE
    for _ in 0..9 {
        cursor.skip_h()?; // table control objects
    }
    if is_r2000 {
        cursor.skip_h()?; // VIEWPORT ENTITY HEADER CONTROL (R13-R15)
    }
    for _ in 0..3 {
        cursor.skip_h()?; // ACAD_GROUP ACAD_MLINESTYLE NAMED OBJECTS
    }
    cursor.skip_bs(2)?; // TSTACKALIGN TSTACKSIZE
    cursor.skip_tv()?; // HYPERLINKBASE
    cursor.skip_tv()?; // STYLESHEET
    for _ in 0..3 {
        cursor.skip_h()?; // LAYOUTS PLOTSETTINGS PLOTSTYLES
    }
    if r2004_plus {
        cursor.skip_h()?; // MATERIALS
        cursor.skip_h()?; // COLORS
    }
    if r2007_plus {
        cursor.skip_h()?; // VISUALSTYLE
    }
    if r2013_plus {
        cursor.skip_h()?; // unknown
    }
    cursor.skip_bl(1)?; // flags (CELWEIGHT/ENDCAPS/JOINSTYLE/...)
    vars.insunits = Some(cursor.reader.read_bs()?);
    if r2004_plus {
        vars.xclipframe = read_xclipframe(cursor).ok();
    }
    Ok(vars)
}

/// Reads `XCLIPFRAME`, skipping the variables between `INSUNITS` and it (R2004+).
fn read_xclipframe(cursor: &mut HeaderCursor<'_>) -> Result<u8> {
    let cepsntype = cursor.reader.read_bs()?; // CEPSNTYPE
    if cepsntype == 3 {
        cursor.skip_h()?; // CPSNID
    }
    cursor.skip_tv()?; // FINGERPRINTGUID
    cursor.skip_tv()?; // VERSIONGUID
    for _ in 0..3 {
        cursor.reader.read_rc()?; // SORTENTS INDEXCTL HIDETEXT
    }
    let value = cursor.reader.read_rc()?;
    if cursor.reader.tell_bits() > cursor.reader.total_bits() || value > 2 {
        return Err(DwgError::new(
            ErrorKind::Format,
            "invalid or truncated XCLIPFRAME",
        ));
    }
    Ok(value)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bit::BitWriter;

    fn xclip_tail(streamed: bool, cepsntype: u16, frame: u8) -> Vec<u8> {
        let mut writer = BitWriter::new();
        writer.write_bs(cepsntype).unwrap();
        if !streamed {
            if cepsntype == 3 {
                writer.write_h(5, 0xABCD).unwrap();
            }
            writer.write_tv("fingerprint").unwrap();
            writer.write_tv("version").unwrap();
        }
        for byte in [1, 2, 3, frame] {
            writer.write_rc(byte).unwrap();
        }
        writer.into_bytes()
    }

    #[test]
    fn xclipframe_skips_inline_and_streamed_preceding_fields() {
        for streamed in [false, true] {
            for cepsntype in [0, 3] {
                for frame in [0, 1, 2, 255] {
                    let data = xclip_tail(streamed, cepsntype, frame);
                    let mut cursor = HeaderCursor {
                        reader: BitReader::new(&data),
                        r2004_plus: true,
                        r2007_plus: streamed,
                    };
                    let result = read_xclipframe(&mut cursor);
                    if frame <= 2 {
                        assert_eq!(result.unwrap(), frame);
                    } else {
                        assert!(result.is_err());
                    }
                    for length in 0..data.len() {
                        let mut cursor = HeaderCursor {
                            reader: BitReader::new(&data[..length]),
                            r2004_plus: true,
                            r2007_plus: streamed,
                        };
                        assert!(read_xclipframe(&mut cursor).is_err(), "cut at {length}");
                    }
                }
            }
        }
    }
    use crate::core::config::ParseConfig;
    use crate::dwg::decoder::Decoder;
    use crate::dwg::file_open;
    use std::path::PathBuf;

    fn sample(relative: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../..")
            .join(relative)
    }

    fn decode(relative: &str) -> HeaderVariables {
        let bytes = file_open::read_file(sample(relative)).expect("read sample");
        let decoder = Decoder::new(&bytes, ParseConfig::default()).expect("decoder");
        decoder.header_variables().expect("header variables")
    }

    #[test]
    fn r2000_metric_sample() {
        let vars = decode("examples/data/line_2000.dwg");
        assert_eq!(vars.insunits, Some(4));
        assert_eq!(vars.lunits, Some(2));
        assert_eq!(vars.aunits, Some(0));
        assert_eq!(vars.ltscale, Some(1.0));
        // R2000 stores no XCLIPFRAME.
        assert_eq!(vars.xclipframe, None);
    }

    #[test]
    fn r2004_sample() {
        let vars = decode("examples/data/insert_2004.dwg");
        assert_eq!(vars.insunits, Some(0));
        assert_eq!(vars.lunits, Some(2));
        assert_eq!(vars.xclipframe, Some(0));
    }

    #[test]
    fn r2010_imperial_sample() {
        let vars = decode("examples/data/mechanical_example-imperial.dwg");
        assert_eq!(vars.insunits, Some(1));
        assert_eq!(vars.lunits, Some(4));
    }

    #[test]
    fn r2018_sample() {
        let vars = decode("test_dwg/acadsharp/BLOCKPOINTPARAMETER.dwg");
        assert_eq!(vars.insunits, Some(1));
        assert_eq!(vars.lunits, Some(2));
        assert_eq!(vars.xclipframe, Some(2));
    }
}