1use super::arabic::ArabicShapePlan;
2use super::syllabic::*;
3use super::*;
4use crate::algs::*;
5use crate::ot::layout::*;
6use crate::ot::map::*;
7use crate::plan::ShapePlan;
8use crate::unicode::{CharExt, Codepoint};
9use crate::ShaperFont;
10use crate::{GlyphInfo, Mask, Script, Tag};
11use alloc::boxed::Box;
12
13pub const UNIVERSAL_SHAPER: OtShaper = OtShaper {
14 collect_features: Some(collect_features),
15 override_features: None,
16 create_data: Some(|plan| Box::new(UniversalShapePlan::new(plan))),
17 preprocess_text: Some(preprocess_text),
18 postprocess_glyphs: None,
19 normalization_preference: NormalizationMode::ComposedDiacriticsNoShortCircuit,
20 decompose: None,
21 compose: Some(compose),
22 setup_masks: Some(setup_masks),
23 gpos_tag: None,
24 reorder_marks: None,
25 zero_width_marks: ZeroWidthMarks::ByGdefEarly,
26 fallback_position: false,
27};
28
29impl GlyphInfo {
30 declare_buffer_var_alias!(
31 OT_SHAPER_VAR_U8_CATEGORY_VAR,
32 u8,
33 USE_CATEGORY_VAR,
34 use_category,
35 set_use_category
36 );
37
38 fn is_halant_use(&self) -> bool {
39 matches!(
40 self.use_category(),
41 category::H | category::HVM | category::IS
42 ) && !self.ligated()
43 }
44}
45
46pub type Category = u8;
47#[allow(dead_code)]
48pub mod category {
49 pub const O: u8 = 0; pub const B: u8 = 1; pub const N: u8 = 4; pub const GB: u8 = 5; pub const CGJ: u8 = 6;
58
59 pub const SUB: u8 = 11; pub const H: u8 = 12; pub const HN: u8 = 13; pub const ZWNJ: u8 = 14; pub const WJ: u8 = 16; pub const RSV: u8 = 17; pub const R: u8 = 18; pub const S: u8 = 19; pub const CS: u8 = 43; pub const IS: u8 = 44; pub const Sk: u8 = 48; pub const FAbv: u8 = 24; pub const FBlw: u8 = 25; pub const FPst: u8 = 26; pub const MAbv: u8 = 27; pub const MBlw: u8 = 28; pub const MPst: u8 = 29; pub const MPre: u8 = 30; pub const CMAbv: u8 = 31; pub const CMBlw: u8 = 32; pub const VAbv: u8 = 33; pub const VBlw: u8 = 34; pub const VPst: u8 = 35; pub const VPre: u8 = 22; pub const VMAbv: u8 = 37; pub const VMBlw: u8 = 38; pub const VMPst: u8 = 39; pub const VMPre: u8 = 23; pub const SMAbv: u8 = 41; pub const SMBlw: u8 = 42; pub const FMAbv: u8 = 45; pub const FMBlw: u8 = 46; pub const FMPst: u8 = 47; pub const G: u8 = 49; pub const J: u8 = 50; pub const SB: u8 = 51; pub const SE: u8 = 52; pub const HVM: u8 = 53; pub const HM: u8 = 54; pub const HR: u8 = 55; pub const RK: u8 = 56; }
121
122static BASIC_FEATURES: &[Tag] = &[
125 Tag::new(b"rkrf"),
126 Tag::new(b"abvf"),
127 Tag::new(b"blwf"),
128 Tag::new(b"half"),
129 Tag::new(b"pstf"),
130 Tag::new(b"vatu"),
131 Tag::new(b"cjct"),
132];
133
134static TOPOGRAPHICAL_FEATURES: &[Tag] = &[
135 Tag::new(b"isol"),
136 Tag::new(b"init"),
137 Tag::new(b"medi"),
138 Tag::new(b"fina"),
139];
140
141#[derive(Clone, Copy, PartialEq)]
143enum JoiningForm {
144 Isolated = 0,
145 Initial,
146 Medial,
147 Terminal,
148}
149
150static OTHER_FEATURES: &[Tag] = &[
152 Tag::new(b"abvs"),
153 Tag::new(b"blws"),
154 Tag::new(b"haln"),
155 Tag::new(b"pres"),
156 Tag::new(b"psts"),
157];
158
159struct UniversalShapePlan {
160 rphf_mask: Mask,
161 arabic_plan: Option<ArabicShapePlan>,
162}
163
164impl UniversalShapePlan {
165 fn new(plan: &ShapePlan) -> UniversalShapePlan {
166 let mut arabic_plan = None;
167
168 if plan.script.is_some_and(has_arabic_joining) {
169 arabic_plan = Some(arabic::data_create_arabic(plan));
170 }
171
172 UniversalShapePlan {
173 rphf_mask: plan.ot_map.get_1_mask(Tag::new(b"rphf")),
174 arabic_plan,
175 }
176 }
177}
178
179fn collect_features(planner: &mut ShapePlanner) {
180 planner.ot_map.add_gsub_pause(Some(setup_syllables));
182
183 planner
185 .ot_map
186 .enable_feature(Tag::new(b"locl"), F_PER_SYLLABLE, 1);
187 planner
188 .ot_map
189 .enable_feature(Tag::new(b"ccmp"), F_PER_SYLLABLE, 1);
190 planner
191 .ot_map
192 .enable_feature(Tag::new(b"nukt"), F_PER_SYLLABLE, 1);
193 planner
194 .ot_map
195 .enable_feature(Tag::new(b"akhn"), F_MANUAL_ZWJ | F_PER_SYLLABLE, 1);
196
197 planner
199 .ot_map
200 .add_gsub_pause(Some(clear_substitution_flags));
201 planner
202 .ot_map
203 .add_feature(Tag::new(b"rphf"), F_MANUAL_ZWJ | F_PER_SYLLABLE, 1);
204 planner.ot_map.add_gsub_pause(Some(record_rphf));
205 planner
206 .ot_map
207 .add_gsub_pause(Some(clear_substitution_flags));
208 planner
209 .ot_map
210 .enable_feature(Tag::new(b"pref"), F_MANUAL_ZWJ | F_PER_SYLLABLE, 1);
211 planner.ot_map.add_gsub_pause(Some(record_pref));
212
213 for feature in BASIC_FEATURES {
215 planner
216 .ot_map
217 .enable_feature(*feature, F_MANUAL_ZWJ | F_PER_SYLLABLE, 1);
218 }
219
220 planner.ot_map.add_gsub_pause(Some(reorder_use));
221 planner.ot_map.add_gsub_pause(Some(syllabic_clear_var)); for feature in TOPOGRAPHICAL_FEATURES {
225 planner.ot_map.add_feature(*feature, F_NONE, 1);
226 }
227 planner.ot_map.add_gsub_pause(None);
228
229 for feature in OTHER_FEATURES {
231 planner.ot_map.enable_feature(*feature, F_MANUAL_ZWJ, 1);
232 }
233}
234
235fn setup_syllables(plan: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
236 buffer.allocate_var(GlyphInfo::SYLLABLE_VAR);
237
238 use_machine::find_syllables(buffer);
239
240 foreach_syllable!(buffer, start, end, {
241 buffer.unsafe_to_break(Some(start), Some(end));
242 });
243
244 if setup_rphf_mask(plan, buffer).is_none() {
245 buffer.index_failed();
246 }
247 if setup_topographical_masks(plan, buffer).is_none() {
248 buffer.index_failed();
249 }
250
251 Some(false)
252}
253
254fn setup_rphf_mask(plan: &ShapePlan, buffer: &mut Buffer) -> Option<()> {
255 let universal_plan = plan.data::<UniversalShapePlan>();
256
257 let mask = universal_plan.rphf_mask;
258 if mask == 0 {
259 return Some(());
260 }
261
262 let mut start = 0;
263 let mut end = buffer.next_syllable(0);
264 while start < buffer.len {
265 let limit = if buffer.info.get(start)?.use_category() == category::R {
266 1
267 } else {
268 core::cmp::min(3, end - start)
269 };
270
271 for info in buffer.info.get_mut(start..start + limit)? {
272 info.mask |= mask;
273 }
274
275 start = end;
276 end = buffer.next_syllable(start);
277 }
278
279 Some(())
280}
281
282fn setup_topographical_masks(plan: &ShapePlan, buffer: &mut Buffer) -> Option<()> {
283 use super::use_machine::SyllableType;
284
285 if plan.data::<UniversalShapePlan>().arabic_plan.is_some() {
286 return Some(());
287 }
288
289 let mut masks = [0; 4];
290 let mut all_masks = 0;
291 for i in 0..4 {
292 masks[i] = plan.ot_map.get_1_mask(TOPOGRAPHICAL_FEATURES[i]);
293 if masks[i] == plan.ot_map.get_global_mask() {
294 masks[i] = 0;
295 }
296
297 all_masks |= masks[i];
298 }
299
300 if all_masks == 0 {
301 return Some(());
302 }
303
304 let other_masks = !all_masks;
305
306 let mut last_start = 0;
307 let mut last_form = None;
308 let mut start = 0;
309 let mut end = buffer.next_syllable(0);
310 while start < buffer.len {
311 let syllable = buffer.info.get(start)?.syllable() & 0x0F;
312 if syllable == SyllableType::HieroglyphCluster as u8
313 || syllable == SyllableType::NonCluster as u8
314 {
315 last_form = None;
316 } else {
317 let join = last_form == Some(JoiningForm::Terminal)
318 || last_form == Some(JoiningForm::Isolated);
319
320 if join {
321 let form = if last_form == Some(JoiningForm::Terminal) {
323 JoiningForm::Medial
324 } else {
325 JoiningForm::Initial
326 };
327
328 for info in buffer.info.get_mut(last_start..start)? {
329 info.mask = (info.mask & other_masks) | masks[form as usize];
330 }
331 }
332
333 let form = if join {
335 JoiningForm::Terminal
336 } else {
337 JoiningForm::Isolated
338 };
339 last_form = Some(form);
340 for info in buffer.info.get_mut(start..end)? {
341 info.mask = (info.mask & other_masks) | masks[form as usize];
342 }
343 }
344
345 last_start = start;
346 start = end;
347 end = buffer.next_syllable(start);
348 }
349 Some(())
350}
351
352fn record_rphf(plan: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
353 let universal_plan = plan.data::<UniversalShapePlan>();
354
355 let mask = universal_plan.rphf_mask;
356 if mask == 0 {
357 return Some(false);
358 }
359
360 let mut start = 0;
361 let mut end = buffer.next_syllable(0);
362 while start < buffer.len {
363 for info in buffer.info.get_mut(start..end).unwrap_or_default() {
365 if info.mask & mask == 0 {
366 break;
367 }
368
369 if info.substituted() {
370 info.set_use_category(category::R);
371 break;
372 }
373 }
374
375 start = end;
376 end = buffer.next_syllable(start);
377 }
378
379 Some(false)
380}
381
382fn reorder_use(_: &ShapePlan, font: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
383 use super::use_machine::SyllableType;
384
385 let mut ret = false;
386 if !buffer_message!(buffer, font, "start reordering USE") {
387 buffer.deallocate_var(GlyphInfo::USE_CATEGORY_VAR);
388 return Some(ret);
389 }
390
391 if insert_dotted_circles(
392 font,
393 buffer,
394 SyllableType::BrokenCluster as u8,
395 category::B,
396 Some(category::R),
397 None,
398 )? {
399 ret = true;
400 }
401
402 let mut start = 0;
403 let mut end = buffer.next_syllable(0);
404 while start < buffer.len {
405 if reorder_syllable_use(start, end, buffer).is_none() {
406 buffer.index_failed();
407 }
408 start = end;
409 end = buffer.next_syllable(start);
410 }
411
412 buffer.deallocate_var(GlyphInfo::USE_CATEGORY_VAR);
413
414 buffer_message!(buffer, font, "end reordering USE");
415 Some(ret)
416}
417
418const fn category_flag(c: Category) -> u32 {
419 rb_flag(c as u32)
420}
421
422const fn category_flag64(c: Category) -> u64 {
423 rb_flag64(c as u32)
424}
425
426const POST_BASE_FLAGS: u64 = category_flag64(category::FAbv)
427 | category_flag64(category::FBlw)
428 | category_flag64(category::FPst)
429 | category_flag64(category::FMAbv)
430 | category_flag64(category::FMBlw)
431 | category_flag64(category::FMPst)
432 | category_flag64(category::MAbv)
433 | category_flag64(category::MBlw)
434 | category_flag64(category::MPst)
435 | category_flag64(category::MPre)
436 | category_flag64(category::VAbv)
437 | category_flag64(category::VBlw)
438 | category_flag64(category::VPst)
439 | category_flag64(category::VPre)
440 | category_flag64(category::VMAbv)
441 | category_flag64(category::VMBlw)
442 | category_flag64(category::VMPst)
443 | category_flag64(category::VMPre);
444
445fn reorder_syllable_use(start: usize, end: usize, buffer: &mut Buffer) -> Option<()> {
446 use super::use_machine::SyllableType;
447
448 let syllable_type = (buffer.info.get(start)?.syllable() & 0x0F) as u32;
449 if (rb_flag_unsafe(syllable_type)
451 & (rb_flag(SyllableType::ViramaTerminatedCluster as u32)
452 | rb_flag(SyllableType::SakotTerminatedCluster as u32)
453 | rb_flag(SyllableType::StandardCluster as u32)
454 | rb_flag(SyllableType::SymbolCluster as u32)
455 | rb_flag(SyllableType::BrokenCluster as u32)
456 | 0))
457 == 0
458 {
459 return Some(());
460 }
461
462 if buffer.info.get(start)?.use_category() == category::R && end - start > 1 {
464 for i in start + 1..end {
466 let info = buffer.info.get(i)?;
467 let is_post_base_glyph =
468 (rb_flag64_unsafe(info.use_category() as u32) & POST_BASE_FLAGS) != 0
469 || info.is_halant_use();
470
471 if is_post_base_glyph || i == end - 1 {
472 let mut i = i;
476 if is_post_base_glyph {
477 i -= 1;
478 }
479
480 buffer.merge_clusters(start, i + 1);
481 buffer.info.get_mut(start..=i)?.rotate_left(1);
482
483 break;
484 }
485 }
486 }
487
488 let mut j = start;
490 for i in start..end {
491 let info = buffer.info.get(i)?;
492 let flag = rb_flag_unsafe(info.use_category() as u32);
493 if info.is_halant_use() {
494 j = i + 1;
497 } else if (flag & (category_flag(category::VPre) | category_flag(category::VMPre))) != 0
498 && info.lig_comp() == 0
499 && j < i
500 {
501 buffer.merge_clusters(j, i + 1);
503 buffer.info.get_mut(j..=i)?.rotate_right(1);
504 }
505 }
506 Some(())
507}
508
509fn record_pref(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<bool> {
510 let mut start = 0;
511 let mut end = buffer.next_syllable(0);
512 while start < buffer.len {
513 for info in buffer.info.get_mut(start..end).unwrap_or_default() {
515 if info.substituted() {
516 info.set_use_category(category::VPre);
517 break;
518 }
519 }
520
521 start = end;
522 end = buffer.next_syllable(start);
523 }
524
525 Some(false)
526}
527
528fn has_arabic_joining(script: Script) -> bool {
529 matches!(
531 script,
532 Script::ADLAM
533 | Script::ARABIC
534 | Script::CHORASMIAN
535 | Script::HANIFI_ROHINGYA
536 | Script::MANDAIC
537 | Script::MANICHAEAN
538 | Script::MONGOLIAN
539 | Script::NKO
540 | Script::OLD_UYGHUR
541 | Script::PHAGS_PA
542 | Script::PSALTER_PAHLAVI
543 | Script::SOGDIAN
544 | Script::SYRIAC
545 )
546}
547
548fn preprocess_text(_: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) -> Option<()> {
549 vowel_constraints::preprocess_text_vowel_constraints(buffer);
550 Some(())
551}
552
553fn compose(_: &NormalizeContext, a: Codepoint, b: Codepoint) -> Option<Codepoint> {
554 if a.general_category().is_mark() {
556 return None;
557 }
558 unicode::compose(a, b)
559}
560
561fn setup_masks(plan: &ShapePlan, _: &ShaperFont<'_, '_>, buffer: &mut Buffer) {
562 let universal_plan = plan.data::<UniversalShapePlan>();
563
564 if let Some(ref arabic_plan) = universal_plan.arabic_plan {
566 arabic::setup_masks_inner(arabic_plan, plan.script, buffer);
567 }
568
569 buffer.allocate_var(GlyphInfo::USE_CATEGORY_VAR);
570
571 for info in buffer.info_slice_mut() {
574 info.set_use_category(use_table::use_get_category(info.glyph_id as usize));
575 }
576}
577
578#[cfg(test)]
579mod tests {
580 use super::super::use_machine::{find_syllables, SyllableType};
581 use super::*;
582
583 #[test]
584 fn symbol_clusters_reorder_prebase_marks() {
585 for base in [category::O, category::GB, category::SB] {
586 for mark in [category::VPre, category::VMPre] {
587 let mut buffer = Buffer::new();
588 buffer.push(0x111CD, 0);
589 buffer.push(0x111CE, 4);
590 buffer.info[0].set_use_category(base);
591 buffer.info[1].set_use_category(mark);
592 find_syllables(&mut buffer);
593 assert_eq!(
594 buffer.info[0].syllable() & 0x0F,
595 if base == category::GB {
596 SyllableType::StandardCluster
597 } else {
598 SyllableType::SymbolCluster
599 } as u8
600 );
601 reorder_syllable_use(0, 2, &mut buffer);
602 assert_eq!(buffer.info[0].glyph_id, 0x111CE);
603 assert_eq!(buffer.info[1].glyph_id, 0x111CD);
604 assert_eq!(buffer.info[0].cluster, 0);
605 assert_eq!(buffer.info[1].cluster, 0);
606 }
607 }
608 }
609}