Skip to main content

harfrust/ot/shaper/
myanmar.rs

1use super::indic::{category, position};
2use super::syllabic::*;
3use super::*;
4use crate::ot::map::*;
5use crate::ot::shaper::indic::category::OT_VPre;
6use crate::plan::ShapePlan;
7use crate::ShaperFont;
8use crate::{GlyphInfo, Tag};
9
10pub const MYANMAR_SHAPER: OtShaper = OtShaper {
11    collect_features: Some(collect_features),
12    override_features: None,
13    create_data: None,
14    preprocess_text: None,
15    postprocess_glyphs: None,
16    normalization_preference: NormalizationMode::ComposedDiacriticsNoShortCircuit,
17    decompose: None,
18    compose: None,
19    setup_masks: Some(setup_masks),
20    gpos_tag: None,
21    reorder_marks: None,
22    zero_width_marks: ZeroWidthMarks::ByGdefEarly,
23    fallback_position: false,
24};
25
26impl GlyphInfo {
27    declare_buffer_var_alias!(
28        OT_SHAPER_VAR_U8_CATEGORY_VAR,
29        u8,
30        MYANMAR_CATEGORY_VAR,
31        myanmar_category,
32        set_myanmar_category
33    );
34    declare_buffer_var_alias!(
35        OT_SHAPER_VAR_U8_AUXILIARY_VAR,
36        u8,
37        MYANMAR_POSITION_VAR,
38        myanmar_position,
39        set_myanmar_position
40    );
41}
42
43// Ugly Zawgyi encoding.
44// Disable all auto processing.
45// https://github.com/harfbuzz/harfbuzz/issues/1162
46pub const MYANMAR_ZAWGYI_SHAPER: OtShaper = OtShaper {
47    collect_features: None,
48    override_features: None,
49    create_data: None,
50    preprocess_text: None,
51    postprocess_glyphs: None,
52    normalization_preference: NormalizationMode::None,
53    decompose: None,
54    compose: None,
55    setup_masks: None,
56    gpos_tag: None,
57    reorder_marks: None,
58    zero_width_marks: ZeroWidthMarks::None,
59    fallback_position: false,
60};
61
62static MYANMAR_FEATURES: &[Tag] = &[
63    // Basic features.
64    // These features are applied in order, one at a time, after reordering,
65    // constrained to the syllable.
66    Tag::new(b"rphf"),
67    Tag::new(b"pref"),
68    Tag::new(b"blwf"),
69    Tag::new(b"pstf"),
70    // Other features.
71    // These features are applied all at once after clearing syllables.
72    Tag::new(b"pres"),
73    Tag::new(b"abvs"),
74    Tag::new(b"blws"),
75    Tag::new(b"psts"),
76];
77
78impl GlyphInfo {
79    fn set_myanmar_properties(&mut self) {
80        let u = self.glyph_id;
81        let (cat, _) = indic_table::get_categories(u);
82
83        self.set_myanmar_category(cat);
84    }
85}
86
87fn collect_features(planner: &mut ShapePlanner) {
88    // Do this before any lookups have been applied.
89    planner.ot_map.add_gsub_pause(Some(setup_syllables));
90
91    planner
92        .ot_map
93        .enable_feature(Tag::new(b"locl"), F_PER_SYLLABLE, 1);
94    // The Indic specs do not require ccmp, but we apply it here since if
95    // there is a use of it, it's typically at the beginning.
96    planner
97        .ot_map
98        .enable_feature(Tag::new(b"ccmp"), F_PER_SYLLABLE, 1);
99
100    planner.ot_map.add_gsub_pause(Some(reorder_myanmar));
101
102    for feature in MYANMAR_FEATURES.iter().take(4) {
103        planner
104            .ot_map
105            .enable_feature(*feature, F_MANUAL_ZWJ | F_PER_SYLLABLE, 1);
106        planner.ot_map.add_gsub_pause(None);
107    }
108
109    planner.ot_map.add_gsub_pause(Some(syllabic_clear_var)); // Don't need syllables anymore.
110
111    for feature in MYANMAR_FEATURES.iter().skip(4) {
112        planner.ot_map.enable_feature(*feature, F_MANUAL_ZWJ, 1);
113    }
114}
115
116fn setup_syllables(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
117    buffer.allocate_var(GlyphInfo::SYLLABLE_VAR);
118
119    myanmar_machine::find_syllables_myanmar(buffer);
120
121    let mut start = 0;
122    let mut end = buffer.next_syllable(0);
123    while start < buffer.len {
124        buffer.unsafe_to_break(Some(start), Some(end));
125        start = end;
126        end = buffer.next_syllable(start);
127    }
128
129    Some(false)
130}
131
132fn reorder_myanmar(_: &ShapePlan, font: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
133    use super::myanmar_machine::SyllableType;
134
135    let mut ret = false;
136    if !buffer_message!(buffer, font, "start reordering myanmar") {
137        buffer.deallocate_var(GlyphInfo::MYANMAR_CATEGORY_VAR);
138        buffer.deallocate_var(GlyphInfo::MYANMAR_POSITION_VAR);
139        return Some(ret);
140    }
141
142    if insert_dotted_circles(
143        font,
144        buffer,
145        SyllableType::BrokenCluster as u8,
146        category::OT_DOTTEDCIRCLE,
147        None,
148        None,
149    )? {
150        ret = true;
151    }
152
153    let mut start = 0;
154    let mut end = buffer.next_syllable(0);
155    while start < buffer.len {
156        if reorder_syllable_myanmar(start, end, buffer).is_none() {
157            buffer.index_failed();
158        }
159        start = end;
160        end = buffer.next_syllable(start);
161    }
162
163    buffer.deallocate_var(GlyphInfo::MYANMAR_CATEGORY_VAR);
164    buffer.deallocate_var(GlyphInfo::MYANMAR_POSITION_VAR);
165
166    buffer_message!(buffer, font, "end reordering myanmar");
167    Some(ret)
168}
169
170fn reorder_syllable_myanmar(start: usize, end: usize, buffer: &mut Buffer) -> Option<()> {
171    use super::myanmar_machine::SyllableType;
172
173    let syllable_type = match buffer.info.get(start)?.syllable() & 0x0F {
174        0 => SyllableType::ConsonantSyllable,
175        1 => SyllableType::PunctuationCluster,
176        2 => SyllableType::BrokenCluster,
177        3 => SyllableType::NonMyanmarCluster,
178        _ => unreachable!(),
179    };
180
181    match syllable_type {
182        // We already inserted dotted-circles, so just call the consonant_syllable.
183        SyllableType::ConsonantSyllable | SyllableType::BrokenCluster => {
184            initial_reordering_consonant_syllable(start, end, buffer)?;
185        }
186        SyllableType::PunctuationCluster | SyllableType::NonMyanmarCluster => {}
187    }
188    Some(())
189}
190
191// Rules from:
192// https://docs.microsoft.com/en-us/typography/script-development/myanmar
193fn initial_reordering_consonant_syllable(
194    start: usize,
195    end: usize,
196    buffer: &mut Buffer,
197) -> Option<()> {
198    let mut base = end;
199    let mut has_reph = false;
200
201    {
202        let mut limit = start;
203        if start + 3 <= end
204            && buffer.info.get(start)?.myanmar_category() == category::OT_Ra
205            && buffer.info.get(start + 1)?.myanmar_category() == category::OT_As
206            && buffer.info.get(start + 2)?.myanmar_category() == category::OT_H
207        {
208            limit += 3;
209            base = start;
210            has_reph = true;
211        }
212
213        {
214            if !has_reph {
215                base = limit;
216            }
217
218            for i in limit..end {
219                if buffer.info.get(i)?.is_consonant() {
220                    base = i;
221                    break;
222                }
223            }
224        }
225    }
226
227    // Reorder!
228    {
229        let mut i = start;
230        while i < start + if has_reph { 3 } else { 0 } {
231            buffer
232                .info
233                .get_mut(i)?
234                .set_myanmar_position(position::POS_AFTER_MAIN);
235            i += 1;
236        }
237
238        while i < base {
239            buffer
240                .info
241                .get_mut(i)?
242                .set_myanmar_position(position::POS_PRE_C);
243            i += 1;
244        }
245
246        if i < end {
247            buffer
248                .info
249                .get_mut(i)?
250                .set_myanmar_position(position::POS_BASE_C);
251            i += 1;
252        }
253
254        let mut pos = position::POS_AFTER_MAIN;
255        // The following loop may be ugly, but it implements all of
256        // Myanmar reordering!
257        for i in i..end {
258            // Split off the preceding glyph, which a variation selector copies.
259            let (before, rest) = buffer.info.split_at_mut_checked(i)?;
260            let info = rest.first_mut()?;
261            let cat = info.myanmar_category();
262
263            // Pre-base reordering
264            if cat == category::OT_MR {
265                info.set_myanmar_position(position::POS_PRE_C);
266                continue;
267            }
268
269            // Left matra
270            if cat == OT_VPre {
271                info.set_myanmar_position(position::POS_PRE_M);
272                continue;
273            }
274
275            if cat == category::OT_VS {
276                let t = before.last()?.myanmar_position();
277                info.set_myanmar_position(t);
278                continue;
279            }
280
281            if pos == position::POS_AFTER_MAIN && cat == category::OT_VBlw {
282                pos = position::POS_BELOW_C;
283                info.set_myanmar_position(pos);
284                continue;
285            }
286
287            if pos == position::POS_BELOW_C && cat == category::OT_A {
288                info.set_myanmar_position(position::POS_BEFORE_SUB);
289                continue;
290            }
291
292            if pos == position::POS_BELOW_C && cat == category::OT_VBlw {
293                info.set_myanmar_position(pos);
294                continue;
295            }
296
297            if pos == position::POS_BELOW_C && cat != category::OT_A {
298                pos = position::POS_AFTER_SUB;
299                info.set_myanmar_position(pos);
300                continue;
301            }
302
303            info.set_myanmar_position(pos);
304        }
305    }
306
307    buffer.sort(start, end, |a, b| {
308        a.myanmar_position().cmp(&b.myanmar_position()) == core::cmp::Ordering::Greater
309    });
310
311    // Flip left-mantra sequence
312    let mut first_left_matra = end;
313    let mut last_left_matra = end;
314
315    for i in start..end {
316        if buffer.info.get(i)?.myanmar_position() == position::POS_PRE_M {
317            if first_left_matra == end {
318                first_left_matra = i;
319            }
320
321            last_left_matra = i;
322        }
323    }
324
325    // https://github.com/harfbuzz/harfbuzz/issues/3863
326    if first_left_matra < last_left_matra {
327        // No need to merge clusters, done already?
328        buffer.reverse_range(first_left_matra, last_left_matra + 1);
329        // Reverse back VS, etc.
330        let mut i = first_left_matra;
331
332        for j in i..=last_left_matra {
333            if buffer.info.get(j)?.myanmar_category() == OT_VPre {
334                buffer.reverse_range(i, j + 1);
335                i = j + 1;
336            }
337        }
338    }
339    Some(())
340}
341
342fn setup_masks(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
343    buffer.allocate_var(GlyphInfo::MYANMAR_CATEGORY_VAR);
344    buffer.allocate_var(GlyphInfo::MYANMAR_POSITION_VAR);
345
346    // No masks, we just save information about characters.
347    for info in buffer.info_slice_mut() {
348        info.set_myanmar_properties();
349    }
350}