ui_access

source of pkg::alloy::text::shaping::normalize

1// Normalizing a run's characters as HarfBuzz does for the face2// (hb-ot-shape-normalize.cc): each character decomposed where the face3// lacks it and has its parts (or always, for some shapers), marks put in4// canonical order, then each mark composed with its base where the face5// has the result; every character given the glyph the face maps it to.67import std::vector;8import std::option;910import font::face;11import unicode::tables;12import unicode::properties;13import buffer;14import plan;15import shapers;1617// marks reordered at most, in one run18const MAX_COMBINING_MARKS: u64 = 32;1920exp fn normalize(plan: &Plan, face: &Face, buffer: &var Buffer) {21    if (buffer.infos.length() == 0) {22        return;23    }24    var mode = plan.shaper.normalization();25    if (mode is ::Auto) {26        mode = Normalization::ComposedDiacritics;27    }28    const always_short = mode is ::Nothing;29    const might_short = always_short30        || !(mode is ::Decomposed || mode is ::ComposedDiacriticsNoShortCircuit);31    const input = &buffer.infos.view();32    const count = input.length();33    var out = Vector<GlyphInfo>::with_capacity(count + count / 4);34    var all_simple = true;35    var i: u64 = 0;36    while (i < count) {37        // up to the last character before a mark, leaving it for the marks38        var end = i + 1;39        while (end < count && !input[end].is_unicode_mark()) {40            end += 1;41        }42        if (end < count) {43            end -= 1;44        }45        if (might_short) {46            while (i < end) {47                const glyph = face.glyph_of(input[i].glyph);48                if (glyph == 0) {49                    break;50                }51                var info = input[i];52                info.mapped = glyph;53                out.push(info);54                i += 1;55            }56        }57        while (i < end) {58            decompose_current(&plan, &face, &var buffer, &input[i], &var out, might_short);59            i += 1;60        }61        if (i == count) {62            break;63        }64        all_simple = false;65        // the cluster: this character and the marks after it66        end = i + 1;67        while (end < count && input[end].is_unicode_mark()) {68            end += 1;69        }70        decompose_cluster(&plan, &face, &var buffer, &input[i..end], &var out, always_short);71        i = end;72    }73    buffer.infos = move out;74    if (!all_simple) {75        reorder(&plan, &var buffer);76    }77    if (buffer.has_grapheme_joiner) {78        // a CGJ not keeping marks out of order may be skipped after all79        const infos = &var buffer.infos.items();80        for ([1..infos.length()]) |k| {81            if (82                k + 1 < infos.length()83                && infos[k].glyph == 0x034F84                && (85                    infos[k + 1].combining_class() == 086                    || infos[k - 1].combining_class() <= infos[k + 1].combining_class()87                )88            ) {89                infos[k].unhide();90            }91        }92    }93    if (94        !all_simple95        && (mode is ::ComposedDiacritics || mode is ::ComposedDiacriticsNoShortCircuit)96    ) {97        recompose(&plan, &face, &var buffer);98    }99}100101// The character as itself, decomposed, or as a space or hyphen the face102// has (HarfBuzz's decompose_current_character).103fn decompose_current(104    plan: &Plan,105    face: &Face,106    buffer: &var Buffer,107    current: &GlyphInfo,108    out: &var Vector<GlyphInfo>,109    shortest: bool110) {111    const u = current.glyph;112    const glyph = face.glyph_of(u);113    if (shortest && glyph != 0) {114        push_char(&var out, &current, u, glyph, &var buffer, false);115        return;116    }117    if (decompose(&plan, &face, &var buffer, &current, &var out, shortest, u) > 0) {118        return;119    }120    if (!shortest && glyph != 0) {121        push_char(&var out, &current, u, glyph, &var buffer, false);122        return;123    }124    if (current.is_unicode_space()) {125        const kind = space_kind_of(u);126        const space = face.glyph_of(0x20);127        if (kind != 0 && space != 0) {128            var info = copy current;129            info.mapped = space;130            info.set_space_kind(kind);131            out.push(info);132            buffer.has_space_fallback = true;133            return;134        }135    }136    if (u == 0x2011) {137        // the one no-break character, not a space, with a breaking twin138        const hyphen = face.glyph_of(0x2010);139        if (hyphen != 0) {140            push_char(&var out, &current, u, hyphen, &var buffer, false);141            return;142        }143    }144    push_char(&var out, &current, u, glyph, &var buffer, false);145}146147// The character the glyph shows, a copy of `current`, with its props148// set again when it is another character.149fn push_char(150    out: &var Vector<GlyphInfo>,151    current: &GlyphInfo,152    c: u32,153    glyph: u32,154    buffer: &var Buffer,155    other: bool156) {157    var info = copy current;158    info.glyph = c;159    info.mapped = glyph;160    if (other) {161        info.set_unicode(&var buffer);162    }163    out.push(info);164}165166// Puts the character's decomposition, its first part decomposed again,167// when the face has the parts: how many characters it put.168fn decompose(169    plan: &Plan,170    face: &Face,171    buffer: &var Buffer,172    current: &GlyphInfo,173    out: &var Vector<GlyphInfo>,174    shortest: bool,175    ab: u32176) -> u64 {177    const parts = if (shapers::decompose(&plan, ab) is ::Some |p|) p else {178        return 0;179    };180    const a = parts[0];181    const b = parts[1];182    const b_glyph = if (b != 0) face.glyph_of(b) else 0;183    if (b != 0 && b_glyph == 0) {184        return 0;185    }186    const a_glyph = face.glyph_of(a);187    const has_a = a_glyph != 0;188    if (shortest && has_a) {189        push_char(&var out, &current, a, a_glyph, &var buffer, true);190        if (b != 0) {191            push_char(&var out, &current, b, b_glyph, &var buffer, true);192            return 2;193        }194        return 1;195    }196    const put = decompose(&plan, &face, &var buffer, &current, &var out, shortest, a);197    if (put > 0) {198        if (b != 0) {199            push_char(&var out, &current, b, b_glyph, &var buffer, true);200            return put + 1;201        }202        return put;203    }204    if (has_a) {205        push_char(&var out, &current, a, a_glyph, &var buffer, true);206        if (b != 0) {207            push_char(&var out, &current, b, b_glyph, &var buffer, true);208            return 2;209        }210        return 1;211    }212    return 0;213}214215// A base and its marks, or a cluster with a variation selector, whose216// sequences the face may map whole.217fn decompose_cluster(218    plan: &Plan,219    face: &Face,220    buffer: &var Buffer,221    cluster: &[GlyphInfo],222    out: &var Vector<GlyphInfo>,223    shortest: bool224) {225    var has_selector = false;226    for (cluster) |&info| {227        has_selector = has_selector || is_variation_selector(info.glyph);228    }229    if (!has_selector) {230        for (cluster) |&info| {231            decompose_current(&plan, &face, &var buffer, &info, &var out, shortest);232        }233        return;234    }235    // with a variation selector, the sequences the face has, the others236    // left to GSUB237    var k: u64 = 0;238    const end = cluster.length();239    while (k + 1 < end) {240        if (is_variation_selector(cluster[k + 1].glyph)) {241            if (face.variation_glyph(cluster[k].glyph, cluster[k + 1].glyph) is ::Some |glyph|) {242                var info = cluster[k];243                info.mapped = glyph;244                // the two as one glyph, its cluster theirs245                if (cluster[k + 1].cluster < info.cluster) {246                    info.cluster = cluster[k + 1].cluster;247                }248                out.push(info);249                k += 2;250            } else {251                var base = cluster[k];252                base.mapped = face.glyph_of(base.glyph);253                out.push(base);254                buffer.has_variation_selector_fallback = true;255                var selector = cluster[k + 1];256                selector.set_variation_selector(true);257                selector.mapped = face.glyph_of(selector.glyph);258                out.push(selector);259                k += 2;260            }261            // any further selectors as they are262            while (k < end && is_variation_selector(cluster[k].glyph)) {263                var info = cluster[k];264                info.mapped = face.glyph_of(info.glyph);265                out.push(info);266                k += 1;267            }268        } else {269            var info = cluster[k];270            info.mapped = face.glyph_of(info.glyph);271            out.push(info);272            k += 1;273        }274    }275    if (k < end) {276        var info = cluster[k];277        info.mapped = face.glyph_of(info.glyph);278        out.push(info);279    }280}281282fn is_variation_selector(c: u32) -> bool => (c >= 0x180B && c <= 0x180D) || c == 0x180F283    || (c >= 0xFE00 && c <= 0xFE0F)284    || (c >= 0xE0100 && c <= 0xE01EF);285286// Each run of marks sorted by combining class, then reordered as the287// shaper wants.288fn reorder(plan: &Plan, buffer: &var Buffer) {289    const count = buffer.infos.length();290    var i: u64 = 0;291    while (i < count) {292        if (buffer.infos.get(i).combining_class() == 0) {293            i += 1;294            continue;295        }296        var end = i + 1;297        while (end < count && buffer.infos.get(end).combining_class() != 0) {298            end += 1;299        }300        if (end - i <= MAX_COMBINING_MARKS) {301            buffer.sort(i, end, (a, b) => a.combining_class() < b.combining_class());302            reorder_marks(&plan, &var buffer, i, end);303        }304        i = end + 1;305    }306}307308// Each mark composed with the base before it, where nothing between them309// blocks it and the face has the result.310fn recompose(plan: &Plan, face: &Face, buffer: &var Buffer) {311    const input = &buffer.infos.view();312    const count = input.length();313    var out = Vector<GlyphInfo>::with_capacity(count);314    out.push(input[0]);315    var starter: u64 = 0;316    var i: u64 = 1;317    while (i < count) {318        const current = &input[i];319        if (current.is_unicode_mark()) {320            const unblocked = starter == out.length() - 1321                || out.last().combining_class() < current.combining_class();322            if (unblocked && compose(&plan, out.get(starter).glyph, current.glyph) is ::Some |c|) {323                const glyph = face.glyph_of(c);324                if (glyph != 0) {325                    // the two one cluster, the input after them too where326                    // it shared the last one's327                    out.push(copy current);328                    merge_out_clusters(&var out, starter, &var buffer.infos.items()[i + 1..]);329                    out.pop();330                    var base = &var out.get_var(starter);331                    base.glyph = c;332                    base.mapped = glyph;333                    base.set_unicode(&var buffer);334                    i += 1;335                    continue;336                }337            }338        }339        out.push(copy current);340        i += 1;341        if (out.last().combining_class() == 0) {342            starter = out.length() - 1;343        }344    }345    buffer.infos = move out;346}347348// HarfBuzz's merge_out_clusters from `start` to the output's end, going349// on into the input's glyphs of the last one's cluster.350fn merge_out_clusters(out: &var Vector<GlyphInfo>, from: u64, rest: &var [GlyphInfo]) {351    const items = &var out.items();352    var start = from;353    const end = items.length();354    if (end - start < 2) {355        return;356    }357    var cluster = items[start].cluster;358    for (items[start + 1..end]) |&info| {359        if (info.cluster < cluster) {360            cluster = info.cluster;361        }362    }363    while (start > 0 && items[start - 1].cluster == items[start].cluster) {364        start -= 1;365    }366    const last = items[end - 1].cluster;367    for (rest) |&var info| {368        if (info.cluster != last) {369            break;370        }371        info.cluster = cluster;372    }373    for (items[start..end]) |&var info| {374        info.cluster = cluster;375    }376}377378// The width of a space character the face may lack, HarfBuzz's space_t.379exp const SPACE_EM: u32 = 1;380exp const SPACE_EM_16: u32 = 16;381exp const SPACE_4_EM_18: u32 = 17;382exp const SPACE_PLAIN: u32 = 18;383exp const SPACE_FIGURE: u32 = 19;384exp const SPACE_PUNCTUATION: u32 = 20;385exp const SPACE_NARROW: u32 = 21;386387fn space_kind_of(c: u32) -> u32 => match (c) {388    0x0020, 0x00A0 SPACE_PLAIN;389    0x2000, 0x2002 2;390    0x2001, 0x2003, 0x3000 SPACE_EM;391    0x2004 3;392    0x2005 4;393    0x2006 6;394    0x2007 SPACE_FIGURE;395    0x2008 SPACE_PUNCTUATION;396    0x2009 5;397    0x200A SPACE_EM_16;398    0x202F SPACE_NARROW;399    0x205F SPACE_4_EM_18;400    else 0;401};