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
use super::*;

/// Stateful CSS `text-transform` word-boundary context for one inline formatting context.
///
/// CSS Text Level 3 defines `capitalize` word boundaries across inline box
/// boundaries, and requires out-of-flow boxes to be ignored while determining
/// those boundaries:
/// <https://www.w3.org/TR/css-text-3/#text-transform-property>.
#[derive(Debug, Clone)]
pub(in crate::layout) struct TextTransformState {
    pub(in crate::layout) new_word: bool,
}

impl Default for TextTransformState {
    fn default() -> Self {
        Self { new_word: true }
    }
}

/// Applies CSS `text-transform` while updating word-boundary state.
///
/// CSS Text Level 3 allows UAs to choose word-boundary detection for
/// `capitalize`, but inline box boundaries and out-of-flow boxes must not
/// introduce boundaries. Callers that lay out a sequence of inline fragments
/// should share one state across in-flow text fragments:
/// <https://www.w3.org/TR/css-text-3/#text-transform-property>.
pub(in crate::layout) fn transform_text_with_state(
    text: &str,
    style: &ComputedStyle,
    state: &mut TextTransformState,
) -> String {
    transform_text_inner(text, style, Some(state))
}

/// Apply CSS's permitted small-caps synthesis when the selected face has no
/// matching OpenType caps feature. Compatibility ligatures and U+00DF are
/// expanded even when the face has the feature: those source glyphs commonly
/// lack a caps alternate and must receive tracking between their expansion.
/// <https://drafts.csswg.org/css-fonts-4/#font-variant-caps-prop>
pub(in crate::layout) fn synthesize_missing_font_caps_text(
    font_system: &mut FontSystem,
    text: &str,
    style: &ComputedStyle,
) -> String {
    if !style.font_synthesis.small_caps
        || !matches!(
            style.font_variant_caps,
            crate::css::FontVariantCaps::SmallCaps
                | crate::css::FontVariantCaps::AllSmallCaps
                | crate::css::FontVariantCaps::PetiteCaps
                | crate::css::FontVariantCaps::AllPetiteCaps
        )
    {
        return text.to_owned();
    }
    let synthesize_all = !font_system.selected_font_supports_caps_feature(style);
    let has_unsupported_compatibility_form = text
        .chars()
        .any(|character| character == '\u{00df}' || matches!(character, '\u{fb00}'..='\u{fb06}'));
    if !synthesize_all && !has_unsupported_compatibility_form {
        return text.to_owned();
    }
    let language = case_mapping_language(style.language.as_deref());
    let mapper = CaseMapper::new();
    text.chars()
        .map(|character| {
            if synthesize_all
                || character == '\u{00df}'
                || matches!(character, '\u{fb00}'..='\u{fb06}')
            {
                mapper
                    .uppercase_to_string(&character.to_string(), &language)
                    .into_owned()
            } else {
                character.to_string()
            }
        })
        .collect()
}

/// Applies CSS `text-transform` for independent text contexts.
///
/// CSS Text Level 3 defines the case-transform values for generated visual
/// text. This convenience wrapper starts a fresh word-boundary context, which
/// is appropriate for isolated block text and intrinsic-size estimates:
/// <https://www.w3.org/TR/css-text-3/#text-transform-property>.
pub(in crate::layout) fn transform_text(text: &str, style: &ComputedStyle) -> String {
    transform_text_inner(text, style, None)
}

pub(in crate::layout) fn transform_text_inner(
    text: &str,
    style: &ComputedStyle,
    state: Option<&mut TextTransformState>,
) -> String {
    let mut fallback_state = TextTransformState::default();
    let state = state.unwrap_or(&mut fallback_state);
    let mut text = match style.text_transform.case() {
        None => map_text_transform_characters(text, state, |character, _| character.to_string()),
        Some(TextTransformCase::Uppercase) => {
            uppercase_text(text, style.language.as_deref(), state)
        }
        Some(TextTransformCase::Lowercase) => {
            lowercase_text(text, style.language.as_deref(), state)
        }
        Some(TextTransformCase::Capitalize) => {
            capitalize_text(text, style.language.as_deref(), state)
        }
    };
    if style.text_transform.applies_full_width() {
        text = full_width_text(&text);
    }
    if style.text_transform.applies_full_size_kana() {
        text = full_size_kana_text(&text);
    }
    if style.text_transform.applies_math_auto() {
        text = math_auto_text(&text);
    }
    text
}

/// Apply MathML Core's `math-auto` mathematical-italic mappings.
///
/// These are Unicode character substitutions, not an OpenType feature: the
/// transformed scalar is shaped, measured, painted, and exported through the
/// ordinary text pipeline. The exceptional Latin `h` and the Greek symbol
/// variants occupy compatibility code points, while the primary Latin and
/// Greek alphabets use contiguous Mathematical Alphanumeric Symbol ranges:
/// <https://w3c.github.io/mathml-core/#math-auto-transform> and
/// <https://w3c.github.io/mathml-core/#italic-mappings>.
pub(in crate::layout) fn math_auto_text(text: &str) -> String {
    let mut characters = text.chars();
    let Some(character) = characters.next() else {
        return String::new();
    };
    if characters.next().is_some() {
        return text.to_owned();
    }
    math_auto_character(character).to_string()
}

fn math_auto_character(character: char) -> char {
    let scalar = character as u32;
    let mapped = match character {
        'A'..='Z' => Some(0x1D434 + scalar - 'A' as u32),
        'a'..='g' | 'i'..='z' => Some(0x1D44E + scalar - 'a' as u32),
        'h' => Some(0x210E),
        '\u{0131}' => Some(0x1D6A4),
        '\u{0237}' => Some(0x1D6A5),
        '\u{0391}'..='\u{03A1}' => Some(0x1D6E2 + scalar - 0x0391),
        '\u{03F4}' => Some(0x1D6F3),
        '\u{03A3}'..='\u{03A9}' => Some(0x1D6F4 + scalar - 0x03A3),
        '\u{2207}' => Some(0x1D6FB),
        '\u{03B1}'..='\u{03C9}' => Some(0x1D6FC + scalar - 0x03B1),
        '\u{2202}' => Some(0x1D715),
        '\u{03F5}' => Some(0x1D716),
        '\u{03D1}' => Some(0x1D717),
        '\u{03F0}' => Some(0x1D718),
        '\u{03D5}' => Some(0x1D719),
        '\u{03F1}' => Some(0x1D71A),
        '\u{03D6}' => Some(0x1D71B),
        _ => None,
    };
    mapped.and_then(char::from_u32).unwrap_or(character)
}

/// Map text through ICU's full uppercase mapping.
///
/// CSS Text defines `text-transform: uppercase` in terms of the Unicode
/// Default Case Conversion algorithm, with language-sensitive tailorings from
/// the element language:
/// <https://www.w3.org/TR/css-text-3/#valdef-text-transform-uppercase> and
/// <https://www.unicode.org/versions/latest/ch03.pdf#G33992>.
pub(in crate::layout) fn uppercase_text(
    text: &str,
    language: Option<&str>,
    state: &mut TextTransformState,
) -> String {
    let language = case_mapping_language(language);
    let mapped = CaseMapper::new().uppercase_to_string(text, &language);
    update_text_transform_state_for_output(state, text);
    mapped.into_owned()
}

/// Map text through ICU's full lowercase mapping.
///
/// CSS Text defines `text-transform: lowercase` in terms of the Unicode
/// Default Case Conversion algorithm, with language-sensitive tailorings from
/// the element language:
/// <https://www.w3.org/TR/css-text-3/#valdef-text-transform-lowercase> and
/// <https://www.unicode.org/versions/latest/ch03.pdf#G33992>.
pub(in crate::layout) fn lowercase_text(
    text: &str,
    language: Option<&str>,
    state: &mut TextTransformState,
) -> String {
    let language = case_mapping_language(language);
    let mapped = CaseMapper::new().lowercase_to_string(text, &language);
    update_text_transform_state_for_output(state, text);
    mapped.into_owned()
}

pub(in crate::layout) fn map_text_transform_characters(
    text: &str,
    state: &mut TextTransformState,
    mut map: impl FnMut(char, bool) -> String,
) -> String {
    let mut output = String::new();
    for character in text.chars() {
        let new_word = state.new_word;
        output.push_str(&map(character, new_word));
        state.update(character);
    }
    output
}

pub(in crate::layout) fn capitalize_text(
    text: &str,
    language: Option<&str>,
    state: &mut TextTransformState,
) -> String {
    let mut output = String::new();
    let language = case_mapping_language(language);
    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() {
            push_capitalized_word_segment(&mut output, segment, &language, state);
            state.mark_after_word();
        } else {
            output.push_str(segment);
            state.update_non_word_segment(segment);
        }
        start = end;
    }
    if start < text.len() {
        let segment = &text[start..];
        output.push_str(segment);
        state.update_non_word_segment(segment);
    }
    output
}

pub(in crate::layout) fn push_capitalized_word_segment(
    output: &mut String,
    segment: &str,
    language: &LanguageIdentifier,
    state: &mut TextTransformState,
) {
    for (offset, character) in segment.char_indices() {
        if character_is_unicode_alphanumeric(character) {
            if state.new_word {
                if character_is_unicode_typographic_letter(character) {
                    output.push_str(&titlecase_word_tail(&segment[offset..], language));
                    update_text_transform_state_for_output(state, &segment[offset..]);
                    break;
                } else {
                    state.update(character);
                    output.push(character);
                }
            } else {
                output.push(character);
                state.update(character);
            }
        } else {
            output.push(character);
        }
    }
}

/// Titlecase one CSS word tail while preserving non-initial casing.
///
/// CSS `capitalize` titlecases the first typographic letter unit of each word
/// and leaves the remaining characters unchanged. ICU's titlecase mapping is
/// used to identify the full language-tailored leading titlecase unit, and the
/// untouched source tail is spliced back afterward so CSS trailing case is
/// preserved:
/// <https://www.w3.org/TR/css-text-3/#valdef-text-transform-capitalize> and
/// <https://www.unicode.org/reports/tr21/tr21-5.html#Caseless_Matching>.
pub(in crate::layout) fn titlecase_word_tail(
    segment: &str,
    language: &LanguageIdentifier,
) -> String {
    let options = TitlecaseOptions::default();
    let title_lower = TitlecaseMapper::new()
        .titlecase_segment_to_string(segment, language, options)
        .into_owned();
    let lower_source = CaseMapper::new()
        .lowercase_to_string(segment, language)
        .into_owned();
    if title_lower == lower_source {
        return segment.to_string();
    }
    for source_boundary in segment
        .char_indices()
        .map(|(offset, character)| offset + character.len_utf8())
    {
        let lower_tail = CaseMapper::new()
            .lowercase_to_string(&segment[source_boundary..], language)
            .into_owned();
        for title_boundary in title_lower
            .char_indices()
            .map(|(offset, character)| offset + character.len_utf8())
        {
            if title_lower[title_boundary..] == lower_tail {
                let mut output = String::with_capacity(title_lower.len() + segment.len());
                output.push_str(&title_lower[..title_boundary]);
                output.push_str(&segment[source_boundary..]);
                return output;
            }
        }
    }
    title_lower
}

pub(in crate::layout) fn case_mapping_language(language: Option<&str>) -> LanguageIdentifier {
    let Some(language) = language else {
        return root_language_identifier();
    };
    let language = language.replace('_', "-");
    let identifier = language
        .parse::<LanguageIdentifier>()
        .unwrap_or_else(|_| root_language_identifier());
    if language_uses_turkic_case_mapping(&language) && language_declares_non_latin_script(&language)
    {
        // CSS Text's writing-system rules give an explicit script subtag
        // precedence over a contradictory language default. Turkish and Azeri
        // dotted-I casing applies to their Latin writing systems, not to text
        // explicitly tagged as Cyrillic, Arabic, and so on.
        // <https://www.w3.org/TR/css-text-3/#script-tagging>
        return root_language_identifier();
    }
    identifier
}

fn language_uses_turkic_case_mapping(language: &str) -> bool {
    matches!(
        language.split('-').next(),
        Some(primary) if primary.eq_ignore_ascii_case("tr") || primary.eq_ignore_ascii_case("az")
    )
}

fn language_declares_non_latin_script(language: &str) -> bool {
    language.split('-').skip(1).any(|subtag| {
        subtag.len() == 4
            && subtag
                .chars()
                .all(|character| character.is_ascii_alphabetic())
            && !subtag.eq_ignore_ascii_case("latn")
    })
}

pub(in crate::layout) fn root_language_identifier() -> LanguageIdentifier {
    "und"
        .parse()
        .expect("the Unicode root language identifier is valid")
}

pub(in crate::layout) fn update_text_transform_state_for_output(
    state: &mut TextTransformState,
    text: &str,
) {
    for character in text.chars() {
        state.update(character);
    }
}

/// Map text for `text-transform: full-width`.
///
/// CSS Text defines `full-width` as converting characters to their fullwidth
/// forms, notably ASCII and halfwidth Katakana compatibility characters:
/// <https://www.w3.org/TR/css-text-3/#valdef-text-transform-full-width>.
pub(in crate::layout) fn full_width_text(text: &str) -> String {
    let mut output = String::new();
    let mut characters = text.chars().peekable();
    while let Some(character) = characters.next() {
        if let Some(next) = characters.peek().cloned() {
            if next == '\u{ff9e}'
                && let Some(composed) = full_width_voiced_kana(character)
            {
                output.push_str(composed);
                characters.next();
                continue;
            }
            if next == '\u{ff9f}'
                && let Some(composed) = full_width_semi_voiced_kana(character)
            {
                output.push_str(composed);
                characters.next();
                continue;
            }
        }
        if let Some(mapped) = full_width_compatibility_character(character) {
            output.push(mapped);
            continue;
        }
        let mapped = full_width_character(character);
        if mapped.is_empty() {
            output.push(character);
        } else {
            output.push_str(mapped);
        }
    }
    output
}

/// Return the single-scalar compatibility mappings used by CSS `full-width`
/// that do not belong to ASCII or halfwidth Katakana.
///
/// The halfwidth Hangul ranges have sparse source code-point ranges but map to
/// consecutive Hangul Compatibility Jamo ranges. The remaining mappings are
/// the Unicode `<wide>` compatibility mappings selected by CSS Text.
/// <https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt> and
/// <https://www.w3.org/TR/css-text-3/#valdef-text-transform-full-width>
pub(in crate::layout) fn full_width_compatibility_character(character: char) -> Option<char> {
    let scalar = character as u32;
    let mapped = match scalar {
        0x2985 => 0xff5f,
        0x2986 => 0xff60,
        0xffa0 => 0x3164,
        0xffa1..=0xffbe => 0x3131 + scalar - 0xffa1,
        0xffc2..=0xffc6 => 0x314f + scalar - 0xffc2,
        0xffc7 => 0x3154,
        0xffca..=0xffcf => 0x3155 + scalar - 0xffca,
        0xffd2..=0xffd7 => 0x315b + scalar - 0xffd2,
        0xffda..=0xffdc => 0x3161 + scalar - 0xffda,
        0x00a2 => 0xffe0,
        0x00a3 => 0xffe1,
        0x00ac => 0xffe2,
        0x00af => 0xffe3,
        0x00a6 => 0xffe4,
        0x00a5 => 0xffe5,
        0x20a9 => 0xffe6,
        0xffe8 => 0x2502,
        0xffe9 => 0x2190,
        0xffea => 0x2191,
        0xffeb => 0x2192,
        0xffec => 0x2193,
        0xffed => 0x25a0,
        0xffee => 0x25cb,
        _ => return None,
    };
    char::from_u32(mapped)
}

pub(in crate::layout) fn full_width_character(character: char) -> &'static str {
    match character {
        ' ' => "\u{3000}",
        '!' => "!",
        '"' => """,
        '#' => "#",
        '$' => "$",
        '%' => "%",
        '&' => "&",
        '\'' => "'",
        '(' => "(",
        ')' => ")",
        '*' => "*",
        '+' => "+",
        ',' => ",",
        '-' => "-",
        '.' => ".",
        '/' => "/",
        '0' => "0",
        '1' => "1",
        '2' => "2",
        '3' => "3",
        '4' => "4",
        '5' => "5",
        '6' => "6",
        '7' => "7",
        '8' => "8",
        '9' => "9",
        ':' => ":",
        ';' => ";",
        '<' => "<",
        '=' => "=",
        '>' => ">",
        '?' => "?",
        '@' => "@",
        'A' => "A",
        'B' => "B",
        'C' => "C",
        'D' => "D",
        'E' => "E",
        'F' => "F",
        'G' => "G",
        'H' => "H",
        'I' => "I",
        'J' => "J",
        'K' => "K",
        'L' => "L",
        'M' => "M",
        'N' => "N",
        'O' => "O",
        'P' => "P",
        'Q' => "Q",
        'R' => "R",
        'S' => "S",
        'T' => "T",
        'U' => "U",
        'V' => "V",
        'W' => "W",
        'X' => "X",
        'Y' => "Y",
        'Z' => "Z",
        '[' => "[",
        '\\' => "\",
        ']' => "]",
        '^' => "^",
        '_' => "_",
        '`' => "`",
        'a' => "a",
        'b' => "b",
        'c' => "c",
        'd' => "d",
        'e' => "e",
        'f' => "f",
        'g' => "g",
        'h' => "h",
        'i' => "i",
        'j' => "j",
        'k' => "k",
        'l' => "l",
        'm' => "m",
        'n' => "n",
        'o' => "o",
        'p' => "p",
        'q' => "q",
        'r' => "r",
        's' => "s",
        't' => "t",
        'u' => "u",
        'v' => "v",
        'w' => "w",
        'x' => "x",
        'y' => "y",
        'z' => "z",
        '{' => "{",
        '|' => "|",
        '}' => "}",
        '~' => "~",
        '\u{ff61}' => "\u{3002}",
        '\u{ff62}' => "\u{300c}",
        '\u{ff63}' => "\u{300d}",
        '\u{ff64}' => "\u{3001}",
        '\u{ff65}' => "\u{30fb}",
        '\u{ff66}' => "\u{30f2}",
        '\u{ff67}' => "\u{30a1}",
        '\u{ff68}' => "\u{30a3}",
        '\u{ff69}' => "\u{30a5}",
        '\u{ff6a}' => "\u{30a7}",
        '\u{ff6b}' => "\u{30a9}",
        '\u{ff6c}' => "\u{30e3}",
        '\u{ff6d}' => "\u{30e5}",
        '\u{ff6e}' => "\u{30e7}",
        '\u{ff6f}' => "\u{30c3}",
        '\u{ff70}' => "\u{30fc}",
        '\u{ff71}' => "\u{30a2}",
        '\u{ff72}' => "\u{30a4}",
        '\u{ff73}' => "\u{30a6}",
        '\u{ff74}' => "\u{30a8}",
        '\u{ff75}' => "\u{30aa}",
        '\u{ff76}' => "\u{30ab}",
        '\u{ff77}' => "\u{30ad}",
        '\u{ff78}' => "\u{30af}",
        '\u{ff79}' => "\u{30b1}",
        '\u{ff7a}' => "\u{30b3}",
        '\u{ff7b}' => "\u{30b5}",
        '\u{ff7c}' => "\u{30b7}",
        '\u{ff7d}' => "\u{30b9}",
        '\u{ff7e}' => "\u{30bb}",
        '\u{ff7f}' => "\u{30bd}",
        '\u{ff80}' => "\u{30bf}",
        '\u{ff81}' => "\u{30c1}",
        '\u{ff82}' => "\u{30c4}",
        '\u{ff83}' => "\u{30c6}",
        '\u{ff84}' => "\u{30c8}",
        '\u{ff85}' => "\u{30ca}",
        '\u{ff86}' => "\u{30cb}",
        '\u{ff87}' => "\u{30cc}",
        '\u{ff88}' => "\u{30cd}",
        '\u{ff89}' => "\u{30ce}",
        '\u{ff8a}' => "\u{30cf}",
        '\u{ff8b}' => "\u{30d2}",
        '\u{ff8c}' => "\u{30d5}",
        '\u{ff8d}' => "\u{30d8}",
        '\u{ff8e}' => "\u{30db}",
        '\u{ff8f}' => "\u{30de}",
        '\u{ff90}' => "\u{30df}",
        '\u{ff91}' => "\u{30e0}",
        '\u{ff92}' => "\u{30e1}",
        '\u{ff93}' => "\u{30e2}",
        '\u{ff94}' => "\u{30e4}",
        '\u{ff95}' => "\u{30e6}",
        '\u{ff96}' => "\u{30e8}",
        '\u{ff97}' => "\u{30e9}",
        '\u{ff98}' => "\u{30ea}",
        '\u{ff99}' => "\u{30eb}",
        '\u{ff9a}' => "\u{30ec}",
        '\u{ff9b}' => "\u{30ed}",
        '\u{ff9c}' => "\u{30ef}",
        '\u{ff9d}' => "\u{30f3}",
        '\u{ff9e}' => "\u{3099}",
        '\u{ff9f}' => "\u{309a}",
        _ => "",
    }
}

pub(in crate::layout) fn full_width_voiced_kana(character: char) -> Option<&'static str> {
    match character {
        '\u{ff73}' => Some("\u{30f4}"),
        '\u{ff76}' => Some("\u{30ac}"),
        '\u{ff77}' => Some("\u{30ae}"),
        '\u{ff78}' => Some("\u{30b0}"),
        '\u{ff79}' => Some("\u{30b2}"),
        '\u{ff7a}' => Some("\u{30b4}"),
        '\u{ff7b}' => Some("\u{30b6}"),
        '\u{ff7c}' => Some("\u{30b8}"),
        '\u{ff7d}' => Some("\u{30ba}"),
        '\u{ff7e}' => Some("\u{30bc}"),
        '\u{ff7f}' => Some("\u{30be}"),
        '\u{ff80}' => Some("\u{30c0}"),
        '\u{ff81}' => Some("\u{30c2}"),
        '\u{ff82}' => Some("\u{30c5}"),
        '\u{ff83}' => Some("\u{30c7}"),
        '\u{ff84}' => Some("\u{30c9}"),
        '\u{ff8a}' => Some("\u{30d0}"),
        '\u{ff8b}' => Some("\u{30d3}"),
        '\u{ff8c}' => Some("\u{30d6}"),
        '\u{ff8d}' => Some("\u{30d9}"),
        '\u{ff8e}' => Some("\u{30dc}"),
        _ => None,
    }
}

pub(in crate::layout) fn full_width_semi_voiced_kana(character: char) -> Option<&'static str> {
    match character {
        '\u{ff8a}' => Some("\u{30d1}"),
        '\u{ff8b}' => Some("\u{30d4}"),
        '\u{ff8c}' => Some("\u{30d7}"),
        '\u{ff8d}' => Some("\u{30da}"),
        '\u{ff8e}' => Some("\u{30dd}"),
        _ => None,
    }
}

/// Map text for `text-transform: full-size-kana`.
///
/// CSS Text defines `full-size-kana` as converting small Kana to their
/// ordinary-sized equivalents for ruby and emphasis readability:
/// <https://www.w3.org/TR/css-text-3/#valdef-text-transform-full-size-kana>.
pub(in crate::layout) fn full_size_kana_text(text: &str) -> String {
    let mut output = String::new();
    for character in text.chars() {
        let mapped = full_size_kana_character(character);
        if mapped.is_empty() {
            output.push(character);
        } else {
            output.push_str(mapped);
        }
    }
    output
}

pub(in crate::layout) fn full_size_kana_character(character: char) -> &'static str {
    match character {
        // CSS Text Appendix G's normative small Kana mapping table, synchronized
        // through Unicode 15.0. This is not Unicode normalization or a
        // fullwidth conversion: the halfwidth mappings deliberately retain
        // halfwidth output.
        // <https://drafts.csswg.org/css-text-3/#small-kana-mappings>
        '\u{1b132}' => "こ",
        '\u{1b150}' => "ゐ",
        '\u{1b151}' => "ゑ",
        '\u{1b152}' => "を",
        '\u{1b155}' => "コ",
        '\u{1b164}' => "ヰ",
        '\u{1b165}' => "ヱ",
        '\u{1b166}' => "ヲ",
        '\u{1b167}' => "ン",
        'ぁ' => "あ",
        'ぃ' => "い",
        'ぅ' => "う",
        'ぇ' => "え",
        'ぉ' => "お",
        'ゕ' => "か",
        'ゖ' => "け",
        'っ' => "つ",
        'ゃ' => "や",
        'ゅ' => "ゆ",
        'ょ' => "よ",
        'ゎ' => "わ",
        'ァ' => "ア",
        'ィ' => "イ",
        'ゥ' => "ウ",
        'ェ' => "エ",
        'ォ' => "オ",
        'ヵ' => "カ",
        'ヶ' => "ケ",
        'ッ' => "ツ",
        'ャ' => "ヤ",
        'ュ' => "ユ",
        'ョ' => "ヨ",
        'ヮ' => "ワ",
        'ㇰ' => "ク",
        'ㇱ' => "シ",
        'ㇲ' => "ス",
        'ㇳ' => "ト",
        'ㇴ' => "ヌ",
        'ㇵ' => "ハ",
        'ㇶ' => "ヒ",
        'ㇷ' => "フ",
        'ㇸ' => "ヘ",
        'ㇹ' => "ホ",
        'ㇺ' => "ム",
        'ㇻ' => "ラ",
        'ㇼ' => "リ",
        'ㇽ' => "ル",
        'ㇾ' => "レ",
        'ㇿ' => "ロ",
        'ァ' => "ア",
        'ィ' => "イ",
        'ゥ' => "ウ",
        'ェ' => "エ",
        'ォ' => "オ",
        'ッ' => "ツ",
        'ャ' => "ヤ",
        'ュ' => "ユ",
        'ョ' => "ヨ",
        _ => "",
    }
}

impl TextTransformState {
    pub(in crate::layout) fn force_word_boundary(&mut self) {
        self.new_word = true;
    }

    pub(in crate::layout) fn update(&mut self, character: char) {
        self.new_word = !character_is_unicode_alphanumeric(character);
    }

    pub(in crate::layout) fn mark_after_word(&mut self) {
        self.new_word = false;
    }

    pub(in crate::layout) fn update_non_word_segment(&mut self, segment: &str) {
        if self.new_word {
            return;
        }
        if segment
            .chars()
            .all(character_preserves_word_boundary_context)
        {
            return;
        }
        self.new_word = true;
    }
}

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

    #[test]
    fn math_auto_uses_mathml_italic_unicode_mappings_for_single_character_text() {
        assert_eq!(math_auto_text("∂"), "\u{1d715}");
        assert_eq!(math_auto_text("∂∇"), "∂∇");
    }

    #[test]
    fn explicit_non_latin_script_overrides_turkish_case_tailoring() {
        let mut state = TextTransformState::default();
        assert_eq!(lowercase_text("I", Some("tr-Cyrl"), &mut state), "i");

        let mut latin_state = TextTransformState::default();
        assert_eq!(lowercase_text("I", Some("tr-Latn"), &mut latin_state), "ı");
    }

    #[test]
    fn full_size_kana_maps_every_css_text_appendix_g_pair() {
        let mappings = [
            ('ぁ', "あ"),
            ('ぃ', "い"),
            ('ぅ', "う"),
            ('ぇ', "え"),
            ('ぉ', "お"),
            ('ゕ', "か"),
            ('ゖ', "け"),
            ('\u{1b132}', "こ"),
            ('っ', "つ"),
            ('ゃ', "や"),
            ('ゅ', "ゆ"),
            ('ょ', "よ"),
            ('ゎ', "わ"),
            ('\u{1b150}', "ゐ"),
            ('\u{1b151}', "ゑ"),
            ('\u{1b152}', "を"),
            ('ァ', "ア"),
            ('ィ', "イ"),
            ('ゥ', "ウ"),
            ('ェ', "エ"),
            ('ォ', "オ"),
            ('ヵ', "カ"),
            ('ㇰ', "ク"),
            ('ヶ', "ケ"),
            ('\u{1b155}', "コ"),
            ('ㇱ', "シ"),
            ('ㇲ', "ス"),
            ('ッ', "ツ"),
            ('ㇳ', "ト"),
            ('ㇴ', "ヌ"),
            ('ㇵ', "ハ"),
            ('ㇶ', "ヒ"),
            ('ㇷ', "フ"),
            ('ㇸ', "ヘ"),
            ('ㇹ', "ホ"),
            ('ㇺ', "ム"),
            ('ャ', "ヤ"),
            ('ュ', "ユ"),
            ('ョ', "ヨ"),
            ('ㇻ', "ラ"),
            ('ㇼ', "リ"),
            ('ㇽ', "ル"),
            ('ㇾ', "レ"),
            ('ㇿ', "ロ"),
            ('ヮ', "ワ"),
            ('\u{1b164}', "ヰ"),
            ('\u{1b165}', "ヱ"),
            ('\u{1b166}', "ヲ"),
            ('\u{1b167}', "ン"),
            ('ァ', "ア"),
            ('ィ', "イ"),
            ('ゥ', "ウ"),
            ('ェ', "エ"),
            ('ォ', "オ"),
            ('ッ', "ツ"),
            ('ャ', "ヤ"),
            ('ュ', "ユ"),
            ('ョ', "ヨ"),
        ];

        for (small, full_size) in mappings {
            assert_eq!(
                full_size_kana_character(small),
                full_size,
                "U+{:04X}",
                small as u32
            );
            assert_eq!(
                full_size_kana_text(&small.to_string()),
                full_size,
                "U+{:04X}",
                small as u32
            );
        }
    }

    #[test]
    fn full_width_maps_hangul_and_symbol_compatibility_characters() {
        assert_eq!(
            full_width_text(
                "\u{2985}\u{2986}\u{ffa0}\u{ffa1}\u{ffbe}\u{ffc2}\u{ffdc}\u{a2}\u{20a9}\u{ffee}"
            ),
            "⦅⦆ㅤㄱㅎㅏㅣ¢₩○"
        );
    }

    #[test]
    fn capitalize_titlecases_letter_number_characters() {
        let mut state = TextTransformState::default();
        assert_eq!(capitalize_text("ⅰⅰⅰ", Some("en"), &mut state), "Ⅰⅰⅰ");
    }
}