draw

source of pkg::alloy::text::unicode::words

1// Word boundaries, by UAX #29's rules (Unicode 16.0): where a screen2// reader's and the caret's words start and end. Thai, Lao, Khmer and3// Myanmar have no dictionary here, so a run of them is one word.45import std::vector;6import std::option;78import tables;9import properties;1011// The code point's Word_Break.12pub fn word_break(c: u32) -> WordBreak {13    // the last run starting at or before it14    var low: u64 = 0;15    var high = WORD_BREAKS.length();16    while (low < high) {17        const middle = (low + high) / 2;18        if (WORD_BREAKS[middle] >> 5 <= c) {19            low = middle + 1;20        } else {21            high = middle;22        }23    }24    return if (low == 0) WordBreak::Other else (WORD_BREAKS[low - 1] & 31) to WordBreak;25}2627// A code point of the text as the rules see it.28type Point = struct {29    at: u64,30    kind: WordBreak,31    pictographic: bool,32    // what WB4 makes of it: the point standing for it, itself unless an33    // Extend, Format or ZWJ following something else34    base: u64,35};3637fn is_letter(kind: WordBreak) -> bool => kind is ::ALetter || kind is ::HebrewLetter;3839fn is_mid_letter(kind: WordBreak) -> bool => kind is ::MidLetter || kind is ::MidNumLet40    || kind is ::SingleQuote;4142fn is_mid_number(kind: WordBreak) -> bool => kind is ::MidNum || kind is ::MidNumLet43    || kind is ::SingleQuote;4445fn ignored(kind: WordBreak) -> bool => kind is ::Extend || kind is ::Format || kind is ::ZWJ;4647fn is_newline(kind: WordBreak) -> bool => kind is ::CR || kind is ::LF || kind is ::Newline;4849// The byte offsets of every word boundary, the first and last included50// (none for empty text).51pub fn word_boundaries(text: &[u8]) -> Vector<u64> {52    var out = Vector<u64>::empty();53    if (text.length() == 0) {54        return move out;55    }56    var points = Vector<Point>::empty();57    var at: u64 = 0;58    while (at < text.length()) {59        const d = decode(&text, at);60        const kind = word_break(d.c);61        var base = points.length();62        if (ignored(kind) && base > 0 && !is_newline(points.get(base - 1).kind)) {63            base = points.get(base - 1).base;64        }65        points.push(Point {66            .at = at,67            .kind = kind,68            .pictographic = (properties(d.c) & PICTOGRAPHIC) != 0,69            .base = base70        });71        at += d.length;72    }73    out.push(0);74    var regional: u64 = 0; // regional indicators ending at the point before, WB4 applied75    for ([1..points.length()]) |k| {76        const before = &points.get(k - 1);77        if (before.base == k - 1) {78            regional = if (before.kind is ::RegionalIndicator) regional + 1 else 0;79        }80        if (breaks(&points.view(), k, regional)) {81            out.push(points.get(k).at);82        }83    }84    out.push(text.length());85    return move out;86}8788// The kind of the point WB4 leaves before the one standing for `k`, and89// of the one after `k`, skipping what WB4 ignores; Other at the ends.90fn before_base(points: &[Point], base: u64) -> WordBreak {91    return if (base == 0) WordBreak::Other else points[points[base - 1].base].kind;92}9394fn after(points: &[Point], k: u64) -> WordBreak {95    var j = k + 1;96    while (j < points.length() && points[j].base != j) {97        j += 1;98    }99    return if (j < points.length()) points[j].kind else WordBreak::Other;100}101102// Whether a word boundary comes before the point `k`.103fn breaks(points: &[Point], k: u64, regional: u64) -> bool {104    const raw = points[k - 1].kind;105    const b = points[k].kind;106    if (raw is ::CR && b is ::LF) {107        return false; // WB3108    }109    if (is_newline(raw) || is_newline(b)) {110        return true; // WB3a, WB3b111    }112    if (raw is ::ZWJ && points[k].pictographic) {113        return false; // WB3c114    }115    if (raw is ::WSegSpace && b is ::WSegSpace) {116        return false; // WB3d117    }118    if (ignored(b)) {119        return false; // WB4120    }121    const base = points[k - 1].base;122    const a = points[base].kind;123    if (is_letter(a) && is_letter(b)) {124        return false; // WB5125    }126    if (is_letter(a) && is_mid_letter(b) && is_letter(after(&points, k))) {127        return false; // WB6128    }129    if (is_mid_letter(a) && is_letter(b) && is_letter(before_base(&points, base))) {130        return false; // WB7131    }132    if (a is ::HebrewLetter && b is ::SingleQuote) {133        return false; // WB7a134    }135    if (a is ::HebrewLetter && b is ::DoubleQuote && after(&points, k) is ::HebrewLetter) {136        return false; // WB7b137    }138    if (a is ::DoubleQuote && b is ::HebrewLetter && before_base(&points, base) is ::HebrewLetter) {139        return false; // WB7c140    }141    if ((a is ::Numeric || is_letter(a)) && b is ::Numeric) {142        return false; // WB8, WB9143    }144    if (a is ::Numeric && is_letter(b)) {145        return false; // WB10146    }147    if (is_mid_number(a) && b is ::Numeric && before_base(&points, base) is ::Numeric) {148        return false; // WB11149    }150    if (a is ::Numeric && is_mid_number(b) && after(&points, k) is ::Numeric) {151        return false; // WB12152    }153    if (a is ::Katakana && b is ::Katakana) {154        return false; // WB13155    }156    const joins = is_letter(a) || a is ::Numeric || a is ::Katakana || a is ::ExtendNumLet;157    if (joins && b is ::ExtendNumLet) {158        return false; // WB13a159    }160    if (a is ::ExtendNumLet && (is_letter(b) || b is ::Numeric || b is ::Katakana)) {161        return false; // WB13b162    }163    if (a is ::RegionalIndicator && b is ::RegionalIndicator && regional % 2 == 1) {164        return false; // WB15, WB16165    }166    return true; // WB999167}168169// Where the text's words start: the boundaries before a letter, a digit,170// an ideograph or an emoji, never between two characters of a script171// broken by dictionaries (line breaking's SA).172pub fn find_word_starts(text: &[u8]) -> Vector<u32> {173    var out = Vector<u32>::empty();174    var last_sa = false; // the segment before the boundary starts with SA's175    for (word_boundaries(&text).view()) |at| {176        if (at >= text.length()) {177            break;178        }179        const c = decode(&text, at).c;180        const value = properties(c);181        const sa = line_class(value) == LB_SA;182        const within = sa && last_sa;183        last_sa = sa;184        if (within) {185            continue;186        }187        const kind = word_break(c);188        const word = is_letter(kind) || kind is ::Numeric || kind is ::Katakana189            || kind is ::ExtendNumLet || kind is ::RegionalIndicator || sa190            || line_class(value) == LB_ID || (value & PICTOGRAPHIC) != 0;191        if (word) {192            out.push(at to u32);193        }194    }195    return move out;196}