xberg 1.1.0

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
//! DOCX drawing object parsing.
//!
//! This module handles extraction and parsing of drawing objects (`<w:drawing>`)
//! from DOCX documents. Drawing objects can be inline or anchored and may contain
//! images or shapes.

use crate::extractors::security::{SecurityBudget, SecurityError};
use quick_xml::Reader;
use quick_xml::events::{BytesStart, Event};
use serde::{Deserialize, Serialize};

/// A drawing object extracted from `<w:drawing>`.
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct Drawing {
    /// Whether the drawing is inline (in text flow) or anchored (floating).
    pub drawing_type: DrawingType,
    /// Physical dimensions in EMUs (English Metric Units).
    pub extent: Option<Extent>,
    /// Document properties such as ID, name, and alt-text description.
    pub doc_properties: Option<DocProperties>,
    /// Relationship ID (`r:embed`) referencing the image part in the DOCX package.
    pub image_ref: Option<String>,
    /// Text extracted from a text box hosted by this drawing (#81): either the
    /// DrawingML `wps:txbx/w:txbxContent` path, or the VML `v:textbox/w:txbxContent`
    /// fallback path parsed via [`parse_vml_pict`].
    pub text_box_content: Option<String>,
}

/// Whether the drawing is inline or anchored.
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub enum DrawingType {
    /// Drawing is inline: placed within the text flow at its insertion point.
    #[default]
    Inline,
    /// Drawing is anchored: floats at a fixed position relative to the page or paragraph.
    Anchored(AnchorProperties),
}

/// Size in EMUs (English Metric Units, 1 inch = 914400 EMU).
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct Extent {
    /// Width in EMU.
    pub cx: i64,
    /// Height in EMU.
    pub cy: i64,
}

impl Extent {
    /// Convert width to inches.
    pub(crate) fn width_inches(&self) -> f64 {
        self.cx as f64 / super::EMUS_PER_INCH as f64
    }

    /// Convert height to inches.
    pub(crate) fn height_inches(&self) -> f64 {
        self.cy as f64 / super::EMUS_PER_INCH as f64
    }
}

/// Document properties from `<wp:docPr>`.
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct DocProperties {
    /// Unique numeric identifier for this drawing within the document.
    pub id: Option<String>,
    /// Human-readable name for this drawing object.
    pub name: Option<String>,
    /// Alt-text description for accessibility.
    pub description: Option<String>,
}

/// Properties for anchored drawings.
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct AnchorProperties {
    /// Whether the drawing is placed behind the document text.
    pub behind_doc: bool,
    /// Whether the drawing is laid out inside a table cell.
    pub layout_in_cell: bool,
    /// Z-order relative height used for stacking overlapping objects.
    pub relative_height: Option<i64>,
    /// Horizontal position specification.
    pub position_h: Option<Position>,
    /// Vertical position specification.
    pub position_v: Option<Position>,
    /// Text-wrapping mode around this drawing.
    pub wrap_type: WrapType,
}

/// Horizontal or vertical position.
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct Position {
    /// Reference object for this position: `"page"`, `"margin"`, `"column"`, `"paragraph"`, or `"character"`.
    pub relative_from: String,
    /// Offset from the reference object in EMUs.
    pub offset: Option<i64>,
}

/// Text wrapping type around an anchored drawing.
#[cfg_attr(alef, alef(skip))]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub enum WrapType {
    /// No text wrapping; drawing floats above or below text.
    #[default]
    None,
    /// Text wraps in a square around the drawing bounding box.
    Square,
    /// Text wraps tightly to the drawing outline.
    Tight,
    /// Text appears above and below but not to the sides.
    TopAndBottom,
    /// Text flows through the drawing's transparent areas.
    Through,
}

/// Parse a drawing object starting after the `<w:drawing>` Start event.
///
/// This function reads events until it encounters the closing `</w:drawing>` tag,
/// parsing the drawing type (inline or anchored), extent, properties, and image references.
///
/// Threads `budget` through every event so nesting inside `w:drawing` is measured
/// against the caller's depth cap instead of passing through unaccounted (GH#384).
/// The local `depth` counter below is a separate, pre-existing mechanism: it tracks
/// same-named nesting so this function can find its *own* matching `</w:drawing>`
/// end tag, and is unrelated to `budget`'s document-wide depth accounting.
pub(crate) fn parse_drawing(reader: &mut Reader<&[u8]>, budget: &mut SecurityBudget) -> Result<Drawing, SecurityError> {
    let mut drawing = Drawing {
        drawing_type: DrawingType::Inline,
        extent: None,
        doc_properties: None,
        image_ref: None,
        text_box_content: None,
    };

    let mut depth = 1;
    let mut buf = Vec::new();

    loop {
        budget.step()?;
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                budget.enter()?;
                let local = e.local_name();
                let local_name = local.as_ref();

                match local_name {
                    "inline" => {
                        drawing.drawing_type = DrawingType::Inline;
                        depth += 1;
                    }
                    "anchor" => {
                        let anchor = AnchorProperties {
                            behind_doc: get_attr_bool(e, "behindDoc"),
                            layout_in_cell: get_attr_bool(e, "layoutInCell"),
                            relative_height: get_attr_i64(e, "relativeHeight"),
                            ..Default::default()
                        };
                        drawing.drawing_type = DrawingType::Anchored(anchor);
                        depth += 1;
                    }
                    "positionH" => {
                        let relative_from = get_attr(e, "relativeFrom").unwrap_or_else(|| "page".to_string());
                        let position = parse_position(reader, "positionH");
                        // `parse_position` reads through its own `</wp:positionH>`
                        // without touching `budget`; refund the enter above.
                        budget.leave();
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.position_h = Some(Position {
                                relative_from,
                                offset: position,
                            });
                        }
                    }
                    "positionV" => {
                        let relative_from = get_attr(e, "relativeFrom").unwrap_or_else(|| "paragraph".to_string());
                        let position = parse_position(reader, "positionV");
                        // Same as `positionH`: consumes its own end tag.
                        budget.leave();
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.position_v = Some(Position {
                                relative_from,
                                offset: position,
                            });
                        }
                    }
                    "blip" => {
                        if drawing.image_ref.is_none() {
                            drawing.image_ref = get_attr(e, "embed").or_else(|| get_attr(e, "link"));
                        }
                        depth += 1;
                    }
                    "txbxContent" => {
                        // Consumes through its own `</w:txbxContent>` end tag, so it
                        // must not also increment `depth` (#81). `collect_txbx_content_text`
                        // now threads `budget` through and balances the `enter()` above
                        // internally, so no manual `budget.leave()` is needed here. ~keep
                        let text = collect_txbx_content_text(reader, budget)?;
                        if !text.is_empty() {
                            drawing.text_box_content = Some(text);
                        }
                    }
                    "wrapSquare" | "wrapTight" | "wrapTopAndBottom" | "wrapThrough" => {
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            match local_name {
                                "wrapSquare" => anchor.wrap_type = WrapType::Square,
                                "wrapTight" => anchor.wrap_type = WrapType::Tight,
                                "wrapTopAndBottom" => anchor.wrap_type = WrapType::TopAndBottom,
                                "wrapThrough" => anchor.wrap_type = WrapType::Through,
                                _ => {}
                            }
                        }
                        depth += 1;
                    }
                    _ => {
                        depth += 1;
                    }
                }
            }
            Ok(Event::Empty(ref e)) => {
                let local = e.local_name();
                let local_name = local.as_ref();

                match local_name {
                    "extent" => {
                        if let (Some(cx), Some(cy)) = (get_attr_i64(e, "cx"), get_attr_i64(e, "cy")) {
                            drawing.extent = Some(Extent { cx, cy });
                        }
                    }
                    "docPr" => {
                        drawing.doc_properties = Some(DocProperties {
                            id: get_attr(e, "id"),
                            name: get_attr(e, "name"),
                            description: get_attr(e, "descr"),
                        });
                    }
                    "blip" if drawing.image_ref.is_none() => {
                        drawing.image_ref = get_attr(e, "embed").or_else(|| get_attr(e, "link"));
                    }
                    "wrapNone" => {
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.wrap_type = WrapType::None;
                        }
                    }
                    "wrapSquare" => {
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.wrap_type = WrapType::Square;
                        }
                    }
                    "wrapTight" => {
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.wrap_type = WrapType::Tight;
                        }
                    }
                    "wrapTopAndBottom" => {
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.wrap_type = WrapType::TopAndBottom;
                        }
                    }
                    "wrapThrough" => {
                        if let DrawingType::Anchored(ref mut anchor) = drawing.drawing_type {
                            anchor.wrap_type = WrapType::Through;
                        }
                    }
                    _ => {}
                }
            }
            Ok(Event::End(e)) => {
                budget.leave();
                depth -= 1;
                if e.local_name().as_ref() == "drawing" && depth == 0 {
                    break;
                }
            }
            Ok(Event::Eof) => {
                break;
            }
            Err(_) => {
                break;
            }
            _ => {}
        }
        buf.clear();
    }

    Ok(drawing)
}

/// Parse position offset from positionH or positionV element.
/// Consumes all events through the closing element_name end tag.
fn parse_position(reader: &mut Reader<&[u8]>, element_name: &str) -> Option<i64> {
    let mut buf = Vec::new();
    let mut result = None;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) if e.local_name().as_ref() == "posOffset" => {
                let mut text_buf = Vec::new();
                if let Ok(Event::Text(t)) = reader.read_event_into(&mut text_buf) {
                    let text = t.xml10_content();
                    result = text.parse::<i64>().ok();
                }
                let mut end_buf = Vec::new();
                let _ = reader.read_event_into(&mut end_buf);
            }
            Ok(Event::End(e)) if e.local_name().as_ref() == element_name => {
                return result;
            }
            Ok(Event::Eof) => {
                return result;
            }
            Err(_) => {
                return result;
            }
            _ => {}
        }
        buf.clear();
    }
}

/// Extract a string attribute by local name.
fn get_attr(e: &BytesStart, key: &str) -> Option<String> {
    e.attributes()
        .flatten()
        .find(|attr| attr.key.local_name().as_ref() == key)
        .and_then(|attr| {
            let raw = attr.value.as_ref();
            quick_xml::escape::unescape(raw).ok().map(|s| s.into_owned())
        })
}

/// Extract an i64 attribute by local name.
fn get_attr_i64(e: &BytesStart, key: &str) -> Option<i64> {
    get_attr(e, key).and_then(|s| s.parse().ok())
}

/// Extract a boolean attribute by local name (value "1" = true).
fn get_attr_bool(e: &BytesStart, key: &str) -> bool {
    get_attr(e, key).as_deref() == Some("1")
}

/// Collect visible text from a `<w:txbxContent>` subtree (#81): the paragraphs of a
/// text box, reached either via the DrawingML `wps:txbx` path (from [`parse_drawing`])
/// or the VML `v:textbox` fallback path (from [`parse_vml_pict`]).
///
/// Called with the reader positioned right after the `<w:txbxContent>` start tag;
/// consumes events through the matching `</w:txbxContent>` end tag. Paragraphs are
/// joined with newlines; `w:tab`/`w:br` become `\t`/`\n` within a paragraph, matching
/// how the main body loop renders inline breaks.
///
/// Threads `budget` through every event so nesting and iteration count inside
/// `w:txbxContent` are measured against the caller's caps instead of passing through
/// unaccounted (GH#1395/#384). The caller already performed `budget.enter()` for the
/// opening `<w:txbxContent>` tag; this function balances that when it reaches its own
/// matching `</w:txbxContent>` (depth 0).
fn collect_txbx_content_text(reader: &mut Reader<&[u8]>, budget: &mut SecurityBudget) -> Result<String, SecurityError> {
    let mut buf = Vec::new();
    let mut depth = 1u32;
    let mut paragraphs: Vec<String> = Vec::new();
    let mut current = String::new();
    let mut in_text = false;

    loop {
        budget.step()?;
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                budget.enter()?;
                match e.local_name().as_ref() {
                    "txbxContent" => depth += 1,
                    "t" => in_text = true,
                    _ => {}
                }
            }
            Ok(Event::Empty(ref e)) => match e.local_name().as_ref() {
                "tab" => current.push('\t'),
                "br" => current.push('\n'),
                _ => {}
            },
            Ok(Event::Text(e)) if in_text => {
                let text = e.xml10_content();
                budget.check_entity(&text)?;
                budget.account_text(text.len())?;
                current.push_str(&text);
            }
            Ok(Event::GeneralRef(ref e)) if in_text => {
                let text = crate::utils::xml_utils::resolve_general_ref(e);
                budget.account_text(text.len())?;
                current.push_str(&text);
            }
            Ok(Event::End(ref e)) => {
                budget.leave();
                match e.local_name().as_ref() {
                    "t" => in_text = false,
                    "p" => paragraphs.push(std::mem::take(&mut current)),
                    "txbxContent" => {
                        depth -= 1;
                        if depth == 0 {
                            break;
                        }
                    }
                    _ => {}
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }

    if !current.is_empty() {
        paragraphs.push(current);
    }

    Ok(paragraphs
        .into_iter()
        .filter(|p| !p.is_empty())
        .collect::<Vec<_>>()
        .join("\n"))
}

/// Parse a `<v:textbox>` element (already open), looking for a nested
/// `<w:txbxContent>` (#81). Consumes events through the matching `</v:textbox>` end
/// tag regardless of whether a `w:txbxContent` was found.
///
/// Threads `budget` through every event so nesting and iteration count inside
/// `v:textbox` are measured against the caller's caps instead of passing through
/// unaccounted (GH#1395/#384). The caller already performed `budget.enter()` for the
/// opening `<v:textbox>` tag; this function balances that when it reaches its own
/// matching `</v:textbox>` (depth 0).
fn parse_vml_textbox(reader: &mut Reader<&[u8]>, budget: &mut SecurityBudget) -> Result<Option<String>, SecurityError> {
    let mut buf = Vec::new();
    let mut depth = 1u32;
    let mut text: Option<String> = None;

    loop {
        budget.step()?;
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                budget.enter()?;
                if e.local_name().as_ref() == "txbxContent" {
                    // `collect_txbx_content_text` consumes its own end tag and
                    // balances the `enter()` above internally, so no manual
                    // `budget.leave()` is needed here. ~keep
                    let collected = collect_txbx_content_text(reader, budget)?;
                    if !collected.is_empty() {
                        text = Some(collected);
                    }
                } else {
                    depth += 1;
                }
            }
            Ok(Event::End(_)) => {
                budget.leave();
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }

    Ok(text)
}

/// Parse a `<w:pict>` VML fallback wrapper, extracting text-box content from a
/// nested `<v:textbox><w:txbxContent>` if present (#81, #224).
///
/// Consumes events through the matching `</w:pict>` end tag regardless of whether a
/// text box was found, so the caller's own event loop never sees `w:pict`'s inner
/// `v:shape`/`w:p`/`w:r`/`w:t` events leak out as if they were ordinary body content.
/// Returns `Ok(None)` when no text box was found (nothing to attach to the document).
///
/// Threads `budget` through every event so nesting and iteration count inside
/// `w:pict` are measured against the caller's caps instead of passing through
/// unaccounted (a25335db0a left this delegate unthreaded, unlike every other
/// budget-aware delegate in this module; see GH#1395/#384). The caller already
/// performed `budget.enter()` for the opening `<w:pict>` tag; this function balances
/// that when it reaches its own matching `</w:pict>` (depth 0).
pub(crate) fn parse_vml_pict(
    reader: &mut Reader<&[u8]>,
    budget: &mut SecurityBudget,
) -> Result<Option<Drawing>, SecurityError> {
    let mut buf = Vec::new();
    let mut depth = 1u32;
    let mut text_box_content: Option<String> = None;

    loop {
        budget.step()?;
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                budget.enter()?;
                if e.local_name().as_ref() == "textbox" {
                    // `parse_vml_textbox` consumes its own end tag and balances
                    // the `enter()` above internally, so no manual
                    // `budget.leave()` is needed here. ~keep
                    text_box_content = parse_vml_textbox(reader, budget)?;
                } else {
                    depth += 1;
                }
            }
            Ok(Event::End(_)) => {
                budget.leave();
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }

    Ok(text_box_content.map(|text| Drawing {
        text_box_content: Some(text),
        ..Default::default()
    }))
}

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

    /// Helper to parse drawing XML and return the Drawing object.
    fn parse_drawing_from_xml(xml: &[u8]) -> Drawing {
        let mut reader = Reader::from_reader(xml);
        let mut buf = Vec::new();

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(e)) if e.local_name().as_ref() == "drawing" => {
                    break;
                }
                Ok(Event::Eof) => {
                    return Drawing {
                        drawing_type: DrawingType::Inline,
                        extent: None,
                        doc_properties: None,
                        image_ref: None,
                        text_box_content: None,
                    };
                }
                Err(_) => {
                    return Drawing {
                        drawing_type: DrawingType::Inline,
                        extent: None,
                        doc_properties: None,
                        image_ref: None,
                        text_box_content: None,
                    };
                }
                _ => {}
            }
            buf.clear();
        }

        let mut budget = SecurityBudget::with_defaults();
        parse_drawing(&mut reader, &mut budget).expect("parse_drawing should not exceed the default budget")
    }

    #[test]
    fn test_parse_inline_drawing() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <wp:inline>
            <wp:extent cx="914400" cy="457200"/>
            <wp:docPr id="1" name="Picture 1" descr="A test image"/>
            <a:graphic>
              <a:graphicData>
                <pic:pic>
                  <pic:blipFill>
                    <a:blip r:embed="rId5"/>
                  </pic:blipFill>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        assert_eq!(drawing.drawing_type, DrawingType::Inline);
        assert_eq!(drawing.extent, Some(Extent { cx: 914400, cy: 457200 }));
        assert_eq!(
            drawing.doc_properties,
            Some(DocProperties {
                id: Some("1".to_string()),
                name: Some("Picture 1".to_string()),
                description: Some("A test image".to_string()),
            })
        );
        assert_eq!(drawing.image_ref, Some("rId5".to_string()));
    }

    #[test]
    fn test_parse_anchored_drawing() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <wp:anchor behindDoc="0" layoutInCell="1" relativeHeight="251573248">
            <wp:positionH relativeFrom="page">
              <wp:posOffset>621792</wp:posOffset>
            </wp:positionH>
            <wp:positionV relativeFrom="paragraph">
              <wp:posOffset>274320</wp:posOffset>
            </wp:positionV>
            <wp:extent cx="209550" cy="209550"/>
            <wp:wrapSquare/>
            <wp:docPr id="2" name="Picture 2"/>
            <a:graphic>
              <a:graphicData>
                <pic:pic>
                  <pic:blipFill>
                    <a:blip r:embed="rId6"/>
                  </pic:blipFill>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:anchor>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        match drawing.drawing_type {
            DrawingType::Anchored(anchor) => {
                assert!(!anchor.behind_doc);
                assert!(anchor.layout_in_cell);
                assert_eq!(anchor.relative_height, Some(251573248));
                assert_eq!(anchor.wrap_type, WrapType::Square);

                assert!(anchor.position_h.is_some());
                if let Some(pos_h) = anchor.position_h {
                    assert_eq!(pos_h.relative_from, "page");
                    assert_eq!(pos_h.offset, Some(621792));
                }

                assert!(anchor.position_v.is_some());
                if let Some(pos_v) = anchor.position_v {
                    assert_eq!(pos_v.relative_from, "paragraph");
                    assert_eq!(pos_v.offset, Some(274320));
                }
            }
            _ => panic!("Expected DrawingType::Anchored"),
        }

        assert_eq!(drawing.extent, Some(Extent { cx: 209550, cy: 209550 }));
        assert_eq!(drawing.image_ref, Some("rId6".to_string()));
    }

    #[test]
    fn test_parse_drawing_wrap_none() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <wp:anchor behindDoc="0" layoutInCell="0" relativeHeight="0">
            <wp:wrapNone/>
            <wp:extent cx="100000" cy="100000"/>
            <wp:docPr id="3" name="Picture 3"/>
            <a:graphic>
              <a:graphicData>
                <pic:pic>
                  <pic:blipFill>
                    <a:blip r:embed="rId7"/>
                  </pic:blipFill>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:anchor>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        match drawing.drawing_type {
            DrawingType::Anchored(anchor) => {
                assert_eq!(anchor.wrap_type, WrapType::None);
            }
            _ => panic!("Expected DrawingType::Anchored"),
        }
    }

    #[test]
    fn test_parse_drawing_wrap_top_and_bottom() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <wp:anchor behindDoc="0" layoutInCell="0" relativeHeight="0">
            <wp:wrapTopAndBottom/>
            <wp:extent cx="100000" cy="100000"/>
            <wp:docPr id="4" name="Picture 4"/>
            <a:graphic>
              <a:graphicData>
                <pic:pic>
                  <pic:blipFill>
                    <a:blip r:embed="rId8"/>
                  </pic:blipFill>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:anchor>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        match drawing.drawing_type {
            DrawingType::Anchored(anchor) => {
                assert_eq!(anchor.wrap_type, WrapType::TopAndBottom);
            }
            _ => panic!("Expected DrawingType::Anchored"),
        }
    }

    #[test]
    fn test_extent_conversion() {
        let extent = Extent { cx: 914400, cy: 914400 };

        assert_eq!(extent.width_inches(), 1.0);
        assert_eq!(extent.height_inches(), 1.0);

        let extent2 = Extent {
            cx: 1828800,
            cy: 914400,
        };

        assert_eq!(extent2.width_inches(), 2.0);
        assert_eq!(extent2.height_inches(), 1.0);
    }

    #[test]
    fn test_parse_drawing_no_image() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
          <wp:inline>
            <wp:extent cx="100000" cy="100000"/>
            <wp:docPr id="5" name="Shape 5"/>
            <a:graphic>
              <a:graphicData>
                <!-- No blip element, just a shape -->
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        assert_eq!(drawing.drawing_type, DrawingType::Inline);
        assert_eq!(drawing.extent, Some(Extent { cx: 100000, cy: 100000 }));
        assert_eq!(drawing.image_ref, None);
    }

    #[test]
    fn test_parse_drawing_empty_extent() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <wp:inline>
            <wp:docPr id="6" name="Picture 6"/>
            <a:graphic>
              <a:graphicData>
                <pic:pic>
                  <pic:blipFill>
                    <a:blip r:embed="rId9"/>
                  </pic:blipFill>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        assert_eq!(drawing.drawing_type, DrawingType::Inline);
        assert_eq!(drawing.extent, None);
        assert_eq!(drawing.image_ref, Some("rId9".to_string()));
    }

    /// Regression test for issue #590: <a:blip> with children (e.g. <a:extLst>) is parsed
    /// as Event::Start, not Event::Empty — the image reference must still be extracted.
    #[test]
    fn test_parse_blip_with_extlst_children() {
        let xml = br#"<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                        xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"
                        xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <wp:inline distT="0" distB="0" distL="0" distR="0">
            <wp:extent cx="6480175" cy="9064625"/>
            <wp:docPr id="1" name="Picture 1"/>
            <a:graphic>
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                <pic:pic>
                  <pic:blipFill>
                    <a:blip r:embed="rId4" cstate="print">
                      <a:extLst>
                        <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                          <a14:useLocalDpi val="0"/>
                        </a:ext>
                      </a:extLst>
                    </a:blip>
                  </pic:blipFill>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>"#;

        let drawing = parse_drawing_from_xml(xml);

        assert_eq!(drawing.drawing_type, DrawingType::Inline);
        assert_eq!(
            drawing.image_ref,
            Some("rId4".to_string()),
            "image_ref must be extracted even when <a:blip> has child elements"
        );
    }

    #[test]
    fn test_drawing_serialization() {
        let drawing = Drawing {
            drawing_type: DrawingType::Inline,
            extent: Some(Extent { cx: 914400, cy: 457200 }),
            doc_properties: Some(DocProperties {
                id: Some("1".to_string()),
                name: Some("Test".to_string()),
                description: Some("Test description".to_string()),
            }),
            image_ref: Some("rId5".to_string()),
            text_box_content: None,
        };

        let json = serde_json::to_string(&drawing).expect("Failed to serialize");

        let deserialized: Drawing = serde_json::from_str(&json).expect("Failed to deserialize");

        assert_eq!(drawing, deserialized);
    }
}