Skip to main content

harfrust/ot/shaper/
indic.rs

1use alloc::boxed::Box;
2use core::cmp;
3use core::ops::Range;
4
5use read_fonts::types::GlyphId;
6
7use super::syllabic::*;
8use super::*;
9use crate::algs::*;
10use crate::ot::apply::WouldApplyContext;
11use crate::ot::layout::*;
12use crate::ot::map::*;
13use crate::ot::OtData;
14use crate::plan::ShapePlan;
15use crate::unicode::GeneralCategory;
16use crate::unicode::{CharExt, Codepoint};
17use crate::ShaperFont;
18use crate::{GlyphInfo, Mask, Script, Tag};
19
20pub const INDIC_SHAPER: OtShaper = OtShaper {
21    collect_features: Some(collect_features),
22    override_features: Some(override_features),
23    create_data: Some(|plan| Box::new(IndicShapePlan::new(plan))),
24    preprocess_text: Some(preprocess_text),
25    postprocess_glyphs: None,
26    normalization_preference: NormalizationMode::ComposedDiacriticsNoShortCircuit,
27    decompose: Some(decompose),
28    compose: Some(compose),
29    setup_masks: Some(setup_masks),
30    gpos_tag: None,
31    reorder_marks: None,
32    zero_width_marks: ZeroWidthMarks::None,
33    fallback_position: false,
34};
35
36impl GlyphInfo {
37    declare_buffer_var_alias!(
38        OT_SHAPER_VAR_U8_CATEGORY_VAR,
39        u8,
40        INDIC_CATEGORY_VAR,
41        indic_category,
42        set_indic_category
43    );
44    declare_buffer_var_alias!(
45        OT_SHAPER_VAR_U8_AUXILIARY_VAR,
46        u8,
47        INDIC_POSITION_VAR,
48        indic_position,
49        set_indic_position
50    );
51
52    fn is_one_of(&self, flags: u32) -> bool {
53        // If it ligated, all bets are off.
54        if self.ligated() {
55            return false;
56        }
57
58        rb_flag_unsafe(self.indic_category() as u32) & flags != 0
59    }
60
61    fn is_joiner(&self) -> bool {
62        self.is_one_of(JOINER_FLAGS)
63    }
64
65    pub(crate) fn is_consonant(&self) -> bool {
66        self.is_one_of(CONSONANT_FLAGS_INDIC)
67    }
68
69    fn is_halant(&self) -> bool {
70        self.is_one_of(rb_flag(category::OT_H as u32))
71    }
72
73    fn set_indic_properties(&mut self) {
74        let u = self.glyph_id;
75        let (cat, pos) = indic_table::get_categories(u);
76
77        self.set_indic_category(cat);
78        self.set_indic_position(pos);
79    }
80}
81
82pub type Category = u8;
83
84// This mod doesn't exist in harfbuzz anymore. Instead, the corresponding values are auto-generated
85// by the various machines and stored in `hb-ot-shaper-indic-table`. This means that when updating the
86// values in the machines, we also need to update them here.
87#[allow(dead_code)]
88pub mod category {
89    pub const OT_X: u8 = 0;
90    pub const OT_C: u8 = 1;
91    pub const OT_V: u8 = 2;
92    pub const OT_N: u8 = 3;
93    pub const OT_H: u8 = 4;
94    pub const OT_ZWNJ: u8 = 5;
95    pub const OT_ZWJ: u8 = 6;
96    pub const OT_M: u8 = 7;
97    pub const OT_SM: u8 = 8;
98    pub const OT_A: u8 = 9;
99    pub const OT_VD: u8 = OT_A;
100    pub const OT_PLACEHOLDER: u8 = 10;
101    pub const OT_GB: u8 = OT_PLACEHOLDER;
102    pub const OT_DOTTEDCIRCLE: u8 = 11;
103    pub const OT_RS: u8 = 12; // Register Shifter, used in Khmer OT spec.
104    pub const OT_MPst: u8 = 13;
105    pub const OT_Repha: u8 = 14; // Atomically-encoded logical or visual repha.
106    pub const OT_Ra: u8 = 15;
107    pub const OT_CM: u8 = 16; // Consonant-Medial.
108    pub const OT_Symbol: u8 = 17; // Avagraha, etc that take marks (SM,A,VD).
109    pub const OT_CS: u8 = 18;
110
111    /* Khmer & Myanmar shapers. */
112    pub const OT_VAbv: u8 = 20;
113    pub const OT_VBlw: u8 = 21;
114    pub const OT_VPre: u8 = 22;
115    pub const OT_VPst: u8 = 23;
116
117    /* Khmer. */
118    pub const OT_Robatic: u8 = 25;
119    pub const OT_Xgroup: u8 = 26;
120    pub const OT_Ygroup: u8 = 27;
121
122    /* Myanmar */
123    pub const OT_As: u8 = 32; // Asat
124    pub const OT_MH: u8 = 35; // Medial
125    pub const OT_MR: u8 = 36; // Medial
126    pub const OT_MW: u8 = 37; // Medial
127    pub const OT_MY: u8 = 38; // Medial
128    pub const OT_PT: u8 = 39; // Pwo and other tones
129    pub const OT_VS: u8 = 40; // Variation selectors
130    pub const OT_ML: u8 = 41; // Consonant medials
131
132    pub const OT_SMPst: u8 = 57; // Syllable Medial Post-base
133
134    // This one doesn't exist in category in harfbuzz, only in
135    // the Myanmar machine. However, in Rust we unfortunately can't export
136    // inside the Ragel file, so we have to define it here as well. Needs to
137    // be kept in sync with the value in the machine.
138    pub const IV: u8 = 2;
139}
140
141pub mod position {
142    pub const POS_START: u8 = 0;
143
144    pub const POS_RA_TO_BECOME_REPH: u8 = 1;
145    pub const POS_PRE_M: u8 = 2;
146    pub const POS_PRE_C: u8 = 3;
147
148    pub const POS_BASE_C: u8 = 4;
149    pub const POS_AFTER_MAIN: u8 = 5;
150
151    pub const POS_ABOVE_C: u8 = 6;
152
153    pub const POS_BEFORE_SUB: u8 = 7;
154    pub const POS_BELOW_C: u8 = 8;
155    pub const POS_AFTER_SUB: u8 = 9;
156
157    pub const POS_BEFORE_POST: u8 = 10;
158    pub const POS_POST_C: u8 = 11;
159    pub const POS_AFTER_POST: u8 = 12;
160
161    pub const POS_SMVD: u8 = 13;
162
163    pub const POS_END: u8 = 14;
164}
165
166const INDIC_FEATURES: &[(Tag, MapFeatureFlags)] = &[
167    // Basic features.
168    // These features are applied in order, one at a time, after initial_reordering,
169    // constrained to the syllable.
170    (Tag::new(b"nukt"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
171    (Tag::new(b"akhn"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
172    (Tag::new(b"rphf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
173    (Tag::new(b"rkrf"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
174    (Tag::new(b"pref"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
175    (Tag::new(b"blwf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
176    (Tag::new(b"abvf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
177    (Tag::new(b"half"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
178    (Tag::new(b"pstf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
179    (Tag::new(b"vatu"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
180    (Tag::new(b"cjct"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
181    // Other features.
182    // These features are applied all at once, after final_reordering, constrained
183    // to the syllable.
184    // Default Bengali font in Windows for example has intermixed
185    // lookups for init,pres,abvs,blws features.
186    (Tag::new(b"init"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
187    (Tag::new(b"pres"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
188    (Tag::new(b"abvs"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
189    (Tag::new(b"blws"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
190    (Tag::new(b"psts"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
191    (Tag::new(b"haln"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
192];
193
194// Must be in the same order as the INDIC_FEATURES array.
195#[allow(dead_code)]
196mod indic_feature {
197    pub const NUKT: usize = 0;
198    pub const AKHN: usize = 1;
199    pub const RPHF: usize = 2;
200    pub const RKRF: usize = 3;
201    pub const PREF: usize = 4;
202    pub const BLWF: usize = 5;
203    pub const ABVF: usize = 6;
204    pub const HALF: usize = 7;
205    pub const PSTF: usize = 8;
206    pub const VATU: usize = 9;
207    pub const CJCT: usize = 10;
208    pub const INIT: usize = 11;
209    pub const PRES: usize = 12;
210    pub const ABVS: usize = 13;
211    pub const BLWS: usize = 14;
212    pub const PSTS: usize = 15;
213    pub const HALN: usize = 16;
214}
215
216pub(crate) const fn category_flag(c: Category) -> u32 {
217    rb_flag(c as u32)
218}
219
220// Note:
221//
222// We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
223// cannot happen in a consonant syllable.  The plus side however is, we can call the
224// consonant syllable logic from the vowel syllable function and get it all right!
225const CONSONANT_FLAGS_INDIC: u32 = category_flag(category::OT_C)
226    | category_flag(category::OT_CS)
227    | category_flag(category::OT_Ra)
228    | category_flag(category::OT_CM)
229    | category_flag(category::OT_V)
230    | category_flag(category::OT_PLACEHOLDER)
231    | category_flag(category::OT_DOTTEDCIRCLE);
232
233const JOINER_FLAGS: u32 = category_flag(category::OT_ZWJ) | category_flag(category::OT_ZWNJ);
234
235#[derive(Clone, Copy, PartialEq)]
236enum RephPosition {
237    AfterMain = position::POS_AFTER_MAIN as isize,
238    BeforeSub = position::POS_BEFORE_SUB as isize,
239    AfterSub = position::POS_AFTER_SUB as isize,
240    BeforePost = position::POS_BEFORE_POST as isize,
241    AfterPost = position::POS_AFTER_POST as isize,
242}
243
244#[derive(Clone, Copy, PartialEq)]
245enum RephMode {
246    /// Reph formed out of initial Ra,H sequence.
247    Implicit,
248    /// Reph formed out of initial Ra,H,ZWJ sequence.
249    Explicit,
250    /// Encoded Repha character, needs reordering.
251    LogRepha,
252}
253
254#[derive(Clone, Copy, PartialEq)]
255enum BlwfMode {
256    /// Below-forms feature applied to pre-base and post-base.
257    PreAndPost,
258    /// Below-forms feature applied to post-base only.
259    PostOnly,
260}
261
262#[derive(Clone, Copy)]
263struct IndicConfig {
264    script: Option<Script>,
265    has_old_spec: bool,
266    virama: u32,
267    reph_pos: RephPosition,
268    reph_mode: RephMode,
269    blwf_mode: BlwfMode,
270}
271
272impl IndicConfig {
273    const fn new(
274        script: Option<Script>,
275        has_old_spec: bool,
276        virama: u32,
277        reph_pos: RephPosition,
278        reph_mode: RephMode,
279        blwf_mode: BlwfMode,
280    ) -> Self {
281        IndicConfig {
282            script,
283            has_old_spec,
284            virama,
285            reph_pos,
286            reph_mode,
287            blwf_mode,
288        }
289    }
290}
291
292static INDIC_CONFIGS: &[IndicConfig] = &[
293    IndicConfig::new(
294        None,
295        false,
296        0,
297        RephPosition::BeforePost,
298        RephMode::Implicit,
299        BlwfMode::PreAndPost,
300    ),
301    IndicConfig::new(
302        Some(Script::DEVANAGARI),
303        true,
304        0x094D,
305        RephPosition::BeforePost,
306        RephMode::Implicit,
307        BlwfMode::PreAndPost,
308    ),
309    IndicConfig::new(
310        Some(Script::BENGALI),
311        true,
312        0x09CD,
313        RephPosition::AfterSub,
314        RephMode::Implicit,
315        BlwfMode::PreAndPost,
316    ),
317    IndicConfig::new(
318        Some(Script::GURMUKHI),
319        true,
320        0x0A4D,
321        RephPosition::BeforeSub,
322        RephMode::Implicit,
323        BlwfMode::PreAndPost,
324    ),
325    IndicConfig::new(
326        Some(Script::GUJARATI),
327        true,
328        0x0ACD,
329        RephPosition::BeforePost,
330        RephMode::Implicit,
331        BlwfMode::PreAndPost,
332    ),
333    IndicConfig::new(
334        Some(Script::ORIYA),
335        true,
336        0x0B4D,
337        RephPosition::AfterMain,
338        RephMode::Implicit,
339        BlwfMode::PreAndPost,
340    ),
341    IndicConfig::new(
342        Some(Script::TAMIL),
343        true,
344        0x0BCD,
345        RephPosition::AfterPost,
346        RephMode::Implicit,
347        BlwfMode::PreAndPost,
348    ),
349    IndicConfig::new(
350        Some(Script::TELUGU),
351        true,
352        0x0C4D,
353        RephPosition::AfterPost,
354        RephMode::Explicit,
355        BlwfMode::PostOnly,
356    ),
357    IndicConfig::new(
358        Some(Script::KANNADA),
359        true,
360        0x0CCD,
361        RephPosition::AfterPost,
362        RephMode::Implicit,
363        BlwfMode::PostOnly,
364    ),
365    IndicConfig::new(
366        Some(Script::MALAYALAM),
367        true,
368        0x0D4D,
369        RephPosition::AfterMain,
370        RephMode::LogRepha,
371        BlwfMode::PreAndPost,
372    ),
373    IndicConfig::new(
374        Some(Script::SINHALA),
375        false,
376        0x0DCA,
377        RephPosition::AfterPost,
378        RephMode::Explicit,
379        BlwfMode::PreAndPost,
380    ),
381];
382
383struct IndicWouldSubstituteFeature {
384    lookups: Range<usize>,
385    zero_context: bool,
386}
387
388impl IndicWouldSubstituteFeature {
389    pub fn new(map: &OtMap, feature_tag: Tag, zero_context: bool) -> Self {
390        IndicWouldSubstituteFeature {
391            lookups: match map.get_feature_stage(LayoutTableKind::Gsub, feature_tag) {
392                Some(stage) => map.stage_lookup_range(LayoutTableKind::Gsub, stage),
393                None => 0..0,
394            },
395            zero_context,
396        }
397    }
398
399    pub fn would_substitute(&self, map: &OtMap, ot: &OtData<'_>, glyphs: &[GlyphId]) -> bool {
400        for index in self.lookups.clone() {
401            let lookup = map.lookup(LayoutTableKind::Gsub, index);
402            let ctx = WouldApplyContext {
403                glyphs,
404                zero_context: self.zero_context,
405            };
406            if ot
407                .lookup(LayoutTableKind::Gsub, lookup.index)
408                .is_some_and(|lookup| lookup.would_apply(&ctx) == Some(true))
409            {
410                return true;
411            }
412        }
413
414        false
415    }
416}
417
418struct IndicShapePlan {
419    config: IndicConfig,
420    is_old_spec: bool,
421    // virama_glyph: Option<u32>,
422    rphf: IndicWouldSubstituteFeature,
423    pref: IndicWouldSubstituteFeature,
424    blwf: IndicWouldSubstituteFeature,
425    pstf: IndicWouldSubstituteFeature,
426    vatu: IndicWouldSubstituteFeature,
427    mask_array: [Mask; INDIC_FEATURES.len()],
428}
429
430impl IndicShapePlan {
431    fn new(plan: &ShapePlan) -> Self {
432        let script = plan.script;
433        let config = if let Some(c) = INDIC_CONFIGS.iter().skip(1).find(|c| c.script == script) {
434            *c
435        } else {
436            INDIC_CONFIGS[0]
437        };
438
439        let is_old_spec = config.has_old_spec
440            && plan
441                .ot_map
442                .chosen_script(LayoutTableKind::Gsub)
443                .is_none_or(|tag| tag.to_be_bytes()[3] != b'2');
444
445        // Use zero-context would_substitute() matching for new-spec of the main
446        // Indic scripts, and scripts with one spec only, but not for old-specs.
447        // The new-spec for all dual-spec scripts says zero-context matching happens.
448        //
449        // However, testing with Malayalam shows that old and new spec both allow
450        // context.  Testing with Bengali new-spec however shows that it doesn't.
451        // So, the heuristic here is the way it is.  It should *only* be changed,
452        // as we discover more cases of what Windows does.  DON'T TOUCH OTHERWISE.
453        let zero_context = is_old_spec && script != Some(Script::MALAYALAM);
454
455        let mut mask_array = [0; INDIC_FEATURES.len()];
456        for (i, feature) in INDIC_FEATURES.iter().enumerate() {
457            mask_array[i] = if feature.1 & F_GLOBAL != 0 {
458                0
459            } else {
460                plan.ot_map.get_1_mask(feature.0)
461            }
462        }
463
464        // TODO: what is this?
465        // let mut virama_glyph = None;
466        // if config.virama != 0 {
467        //     if let Some(g) = face.glyph_index(char::try_from(config.virama).unwrap()) {
468        //         virama_glyph = Some(g.0 as u32);
469        //     }
470        // }
471
472        IndicShapePlan {
473            config,
474            is_old_spec,
475            // virama_glyph,
476            rphf: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"rphf"), zero_context),
477            pref: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"pref"), zero_context),
478            blwf: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"blwf"), zero_context),
479            pstf: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"pstf"), zero_context),
480            vatu: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"vatu"), zero_context),
481            mask_array,
482        }
483    }
484}
485
486fn collect_features(planner: &mut ShapePlanner) {
487    // Do this before any lookups have been applied.
488    planner.ot_map.add_gsub_pause(Some(setup_syllables));
489
490    planner
491        .ot_map
492        .enable_feature(Tag::new(b"locl"), F_PER_SYLLABLE, 1);
493    // The Indic specs do not require ccmp, but we apply it here since if
494    // there is a use of it, it's typically at the beginning.
495    planner
496        .ot_map
497        .enable_feature(Tag::new(b"ccmp"), F_PER_SYLLABLE, 1);
498
499    planner.ot_map.add_gsub_pause(Some(initial_reordering));
500
501    for feature in INDIC_FEATURES.iter().take(11) {
502        planner.ot_map.add_feature(feature.0, feature.1, 1);
503        planner.ot_map.add_gsub_pause(None);
504    }
505
506    planner.ot_map.add_gsub_pause(Some(final_reordering));
507
508    for feature in INDIC_FEATURES.iter().skip(11) {
509        planner.ot_map.add_feature(feature.0, feature.1, 1);
510    }
511}
512
513fn override_features(planner: &mut ShapePlanner) {
514    planner.ot_map.disable_feature(Tag::new(b"liga"));
515    planner.ot_map.add_gsub_pause(Some(syllabic_clear_var)); // Don't need syllables anymore.
516}
517
518fn preprocess_text(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<()> {
519    vowel_constraints::preprocess_text_vowel_constraints(buffer);
520    Some(())
521}
522
523fn decompose(_: &NormalizeContext, ab: Codepoint) -> Option<(Codepoint, Codepoint)> {
524    // Don't decompose these.
525    match ab {
526        0x0931 |               // DEVANAGARI LETTER RRA
527        // https://github.com/harfbuzz/harfbuzz/issues/779
528        0x09DC |               // BENGALI LETTER RRA
529        0x09DD |               // BENGALI LETTER RHA
530        0x0B94 => return None, // TAMIL LETTER AU
531        _ => {}
532    }
533
534    unicode::decompose_impl(ab)
535}
536
537fn compose(_: &NormalizeContext, a: Codepoint, b: Codepoint) -> Option<Codepoint> {
538    // Avoid recomposing split matras.
539    if a.general_category().is_mark() {
540        return None;
541    }
542
543    // Composition-exclusion exceptions that we want to recompose.
544    if a == 0x09AF && b == 0x09BC {
545        return Some(0x09DF);
546    }
547
548    unicode::compose(a, b)
549}
550
551fn setup_masks(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
552    buffer.allocate_var(GlyphInfo::INDIC_CATEGORY_VAR);
553    buffer.allocate_var(GlyphInfo::INDIC_POSITION_VAR);
554
555    // We cannot setup masks here.  We save information about characters
556    // and setup masks later on in a pause-callback.
557    for info in buffer.info_slice_mut() {
558        info.set_indic_properties();
559    }
560}
561
562fn setup_syllables(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
563    buffer.allocate_var(GlyphInfo::SYLLABLE_VAR);
564
565    indic_machine::find_syllables_indic(buffer);
566
567    let mut start = 0;
568    let mut end = buffer.next_syllable(0);
569    while start < buffer.len {
570        buffer.unsafe_to_break(Some(start), Some(end));
571        start = end;
572        end = buffer.next_syllable(start);
573    }
574
575    Some(false)
576}
577
578fn initial_reordering(
579    plan: &ShapePlan,
580    font: &ShaperFont<'_, '_>,
581    buffer: &mut Buffer,
582) -> Option<bool> {
583    use super::indic_machine::SyllableType;
584
585    let mut ret = false;
586    if !buffer_message!(buffer, font, "start reordering indic initial") {
587        return Some(ret);
588    }
589
590    let indic_plan = plan.data::<IndicShapePlan>();
591
592    update_consonant_positions(plan, indic_plan, font, buffer);
593    if insert_dotted_circles(
594        font,
595        buffer,
596        SyllableType::BrokenCluster as u8,
597        category::OT_DOTTEDCIRCLE,
598        Some(category::OT_Repha),
599        Some(position::POS_END),
600    )? {
601        ret = true;
602    }
603
604    let mut start = 0;
605    let mut end = buffer.next_syllable(0);
606    while start < buffer.len {
607        if initial_reordering_syllable(plan, indic_plan, font, start, end, buffer).is_none() {
608            buffer.index_failed();
609        }
610        start = end;
611        end = buffer.next_syllable(start);
612    }
613
614    buffer_message!(buffer, font, "end reordering indic initial");
615    Some(ret)
616}
617
618fn update_consonant_positions(
619    plan: &ShapePlan,
620    indic_plan: &IndicShapePlan,
621    font: &ShaperFont<'_, '_>,
622    buffer: &mut Buffer,
623) {
624    let mut virama_glyph = None;
625    if indic_plan.config.virama != 0 {
626        virama_glyph = font.nominal_glyph(indic_plan.config.virama);
627    }
628
629    if let Some(virama) = virama_glyph {
630        let ot = font.layout().ot;
631        for info in buffer.info_slice_mut() {
632            if info.indic_position() == position::POS_BASE_C {
633                let consonant = info.as_glyph();
634                info.set_indic_position(consonant_position_from_ot(
635                    plan, indic_plan, ot, consonant, virama,
636                ));
637            }
638        }
639    }
640}
641
642fn consonant_position_from_ot(
643    plan: &ShapePlan,
644    indic_plan: &IndicShapePlan,
645    ot: &OtData<'_>,
646    consonant: GlyphId,
647    virama: GlyphId,
648) -> u8 {
649    // For old-spec, the order of glyphs is Consonant,Virama,
650    // whereas for new-spec, it's Virama,Consonant.  However,
651    // some broken fonts (like Free Sans) simply copied lookups
652    // from old-spec to new-spec without modification.
653    // And oddly enough, Uniscribe seems to respect those lookups.
654    // Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
655    // base at 0.  The font however, only has lookups matching
656    // 930,94D in 'blwf', not the expected 94D,930 (with new-spec
657    // table).  As such, we simply match both sequences.  Seems
658    // to work.
659    //
660    // Vatu is done as well, for:
661    // https://github.com/harfbuzz/harfbuzz/issues/1587
662
663    if indic_plan
664        .blwf
665        .would_substitute(&plan.ot_map, ot, &[virama, consonant])
666        || indic_plan
667            .blwf
668            .would_substitute(&plan.ot_map, ot, &[consonant, virama])
669        || indic_plan
670            .vatu
671            .would_substitute(&plan.ot_map, ot, &[virama, consonant])
672        || indic_plan
673            .vatu
674            .would_substitute(&plan.ot_map, ot, &[consonant, virama])
675    {
676        return position::POS_BELOW_C;
677    }
678
679    if indic_plan
680        .pstf
681        .would_substitute(&plan.ot_map, ot, &[virama, consonant])
682        || indic_plan
683            .pstf
684            .would_substitute(&plan.ot_map, ot, &[consonant, virama])
685    {
686        return position::POS_POST_C;
687    }
688
689    if indic_plan
690        .pref
691        .would_substitute(&plan.ot_map, ot, &[virama, consonant])
692        || indic_plan
693            .pref
694            .would_substitute(&plan.ot_map, ot, &[consonant, virama])
695    {
696        return position::POS_POST_C;
697    }
698
699    position::POS_BASE_C
700}
701
702fn initial_reordering_syllable(
703    plan: &ShapePlan,
704    indic_plan: &IndicShapePlan,
705    font: &ShaperFont<'_, '_>,
706    start: usize,
707    end: usize,
708    buffer: &mut Buffer,
709) -> Option<()> {
710    use super::indic_machine::SyllableType;
711
712    let syllable_type = match buffer.info.get(start)?.syllable() & 0x0F {
713        0 => SyllableType::ConsonantSyllable,
714        1 => SyllableType::VowelSyllable,
715        2 => SyllableType::StandaloneCluster,
716        3 => SyllableType::SymbolCluster,
717        4 => SyllableType::BrokenCluster,
718        5 => SyllableType::NonIndicCluster,
719        _ => unreachable!(),
720    };
721
722    match syllable_type {
723        // We made the vowels look like consonants.  So let's call the consonant logic!
724        SyllableType::VowelSyllable | SyllableType::ConsonantSyllable => {
725            initial_reordering_consonant_syllable(plan, indic_plan, font, start, end, buffer)
726        }
727        // We already inserted dotted-circles, so just call the standalone_cluster.
728        SyllableType::BrokenCluster | SyllableType::StandaloneCluster => {
729            initial_reordering_standalone_cluster(plan, indic_plan, font, start, end, buffer)
730        }
731        SyllableType::SymbolCluster | SyllableType::NonIndicCluster => Some(()),
732    }
733}
734
735// Rules from:
736// https://docs.microsqoft.com/en-us/typography/script-development/devanagari */
737fn initial_reordering_consonant_syllable(
738    plan: &ShapePlan,
739    indic_plan: &IndicShapePlan,
740    font: &ShaperFont<'_, '_>,
741    start: usize,
742    end: usize,
743    buffer: &mut Buffer,
744) -> Option<()> {
745    let ot = font.layout().ot;
746    // https://github.com/harfbuzz/harfbuzz/issues/435#issuecomment-335560167
747    // For compatibility with legacy usage in Kannada,
748    // Ra+h+ZWJ must behave like Ra+ZWJ+h...
749    if buffer.script == Some(Script::KANNADA)
750        && start + 3 <= end
751        && buffer
752            .info
753            .get(start)?
754            .is_one_of(category_flag(category::OT_Ra))
755        && buffer
756            .info
757            .get(start + 1)?
758            .is_one_of(category_flag(category::OT_H))
759        && buffer
760            .info
761            .get(start + 2)?
762            .is_one_of(category_flag(category::OT_ZWJ))
763    {
764        buffer.merge_clusters(start + 1, start + 3);
765        buffer.info.get_mut(start + 1..start + 3)?.swap(0, 1);
766    }
767
768    // 1. Find base consonant:
769    //
770    // The shaping engine finds the base consonant of the syllable, using the
771    // following algorithm: starting from the end of the syllable, move backwards
772    // until a consonant is found that does not have a below-base or post-base
773    // form (post-base forms have to follow below-base forms), or that is not a
774    // pre-base-reordering Ra, or arrive at the first consonant. The consonant
775    // stopped at will be the base.
776    //
777    //   - If the syllable starts with Ra + Halant (in a script that has Reph)
778    //     and has more than one consonant, Ra is excluded from candidates for
779    //     base consonants.
780
781    let mut base = end;
782    let mut has_reph = false;
783
784    {
785        // -> If the syllable starts with Ra + Halant (in a script that has Reph)
786        //    and has more than one consonant, Ra is excluded from candidates for
787        //    base consonants.
788        let mut limit = start;
789        if indic_plan.mask_array[indic_feature::RPHF] != 0
790            && start + 3 <= end
791            && ((indic_plan.config.reph_mode == RephMode::Implicit
792                && !buffer.info.get(start + 2)?.is_joiner())
793                || (indic_plan.config.reph_mode == RephMode::Explicit
794                    && buffer.info.get(start + 2)?.indic_category() == category::OT_ZWJ))
795        {
796            // See if it matches the 'rphf' feature.
797            let glyphs = &[
798                buffer.info.get(start)?.as_glyph(),
799                buffer.info.get(start + 1)?.as_glyph(),
800                if indic_plan.config.reph_mode == RephMode::Explicit {
801                    buffer.info.get(start + 2)?.as_glyph()
802                } else {
803                    GlyphId::NOTDEF
804                },
805            ];
806            if indic_plan
807                .rphf
808                .would_substitute(&plan.ot_map, ot, &glyphs[0..2])
809                || (indic_plan.config.reph_mode == RephMode::Explicit
810                    && indic_plan.rphf.would_substitute(&plan.ot_map, ot, glyphs))
811            {
812                limit += 2;
813                while limit < end && buffer.info.get(limit)?.is_joiner() {
814                    limit += 1;
815                }
816                base = start;
817                has_reph = true;
818            }
819        } else if indic_plan.config.reph_mode == RephMode::LogRepha
820            && buffer.info.get(start)?.indic_category() == category::OT_Repha
821        {
822            limit += 1;
823            while limit < end && buffer.info.get(limit)?.is_joiner() {
824                limit += 1;
825            }
826            base = start;
827            has_reph = true;
828        }
829
830        {
831            // -> starting from the end of the syllable, move backwards
832            let mut i = end;
833            let mut seen_below = false;
834            loop {
835                i -= 1;
836                let info = buffer.info.get(i)?;
837                // -> until a consonant is found
838                if info.is_consonant() {
839                    // -> that does not have a below-base or post-base form
840                    // (post-base forms have to follow below-base forms),
841                    if info.indic_position() != position::POS_BELOW_C
842                        && (info.indic_position() != position::POS_POST_C || seen_below)
843                    {
844                        base = i;
845                        break;
846                    }
847                    if info.indic_position() == position::POS_BELOW_C {
848                        seen_below = true;
849                    }
850
851                    // -> or that is not a pre-base-reordering Ra,
852                    //
853                    // IMPLEMENTATION NOTES:
854                    //
855                    // Our pre-base-reordering Ra's are marked position::PostC, so will be skipped
856                    // by the logic above already.
857
858                    // -> or arrive at the first consonant. The consonant stopped at will
859                    // be the base.
860                    base = i;
861                } else {
862                    // A ZWJ after a Halant stops the base search, and requests an explicit
863                    // half form.
864                    // A ZWJ before a Halant, requests a subjoined form instead, and hence
865                    // search continues.  This is particularly important for Bengali
866                    // sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya.
867                    if start < i
868                        && info.indic_category() == category::OT_ZWJ
869                        && buffer.info.get(i - 1)?.indic_category() == category::OT_H
870                    {
871                        break;
872                    }
873                }
874
875                if i <= limit {
876                    break;
877                }
878            }
879        }
880
881        // -> If the syllable starts with Ra + Halant (in a script that has Reph)
882        //    and has more than one consonant, Ra is excluded from candidates for
883        //    base consonants.
884        //
885        //  Only do this for unforced Reph. (ie. not for Ra,H,ZWJ.
886        if has_reph && base == start && limit - base <= 2 {
887            // Have no other consonant, so Reph is not formed and Ra becomes base.
888            has_reph = false;
889        }
890    }
891
892    // 2. Decompose and reorder Matras:
893    //
894    // Each matra and any syllable modifier sign in the syllable are moved to the
895    // appropriate position relative to the consonant(s) in the syllable. The
896    // shaping engine decomposes two- or three-part matras into their constituent
897    // parts before any repositioning. Matra characters are classified by which
898    // consonant in a conjunct they have affinity for and are reordered to the
899    // following positions:
900    //
901    //   - Before first half form in the syllable
902    //   - After subjoined consonants
903    //   - After post-form consonant
904    //   - After main consonant (for above marks)
905    //
906    // IMPLEMENTATION NOTES:
907    //
908    // The normalize() routine has already decomposed matras for us, so we don't
909    // need to worry about that.
910
911    // 3.  Reorder marks to canonical order:
912    //
913    // Adjacent nukta and halant or nukta and vedic sign are always repositioned
914    // if necessary, so that the nukta is first.
915    //
916    // IMPLEMENTATION NOTES:
917    //
918    // We don't need to do this: the normalize() routine already did this for us.
919
920    // Reorder characters
921
922    for info in buffer.info.get_mut(start..base)? {
923        info.set_indic_position(cmp::min(position::POS_PRE_C, info.indic_position()));
924    }
925
926    if base < end {
927        buffer
928            .info
929            .get_mut(base)?
930            .set_indic_position(position::POS_BASE_C);
931    }
932
933    // Handle beginning Ra
934    if has_reph {
935        buffer
936            .info
937            .get_mut(start)?
938            .set_indic_position(position::POS_RA_TO_BECOME_REPH);
939    }
940
941    // For old-style Indic script tags, move the first post-base Halant after
942    // last consonant.
943    //
944    // Reports suggest that in some scripts Uniscribe does this only if there
945    // is *not* a Halant after last consonant already.  We know that is the
946    // case for Kannada, while it reorders unconditionally in other scripts,
947    // eg. Malayalam, Bengali, and Devanagari.  We don't currently know about
948    // other scripts, so we block Kannada.
949    //
950    // Kannada test case:
951    // U+0C9A,U+0CCD,U+0C9A,U+0CCD
952    // With some versions of Lohit Kannada.
953    // https://bugs.freedesktop.org/show_bug.cgi?id=59118
954    //
955    // Malayalam test case:
956    // U+0D38,U+0D4D,U+0D31,U+0D4D,U+0D31,U+0D4D
957    // With lohit-ttf-20121122/Lohit-Malayalam.ttf
958    //
959    // Bengali test case:
960    // U+0998,U+09CD,U+09AF,U+09CD
961    // With Windows XP vrinda.ttf
962    // https://github.com/harfbuzz/harfbuzz/issues/1073
963    //
964    // Devanagari test case:
965    // U+091F,U+094D,U+0930,U+094D
966    // With chandas.ttf
967    // https://github.com/harfbuzz/harfbuzz/issues/1071
968    if indic_plan.is_old_spec {
969        let disallow_double_halants = buffer.script == Some(Script::KANNADA);
970        for i in base + 1..end {
971            if buffer.info.get(i)?.indic_category() == category::OT_H {
972                let mut j = end - 1;
973                while j > i {
974                    let info = buffer.info.get(j)?;
975                    if info.is_consonant()
976                        || (disallow_double_halants && info.indic_category() == category::OT_H)
977                    {
978                        break;
979                    }
980
981                    j -= 1;
982                }
983
984                if buffer.info.get(j)?.indic_category() != category::OT_H && j > i {
985                    // Move Halant to after last consonant.
986                    buffer.info.get_mut(i..=j)?.rotate_left(1);
987                }
988
989                break;
990            }
991        }
992    }
993
994    // Attach misc marks to previous char to move with them.
995    {
996        let mut last_pos = position::POS_START;
997        for i in start..end {
998            let info = *buffer.info.get(i)?;
999            let ok = rb_flag_unsafe(info.indic_category() as u32)
1000                & (category_flag(category::OT_ZWJ)
1001                    | category_flag(category::OT_ZWNJ)
1002                    | category_flag(category::OT_N)
1003                    | category_flag(category::OT_RS)
1004                    | category_flag(category::OT_CM)
1005                    | category_flag(category::OT_H))
1006                != 0;
1007            if ok {
1008                let mut pos = last_pos;
1009                if info.indic_category() == category::OT_H && pos == position::POS_PRE_M {
1010                    // Uniscribe doesn't move the Halant with Left Matra.
1011                    // TEST: U+092B,U+093F,U+094DE
1012                    // We follow.
1013                    for j in (start + 1..=i).rev() {
1014                        let prev_pos = buffer.info.get(j - 1)?.indic_position();
1015                        if prev_pos != position::POS_PRE_M {
1016                            pos = prev_pos;
1017                            break;
1018                        }
1019                    }
1020                }
1021
1022                buffer.info.get_mut(i)?.set_indic_position(pos);
1023            } else if info.indic_position() != position::POS_SMVD {
1024                if info.indic_category() == category::OT_MPst
1025                    && i > start
1026                    && buffer.info.get(i - 1)?.indic_category() == category::OT_SM
1027                {
1028                    buffer
1029                        .info
1030                        .get_mut(i - 1)?
1031                        .set_indic_position(info.indic_position());
1032                }
1033
1034                last_pos = info.indic_position();
1035            }
1036        }
1037    }
1038    // For post-base consonants let them own anything before them
1039    // since the last consonant or matra.
1040    {
1041        let mut last = base;
1042        for i in base + 1..end {
1043            let info = *buffer.info.get(i)?;
1044            if info.is_consonant() {
1045                for other in buffer.info.get_mut(last + 1..i)? {
1046                    if other.indic_position() < position::POS_SMVD {
1047                        other.set_indic_position(info.indic_position());
1048                    }
1049                }
1050
1051                last = i;
1052            } else if (rb_flag_unsafe(info.indic_category() as u32)
1053                & (rb_flag(category::OT_M as u32) | rb_flag(category::OT_MPst as u32)))
1054                != 0
1055            {
1056                last = i;
1057            }
1058        }
1059    }
1060
1061    {
1062        // Use syllable() for sort accounting temporarily.
1063        let syllable = buffer.info.get(start)?.syllable();
1064        let infos = buffer.info.get_mut(start..end)?;
1065        for (i, info) in infos.iter_mut().enumerate() {
1066            // We don't care about overflow here as we won't actually use these
1067            // values if `end - start > 127`.
1068            info.set_syllable(i as u8);
1069        }
1070
1071        stable_sort_by(infos, |a, b| a.indic_position().cmp(&b.indic_position()));
1072
1073        // Find base again; also flip left-matra sequence.
1074        let mut first_left_mantra = end;
1075        let mut last_left_mantra = end;
1076        base = end;
1077
1078        for (i, info) in (start..).zip(infos.iter()) {
1079            if info.indic_position() == position::POS_BASE_C {
1080                base = i;
1081                break;
1082            } else if info.indic_position() == position::POS_PRE_M {
1083                if first_left_mantra == end {
1084                    first_left_mantra = i;
1085                }
1086
1087                last_left_mantra = i;
1088            }
1089        }
1090
1091        // The syllable range is known to be in bounds from here on, so no
1092        // lookup below can return early and leave the syllables unrestored.
1093
1094        // https://github.com/harfbuzz/harfbuzz/issues/3863
1095        if first_left_mantra < last_left_mantra {
1096            // No need to merge clusters, handled later.
1097            buffer.reverse_range(first_left_mantra, last_left_mantra + 1);
1098            // Reverse back nuktas, etc.
1099            let mut i = first_left_mantra;
1100
1101            for j in i..=last_left_mantra {
1102                if (rb_flag_unsafe(buffer.info.get(j)?.indic_category() as u32)
1103                    & (rb_flag(category::OT_M as u32) | rb_flag(category::OT_MPst as u32)))
1104                    != 0
1105                {
1106                    buffer.reverse_range(i, j + 1);
1107                    i = j + 1;
1108                }
1109            }
1110        }
1111
1112        // Things are out-of-control for post base positions, they may shuffle
1113        // around like crazy.  In old-spec mode, we move halants around, so in
1114        // that case merge all clusters after base.  Otherwise, check the sort
1115        // order and merge as needed.
1116        // For pre-base stuff, we handle cluster issues in final reordering.
1117        //
1118        // We could use buffer->sort() for this, if there was no special
1119        // reordering of pre-base stuff happening later...
1120        // We don't want to merge_clusters all of that, which buffer->sort()
1121        // would.  Here's a concrete example:
1122        //
1123        // Assume there's a pre-base consonant and explicit Halant before base,
1124        // followed by a prebase-reordering (left) Matra:
1125        //
1126        //   C,H,ZWNJ,B,M
1127        //
1128        // At this point in reordering we would have:
1129        //
1130        //   M,C,H,ZWNJ,B
1131        //
1132        // whereas in final reordering we will bring the Matra closer to Base:
1133        //
1134        //   C,H,ZWNJ,M,B
1135        //
1136        // That's why we don't want to merge-clusters anything before the Base
1137        // at this point.  But if something moved from after Base to before it,
1138        // we should merge clusters from base to them.  In final-reordering, we
1139        // only move things around before base, and merge-clusters up to base.
1140        // These two merge-clusters from the two sides of base will interlock
1141        // to merge things correctly.  See:
1142        // https://github.com/harfbuzz/harfbuzz/issues/2272
1143        if indic_plan.is_old_spec || end - start > 127 {
1144            buffer.merge_clusters(base, end);
1145        } else {
1146            // Note! syllable() is a one-byte field.
1147            for i in base..end {
1148                let order = buffer.info.get(i)?.syllable();
1149                if order != 255 {
1150                    let mut min = i;
1151                    let mut max = i;
1152                    let mut j = start + order as usize;
1153                    while j != i {
1154                        min = cmp::min(min, j);
1155                        max = cmp::max(max, j);
1156                        let info = buffer.info.get_mut(j)?;
1157                        let next = start + info.syllable() as usize;
1158                        info.set_syllable(255); // So we don't process j later again.
1159                        j = next;
1160                    }
1161
1162                    buffer.merge_clusters(cmp::max(base, min), max + 1);
1163                }
1164            }
1165        }
1166
1167        // Put syllable back in.
1168        for info in buffer.info.get_mut(start..end)? {
1169            info.set_syllable(syllable);
1170        }
1171    }
1172
1173    // Setup masks now
1174
1175    {
1176        // Reph
1177        for info in buffer.info.get_mut(start..end)? {
1178            if info.indic_position() != position::POS_RA_TO_BECOME_REPH {
1179                break;
1180            }
1181
1182            info.mask |= indic_plan.mask_array[indic_feature::RPHF];
1183        }
1184
1185        // Pre-base
1186        let mut mask = indic_plan.mask_array[indic_feature::HALF];
1187        if !indic_plan.is_old_spec && indic_plan.config.blwf_mode == BlwfMode::PreAndPost {
1188            mask |= indic_plan.mask_array[indic_feature::BLWF];
1189        }
1190
1191        for info in buffer.info.get_mut(start..base)? {
1192            info.mask |= mask;
1193        }
1194
1195        // Base
1196        mask = 0;
1197        if base < end {
1198            buffer.info.get_mut(base)?.mask |= mask;
1199        }
1200
1201        // Post-base
1202        mask = indic_plan.mask_array[indic_feature::BLWF]
1203            | indic_plan.mask_array[indic_feature::ABVF]
1204            | indic_plan.mask_array[indic_feature::PSTF];
1205        for i in base + 1..end {
1206            buffer.info.get_mut(i)?.mask |= mask;
1207        }
1208    }
1209
1210    if indic_plan.is_old_spec && buffer.script == Some(Script::DEVANAGARI) {
1211        // Old-spec eye-lash Ra needs special handling.  From the
1212        // spec:
1213        //
1214        // "The feature 'below-base form' is applied to consonants
1215        // having below-base forms and following the base consonant.
1216        // The exception is vattu, which may appear below half forms
1217        // as well as below the base glyph. The feature 'below-base
1218        // form' will be applied to all such occurrences of Ra as well."
1219        //
1220        // Test case: U+0924,U+094D,U+0930,U+094d,U+0915
1221        // with Sanskrit 2003 font.
1222        //
1223        // However, note that Ra,Halant,ZWJ is the correct way to
1224        // request eyelash form of Ra, so we wouldbn't inhibit it
1225        // in that sequence.
1226        //
1227        // Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
1228        for i in start..base.saturating_sub(1) {
1229            if buffer.info.get(i)?.indic_category() == category::OT_Ra
1230                && buffer.info.get(i + 1)?.indic_category() == category::OT_H
1231                && (i + 2 == base || buffer.info.get(i + 2)?.indic_category() != category::OT_ZWJ)
1232            {
1233                for info in buffer.info.get_mut(i..i + 2)? {
1234                    info.mask |= indic_plan.mask_array[indic_feature::BLWF];
1235                }
1236            }
1237        }
1238    }
1239
1240    let pref_len = 2;
1241    if indic_plan.mask_array[indic_feature::PREF] != 0 && base + pref_len < end {
1242        // Find a Halant,Ra sequence and mark it for pre-base-reordering processing.
1243        for i in base + 1..end - pref_len + 1 {
1244            let glyphs = &[
1245                buffer.info.get(i)?.as_glyph(),
1246                buffer.info.get(i + 1)?.as_glyph(),
1247            ];
1248            if indic_plan.pref.would_substitute(&plan.ot_map, ot, glyphs) {
1249                for info in buffer.info.get_mut(i..i + pref_len)? {
1250                    info.mask |= indic_plan.mask_array[indic_feature::PREF];
1251                }
1252                break;
1253            }
1254        }
1255    }
1256
1257    // Apply ZWJ/ZWNJ effects
1258    for i in start + 1..end {
1259        let info = buffer.info.get(i)?;
1260        if info.is_joiner() {
1261            let non_joiner = info.indic_category() == category::OT_ZWNJ;
1262            let mut j = i;
1263
1264            loop {
1265                j -= 1;
1266
1267                // ZWJ/ZWNJ should disable CJCT.  They do that by simply
1268                // being there, since we don't skip them for the CJCT
1269                // feature (ie. F_MANUAL_ZWJ)
1270
1271                let info = buffer.info.get_mut(j)?;
1272
1273                // A ZWNJ disables HALF.
1274                if non_joiner {
1275                    info.mask &= !indic_plan.mask_array[indic_feature::HALF];
1276                }
1277
1278                if j <= start || info.is_consonant() {
1279                    break;
1280                }
1281            }
1282        }
1283    }
1284
1285    Some(())
1286}
1287
1288fn initial_reordering_standalone_cluster(
1289    plan: &ShapePlan,
1290    indic_plan: &IndicShapePlan,
1291    font: &ShaperFont<'_, '_>,
1292    start: usize,
1293    end: usize,
1294    buffer: &mut Buffer,
1295) -> Option<()> {
1296    // We treat placeholder/dotted-circle as if they are consonants, so we
1297    // should just chain.  Only if not in compatibility mode that is...
1298    initial_reordering_consonant_syllable(plan, indic_plan, font, start, end, buffer)
1299}
1300
1301fn final_reordering(
1302    plan: &ShapePlan,
1303    font: &ShaperFont<'_, '_>,
1304    buffer: &mut Buffer,
1305) -> Option<bool> {
1306    if buffer.is_empty() {
1307        return Some(false);
1308    }
1309
1310    if buffer_message!(buffer, font, "start reordering indic final") {
1311        foreach_syllable!(buffer, start, end, {
1312            if final_reordering_impl(plan, font, start, end, buffer).is_none() {
1313                buffer.index_failed();
1314            }
1315        });
1316        buffer_message!(buffer, font, "end reordering indic final");
1317    }
1318
1319    buffer.deallocate_var(GlyphInfo::INDIC_CATEGORY_VAR);
1320    buffer.deallocate_var(GlyphInfo::INDIC_POSITION_VAR);
1321
1322    Some(false)
1323}
1324
1325fn final_reordering_impl(
1326    plan: &ShapePlan,
1327    font: &ShaperFont<'_, '_>,
1328    start: usize,
1329    end: usize,
1330    buffer: &mut Buffer,
1331) -> Option<()> {
1332    let indic_plan = plan.data::<IndicShapePlan>();
1333
1334    // This function relies heavily on halant glyphs.  Lots of ligation
1335    // and possibly multiple substitutions happened prior to this
1336    // phase, and that might have messed up our properties.  Recover
1337    // from a particular case of that where we're fairly sure that a
1338    // class of OT_H is desired but has been lost.
1339    //
1340    // We don't call load_virama_glyph(), since we know it's already loaded.
1341    let mut virama_glyph = None;
1342    if indic_plan.config.virama != 0 {
1343        if let Some(g) = font.nominal_glyph(indic_plan.config.virama) {
1344            virama_glyph = Some(g.to_u32());
1345        }
1346    }
1347
1348    if let Some(virama_glyph) = virama_glyph {
1349        for info in buffer.info.get_mut(start..end)? {
1350            if info.glyph_id == virama_glyph && info.ligated() && info.multiplied() {
1351                // This will make sure that this glyph passes is_halant() test.
1352                info.set_indic_category(category::OT_H);
1353                info.clear_ligated_and_multiplied();
1354            }
1355        }
1356    }
1357
1358    // 4. Final reordering:
1359    //
1360    // After the localized forms and basic shaping forms GSUB features have been
1361    // applied (see below), the shaping engine performs some final glyph
1362    // reordering before applying all the remaining font features to the entire
1363    // syllable.
1364
1365    let mut try_pref = indic_plan.mask_array[indic_feature::PREF] != 0;
1366
1367    let mut base = start;
1368    while base < end {
1369        if buffer.info.get(base)?.indic_position() as u32 >= position::POS_BASE_C as u32 {
1370            if try_pref && base + 1 < end {
1371                for i in base + 1..end {
1372                    let info = buffer.info.get(i)?;
1373                    if (info.mask & indic_plan.mask_array[indic_feature::PREF]) != 0 {
1374                        if !(info.substituted() && info.ligated_and_didnt_multiply()) {
1375                            // Ok, this was a 'pref' candidate but didn't form any.
1376                            // Base is around here...
1377                            base = i;
1378                            while base < end && buffer.info.get(base)?.is_halant() {
1379                                base += 1;
1380                            }
1381
1382                            if base < end {
1383                                buffer
1384                                    .info
1385                                    .get_mut(base)?
1386                                    .set_indic_position(position::POS_BASE_C);
1387                            }
1388
1389                            try_pref = false;
1390                        }
1391
1392                        break;
1393                    }
1394
1395                    if base == end {
1396                        break;
1397                    }
1398                }
1399            }
1400
1401            // For Malayalam, skip over unformed below- (but NOT post-) forms.
1402            if buffer.script == Some(Script::MALAYALAM) {
1403                let mut i = base + 1;
1404                while i < end {
1405                    while i < end && buffer.info.get(i)?.is_joiner() {
1406                        i += 1;
1407                    }
1408
1409                    if i == end || !buffer.info.get(i)?.is_halant() {
1410                        break;
1411                    }
1412
1413                    i += 1; // Skip halant.
1414
1415                    while i < end && buffer.info.get(i)?.is_joiner() {
1416                        i += 1;
1417                    }
1418
1419                    if i < end {
1420                        let info = buffer.info.get_mut(i)?;
1421                        if info.is_consonant() && info.indic_position() == position::POS_BELOW_C {
1422                            base = i;
1423                            info.set_indic_position(position::POS_BASE_C);
1424                        }
1425                    }
1426
1427                    i += 1;
1428                }
1429            }
1430
1431            if start < base
1432                && buffer.info.get(base)?.indic_position() as u32 > position::POS_BASE_C as u32
1433            {
1434                base -= 1;
1435            }
1436
1437            break;
1438        }
1439
1440        base += 1;
1441    }
1442
1443    if base == end
1444        && start < base
1445        && buffer
1446            .info
1447            .get(base - 1)?
1448            .is_one_of(rb_flag(category::OT_ZWJ as u32))
1449    {
1450        base -= 1;
1451    }
1452
1453    if base < end {
1454        while start < base
1455            && buffer
1456                .info
1457                .get(base)?
1458                .is_one_of(rb_flag(category::OT_N as u32) | rb_flag(category::OT_H as u32))
1459        {
1460            base -= 1;
1461        }
1462    }
1463
1464    // - Reorder matras:
1465    //
1466    //   If a pre-base matra character had been reordered before applying basic
1467    //   features, the glyph can be moved closer to the main consonant based on
1468    //   whether half-forms had been formed. Actual position for the matra is
1469    //   defined as “after last standalone halant glyph, after initial matra
1470    //   position and before the main consonant”. If ZWJ or ZWNJ follow this
1471    //   halant, position is moved after it.
1472    //
1473    // IMPLEMENTATION NOTES:
1474    //
1475    // It looks like the last sentence is wrong.  Testing, with Windows 7 Uniscribe
1476    // and Devanagari shows that the behavior is best described as:
1477    //
1478    // "If ZWJ follows this halant, matra is NOT repositioned after this halant.
1479    //  If ZWNJ follows this halant, position is moved after it."
1480    //
1481    // Test case, with Adobe Devanagari or Nirmala UI:
1482    //
1483    //   U+091F,U+094D,U+200C,U+092F,U+093F
1484    //   (Matra moves to the middle, after ZWNJ.)
1485    //
1486    //   U+091F,U+094D,U+200D,U+092F,U+093F
1487    //   (Matra does NOT move, stays to the left.)
1488    //
1489    // https://github.com/harfbuzz/harfbuzz/issues/1070
1490
1491    // Otherwise there can't be any pre-base matra characters.
1492    if start + 1 < end && start < base {
1493        // If we lost track of base, alas, position before last thingy.
1494        let mut new_pos = if base == end { base - 2 } else { base - 1 };
1495
1496        // Malayalam / Tamil do not have "half" forms or explicit virama forms.
1497        // The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1498        // We want to position matra after them.
1499        if buffer.script != Some(Script::MALAYALAM) && buffer.script != Some(Script::TAMIL) {
1500            loop {
1501                while new_pos > start
1502                    && !buffer.info.get(new_pos)?.is_one_of(
1503                        rb_flag(category::OT_M as u32)
1504                            | rb_flag(category::OT_MPst as u32)
1505                            | rb_flag(category::OT_H as u32),
1506                    )
1507                {
1508                    new_pos -= 1;
1509                }
1510
1511                // If we found no Halant we are done.
1512                // Otherwise only proceed if the Halant does
1513                // not belong to the Matra itself!
1514                let info = buffer.info.get(new_pos)?;
1515                if info.is_halant() && info.indic_position() != position::POS_PRE_M {
1516                    if new_pos + 1 < end {
1517                        // -> If ZWJ follows this halant, matra is NOT repositioned after this halant.
1518                        if buffer.info.get(new_pos + 1)?.indic_category() == category::OT_ZWJ {
1519                            // Keep searching.
1520                            if new_pos > start {
1521                                new_pos -= 1;
1522                                continue;
1523                            }
1524                        }
1525
1526                        // -> If ZWNJ follows this halant, position is moved after it.
1527                        //
1528                        // IMPLEMENTATION NOTES:
1529                        //
1530                        // This is taken care of by the state-machine. A Halant,ZWNJ is a terminating
1531                        // sequence for a consonant syllable; any pre-base matras occurring after it
1532                        // will belong to the subsequent syllable.
1533                    }
1534                } else {
1535                    new_pos = start; // No move.
1536                }
1537
1538                break;
1539            }
1540        }
1541
1542        if start < new_pos && buffer.info.get(new_pos)?.indic_position() != position::POS_PRE_M {
1543            // Now go see if there's actually any matras...
1544            for i in (start + 1..=new_pos).rev() {
1545                if buffer.info.get(i - 1)?.indic_position() == position::POS_PRE_M {
1546                    let old_pos = i - 1;
1547                    // Shouldn't actually happen.
1548                    if old_pos < base && base <= new_pos {
1549                        base -= 1;
1550                    }
1551
1552                    buffer.info.get_mut(old_pos..=new_pos)?.rotate_left(1);
1553
1554                    // Note: this merge_clusters() is intentionally *after* the reordering.
1555                    // Indic matra reordering is special and tricky...
1556                    buffer.merge_clusters(new_pos, cmp::min(end, base + 1));
1557
1558                    new_pos -= 1;
1559                }
1560            }
1561        } else {
1562            for i in start..base {
1563                if buffer.info.get(i)?.indic_position() == position::POS_PRE_M {
1564                    buffer.merge_clusters(i, cmp::min(end, base + 1));
1565                    break;
1566                }
1567            }
1568        }
1569    }
1570
1571    // - Reorder reph:
1572    //
1573    //   Reph’s original position is always at the beginning of the syllable,
1574    //   (i.e. it is not reordered at the character reordering stage). However,
1575    //   it will be reordered according to the basic-forms shaping results.
1576    //   Possible positions for reph, depending on the script, are; after main,
1577    //   before post-base consonant forms, and after post-base consonant forms.
1578
1579    // Two cases:
1580    //
1581    // - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then
1582    //   we should only move it if the sequence ligated to the repha form.
1583    //
1584    // - If repha is encoded separately and in the logical position, we should only
1585    //   move it if it did NOT ligate.  If it ligated, it's probably the font trying
1586    //   to make it work without the reordering.
1587
1588    if start + 1 < end
1589        && buffer.info.get(start).is_some_and(|info| {
1590            info.indic_position() == position::POS_RA_TO_BECOME_REPH
1591                && (info.indic_category() == category::OT_Repha) ^ info.ligated_and_didnt_multiply()
1592        })
1593    {
1594        let mut new_reph_pos;
1595        'reph: {
1596            let reph_pos = indic_plan.config.reph_pos;
1597
1598            // 1. If reph should be positioned after post-base consonant forms,
1599            //    proceed to step 5.
1600            if reph_pos != RephPosition::AfterPost {
1601                // 2. If the reph repositioning class is not after post-base: target
1602                //    position is after the first explicit halant glyph between the
1603                //    first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1604                //    are following this halant, position is moved after it. If such
1605                //    position is found, this is the target position. Otherwise,
1606                //    proceed to the next step.
1607                //
1608                //    Note: in old-implementation fonts, where classifications were
1609                //    fixed in shaping engine, there was no case where reph position
1610                //    will be found on this step.
1611                {
1612                    new_reph_pos = start + 1;
1613                    while new_reph_pos < base && !buffer.info.get(new_reph_pos)?.is_halant() {
1614                        new_reph_pos += 1;
1615                    }
1616
1617                    if new_reph_pos < base && buffer.info.get(new_reph_pos)?.is_halant() {
1618                        // ->If ZWJ or ZWNJ are following this halant, position is moved after it.
1619                        if new_reph_pos + 1 < base && buffer.info.get(new_reph_pos + 1)?.is_joiner()
1620                        {
1621                            new_reph_pos += 1;
1622                        }
1623
1624                        break 'reph;
1625                    }
1626                }
1627
1628                // 3. If reph should be repositioned after the main consonant: find the
1629                //    first consonant not ligated with main, or find the first
1630                //    consonant that is not a potential pre-base-reordering Ra.
1631                if reph_pos == RephPosition::AfterMain {
1632                    new_reph_pos = base;
1633                    while new_reph_pos + 1 < end
1634                        && buffer.info.get(new_reph_pos + 1)?.indic_position()
1635                            <= position::POS_AFTER_MAIN
1636                    {
1637                        new_reph_pos += 1;
1638                    }
1639
1640                    if new_reph_pos < end {
1641                        break 'reph;
1642                    }
1643                }
1644
1645                // 4. If reph should be positioned before post-base consonant, find
1646                //    first post-base classified consonant not ligated with main. If no
1647                //    consonant is found, the target position should be before the
1648                //    first matra, syllable modifier sign or vedic sign.
1649                //
1650                // This is our take on what step 4 is trying to say (and failing, BADLY).
1651                if reph_pos == RephPosition::AfterSub {
1652                    new_reph_pos = base;
1653                    while new_reph_pos + 1 < end
1654                        && (rb_flag_unsafe(
1655                            buffer.info.get(new_reph_pos + 1)?.indic_position() as u32
1656                        ) & (rb_flag(position::POS_POST_C as u32)
1657                            | rb_flag(position::POS_AFTER_POST as u32)
1658                            | rb_flag(position::POS_SMVD as u32)))
1659                            == 0
1660                    {
1661                        new_reph_pos += 1;
1662                    }
1663
1664                    if new_reph_pos < end {
1665                        break 'reph;
1666                    }
1667                }
1668            }
1669
1670            // 5. If no consonant is found in steps 3 or 4, move reph to a position
1671            //    immediately before the first post-base matra, syllable modifier
1672            //    sign or vedic sign that has a reordering class after the intended
1673            //    reph position. For example, if the reordering position for reph
1674            //    is post-main, it will skip above-base matras that also have a
1675            //    post-main position.
1676            //
1677            // Copied from step 2.
1678            new_reph_pos = start + 1;
1679            while new_reph_pos < base && !buffer.info.get(new_reph_pos)?.is_halant() {
1680                new_reph_pos += 1;
1681            }
1682
1683            if new_reph_pos < base && buffer.info.get(new_reph_pos)?.is_halant() {
1684                /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1685                if new_reph_pos + 1 < base && buffer.info.get(new_reph_pos + 1)?.is_joiner() {
1686                    new_reph_pos += 1;
1687                }
1688
1689                break 'reph;
1690            }
1691            // See https://github.com/harfbuzz/harfbuzz/issues/2298#issuecomment-615318654
1692
1693            // 6. Otherwise, reorder reph to the end of the syllable.
1694            {
1695                new_reph_pos = end - 1;
1696                while new_reph_pos > start
1697                    && buffer.info.get(new_reph_pos)?.indic_position() == position::POS_SMVD
1698                {
1699                    new_reph_pos -= 1;
1700                }
1701
1702                // If the Reph is to be ending up after a Matra,Halant sequence,
1703                // position it before that Halant so it can interact with the Matra.
1704                // However, if it's a plain Consonant,Halant we shouldn't do that.
1705                // Uniscribe doesn't do this.
1706                // TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1707                if buffer.info.get(new_reph_pos)?.is_halant() {
1708                    // An empty or inverted range means there are no matras to skip.
1709                    for info in buffer.info.get(base + 1..new_reph_pos).unwrap_or_default() {
1710                        if (rb_flag_unsafe(info.indic_category() as u32)
1711                            & (rb_flag(category::OT_M as u32) | rb_flag(category::OT_MPst as u32)))
1712                            != 0
1713                        {
1714                            // Ok, got it.
1715                            new_reph_pos -= 1;
1716                        }
1717                    }
1718                }
1719            }
1720
1721            break 'reph;
1722        }
1723
1724        // Move
1725        buffer.merge_clusters(start, new_reph_pos + 1);
1726
1727        buffer.info.get_mut(start..=new_reph_pos)?.rotate_left(1);
1728
1729        if start < base && base <= new_reph_pos {
1730            base -= 1;
1731        }
1732    }
1733
1734    // - Reorder pre-base-reordering consonants:
1735    //
1736    //   If a pre-base-reordering consonant is found, reorder it according to
1737    //   the following rules:
1738
1739    // Otherwise there can't be any pre-base-reordering Ra.
1740    if try_pref && base + 1 < end {
1741        for i in base + 1..end {
1742            let info = buffer.info.get(i)?;
1743            if (info.mask & indic_plan.mask_array[indic_feature::PREF]) != 0 {
1744                // 1. Only reorder a glyph produced by substitution during application
1745                //    of the <pref> feature. (Note that a font may shape a Ra consonant with
1746                //    the feature generally but block it in certain contexts.)
1747                //
1748                // Note: We just check that something got substituted.  We don't check that
1749                // the <pref> feature actually did it...
1750                //
1751                // Reorder pref only if it ligated.
1752                if info.ligated_and_didnt_multiply() {
1753                    // 2. Try to find a target position the same way as for pre-base matra.
1754                    //    If it is found, reorder pre-base consonant glyph.
1755                    //
1756                    // 3. If position is not found, reorder immediately before main consonant.
1757
1758                    let mut new_pos = base;
1759                    // Malayalam / Tamil do not have "half" forms or explicit virama forms.
1760                    // The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1761                    // We want to position matra after them.
1762                    if buffer.script != Some(Script::MALAYALAM)
1763                        && buffer.script != Some(Script::TAMIL)
1764                    {
1765                        while new_pos > start
1766                            && !buffer.info.get(new_pos - 1)?.is_one_of(
1767                                rb_flag(category::OT_M as u32)
1768                                    | rb_flag(category::OT_MPst as u32)
1769                                    | rb_flag(category::OT_H as u32),
1770                            )
1771                        {
1772                            new_pos -= 1;
1773                        }
1774                    }
1775
1776                    if new_pos > start && buffer.info.get(new_pos - 1)?.is_halant() {
1777                        // -> If ZWJ or ZWNJ follow this halant, position is moved after it.
1778                        if new_pos < end && buffer.info.get(new_pos)?.is_joiner() {
1779                            new_pos += 1;
1780                        }
1781                    }
1782
1783                    {
1784                        let old_pos = i;
1785
1786                        buffer.merge_clusters(new_pos, old_pos + 1);
1787                        buffer.info.get_mut(new_pos..=old_pos)?.rotate_right(1);
1788
1789                        if new_pos <= base && base < old_pos {
1790                            // TODO: investigate
1791                            #[allow(unused_assignments)]
1792                            {
1793                                base += 1;
1794                            }
1795                        }
1796                    }
1797                }
1798
1799                break;
1800            }
1801        }
1802    }
1803
1804    // Apply 'init' to the Left Matra if it's a word start.
1805    if buffer.info.get(start)?.indic_position() == position::POS_PRE_M {
1806        if start == 0
1807            || !buffer
1808                .info
1809                .get(start - 1)?
1810                .general_category()
1811                .in_range_inclusive(GeneralCategory::FORMAT, GeneralCategory::NON_SPACING_MARK)
1812        {
1813            buffer.info.get_mut(start)?.mask |= indic_plan.mask_array[indic_feature::INIT];
1814        } else {
1815            buffer.unsafe_to_break(Some(start - 1), Some(start + 1));
1816        }
1817    }
1818
1819    Some(())
1820}