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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::{props::EnumeratedProperty, provider::BidiMirroringGlyphV1Marker};
use icu_collections::codepointtrie::TrieValue;
use zerovec::ule::{AsULE, RawBytesULE};

/// This is a bitpacked combination of the `Bidi_Mirroring_Glyph`,
/// `Bidi_Mirrored`, and `Bidi_Paired_Bracket_Type` properties.
#[derive(Debug, Eq, PartialEq, Clone, Copy, Default)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_properties::bidi))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[non_exhaustive]
pub struct BidiMirroringGlyph {
    /// The mirroring glyph
    pub mirroring_glyph: Option<char>,
    /// Whether the glyph is mirrored
    pub mirrored: bool,
    /// The paired bracket type
    pub paired_bracket_type: BidiPairedBracketType,
}

impl EnumeratedProperty for BidiMirroringGlyph {
    type DataMarker = BidiMirroringGlyphV1Marker;
    #[cfg(feature = "compiled_data")]
    const SINGLETON: &'static crate::provider::PropertyCodePointMapV1<'static, Self> =
        crate::provider::Baked::SINGLETON_BIDI_MIRRORING_GLYPH_V1_MARKER;
    const NAME: &'static [u8] = b"Bidi_Mirroring_Glyph";
    const SHORT_NAME: &'static [u8] = b"Bidi_Mirroring_Glyph";
}

impl crate::private::Sealed for BidiMirroringGlyph {}

impl AsULE for BidiMirroringGlyph {
    type ULE = zerovec::ule::RawBytesULE<3>;

    fn to_unaligned(self) -> Self::ULE {
        let [a, b, c, _] = TrieValue::to_u32(self).to_le_bytes();
        RawBytesULE([a, b, c])
    }
    fn from_unaligned(unaligned: Self::ULE) -> Self {
        let [a, b, c] = unaligned.0;
        TrieValue::try_from_u32(u32::from_le_bytes([a, b, c, 0])).unwrap_or_default()
    }
}

/// The enum represents Bidi_Paired_Bracket_Type.
///
/// It does not implement [`EnumeratedProperty`], instead it can be obtained
/// through the bitpacked [`BidiMirroringGlyph`] property.
///
/// If you have a use case this property without also needing the [`BidiMirroringGlyph`]
/// property, and need to optimize data size, please file an issue.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_properties::bidi))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[non_exhaustive]
pub enum BidiPairedBracketType {
    /// Represents Bidi_Paired_Bracket_Type=Open.
    Open,
    /// Represents Bidi_Paired_Bracket_Type=Close.
    Close,
    /// Represents Bidi_Paired_Bracket_Type=None.
    #[default]
    None,
}

/// Implements [`unicode_bidi::BidiDataSource`] on [`CodePointMapDataBorrowed<BidiClass>`](crate::CodePointMapDataBorrowed).
///
/// ✨ *Enabled with the `unicode_bidi` Cargo feature.*
///
/// # Examples
///
///```
/// use icu::properties::CodePointMapData;
/// use icu::properties::props::BidiClass;
/// use unicode_bidi::BidiInfo;
///
/// // This example text is defined using `concat!` because some browsers
/// // and text editors have trouble displaying bidi strings.
/// let text =  concat!["א", // RTL#1
///                     "ב", // RTL#2
///                     "ג", // RTL#3
///                     "a", // LTR#1
///                     "b", // LTR#2
///                     "c", // LTR#3
///                     ]; //
///
///
/// let bidi_map = CodePointMapData::<BidiClass>::new();
///
/// // Resolve embedding levels within the text.  Pass `None` to detect the
/// // paragraph level automatically.
/// let bidi_info = BidiInfo::new_with_data_source(&bidi_map, text, None);
///
/// // This paragraph has embedding level 1 because its first strong character is RTL.
/// assert_eq!(bidi_info.paragraphs.len(), 1);
/// let para = &bidi_info.paragraphs[0];
/// assert_eq!(para.level.number(), 1);
/// assert!(para.level.is_rtl());
///
/// // Re-ordering is done after wrapping each paragraph into a sequence of
/// // lines. For this example, I'll just use a single line that spans the
/// // entire paragraph.
/// let line = para.range.clone();
///
/// let display = bidi_info.reorder_line(para, line);
/// assert_eq!(display, concat!["a", // LTR#1
///                             "b", // LTR#2
///                             "c", // LTR#3
///                             "ג", // RTL#3
///                             "ב", // RTL#2
///                             "א", // RTL#1
///                             ]);
/// ```
#[cfg(feature = "unicode_bidi")]
impl unicode_bidi::data_source::BidiDataSource
    for crate::CodePointMapDataBorrowed<'_, crate::props::BidiClass>
{
    fn bidi_class(&self, c: char) -> unicode_bidi::BidiClass {
        use crate::props::BidiClass;
        match self.get(c) {
            BidiClass::LeftToRight => unicode_bidi::BidiClass::L,
            BidiClass::RightToLeft => unicode_bidi::BidiClass::R,
            BidiClass::EuropeanNumber => unicode_bidi::BidiClass::EN,
            BidiClass::EuropeanSeparator => unicode_bidi::BidiClass::ES,
            BidiClass::EuropeanTerminator => unicode_bidi::BidiClass::ET,
            BidiClass::ArabicNumber => unicode_bidi::BidiClass::AN,
            BidiClass::CommonSeparator => unicode_bidi::BidiClass::CS,
            BidiClass::ParagraphSeparator => unicode_bidi::BidiClass::B,
            BidiClass::SegmentSeparator => unicode_bidi::BidiClass::S,
            BidiClass::WhiteSpace => unicode_bidi::BidiClass::WS,
            BidiClass::OtherNeutral => unicode_bidi::BidiClass::ON,
            BidiClass::LeftToRightEmbedding => unicode_bidi::BidiClass::LRE,
            BidiClass::LeftToRightOverride => unicode_bidi::BidiClass::LRO,
            BidiClass::ArabicLetter => unicode_bidi::BidiClass::AL,
            BidiClass::RightToLeftEmbedding => unicode_bidi::BidiClass::RLE,
            BidiClass::RightToLeftOverride => unicode_bidi::BidiClass::RLO,
            BidiClass::PopDirectionalFormat => unicode_bidi::BidiClass::PDF,
            BidiClass::NonspacingMark => unicode_bidi::BidiClass::NSM,
            BidiClass::BoundaryNeutral => unicode_bidi::BidiClass::BN,
            BidiClass::FirstStrongIsolate => unicode_bidi::BidiClass::FSI,
            BidiClass::LeftToRightIsolate => unicode_bidi::BidiClass::LRI,
            BidiClass::RightToLeftIsolate => unicode_bidi::BidiClass::RLI,
            BidiClass::PopDirectionalIsolate => unicode_bidi::BidiClass::PDI,
            // This must not happen.
            _ => unicode_bidi::BidiClass::ON,
        }
    }
}