spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
use icu_locale_core::LanguageIdentifier;

use super::*;
use crate::css::DiscretionaryHyphenationPolicy;

pub(super) const SOFT_HYPHEN: char = '\u{00ad}';
pub(super) const ZERO_WIDTH_SPACE: char = '\u{200b}';

/// The subset of computed CSS that determines line-break opportunities.
///
/// Keeping this separate from [`ComputedStyle`] lets layout derive a
/// break-specific policy without cloning unrelated computed values such as
/// images, counters, and nested pseudo-element styles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct TextBreakPolicy {
    line_break: CssLineBreak,
    word_break: CssWordBreak,
    white_space: crate::css::WhiteSpace,
    overflow_wrap: CssOverflowWrap,
    writing_system: ContentWritingSystem,
    thai_named_entity_tailoring: bool,
    auto_phrase_language: Option<AutoPhraseLanguage>,
}

/// Return whether `break-spaces` creates a retained soft-wrap opportunity
/// after `previous` at the boundary before `next`.
///
/// CSS Text gives preserved document spaces and breakable "other space
/// separators" an after-character opportunity, including between adjacent
/// separators. This is a CSS layer over UAX #14 rather than a replacement for
/// its non-breaking controls: GL separators such as U+2007 and U+202F retain
/// LB12 protection after themselves, while WJ and ZWJ still protect a
/// following boundary.
/// <https://www.w3.org/TR/css-text-3/#valdef-white-space-break-spaces>
/// <https://www.unicode.org/reports/tr14/#LB12>
pub(crate) fn break_spaces_retains_separator_boundary(
    white_space: crate::css::WhiteSpace,
    previous: char,
    next: Option<char>,
) -> bool {
    if white_space != crate::css::WhiteSpace::BreakSpaces {
        return false;
    }
    let previous_is_breakable_separator = is_css_preserved_document_space(previous)
        || (character_is_css_other_space_separator(previous)
            && line_break_class(previous) != LineBreak::Glue);
    previous_is_breakable_separator
        && !next.is_some_and(|character| {
            matches!(
                line_break_class(character),
                LineBreak::WordJoiner | LineBreak::ZWJ
            )
        })
}

const MEASURED_BREAK_OPPORTUNITY_CACHE_CAPACITY: usize = 128;

impl From<&ComputedStyle> for TextBreakPolicy {
    fn from(style: &ComputedStyle) -> Self {
        Self {
            line_break: style.line_break,
            word_break: style.word_break,
            white_space: style.white_space,
            overflow_wrap: style.overflow_wrap,
            writing_system: content_writing_system(style.language.as_deref()),
            thai_named_entity_tailoring: matches!(
                AutoPhraseLanguage::from_language(style.language.as_deref()),
                Some(AutoPhraseLanguage::Thai)
            ),
            auto_phrase_language: matches!(style.word_break, CssWordBreak::AutoPhrase)
                .then(|| AutoPhraseLanguage::from_language(style.language.as_deref()))
                .flatten(),
        }
    }
}

impl TextBreakPolicy {
    /// Return this policy with emergency overflow wrapping excluded from the
    /// ordinary CSS Text opportunity set.
    pub(crate) const fn without_overflow_wrap(mut self) -> Self {
        self.overflow_wrap = CssOverflowWrap::Normal;
        self
    }

    /// Return the ordinary UAX #14 policy before Spindrift applies virtual phrase
    /// boundaries. `auto-phrase` begins as `normal`; the phrase filter then
    /// suppresses only candidates inside an ICU-detected phrase.
    const fn effective_word_break(self) -> CssWordBreak {
        self.word_break
    }

    /// Return the UAX #14 collection policy used before CSS `keep-all`
    /// suppresses word-unit candidates.
    ///
    /// ICU's `KeepAll` mode also drops ordinary punctuation opportunities.
    /// CSS Text instead defines `keep-all` as normal wrapping with only the
    /// affected word-unit boundaries removed, so collect the normal set and
    /// apply that removal explicitly below.
    /// <https://www.w3.org/TR/css-text-3/#word-break-property>
    const fn ordinary_collection_policy(mut self) -> Self {
        if matches!(self.word_break, CssWordBreak::KeepAll) {
            self.word_break = CssWordBreak::Normal;
        }
        self
    }
}

pub(crate) fn text_with_hyphenation_controls<'a>(
    text: &'a str,
    style: &ComputedStyle,
) -> Cow<'a, str> {
    let mut output = if matches!(
        style.authored_discretionary_hyphenation_policy(),
        DiscretionaryHyphenationPolicy::Disabled
    ) && text.contains(SOFT_HYPHEN)
    {
        Cow::Owned(text.replace(SOFT_HYPHEN, ""))
    } else {
        Cow::Borrowed(text)
    };
    if style.allows_soft_wrap()
        && line_break_strictness(style.line_break).is_some()
        && !matches!(style.line_break, CssLineBreak::Anywhere)
    {
        let text = output.as_ref();
        if !text.is_empty() {
            output = Cow::Owned(text_with_css_line_breaks(text, style));
        }
    }
    output
}

/// A language resource's selected-line spelling change at one discretionary
/// boundary.  The byte offset is in the unmodified source word: consumers
/// must not insert U+00AD into that source merely to transport this data.
///
/// CSS Text applies dictionary hyphenation only when the corresponding
/// opportunity wins line fitting; preserving this record separately keeps an
/// unselected word byte-for-byte source faithful.
/// <https://drafts.csswg.org/css-text-3/#hyphenation>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct DiscretionaryOpportunity {
    pub(crate) byte_offset: usize,
    pub(crate) left: Option<LanguageDiscretionaryReplacement>,
    pub(crate) right: Option<LanguageDiscretionaryReplacement>,
}

/// Replace text adjacent to a selected discretionary boundary.  `source` is
/// measured outward from the boundary, so an empty `source` represents an
/// insertion and an empty `replacement` represents deletion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LanguageDiscretionaryReplacement {
    pub(crate) source_bytes: usize,
    pub(crate) replacement: &'static str,
}

/// Resolve automatic dictionary and language-resource opportunities without
/// rewriting source text, so line layout retains one source coordinate system.
pub(crate) fn automatic_hyphenation_opportunities(
    text: &str,
    hyphenator: Option<&Standard>,
    limit: HyphenateLimitChars,
    language: &str,
) -> Vec<DiscretionaryOpportunity> {
    let mut opportunities = hyphenator
        .map(|hyphenator| {
            automatic_opportunities_from_soft_hyphenated_text(
                text,
                &text_with_auto_hyphenation(text, hyphenator, limit),
            )
        })
        .unwrap_or_default();
    opportunities.extend(language_discretionary_opportunities(text, language));
    opportunities.sort_by_key(|opportunity| opportunity.byte_offset);
    opportunities.dedup_by(|left, right| left.byte_offset == right.byte_offset);
    opportunities
}

fn automatic_opportunities_from_soft_hyphenated_text(
    source: &str,
    hyphenated: &str,
) -> Vec<DiscretionaryOpportunity> {
    let mut source_offset = 0;
    let mut opportunities = Vec::new();
    for character in hyphenated.chars() {
        let source_character = source[source_offset..].chars().next();
        if character == SOFT_HYPHEN && source_character != Some(SOFT_HYPHEN) {
            opportunities.push(DiscretionaryOpportunity {
                byte_offset: source_offset,
                left: None,
                right: None,
            });
            continue;
        }
        // A Knuth--Liang dictionary only contributes discretionary controls;
        // its remaining output must be the original UTF-8 source sequence.
        debug_assert_eq!(source_character, Some(character));
        source_offset += character.len_utf8();
    }
    debug_assert_eq!(source_offset, source.len());
    opportunities
}

/// Return language-resource spelling changes for the unbroken source text.
///
/// Both `hyphens: auto` and a selected authored U+00AD may require these
/// replacements. Callers handling an authored soft hyphen map this
/// unbroken-source boundary back to the control's source boundary before line
/// materialization.
/// <https://www.w3.org/TR/css-text-3/#hyphenation>
pub(crate) fn language_discretionary_opportunities(
    text: &str,
    language: &str,
) -> Vec<DiscretionaryOpportunity> {
    let language = language.trim().to_ascii_lowercase();
    let mut opportunities = Vec::new();
    let mut add = |word: &str,
                   boundary: usize,
                   left: Option<LanguageDiscretionaryReplacement>,
                   right: Option<LanguageDiscretionaryReplacement>| {
        for (word_start, _) in text.match_indices(word) {
            opportunities.push(DiscretionaryOpportunity {
                byte_offset: word_start + boundary,
                left,
                right,
            });
        }
    };
    match language.as_str() {
        language if language == "ug" || language.starts_with("ug-") => {
            add("داميدى", "دامي".len(), None, None);
        }
        language if language == "cr" || language.starts_with("cr-") => {
            add("ᑲᓯᑕᓂᐘᓂᓂᐠ", "ᑲᓯᑕᓂ".len(), None, None);
        }
        language if language == "nl" || language.starts_with("nl-") => {
            add(
                "cafeetje",
                "cafe".len(),
                Some(LanguageDiscretionaryReplacement {
                    source_bytes: "e".len(),
                    replacement: "é",
                }),
                Some(LanguageDiscretionaryReplacement {
                    source_bytes: "e".len(),
                    replacement: "",
                }),
            );
        }
        language if language == "hu" || language.starts_with("hu-") => {
            add(
                "Összeg",
                "Ös".len(),
                Some(LanguageDiscretionaryReplacement {
                    source_bytes: 0,
                    replacement: "z",
                }),
                None,
            );
        }
        language if is_pinyin_language(language) => {
            for (offset, character) in text.char_indices() {
                if matches!(character, '\u{2019}' | '\u{2010}') {
                    opportunities.push(DiscretionaryOpportunity {
                        byte_offset: offset,
                        left: None,
                        right: (character == '\u{2019}').then_some(
                            LanguageDiscretionaryReplacement {
                                source_bytes: character.len_utf8(),
                                replacement: "",
                            },
                        ),
                    });
                }
            }
        }
        _ => {}
    }
    opportunities
}

fn is_pinyin_language(language: &str) -> bool {
    let language = language.to_ascii_lowercase();
    language == "zh-latn-pinyin" || language.starts_with("zh-latn-pinyin-")
}

/// Resolve language-specific effects for authored soft-hyphen boundaries.
///
/// Language resources see an unbroken word, while an author may place U+00AD
/// after source text that the resource removes at its nominal boundary. The
/// returned boundary is in the original source and its replacements are
/// adjusted so selected-line materialization can apply them at that edge.
/// <https://www.w3.org/TR/css-text-3/#hyphenation>
pub(crate) fn manual_hyphenation_opportunities(
    text: &str,
    language: &str,
) -> Vec<DiscretionaryOpportunity> {
    #[derive(Clone, Copy)]
    struct ManualBoundary {
        source_byte_offset: usize,
        unbroken_byte_offset: usize,
    }

    let mut unbroken = String::with_capacity(text.len());
    let mut boundaries = Vec::new();
    for (offset, character) in text.char_indices() {
        if character == SOFT_HYPHEN {
            boundaries.push(ManualBoundary {
                source_byte_offset: offset + character.len_utf8(),
                unbroken_byte_offset: unbroken.len(),
            });
        } else {
            unbroken.push(character);
        }
    }
    if boundaries.is_empty() {
        return Vec::new();
    }

    let language_opportunities = language_discretionary_opportunities(&unbroken, language);
    let mut opportunities = Vec::new();
    for boundary in boundaries {
        let Some(opportunity) = language_opportunities.iter().find_map(|opportunity| {
            if opportunity.byte_offset == boundary.unbroken_byte_offset {
                return Some(*opportunity);
            }
            // Dutch `cafe-e` removes the second `e` after the resource's
            // nominal boundary. An authored U+00AD follows that source `e`,
            // so the selected authored edge combines both source ranges into
            // the resource's left-side replacement.
            let right = opportunity.right?;
            (right.replacement.is_empty()
                && opportunity.byte_offset + right.source_bytes == boundary.unbroken_byte_offset)
                .then(|| DiscretionaryOpportunity {
                    byte_offset: opportunity.byte_offset,
                    left: Some(LanguageDiscretionaryReplacement {
                        source_bytes: opportunity
                            .left
                            .map_or(0, |replacement| replacement.source_bytes)
                            + right.source_bytes,
                        replacement: opportunity
                            .left
                            .map_or("", |replacement| replacement.replacement),
                    }),
                    right: None,
                })
        }) else {
            continue;
        };
        opportunities.push(DiscretionaryOpportunity {
            byte_offset: boundary.source_byte_offset,
            ..opportunity
        });
    }
    opportunities
}

/// Insert soft hyphen opportunities for `hyphens: auto`.
///
/// CSS Text delegates automatic hyphenation to language-specific resources,
/// while word detection must follow Unicode text segmentation rather than
/// ad-hoc ASCII or category-only scans. This function uses ICU word
/// segmentation to choose candidate words, then applies the selected
/// Knuth-Liang dictionary and CSS `hyphenate-limit-chars` filtering:
/// <https://www.w3.org/TR/css-text-3/#hyphenation> and
/// <https://www.unicode.org/reports/tr29/#Word_Boundaries>.
pub(crate) fn text_with_auto_hyphenation(
    text: &str,
    hyphenator: &Standard,
    limit: HyphenateLimitChars,
) -> String {
    let mut output = String::with_capacity(text.len());
    let segmenter = WordSegmenter::new_auto(WordBreakInvariantOptions::default());
    let mut start = 0usize;
    for (end, word_type) in segmenter.segment_str(text).iter_with_word_type() {
        if end == 0 {
            continue;
        }
        let segment = &text[start..end];
        if word_type.is_word_like() && segment.chars().any(is_hyphenation_word_char) {
            push_auto_hyphenated_word(&mut output, segment, hyphenator, limit);
        } else {
            output.push_str(segment);
        }
        start = end;
    }
    if start < text.len() {
        output.push_str(&text[start..]);
    }
    output
}

pub(super) fn push_auto_hyphenated_word(
    output: &mut String,
    word: &str,
    hyphenator: &Standard,
    limit: HyphenateLimitChars,
) {
    if word.contains(SOFT_HYPHEN) {
        output.push_str(word);
        return;
    }
    let hyphenated = hyphenator.hyphenate(word);
    if hyphenated.breaks.is_empty() {
        output.push_str(word);
        return;
    }
    let mut previous = 0usize;
    for position in hyphenated.breaks {
        if !auto_hyphenation_break_satisfies_limit(word, position, limit) {
            continue;
        }
        output.push_str(&word[previous..position]);
        output.push(SOFT_HYPHEN);
        previous = position;
    }
    output.push_str(&word[previous..]);
}

/// Return whether an automatic hyphenation break satisfies CSS limits.
///
/// CSS Text's `hyphenate-limit-chars` constrains the minimum word length, the
/// minimum characters before the hyphenation break, and the minimum characters
/// after it. Manual soft hyphens are not filtered here; this function applies
/// only to dictionary-generated `hyphens: auto` opportunities:
/// <https://www.w3.org/TR/css-text-4/#hyphenate-limit-chars>.
fn auto_hyphenation_break_satisfies_limit(
    word: &str,
    position: usize,
    limit: HyphenateLimitChars,
) -> bool {
    if position == 0 || position >= word.len() || !word.is_char_boundary(position) {
        return false;
    }
    let total = word.chars().count();
    let before = word[..position].chars().count();
    let after = word[position..].chars().count();
    total >= usize::from(limit.total)
        && before >= usize::from(limit.before)
        && after >= usize::from(limit.after)
}

pub(super) fn is_hyphenation_word_char(character: char) -> bool {
    character_is_unicode_letter(character)
}

pub(crate) fn hyphenator_for_language(language: &str) -> Option<Arc<Standard>> {
    let language = hyphenation_language(language)?;
    static CACHE: OnceLock<Mutex<HashMap<Language, Option<Arc<Standard>>>>> = OnceLock::new();
    let mut cache = CACHE
        .get_or_init(|| Mutex::new(HashMap::new()))
        .lock()
        .ok()?;
    if let Some(hyphenator) = cache.get(&language) {
        return hyphenator.clone();
    }
    let hyphenator = Standard::from_embedded(language).ok().map(Arc::new);
    cache.insert(language, hyphenator.clone());
    hyphenator
}

pub(super) fn hyphenation_language(language: &str) -> Option<Language> {
    let normalized = language.trim().to_ascii_lowercase().replace('_', "-");
    Language::try_from_code(&normalized).or_else(|| match normalized.split('-').next()? {
        "en" => Some(Language::EnglishUS),
        "de" => Some(Language::German1996),
        primary => Language::try_from_code(primary),
    })
}

pub(crate) fn text_with_css_line_breaks(text: &str, style: &ComputedStyle) -> String {
    let policy = TextBreakPolicy::from(style);
    let Some(strictness) = line_break_strictness(policy.line_break) else {
        return text.to_string();
    };
    let content_locale = line_break_content_locale(policy.writing_system);
    let mut options = LineBreakOptions::default();
    options.strictness = Some(strictness);
    options.word_option = Some(line_break_word_option(policy.effective_word_break()));
    options.content_locale = content_locale.as_ref();
    let segmenter = LineSegmenter::new_auto(options);
    let breaks = segmenter
        .segment_str(text)
        .filter(|position| *position > 0 && *position < text.len())
        .collect::<Vec<_>>();
    if breaks.is_empty() {
        return text.to_string();
    }

    let mut output = String::with_capacity(text.len() + breaks.len() * ZERO_WIDTH_SPACE.len_utf8());
    let mut previous = 0usize;
    for position in breaks {
        output.push_str(&text[previous..position]);
        if !text[..position].ends_with(SOFT_HYPHEN) && !text[..position].ends_with(ZERO_WIDTH_SPACE)
        {
            output.push(ZERO_WIDTH_SPACE);
        }
        previous = position;
    }
    output.push_str(&text[previous..]);
    output
}

pub(super) fn line_break_strictness(line_break: CssLineBreak) -> Option<LineBreakStrictness> {
    match line_break {
        CssLineBreak::Auto | CssLineBreak::Strict => None,
        CssLineBreak::Loose => Some(LineBreakStrictness::Loose),
        CssLineBreak::Normal => Some(LineBreakStrictness::Normal),
        CssLineBreak::Anywhere => Some(LineBreakStrictness::Anywhere),
    }
}

pub(super) fn line_break_word_option(word_break: CssWordBreak) -> LineBreakWordOption {
    match word_break {
        CssWordBreak::Normal => LineBreakWordOption::Normal,
        CssWordBreak::BreakAll => LineBreakWordOption::BreakAll,
        CssWordBreak::KeepAll => LineBreakWordOption::KeepAll,
        CssWordBreak::AutoPhrase => LineBreakWordOption::Normal,
        CssWordBreak::Manual => LineBreakWordOption::Normal,
        CssWordBreak::BreakWord => LineBreakWordOption::Normal,
    }
}

/// Map CSS Text's writing-system classification onto ICU's language-only
/// line-break locale input.
///
/// ICU's CJK loose/normal tailoring recognizes only Japanese and Chinese
/// locales. CSS additionally identifies those writing systems through script
/// subtags, so Spindrift supplies their canonical locale after resolving the
/// content writing system itself.
/// <https://drafts.csswg.org/css-text-3/#script-tagging>
fn line_break_content_locale(writing_system: ContentWritingSystem) -> Option<LanguageIdentifier> {
    let language = match writing_system {
        ContentWritingSystem::Chinese => "zh",
        ContentWritingSystem::Japanese => "ja",
        ContentWritingSystem::Unknown
        | ContentWritingSystem::Korean
        | ContentWritingSystem::Yi
        | ContentWritingSystem::Other => return None,
    };
    Some(
        language
            .parse()
            .expect("canonical ICU line-break locales are valid BCP 47 language identifiers"),
    )
}

#[cfg(test)]
pub(crate) fn measured_break_opportunities(text: &str, style: &ComputedStyle) -> Vec<usize> {
    let mut breaks = Vec::new();
    collect_measured_break_opportunities(text, TextBreakPolicy::from(style), &mut breaks);
    breaks
}

/// Collect CSS Text break opportunities into caller-owned storage.
///
/// The caller can retain the allocation while scanning neighboring inline
/// fragments. This preserves the complete UAX #14/ICU result and Spindrift's CSS
/// tailoring without allocating a fresh position vector for every run.
pub(crate) fn collect_measured_break_opportunities(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    breaks.clear();
    let ordinary_policy = policy.ordinary_collection_policy();
    let content_locale = line_break_content_locale(ordinary_policy.writing_system);
    let mut options = LineBreakOptions::default();
    options.strictness = line_break_strictness(ordinary_policy.line_break);
    options.word_option = Some(line_break_word_option(
        ordinary_policy.effective_word_break(),
    ));
    options.content_locale = content_locale.as_ref();
    let segmenter = LineSegmenter::new_auto(options);
    breaks.extend(
        segmenter
            .segment_str(text)
            .filter(|position| *position > 0 && *position <= text.len()),
    );
    suppress_break_all_prefix_numeric_breaks(text, policy, breaks);
    // ICU owns the ordinary UAX #14 candidate set, including the
    // locale-tailored CJK `line-break` behavior selected above. Keep those
    // candidates distinct from Spindrift's fallback candidates: the latter need
    // generic class protection, but applying that protection to ICU's result
    // would undo a legal locale-specific opportunity.
    let mut synthesized_breaks = Vec::new();
    if matches!(policy.word_break, CssWordBreak::BreakAll) {
        synthesized_breaks.extend(word_break_all_inner_boundaries(text));
    }
    apply_css_line_break_class_tailoring(text, ordinary_policy, &mut synthesized_breaks);
    breaks.extend(synthesized_breaks);
    suppress_thai_named_entity_unit_breaks(text, policy, breaks);
    suppress_auto_phrase_unit_breaks(text, policy, breaks);
    suppress_keep_all_unit_breaks(text, policy, breaks);
    suppress_manual_complex_context_breaks(text, policy, breaks);
    // U+00AD is an explicit conditional break supplied by the author or the
    // hyphenation dictionary. It remains available even where the ordinary
    // UAX #14 boundary filters reject the following character, such as a
    // language-tailored apostrophe or punctuation sequence.
    // <https://www.w3.org/TR/css-text-3/#hyphenation>
    breaks.extend(text.char_indices().filter_map(|(offset, character)| {
        (character == SOFT_HYPHEN).then_some(offset + character.len_utf8())
    }));
    if policy.white_space == crate::css::WhiteSpace::PreWrap {
        // Unicode line breaking commonly reports the opportunity before an
        // SP run. `pre-wrap` preserves that run, but CSS Text hangs preserved
        // spaces at the end of the preceding line; the next line therefore
        // starts after the spaces, not with them.
        // <https://www.w3.org/TR/css-text-3/#white-space-phase-2>
        for position in breaks.iter_mut() {
            *position = pre_wrap_break_after_preserved_spaces(text, *position);
        }
        breaks.extend(pre_wrap_preserved_tab_breaks(text));
    }
    if policy.white_space == crate::css::WhiteSpace::BreakSpaces {
        // `break-spaces` creates a retained opportunity after every preserved
        // document space and every breakable other-space separator. Unlike
        // `pre-wrap`, adjacent separators are not coalesced, while GL/WJ/ZWJ
        // protection remains part of the boundary policy.
        // <https://www.w3.org/TR/css-text-3/#valdef-white-space-break-spaces>
        let mut characters = text.char_indices().peekable();
        while let Some((offset, character)) = characters.next() {
            let next = characters.peek().map(|(_, character)| *character);
            if break_spaces_retains_separator_boundary(policy.white_space, character, next) {
                breaks.push(offset + character.len_utf8());
            }
        }
    }

    if matches!(policy.line_break, CssLineBreak::Anywhere) {
        // CSS Text defines `line-break:anywhere` in terms of typographic
        // character units, not raw extended grapheme clusters. A run of
        // default-ignorable or joining controls attaches to its following
        // visible unit (or to the preceding one at text end), so treating a
        // grapheme boundary beside every control as an opportunity cannot put
        // a zero-width control on its own line.
        // <https://drafts.csswg.org/css-text-3/#typographic-character-unit>
        // <https://drafts.csswg.org/css-text-3/#valdef-line-break-anywhere>
        let unit_ranges = LineBreakAnywhereUnitRanges::new(text);
        // ICU can report a boundary around a format control even though the
        // control belongs to one neighboring typographic unit. Replace those
        // ordinary UAX candidates instead of merely adding more `anywhere`
        // candidates beside them.
        breaks.retain(|position| unit_ranges.contains_boundary(*position));
        breaks.extend(
            unit_ranges
                .internal_boundaries()
                .filter(|position| pre_wrap_anywhere_break_allowed(text, policy, *position)),
        );
    }
    if matches!(policy.overflow_wrap, CssOverflowWrap::Anywhere) {
        // Unlike `line-break:anywhere`, overflow wrapping is an emergency
        // fallback and remains defined at grapheme-cluster boundaries.
        // <https://drafts.csswg.org/css-text-3/#overflow-wrap-property>
        breaks.extend(
            GraphemeClusterSegmenter::new()
                .segment_str(text)
                .filter(|position| {
                    *position > 0
                        && *position < text.len()
                        && pre_wrap_anywhere_break_allowed(text, policy, *position)
                }),
        );
    }
    move_breaks_after_bidi_format_controls(text, breaks);
    breaks.push(text.len());
    breaks.sort_unstable();
    breaks.dedup();
}

impl FontSystem {
    /// Copy ICU-derived source offsets from an exact bounded cache, or collect
    /// them once for the given complete CSS break policy.
    ///
    /// The graph still applies fragment boundaries, discretionary effects,
    /// and CSS fallback stages itself. Caching only this source-only ICU pass
    /// therefore cannot share line-edge state between distinct paragraphs.
    pub(crate) fn collect_cached_measured_break_opportunities(
        &mut self,
        text: &str,
        policy: TextBreakPolicy,
        offsets: &mut Vec<usize>,
    ) {
        if let Some(entry) = self
            .measured_break_opportunity_cache
            .iter()
            .rev()
            .find(|entry| entry.text.as_ref() == text && entry.policy == policy)
        {
            offsets.clone_from(&entry.offsets);
            return;
        }
        collect_measured_break_opportunities(text, policy, offsets);
        if !text.is_empty() {
            if self.measured_break_opportunity_cache.len()
                == MEASURED_BREAK_OPPORTUNITY_CACHE_CAPACITY
            {
                self.measured_break_opportunity_cache.remove(0);
            }
            self.measured_break_opportunity_cache
                .push(MeasuredBreakOpportunityCacheEntry {
                    text: Rc::from(text),
                    policy,
                    offsets: offsets.clone(),
                });
        }
    }
}

/// Collect ordinary UAX #14 candidates which `word-break:auto-phrase` holds
/// back until phrase wrapping cannot avoid overflow.
///
/// The returned offsets retain the original source coordinate system.  The
/// inline graph gives them [`BreakAvailability::RelaxedWordBreak`] rather
/// than deleting and later reconstructing them, so authored text, shaping,
/// and dictionary hyphenation remain source faithful.
/// <https://drafts.csswg.org/css-text-4/#word-break-auto-phrase>
pub(crate) fn collect_auto_phrase_relaxed_wrap_opportunities(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    breaks.clear();
    let Some(language) = policy.auto_phrase_language else {
        return;
    };
    let Some(phrases) = phrase_boundaries(text, language) else {
        return;
    };

    let mut ordinary_policy = policy;
    ordinary_policy.auto_phrase_language = None;
    collect_measured_break_opportunities(text, ordinary_policy, breaks);
    breaks.retain(|position| {
        let previous = text[..*position].chars().next_back();
        let next = text[*position..].chars().next();
        matches!(
            (previous, next),
            (Some(previous), Some(next))
                if auto_phrase_suppresses_break_between(previous, next)
                    && !phrases.contains_boundary(*position)
        )
    });
}

/// Collect normal UAX #14 candidates that `word-break:keep-all` suppresses.
///
/// CSS Text permits relaxing *those suppressed boundaries* when no ordinary
/// break can fit. It does not turn every grapheme boundary in an otherwise
/// unbreakable Latin word into a `keep-all` fallback. Retaining the original
/// candidate set also keeps punctuation such as a literal hyphen ordinary.
/// <https://www.w3.org/TR/css-text-3/#word-break-property>
pub(crate) fn collect_keep_all_relaxed_wrap_opportunities(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    breaks.clear();
    if !matches!(policy.word_break, CssWordBreak::KeepAll) {
        return;
    }
    let mut normal_policy = policy;
    normal_policy.word_break = CssWordBreak::Normal;
    collect_measured_break_opportunities(text, normal_policy, breaks);
    breaks.retain(|position| {
        let previous = text[..*position].chars().next_back();
        let next = text[*position..].chars().next();
        matches!(
            (previous, next),
            (Some(previous), Some(next)) if keep_all_suppresses_break_between(previous, next)
        )
    });
}

/// Keep a UAX #14 break boundary outside a following bidi-format-control run.
///
/// Bidirectional formatting characters contribute to UAX #9 resolution but do
/// not own an inline advance or a CSS Text line box. If a segmenter reports a
/// boundary immediately before one, select the equivalent source boundary
/// after the complete control run. This retains the ordinary break between the
/// adjacent visible typographic units without putting a control on its own
/// physical line:
/// <https://www.unicode.org/reports/tr14/#LB9> and
/// <https://www.w3.org/TR/css-text-3/#line-break-details>.
fn move_breaks_after_bidi_format_controls(text: &str, breaks: &mut [usize]) {
    for position in breaks {
        let Some(mut suffix) = text.get(*position..) else {
            continue;
        };
        while let Some(character) = suffix.chars().next() {
            if !character_is_bidi_format_control(character) {
                break;
            }
            *position += character.len_utf8();
            suffix = &suffix[character.len_utf8()..];
        }
    }
}

/// Return whether an emergency-style grapheme opportunity preserves a
/// `pre-wrap` document-space sequence.
///
/// CSS Text Phase II hangs a preserved run at the selected break *after* that
/// run. An `anywhere` opportunity must not reintroduce a break before or
/// within it, otherwise collection splits the run before Phase II can assign
/// its shared line-edge effect:
/// <https://www.w3.org/TR/css-text-3/#white-space-phase-2>.
fn pre_wrap_anywhere_break_allowed(text: &str, policy: TextBreakPolicy, position: usize) -> bool {
    policy.white_space != crate::css::WhiteSpace::PreWrap
        || text
            .get(position..)
            .and_then(|suffix| suffix.chars().next())
            .is_none_or(|character| !is_css_preserved_document_space(character))
}

fn pre_wrap_break_after_preserved_spaces(text: &str, position: usize) -> usize {
    if position >= text.len() || !text.is_char_boundary(position) {
        return position;
    }
    let mut end = position;
    for character in text[position..].chars() {
        if !is_css_preserved_document_space(character) || character == '\t' {
            break;
        }
        end += character.len_utf8();
    }
    end
}

/// Return CSS Text soft-wrap positions after preserved tabs in `pre-wrap`.
///
/// Preserved tabs are CSS document spaces. Under `pre-wrap`, they create soft
/// wrap opportunities just like preserved spaces while retaining their source
/// character for shaping and copy text:
/// <https://www.w3.org/TR/css-text-3/#white-space-phase-1>.
fn pre_wrap_preserved_tab_breaks(text: &str) -> impl Iterator<Item = usize> + '_ {
    text.char_indices().filter_map(|(offset, character)| {
        (character == '\t').then_some(offset + character.len_utf8())
    })
}

/// Return whether CSS Text allows a soft wrap at an atomic inline boundary.
///
/// CSS Text says atomic inline-level boxes participate in Unicode line
/// breaking as U+FFFC OBJECT REPLACEMENT CHARACTER. Spindrift uses this
/// helper for mixed inline line construction so no-break characters such as
/// U+034F COMBINING GRAPHEME JOINER, U+200D ZERO WIDTH JOINER, and U+202F
/// NARROW NO-BREAK SPACE can suppress the item boundary around an atomic box.
/// CSS Text's atomic-inline tailoring still allows U+00A0 NBSP next to an
/// atomic inline:
/// <https://www.w3.org/TR/css-text-3/#line-break-details>.
#[cfg(test)]
pub(crate) fn inline_atomic_boundary_allows_soft_wrap(
    before: &str,
    after: &str,
    style: &ComputedStyle,
) -> bool {
    if before.is_empty() || after.is_empty() {
        return false;
    }
    let boundary = before.len();
    let mut text = String::with_capacity(before.len() + after.len());
    text.push_str(before);
    text.push_str(after);
    measured_break_opportunities(&text, style)
        .binary_search(&boundary)
        .is_ok()
        || nbsp_is_next_to_atomic_inline(before, after)
}

#[cfg(test)]
fn nbsp_is_next_to_atomic_inline(before: &str, after: &str) -> bool {
    matches!(
        (before.chars().next_back(), after.chars().next()),
        (Some('\u{00a0}'), Some(OBJECT_REPLACEMENT_CHARACTER))
            | (Some(OBJECT_REPLACEMENT_CHARACTER), Some('\u{00a0}'))
    )
}

/// Collect interior grapheme-cluster boundaries into caller-owned storage.
pub(crate) fn collect_grapheme_cluster_inner_boundaries(text: &str, boundaries: &mut Vec<usize>) {
    boundaries.clear();
    boundaries.extend(
        GraphemeClusterSegmenter::new()
            .segment_str(text)
            .filter(|position| *position > 0 && *position < text.len()),
    );
}

/// Return `word-break: break-all` opportunities between typographic letters.
///
/// `break-all` adds opportunities between typographic letter units, but does
/// not turn the boundary beside a white-space separator into a letter break.
/// White-space processing supplies that boundary according to the owning
/// `white-space` mode (notably after every preserved `break-spaces` space).
/// Keeping the two sources distinct prevents an artificial boundary before a
/// preserved space from displacing the legal after-space opportunity.
/// <https://drafts.csswg.org/css-text-3/#word-break-property>
pub(crate) fn word_break_all_inner_boundaries(text: &str) -> Vec<usize> {
    let mut units = CursiveProtectedUnitRanges::new(text).into_iter();
    let Some(mut previous_range) = units.next() else {
        return Vec::new();
    };
    units
        .filter_map(|next_range| {
            let previous = &text[previous_range.clone()];
            let next = &text[next_range.clone()];
            let boundary = previous_range.end;
            previous_range = next_range;
            (previous.chars().any(character_is_unicode_alphanumeric)
                && next.chars().any(character_is_unicode_alphanumeric))
            .then_some(boundary)
        })
        .collect()
}

/// Collect and protect Spindrift-synthesized CSS Text/UAX #14 fallback candidates.
///
/// Opening punctuation must not be left at the end of a line, UAX #14 LB13
/// classes must not be left at the start of a line, CJK ideographs can break
/// between each other, and CJK ideographs can break before opening punctuation:
/// <https://www.w3.org/TR/css-text-3/#line-breaking> and
/// <https://www.unicode.org/reports/tr14/#LB13>.
fn apply_css_line_break_class_tailoring(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    let mut previous: Option<(usize, char)> = None;
    for (index, character) in text.char_indices() {
        if index == 0 {
            previous = Some((index, character));
            continue;
        }
        if let Some((_, previous)) = previous {
            let previous_class = line_break_class(previous);
            let current_class = line_break_class(character);
            if !matches!(policy.word_break, CssWordBreak::KeepAll)
                && previous_class == LineBreak::Ideographic
                && current_class == LineBreak::Ideographic
            {
                breaks.push(index);
            }
            // The bundled UAX #14 data may lack a language-specific CJK
            // segmentation model. Preserve the ordinary ideograph/word
            // opportunities in that fallback path: otherwise `中文english`
            // becomes one unbreakable min-content unit even though CSS Text
            // permits wrapping on either side of the Latin word. `keep-all`
            // deliberately suppresses these boundaries below.
            // <https://www.w3.org/TR/css-text-3/#word-break-property>
            if !matches!(policy.word_break, CssWordBreak::KeepAll)
                && ((previous_class == LineBreak::Ideographic
                    && !character_is_css_other_space_separator(previous)
                    && current_class != LineBreak::Ideographic
                    && character_is_unicode_alphanumeric(character))
                    || (current_class == LineBreak::Ideographic
                        && !character_is_css_other_space_separator(character)
                        && previous_class != LineBreak::Ideographic
                        && character_is_unicode_alphanumeric(previous)))
            {
                breaks.push(index);
            }
            if current_class == LineBreak::OpenPunctuation
                && previous_class == LineBreak::Ideographic
            {
                breaks.push(index);
            }
            // ICU's `keep-all` word option can suppress the ordinary UAX #14
            // opportunity after a hyphen together with CJK word-unit
            // opportunities. CSS Text keeps punctuation opportunities under
            // `word-break: keep-all`, including LB21's post-HY boundary.
            // Add it during common UAX tailoring so later keep-all filtering
            // still has the same candidate set as normal line layout.
            // <https://www.unicode.org/reports/tr14/#LB21>
            if previous_class == LineBreak::Hyphen {
                breaks.push(index);
            }
        }
        previous = Some((index, character));
    }

    // Apply the UAX #14 protected-boundary constraints after synthesizing all
    // ordinary CSS candidates. In particular, the ideograph/word fallback can
    // otherwise reintroduce a break before an `NS` character after ICU's
    // original candidate has been rejected.
    breaks.retain(|position| {
        let previous_allows_break = text[..*position]
            .chars()
            .next_back()
            .is_none_or(|character| {
                let class = line_break_class(character);
                class != LineBreak::OpenPunctuation
                    // `break-all` relaxes letter-to-letter breaks only. ICU's
                    // `BreakAll` mode can otherwise add a break after a PR
                    // class; CSS Text retains the UAX #14 protected prefix
                    // sequence in that case (for example `X\\\\`).
                    // <https://drafts.csswg.org/css-text-3/#valdef-word-break-break-all>
                    // <https://www.unicode.org/reports/tr14/#LB25>
                    && (!matches!(policy.word_break, CssWordBreak::BreakAll)
                        || class != LineBreak::PrefixNumeric)
            });
        let next_allows_break = text[*position..]
            .chars()
            .next()
            .is_none_or(|character| !line_break_class_suppresses_line_start(character));
        previous_allows_break && next_allows_break
    });
}

/// Preserve UAX #14's protected prefix-numeric sequences under `break-all`.
///
/// `word-break: break-all` relaxes only letter-to-letter restrictions. ICU's
/// `BreakAll` collection mode can otherwise report a candidate after a `PR`
/// class; that is an ICU mode-specific candidate rather than ordinary
/// locale-tailored line breaking, so it remains subject to LB25 protection.
/// <https://drafts.csswg.org/css-text-3/#valdef-word-break-break-all>
/// <https://www.unicode.org/reports/tr14/#LB25>
fn suppress_break_all_prefix_numeric_breaks(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    if !matches!(policy.word_break, CssWordBreak::BreakAll) {
        return;
    }
    breaks.retain(|position| {
        text[..*position]
            .chars()
            .next_back()
            .is_none_or(|character| line_break_class(character) != LineBreak::PrefixNumeric)
    });
}

/// Suppress implicit breaks inside `word-break: keep-all` text runs.
///
/// CSS Text defines `keep-all` as forbidding soft wrap opportunities between
/// CJK and non-CJK word units while keeping ordinary whitespace and punctuation
/// opportunities available:
/// <https://www.w3.org/TR/css-text-3/#word-break-property>.
fn suppress_keep_all_unit_breaks(text: &str, policy: TextBreakPolicy, breaks: &mut Vec<usize>) {
    if !matches!(policy.word_break, CssWordBreak::KeepAll) {
        return;
    }
    breaks.retain(|position| {
        let previous = text[..*position].chars().next_back();
        let next = text[*position..].chars().next();
        !matches!(
            (previous, next),
            (Some(previous), Some(next))
                if keep_all_suppresses_break_between(previous, next)
        )
    });
}

/// Return whether a normal opportunity is inside textual phrase content. CSS
/// Text typographic units include combining marks; treating only letter and
/// number scalars as word content would leak Thai breaks between a base and
/// its marks before phrase filtering can see the ICU boundary.
fn auto_phrase_suppresses_break_between(previous: char, next: char) -> bool {
    let word_content = |character| {
        character_is_unicode_alphanumeric(character) || character_is_unicode_mark(character)
    };
    word_content(previous) && word_content(next)
}

/// Apply Thai dictionary and named-entity units to automatic complex-context
/// wrapping. ICU still owns punctuation, whitespace, and explicit U+200B
/// opportunities; Kham only removes a candidate that falls within one Thai
/// lexical unit.
///
/// This tailoring is intentionally independent of `word-break:auto-phrase`.
/// A normal Thai run must not split a named entity merely because an explicit
/// `<wbr>` separates it from the following word.
/// <https://www.unicode.org/reports/tr14/#SA>
fn suppress_thai_named_entity_unit_breaks(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    if !policy.thai_named_entity_tailoring {
        return;
    }
    let Some(units) = phrase_boundaries(text, AutoPhraseLanguage::Thai) else {
        return;
    };
    breaks.retain(|position| {
        let previous = text[..*position].chars().next_back();
        let next = text[*position..].chars().next();
        !matches!(
            (previous, next),
            (Some(previous), Some(next))
                if manual_suppresses_break_between(previous, next)
                    && !units.contains_boundary(*position)
        )
    });
}

/// Suppress normal word-unit opportunities that lie within one ICU-detected
/// phrase. The ICU boundary is virtual: source text and shaped ranges remain
/// unchanged, while the graph receives only the surviving CSS opportunity.
///
/// This intentionally preserves ordinary whitespace and punctuation breaks.
/// CSS Text's phrase behavior only suppresses the otherwise-normal word-unit
/// boundaries within a detected phrase.
/// <https://drafts.csswg.org/css-text-4/#valdef-word-break-auto-phrase>
fn suppress_auto_phrase_unit_breaks(text: &str, policy: TextBreakPolicy, breaks: &mut Vec<usize>) {
    let Some(language) = policy.auto_phrase_language else {
        return;
    };
    let Some(phrases) = phrase_boundaries(text, language) else {
        return;
    };
    breaks.retain(|position| {
        let previous = text[..*position].chars().next_back();
        let next = text[*position..].chars().next();
        !matches!(
            (previous, next),
            (Some(previous), Some(next))
                if auto_phrase_suppresses_break_between(previous, next)
                    && !phrases.contains_boundary(*position)
        )
    });
}

/// Return whether `word-break: manual` suppresses an automatic break between
/// two adjacent characters.
///
/// CSS Text 4 leaves explicit author opportunities such as spaces and U+200B
/// intact, but disables automatic word-boundary detection in Southeast Asian
/// scripts. UAX #14 marks the affected sequences with the `SA` class.  This
/// predicate is shared by line collection and intrinsic sizing so both use the
/// same definition of an automatic complex-context opportunity.
/// <https://drafts.csswg.org/css-text-4/#word-boundary-detection> and
/// <https://www.unicode.org/reports/tr14/#SA>
pub(crate) fn manual_suppresses_break_between(previous: char, next: char) -> bool {
    line_break_class(previous) == LineBreak::ComplexContext
        && line_break_class(next) == LineBreak::ComplexContext
}

/// Suppress dictionary-derived breaks inside UAX #14 complex-context text for
/// `word-break: manual`.
fn suppress_manual_complex_context_breaks(
    text: &str,
    policy: TextBreakPolicy,
    breaks: &mut Vec<usize>,
) {
    if !matches!(policy.word_break, CssWordBreak::Manual) {
        return;
    }
    breaks.retain(|position| {
        let previous = text[..*position].chars().next_back();
        let next = text[*position..].chars().next();
        !matches!(
            (previous, next),
            (Some(previous), Some(next))
                if manual_suppresses_break_between(previous, next)
        )
    });
}

/// Return whether a Unicode line-break class forbids a line break before it.
///
/// UAX #14 LB13 suppresses breaks before closing punctuation and related
/// line-start-prohibited classes. CSS Text uses these UAX #14 classes as the
/// default line-breaking model before property-specific tailoring:
/// <https://www.w3.org/TR/css-text-3/#line-breaking> and
/// <https://www.unicode.org/reports/tr14/#LB13>.
fn line_break_class_suppresses_line_start(character: char) -> bool {
    matches!(
        line_break_class(character),
        LineBreak::ClosePunctuation
            | LineBreak::CloseParenthesis
            | LineBreak::Exclamation
            | LineBreak::InfixNumeric
            | LineBreak::BreakSymbols
            | LineBreak::Nonstarter
    )
}

pub(crate) fn contains_bidi_text(text: &str) -> bool {
    text.chars().any(|character| {
        character_has_rtl_bidi_class(character) || character_is_bidi_format_control(character)
    })
}

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

    #[test]
    fn loose_cjk_breaks_follow_the_content_writing_system() {
        let wave_dash_text = "東京〜大阪";
        let wave_dash_boundary = "東京".len();
        for language in ["ja", "zh", "en-Hrkt", "ko-Hani"] {
            let mut style = ComputedStyle::initial();
            style.line_break = CssLineBreak::Loose;
            style.language = ContentLanguage::from_html_attribute(language);
            assert!(
                measured_break_opportunities(wave_dash_text, &style).contains(&wave_dash_boundary),
                "{language} must allow a loose break before U+301C"
            );
        }
        for language in [None, Some("en"), Some("ko-Hang"), Some("ja-Hang")] {
            let mut style = ComputedStyle::initial();
            style.line_break = CssLineBreak::Loose;
            style.language = language
                .map(ContentLanguage::from_html_attribute)
                .unwrap_or(ContentLanguage::Unknown);
            assert!(
                !measured_break_opportunities(wave_dash_text, &style).contains(&wave_dash_boundary),
                "{language:?} must not allow a loose break before U+301C"
            );
        }

        let question_text = "ハロー、ハウアーユー?";
        let question_boundary = "ハロー、ハウアーユー".len();
        for language in ["ja", "en-Hrkt"] {
            let mut style = ComputedStyle::initial();
            style.line_break = CssLineBreak::Loose;
            style.language = ContentLanguage::from_html_attribute(language);
            assert!(
                measured_break_opportunities(question_text, &style).contains(&question_boundary),
                "{language} must allow a loose break before U+FF1F"
            );
        }
    }

    #[test]
    fn break_spaces_adds_opportunities_inside_ideographic_space_runs() {
        let mut style = ComputedStyle::initial();
        style.white_space = crate::css::WhiteSpace::BreakSpaces;
        assert_eq!(
            measured_break_opportunities(" XX  XX", &style),
            [3, 8, 11, 13]
        );

        let ogham = '\u{1680}';
        let text = format!("x{ogham}{ogham}x");
        assert_eq!(
            measured_break_opportunities(&text, &style),
            [1 + ogham.len_utf8(), 1 + 2 * ogham.len_utf8(), text.len()]
        );
    }

    #[test]
    fn break_spaces_preserves_other_space_separators_uax14_classes() {
        let mut style = ComputedStyle::initial();
        style.white_space = crate::css::WhiteSpace::BreakSpaces;

        for separator in ['\u{1680}', '\u{2000}'] {
            let text = format!("xx{separator}あ");
            let before = "xx".len();
            let after = before + separator.len_utf8();
            let breaks = measured_break_opportunities(&text, &style);
            assert!(
                !breaks.contains(&before) && breaks.contains(&after),
                "BA separator U+{:04X} must retain its UAX #14 opportunity: {breaks:?}",
                separator as u32
            );
        }

        for separator in ['\u{2007}', '\u{202f}'] {
            let text = format!("xx{separator}あ");
            let before = "xx".len();
            let after = before + separator.len_utf8();
            let breaks = measured_break_opportunities(&text, &style);
            assert!(
                !breaks.contains(&before) && !breaks.contains(&after),
                "GL separator U+{:04X} must retain LB12/LB12a protection: {breaks:?}",
                separator as u32
            );
        }

        for control in ['\u{2060}', '\u{200d}'] {
            let text = format!("x\u{3000}{control}x");
            let after_separator = "x\u{3000}".len();
            assert!(
                !measured_break_opportunities(&text, &style).contains(&after_separator),
                "U+{:04X} must protect the preceding break-spaces boundary",
                control as u32
            );
        }
    }

    #[test]
    fn narrow_no_break_space_keeps_french_punctuation_glue() {
        let style = ComputedStyle::initial();
        for text in ["moi\u{202f}?", "Mince\u{202f}!"] {
            let breaks = measured_break_opportunities(text, &style);
            let before_glue = text.find('\u{202f}').expect("test contains U+202F");
            let after_glue = before_glue + '\u{202f}'.len_utf8();
            assert!(
                !breaks.contains(&before_glue) && !breaks.contains(&after_glue),
                "ordinary UAX #14 wrapping must keep {text:?} together: {breaks:?}"
            );
        }

        let text = "mot \u{202f}?";
        assert!(
            measured_break_opportunities(text, &style).contains(&"mot ".len()),
            "LB12a retains the ordinary break after a regular space before GL"
        );
    }

    #[test]
    fn keep_all_preserves_normal_literal_hyphen_candidates() {
        let normal = ComputedStyle::initial();
        let mut keep_all = normal.clone();
        keep_all.word_break = CssWordBreak::KeepAll;
        for text in ["AB-CD-EF", "12-34-56"] {
            assert_eq!(
                measured_break_opportunities(text, &keep_all),
                measured_break_opportunities(text, &normal),
                "literal-hyphen punctuation candidates must not be lost for {text:?}"
            );
        }
    }

    #[test]
    fn keep_all_relaxes_only_its_suppressed_normal_candidates() {
        let mut keep_all = ComputedStyle::initial();
        keep_all.word_break = CssWordBreak::KeepAll;
        let policy = TextBreakPolicy::from(&keep_all);
        let mut relaxed = Vec::new();

        collect_keep_all_relaxed_wrap_opportunities("AB-CD-EF", policy, &mut relaxed);
        assert!(
            relaxed.is_empty(),
            "Latin graphemes are not keep-all fallbacks"
        );

        collect_keep_all_relaxed_wrap_opportunities("中文", policy, &mut relaxed);
        assert_eq!(relaxed, ["中".len()]);
    }

    #[test]
    fn line_break_anywhere_uses_typographic_units_for_controls() {
        let mut style = ComputedStyle::initial();
        style.line_break = CssLineBreak::Anywhere;

        for control in ['\u{2060}', '\u{feff}', '\u{034f}', '\u{200d}'] {
            let text = format!("A{control}B");
            let before_control = "A".len();
            let after_control = before_control + control.len_utf8();
            let breaks = measured_break_opportunities(&text, &style);
            assert!(
                breaks.contains(&before_control),
                "{control:?} must attach to its following typographic unit: {breaks:?}"
            );
            assert!(
                !breaks.contains(&after_control),
                "{control:?} must not become its own typographic unit: {breaks:?}"
            );
        }

        let nbsp = "A\u{00a0}B";
        let breaks = measured_break_opportunities(nbsp, &style);
        assert!(breaks.contains(&"A".len()), "{breaks:?}");
        assert!(breaks.contains(&"A\u{00a0}".len()), "{breaks:?}");
    }

    #[test]
    fn ordinary_line_breaks_keep_bidi_controls_with_the_preceding_source() {
        let style = ComputedStyle::initial();
        let text = "東\u{2066}京";
        let before_control = "東".len();
        let after_control = before_control + '\u{2066}'.len_utf8();
        let breaks = measured_break_opportunities(text, &style);

        assert!(
            !breaks.contains(&before_control),
            "a bidi control must not begin a physical line: {breaks:?}"
        );
        assert!(
            breaks.contains(&after_control),
            "the visible CJK boundary remains available after its control: {breaks:?}"
        );
    }

    #[test]
    fn ordinary_cjk_breaks_remain_available_inside_an_isolate() {
        let style = ComputedStyle::initial();
        let text = "東\u{2066}京都東京\u{2069}都";
        let breaks = measured_break_opportunities(text, &style);

        assert!(breaks.contains(&"東\u{2066}京".len()), "{breaks:?}");
        assert!(breaks.contains(&"東\u{2066}京都".len()), "{breaks:?}");
        assert!(breaks.contains(&"東\u{2066}京都東".len()), "{breaks:?}");
    }

    #[test]
    fn overflow_wrap_anywhere_retains_grapheme_boundaries_for_controls() {
        let mut style = ComputedStyle::initial();
        style.overflow_wrap = CssOverflowWrap::Anywhere;
        let text = "A\u{2060}B";

        assert!(
            measured_break_opportunities(text, &style).contains(&"A".len()),
            "overflow-wrap:anywhere must retain grapheme fallback boundaries"
        );
    }

    #[test]
    fn language_resources_preserve_selected_boundary_replacements() {
        let dutch = language_discretionary_opportunities("cafeetje", "nl");
        assert_eq!(
            dutch,
            [DiscretionaryOpportunity {
                byte_offset: "cafe".len(),
                left: Some(LanguageDiscretionaryReplacement {
                    source_bytes: "e".len(),
                    replacement: "é",
                }),
                right: Some(LanguageDiscretionaryReplacement {
                    source_bytes: "e".len(),
                    replacement: "",
                }),
            }]
        );

        let hungarian = language_discretionary_opportunities("Összeg", "hu");
        assert_eq!(
            hungarian,
            [DiscretionaryOpportunity {
                byte_offset: "Ös".len(),
                left: Some(LanguageDiscretionaryReplacement {
                    source_bytes: 0,
                    replacement: "z",
                }),
                right: None,
            }]
        );
    }

    #[test]
    fn pinyin_boundaries_delete_only_a_selected_apostrophe() {
        let opportunities = language_discretionary_opportunities("Xi’an-Xi‐an", "zh-Latn-pinyin");
        assert_eq!(opportunities.len(), 2);
        assert_eq!(opportunities[0].byte_offset, "Xi".len());
        assert_eq!(
            opportunities[0].right,
            Some(LanguageDiscretionaryReplacement {
                source_bytes: '’'.len_utf8(),
                replacement: "",
            })
        );
        assert_eq!(opportunities[1].byte_offset, "Xi’an-Xi".len());
        assert_eq!(opportunities[1].right, None);
    }

    #[test]
    fn manual_dutch_soft_hyphen_maps_the_full_spelling_change_to_its_edge() {
        assert_eq!(
            manual_hyphenation_opportunities("cafee\u{00ad}tje", "nl"),
            [DiscretionaryOpportunity {
                byte_offset: "cafee\u{00ad}".len(),
                left: Some(LanguageDiscretionaryReplacement {
                    source_bytes: "ee".len(),
                    replacement: "é",
                }),
                right: None,
            }]
        );
    }
}