ui

source of pkg::alloy::text::shaping::gsub

1// Glyph substitution (GSUB): single, multiple, alternate, ligature,2// contextual, chained contextual and reverse chained lookups, through3// extensions, applied as HarfBuzz applies them.45import std::vector;6import std::option;7import std::math;89import font::reader;10import font::face;11import opentype;12import buffer;13import apply;14import context;1516// Applies the lookup to every glyph its mask selects, from the first (a17// reverse chained one from the last).18exp fn substitute_lookup(c: &var Apply, buffer: &var Buffer, index: u32) {19    const d = &c.data;20    const table = if (c.face.gsub is ::Some |t|) t else {21        return;22    };23    const at = if (lookup_at(&d, table, index) is ::Some |a|) a else {24        return;25    };26    const lookup = Lookup::read(&d, at);27    c.lookup_index = index;28    c.lookup_props = lookup.props();29    c.last_base = ::None;30    c.last_base_until = 0;31    const row = &c.face.gsub_sets.row(index);32    if (lookup.reverse(&d)) {33        var i = buffer.infos.length();34        while (i > 0) {35            i -= 1;36            const info = &buffer.infos.get(i);37            if (38                has_glyph(&row, info.glyph)39                && (info.mask & c.lookup_mask) != 040                && c.sees(&info, c.lookup_props)41            ) {42                c.at = i;43                apply_subtables(&var c, &var buffer, &lookup);44            }45        }46        return;47    }48    c.at = 0;49    while (c.at < buffer.infos.length()) {50        const info = &buffer.infos.get(c.at);51        if (52            has_glyph(&row, info.glyph)53            && (info.mask & c.lookup_mask) != 054            && c.sees(&info, c.lookup_props)55            && apply_subtables(&var c, &var buffer, &lookup)56        ) {57            continue;58        }59        c.at += 1;60    }61}6263// Applies the lookup at `at` in `c.data` (not the face's: one made while64// shaping, as Arabic's fallback makes) to every glyph its mask selects.65exp fn substitute_made_lookup(c: &var Apply, buffer: &var Buffer, at: u64) {66    const lookup = Lookup::read(&c.data, at);67    c.lookup_props = lookup.props();68    c.at = 0;69    while (c.at < buffer.infos.length()) {70        const info = &buffer.infos.get(c.at);71        if (72            (info.mask & c.lookup_mask) != 073            && c.sees(&info, c.lookup_props)74            && apply_subtables(&var c, &var buffer, &lookup)75        ) {76            continue;77        }78        c.at += 1;79    }80}8182// Applies the lookup at the current glyph, as a contextual rule asks:83// whether it did.84fn nested(c: &var Apply, buffer: &var Buffer, index: u32) -> bool {85    const d = &c.data;86    const table = if (c.face.gsub is ::Some |t|) t else {87        return false;88    };89    const at = if (lookup_at(&d, table, index) is ::Some |a|) a else {90        return false;91    };92    const lookup = Lookup::read(&d, at);93    const saved_index = c.lookup_index;94    const saved_props = c.lookup_props;95    c.lookup_index = index;96    c.lookup_props = lookup.props();97    const applied = c.face.gsub_sets.may_apply(index, buffer.infos.get(c.at).glyph)98        && apply_subtables(&var c, &var buffer, &lookup);99    c.lookup_index = saved_index;100    c.lookup_props = saved_props;101    return applied;102}103104// The first of the lookup's subtables that applies at the current glyph.105fn apply_subtables(c: &var Apply, buffer: &var Buffer, lookup: &Lookup) -> bool {106    const d = &c.data;107    // the subtables' digests passing over those that cannot apply, but a108    // lookup's one subtable, which its row has checked109    const digests = &c.face.gsub_sets.digests(c.lookup_index, if (c.digests) lookup.count else 0);110    for ([..lookup.count]) |k| {111        if (112            k * 3 < digests.length()113            && !digest_has(&digests, k * 3, buffer.infos.get(c.at).glyph)114        ) {115            continue;116        }117        const s = lookup.subtable(&d, k, LookupTable::Substitution);118        if (apply_subtable(&var c, &var buffer, s.kind, s.at)) {119            return true;120        }121    }122    return false;123}124125fn apply_subtable(c: &var Apply, buffer: &var Buffer, kind: u32, at: u64) -> bool {126    match (kind) {127        CONTEXT_SUBSTITUTION, CHAINED_CONTEXT_SUBSTITUTION {128            return apply_context(129                &var c,130                &var buffer,131                at,132                kind == CHAINED_CONTEXT_SUBSTITUTION,133                nested134            );135        }136    }137    const d = &c.data;138    const glyph = buffer.infos.get(c.at).glyph;139    const index = if (140        coverage(&d, at + u16_at(&d, at + 2) to u64, glyph) is ::Some |k|141    ) k to u64 else {142        return false;143    };144    match (kind) {145        SINGLE_SUBSTITUTION {146            // format 1 a delta, 2 the glyphs147            if (u16_at(&d, at) == 1) {148                c.replace_glyph(&var buffer, (glyph + u16_at(&d, at + 4)) & 0xFFFF);149            } else {150                if (index >= u16_at(&d, at + 4) to u64) {151                    return false;152                }153                c.replace_glyph(&var buffer, u16_at(&d, at + 6 + index * 2));154            }155            return true;156        }157        MULTIPLE_SUBSTITUTION {158            if (index >= u16_at(&d, at + 4) to u64) {159                return false;160            }161            const sequence = at + u16_at(&d, at + 6 + index * 2) to u64;162            substitute_sequence(&var c, &var buffer, sequence);163            return true;164        }165        ALTERNATE_SUBSTITUTION {166            if (index >= u16_at(&d, at + 4) to u64) {167                return false;168            }169            const set = at + u16_at(&d, at + 6 + index * 2) to u64;170            return substitute_alternate(&var c, &var buffer, set);171        }172        LIGATURE_SUBSTITUTION {173            if (index >= u16_at(&d, at + 4) to u64) {174                return false;175            }176            const set = at + u16_at(&d, at + 6 + index * 2) to u64;177            return substitute_ligature(&var c, &var buffer, set);178        }179        REVERSE_CHAINED_SUBSTITUTION {180            return substitute_reverse(&var c, &var buffer, at, index);181        }182    }183    return false;184}185186// The current glyph replaced by the sequence's, none removing it, one187// replacing it, more each the glyph's component in turn.188fn substitute_sequence(c: &var Apply, buffer: &var Buffer, sequence: u64) {189    const d = &c.data;190    const n = u16_at(&d, sequence) to u64;191    if (n == 1) {192        c.replace_glyph(&var buffer, u16_at(&d, sequence + 2));193        return;194    }195    if (n == 0) {196        buffer.delete_glyph(c.at);197        return;198    }199    const current = buffer.infos.get(c.at);200    const class: u16 = if (current.is_ligature()) BASE_GLYPH else 0;201    const id = current.ligature_id();202    for ([1..n]) {203        buffer.infos.insert(c.at + 1, current);204    }205    for ([..n]) |k| {206        if (id == 0) {207            buffer.infos.get_var(c.at + k).set_component(k to u32);208        }209        c.set_glyph(&var buffer, c.at + k, u16_at(&d, sequence + 2 + k * 2), class, false, true);210    }211    c.at += n;212}213214// One of the alternates, as the feature's value picks it (1 the first).215fn substitute_alternate(c: &var Apply, buffer: &var Buffer, set: u64) -> bool {216    const d = &c.data;217    const count = u16_at(&d, set) to u32;218    if (count == 0) {219        return false;220    }221    const mask = c.lookup_mask;222    var shift: u32 = 0;223    while (shift < 31 && ((mask >> shift) & 1) == 0) {224        shift += 1;225    }226    var alternate = (mask & buffer.infos.get(c.at).mask) >> shift;227    if (alternate == 255 && c.random) {228        // the minimal standard generator as HarfBuzz computes it, in 32 bits229        c.random_state = (c.random_state *% 48271) % 2147483647;230        alternate = c.random_state % count + 1;231    }232    if (alternate > count || alternate == 0) {233        return false;234    }235    c.replace_glyph(&var buffer, u16_at(&d, set + 2 + (alternate - 1) to u64 * 2));236    return true;237}238239// The first ligature of the set whose components follow the current240// glyph replaces them.241fn substitute_ligature(c: &var Apply, buffer: &var Buffer, set: u64) -> bool {242    const d = &c.data;243    const ligatures = u16_at(&d, set) to u64;244    for ([..ligatures]) |l| {245        const ligature = set + u16_at(&d, set + 2 + l * 2) to u64;246        const components = u16_at(&d, ligature + 2) to u64;247        if (components == 0) {248            continue; // a broken table's249        }250        const glyph = u16_at(&d, ligature);251        if (components == 1) {252            c.replace_glyph(&var buffer, glyph);253            return true;254        }255        // most ligatures fail at once, on the glyph after this one256        const next = c.at + 1;257        if (next < buffer.infos.length()) {258            const n = &buffer.infos.get(next);259            if (260                n.glyph != u16_at(&d, ligature + 4)261                && (n.unicode & 0x20) == 0262                && c.sees(&n, c.lookup_props)263            ) {264                continue;265            }266        }267        const input = Items {268            .naming = Naming::Glyphs,269            .at = ligature + 4,270            .count = components - 1271        };272        if (c.match_input(&buffer, &input) is ::Some |m|) {273            c.ligate(&var buffer, m[0], glyph, m[1]);274            return true;275        }276    }277    return false;278}279280// Replaces the current glyph when what is before and after it matches,281// never from within a contextual rule.282fn substitute_reverse(c: &var Apply, buffer: &var Buffer, at: u64, index: u64) -> bool {283    if (c.nesting_left != MAX_NESTING) {284        return false;285    }286    const d = &c.data;287    const backtrack_count = u16_at(&d, at + 4) to u64;288    const lookahead = at + 6 + backtrack_count * 2;289    const lookahead_count = u16_at(&d, lookahead) to u64;290    const substitutes = lookahead + 2 + lookahead_count * 2;291    if (index >= u16_at(&d, substitutes) to u64) {292        return false;293    }294    const backtrack_items = Items {295        .naming = Naming::Coverages(at),296        .at = at + 6,297        .count = backtrack_count298    };299    const lookahead_items = Items {300        .naming = Naming::Coverages(at),301        .at = lookahead + 2,302        .count = lookahead_count303    };304    if (305        c.match_backtrack(&buffer, &backtrack_items)306        && c.match_lookahead(&buffer, &lookahead_items, c.at + 1)307    ) {308        c.set_glyph(&var buffer, c.at, u16_at(&d, substitutes + 2 + index * 2), 0, false, false);309        return true;310    }311    return false;312}313314// Whether the lookup would substitute the glyphs, in sequence with315// nothing around them (HarfBuzz's would_apply, for the Indic shaper's316// questions to a face).317exp fn would_substitute(face: &Face, index: u32, glyphs: &[u32], zero_context: bool) -> bool {318    const d = &face.data();319    const table = if (face.gsub is ::Some |t|) t else {320        return false;321    };322    const at = if (lookup_at(&d, table, index) is ::Some |a|) a else {323        return false;324    };325    if (glyphs.length() == 0 || !face.gsub_sets.may_apply(index, glyphs[0])) {326        return false;327    }328    const lookup = Lookup::read(&d, at);329    for ([..lookup.count]) |k| {330        const s = lookup.subtable(&d, k, LookupTable::Substitution);331        if (would_apply(&d, s.kind, s.at, &glyphs, zero_context)) {332            return true;333        }334    }335    return false;336}337338fn would_apply(d: &[u8], kind: u32, at: u64, glyphs: &[u32], zero_context: bool) -> bool {339    const index = if (340        first_coverage_index(&d, kind, at, glyphs[0]) is ::Some |k|341    ) k else {342        return false;343    };344    match (kind) {345        SINGLE_SUBSTITUTION, MULTIPLE_SUBSTITUTION, ALTERNATE_SUBSTITUTION {346            return glyphs.length() == 1;347        }348        LIGATURE_SUBSTITUTION {349            const set = at + u16_at(&d, at + 6 + index * 2) to u64;350            const ligatures = u16_at(&d, set) to u64;351            for ([..ligatures]) |l| {352                const ligature = set + u16_at(&d, set + 2 + l * 2) to u64;353                const components = u16_at(&d, ligature + 2) to u64;354                if (components != glyphs.length()) {355                    continue;356                }357                var same = true;358                for ([1..components]) |m| {359                    same = same && u16_at(&d, ligature + 4 + (m - 1) * 2) == glyphs[m];360                }361                if (same) {362                    return true;363                }364            }365            return false;366        }367        CONTEXT_SUBSTITUTION, CHAINED_CONTEXT_SUBSTITUTION {368            return would_match_context(369                &d,370                at,371                kind == CHAINED_CONTEXT_SUBSTITUTION,372                &glyphs,373                zero_context374            );375        }376        REVERSE_CHAINED_SUBSTITUTION {377            const backtrack_count = u16_at(&d, at + 4) to u64;378            const lookahead_count = u16_at(&d, at + 6 + backtrack_count * 2) to u64;379            return glyphs.length() == 1380                && (!zero_context || (backtrack_count == 0 && lookahead_count == 0));381        }382    }383    return false;384}385386// The current glyph's index in the subtable's first coverage.387fn first_coverage_index(d: &[u8], kind: u32, at: u64, glyph: u32) -> Option<u64> {388    const contextual = kind == CONTEXT_SUBSTITUTION || kind == CHAINED_CONTEXT_SUBSTITUTION;389    if (contextual && u16_at(&d, at) == CONTEXT_COVERAGES) {390        const first = if (kind == CONTEXT_SUBSTITUTION) at + 6 else at + 4 + u16_at(391            &d,392            at + 2393        ) to u64 * 2 + 2;394        if (kind == CONTEXT_SUBSTITUTION && u16_at(&d, at + 2) == 0) {395            return ::None;396        }397        if (coverage(&d, at + u16_at(&d, first) to u64, glyph) is ::Some |k|) {398            return ::Some(k to u64);399        }400        return ::None;401    }402    if (coverage(&d, at + u16_at(&d, at + 2) to u64, glyph) is ::Some |k|) {403        return ::Some(k to u64);404    }405    return ::None;406}407408// Whether a rule of the contextual subtable matches the glyphs exactly as409// its input, with no backtrack nor lookahead when `zero_context`.410fn would_match_context(411    d: &[u8],412    at: u64,413    chained: bool,414    glyphs: &[u32],415    zero_context: bool416) -> bool {417    const format = u16_at(&d, at);418    if (format == CONTEXT_COVERAGES) {419        var p = at + 2;420        var backtrack: u64 = 0;421        if (chained) {422            backtrack = u16_at(&d, p) to u64;423            p += 2 + backtrack * 2;424        }425        const input = u16_at(&d, p) to u64;426        if (input != glyphs.length()) {427            return false;428        }429        var lookahead: u64 = 0;430        if (chained) {431            lookahead = u16_at(&d, p + 2 + input * 2) to u64;432        }433        if (zero_context && (backtrack != 0 || lookahead != 0)) {434            return false;435        }436        const first = if (chained) p + 2 else p + 4;437        for ([1..input]) |k| {438            if (coverage(&d, at + u16_at(&d, first + k * 2) to u64, glyphs[k]) is ::None) {439                return false;440            }441        }442        return true;443    }444    var sets: u64 = 0;445    var classes: u64 = 0;446    var set_index: u64 = 0;447    if (format == CONTEXT_GLYPHS) {448        sets = at + 4;449        set_index = if (450            coverage(&d, at + u16_at(&d, at + 2) to u64, glyphs[0]) is ::Some |k|451        ) k to u64 else {452            return false;453        };454    } else if (format == CONTEXT_CLASSES) {455        const offset = u16_at(&d, at + if (chained) 6 else 4) to u64;456        classes = if (offset == 0) 0 else at + offset;457        sets = at + if (chained) 10 else 6;458        set_index = class_of(&d, classes, glyphs[0]) to u64;459    } else {460        return false;461    }462    if (set_index >= u16_at(&d, sets) to u64 || u16_at(&d, sets + 2 + set_index * 2) == 0) {463        return false;464    }465    const set = at + u16_at(&d, sets + 2 + set_index * 2) to u64;466    for ([..u16_at(&d, set) to u64]) |r| {467        var p = set + u16_at(&d, set + 2 + r * 2) to u64;468        var backtrack: u64 = 0;469        if (chained) {470            backtrack = u16_at(&d, p) to u64;471            p += 2 + backtrack * 2;472        }473        const input = u16_at(&d, p) to u64;474        p += if (chained) 2 else 4;475        if (input != glyphs.length()) {476            continue;477        }478        const lookahead = if (chained) u16_at(&d, p + (input - 1) * 2) to u64 else 0;479        if (zero_context && (backtrack != 0 || lookahead != 0)) {480            continue;481        }482        var same = true;483        for ([1..input]) |k| {484            const value = u16_at(&d, p + (k - 1) * 2);485            same = same && (if (format == CONTEXT_GLYPHS) value == glyphs[k] else class_of(486                &d,487                classes,488                glyphs[k]489            ) == value);490        }491        if (same) {492            return true;493        }494    }495    return false;496}