1use super::*;
2use crate::normalize::NormalizationMode;
3use crate::ot::layout::*;
4use crate::plan::ShapePlan;
5use crate::unicode::GeneralCategory;
6use crate::Script;
7use crate::ShaperFont;
8
9pub const THAI_SHAPER: OtShaper = OtShaper {
10 collect_features: None,
11 override_features: None,
12 create_data: None,
13 preprocess_text: Some(preprocess_text),
14 postprocess_glyphs: None,
15 normalization_preference: NormalizationMode::Auto,
16 decompose: None,
17 compose: None,
18 setup_masks: None,
19 gpos_tag: None,
20 reorder_marks: None,
21 zero_width_marks: ZeroWidthMarks::ByGdefLate,
22 fallback_position: false,
23};
24
25#[derive(Clone, Copy, PartialEq)]
26enum Consonant {
27 NC = 0,
28 AC,
29 RC,
30 DC,
31 NotConsonant,
32}
33
34fn get_consonant_type(u: u32) -> Consonant {
35 match u {
36 0x0E1B | 0x0E1D | 0x0E1F => Consonant::AC,
37 0x0E0D | 0x0E10 => Consonant::RC,
38 0x0E0E | 0x0E0F => Consonant::DC,
39 0x0E01..=0x0E2E => Consonant::NC,
40 _ => Consonant::NotConsonant,
41 }
42}
43
44#[derive(Clone, Copy, PartialEq)]
45enum Mark {
46 AV,
47 BV,
48 T,
49 NotMark,
50}
51
52fn get_mark_type(u: u32) -> Mark {
53 match u {
54 0x0E31 | 0x0E34..=0x0E37 | 0x0E47 | 0x0E4D..=0x0E4E => Mark::AV,
55 0x0E38..=0x0E3A => Mark::BV,
56 0x0E48..=0x0E4C => Mark::T,
57 _ => Mark::NotMark,
58 }
59}
60
61#[derive(Clone, Copy, PartialEq)]
62enum Action {
63 NOP,
64 SD,
66 SL,
68 SDL,
70 RD,
72}
73
74#[derive(Clone, Copy)]
75struct PuaMapping {
76 u: u16,
77 win_pua: u16,
78 mac_pua: u16,
79}
80
81impl PuaMapping {
82 const fn new(u: u16, win_pua: u16, mac_pua: u16) -> Self {
83 PuaMapping {
84 u,
85 win_pua,
86 mac_pua,
87 }
88 }
89}
90
91static SD_MAPPINGS: &[PuaMapping] = &[
92 PuaMapping::new(0x0E48, 0xF70A, 0xF88B), PuaMapping::new(0x0E49, 0xF70B, 0xF88E), PuaMapping::new(0x0E4A, 0xF70C, 0xF891), PuaMapping::new(0x0E4B, 0xF70D, 0xF894), PuaMapping::new(0x0E4C, 0xF70E, 0xF897), PuaMapping::new(0x0E38, 0xF718, 0xF89B), PuaMapping::new(0x0E39, 0xF719, 0xF89C), PuaMapping::new(0x0E3A, 0xF71A, 0xF89D), PuaMapping::new(0x0000, 0x0000, 0x0000),
101];
102
103static SDL_MAPPINGS: &[PuaMapping] = &[
104 PuaMapping::new(0x0E48, 0xF705, 0xF88C), PuaMapping::new(0x0E49, 0xF706, 0xF88F), PuaMapping::new(0x0E4A, 0xF707, 0xF892), PuaMapping::new(0x0E4B, 0xF708, 0xF895), PuaMapping::new(0x0E4C, 0xF709, 0xF898), PuaMapping::new(0x0000, 0x0000, 0x0000),
110];
111
112static SL_MAPPINGS: &[PuaMapping] = &[
113 PuaMapping::new(0x0E48, 0xF713, 0xF88A), PuaMapping::new(0x0E49, 0xF714, 0xF88D), PuaMapping::new(0x0E4A, 0xF715, 0xF890), PuaMapping::new(0x0E4B, 0xF716, 0xF893), PuaMapping::new(0x0E4C, 0xF717, 0xF896), PuaMapping::new(0x0E31, 0xF710, 0xF884), PuaMapping::new(0x0E34, 0xF701, 0xF885), PuaMapping::new(0x0E35, 0xF702, 0xF886), PuaMapping::new(0x0E36, 0xF703, 0xF887), PuaMapping::new(0x0E37, 0xF704, 0xF888), PuaMapping::new(0x0E47, 0xF712, 0xF889), PuaMapping::new(0x0E4D, 0xF711, 0xF899), PuaMapping::new(0x0000, 0x0000, 0x0000),
126];
127
128static RD_MAPPINGS: &[PuaMapping] = &[
129 PuaMapping::new(0x0E0D, 0xF70F, 0xF89A), PuaMapping::new(0x0E10, 0xF700, 0xF89E), PuaMapping::new(0x0000, 0x0000, 0x0000),
132];
133
134fn pua_shape(u: u32, action: Action, font: &ShaperFont<'_, '_>) -> u32 {
135 let mappings = match action {
136 Action::NOP => return u,
137 Action::SD => SD_MAPPINGS,
138 Action::SL => SL_MAPPINGS,
139 Action::SDL => SDL_MAPPINGS,
140 Action::RD => RD_MAPPINGS,
141 };
142
143 for m in mappings {
144 if m.u as u32 == u {
145 if font.nominal_glyph(m.win_pua as u32).is_some() {
146 return m.win_pua as u32;
147 }
148
149 if font.nominal_glyph(m.mac_pua as u32).is_some() {
150 return m.mac_pua as u32;
151 }
152
153 break;
154 }
155 }
156
157 u
158}
159
160#[derive(Clone, Copy)]
161enum AboveState {
162 T0, T1, T2, T3, }
168
169static ABOVE_START_STATE: &[AboveState] = &[
170 AboveState::T0, AboveState::T1, AboveState::T0, AboveState::T0, AboveState::T3, ];
176
177#[derive(Clone, Copy)]
178struct AboveStateMachineEdge {
179 action: Action,
180 next_state: AboveState,
181}
182
183impl AboveStateMachineEdge {
184 const fn new(action: Action, next_state: AboveState) -> Self {
185 AboveStateMachineEdge { action, next_state }
186 }
187}
188
189type ASME = AboveStateMachineEdge;
190
191static ABOVE_STATE_MACHINE: &[[ASME; 3]] = &[
192 [
195 ASME::new(Action::NOP, AboveState::T3),
196 ASME::new(Action::NOP, AboveState::T0),
197 ASME::new(Action::SD, AboveState::T3),
198 ],
199 [
201 ASME::new(Action::SL, AboveState::T2),
202 ASME::new(Action::NOP, AboveState::T1),
203 ASME::new(Action::SDL, AboveState::T2),
204 ],
205 [
207 ASME::new(Action::NOP, AboveState::T3),
208 ASME::new(Action::NOP, AboveState::T2),
209 ASME::new(Action::SL, AboveState::T3),
210 ],
211 [
213 ASME::new(Action::NOP, AboveState::T3),
214 ASME::new(Action::NOP, AboveState::T3),
215 ASME::new(Action::NOP, AboveState::T3),
216 ],
217];
218
219#[derive(Clone, Copy)]
220enum BelowState {
221 B0,
223 B1,
225 B2,
227}
228
229static BELOW_START_STATE: &[BelowState] = &[
230 BelowState::B0, BelowState::B0, BelowState::B1, BelowState::B2, BelowState::B2, ];
236
237#[derive(Clone, Copy)]
238struct BelowStateMachineEdge {
239 action: Action,
240 next_state: BelowState,
241}
242
243impl BelowStateMachineEdge {
244 const fn new(action: Action, next_state: BelowState) -> Self {
245 BelowStateMachineEdge { action, next_state }
246 }
247}
248
249type BSME = BelowStateMachineEdge;
250
251static BELOW_STATE_MACHINE: &[[BSME; 3]] = &[
252 [
255 BSME::new(Action::NOP, BelowState::B0),
256 BSME::new(Action::NOP, BelowState::B2),
257 BSME::new(Action::NOP, BelowState::B0),
258 ],
259 [
261 BSME::new(Action::NOP, BelowState::B1),
262 BSME::new(Action::RD, BelowState::B2),
263 BSME::new(Action::NOP, BelowState::B1),
264 ],
265 [
267 BSME::new(Action::NOP, BelowState::B2),
268 BSME::new(Action::SD, BelowState::B2),
269 BSME::new(Action::NOP, BelowState::B2),
270 ],
271];
272
273fn do_pua_shaping(font: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<()> {
274 let mut above_state = ABOVE_START_STATE[Consonant::NotConsonant as usize];
275 let mut below_state = BELOW_START_STATE[Consonant::NotConsonant as usize];
276 let mut base = 0;
277
278 for i in 0..buffer.len {
279 let glyph = buffer.info.get(i)?.glyph_id;
280 let mt = get_mark_type(glyph);
281
282 if mt == Mark::NotMark {
283 let ct = get_consonant_type(glyph);
284 above_state = ABOVE_START_STATE[ct as usize];
285 below_state = BELOW_START_STATE[ct as usize];
286 base = i;
287 continue;
288 }
289
290 let above_edge = ABOVE_STATE_MACHINE[above_state as usize][mt as usize];
291 let below_edge = BELOW_STATE_MACHINE[below_state as usize][mt as usize];
292 above_state = above_edge.next_state;
293 below_state = below_edge.next_state;
294
295 let action = if above_edge.action != Action::NOP {
297 above_edge.action
298 } else {
299 below_edge.action
300 };
301
302 buffer.unsafe_to_break(Some(base), Some(i));
303 let target = if action == Action::RD { base } else { i };
304 let info = buffer.info.get_mut(target)?;
305 info.glyph_id = pua_shape(info.glyph_id, action, font);
306 }
307 Some(())
308}
309
310fn move_nikhahit(buffer: &mut Buffer, end: usize) -> Option<usize> {
313 #[inline]
314 fn is_above_base_mark(u: u32) -> bool {
315 let u = u & !0x0080;
316 matches!(u, 0x0E34..=0x0E37 | 0x0E47..=0x0E4E | 0x0E31..=0x0E31 | 0x0E3B..=0x0E3B)
317 }
318
319 let nikhahit = end.checked_sub(2)?;
320 buffer
322 .out_info_mut()
323 .get_mut(nikhahit)?
324 .set_general_category(GeneralCategory::NON_SPACING_MARK);
325
326 let mut start = nikhahit;
327 while start > 0 && is_above_base_mark(buffer.out_info().get(start - 1)?.glyph_id) {
328 start -= 1;
329 }
330 if start < nikhahit {
331 buffer.merge_out_clusters(start, end);
332 buffer
333 .out_info_mut()
334 .get_mut(start..=nikhahit)?
335 .rotate_right(1);
336 }
337 Some(start)
338}
339
340fn preprocess_text(plan: &ShapePlan, font: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<()> {
342 #[inline]
388 fn is_sara_am(u: u32) -> bool {
389 (u & !0x0080) == 0x0E33
390 }
391 #[inline]
392 fn nikhahit_from_sara_am(u: u32) -> u32 {
393 u - 0x0E33 + 0x0E4D
394 }
395 #[inline]
396 fn sara_aa_from_sara_am(u: u32) -> u32 {
397 u - 1
398 }
399 buffer.clear_output();
400 buffer.idx = 0;
401 while buffer.idx < buffer.len {
402 let u = buffer.cur(0)?.glyph_id;
403 if !is_sara_am(u) {
404 buffer.next_glyph();
405 continue;
406 }
407
408 buffer.output_glyph(nikhahit_from_sara_am(u));
410 if let Some(&info) = buffer.out_info().get(buffer.out_len - 1) {
411 let mut info = info;
412 info.set_continuation(&mut buffer.scratch_flags);
413 }
414 buffer.replace_glyph(sara_aa_from_sara_am(u));
415
416 let end = buffer.out_len;
417 let start = move_nikhahit(buffer, end).unwrap_or(end - 2);
418
419 if start != 0 {
422 buffer.merge_out_grapheme_clusters(start - 1, end);
423 }
424 }
425
426 buffer.sync();
427
428 if plan.script == Some(Script::THAI) && !plan.ot_map.found_script(LayoutTableKind::Gsub) {
430 if do_pua_shaping(font, buffer).is_none() {
431 buffer.index_failed();
432 }
433 }
434 Some(())
435}