1// The OpenType language systems a BCP 47 tag names, as HarfBuzz gives2// them (hb-ot-tag.cc), from the tables language_tags.alloy holds.34import std::vector;5import std::option;6import std::text;78import pkg::alloy::text::shaping::tags;9import language_tags;1011// The language systems the BCP 47 tag names, in the order a face's are12// searched; none for none.13pub fn language_tags(written: &[u8]) -> Tags {14 var tags = Tags {};15 if (written.length() == 0) {16 return tags;17 }18 // lowercase, with `-` for `_`, as HarfBuzz keeps a language19 var lower = Vector<u8>::with_capacity(written.length());20 for (written) |c| {21 lower.push(if (c == '_') '-' else if (c >= 'A' && c <= 'Z') c + 32 else c);22 }23 const all = &lower.view();24 // the private use part (`-x-...`) is left out, but for `-hbot`25 var limit = all.length();26 var private: Option<u64> = ::None;27 if (all.length() >= 2 && all[0] == 'x' && all[1] == '-') {28 private = ::Some(0);29 limit = 0;30 } else {31 for ([1..all.length()]) |k| {32 if (all[k - 1] == '-' && k + 1 < all.length() && all[k + 1] == '-') {33 if (all[k] == 'x') {34 private = ::Some(k);35 limit = min_u64(limit, k - 1);36 break;37 }38 limit = min_u64(limit, k - 1);39 }40 }41 }42 if (private is ::Some |p| && hbot_tag(&all[p..]) is ::Some |t|) {43 tags.push(t);44 return tags;45 }46 const s = &all[..limit];47 if (complex_tags(&s) is ::Some |found|) {48 return found;49 }50 var first = &s;51 // an extended language subtag (`zh-yue`) stands for the language52 const dash = s.find_byte('-', 0);53 if (dash is ::Some |d| && s.length() >= 6) {54 const end = if (s.find_byte('-', d + 1) is ::Some |e|) e else s.length();55 if (end - d - 1 == 3 && is_letter(s[d + 1])) {56 first = &s[d + 1..];57 }58 }59 const first_end = if (first.find_byte('-', 0) is ::Some |e|) e else first.length();60 const key = tag_of(&first[..first_end]);61 if (first_end == 2) {62 // every system the two-letter code lists, in order63 var k = lower_bound(&LANGUAGES2, 2, key);64 while (k < LANGUAGES2.length() / 2 && LANGUAGES2[k * 2] == key) {65 tags.push(LANGUAGES2[k * 2 + 1]);66 k += 1;67 }68 return tags;69 }70 if (first_end == 3) {71 const k = lower_bound(&LANGUAGES3, 2, key);72 if (k < LANGUAGES3.length() / 2 && LANGUAGES3[k * 2] == key) {73 tags.push(LANGUAGES3[k * 2 + 1]);74 return tags;75 }76 const m = lower_bound(&LANGUAGES3_MULTI, 3, key);77 if (m < LANGUAGES3_MULTI.length() / 3 && LANGUAGES3_MULTI[m * 3] == key) {78 const offset = LANGUAGES3_MULTI[m * 3 + 1] to u64;79 const count = LANGUAGES3_MULTI[m * 3 + 2] to u64;80 for (LANGUAGES3_MULTI_VALUES[offset..offset + count]) |t| {81 tags.push(t);82 }83 return tags;84 }85 const b = lower_bound(&LANGUAGES3_BLOCKED, 1, key);86 if (b < LANGUAGES3_BLOCKED.length() && LANGUAGES3_BLOCKED[b] == key) {87 return tags;88 }89 // taken as an ISO 639-3 code, uppercased90 tags.push(key & ~0x20202000);91 return tags;92 }93 // a three-letter first subtag of a tag the tables do not list94 const plain_end = if (s.find_byte('-', 0) is ::Some |e|) e else s.length();95 if (plain_end == 3) {96 tags.push(tag_of(&s[..3]) & ~0x20202000);97 }98 return tags;99}100101fn min_u64(a: u64, b: u64) -> u64 => if (a < b) a else b;102103fn is_letter(c: u8) -> bool => c >= 'a' && c <= 'z';104105fn is_alphanumeric(c: u8) -> bool => is_letter(c) || (c >= '0' && c <= '9');106107// The text as a tag, padded with spaces.108fn tag_of(text: &[u8]) -> u32 {109 var t: u32 = 0;110 for ([..4 to u64]) |k| {111 t = (t << 8) | (if (k < text.length()) text[k] else ' ') to u32;112 }113 return t;114}115116// The first entry of the sorted table, `stride` values each, whose first117// value is not below the key.118fn lower_bound(table: &[u32], stride: u64, key: u32) -> u64 {119 var low: u64 = 0;120 var high = table.length() / stride;121 while (low < high) {122 const middle = (low + high) / 2;123 if (table[middle * stride] < key) {124 low = middle + 1;125 } else {126 high = middle;127 }128 }129 return low;130}131132// A language system named in a private use subtag, `-x-hbot-xxxx` (or133// `-hbotxxxx`), uppercased and padded with spaces.134fn hbot_tag(private: &[u8]) -> Option<u32> {135 const at = if (private.find(&"-hbot", 0) is ::Some |a|) a + 5 else {136 return ::None;137 };138 var start = at;139 if (start < private.length() && private[start] == '-') {140 start += 1;141 }142 var t: u32 = 0;143 var k: u64 = 0;144 while (k < 4 && start + k < private.length() && is_alphanumeric(private[start + k])) {145 const c = private[start + k];146 t = (t << 8) | (if (c >= 'a' && c <= 'z') c - 32 else c) to u32;147 k += 1;148 }149 if (k == 0) {150 return ::None;151 }152 while (k < 4) {153 t = (t << 8) | ' ' to u32;154 k += 1;155 }156 return ::Some(t);157}158159// Whether the subtag (written with its `-`) appears in the tag after its160// first subtag, whole.161fn has_subtag(s: &[u8], subtag: &[u8]) -> bool {162 var from: u64 = 0;163 while (s.find(&subtag, from) is ::Some |at|) {164 const end = at + subtag.length();165 if (end >= s.length() || !is_alphanumeric(s[end])) {166 return true;167 }168 from = at + 1;169 }170 return false;171}172173// HarfBuzz's systems for tags matched by more than their first subtag.174fn complex_tags(s: &[u8]) -> Option<Tags> {175 for (COMPLEX_LANGUAGES) |&rule| {176 const matched = match (rule.rule) {177 ::Variant s.length() >= 7178 && (if (s.find_byte('-', 0) is ::Some |p|) s.length() - p >= 5179 && has_subtag(&s[p..], &rule.subtag) else false);180 ::Language s.starts_with(&rule.text)181 && (s.length() == rule.text.length() || s[rule.text.length()] == '-');182 ::Prefix s.starts_with(&rule.text) && has_subtag(&s, &rule.subtag);183 ::Exact s == rule.text;184 };185 if (matched) {186 var tags = Tags {};187 for (rule.tags) |t| {188 if (t != 0) {189 tags.push(t);190 }191 }192 return ::Some(tags);193 }194 }195 return ::None;196}