1// The Indic shaper (hb-ot-shaper-indic.cc), for Devanagari, Bengali,2// Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada and Malayalam: the3// syllables its machine finds, each reordered before the basic features4// (the base consonant found, reph and pre-base matras marked, below and5// post-base forms told apart by asking the face) and again after them6// (pre-base matras and reph moved to where the forms formed put them),7// in either the old OpenType specification's order or the new one's.89import std::vector;10import std::option;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::opentype;19import pkg::alloy::text::shaping::buffer;20import pkg::alloy::text::shaping::plan;21import pkg::alloy::text::shaping::shapers;22import pkg::alloy::text::shaping::gsub;23import machines;24import syllables;2526// Where a script's reph goes, how it is written, and where `blwf` applies.27pub type RephMode = enum {28 Implicit, // Ra, H29 Explicit, // Ra, H, ZWJ30 LogRepha, // an encoded repha, in its logical place31};3233pub type IndicConfig = struct {34 pub old_spec: bool, // whether the script has an old specification35 pub virama: u32,36 pub reph_position: IndicPosition,37 pub reph_mode: RephMode,38 pub below_pre_and_post: bool, // `blwf` before the base too39};4041fn config_of(script: Script) -> IndicConfig => match (script) {42 ::Devanagari IndicConfig {43 .old_spec = true,44 .virama = 0x094D,45 .reph_position = IndicPosition::BeforePost,46 .reph_mode = RephMode::Implicit,47 .below_pre_and_post = true48 };49 ::Bengali IndicConfig {50 .old_spec = true,51 .virama = 0x09CD,52 .reph_position = IndicPosition::AfterSub,53 .reph_mode = RephMode::Implicit,54 .below_pre_and_post = true55 };56 ::Gurmukhi IndicConfig {57 .old_spec = true,58 .virama = 0x0A4D,59 .reph_position = IndicPosition::BeforeSub,60 .reph_mode = RephMode::Implicit,61 .below_pre_and_post = true62 };63 ::Gujarati IndicConfig {64 .old_spec = true,65 .virama = 0x0ACD,66 .reph_position = IndicPosition::BeforePost,67 .reph_mode = RephMode::Implicit,68 .below_pre_and_post = true69 };70 ::Oriya IndicConfig {71 .old_spec = true,72 .virama = 0x0B4D,73 .reph_position = IndicPosition::AfterMain,74 .reph_mode = RephMode::Implicit,75 .below_pre_and_post = true76 };77 ::Tamil IndicConfig {78 .old_spec = true,79 .virama = 0x0BCD,80 .reph_position = IndicPosition::AfterPost,81 .reph_mode = RephMode::Implicit,82 .below_pre_and_post = true83 };84 ::Telugu IndicConfig {85 .old_spec = true,86 .virama = 0x0C4D,87 .reph_position = IndicPosition::AfterPost,88 .reph_mode = RephMode::Explicit,89 .below_pre_and_post = false90 };91 ::Kannada IndicConfig {92 .old_spec = true,93 .virama = 0x0CCD,94 .reph_position = IndicPosition::AfterPost,95 .reph_mode = RephMode::Implicit,96 .below_pre_and_post = false97 };98 ::Malayalam IndicConfig {99 .old_spec = true,100 .virama = 0x0D4D,101 .reph_position = IndicPosition::AfterMain,102 .reph_mode = RephMode::LogRepha,103 .below_pre_and_post = true104 };105 else IndicConfig {106 .old_spec = false,107 .virama = 0,108 .reph_position = IndicPosition::BeforePost,109 .reph_mode = RephMode::Implicit,110 .below_pre_and_post = true111 };112};113114// The features, in order: the basic ones applied one at a time after the115// initial reordering, then the others together after the final one.116const FEATURES: [&[u8] : 17] = [117 &"nukt", &"akhn", &"rphf", &"rkrf", &"pref", &"blwf", &"abvf", &"half", &"pstf", &"vatu",118 &"cjct", &"init", &"pres", &"abvs", &"blws", &"psts", &"haln"119];120// which are on everywhere121const GLOBAL: [bool : 17] = [122 true, true, false, true, false, false, false, false, false, true, true, false, true, true,123 true, true, true124];125const BASIC: u64 = 11;126const RPHF: u64 = 2;127const PREF: u64 = 4;128const BLWF: u64 = 5;129const ABVF: u64 = 6;130const HALF: u64 = 7;131const PSTF: u64 = 8;132const VATU: u64 = 9;133const INIT: u64 = 11;134135// What the shaper keeps in its plan: its script's configuration, whether136// the face follows the old specification, the virama's glyph, each137// feature's mask (0 for the global ones), the lookups of the features it138// asks the face about (rphf, pref, blwf, pstf, vatu), and the positions139// of the script's consonants as the face forms them.140pub type IndicData = struct {141 pub config: IndicConfig,142 pub old_spec: bool,143 pub virama_glyph: u32,144 pub masks: [u32 : 17],145 pub zero_context: bool,146 pub asked: [[u64 : 2] : 5],147 pub positions: Vector<[u32 : 2]>,148};149150pub fn collect_indic_features(b: &var Builder) {151 b.add_gsub_pause(Pause::IndicSyllables);152 b.enable_feature(tag(&"locl"), FeatureFlags { .per_syllable = true });153 b.enable_feature(tag(&"ccmp"), FeatureFlags { .per_syllable = true });154 b.add_gsub_pause(Pause::IndicInitialReordering);155 for (FEATURES, GLOBAL, [..FEATURES.length()]) |&name, global, k| {156 const flags = FeatureFlags {157 .global = global,158 .manual_zwnj = true,159 .manual_zwj = true,160 .per_syllable = true161 };162 b.add_feature(tag(&name), flags, 1);163 if (k < BASIC) {164 b.add_gsub_pause(Pause::Nothing);165 }166 if (k + 1 == BASIC) {167 b.add_gsub_pause(Pause::IndicFinalReordering);168 }169 }170}171172pub fn override_indic_features(b: &var Builder) {173 b.add_feature(tag(&"liga"), FeatureFlags { .global = true }, 0);174 b.add_gsub_pause(Pause::Nothing);175}176177pub fn indic_data(plan: &Plan, face: &Face) -> IndicData {178 const config = config_of(plan.script);179 const old_spec = config.old_spec && (plan.chosen[0] & 0xFF) != ('2' to u32);180 var data = IndicData {181 .config = config,182 .old_spec = old_spec,183 .virama_glyph = if (config.virama == 0) 0 else face.glyph_of(config.virama),184 .masks = [0 : 17],185 .zero_context = !old_spec && !(plan.script is ::Malayalam),186 .asked = [[0, 0] : 5],187 .positions = Vector<[u32 : 2]>::empty()188 };189 for (FEATURES, GLOBAL, [..FEATURES.length()]) |&name, global, k| {190 data.masks[k] = if (global) 0 else plan.one_mask(tag(&name));191 }192 const asked: [u64 : 5] = [RPHF, PREF, BLWF, PSTF, VATU];193 for (asked, [..5 to u64]) |feature, k| {194 data.asked[k] = stage_lookups(&plan, tag(&FEATURES[feature]));195 }196 // the consonants of the script's block, their positions asked once197 if (data.virama_glyph != 0) {198 const block = config.virama & ~0x7F;199 for ([block..block + 0x80]) |c| {200 const p = indic_properties(c);201 if (p.position is ::BaseC) {202 const glyph = face.glyph_of(c);203 if (glyph != 0) {204 const position = consonant_position(&plan, &data, &face, glyph);205 data.positions.push([glyph, position to u32]);206 }207 }208 }209 sort_pairs(&var data.positions);210 }211 return move data;212}213214// The range of the plan's GSUB lookups in the stage of the feature.215fn stage_lookups(plan: &Plan, feature: u32) -> [u64 : 2] {216 const f = if (plan.feature(feature) is ::Some |f|) f else {217 return [0, 0];218 };219 const stage = f.stage[0] to u64;220 const stages = &plan.substitution_stages.view();221 if (stage >= stages.length()) {222 return [0, 0];223 }224 const start = if (stage == 0) 0 else stages[stage - 1].end;225 return [start, stages[stage].end];226}227228fn sort_pairs(pairs: &var Vector<[u32 : 2]>) {229 // insertion sort: a script has a few dozen consonants230 const items = &var pairs.items();231 for ([min_u64(1, items.length())..items.length()]) |i| {232 var j = i;233 const moved = items[i];234 while (j > 0 && items[j - 1][0] > moved[0]) {235 items[j] = items[j - 1];236 j -= 1;237 }238 items[j] = moved;239 }240}241242// Whether one of the lookups the shaper asks about (by its index in243// `asked`) would substitute the glyphs.244fn would_substitute_feature(245 plan: &Plan,246 data: &IndicData,247 face: &Face,248 asked: u64,249 glyphs: &[u32]250) -> bool {251 const range = data.asked[asked];252 for ([range[0]..range[1]]) |k| {253 if (would_substitute(&face, plan.substitutions.get(k).index, &glyphs, data.zero_context)) {254 return true;255 }256 }257 return false;258}259260const ASK_RPHF: u64 = 0;261const ASK_PREF: u64 = 1;262const ASK_BLWF: u64 = 2;263const ASK_PSTF: u64 = 3;264const ASK_VATU: u64 = 4;265266// Where a consonant goes as the face forms it after a virama (or before267// one, as old fonts have it): below, after, or as the base.268fn consonant_position(plan: &Plan, data: &IndicData, face: &Face, consonant: u32) -> IndicPosition {269 const glyphs: [u32 : 3] = [data.virama_glyph, consonant, data.virama_glyph];270 const first = &glyphs[..2];271 const second = &glyphs[1..];272 if (273 would_substitute_feature(&plan, &data, &face, ASK_BLWF, &first)274 || would_substitute_feature(&plan, &data, &face, ASK_BLWF, &second)275 || would_substitute_feature(&plan, &data, &face, ASK_VATU, &first)276 || would_substitute_feature(&plan, &data, &face, ASK_VATU, &second)277 ) {278 return IndicPosition::BelowC;279 }280 if (281 would_substitute_feature(&plan, &data, &face, ASK_PSTF, &first)282 || would_substitute_feature(&plan, &data, &face, ASK_PSTF, &second)283 || would_substitute_feature(&plan, &data, &face, ASK_PREF, &first)284 || would_substitute_feature(&plan, &data, &face, ASK_PREF, &second)285 ) {286 return IndicPosition::PostC;287 }288 return IndicPosition::BaseC;289}290291fn known_position(plan: &Plan, data: &IndicData, face: &Face, glyph: u32) -> IndicPosition {292 const items = &data.positions.view();293 var low: u64 = 0;294 var high = items.length();295 while (low < high) {296 const middle = (low + high) / 2;297 if (items[middle][0] < glyph) {298 low = middle + 1;299 } else {300 high = middle;301 }302 }303 if (low < items.length() && items[low][0] == glyph) {304 return items[low][1] to IndicPosition;305 }306 return consonant_position(&plan, &data, &face, glyph);307}308309// Each character's category and position.310pub fn setup_indic_masks(buffer: &var Buffer) {311 for (buffer.infos.items()) |&var info| {312 const p = indic_properties(info.glyph);313 info.category = p.category to u8;314 info.position = p.position to u8;315 }316}317318// The categories as bytes, for patterns319const X: u8 = 0;320const C: u8 = 1;321const V: u8 = 2;322const N: u8 = 3;323const H: u8 = 4;324const ZWNJ: u8 = 5;325const ZWJ: u8 = 6;326const M: u8 = 7;327const SM: u8 = 8;328const PLACEHOLDER: u8 = 10;329const DOTTED_CIRCLE: u8 = 11;330const RS: u8 = 12;331const MPST: u8 = 13;332const REPHA: u8 = 14;333const RA: u8 = 15;334const CM: u8 = 16;335const CS: u8 = 18;336337// The positions as bytes338const POS_START: u8 = 0;339const POS_RA_TO_BECOME_REPH: u8 = 1;340const POS_PRE_M: u8 = 2;341const POS_PRE_C: u8 = 3;342const POS_BASE_C: u8 = 4;343const POS_AFTER_MAIN: u8 = 5;344const POS_BELOW_C: u8 = 8;345const POS_POST_C: u8 = 11;346const POS_AFTER_POST: u8 = 12;347const POS_SMVD: u8 = 13;348const POS_END: u8 = 14;349350// Vowels and placeholders count as consonants: a vowel syllable is351// reordered as a consonant one.352fn is_consonant(info: &GlyphInfo) -> bool => !info.ligated() && match (info.category) {353 C, CS, RA, CM, V, PLACEHOLDER, DOTTED_CIRCLE true;354 else false;355};356357fn is_joiner(info: &GlyphInfo) -> bool => !info.ligated()358 && (info.category == ZWJ || info.category == ZWNJ);359360fn is_halant(info: &GlyphInfo) -> bool => !info.ligated() && info.category == H;361362fn is_matra_or_halant(info: &GlyphInfo) -> bool => !info.ligated()363 && (info.category == M || info.category == MPST || info.category == H);364365// The syllables, found by the Indic machine.366pub fn find_indic_syllables(buffer: &var Buffer) {367 var categories = Vector<u8>::with_capacity(buffer.infos.length());368 for (buffer.infos.view()) |&info| {369 categories.push(info.category);370 }371 const none: [u64 : 0] = [];372 if (find_syllables(373 &INDIC_MACHINE,374 &categories.view(),375 &none,376 &var buffer.infos.items(),377 IndicSyllable::BrokenCluster to u8378 )) {379 buffer.has_broken_syllable = true;380 }381}382383pub fn initial_indic_reordering(plan: &Plan, data: &IndicData, face: &Face, buffer: &var Buffer) {384 // the positions of the consonants the face forms below or after385 if (data.virama_glyph != 0) {386 for (buffer.infos.items()) |&var info| {387 if (info.position == POS_BASE_C) {388 info.position = known_position(&plan, &data, &face, info.glyph) to u8;389 }390 }391 }392 insert_dotted_circles(393 &face,394 &var buffer,395 IndicSyllable::BrokenCluster to u8,396 DOTTED_CIRCLE,397 ::Some(REPHA),398 POS_END399 );400 var start: u64 = 0;401 while (start < buffer.infos.length()) {402 const end = buffer.syllable_end(start);403 const kind = buffer.infos.get(start).syllable & 0x0F;404 if (405 kind != IndicSyllable::SymbolCluster to u8406 && kind != IndicSyllable::NonIndicCluster to u8407 ) {408 reorder_consonant_syllable(&plan, &data, &face, &var buffer, start, end);409 }410 start = end;411 }412}413414fn reorder_consonant_syllable(415 plan: &Plan,416 data: &IndicData,417 face: &Face,418 buffer: &var Buffer,419 start: u64,420 end: u64421) {422 const config = &data.config;423 // Kannada's Ra, H, ZWJ behaves as Ra, ZWJ, H424 if (425 buffer.script is ::Kannada426 && start + 3 <= end427 && is_category(&buffer.infos.get(start), RA)428 && is_category(&buffer.infos.get(start + 1), H)429 && is_category(&buffer.infos.get(start + 2), ZWJ)430 ) {431 buffer.merge_clusters(start + 1, start + 3);432 swap(&var buffer, start + 1, start + 2);433 }434 // 1. the base consonant: from the end, the first consonant without a435 // below-base or post-base form, a reph's Ra left out436 var base = end;437 var has_reph = false;438 var limit = start;439 {440 const infos = &buffer.infos.view();441 if (442 data.masks[RPHF] != 0443 && start + 3 <= end444 && (445 (config.reph_mode is ::Implicit && !is_joiner(&infos[start + 2]))446 || (config.reph_mode is ::Explicit && infos[start + 2].category == ZWJ)447 )448 ) {449 const explicit = config.reph_mode is ::Explicit;450 const glyphs: [u32 : 3] = [451 infos[start].glyph,452 infos[start + 1].glyph,453 if (explicit) infos[start + 2].glyph else 0454 ];455 if (456 would_substitute_feature(&plan, &data, &face, ASK_RPHF, &glyphs[..2])457 || (458 explicit459 && would_substitute_feature(&plan, &data, &face, ASK_RPHF, &glyphs[..])460 )461 ) {462 limit += 2;463 while (limit < end && is_joiner(&infos[limit])) {464 limit += 1;465 }466 base = start;467 has_reph = true;468 }469 } else if (config.reph_mode is ::LogRepha && infos[start].category == REPHA) {470 limit += 1;471 while (limit < end && is_joiner(&infos[limit])) {472 limit += 1;473 }474 base = start;475 has_reph = true;476 }477 var i = end;478 var seen_below = false;479 while (true) {480 i -= 1;481 if (is_consonant(&infos[i])) {482 const position = infos[i].position;483 if (position != POS_BELOW_C && (position != POS_POST_C || seen_below)) {484 base = i;485 break;486 }487 if (position == POS_BELOW_C) {488 seen_below = true;489 }490 base = i;491 } else if (start < i && infos[i].category == ZWJ && infos[i - 1].category == H) {492 // a ZWJ after a halant asks for a half form and ends the search493 break;494 }495 if (i <= limit) {496 break;497 }498 }499 // a Ra alone with its halant is no reph but the base500 if (has_reph && base == start && limit - base <= 2) {501 has_reph = false;502 }503 }504 // before the base, at most pre-base505 for (buffer.infos.items()[start..base]) |&var info| {506 if (info.position > POS_PRE_C) {507 info.position = POS_PRE_C;508 }509 }510 if (base < end) {511 buffer.infos.get_var(base).position = POS_BASE_C;512 }513 if (has_reph) {514 buffer.infos.get_var(start).position = POS_RA_TO_BECOME_REPH;515 }516 // the old specification's first halant after the base goes after the517 // last consonant (not in Kannada when one is there already)518 if (data.old_spec) {519 const no_double = buffer.script is ::Kannada;520 for ([min_u64(base + 1, end)..end]) |i| {521 if (buffer.infos.get(i).category != H) {522 continue;523 }524 var j = end - 1;525 while (j > i) {526 const info = &buffer.infos.get(j);527 if (is_consonant(&info) || (no_double && info.category == H)) {528 break;529 }530 j -= 1;531 }532 if (buffer.infos.get(j).category != H && j > i) {533 const moved = buffer.infos.get(i);534 const infos = &var buffer.infos.items();535 for ([i..j]) |k| {536 infos[k] = infos[k + 1];537 }538 infos[j] = moved;539 }540 break;541 }542 }543 // marks attached to the character before them, to move with it544 {545 var last = POS_START;546 for ([start..end]) |i| {547 const info = buffer.infos.get(i);548 const c = info.category;549 if (c == ZWJ || c == ZWNJ || c == N || c == RS || c == CM || c == H) {550 buffer.infos.get_var(i).position = last;551 if (c == H && last == POS_PRE_M) {552 // Uniscribe does not move a halant with a left matra553 var j = i;554 while (j > start) {555 if (buffer.infos.get(j - 1).position != POS_PRE_M) {556 buffer.infos.get_var(i).position = buffer.infos.get(j - 1).position;557 break;558 }559 j -= 1;560 }561 }562 } else if (info.position != POS_SMVD) {563 if (c == MPST && i > start && buffer.infos.get(i - 1).category == SM) {564 buffer.infos.get_var(i - 1).position = info.position;565 }566 last = info.position;567 }568 }569 }570 // post-base consonants own what is before them since the last571 // consonant or matra572 {573 var last = base;574 for ([min_u64(base + 1, end)..end]) |i| {575 if (is_consonant(&buffer.infos.get(i))) {576 const position = buffer.infos.get(i).position;577 for ([last + 1..i]) |j| {578 if (buffer.infos.get(j).position < POS_SMVD) {579 buffer.infos.get_var(j).position = position;580 }581 }582 last = i;583 } else {584 const c = buffer.infos.get(i).category;585 if (c == M || c == MPST) {586 last = i;587 }588 }589 }590 }591 // the syllable sorted by position, each glyph's former place kept in592 // its syllable byte meanwhile593 {594 const syllable = buffer.infos.get(start).syllable;595 for ([start..end]) |i| {596 buffer.infos.get_var(i).syllable = (i - start) to u8;597 }598 stable_sort_by_position(&var buffer.infos.items()[start..end]);599 // the base again, and a sequence of left matras flipped600 var first_left: u64 = end;601 var last_left: u64 = end;602 base = end;603 for ([start..end]) |i| {604 const position = buffer.infos.get(i).position;605 if (position == POS_BASE_C) {606 base = i;607 break;608 }609 if (position == POS_PRE_M) {610 if (first_left == end) {611 first_left = i;612 }613 last_left = i;614 }615 }616 if (first_left < last_left) {617 buffer.reverse_range(first_left, last_left + 1);618 // nuktas and the like back after their matras619 var i = first_left;620 for ([first_left..last_left + 1]) |j| {621 const c = buffer.infos.get(j).category;622 if (c == M || c == MPST) {623 buffer.reverse_range(i, j + 1);624 i = j + 1;625 }626 }627 }628 // the clusters of what moved after the base merged629 if (data.old_spec || end - start > 127) {630 buffer.merge_clusters(base, end);631 } else {632 for ([base..end]) |i| {633 if (buffer.infos.get(i).syllable == 255) {634 continue;635 }636 var low = i;637 var high = i;638 var j = start + buffer.infos.get(i).syllable to u64;639 while (j != i) {640 low = if (j < low) j else low;641 high = if (j > high) j else high;642 const next = start + buffer.infos.get(j).syllable to u64;643 buffer.infos.get_var(j).syllable = 255;644 j = next;645 }646 buffer.merge_clusters(if (base > low) base else low, high + 1);647 }648 }649 for ([start..end]) |i| {650 buffer.infos.get_var(i).syllable = syllable;651 }652 }653 // the masks654 {655 var i = start;656 while (i < end && buffer.infos.get(i).position == POS_RA_TO_BECOME_REPH) {657 buffer.infos.get_var(i).mask |= data.masks[RPHF];658 i += 1;659 }660 var pre = data.masks[HALF];661 if (!data.old_spec && config.below_pre_and_post) {662 pre |= data.masks[BLWF];663 }664 for (buffer.infos.items()[start..base]) |&var info| {665 info.mask |= pre;666 }667 const post = data.masks[BLWF] | data.masks[ABVF] | data.masks[PSTF];668 if (base < end) {669 for (buffer.infos.items()[base + 1..end]) |&var info| {670 info.mask |= post;671 }672 }673 }674 // the old specification's eyelash Ra in Devanagari takes `blwf` before the base675 if (data.old_spec && buffer.script is ::Devanagari) {676 var i = start;677 while (i + 1 < base) {678 const infos = &buffer.infos.view();679 if (680 infos[i].category == RA681 && infos[i + 1].category == H682 && (i + 2 == base || infos[i + 2].category != ZWJ)683 ) {684 buffer.infos.get_var(i).mask |= data.masks[BLWF];685 buffer.infos.get_var(i + 1).mask |= data.masks[BLWF];686 }687 i += 1;688 }689 }690 // a halant and Ra the face forms a pre-base form of691 if (data.masks[PREF] != 0 && base + 2 < end) {692 var i = base + 1;693 while (i + 1 < end) {694 const glyphs: [u32 : 2] = [buffer.infos.get(i).glyph, buffer.infos.get(i + 1).glyph];695 if (would_substitute_feature(&plan, &data, &face, ASK_PREF, &glyphs)) {696 buffer.infos.get_var(i).mask |= data.masks[PREF];697 buffer.infos.get_var(i + 1).mask |= data.masks[PREF];698 break;699 }700 i += 1;701 }702 }703 // a ZWNJ keeps what is before it from half forms704 for ([start + 1..end]) |i| {705 if (!is_joiner(&buffer.infos.get(i))) {706 continue;707 }708 const non_joiner = buffer.infos.get(i).category == ZWNJ;709 var j = i;710 while (true) {711 j -= 1;712 if (non_joiner) {713 buffer.infos.get_var(j).mask &= ~data.masks[HALF];714 }715 if (j <= start || is_consonant(&buffer.infos.get(j))) {716 break;717 }718 }719 }720}721722fn is_category(info: &GlyphInfo, category: u8) -> bool => !info.ligated()723 && info.category == category;724725fn swap(buffer: &var Buffer, a: u64, b: u64) {726 const t = buffer.infos.get(a);727 buffer.infos.get_var(a) = buffer.infos.get(b);728 buffer.infos.get_var(b) = t;729}730731// An insertion sort by position, keeping the order of equal ones.732fn stable_sort_by_position(infos: &var [GlyphInfo]) {733 for ([min_u64(1, infos.length())..infos.length()]) |i| {734 const moved = infos[i];735 var j = i;736 while (j > 0 && infos[j - 1].position > moved.position) {737 infos[j] = infos[j - 1];738 j -= 1;739 }740 infos[j] = moved;741 }742}743744pub fn final_indic_reordering(plan: &Plan, data: &IndicData, buffer: &var Buffer) {745 var start: u64 = 0;746 while (start < buffer.infos.length()) {747 const end = buffer.syllable_end(start);748 final_reorder_syllable(&plan, &data, &var buffer, start, end);749 start = end;750 }751}752753fn final_reorder_syllable(754 plan: &Plan,755 data: &IndicData,756 buffer: &var Buffer,757 start: u64,758 end: u64759) {760 const config = &data.config;761 // a virama ligated and multiplied again is a halant still762 if (data.virama_glyph != 0) {763 for (buffer.infos.items()[start..end]) |&var info| {764 if (info.glyph == data.virama_glyph && info.ligated() && info.multiplied()) {765 info.category = H;766 info.clear_ligated_and_multiplied();767 }768 }769 }770 var try_pref = data.masks[PREF] != 0;771 // the base again772 var base = start;773 while (base < end) {774 if (buffer.infos.get(base).position >= POS_BASE_C) {775 if (try_pref && base + 1 < end) {776 for ([base + 1..end]) |i| {777 if ((buffer.infos.get(i).mask & data.masks[PREF]) != 0) {778 const info = &buffer.infos.get(i);779 if (!(info.substituted() && info.ligated() && !info.multiplied())) {780 // a `pref` candidate that formed nothing: the base is about here781 base = i;782 while (base < end && is_halant(&buffer.infos.get(base))) {783 base += 1;784 }785 if (base < end) {786 buffer.infos.get_var(base).position = POS_BASE_C;787 }788 try_pref = false;789 }790 break;791 }792 }793 if (base == end) {794 break;795 }796 }797 // Malayalam skips below forms not formed (but not post-base ones)798 if (buffer.script is ::Malayalam) {799 var i = base + 1;800 while (i < end) {801 while (i < end && is_joiner(&buffer.infos.get(i))) {802 i += 1;803 }804 if (i == end || !is_halant(&buffer.infos.get(i))) {805 break;806 }807 i += 1;808 while (i < end && is_joiner(&buffer.infos.get(i))) {809 i += 1;810 }811 if (812 i < end813 && is_consonant(&buffer.infos.get(i))814 && buffer.infos.get(i).position == POS_BELOW_C815 ) {816 base = i;817 buffer.infos.get_var(base).position = POS_BASE_C;818 }819 i += 1;820 }821 }822 if (start < base && buffer.infos.get(base).position > POS_BASE_C) {823 base -= 1;824 }825 break;826 }827 base += 1;828 }829 if (base == end && start < base && is_category(&buffer.infos.get(base - 1), ZWJ)) {830 base -= 1;831 }832 if (base < end) {833 while (834 start < base835 && (is_category(&buffer.infos.get(base), N) || is_category(&buffer.infos.get(base), H))836 ) {837 base -= 1;838 }839 }840 // pre-base matras moved closer to the base: after the last halant841 // before it that no ZWJ follows842 if (start + 1 < end && start < base) {843 var new_position = if (base == end) base - 2 else base - 1;844 if (!(buffer.script is ::Malayalam) && !(buffer.script is ::Tamil)) {845 while (true) {846 while (847 new_position > start848 && !is_matra_or_halant(&buffer.infos.get(new_position))849 ) {850 new_position -= 1;851 }852 if (853 is_halant(&buffer.infos.get(new_position))854 && buffer.infos.get(new_position).position != POS_PRE_M855 ) {856 if (857 new_position + 1 < end858 && buffer.infos.get(new_position + 1).category == ZWJ859 && new_position > start860 ) {861 // keep looking862 new_position -= 1;863 continue;864 }865 } else {866 new_position = start;867 }868 break;869 }870 }871 if (start < new_position && buffer.infos.get(new_position).position != POS_PRE_M) {872 var i = new_position;873 while (i > start) {874 if (buffer.infos.get(i - 1).position == POS_PRE_M) {875 const old_position = i - 1;876 if (old_position < base && base <= new_position) {877 base -= 1;878 }879 move_to(&var buffer, old_position, new_position);880 buffer.merge_clusters(new_position, min_u64(end, base + 1));881 new_position -= 1;882 }883 i -= 1;884 }885 } else {886 for ([start..base]) |i| {887 if (buffer.infos.get(i).position == POS_PRE_M) {888 buffer.merge_clusters(i, min_u64(end, base + 1));889 break;890 }891 }892 }893 }894 // the reph moved: written Ra, H only when it formed the reph, an895 // encoded repha only when it did not ligate896 if (897 start + 1 < end898 && buffer.infos.get(start).position == POS_RA_TO_BECOME_REPH899 && ((buffer.infos.get(start).category == REPHA) != ligated_alone(&buffer.infos.get(start)))900 ) {901 const new_reph = reph_target(&data, &buffer, start, end, base);902 buffer.merge_clusters(start, new_reph + 1);903 move_to(&var buffer, start, new_reph);904 if (start < base && base <= new_reph) {905 base -= 1;906 }907 }908 // a pre-base reordering consonant `pref` formed moves before the base909 if (try_pref && base + 1 < end) {910 for ([base + 1..end]) |i| {911 if ((buffer.infos.get(i).mask & data.masks[PREF]) == 0) {912 continue;913 }914 if (ligated_alone(&buffer.infos.get(i))) {915 var new_position = base;916 if (!(buffer.script is ::Malayalam) && !(buffer.script is ::Tamil)) {917 while (918 new_position > start919 && !is_matra_or_halant(&buffer.infos.get(new_position - 1))920 ) {921 new_position -= 1;922 }923 }924 if (new_position > start && is_halant(&buffer.infos.get(new_position - 1))) {925 if (new_position < end && is_joiner(&buffer.infos.get(new_position))) {926 new_position += 1;927 }928 }929 buffer.merge_clusters(new_position, i + 1);930 move_to(&var buffer, i, new_position);931 if (new_position <= base && base < i) {932 base += 1;933 }934 }935 break;936 }937 }938 // `init` on a left matra starting a word939 if (buffer.infos.get(start).position == POS_PRE_M) {940 if (start == 0 || !is_format_to_mark(buffer.infos.get(start - 1).general())) {941 buffer.infos.get_var(start).mask |= data.masks[INIT];942 }943 }944}945946// Format, Unassigned, PrivateUse, Surrogate, LowercaseLetter,947// ModifierLetter, OtherLetter, TitlecaseLetter, UppercaseLetter,948// SpacingMark, EnclosingMark or NonspacingMark: what a word goes on with.949fn is_format_to_mark(950 category: GeneralCategory951) -> bool => category to u32 >= GeneralCategory::Format to u32952 && category to u32 <= GeneralCategory::NonspacingMark to u32;953954fn ligated_alone(info: &GlyphInfo) -> bool => info.ligated() && !info.multiplied();955956fn min_u64(a: u64, b: u64) -> u64 => if (a < b) a else b;957958// Moves the glyph at `from` to `target`, the others between shifting over.959fn move_to(buffer: &var Buffer, from: u64, target: u64) {960 const infos = &var buffer.infos.items();961 const moved = infos[from];962 if (from < target) {963 for ([from..target]) |k| {964 infos[k] = infos[k + 1];965 }966 } else {967 var k = from;968 while (k > target) {969 infos[k] = infos[k - 1];970 k -= 1;971 }972 }973 infos[target] = moved;974}975976// Where the reph goes, by the script's rules (the specification's steps977// 2 to 6 as HarfBuzz reads them).978fn reph_target(data: &IndicData, buffer: &Buffer, start: u64, end: u64, base: u64) -> u64 {979 const infos = &buffer.infos.view();980 const position = data.config.reph_position;981 // 2. after the first halant between the reph and the base982 if (!(position is ::AfterPost)) {983 if (after_halant(&infos, start, base) is ::Some |p|) {984 return p;985 }986 }987 // 3. after the main consonant988 if (position is ::AfterMain) {989 var p = base;990 while (p + 1 < end && infos[p + 1].position <= POS_AFTER_MAIN) {991 p += 1;992 }993 if (p < end) {994 return p;995 }996 }997 // 4. before the post-base consonants998 if (position is ::AfterSub) {999 var p = base;1000 while (p + 1 < end) {1001 const next = infos[p + 1].position;1002 if (next == POS_POST_C || next == POS_AFTER_POST || next == POS_SMVD) {1003 break;1004 }1005 p += 1;1006 }1007 if (p < end) {1008 return p;1009 }1010 }1011 // 5. as step 21012 if (after_halant(&infos, start, base) is ::Some |p|) {1013 return p;1014 }1015 // 6. the end of the syllable, before its vedic signs, and before a1016 // halant ending a matra1017 var p = end - 1;1018 while (p > start && infos[p].position == POS_SMVD) {1019 p -= 1;1020 }1021 if (is_halant(&infos[p])) {1022 var i = base + 1;1023 while (i < p) {1024 if (infos[i].category == M || infos[i].category == MPST) {1025 p -= 1;1026 }1027 i += 1;1028 }1029 }1030 return p;1031}10321033fn after_halant(infos: &[GlyphInfo], start: u64, base: u64) -> Option<u64> {1034 var p = start + 1;1035 while (p < base && !is_halant(&infos[p])) {1036 p += 1;1037 }1038 if (p < base && is_halant(&infos[p])) {1039 if (p + 1 < base && is_joiner(&infos[p + 1])) {1040 p += 1;1041 }1042 return ::Some(p);1043 }1044 return ::None;1045}10461047// No decomposition of a few letters fonts show whole.1048pub fn decompose_indic(c: u32) -> Option<[u32 : 2]> {1049 match (c) {1050 0x0931, 0x09DC, 0x09DD, 0x0B94 {1051 return ::None;1052 }1053 }1054 return canonical_decomposition(c);1055}10561057// No split matras composed again, and Bengali's Yya composed though it1058// is excluded.1059pub fn compose_indic(a: u32, b: u32) -> Option<u32> {1060 const category = general_category(a);1061 if (category is ::SpacingMark || category is ::EnclosingMark || category is ::NonspacingMark) {1062 return ::None;1063 }1064 if (a == 0x09AF && b == 0x09BC) {1065 return ::Some(0x09DF);1066 }1067 return unicode_composition(a, b);1068}