servo-layout 0.3.0

A component of the servo web-engine.
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::mem;
use std::ops::Range;
use std::sync::Arc;

use app_units::Au;
use fonts::{
    FontContext, FontRef, ShapedTextSlice, ShapedTextSlicer, ShapingFlags, ShapingOptions,
};
use icu_locid::subtags::Language;
use icu_properties::{self, LineBreak};
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use servo_arc::Arc as ServoArc;
use servo_base::text::is_bidi_control;
use style::Zero;
use style::computed_values::font_kerning::T as FontKerning;
use style::computed_values::font_variant_position::T as FontVariantPosition;
use style::computed_values::text_rendering::T as TextRendering;
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
use style::computed_values::word_break::T as WordBreak;
use style::properties::ComputedValues;
use style::str::char_is_whitespace;
use style::values::computed::{
    FontFeatureSettings, FontVariantEastAsian, FontVariantLigatures, FontVariantNumeric,
    OverflowWrap,
};
use unicode_bidi::{BidiInfo, Level};
use unicode_script::Script;

use super::line_breaker::LineBreaker;
use super::{InlineFormattingContextLayout, SharedInlineStyles};
use crate::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::WeakLayoutBox;
use crate::flow::inline::line::TextRunOffsets;
use crate::flow::inline::{LineBlockSizes, LineItem, SegmentContentFlags};
use crate::fragment_tree::BaseFragmentInfo;

// There are two reasons why we might want to break at the start:
//
//  1. The line breaker told us that a break was necessary between two separate
//     instances of sending text to it.
//  2. We are following replaced content ie `have_deferred_soft_wrap_opportunity`.
//
// In both cases, we don't want to do this if the first character prevents a
// soft wrap opportunity.
#[derive(PartialEq)]
enum SegmentStartSoftWrapPolicy {
    Force,
    FollowLinebreaker,
}

/// A data structure which contains information used when shaping a [`TextRunSegment`].
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub(crate) struct FontAndScriptInfo {
    /// The font used when shaping a [`TextRunSegment`].
    pub font: FontRef,
    /// The script used when shaping a [`TextRunSegment`].
    pub script: Script,
    /// The BiDi [`Level`] used when shaping a [`TextRunSegment`].
    pub bidi_level: Level,
    /// The [`Language`] used when shaping a [`TextRunSegment`].
    pub language: Language,
    /// Spacing to add between each letter. Corresponds to the CSS 2.1 `letter-spacing` property.
    ///
    /// Letter spacing is not applied to all characters. Use [Self::letter_spacing_for_character] to
    /// determine the amount of spacing to apply.
    pub letter_spacing: Option<Au>,
    /// Spacing to add between each word. Corresponds to the CSS 2.1 `word-spacing` property.
    pub word_spacing: Option<Au>,
    /// The [`TextRendering`] value from the original style.
    pub text_rendering: TextRendering,
    /// The value of the `font-kerning` property from the original style.
    pub kerning: FontKerning,
    /// The value of the `font-variant-ligatures` property from the original style.
    pub ligatures: FontVariantLigatures,
    /// The value of the `font-variant-numeric` property from the original style.
    pub numeric: FontVariantNumeric,
    /// The value of the `font-variant-east-asian` property from the original style.
    pub east_asian: FontVariantEastAsian,
    /// The value of the `font-feature-settings` property from the original style.
    pub feature_settings: FontFeatureSettings,
    /// The value of the `font-variant-position` property from the original style.
    pub position: FontVariantPosition,
}

impl FontAndScriptInfo {
    /// Creates a minimal [`FontAndScriptInfo`] for a single font, with generic language settings
    /// and the default shaping configuration. This is only used to generate placeholders for
    /// text carets on otherwise empty lines.
    pub(crate) fn simple_for_font(font: FontRef) -> Self {
        Self {
            font,
            script: Script::Common,
            bidi_level: Level::ltr(),
            language: Language::UND,
            letter_spacing: None,
            word_spacing: None,
            text_rendering: TextRendering::Auto,
            kerning: FontKerning::Auto,
            ligatures: FontVariantLigatures::NORMAL,
            numeric: FontVariantNumeric::NORMAL,
            east_asian: FontVariantEastAsian::NORMAL,
            feature_settings: FontFeatureSettings::normal(),
            position: FontVariantPosition::Normal,
        }
    }
}

impl From<&FontAndScriptInfo> for ShapingOptions {
    fn from(info: &FontAndScriptInfo) -> Self {
        let mut ligatures = info.ligatures;
        let mut flags = ShapingFlags::empty();
        if info.bidi_level.is_rtl() {
            flags.insert(ShapingFlags::RTL_FLAG);
        }

        // From https://www.w3.org/TR/css-text-3/#cursive-script:
        // Cursive scripts do not admit gaps between their letters for either
        // justification or letter-spacing.
        let letter_spacing = info
            .letter_spacing
            .filter(|_| !is_cursive_script(info.script));
        if letter_spacing.is_some() {
            ligatures = FontVariantLigatures::NONE;
        };
        if info.text_rendering == TextRendering::Optimizespeed {
            ligatures = FontVariantLigatures::NONE;
            flags.insert(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG)
        }

        // We currently always leave kerning enabled for "font-kerning: auto".
        if info.kerning == FontKerning::None {
            flags.insert(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG);
        }

        Self {
            letter_spacing,
            word_spacing: info.word_spacing,
            script: info.script,
            language: info.language,
            ligatures,
            numeric: info.numeric,
            east_asian: info.east_asian,
            feature_settings: info.feature_settings.clone(),
            position: info.position,
            flags,
        }
    }
}

#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) struct TextRunSegment {
    /// Information about the font and language used in this text run. This is produced by
    /// segmenting the inline formatting context's text content by font, script, and bidi level.
    #[conditional_malloc_size_of]
    pub info: Arc<FontAndScriptInfo>,

    /// The range of bytes in the parent [`super::InlineFormattingContext`]'s text content.
    pub byte_range: Range<usize>,

    /// The range of characters in the parent [`super::InlineFormattingContext`]'s text content.
    pub character_range: Range<usize>,

    /// Whether or not the linebreaker said that we should allow a line break at the start of this
    /// segment.
    pub break_at_start: bool,

    /// The shaped runs within this segment.
    #[conditional_malloc_size_of]
    pub runs: Vec<Arc<ShapedTextSlice>>,
}

impl TextRunSegment {
    fn new(
        info: Arc<FontAndScriptInfo>,
        byte_range: Range<usize>,
        character_range: Range<usize>,
    ) -> Self {
        Self {
            info,
            byte_range,
            character_range,
            runs: Vec::new(),
            break_at_start: false,
        }
    }

    /// Returns true if the new `Font`, `Script` and BiDi `Level` are compatible with this segment
    /// or false otherwise.
    fn is_compatible(
        &self,
        new_font: &Option<FontRef>,
        new_script: Script,
        new_bidi_level: Level,
    ) -> bool {
        if self.info.bidi_level != new_bidi_level {
            return false;
        }
        if new_font
            .as_ref()
            .is_some_and(|new_font| !Arc::ptr_eq(&self.info.font, new_font))
        {
            return false;
        }

        !script_is_specific(self.info.script) ||
            !script_is_specific(new_script) ||
            self.info.script == new_script
    }

    /// Update this segment to end at the given byte and character index. The update will only ever
    /// make the Script specific and will not change it otherwise.
    fn update(&mut self, next_byte_index: usize, next_character_index: usize, new_script: Script) {
        if !script_is_specific(self.info.script) && script_is_specific(new_script) {
            self.info = Arc::new(FontAndScriptInfo {
                script: new_script,
                ..(*self.info).clone()
            });
        }
        self.character_range.end = next_character_index;
        self.byte_range.end = next_byte_index;
    }

    fn layout_into_line_items(
        &self,
        text_run: &TextRun,
        mut soft_wrap_policy: SegmentStartSoftWrapPolicy,
        ifc: &mut InlineFormattingContextLayout,
    ) {
        if self.break_at_start && soft_wrap_policy == SegmentStartSoftWrapPolicy::FollowLinebreaker
        {
            soft_wrap_policy = SegmentStartSoftWrapPolicy::Force;
        }

        let mut character_range_start = self.character_range.start;
        for (run_index, run) in self.runs.iter().enumerate() {
            let new_character_range_end = character_range_start + run.character_count();
            let offsets = ifc
                .ifc
                .shared_selection
                .clone()
                .map(|shared_selection| TextRunOffsets {
                    shared_selection,
                    character_range: character_range_start..new_character_range_end,
                });

            // Break before each unbreakable run in this TextRun, except the first unless the
            // linebreaker was set to break before the first run.
            if run_index != 0 || soft_wrap_policy == SegmentStartSoftWrapPolicy::Force {
                ifc.process_soft_wrap_opportunity();
            }

            ifc.push_glyph_store_to_unbreakable_segment(run.clone(), text_run, &self.info, offsets);
            character_range_start = new_character_range_end;
        }
    }

    /// Shape the text of this [`TextRunSegment`], first finding "words" for the shaper by processing
    /// the linebreaks found in the owning [`super::InlineFormattingContext`]. Linebreaks are filtered,
    /// based on the style of the parent inline box.
    fn shape_text(
        &mut self,
        parent_style: &ComputedValues,
        formatting_context_text: &str,
        linebreaker: &mut LineBreaker,
        old_text_run_item: Option<TextRunItem>,
    ) {
        // Gather the linebreaks that apply to this segment from the inline formatting context's collection
        // of line breaks. Also add a simulated break at the end of the segment in order to ensure the final
        // piece of text is processed.
        let range = self.byte_range.clone();
        let linebreaks = linebreaker.advance_to_linebreaks_in_range(self.byte_range.clone());
        let linebreak_iter = linebreaks.iter().chain(std::iter::once(&range.end));

        let options: ShapingOptions = (&*self.info).into();
        let shaped_text = old_text_run_item
            .and_then(|old_text_run_item| {
                let TextRunItem::TextSegment(old_text_segment) = old_text_run_item else {
                    return None;
                };
                if !self.is_compatible_with_old_shaping_result(&old_text_segment) {
                    return None;
                }
                Some(old_text_segment.runs.first()?.shaped_text())
            })
            .unwrap_or_else(|| {
                self.info
                    .font
                    .shape_text(&formatting_context_text[range.clone()], &options)
            });

        let mut shaped_text_slicer = ShapedTextSlicer::new(shaped_text);

        self.runs.clear();
        self.runs.reserve(linebreaks.len());
        self.break_at_start = false;

        let text_style = parent_style.get_inherited_text().clone();
        let can_break_anywhere = text_style.word_break == WordBreak::BreakAll ||
            text_style.overflow_wrap == OverflowWrap::Anywhere ||
            text_style.overflow_wrap == OverflowWrap::BreakWord;

        let mut last_slice = self.byte_range.start..self.byte_range.start;
        for break_index in linebreak_iter {
            if *break_index == self.byte_range.start {
                self.break_at_start = true;
                continue;
            }

            // Extend the slice to the next UAX#14 line break opportunity.
            let mut slice = last_slice.end..*break_index;
            let word = &formatting_context_text[slice.clone()];

            // Split off any trailing whitespace into a separate glyph run.
            let mut whitespace = slice.end..slice.end;
            let rev_char_indices = word.char_indices().rev().peekable();

            let mut non_whitespace_slice_ends_with_whitespace = false;
            let mut ends_with_whitespace = false;
            if let Some((first_white_space_index, first_white_space_character)) = rev_char_indices
                .take_while(|&(_, character)| char_is_whitespace(character))
                .last()
            {
                ends_with_whitespace = true;
                whitespace.start = slice.start + first_white_space_index;

                // If line breaking for a piece of text that has `white-space-collapse:
                // break-spaces` there is a line break opportunity *after* every preserved space,
                // but not before. This means that we should not split off the first whitespace.
                //
                // An exception to this is if the style tells us that we can break in the middle of words.
                if text_style.white_space_collapse == WhiteSpaceCollapse::BreakSpaces &&
                    !can_break_anywhere
                {
                    whitespace.start += first_white_space_character.len_utf8();
                    non_whitespace_slice_ends_with_whitespace = true;
                }

                slice.end = whitespace.start;
            }

            // If there's no whitespace and `word-break` is set to `keep-all`, try increasing the slice.
            // TODO: This should only happen for CJK text.
            if !ends_with_whitespace &&
                *break_index != self.byte_range.end &&
                text_style.word_break == WordBreak::KeepAll &&
                !can_break_anywhere
            {
                continue;
            }

            // Only advance the last slice if we are not going to try to expand the slice.
            last_slice = slice.start..*break_index;

            // Push the non-whitespace part of the range.
            if !slice.is_empty() {
                let character_count = formatting_context_text[slice].chars().count();
                self.runs.push(shaped_text_slicer.slice_for_character_count(
                    character_count,
                    false, /* is_whitespace */
                    non_whitespace_slice_ends_with_whitespace,
                ));
            }

            if whitespace.is_empty() {
                continue;
            }

            // If `white-space-collapse: break-spaces` is active, insert a line breaking opportunity
            // between each white space character in the white space that we trimmed off.
            if text_style.white_space_collapse == WhiteSpaceCollapse::BreakSpaces {
                for _ in formatting_context_text[whitespace].chars() {
                    self.runs.push(shaped_text_slicer.slice_for_character_count(
                        1, true, /* is_whitespace */
                        true, /* ends_with_whitespace */
                    ));
                }
                continue;
            }

            let character_count = formatting_context_text[whitespace].chars().count();
            self.runs.push(shaped_text_slicer.slice_for_character_count(
                character_count,
                true, /* is_whitespace */
                true, /* ends_with_whitespace */
            ));
        }
    }

    fn is_compatible_with_old_shaping_result(&self, old_segment: &Self) -> bool {
        *old_segment.info == *self.info && self.byte_range == old_segment.byte_range
    }
}

/// A single item in a [`TextRun`].
#[derive(Debug, MallocSizeOf)]
pub(crate) enum TextRunItem {
    /// A hard line break i.e. a "\n" as other types line breaks are normalized to "\n".
    LineBreak { character_index: usize },
    /// A preserved tab character that should advance the line to a tab stop.
    Tab { bidi_level: Level },
    /// Any other text for which a font can be matched. We store a `Box` here as [`TextRunSegment`]
    /// is quite a bit larger than the other enum variants.
    TextSegment(Box<TextRunSegment>),
}

/// A single [`TextRun`] for the box tree. These are all descendants of
/// [`super::InlineBox`] or the root of the [`super::InlineFormattingContext`].  During
/// box tree construction, text is split into [`TextRun`]s based on their font, script,
/// etc. When these are created text is already shaped.
///
/// <https://www.w3.org/TR/css-display-3/#css-text-run>
#[derive(Debug, MallocSizeOf)]
pub(crate) struct TextRun {
    /// The [`BaseFragmentInfo`] for this [`TextRun`]. Usually this comes from the
    /// original text node in the DOM for the text.
    pub base_fragment_info: BaseFragmentInfo,

    /// A weak reference to the parent of this layout box. This becomes valid as soon
    /// as the *parent* of this box is added to the tree.
    pub parent_box: Option<WeakLayoutBox>,

    /// The [`crate::SharedStyle`] from this [`TextRun`]s parent element. This is
    /// shared so that incremental layout can simply update the parent element and
    /// this [`TextRun`] will be updated automatically.
    pub inline_styles: SharedInlineStyles,

    /// The range of text in [`super::InlineFormattingContext::text_content`] of the
    /// [`super::InlineFormattingContext`] that owns this [`TextRun`]. These are UTF-8 offsets.
    pub text_range: Range<usize>,

    /// The range of characters in this text in [`super::InlineFormattingContext::text_content`]
    /// of the [`super::InlineFormattingContext`] that owns this [`TextRun`]. These are *not*
    /// UTF-8 offsets.
    pub character_range: Range<usize>,

    /// The [`TextRunItem`]s of this text run. This is produced by segmenting the incoming text
    /// by things such as font and script as well as separating out hard line breaks.
    /// segments, and shaped.
    pub items: Vec<TextRunItem>,
}

impl TextRun {
    pub(crate) fn new(
        base_fragment_info: BaseFragmentInfo,
        inline_styles: SharedInlineStyles,
        text_range: Range<usize>,
        character_range: Range<usize>,
        old_text_run: Option<ArcRefCell<TextRun>>,
    ) -> Self {
        // If there was a previous box tree layout of this text run, try to preserve the old shaped text.
        let items = old_text_run
            .map(|old_text_run| std::mem::take(&mut old_text_run.borrow_mut().items))
            .unwrap_or_default();
        Self {
            base_fragment_info,
            parent_box: None,
            inline_styles,
            text_range,
            character_range,
            items,
        }
    }

    pub(super) fn segment_and_shape(
        &mut self,
        formatting_context_text: &str,
        layout_context: &LayoutContext,
        linebreaker: &mut LineBreaker,
        bidi_info: &BidiInfo,
    ) {
        let parent_style = self.inline_styles.style.borrow().clone();
        let items = self.segment_text_by_font(
            layout_context,
            formatting_context_text,
            bidi_info,
            &parent_style,
        );

        // If a previous box tree layout seeded this [`TextRun`] with old shaping results, use those
        // to try to prevent re-shaping.
        let mut old_text_run_items = std::mem::replace(&mut self.items, items).into_iter();
        for item in self.items.iter_mut() {
            let old_text_run_item = old_text_run_items.next();
            if let TextRunItem::TextSegment(text_segment) = item {
                text_segment.shape_text(
                    &parent_style,
                    formatting_context_text,
                    linebreaker,
                    old_text_run_item,
                );
            }
        }
    }

    /// Take the [`TextRun`]'s text and turn it into [`TextRunSegment`]s. Each segment has a matched
    /// font and script. Fonts may differ when glyphs are found in fallback fonts.
    /// [`super::InlineFormattingContext`].
    fn segment_text_by_font(
        &mut self,
        layout_context: &LayoutContext,
        formatting_context_text: &str,
        bidi_info: &BidiInfo,
        parent_style: &ServoArc<ComputedValues>,
    ) -> Vec<TextRunItem> {
        let font_style = parent_style.clone_font();
        let language = font_style._x_lang.0.parse().unwrap_or(Language::UND);
        let font_size = font_style.font_size.computed_size().into();
        let kerning = font_style.font_kerning;
        let ligatures = font_style.font_variant_ligatures;
        let numeric = font_style.font_variant_numeric;
        let east_asian = font_style.font_variant_east_asian;
        let feature_settings = font_style.font_feature_settings.clone();
        let position = font_style.font_variant_position;
        let font_group = layout_context.font_context.font_group(font_style);
        let inherited_text_style = parent_style.get_inherited_text();
        let word_spacing = Some(inherited_text_style.word_spacing.to_used_value(font_size));
        let letter_spacing = inherited_text_style
            .letter_spacing
            .0
            .to_used_value(font_size);
        let letter_spacing = if !letter_spacing.is_zero() {
            Some(letter_spacing)
        } else {
            None
        };
        let text_rendering = inherited_text_style.text_rendering;

        let mut current: Option<TextRunSegment> = None;
        let mut results = Vec::new();
        let finish_current_segment =
            |current: &mut Option<TextRunSegment>, results: &mut Vec<TextRunItem>| {
                if let Some(current) = current.take() {
                    results.push(TextRunItem::TextSegment(Box::new(current)));
                }
            };

        let text_run_text = &formatting_context_text[self.text_range.clone()];
        let char_iterator = TwoCharsAtATimeIterator::new(text_run_text.chars());
        // The next bytes index of the character within the entire inline formatting context's text.
        let mut next_byte_index = self.text_range.start;
        for (relative_character_index, (character, next_character)) in char_iterator.enumerate() {
            // The current character index within the entire inline formatting context's text.
            let current_character_index = self.character_range.start + relative_character_index;

            let current_byte_index = next_byte_index;
            next_byte_index += character.len_utf8();

            if character == '\n' {
                finish_current_segment(&mut current, &mut results);
                results.push(TextRunItem::LineBreak {
                    character_index: current_character_index,
                });
                continue;
            }

            if character == '\t' {
                finish_current_segment(&mut current, &mut results);
                results.push(TextRunItem::Tab {
                    bidi_level: bidi_info.levels[current_byte_index],
                });
                continue;
            }

            let (font, script, bidi_level) = if character_cannot_change_font(character) {
                (None, Script::Common, bidi_info.levels[current_byte_index])
            } else {
                (
                    font_group.find_by_codepoint(
                        &layout_context.font_context,
                        character,
                        next_character,
                        language,
                    ),
                    Script::from(character),
                    bidi_info.levels[current_byte_index],
                )
            };

            // If the existing segment is compatible with the character, just merge the character into it.
            if let Some(current) = current.as_mut() &&
                current.is_compatible(&font, script, bidi_level)
            {
                current.update(next_byte_index, current_character_index + 1, script);
                continue;
            }

            let Some(font) = font.or_else(|| font_group.first(&layout_context.font_context)) else {
                continue;
            };
            let info = FontAndScriptInfo {
                font,
                script,
                bidi_level,
                language,
                word_spacing,
                letter_spacing,
                text_rendering,
                kerning,
                ligatures,
                numeric,
                east_asian,
                feature_settings: feature_settings.clone(),
                position,
            };

            finish_current_segment(&mut current, &mut results);
            assert!(current.is_none());

            current = Some(TextRunSegment::new(
                Arc::new(info),
                current_byte_index..next_byte_index,
                current_character_index..current_character_index + 1,
            ));
        }

        finish_current_segment(&mut current, &mut results);
        results
    }

    pub(super) fn layout_into_line_items(&self, ifc: &mut InlineFormattingContextLayout) {
        if self.text_range.is_empty() {
            return;
        }

        // If we are following replaced content, we should have a soft wrap opportunity, unless the
        // first character of this `TextRun` prevents that soft wrap opportunity. If we see such a
        // character it should also override the LineBreaker's indication to break at the start.
        let have_deferred_soft_wrap_opportunity =
            mem::replace(&mut ifc.have_deferred_soft_wrap_opportunity, false);
        let mut soft_wrap_policy = match have_deferred_soft_wrap_opportunity {
            true => SegmentStartSoftWrapPolicy::Force,
            false => SegmentStartSoftWrapPolicy::FollowLinebreaker,
        };

        for item in self.items.iter() {
            ifc.possibly_flush_deferred_forced_line_break();

            match item {
                // If this whitespace forces a line break, queue up a hard line break the next time we
                // see any content. We don't line break immediately, because we'd like to finish processing
                // any ongoing inline boxes before ending the line.
                TextRunItem::LineBreak { character_index } => {
                    ifc.defer_forced_line_break_at_character_offset(*character_index);
                },
                TextRunItem::Tab { bidi_level } => self.process_preserved_tab(ifc, *bidi_level),
                TextRunItem::TextSegment(segment) => {
                    segment.layout_into_line_items(self, soft_wrap_policy, ifc)
                },
            }
            soft_wrap_policy = SegmentStartSoftWrapPolicy::FollowLinebreaker;
        }
    }

    fn process_preserved_tab(
        &self,
        ifc_layout: &mut InlineFormattingContextLayout,
        bidi_level: Level,
    ) {
        let advance = ifc_layout
            .ifc
            .next_tab_stop_after_inline_advance(ifc_layout.potential_line_size().inline);
        if advance.is_zero() {
            return;
        }

        ifc_layout.update_unbreakable_segment_for_new_content(
            &LineBlockSizes::zero(),
            advance,
            SegmentContentFlags::empty(),
        );
        ifc_layout.push_line_item_to_unbreakable_segment(LineItem::Tab {
            inline_box_identifier: ifc_layout.current_inline_box_identifier(),
            advance,
            bidi_level,
        });

        if ifc_layout
            .current_inline_container_state()
            .style
            .get_inherited_text()
            .white_space_collapse ==
            WhiteSpaceCollapse::BreakSpaces
        {
            ifc_layout.process_soft_wrap_opportunity();
        }
    }
}

/// From <https://www.w3.org/TR/css-text-3/#cursive-script>:
/// Cursive scripts do not admit gaps between their letters for either justification
/// or letter-spacing. The following Unicode scripts are included: Arabic, Hanifi
/// Rohingya, Mandaic, Mongolian, N’Ko, Phags Pa, Syriac
fn is_cursive_script(script: Script) -> bool {
    matches!(
        script,
        Script::Arabic |
            Script::Hanifi_Rohingya |
            Script::Mandaic |
            Script::Mongolian |
            Script::Nko |
            Script::Phags_Pa |
            Script::Syriac
    )
}

/// Whether or not this character should be able to change the font during segmentation.  Certain
/// character are not rendered at all, so it doesn't matter what font we use to render them. They
/// should just be added to the current segment.
fn character_cannot_change_font(character: char) -> bool {
    if character.is_control() {
        return true;
    }
    if character == '\u{00A0}' {
        return true;
    }
    if is_bidi_control(character) {
        return false;
    }

    matches!(
        icu_properties::maps::line_break().get(character),
        LineBreak::CombiningMark |
            LineBreak::Glue |
            LineBreak::ZWSpace |
            LineBreak::WordJoiner |
            LineBreak::ZWJ
    )
}

pub(super) fn get_font_for_first_font_for_style(
    style: &ComputedValues,
    font_context: &FontContext,
) -> Option<FontRef> {
    let font = font_context
        .font_group(style.clone_font())
        .first(font_context);
    if font.is_none() {
        warn!("Could not find font for style: {:?}", style.clone_font());
    }
    font
}
pub(crate) struct TwoCharsAtATimeIterator<InputIterator> {
    /// The input character iterator.
    iterator: InputIterator,
    /// The first character to produce in the next run of the iterator.
    next_character: Option<char>,
}

impl<InputIterator> TwoCharsAtATimeIterator<InputIterator> {
    fn new(iterator: InputIterator) -> Self {
        Self {
            iterator,
            next_character: None,
        }
    }
}

impl<InputIterator> Iterator for TwoCharsAtATimeIterator<InputIterator>
where
    InputIterator: Iterator<Item = char>,
{
    type Item = (char, Option<char>);

    fn next(&mut self) -> Option<Self::Item> {
        // If the iterator isn't initialized do that now.
        if self.next_character.is_none() {
            self.next_character = self.iterator.next();
        }
        let character = self.next_character?;
        self.next_character = self.iterator.next();
        Some((character, self.next_character))
    }
}

fn script_is_specific(script: Script) -> bool {
    script != Script::Common && script != Script::Inherited
}