1use alloc::boxed::Box;
2use core::cmp;
3use core::ops::Range;
4
5use read_fonts::types::GlyphId;
6
7use super::syllabic::*;
8use super::*;
9use crate::algs::*;
10use crate::ot::apply::WouldApplyContext;
11use crate::ot::layout::*;
12use crate::ot::map::*;
13use crate::ot::OtData;
14use crate::plan::ShapePlan;
15use crate::unicode::GeneralCategory;
16use crate::unicode::{CharExt, Codepoint};
17use crate::ShaperFont;
18use crate::{GlyphInfo, Mask, Script, Tag};
19
20pub const INDIC_SHAPER: OtShaper = OtShaper {
21 collect_features: Some(collect_features),
22 override_features: Some(override_features),
23 create_data: Some(|plan| Box::new(IndicShapePlan::new(plan))),
24 preprocess_text: Some(preprocess_text),
25 postprocess_glyphs: None,
26 normalization_preference: NormalizationMode::ComposedDiacriticsNoShortCircuit,
27 decompose: Some(decompose),
28 compose: Some(compose),
29 setup_masks: Some(setup_masks),
30 gpos_tag: None,
31 reorder_marks: None,
32 zero_width_marks: ZeroWidthMarks::None,
33 fallback_position: false,
34};
35
36impl GlyphInfo {
37 declare_buffer_var_alias!(
38 OT_SHAPER_VAR_U8_CATEGORY_VAR,
39 u8,
40 INDIC_CATEGORY_VAR,
41 indic_category,
42 set_indic_category
43 );
44 declare_buffer_var_alias!(
45 OT_SHAPER_VAR_U8_AUXILIARY_VAR,
46 u8,
47 INDIC_POSITION_VAR,
48 indic_position,
49 set_indic_position
50 );
51
52 fn is_one_of(&self, flags: u32) -> bool {
53 if self.ligated() {
55 return false;
56 }
57
58 rb_flag_unsafe(self.indic_category() as u32) & flags != 0
59 }
60
61 fn is_joiner(&self) -> bool {
62 self.is_one_of(JOINER_FLAGS)
63 }
64
65 pub(crate) fn is_consonant(&self) -> bool {
66 self.is_one_of(CONSONANT_FLAGS_INDIC)
67 }
68
69 fn is_halant(&self) -> bool {
70 self.is_one_of(rb_flag(category::OT_H as u32))
71 }
72
73 fn set_indic_properties(&mut self) {
74 let u = self.glyph_id;
75 let (cat, pos) = indic_table::get_categories(u);
76
77 self.set_indic_category(cat);
78 self.set_indic_position(pos);
79 }
80}
81
82pub type Category = u8;
83
84#[allow(dead_code)]
88pub mod category {
89 pub const OT_X: u8 = 0;
90 pub const OT_C: u8 = 1;
91 pub const OT_V: u8 = 2;
92 pub const OT_N: u8 = 3;
93 pub const OT_H: u8 = 4;
94 pub const OT_ZWNJ: u8 = 5;
95 pub const OT_ZWJ: u8 = 6;
96 pub const OT_M: u8 = 7;
97 pub const OT_SM: u8 = 8;
98 pub const OT_A: u8 = 9;
99 pub const OT_VD: u8 = OT_A;
100 pub const OT_PLACEHOLDER: u8 = 10;
101 pub const OT_GB: u8 = OT_PLACEHOLDER;
102 pub const OT_DOTTEDCIRCLE: u8 = 11;
103 pub const OT_RS: u8 = 12; pub const OT_MPst: u8 = 13;
105 pub const OT_Repha: u8 = 14; pub const OT_Ra: u8 = 15;
107 pub const OT_CM: u8 = 16; pub const OT_Symbol: u8 = 17; pub const OT_CS: u8 = 18;
110
111 pub const OT_VAbv: u8 = 20;
113 pub const OT_VBlw: u8 = 21;
114 pub const OT_VPre: u8 = 22;
115 pub const OT_VPst: u8 = 23;
116
117 pub const OT_Robatic: u8 = 25;
119 pub const OT_Xgroup: u8 = 26;
120 pub const OT_Ygroup: u8 = 27;
121
122 pub const OT_As: u8 = 32; pub const OT_MH: u8 = 35; pub const OT_MR: u8 = 36; pub const OT_MW: u8 = 37; pub const OT_MY: u8 = 38; pub const OT_PT: u8 = 39; pub const OT_VS: u8 = 40; pub const OT_ML: u8 = 41; pub const OT_SMPst: u8 = 57; pub const IV: u8 = 2;
139}
140
141pub mod position {
142 pub const POS_START: u8 = 0;
143
144 pub const POS_RA_TO_BECOME_REPH: u8 = 1;
145 pub const POS_PRE_M: u8 = 2;
146 pub const POS_PRE_C: u8 = 3;
147
148 pub const POS_BASE_C: u8 = 4;
149 pub const POS_AFTER_MAIN: u8 = 5;
150
151 pub const POS_ABOVE_C: u8 = 6;
152
153 pub const POS_BEFORE_SUB: u8 = 7;
154 pub const POS_BELOW_C: u8 = 8;
155 pub const POS_AFTER_SUB: u8 = 9;
156
157 pub const POS_BEFORE_POST: u8 = 10;
158 pub const POS_POST_C: u8 = 11;
159 pub const POS_AFTER_POST: u8 = 12;
160
161 pub const POS_SMVD: u8 = 13;
162
163 pub const POS_END: u8 = 14;
164}
165
166const INDIC_FEATURES: &[(Tag, MapFeatureFlags)] = &[
167 (Tag::new(b"nukt"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
171 (Tag::new(b"akhn"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
172 (Tag::new(b"rphf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
173 (Tag::new(b"rkrf"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
174 (Tag::new(b"pref"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
175 (Tag::new(b"blwf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
176 (Tag::new(b"abvf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
177 (Tag::new(b"half"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
178 (Tag::new(b"pstf"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
179 (Tag::new(b"vatu"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
180 (Tag::new(b"cjct"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
181 (Tag::new(b"init"), F_MANUAL_JOINERS | F_PER_SYLLABLE),
187 (Tag::new(b"pres"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
188 (Tag::new(b"abvs"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
189 (Tag::new(b"blws"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
190 (Tag::new(b"psts"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
191 (Tag::new(b"haln"), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE),
192];
193
194#[allow(dead_code)]
196mod indic_feature {
197 pub const NUKT: usize = 0;
198 pub const AKHN: usize = 1;
199 pub const RPHF: usize = 2;
200 pub const RKRF: usize = 3;
201 pub const PREF: usize = 4;
202 pub const BLWF: usize = 5;
203 pub const ABVF: usize = 6;
204 pub const HALF: usize = 7;
205 pub const PSTF: usize = 8;
206 pub const VATU: usize = 9;
207 pub const CJCT: usize = 10;
208 pub const INIT: usize = 11;
209 pub const PRES: usize = 12;
210 pub const ABVS: usize = 13;
211 pub const BLWS: usize = 14;
212 pub const PSTS: usize = 15;
213 pub const HALN: usize = 16;
214}
215
216pub(crate) const fn category_flag(c: Category) -> u32 {
217 rb_flag(c as u32)
218}
219
220const CONSONANT_FLAGS_INDIC: u32 = category_flag(category::OT_C)
226 | category_flag(category::OT_CS)
227 | category_flag(category::OT_Ra)
228 | category_flag(category::OT_CM)
229 | category_flag(category::OT_V)
230 | category_flag(category::OT_PLACEHOLDER)
231 | category_flag(category::OT_DOTTEDCIRCLE);
232
233const JOINER_FLAGS: u32 = category_flag(category::OT_ZWJ) | category_flag(category::OT_ZWNJ);
234
235#[derive(Clone, Copy, PartialEq)]
236enum RephPosition {
237 AfterMain = position::POS_AFTER_MAIN as isize,
238 BeforeSub = position::POS_BEFORE_SUB as isize,
239 AfterSub = position::POS_AFTER_SUB as isize,
240 BeforePost = position::POS_BEFORE_POST as isize,
241 AfterPost = position::POS_AFTER_POST as isize,
242}
243
244#[derive(Clone, Copy, PartialEq)]
245enum RephMode {
246 Implicit,
248 Explicit,
250 LogRepha,
252}
253
254#[derive(Clone, Copy, PartialEq)]
255enum BlwfMode {
256 PreAndPost,
258 PostOnly,
260}
261
262#[derive(Clone, Copy)]
263struct IndicConfig {
264 script: Option<Script>,
265 has_old_spec: bool,
266 virama: u32,
267 reph_pos: RephPosition,
268 reph_mode: RephMode,
269 blwf_mode: BlwfMode,
270}
271
272impl IndicConfig {
273 const fn new(
274 script: Option<Script>,
275 has_old_spec: bool,
276 virama: u32,
277 reph_pos: RephPosition,
278 reph_mode: RephMode,
279 blwf_mode: BlwfMode,
280 ) -> Self {
281 IndicConfig {
282 script,
283 has_old_spec,
284 virama,
285 reph_pos,
286 reph_mode,
287 blwf_mode,
288 }
289 }
290}
291
292static INDIC_CONFIGS: &[IndicConfig] = &[
293 IndicConfig::new(
294 None,
295 false,
296 0,
297 RephPosition::BeforePost,
298 RephMode::Implicit,
299 BlwfMode::PreAndPost,
300 ),
301 IndicConfig::new(
302 Some(Script::DEVANAGARI),
303 true,
304 0x094D,
305 RephPosition::BeforePost,
306 RephMode::Implicit,
307 BlwfMode::PreAndPost,
308 ),
309 IndicConfig::new(
310 Some(Script::BENGALI),
311 true,
312 0x09CD,
313 RephPosition::AfterSub,
314 RephMode::Implicit,
315 BlwfMode::PreAndPost,
316 ),
317 IndicConfig::new(
318 Some(Script::GURMUKHI),
319 true,
320 0x0A4D,
321 RephPosition::BeforeSub,
322 RephMode::Implicit,
323 BlwfMode::PreAndPost,
324 ),
325 IndicConfig::new(
326 Some(Script::GUJARATI),
327 true,
328 0x0ACD,
329 RephPosition::BeforePost,
330 RephMode::Implicit,
331 BlwfMode::PreAndPost,
332 ),
333 IndicConfig::new(
334 Some(Script::ORIYA),
335 true,
336 0x0B4D,
337 RephPosition::AfterMain,
338 RephMode::Implicit,
339 BlwfMode::PreAndPost,
340 ),
341 IndicConfig::new(
342 Some(Script::TAMIL),
343 true,
344 0x0BCD,
345 RephPosition::AfterPost,
346 RephMode::Implicit,
347 BlwfMode::PreAndPost,
348 ),
349 IndicConfig::new(
350 Some(Script::TELUGU),
351 true,
352 0x0C4D,
353 RephPosition::AfterPost,
354 RephMode::Explicit,
355 BlwfMode::PostOnly,
356 ),
357 IndicConfig::new(
358 Some(Script::KANNADA),
359 true,
360 0x0CCD,
361 RephPosition::AfterPost,
362 RephMode::Implicit,
363 BlwfMode::PostOnly,
364 ),
365 IndicConfig::new(
366 Some(Script::MALAYALAM),
367 true,
368 0x0D4D,
369 RephPosition::AfterMain,
370 RephMode::LogRepha,
371 BlwfMode::PreAndPost,
372 ),
373 IndicConfig::new(
374 Some(Script::SINHALA),
375 false,
376 0x0DCA,
377 RephPosition::AfterPost,
378 RephMode::Explicit,
379 BlwfMode::PreAndPost,
380 ),
381];
382
383struct IndicWouldSubstituteFeature {
384 lookups: Range<usize>,
385 zero_context: bool,
386}
387
388impl IndicWouldSubstituteFeature {
389 pub fn new(map: &OtMap, feature_tag: Tag, zero_context: bool) -> Self {
390 IndicWouldSubstituteFeature {
391 lookups: match map.get_feature_stage(LayoutTableKind::Gsub, feature_tag) {
392 Some(stage) => map.stage_lookup_range(LayoutTableKind::Gsub, stage),
393 None => 0..0,
394 },
395 zero_context,
396 }
397 }
398
399 pub fn would_substitute(&self, map: &OtMap, ot: &OtData<'_>, glyphs: &[GlyphId]) -> bool {
400 for index in self.lookups.clone() {
401 let lookup = map.lookup(LayoutTableKind::Gsub, index);
402 let ctx = WouldApplyContext {
403 glyphs,
404 zero_context: self.zero_context,
405 };
406 if ot
407 .lookup(LayoutTableKind::Gsub, lookup.index)
408 .is_some_and(|lookup| lookup.would_apply(&ctx) == Some(true))
409 {
410 return true;
411 }
412 }
413
414 false
415 }
416}
417
418struct IndicShapePlan {
419 config: IndicConfig,
420 is_old_spec: bool,
421 rphf: IndicWouldSubstituteFeature,
423 pref: IndicWouldSubstituteFeature,
424 blwf: IndicWouldSubstituteFeature,
425 pstf: IndicWouldSubstituteFeature,
426 vatu: IndicWouldSubstituteFeature,
427 mask_array: [Mask; INDIC_FEATURES.len()],
428}
429
430impl IndicShapePlan {
431 fn new(plan: &ShapePlan) -> Self {
432 let script = plan.script;
433 let config = if let Some(c) = INDIC_CONFIGS.iter().skip(1).find(|c| c.script == script) {
434 *c
435 } else {
436 INDIC_CONFIGS[0]
437 };
438
439 let is_old_spec = config.has_old_spec
440 && plan
441 .ot_map
442 .chosen_script(LayoutTableKind::Gsub)
443 .is_none_or(|tag| tag.to_be_bytes()[3] != b'2');
444
445 let zero_context = is_old_spec && script != Some(Script::MALAYALAM);
454
455 let mut mask_array = [0; INDIC_FEATURES.len()];
456 for (i, feature) in INDIC_FEATURES.iter().enumerate() {
457 mask_array[i] = if feature.1 & F_GLOBAL != 0 {
458 0
459 } else {
460 plan.ot_map.get_1_mask(feature.0)
461 }
462 }
463
464 IndicShapePlan {
473 config,
474 is_old_spec,
475 rphf: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"rphf"), zero_context),
477 pref: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"pref"), zero_context),
478 blwf: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"blwf"), zero_context),
479 pstf: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"pstf"), zero_context),
480 vatu: IndicWouldSubstituteFeature::new(&plan.ot_map, Tag::new(b"vatu"), zero_context),
481 mask_array,
482 }
483 }
484}
485
486fn collect_features(planner: &mut ShapePlanner) {
487 planner.ot_map.add_gsub_pause(Some(setup_syllables));
489
490 planner
491 .ot_map
492 .enable_feature(Tag::new(b"locl"), F_PER_SYLLABLE, 1);
493 planner
496 .ot_map
497 .enable_feature(Tag::new(b"ccmp"), F_PER_SYLLABLE, 1);
498
499 planner.ot_map.add_gsub_pause(Some(initial_reordering));
500
501 for feature in INDIC_FEATURES.iter().take(11) {
502 planner.ot_map.add_feature(feature.0, feature.1, 1);
503 planner.ot_map.add_gsub_pause(None);
504 }
505
506 planner.ot_map.add_gsub_pause(Some(final_reordering));
507
508 for feature in INDIC_FEATURES.iter().skip(11) {
509 planner.ot_map.add_feature(feature.0, feature.1, 1);
510 }
511}
512
513fn override_features(planner: &mut ShapePlanner) {
514 planner.ot_map.disable_feature(Tag::new(b"liga"));
515 planner.ot_map.add_gsub_pause(Some(syllabic_clear_var)); }
517
518fn preprocess_text(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<()> {
519 vowel_constraints::preprocess_text_vowel_constraints(buffer);
520 Some(())
521}
522
523fn decompose(_: &NormalizeContext, ab: Codepoint) -> Option<(Codepoint, Codepoint)> {
524 match ab {
526 0x0931 | 0x09DC | 0x09DD | 0x0B94 => return None, _ => {}
532 }
533
534 unicode::decompose_impl(ab)
535}
536
537fn compose(_: &NormalizeContext, a: Codepoint, b: Codepoint) -> Option<Codepoint> {
538 if a.general_category().is_mark() {
540 return None;
541 }
542
543 if a == 0x09AF && b == 0x09BC {
545 return Some(0x09DF);
546 }
547
548 unicode::compose(a, b)
549}
550
551fn setup_masks(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
552 buffer.allocate_var(GlyphInfo::INDIC_CATEGORY_VAR);
553 buffer.allocate_var(GlyphInfo::INDIC_POSITION_VAR);
554
555 for info in buffer.info_slice_mut() {
558 info.set_indic_properties();
559 }
560}
561
562fn setup_syllables(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
563 buffer.allocate_var(GlyphInfo::SYLLABLE_VAR);
564
565 indic_machine::find_syllables_indic(buffer);
566
567 let mut start = 0;
568 let mut end = buffer.next_syllable(0);
569 while start < buffer.len {
570 buffer.unsafe_to_break(Some(start), Some(end));
571 start = end;
572 end = buffer.next_syllable(start);
573 }
574
575 Some(false)
576}
577
578fn initial_reordering(
579 plan: &ShapePlan,
580 font: &ShaperFont<'_, '_>,
581 buffer: &mut Buffer,
582) -> Option<bool> {
583 use super::indic_machine::SyllableType;
584
585 let mut ret = false;
586 if !buffer_message!(buffer, font, "start reordering indic initial") {
587 return Some(ret);
588 }
589
590 let indic_plan = plan.data::<IndicShapePlan>();
591
592 update_consonant_positions(plan, indic_plan, font, buffer);
593 if insert_dotted_circles(
594 font,
595 buffer,
596 SyllableType::BrokenCluster as u8,
597 category::OT_DOTTEDCIRCLE,
598 Some(category::OT_Repha),
599 Some(position::POS_END),
600 )? {
601 ret = true;
602 }
603
604 let mut start = 0;
605 let mut end = buffer.next_syllable(0);
606 while start < buffer.len {
607 if initial_reordering_syllable(plan, indic_plan, font, start, end, buffer).is_none() {
608 buffer.index_failed();
609 }
610 start = end;
611 end = buffer.next_syllable(start);
612 }
613
614 buffer_message!(buffer, font, "end reordering indic initial");
615 Some(ret)
616}
617
618fn update_consonant_positions(
619 plan: &ShapePlan,
620 indic_plan: &IndicShapePlan,
621 font: &ShaperFont<'_, '_>,
622 buffer: &mut Buffer,
623) {
624 let mut virama_glyph = None;
625 if indic_plan.config.virama != 0 {
626 virama_glyph = font.nominal_glyph(indic_plan.config.virama);
627 }
628
629 if let Some(virama) = virama_glyph {
630 let ot = font.layout().ot;
631 for info in buffer.info_slice_mut() {
632 if info.indic_position() == position::POS_BASE_C {
633 let consonant = info.as_glyph();
634 info.set_indic_position(consonant_position_from_ot(
635 plan, indic_plan, ot, consonant, virama,
636 ));
637 }
638 }
639 }
640}
641
642fn consonant_position_from_ot(
643 plan: &ShapePlan,
644 indic_plan: &IndicShapePlan,
645 ot: &OtData<'_>,
646 consonant: GlyphId,
647 virama: GlyphId,
648) -> u8 {
649 if indic_plan
664 .blwf
665 .would_substitute(&plan.ot_map, ot, &[virama, consonant])
666 || indic_plan
667 .blwf
668 .would_substitute(&plan.ot_map, ot, &[consonant, virama])
669 || indic_plan
670 .vatu
671 .would_substitute(&plan.ot_map, ot, &[virama, consonant])
672 || indic_plan
673 .vatu
674 .would_substitute(&plan.ot_map, ot, &[consonant, virama])
675 {
676 return position::POS_BELOW_C;
677 }
678
679 if indic_plan
680 .pstf
681 .would_substitute(&plan.ot_map, ot, &[virama, consonant])
682 || indic_plan
683 .pstf
684 .would_substitute(&plan.ot_map, ot, &[consonant, virama])
685 {
686 return position::POS_POST_C;
687 }
688
689 if indic_plan
690 .pref
691 .would_substitute(&plan.ot_map, ot, &[virama, consonant])
692 || indic_plan
693 .pref
694 .would_substitute(&plan.ot_map, ot, &[consonant, virama])
695 {
696 return position::POS_POST_C;
697 }
698
699 position::POS_BASE_C
700}
701
702fn initial_reordering_syllable(
703 plan: &ShapePlan,
704 indic_plan: &IndicShapePlan,
705 font: &ShaperFont<'_, '_>,
706 start: usize,
707 end: usize,
708 buffer: &mut Buffer,
709) -> Option<()> {
710 use super::indic_machine::SyllableType;
711
712 let syllable_type = match buffer.info.get(start)?.syllable() & 0x0F {
713 0 => SyllableType::ConsonantSyllable,
714 1 => SyllableType::VowelSyllable,
715 2 => SyllableType::StandaloneCluster,
716 3 => SyllableType::SymbolCluster,
717 4 => SyllableType::BrokenCluster,
718 5 => SyllableType::NonIndicCluster,
719 _ => unreachable!(),
720 };
721
722 match syllable_type {
723 SyllableType::VowelSyllable | SyllableType::ConsonantSyllable => {
725 initial_reordering_consonant_syllable(plan, indic_plan, font, start, end, buffer)
726 }
727 SyllableType::BrokenCluster | SyllableType::StandaloneCluster => {
729 initial_reordering_standalone_cluster(plan, indic_plan, font, start, end, buffer)
730 }
731 SyllableType::SymbolCluster | SyllableType::NonIndicCluster => Some(()),
732 }
733}
734
735fn initial_reordering_consonant_syllable(
738 plan: &ShapePlan,
739 indic_plan: &IndicShapePlan,
740 font: &ShaperFont<'_, '_>,
741 start: usize,
742 end: usize,
743 buffer: &mut Buffer,
744) -> Option<()> {
745 let ot = font.layout().ot;
746 if buffer.script == Some(Script::KANNADA)
750 && start + 3 <= end
751 && buffer
752 .info
753 .get(start)?
754 .is_one_of(category_flag(category::OT_Ra))
755 && buffer
756 .info
757 .get(start + 1)?
758 .is_one_of(category_flag(category::OT_H))
759 && buffer
760 .info
761 .get(start + 2)?
762 .is_one_of(category_flag(category::OT_ZWJ))
763 {
764 buffer.merge_clusters(start + 1, start + 3);
765 buffer.info.get_mut(start + 1..start + 3)?.swap(0, 1);
766 }
767
768 let mut base = end;
782 let mut has_reph = false;
783
784 {
785 let mut limit = start;
789 if indic_plan.mask_array[indic_feature::RPHF] != 0
790 && start + 3 <= end
791 && ((indic_plan.config.reph_mode == RephMode::Implicit
792 && !buffer.info.get(start + 2)?.is_joiner())
793 || (indic_plan.config.reph_mode == RephMode::Explicit
794 && buffer.info.get(start + 2)?.indic_category() == category::OT_ZWJ))
795 {
796 let glyphs = &[
798 buffer.info.get(start)?.as_glyph(),
799 buffer.info.get(start + 1)?.as_glyph(),
800 if indic_plan.config.reph_mode == RephMode::Explicit {
801 buffer.info.get(start + 2)?.as_glyph()
802 } else {
803 GlyphId::NOTDEF
804 },
805 ];
806 if indic_plan
807 .rphf
808 .would_substitute(&plan.ot_map, ot, &glyphs[0..2])
809 || (indic_plan.config.reph_mode == RephMode::Explicit
810 && indic_plan.rphf.would_substitute(&plan.ot_map, ot, glyphs))
811 {
812 limit += 2;
813 while limit < end && buffer.info.get(limit)?.is_joiner() {
814 limit += 1;
815 }
816 base = start;
817 has_reph = true;
818 }
819 } else if indic_plan.config.reph_mode == RephMode::LogRepha
820 && buffer.info.get(start)?.indic_category() == category::OT_Repha
821 {
822 limit += 1;
823 while limit < end && buffer.info.get(limit)?.is_joiner() {
824 limit += 1;
825 }
826 base = start;
827 has_reph = true;
828 }
829
830 {
831 let mut i = end;
833 let mut seen_below = false;
834 loop {
835 i -= 1;
836 let info = buffer.info.get(i)?;
837 if info.is_consonant() {
839 if info.indic_position() != position::POS_BELOW_C
842 && (info.indic_position() != position::POS_POST_C || seen_below)
843 {
844 base = i;
845 break;
846 }
847 if info.indic_position() == position::POS_BELOW_C {
848 seen_below = true;
849 }
850
851 base = i;
861 } else {
862 if start < i
868 && info.indic_category() == category::OT_ZWJ
869 && buffer.info.get(i - 1)?.indic_category() == category::OT_H
870 {
871 break;
872 }
873 }
874
875 if i <= limit {
876 break;
877 }
878 }
879 }
880
881 if has_reph && base == start && limit - base <= 2 {
887 has_reph = false;
889 }
890 }
891
892 for info in buffer.info.get_mut(start..base)? {
923 info.set_indic_position(cmp::min(position::POS_PRE_C, info.indic_position()));
924 }
925
926 if base < end {
927 buffer
928 .info
929 .get_mut(base)?
930 .set_indic_position(position::POS_BASE_C);
931 }
932
933 if has_reph {
935 buffer
936 .info
937 .get_mut(start)?
938 .set_indic_position(position::POS_RA_TO_BECOME_REPH);
939 }
940
941 if indic_plan.is_old_spec {
969 let disallow_double_halants = buffer.script == Some(Script::KANNADA);
970 for i in base + 1..end {
971 if buffer.info.get(i)?.indic_category() == category::OT_H {
972 let mut j = end - 1;
973 while j > i {
974 let info = buffer.info.get(j)?;
975 if info.is_consonant()
976 || (disallow_double_halants && info.indic_category() == category::OT_H)
977 {
978 break;
979 }
980
981 j -= 1;
982 }
983
984 if buffer.info.get(j)?.indic_category() != category::OT_H && j > i {
985 buffer.info.get_mut(i..=j)?.rotate_left(1);
987 }
988
989 break;
990 }
991 }
992 }
993
994 {
996 let mut last_pos = position::POS_START;
997 for i in start..end {
998 let info = *buffer.info.get(i)?;
999 let ok = rb_flag_unsafe(info.indic_category() as u32)
1000 & (category_flag(category::OT_ZWJ)
1001 | category_flag(category::OT_ZWNJ)
1002 | category_flag(category::OT_N)
1003 | category_flag(category::OT_RS)
1004 | category_flag(category::OT_CM)
1005 | category_flag(category::OT_H))
1006 != 0;
1007 if ok {
1008 let mut pos = last_pos;
1009 if info.indic_category() == category::OT_H && pos == position::POS_PRE_M {
1010 for j in (start + 1..=i).rev() {
1014 let prev_pos = buffer.info.get(j - 1)?.indic_position();
1015 if prev_pos != position::POS_PRE_M {
1016 pos = prev_pos;
1017 break;
1018 }
1019 }
1020 }
1021
1022 buffer.info.get_mut(i)?.set_indic_position(pos);
1023 } else if info.indic_position() != position::POS_SMVD {
1024 if info.indic_category() == category::OT_MPst
1025 && i > start
1026 && buffer.info.get(i - 1)?.indic_category() == category::OT_SM
1027 {
1028 buffer
1029 .info
1030 .get_mut(i - 1)?
1031 .set_indic_position(info.indic_position());
1032 }
1033
1034 last_pos = info.indic_position();
1035 }
1036 }
1037 }
1038 {
1041 let mut last = base;
1042 for i in base + 1..end {
1043 let info = *buffer.info.get(i)?;
1044 if info.is_consonant() {
1045 for other in buffer.info.get_mut(last + 1..i)? {
1046 if other.indic_position() < position::POS_SMVD {
1047 other.set_indic_position(info.indic_position());
1048 }
1049 }
1050
1051 last = i;
1052 } else if (rb_flag_unsafe(info.indic_category() as u32)
1053 & (rb_flag(category::OT_M as u32) | rb_flag(category::OT_MPst as u32)))
1054 != 0
1055 {
1056 last = i;
1057 }
1058 }
1059 }
1060
1061 {
1062 let syllable = buffer.info.get(start)?.syllable();
1064 let infos = buffer.info.get_mut(start..end)?;
1065 for (i, info) in infos.iter_mut().enumerate() {
1066 info.set_syllable(i as u8);
1069 }
1070
1071 stable_sort_by(infos, |a, b| a.indic_position().cmp(&b.indic_position()));
1072
1073 let mut first_left_mantra = end;
1075 let mut last_left_mantra = end;
1076 base = end;
1077
1078 for (i, info) in (start..).zip(infos.iter()) {
1079 if info.indic_position() == position::POS_BASE_C {
1080 base = i;
1081 break;
1082 } else if info.indic_position() == position::POS_PRE_M {
1083 if first_left_mantra == end {
1084 first_left_mantra = i;
1085 }
1086
1087 last_left_mantra = i;
1088 }
1089 }
1090
1091 if first_left_mantra < last_left_mantra {
1096 buffer.reverse_range(first_left_mantra, last_left_mantra + 1);
1098 let mut i = first_left_mantra;
1100
1101 for j in i..=last_left_mantra {
1102 if (rb_flag_unsafe(buffer.info.get(j)?.indic_category() as u32)
1103 & (rb_flag(category::OT_M as u32) | rb_flag(category::OT_MPst as u32)))
1104 != 0
1105 {
1106 buffer.reverse_range(i, j + 1);
1107 i = j + 1;
1108 }
1109 }
1110 }
1111
1112 if indic_plan.is_old_spec || end - start > 127 {
1144 buffer.merge_clusters(base, end);
1145 } else {
1146 for i in base..end {
1148 let order = buffer.info.get(i)?.syllable();
1149 if order != 255 {
1150 let mut min = i;
1151 let mut max = i;
1152 let mut j = start + order as usize;
1153 while j != i {
1154 min = cmp::min(min, j);
1155 max = cmp::max(max, j);
1156 let info = buffer.info.get_mut(j)?;
1157 let next = start + info.syllable() as usize;
1158 info.set_syllable(255); j = next;
1160 }
1161
1162 buffer.merge_clusters(cmp::max(base, min), max + 1);
1163 }
1164 }
1165 }
1166
1167 for info in buffer.info.get_mut(start..end)? {
1169 info.set_syllable(syllable);
1170 }
1171 }
1172
1173 {
1176 for info in buffer.info.get_mut(start..end)? {
1178 if info.indic_position() != position::POS_RA_TO_BECOME_REPH {
1179 break;
1180 }
1181
1182 info.mask |= indic_plan.mask_array[indic_feature::RPHF];
1183 }
1184
1185 let mut mask = indic_plan.mask_array[indic_feature::HALF];
1187 if !indic_plan.is_old_spec && indic_plan.config.blwf_mode == BlwfMode::PreAndPost {
1188 mask |= indic_plan.mask_array[indic_feature::BLWF];
1189 }
1190
1191 for info in buffer.info.get_mut(start..base)? {
1192 info.mask |= mask;
1193 }
1194
1195 mask = 0;
1197 if base < end {
1198 buffer.info.get_mut(base)?.mask |= mask;
1199 }
1200
1201 mask = indic_plan.mask_array[indic_feature::BLWF]
1203 | indic_plan.mask_array[indic_feature::ABVF]
1204 | indic_plan.mask_array[indic_feature::PSTF];
1205 for i in base + 1..end {
1206 buffer.info.get_mut(i)?.mask |= mask;
1207 }
1208 }
1209
1210 if indic_plan.is_old_spec && buffer.script == Some(Script::DEVANAGARI) {
1211 for i in start..base.saturating_sub(1) {
1229 if buffer.info.get(i)?.indic_category() == category::OT_Ra
1230 && buffer.info.get(i + 1)?.indic_category() == category::OT_H
1231 && (i + 2 == base || buffer.info.get(i + 2)?.indic_category() != category::OT_ZWJ)
1232 {
1233 for info in buffer.info.get_mut(i..i + 2)? {
1234 info.mask |= indic_plan.mask_array[indic_feature::BLWF];
1235 }
1236 }
1237 }
1238 }
1239
1240 let pref_len = 2;
1241 if indic_plan.mask_array[indic_feature::PREF] != 0 && base + pref_len < end {
1242 for i in base + 1..end - pref_len + 1 {
1244 let glyphs = &[
1245 buffer.info.get(i)?.as_glyph(),
1246 buffer.info.get(i + 1)?.as_glyph(),
1247 ];
1248 if indic_plan.pref.would_substitute(&plan.ot_map, ot, glyphs) {
1249 for info in buffer.info.get_mut(i..i + pref_len)? {
1250 info.mask |= indic_plan.mask_array[indic_feature::PREF];
1251 }
1252 break;
1253 }
1254 }
1255 }
1256
1257 for i in start + 1..end {
1259 let info = buffer.info.get(i)?;
1260 if info.is_joiner() {
1261 let non_joiner = info.indic_category() == category::OT_ZWNJ;
1262 let mut j = i;
1263
1264 loop {
1265 j -= 1;
1266
1267 let info = buffer.info.get_mut(j)?;
1272
1273 if non_joiner {
1275 info.mask &= !indic_plan.mask_array[indic_feature::HALF];
1276 }
1277
1278 if j <= start || info.is_consonant() {
1279 break;
1280 }
1281 }
1282 }
1283 }
1284
1285 Some(())
1286}
1287
1288fn initial_reordering_standalone_cluster(
1289 plan: &ShapePlan,
1290 indic_plan: &IndicShapePlan,
1291 font: &ShaperFont<'_, '_>,
1292 start: usize,
1293 end: usize,
1294 buffer: &mut Buffer,
1295) -> Option<()> {
1296 initial_reordering_consonant_syllable(plan, indic_plan, font, start, end, buffer)
1299}
1300
1301fn final_reordering(
1302 plan: &ShapePlan,
1303 font: &ShaperFont<'_, '_>,
1304 buffer: &mut Buffer,
1305) -> Option<bool> {
1306 if buffer.is_empty() {
1307 return Some(false);
1308 }
1309
1310 if buffer_message!(buffer, font, "start reordering indic final") {
1311 foreach_syllable!(buffer, start, end, {
1312 if final_reordering_impl(plan, font, start, end, buffer).is_none() {
1313 buffer.index_failed();
1314 }
1315 });
1316 buffer_message!(buffer, font, "end reordering indic final");
1317 }
1318
1319 buffer.deallocate_var(GlyphInfo::INDIC_CATEGORY_VAR);
1320 buffer.deallocate_var(GlyphInfo::INDIC_POSITION_VAR);
1321
1322 Some(false)
1323}
1324
1325fn final_reordering_impl(
1326 plan: &ShapePlan,
1327 font: &ShaperFont<'_, '_>,
1328 start: usize,
1329 end: usize,
1330 buffer: &mut Buffer,
1331) -> Option<()> {
1332 let indic_plan = plan.data::<IndicShapePlan>();
1333
1334 let mut virama_glyph = None;
1342 if indic_plan.config.virama != 0 {
1343 if let Some(g) = font.nominal_glyph(indic_plan.config.virama) {
1344 virama_glyph = Some(g.to_u32());
1345 }
1346 }
1347
1348 if let Some(virama_glyph) = virama_glyph {
1349 for info in buffer.info.get_mut(start..end)? {
1350 if info.glyph_id == virama_glyph && info.ligated() && info.multiplied() {
1351 info.set_indic_category(category::OT_H);
1353 info.clear_ligated_and_multiplied();
1354 }
1355 }
1356 }
1357
1358 let mut try_pref = indic_plan.mask_array[indic_feature::PREF] != 0;
1366
1367 let mut base = start;
1368 while base < end {
1369 if buffer.info.get(base)?.indic_position() as u32 >= position::POS_BASE_C as u32 {
1370 if try_pref && base + 1 < end {
1371 for i in base + 1..end {
1372 let info = buffer.info.get(i)?;
1373 if (info.mask & indic_plan.mask_array[indic_feature::PREF]) != 0 {
1374 if !(info.substituted() && info.ligated_and_didnt_multiply()) {
1375 base = i;
1378 while base < end && buffer.info.get(base)?.is_halant() {
1379 base += 1;
1380 }
1381
1382 if base < end {
1383 buffer
1384 .info
1385 .get_mut(base)?
1386 .set_indic_position(position::POS_BASE_C);
1387 }
1388
1389 try_pref = false;
1390 }
1391
1392 break;
1393 }
1394
1395 if base == end {
1396 break;
1397 }
1398 }
1399 }
1400
1401 if buffer.script == Some(Script::MALAYALAM) {
1403 let mut i = base + 1;
1404 while i < end {
1405 while i < end && buffer.info.get(i)?.is_joiner() {
1406 i += 1;
1407 }
1408
1409 if i == end || !buffer.info.get(i)?.is_halant() {
1410 break;
1411 }
1412
1413 i += 1; while i < end && buffer.info.get(i)?.is_joiner() {
1416 i += 1;
1417 }
1418
1419 if i < end {
1420 let info = buffer.info.get_mut(i)?;
1421 if info.is_consonant() && info.indic_position() == position::POS_BELOW_C {
1422 base = i;
1423 info.set_indic_position(position::POS_BASE_C);
1424 }
1425 }
1426
1427 i += 1;
1428 }
1429 }
1430
1431 if start < base
1432 && buffer.info.get(base)?.indic_position() as u32 > position::POS_BASE_C as u32
1433 {
1434 base -= 1;
1435 }
1436
1437 break;
1438 }
1439
1440 base += 1;
1441 }
1442
1443 if base == end
1444 && start < base
1445 && buffer
1446 .info
1447 .get(base - 1)?
1448 .is_one_of(rb_flag(category::OT_ZWJ as u32))
1449 {
1450 base -= 1;
1451 }
1452
1453 if base < end {
1454 while start < base
1455 && buffer
1456 .info
1457 .get(base)?
1458 .is_one_of(rb_flag(category::OT_N as u32) | rb_flag(category::OT_H as u32))
1459 {
1460 base -= 1;
1461 }
1462 }
1463
1464 if start + 1 < end && start < base {
1493 let mut new_pos = if base == end { base - 2 } else { base - 1 };
1495
1496 if buffer.script != Some(Script::MALAYALAM) && buffer.script != Some(Script::TAMIL) {
1500 loop {
1501 while new_pos > start
1502 && !buffer.info.get(new_pos)?.is_one_of(
1503 rb_flag(category::OT_M as u32)
1504 | rb_flag(category::OT_MPst as u32)
1505 | rb_flag(category::OT_H as u32),
1506 )
1507 {
1508 new_pos -= 1;
1509 }
1510
1511 let info = buffer.info.get(new_pos)?;
1515 if info.is_halant() && info.indic_position() != position::POS_PRE_M {
1516 if new_pos + 1 < end {
1517 if buffer.info.get(new_pos + 1)?.indic_category() == category::OT_ZWJ {
1519 if new_pos > start {
1521 new_pos -= 1;
1522 continue;
1523 }
1524 }
1525
1526 }
1534 } else {
1535 new_pos = start; }
1537
1538 break;
1539 }
1540 }
1541
1542 if start < new_pos && buffer.info.get(new_pos)?.indic_position() != position::POS_PRE_M {
1543 for i in (start + 1..=new_pos).rev() {
1545 if buffer.info.get(i - 1)?.indic_position() == position::POS_PRE_M {
1546 let old_pos = i - 1;
1547 if old_pos < base && base <= new_pos {
1549 base -= 1;
1550 }
1551
1552 buffer.info.get_mut(old_pos..=new_pos)?.rotate_left(1);
1553
1554 buffer.merge_clusters(new_pos, cmp::min(end, base + 1));
1557
1558 new_pos -= 1;
1559 }
1560 }
1561 } else {
1562 for i in start..base {
1563 if buffer.info.get(i)?.indic_position() == position::POS_PRE_M {
1564 buffer.merge_clusters(i, cmp::min(end, base + 1));
1565 break;
1566 }
1567 }
1568 }
1569 }
1570
1571 if start + 1 < end
1589 && buffer.info.get(start).is_some_and(|info| {
1590 info.indic_position() == position::POS_RA_TO_BECOME_REPH
1591 && (info.indic_category() == category::OT_Repha) ^ info.ligated_and_didnt_multiply()
1592 })
1593 {
1594 let mut new_reph_pos;
1595 'reph: {
1596 let reph_pos = indic_plan.config.reph_pos;
1597
1598 if reph_pos != RephPosition::AfterPost {
1601 {
1612 new_reph_pos = start + 1;
1613 while new_reph_pos < base && !buffer.info.get(new_reph_pos)?.is_halant() {
1614 new_reph_pos += 1;
1615 }
1616
1617 if new_reph_pos < base && buffer.info.get(new_reph_pos)?.is_halant() {
1618 if new_reph_pos + 1 < base && buffer.info.get(new_reph_pos + 1)?.is_joiner()
1620 {
1621 new_reph_pos += 1;
1622 }
1623
1624 break 'reph;
1625 }
1626 }
1627
1628 if reph_pos == RephPosition::AfterMain {
1632 new_reph_pos = base;
1633 while new_reph_pos + 1 < end
1634 && buffer.info.get(new_reph_pos + 1)?.indic_position()
1635 <= position::POS_AFTER_MAIN
1636 {
1637 new_reph_pos += 1;
1638 }
1639
1640 if new_reph_pos < end {
1641 break 'reph;
1642 }
1643 }
1644
1645 if reph_pos == RephPosition::AfterSub {
1652 new_reph_pos = base;
1653 while new_reph_pos + 1 < end
1654 && (rb_flag_unsafe(
1655 buffer.info.get(new_reph_pos + 1)?.indic_position() as u32
1656 ) & (rb_flag(position::POS_POST_C as u32)
1657 | rb_flag(position::POS_AFTER_POST as u32)
1658 | rb_flag(position::POS_SMVD as u32)))
1659 == 0
1660 {
1661 new_reph_pos += 1;
1662 }
1663
1664 if new_reph_pos < end {
1665 break 'reph;
1666 }
1667 }
1668 }
1669
1670 new_reph_pos = start + 1;
1679 while new_reph_pos < base && !buffer.info.get(new_reph_pos)?.is_halant() {
1680 new_reph_pos += 1;
1681 }
1682
1683 if new_reph_pos < base && buffer.info.get(new_reph_pos)?.is_halant() {
1684 if new_reph_pos + 1 < base && buffer.info.get(new_reph_pos + 1)?.is_joiner() {
1686 new_reph_pos += 1;
1687 }
1688
1689 break 'reph;
1690 }
1691 {
1695 new_reph_pos = end - 1;
1696 while new_reph_pos > start
1697 && buffer.info.get(new_reph_pos)?.indic_position() == position::POS_SMVD
1698 {
1699 new_reph_pos -= 1;
1700 }
1701
1702 if buffer.info.get(new_reph_pos)?.is_halant() {
1708 for info in buffer.info.get(base + 1..new_reph_pos).unwrap_or_default() {
1710 if (rb_flag_unsafe(info.indic_category() as u32)
1711 & (rb_flag(category::OT_M as u32) | rb_flag(category::OT_MPst as u32)))
1712 != 0
1713 {
1714 new_reph_pos -= 1;
1716 }
1717 }
1718 }
1719 }
1720
1721 break 'reph;
1722 }
1723
1724 buffer.merge_clusters(start, new_reph_pos + 1);
1726
1727 buffer.info.get_mut(start..=new_reph_pos)?.rotate_left(1);
1728
1729 if start < base && base <= new_reph_pos {
1730 base -= 1;
1731 }
1732 }
1733
1734 if try_pref && base + 1 < end {
1741 for i in base + 1..end {
1742 let info = buffer.info.get(i)?;
1743 if (info.mask & indic_plan.mask_array[indic_feature::PREF]) != 0 {
1744 if info.ligated_and_didnt_multiply() {
1753 let mut new_pos = base;
1759 if buffer.script != Some(Script::MALAYALAM)
1763 && buffer.script != Some(Script::TAMIL)
1764 {
1765 while new_pos > start
1766 && !buffer.info.get(new_pos - 1)?.is_one_of(
1767 rb_flag(category::OT_M as u32)
1768 | rb_flag(category::OT_MPst as u32)
1769 | rb_flag(category::OT_H as u32),
1770 )
1771 {
1772 new_pos -= 1;
1773 }
1774 }
1775
1776 if new_pos > start && buffer.info.get(new_pos - 1)?.is_halant() {
1777 if new_pos < end && buffer.info.get(new_pos)?.is_joiner() {
1779 new_pos += 1;
1780 }
1781 }
1782
1783 {
1784 let old_pos = i;
1785
1786 buffer.merge_clusters(new_pos, old_pos + 1);
1787 buffer.info.get_mut(new_pos..=old_pos)?.rotate_right(1);
1788
1789 if new_pos <= base && base < old_pos {
1790 #[allow(unused_assignments)]
1792 {
1793 base += 1;
1794 }
1795 }
1796 }
1797 }
1798
1799 break;
1800 }
1801 }
1802 }
1803
1804 if buffer.info.get(start)?.indic_position() == position::POS_PRE_M {
1806 if start == 0
1807 || !buffer
1808 .info
1809 .get(start - 1)?
1810 .general_category()
1811 .in_range_inclusive(GeneralCategory::FORMAT, GeneralCategory::NON_SPACING_MARK)
1812 {
1813 buffer.info.get_mut(start)?.mask |= indic_plan.mask_array[indic_feature::INIT];
1814 } else {
1815 buffer.unsafe_to_break(Some(start - 1), Some(start + 1));
1816 }
1817 }
1818
1819 Some(())
1820}