1// The Thai and Lao shaper (hb-ot-shaper-thai.cc): Sara Am split into2// Nikhahit and Sara Aa, the Nikhahit moved before the above-base marks3// before it; for a Thai face without GSUB's Thai script, marks and4// descenders moved by the face's private-use glyphs, as Windows and the5// Mac lay them out.67import std::vector;8import std::option;910import pkg::alloy::text::font::face;11import pkg::alloy::text::unicode::tables;12import unicode::script_tables;13import pkg::alloy::text::shaping::buffer;14import pkg::alloy::text::shaping::plan;1516// What the private-use glyphs do.17type Action = enum {18 Nothing,19 Down, // a mark shifted down20 Left, // a mark shifted left21 DownLeft,22 RemoveDescender, // the base's descender removed23};2425type Edge = struct {26 action: Action,27 next: u8,28};2930// The marks' kinds: above-base vowels, below-base vowels, tones.31const AV: u64 = 0;32const BV: u64 = 1;33const T: u64 = 2;34const NOT_MARK: u64 = 3;3536// The consonants' kinds: plain, ascending, removable descender, strict37// descender, none.38const NC: u64 = 0;39const AC: u64 = 1;40const RC: u64 = 2;41const DC: u64 = 3;42const NOT_CONSONANT: u64 = 4;4344const ABOVE_START: [u8 : 5] = [0, 1, 0, 0, 3];45const ABOVE: [[Edge : 3] : 4] = [46 [47 Edge { .action = Action::Nothing, .next = 3 },48 Edge { .action = Action::Nothing, .next = 0 },49 Edge { .action = Action::Down, .next = 3 }50 ],51 [52 Edge { .action = Action::Left, .next = 2 },53 Edge { .action = Action::Nothing, .next = 1 },54 Edge { .action = Action::DownLeft, .next = 2 }55 ],56 [57 Edge { .action = Action::Nothing, .next = 3 },58 Edge { .action = Action::Nothing, .next = 2 },59 Edge { .action = Action::Left, .next = 3 }60 ],61 [62 Edge { .action = Action::Nothing, .next = 3 },63 Edge { .action = Action::Nothing, .next = 3 },64 Edge { .action = Action::Nothing, .next = 3 }65 ]66];67const BELOW_START: [u8 : 5] = [0, 0, 1, 2, 2];68const BELOW: [[Edge : 3] : 3] = [69 [70 Edge { .action = Action::Nothing, .next = 0 },71 Edge { .action = Action::Nothing, .next = 2 },72 Edge { .action = Action::Nothing, .next = 0 }73 ],74 [75 Edge { .action = Action::Nothing, .next = 1 },76 Edge { .action = Action::RemoveDescender, .next = 2 },77 Edge { .action = Action::Nothing, .next = 1 }78 ],79 [80 Edge { .action = Action::Nothing, .next = 2 },81 Edge { .action = Action::Down, .next = 2 },82 Edge { .action = Action::Nothing, .next = 2 }83 ]84];8586// Each action's characters with their Windows and Mac private-use forms.87const DOWN: [[u32 : 3] : 8] = [88 [0x0E48, 0xF70A, 0xF88B],89 [0x0E49, 0xF70B, 0xF88E],90 [0x0E4A, 0xF70C, 0xF891],91 [0x0E4B, 0xF70D, 0xF894],92 [0x0E4C, 0xF70E, 0xF897],93 [0x0E38, 0xF718, 0xF89B],94 [0x0E39, 0xF719, 0xF89C],95 [0x0E3A, 0xF71A, 0xF89D]96];97const DOWN_LEFT: [[u32 : 3] : 5] = [98 [0x0E48, 0xF705, 0xF88C],99 [0x0E49, 0xF706, 0xF88F],100 [0x0E4A, 0xF707, 0xF892],101 [0x0E4B, 0xF708, 0xF895],102 [0x0E4C, 0xF709, 0xF898]103];104const LEFT: [[u32 : 3] : 12] = [105 [0x0E48, 0xF713, 0xF88A],106 [0x0E49, 0xF714, 0xF88D],107 [0x0E4A, 0xF715, 0xF890],108 [0x0E4B, 0xF716, 0xF893],109 [0x0E4C, 0xF717, 0xF896],110 [0x0E31, 0xF710, 0xF884],111 [0x0E34, 0xF701, 0xF885],112 [0x0E35, 0xF702, 0xF886],113 [0x0E36, 0xF703, 0xF887],114 [0x0E37, 0xF704, 0xF888],115 [0x0E47, 0xF712, 0xF889],116 [0x0E4D, 0xF711, 0xF899]117];118const REMOVE_DESCENDER: [[u32 : 3] : 2] = [[0x0E0D, 0xF70F, 0xF89A], [0x0E10, 0xF700, 0xF89E]];119120pub fn preprocess_thai(plan: &Plan, face: &Face, buffer: &var Buffer) {121 var i: u64 = 0;122 while (i < buffer.infos.length()) {123 const u = buffer.infos.get(i).glyph;124 if ((u & ~0x80) != 0x0E33) {125 i += 1;126 continue;127 }128 // Sara Am as Nikhahit and Sara Aa129 var nikhahit = buffer.infos.get(i);130 nikhahit.glyph = u - 0x0E33 + 0x0E4D;131 nikhahit.set_continuation();132 nikhahit.set_general(GeneralCategory::NonspacingMark);133 buffer.infos.get_var(i).glyph = u - 1;134 buffer.infos.insert(i, nikhahit);135 const end = i + 2;136 var start = i;137 while (start > 0 && is_above_base_mark(buffer.infos.get(start - 1).glyph)) {138 start -= 1;139 }140 if (start + 2 < end) {141 buffer.merge_clusters(start, end);142 const infos = &var buffer.infos.items();143 const moved = infos[end - 2];144 var k = end - 2;145 while (k > start) {146 infos[k] = infos[k - 1];147 k -= 1;148 }149 infos[start] = moved;150 }151 if (start > 0) {152 buffer.merge_clusters(start - 1, end);153 }154 i = end;155 }156 if (plan.script is ::Thai && !plan.found[0]) {157 shape_private_use(&face, &var buffer);158 }159}160161fn is_above_base_mark(c: u32) -> bool {162 const u = c & ~0x80;163 return (u >= 0x0E34 && u <= 0x0E37)164 || (u >= 0x0E47 && u <= 0x0E4E)165 || u == 0x0E31166 || u == 0x0E3B;167}168169fn consonant_kind(u: u32) -> u64 {170 return match (u) {171 0x0E1B, 0x0E1D, 0x0E1F AC;172 0x0E0D, 0x0E10 RC;173 0x0E0E, 0x0E0F DC;174 else if (u >= 0x0E01 && u <= 0x0E2E) NC else NOT_CONSONANT;175 };176}177178fn mark_kind(u: u32) -> u64 {179 if (180 u == 0x0E31181 || (u >= 0x0E34 && u <= 0x0E37)182 || u == 0x0E47183 || (u >= 0x0E4D && u <= 0x0E4E)184 ) {185 return AV;186 }187 if (u >= 0x0E38 && u <= 0x0E3A) {188 return BV;189 }190 if (u >= 0x0E48 && u <= 0x0E4C) {191 return T;192 }193 return NOT_MARK;194}195196fn shape_private_use(face: &Face, buffer: &var Buffer) {197 var above = ABOVE_START[NOT_CONSONANT] to u64;198 var below = BELOW_START[NOT_CONSONANT] to u64;199 var base: u64 = 0;200 const infos = &var buffer.infos.items();201 for ([..infos.length()]) |i| {202 const kind = mark_kind(infos[i].glyph);203 if (kind == NOT_MARK) {204 const consonant = consonant_kind(infos[i].glyph);205 above = ABOVE_START[consonant] to u64;206 below = BELOW_START[consonant] to u64;207 base = i;208 continue;209 }210 const above_edge = ABOVE[above][kind];211 const below_edge = BELOW[below][kind];212 above = above_edge.next to u64;213 below = below_edge.next to u64;214 const action = if (above_edge.action is ::Nothing) below_edge.action else above_edge.action;215 if (action is ::RemoveDescender) {216 infos[base].glyph = private_use(&face, infos[base].glyph, action);217 } else {218 infos[i].glyph = private_use(&face, infos[i].glyph, action);219 }220 }221}222223fn private_use(face: &Face, u: u32, action: Action) -> u32 {224 if (action is ::Nothing) {225 return u;226 }227 const mappings: &[[u32 : 3]] = match (action) {228 ::Down &DOWN;229 ::DownLeft &DOWN_LEFT;230 ::Left &LEFT;231 else &REMOVE_DESCENDER;232 };233 for (mappings) |&m| {234 if (m[0] == u) {235 if (face.glyph_of(m[1]) != 0) {236 return m[1];237 }238 if (face.glyph_of(m[2]) != 0) {239 return m[2];240 }241 break;242 }243 }244 return u;245}