undoc 0.2.0

High-performance Microsoft Office document extraction to Markdown
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
//! Paragraph and text run models.

use serde::{Deserialize, Serialize};

/// Text alignment within a paragraph.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TextAlignment {
    #[default]
    Left,
    Center,
    Right,
    Justify,
}

/// Heading level (h1-h6 or none).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum HeadingLevel {
    #[default]
    None,
    H1,
    H2,
    H3,
    H4,
    H5,
    H6,
}

impl HeadingLevel {
    /// Create a heading level from a number (1-6).
    pub fn from_number(n: u8) -> Self {
        match n {
            1 => HeadingLevel::H1,
            2 => HeadingLevel::H2,
            3 => HeadingLevel::H3,
            4 => HeadingLevel::H4,
            5 => HeadingLevel::H5,
            6 => HeadingLevel::H6,
            _ => HeadingLevel::None,
        }
    }

    /// Get the numeric level (0 for none, 1-6 for headings).
    pub fn level(&self) -> u8 {
        match self {
            HeadingLevel::None => 0,
            HeadingLevel::H1 => 1,
            HeadingLevel::H2 => 2,
            HeadingLevel::H3 => 3,
            HeadingLevel::H4 => 4,
            HeadingLevel::H5 => 5,
            HeadingLevel::H6 => 6,
        }
    }

    /// Check if this is a heading (not None).
    pub fn is_heading(&self) -> bool {
        !matches!(self, HeadingLevel::None)
    }
}

/// List type for paragraphs.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ListType {
    #[default]
    None,
    /// Unordered (bulleted) list
    Bullet,
    /// Ordered (numbered) list
    Numbered,
}

/// Revision type for tracked changes support.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RevisionType {
    /// Normal text (not a tracked change)
    #[default]
    None,
    /// Inserted text (addition)
    Inserted,
    /// Deleted text (deletion)
    Deleted,
}

/// List information for a paragraph.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ListInfo {
    /// Type of list
    pub list_type: ListType,
    /// Nesting level (0 = top level)
    pub level: u8,
    /// Item number (for numbered lists)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub number: Option<u32>,
}

/// Text style properties.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct TextStyle {
    /// Bold text
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub bold: bool,

    /// Italic text
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub italic: bool,

    /// Underlined text
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub underline: bool,

    /// Strikethrough text
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub strikethrough: bool,

    /// Superscript
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub superscript: bool,

    /// Subscript
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub subscript: bool,

    /// Code/monospace font
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub code: bool,

    /// Font name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font: Option<String>,

    /// Font size in half-points (e.g., 24 = 12pt)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<u32>,

    /// Text color (hex, e.g., "FF0000")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,

    /// Background/highlight color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub highlight: Option<String>,
}

impl TextStyle {
    /// Create a new default style.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a bold style.
    pub fn bold() -> Self {
        Self {
            bold: true,
            ..Default::default()
        }
    }

    /// Create an italic style.
    pub fn italic() -> Self {
        Self {
            italic: true,
            ..Default::default()
        }
    }

    /// Check if style has any formatting.
    pub fn has_formatting(&self) -> bool {
        self.bold
            || self.italic
            || self.underline
            || self.strikethrough
            || self.superscript
            || self.subscript
            || self.code
    }
}

/// A run of text with consistent styling.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TextRun {
    /// The text content
    pub text: String,

    /// Text styling
    #[serde(default, skip_serializing_if = "is_default_style")]
    pub style: TextStyle,

    /// Hyperlink URL (if this run is a link)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hyperlink: Option<String>,

    /// Whether this run ends with a line break (<w:br/>)
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub line_break: bool,

    /// Whether this run ends with a page break (<w:br w:type="page"/>)
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub page_break: bool,

    /// Revision type for tracked changes (inserted/deleted)
    #[serde(default, skip_serializing_if = "is_default_revision")]
    pub revision: RevisionType,
}

fn is_default_style(style: &TextStyle) -> bool {
    *style == TextStyle::default()
}

fn is_default_revision(revision: &RevisionType) -> bool {
    *revision == RevisionType::None
}

impl TextRun {
    /// Create a plain text run with no styling.
    pub fn plain(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            style: TextStyle::default(),
            hyperlink: None,
            line_break: false,
            page_break: false,
            revision: RevisionType::None,
        }
    }

    /// Create a styled text run.
    pub fn styled(text: impl Into<String>, style: TextStyle) -> Self {
        Self {
            text: text.into(),
            style,
            hyperlink: None,
            line_break: false,
            page_break: false,
            revision: RevisionType::None,
        }
    }

    /// Create a hyperlink text run.
    pub fn link(text: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            style: TextStyle::default(),
            hyperlink: Some(url.into()),
            line_break: false,
            page_break: false,
            revision: RevisionType::None,
        }
    }

    /// Check if this run is a hyperlink.
    pub fn is_link(&self) -> bool {
        self.hyperlink.is_some()
    }

    /// Check if this run is empty.
    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }
}

/// An inline image within text.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InlineImage {
    /// Resource ID for the image
    pub resource_id: String,

    /// Alt text
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alt_text: Option<String>,

    /// Width in EMUs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,

    /// Height in EMUs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,
}

/// An element within a paragraph (text run or inline image).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ParagraphElement {
    Text(TextRun),
    Image(InlineImage),
}

/// A paragraph of text.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Paragraph {
    /// Text runs in this paragraph
    #[serde(default)]
    pub runs: Vec<TextRun>,

    /// Inline images in this paragraph
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub images: Vec<InlineImage>,

    /// Heading level
    #[serde(default, skip_serializing_if = "HeadingLevel::is_none")]
    pub heading: HeadingLevel,

    /// Text alignment
    #[serde(default, skip_serializing_if = "is_default_alignment")]
    pub alignment: TextAlignment,

    /// List information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_info: Option<ListInfo>,

    /// Style ID reference
    #[serde(skip_serializing_if = "Option::is_none")]
    pub style_id: Option<String>,

    /// Style name (human-readable, from styles.xml)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub style_name: Option<String>,

    /// Indentation level
    #[serde(default, skip_serializing_if = "is_zero")]
    pub indent_level: u8,
}

fn is_default_alignment(a: &TextAlignment) -> bool {
    *a == TextAlignment::Left
}

fn is_zero(n: &u8) -> bool {
    *n == 0
}

impl HeadingLevel {
    fn is_none(&self) -> bool {
        matches!(self, HeadingLevel::None)
    }
}

impl Paragraph {
    /// Create a new empty paragraph.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a paragraph with the given text.
    pub fn with_text(text: impl Into<String>) -> Self {
        Self {
            runs: vec![TextRun::plain(text)],
            ..Default::default()
        }
    }

    /// Create a heading paragraph.
    pub fn heading(level: HeadingLevel, text: impl Into<String>) -> Self {
        Self {
            runs: vec![TextRun::plain(text)],
            heading: level,
            ..Default::default()
        }
    }

    /// Add a text run to this paragraph.
    pub fn add_run(&mut self, run: TextRun) {
        self.runs.push(run);
    }

    /// Get the plain text content.
    pub fn plain_text(&self) -> String {
        let mut text = String::new();

        for run in &self.runs {
            text.push_str(&run.text);
            if run.line_break {
                text.push('\n');
            }
            if run.page_break {
                text.push_str("\n---\n");
            }
        }

        text
    }

    /// Check if this paragraph is empty.
    pub fn is_empty(&self) -> bool {
        self.runs.is_empty() || self.runs.iter().all(|r| r.is_empty())
    }

    /// Check if this paragraph is a heading.
    pub fn is_heading(&self) -> bool {
        self.heading.is_heading()
    }

    /// Check if this paragraph is a list item.
    pub fn is_list_item(&self) -> bool {
        self.list_info.is_some()
    }

    /// Merge consecutive runs with the same style.
    ///
    /// This is useful for documents where each character or word is in a separate run
    /// with the same styling (common in Word documents with letter spacing).
    ///
    /// Example: `**시** **험**` becomes `**시험**` after merging.
    ///
    /// Smart spacing is applied when merging runs - spaces are added between
    /// CJK text and ASCII alphanumeric characters, and between ASCII words.
    pub fn merge_adjacent_runs(&mut self) {
        if self.runs.len() <= 1 {
            return;
        }

        let mut merged: Vec<TextRun> = Vec::with_capacity(self.runs.len());

        for run in self.runs.drain(..) {
            // Check if we can merge with the last run
            let should_merge = merged.last().is_some_and(|last: &TextRun| {
                // Same style and same hyperlink (both None or both Some with same URL)
                // Don't merge if the previous run has a line break (preserve the break)
                last.style == run.style
                    && last.hyperlink == run.hyperlink
                    && !last.line_break
                    && !last.page_break
            });

            if should_merge {
                // Merge text with the last run, with smart spacing
                if let Some(last) = merged.last_mut() {
                    // Check if we need to add a space between the runs
                    let needs_space = Self::needs_space_between(&last.text, &run.text);
                    if needs_space {
                        last.text.push(' ');
                    }
                    last.text.push_str(&run.text);
                    // Preserve line_break from the merged run
                    if run.line_break {
                        last.line_break = true;
                    }
                    if run.page_break {
                        last.page_break = true;
                    }
                }
            } else {
                // Start a new run
                merged.push(run);
            }
        }

        self.runs = merged;
    }

    /// Determine if a space is needed between two text fragments when merging.
    fn needs_space_between(prev: &str, next: &str) -> bool {
        let last_char = match prev.chars().last() {
            Some(c) => c,
            None => return false,
        };
        let first_char = match next.chars().next() {
            Some(c) => c,
            None => return false,
        };

        // No space needed if either side already has whitespace
        if last_char.is_whitespace() || first_char.is_whitespace() {
            return false;
        }

        // No space before punctuation
        if matches!(
            first_char,
            '.' | ',' | ':' | ';' | '!' | '?' | ')' | ']' | '}' | '"' | '\'' | '' | '~'
        ) {
            return false;
        }

        // No space after opening brackets/quotes
        if matches!(last_char, '(' | '[' | '{' | '"' | '\'') {
            return false;
        }

        // Same-style runs = same word (artificially split by Word)
        // This applies to ALL scripts: ASCII, CJK, Hangul, mixed scripts, etc.
        //
        // Examples of runs that should NOT have spaces added:
        // - "DRBD" stored as ["DRB", "D"] → "DRBD"
        // - "정의" stored as ["정", "의"] → "정의"
        // - "CJ대한통운" stored as ["C", "J", "대한통운"] → "CJ대한통운" (brand name)
        //
        // Key insight: Word splits runs for various reasons (formatting, editing history),
        // but same-style consecutive runs are ALWAYS part of the same word.
        // If they were different words, they would have explicit whitespace between them.
        //
        // Note: Korean DOES use spaces between words, but those spaces exist in the
        // source document (with xml:space="preserve"). We don't invent spaces.
        //
        // Previously this function added spaces at ASCII↔CJK boundaries, but this was
        // incorrect for Korean brand names like "CJ대한통운" where the intent is no space.
        false
    }

    /// Get a version of this paragraph with merged adjacent runs.
    ///
    /// This is a non-mutating version of `merge_adjacent_runs`.
    pub fn with_merged_runs(&self) -> Self {
        let mut para = self.clone();
        para.merge_adjacent_runs();
        para
    }
}

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

    #[test]
    fn test_heading_level() {
        assert_eq!(HeadingLevel::from_number(1), HeadingLevel::H1);
        assert_eq!(HeadingLevel::from_number(6), HeadingLevel::H6);
        assert_eq!(HeadingLevel::from_number(7), HeadingLevel::None);
        assert_eq!(HeadingLevel::from_number(0), HeadingLevel::None);

        assert_eq!(HeadingLevel::H3.level(), 3);
        assert!(HeadingLevel::H1.is_heading());
        assert!(!HeadingLevel::None.is_heading());
    }

    #[test]
    fn test_text_run() {
        let plain = TextRun::plain("Hello");
        assert_eq!(plain.text, "Hello");
        assert!(!plain.is_link());

        let link = TextRun::link("Click here", "https://example.com");
        assert!(link.is_link());
        assert_eq!(link.hyperlink, Some("https://example.com".to_string()));
    }

    #[test]
    fn test_text_style() {
        let style = TextStyle::bold();
        assert!(style.bold);
        assert!(style.has_formatting());

        let plain = TextStyle::default();
        assert!(!plain.has_formatting());
    }

    #[test]
    fn test_paragraph() {
        let para = Paragraph::with_text("Hello, World!");
        assert_eq!(para.plain_text(), "Hello, World!");
        assert!(!para.is_heading());
        assert!(!para.is_empty());

        let heading = Paragraph::heading(HeadingLevel::H1, "Title");
        assert!(heading.is_heading());
        assert_eq!(heading.heading.level(), 1);
    }

    #[test]
    fn test_paragraph_plain_text_preserves_run_breaks() {
        let para = Paragraph {
            runs: vec![
                TextRun {
                    text: "First line".to_string(),
                    line_break: true,
                    ..Default::default()
                },
                TextRun {
                    text: "Second line".to_string(),
                    page_break: true,
                    ..Default::default()
                },
                TextRun::plain("Third line"),
            ],
            ..Default::default()
        };

        assert_eq!(
            para.plain_text(),
            "First line\nSecond line\n---\nThird line"
        );
    }

    #[test]
    fn test_paragraph_serialization() {
        let para = Paragraph::with_text("Test");
        let json = serde_json::to_string(&para).unwrap();
        // Default values should not be serialized
        assert!(!json.contains("heading"));
        assert!(!json.contains("alignment"));
    }

    #[test]
    fn test_merge_adjacent_runs_ascii_no_split() {
        // Test fix for word splitting bug: "DRBD" stored as ["DRB", "D"] should merge to "DRBD"
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("DRB"));
        para.runs.push(TextRun::plain("D"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "DRBD"); // NOT "DRB D"
    }

    #[test]
    fn test_merge_adjacent_runs_ping() {
        // Test fix: "PING" stored as ["P", "ING"] should merge to "PING"
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("P"));
        para.runs.push(TextRun::plain("ING"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "PING"); // NOT "P ING"
    }

    #[test]
    fn test_merge_adjacent_runs_tcp() {
        // Test fix: "TCP" stored as ["T", "CP"] should merge to "TCP"
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("T"));
        para.runs.push(TextRun::plain("CP"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "TCP"); // NOT "T CP"
    }

    #[test]
    fn test_merge_adjacent_runs_cjk_ascii_no_space() {
        // Same-style runs merge WITHOUT space - even across script boundaries
        // Example: "VIP리소스" is a valid Korean compound where VIP is a brand/term
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("리소스"));
        para.runs.push(TextRun::plain("DRBD"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "리소스DRBD"); // No space - same-style runs = same word
    }

    #[test]
    fn test_merge_adjacent_runs_ascii_cjk_no_space() {
        // Same-style runs merge WITHOUT space - even across script boundaries
        // Example: "CJ대한통운" is a brand name where CJ is part of the Korean name
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("CJ"));
        para.runs.push(TextRun::plain("대한통운"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "CJ대한통운"); // No space - brand name
    }

    #[test]
    fn test_merge_adjacent_runs_korean_no_space() {
        // Korean: Same-style runs merge WITHOUT space (like ASCII, Chinese, Japanese)
        // Word may split a single word into multiple runs for various reasons.
        // Example: "정의" stored as ["정", "의"] should become "정의", not "정 의"
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("네트워크"));
        para.runs.push(TextRun::plain("카드"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "네트워크카드"); // No space - same word or compound
    }

    #[test]
    fn test_merge_adjacent_runs_korean_syllables() {
        // When Word splits Korean syllables, they should merge without space
        // This was the bug: "정의" → "정 의" (wrong) instead of "정의" (correct)
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain(""));
        para.runs.push(TextRun::plain(""));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "정의"); // Syllables merge without space
    }

    #[test]
    fn test_merge_adjacent_runs_korean_with_explicit_space() {
        // When source has explicit space, it's preserved in the run text
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("서버 ")); // Note: space is in the text
        para.runs.push(TextRun::plain("리부팅"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "서버 리부팅"); // Space preserved from source
    }

    #[test]
    fn test_merge_adjacent_runs_chinese_no_space() {
        // Chinese: Same-style runs merge without space
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("中文"));
        para.runs.push(TextRun::plain("测试"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "中文测试"); // No space between Chinese
    }

    #[test]
    fn test_merge_adjacent_runs_japanese_no_space() {
        // Japanese: Same-style runs merge without space
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("日本語"));
        para.runs.push(TextRun::plain("テスト"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "日本語テスト"); // No space between Japanese
    }

    #[test]
    fn test_merge_adjacent_runs_different_styles_not_merged() {
        // Runs with different styles should NOT be merged
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("normal"));
        para.runs.push(TextRun::styled("bold", TextStyle::bold()));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 2); // Still 2 runs
        assert_eq!(para.runs[0].text, "normal");
        assert_eq!(para.runs[1].text, "bold");
    }

    #[test]
    fn test_merge_preserves_existing_spaces() {
        // Existing spaces should be preserved
        let mut para = Paragraph::new();
        para.runs.push(TextRun::plain("Hello "));
        para.runs.push(TextRun::plain("World"));
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 1);
        assert_eq!(para.runs[0].text, "Hello World"); // Original space preserved
    }

    #[test]
    fn test_merge_adjacent_runs_preserves_page_break() {
        let mut para = Paragraph {
            runs: vec![
                TextRun::plain("Before"),
                TextRun {
                    text: "After".to_string(),
                    page_break: true,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        para.merge_adjacent_runs();

        assert!(
            para.runs.iter().any(|r| r.page_break),
            "page_break lost after merge: runs = {:?}",
            para.runs
        );
    }

    #[test]
    fn test_merge_adjacent_runs_blocks_on_last_page_break() {
        let mut para = Paragraph {
            runs: vec![
                TextRun {
                    text: "Before".to_string(),
                    page_break: true,
                    ..Default::default()
                },
                TextRun::plain("After"),
            ],
            ..Default::default()
        };
        para.merge_adjacent_runs();

        assert_eq!(para.runs.len(), 2, "must not merge across a page_break");
        assert!(para.runs[0].page_break);
        assert!(!para.runs[1].page_break);
    }
}