1// The complex shapers HarfBuzz chooses between (hb-ot-shaper.hh): which2// one a script takes, and the steps of shaping each adds, dispatched3// here to their modules from the `ComplexShaper` a plan keeps.45import std::vector;6import std::option;78import pkg::alloy::text::font::reader;9import pkg::alloy::text::font::face;10import pkg::alloy::text::unicode::tables;11import pkg::alloy::text::unicode::properties;12import unicode::script_properties;13import pkg::alloy::text::shaping::buffer;14import pkg::alloy::text::shaping::plan;15import pkg::alloy::text::shaping::shapers;16import hebrew;17import arabic;18import indic;19import khmer;20import myanmar;21import universal;22import thai;23import hangul;24import syllables;2526// What a shaper keeps in its plan.27pub type ShaperData = enum {28 Nothing,29 Arabic: ArabicData,30 Indic: IndicData,31 Khmer: KhmerData,32 Use: UseData,33 Hangul: HangulData,34};3536// A complex shaper, as a plan keeps it.37pub type Complex : ComplexShaper = struct {38 shaper: Shaper,39 data: ShaperData = ShaperData::Nothing,40};4142// The complex shaper for a run of the script in a face whose GSUB was43// read for the tag; none where the default shaper takes it.44pub fn complex_shaper(script: Script, gsub_tag: u32) -> Option<*var ComplexShaper> {45 const shaper = shaper_for(script, gsub_tag);46 if (shaper is ::Default) {47 return ::None;48 }49 return ::Some(new Complex { .shaper = shaper });50}5152// The shaper for a script, given the tag its face's GSUB was read for.53pub fn shaper_for(script: Script, gsub_tag: u32) -> Shaper {54 match (script) {55 ::Arabic {56 return Shaper::Arabic;57 }58 ::Syriac {59 return if (gsub_tag != tag(&"DFLT")) Shaper::Arabic else Shaper::Default;60 }61 ::Hebrew {62 return Shaper::Hebrew;63 }64 ::Bengali, ::Devanagari, ::Gujarati, ::Gurmukhi, ::Kannada, ::Malayalam, ::Oriya, ::Tamil, ::Telugu {65 // a face made for DFLT (or latn) takes the default shaper, one66 // with the newest tags (dev3) the Universal Shaping Engine67 if (gsub_tag == tag(&"DFLT") || gsub_tag == tag(&"latn")) {68 return Shaper::Default;69 }70 return if ((gsub_tag & 0xFF) == ('3' to u32)) Shaper::Use else Shaper::Indic;71 }72 ::Khmer {73 return Shaper::Khmer;74 }75 ::Myanmar {76 // `mymr` is the tag of the faces made before the specification77 if (gsub_tag == tag(&"DFLT") || gsub_tag == tag(&"latn") || gsub_tag == tag(&"mymr")) {78 return Shaper::Default;79 }80 return Shaper::Myanmar;81 }82 ::Thai, ::Lao {83 return Shaper::Thai;84 }85 ::Hangul {86 return Shaper::Hangul;87 }88 }89 if (uses_use(script) && gsub_tag != tag(&"DFLT") && gsub_tag != tag(&"latn")) {90 return Shaper::Use;91 }92 return Shaper::Default;93}9495// The scripts HarfBuzz sends to the Universal Shaping Engine (those of96// Unicode 16.0).97fn uses_use(script: Script) -> bool => match (script) {98 ::Tibetan,99 ::Mongolian,100 ::Sinhala,101 ::Buhid,102 ::Hanunoo,103 ::Tagalog,104 ::Tagbanwa,105 ::Limbu,106 ::TaiLe,107 ::Buginese,108 ::Kharoshthi,109 ::SylotiNagri,110 ::Tifinagh,111 ::Balinese,112 ::Nko,113 ::PhagsPa,114 ::Cham,115 ::KayahLi,116 ::Lepcha,117 ::Rejang,118 ::Saurashtra,119 ::Sundanese,120 ::EgyptianHieroglyphs,121 ::Javanese,122 ::Kaithi,123 ::MeeteiMayek,124 ::TaiTham,125 ::TaiViet,126 ::Batak,127 ::Brahmi,128 ::Mandaic,129 ::Chakma,130 ::Miao,131 ::Sharada,132 ::Takri,133 ::Duployan,134 ::Grantha,135 ::Khojki,136 ::Khudawadi,137 ::Mahajani,138 ::Manichaean,139 ::Modi,140 ::PahawhHmong,141 ::PsalterPahlavi,142 ::Siddham,143 ::Tirhuta,144 ::Ahom,145 ::Multani,146 ::Adlam,147 ::Bhaiksuki,148 ::Marchen,149 ::Newa,150 ::MasaramGondi,151 ::Soyombo,152 ::ZanabazarSquare,153 ::Dogra,154 ::GunjalaGondi,155 ::HanifiRohingya,156 ::Makasar,157 ::Medefaidrin,158 ::OldSogdian,159 ::Sogdian,160 ::Elymaic,161 ::Nandinagari,162 ::NyiakengPuachueHmong,163 ::Wancho,164 ::Chorasmian,165 ::DivesAkuru,166 ::KhitanSmallScript,167 ::Yezidi,168 ::CyproMinoan,169 ::OldUyghur,170 ::Tangsa,171 ::Toto,172 ::Vithkuqi,173 ::Kawi,174 ::NagMundari,175 ::Garay,176 ::GurungKhema,177 ::KiratRai,178 ::OlOnal,179 ::Sunuwar,180 ::Todhri,181 ::TuluTigalari true;182 else false;183};184185fn kind(self c: &Complex) -> Shaper => c.shaper;186187// Adds the shaper's features and pauses: whether it has any.188fn collect_features(self c: &Complex, b: &var Builder) -> bool {189 match (c.shaper) {190 ::Arabic {191 collect_arabic_features(&var b);192 return true;193 }194 ::Indic {195 collect_indic_features(&var b);196 return true;197 }198 ::Khmer {199 collect_khmer_features(&var b);200 return true;201 }202 ::Myanmar {203 collect_myanmar_features(&var b);204 return true;205 }206 ::Use {207 collect_use_features(&var b);208 return true;209 }210 ::Hangul {211 collect_hangul_features(&var b);212 return true;213 }214 else {215 return false;216 }217 }218}219220// Changes the features after the common ones are added.221fn override_features(self c: &Complex, b: &var Builder) {222 match (c.shaper) {223 ::Indic {224 override_indic_features(&var b);225 }226 ::Khmer {227 override_khmer_features(&var b);228 }229 ::Hangul {230 override_hangul_features(&var b);231 }232 else {}233 }234}235236// What the shaper keeps of the plan and the face.237fn prepare(self c: &var Complex, plan: &Plan, face: &Face) {238 c.data = shaper_data(&plan, &face);239}240241fn shaper_data(plan: &Plan, face: &Face) -> ShaperData => match (plan.shaper) {242 ::Arabic ShaperData::Arabic(arabic_data(&plan, &face));243 ::Indic ShaperData::Indic(indic_data(&plan, &face));244 ::Khmer ShaperData::Khmer(khmer_data(&plan));245 ::Use ShaperData::Use(use_data(&plan, &face));246 ::Hangul ShaperData::Hangul(hangul_data(&plan));247 else ShaperData::Nothing;248};249250// The character two compose into for the shaper, if any.251fn compose(self c: &Complex, plan: &Plan, a: u32, b: u32) -> Option<u32> {252 return match (c.shaper) {253 ::Hebrew compose_hebrew(&plan, a, b);254 ::Indic compose_indic(a, b);255 ::Khmer, ::Use compose_unless_mark(a, b);256 else unicode_composition(a, b);257 };258}259260// The characters one decomposes into for the shaper, the second 0 for a261// singleton, if any.262fn decompose(self c: &Complex, ab: u32) -> Option<[u32 : 2]> => match (c.shaper) {263 ::Indic decompose_indic(ab);264 ::Khmer decompose_khmer(ab);265 else canonical_decomposition(ab);266};267268// Reorders the marks from start to end, after their canonical sort.269fn reorder_marks(self c: &Complex, buffer: &var Buffer, start: u64, end: u64) {270 match (c.shaper) {271 ::Hebrew {272 reorder_marks_hebrew(&var buffer, start, end);273 }274 ::Arabic {275 reorder_arabic_marks(&var buffer, start, end);276 }277 else {}278 }279}280281// What the shaper does to the characters before they are normalized.282fn preprocess_text(self c: &Complex, plan: &Plan, face: &Face, buffer: &var Buffer) {283 match (c.shaper) {284 ::Indic, ::Use {285 constrain_vowels(&var buffer);286 }287 ::Thai {288 preprocess_thai(&plan, &face, &var buffer);289 }290 ::Hangul {291 preprocess_hangul(&face, &var buffer);292 }293 else {}294 }295}296297// Sets the masks of the shaper's features on the characters.298fn setup_masks(self c: &Complex, plan: &Plan, face: &Face, buffer: &var Buffer) {299 match (c.data) {300 ::Arabic |&data| {301 setup_arabic_masks(&data, &var buffer);302 }303 ::Indic {304 setup_indic_masks(&var buffer);305 }306 ::Khmer {307 setup_khmer_masks(&var buffer);308 }309 ::Use |&data| {310 setup_use_masks(&data, &var buffer);311 }312 ::Hangul |&data| {313 setup_hangul_masks(&data, &var buffer);314 }315 else {316 if (c.shaper is ::Myanmar) {317 setup_myanmar_masks(&var buffer);318 }319 }320 }321}322323// What the shaper does to the glyphs once positioned.324fn postprocess_glyphs(self c: &Complex, face: &Face, buffer: &var Buffer) {325 if (c.shaper is ::Arabic) {326 apply_stretch(&face, &var buffer);327 }328}329330// Runs the pause between two stages of lookups.331fn run_pause(self c: &Complex, pause: Pause, plan: &Plan, face: &Face, buffer: &var Buffer) {332 match (pause) {333 ::ArabicStretch {334 if (c.data is ::Arabic |&data|) {335 record_stretch(&data, &var buffer);336 }337 }338 ::ArabicFallback {339 if (c.data is ::Arabic |&data|) {340 apply_fallback(&data, &face, &var buffer);341 }342 }343 ::IndicSyllables {344 find_indic_syllables(&var buffer);345 }346 ::IndicInitialReordering {347 if (c.data is ::Indic |&data|) {348 initial_indic_reordering(&plan, &data, &face, &var buffer);349 }350 }351 ::IndicFinalReordering {352 if (c.data is ::Indic |&data|) {353 final_indic_reordering(&plan, &data, &var buffer);354 }355 }356 ::KhmerSyllables {357 find_khmer_syllables(&var buffer);358 }359 ::KhmerReordering {360 if (c.data is ::Khmer |&data|) {361 reorder_khmer(&data, &face, &var buffer);362 }363 }364 ::MyanmarSyllables {365 find_myanmar_syllables(&var buffer);366 }367 ::MyanmarReordering {368 reorder_myanmar(&face, &var buffer);369 }370 ::UseSyllables {371 if (c.data is ::Use |&data|) {372 find_use_syllables(&data, &var buffer);373 }374 }375 ::UseRepha {376 if (c.data is ::Use |&data|) {377 record_use_repha(&data, &var buffer);378 }379 }380 ::UsePreBase {381 record_use_pre_base(&var buffer);382 }383 ::UseReordering {384 reorder_use(&face, &var buffer);385 }386 else {}387 }388}389390// The character two compose into unless the first is a mark.391fn compose_unless_mark(a: u32, b: u32) -> Option<u32> {392 const category = general_category(a);393 if (category is ::SpacingMark || category is ::EnclosingMark || category is ::NonspacingMark) {394 return ::None;395 }396 return unicode_composition(a, b);397}