1use super::buffer::GlyphInfo;
4use super::buffer::{Buffer, GlyphPropsFlags};
5use super::cache::Cache;
6use super::layout::*;
7use super::lookup_flags;
8use super::Mask;
9use super::OtData;
10use crate::hint::Likely;
11use crate::unicode::GeneralCategory;
12use crate::Scale;
13use read_fonts::tables::layout::SequenceLookupRecord;
14use read_fonts::types::GlyphId;
15
16pub(crate) type MatchPositions = smallvec::SmallVec<[u32; 8]>;
17
18pub fn match_glyph(info: &mut GlyphInfo, value: u32) -> bool {
20 info.glyph_id == value
21}
22
23pub fn match_always(_info: &mut GlyphInfo, _value: u32) -> bool {
24 true
25}
26
27pub fn match_input(
28 ctx: &mut ApplyContext,
29 input_len: u16,
30 match_func: impl Fn(&mut GlyphInfo, u32) -> bool,
31 end_position: &mut usize,
32 p_total_component_count: Option<&mut u8>,
33) -> Option<bool> {
34 #[derive(PartialEq)]
57 enum Ligbase {
58 NotChecked,
59 MayNotSkip,
60 MaySkip,
61 }
62
63 let count = usize::from(input_len) + 1;
64
65 if count == 1 {
66 *end_position = ctx.buffer.idx + 1;
67 ctx.match_positions_len = 1;
68 ctx.match_positions[0] = ctx.buffer.idx as u32;
69 if let Some(p_total_component_count) = p_total_component_count {
70 *p_total_component_count = ctx.buffer.cur(0)?.lig_num_comps();
71 }
72 return Some(true);
73 }
74
75 if count > MAX_CONTEXT_LENGTH {
76 return Some(false);
77 }
78 ctx.match_positions_len = count;
79
80 let mut iter = SkippingIterator::with_match_fn(ctx, false, Some(match_func));
81 if iter.reset(iter.buffer.idx).is_none() {
82 iter.buffer.index_failed();
83 }
84 iter.set_glyph_data(0);
85
86 let first = *iter.buffer.cur(0)?;
87 let first_lig_id = first.lig_id();
88 let first_lig_comp = first.lig_comp();
89 let mut total_component_count = 0;
90 let mut ligbase = Ligbase::NotChecked;
91
92 for i in 1..count {
93 let mut unsafe_to = 0;
94 if !iter.next(Some(&mut unsafe_to)) {
95 *end_position = unsafe_to;
96 return Some(false);
97 }
98
99 iter.set_match_position(i, iter.index());
100
101 let Some(&this) = iter.buffer.info.get(iter.index()) else {
102 return Some(false);
103 };
104 let this_lig_id = this.lig_id();
105 let this_lig_comp = this.lig_comp();
106
107 if first_lig_id != 0 && first_lig_comp != 0 {
108 if first_lig_id != this_lig_id || first_lig_comp != this_lig_comp {
112 if ligbase == Ligbase::NotChecked {
115 let out = iter.buffer.out_info();
116 let mut j = iter.buffer.out_len;
117 let mut found = false;
118 while let Some(info) = j.checked_sub(1).and_then(|k| out.get(k)) {
119 if info.lig_id() != first_lig_id {
120 break;
121 }
122 j -= 1;
123 if info.lig_comp() == 0 {
124 found = true;
125 break;
126 }
127 }
128
129 ligbase = if found
130 && out
131 .get(j)
132 .is_some_and(|info| iter.may_skip(info) == MaySkip::Yes)
133 {
134 Ligbase::MaySkip
135 } else {
136 Ligbase::MayNotSkip
137 };
138 }
139
140 if ligbase == Ligbase::MayNotSkip {
141 return Some(false);
142 }
143 }
144 } else {
145 if this_lig_id != 0 && this_lig_comp != 0 && (this_lig_id != first_lig_id) {
149 return Some(false);
150 }
151 }
152
153 total_component_count += this.lig_num_comps_in_ligation();
154 }
155
156 *end_position = iter.index() + 1;
157
158 if let Some(p_total_component_count) = p_total_component_count {
159 total_component_count += first.lig_num_comps();
160 *p_total_component_count = total_component_count;
161 }
162
163 ctx.match_positions[0] = iter.buffer.idx as u32;
164
165 Some(true)
166}
167
168pub fn match_backtrack(
169 ctx: &mut ApplyContext,
170 backtrack_len: u16,
171 match_func: impl Fn(&mut GlyphInfo, u32) -> bool,
172 match_start: &mut usize,
173) -> bool {
174 if backtrack_len == 0 {
175 *match_start = ctx.buffer.backtrack_len();
176 return true;
177 }
178
179 let mut iter = SkippingIterator::with_match_fn(ctx, true, Some(match_func));
180 if iter.reset_back(iter.buffer.backtrack_len()).is_none() {
181 iter.buffer.index_failed();
182 }
183 iter.set_glyph_data(0);
184
185 for _ in 0..backtrack_len {
186 let mut unsafe_from = 0;
187 if !iter.prev(Some(&mut unsafe_from)) {
188 *match_start = unsafe_from;
189 return false;
190 }
191 }
192
193 *match_start = iter.index();
194 true
195}
196
197pub fn match_lookahead(
198 ctx: &mut ApplyContext,
199 lookahead_len: u16,
200 match_func: impl Fn(&mut GlyphInfo, u32) -> bool,
201 start_index: usize,
202 end_index: &mut usize,
203) -> bool {
204 if lookahead_len == 0 {
205 *end_index = start_index;
206 return true;
207 }
208
209 debug_assert!(start_index >= 1);
212 let mut iter = SkippingIterator::with_match_fn(ctx, true, Some(match_func));
213 if iter.reset(start_index - 1).is_none() {
214 iter.buffer.index_failed();
215 }
216 iter.set_glyph_data(0);
217
218 for _ in 0..lookahead_len {
219 let mut unsafe_to = 0;
220 if !iter.next(Some(&mut unsafe_to)) {
221 *end_index = unsafe_to;
222 return false;
223 }
224 }
225
226 *end_index = iter.index() + 1;
227 true
228}
229
230#[derive(PartialEq, Eq, Copy, Clone)]
231pub enum MatchResult {
232 Match,
233 NotMatch,
234 Skip,
235}
236
237#[derive(PartialEq, Eq, Copy, Clone)]
238enum MayMatch {
239 No,
240 Yes,
241 Maybe,
242}
243
244#[derive(PartialEq, Eq, Copy, Clone)]
245pub enum MaySkip {
246 No,
247 Yes,
248 Maybe,
249}
250
251#[derive(Default)]
252pub struct Matcher {
253 lookup_props: u32,
254 mask: Mask,
255 ignore_zwnj: bool,
256 ignore_zwj: bool,
257 ignore_hidden: bool,
258 per_syllable: bool,
259}
260
261impl Matcher {
262 fn new(ctx: &ApplyContext, context_match: bool) -> Self {
263 Matcher {
264 lookup_props: ctx.lookup_props,
265 ignore_zwnj: ctx.table_index == LayoutTableKind::Gpos
267 || (context_match && ctx.auto_zwnj),
268 ignore_zwj: context_match || ctx.auto_zwj,
270 ignore_hidden: ctx.table_index == LayoutTableKind::Gpos,
272 mask: if context_match {
273 u32::MAX
274 } else {
275 ctx.lookup_mask()
276 },
277 per_syllable: ctx.table_index == LayoutTableKind::Gsub && ctx.per_syllable,
279 }
280 }
281
282 fn may_match(
283 &self,
284 info: &mut GlyphInfo,
285 glyph_data: u32,
286 match_func: Option<&impl Fn(&mut GlyphInfo, u32) -> bool>,
287 syllable: u8,
288 ) -> MayMatch {
289 if (info.mask & self.mask) == 0
290 || (self.per_syllable && syllable != 0 && syllable != info.syllable())
291 {
292 return MayMatch::No;
293 }
294
295 if let Some(match_func) = match_func {
296 return if match_func(info, glyph_data) {
297 MayMatch::Yes
298 } else {
299 MayMatch::No
300 };
301 }
302
303 MayMatch::Maybe
304 }
305
306 #[inline(always)]
307 fn may_skip(&self, info: &GlyphInfo, ot: &OtData, lookup_props: u32) -> MaySkip {
308 if !check_glyph_property(ot, info, lookup_props) {
309 return MaySkip::Yes;
310 }
311
312 if info.is_default_ignorable()
313 && (self.ignore_zwnj || !info.is_zwnj())
314 && (self.ignore_zwj || !info.is_zwj())
315 && (self.ignore_hidden || !info.is_hidden())
316 {
317 return MaySkip::Maybe;
318 }
319
320 MaySkip::No
321 }
322}
323
324#[inline(always)]
325fn match_info<F: Fn(&mut GlyphInfo, u32) -> bool>(
326 matcher: &Matcher,
327 info: &mut GlyphInfo,
328 ot: &OtData,
329 lookup_props: u32,
330 glyph_data: u32,
331 match_func: Option<&F>,
332 syllable: u8,
333) -> MatchResult {
334 let skip = matcher.may_skip(info, ot, lookup_props);
335
336 if skip == MaySkip::Yes {
337 return MatchResult::Skip;
338 }
339
340 let _match = matcher.may_match(info, glyph_data, match_func, syllable);
341
342 if _match == MayMatch::Yes || (_match == MayMatch::Maybe && skip == MaySkip::No) {
343 return MatchResult::Match;
344 }
345
346 if skip == MaySkip::No {
347 return MatchResult::NotMatch;
348 }
349
350 MatchResult::Skip
351}
352
353pub struct SkippingIterator<'f, 'c, F> {
360 pub(crate) buffer: &'c mut Buffer,
361 ot: &'c OtData<'f>,
362 matcher: &'c Matcher,
363 match_positions: &'c mut MatchPositions,
364 buf_len: usize,
365 glyph_data: u32,
366 pub(crate) buf_idx: usize,
367 match_func: Option<F>,
368 lookup_props: u32,
369 syllable: u8,
370}
371
372impl<'f, 'c> SkippingIterator<'f, 'c, fn(&mut GlyphInfo, u32) -> bool> {
373 pub fn new(ctx: &'c mut ApplyContext<'f, '_, '_>, context_match: bool) -> Self {
374 Self::with_match_fn(ctx, context_match, None)
375 }
376}
377
378impl<'f, 'c, F> SkippingIterator<'f, 'c, F>
379where
380 F: Fn(&mut GlyphInfo, u32) -> bool,
381{
382 pub fn with_match_fn(
383 ctx: &'c mut ApplyContext<'f, '_, '_>,
384 context_match: bool,
385 match_fn: Option<F>,
386 ) -> Self {
387 let matcher = if context_match {
388 &ctx.context_matcher
389 } else {
390 &ctx.matcher
391 };
392 let buf_len = ctx.buffer.len;
393 SkippingIterator {
394 buffer: ctx.buffer,
395 ot: ctx.layout.ot,
396 glyph_data: 0,
397 buf_len,
398 buf_idx: 0,
399 matcher,
400 match_func: match_fn,
401 match_positions: &mut ctx.match_positions,
402 lookup_props: matcher.lookup_props,
403 syllable: 0,
404 }
405 }
406
407 pub fn set_match_position(&mut self, idx: usize, position: usize) {
408 let count = idx + 1;
409 if count > self.match_positions.len() {
410 self.match_positions.resize(count, 0);
411 }
412
413 self.match_positions[idx] = position as u32;
414 }
415
416 pub fn set_glyph_data(&mut self, glyph_data: u32) {
417 self.glyph_data = glyph_data;
418 }
419
420 fn advance_glyph_data(&mut self) {
421 self.glyph_data += 1;
422 }
423
424 pub fn set_lookup_props(&mut self, lookup_props: u32) {
425 self.lookup_props = lookup_props;
426 }
427
428 pub fn index(&self) -> usize {
429 self.buf_idx
430 }
431
432 #[inline]
433 pub fn next(&mut self, unsafe_to: Option<&mut usize>) -> bool {
434 let stop = self.buf_len.saturating_sub(1);
435
436 while self.buf_idx < stop {
437 self.buf_idx += 1;
438
439 match self.match_at(self.buf_idx) {
440 MatchResult::Match => {
441 self.advance_glyph_data();
442 return true;
443 }
444 MatchResult::NotMatch => {
445 if let Some(unsafe_to) = unsafe_to {
446 *unsafe_to = self.buf_idx + 1;
447 }
448
449 return false;
450 }
451 MatchResult::Skip => continue,
452 }
453 }
454
455 if let Some(unsafe_to) = unsafe_to {
456 *unsafe_to = self.buf_idx + 1;
457 }
458
459 false
460 }
461
462 #[inline]
463 pub fn prev(&mut self, unsafe_from: Option<&mut usize>) -> bool {
464 let stop: usize = 0;
465 let out_info = self.buffer.out_info_mut();
466
467 while self.buf_idx > stop {
468 self.buf_idx -= 1;
469
470 let result = match out_info.get_mut(self.buf_idx) {
472 Some(info) => match_info(
473 self.matcher,
474 info,
475 self.ot,
476 self.lookup_props,
477 self.glyph_data,
478 self.match_func.as_ref(),
479 self.syllable,
480 ),
481 None => MatchResult::NotMatch,
482 };
483 match result {
484 MatchResult::Match => {
485 self.advance_glyph_data();
486 return true;
487 }
488 MatchResult::NotMatch => {
489 if let Some(unsafe_from) = unsafe_from {
490 *unsafe_from = self.buf_idx.max(1) - 1;
491 }
492
493 return false;
494 }
495 MatchResult::Skip => {
496 continue;
497 }
498 }
499 }
500
501 if let Some(unsafe_from) = unsafe_from {
502 *unsafe_from = 0;
503 }
504
505 false
506 }
507
508 pub fn reset(&mut self, start_index: usize) -> Option<()> {
509 self.buf_idx = start_index;
511 self.buf_len = self.buffer.len;
512 self.syllable = self.buffer.cur(0)?.syllable();
513 Some(())
514 }
515
516 pub fn reset_back(&mut self, start_index: usize) -> Option<()> {
517 self.buf_idx = start_index;
519 self.syllable = self.buffer.cur(0)?.syllable();
520 Some(())
521 }
522
523 pub fn reset_fast(&mut self, start_index: usize) {
524 self.buf_idx = start_index;
526 }
527
528 pub fn may_skip(&self, info: &GlyphInfo) -> MaySkip {
529 self.matcher.may_skip(info, self.ot, self.lookup_props)
530 }
531
532 #[inline]
533 pub fn match_at(&mut self, idx: usize) -> MatchResult {
534 let Some(info) = self.buffer.info.get_mut(idx).likely() else {
536 return MatchResult::NotMatch;
537 };
538 match_info(
539 self.matcher,
540 info,
541 self.ot,
542 self.lookup_props,
543 self.glyph_data,
544 self.match_func.as_ref(),
545 self.syllable,
546 )
547 }
548}
549
550pub(crate) fn apply_lookup(
551 ctx: &mut ApplyContext,
552 input_len: usize,
553 match_end: usize,
554 lookups: &[SequenceLookupRecord],
555) {
556 let mut count = input_len + 1;
557
558 debug_assert!(count <= ctx.match_positions.len(), "");
559
560 let mut end: isize = {
563 let backtrack_len = ctx.buffer.backtrack_len();
564 let delta = backtrack_len as isize - ctx.buffer.idx as isize;
565
566 for j in 0..count {
568 ctx.match_positions[j] = (ctx.match_positions[j] as isize + delta) as _;
569 }
570
571 backtrack_len as isize + match_end as isize - ctx.buffer.idx as isize
572 };
573
574 for record in lookups {
575 if !ctx.buffer.successful {
576 break;
577 }
578
579 let idx = usize::from(record.sequence_index.get());
580 if idx >= count {
581 continue;
582 }
583
584 let orig_len = ctx.buffer.backtrack_len() + ctx.buffer.lookahead_len();
585
586 if ctx.match_positions[idx] as usize >= orig_len {
588 continue;
589 }
590
591 if !ctx.buffer.move_to(ctx.match_positions[idx] as usize) {
592 break;
593 }
594
595 if ctx.buffer.max_ops <= 0 {
596 break;
597 }
598
599 message!(
600 ctx,
601 "start recursing to lookup {} at {}",
602 record.lookup_list_index.get(),
603 ctx.buffer.message_idx()
604 );
605
606 let applied = ctx.recurse(record.lookup_list_index.get()).is_some();
607 message!(
608 ctx,
609 "end recursing to lookup {}",
610 record.lookup_list_index.get(),
611 );
612 if !applied {
613 continue;
614 }
615
616 let new_len = ctx.buffer.backtrack_len() + ctx.buffer.lookahead_len();
617 let mut delta = new_len as isize - orig_len as isize;
618 if delta == 0 {
619 continue;
620 }
621
622 end += delta;
646 if end < ctx.match_positions[idx] as isize {
647 delta += ctx.match_positions[idx] as isize - end;
656 end = ctx.match_positions[idx] as isize;
657 }
658
659 let mut next = idx + 1;
661
662 if delta > 0 {
663 if delta as usize + count > MAX_CONTEXT_LENGTH {
664 break;
665 }
666
667 if delta as usize + count > ctx.match_positions.len() {
668 ctx.match_positions.resize(delta as usize + count, 0);
669 }
670 } else {
671 delta = delta.max(next as isize - count as isize);
673 next = (next as isize - delta) as _;
674 }
675
676 ctx.match_positions
678 .copy_within(next..count, (next as isize + delta) as _);
679 next = (next as isize + delta) as _;
680 count = (count as isize + delta) as _;
681 ctx.match_positions_len = count;
682
683 for j in idx + 1..next {
685 ctx.match_positions[j] = ctx.match_positions[j - 1] + 1;
686 }
687
688 while next < count {
690 ctx.match_positions[next] = (ctx.match_positions[next] as isize + delta) as _;
691 next += 1;
692 }
693 }
694
695 ctx.buffer.move_to(end.try_into().unwrap());
696}
697
698pub(crate) type MappingCache = Cache<
701 16, 8, 256, 16, >;
706
707#[derive(Copy, Clone, Default)]
708pub(crate) struct RuleSetDigest(u64);
710
711impl RuleSetDigest {
712 pub fn full() -> Self {
713 Self(u64::MAX)
714 }
715
716 pub fn add(&mut self, value: u32) {
717 self.0 |= 1 << (value & 63);
718 }
719
720 pub fn is_full(self) -> bool {
721 self.0 == u64::MAX
722 }
723
724 pub fn may_have(self, value: u32) -> bool {
725 self.0 & (1 << (value & 63)) != 0
726 }
727}
728
729pub struct WouldApplyContext<'a> {
730 pub glyphs: &'a [GlyphId],
731 pub zero_context: bool,
732}
733
734fn match_properties_mark(
735 ot: &OtData,
736 info: &GlyphInfo,
737 glyph_props: u16,
738 match_props: u32,
739) -> bool {
740 if match_props as u16 & lookup_flags::USE_MARK_FILTERING_SET != 0 {
743 let set_index = (match_props >> 16) as u16;
744 return ot.is_mark_glyph(info.as_glyph().to_u32(), set_index);
745 }
746
747 if match_props as u16 & lookup_flags::MARK_ATTACHMENT_TYPE_MASK != 0 {
751 return (match_props as u16 & lookup_flags::MARK_ATTACHMENT_TYPE_MASK)
752 == (glyph_props & lookup_flags::MARK_ATTACHMENT_TYPE_MASK);
753 }
754
755 true
756}
757
758#[inline(always)]
759pub fn check_glyph_property(ot: &OtData, info: &GlyphInfo, match_props: u32) -> bool {
760 let glyph_props = info.glyph_props();
761
762 if glyph_props & match_props as u16 & lookup_flags::IGNORE_FLAGS != 0 {
765 return false;
766 }
767
768 if glyph_props & GlyphPropsFlags::MARK.bits() != 0 {
769 return match_properties_mark(ot, info, glyph_props, match_props);
770 }
771
772 true
773}
774
775pub struct ApplyContext<'a, 'f, 'c> {
776 pub table_index: LayoutTableKind,
777 pub layout: crate::LayoutData<'a>,
778 pub scale: Scale,
779 #[cfg(feature = "tracing")]
780 pub font: &'a crate::ShaperFont<'f, 'c>,
781 #[cfg(not(feature = "tracing"))]
782 font_lifetimes: core::marker::PhantomData<(&'f (), &'c ())>,
783 pub buffer: &'a mut Buffer,
784 lookup_mask: Mask,
785 pub per_syllable: bool,
786 pub lookup_index: u16,
787 pub lookup_props: u32,
788 pub nesting_level_left: usize,
789 pub auto_zwnj: bool,
790 pub auto_zwj: bool,
791 pub random: bool,
792 pub random_state: u32,
793 pub new_syllables: Option<u8>,
794 pub last_base: i32,
795 pub last_base_until: u32,
796 pub(crate) matcher: Matcher,
797 pub(crate) context_matcher: Matcher,
798 pub(crate) match_positions_len: usize,
799 pub(crate) match_positions: MatchPositions,
800}
801
802#[cfg_attr(not(feature = "tracing"), allow(clippy::elidable_lifetime_names))]
804impl<'a, 'f, 'c> ApplyContext<'a, 'f, 'c> {
805 pub fn new(
806 table_index: LayoutTableKind,
807 layout: crate::LayoutData<'a>,
808 scale: Scale,
809 buffer: &'a mut Buffer,
810 #[cfg(feature = "tracing")] font: &'a crate::ShaperFont<'f, 'c>,
811 ) -> Self {
812 Self {
813 table_index,
814 layout,
815 scale,
816 #[cfg(not(feature = "tracing"))]
817 font_lifetimes: core::marker::PhantomData,
818 buffer,
819 #[cfg(feature = "tracing")]
820 font,
821 lookup_mask: 1,
822 per_syllable: false,
823 lookup_index: u16::MAX,
824 lookup_props: u32::MAX,
825 nesting_level_left: MAX_NESTING_LEVEL,
826 auto_zwnj: true,
827 auto_zwj: true,
828 random: false,
829 random_state: 1,
830 new_syllables: None,
831 last_base: -1,
832 last_base_until: 0,
833 matcher: Matcher::default(),
834 context_matcher: Matcher::default(),
835 match_positions_len: 0,
836 match_positions: MatchPositions::from_elem(0, 1),
837 }
838 }
839
840 #[inline(always)]
841 pub fn scale_x(&self, value: f32) -> i32 {
842 self.scale.scale_x_f(value)
843 }
844
845 #[inline(always)]
846 pub fn scale_y(&self, value: f32) -> i32 {
847 self.scale.scale_y_f(value)
848 }
849
850 pub fn random_number(&mut self) -> u32 {
851 self.random_state = self.random_state.wrapping_mul(48271) % (i32::MAX as u32);
853 self.random_state
854 }
855
856 pub fn set_lookup_mask(&mut self, mask: Mask) {
857 self.lookup_mask = mask;
858 self.last_base = -1;
859 self.last_base_until = 0;
860 }
861
862 pub fn lookup_mask(&self) -> Mask {
863 self.lookup_mask
864 }
865
866 pub fn update_matchers(&mut self) {
867 self.matcher = Matcher::new(self, false);
868 self.context_matcher = Matcher::new(self, true);
869 }
870
871 pub fn recurse(&mut self, sub_lookup_index: u16) -> Option<()> {
872 if self.nesting_level_left == 0 {
873 self.buffer.successful = false;
874 return None;
875 }
876
877 self.buffer.max_ops -= 1;
878 if self.buffer.max_ops < 0 {
879 self.buffer.successful = false;
880 return None;
881 }
882
883 self.nesting_level_left -= 1;
884 let saved_props = self.lookup_props;
885 let saved_index = self.lookup_index;
886
887 let saved_match_positions =
893 core::mem::replace(&mut self.match_positions, MatchPositions::from_elem(0, 1));
894 let saved_match_positions_len = self.match_positions_len;
895
896 self.lookup_index = sub_lookup_index;
897 let applied = self
898 .layout
899 .ot
900 .lookup(self.table_index, sub_lookup_index)
901 .and_then(|lookup| {
902 self.lookup_props = lookup.props();
903 self.update_matchers();
904 lookup.apply(self, false)
905 });
906 self.lookup_props = saved_props;
907 self.lookup_index = saved_index;
908 self.update_matchers();
909 self.match_positions = saved_match_positions;
910 self.match_positions_len = saved_match_positions_len;
911 self.nesting_level_left += 1;
912 applied
913 }
914
915 fn set_glyph_class(
916 &mut self,
917 glyph_id: GlyphId,
918 class_guess: GlyphPropsFlags,
919 ligature: bool,
920 component: bool,
921 ) -> Option<()> {
922 self.buffer.digest.add(glyph_id.into());
923
924 if let Some(syllable) = self.new_syllables {
925 self.buffer.cur_mut(0)?.set_syllable(syllable);
926 }
927
928 let cur = self.buffer.cur_mut(0)?;
929 let mut props = cur.glyph_props();
930
931 props |= GlyphPropsFlags::SUBSTITUTED.bits();
932
933 if ligature {
934 props |= GlyphPropsFlags::LIGATED.bits();
935 props &= !GlyphPropsFlags::MULTIPLIED.bits();
941 }
942
943 if component {
944 props |= GlyphPropsFlags::MULTIPLIED.bits();
945 }
946
947 let has_glyph_classes = self.layout.ot.has_glyph_classes();
948
949 if has_glyph_classes {
950 props &= GlyphPropsFlags::PRESERVE.bits();
951 cur.set_glyph_props(props | self.layout.ot.glyph_props(glyph_id));
952 } else if !class_guess.is_empty() {
953 props &= GlyphPropsFlags::PRESERVE.bits();
954 cur.set_glyph_props(props | class_guess.bits());
955 } else {
956 cur.set_glyph_props(props);
957 }
958 Some(())
959 }
960
961 pub fn replace_glyph(&mut self, glyph_id: GlyphId) {
962 if self
963 .set_glyph_class(glyph_id, GlyphPropsFlags::empty(), false, false)
964 .is_none()
965 {
966 self.buffer.index_failed();
967 }
968 self.buffer.replace_glyph(u32::from(glyph_id));
969 }
970
971 pub fn replace_glyph_inplace(&mut self, glyph_id: GlyphId) -> Option<()> {
972 if self
973 .set_glyph_class(glyph_id, GlyphPropsFlags::empty(), false, false)
974 .is_none()
975 {
976 self.buffer.index_failed();
977 }
978 self.buffer.cur_mut(0)?.glyph_id = u32::from(glyph_id);
979 Some(())
980 }
981
982 pub fn replace_glyph_with_ligature(&mut self, glyph_id: GlyphId, class_guess: GlyphPropsFlags) {
983 if self
984 .set_glyph_class(glyph_id, class_guess, true, false)
985 .is_none()
986 {
987 self.buffer.index_failed();
988 }
989 self.buffer.replace_glyph(u32::from(glyph_id));
990 }
991
992 pub fn output_glyph_for_component(&mut self, glyph_id: GlyphId, class_guess: GlyphPropsFlags) {
993 if self
994 .set_glyph_class(glyph_id, class_guess, false, true)
995 .is_none()
996 {
997 self.buffer.index_failed();
998 }
999 self.buffer.output_glyph(u32::from(glyph_id));
1000 }
1001}
1002
1003pub fn ligate_input(
1004 ctx: &mut ApplyContext,
1005 count: usize,
1007 match_end: usize,
1009 total_component_count: u8,
1010 lig_glyph: GlyphId,
1011) -> Option<()> {
1012 let info = &ctx.buffer.info;
1047 let first_info = info.get(ctx.match_positions[0] as usize).likely()?;
1048 let mut is_base_ligature = first_info.is_base_glyph();
1049 let mut is_mark_ligature = first_info.is_mark();
1050 for i in 1..count {
1051 if !info
1052 .get(ctx.match_positions[i] as usize)
1053 .likely()?
1054 .is_mark()
1055 {
1056 is_base_ligature = false;
1057 is_mark_ligature = false;
1058 }
1059 }
1060
1061 ctx.buffer.merge_clusters(ctx.buffer.idx, match_end);
1062
1063 let is_ligature = !is_base_ligature && !is_mark_ligature;
1064 let class = if is_ligature {
1065 GlyphPropsFlags::LIGATURE
1066 } else {
1067 GlyphPropsFlags::empty()
1068 };
1069 let lig_id = if is_ligature {
1070 ctx.buffer.allocate_lig_id()
1071 } else {
1072 0
1073 };
1074 let first = ctx.buffer.cur_mut(0)?;
1075 let mut last_lig_id = first.lig_id();
1076 let mut last_num_comps = first.lig_num_comps();
1077 let mut comps_so_far = last_num_comps;
1078
1079 if is_ligature {
1080 first.set_lig_props_for_ligature(lig_id, total_component_count);
1081 if first.general_category() == GeneralCategory::NON_SPACING_MARK {
1082 first.set_general_category(GeneralCategory::OTHER_LETTER);
1083 }
1084 }
1085
1086 ctx.replace_glyph_with_ligature(lig_glyph, class);
1087
1088 for i in 1..count {
1089 while ctx.buffer.idx < ctx.match_positions[i] as usize && ctx.buffer.successful {
1090 if is_ligature {
1091 let cur = ctx.buffer.cur_mut(0)?;
1092 let mut this_comp = cur.lig_comp();
1093 if this_comp == 0 {
1094 this_comp = last_num_comps;
1095 }
1096 debug_assert!(comps_so_far >= last_num_comps);
1099 let new_lig_comp = comps_so_far - last_num_comps + this_comp.min(last_num_comps);
1100 cur.set_lig_props_for_mark(lig_id, new_lig_comp);
1101 }
1102 ctx.buffer.next_glyph();
1103 }
1104
1105 let cur = ctx.buffer.cur(0)?;
1106 last_lig_id = cur.lig_id();
1107 last_num_comps = cur.lig_num_comps_in_ligation();
1108 comps_so_far += last_num_comps;
1109
1110 ctx.buffer.idx += 1;
1112 }
1113
1114 if !is_mark_ligature && last_lig_id != 0 {
1115 let idx = ctx.buffer.idx;
1117 for info in ctx.buffer.info_slice_mut().iter_mut().skip(idx) {
1118 if last_lig_id != info.lig_id() {
1119 break;
1120 }
1121
1122 let this_comp = info.lig_comp();
1123 if this_comp == 0 {
1124 break;
1125 }
1126
1127 debug_assert!(comps_so_far >= last_num_comps);
1130 let new_lig_comp = comps_so_far - last_num_comps + this_comp.min(last_num_comps);
1131 info.set_lig_props_for_mark(lig_id, new_lig_comp);
1132 }
1133 }
1134
1135 Some(())
1136}