text_scripts

source of pkg::alloy::text::shaping::context

1// Contextual lookups, GSUB's 5 and 6 and GPOS's 7 and 8: a sequence of2// glyphs matched by glyph, class or coverage, with what comes before and3// after it, then other lookups applied at positions within it.45import std::vector;6import std::option;78import font::reader;9import font::face;10import opentype;11import buffer;12import apply;1314// A rule's sequences and lookup records, read from `p`.15type Rule = struct {16    backtrack: Items,17    input: Items, // after its first item18    lookahead: Items,19    first: Option<u64>, // where the input's first item is written20    records: u64,21    record_count: u64,22};2324// Applies the contextual subtable at `at` (of either table, `chained`25// for a chained one) at the current glyph, `nested` applying the lookups26// its rule names: whether a rule matched.27exp fn apply_context(28    c: &var Apply,29    buffer: &var Buffer,30    at: u64,31    chained: bool,32    nested: (&var Apply, &var Buffer, u32) -> bool33) -> bool {34    const d = &c.data;35    const format = u16_at(&d, at);36    const glyph = buffer.infos.get(c.at).glyph;37    if (format == CONTEXT_COVERAGES) {38        // the input's first item is a coverage table, matched here39        const coverages = Naming::Coverages(at);40        const rule = read_rule(&d, at + 2, chained, [coverages, coverages, coverages]);41        if (42            rule.first is ::Some |first|43            && coverage(&d, at + u16_at(&d, first) to u64, glyph) is ::Some44        ) {45            return apply_rule(&var c, &var buffer, &rule, nested);46        }47        return false;48    }49    const covered = if (coverage(&d, at + u16_at(&d, at + 2) to u64, glyph) is ::Some |k|) k else {50        return false;51    };52    // the rule sets, and the one the glyph picks53    var naming: [Naming : 3] = [Naming::Glyphs, Naming::Glyphs, Naming::Glyphs];54    var sets: u64 = 0;55    var set_index: u64 = 0;56    match (format) {57        CONTEXT_GLYPHS {58            sets = at + 4;59            set_index = covered to u64;60        }61        CONTEXT_CLASSES {62            // the backtrack's, input's and lookahead's classes63            var classes: [u64 : 3] = [0, 0, 0];64            if (chained) {65                classes = [66                    offset_or_zero(&d, at, at + 4),67                    offset_or_zero(&d, at, at + 6),68                    offset_or_zero(&d, at, at + 8)69                ];70                sets = at + 10;71            } else {72                classes[1] = offset_or_zero(&d, at, at + 4);73                sets = at + 6;74            }75            naming = [76                Naming::Classes(classes[0]),77                Naming::Classes(classes[1]),78                Naming::Classes(classes[2])79            ];80            set_index = class_of(&d, classes[1], glyph) to u64;81        }82        else {83            return false;84        }85    }86    if (set_index >= u16_at(&d, sets) to u64) {87        return false;88    }89    const set_offset = u16_at(&d, sets + 2 + set_index * 2) to u64;90    if (set_offset == 0) {91        return false;92    }93    const set = at + set_offset;94    const rules = u16_at(&d, set) to u64;95    for ([..rules]) |r| {96        const rule = read_rule(&d, set + u16_at(&d, set + 2 + r * 2) to u64, chained, naming);97        if (apply_rule(&var c, &var buffer, &rule, nested)) {98            return true;99        }100    }101    return false;102}103104// Matches the rule at the current glyph, applying its lookups when it105// does (HarfBuzz's context_apply_lookup and chain_context_apply_lookup).106fn apply_rule(107    c: &var Apply,108    buffer: &var Buffer,109    rule: &Rule,110    nested: (&var Apply, &var Buffer, u32) -> bool111) -> bool {112    const end = if (c.match_input(&buffer, &rule.input) is ::Some |m|) m[0] else {113        return false;114    };115    if (116        !c.match_lookahead(&buffer, &rule.lookahead, end)117        || !c.match_backtrack(&buffer, &rule.backtrack)118    ) {119        return false;120    }121    c.apply_records(&var buffer, rule.records, rule.record_count, end, nested);122    return true;123}124125// Reads the rule at `p`: a chained one writes its backtrack, input and126// lookahead, each after its count, then its records' count; another its127// input's count, its records' count, then its input. A format 3 rule128// (one naming coverages) writes the input's first item, the others leave129// it to the subtable's coverage.130fn read_rule(d: &[u8], p: u64, chained: bool, naming: [Naming : 3]) -> Rule {131    var rule = Rule {132        .backtrack = Items { .naming = naming[0], .at = 0, .count = 0 },133        .input = Items { .naming = naming[1], .at = 0, .count = 0 },134        .lookahead = Items { .naming = naming[2], .at = 0, .count = 0 },135        .first = ::None,136        .records = 0,137        .record_count = 0138    };139    var at = p;140    if (chained) {141        rule.backtrack.count = u16_at(&d, at) to u64;142        rule.backtrack.at = at + 2;143        at += 2 + rule.backtrack.count * 2;144    }145    const input_count = u16_at(&d, at) to u64;146    at += 2;147    if (!chained) {148        rule.record_count = u16_at(&d, at) to u64;149        at += 2;150    }151    if (naming[1] is ::Coverages && input_count > 0) {152        rule.first = ::Some(at);153        at += 2;154    }155    rule.input.at = at;156    rule.input.count = if (input_count == 0) 0 else input_count - 1;157    at += rule.input.count * 2;158    if (chained) {159        rule.lookahead.count = u16_at(&d, at) to u64;160        rule.lookahead.at = at + 2;161        at += 2 + rule.lookahead.count * 2;162        rule.record_count = u16_at(&d, at) to u64;163        at += 2;164    }165    rule.records = at;166    return rule;167}168169fn offset_or_zero(d: &[u8], base: u64, at: u64) -> u64 {170    const offset = u16_at(&d, at) to u64;171    return if (offset == 0) 0 else base + offset;172}