Skip to main content

harfrust/ot/shaper/
khmer_machine.rs

1#![allow(
2    dead_code,
3    non_upper_case_globals,
4    unused_assignments,
5    unused_parens,
6    while_true,
7    clippy::assign_op_pattern,
8    clippy::collapsible_if,
9    clippy::comparison_chain,
10    clippy::double_parens,
11    clippy::unnecessary_cast,
12    clippy::single_match,
13    clippy::never_loop
14)]
15
16use crate::buffer::{Buffer, HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE};
17
18static _khmer_syllable_machine_actions: [i8; 29] = [
19    0, 1, 0, 1, 1, 1, 2, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 2, 2, 3, 2, 2, 4, 0, 0,
20];
21static _khmer_syllable_machine_key_offsets: [i16; 45] = [
22    0, 5, 8, 11, 15, 18, 21, 25, 28, 32, 35, 40, 45, 48, 51, 55, 58, 61, 65, 68, 72, 75, 90, 100,
23    103, 113, 122, 123, 129, 134, 141, 149, 158, 168, 171, 181, 190, 191, 197, 202, 209, 217, 226,
24    0, 0,
25];
26static _khmer_syllable_machine_trans_keys: [u8; 232] = [
27    20, 25, 26, 5, 6, 26, 5, 6, 15, 1, 2, 20, 26, 5, 6, 26, 5, 6, 26, 5, 6, 20, 26, 5, 6, 26, 5, 6,
28    20, 26, 5, 6, 26, 5, 6, 20, 25, 26, 5, 6, 20, 25, 26, 5, 6, 26, 5, 6, 15, 1, 2, 20, 26, 5, 6,
29    26, 5, 6, 26, 5, 6, 20, 26, 5, 6, 26, 5, 6, 20, 26, 5, 6, 26, 5, 6, 4, 15, 20, 21, 22, 23, 25,
30    26, 27, 1, 2, 5, 6, 10, 11, 4, 20, 21, 22, 23, 25, 26, 27, 5, 6, 15, 1, 2, 4, 20, 21, 22, 23,
31    25, 26, 27, 5, 6, 4, 20, 21, 22, 23, 26, 27, 5, 6, 27, 4, 23, 26, 27, 5, 6, 4, 26, 27, 5, 6, 4,
32    20, 23, 26, 27, 5, 6, 4, 20, 21, 23, 26, 27, 5, 6, 4, 20, 21, 22, 23, 26, 27, 5, 6, 4, 20, 21,
33    22, 23, 25, 26, 27, 5, 6, 15, 1, 2, 4, 20, 21, 22, 23, 25, 26, 27, 5, 6, 4, 20, 21, 22, 23, 26,
34    27, 5, 6, 27, 4, 23, 26, 27, 5, 6, 4, 26, 27, 5, 6, 4, 20, 23, 26, 27, 5, 6, 4, 20, 21, 23, 26,
35    27, 5, 6, 4, 20, 21, 22, 23, 26, 27, 5, 6, 20, 26, 5, 6, 0, 0,
36];
37static _khmer_syllable_machine_single_lengths: [i8; 45] = [
38    3, 1, 1, 2, 1, 1, 2, 1, 2, 1, 3, 3, 1, 1, 2, 1, 1, 2, 1, 2, 1, 9, 8, 1, 8, 7, 1, 4, 3, 5, 6, 7,
39    8, 1, 8, 7, 1, 4, 3, 5, 6, 7, 2, 0, 0,
40];
41static _khmer_syllable_machine_range_lengths: [i8; 45] = [
42    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
43    1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0,
44];
45static _khmer_syllable_machine_index_offsets: [i16; 45] = [
46    0, 5, 8, 11, 15, 18, 21, 25, 28, 32, 35, 40, 45, 48, 51, 55, 58, 61, 65, 68, 72, 75, 88, 98,
47    101, 111, 120, 122, 128, 133, 140, 148, 157, 167, 170, 180, 189, 191, 197, 202, 209, 217, 226,
48    0, 0,
49];
50static _khmer_syllable_machine_cond_targs: [i8; 275] = [
51    27, 31, 25, 1, 21, 25, 1, 21, 26, 26, 21, 27, 25, 1, 21, 27, 4, 21, 28, 5, 21, 27, 29, 7, 21,
52    29, 7, 21, 27, 30, 9, 21, 30, 9, 21, 27, 32, 25, 1, 21, 37, 41, 35, 12, 21, 35, 12, 21, 36, 36,
53    21, 37, 35, 12, 21, 37, 15, 21, 38, 16, 21, 37, 39, 18, 21, 39, 18, 21, 37, 40, 20, 21, 40, 20,
54    21, 33, 22, 37, 39, 40, 38, 41, 35, 36, 22, 42, 32, 21, 23, 27, 29, 30, 28, 32, 25, 26, 10, 21,
55    24, 24, 21, 23, 27, 29, 30, 28, 31, 25, 26, 0, 21, 2, 27, 29, 30, 28, 25, 26, 3, 21, 26, 21, 2,
56    28, 27, 26, 4, 21, 2, 28, 26, 5, 21, 2, 27, 28, 29, 26, 6, 21, 2, 27, 29, 28, 30, 26, 8, 21,
57    23, 27, 29, 30, 28, 25, 26, 3, 21, 23, 27, 29, 30, 28, 31, 25, 26, 3, 21, 34, 34, 21, 33, 37,
58    39, 40, 38, 41, 35, 36, 11, 21, 13, 37, 39, 40, 38, 35, 36, 14, 21, 36, 21, 13, 38, 37, 36, 15,
59    21, 13, 38, 36, 16, 21, 13, 37, 38, 39, 36, 17, 21, 13, 37, 39, 38, 40, 36, 19, 21, 33, 37, 39,
60    40, 38, 35, 36, 14, 21, 37, 35, 12, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
61    21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
62    21, 21, 21, 21, 21, 0, 0,
63];
64static _khmer_syllable_machine_cond_actions: [i8; 275] = [
65    5, 5, 5, 0, 15, 5, 0, 15, 0, 0, 15, 5, 5, 0, 15, 5, 0, 15, 5, 0, 15, 5, 5, 0, 15, 5, 0, 15, 5,
66    5, 0, 15, 5, 0, 15, 5, 5, 5, 0, 15, 5, 21, 21, 0, 17, 21, 0, 19, 0, 0, 17, 5, 21, 0, 17, 5, 0,
67    17, 5, 0, 17, 5, 5, 0, 17, 5, 0, 17, 5, 5, 0, 17, 5, 0, 17, 0, 5, 5, 5, 5, 5, 21, 21, 0, 5, 24,
68    5, 7, 0, 5, 5, 5, 5, 5, 5, 0, 0, 9, 5, 5, 9, 0, 5, 5, 5, 5, 5, 5, 0, 0, 9, 0, 5, 5, 5, 5, 5, 0,
69    0, 9, 0, 9, 0, 5, 5, 0, 0, 9, 0, 5, 0, 0, 9, 0, 5, 5, 5, 0, 0, 9, 0, 5, 5, 5, 5, 0, 0, 9, 0, 5,
70    5, 5, 5, 5, 0, 0, 9, 0, 5, 5, 5, 5, 5, 5, 0, 0, 9, 21, 21, 11, 0, 5, 5, 5, 5, 21, 21, 0, 0, 11,
71    0, 5, 5, 5, 5, 21, 0, 0, 11, 0, 11, 0, 5, 5, 0, 0, 11, 0, 5, 0, 0, 11, 0, 5, 5, 5, 0, 0, 11, 0,
72    5, 5, 5, 5, 0, 0, 11, 0, 5, 5, 5, 5, 21, 0, 0, 11, 5, 21, 0, 13, 15, 15, 15, 15, 15, 15, 15,
73    15, 15, 15, 15, 17, 19, 17, 17, 17, 17, 17, 17, 17, 17, 0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11,
74    11, 11, 11, 11, 11, 11, 11, 11, 13, 0, 0,
75];
76static _khmer_syllable_machine_to_state_actions: [i8; 45] = [
77    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79];
80static _khmer_syllable_machine_from_state_actions: [i8; 45] = [
81    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83];
84static _khmer_syllable_machine_eof_trans: [i16; 45] = [
85    231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
86    250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268,
87    269, 270, 271, 272, 273, 0, 0,
88];
89static khmer_syllable_machine_start: i32 = 21;
90static khmer_syllable_machine_first_final: i32 = 21;
91static khmer_syllable_machine_error: i32 = -1;
92static khmer_syllable_machine_en_main: i32 = 21;
93
94// IMPORTANT: Before updating any values here, make sure to read the comment in `category`.
95
96#[derive(Clone, Copy)]
97pub enum SyllableType {
98    ConsonantSyllable = 0,
99    BrokenCluster,
100    NonKhmerCluster,
101}
102
103pub fn find_syllables_khmer(buffer: &mut Buffer) -> Option<()> {
104    let mut cs = 0;
105    let mut ts = 0;
106    let mut te = 0;
107    let mut act = 0;
108    let mut p = 0;
109    let pe = buffer.len;
110    let eof = buffer.len;
111    let mut syllable_serial = 1u8;
112
113    macro_rules! found_syllable {
114        ($kind:expr) => {{
115            found_syllable(ts, te, &mut syllable_serial, $kind, buffer);
116        }};
117    }
118
119    {
120        cs = (khmer_syllable_machine_start) as i32;
121        ts = 0;
122        te = 0;
123        act = 0;
124    }
125
126    {
127        let mut _klen = 0;
128        let mut _trans = 0;
129        let mut _keys: i32 = 0;
130        let mut _acts: i32 = 0;
131        let mut _nacts = 0;
132        let mut __have = 0;
133        '_resume: while (p != pe || p == eof) {
134            '_again: while (true) {
135                _acts = (_khmer_syllable_machine_from_state_actions[(cs) as usize]) as i32;
136                _nacts = (_khmer_syllable_machine_actions[(_acts) as usize]) as u32;
137                _acts += 1;
138                while (_nacts > 0) {
139                    match (_khmer_syllable_machine_actions[(_acts) as usize]) {
140                        1 => {
141                            ts = p;
142                        }
143
144                        _ => {}
145                    }
146                    _nacts -= 1;
147                    _acts += 1;
148                }
149                if (p == eof) {
150                    {
151                        if (_khmer_syllable_machine_eof_trans[(cs) as usize] > 0) {
152                            {
153                                _trans =
154                                    (_khmer_syllable_machine_eof_trans[(cs) as usize]) as u32 - 1;
155                            }
156                        }
157                    }
158                } else {
159                    {
160                        _keys = (_khmer_syllable_machine_key_offsets[(cs) as usize]) as i32;
161                        _trans = (_khmer_syllable_machine_index_offsets[(cs) as usize]) as u32;
162                        _klen = (_khmer_syllable_machine_single_lengths[(cs) as usize]) as i32;
163                        __have = 0;
164                        if (_klen > 0) {
165                            {
166                                let mut _lower: i32 = _keys;
167                                let mut _upper: i32 = _keys + _klen - 1;
168                                let mut _mid: i32 = 0;
169                                while (true) {
170                                    if (_upper < _lower) {
171                                        {
172                                            _keys += _klen;
173                                            _trans += (_klen) as u32;
174                                            break;
175                                        }
176                                    }
177                                    _mid = _lower + ((_upper - _lower) >> 1);
178                                    if ((buffer.info.get(p)?.khmer_category() as u8)
179                                        < _khmer_syllable_machine_trans_keys[(_mid) as usize])
180                                    {
181                                        _upper = _mid - 1;
182                                    } else if ((buffer.info.get(p)?.khmer_category() as u8)
183                                        > _khmer_syllable_machine_trans_keys[(_mid) as usize])
184                                    {
185                                        _lower = _mid + 1;
186                                    } else {
187                                        {
188                                            __have = 1;
189                                            _trans += (_mid - _keys) as u32;
190                                            break;
191                                        }
192                                    }
193                                }
194                            }
195                        }
196                        _klen = (_khmer_syllable_machine_range_lengths[(cs) as usize]) as i32;
197                        if (__have == 0 && _klen > 0) {
198                            {
199                                let mut _lower: i32 = _keys;
200                                let mut _upper: i32 = _keys + (_klen << 1) - 2;
201                                let mut _mid: i32 = 0;
202                                while (true) {
203                                    if (_upper < _lower) {
204                                        {
205                                            _trans += (_klen) as u32;
206                                            break;
207                                        }
208                                    }
209                                    _mid = _lower + (((_upper - _lower) >> 1) & !1);
210                                    if ((buffer.info.get(p)?.khmer_category() as u8)
211                                        < _khmer_syllable_machine_trans_keys[(_mid) as usize])
212                                    {
213                                        _upper = _mid - 2;
214                                    } else if ((buffer.info.get(p)?.khmer_category() as u8)
215                                        > _khmer_syllable_machine_trans_keys[(_mid + 1) as usize])
216                                    {
217                                        _lower = _mid + 2;
218                                    } else {
219                                        {
220                                            _trans += ((_mid - _keys) >> 1) as u32;
221                                            break;
222                                        }
223                                    }
224                                }
225                            }
226                        }
227                    }
228                }
229                cs = (_khmer_syllable_machine_cond_targs[(_trans) as usize]) as i32;
230                if (_khmer_syllable_machine_cond_actions[(_trans) as usize] != 0) {
231                    {
232                        _acts = (_khmer_syllable_machine_cond_actions[(_trans) as usize]) as i32;
233                        _nacts = (_khmer_syllable_machine_actions[(_acts) as usize]) as u32;
234                        _acts += 1;
235                        while (_nacts > 0) {
236                            match (_khmer_syllable_machine_actions[(_acts) as usize]) {
237                                2 => {
238                                    te = p + 1;
239                                }
240                                3 => {
241                                    act = 2;
242                                }
243                                4 => {
244                                    act = 3;
245                                }
246                                5 => {
247                                    te = p + 1;
248                                    {
249                                        found_syllable!(SyllableType::NonKhmerCluster);
250                                    }
251                                }
252                                6 => {
253                                    te = p;
254                                    p = p - 1;
255                                    {
256                                        found_syllable!(SyllableType::ConsonantSyllable);
257                                    }
258                                }
259                                7 => {
260                                    te = p;
261                                    p = p - 1;
262                                    {
263                                        found_syllable!(SyllableType::BrokenCluster);
264                                        buffer.scratch_flags |=
265                                            HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE;
266                                    }
267                                }
268                                8 => {
269                                    te = p;
270                                    p = p - 1;
271                                    {
272                                        found_syllable!(SyllableType::NonKhmerCluster);
273                                    }
274                                }
275                                9 => {
276                                    p = (te) - 1;
277                                    {
278                                        found_syllable!(SyllableType::ConsonantSyllable);
279                                    }
280                                }
281                                10 => {
282                                    p = (te) - 1;
283                                    {
284                                        found_syllable!(SyllableType::BrokenCluster);
285                                        buffer.scratch_flags |=
286                                            HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE;
287                                    }
288                                }
289                                11 => match (act) {
290                                    2 => {
291                                        p = (te) - 1;
292                                        {
293                                            found_syllable!(SyllableType::BrokenCluster);
294                                            buffer.scratch_flags |=
295                                                HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE;
296                                        }
297                                    }
298                                    3 => {
299                                        p = (te) - 1;
300                                        {
301                                            found_syllable!(SyllableType::NonKhmerCluster);
302                                        }
303                                    }
304
305                                    _ => {}
306                                },
307
308                                _ => {}
309                            }
310                            _nacts -= 1;
311                            _acts += 1;
312                        }
313                    }
314                }
315                break '_again;
316            }
317            if (p == eof) {
318                {
319                    if (cs >= 21) {
320                        break '_resume;
321                    }
322                }
323            } else {
324                {
325                    _acts = (_khmer_syllable_machine_to_state_actions[(cs) as usize]) as i32;
326                    _nacts = (_khmer_syllable_machine_actions[(_acts) as usize]) as u32;
327                    _acts += 1;
328                    while (_nacts > 0) {
329                        match (_khmer_syllable_machine_actions[(_acts) as usize]) {
330                            0 => {
331                                ts = 0;
332                            }
333
334                            _ => {}
335                        }
336                        _nacts -= 1;
337                        _acts += 1;
338                    }
339                    p += 1;
340                    continue '_resume;
341                }
342            }
343            break '_resume;
344        }
345    }
346    Some(())
347}
348
349// Called from every syllable action, so one shared copy keeps the machine small.
350#[inline(never)]
351fn found_syllable(
352    start: usize,
353    end: usize,
354    syllable_serial: &mut u8,
355    kind: SyllableType,
356    buffer: &mut Buffer,
357) {
358    for info in buffer.info.get_mut(start..end).unwrap_or_default() {
359        info.set_syllable((*syllable_serial << 4) | kind as u8);
360    }
361
362    *syllable_serial += 1;
363
364    if *syllable_serial == 16 {
365        *syllable_serial = 1;
366    }
367}