text_scripts

source of shaping::myanmar

1// The Myanmar shaper (hb-ot-shaper-myanmar.cc), for faces made for the2// `mym2` specification: syllables found by its machine, each sorted by3// the positions its parts take around the base (kinzi after the base,4// medial Ra and pre-base vowels before it).56import std::vector;7import std::option;89import pkg::alloy::text::font::reader;10import pkg::alloy::text::font::face;11import pkg::alloy::text::unicode::tables;12import unicode::script_tables;13import pkg::alloy::text::unicode::properties;14import unicode::script_properties;15import pkg::alloy::text::shaping::buffer;16import pkg::alloy::text::shaping::plan;17import pkg::alloy::text::shaping::shapers;18import machines;19import syllables;2021const BASIC: [&[u8] : 4] = [&"rphf", &"pref", &"blwf", &"pstf"];22const OTHER: [&[u8] : 4] = [&"pres", &"abvs", &"blws", &"psts"];2324// The categories it reads, as bytes25const A: u8 = 9;26const AS: u8 = 32;27const C: u8 = 1;28const CS: u8 = 18;29const DOTTED_CIRCLE: u8 = 11;30const GB: u8 = 10;31const H: u8 = 4;32const IV: u8 = 2;33const MR: u8 = 36;34const RA: u8 = 15;35const VBLW: u8 = 21;36const VPRE: u8 = 22;37const VS: u8 = 40;3839// The positions as bytes40const POS_PRE_M: u8 = 2;41const POS_PRE_C: u8 = 3;42const POS_BASE_C: u8 = 4;43const POS_AFTER_MAIN: u8 = 5;44const POS_BEFORE_SUB: u8 = 7;45const POS_BELOW_C: u8 = 8;46const POS_AFTER_SUB: u8 = 9;4748pub fn collect_myanmar_features(b: &var Builder) {49    b.add_gsub_pause(Pause::MyanmarSyllables);50    b.enable_feature(tag(&"locl"), FeatureFlags { .per_syllable = true });51    b.enable_feature(tag(&"ccmp"), FeatureFlags { .per_syllable = true });52    b.add_gsub_pause(Pause::MyanmarReordering);53    for (BASIC) |&name| {54        b.enable_feature(tag(&name), FeatureFlags { .manual_zwj = true, .per_syllable = true });55        b.add_gsub_pause(Pause::Nothing);56    }57    b.add_gsub_pause(Pause::Nothing);58    for (OTHER) |&name| {59        b.enable_feature(tag(&name), FeatureFlags { .manual_zwj = true });60    }61}6263pub fn setup_myanmar_masks(buffer: &var Buffer) {64    for (buffer.infos.items()) |&var info| {65        info.category = indic_properties(info.glyph).category to u8;66    }67}6869pub fn find_myanmar_syllables(buffer: &var Buffer) {70    var categories = Vector<u8>::with_capacity(buffer.infos.length());71    for (buffer.infos.view()) |&info| {72        categories.push(info.category);73    }74    const none: [u64 : 0] = [];75    if (76        find_syllables(77            &MYANMAR_MACHINE,78            &categories.view(),79            &none,80            &var buffer.infos.items(),81            MyanmarSyllable::BrokenCluster to u882        )83    ) {84        buffer.has_broken_syllable = true;85    }86}8788pub fn reorder_myanmar(face: &Face, buffer: &var Buffer) {89    insert_dotted_circles(90        &face,91        &var buffer,92        MyanmarSyllable::BrokenCluster to u8,93        DOTTED_CIRCLE,94        ::None,95        096    );97    var start: u64 = 0;98    while (start < buffer.infos.length()) {99        const end = buffer.syllable_end(start);100        if ((buffer.infos.get(start).syllable & 0x0F) != MyanmarSyllable::NonMyanmarCluster to u8) {101            reorder_syllable(&var buffer, start, end);102        }103        start = end;104    }105}106107fn is_consonant(info: &GlyphInfo) -> bool => !info.ligated() && match (info.category) {108    C, CS, RA, IV, GB, DOTTED_CIRCLE true;109    else false;110};111112fn reorder_syllable(buffer: &var Buffer, start: u64, end: u64) {113    // the base: the first consonant, after a kinzi (Ra, Asat, H)114    var base = end;115    var has_reph = false;116    var limit = start;117    {118        const infos = &buffer.infos.view();119        if (120            start + 3 <= end121            && infos[start].category == RA122            && infos[start + 1].category == AS123            && infos[start + 2].category == H124        ) {125            limit += 3;126            base = start;127            has_reph = true;128        }129        if (!has_reph) {130            base = limit;131        }132        for ([limit..end]) |i| {133            if (is_consonant(&infos[i])) {134                base = i;135                break;136            }137        }138    }139    // the positions140    {141        const infos = &var buffer.infos.items();142        var i = start;143        const kinzi_end = if (has_reph) start + 3 else start;144        while (i < kinzi_end) {145            infos[i].position = POS_AFTER_MAIN;146            i += 1;147        }148        while (i < base) {149            infos[i].position = POS_PRE_C;150            i += 1;151        }152        if (i < end) {153            infos[i].position = POS_BASE_C;154            i += 1;155        }156        var position = POS_AFTER_MAIN;157        while (i < end) {158            const category = infos[i].category;159            if (category == MR) {160                infos[i].position = POS_PRE_C; // pre-base reordering161            } else if (category == VPRE) {162                infos[i].position = POS_PRE_M; // a left matra163            } else if (category == VS) {164                infos[i].position = infos[i - 1].position;165            } else if (position == POS_AFTER_MAIN && category == VBLW) {166                position = POS_BELOW_C;167                infos[i].position = position;168            } else if (position == POS_BELOW_C && category == A) {169                infos[i].position = POS_BEFORE_SUB;170            } else if (position == POS_BELOW_C && category == VBLW) {171                infos[i].position = position;172            } else if (position == POS_BELOW_C) {173                position = POS_AFTER_SUB;174                infos[i].position = position;175            } else {176                infos[i].position = position;177            }178            i += 1;179        }180    }181    buffer.sort(start, end, (a, b) => a.position < b.position);182    // a sequence of left matras flipped183    var first_left = end;184    var last_left = end;185    for ([start..end]) |i| {186        if (buffer.infos.get(i).position == POS_PRE_M) {187            if (first_left == end) {188                first_left = i;189            }190            last_left = i;191        }192    }193    if (first_left < last_left) {194        buffer.reverse_range(first_left, last_left + 1);195        var i = first_left;196        for ([first_left..last_left + 1]) |j| {197            if (buffer.infos.get(j).category == VPRE) {198                buffer.reverse_range(i, j + 1);199                i = j + 1;200            }201        }202    }203}