dxpdf 0.2.23

A fast DOCX-to-PDF converter powered by Skia
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
//! Cross-run grapheme cluster reassembly.
//!
//! See `docs/cross-run-cluster-reassembly.md` for the full design.
//!
//! Word splits a UAX #29 grapheme cluster across `<w:r>` runs whenever
//! consecutive characters land in different `<w:rFonts>` slots
//! (§17.3.2.26). The classic example is the keycap `1️⃣` — digit `1`
//! lands in the `ascii` slot, `VS-16` + `U+20E3` land in the `hAnsi`
//! slot, so Word emits three separate runs even though the user authored
//! one emoji.
//!
//! This module pre-passes a paragraph's inline list, joining consecutive
//! text-only runs into a flat string with per-character provenance, then
//! classifies the joined string per UAX #29 + UTS #51 (via the existing
//! `cluster` module). Each resulting [`SegmentPiece`] is either a single-
//! run text span or a cross-run emoji cluster ready for the existing
//! emit-fragments path.

use unicode_segmentation::UnicodeSegmentation;

use crate::model::{Inline, RunElement, TextRun};
use crate::render::emoji::cluster::{self, EmojiPresentation, EmojiStructure, InlineCluster};

// ─── Public ADTs ─────────────────────────────────────────────────────────────

/// A maximal contiguous unit within a paragraph's inline list, after
/// stripping run boundaries that don't carry semantic meaning.
///
/// Joining stops at:
/// - Any inline that isn't a `Inline::TextRun` (Image, Hyperlink, Pict,
///   FieldChar, InstrText, BookmarkStart/End, FootnoteRef, EndnoteRef,
///   Symbol, Separator, AlternateContent, …).
/// - Any `Inline::TextRun` whose `content` contains a `RunElement` other
///   than `RunElement::Text` (Tab, LineBreak, PageBreak, ColumnBreak,
///   LastRenderedPageBreak).
///
/// Both classes surface as `Discrete` so the existing dispatch in
/// `collect_fragments` handles them unchanged.
#[derive(Debug)]
pub(super) enum InlineUnit<'a> {
    /// One or more consecutive text-only `Inline::TextRun`s, joined into
    /// a flat string with per-character provenance back to the originating
    /// run.
    TextSegment(JoinedTextSegment<'a>),
    /// Anything else — a borrow into the original `Inline` tree.
    Discrete(&'a Inline),
}

/// A flat text segment with per-character provenance.
///
/// Invariants:
/// - `text.chars().count() == char_runs.len()`
/// - `text` is C0-control-stripped (XML §2.1) except for TAB.
/// - Every `char_runs[i]` is a stable reference into the parsed model.
#[derive(Debug)]
pub(super) struct JoinedTextSegment<'a> {
    text: String,
    /// One entry per `char` in `text`. Tracks which originating
    /// [`TextRun`] produced that scalar.
    char_runs: Vec<&'a TextRun>,
}

impl<'a> JoinedTextSegment<'a> {
    /// Joined-and-stripped text. Used by tests to verify provenance and
    /// by future paragraph-level diagnostics — kept as part of the public
    /// API of this module.
    #[allow(dead_code)]
    pub(super) fn text(&self) -> &str {
        &self.text
    }

    pub(super) fn char_runs(&self) -> &[&'a TextRun] {
        &self.char_runs
    }

    /// Classify this segment per UAX #29 + UTS #51, splitting at run
    /// boundaries within text spans and packaging emoji clusters
    /// (possibly cross-run) as a single piece using the cluster's first
    /// run as the base.
    pub(super) fn classify(self) -> Vec<SegmentPiece<'a>> {
        let mut out = Vec::new();
        let mut buffer: Option<TextBuffer<'a>> = None;
        let mut char_idx = 0usize;

        for ic in cluster::classify(&self.text) {
            match ic {
                InlineCluster::Text(span) => {
                    // Walk graphemes inside this text span so we can
                    // split when the originating run changes.
                    for grapheme in UnicodeSegmentation::graphemes(span, true) {
                        let chars_in = grapheme.chars().count();
                        let run = self.char_runs[char_idx];
                        match buffer.as_mut() {
                            None => {
                                buffer = Some(TextBuffer {
                                    run,
                                    text: grapheme.to_string(),
                                });
                            }
                            Some(buf) if std::ptr::eq(buf.run, run) => {
                                buf.text.push_str(grapheme);
                            }
                            Some(_) => {
                                // Run boundary inside a text span — flush.
                                let prev = buffer.take().unwrap();
                                out.push(SegmentPiece::Text {
                                    run: prev.run,
                                    text: prev.text,
                                });
                                buffer = Some(TextBuffer {
                                    run,
                                    text: grapheme.to_string(),
                                });
                            }
                        }
                        char_idx += chars_in;
                    }
                }
                InlineCluster::Emoji(ec) => {
                    // Flush any pending text.
                    if let Some(prev) = buffer.take() {
                        out.push(SegmentPiece::Text {
                            run: prev.run,
                            text: prev.text,
                        });
                    }
                    let base_run = self.char_runs[char_idx];
                    let chars_in = ec.text.chars().count();
                    out.push(SegmentPiece::Emoji {
                        base_run,
                        text: ec.text.to_string(),
                        presentation: ec.presentation,
                        structure: ec.structure,
                    });
                    char_idx += chars_in;
                }
            }
        }

        if let Some(last) = buffer {
            out.push(SegmentPiece::Text {
                run: last.run,
                text: last.text,
            });
        }

        out
    }
}

struct TextBuffer<'a> {
    run: &'a TextRun,
    text: String,
}

/// One classified piece of a [`JoinedTextSegment`] after applying UAX #29
/// + UTS #51.
///
/// `Text` is split at run boundaries so each piece carries a single
/// originating run; that run's properties drive the whole piece. `Emoji`
/// is not split — a UAX #29 cluster is one visual unit at one size, so
/// when it spans runs we use the base run's formatting (font-name hint
/// per §17.3.2.26, color, baseline offset, font size).
#[derive(Debug)]
pub(super) enum SegmentPiece<'a> {
    Text {
        run: &'a TextRun,
        text: String,
    },
    Emoji {
        base_run: &'a TextRun,
        text: String,
        presentation: EmojiPresentation,
        structure: EmojiStructure,
    },
}

// ─── Builder ─────────────────────────────────────────────────────────────────

/// Walk a paragraph's inline list and produce [`InlineUnit`]s with
/// consecutive text-only runs joined into [`JoinedTextSegment`]s.
pub(super) fn build_inline_units(inlines: &[Inline]) -> Vec<InlineUnit<'_>> {
    let mut out = Vec::new();
    let mut buf = SegmentBuilder::default();

    for inline in inlines {
        match inline {
            Inline::TextRun(tr) if is_text_only_run(tr) => {
                for el in &tr.content {
                    if let RunElement::Text(s) = el {
                        buf.append(s, tr);
                    }
                }
            }
            other => {
                if let Some(seg) = buf.take() {
                    out.push(InlineUnit::TextSegment(seg));
                }
                out.push(InlineUnit::Discrete(other));
            }
        }
    }

    if let Some(seg) = buf.take() {
        out.push(InlineUnit::TextSegment(seg));
    }
    out
}

fn is_text_only_run(tr: &TextRun) -> bool {
    !tr.content.is_empty() && tr.content.iter().all(|e| matches!(e, RunElement::Text(_)))
}

#[derive(Default)]
struct SegmentBuilder<'a> {
    text: String,
    char_runs: Vec<&'a TextRun>,
}

impl<'a> SegmentBuilder<'a> {
    fn append(&mut self, raw: &str, run: &'a TextRun) {
        // XML §2.1: strip C0 control chars (other than TAB) at segment
        // build time. Matches the legacy `emit_text_fragments` contract.
        for c in raw.chars() {
            if c.is_control() && c != '\t' {
                continue;
            }
            self.text.push(c);
            self.char_runs.push(run);
        }
    }

    fn take(&mut self) -> Option<JoinedTextSegment<'a>> {
        if self.text.is_empty() {
            return None;
        }
        Some(JoinedTextSegment {
            text: std::mem::take(&mut self.text),
            char_runs: std::mem::take(&mut self.char_runs),
        })
    }
}

// ─── Tests (Phase α.0 + α.1) ─────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{RevisionIds, RunProperties, Symbol};

    // ── Helpers ────────────────────────────────────────────────────────

    fn text_run(text: &str) -> Inline {
        Inline::TextRun(Box::new(TextRun {
            style_id: None,
            properties: RunProperties::default(),
            content: vec![RunElement::Text(text.into())],
            rsids: RevisionIds::default(),
        }))
    }

    fn run_with_elements(elements: Vec<RunElement>) -> Inline {
        Inline::TextRun(Box::new(TextRun {
            style_id: None,
            properties: RunProperties::default(),
            content: elements,
            rsids: RevisionIds::default(),
        }))
    }

    /// Any non-TextRun inline that breaks segment joining. We use Symbol
    /// (small, simple) as the canonical "interrupter" in tests; the real
    /// pipeline treats every non-TextRun inline the same way.
    fn discrete_inline() -> Inline {
        Inline::Symbol(Symbol {
            font: "Wingdings".into(),
            char_code: 0xF0FE,
        })
    }

    fn extract_run(inline: &Inline) -> &TextRun {
        match inline {
            Inline::TextRun(tr) => tr,
            _ => panic!("expected TextRun"),
        }
    }

    fn segment_of<'a>(unit: &'a InlineUnit<'a>) -> &'a JoinedTextSegment<'a> {
        match unit {
            InlineUnit::TextSegment(s) => s,
            other => panic!("expected TextSegment, got {other:?}"),
        }
    }

    // ── Phase α.0: build_inline_units ──────────────────────────────────

    /// A1 — single TextRun produces one segment whose char_runs all
    /// point to that run.
    #[test]
    fn a1_single_text_run() {
        let inlines = vec![text_run("hello")];
        let r1 = extract_run(&inlines[0]);

        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 1);
        let seg = segment_of(&units[0]);
        assert_eq!(seg.text(), "hello");
        assert_eq!(seg.char_runs().len(), 5);
        for &cr in seg.char_runs() {
            assert!(std::ptr::eq(cr, r1));
        }
    }

    /// A2 — two consecutive TextRuns join into one segment with
    /// per-character provenance interleaved.
    #[test]
    fn a2_two_runs_join() {
        let inlines = vec![text_run("ab"), text_run("cd")];
        let r1 = extract_run(&inlines[0]);
        let r2 = extract_run(&inlines[1]);

        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 1);
        let seg = segment_of(&units[0]);
        assert_eq!(seg.text(), "abcd");
        assert_eq!(seg.char_runs().len(), 4);
        assert!(std::ptr::eq(seg.char_runs()[0], r1));
        assert!(std::ptr::eq(seg.char_runs()[1], r1));
        assert!(std::ptr::eq(seg.char_runs()[2], r2));
        assert!(std::ptr::eq(seg.char_runs()[3], r2));
    }

    /// A3 — a non-TextRun inline (Symbol stands in for any of Image,
    /// Hyperlink, Pict, FootnoteRef, …) breaks segment joining.
    #[test]
    fn a3_non_text_inline_breaks_join() {
        let inlines = vec![text_run("ab"), discrete_inline(), text_run("cd")];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 3);
        assert_eq!(segment_of(&units[0]).text(), "ab");
        assert!(matches!(units[1], InlineUnit::Discrete(Inline::Symbol(_))));
        assert_eq!(segment_of(&units[2]).text(), "cd");
    }

    /// A4 — a TextRun whose content contains a non-text element (Tab)
    /// is treated as Discrete; subsequent text-only runs start a fresh
    /// segment.
    #[test]
    fn a4_text_run_with_tab_is_discrete() {
        let r1 = run_with_elements(vec![
            RunElement::Text("a".into()),
            RunElement::Tab,
            RunElement::Text("b".into()),
        ]);
        let inlines = vec![text_run("hello"), r1, text_run("world")];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 3);
        assert_eq!(segment_of(&units[0]).text(), "hello");
        assert!(matches!(units[1], InlineUnit::Discrete(Inline::TextRun(_))));
        assert_eq!(segment_of(&units[2]).text(), "world");
    }

    /// A5 — empty TextRun produces no segment.
    #[test]
    fn a5_empty_text_run_is_skipped() {
        let inlines = vec![text_run("")];
        let units = build_inline_units(&inlines);
        assert!(units.is_empty(), "no segment from empty text");
    }

    /// A5b — purely-empty TextRun (no content elements) is treated as
    /// not-text-only and surfaces as Discrete.
    #[test]
    fn a5b_no_content_text_run_is_discrete() {
        let inlines = vec![run_with_elements(vec![])];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 1);
        assert!(matches!(units[0], InlineUnit::Discrete(_)));
    }

    /// A6 — XML §2.1: C0 control chars (other than TAB) are stripped at
    /// segment-build time. char_runs.len() reflects the cleaned text.
    #[test]
    fn a6_control_chars_stripped() {
        let inlines = vec![text_run("\u{0001}hi\u{0002}")];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 1);
        let seg = segment_of(&units[0]);
        assert_eq!(seg.text(), "hi");
        assert_eq!(seg.char_runs().len(), 2);
    }

    /// A6b — TAB chars in text are PRESERVED (legacy contract; tabs in
    /// `RunElement::Text` are different from `RunElement::Tab`).
    #[test]
    fn a6b_tab_preserved_in_text() {
        let inlines = vec![text_run("a\tb")];
        let units = build_inline_units(&inlines);
        let seg = segment_of(&units[0]);
        assert_eq!(seg.text(), "a\tb");
    }

    /// A7 — three runs all with text → one segment with full
    /// invariant `text.chars().count() == char_runs.len()`.
    #[test]
    fn a7_three_runs_invariant() {
        let inlines = vec![text_run("ab"), text_run("c"), text_run("def")];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 1);
        let seg = segment_of(&units[0]);
        assert_eq!(seg.text().chars().count(), seg.char_runs().len());
        assert_eq!(seg.text(), "abcdef");
    }

    /// A8 — a Hyperlink Inline breaks the join (hyperlink content is its
    /// own scope handled via recursion at the emit site).
    #[test]
    fn a8_hyperlink_breaks_join() {
        use crate::model::{Hyperlink, HyperlinkTarget, RelId};
        let inlines = vec![
            text_run("before "),
            Inline::Hyperlink(Hyperlink {
                target: HyperlinkTarget::External(RelId::new("rId1")),
                content: vec![text_run("link")],
            }),
            text_run(" after"),
        ];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 3);
        assert_eq!(segment_of(&units[0]).text(), "before ");
        assert!(matches!(
            units[1],
            InlineUnit::Discrete(Inline::Hyperlink(_))
        ));
        assert_eq!(segment_of(&units[2]).text(), " after");
    }

    /// A9 — Inline::FieldChar breaks the join (so the field state
    /// machine sees each text segment as a distinct field-state zone).
    #[test]
    fn a9_field_char_breaks_join() {
        use crate::model::{FieldChar, FieldCharType};
        let inlines = vec![
            text_run("page "),
            Inline::FieldChar(FieldChar {
                field_char_type: FieldCharType::Begin,
                dirty: None,
                fld_lock: None,
            }),
            text_run("instr"),
        ];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 3);
        assert_eq!(segment_of(&units[0]).text(), "page ");
        assert!(matches!(
            units[1],
            InlineUnit::Discrete(Inline::FieldChar(_))
        ));
        assert_eq!(segment_of(&units[2]).text(), "instr");
    }

    // ── Phase α.1: JoinedTextSegment::classify ─────────────────────────

    /// B1 — single-run "hello" → one Text piece.
    #[test]
    fn b1_single_run_text() {
        let inlines = vec![text_run("hello")];
        let r1 = extract_run(&inlines[0]);

        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        match &pieces[0] {
            SegmentPiece::Text { run, text } => {
                assert!(std::ptr::eq(*run, r1));
                assert_eq!(text, "hello");
            }
            other => panic!("expected Text, got {other:?}"),
        }
    }

    /// B2 — two-run "ab" + "cd" → two Text pieces, one per run.
    #[test]
    fn b2_two_runs_split_text() {
        let inlines = vec![text_run("ab"), text_run("cd")];
        let r1 = extract_run(&inlines[0]);
        let r2 = extract_run(&inlines[1]);

        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 2);
        match &pieces[0] {
            SegmentPiece::Text { run, text } => {
                assert!(std::ptr::eq(*run, r1));
                assert_eq!(text, "ab");
            }
            _ => panic!(),
        }
        match &pieces[1] {
            SegmentPiece::Text { run, text } => {
                assert!(std::ptr::eq(*run, r2));
                assert_eq!(text, "cd");
            }
            _ => panic!(),
        }
    }

    /// B3 — keycap split as ["1", VS-16, U+20E3] → one Emoji piece.
    /// **Direct fix for the user-visible keycap bug.**
    #[test]
    fn b3_cross_run_keycap_reassembles() {
        let inlines = vec![text_run("1"), text_run("\u{FE0F}"), text_run("\u{20E3}")];
        let r1 = extract_run(&inlines[0]);

        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        match &pieces[0] {
            SegmentPiece::Emoji {
                base_run,
                text,
                presentation,
                structure,
            } => {
                assert!(std::ptr::eq(*base_run, r1));
                assert_eq!(text, "1\u{FE0F}\u{20E3}");
                assert_eq!(*presentation, EmojiPresentation::Emoji);
                assert!(matches!(
                    structure,
                    EmojiStructure::KeycapSequence { base: '1' }
                ));
            }
            other => panic!("expected Emoji KeycapSequence, got {other:?}"),
        }
    }

    /// B4 — ZWJ family split per emoji → one Emoji piece.
    #[test]
    fn b4_cross_run_zwj_family() {
        let inlines = vec![
            text_run("\u{1F468}"),
            text_run("\u{200D}"),
            text_run("\u{1F469}"),
            text_run("\u{200D}"),
            text_run("\u{1F467}"),
        ];
        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        match &pieces[0] {
            SegmentPiece::Emoji {
                text, structure, ..
            } => {
                assert_eq!(text, "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}");
                assert!(matches!(structure, EmojiStructure::ZwjSequence));
            }
            _ => panic!(),
        }
    }

    /// B5 — modifier sequence split as [👍, 🏿] → one Emoji piece.
    #[test]
    fn b5_cross_run_modifier_sequence() {
        let inlines = vec![text_run("\u{1F44D}"), text_run("\u{1F3FF}")];
        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        match &pieces[0] {
            SegmentPiece::Emoji {
                text, structure, ..
            } => {
                assert_eq!(text, "\u{1F44D}\u{1F3FF}");
                assert!(matches!(
                    structure,
                    EmojiStructure::ModifierSequence {
                        base: '\u{1F44D}',
                        ..
                    }
                ));
            }
            _ => panic!(),
        }
    }

    /// B6 — mixed: ["hi 1", VS-16+U+20E3, " there"] → three pieces:
    /// Text("hi "), Emoji("1️⃣"), Text(" there").
    #[test]
    fn b6_mixed_text_and_keycap() {
        let inlines = vec![
            text_run("hi 1"),
            text_run("\u{FE0F}\u{20E3}"),
            text_run(" there"),
        ];
        let r1 = extract_run(&inlines[0]);
        let r3 = extract_run(&inlines[2]);

        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 3);
        match &pieces[0] {
            SegmentPiece::Text { run, text } => {
                assert!(std::ptr::eq(*run, r1));
                assert_eq!(text, "hi ");
            }
            _ => panic!(),
        }
        match &pieces[1] {
            SegmentPiece::Emoji {
                base_run,
                text,
                structure,
                ..
            } => {
                assert!(std::ptr::eq(*base_run, r1));
                assert_eq!(text, "1\u{FE0F}\u{20E3}");
                assert!(matches!(
                    structure,
                    EmojiStructure::KeycapSequence { base: '1' }
                ));
            }
            _ => panic!(),
        }
        match &pieces[2] {
            SegmentPiece::Text { run, text } => {
                assert!(std::ptr::eq(*run, r3));
                assert_eq!(text, " there");
            }
            _ => panic!(),
        }
    }

    /// B7 — two distinct emoji "📞📧" in two runs → two Emoji pieces
    /// (UAX #29 clusters: each emoji is its own grapheme).
    #[test]
    fn b7_adjacent_distinct_emojis() {
        let inlines = vec![text_run("\u{1F4DE}"), text_run("\u{1F4E7}")];
        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 2);
        for p in &pieces {
            assert!(matches!(p, SegmentPiece::Emoji { .. }));
        }
    }

    /// B8 — combining mark cross-run: 'a' + U+0301 → one Text piece
    /// (UAX #29 binds the combining mark).
    #[test]
    fn b8_cross_run_combining_mark() {
        let inlines = vec![text_run("a"), text_run("\u{0301}")];
        let r1 = extract_run(&inlines[0]);

        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        match &pieces[0] {
            SegmentPiece::Text { run, text } => {
                assert!(std::ptr::eq(*run, r1));
                assert_eq!(text, "a\u{0301}");
            }
            _ => panic!(),
        }
    }

    /// B9 — RIS pair split: ["🇩", "🇪"] → one Emoji FlagSequence.
    #[test]
    fn b9_cross_run_ris_pair() {
        let inlines = vec![text_run("\u{1F1E9}"), text_run("\u{1F1EA}")];
        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        match &pieces[0] {
            SegmentPiece::Emoji {
                text, structure, ..
            } => {
                assert_eq!(text, "\u{1F1E9}\u{1F1EA}");
                assert!(matches!(
                    structure,
                    EmojiStructure::FlagSequence(crate::render::emoji::cluster::FlagKind::Regional)
                ));
            }
            _ => panic!(),
        }
    }

    /// B10 — default-text codepoints (digits) NOT promoted: regression
    /// for the digit fix. Single-run digits stay text.
    #[test]
    fn b10_digits_in_one_run_stay_text() {
        let inlines = vec![text_run("Numbers: 1, 2, 3")];
        let units = build_inline_units(&inlines);
        let seg = match units.into_iter().next().unwrap() {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        let pieces = seg.classify();
        assert_eq!(pieces.len(), 1);
        assert!(matches!(pieces[0], SegmentPiece::Text { .. }));
    }

    /// B11 — field char between text-only runs has already produced a
    /// segment break at A9; classification of each segment is independent.
    #[test]
    fn b11_field_char_isolates_segments() {
        use crate::model::{FieldChar, FieldCharType};
        let inlines = vec![
            text_run("a"),
            text_run("b"),
            Inline::FieldChar(FieldChar {
                field_char_type: FieldCharType::Begin,
                dirty: None,
                fld_lock: None,
            }),
            text_run("c"),
            text_run("d"),
        ];
        let units = build_inline_units(&inlines);
        assert_eq!(units.len(), 3);
        // First segment "ab" classifies independently.
        let seg1 = match &units[0] {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        assert_eq!(seg1.text(), "ab");
        // Second segment "cd" classifies independently.
        let seg2 = match &units[2] {
            InlineUnit::TextSegment(s) => s,
            _ => panic!(),
        };
        assert_eq!(seg2.text(), "cd");
    }
}