1use super::*;
2use crate::normalize::NormalizationMode;
3use crate::once::Once;
4use crate::ot::map::*;
5use crate::plan::ShapePlan;
6use crate::unicode::*;
7use crate::Direction;
8use crate::ShaperFont;
9use crate::{GlyphInfo, Mask, Script, Tag};
10use alloc::boxed::Box;
11
12const HB_BUFFER_SCRATCH_FLAG_ARABIC_HAS_STCH: ScratchFlags = HB_BUFFER_SCRATCH_FLAG_SHAPER0;
13
14fn is_word_category(gc: GeneralCategory) -> bool {
17 (gc.flag_unsafe()
18 & (GeneralCategory::UNASSIGNED.flag()
19 | GeneralCategory::PRIVATE_USE.flag()
20 | GeneralCategory::MODIFIER_LETTER.flag()
21 | GeneralCategory::OTHER_LETTER.flag()
22 | GeneralCategory::SPACING_MARK.flag()
23 | GeneralCategory::ENCLOSING_MARK.flag()
24 | GeneralCategory::NON_SPACING_MARK.flag()
25 | GeneralCategory::DECIMAL_NUMBER.flag()
26 | GeneralCategory::LETTER_NUMBER.flag()
27 | GeneralCategory::OTHER_NUMBER.flag()
28 | GeneralCategory::CURRENCY_SYMBOL.flag()
29 | GeneralCategory::MODIFIER_SYMBOL.flag()
30 | GeneralCategory::MATH_SYMBOL.flag()
31 | GeneralCategory::OTHER_SYMBOL.flag()))
32 != 0
33}
34
35#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
36pub enum ArabicJoiningType {
37 U = 0,
38 L = 1,
39 R = 2,
40 D = 3,
41 GroupAlaph = 4,
43 GroupDalathRish = 5,
44 T = 6,
45 X = 7, }
47
48fn get_joining_type(u: Codepoint, gc: GeneralCategory) -> ArabicJoiningType {
49 let j_type = arabic_table::joining_type(u);
50 if j_type != ArabicJoiningType::X {
51 return j_type;
52 }
53
54 let ok = gc.flag_unsafe()
55 & (GeneralCategory::NON_SPACING_MARK.flag()
56 | GeneralCategory::ENCLOSING_MARK.flag()
57 | GeneralCategory::FORMAT.flag());
58
59 if ok != 0 {
60 ArabicJoiningType::T
61 } else {
62 ArabicJoiningType::U
63 }
64}
65
66fn feature_is_syriac(tag: Tag) -> bool {
67 matches!(tag.to_be_bytes()[3], b'2' | b'3')
68}
69
70const ARABIC_FEATURES: &[Tag] = &[
71 Tag::new(b"isol"),
72 Tag::new(b"fina"),
73 Tag::new(b"fin2"),
74 Tag::new(b"fin3"),
75 Tag::new(b"medi"),
76 Tag::new(b"med2"),
77 Tag::new(b"init"),
78];
79
80mod arabic_action {
81 pub const ISOL: u8 = 0;
82 pub const FINA: u8 = 1;
83 pub const FIN2: u8 = 2;
84 pub const FIN3: u8 = 3;
85 pub const MEDI: u8 = 4;
86 pub const MED2: u8 = 5;
87 pub const INIT: u8 = 6;
88 pub const NONE: u8 = 7;
89
90 pub const STRETCHING_FIXED: u8 = 8;
92 pub const STRETCHING_REPEATING: u8 = 9;
93
94 #[inline]
95 pub fn is_stch(n: u8) -> bool {
96 matches!(n, STRETCHING_FIXED | STRETCHING_REPEATING)
97 }
98}
99
100static STATE_TABLE: &[[(u8, u8, u16); 6]] = &[
101 [
106 (arabic_action::NONE, arabic_action::NONE, 0),
107 (arabic_action::NONE, arabic_action::ISOL, 2),
108 (arabic_action::NONE, arabic_action::ISOL, 1),
109 (arabic_action::NONE, arabic_action::ISOL, 2),
110 (arabic_action::NONE, arabic_action::ISOL, 1),
111 (arabic_action::NONE, arabic_action::ISOL, 6),
112 ],
113 [
115 (arabic_action::NONE, arabic_action::NONE, 0),
116 (arabic_action::NONE, arabic_action::ISOL, 2),
117 (arabic_action::NONE, arabic_action::ISOL, 1),
118 (arabic_action::NONE, arabic_action::ISOL, 2),
119 (arabic_action::NONE, arabic_action::FIN2, 5),
120 (arabic_action::NONE, arabic_action::ISOL, 6),
121 ],
122 [
124 (arabic_action::NONE, arabic_action::NONE, 0),
125 (arabic_action::NONE, arabic_action::ISOL, 2),
126 (arabic_action::INIT, arabic_action::FINA, 1),
127 (arabic_action::INIT, arabic_action::FINA, 3),
128 (arabic_action::INIT, arabic_action::FINA, 4),
129 (arabic_action::INIT, arabic_action::FINA, 6),
130 ],
131 [
133 (arabic_action::NONE, arabic_action::NONE, 0),
134 (arabic_action::NONE, arabic_action::ISOL, 2),
135 (arabic_action::MEDI, arabic_action::FINA, 1),
136 (arabic_action::MEDI, arabic_action::FINA, 3),
137 (arabic_action::MEDI, arabic_action::FINA, 4),
138 (arabic_action::MEDI, arabic_action::FINA, 6),
139 ],
140 [
142 (arabic_action::NONE, arabic_action::NONE, 0),
143 (arabic_action::NONE, arabic_action::ISOL, 2),
144 (arabic_action::MED2, arabic_action::ISOL, 1),
145 (arabic_action::MED2, arabic_action::ISOL, 2),
146 (arabic_action::MED2, arabic_action::FIN2, 5),
147 (arabic_action::MED2, arabic_action::ISOL, 6),
148 ],
149 [
151 (arabic_action::NONE, arabic_action::NONE, 0),
152 (arabic_action::NONE, arabic_action::ISOL, 2),
153 (arabic_action::ISOL, arabic_action::ISOL, 1),
154 (arabic_action::ISOL, arabic_action::ISOL, 2),
155 (arabic_action::ISOL, arabic_action::FIN2, 5),
156 (arabic_action::ISOL, arabic_action::ISOL, 6),
157 ],
158 [
160 (arabic_action::NONE, arabic_action::NONE, 0),
161 (arabic_action::NONE, arabic_action::ISOL, 2),
162 (arabic_action::NONE, arabic_action::ISOL, 1),
163 (arabic_action::NONE, arabic_action::ISOL, 2),
164 (arabic_action::NONE, arabic_action::FIN3, 5),
165 (arabic_action::NONE, arabic_action::ISOL, 6),
166 ],
167];
168
169impl GlyphInfo {
170 declare_buffer_var_alias!(
171 OT_SHAPER_VAR_U8_AUXILIARY_VAR,
172 u8,
173 ARABIC_SHAPING_ACTION_VAR,
174 arabic_shaping_action,
175 set_arabic_shaping_action
176 );
177}
178
179fn deallocate_buffer_var(
180 _: &ShapePlan,
181 _: &ShaperFont<'_, '_>,
182 buffer: &mut Buffer,
183) -> Option<bool> {
184 buffer.deallocate_var(GlyphInfo::ARABIC_SHAPING_ACTION_VAR);
185
186 Some(false)
187}
188
189fn collect_features(planner: &mut ShapePlanner) {
190 planner.ot_map.enable_feature(Tag::new(b"stch"), F_NONE, 1);
212 planner.ot_map.add_gsub_pause(Some(record_stch));
213
214 planner
215 .ot_map
216 .enable_feature(Tag::new(b"ccmp"), F_MANUAL_ZWJ, 1);
217 planner
218 .ot_map
219 .enable_feature(Tag::new(b"locl"), F_MANUAL_ZWJ, 1);
220
221 planner.ot_map.add_gsub_pause(None);
222
223 for feature in ARABIC_FEATURES {
224 let has_fallback = planner.script == Some(Script::ARABIC) && !feature_is_syriac(*feature);
225 let flags = if has_fallback { F_HAS_FALLBACK } else { F_NONE };
226 planner
227 .ot_map
228 .add_feature(*feature, F_MANUAL_ZWJ | flags, 1);
229 planner.ot_map.add_gsub_pause(None);
230 }
231 planner.ot_map.add_gsub_pause(Some(deallocate_buffer_var));
232
233 planner
238 .ot_map
239 .enable_feature(Tag::new(b"rlig"), F_MANUAL_ZWJ | F_HAS_FALLBACK, 1);
240
241 if planner.script == Some(Script::ARABIC) {
242 planner.ot_map.add_gsub_pause(Some(arabic_fallback_shape));
243 }
244
245 planner
248 .ot_map
249 .enable_feature(Tag::new(b"calt"), F_MANUAL_ZWJ, 1);
250 if !planner.ot_map.has_feature(Tag::new(b"rclt")) {
252 planner.ot_map.add_gsub_pause(None);
253 }
254
255 planner
256 .ot_map
257 .enable_feature(Tag::new(b"liga"), F_MANUAL_ZWJ, 1);
258 planner
259 .ot_map
260 .enable_feature(Tag::new(b"clig"), F_MANUAL_ZWJ, 1);
261
262 planner
273 .ot_map
274 .enable_feature(Tag::new(b"mset"), F_MANUAL_ZWJ, 1);
275}
276
277pub struct ArabicShapePlan {
278 mask_array: [Mask; ARABIC_FEATURES.len() + 1],
283 do_fallback: bool,
284 has_stch: bool,
285 fallback_plan: Once<Option<arabic_fallback::FallbackPlan>>,
286}
287
288pub fn data_create_arabic(plan: &ShapePlan) -> ArabicShapePlan {
289 let has_stch = plan.ot_map.get_1_mask(Tag::new(b"stch")) != 0;
290 let mut do_fallback = plan.script == Some(Script::ARABIC);
291
292 let mut mask_array = [0; ARABIC_FEATURES.len() + 1];
293 for i in 0..ARABIC_FEATURES.len() {
294 mask_array[i] = plan.ot_map.get_1_mask(ARABIC_FEATURES[i]);
295 do_fallback &=
296 feature_is_syriac(ARABIC_FEATURES[i]) || plan.ot_map.needs_fallback(ARABIC_FEATURES[i]);
297 }
298
299 ArabicShapePlan {
300 mask_array,
301 do_fallback,
302 has_stch,
303 fallback_plan: Once::new(),
304 }
305}
306
307fn arabic_joining(buffer: &mut Buffer) -> Option<()> {
308 let mut prev: Option<usize> = None;
309 let mut state = 0;
310
311 let pre_context = buffer.context[0]
313 .get(..buffer.context_len[0])
314 .unwrap_or_default();
315 for &c in pre_context {
316 let this_type = get_joining_type(c, c.general_category());
317 if this_type == ArabicJoiningType::T {
318 continue;
319 }
320
321 state = STATE_TABLE[state][this_type as usize].2 as usize;
322 break;
323 }
324
325 for i in 0..buffer.len {
326 let info = buffer.info.get_mut(i)?;
327 let this_type = get_joining_type(info.as_codepoint(), info.general_category());
328 if this_type == ArabicJoiningType::T {
329 info.set_arabic_shaping_action(arabic_action::NONE);
330 continue;
331 }
332
333 let entry = &STATE_TABLE[state][this_type as usize];
334 if entry.0 != arabic_action::NONE && prev.is_some() {
335 if let Some(prev) = prev {
336 buffer
337 .info
338 .get_mut(prev)?
339 .set_arabic_shaping_action(entry.0);
340 buffer.safe_to_insert_tatweel(Some(prev), Some(i + 1));
341 }
342 }
343 else {
345 if let Some(prev) = prev {
346 if this_type >= ArabicJoiningType::R || (2 <= state && state <= 5) {
347 buffer.unsafe_to_concat(Some(prev), Some(i + 1));
348 }
349 } else {
350 if this_type >= ArabicJoiningType::R {
351 buffer.unsafe_to_concat_from_outbuffer(Some(0), Some(i + 1));
352 }
353 }
354 }
355
356 buffer.info.get_mut(i)?.set_arabic_shaping_action(entry.1);
357
358 prev = Some(i);
359 state = entry.2 as usize;
360 }
361
362 let post_context = buffer.context[1];
364 let post_context_len = buffer.context_len[1];
365 for &c in post_context.get(..post_context_len).unwrap_or_default() {
366 let this_type = get_joining_type(c, c.general_category());
367 if this_type == ArabicJoiningType::T {
368 continue;
369 }
370
371 let entry = &STATE_TABLE[state][this_type as usize];
372 if entry.0 != arabic_action::NONE && prev.is_some() {
373 if let Some(prev) = prev {
374 buffer
375 .info
376 .get_mut(prev)?
377 .set_arabic_shaping_action(entry.0);
378 buffer.safe_to_insert_tatweel(Some(prev), Some(buffer.len));
379 }
380 }
381 else if 2 <= state && state <= 5 {
383 if let Some(prev) = prev {
384 buffer.unsafe_to_concat(Some(prev), Some(buffer.len));
385 }
386 }
387
388 break;
389 }
390 Some(())
391}
392
393fn mongolian_variation_selectors(buffer: &mut Buffer) {
394 let Some((first, rest)) = buffer.info_slice_mut().split_first_mut() else {
396 return;
397 };
398 let mut action = first.arabic_shaping_action();
399 for info in rest {
400 if (0x180B..=0x180D).contains(&info.glyph_id) || info.glyph_id == 0x180F {
401 info.set_arabic_shaping_action(action);
402 }
403 action = info.arabic_shaping_action();
404 }
405}
406
407fn setup_masks_arabic_plan(plan: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
408 buffer.allocate_var(GlyphInfo::ARABIC_SHAPING_ACTION_VAR);
409
410 let arabic_plan = plan.data::<ArabicShapePlan>();
411 setup_masks_inner(arabic_plan, plan.script, buffer);
412}
413
414pub fn setup_masks_inner(
415 arabic_plan: &ArabicShapePlan,
416 script: Option<Script>,
417 buffer: &mut Buffer,
418) {
419 if arabic_joining(buffer).is_none() {
420 buffer.index_failed();
421 }
422 if script == Some(Script::MONGOLIAN) {
423 mongolian_variation_selectors(buffer);
424 }
425
426 for info in buffer.info_slice_mut() {
427 info.mask |= arabic_plan.mask_array[info.arabic_shaping_action() as usize];
428 }
429}
430
431fn arabic_fallback_shape(
432 plan: &ShapePlan,
433 font: &ShaperFont<'_, '_>,
434 buffer: &mut Buffer,
435) -> Option<bool> {
436 let arabic_plan = plan.data::<ArabicShapePlan>();
437 if !arabic_plan.do_fallback {
438 return Some(false);
439 }
440
441 let fallback_plan = arabic_plan
442 .fallback_plan
443 .get_or_init(|| arabic_fallback::FallbackPlan::new(plan, font));
444 if let Some(fallback_plan) = fallback_plan {
445 fallback_plan.apply(font, buffer);
446 }
447 Some(true)
448}
449
450fn record_stch(plan: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
456 let arabic_plan = plan.data::<ArabicShapePlan>();
457 if !arabic_plan.has_stch {
458 return Some(false);
459 }
460
461 let mut has_stch = false;
467 for glyph_info in buffer.info_slice_mut() {
468 if glyph_info.multiplied() {
469 let comp = if glyph_info.lig_comp() % 2 != 0 {
470 arabic_action::STRETCHING_REPEATING
471 } else {
472 arabic_action::STRETCHING_FIXED
473 };
474
475 glyph_info.set_arabic_shaping_action(comp);
476 has_stch = true;
477 }
478 }
479
480 if has_stch {
481 buffer.scratch_flags |= HB_BUFFER_SCRATCH_FLAG_ARABIC_HAS_STCH;
482 }
483
484 Some(false)
485}
486
487fn apply_stch(font: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
488 if buffer.scratch_flags & HB_BUFFER_SCRATCH_FLAG_ARABIC_HAS_STCH == 0 {
489 return;
490 }
491
492 let rtl = buffer.direction == Direction::RightToLeft;
493
494 if !rtl {
495 buffer.reverse();
496 }
497
498 if apply_stch_passes(font, buffer, rtl).is_none() {
500 buffer.index_failed();
501 }
502
503 if !rtl {
504 buffer.reverse();
505 }
506}
507
508fn copy_glyph_to(buffer: &mut Buffer, from: usize, to: usize) -> Option<()> {
510 let info = *buffer.info.get(from)?;
511 let pos = *buffer.pos.get(from)?;
512 *buffer.info.get_mut(to)? = info;
513 *buffer.pos.get_mut(to)? = pos;
514 Some(())
515}
516
517fn apply_stch_passes(font: &ShaperFont<'_, '_>, buffer: &mut Buffer, rtl: bool) -> Option<()> {
518 let mut extra_glyphs_needed: usize = 0; const MEASURE: usize = 0;
525 const CUT: usize = 1;
526
527 for step in 0..2 {
528 let new_len = buffer.len + extra_glyphs_needed; let mut i = buffer.len;
530 let mut j = new_len;
531 while i != 0 {
532 if !arabic_action::is_stch(buffer.info.get(i - 1)?.arabic_shaping_action()) {
533 if step == CUT {
534 j -= 1;
535 copy_glyph_to(buffer, i - 1, j)?;
536 }
537
538 i -= 1;
539 continue;
540 }
541
542 let mut w_total: i32 = 0; let mut w_fixed: i32 = 0; let mut w_repeating: i32 = 0; let mut n_repeating: i32 = 0;
548
549 let end = i;
550 while i != 0 && arabic_action::is_stch(buffer.info.get(i - 1)?.arabic_shaping_action())
551 {
552 i -= 1;
553 let info = buffer.info.get(i)?;
554 let width = font.glyph_h_advance(info.as_glyph());
555
556 if info.arabic_shaping_action() == arabic_action::STRETCHING_FIXED {
557 w_fixed = w_fixed.saturating_add(width);
558 } else {
559 w_repeating = w_repeating.saturating_add(width);
560 n_repeating += 1;
561 }
562 }
563
564 let start = i;
565 let mut context = i;
566 while context != 0 {
567 let info = buffer.info.get(context - 1)?;
568 if arabic_action::is_stch(info.arabic_shaping_action())
569 || !(info.is_default_ignorable() || is_word_category(info.general_category()))
570 {
571 break;
572 }
573 context -= 1;
574 w_total = w_total.saturating_add(buffer.pos.get(context)?.x_advance);
575 }
576
577 i += 1; let mut n_copies: i32 = 0;
581
582 let mut w_remaining = w_total.saturating_sub(w_fixed);
583 if w_remaining > w_repeating && w_repeating > 0 {
584 n_copies = w_remaining / (w_repeating) - 1;
585 }
586
587 let mut extra_repeat_overlap = 0;
589 let shortfall =
590 i64::from(w_remaining) - i64::from(w_repeating) * i64::from(n_copies + 1);
591 if shortfall > 0 && n_repeating > 0 {
592 n_copies += 1;
593 let excess =
594 i64::from(n_copies + 1) * i64::from(w_repeating) - i64::from(w_remaining);
595 if excess > 0 {
596 extra_repeat_overlap =
597 clamp_i64_to_i32(excess / (i64::from(n_copies) * i64::from(n_repeating)));
598 w_remaining = 0;
599 }
600 }
601
602 if step == MEASURE {
603 extra_glyphs_needed += (n_copies * n_repeating) as usize;
604 } else {
605 buffer.unsafe_to_break(Some(context), Some(end));
606 let mut x_offset = w_remaining / 2;
607 for k in (start + 1..=end).rev() {
608 let info = *buffer.info.get(k - 1)?;
609 let width = font.glyph_h_advance(info.as_glyph());
610
611 let mut repeat = 1;
612 if info.arabic_shaping_action() == arabic_action::STRETCHING_REPEATING {
613 repeat += n_copies;
614 }
615
616 let mut pos = *buffer.pos.get(k - 1)?;
618 pos.x_advance = 0;
619
620 for n in 0..repeat {
621 if rtl {
622 x_offset = x_offset.saturating_sub(width);
623 if n > 0 {
624 x_offset = x_offset.saturating_add(extra_repeat_overlap);
625 }
626 }
627
628 pos.x_offset = x_offset;
629
630 j -= 1;
632 *buffer.info.get_mut(j)? = info;
633 *buffer.pos.get_mut(j)? = pos;
634
635 if !rtl {
636 x_offset = x_offset.saturating_add(width);
637
638 if n > 0 {
639 x_offset = x_offset.saturating_sub(extra_repeat_overlap);
640 }
641 }
642 }
643 *buffer.pos.get_mut(k - 1)? = pos;
644 }
645 }
646
647 i -= 1;
648 }
649
650 if step == MEASURE {
651 if !buffer.ensure(buffer.len + extra_glyphs_needed) {
652 break;
653 }
654 } else {
655 debug_assert_eq!(j, 0);
656 buffer.len = new_len;
657 }
658 }
659 Some(())
660}
661
662fn postprocess_glyphs_arabic(_: &ShapePlan, font: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
663 apply_stch(font, buffer);
664}
665
666static MODIFIER_COMBINING_MARKS: &[u32] = &[
668 0x0654, 0x0655, 0x0658, 0x06DC, 0x06E3, 0x06E7, 0x06E8, 0x08CA, 0x08CB, 0x08CD, 0x08CE, 0x08CF, 0x08D3, 0x08F3, ];
683
684fn reorder_marks_arabic(_: &ShapePlan, buffer: &mut Buffer, start: usize, end: usize) {
685 if reorder_modifier_combining_marks(buffer, start, end).is_none() {
686 buffer.index_failed();
687 }
688}
689
690fn reorder_modifier_combining_marks(
691 buffer: &mut Buffer,
692 mut start: usize,
693 end: usize,
694) -> Option<()> {
695 let mut i = start;
696 for cc in [220u8, 230] {
697 while i < end && buffer.info.get(i)?.modified_combining_class() < cc {
698 i += 1;
699 }
700
701 if i == end {
702 break;
703 }
704
705 if buffer.info.get(i)?.modified_combining_class() > cc {
706 continue;
707 }
708
709 let mut j = i;
710 while j < end {
711 let info = buffer.info.get(j)?;
712 if info.modified_combining_class() != cc
713 || !MODIFIER_COMBINING_MARKS.contains(&info.glyph_id)
714 {
715 break;
716 }
717 j += 1;
718 }
719
720 if i == j {
721 continue;
722 }
723
724 buffer.merge_clusters(start, j);
726 buffer.info.get_mut(start..j)?.rotate_left(i - start);
727
728 let new_start = start + j - i;
740 let new_cc = if cc == 220 {
741 modified_combining_class::CCC22
742 } else {
743 modified_combining_class::CCC26
744 };
745
746 for info in buffer.info.get_mut(start..new_start)? {
747 info.set_modified_combining_class(new_cc);
748 }
749 start = new_start;
750
751 i = j;
752 }
753 Some(())
754}
755
756pub const ARABIC_SHAPER: OtShaper = OtShaper {
757 collect_features: Some(collect_features),
758 override_features: None,
759 create_data: Some(|plan| Box::new(data_create_arabic(plan))),
760 preprocess_text: None,
761 postprocess_glyphs: Some(postprocess_glyphs_arabic),
762 normalization_preference: NormalizationMode::Auto,
763 decompose: None,
764 compose: None,
765 setup_masks: Some(setup_masks_arabic_plan),
766 gpos_tag: None,
767 reorder_marks: Some(reorder_marks_arabic),
768 zero_width_marks: ZeroWidthMarks::ByGdefLate,
769 fallback_position: true,
770};