ui_access

source of pkg::alloy::text::shaping::opentype

1// What GSUB and GPOS share: the script, language and feature lists that2// pick lookups, coverage and class tables, lookup flags and GDEF's glyph3// classes.45import std::vector;6import std::option;7import std::sort;89import font::reader;10import font::face;1112// GSUB's lookup types13exp const SINGLE_SUBSTITUTION: u32 = 1;14exp const MULTIPLE_SUBSTITUTION: u32 = 2;15exp const ALTERNATE_SUBSTITUTION: u32 = 3;16exp const LIGATURE_SUBSTITUTION: u32 = 4;17exp const CONTEXT_SUBSTITUTION: u32 = 5;18exp const CHAINED_CONTEXT_SUBSTITUTION: u32 = 6;19const EXTENSION_SUBSTITUTION: u32 = 7;20exp const REVERSE_CHAINED_SUBSTITUTION: u32 = 8;21// GPOS's lookup types22exp const SINGLE_ADJUSTMENT: u32 = 1;23exp const PAIR_ADJUSTMENT: u32 = 2;24exp const CURSIVE_ATTACHMENT_LOOKUP: u32 = 3;25exp const MARK_TO_BASE: u32 = 4;26exp const MARK_TO_LIGATURE: u32 = 5;27exp const MARK_TO_MARK: u32 = 6;28exp const CONTEXT_POSITIONING: u32 = 7;29exp const CHAINED_CONTEXT_POSITIONING: u32 = 8;30const EXTENSION_POSITIONING: u32 = 9;3132// The formats of a coverage table: a list of glyphs, ranges of glyphs.33type CoverageFormat = enum : u32 {34    Glyphs = 1,35    Ranges = 2,36};37// class definition formats: the classes of glyphs from a first one,38// ranges of glyphs39const CLASSES_ARRAY: u32 = 1;40const CLASSES_RANGES: u32 = 2;41// contextual subtable formats: rules by glyphs, rules by classes, one42// rule of coverage tables43exp const CONTEXT_GLYPHS: u32 = 1;44exp const CONTEXT_CLASSES: u32 = 2;45exp const CONTEXT_COVERAGES: u32 = 3;4647// The feature table the feature's index names: its record's, or the one48// the feature variation in force puts in its place.49exp fn feature_table(d: &[u8], feature_list: u64, variation: Option<u64>, index: u32) -> u64 {50    if (variation is ::Some |v|) {51        const count = u16_at(&d, v + 4) to u64;52        for ([..count]) |k| {53            const record = v + 6 + k * 6;54            if (u16_at(&d, record) == index) {55                return v + u32_at(&d, record + 2) to u64;56            }57        }58    }59    return feature_list + u16_at(&d, feature_list + 2 + index to u64 * 6 + 4) to u64;60}6162// The table a lookup is in, whose lookup types differ.63exp type LookupTable = enum {64    Substitution,65    Positioning,66};6768// The table's extension lookup type, whose subtables hold one of another.69fn extension(self table: &LookupTable) -> u32 => match (table) {70    ::Substitution EXTENSION_SUBSTITUTION;71    ::Positioning EXTENSION_POSITIONING;72};7374// A lookup's header: its type, the glyphs it sees and its subtables.75exp type Lookup = struct {76    exp kind: u32,77    exp flags: u32,78    exp filter: u32, // its mark filtering set79    exp count: u64, // its subtables80    at: u64,81};8283exp fn Lookup::read(d: &[u8], at: u64) -> Lookup {84    const flags = u16_at(&d, at + 2);85    const count = u16_at(&d, at + 4) to u64;86    return Lookup {87        .kind = u16_at(&d, at),88        .flags = flags,89        .filter = if ((flags & 0x10) != 0) u16_at(&d, at + 6 + count * 2) else 0,90        .count = count,91        .at = at92    };93}9495// The lookup's flags, its mark filtering set in the high half when it96// uses one (HarfBuzz's lookup props).97exp fn props(self l: &Lookup) -> u32 => if (98    (l.flags & 0x10) != 099) l.flags | (l.filter << 16) else l.flags;100101// Whether it is a reverse chained lookup, applied from the last glyph.102exp fn reverse(self l: &Lookup, d: &[u8]) -> bool {103    if (l.count == 0) {104        return false;105    }106    return l.subtable(&d, 0, LookupTable::Substitution).kind == REVERSE_CHAINED_SUBSTITUTION107        && (l.kind == REVERSE_CHAINED_SUBSTITUTION || l.kind == EXTENSION_SUBSTITUTION);108}109110// A lookup's subtable: its type and offset in the font.111exp type Subtable = struct {112    exp kind: u32,113    exp at: u64,114};115116// The lookup's subtable, through an extension subtable to the one it117// holds.118exp fn subtable(self l: &Lookup, d: &[u8], k: u64, table: LookupTable) -> Subtable {119    const at = l.at + u16_at(&d, l.at + 6 + k * 2) to u64;120    if (l.kind == table.extension()) {121        return Subtable { .kind = u16_at(&d, at + 2), .at = at + u32_at(&d, at + 4) to u64 };122    }123    return Subtable { .kind = l.kind, .at = at };124}125126// The offset of the table's lookup in the font, if it has the lookup.127exp fn lookup_at(d: &[u8], table: u64, index: u32) -> Option<u64> {128    const list = table + u16_at(&d, table + 8) to u64;129    if (index >= u16_at(&d, list)) {130        return ::None;131    }132    return ::Some(list + u16_at(&d, list + 2 + index to u64 * 2) to u64);133}134135// Which glyphs each lookup of a GSUB or GPOS table may apply at, a bit a136// glyph: those its subtables' first coverage tables cover, every glyph137// for a subtable of a type not known. Shaping passes over the rest at138// once. After the lookups' rows, where each lookup's subtables start139// among the digests, then each subtable's digest: three 64-bit masks of140// a covered glyph's bits 0 to 5, 4 to 9 and 9 to 14 (HarfBuzz's141// hb_set_digest_t), which most glyphs a subtable does not cover miss.142exp type LookupSets = struct {143    exp words: u64, // a lookup's 64-bit words144    exp count: u64,145    exp bits: Bits,146};147148const DIGEST_SHIFTS: [u32 : 3] = [0, 4, 9];149150// The sets' bits: their own, or those of the face an instance of a151// variable face shares them with.152exp type Bits = enum {153    Owned: Vector<u64>,154    Shared: &[u64],155};156157fn view(self bits: &Bits) -> &[u64] => match (bits) {158    ::Owned |&v| &v.view();159    ::Shared |&s| &s;160};161162// The sets of a face without the table.163exp fn LookupSets::none() -> LookupSets {164    return LookupSets { .words = 0, .count = 0, .bits = Bits::Owned(Vector<u64>::empty()) };165}166167// The same sets, reading these ones' bits, which must outlive them.168exp fn shared(self sets: &LookupSets) -> LookupSets {169    return LookupSets {170        .words = sets.words,171        .count = sets.count,172        .bits = Bits::Shared(&sets.bits.view())173    };174}175176exp fn LookupSets::of(177    d: &[u8],178    table: Option<u64>,179    glyph_count: u32,180    kind: LookupTable181) -> LookupSets {182    const t = if (table is ::Some |t|) t else {183        return LookupSets::none();184    };185    const list = t + u16_at(&d, t + 8) to u64;186    const words = (glyph_count to u64 + 63) / 64;187    const count = u16_at(&d, list) to u64;188    var bits = Vector<u64>::filled(0, count * words + count + 1);189    var digests = Vector<u64>::empty();190    for ([..count]) |l| {191        const lookup = Lookup::read(&d, list + u16_at(&d, list + 2 + l * 2) to u64);192        bits.get_var(count * words + l) = (digests.length() / 3) to u64;193        for ([..lookup.count]) |k| {194            const s = lookup.subtable(&d, k, kind);195            const coverage = first_coverage(&d, s.at, s.kind, kind);196            cover(&var bits.items()[l * words..(l + 1) * words], &d, coverage, glyph_count);197            const digest = digest_of(&d, coverage);198            digests.append(&digest[..]);199        }200    }201    bits.get_var(count * words + count) = (digests.length() / 3) to u64;202    bits.append(&digests.view());203    return LookupSets { .words = words, .count = count, .bits = Bits::Owned(move bits) };204}205206// The subtable's first coverage table, which the glyph a lookup is207// applied at must be in; None when the type is not known or the rule208// lists no glyph to start at.209fn first_coverage(d: &[u8], at: u64, kind: u32, table: LookupTable) -> Option<u64> {210    const contextual = match (kind) {211        CONTEXT_SUBSTITUTION, CHAINED_CONTEXT_SUBSTITUTION table is ::Substitution;212        CONTEXT_POSITIONING, CHAINED_CONTEXT_POSITIONING table is ::Positioning;213        else false;214    };215    if (contextual && u16_at(&d, at) == CONTEXT_COVERAGES) {216        if (kind == CONTEXT_SUBSTITUTION || kind == CONTEXT_POSITIONING) {217            return if (u16_at(&d, at + 2) == 0) ::None else ::Some(at + u16_at(&d, at + 6) to u64);218        }219        const input = at + 4 + u16_at(&d, at + 2) to u64 * 2;220        return if (u16_at(&d, input) == 0) ::None else ::Some(at + u16_at(&d, input + 2) to u64);221    }222    if (kind >= 1 && kind <= 8) {223        return ::Some(at + u16_at(&d, at + 2) to u64);224    }225    return ::None;226}227228// The digest of the glyphs a coverage table covers, every glyph's229// without one.230fn digest_of(d: &[u8], coverage: Option<u64>) -> [u64 : 3] {231    const every = ~(0 to u64);232    const at = if (coverage is ::Some |c|) c else {233        return [every, every, every];234    };235    var digest: [u64 : 3] = [0, 0, 0];236    const count = u16_at(&d, at + 2) to u64;237    const format = if (CoverageFormat::from(u16_at(&d, at)) is ::Some |f|) f else {238        return if (count == 0) digest else [every, every, every];239    };240    for ([..count]) |k| {241        var first: u32 = 0;242        var last: u32 = 0;243        match (format) {244            ::Glyphs {245                first = u16_at(&d, at + 4 + k * 2);246                last = first;247            }248            ::Ranges {249                first = u16_at(&d, at + 4 + k * 6);250                last = u16_at(&d, at + 6 + k * 6);251            }252        }253        for (DIGEST_SHIFTS, [..3 to u64]) |shift, m| {254            const a = first >> shift;255            const b = last >> shift;256            if (b < a || b - a >= 63) {257                digest[m] = every;258            } else {259                for ([a..b + 1]) |v| {260                    digest[m] |= 1 to u64 << (v & 63) to u64;261                }262            }263        }264    }265    return digest;266}267268// The digests of the lookup's subtables, three words each; none past the269// face's lookups.270exp fn digests(self sets: &LookupSets, lookup: u32, subtables: u64) -> &[u64] {271    const bits = &sets.bits.view();272    const l = lookup to u64;273    if (subtables < 2 || l >= sets.count) {274        return &bits[..0];275    }276    const base = sets.count * sets.words;277    const at = base + sets.count + 1 + bits[base + l] * 3;278    if (at + subtables * 3 > bits.length()) {279        return &bits[..0];280    }281    return &bits[at..at + subtables * 3];282}283284// Whether a digest (three words at `at`) may hold the glyph.285exp fn digest_has(digests: &[u64], at: u64, glyph: u32) -> bool => (286    (digests[at] >> (glyph & 63) to u64) & 1287) != 0288    && ((digests[at + 1] >> ((glyph >> 4) & 63) to u64) & 1) != 0289    && ((digests[at + 2] >> ((glyph >> 9) & 63) to u64) & 1) != 0;290291// Sets the bits of the glyphs the coverage table covers, or of every292// glyph without one.293fn cover(bits: &var [u64], d: &[u8], coverage: Option<u64>, glyph_count: u32) {294    const at = if (coverage is ::Some |c|) c else {295        for (bits) |&var w| {296            w = ~(0 to u64);297        }298        return;299    };300    const format = CoverageFormat::from(u16_at(&d, at));301    const count = u16_at(&d, at + 2) to u64;302    for ([..count]) |k| {303        var first: u32 = 0;304        var last: u32 = 0;305        if (format is ::Some |f|) {306            match (f) {307                ::Glyphs {308                    first = u16_at(&d, at + 4 + k * 2);309                    last = first;310                }311                ::Ranges {312                    first = u16_at(&d, at + 4 + k * 6);313                    last = u16_at(&d, at + 6 + k * 6);314                }315            }316        }317        var g = first;318        while (g <= last && g < glyph_count) {319            bits[(g / 64) to u64] |= 1 to u64 << (g % 64) to u64;320            g += 1;321        }322    }323}324325// The lookup's bits, a glyph each, none past the face's lookups.326exp fn row(self sets: &LookupSets, lookup: u32) -> &[u64] {327    const bits = &sets.bits.view();328    const start = lookup to u64 * sets.words;329    if (lookup to u64 >= sets.count || start + sets.words > bits.length()) {330        return &bits[..0];331    }332    return &bits[start..start + sets.words];333}334335// Whether the glyph's bit is set in a lookup's row.336exp fn has_glyph(row: &[u64], glyph: u32) -> bool {337    const word = (glyph / 64) to u64;338    return word < row.length() && ((row[word] >> (glyph % 64) to u64) & 1) != 0;339}340341// Whether the lookup may apply at the glyph.342exp fn may_apply(self sets: &LookupSets, lookup: u32, glyph: u32) -> bool {343    const bits = &sets.bits.view();344    const word = lookup to u64 * sets.words + (glyph / 64) to u64;345    if (lookup to u64 >= sets.count || word >= bits.length()) {346        return false;347    }348    return ((bits[word] >> (glyph % 64) to u64) & 1) != 0;349}350351// The glyph's index in the coverage table, if it covers it.352exp fn coverage(d: &[u8], at: u64, glyph: u32) -> Option<u32> {353    const format = if (CoverageFormat::from(u16_at(&d, at)) is ::Some |f|) f else {354        return ::None;355    };356    var low: u64 = 0;357    var high = u16_at(&d, at + 2) to u64;358    match (format) {359        ::Glyphs {360            while (low < high) {361                const middle = (low + high) / 2;362                const g = u16_at(&d, at + 4 + middle * 2);363                if (g < glyph) {364                    low = middle + 1;365                } else if (g > glyph) {366                    high = middle;367                } else {368                    return ::Some(middle to u32);369                }370            }371        }372        ::Ranges {373            while (low < high) {374                const middle = (low + high) / 2;375                const range = at + 4 + middle * 6;376                const first = u16_at(&d, range);377                const last = u16_at(&d, range + 2);378                if (last < glyph) {379                    low = middle + 1;380                } else if (first > glyph) {381                    high = middle;382                } else {383                    return ::Some(u16_at(&d, range + 4) + glyph - first);384                }385            }386        }387    }388    return ::None;389}390391// The glyph's class in the class definition table, 0 when unlisted or392// with no table (`at` 0).393exp fn class_of(d: &[u8], at: u64, glyph: u32) -> u32 {394    if (at == 0) {395        return 0;396    }397    match (u16_at(&d, at)) {398        CLASSES_ARRAY {399            const first = u16_at(&d, at + 2);400            const count = u16_at(&d, at + 4);401            if (glyph >= first && glyph < first + count) {402                return u16_at(&d, at + 6 + (glyph - first) to u64 * 2);403            }404        }405        CLASSES_RANGES {406            var low: u64 = 0;407            var high = u16_at(&d, at + 2) to u64;408            while (low < high) {409                const middle = (low + high) / 2;410                const range = at + 4 + middle * 6;411                if (u16_at(&d, range + 2) < glyph) {412                    low = middle + 1;413                } else if (u16_at(&d, range) > glyph) {414                    high = middle;415                } else {416                    return u16_at(&d, range + 4);417                }418            }419        }420    }421    return 0;422}423424// The offset of the table's (GSUB's or GPOS's) script record for the425// tag, if it has one.426exp fn find_script(d: &[u8], table: u64, tag: u32) -> Option<u64> {427    const scripts = table + u16_at(&d, table + 4) to u64;428    const count = u16_at(&d, scripts) to u64;429    for ([..count]) |k| {430        const record = scripts + 2 + k * 6;431        if (u32_at(&d, record) == tag) {432            return ::Some(scripts + u16_at(&d, record + 4) to u64);433        }434    }435    return ::None;436}437438// The script's language system for the tag, if it has one.439exp fn find_language(d: &[u8], script: u64, tag: u32) -> Option<u64> {440    const count = u16_at(&d, script + 2) to u64;441    for ([..count]) |k| {442        const record = script + 4 + k * 6;443        if (u32_at(&d, record) == tag) {444            return ::Some(script + u16_at(&d, record + 4) to u64);445        }446    }447    return ::None;448}449450// The script's default language system, if it has one.451exp fn default_language(d: &[u8], script: u64) -> Option<u64> {452    const offset = u16_at(&d, script) to u64;453    return if (offset == 0) ::None else ::Some(script + offset);454}455456// The tag of the table's feature.457exp fn feature_tag(d: &[u8], table: u64, index: u32) -> u32 {458    const list = table + u16_at(&d, table + 6) to u64;459    return u32_at(&d, list + 2 + index to u64 * 6);460}461462// How many features the table lists.463exp fn feature_count(d: &[u8], table: u64) -> u32 => u16_at(464    &d,465    table + u16_at(&d, table + 6) to u64466);467468// How many lookups the table has.469exp fn lookup_count(d: &[u8], table: u64) -> u32 => u16_at(470    &d,471    table + u16_at(&d, table + 8) to u64472);