office2pdf 0.6.3

Convert DOCX, XLSX, and PPTX files to PDF using pure Rust
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
use std::collections::{HashMap, HashSet};
use std::io::Cursor;

use crate::ir::Chart;
use crate::parser::chart::parse_chart_xml;
use crate::parser::xml_util;

/// Extract charts from the XLSX ZIP with their anchor positions per sheet.
///
/// Returns a map from sheet name → list of (anchor_row, Chart).
/// Charts with drawing anchors get positioned at their anchor row.
/// Charts without anchors (no drawing reference found) use `u32::MAX`
/// as a sentinel to place them at the end of the sheet.
pub(super) fn extract_charts_with_anchors(data: &[u8]) -> HashMap<String, Vec<(u32, Chart)>> {
    let Ok(mut archive) = crate::parser::open_zip(data) else {
        return HashMap::new();
    };

    // Step 1: Read workbook.xml to get sheet name → rId mapping
    let workbook_xml = read_zip_entry_string(&mut archive, "xl/workbook.xml");
    let sheet_rids = parse_workbook_sheet_rids(&workbook_xml);

    // Step 2: Read workbook rels to get rId → sheet file path
    let workbook_rels_xml = read_zip_entry_string(&mut archive, "xl/_rels/workbook.xml.rels");
    let rid_to_target = parse_rels_targets(&workbook_rels_xml);

    // Step 3: For each sheet, find its drawing and extract chart anchors
    let mut result: HashMap<String, Vec<(u32, Chart)>> = HashMap::new();

    for (sheet_name, sheet_rid) in &sheet_rids {
        let Some(sheet_target) = rid_to_target.get(sheet_rid) else {
            continue;
        };
        // Sheet target is relative to xl/ (e.g., "worksheets/sheet1.xml")
        let sheet_full_path = format!("xl/{sheet_target}");
        let sheet_filename = sheet_full_path.rsplit('/').next().unwrap_or(sheet_target);
        let sheet_rels_path = format!("xl/worksheets/_rels/{sheet_filename}.rels");

        let sheet_rels_xml = read_zip_entry_string(&mut archive, &sheet_rels_path);
        if sheet_rels_xml.is_empty() {
            continue;
        }

        // Find drawing relationship
        let drawing_targets = parse_rels_by_type(&sheet_rels_xml, "drawing");
        for drawing_target in &drawing_targets {
            // Resolve relative path from worksheets/ to drawings/
            let drawing_path = resolve_relative_xl_path("xl/worksheets", drawing_target);
            let drawing_xml = read_zip_entry_string(&mut archive, &drawing_path);
            if drawing_xml.is_empty() {
                continue;
            }

            // Parse drawing for chart anchor positions
            let anchors = parse_drawing_chart_anchors(&drawing_xml);

            // Find drawing rels for chart rId resolution
            let drawing_filename = drawing_path.rsplit('/').next().unwrap_or(&drawing_path);
            let drawing_dir = drawing_path
                .rsplit_once('/')
                .map(|(d, _)| d)
                .unwrap_or("xl/drawings");
            let drawing_rels_path = format!("{drawing_dir}/_rels/{drawing_filename}.rels");
            let drawing_rels_xml = read_zip_entry_string(&mut archive, &drawing_rels_path);
            let drawing_rid_targets = parse_rels_targets(&drawing_rels_xml);

            for (anchor_row, chart_rid) in &anchors {
                let Some(chart_target) = drawing_rid_targets.get(chart_rid) else {
                    continue;
                };
                let chart_path = resolve_relative_xl_path(drawing_dir, chart_target);
                let chart_xml = read_zip_entry_string(&mut archive, &chart_path);
                if let Some(chart) = parse_chart_xml(&chart_xml) {
                    result
                        .entry(sheet_name.clone())
                        .or_default()
                        .push((*anchor_row, chart));
                }
            }
        }
    }

    // Step 4: Find any charts not associated with drawings (orphaned charts)
    // and assign them to the first sheet with u32::MAX sentinel
    let all_positioned_chart_paths: HashSet<String> = result
        .values()
        .flatten()
        .filter_map(|_| None::<String>) // We don't track paths, just check coverage below
        .collect();
    let _ = all_positioned_chart_paths; // consumed

    // Scan for chart XML files and check if they were captured by drawing anchors
    let chart_paths: Vec<String> = (0..archive.len())
        .filter_map(|i| {
            let entry = archive.by_index(i).ok()?;
            let name = entry.name().to_string();
            if name.starts_with("xl/charts/chart") && name.ends_with(".xml") {
                Some(name)
            } else {
                None
            }
        })
        .collect();

    // Count total positioned charts
    let positioned_count: usize = result.values().map(|v| v.len()).sum();

    if chart_paths.len() > positioned_count {
        // Some charts weren't found via drawing anchors — parse them as unanchored
        let positioned_charts: HashSet<String> = collect_positioned_chart_paths(&result, data);

        let first_sheet = sheet_rids
            .first()
            .map(|(name, _)| name.clone())
            .unwrap_or_else(|| "Sheet1".to_string());

        for path in &chart_paths {
            if positioned_charts.contains(path) {
                continue;
            }
            let chart_xml = read_zip_entry_string(&mut archive, path);
            if let Some(chart) = parse_chart_xml(&chart_xml) {
                result
                    .entry(first_sheet.clone())
                    .or_default()
                    .push((u32::MAX, chart));
            }
        }
    }

    result
}

/// Collect the set of chart XML paths that were already positioned via drawing anchors.
pub(super) fn collect_positioned_chart_paths(
    chart_map: &HashMap<String, Vec<(u32, Chart)>>,
    data: &[u8],
) -> HashSet<String> {
    // Re-trace the drawing → chart resolution to find which chart paths are covered.
    // This is intentionally conservative — if we can't determine the path, we skip.
    let Ok(mut archive) = crate::parser::open_zip(data) else {
        return HashSet::new();
    };
    let mut positioned = HashSet::new();

    let workbook_xml = read_zip_entry_string(&mut archive, "xl/workbook.xml");
    let sheet_rids = parse_workbook_sheet_rids(&workbook_xml);
    let workbook_rels_xml = read_zip_entry_string(&mut archive, "xl/_rels/workbook.xml.rels");
    let rid_to_target = parse_rels_targets(&workbook_rels_xml);

    for (sheet_name, sheet_rid) in &sheet_rids {
        if !chart_map.contains_key(sheet_name) {
            continue;
        }
        let Some(sheet_target) = rid_to_target.get(sheet_rid) else {
            continue;
        };
        let sheet_full_path = format!("xl/{sheet_target}");
        let sheet_filename = sheet_full_path.rsplit('/').next().unwrap_or(sheet_target);
        let sheet_rels_path = format!("xl/worksheets/_rels/{sheet_filename}.rels");
        let sheet_rels_xml = read_zip_entry_string(&mut archive, &sheet_rels_path);
        let drawing_targets = parse_rels_by_type(&sheet_rels_xml, "drawing");

        for drawing_target in &drawing_targets {
            let drawing_path = resolve_relative_xl_path("xl/worksheets", drawing_target);
            let drawing_xml = read_zip_entry_string(&mut archive, &drawing_path);
            let anchors = parse_drawing_chart_anchors(&drawing_xml);
            let drawing_filename = drawing_path.rsplit('/').next().unwrap_or(&drawing_path);
            let drawing_dir = drawing_path
                .rsplit_once('/')
                .map(|(d, _)| d)
                .unwrap_or("xl/drawings");
            let drawing_rels_path = format!("{drawing_dir}/_rels/{drawing_filename}.rels");
            let drawing_rels_xml = read_zip_entry_string(&mut archive, &drawing_rels_path);
            let drawing_rid_targets = parse_rels_targets(&drawing_rels_xml);

            for (_row, chart_rid) in &anchors {
                if let Some(chart_target) = drawing_rid_targets.get(chart_rid) {
                    positioned.insert(resolve_relative_xl_path(drawing_dir, chart_target));
                }
            }
        }
    }

    positioned
}

/// Read a ZIP entry as a string. Returns empty string if not found.
pub(super) fn read_zip_entry_string(
    archive: &mut zip::ZipArchive<Cursor<&[u8]>>,
    path: &str,
) -> String {
    let Ok(mut entry) = archive.by_name(path) else {
        return String::new();
    };
    let mut xml = String::new();
    let _ = std::io::Read::read_to_string(&mut entry, &mut xml);
    xml
}

/// Parse workbook.xml to extract sheet name → rId pairs (preserving order).
pub(super) fn parse_workbook_sheet_rids(xml: &str) -> Vec<(String, String)> {
    let mut result = Vec::new();
    let mut reader = quick_xml::Reader::from_str(xml);

    loop {
        match reader.read_event() {
            Ok(quick_xml::events::Event::Start(ref e))
            | Ok(quick_xml::events::Event::Empty(ref e)) => {
                if e.local_name().as_ref() == b"sheet" {
                    let mut name = None;
                    let mut rid = None;
                    for attr in e.attributes().flatten() {
                        match attr.key.local_name().as_ref() {
                            b"name" => {
                                if let Ok(v) = attr.unescape_value() {
                                    name = Some(v.to_string());
                                }
                            }
                            b"id" => {
                                if let Ok(v) = attr.unescape_value() {
                                    rid = Some(v.to_string());
                                }
                            }
                            _ => {}
                        }
                    }
                    if let (Some(n), Some(r)) = (name, rid) {
                        result.push((n, r));
                    }
                }
            }
            Ok(quick_xml::events::Event::Eof) => break,
            Err(_) => break,
            _ => {}
        }
    }

    result
}

/// Parse a .rels file to get Id → Target mapping.
pub(super) fn parse_rels_targets(xml: &str) -> HashMap<String, String> {
    xml_util::parse_rels_id_target(xml)
}

/// Parse a .rels file and return targets whose Type contains the given substring.
pub(super) fn parse_rels_by_type(xml: &str, type_substring: &str) -> Vec<String> {
    let mut targets = Vec::new();
    let mut reader = quick_xml::Reader::from_str(xml);

    loop {
        match reader.read_event() {
            Ok(quick_xml::events::Event::Start(ref e))
            | Ok(quick_xml::events::Event::Empty(ref e)) => {
                if e.local_name().as_ref() == b"Relationship" {
                    let mut target = None;
                    let mut matches_type = false;
                    for attr in e.attributes().flatten() {
                        match attr.key.local_name().as_ref() {
                            b"Target" => {
                                if let Ok(v) = attr.unescape_value() {
                                    target = Some(v.to_string());
                                }
                            }
                            b"Type" => {
                                if let Ok(v) = attr.unescape_value()
                                    && v.contains(type_substring)
                                {
                                    matches_type = true;
                                }
                            }
                            _ => {}
                        }
                    }
                    if matches_type && let Some(t) = target {
                        targets.push(t);
                    }
                }
            }
            Ok(quick_xml::events::Event::Eof) => break,
            Err(_) => break,
            _ => {}
        }
    }

    targets
}

/// Resolve a relative path (like `../drawings/drawing1.xml`) against a base directory.
pub(super) fn resolve_relative_xl_path(base_dir: &str, relative: &str) -> String {
    xml_util::resolve_relative_path(base_dir, relative)
}

/// Parse drawing XML for chart anchor positions.
/// Returns (anchor_row, chart_rId) pairs from `<xdr:twoCellAnchor>` elements.
pub(super) fn parse_drawing_chart_anchors(xml: &str) -> Vec<(u32, String)> {
    let mut result = Vec::new();
    let mut reader = quick_xml::Reader::from_str(xml);

    let mut in_two_cell_anchor = false;
    let mut in_from = false;
    let mut in_row = false;
    let mut anchor_row: Option<u32> = None;
    let mut chart_rid: Option<String> = None;
    let mut in_graphic_data = false;

    loop {
        match reader.read_event() {
            Ok(quick_xml::events::Event::Start(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"twoCellAnchor" | b"oneCellAnchor" => {
                        in_two_cell_anchor = true;
                        anchor_row = None;
                        chart_rid = None;
                    }
                    b"from" if in_two_cell_anchor => {
                        in_from = true;
                    }
                    b"row" if in_from => {
                        in_row = true;
                    }
                    b"graphicData" if in_two_cell_anchor => {
                        for attr in e.attributes().flatten() {
                            if attr.key.local_name().as_ref() == b"uri"
                                && let Ok(val) = attr.unescape_value()
                                && val.contains("chart")
                            {
                                in_graphic_data = true;
                            }
                        }
                    }
                    _ => {}
                }
            }
            Ok(quick_xml::events::Event::Empty(ref e)) => {
                let local = e.local_name();
                if in_graphic_data && local.as_ref() == b"chart" {
                    for attr in e.attributes().flatten() {
                        if (attr.key.as_ref() == b"r:id" || attr.key.local_name().as_ref() == b"id")
                            && let Ok(val) = attr.unescape_value()
                        {
                            chart_rid = Some(val.to_string());
                        }
                    }
                }
            }
            Ok(quick_xml::events::Event::Text(ref t)) => {
                if in_row
                    && let Ok(s) = t.xml_content()
                    && let Ok(row) = s.trim().parse::<u32>()
                {
                    anchor_row = Some(row);
                }
            }
            Ok(quick_xml::events::Event::End(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"twoCellAnchor" | b"oneCellAnchor" => {
                        if let (Some(row), Some(rid)) = (anchor_row.take(), chart_rid.take()) {
                            result.push((row, rid));
                        }
                        in_two_cell_anchor = false;
                        in_from = false;
                        in_graphic_data = false;
                    }
                    b"from" => {
                        in_from = false;
                    }
                    b"row" => {
                        in_row = false;
                    }
                    b"graphicData" => {
                        in_graphic_data = false;
                    }
                    _ => {}
                }
            }
            Ok(quick_xml::events::Event::Eof) => break,
            Err(_) => break,
            _ => {}
        }
    }

    result
}

// ── Drawing images ──────────────────────────────────────────────────────

/// A picture anchor from a worksheet drawing, in raw drawing coordinates.
/// Rows/columns are 0-indexed as in the XML; offsets and extents are EMU.
pub(super) struct RawImageAnchor {
    pub(super) from_row: u32,
    pub(super) from_col: u32,
    pub(super) from_col_off_emu: i64,
    pub(super) from_row_off_emu: i64,
    /// twoCellAnchor bottom-right corner: (col, col_off, row, row_off).
    pub(super) to: Option<(u32, i64, u32, i64)>,
    /// oneCellAnchor extent (cx, cy).
    pub(super) ext_emu: Option<(i64, i64)>,
    pub(super) data: Vec<u8>,
    pub(super) format: crate::ir::ImageFormat,
}

/// Extract anchored pictures per sheet from worksheet drawings.
/// Metafiles (EMF/WMF) are converted to SVG; unknown formats are skipped.
pub(super) fn extract_images_with_anchors(data: &[u8]) -> HashMap<String, Vec<RawImageAnchor>> {
    let Ok(mut archive) = crate::parser::open_zip(data) else {
        return HashMap::new();
    };

    let workbook_xml = read_zip_entry_string(&mut archive, "xl/workbook.xml");
    let sheet_rids = parse_workbook_sheet_rids(&workbook_xml);
    let workbook_rels_xml = read_zip_entry_string(&mut archive, "xl/_rels/workbook.xml.rels");
    let rid_to_target = parse_rels_targets(&workbook_rels_xml);

    let mut result: HashMap<String, Vec<RawImageAnchor>> = HashMap::new();

    for (sheet_name, sheet_rid) in &sheet_rids {
        let Some(sheet_target) = rid_to_target.get(sheet_rid) else {
            continue;
        };
        let sheet_full_path = format!("xl/{sheet_target}");
        let sheet_filename = sheet_full_path.rsplit('/').next().unwrap_or(sheet_target);
        let sheet_rels_path = format!("xl/worksheets/_rels/{sheet_filename}.rels");
        let sheet_rels_xml = read_zip_entry_string(&mut archive, &sheet_rels_path);
        if sheet_rels_xml.is_empty() {
            continue;
        }

        for drawing_target in &parse_rels_by_type(&sheet_rels_xml, "drawing") {
            let drawing_path = resolve_relative_xl_path("xl/worksheets", drawing_target);
            let drawing_xml = read_zip_entry_string(&mut archive, &drawing_path);
            if drawing_xml.is_empty() {
                continue;
            }

            let anchors = parse_drawing_image_anchors(&drawing_xml);
            if anchors.is_empty() {
                continue;
            }

            let drawing_filename = drawing_path.rsplit('/').next().unwrap_or(&drawing_path);
            let drawing_dir = drawing_path
                .rsplit_once('/')
                .map(|(dir, _)| dir)
                .unwrap_or("xl/drawings");
            let drawing_rels_path = format!("{drawing_dir}/_rels/{drawing_filename}.rels");
            let drawing_rels_xml = read_zip_entry_string(&mut archive, &drawing_rels_path);
            let rid_to_media = parse_rels_targets(&drawing_rels_xml);

            for (geometry, rid) in anchors {
                let Some(media_target) = rid_to_media.get(&rid) else {
                    continue;
                };
                let media_path = resolve_relative_xl_path(drawing_dir, media_target);
                let Some(bytes) = read_zip_entry_bytes(&mut archive, &media_path) else {
                    continue;
                };
                let Some((data, format)) = decode_media(&media_path, bytes) else {
                    continue;
                };
                result
                    .entry(sheet_name.clone())
                    .or_default()
                    .push(RawImageAnchor {
                        from_row: geometry.from_row,
                        from_col: geometry.from_col,
                        from_col_off_emu: geometry.from_col_off_emu,
                        from_row_off_emu: geometry.from_row_off_emu,
                        to: geometry.to,
                        ext_emu: geometry.ext_emu,
                        data,
                        format,
                    });
            }
        }
    }

    result
}

fn read_zip_entry_bytes<R: std::io::Read + std::io::Seek>(
    archive: &mut zip::ZipArchive<R>,
    path: &str,
) -> Option<Vec<u8>> {
    use std::io::Read;
    let mut file = archive.by_name(path).ok()?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer).ok()?;
    Some(buffer)
}

/// Map media bytes to a renderable (data, format) pair; metafiles are
/// converted to SVG.
fn decode_media(path: &str, bytes: Vec<u8>) -> Option<(Vec<u8>, crate::ir::ImageFormat)> {
    use crate::ir::ImageFormat;
    let extension: String = path.rsplit('.').next().unwrap_or("").to_ascii_lowercase();
    match extension.as_str() {
        "png" => Some((bytes, ImageFormat::Png)),
        "jpg" | "jpeg" => Some((bytes, ImageFormat::Jpeg)),
        "gif" => Some((bytes, ImageFormat::Gif)),
        "bmp" => Some((bytes, ImageFormat::Bmp)),
        "tif" | "tiff" => Some((bytes, ImageFormat::Tiff)),
        "svg" => Some((bytes, ImageFormat::Svg)),
        "emf" => crate::parser::emf::convert_emf_to_svg(&bytes).map(|svg| (svg, ImageFormat::Svg)),
        "wmf" => crate::parser::wmf::convert_wmf_to_svg(&bytes).map(|svg| (svg, ImageFormat::Svg)),
        _ => None,
    }
}

/// Geometry captured from a single pic anchor before media resolution.
pub(super) struct ImageAnchorGeometry {
    pub(super) from_row: u32,
    pub(super) from_col: u32,
    pub(super) from_col_off_emu: i64,
    pub(super) from_row_off_emu: i64,
    pub(super) to: Option<(u32, i64, u32, i64)>,
    pub(super) ext_emu: Option<(i64, i64)>,
}

/// Parse `<xdr:pic>` anchors from a worksheet drawing: anchor geometry plus
/// the blip relationship id.
pub(super) fn parse_drawing_image_anchors(xml: &str) -> Vec<(ImageAnchorGeometry, String)> {
    #[derive(Default, Clone, Copy)]
    struct Corner {
        col: u32,
        col_off: i64,
        row: u32,
        row_off: i64,
    }

    let mut result: Vec<(ImageAnchorGeometry, String)> = Vec::new();
    let mut reader = quick_xml::Reader::from_str(xml);

    let mut in_anchor = false;
    let mut in_pic = false;
    let mut corner_target: Option<bool> = None; // Some(true)=from, Some(false)=to
    let mut current_field: Option<&'static str> = None;
    let mut from = Corner::default();
    let mut to: Option<Corner> = None;
    let mut ext_emu: Option<(i64, i64)> = None;
    let mut blip_rid: Option<String> = None;

    loop {
        match reader.read_event() {
            Ok(quick_xml::events::Event::Start(ref e)) => match e.local_name().as_ref() {
                b"twoCellAnchor" | b"oneCellAnchor" | b"absoluteAnchor" => {
                    in_anchor = true;
                    in_pic = false;
                    from = Corner::default();
                    to = None;
                    ext_emu = None;
                    blip_rid = None;
                }
                b"from" if in_anchor => corner_target = Some(true),
                b"to" if in_anchor => {
                    corner_target = Some(false);
                    to = Some(Corner::default());
                }
                b"col" if corner_target.is_some() => current_field = Some("col"),
                b"colOff" if corner_target.is_some() => current_field = Some("colOff"),
                b"row" if corner_target.is_some() => current_field = Some("row"),
                b"rowOff" if corner_target.is_some() => current_field = Some("rowOff"),
                b"pic" if in_anchor => in_pic = true,
                _ => {}
            },
            Ok(quick_xml::events::Event::Empty(ref e)) => {
                let local = e.local_name();
                if in_anchor && local.as_ref() == b"ext" && !in_pic && to.is_none() {
                    // oneCellAnchor extent (the pic's own a:ext lives inside
                    // xfrm, which we ignore by requiring !in_pic).
                    let mut cx: i64 = 0;
                    let mut cy: i64 = 0;
                    for attr in e.attributes().flatten() {
                        let value: i64 = attr
                            .unescape_value()
                            .ok()
                            .and_then(|v| v.parse().ok())
                            .unwrap_or(0);
                        match attr.key.local_name().as_ref() {
                            b"cx" => cx = value,
                            b"cy" => cy = value,
                            _ => {}
                        }
                    }
                    ext_emu = Some((cx, cy));
                }
                if in_pic && local.as_ref() == b"blip" {
                    for attr in e.attributes().flatten() {
                        if attr.key.local_name().as_ref() == b"embed"
                            && let Ok(val) = attr.unescape_value()
                        {
                            blip_rid = Some(val.to_string());
                        }
                    }
                }
            }
            Ok(quick_xml::events::Event::Text(ref t)) => {
                if let (Some(is_from), Some(field)) = (corner_target, current_field)
                    && let Ok(text) = t.xml_content()
                    && let Ok(number) = text.trim().parse::<i64>()
                {
                    let corner: &mut Corner = if is_from {
                        &mut from
                    } else {
                        to.as_mut().expect("to corner initialized on <to>")
                    };
                    match field {
                        "col" => corner.col = number as u32,
                        "colOff" => corner.col_off = number,
                        "row" => corner.row = number as u32,
                        "rowOff" => corner.row_off = number,
                        _ => {}
                    }
                }
            }
            Ok(quick_xml::events::Event::End(ref e)) => match e.local_name().as_ref() {
                b"twoCellAnchor" | b"oneCellAnchor" | b"absoluteAnchor" => {
                    if in_pic && let Some(rid) = blip_rid.take() {
                        result.push((
                            ImageAnchorGeometry {
                                from_row: from.row,
                                from_col: from.col,
                                from_col_off_emu: from.col_off,
                                from_row_off_emu: from.row_off,
                                to: to.map(|c| (c.col, c.col_off, c.row, c.row_off)),
                                ext_emu,
                            },
                            rid,
                        ));
                    }
                    in_anchor = false;
                    in_pic = false;
                    corner_target = None;
                }
                b"from" | b"to" => corner_target = None,
                b"col" | b"colOff" | b"row" | b"rowOff" => current_field = None,
                _ => {}
            },
            Ok(quick_xml::events::Event::Eof) => break,
            Err(_) => break,
            _ => {}
        }
    }

    result
}

// ── Drawing text boxes ──────────────────────────────────────────────────

/// A text-box shape from a worksheet drawing, in raw drawing coordinates.
pub(super) struct RawTextBoxAnchor {
    pub(super) geometry: ImageAnchorGeometry,
    pub(super) paragraphs: Vec<crate::ir::Paragraph>,
    pub(super) fill: Option<crate::ir::Color>,
    pub(super) border: Option<crate::ir::BorderSide>,
    pub(super) vertical_center: bool,
}

/// Extract anchored text boxes per sheet from worksheet drawings.
pub(super) fn extract_text_boxes_with_anchors(
    data: &[u8],
) -> HashMap<String, Vec<RawTextBoxAnchor>> {
    let Ok(mut archive) = crate::parser::open_zip(data) else {
        return HashMap::new();
    };

    let workbook_xml = read_zip_entry_string(&mut archive, "xl/workbook.xml");
    let sheet_rids = parse_workbook_sheet_rids(&workbook_xml);
    let workbook_rels_xml = read_zip_entry_string(&mut archive, "xl/_rels/workbook.xml.rels");
    let rid_to_target = parse_rels_targets(&workbook_rels_xml);

    let mut result: HashMap<String, Vec<RawTextBoxAnchor>> = HashMap::new();

    for (sheet_name, sheet_rid) in &sheet_rids {
        let Some(sheet_target) = rid_to_target.get(sheet_rid) else {
            continue;
        };
        let sheet_full_path = format!("xl/{sheet_target}");
        let sheet_filename = sheet_full_path.rsplit('/').next().unwrap_or(sheet_target);
        let sheet_rels_path = format!("xl/worksheets/_rels/{sheet_filename}.rels");
        let sheet_rels_xml = read_zip_entry_string(&mut archive, &sheet_rels_path);
        if sheet_rels_xml.is_empty() {
            continue;
        }

        for drawing_target in &parse_rels_by_type(&sheet_rels_xml, "drawing") {
            let drawing_path = resolve_relative_xl_path("xl/worksheets", drawing_target);
            let drawing_xml = read_zip_entry_string(&mut archive, &drawing_path);
            if drawing_xml.is_empty() {
                continue;
            }
            let boxes = parse_drawing_text_boxes(&drawing_xml);
            if !boxes.is_empty() {
                result.entry(sheet_name.clone()).or_default().extend(boxes);
            }
        }
    }

    result
}

/// Resolve a drawing color element to RGB. Scheme colors fall back to the
/// standard light/dark mapping (worksheet drawings rarely restyle them).
fn drawing_color(name: &[u8], val: &str) -> Option<crate::ir::Color> {
    use crate::ir::Color;
    match name {
        b"srgbClr" => {
            let v = u32::from_str_radix(val, 16).ok()?;
            Some(Color::new(
                ((v >> 16) & 0xFF) as u8,
                ((v >> 8) & 0xFF) as u8,
                (v & 0xFF) as u8,
            ))
        }
        b"schemeClr" => match val {
            "lt1" | "bg1" | "lt2" | "bg2" => Some(Color::new(0xFF, 0xFF, 0xFF)),
            "dk1" | "tx1" | "dk2" | "tx2" => Some(Color::new(0, 0, 0)),
            _ => None,
        },
        _ => None,
    }
}

/// Parse `<xdr:sp>` text boxes from a worksheet drawing.
pub(super) fn parse_drawing_text_boxes(xml: &str) -> Vec<RawTextBoxAnchor> {
    use crate::ir::{
        Alignment, BorderLineStyle, BorderSide, Paragraph, ParagraphStyle, Run, TextStyle,
    };

    #[derive(Default, Clone, Copy)]
    struct Corner {
        col: u32,
        col_off: i64,
        row: u32,
        row_off: i64,
    }

    let mut result: Vec<RawTextBoxAnchor> = Vec::new();
    let mut reader = quick_xml::Reader::from_str(xml);

    let mut in_anchor = false;
    let mut in_sp = false;
    let mut in_tx_body = false;
    let mut in_sp_fill = false;
    let mut in_line = false;
    let mut corner_target: Option<bool> = None;
    let mut current_field: Option<&'static str> = None;
    let mut from = Corner::default();
    let mut to: Option<Corner> = None;
    let mut ext_emu: Option<(i64, i64)> = None;

    let mut paragraphs: Vec<Paragraph> = Vec::new();
    let mut current_para: Option<Paragraph> = None;
    let mut current_style = TextStyle::default();
    let mut in_run = false;
    let mut in_text = false;
    let mut fill: Option<crate::ir::Color> = None;
    let mut border_color: Option<crate::ir::Color> = None;
    let mut border_width: f64 = 0.75;
    let mut vertical_center = false;
    // Color element opened with children (e.g. <a:schemeClr><a:shade/>...):
    // committed on its End event after transforms apply.
    let mut pending_color: Option<crate::ir::Color> = None;

    loop {
        match reader.read_event() {
            Ok(quick_xml::events::Event::Start(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"twoCellAnchor" | b"oneCellAnchor" | b"absoluteAnchor" => {
                        in_anchor = true;
                        in_sp = false;
                        from = Corner::default();
                        to = None;
                        ext_emu = None;
                        paragraphs.clear();
                        fill = None;
                        border_color = None;
                        border_width = 0.75;
                        vertical_center = false;
                    }
                    b"from" if in_anchor => corner_target = Some(true),
                    b"to" if in_anchor => {
                        corner_target = Some(false);
                        to = Some(Corner::default());
                    }
                    b"col" if corner_target.is_some() => current_field = Some("col"),
                    b"colOff" if corner_target.is_some() => current_field = Some("colOff"),
                    b"row" if corner_target.is_some() => current_field = Some("row"),
                    b"rowOff" if corner_target.is_some() => current_field = Some("rowOff"),
                    b"sp" if in_anchor => in_sp = true,
                    b"txBody" if in_sp => in_tx_body = true,
                    b"solidFill" if in_sp && !in_tx_body && !in_line => in_sp_fill = true,
                    b"ln" if in_sp && !in_tx_body => {
                        in_line = true;
                        for attr in e.attributes().flatten() {
                            if attr.key.local_name().as_ref() == b"w"
                                && let Ok(v) = attr.unescape_value()
                                && let Ok(w) = v.parse::<f64>()
                            {
                                border_width = w / 12_700.0;
                            }
                        }
                    }
                    b"p" if in_tx_body => {
                        current_para = Some(Paragraph {
                            style: ParagraphStyle::default(),
                            runs: Vec::new(),
                        });
                    }
                    b"r" if current_para.is_some() => {
                        in_run = true;
                        current_style = TextStyle::default();
                    }
                    b"rPr" if in_run => {
                        for attr in e.attributes().flatten() {
                            match attr.key.local_name().as_ref() {
                                b"sz" => {
                                    if let Ok(v) = attr.unescape_value()
                                        && let Ok(sz) = v.parse::<f64>()
                                    {
                                        current_style.font_size = Some(sz / 100.0);
                                    }
                                }
                                b"b" if attr.unescape_value().ok().as_deref() == Some("1") => {
                                    current_style.bold = Some(true);
                                }
                                b"i" if attr.unescape_value().ok().as_deref() == Some("1") => {
                                    current_style.italic = Some(true);
                                }
                                _ => {}
                            }
                        }
                    }
                    b"t" if in_run => in_text = true,
                    b"srgbClr" | b"schemeClr" => {
                        let mut val = String::new();
                        for attr in e.attributes().flatten() {
                            if attr.key.local_name().as_ref() == b"val"
                                && let Ok(v) = attr.unescape_value()
                            {
                                val = v.to_string();
                            }
                        }
                        pending_color = drawing_color(local.as_ref(), &val);
                    }
                    _ => {}
                }
            }
            Ok(quick_xml::events::Event::Empty(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"bodyPr" if in_tx_body || in_sp => {
                        for attr in e.attributes().flatten() {
                            if attr.key.local_name().as_ref() == b"anchor"
                                && attr.unescape_value().ok().as_deref() == Some("ctr")
                            {
                                vertical_center = true;
                            }
                        }
                    }
                    b"pPr" if current_para.is_some() => {
                        for attr in e.attributes().flatten() {
                            if attr.key.local_name().as_ref() == b"algn"
                                && let Ok(v) = attr.unescape_value()
                                && let Some(para) = current_para.as_mut()
                            {
                                para.style.alignment = match v.as_ref() {
                                    "ctr" => Some(Alignment::Center),
                                    "r" => Some(Alignment::Right),
                                    "just" => Some(Alignment::Justify),
                                    _ => None,
                                };
                            }
                        }
                    }
                    b"shade" => {
                        if let Some(color) = pending_color.as_mut()
                            && let Some(factor) = e.attributes().flatten().find_map(|attr| {
                                (attr.key.local_name().as_ref() == b"val")
                                    .then(|| attr.unescape_value().ok())
                                    .flatten()
                                    .and_then(|v| v.parse::<f64>().ok())
                            })
                        {
                            let shade = |channel: u8| -> u8 {
                                (channel as f64 * factor / 100_000.0)
                                    .round()
                                    .clamp(0.0, 255.0) as u8
                            };
                            *color = crate::ir::Color::new(
                                shade(color.r),
                                shade(color.g),
                                shade(color.b),
                            );
                        }
                    }
                    b"srgbClr" | b"schemeClr" => {
                        let mut val = String::new();
                        for attr in e.attributes().flatten() {
                            if attr.key.local_name().as_ref() == b"val"
                                && let Ok(v) = attr.unescape_value()
                            {
                                val = v.to_string();
                            }
                        }
                        let color = drawing_color(local.as_ref(), &val);
                        if in_run {
                            if current_style.color.is_none() {
                                current_style.color = color;
                            }
                        } else if in_line {
                            if border_color.is_none() {
                                border_color = color;
                            }
                        } else if in_sp_fill && fill.is_none() {
                            fill = color;
                        }
                    }
                    b"ext" if in_anchor && !in_sp && to.is_none() => {
                        let mut cx: i64 = 0;
                        let mut cy: i64 = 0;
                        for attr in e.attributes().flatten() {
                            let value: i64 = attr
                                .unescape_value()
                                .ok()
                                .and_then(|v| v.parse().ok())
                                .unwrap_or(0);
                            match attr.key.local_name().as_ref() {
                                b"cx" => cx = value,
                                b"cy" => cy = value,
                                _ => {}
                            }
                        }
                        ext_emu = Some((cx, cy));
                    }
                    _ => {}
                }
            }
            Ok(quick_xml::events::Event::Text(ref t)) => {
                if in_text && let Ok(text) = t.xml_content() {
                    if let Some(para) = current_para.as_mut() {
                        para.runs.push(Run {
                            text: text.to_string(),
                            style: current_style.clone(),
                            href: None,
                            footnote: None,
                        });
                    }
                } else if let (Some(is_from), Some(field)) = (corner_target, current_field)
                    && let Ok(text) = t.xml_content()
                    && let Ok(number) = text.trim().parse::<i64>()
                {
                    let corner: &mut Corner = if is_from {
                        &mut from
                    } else {
                        to.as_mut().expect("to corner initialized on <to>")
                    };
                    match field {
                        "col" => corner.col = number as u32,
                        "colOff" => corner.col_off = number,
                        "row" => corner.row = number as u32,
                        "rowOff" => corner.row_off = number,
                        _ => {}
                    }
                }
            }
            Ok(quick_xml::events::Event::End(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"twoCellAnchor" | b"oneCellAnchor" | b"absoluteAnchor" => {
                        if in_sp && !paragraphs.is_empty() {
                            result.push(RawTextBoxAnchor {
                                geometry: ImageAnchorGeometry {
                                    from_row: from.row,
                                    from_col: from.col,
                                    from_col_off_emu: from.col_off,
                                    from_row_off_emu: from.row_off,
                                    to: to.map(|c| (c.col, c.col_off, c.row, c.row_off)),
                                    ext_emu,
                                },
                                paragraphs: std::mem::take(&mut paragraphs),
                                fill,
                                border: border_color.map(|color| BorderSide {
                                    width: border_width,
                                    color,
                                    style: BorderLineStyle::Solid,
                                }),
                                vertical_center,
                            });
                        }
                        in_anchor = false;
                        in_sp = false;
                        corner_target = None;
                    }
                    b"from" | b"to" => corner_target = None,
                    b"col" | b"colOff" | b"row" | b"rowOff" => current_field = None,
                    b"srgbClr" | b"schemeClr" => {
                        if let Some(color) = pending_color.take() {
                            if in_run {
                                if current_style.color.is_none() {
                                    current_style.color = Some(color);
                                }
                            } else if in_line {
                                if border_color.is_none() {
                                    border_color = Some(color);
                                }
                            } else if in_sp_fill && fill.is_none() {
                                fill = Some(color);
                            }
                        }
                    }
                    b"txBody" => in_tx_body = false,
                    b"solidFill" => in_sp_fill = false,
                    b"ln" => in_line = false,
                    b"p" => {
                        if let Some(para) = current_para.take() {
                            paragraphs.push(para);
                        }
                    }
                    b"r" => in_run = false,
                    b"t" => in_text = false,
                    _ => {}
                }
            }
            Ok(quick_xml::events::Event::Eof) => break,
            Err(_) => break,
            _ => {}
        }
    }

    result
}