swash/text/cluster/
myanmar.rs

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
//! Parser for Myanmar clusters.

use super::unicode_data::{Category, ClusterBreak, MyanmarClass};
use super::{CharCluster, Emoji, ShapeClass, Token, Whitespace, MAX_CLUSTER_SIZE};

type Kind = MyanmarClass;

pub struct MyanmarState<I> {
    chars: I,
    cur: Token,
    cur_kind: Kind,
    cur_emoji: bool,
    done: bool,
}

impl<I> MyanmarState<I>
where
    I: Iterator<Item = Token> + Clone,
{
    pub fn new(mut chars: I) -> Self {
        if let Some(first) = chars.by_ref().next() {
            let (kind, emoji) = first.info.myanmar_class();
            Self {
                chars,
                cur: first,
                cur_kind: kind,
                cur_emoji: emoji,
                done: false,
            }
        } else {
            Self {
                chars,
                cur: Token::default(),
                cur_kind: MyanmarClass::O,
                cur_emoji: false,
                done: true,
            }
        }
    }

    pub fn next(&mut self, cluster: &mut CharCluster) -> bool {
        if self.done {
            return false;
        }
        Parser::new(self, cluster).parse();
        true
    }
}

struct Parser<'a, I> {
    s: &'a mut MyanmarState<I>,
    cluster: &'a mut CharCluster,
    vt: bool,
}

impl<'a, I> Parser<'a, I>
where
    I: Iterator<Item = Token> + Clone,
{
    fn new(s: &'a mut MyanmarState<I>, cluster: &'a mut CharCluster) -> Self {
        Self {
            s,
            cluster,
            vt: false,
        }
    }

    fn parse(&mut self) -> Option<()> {
        use MyanmarClass::*;
        if self.s.done {
            return Some(());
        }
        if self.emoji() {
            self.cluster.info_mut().set_emoji(Emoji::Default);
            while self.emoji() {
                self.accept_any_as(ShapeClass::Base)?;
                if !self.parse_emoji_extension()? {
                    break;
                }
            }
            return Some(());
        }
        match self.kind() {
            O => {
                // This is not in the Myanmar spec, but added to support uniform
                // clustering of CRLF across the parsers.
                match self.s.cur.ch {
                    '\r' => {
                        self.cluster.info_mut().set_space(Whitespace::Newline);
                        self.accept_any_as(ShapeClass::Control)?;
                        if self.s.cur.ch == '\n' {
                            self.accept_any_as(ShapeClass::Control)?;
                        }
                    }
                    '\n' => {
                        self.cluster.info_mut().set_space(Whitespace::Newline);
                        self.accept_any_as(ShapeClass::Control)?;
                    }
                    _ => {
                        self.cluster.info_mut().set_space_from_char(self.s.cur.ch);
                        let class = match self.s.cur.info.category() {
                            Category::Format => match self.s.cur.ch as u32 {
                                0x200C => ShapeClass::Zwnj,
                                0x200D => ShapeClass::Zwj,
                                _ => ShapeClass::Control,
                            },
                            Category::Control => ShapeClass::Control,
                            _ => ShapeClass::Base,
                        };
                        self.accept_any_as(class)?;
                    }
                }
            }
            P | S | R | WJ | D0 => {
                self.accept_any()?;
            }
            _ => {
                match self.s.cur.ch as u32 {
                    0x1004 | 0x101B | 0x105A => {
                        let mut iter = self.s.chars.clone();
                        if let Some(b) = iter.next() {
                            if b.ch == '\u{103A}' {
                                if let Some(c) = iter.next() {
                                    if c.ch == '\u{1039}' {
                                        self.cluster.push(&self.s.cur, ShapeClass::Kinzi);
                                        self.cluster.push(&b, ShapeClass::Kinzi);
                                        self.cluster.push(&c, ShapeClass::Kinzi);
                                        self.advance();
                                        self.advance();
                                        self.advance();
                                    }
                                }
                            }
                        }
                    }
                    _ => {}
                }
                match self.kind() {
                    C | IV | D | DB => {
                        self.accept_any_as(ShapeClass::Base)?;
                        self.accept_as(VS, ShapeClass::Vs)?;
                        while self.parse_stacked_consonant_or_vowel()? {}
                        if self.vt {
                            return Some(());
                        }
                        self.accept_zero_or_many(As)?;
                        if self.accept(MY)? {
                            self.accept(As)?;
                        }
                        self.accept_as(MR, ShapeClass::MedialRa)?;
                        if self.accept(MW)? {
                            self.accept(MH)?;
                            self.accept(As)?;
                        } else if self.accept(MH)? {
                            self.accept(As)?;
                        }
                        self.accept_zero_or_many_as(VPre, ShapeClass::VPre)?;
                        self.accept_zero_or_many(VAbv)?;
                        self.accept_zero_or_many_as(VBlw, ShapeClass::VBlw)?;
                        self.accept_zero_or_many_as(A, ShapeClass::Anusvara)?;
                        if self.accept(DB)? {
                            self.accept(As)?;
                        }
                        while self.parse_post_base_vowel()? {}
                        while self.parse_pwo_tone_mark()? {}
                        self.accept_zero_or_many(V)?;
                        self.accept(J)?;
                        return Some(());
                    }
                    _ => {
                        self.cluster.info_mut().set_broken();
                        self.accept_any()?;
                        return Some(());
                    }
                }
            }
        }
        None
    }

    fn parse_stacked_consonant_or_vowel(&mut self) -> Option<bool> {
        use MyanmarClass::*;
        match self.kind() {
            H => {
                self.vt = true;
                self.accept_any_as(ShapeClass::Halant)?;
                match self.kind() {
                    C | IV => {
                        self.vt = false;
                        self.accept_any_as(ShapeClass::Base)?;
                        self.accept_as(VS, ShapeClass::Vs)?;
                        Some(true)
                    }
                    _ => Some(false),
                }
            }
            _ => Some(false),
        }
    }

    fn parse_post_base_vowel(&mut self) -> Option<bool> {
        use MyanmarClass::*;
        match self.kind() {
            VPst => {
                self.accept_any()?;
                self.accept(MH)?;
                self.accept_zero_or_many(As)?;
                self.accept_zero_or_many(VAbv)?;
                self.accept_zero_or_many_as(A, ShapeClass::Anusvara)?;
                if self.accept(DB)? {
                    self.accept(As)?;
                }
                Some(true)
            }
            _ => Some(false),
        }
    }

    fn parse_pwo_tone_mark(&mut self) -> Option<bool> {
        use MyanmarClass::*;
        match self.kind() {
            PT => {
                self.accept_any()?;
                if self.accept(As)? {
                    self.accept_as(A, ShapeClass::Anusvara)?;
                } else {
                    // This goes against the spec, but seems to be necessary to handle the actual
                    // example of a complex cluster here:
                    // https://docs.microsoft.com/en-us/typography/script-development/myanmar#well-formed-clusters
                    self.accept_zero_or_many_as(A, ShapeClass::Anusvara)?; // self.accept(A)?;
                    self.accept(DB)?;
                    self.accept(As)?;
                }
                Some(true)
            }
            _ => Some(false),
        }
    }

    fn parse_emoji_extension(&mut self) -> Option<bool> {
        use ClusterBreak::*;
        loop {
            match self.s.cur.info.cluster_break() {
                EX => match self.s.cur.ch as u32 {
                    0x200C => self.accept_any_as(ShapeClass::Zwnj)?,
                    0xFE0F => {
                        self.cluster.info_mut().set_emoji(Emoji::Color);
                        self.cluster.note_char(&self.s.cur);
                        self.advance()?;
                    }
                    0xFE0E => {
                        self.cluster.info_mut().set_emoji(Emoji::Text);
                        self.cluster.note_char(&self.s.cur);
                        self.advance()?;
                    }
                    _ => self.accept_any_as(ShapeClass::Mark)?,
                },
                ZWJ => {
                    self.accept_any_as(ShapeClass::Zwj)?;
                    return Some(true);
                }
                _ => break,
            }
        }
        Some(false)
    }

    #[inline(always)]
    fn emoji(&self) -> bool {
        self.s.cur_emoji
    }

    #[inline(always)]
    fn kind(&self) -> Kind {
        self.s.cur_kind
    }

    fn accept(&mut self, kind: Kind) -> Option<bool> {
        self.accept_as(kind, ShapeClass::Other)
    }

    fn accept_as(&mut self, kind: Kind, as_class: ShapeClass) -> Option<bool> {
        if self.s.cur_kind == kind {
            self.accept_any_as(as_class)?;
            Some(true)
        } else {
            Some(false)
        }
    }

    fn accept_zero_or_many(&mut self, kind: Kind) -> Option<bool> {
        let mut some = false;
        while self.accept(kind)? {
            some = true;
        }
        Some(some)
    }

    fn accept_zero_or_many_as(&mut self, kind: Kind, as_class: ShapeClass) -> Option<bool> {
        let mut some = false;
        while self.accept_as(kind, as_class)? {
            some = true;
        }
        Some(some)
    }

    fn accept_any(&mut self) -> Option<()> {
        self.cluster.push(&self.s.cur, ShapeClass::Other);
        self.advance()?;
        Some(())
    }

    fn accept_any_as(&mut self, as_class: ShapeClass) -> Option<()> {
        self.cluster.push(&self.s.cur, as_class);
        self.advance()?;
        Some(())
    }

    fn advance(&mut self) -> Option<()> {
        if self.cluster.len() as usize == MAX_CLUSTER_SIZE {
            return None;
        }
        if let Some(input) = self.s.chars.next() {
            let (kind, emoji) = input.info.myanmar_class();
            self.s.cur = input;
            self.s.cur_emoji = emoji;
            self.s.cur_kind = kind;
            if input.ch == '\u{34f}' {
                self.accept_any()?;
            }
            Some(())
        } else {
            self.s.done = true;
            None
        }
    }
}