text_scripts

source of shaping::hangul

1// The Hangul shaper (hb-ot-shaper-hangul.cc): jamo composed into the2// syllable they spell where the face has its glyph, syllables decomposed3// into jamo where it has not (or a trailing jamo follows), the jamo then4// given `ljmo`, `vjmo` and `tjmo`; tone marks moved before their5// syllable when they take space.67import std::vector;8import std::option;910import pkg::alloy::text::font::reader;11import pkg::alloy::text::font::face;12import pkg::alloy::text::shaping::buffer;13import pkg::alloy::text::shaping::plan;1415const L_BASE: u32 = 0x1100;16const V_BASE: u32 = 0x1161;17const T_BASE: u32 = 0x11A7;18const L_COUNT: u32 = 19;19const V_COUNT: u32 = 21;20const T_COUNT: u32 = 28;21const S_BASE: u32 = 0xAC00;22const N_COUNT: u32 = V_COUNT * T_COUNT;23const S_COUNT: u32 = L_COUNT * N_COUNT;2425// The jamo's features, kept in a glyph's category until its mask is set.26const FEATURES: [&[u8] : 3] = [&"ljmo", &"vjmo", &"tjmo"];27const LJMO: u8 = 1;28const VJMO: u8 = 2;29const TJMO: u8 = 3;3031// What the shaper keeps in its plan: the masks of nothing and each jamo32// feature.33pub type HangulData = struct {34    pub masks: [u32 : 4],35};3637pub fn collect_hangul_features(b: &var Builder) {38    for (FEATURES) |&name| {39        b.add_feature(tag(&name), FeatureFlags {}, 1);40    }41}4243pub fn override_hangul_features(b: &var Builder) {44    b.add_feature(tag(&"calt"), FeatureFlags { .global = true }, 0);45}4647pub fn hangul_data(plan: &Plan) -> HangulData {48    var data = HangulData { .masks = [0, 0, 0, 0] };49    for (FEATURES, [1..4 to u64]) |&name, k| {50        data.masks[k] = plan.one_mask(tag(&name));51    }52    return data;53}5455pub fn setup_hangul_masks(data: &HangulData, buffer: &var Buffer) {56    for (buffer.infos.items()) |&var info| {57        info.mask |= data.masks[info.category to u64];58        info.category = 0;59    }60}6162fn is_combining_l(u: u32) -> bool => u >= L_BASE && u < L_BASE + L_COUNT;6364fn is_combining_v(u: u32) -> bool => u >= V_BASE && u < V_BASE + V_COUNT;6566fn is_combining_t(u: u32) -> bool => u > T_BASE && u < T_BASE + T_COUNT;6768fn is_combined_s(u: u32) -> bool => u >= S_BASE && u < S_BASE + S_COUNT;6970fn is_l(u: u32) -> bool => (u >= 0x1100 && u <= 0x115F) || (u >= 0xA960 && u <= 0xA97C);7172fn is_v(u: u32) -> bool => (u >= 0x1160 && u <= 0x11A7) || (u >= 0xD7B0 && u <= 0xD7C6);7374fn is_t(u: u32) -> bool => (u >= 0x11A8 && u <= 0x11FF) || (u >= 0xD7CB && u <= 0xD7FB);7576fn is_tone(u: u32) -> bool => u >= 0x302E && u <= 0x302F;7778fn has_glyph(face: &Face, u: u32) -> bool => face.glyph_of(u) != 0;7980fn is_zero_width(face: &Face, u: u32) -> bool {81    const glyph = face.glyph_of(u);82    return glyph != 0 && face.advance(glyph) == 0.0;83}8485// The glyphs read from `input` go into `out` as HarfBuzz's output buffer:86// `replace` puts characters in place of `count` of them, one cluster.87type Jamo = struct {88    input: Vector<GlyphInfo>,89    out: Vector<GlyphInfo>,90    at: u64,91};9293fn next(self j: &var Jamo) {94    j.out.push(j.input.get(j.at));95    j.at += 1;96}9798fn current(self j: &Jamo, ahead: u64) -> u32 => j.input.get(j.at + ahead).glyph;99100fn replace(self j: &var Jamo, count: u64, characters: &[u32]) {101    merge_clusters_of(&var j.input.items(), j.at, j.at + count);102    const original = j.input.get(j.at);103    for (characters) |c| {104        var info = original;105        info.glyph = c;106        j.out.push(info);107    }108    j.at += count;109}110111pub fn preprocess_hangul(face: &Face, buffer: &var Buffer) {112    const count = buffer.infos.length();113    var j = Jamo {114        .input = move buffer.infos,115        .out = Vector<GlyphInfo>::with_capacity(count + 4),116        .at = 0117    };118    // the syllable last seen119    var start: u64 = 0;120    var end: u64 = 0;121    while (j.at < count) {122        const u = j.current(0);123        if (is_tone(u)) {124            if (start < end && end == j.out.length()) {125                j.next();126                if (!is_zero_width(&face, u)) {127                    // the tone before its syllable128                    merge_clusters_of(&var j.out.items(), start, end + 1);129                    const infos = &var j.out.items();130                    const tone = infos[end];131                    var k = end;132                    while (k > start) {133                        infos[k] = infos[k - 1];134                        k -= 1;135                    }136                    infos[start] = tone;137                }138            } else if (has_glyph(&face, 0x25CC)) {139                const characters: [u32 : 2] = if (is_zero_width(140                    &face,141                    u142                )) [0x25CC, u] else [u, 0x25CC];143                j.replace(1, &characters);144            } else {145                j.next();146            }147            start = j.out.length();148            end = start;149            continue;150        }151        start = j.out.length();152        if (is_l(u) && j.at + 1 < count) {153            const l = u;154            const v = j.current(1);155            if (is_v(v)) {156                var t: u32 = 0;157                var t_index: u32 = 0;158                if (j.at + 2 < count) {159                    t = j.current(2);160                    if (is_t(t)) {161                        t_index = t - T_BASE;162                    } else {163                        t = 0;164                    }165                }166                const length: u64 = if (t != 0) 3 else 2;167                if (is_combining_l(l) && is_combining_v(v) && (t == 0 || is_combining_t(t))) {168                    const s = S_BASE + (l - L_BASE) * N_COUNT + (v - V_BASE) * T_COUNT + t_index;169                    if (has_glyph(&face, s)) {170                        const composed: [u32 : 1] = [s];171                        j.replace(length, &composed);172                        end = start + 1;173                        continue;174                    }175                }176                j.input.get_var(j.at).category = LJMO;177                j.next();178                j.input.get_var(j.at).category = VJMO;179                j.next();180                if (t != 0) {181                    j.input.get_var(j.at).category = TJMO;182                    j.next();183                }184                end = start + length;185                merge_clusters_of(&var j.out.items(), start, end);186                continue;187            }188        } else if (is_combined_s(u)) {189            const s = u;190            const s_has_glyph = has_glyph(&face, s);191            const l_index = (s - S_BASE) / N_COUNT;192            const n_index = (s - S_BASE) % N_COUNT;193            const v_index = n_index / T_COUNT;194            const t_index = n_index % T_COUNT;195            const trailing_next = t_index == 0 && j.at + 1 < count;196            if (trailing_next && is_combining_t(j.current(1))) {197                const new_s = s + (j.current(1) - T_BASE);198                if (has_glyph(&face, new_s)) {199                    const composed: [u32 : 1] = [new_s];200                    j.replace(2, &composed);201                    end = start + 1;202                    continue;203                }204            }205            if (!s_has_glyph || (trailing_next && is_t(j.current(1)))) {206                const decomposed: [u32 : 3] = [207                    L_BASE + l_index,208                    V_BASE + v_index,209                    T_BASE + t_index210                ];211                if (212                    has_glyph(&face, decomposed[0])213                    && has_glyph(&face, decomposed[1])214                    && (t_index == 0 || has_glyph(&face, decomposed[2]))215                ) {216                    var length: u64 = if (t_index != 0) 3 else 2;217                    j.replace(1, &decomposed[..length]);218                    if (s_has_glyph && t_index == 0) {219                        j.next();220                        length += 1;221                    }222                    end = start + length;223                    const infos = &var j.out.items();224                    infos[start].category = LJMO;225                    infos[start + 1].category = VJMO;226                    if (start + 2 < end) {227                        infos[start + 2].category = TJMO;228                    }229                    merge_clusters_of(&var j.out.items(), start, end);230                    continue;231                }232            }233            if (s_has_glyph) {234                end = start + 1;235            }236        }237        j.next();238    }239    buffer.infos = move j.out;240}