text_scripts

source of pkg::alloy::text::unicode::runs

1// Text split into runs a face shapes whole (decisions/F10.md,2// question 1): by script, each character's Script and Script_Extensions3// (UAX #24) resolved as HarfBuzz and ICU resolve them, Common and4// Inherited characters joining the run around them and a closing bracket5// the run of its opening one; and by emoji presentation (UTS #51), a6// grapheme cluster at a time, so a run never splits one.78import std::vector;9import std::option;1011import tables;12import properties;13import graphemes;1415// A run: its bytes, its script (Common for one of no script, as an emoji16// run is) and whether it is in emoji presentation.17pub type TextRun = struct {18    pub start: u64,19    pub end: u64,20    pub script: Script,21    pub emoji: bool,22    pub ascii: bool = false,23};2425// A set of scripts, a bit each by Script's index.26type Scripts = struct {27    bits: [u64 : 3] = [0, 0, 0],28};2930fn Scripts::of(s: Script) -> Scripts {31    var set = Scripts {};32    set.add(s to u64);33    return set;34}3536fn add(self set: &var Scripts, k: u64) {37    set.bits[k / 64] |= 1 to u64 << (k % 64);38}3940fn empty(self set: &Scripts) -> bool => set.bits[0] == 0 && set.bits[1] == 0 && set.bits[2] == 0;4142fn and(self a: &Scripts, b: &Scripts) -> Scripts {43    return Scripts { .bits = [44        a.bits[0] & b.bits[0],45        a.bits[1] & b.bits[1],46        a.bits[2] & b.bits[2]47    ] };48}4950// The set's first script, Common for none.51fn first(self set: &Scripts) -> Script {52    for ([..3 to u64]) |w| {53        var bits = set.bits[w];54        if (bits != 0) {55            var k: u64 = 0;56            while ((bits & 1) == 0) {57                bits >>= 1;58                k += 1;59            }60            return (w * 64 + k) to u8 to Script;61        }62    }63    return Script::Common;64}6566// An opening bracket waiting for its closing one, with the scripts its67// run had.68type Open = struct {69    closing: u32,70    scripts: Scripts,71};7273const MAX_OPEN: u64 = 64;7475// The bytes from start to end in runs.76pub fn itemize(text: &[u8], start: u64, end: u64) -> Vector<TextRun> {77    var runs = Vector<TextRun>::empty();78    // ASCII, as most text is: one run, Latin when it has a letter79    var ascii = true;80    var letter = false;81    for (text[start..end]) |b| {82        ascii = ascii && b < 0x80;83        letter = letter || (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z');84    }85    if (ascii) {86        if (end > start) {87            runs.push(TextRun {88                .start = start,89                .end = end,90                .script = if (letter) Script::Latin else Script::Common,91                .emoji = false,92                .ascii = true93            });94        }95        return move runs;96    }97    var scripts = Scripts {}; // the current run's possible scripts, none yet98    var open = Vector<Open>::empty();99    var run_start = start;100    var run_emoji = false;101    var state = GraphemeState::start();102    var cluster = start;103    var at = start;104    while (cluster < end) {105        // the cluster from `cluster`, its characters' properties106        var cluster_end = cluster;107        var first = true;108        while (cluster_end < end) {109            const d = decode(&text, cluster_end);110            const value = properties(d.c);111            if (!first && state.breaks_before(value)) {112                break;113            }114            state.advance(value);115            cluster_end += d.length;116            first = false;117        }118        const emoji = emoji_presentation(&text, cluster, cluster_end);119        if (emoji != run_emoji && cluster > run_start) {120            runs.push(TextRun {121                .start = run_start,122                .end = cluster,123                .script = if (run_emoji) Script::Common else scripts.first(),124                .emoji = run_emoji125            });126            run_start = cluster;127        }128        run_emoji = emoji;129        if (!emoji) {130            at = cluster;131            while (at < cluster_end) {132                const d = decode(&text, at);133                const found = scripts_of(d.c, &var open, &scripts);134                if (found is ::Some |&candidates|) {135                    const both = scripts.and(&candidates);136                    if (scripts.empty()) {137                        scripts = candidates;138                    } else if (!both.empty()) {139                        scripts = both;140                    } else if (at == cluster) {141                        // a new script, from this cluster on142                        if (cluster > run_start) {143                            runs.push(TextRun {144                                .start = run_start,145                                .end = cluster,146                                .script = scripts.first(),147                                .emoji = false148                            });149                            run_start = cluster;150                        }151                        scripts = candidates;152                    }153                }154                at += d.length;155            }156        }157        cluster = cluster_end;158    }159    if (end > run_start) {160        runs.push(TextRun {161            .start = run_start,162            .end = end,163            .script = if (run_emoji) Script::Common else scripts.first(),164            .emoji = run_emoji165        });166    }167    return move runs;168}169170// The scripts the character may be in, None for one joining any run (of171// Common or Inherited script, as an opening bracket is); a closing172// bracket takes the scripts of its opening one's run.173fn scripts_of(c: u32, open: &var Vector<Open>, current: &Scripts) -> Option<Scripts> {174    if ((properties(c) & SCRIPT_EXTENDED) != 0) {175        var set = Scripts {};176        for (script_extensions(c)) |k| {177            set.add(k to u64);178        }179        return ::Some(set);180    }181    const s = script(c);182    match (s) {183        ::Inherited, ::Unknown {184            return ::None;185        }186        ::Common {187            if (bracket(c) is ::Some |b|) {188                if (!b.closing) {189                    if (open.length() == MAX_OPEN) {190                        open.remove(0);191                    }192                    open.push(Open { .closing = b.other, .scripts = copy current });193                } else {194                    var k = open.length();195                    while (k > 0) {196                        k -= 1;197                        if (open.get(k).closing == c) {198                            const scripts = open.get(k).scripts;199                            open.truncate(k);200                            return if (scripts.empty()) ::None else ::Some(scripts);201                        }202                    }203                }204            }205            return ::None;206        }207        else {208            return ::Some(Scripts::of(s));209        }210    }211}212213// Whether the grapheme cluster shows in emoji presentation (UTS #51): with214// variation selector 16 and not 15, as a keycap, a flag, a modified215// emoji, a tag sequence, a zero width joiner sequence of emoji, or a216// character whose default presentation is emoji's.217pub fn emoji_presentation(text: &[u8], start: u64, end: u64) -> bool {218    if (start >= end) {219        return false;220    }221    const first = decode(&text, start);222    const value = properties(first.c);223    var at = start + first.length;224    var previous = value;225    var sequence = false;226    while (at < end) {227        const d = decode(&text, at);228        const v = properties(d.c);229        match (d.c) {230            0xFE0E {231                return false;232            }233            0xFE0F, 0x20E3 {234                return true;235            }236            0x200D {237                sequence = true;238            }239        }240        if ((v & EMOJI_MODIFIER) != 0 && (previous & EMOJI_MODIFIER_BASE) != 0) {241            return true;242        }243        if (first.c == 0x1F3F4 && d.c >= 0xE0020 && d.c <= 0xE007F) {244            return true;245        }246        if (sequence && (v & EMOJI_PRESENTATION) != 0) {247            return true;248        }249        previous = v;250        at += d.length;251    }252    return (value & EMOJI_PRESENTATION) != 0;253}