text_scripts

source of unicode::script_properties

1// The Unicode properties bidi and the complex shapers read, from the2// generated tables (script_tables.alloy, which text's3// unicode/generate.alloy writes).45import std::option;67import script_tables;89// The code point's Bidi_Class (Left_To_Right past Unicode's range).10pub fn bidi_class(c: u32) -> BidiClass => (bidi_byte(c) & 31) to BidiClass;1112fn bidi_byte(c: u32) -> u8 {13    if (c >= 0x110000) {14        return 0;15    }16    const chunk = BIDI_TOP[(c >> 9) to u64] to u64;17    const block = BIDI_MIDDLE[chunk * 32 + (c >> 4 & 31) to u64] to u64;18    return BIDI_BLOCKS[block * 16 + (c & 15) to u64];19}2021// The character whose glyph mirrors the code point's (Bidi_Mirroring_Glyph),22// if one does.23pub fn mirror(c: u32) -> Option<u32> {24    if ((bidi_byte(c) & MIRRORED) == 0) {25        return ::None;26    }27    var low: u64 = 0;28    var high = MIRRORS.length() / 2;29    while (low < high) {30        const middle = (low + high) / 2;31        const found = MIRRORS[middle * 2];32        if (found < c) {33            low = middle + 1;34        } else if (found > c) {35            high = middle;36        } else {37            return ::Some(MIRRORS[middle * 2 + 1]);38        }39    }40    return ::None;41}4243// What the syllabic shapers know of a code point: its Indic category and44// position (Indic scripts, Khmer and Myanmar) and its USE category.45pub type IndicProperties = struct {46    pub category: IndicCategory,47    pub position: IndicPosition,48    pub use_category: UseCategory,49};5051pub fn indic_properties(c: u32) -> IndicProperties {52    var v: u32 = 14 << 8; // X, End, O53    if (c < 0x110000) {54        const chunk = INDIC_TOP[(c >> 9) to u64] to u64;55        const block = INDIC_MIDDLE[chunk * 32 + (c >> 4 & 31) to u64] to u64;56        v = INDIC_VALUES[INDIC_BLOCKS[block * 16 + (c & 15) to u64] to u64];57    }58    return IndicProperties {59        .category = (v & 255) to IndicCategory,60        .position = ((v >> 8) & 255) to IndicPosition,61        .use_category = (v >> 16) to UseCategory62    };63}