rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
//! Bounded reader for the legacy VtChart 6 presentation archive.
//!
//! Follow object references, never search arbitrary payloads for type signatures.
//! Unsupported class versions fail closed; no native chart library is required.

use std::collections::BTreeMap;
use std::rc::Rc;

type Result<T> = std::result::Result<T, ()>;

#[derive(Clone, Copy)]
pub(super) struct Axis {
    pub min: f64,
    pub max: f64,
    pub divisions: u16,
}

#[derive(Clone)]
pub(super) struct Series {
    pub color: u32,
    pub fill: u32,
    pub width: f64,
    pub secondary: bool,
    pub index: usize,
    pub selectors: Vec<i16>,
    pub legend: Option<Text>,
}

/// 저장 글꼴 크기는 포인트 단위이며 차트의 캐시 사각형 크기와 구별한다.
#[derive(Clone, Debug, PartialEq)]
pub(super) struct Font {
    pub family: String,
    pub size: f64,
    pub style: u16,
    pub effects: u16,
    pub color: u32,
}

#[derive(Clone, Debug)]
pub(super) struct Text {
    pub value: String,
    pub font: Font,
    pub metrics: TextMetrics,
}

pub(super) struct Presentation {
    pub chart_type: usize,
    pub axes: [Axis; 2],
    pub series: Vec<Series>,
    pub extent: [f64; 4],
    pub plot: [f64; 4],
    pub title: Option<Text>,
    pub legend_font: Option<Font>,
    pub label_fonts: [Option<Font>; 4],
    /// 계열 막대 사이와 범주 사이의 간격을 막대 너비에 대한 비율로 저장한다.
    pub bar_gap: f64,
    pub x_gap: f64,
    pub backdrop: Option<Backdrop>,
    pub title_section: Option<Section>,
    pub legend_section: Option<Section>,
    pub plot_section: Section,
    pub axis_styles: [AxisStyle; 4],
    pub labels: [Option<Text>; 4],
}

/// 소유 텍스트의 저장 점유 정보. 글꼴 크기와 같은 값으로 추정하지 않는다.
#[derive(Clone, Debug)]
pub(super) struct TextMetrics {
    pub margin: i32,
    pub rect: [i32; 4],
    pub extent: [i32; 2],
    pub cache_valid: bool,
    pub horizontal: u16,
    pub vertical: u16,
    pub orientation: u16,
    pub paint_rect: [i32; 4],
}
#[derive(Clone, Debug)]
pub(super) struct Section {
    pub external: i16,
    pub internal: i16,
    pub visible: bool,
    pub rect: [f64; 4],
    pub location: u16,
    pub backdrop: Option<Backdrop>,
}
#[derive(Clone, Debug)]
pub(super) struct Backdrop {
    pub frame_color: u32,
    pub frame_width: f64,
    pub frame_enabled: bool,
    pub fill_kind: i16,
    pub fill_color: u32,
    pub shadow_enabled: bool,
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct Pen {
    pub enabled: bool,
    pub color: u32,
    pub width: f64,
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct AxisStyle {
    pub labels_visible: bool,
    pub pen: Pen,
    pub major_grid: Pen,
    pub minor_grid: Pen,
    pub tick_length: f64,
    pub tick_style: u16,
}
fn pen(bytes: &[u8]) -> Result<Pen> {
    if bytes.len() != 22 || bytes[..2] != [1, 0] || bytes[8..10] != [1, 0] {
        return Err(());
    }
    let width = f32::from_le_bytes(bytes[14..18].try_into().map_err(|_| ())?) as f64;
    if !width.is_finite() || width < 0.0 {
        return Err(());
    }
    Ok(Pen {
        enabled: u16::from_le_bytes(bytes[2..4].try_into().map_err(|_| ())?) != 0,
        color: u32::from_le_bytes(bytes[10..14].try_into().map_err(|_| ())?),
        width,
    })
}
fn axis_label(node: &Node) -> Option<Text> {
    match node {
        Node::Text(text) => Some(text.clone()),
        Node::Array(items) => items
            .iter()
            .filter_map(|item| axis_label(item))
            .max_by_key(|text| text.metrics.extent[0]),
        _ => None,
    }
}

enum Node {
    Empty,
    Count(usize),
    Array(Vec<Rc<Node>>),
    Axis(Option<Axis>, Option<Font>, AxisStyle, Option<Text>),
    Series(Series),
    Section(Section),
    Backdrop(Backdrop),
    Fill(i16, u32),
    Plot(
        Box<(
            [Option<Axis>; 4],
            [Option<Font>; 4],
            Section,
            [f64; 2],
            [AxisStyle; 4],
            [Option<Text>; 4],
        )>,
    ),
    String(String),
    Font(Font),
    Text(Text),
    Title(Option<Text>, Section),
    Legend(Option<Font>, Section),
}

fn axis_label_font(node: &Node) -> Option<Font> {
    match node {
        Node::Text(text) => Some(text.font.clone()),
        Node::Array(items) => {
            let mut font = None;
            for item in items {
                match item.as_ref() {
                    Node::Empty => {}
                    Node::Text(text) => {
                        if font.as_ref().is_some_and(|font| *font != text.font) {
                            // 다단 축의 서로 다른 레이블 글꼴을 하나로 덮어쓰지 않는다.
                            return None;
                        }
                        font = Some(text.font.clone());
                    }
                    _ => return None,
                }
            }
            font
        }
        _ => None,
    }
}

struct Reader<'a> {
    data: &'a [u8],
    at: usize,
    types: BTreeMap<i32, (String, u16)>,
    objects: BTreeMap<i32, Rc<Node>>,
    depth: usize,
    remaining: usize,
}

impl<'a> Reader<'a> {
    fn bytes(&mut self, len: usize) -> Result<&'a [u8]> {
        let end = self.at.checked_add(len).ok_or(())?;
        let bytes = self.data.get(self.at..end).ok_or(())?;
        self.at = end;
        Ok(bytes)
    }

    fn skip(&mut self, len: usize) -> Result<()> {
        self.bytes(len).map(|_| ())
    }

    fn u16(&mut self) -> Result<u16> {
        Ok(u16::from_le_bytes(
            self.bytes(2)?.try_into().map_err(|_| ())?,
        ))
    }

    fn i16(&mut self) -> Result<i16> {
        Ok(self.u16()? as i16)
    }

    fn i32(&mut self) -> Result<i32> {
        Ok(i32::from_le_bytes(
            self.bytes(4)?.try_into().map_err(|_| ())?,
        ))
    }

    fn f64(&mut self) -> Result<f64> {
        Ok(f64::from_le_bytes(
            self.bytes(8)?.try_into().map_err(|_| ())?,
        ))
    }

    fn rect(&mut self) -> Result<[f64; 4]> {
        let mut result = [0.0; 4];
        for value in &mut result {
            *value = f32::from_le_bytes(self.bytes(4)?.try_into().map_err(|_| ())?) as f64;
            if !value.is_finite() {
                return Err(());
            }
        }
        Ok(result)
    }

    fn class(&mut self) -> Result<(String, u16)> {
        let id = self.i32()?;
        if let Some(class) = self.types.get(&id) {
            return Ok(class.clone());
        }
        if id < 0 || self.types.len() >= 64 {
            return Err(());
        }
        let len = self.u16()? as usize;
        if !(2..=64).contains(&len) {
            return Err(());
        }
        let bytes = self.bytes(len)?;
        if bytes.last() != Some(&0) {
            return Err(());
        }
        let name = std::str::from_utf8(&bytes[..len - 1])
            .map_err(|_| ())?
            .to_owned();
        let version = self.u16()?;
        self.types.insert(id, (name.clone(), version));
        Ok((name, version))
    }

    fn base(&mut self, expected: &str) -> Result<Node> {
        let (name, version) = self.class()?;
        if name != expected {
            return Err(());
        }
        self.body(&name, version)
    }

    fn object(&mut self) -> Result<Rc<Node>> {
        self.remaining = self.remaining.checked_sub(1).ok_or(())?;
        let id = self.i32()?;
        if id == -1 {
            return Ok(Rc::new(Node::Empty));
        }
        if let Some(object) = self.objects.get(&id) {
            return Ok(Rc::clone(object));
        }
        if id < 0 || self.depth >= 32 || self.objects.len() >= 32768 {
            return Err(());
        }
        let (name, version) = self.class()?;
        self.depth += 1;
        let result = self.body(&name, version);
        self.depth -= 1;
        let object = Rc::new(result?);
        self.objects.insert(id, Rc::clone(&object));
        Ok(object)
    }

    fn body(&mut self, name: &str, version: u16) -> Result<Node> {
        let expected_version = match name {
            "VtChartPlot" => 4,
            "VtAxis" => 3,
            "VtSeries" | "VtTextBlock" | "VtWindow" => 2,
            _ => 1,
        };
        if version != expected_version {
            return Err(());
        }
        match name {
            "VtObject" => {}
            "VtCollection" => {
                let count = self.u16()? as usize;
                self.base("VtObject")?;
                return Ok(Node::Count(count));
            }
            "VtList" => {
                let Node::Count(0) = self.base("VtCollection")? else {
                    return Err(());
                };
            }
            "VtArray" => {
                let capacity = self.u16()? as usize;
                let Node::Count(count) = self.base("VtCollection")? else {
                    return Err(());
                };
                if count > capacity || capacity > self.remaining {
                    return Err(());
                }
                let mut items = Vec::with_capacity(capacity);
                for _ in 0..capacity {
                    items.push(self.object()?);
                }
                return Ok(Node::Array(items));
            }
            "VtDataGrid" => {
                self.base("VtMatrix")?;
                self.skip(8)?;
            }
            "VtMatrix" => {
                self.base("VtCollection")?;
                let rows = self.u16()? as usize;
                let cols = self.u16()? as usize;
                let count = rows.checked_mul(cols).ok_or(())?;
                if count > self.remaining {
                    return Err(());
                }
                for _ in 0..count {
                    self.object()?;
                }
            }
            "VtString" => {
                let len = self.u16()? as usize;
                let text = if len > 0 {
                    // Legacy archives reserve one trailing byte, but newer
                    // writers do not always zero it. Keep the length/bounds
                    // check without treating that byte as a NUL contract.
                    let bytes = self.bytes(len + 1)?;
                    // 자료 그리드와 같은 UTF-16 정본을 읽는다. 표현 문자열의
                    // 전각 공백은 제목의 실제 간격이므로 정규화하지 않는다.
                    let text = super::legacy_string::decode(&bytes[..len]).ok_or(())?;
                    text.split('\0').next().unwrap_or_default().to_owned()
                } else {
                    String::new()
                };
                self.base("VtValue")?;
                return Ok(Node::String(text));
            }
            "VtDouble" => {
                self.skip(10)?;
                self.base("VtValue")?;
            }
            "VtValue" => {
                self.base("VtObject")?;
            }
            "VtBackdrop" => {
                let bytes = self.bytes(50)?;
                let frame_width =
                    f32::from_le_bytes(bytes[44..48].try_into().map_err(|_| ())?) as f64;
                if !frame_width.is_finite() || frame_width < 0.0 {
                    return Err(());
                }
                let fill = self.object()?;
                let (fill_kind, fill_color) = match fill.as_ref() {
                    Node::Fill(kind, color) => (*kind, *color),
                    Node::Empty => (0, 0),
                    _ => return Err(()),
                };
                let backdrop = Backdrop {
                    frame_color: u32::from_le_bytes(bytes[34..38].try_into().map_err(|_| ())?),
                    frame_width,
                    frame_enabled: bytes[48..50] != [0, 0],
                    shadow_enabled: bytes[28..30] != [0, 0],
                    fill_kind,
                    fill_color,
                };
                self.base("VtObject")?;
                return Ok(Node::Backdrop(backdrop));
            }
            "VtFill" => {
                let bytes = self.bytes(34)?;
                let color = u32::from_le_bytes(bytes[6..10].try_into().map_err(|_| ())?);
                self.object()?;
                let kind = self.i16()?;
                self.base("VtObject")?;
                return Ok(Node::Fill(kind, color));
            }
            "VtPicture" => {
                self.skip(2)?;
                if self.i16()? != 0 {
                    // The embedded-picture branch still uses an object reference.
                    // A null reference is a valid empty backdrop/marker, not a
                    // bitmap payload. Nonempty VtPict objects remain unsupported.
                    if self.i32()? != -1 {
                        return Err(());
                    }
                } else {
                    self.object()?;
                }
                self.base("VtObject")?;
            }
            "VtFont" => {
                let object = self.object()?;
                let Node::String(family) = object.as_ref() else {
                    return Err(());
                };
                let bytes = self.bytes(14)?;
                let size = f32::from_le_bytes(bytes[..4].try_into().map_err(|_| ())?) as f64;
                if !size.is_finite() || size <= 0.0 || bytes[8..10] != [1, 0] {
                    return Err(());
                }
                let font = Font {
                    family: family.clone(),
                    size,
                    style: u16::from_le_bytes(bytes[4..6].try_into().map_err(|_| ())?),
                    effects: u16::from_le_bytes(bytes[6..8].try_into().map_err(|_| ())?),
                    color: u32::from_le_bytes(bytes[10..14].try_into().map_err(|_| ())?),
                };
                self.base("VtObject")?;
                return Ok(Node::Font(font));
            }
            "VtTextBlock" => {
                let layout = self.bytes(12)?;
                if layout[..2] != [1, 0] {
                    return Err(());
                }
                let horizontal = u16::from_le_bytes(layout[4..6].try_into().map_err(|_| ())?);
                let vertical = u16::from_le_bytes(layout[6..8].try_into().map_err(|_| ())?);
                let orientation = u16::from_le_bytes(layout[8..10].try_into().map_err(|_| ())?);
                self.object()?;
                let font = self.object()?;
                let Node::Font(font) = font.as_ref() else {
                    return Err(());
                };
                self.u16()?;
                let margin = self.i32()?;
                let mut rect = [0; 4];
                for value in &mut rect {
                    *value = self.i32()?;
                }
                let cache_valid = self.u16()? != 0;
                let text = self.object()?;
                let value = match text.as_ref() {
                    Node::String(value) => value.clone(),
                    Node::Empty => String::new(),
                    _ => return Err(()),
                };
                let extent = [self.i32()?, self.i32()?];
                let mut paint_rect = [0; 4];
                for value in &mut paint_rect {
                    *value = self.i32()?;
                }
                self.skip(2)?;
                self.base("VtObject")?;
                return Ok(Node::Text(Text {
                    value,
                    font: font.clone(),
                    metrics: TextMetrics {
                        margin,
                        rect,
                        extent,
                        cache_valid,
                        horizontal,
                        vertical,
                        orientation,
                        paint_rect,
                    },
                }));
            }
            "VtChartText" => {
                let text = self.object()?;
                let Node::Text(text) = text.as_ref() else {
                    return Err(());
                };
                let Node::Section(section) = self.base("VtChartSection")? else {
                    return Err(());
                };
                return Ok(Node::Title(section.visible.then(|| text.clone()), section));
            }
            "VtChartTitle" | "VtChartFootnote" => {
                return self.base("VtChartText");
            }
            "VtChartSection" => {
                let external = self.i16()?;
                let internal = self.i16()?;
                if self.u16()? != 1 || external < 0 || internal < 0 {
                    return Err(());
                }
                let visible = self.u16()? != 0;
                let rect = self.rect()?;
                let location = self.u16()?;
                let object = self.object()?;
                let backdrop = match object.as_ref() {
                    Node::Backdrop(b) => Some(b.clone()),
                    Node::Empty => None,
                    _ => return Err(()),
                };
                self.base("VtObject")?;
                return Ok(Node::Section(Section {
                    external,
                    internal,
                    visible,
                    rect,
                    location,
                    backdrop,
                }));
            }
            "VtChartLegend" => {
                let font = self.object()?;
                let Node::Font(font) = font.as_ref() else {
                    return Err(());
                };
                self.skip(10)?;
                let Node::Section(section) = self.base("VtChartSection")? else {
                    return Err(());
                };
                return Ok(Node::Legend(section.visible.then(|| font.clone()), section));
            }
            "VtLight3" => {
                self.object()?;
                self.skip(10)?;
                self.base("VtObject")?;
            }
            "VtInfLight3" => {
                self.skip(16)?;
                self.base("VtObject")?;
            }
            "VtSeriesLabel" => {
                return self.base("VtTextBlock");
            }
            "VtSeriesPoint" => {
                self.object()?;
                self.skip(12)?;
                self.object()?;
                self.object()?;
                self.base("VtObject")?;
            }
            "VtTextFormat" => {
                self.base("VtObject")?;
                self.skip(2)?;
                self.object()?;
            }
            "VtValueBlock" => {
                self.object()?;
                self.object()?;
                self.skip(2)?;
                self.object()?;
                self.skip(3)?;
                return self.base("VtTextBlock");
            }
            "VtAxisScaleBlock" => {
                self.object()?;
                self.skip(4)?;
                return self.base("VtValueBlock");
            }
            "VtAxis" => {
                self.u16()?;
                let labels_visible = self.u16()? != 0;
                self.u16()?;
                let manual = self.i16()? != 0;
                let props = self.bytes(74)?;
                let tick_length =
                    f32::from_le_bytes(props[66..70].try_into().map_err(|_| ())?) as f64;
                if !tick_length.is_finite() || tick_length < 0.0 {
                    return Err(());
                }
                let mut style = AxisStyle {
                    labels_visible,
                    tick_style: 0,
                    pen: pen(&props[..22])?,
                    major_grid: pen(&props[22..44])?,
                    minor_grid: pen(&props[44..66])?,
                    tick_length,
                };
                // 첫 참조는 축 제목, 둘째 참조는 눈금/범주 레이블이다.
                self.object()?;
                let label = self.object()?;
                let label_font = axis_label_font(label.as_ref());
                self.skip(34)?;
                let axis = if manual {
                    let min = self.f64()?;
                    let max = self.f64()?;
                    let divisions = self.u16()?;
                    self.skip(6)?;
                    if !min.is_finite()
                        || !max.is_finite()
                        || max <= min
                        || !(max - min).is_finite()
                        || !(1..=100).contains(&divisions)
                    {
                        return Err(());
                    }
                    Some(Axis {
                        min,
                        max,
                        divisions,
                    })
                } else {
                    None
                };
                let tail = self.bytes(16)?;
                // WriteOn에서 percent basis/unit 뒤에 저장하는 TickStyle.
                style.tick_style = u16::from_le_bytes(tail[4..6].try_into().map_err(|_| ())?);
                self.base("VtObject")?;
                return Ok(Node::Axis(
                    axis,
                    label_font,
                    style,
                    axis_label(label.as_ref()),
                ));
            }
            "VtCLineItem" => {
                self.skip(48)?;
                self.object()?;
                self.base("VtObject")?;
            }
            "VtSurfaceDesc" => {
                self.skip(46)?;
                self.object()?;
                self.object()?;
                self.skip(2)?;
                self.object()?;
                self.object()?;
                self.skip(14)?;
                self.base("VtObject")?;
            }
            "VtChartPlot" => {
                self.object()?;
                // VtChartPlot v4의 고정 prefix: 깊이/너비 비율 뒤의
                // BarGap·xGap를 읽고, 아직 지원하지 않는 나머지 표현은 건너뛴다.
                // 공식 Plot 속성과 ReadFrom/SetBarGap/SetXGap의 저장 순서를 대조했다.
                let prefix = self.bytes(136)?;
                let gaps = [8, 12].map(|at| {
                    f32::from_le_bytes(prefix[at..at + 4].try_into().expect("bounded plot prefix"))
                        as f64
                });
                if gaps.iter().any(|gap| !gap.is_finite() || *gap < 0.0) {
                    return Err(());
                }
                self.object()?;
                let mut axes = [None; 4];
                let mut fonts = std::array::from_fn(|_| None);
                let mut axis_styles = [AxisStyle::default(); 4];
                let mut labels = std::array::from_fn(|_| None);
                for (index, (axis, font)) in axes.iter_mut().zip(&mut fonts).enumerate() {
                    let object = self.object()?;
                    let Node::Axis(value, label_font, style, label) = object.as_ref() else {
                        return Err(());
                    };
                    *axis = *value;
                    *font = label_font.clone();
                    axis_styles[index] = *style;
                    labels[index] = label.clone();
                }
                self.skip(30)?;
                self.object()?;
                self.skip(16)?;
                let Node::Section(section) = self.base("VtChartSection")? else {
                    return Err(());
                };
                return Ok(Node::Plot(Box::new((
                    axes,
                    fonts,
                    section,
                    gaps,
                    axis_styles,
                    labels,
                ))));
            }
            "VtSeries" => return self.series(),
            "VtWindow" => {
                self.base("VtObject")?;
                self.skip(2)?;
            }
            _ => return Err(()),
        }
        Ok(Node::Empty)
    }

    fn series(&mut self) -> Result<Node> {
        // Two VtPen v1 values (22 bytes each), then VtBrush v1 (18 bytes).
        let prefix = self.bytes(62)?;
        for offset in [0, 22, 44] {
            if prefix[offset..offset + 2] != [1, 0] {
                return Err(());
            }
        }
        let color = u32::from_le_bytes(prefix[10..14].try_into().map_err(|_| ())?);
        let fill = u32::from_le_bytes(prefix[50..54].try_into().map_err(|_| ())?);
        let width = f32::from_le_bytes(prefix[14..18].try_into().map_err(|_| ())?) as f64;
        if !width.is_finite() || width < 0.0 {
            return Err(());
        }
        self.object()?;
        self.object()?;
        // Guides, outline pen, point options, label offsets and the v2 name.
        self.skip(12 + 22 + 16 + 16)?;
        self.object()?;
        self.object()?;
        let label = self.object()?;
        let legend = match label.as_ref() {
            Node::Text(text) => Some(text.clone()),
            Node::Empty => None,
            _ => return Err(()),
        };
        self.skip(2)?;
        self.object()?;
        self.object()?;
        let secondary = self.i16()? != 0;
        self.skip(2)?;
        if self.u16()? != 2 {
            // Inline VtMarker v2.
            return Err(());
        }
        self.skip(34)?;
        self.object()?;
        self.skip(2 + 8 + 12)?;
        let index = self.u16()? as usize;
        let count = self.u16()? as usize;
        if count != 27 {
            return Err(());
        }
        let mut selectors = Vec::with_capacity(count);
        for _ in 0..count {
            selectors.push(self.i16()?);
        }
        let extra = self.u16()? as usize;
        if extra > 27 {
            return Err(());
        }
        self.skip(extra * 2 + 10)?;
        self.base("VtObject")?;
        Ok(Node::Series(Series {
            color,
            fill,
            width,
            secondary,
            index,
            selectors,
            legend,
        }))
    }
}

pub(super) fn parse(data: &[u8]) -> Option<Presentation> {
    parse_inner(data).ok()
}

fn parse_inner(data: &[u8]) -> Result<Presentation> {
    if data.len() > 16 * 1024 * 1024 {
        return Err(());
    }
    // Fixed legacy Contents header, not a fixture-specific object offset.
    let mut reader = Reader {
        data,
        at: 40,
        types: BTreeMap::new(),
        objects: BTreeMap::new(),
        depth: 0,
        remaining: 262144,
    };
    let (name, version) = reader.class()?;
    if name != "VtChart" || version != 6 {
        return Err(());
    }
    reader.object()?;
    reader.skip(2)?;
    let extent = reader.rect()?;
    let backdrop = reader.object()?;
    let backdrop = match backdrop.as_ref() {
        Node::Backdrop(b) => Some(b.clone()),
        Node::Empty => None,
        _ => return Err(()),
    };
    reader.object()?;
    let legend = reader.object()?;
    let plot = reader.object()?;
    let series = reader.object()?;
    let title = reader.object()?;
    reader.object()?;
    reader.skip(6)?;
    reader.object()?;
    reader.skip(10)?;
    let chart_type = reader.i16()?;
    reader.skip(4)?;
    reader.base("VtWindow")?;
    if !matches!(chart_type, 1 | 9) || reader.at != data.len() {
        return Err(());
    }
    let Node::Plot(plot) = plot.as_ref() else {
        return Err(());
    };
    let (axes, label_fonts, plot_section, gaps, axis_styles, labels) = plot.as_ref();
    let (title, title_section) = match title.as_ref() {
        Node::Title(text, section) => (text.clone(), Some(section.clone())),
        Node::Empty => (None, None),
        _ => return Err(()),
    };
    let (legend_font, legend_section) = match legend.as_ref() {
        Node::Legend(font, section) => (font.clone(), Some(section.clone())),
        Node::Empty => (None, None),
        _ => return Err(()),
    };
    let Node::Array(items) = series.as_ref() else {
        return Err(());
    };
    if items.is_empty() || items.len() > 64 {
        return Err(());
    }
    let mut styles = Vec::with_capacity(items.len());
    for (index, item) in items.iter().enumerate() {
        let Node::Series(style) = item.as_ref() else {
            return Err(());
        };
        // 저장 차트 종류에 대응하는 painter를 사용한다.
        // 막대형(1)은 column만, 혼합형(9)은 column/line만 지원한다.
        let painter = style.selectors[chart_type as usize];
        if style.index != index || !(painter == 1 || chart_type == 9 && painter == 6) {
            return Err(());
        }
        styles.push(style.clone());
    }
    if chart_type == 9
        && (!styles.iter().any(|s| s.selectors[9] == 1)
            || !styles.iter().any(|s| s.selectors[9] == 6))
    {
        return Err(());
    }
    for rect in [extent, plot_section.rect] {
        if rect[2] <= rect[0] || rect[3] <= rect[1] {
            return Err(());
        }
    }
    Ok(Presentation {
        chart_type: chart_type as usize,
        axes: [
            axes[1].ok_or(())?,
            if styles.iter().any(|series| series.secondary) {
                axes[2].ok_or(())?
            } else {
                axes[1].ok_or(())?
            },
        ],
        series: styles,
        extent,
        plot: plot_section.rect,
        title,
        legend_font,
        label_fonts: label_fonts.clone(),
        bar_gap: gaps[0],
        x_gap: gaps[1],
        backdrop,
        title_section,
        legend_section,
        plot_section: plot_section.clone(),
        axis_styles: *axis_styles,
        labels: labels.clone(),
    })
}