text_scripts

source of shaping::syllables

1// What the syllabic shapers share (hb-ot-shaper-syllabic.cc): their2// syllables found by a machine of Ragel's tables (machines.alloy), dotted3// circles inserted in broken clusters, and in vowel sequences that would4// look like another vowel.56import std::vector;7import std::option;89import pkg::alloy::text::font::face;10import pkg::alloy::text::unicode::tables;11import unicode::script_tables;12import pkg::alloy::text::shaping::buffer;13import machines;1415// Finds the syllables of the categories, as the machine's scanner does,16// giving each glyph its syllable's serial (1 to 15 in turn) in the high17// four bits and its kind below; `origins` the glyph each category is of18// (none for the glyphs in order: Indic's, Khmer's and Myanmar's), a19// syllable then taking every glyph from its first's to the next's.20// Whether it found a broken cluster of the kind `broken`.21pub fn find_syllables(22    m: &Machine,23    categories: &[u8],24    origins: &[u64],25    infos: &var [GlyphInfo],26    broken: u827) -> bool {28    var serial: u32 = 1;29    var found_broken = false;30    const pe = categories.length() to i64;31    var cs = m.start to u64;32    var p: i64 = 0;33    var ts: i64 = 0;34    var te: i64 = 0;35    var act: u8 = 0;36    var trans: u64 = 0;37    // where the scanner goes next: read a category, take the end of input's38    // transition, or stop39    var reading = p != pe;40    var at_end = p == pe;41    while (reading || at_end) {42        if (reading) {43            if (m.from_state[cs] != 0) {44                ts = p;45            }46            const c = categories[p to u64];47            const low = m.keys[cs * 2];48            const high = m.keys[cs * 2 + 1];49            const span = m.spans[cs] to u64;50            const k = if (span > 0 && low <= c && c <= high) (c - low) to u64 else span;51            trans = m.indices[m.offsets[cs] to u64 + k] to u64;52        } else {53            if (m.eof[cs] == 0) {54                break;55            }56            trans = m.eof[cs] to u64 - 1;57        }58        cs = m.targets[trans] to u64;59        const action = m.programs[m.actions[trans] to u64];60        var kind: Option<u8> = ::None;61        match (action.step) {62            ::Nothing {}63            ::Mark {64                te = p + 1;65            }66            ::Found {67                te = p + 1;68                kind = ::Some(action.value);69            }70            ::FoundBefore {71                te = p;72                p -= 1;73                kind = ::Some(action.value);74            }75            ::FoundBack {76                p = te - 1;77                kind = ::Some(action.value);78            }79            ::FoundByAct {80                p = te - 1;81                kind = ::Some(m.acts[act to u64]);82            }83            ::Act {84                te = p + 1;85                act = action.value;86            }87        }88        if (kind is ::Some |k|) {89            const first = origin(&origins, ts to u64, infos.length());90            const last = origin(&origins, te to u64, infos.length());91            for (infos[first..last]) |&var info| {92                info.syllable = ((serial << 4) to u8) | k;93            }94            serial += 1;95            if (serial == 16) {96                serial = 1;97            }98            found_broken = found_broken || k == broken;99        }100        if (m.to_state[cs] != 0) {101            ts = 0;102        }103        p += 1;104        reading = p < pe;105        at_end = p == pe;106    }107    return found_broken;108}109110fn origin(origins: &[u64], k: u64, end: u64) -> u64 {111    if (origins.length() == 0) {112        return k;113    }114    return if (k < origins.length()) origins[k] else end;115}116117// Inserts a dotted circle at the start of each broken cluster (after its118// repha, if any, of the category `repha`), with the category and position119// given, where the face has one (HarfBuzz's120// hb_syllabic_insert_dotted_circles): whether it inserted any.121pub fn insert_dotted_circles(122    face: &Face,123    buffer: &var Buffer,124    broken: u8,125    category: u8,126    repha: Option<u8>,127    position: u8128) -> bool {129    if (!buffer.has_broken_syllable) {130        return false;131    }132    const glyph = face.glyph_of(0x25CC);133    if (glyph == 0) {134        return false;135    }136    var out = Vector<GlyphInfo>::with_capacity(buffer.infos.length() + 4);137    var last: u8 = 0;138    var i: u64 = 0;139    const count = buffer.infos.length();140    while (i < count) {141        const info = buffer.infos.get(i);142        if (last != info.syllable && (info.syllable & 0x0F) == broken) {143            last = info.syllable;144            if (repha is ::Some |r|) {145                while (146                    i < count147                    && buffer.infos.get(i).syllable == last148                    && buffer.infos.get(i).category == r149                ) {150                    out.push(buffer.infos.get(i));151                    i += 1;152                }153            }154            out.push(GlyphInfo {155                .glyph = glyph,156                .cluster = info.cluster,157                .mask = info.mask,158                .syllable = info.syllable,159                .category = category,160                .position = position161            });162            continue;163        }164        out.push(info);165        i += 1;166    }167    buffer.infos = move out;168    return true;169}170171// Puts a dotted circle before the vowel sign of a sequence the script's172// writers would read as another vowel (IndicShapingInvalidCluster.txt,173// HarfBuzz's _hb_preprocess_text_vowel_constraints), copying the sign's174// glyph info but for its continuation.175pub fn constrain_vowels(buffer: &var Buffer) {176    const first = VOWEL_CONSTRAINTS[0];177    const last = VOWEL_CONSTRAINTS[VOWEL_CONSTRAINTS.length() - 4];178    const script = buffer.script to u32;179    var out = Vector<GlyphInfo>::empty();180    var changed = false;181    const count = buffer.infos.length();182    var i: u64 = 0;183    while (i + 1 < count) {184        const c = buffer.infos.get(i).glyph;185        var matched: u64 = 0; // the characters before the circle186        if (c >= first && c <= last) {187            matched = constraint_at(&buffer, i, script);188        }189        if (matched == 0) {190            if (changed) {191                out.push(buffer.infos.get(i));192            }193            i += 1;194            continue;195        }196        if (!changed) {197            changed = true;198            out = Vector<GlyphInfo>::with_capacity(count + 4);199            out.append(&buffer.infos.view()[..i]);200        }201        out.append(&buffer.infos.view()[i..i + matched]);202        var circle = buffer.infos.get(i + matched);203        circle.glyph = 0x25CC;204        circle.clear_continuation();205        out.push(circle);206        out.push(buffer.infos.get(i + matched));207        i += matched + 1;208    }209    if (changed) {210        out.append(&buffer.infos.view()[i..]);211        buffer.infos = move out;212    }213}214215// How many characters at `i` a constraint of the script matches before216// its last (0 for none).217fn constraint_at(buffer: &Buffer, i: u64, script: u32) -> u64 {218    const c = buffer.infos.get(i).glyph;219    const n = VOWEL_CONSTRAINTS.length() / 4;220    var low: u64 = 0;221    var high = n;222    while (low < high) {223        const middle = (low + high) / 2;224        if (VOWEL_CONSTRAINTS[middle * 4] < c) {225            low = middle + 1;226        } else {227            high = middle;228        }229    }230    const count = buffer.infos.length();231    while (low < n && VOWEL_CONSTRAINTS[low * 4] == c) {232        const k = low * 4;233        if (234            VOWEL_CONSTRAINTS[k + 3] == script235            && VOWEL_CONSTRAINTS[k + 1] == buffer.infos.get(i + 1).glyph236        ) {237            if (VOWEL_CONSTRAINTS[k + 2] == 0) {238                return 1;239            }240            if (i + 2 < count && VOWEL_CONSTRAINTS[k + 2] == buffer.infos.get(i + 2).glyph) {241                return 2;242            }243        }244        low += 1;245    }246    return 0;247}