Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-fonts

Source of release 0.7.2

0.7.2 / src / shaping / layout.lucb

#==============================================================================================
#
#   layout - OpenType layout for the shaper: GDEF classes, the feature plan and GSUB
#
#   DESCRIPTION:
#       The part of HarfBuzz's hb-ot-layout and hb-ot-map a default (Latin-style) shaping plan uses:
#
#       - the plan: the script's default language system (the text's script, else DFLT, dflt,
#         latn), the features HarfBuzz enables by default (rvrn in a stage of its own, then
#         ltra/ltrm or rtla/rtlm, abvm, blwm, ccmp, locl, mark, mkmk, rlig, calt, clig, curs,
#         dist, kern, liga, rclt) overridden by the font's features, and the lookups of the
#         chosen features applied in lookup-list order;
#       - GSUB lookups 1 (single), 2 (multiple), 3 (alternate), 4 (ligature), 5 and 6
#         (contextual, in context.lucb) and 7 (extension);
#       - lookup flags (ignore base/ligature/mark, mark attachment type, mark filtering sets).
#
#       GPOS and the kern table are in position.lucb.
#
#       FIXME: reverse chaining substitution (GSUB 8) and FeatureVariations are not applied.
#
#==============================================================================================

# mark: Coverage and class definitions ===============================================================

## The coverage index of `glyph` in the Coverage table at `offset` of `table`, or none.
func ot_coverage_index(table: const u8[], offset: usize, glyph: u32) -> usize?:
    let coverage_format = opentype.sfnt_u16(table, offset)
    if coverage_format == 1:
        let count = (usize)opentype.sfnt_u16(table, offset + 2)
        var low: usize = 0
        var high = count
        while low < high:
            let middle = low + (high - low) // 2
            let candidate = (u32)opentype.sfnt_u16(table, offset + 4 + middle * 2)
            if glyph < candidate:
                high = middle
            elif glyph > candidate:
                low = middle + 1
            else:
                return middle
        return none
    if coverage_format == 2:
        let count = (usize)opentype.sfnt_u16(table, offset + 2)
        var low: usize = 0
        var high = count
        while low < high:
            let middle = low + (high - low) // 2
            let record = offset + 4 + middle * 6
            let start = (u32)opentype.sfnt_u16(table, record)
            let end = (u32)opentype.sfnt_u16(table, record + 2)
            if glyph < start:
                high = middle
            elif glyph > end:
                low = middle + 1
            else:
                return (usize)opentype.sfnt_u16(table, record + 4) + (usize)(glyph - start)
        return none
    return none

## The class of `glyph` in the ClassDef table at `offset` of `table` (0 when unlisted).
func ot_class_of(table: const u8[], offset: usize, glyph: u32) -> u16:
    let class_format = opentype.sfnt_u16(table, offset)
    if class_format == 1:
        let start = (u32)opentype.sfnt_u16(table, offset + 2)
        let count = (u32)opentype.sfnt_u16(table, offset + 4)
        if glyph < start or glyph - start >= count:
            return 0
        return opentype.sfnt_u16(table, offset + 6 + (usize)(glyph - start) * 2)
    if class_format == 2:
        let count = (usize)opentype.sfnt_u16(table, offset + 2)
        var low: usize = 0
        var high = count
        while low < high:
            let middle = low + (high - low) // 2
            let record = offset + 4 + middle * 6
            let start = (u32)opentype.sfnt_u16(table, record)
            let end = (u32)opentype.sfnt_u16(table, record + 2)
            if glyph < start:
                high = middle
            elif glyph > end:
                low = middle + 1
            else:
                return opentype.sfnt_u16(table, record + 4)
        return 0
    return 0

# mark: GDEF ====================================================================================

## Whether GDEF has a glyph class definition (hb_ot_layout_has_glyph_classes).
func gdef_has_glyph_classes(face: const opentype.SfntFace*) -> bool:
    return face.gdef.length > 0 and opentype.sfnt_u16(face.gdef, 4) != 0

## The GDEF glyph class of `glyph`.
func gdef_glyph_class(face: const opentype.SfntFace*, glyph: u32) -> u8:
    let offset = (usize)opentype.sfnt_u16(face.gdef, 4)
    if offset == 0:
        return 0
    return (u8)ot_class_of(face.gdef, offset, glyph)

## The GDEF mark attachment class of `glyph`.
func gdef_mark_attachment_class(face: const opentype.SfntFace*, glyph: u32) -> u16:
    let offset = (usize)opentype.sfnt_u16(face.gdef, 10)
    if offset == 0:
        return 0
    return ot_class_of(face.gdef, offset, glyph)

## Whether `glyph` is in GDEF mark glyph set `set` (GDEF 1.2).
func gdef_mark_set_covers(face: const opentype.SfntFace*, set: u16, glyph: u32) -> bool:
    if opentype.sfnt_u16(face.gdef, 2) < 2:
        return false
    let sets = (usize)opentype.sfnt_u16(face.gdef, 12)
    if sets == 0 or set >= opentype.sfnt_u16(face.gdef, sets + 2):
        return false
    let coverage = sets + (usize)opentype.sfnt_u32(face.gdef, sets + 4 + (usize)set * 4)
    return ot_coverage_index(face.gdef, coverage, glyph) != none

## hb_ot_layout_substitute_start's glyph properties: GDEF's classes, or, without them,
## hb_synthesize_glyph_classes (a non-spacing mark that is not default ignorable is a mark).
func hb_set_glyph_classes(face: const opentype.SfntFace*, buffer: HbBufferT*):
    let infos = buffer.infos.view()
    if gdef_has_glyph_classes(face):
        for index in 0..<infos.length:
            infos[index].glyph_class = gdef_glyph_class(face, infos[index].codepoint)
        return
    for index in 0..<infos.length:
        let is_mark = infos[index].is_nonspacing_mark and not infos[index].is_default_ignorable
        infos[index].glyph_class = 3 if is_mark else 1

## The class a substituted glyph takes: GDEF's, else `fallback`.
func hb_class_for_substitute(face: const opentype.SfntFace*, glyph: u32, fallback: u8) -> u8:
    if gdef_has_glyph_classes(face):
        return gdef_glyph_class(face, glyph)
    return fallback

# mark: The plan ================================================================================

## A lookup to apply, with the value of the feature that enabled it.
struct HbPlanLookup:
    var index: u16
    var feature_value: u32
    ## Whether the lookup's features match joiners automatically (all but mark and mkmk,
    ## which HarfBuzz adds with F_MANUAL_JOINERS; a lookup shared by features keeps the
    ## automatic behaviour only if all of them have it).
    var auto_zwj: bool
    var auto_zwnj: bool

## The shaping plan: GSUB's two stages and GPOS's lookups, in lookup-list order.
pub struct HbLayoutPlan:
    var gsub_stage_rvrn: opentype.List[HbPlanLookup]
    var gsub_stage_main: opentype.List[HbPlanLookup]
    var gpos_lookups: opentype.List[HbPlanLookup]
    var apply_kern_table: bool
    var apply_trak: bool

    ## Free the plan's lookup lists.
    func release():
        self.gsub_stage_rvrn.release()
        self.gsub_stage_main.release()
        self.gpos_lookups.release()

## A feature the plan asks for.
struct HbWantedFeature:
    var tag: u32
    var value: u32

## The features HarfBuzz enables for a horizontal run of the default shaper (hb-ot-shape.cc
## hb_ot_shape_collect_features), then the font's features, which override them.
func hb_wanted_features(direction: HbDirection, features: const HbFeature[]) -> opentype.List[HbWantedFeature]:
    var wanted: opentype.List[HbWantedFeature]
    let defaults: str[16] = ["ltra", "ltrm", "abvm", "blwm", "ccmp", "locl", "mark", "mkmk", "rlig", "calt", "clig", "curs", "dist", "kern", "liga", "rclt"]
    for name in defaults:
        let bytes = name.bytes
        var tag = opentype.sfnt_tag(bytes[0], bytes[1], bytes[2], bytes[3])
        if direction == HbDirection.rtl:
            if tag == opentype.sfnt_tag('l', 't', 'r', 'a'):
                tag = opentype.sfnt_tag('r', 't', 'l', 'a')
            elif tag == opentype.sfnt_tag('l', 't', 'r', 'm'):
                tag = opentype.sfnt_tag('r', 't', 'l', 'm')
        wanted.push(HbWantedFeature(tag = tag, value = 1))
    for feature in features:
        var found = false
        for index in 0..<wanted.length():
            let entry = wanted.at(index)
            if entry.tag == feature.tag:
                entry.value = feature.value
                found = true
        if not found:
            wanted.push(HbWantedFeature(tag = feature.tag, value = feature.value))
    return wanted

## The OpenType script tags of a script, in HarfBuzz's order of preference
## (hb_ot_all_tags_from_script: the new Indic tags first, then the old tag), followed by the
## fallbacks of hb_ot_layout_table_select_script (DFLT, dflt, latn).
func ot_script_tags(script: u32) -> opentype.List[u32]:
    var tags: opentype.List[u32]
    let indic: str[10] = ["Bengbng3bng2", "Devadev3dev2", "Gujrgjr3gjr2", "Gurugur3gur2", "Kndaknd3knd2", "Mlymmlm3mlm2", "Oryaory3ory2", "Tamltml3tml2", "Telutel3tel2", "Mymrmym2mym2"]
    for entry in indic:
        let bytes = entry.bytes
        if script == opentype.sfnt_tag(bytes[0], bytes[1], bytes[2], bytes[3]):
            tags.push(opentype.sfnt_tag(bytes[4], bytes[5], bytes[6], bytes[7]))
            let second = opentype.sfnt_tag(bytes[8], bytes[9], bytes[10], bytes[11])
            if second != *tags.at(0):
                tags.push(second)
    if script != 0:
        # hb_ot_old_tag_from_script
        if script == opentype.sfnt_tag('H', 'i', 'r', 'a'):
            tags.push(opentype.sfnt_tag('k', 'a', 'n', 'a'))
        elif script == opentype.sfnt_tag('L', 'a', 'o', 'o'):
            tags.push(opentype.sfnt_tag('l', 'a', 'o', ' '))
        elif script == opentype.sfnt_tag('Y', 'i', 'i', 'i'):
            tags.push(opentype.sfnt_tag('y', 'i', ' ', ' '))
        elif script == opentype.sfnt_tag('N', 'k', 'o', 'o'):
            tags.push(opentype.sfnt_tag('n', 'k', 'o', ' '))
        elif script == opentype.sfnt_tag('V', 'a', 'i', 'i'):
            tags.push(opentype.sfnt_tag('v', 'a', 'i', ' '))
        else:
            # The script tag with its first letter lowercased.
            tags.push(script | 0x20000000)
    tags.push(opentype.sfnt_tag('D', 'F', 'L', 'T'))
    tags.push(opentype.sfnt_tag('d', 'f', 'l', 't'))
    tags.push(opentype.sfnt_tag('l', 'a', 't', 'n'))
    return tags

## The offset in `table` (GSUB or GPOS) of the default language system of the script.
func ot_find_lang_sys(table: const u8[], script: u32) -> usize?:
    let script_list = (usize)opentype.sfnt_u16(table, 4)
    if script_list == 0:
        return none
    let count = (usize)opentype.sfnt_u16(table, script_list)
    var candidates = ot_script_tags(script)
    defer candidates.release()
    for candidate in candidates.view():
        if candidate == 0:
            continue
        for index in 0..<count:
            let record = script_list + 2 + index * 6
            if opentype.sfnt_u32(table, record) != candidate:
                continue
            let script_table = script_list + (usize)opentype.sfnt_u16(table, record + 4)
            let default_lang_sys = (usize)opentype.sfnt_u16(table, script_table)
            if default_lang_sys == 0:
                return none
            return script_table + default_lang_sys
    return none

## The lookups of the wanted features in the language system, sorted by lookup index.
func ot_collect_lookups(table: const u8[], lang_sys: usize?, wanted: const HbWantedFeature[]) -> opentype.List[HbPlanLookup]:
    var lookups: opentype.List[HbPlanLookup]
    let lang = lang_sys else return lookups
    let feature_list = (usize)opentype.sfnt_u16(table, 6)
    let required = opentype.sfnt_u16(table, lang + 2)
    let feature_count = (usize)opentype.sfnt_u16(table, lang + 4)
    for position in 0..<feature_count + 1:
        var feature_index: u16
        var value: u32 = 1
        var manual_joiners = false
        if position == feature_count:
            # The required feature is always applied.
            if required == 0xFFFF:
                break
            feature_index = required
        else:
            feature_index = opentype.sfnt_u16(table, lang + 6 + position * 2)
            let tag = opentype.sfnt_u32(table, feature_list + 2 + (usize)feature_index * 6)
            manual_joiners = tag == opentype.sfnt_tag('m', 'a', 'r', 'k') or tag == opentype.sfnt_tag('m', 'k', 'm', 'k')
            var enabled = false
            for entry in wanted:
                if entry.tag == tag:
                    enabled = entry.value != 0
                    value = entry.value
            if not enabled:
                continue
        let feature = feature_list + (usize)opentype.sfnt_u16(table, feature_list + 2 + (usize)feature_index * 6 + 4)
        let lookup_count = (usize)opentype.sfnt_u16(table, feature + 2)
        for lookup_position in 0..<lookup_count:
            let lookup_index = opentype.sfnt_u16(table, feature + 4 + lookup_position * 2)
            hb_plan_add_lookup(&lookups, lookup_index, value, manual_joiners)
    return lookups

## Adds a lookup keeping the list sorted and without duplicates (hb_ot_map_t merges the
## lookups of a stage by index).
func hb_plan_add_lookup(lookups: opentype.List[HbPlanLookup]*, index: u16, value: u32, manual_joiners: bool):
    var position: usize = 0
    while position < lookups.length():
        let existing = lookups.at(position)
        if existing.index == index:
            if manual_joiners:
                existing.auto_zwj = false
                existing.auto_zwnj = false
            return
        if existing.index > index:
            break
        position += 1
    lookups.insert(position, HbPlanLookup(index = index, feature_value = value, auto_zwj = not manual_joiners, auto_zwnj = not manual_joiners))

## Plans the layout of `buffer` with the font's `features`.
func hb_layout_plan(face: const opentype.SfntFace*, buffer: const HbBufferT*, features: const HbFeature[]) -> HbLayoutPlan:
    var plan: HbLayoutPlan
    var wanted = hb_wanted_features(buffer.direction, features)
    defer wanted.release()
    let wanted_span = wanted.view()
    if face.gsub.length > 0:
        let lang_sys = ot_find_lang_sys(face.gsub, buffer.script)
        let rvrn: HbWantedFeature[1] = [HbWantedFeature(tag = opentype.sfnt_tag('r', 'v', 'r', 'n'), value = 1)]
        plan.gsub_stage_rvrn = ot_collect_lookups(face.gsub, lang_sys, rvrn)
        plan.gsub_stage_main = ot_collect_lookups(face.gsub, lang_sys, wanted_span)
    var kern_enabled = false
    for entry in wanted_span:
        if entry.tag == opentype.sfnt_tag('k', 'e', 'r', 'n'):
            kern_enabled = entry.value != 0
    var gpos_has_kern = false
    if face.gpos.length > 0:
        let lang_sys = ot_find_lang_sys(face.gpos, buffer.script)
        plan.gpos_lookups = ot_collect_lookups(face.gpos, lang_sys, wanted_span)
        let kern_only: HbWantedFeature[1] = [HbWantedFeature(tag = opentype.sfnt_tag('k', 'e', 'r', 'n'), value = 1)]
        var kern_lookups = ot_collect_lookups(face.gpos, lang_sys, kern_only)
        gpos_has_kern = kern_lookups.length() > 0
        kern_lookups.release()
    plan.apply_kern_table = kern_enabled and not gpos_has_kern and face.kern.length > 0
    # The AAT 'trak' table, which HarfBuzz applies through the default 'trak' feature.
    var trak_enabled = true
    for entry in wanted_span:
        if entry.tag == opentype.sfnt_tag('t', 'r', 'a', 'k'):
            trak_enabled = entry.value != 0
    if let trak = opentype.sfnt_face_table(face, opentype.sfnt_tag('t', 'r', 'a', 'k')):
        plan.apply_trak = trak_enabled and opentype.sfnt_u32(trak, 0) != 0
    return plan

# mark: Lookups ==================================================================================

## A lookup of a GSUB or GPOS table: its type (extensions resolved per subtable), flags and
## subtables.
struct OtLookup:
    var table: const u8[]
    var offset: usize
    var lookup_type: u16
    var flags: u16
    var mark_filtering_set: u16
    # The applying context's joiner handling and table (hb_ot_apply_context_t's auto_zwj,
    # auto_zwnj and table_index), which nested lookups inherit.
    var auto_zwj: bool
    var auto_zwnj: bool
    var is_gpos: bool

func ot_lookup(table: const u8[], index: u16, auto_zwj: bool, auto_zwnj: bool, is_gpos: bool) -> OtLookup?:
    let lookup_list = (usize)opentype.sfnt_u16(table, 8)
    if lookup_list == 0 or index >= opentype.sfnt_u16(table, lookup_list):
        return none
    let offset = lookup_list + (usize)opentype.sfnt_u16(table, lookup_list + 2 + (usize)index * 2)
    let flags = opentype.sfnt_u16(table, offset + 2)
    let subtable_count = (usize)opentype.sfnt_u16(table, offset + 4)
    var mark_filtering_set: u16 = 0
    if (flags & 0x10) != 0:
        mark_filtering_set = opentype.sfnt_u16(table, offset + 6 + subtable_count * 2)
    return OtLookup(table = table, offset = offset, lookup_type = opentype.sfnt_u16(table, offset), flags = flags, mark_filtering_set = mark_filtering_set, auto_zwj = auto_zwj, auto_zwnj = auto_zwnj, is_gpos = is_gpos)

## The subtable `index` of a lookup, with its type after resolving an extension subtable
## (`extension_type`: 7 in GSUB, 9 in GPOS).
func ot_lookup_subtable(lookup: const OtLookup*, index: usize, extension_type: u16) -> (u16, usize):
    let subtable = lookup.offset + (usize)opentype.sfnt_u16(lookup.table, lookup.offset + 6 + index * 2)
    if lookup.lookup_type == extension_type:
        return (opentype.sfnt_u16(lookup.table, subtable + 2), subtable + (usize)opentype.sfnt_u32(lookup.table, subtable + 4))
    return (lookup.lookup_type, subtable)

## Whether the lookup's flags skip `info` (hb_ot_layout_match_properties).
func ot_lookup_skips(face: const opentype.SfntFace*, lookup: const OtLookup*, info: const HbGlyphInfo*) -> bool:
    let glyph_class = info.glyph_class
    if glyph_class == 1 and (lookup.flags & 0x02) != 0:
        return true
    if glyph_class == 2 and (lookup.flags & 0x04) != 0:
        return true
    if glyph_class == 3:
        if (lookup.flags & 0x08) != 0:
            return true
        if (lookup.flags & 0x10) != 0:
            return not gdef_mark_set_covers(face, lookup.mark_filtering_set, info.codepoint)
        let attachment_type = lookup.flags >> 8
        if attachment_type != 0:
            return gdef_mark_attachment_class(face, info.codepoint) != attachment_type
    return false

## The next glyph after `index` a lookup would consider (skippy_iter.next without a match
## function: what the flags skip and default ignorables are passed over).
func ot_next_glyph(face: const opentype.SfntFace*, lookup: const OtLookup*, infos: const HbGlyphInfo[], index: usize) -> usize?:
    var candidate = index + 1
    while candidate < infos.length:
        if ot_may_skip(face, lookup, &infos[candidate], false) == 0:
            return candidate
        candidate += 1
    return none

# mark: GSUB ====================================================================================

## Applies the plan's GSUB lookups, stage by stage.
func hb_layout_substitute(face: const opentype.SfntFace*, font: const HbFontT*, plan: const HbLayoutPlan*, buffer: HbBufferT*):
    for planned in plan.gsub_stage_rvrn.view():
        gsub_apply_lookup(face, font, planned, buffer)
    for planned in plan.gsub_stage_main.view():
        gsub_apply_lookup(face, font, planned, buffer)

func gsub_apply_lookup(face: const opentype.SfntFace*, font: const HbFontT*, planned: HbPlanLookup, buffer: HbBufferT*):
    let lookup = ot_lookup(face.gsub, planned.index, planned.auto_zwj, planned.auto_zwnj, false) else return
    let subtable_count = (usize)opentype.sfnt_u16(face.gsub, lookup.offset + 4)
    var index: usize = 0
    while index < buffer.infos.length():
        let info = *buffer.infos.at(index)
        if ot_lookup_skips(face, &lookup, &info):
            index += 1
            continue
        var next = index + 1
        for subtable_index in 0..<subtable_count:
            let (lookup_type, subtable) = ot_lookup_subtable(&lookup, subtable_index, 7)
            if let applied = gsub_apply_subtable(face, font, &lookup, lookup_type, subtable, planned.feature_value, buffer, index, 0):
                next = applied
                break
        index = next

## Applies one subtable at `index`; answers the index to continue at when it applied.
func gsub_apply_subtable(face: const opentype.SfntFace*, font: const HbFontT*, lookup: const OtLookup*, lookup_type: u16, subtable: usize, feature_value: u32, buffer: HbBufferT*, index: usize, nesting: u32) -> usize?:
    let gsub = face.gsub
    # Contextual lookups find their own coverage.
    if lookup_type == 5:
        return ot_apply_context(face, font, false, lookup, subtable, buffer, index, nesting)
    if lookup_type == 6:
        return ot_apply_chain_context(face, font, false, lookup, subtable, buffer, index, nesting)
    let glyph = buffer.infos.at(index).codepoint
    let coverage = ot_coverage_index(gsub, subtable + (usize)opentype.sfnt_u16(gsub, subtable + 2), glyph) else return none
    match lookup_type:
        1:
            var substitute: u32
            if opentype.sfnt_u16(gsub, subtable) == 1:
                substitute = (glyph +% (u32)opentype.sfnt_u16(gsub, subtable + 4)) & 0xFFFF
            else:
                if coverage >= (usize)opentype.sfnt_u16(gsub, subtable + 4):
                    return none
                substitute = (u32)opentype.sfnt_u16(gsub, subtable + 6 + coverage * 2)
            gsub_replace_glyph(face, buffer, index, substitute)
            return index + 1
        2:
            if coverage >= (usize)opentype.sfnt_u16(gsub, subtable + 4):
                return none
            let sequence = subtable + (usize)opentype.sfnt_u16(gsub, subtable + 6 + coverage * 2)
            let count = (usize)opentype.sfnt_u16(gsub, sequence)
            if count == 0:
                buffer.infos.remove(index)
                return index
            let original = *buffer.infos.at(index)
            gsub_replace_glyph(face, buffer, index, (u32)opentype.sfnt_u16(gsub, sequence + 2))
            for position in 1..<count:
                var added = original
                added.codepoint = (u32)opentype.sfnt_u16(gsub, sequence + 2 + position * 2)
                added.substituted = true
                added.glyph_class = hb_class_for_substitute(face, added.codepoint, original.glyph_class)
                buffer.infos.insert(index + position, added)
            return index + count
        3:
            if coverage >= (usize)opentype.sfnt_u16(gsub, subtable + 4):
                return none
            let alternate_set = subtable + (usize)opentype.sfnt_u16(gsub, subtable + 6 + coverage * 2)
            let count = (u32)opentype.sfnt_u16(gsub, alternate_set)
            # The feature's value selects the alternate; 1 is the first.
            if feature_value == 0 or feature_value > count:
                return none
            gsub_replace_glyph(face, buffer, index, (u32)opentype.sfnt_u16(gsub, alternate_set + 2 + (usize)(feature_value - 1) * 2))
            return index + 1
        4:
            return gsub_apply_ligature(face, lookup, subtable, coverage, buffer, index)
        _:
            # FIXME: reverse chaining contextual substitution (8) is not applied.
            return none

func gsub_replace_glyph(face: const opentype.SfntFace*, buffer: HbBufferT*, index: usize, glyph: u32):
    let info = buffer.infos.at(index)
    info.codepoint = glyph
    info.substituted = true
    info.glyph_class = hb_class_for_substitute(face, glyph, info.glyph_class)

## Ligature substitution: the first ligature of the glyph's set whose components follow it
## (skipping what the lookup flags skip, and default ignorables that are not components).
func gsub_apply_ligature(face: const opentype.SfntFace*, lookup: const OtLookup*, subtable: usize, coverage: usize, buffer: HbBufferT*, index: usize) -> usize?:
    let gsub = face.gsub
    if coverage >= (usize)opentype.sfnt_u16(gsub, subtable + 4):
        return none
    let ligature_set = subtable + (usize)opentype.sfnt_u16(gsub, subtable + 6 + coverage * 2)
    let ligature_count = (usize)opentype.sfnt_u16(gsub, ligature_set)
    for ligature_index in 0..<ligature_count:
        let ligature = ligature_set + (usize)opentype.sfnt_u16(gsub, ligature_set + 2 + ligature_index * 2)
        let ligature_glyph = (u32)opentype.sfnt_u16(gsub, ligature)
        let component_count = (usize)opentype.sfnt_u16(gsub, ligature + 2)
        if component_count == 0:
            continue
        var matched: opentype.List[usize]
        defer matched.release()
        var cursor = index
        var ok = true
        for component in 1..<component_count:
            let wanted = (u32)opentype.sfnt_u16(gsub, ligature + 4 + (component - 1) * 2)
            var found = false
            var candidate = cursor + 1
            let infos = buffer.infos.view()
            while candidate < infos.length:
                let info = &infos[candidate]
                let skip = ot_may_skip(face, lookup, info, false)
                if skip != 2 and info.codepoint == wanted:
                    found = true
                    break
                if skip != 0:
                    candidate += 1
                    continue
                break
            if not found:
                ok = false
                break
            matched.push(candidate)
            cursor = candidate
        if not ok:
            continue
        # merge_clusters over the matched range, then the components are removed.
        let infos = buffer.infos.view()
        var cluster = infos[index].cluster
        for position in index..<cursor + 1:
            if infos[position].cluster < cluster:
                cluster = infos[position].cluster
        for position in index..<cursor + 1:
            infos[position].cluster = cluster
        gsub_replace_glyph(face, buffer, index, ligature_glyph)
        let first = buffer.infos.at(index)
        first.ligated = true
        first.glyph_class = hb_class_for_substitute(face, ligature_glyph, 2)
        var removed: usize = 0
        for position in matched.view():
            buffer.infos.remove(position - removed)
            removed += 1
        return index + 1
    return none