1// The Universal Shaping Engine (hb-ot-shaper-use.cc), for Tibetan,2// Sinhala, Javanese, Mongolian and the other scripts HarfBuzz sends to3// it: syllables found by its machine over the USE categories (CGJ left4// out, ZWNJ before a mark too), a repha moved after the base, pre-base5// vowels before it, and joining forms by syllable, or by Arabic's6// joining for the scripts that join.78import std::vector;9import std::option;10import std::math;1112import pkg::alloy::text::font::reader;13import pkg::alloy::text::font::face;14import pkg::alloy::text::unicode::tables;15import unicode::script_tables;16import pkg::alloy::text::unicode::properties;17import unicode::script_properties;18import pkg::alloy::text::shaping::buffer;19import pkg::alloy::text::shaping::plan;20import pkg::alloy::text::shaping::shapers;21import machines;22import syllables;23import arabic;2425const BASIC: [&[u8] : 7] = [&"rkrf", &"abvf", &"blwf", &"half", &"pstf", &"vatu", &"cjct"];26const TOPOGRAPHICAL: [&[u8] : 4] = [&"isol", &"init", &"medi", &"fina"];27const OTHER: [&[u8] : 5] = [&"abvs", &"blws", &"haln", &"pres", &"psts"];2829// The joining forms, as indices into TOPOGRAPHICAL.30const ISOL: u64 = 0;31const INIT: u64 = 1;32const MEDI: u64 = 2;33const FINA: u64 = 3;34const NO_FORM: u64 = 4;3536// The categories it reads, as bytes37const B: u8 = 1;38const CGJ: u8 = 6;39const H: u8 = 12;40const ZWNJ: u8 = 14;41const R: u8 = 18;42const VPRE: u8 = 22;43const VMPRE: u8 = 23;44const IS: u8 = 44;45const HVM: u8 = 53;4647// What the shaper keeps in its plan: `rphf`'s mask, the joining forms'48// (0 for one the face lacks or every glyph takes), and Arabic's data for49// the scripts that join.50pub type UseData = struct {51 pub rphf_mask: u32,52 pub forms: [u32 : 4],53 pub arabic: Option<ArabicData>,54};5556pub fn collect_use_features(b: &var Builder) {57 b.add_gsub_pause(Pause::UseSyllables);58 b.enable_feature(tag(&"locl"), FeatureFlags { .per_syllable = true });59 b.enable_feature(tag(&"ccmp"), FeatureFlags { .per_syllable = true });60 b.enable_feature(tag(&"nukt"), FeatureFlags { .per_syllable = true });61 b.enable_feature(tag(&"akhn"), FeatureFlags { .manual_zwj = true, .per_syllable = true });62 b.add_gsub_pause(Pause::ClearSubstitutionFlags);63 b.add_feature(tag(&"rphf"), FeatureFlags { .manual_zwj = true, .per_syllable = true }, 1);64 b.add_gsub_pause(Pause::UseRepha);65 b.add_gsub_pause(Pause::ClearSubstitutionFlags);66 b.enable_feature(tag(&"pref"), FeatureFlags { .manual_zwj = true, .per_syllable = true });67 b.add_gsub_pause(Pause::UsePreBase);68 for (BASIC) |&name| {69 b.enable_feature(tag(&name), FeatureFlags { .manual_zwj = true, .per_syllable = true });70 }71 b.add_gsub_pause(Pause::UseReordering);72 b.add_gsub_pause(Pause::Nothing);73 for (TOPOGRAPHICAL) |&name| {74 b.add_feature(tag(&name), FeatureFlags {}, 1);75 }76 b.add_gsub_pause(Pause::Nothing);77 for (OTHER) |&name| {78 b.enable_feature(tag(&name), FeatureFlags { .manual_zwj = true });79 }80}8182pub fn use_data(plan: &Plan, face: &Face) -> UseData {83 var data = UseData {84 .rphf_mask = plan.one_mask(tag(&"rphf")),85 .forms = [0, 0, 0, 0],86 .arabic = ::None87 };88 if (has_arabic_joining(plan.script)) {89 data.arabic = ::Some(arabic_data(&plan, &face));90 } else {91 for (TOPOGRAPHICAL, [..4 to u64]) |&name, k| {92 const mask = plan.one_mask(tag(&name));93 data.forms[k] = if (mask == plan.global_mask) 0 else mask;94 }95 }96 return move data;97}9899// The scripts ArabicShaping.txt gives joining types.100fn has_arabic_joining(script: Script) -> bool => match (script) {101 ::Adlam,102 ::Arabic,103 ::Chorasmian,104 ::HanifiRohingya,105 ::Mandaic,106 ::Manichaean,107 ::Mongolian,108 ::Nko,109 ::OldUyghur,110 ::PhagsPa,111 ::PsalterPahlavi,112 ::Sogdian,113 ::Syriac true;114 else false;115};116117pub fn setup_use_masks(data: &UseData, buffer: &var Buffer) {118 if (data.arabic is ::Some |&arabic|) {119 setup_arabic_masks(&arabic, &var buffer);120 }121 for (buffer.infos.items()) |&var info| {122 info.category = indic_properties(info.glyph).use_category to u8;123 }124}125126// Finds the syllables, then marks the glyphs `rphf` may take and the127// syllables' joining forms.128pub fn find_use_syllables(data: &UseData, buffer: &var Buffer) {129 const count = buffer.infos.length();130 var categories = Vector<u8>::with_capacity(count);131 var origins = Vector<u64>::with_capacity(count);132 {133 const infos = &buffer.infos.view();134 for ([..count]) |i| {135 const category = infos[i].category;136 if (category == CGJ) {137 continue;138 }139 if (category == ZWNJ) {140 var j = i + 1;141 while (j < count && infos[j].category == CGJ) {142 j += 1;143 }144 if (j < count && infos[j].is_unicode_mark()) {145 continue;146 }147 }148 categories.push(category);149 origins.push(i);150 }151 }152 if (153 find_syllables(154 &USE_MACHINE,155 &categories.view(),156 &origins.view(),157 &var buffer.infos.items(),158 UseSyllable::BrokenCluster to u8159 )160 ) {161 buffer.has_broken_syllable = true;162 }163 if (data.rphf_mask != 0) {164 var start: u64 = 0;165 while (start < count) {166 const end = buffer.syllable_end(start);167 const limit = if (buffer.infos.get(start).category == R) 1 else min(3, end - start);168 for (buffer.infos.items()[start..start + limit]) |&var info| {169 info.mask |= data.rphf_mask;170 }171 start = end;172 }173 }174 setup_forms(&data, &var buffer);175}176177// Each syllable joins the one before it unless either is a hieroglyph178// cluster or not a cluster.179fn setup_forms(data: &UseData, buffer: &var Buffer) {180 if (data.arabic is ::Some) {181 return;182 }183 const all = data.forms[0] | data.forms[1] | data.forms[2] | data.forms[3];184 if (all == 0) {185 return;186 }187 const others = ~all;188 var last_start: u64 = 0;189 var last_form = NO_FORM;190 var start: u64 = 0;191 const count = buffer.infos.length();192 while (start < count) {193 const end = buffer.syllable_end(start);194 const kind = buffer.infos.get(start).syllable & 0x0F;195 const infos = &var buffer.infos.items();196 match (kind) {197 6, 8 {198 last_form = NO_FORM; // hieroglyph clusters and non-clusters199 }200 else {201 const join = last_form == FINA || last_form == ISOL;202 if (join) {203 last_form = if (last_form == FINA) MEDI else INIT;204 for (infos[last_start..start]) |&var info| {205 info.mask = (info.mask & others) | data.forms[last_form];206 }207 }208 last_form = if (join) FINA else ISOL;209 for (infos[start..end]) |&var info| {210 info.mask = (info.mask & others) | data.forms[last_form];211 }212 }213 }214 last_start = start;215 start = end;216 }217}218219// After `rphf`: the first glyph it substituted in a syllable is a repha.220pub fn record_use_repha(data: &UseData, buffer: &var Buffer) {221 if (data.rphf_mask == 0) {222 return;223 }224 var start: u64 = 0;225 const count = buffer.infos.length();226 while (start < count) {227 const end = buffer.syllable_end(start);228 const infos = &var buffer.infos.items();229 var i = start;230 while (i < end && (infos[i].mask & data.rphf_mask) != 0) {231 if (infos[i].substituted()) {232 infos[i].category = R;233 break;234 }235 i += 1;236 }237 start = end;238 }239}240241// After `pref`: the first glyph it substituted in a syllable is a pre-base242// vowel.243pub fn record_use_pre_base(buffer: &var Buffer) {244 var start: u64 = 0;245 const count = buffer.infos.length();246 while (start < count) {247 const end = buffer.syllable_end(start);248 for (buffer.infos.items()[start..end]) |&var info| {249 if (info.substituted()) {250 info.category = VPRE;251 break;252 }253 }254 start = end;255 }256}257258pub fn reorder_use(face: &Face, buffer: &var Buffer) {259 insert_dotted_circles(&face, &var buffer, UseSyllable::BrokenCluster to u8, B, ::Some(R), 0);260 var start: u64 = 0;261 while (start < buffer.infos.length()) {262 const end = buffer.syllable_end(start);263 reorder_syllable(&var buffer, start, end);264 start = end;265 }266}267268fn is_halant(info: &GlyphInfo) -> bool => !info.ligated() && match (info.category) {269 H, HVM, IS true;270 else false;271};272273// FAbv, FBlw, FPst, FMAbv, FMBlw, FMPst, MAbv, MBlw, MPst, MPre, VAbv,274// VBlw, VPst, VPre, VMAbv, VMBlw, VMPst and VMPre.275fn is_post_base(category: u8) -> bool => match (category) {276 22, 23, 24, 25, 26, 27, 28, 29, 30, 33, 34, 35, 37, 38, 39, 45, 46, 47 true;277 else false;278};279280fn reorder_syllable(buffer: &var Buffer, start: u64, end: u64) {281 // virama-terminated, sakot-terminated, standard, symbol and broken282 // clusters283 match (buffer.infos.get(start).syllable & 0x0F) {284 0, 1, 2, 5, 7 {}285 else {286 return;287 }288 }289 // a repha after the base: before the first post-base glyph, or last290 if (buffer.infos.get(start).category == R && end - start > 1) {291 for ([start + 1..end]) |k| {292 const info = buffer.infos.get(k);293 const post_base = is_post_base(info.category) || is_halant(&info);294 if (post_base || k == end - 1) {295 const i = if (post_base) k - 1 else k;296 buffer.merge_clusters(start, i + 1);297 const infos = &var buffer.infos.items();298 const repha = infos[start];299 for ([start..i]) |j| {300 infos[j] = infos[j + 1];301 }302 infos[i] = repha;303 break;304 }305 }306 }307 // pre-base vowels before the base, after any halant308 var j = start;309 for ([start..end]) |i| {310 const info = buffer.infos.get(i);311 if (is_halant(&info)) {312 j = i + 1;313 } else if (314 (info.category == VPRE || info.category == VMPRE)315 && info.ligature_component() == 0316 && j < i317 ) {318 buffer.merge_clusters(j, i + 1);319 const infos = &var buffer.infos.items();320 const vowel = infos[i];321 var k = i;322 while (k > j) {323 infos[k] = infos[k - 1];324 k -= 1;325 }326 infos[j] = vowel;327 }328 }329}