Skip to main content

harfrust/
buffer.rs

1use super::ot::layout::MAX_SYLLABLE_LENGTH;
2use super::Mask;
3use crate::set_digest::SetDigest;
4use crate::unicode::{CharExt, Codepoint};
5use crate::U32Set;
6use crate::{BufferFlags, ClusterLevel, Direction, Language, Script, SerializeFlags};
7#[cfg(feature = "tracing")]
8use alloc::boxed::Box;
9use alloc::{string::String, vec::Vec};
10use core::cmp::min;
11use read_fonts::types::{GlyphId, GlyphId16};
12
13use crate::hint::Likely;
14
15const CONTEXT_LENGTH: usize = 5;
16
17/// The advances and offsets of a shaped glyph.
18///
19/// Values use the scale of the shaping font. Offsets move a glyph relative to
20/// the current point without changing the position of the next glyph.
21#[repr(C)]
22#[derive(Clone, Copy, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
23pub struct GlyphPosition {
24    /// Horizontal advance after drawing this glyph.
25    pub x_advance: i32,
26    /// Vertical advance after drawing this glyph.
27    pub y_advance: i32,
28    /// Horizontal offset from the current point.
29    pub x_offset: i32,
30    /// Vertical offset from the current point.
31    pub y_offset: i32,
32    pub(crate) var: u32,
33}
34
35impl GlyphPosition {
36    #[inline]
37    pub(crate) fn attach_chain(&self) -> i16 {
38        // glyph to which this attaches to, relative to current glyphs;
39        // negative for going back, positive for forward.
40        let v: &[i16; 2] = bytemuck::cast_ref(&self.var);
41        v[0]
42    }
43
44    #[inline]
45    pub(crate) fn set_attach_chain(&mut self, n: i16) {
46        let v: &mut [i16; 2] = bytemuck::cast_mut(&mut self.var);
47        v[0] = n;
48    }
49
50    #[inline]
51    pub(crate) fn attach_type(&self) -> u8 {
52        // attachment type
53        // Note! if attach_chain() is zero, the value of attach_type() is irrelevant.
54        let v: &[u8; 4] = bytemuck::cast_ref(&self.var);
55        v[2]
56    }
57
58    #[inline]
59    pub(crate) fn set_attach_type(&mut self, n: u8) {
60        let v: &mut [u8; 4] = bytemuck::cast_mut(&mut self.var);
61        v[2] = n;
62    }
63}
64
65/// A glyph and its relationship to the input text.
66///
67/// Before shaping, each entry holds a Unicode codepoint. After shaping, it
68/// holds a glyph ID and the cluster of input text that produced it.
69#[repr(C)]
70#[derive(Clone, Copy, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
71pub struct GlyphInfo {
72    // NOTE: Stores a Unicode codepoint before shaping and a glyph ID after.
73    //       Just like harfbuzz, we are using the same variable for two purposes.
74    /// Unicode codepoint before shaping; glyph ID after shaping.
75    ///
76    /// Glyph IDs are not confined to `u16`. User-provided glyphs (e.g.
77    /// `Buffer::set_invisible_glyph`,
78    /// `Buffer::set_not_found_variation_selector_glyph`, or custom
79    /// `FontFuncs` (see crate::FontFuncs)) are provided as is and can be larger than `u16::MAX`
80    /// or the number of glyphs in the font.
81    pub glyph_id: u32,
82    pub(crate) mask: Mask,
83    /// Index into the original input text for this glyph's cluster.
84    ///
85    /// [Read more on clusters](https://harfbuzz.github.io/clusters.html).
86    pub cluster: u32,
87    pub(crate) vars: [u32; 2],
88}
89
90#[allow(dead_code)]
91pub(crate) struct buffer_var_shape {
92    pub(crate) width: u8,
93    pub(crate) var_index: u8,
94    pub(crate) index: u8,
95}
96
97impl buffer_var_shape {
98    #[inline]
99    pub fn start(&self) -> u8 {
100        (self.var_index - 1) * 4 + self.index * self.width
101    }
102
103    #[inline]
104    pub fn count(&self) -> u8 {
105        self.width
106    }
107
108    #[inline]
109    pub fn bits(&self) -> u8 {
110        let start = self.start();
111        let end = start + self.count();
112        debug_assert!(end <= 8);
113        ((1u16 << end) - (1u16 << start)) as u8
114    }
115}
116
117macro_rules! declare_buffer_var {
118    ($ty:ty, $var_index:expr, $index:expr, $var_name:ident, $getter:ident, $setter:ident) => {
119        #[allow(dead_code)]
120        pub(crate) const $var_name: buffer_var_shape = buffer_var_shape {
121            width: core::mem::size_of::<$ty>() as u8,
122            var_index: $var_index,
123            index: $index,
124        };
125
126        #[inline]
127        #[allow(dead_code)]
128        pub(crate) fn $getter(&self) -> $ty {
129            const LEN: usize = core::mem::size_of::<u32>() / core::mem::size_of::<$ty>();
130            let v: &[$ty; LEN] = bytemuck::cast_ref(&self.vars[$var_index - 1usize]);
131            v[$index]
132        }
133
134        #[inline]
135        #[allow(dead_code)]
136        pub(crate) fn $setter(&mut self, value: $ty) {
137            const LEN: usize = core::mem::size_of::<u32>() / core::mem::size_of::<$ty>();
138            let v: &mut [$ty; LEN] = bytemuck::cast_mut(&mut self.vars[$var_index - 1usize]);
139            v[$index] = value;
140        }
141    };
142}
143
144macro_rules! declare_buffer_var_alias {
145    ($alias_var:ident, $ty:ty, $var_name:ident, $getter:ident, $setter:ident) => {
146        #[allow(dead_code)]
147        pub(crate) const $var_name: buffer_var_shape = GlyphInfo::$alias_var;
148
149        #[inline]
150        pub(crate) fn $getter(&self) -> $ty {
151            const { assert!(GlyphInfo::$alias_var.width == core::mem::size_of::<$ty>() as u8) };
152            const LEN: usize = core::mem::size_of::<u32>() / core::mem::size_of::<$ty>();
153            let v: &[$ty; LEN] =
154                bytemuck::cast_ref(&self.vars[GlyphInfo::$alias_var.var_index as usize - 1usize]);
155            v[GlyphInfo::$alias_var.index as usize]
156        }
157
158        #[inline]
159        pub(crate) fn $setter(&mut self, value: $ty) {
160            const { assert!(GlyphInfo::$alias_var.width == core::mem::size_of::<$ty>() as u8) };
161            const LEN: usize = core::mem::size_of::<u32>() / core::mem::size_of::<$ty>();
162            let v: &mut [$ty; LEN] = bytemuck::cast_mut(
163                &mut self.vars[GlyphInfo::$alias_var.var_index as usize - 1usize],
164            );
165            v[GlyphInfo::$alias_var.index as usize] = value;
166        }
167    };
168}
169
170/// Flags that describe the properties of a glyph.
171#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
172pub struct GlyphFlags(pub(crate) u32);
173
174impl GlyphFlags {
175    /// Breaking before this cluster may change the shaping on either side.
176    ///
177    /// On the flip side, it means that when this flag is not present,
178    /// then it's safe to break the glyph-run at the beginning of this cluster,
179    /// and the two sides represent the exact same result one would get if breaking input text
180    /// at the beginning of this cluster and shaping the two sides separately.
181    /// This can be used to optimize paragraph layout, by avoiding re-shaping of each line
182    /// after line-breaking, or limiting the reshaping to a small piece around
183    /// the breaking point only.
184    pub const UNSAFE_TO_BREAK: Self = Self(0x0000_0001);
185
186    /// Changing text beside this cluster may affect shaping across its boundary.
187    /// Note that the absence of this flag will NOT by itself mean that it IS safe to concat
188    /// text. Only two pieces of text both of which clear of this flag can be concatenated
189    /// safely.
190    ///
191    /// This can be used to optimize paragraph layout, by avoiding re-shaping of each line after
192    /// line-breaking, by limiting the reshaping to a small piece around the breaking position
193    /// only, even if the breaking position carries the unsafe-to-break flag or when hyphenation
194    /// or other text transformation happens at line-break position, in the following way:
195    /// 1. Iterate back from the line-break position until the first cluster start position
196    ///    that is NOT unsafe-to-concat.
197    /// 2. Shape the segment from there till the end of line.
198    /// 3. Check whether the resulting glyph-run also is clear of the unsafe-to-concat at its
199    ///    start-of-text position; if it is, just splice it into place and the line is shaped;
200    ///    If not, move on to a position further back that is clear of unsafe-to-concat and retry
201    ///    from there, and repeat.
202    ///
203    /// At the start of next line a similar algorithm can be implemented. That is:
204    /// 1. Iterate forward from the line-break position until the first cluster start position that is
205    ///    NOT unsafe-to-concat.
206    /// 2. Shape the segment from beginning of the line to that position.
207    /// 3. Check whether the resulting glyph-run also is clear of the unsafe-to-concat at its end-of-text
208    ///    position; if it is, just splice it into place and the beginning is shaped; If not, move on to a
209    ///    position further forward that is clear of unsafe-to-concat and retry up to there, and repeat. A
210    ///    slight complication will arise in the implementation of the algorithm above, because while our
211    ///    buffer API has a way to return flags for position corresponding to start-of-text, there is currently
212    ///    no position corresponding to end-of-text. This limitation can be alleviated by shaping more text
213    ///    than needed and looking for unsafe-to-concat flag within text clusters. The unsafe-to-break flag will
214    ///    always imply this flag. To use this flag, you must enable the buffer flag [`BufferFlags::PRODUCE_UNSAFE_TO_CONCAT`]
215    ///    during shaping, otherwise the buffer flag will not be reliably produced.
216    pub const UNSAFE_TO_CONCAT: Self = Self(0x0000_0002);
217
218    /// A U+0640 TATWEEL can safely be inserted before this cluster.
219    ///
220    /// In scripts that use elongation (Arabic, Mongolian, Syriac, etc.), this flag does not
221    /// determine the script-specific elongation places, but only when it is safe to do the elongation
222    /// without interrupting text shaping.
223    pub const SAFE_TO_INSERT_TATWEEL: Self = Self(0x0000_0004);
224
225    /// All the currently defined flags.
226    pub const ALL: Self = Self(Self::DEFINED_BITS);
227
228    pub(crate) const DEFINED_BITS: u32 = 0x0000_0007; // OR of all defined flags
229
230    /// Creates a `GlyphFlags` from the given bits, ignoring any bits that are not defined.
231    pub const fn from_bits_truncate(bits: u32) -> Self {
232        Self(bits & Self::DEFINED_BITS)
233    }
234
235    /// Returns the underlying flag bits.
236    #[inline(always)]
237    pub const fn to_bits(self) -> u32 {
238        self.0
239    }
240
241    /// Indicates that if input text is broken at the beginning of the cluster this glyph
242    /// is part of, then both sides need to be re-shaped, as the result might be different.
243    ///
244    /// See [Self::UNSAFE_TO_BREAK] for more details.
245    #[inline(always)]
246    pub const fn is_unsafe_to_break(self) -> bool {
247        self.intersects(Self::UNSAFE_TO_BREAK)
248    }
249
250    /// Indicates that if input text is changed on one side of the beginning of the cluster
251    /// this glyph is part of, then the shaping results for the other side might change.
252    ///
253    /// See [Self::UNSAFE_TO_CONCAT] for more details.
254    #[inline(always)]
255    pub const fn is_unsafe_to_concat(self) -> bool {
256        self.intersects(Self::UNSAFE_TO_CONCAT)
257    }
258
259    /// In scripts that use elongation (Arabic, Mongolian, Syriac, etc.), this flag signifies that it is
260    /// safe to insert a U+0640 TATWEEL character before this cluster for elongation.
261    ///
262    /// See [Self::SAFE_TO_INSERT_TATWEEL] for more details.
263    #[inline(always)]
264    pub const fn is_safe_to_insert_tatweel(self) -> bool {
265        self.intersects(Self::SAFE_TO_INSERT_TATWEEL)
266    }
267
268    /// Returns `true` if all of the flags in `other` are contained within `self`.
269    #[inline(always)]
270    pub const fn contains(self, other: Self) -> bool {
271        (self.0 & other.0) == other.0
272    }
273
274    /// Returns `true` if any of the flags in `other` are contained within `self`.
275    #[inline(always)]
276    pub const fn intersects(self, other: Self) -> bool {
277        (self.0 & other.0) != 0
278    }
279}
280
281impl core::ops::BitOr for GlyphFlags {
282    type Output = Self;
283
284    #[inline(always)]
285    fn bitor(self, rhs: Self) -> Self::Output {
286        Self(self.0 | rhs.0)
287    }
288}
289
290impl core::ops::BitOrAssign for GlyphFlags {
291    #[inline(always)]
292    fn bitor_assign(&mut self, rhs: Self) {
293        self.0 |= rhs.0;
294    }
295}
296
297impl core::ops::BitAnd for GlyphFlags {
298    type Output = Self;
299
300    #[inline(always)]
301    fn bitand(self, rhs: Self) -> Self::Output {
302        Self(self.0 & rhs.0)
303    }
304}
305
306impl core::ops::BitAndAssign for GlyphFlags {
307    #[inline(always)]
308    fn bitand_assign(&mut self, rhs: Self) {
309        self.0 &= rhs.0;
310    }
311}
312
313impl core::ops::Not for GlyphFlags {
314    type Output = Self;
315
316    #[inline(always)]
317    fn not(self) -> Self::Output {
318        Self(!self.0 & Self::DEFINED_BITS)
319    }
320}
321
322impl GlyphInfo {
323    /// Returns the flags for this glyph.
324    #[inline(always)]
325    pub const fn flags(&self) -> GlyphFlags {
326        GlyphFlags(self.mask)
327    }
328
329    /// Indicates that if input text is broken at the beginning of the cluster this glyph
330    /// is part of, then both sides need to be re-shaped, as the result might be different.
331    ///
332    /// On the flip side, it means that when this flag is not present,
333    /// then it's safe to break the glyph-run at the beginning of this cluster,
334    /// and the two sides represent the exact same result one would get if breaking input text
335    /// at the beginning of this cluster and shaping the two sides separately.
336    /// This can be used to optimize paragraph layout, by avoiding re-shaping of each line
337    /// after line-breaking, or limiting the reshaping to a small piece around
338    /// the breaking point only.
339    #[inline(always)]
340    pub fn unsafe_to_break(&self) -> bool {
341        self.flags().is_unsafe_to_break()
342    }
343
344    /// Indicates that if input text is changed on one side of the beginning of the cluster
345    /// this glyph is part of, then the shaping results for the other side might change.
346    /// Note that the absence of this flag will NOT by itself mean that it IS safe to concat
347    /// text. Only two pieces of text both of which clear of this flag can be concatenated
348    /// safely.
349    ///
350    /// This can be used to optimize paragraph layout, by avoiding re-shaping of each line after
351    /// line-breaking, by limiting the reshaping to a small piece around the breaking position
352    /// only, even if the breaking position carries the unsafe-to-break flag or when hyphenation
353    /// or other text transformation happens at line-break position, in the following way:
354    /// 1. Iterate back from the line-break position until the first cluster start position
355    ///    that is NOT unsafe-to-concat.
356    /// 2. Shape the segment from there till the end of line.
357    /// 3. Check whether the resulting glyph-run also is clear of the unsafe-to-concat at its
358    ///    start-of-text position; if it is, just splice it into place and the line is shaped;
359    ///    If not, move on to a position further back that is clear of unsafe-to-concat and retry
360    ///    from there, and repeat.
361    ///
362    /// At the start of next line a similar algorithm can be implemented. That is:
363    /// 1. Iterate forward from the line-break position until the first cluster start position that is
364    ///    NOT unsafe-to-concat.
365    /// 2. Shape the segment from beginning of the line to that position.
366    /// 3. Check whether the resulting glyph-run also is clear of the unsafe-to-concat at its end-of-text
367    ///    position; if it is, just splice it into place and the beginning is shaped; If not, move on to a
368    ///    position further forward that is clear of unsafe-to-concat and retry up to there, and repeat. A
369    ///    slight complication will arise in the implementation of the algorithm above, because while our
370    ///    buffer API has a way to return flags for position corresponding to start-of-text, there is currently
371    ///    no position corresponding to end-of-text. This limitation can be alleviated by shaping more text
372    ///    than needed and looking for unsafe-to-concat flag within text clusters. The unsafe-to-break flag will
373    ///    always imply this flag. To use this flag, you must enable the buffer flag [`BufferFlags::PRODUCE_UNSAFE_TO_CONCAT`]
374    ///    during shaping, otherwise the buffer flag will not be reliably produced.
375    #[inline(always)]
376    pub fn unsafe_to_concat(&self) -> bool {
377        self.flags().is_unsafe_to_concat()
378    }
379
380    /// In scripts that use elongation (Arabic, Mongolian, Syriac, etc.), this flag signifies that it is
381    /// safe to insert a U+0640 TATWEEL character before this cluster for elongation. This flag does not
382    /// determine the script-specific elongation places, but only when it is safe to do the elongation
383    /// without interrupting text shaping.
384    #[inline(always)]
385    pub fn safe_to_insert_tatweel(&self) -> bool {
386        self.flags().is_safe_to_insert_tatweel()
387    }
388
389    pub(crate) fn as_codepoint(&self) -> Codepoint {
390        self.glyph_id
391    }
392
393    #[inline]
394    pub(crate) fn as_glyph(&self) -> GlyphId {
395        GlyphId::new(self.glyph_id)
396    }
397
398    #[inline]
399    pub(crate) fn as_gid16(&self) -> Option<GlyphId16> {
400        let gid: u16 = self.glyph_id.try_into().ok()?;
401        Some(gid.into())
402    }
403
404    pub(crate) fn init_unicode_props(&mut self, scratch_flags: &mut ScratchFlags) {
405        let u = self.as_codepoint();
406        let gc = u.general_category();
407        let mut props = gc.0 as u16;
408
409        if u >= 0x80 {
410            if u.is_default_ignorable() {
411                props |= UnicodeProps::IGNORABLE.bits();
412                *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES;
413
414                match u {
415                    0x200C => props |= UnicodeProps::CF_ZWNJ.bits(),
416                    0x200D => props |= UnicodeProps::CF_ZWJ.bits(),
417
418                    // Mongolian Free Variation Selectors need to be remembered
419                    // because although we need to hide them like default-ignorables,
420                    // they need to non-ignorable during shaping.  This is similar to
421                    // what we do for joiners in Indic-like shapers, but since the
422                    // FVSes are GC=Mn, we have use a separate bit to remember them.
423                    // Fixes:
424                    // https://github.com/harfbuzz/harfbuzz/issues/234
425                    0x180B..=0x180D | 0x180F => props |= UnicodeProps::HIDDEN.bits(),
426
427                    // TAG characters need similar treatment. Fixes:
428                    // https://github.com/harfbuzz/harfbuzz/issues/463
429                    0xE0020..=0xE007F => props |= UnicodeProps::HIDDEN.bits(),
430
431                    // COMBINING GRAPHEME JOINER should not be skipped during GSUB either.
432                    // https://github.com/harfbuzz/harfbuzz/issues/554
433                    0x034F => {
434                        props |= UnicodeProps::HIDDEN.bits();
435                        *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_CGJ;
436                    }
437
438                    _ => {}
439                }
440            }
441
442            if gc.is_mark() {
443                *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS;
444                props |= UnicodeProps::CONTINUATION.bits();
445                props |= (u.modified_combining_class() as u16) << 8;
446            }
447        }
448
449        self.set_unicode_props(props);
450    }
451
452    #[inline]
453    pub(crate) fn unhide(&mut self) {
454        let mut n = self.unicode_props();
455        n &= !UnicodeProps::HIDDEN.bits();
456        self.set_unicode_props(n);
457    }
458}
459
460pub type ClusterLevelCode = u32;
461pub const HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES: u32 = 0;
462pub const HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS: u32 = 1;
463pub const HB_BUFFER_CLUSTER_LEVEL_CHARACTERS: u32 = 2;
464pub const HB_BUFFER_CLUSTER_LEVEL_GRAPHEMES: u32 = 3;
465pub const HB_BUFFER_CLUSTER_LEVEL_DEFAULT: u32 = HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES;
466
467/// The type of contents currently held by a [`Buffer`].
468///
469/// This matches HarfBuzz's `hb_buffer_content_type_t`.
470#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
471pub enum ContentType {
472    /// The buffer holds input characters, ready for shaping.
473    Unicode,
474    /// The buffer holds the glyphs produced by shaping.
475    Glyphs,
476}
477
478/// A callback used to trace shaping operations.
479///
480/// See [`Buffer::set_message_function`] for the callback's arguments and return value.
481#[cfg(feature = "tracing")]
482pub type MessageFunction =
483    dyn FnMut(&Buffer, &crate::ShaperFont<'_, '_>, &str) -> bool + Send + Sync;
484
485/// A buffer of text to be shaped, and of the glyphs that shaping produces.
486///
487/// This is the unified buffer type, matching HarfBuzz's `hb_buffer_t`. It
488/// carries a [`ContentType`] describing which of the two states it is
489/// currently in.
490pub struct Buffer {
491    // Information about how the text in the buffer should be treated.
492    pub(crate) flags: BufferFlags,
493    pub(crate) cluster_level: ClusterLevelCode,
494    pub(crate) invisible: Option<GlyphId>,
495    pub(crate) not_found_variation_selector: Option<u32>,
496
497    // Buffer contents.
498    pub(crate) content_type: Option<ContentType>,
499    pub(crate) direction: Direction,
500    pub(crate) script: Option<Script>,
501    pub(crate) language: Option<Language>,
502
503    /// Allocations successful.
504    pub(crate) successful: bool,
505    /// Whether we have an output buffer going on.
506    pub(crate) have_output: bool,
507    pub(crate) have_separate_output: bool,
508    /// Whether we have positions
509    pub(crate) have_positions: bool,
510
511    pub(crate) idx: usize,
512    pub(crate) len: usize,
513    pub(crate) out_len: usize,
514
515    pub(crate) info: Vec<GlyphInfo>,
516    pub(crate) pos: Vec<GlyphPosition>,
517
518    // Text before / after the main buffer contents.
519    // Always in Unicode, and ordered outward.
520    // Index 0 is for "pre-context", 1 for "post-context".
521    pub(crate) context: [[Codepoint; CONTEXT_LENGTH]; 2],
522    pub(crate) context_len: [usize; 2],
523
524    pub(crate) digest: SetDigest,
525    pub(crate) glyph_set: U32Set,
526
527    // Managed by enter / leave
528    pub(crate) allocated_var_bits: u8,
529    pub(crate) serial: u8,
530    pub(crate) scratch_flags: ScratchFlags,
531    /// Maximum allowed len.
532    pub(crate) max_len: usize,
533    /// Maximum allowed operations.
534    pub(crate) max_ops: i32,
535    #[cfg(feature = "tracing")]
536    message_function: Option<Box<MessageFunction>>,
537}
538
539impl Buffer {
540    /// Creates a new `Buffer`.
541    pub fn new() -> Self {
542        Buffer {
543            flags: BufferFlags::empty(),
544            cluster_level: HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
545            invisible: None,
546            scratch_flags: HB_BUFFER_SCRATCH_FLAG_DEFAULT,
547            not_found_variation_selector: None,
548            max_len: Self::MAX_LEN_DEFAULT,
549            max_ops: Self::MAX_OPS_DEFAULT,
550            content_type: None,
551            direction: Direction::Invalid,
552            script: None,
553            language: None,
554            successful: true,
555            have_output: false,
556            have_positions: false,
557            idx: 0,
558            len: 0,
559            out_len: 0,
560            info: Vec::new(),
561            pos: Vec::new(),
562            have_separate_output: false,
563            allocated_var_bits: 0,
564            serial: 0,
565            context: Default::default(),
566            context_len: [0, 0],
567            digest: SetDigest::new(),
568            glyph_set: U32Set::default(),
569            #[cfg(feature = "tracing")]
570            message_function: None,
571        }
572    }
573
574    /// Clears the contents of the buffer, retaining its allocation.
575    ///
576    /// This matches HarfBuzz's `hb_buffer_clear_contents`.
577    pub fn clear(&mut self) {
578        self.content_type = None;
579        self.direction = Direction::Invalid;
580        self.script = None;
581        self.language = None;
582
583        self.successful = true;
584        self.have_output = false;
585        self.have_positions = false;
586
587        self.idx = 0;
588        self.info.clear();
589        self.pos.clear();
590        self.len = 0;
591        self.out_len = 0;
592        self.have_separate_output = false;
593
594        self.context = Default::default();
595        self.context_len = [0, 0];
596
597        self.serial = 0;
598        self.scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
599        // How the buffer is configured -- its flags, cluster level, invisible
600        // glyph and variation-selector fallback -- outlives its contents, as
601        // it does in `hb_buffer_clear_contents`. `Buffer::reset` is what puts
602        // those back.
603    }
604
605    /// Resets the buffer to its default state, clearing its contents along
606    /// with the flags and cluster level.
607    pub fn reset(&mut self) {
608        self.clear();
609        self.flags = BufferFlags::empty();
610        self.cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
611        self.invisible = None;
612        self.not_found_variation_selector = None;
613    }
614
615    /// Returns the type of content currently held by the buffer, or `None`
616    /// if the buffer is empty or has been cleared.
617    #[inline]
618    pub fn content_type(&self) -> Option<ContentType> {
619        self.content_type
620    }
621
622    /// Sets the type of content held by the buffer.
623    ///
624    /// This only relabels the buffer; it never clears it. Shaping sets this to
625    /// [`ContentType::Glyphs`] on its own, so most callers never need it.
626    ///
627    /// Setting it explicitly reinterprets the items already in the buffer, which
628    /// is occasionally what you want: label a buffer of glyph ids as
629    /// [`ContentType::Glyphs`] to serialize a run that was shaped
630    /// elsewhere, or label a shaped buffer as [`ContentType::Unicode`] to
631    /// force [`shape`](crate::shape) to run over it again.
632    #[inline]
633    pub fn set_content_type(&mut self, content_type: Option<ContentType>) {
634        self.content_type = content_type;
635    }
636
637    /// Returns the number of items in the buffer.
638    ///
639    /// Before shaping this is the number of Unicode codepoints; after
640    /// shaping it is the number of glyphs.
641    #[inline]
642    pub fn len(&self) -> usize {
643        self.len
644    }
645
646    /// Checks that buffer contains no elements.
647    /// Returns `true` if the buffer contains no items.
648    pub fn is_empty(&self) -> bool {
649        self.len == 0
650    }
651
652    /// Sets the number of items in the buffer.
653    ///
654    /// Growing the buffer fills the new items with zeros. Returns `false` if
655    /// the allocation failed, in which case the buffer is left unchanged.
656    pub fn set_length(&mut self, len: usize) -> bool {
657        if !self.ensure(len) {
658            return false;
659        }
660        if len > self.len {
661            if let Some(infos) = self.info.get_mut(self.len..len) {
662                infos.fill(GlyphInfo::default());
663            }
664            if self.have_positions {
665                if let Some(positions) = self.pos.get_mut(self.len..len) {
666                    positions.fill(GlyphPosition::default());
667                }
668            }
669        }
670        self.len = len;
671        true
672    }
673
674    /// Ensures that the buffer can hold at least `size` items.
675    #[inline]
676    pub fn reserve(&mut self, size: usize) -> bool {
677        self.ensure(size)
678    }
679
680    /// Returns `true` if every allocation made on this buffer has succeeded.
681    ///
682    /// This goes false when filling the buffer runs out of memory, and when
683    /// shaping needs more room, more operations or more nesting than the
684    /// shaper allows.
685    #[inline]
686    pub fn allocation_successful(&self) -> bool {
687        self.successful
688    }
689
690    /// Appends a codepoint to the buffer with the given cluster value.
691    ///
692    /// This matches HarfBuzz's `hb_buffer_add`.
693    pub fn push(&mut self, codepoint: u32, cluster: u32) {
694        self.content_type = Some(ContentType::Unicode);
695        if !self.ensure(self.len + 1) {
696            return;
697        }
698        let Some(slot) = self.info.get_mut(self.len) else {
699            return;
700        };
701        *slot = GlyphInfo {
702            glyph_id: codepoint,
703            cluster,
704            ..GlyphInfo::default()
705        };
706        self.len += 1;
707    }
708
709    /// Appends a string to the buffer.
710    ///
711    /// Cluster values are set to the UTF-8 byte offset of each character.
712    pub fn push_str(&mut self, text: &str) {
713        if !self.ensure(self.len + text.chars().count()) {
714            return;
715        }
716        self.content_type = Some(ContentType::Unicode);
717
718        let Some(slots) = self.info.get_mut(self.len..) else {
719            return;
720        };
721        for (slot, (i, c)) in slots.iter_mut().zip(text.char_indices()) {
722            *slot = GlyphInfo {
723                glyph_id: c as u32,
724                cluster: i as u32,
725                ..GlyphInfo::default()
726            };
727            self.len += 1;
728        }
729    }
730
731    /// Appends codepoints with cluster values starting at zero.
732    ///
733    /// Cluster indices restart at zero on each call.
734    pub fn push_codepoints(&mut self, codepoints: &[u32]) {
735        if !self.ensure(self.len + codepoints.len()) {
736            return;
737        }
738        self.content_type = Some(ContentType::Unicode);
739        let Some(slots) = self.info.get_mut(self.len..) else {
740            return;
741        };
742        for (slot, (i, &c)) in slots.iter_mut().zip(codepoints.iter().enumerate()) {
743            *slot = GlyphInfo {
744                glyph_id: c,
745                cluster: i as u32,
746                ..GlyphInfo::default()
747            };
748            self.len += 1;
749        }
750    }
751
752    /// Appends glyph infos to the buffer.
753    pub fn push_glyph_infos(&mut self, infos: &[GlyphInfo]) -> bool {
754        let len = infos.len();
755        if !self.ensure(self.len + len) {
756            return false;
757        }
758        let Some(slots) = self.info.get_mut(self.len..self.len + len) else {
759            return false;
760        };
761        slots.copy_from_slice(infos);
762        self.len += len;
763        true
764    }
765
766    /// Sets the pre-context for this buffer.
767    pub fn set_pre_context(&mut self, text: &str) {
768        self.clear_context(0);
769        for (i, c) in text.chars().rev().enumerate().take(CONTEXT_LENGTH) {
770            self.context[0][i] = c as Codepoint;
771            self.context_len[0] += 1;
772        }
773    }
774
775    /// Sets the pre-context for this buffer from codepoints.
776    ///
777    /// The input is expected to be the Unicode codepoints in reverse order.
778    /// This matches HarfBuzz's internal storage of pre-context, and serves
779    /// as a low-overhead method to pass pre-context from HarfBuzz-HarfRust.
780    pub fn set_pre_context_codepoints(&mut self, codepoints: &[u32]) {
781        self.clear_context(0);
782        for (i, &c) in codepoints.iter().take(CONTEXT_LENGTH).enumerate() {
783            self.context[0][i] = c;
784            self.context_len[0] += 1;
785        }
786    }
787
788    /// Returns the pre-context, in the reverse order
789    /// [`Buffer::set_pre_context_codepoints`] takes it.
790    #[inline]
791    pub fn pre_context_codepoints(&self) -> &[u32] {
792        &self.context[0][..self.context_len[0]]
793    }
794
795    /// Sets the post-context for this buffer.
796    pub fn set_post_context(&mut self, text: &str) {
797        self.clear_context(1);
798        for (i, c) in text.chars().enumerate().take(CONTEXT_LENGTH) {
799            self.context[1][i] = c as Codepoint;
800            self.context_len[1] += 1;
801        }
802    }
803
804    /// Sets the post-context for this buffer from codepoints.
805    pub fn set_post_context_codepoints(&mut self, codepoints: &[u32]) {
806        self.clear_context(1);
807        for (i, &c) in codepoints.iter().take(CONTEXT_LENGTH).enumerate() {
808            self.context[1][i] = c;
809            self.context_len[1] += 1;
810        }
811    }
812
813    /// Returns the post-context.
814    #[inline]
815    pub fn post_context_codepoints(&self) -> &[u32] {
816        &self.context[1][..self.context_len[1]]
817    }
818
819    /// Returns the buffer's text direction.
820    #[inline]
821    pub fn direction(&self) -> Direction {
822        self.direction
823    }
824
825    /// Sets the buffer's text direction.
826    #[inline]
827    pub fn set_direction(&mut self, direction: Direction) {
828        self.direction = direction;
829    }
830
831    /// Returns the buffer's ISO 15924 script, or `None` if it has none.
832    ///
833    /// A buffer with no script is not one whose script is `Zzzz`: that is a
834    /// script a caller can ask for, meaning known to be unknown.
835    #[inline]
836    pub fn script(&self) -> Option<Script> {
837        self.script
838    }
839
840    /// Sets the buffer's script from an ISO 15924 tag, or clears it.
841    #[inline]
842    pub fn set_script(&mut self, script: Option<Script>) {
843        self.script = script;
844    }
845
846    /// Returns the buffer's language, or `None` if it has none.
847    #[inline]
848    pub fn language(&self) -> Option<&Language> {
849        self.language.as_ref()
850    }
851
852    /// Sets the buffer's language, or clears it.
853    #[inline]
854    pub fn set_language(&mut self, language: Option<Language>) {
855        self.language = language;
856    }
857
858    /// Guesses the direction, script and language of the buffer contents.
859    ///
860    /// Only properties that are still unset are filled in.
861    pub fn guess_segment_properties(&mut self) {
862        if self.script.is_none() {
863            for info in &self.info {
864                match info.as_codepoint().script() {
865                    Script::COMMON | Script::INHERITED | Script::UNKNOWN => {}
866                    s => {
867                        self.script = Some(s);
868                        break;
869                    }
870                }
871            }
872        }
873
874        if self.direction == Direction::Invalid {
875            if let Some(script) = self.script {
876                self.direction = Direction::from_script(script).unwrap_or_default();
877            }
878
879            if self.direction == Direction::Invalid {
880                self.direction = Direction::LeftToRight;
881            }
882        }
883
884        // TODO: language must be set
885    }
886
887    /// Returns the buffer's flags.
888    #[inline]
889    pub fn flags(&self) -> BufferFlags {
890        self.flags
891    }
892
893    /// Sets the buffer's flags.
894    #[inline]
895    pub fn set_flags(&mut self, flags: BufferFlags) {
896        self.flags = flags;
897    }
898
899    /// Returns the buffer's cluster level.
900    #[inline]
901    pub fn cluster_level(&self) -> ClusterLevel {
902        ClusterLevel::new(self.cluster_level)
903    }
904
905    /// Sets the buffer's cluster level.
906    #[inline]
907    pub fn set_cluster_level(&mut self, cluster_level: ClusterLevel) {
908        self.cluster_level = match cluster_level {
909            ClusterLevel::MonotoneGraphemes => HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES,
910            ClusterLevel::MonotoneCharacters => HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS,
911            ClusterLevel::Characters => HB_BUFFER_CLUSTER_LEVEL_CHARACTERS,
912            ClusterLevel::Graphemes => HB_BUFFER_CLUSTER_LEVEL_GRAPHEMES,
913        };
914    }
915
916    /// Returns the glyph used to replace invisible characters, if set.
917    #[inline]
918    pub fn invisible_glyph(&self) -> Option<GlyphId> {
919        self.invisible
920    }
921
922    /// Sets the glyph used to replace invisible characters.
923    #[inline]
924    pub fn set_invisible_glyph(&mut self, glyph: Option<GlyphId>) {
925        self.invisible = glyph;
926    }
927
928    /// Returns the glyph used to replace not-found variation selectors, if set.
929    #[inline]
930    pub fn not_found_variation_selector_glyph(&self) -> Option<u32> {
931        self.not_found_variation_selector
932    }
933
934    /// Sets the glyph used to replace not-found variation-selector characters.
935    ///
936    /// A variation selector is not present if there is no glyph in the font for
937    /// the combination with the previous character. If None then the
938    /// selector is treated as a default ignorable character. If Some(glyph)
939    /// then the selector is preserved but replaced with glyph with zero
940    /// advance to allow callers to know about missing glyph combinations.
941    /// E.g., fall back to another font.
942    ///
943    /// glyph is taken as is and does not have to be present in the font;
944    /// any u32 value, even above u16::MAX, is accepted.
945    #[inline]
946    pub fn set_not_found_variation_selector_glyph(&mut self, glyph: Option<u32>) {
947        self.not_found_variation_selector = glyph;
948    }
949
950    /// Returns the buffer contents.
951    ///
952    /// Before shaping, [`GlyphInfo::glyph_id`] holds the input Unicode
953    /// codepoint rather than a glyph id.
954    #[inline]
955    pub fn glyph_infos(&self) -> &[GlyphInfo] {
956        debug_assert!(self.len <= self.info.len());
957        self.info.get(..self.len).unwrap_or_default()
958    }
959
960    /// Returns the buffer contents mutably.
961    #[inline]
962    pub fn glyph_infos_mut(&mut self) -> &mut [GlyphInfo] {
963        debug_assert!(self.len <= self.info.len());
964        self.info.get_mut(..self.len).unwrap_or_default()
965    }
966
967    /// Returns the glyph positions.
968    ///
969    /// Returns an empty slice if the buffer has not been shaped and has no
970    /// positions allocated. Use
971    /// [`glyph_positions_mut`](Self::glyph_positions_mut) to allocate them on
972    /// demand.
973    #[inline]
974    pub fn glyph_positions(&self) -> &[GlyphPosition] {
975        if !self.have_positions {
976            return &[];
977        }
978        self.pos.get(..self.len).unwrap_or_default()
979    }
980
981    /// Returns the glyph positions mutably, allocating and zeroing them if
982    /// the buffer does not have positions yet.
983    #[inline]
984    pub fn glyph_positions_mut(&mut self) -> &mut [GlyphPosition] {
985        if !self.have_positions {
986            self.clear_positions();
987        }
988        self.pos.get_mut(..self.len).unwrap_or_default()
989    }
990
991    /// Reverses the buffer contents.
992    #[inline]
993    pub fn reverse(&mut self) {
994        if self.is_empty() {
995            return;
996        }
997
998        self.reverse_range(0, self.len);
999    }
1000
1001    /// Reverses the buffer contents between `start` and `end`.
1002    pub fn reverse_range(&mut self, start: usize, end: usize) {
1003        if end - start < 2 {
1004            return;
1005        }
1006
1007        if let Some(infos) = self.info.get_mut(start..end) {
1008            infos.reverse();
1009        }
1010        if self.have_positions {
1011            if let Some(positions) = self.pos.get_mut(start..end) {
1012                positions.reverse();
1013            }
1014        }
1015    }
1016
1017    /// Reverses the buffer contents, keeping the items within each cluster in
1018    /// their original order.
1019    #[inline]
1020    pub fn reverse_clusters(&mut self) {
1021        self.reverse_groups(|a, b| a.cluster == b.cluster, false);
1022    }
1023
1024    /// Resets the cluster value of each item to its index.
1025    #[inline]
1026    pub fn reset_clusters(&mut self) {
1027        for (i, info) in self.info.iter_mut().enumerate() {
1028            info.cluster = i as u32;
1029        }
1030    }
1031
1032    /// Serializes the buffer contents into a string.
1033    pub fn serialize(
1034        &self,
1035        font: Option<&crate::ShaperFont<'_, '_>>,
1036        flags: SerializeFlags,
1037    ) -> String {
1038        self.serialize_impl(font, flags).unwrap_or_default()
1039    }
1040}
1041
1042impl Buffer {
1043    pub(crate) const MAX_LEN_FACTOR: usize = 256;
1044    pub(crate) const MAX_LEN_MIN: usize = 65536;
1045    // Shaping more than a billion chars? Let us know!
1046    pub(crate) const MAX_LEN_DEFAULT: usize = 0x3FFF_FFFF;
1047
1048    pub(crate) const MAX_OPS_FACTOR: i32 = 4096;
1049    pub(crate) const MAX_OPS_MIN: i32 = 65536;
1050    // Shaping more than a billion operations? Let us know!
1051    pub(crate) const MAX_OPS_DEFAULT: i32 = 0x1FFF_FFFF;
1052
1053    #[inline]
1054    pub(crate) fn allocate_var(&mut self, shape: buffer_var_shape) {
1055        let bits = shape.bits();
1056        debug_assert_eq!(
1057            self.allocated_var_bits & bits,
1058            0,
1059            "Variable already allocated"
1060        );
1061        self.allocated_var_bits |= bits;
1062    }
1063
1064    #[inline]
1065    pub(crate) fn try_allocate_var(&mut self, shape: buffer_var_shape) -> bool {
1066        let bits = shape.bits();
1067        if self.allocated_var_bits & bits != 0 {
1068            return false;
1069        }
1070        self.allocated_var_bits |= bits;
1071        true
1072    }
1073
1074    #[inline]
1075    pub(crate) fn deallocate_var(&mut self, shape: buffer_var_shape) {
1076        let bits = shape.bits();
1077        debug_assert_eq!(
1078            self.allocated_var_bits & bits,
1079            bits,
1080            "Deallocating unallocated var"
1081        );
1082        self.allocated_var_bits &= !bits;
1083    }
1084
1085    #[inline]
1086    pub(crate) fn assert_var(&self, shape: buffer_var_shape) {
1087        let bits = shape.bits();
1088        debug_assert_eq!(
1089            self.allocated_var_bits & bits,
1090            bits,
1091            "Variable not allocated"
1092        );
1093    }
1094
1095    #[inline]
1096    pub(crate) fn info_slice_mut(&mut self) -> &mut [GlyphInfo] {
1097        debug_assert!(self.len <= self.info.len());
1098        self.info.get_mut(..self.len).unwrap_or_default()
1099    }
1100
1101    /// Returns the glyph infos and positions together, for walking both at
1102    /// once.
1103    #[inline]
1104    pub(crate) fn infos_and_positions_mut(&mut self) -> (&mut [GlyphInfo], &mut [GlyphPosition]) {
1105        let len = self.len;
1106        debug_assert!(len <= self.info.len() && len <= self.pos.len());
1107        (
1108            self.info.get_mut(..len).unwrap_or_default(),
1109            self.pos.get_mut(..len).unwrap_or_default(),
1110        )
1111    }
1112
1113    #[inline]
1114    pub(crate) fn out_info(&self) -> &[GlyphInfo] {
1115        if self.have_separate_output {
1116            bytemuck::cast_slice(self.pos.as_slice())
1117        } else {
1118            &self.info
1119        }
1120    }
1121
1122    #[inline]
1123    pub(crate) fn out_info_mut(&mut self) -> &mut [GlyphInfo] {
1124        if self.have_separate_output {
1125            bytemuck::cast_slice_mut(self.pos.as_mut_slice())
1126        } else {
1127            &mut self.info
1128        }
1129    }
1130
1131    /// Marks the buffer unsuccessful after an index fell outside it.
1132    ///
1133    /// Buffer operations skip the out-of-range access but keep their
1134    /// bookkeeping, so shaping loops still advance and then stop on the
1135    /// failure as they do when an allocation fails.
1136    #[cold]
1137    #[inline(never)]
1138    pub(crate) fn index_failed(&mut self) {
1139        debug_assert!(false, "buffer index out of bounds");
1140        self.successful = false;
1141    }
1142
1143    #[inline(always)]
1144    fn set_out_info(&mut self, i: usize, info: GlyphInfo) {
1145        match self.out_info_mut().get_mut(i) {
1146            Some(slot) => *slot = info,
1147            None => self.index_failed(),
1148        }
1149    }
1150
1151    /// Below this many elements, copy inline instead of calling memmove:
1152    /// AAT state machines move a couple of glyphs at a time, and the call
1153    /// plus memmove's size dispatch costs more cycles than the copy.
1154    const SMALL_COPY: usize = 16;
1155
1156    /// Block-copies `info[src..src + count]` to `out_info[dst..dst + count]`.
1157    /// The caller must have made room in the out buffer already. In the
1158    /// shared-array case the destination never starts past the source
1159    /// (`dst <= src`), which makes the ascending inline copy overlap-safe.
1160    // Kept out of line so the callers' common no-copy path stays small enough
1161    // to inline.
1162    #[inline(never)]
1163    fn copy_infos_to_out(&mut self, src: usize, dst: usize, count: usize) {
1164        if self.try_copy_infos_to_out(src, dst, count).is_none() {
1165            self.index_failed();
1166        }
1167    }
1168
1169    #[inline]
1170    fn try_copy_infos_to_out(&mut self, src: usize, dst: usize, count: usize) -> Option<()> {
1171        if self.have_separate_output {
1172            let out: &mut [GlyphInfo] = bytemuck::cast_slice_mut(self.pos.as_mut_slice());
1173            let to = out.get_mut(dst..dst + count)?;
1174            let from = self.info.get(src..src + count)?;
1175            if count < Self::SMALL_COPY {
1176                // Deliberately not copy_from_slice: avoiding the memmove
1177                // call for small counts is this fast path's entire point.
1178                for (to, from) in to.iter_mut().zip(from) {
1179                    *to = *from;
1180                }
1181            } else {
1182                to.copy_from_slice(from);
1183            }
1184        } else {
1185            debug_assert!(dst <= src);
1186            // Destination and source share the array; `dst <= src` makes the
1187            // ascending copy overlap-safe.
1188            let window = self.info.get_mut(dst..src.checked_add(count)?)?;
1189            let offset = src - dst;
1190            if count < Self::SMALL_COPY {
1191                for i in 0..count {
1192                    *window.get_mut(i)? = *window.get(offset + i)?;
1193                }
1194            } else {
1195                window.copy_within(offset.., 0);
1196            }
1197        }
1198        Some(())
1199    }
1200
1201    /// Block-copies `out_info[src..src + count]` to `info[dst..dst + count]`.
1202    /// In the shared-array case the destination never starts before the
1203    /// source (`dst >= src`), so the inline copy must run descending to keep
1204    /// memmove semantics on overlap.
1205    #[inline]
1206    fn copy_out_to_infos(&mut self, src: usize, dst: usize, count: usize) {
1207        if self.try_copy_out_to_infos(src, dst, count).is_none() {
1208            self.index_failed();
1209        }
1210    }
1211
1212    #[inline]
1213    fn try_copy_out_to_infos(&mut self, src: usize, dst: usize, count: usize) -> Option<()> {
1214        if self.have_separate_output {
1215            let out: &[GlyphInfo] = bytemuck::cast_slice(self.pos.as_slice());
1216            let from = out.get(src..src + count)?;
1217            let to = self.info.get_mut(dst..dst + count)?;
1218            if count < Self::SMALL_COPY {
1219                // Deliberately not copy_from_slice; see copy_infos_to_out.
1220                for (to, from) in to.iter_mut().zip(from) {
1221                    *to = *from;
1222                }
1223            } else {
1224                to.copy_from_slice(from);
1225            }
1226        } else {
1227            debug_assert!(dst >= src);
1228            // Destination and source share the array; `dst >= src` needs the
1229            // descending copy to stay overlap-safe.
1230            let window = self.info.get_mut(src..dst.checked_add(count)?)?;
1231            let offset = dst - src;
1232            if count < Self::SMALL_COPY {
1233                for i in (0..count).rev() {
1234                    *window.get_mut(offset + i)? = *window.get(i)?;
1235                }
1236            } else {
1237                window.copy_within(..count, offset);
1238            }
1239        }
1240        Some(())
1241    }
1242
1243    #[inline]
1244    pub(crate) fn cur(&self, i: usize) -> Option<&GlyphInfo> {
1245        self.info.get(self.idx + i).likely()
1246    }
1247
1248    #[inline]
1249    pub(crate) fn cur_mut(&mut self, i: usize) -> Option<&mut GlyphInfo> {
1250        let idx = self.idx + i;
1251        self.info.get_mut(idx).likely()
1252    }
1253
1254    #[inline]
1255    pub(crate) fn cur_pos_mut(&mut self) -> Option<&mut GlyphPosition> {
1256        let idx = self.idx;
1257        self.pos.get_mut(idx).likely()
1258    }
1259
1260    #[inline]
1261    pub(crate) fn prev(&self) -> Option<&GlyphInfo> {
1262        self.out_info().get(self.out_len.checked_sub(1)?).likely()
1263    }
1264
1265    #[inline]
1266    pub(crate) fn prev_mut(&mut self) -> Option<&mut GlyphInfo> {
1267        let idx = self.out_len.checked_sub(1)?;
1268        self.out_info_mut().get_mut(idx).likely()
1269    }
1270
1271    pub(crate) fn update_digest(&mut self) {
1272        self.digest = SetDigest::new();
1273        let len = self.len;
1274        self.digest.add_array(
1275            self.info
1276                .get(..len)
1277                .unwrap_or_default()
1278                .iter()
1279                .map(|i| i.glyph_id),
1280        );
1281    }
1282    pub(crate) fn update_glyph_set(&mut self) {
1283        self.glyph_set.clear();
1284        self.glyph_set
1285            .extend_unsorted(self.info.iter().map(|i| i.glyph_id));
1286    }
1287
1288    #[inline]
1289    pub(crate) fn backtrack_len(&self) -> usize {
1290        if self.have_output {
1291            self.out_len
1292        } else {
1293            self.idx
1294        }
1295    }
1296
1297    #[inline]
1298    pub(crate) fn lookahead_len(&self) -> usize {
1299        self.len - self.idx
1300    }
1301
1302    #[inline]
1303    fn next_serial(&mut self) -> u8 {
1304        // A `serial` overflow/wrap-around here is perfectly fine.
1305        self.serial = self.serial.wrapping_add(1);
1306
1307        if self.serial == 0 {
1308            self.serial += 1;
1309        }
1310
1311        self.serial
1312    }
1313
1314    pub(crate) fn reverse_groups<F>(&mut self, group: F, merge_clusters: bool)
1315    where
1316        F: Fn(&GlyphInfo, &GlyphInfo) -> bool,
1317    {
1318        if self.is_empty() {
1319            return;
1320        }
1321
1322        let mut start = 0;
1323        let mut i = 1;
1324
1325        while i < self.len {
1326            let (Some(a), Some(b)) = (self.info.get(i - 1), self.info.get(i)) else {
1327                self.index_failed();
1328                break;
1329            };
1330            if !group(a, b) {
1331                if merge_clusters {
1332                    self.merge_clusters(start, i);
1333                }
1334
1335                self.reverse_range(start, i);
1336                start = i;
1337            }
1338
1339            i += 1;
1340        }
1341
1342        if merge_clusters {
1343            self.merge_clusters(start, i);
1344        }
1345
1346        self.reverse_range(start, i);
1347
1348        self.reverse();
1349    }
1350
1351    pub(crate) fn group_end<F>(&self, mut start: usize, group: F) -> usize
1352    where
1353        F: Fn(&GlyphInfo, &GlyphInfo) -> bool,
1354    {
1355        start += 1;
1356
1357        while start < self.len {
1358            let (Some(a), Some(b)) = (self.info.get(start - 1), self.info.get(start)) else {
1359                break;
1360            };
1361            if !group(a, b) {
1362                break;
1363            }
1364            start += 1;
1365        }
1366
1367        start
1368    }
1369
1370    pub(crate) fn sync(&mut self) -> bool {
1371        debug_assert!(self.have_output);
1372        debug_assert!(self.idx <= self.len);
1373
1374        if !self.successful {
1375            self.have_output = false;
1376            self.out_len = 0;
1377            self.idx = 0;
1378            return false;
1379        }
1380
1381        self.next_glyphs(self.len - self.idx);
1382
1383        if self.have_separate_output {
1384            // Swap info and pos buffers.
1385            let info: Vec<GlyphPosition> = bytemuck::cast_vec(core::mem::take(&mut self.info));
1386            let pos: Vec<GlyphInfo> = bytemuck::cast_vec(core::mem::take(&mut self.pos));
1387            self.pos = info;
1388            self.info = pos;
1389            self.have_separate_output = false;
1390        }
1391
1392        self.len = self.out_len;
1393
1394        self.have_output = false;
1395        self.out_len = 0;
1396        self.idx = 0;
1397        true
1398    }
1399
1400    pub(crate) fn clear_output(&mut self) {
1401        self.have_output = true;
1402        self.have_positions = false;
1403
1404        self.idx = 0;
1405        self.out_len = 0;
1406        self.have_separate_output = false;
1407    }
1408
1409    pub(crate) fn clear_positions(&mut self) {
1410        self.have_output = false;
1411        self.have_positions = true;
1412
1413        self.out_len = 0;
1414        self.have_separate_output = false;
1415
1416        for pos in &mut self.pos {
1417            *pos = GlyphPosition::default();
1418        }
1419    }
1420
1421    pub(crate) fn replace_glyphs(&mut self, num_in: usize, num_out: usize, glyph_data: &[u32]) {
1422        if !self.make_room_for(num_in, num_out) {
1423            return;
1424        }
1425
1426        debug_assert!(self.idx + num_in <= self.len);
1427
1428        self.merge_clusters(self.idx, self.idx + num_in);
1429
1430        let orig_info = self.info.get(self.idx).copied();
1431        let out_len = self.out_len;
1432        let out = self.out_info_mut().get_mut(out_len..out_len + num_out);
1433        match (orig_info, out, glyph_data.get(..num_out)) {
1434            (Some(orig_info), Some(out), Some(glyph_data)) => {
1435                for (out_info, &glyph_id) in out.iter_mut().zip(glyph_data) {
1436                    *out_info = orig_info;
1437                    out_info.glyph_id = glyph_id;
1438                }
1439            }
1440            _ => self.index_failed(),
1441        }
1442
1443        self.idx += num_in;
1444        self.out_len += num_out;
1445    }
1446
1447    pub(crate) fn replace_glyph(&mut self, glyph_index: u32) {
1448        if self.have_separate_output || self.out_len != self.idx {
1449            if !self.make_room_for(1, 1) {
1450                return;
1451            }
1452
1453            self.copy_cur_to_out(self.out_len);
1454        }
1455
1456        self.set_out_glyph(self.out_len, glyph_index);
1457
1458        self.idx += 1;
1459        self.out_len += 1;
1460    }
1461
1462    pub(crate) fn output_glyph(&mut self, glyph_index: u32) {
1463        if !self.make_room_for(0, 1) {
1464            return;
1465        }
1466
1467        if self.idx == self.len && self.out_len == 0 {
1468            return;
1469        }
1470
1471        let out_len = self.out_len;
1472        if self.idx < self.len {
1473            self.copy_cur_to_out(out_len);
1474        } else {
1475            match self.out_info().get(out_len - 1).copied() {
1476                Some(info) => self.set_out_info(out_len, info),
1477                None => self.index_failed(),
1478            }
1479        }
1480
1481        self.set_out_glyph(out_len, glyph_index);
1482
1483        self.out_len += 1;
1484    }
1485
1486    pub(crate) fn output_info(&mut self, glyph_info: GlyphInfo) {
1487        if !self.make_room_for(0, 1) {
1488            return;
1489        }
1490
1491        self.set_out_info(self.out_len, glyph_info);
1492        self.out_len += 1;
1493    }
1494
1495    /// Copies glyph at idx to output but doesn't advance idx.
1496    pub(crate) fn copy_glyph(&mut self) {
1497        if !self.make_room_for(0, 1) {
1498            return;
1499        }
1500
1501        self.copy_cur_to_out(self.out_len);
1502        self.out_len += 1;
1503    }
1504
1505    /// Copies the current glyph to `out_info[out]`.
1506    #[inline(always)]
1507    fn copy_cur_to_out(&mut self, out: usize) {
1508        match self.info.get(self.idx).copied() {
1509            Some(info) => self.set_out_info(out, info),
1510            None => self.index_failed(),
1511        }
1512    }
1513
1514    #[inline(always)]
1515    fn set_out_glyph(&mut self, out: usize, glyph_index: u32) {
1516        match self.out_info_mut().get_mut(out) {
1517            Some(info) => info.glyph_id = glyph_index,
1518            None => self.index_failed(),
1519        }
1520    }
1521
1522    /// Copies glyph at idx to output and advance idx.
1523    ///
1524    /// If there's no output, just advance idx.
1525    #[inline(always)]
1526    pub(crate) fn next_glyph(&mut self) {
1527        if self.have_output {
1528            if self.have_separate_output || self.out_len != self.idx {
1529                if !self.ensure(self.out_len + 1) {
1530                    return;
1531                }
1532
1533                self.copy_cur_to_out(self.out_len);
1534            }
1535
1536            self.out_len += 1;
1537        }
1538
1539        self.idx += 1;
1540    }
1541
1542    /// Copies n glyphs at idx to output and advance idx.
1543    ///
1544    /// If there's no output, just advance idx.
1545    #[inline]
1546    pub(crate) fn next_glyphs(&mut self, n: usize) {
1547        if self.have_output {
1548            if self.have_separate_output || self.out_len != self.idx {
1549                if !self.ensure(self.out_len + n) {
1550                    return;
1551                }
1552
1553                self.copy_infos_to_out(self.idx, self.out_len, n);
1554            }
1555
1556            self.out_len += n;
1557        }
1558
1559        self.idx += n;
1560    }
1561
1562    /// Advance idx without copying to output.
1563    pub(crate) fn skip_glyph(&mut self) {
1564        self.idx += 1;
1565    }
1566
1567    pub(crate) fn set_masks(
1568        &mut self,
1569        mut value: Mask,
1570        mask: Mask,
1571        cluster_start: u32,
1572        cluster_end: u32,
1573    ) {
1574        if mask == 0 {
1575            return;
1576        }
1577
1578        let not_mask = !mask;
1579        value &= mask;
1580
1581        self.max_ops -= self.len as i32;
1582        if self.max_ops < 0 {
1583            self.successful = false;
1584        }
1585
1586        if cluster_start == 0 && cluster_end == u32::MAX {
1587            for info in self.info_slice_mut() {
1588                info.mask = (info.mask & not_mask) | value;
1589            }
1590
1591            return;
1592        }
1593
1594        for info in self.info_slice_mut() {
1595            if cluster_start <= info.cluster && info.cluster < cluster_end {
1596                info.mask = (info.mask & not_mask) | value;
1597            }
1598        }
1599    }
1600
1601    #[inline(always)]
1602    pub(crate) fn merge_clusters(&mut self, start: usize, end: usize) {
1603        if end - start < 2 {
1604            return;
1605        }
1606
1607        if !ClusterLevel::new(self.cluster_level).is_monotone() {
1608            self.unsafe_to_break(Some(start), Some(end));
1609            return;
1610        }
1611
1612        self.merge_clusters_impl(start, end);
1613    }
1614
1615    fn merge_clusters_impl(&mut self, start: usize, end: usize) {
1616        self.max_ops -= (end - start) as i32;
1617        if self.max_ops < 0 {
1618            self.successful = false;
1619        }
1620
1621        if self.try_merge_clusters(start, end).is_none() {
1622            self.index_failed();
1623        }
1624    }
1625
1626    fn try_merge_clusters(&mut self, mut start: usize, mut end: usize) -> Option<()> {
1627        let cluster_at = |infos: &[GlyphInfo], i: usize| infos.get(i).map(|info| info.cluster);
1628        let cluster = self
1629            .info
1630            .get(start..end)?
1631            .iter()
1632            .map(|info| info.cluster)
1633            .min()?;
1634
1635        // Extend end
1636        if cluster != cluster_at(&self.info, end - 1)? {
1637            while end < self.len && cluster_at(&self.info, end - 1)? == cluster_at(&self.info, end)?
1638            {
1639                end += 1;
1640            }
1641        }
1642
1643        // Extend start
1644        if cluster != cluster_at(&self.info, start)? {
1645            while self.idx < start
1646                && cluster_at(&self.info, start - 1)? == cluster_at(&self.info, start)?
1647            {
1648                start -= 1;
1649            }
1650        }
1651
1652        // If we hit the start of buffer, continue in out-buffer.
1653        let start_cluster = cluster_at(&self.info, start)?;
1654        if self.idx == start && start_cluster != cluster {
1655            let mut i = self.out_len;
1656            while i != 0 {
1657                let info = self.out_info_mut().get_mut(i - 1)?;
1658                if info.cluster != start_cluster {
1659                    break;
1660                }
1661                Self::set_cluster(info, cluster, 0);
1662                i -= 1;
1663            }
1664        }
1665
1666        for info in self.info.get_mut(start..end)? {
1667            Self::set_cluster(info, cluster, 0);
1668        }
1669        Some(())
1670    }
1671
1672    pub(crate) fn merge_grapheme_clusters(&mut self, start: usize, end: usize) {
1673        if end - start < 2 {
1674            return;
1675        }
1676
1677        if !ClusterLevel::new(self.cluster_level).is_graphemes() {
1678            self.unsafe_to_break(Some(start), Some(end));
1679            return;
1680        }
1681
1682        self.merge_clusters_impl(start, end);
1683    }
1684
1685    pub(crate) fn merge_out_clusters(&mut self, start: usize, end: usize) {
1686        if end - start < 2 {
1687            return;
1688        }
1689
1690        if !ClusterLevel::new(self.cluster_level).is_monotone() {
1691            return;
1692        }
1693
1694        self.merge_out_clusters_impl(start, end);
1695    }
1696
1697    pub(crate) fn merge_out_grapheme_clusters(&mut self, start: usize, end: usize) {
1698        if end - start < 2 {
1699            return;
1700        }
1701
1702        if !ClusterLevel::new(self.cluster_level).is_graphemes() {
1703            return;
1704        }
1705
1706        self.merge_out_clusters_impl(start, end);
1707    }
1708
1709    fn merge_out_clusters_impl(&mut self, start: usize, end: usize) {
1710        self.max_ops -= (end - start) as i32;
1711        if self.max_ops < 0 {
1712            self.successful = false;
1713        }
1714
1715        if self.try_merge_out_clusters(start, end).is_none() {
1716            self.index_failed();
1717        }
1718    }
1719
1720    fn try_merge_out_clusters(&mut self, mut start: usize, mut end: usize) -> Option<()> {
1721        let cluster_at = |infos: &[GlyphInfo], i: usize| infos.get(i).map(|info| info.cluster);
1722        let cluster = self
1723            .out_info()
1724            .get(start..end)?
1725            .iter()
1726            .map(|info| info.cluster)
1727            .min()?;
1728
1729        // Extend start
1730        while start != 0
1731            && cluster_at(self.out_info(), start - 1)? == cluster_at(self.out_info(), start)?
1732        {
1733            start -= 1;
1734        }
1735
1736        // Extend end
1737        while end < self.out_len
1738            && cluster_at(self.out_info(), end - 1)? == cluster_at(self.out_info(), end)?
1739        {
1740            end += 1;
1741        }
1742
1743        // If we hit the start of buffer, continue in out-buffer.
1744        if end == self.out_len {
1745            let end_cluster = cluster_at(self.out_info(), end - 1)?;
1746            let mut i = self.idx;
1747            while i < self.len {
1748                let info = self.info.get_mut(i)?;
1749                if info.cluster != end_cluster {
1750                    break;
1751                }
1752                Self::set_cluster(info, cluster, 0);
1753                i += 1;
1754            }
1755        }
1756
1757        for info in self.out_info_mut().get_mut(start..end)? {
1758            Self::set_cluster(info, cluster, 0);
1759        }
1760        Some(())
1761    }
1762
1763    /// Merge clusters for deleting current glyph, and skip it.
1764    pub(crate) fn delete_glyph(&mut self) {
1765        if self.merge_deleted_cluster().is_none() {
1766            self.index_failed();
1767        }
1768        self.skip_glyph();
1769    }
1770
1771    fn merge_deleted_cluster(&mut self) -> Option<()> {
1772        let GlyphInfo { cluster, mask, .. } = *self.info.get(self.idx)?;
1773        let next_cluster = self.info.get(self.idx + 1).map(|info| info.cluster);
1774        let prev_cluster = match self.out_len {
1775            0 => None,
1776            out_len => Some(self.out_info().get(out_len - 1)?.cluster),
1777        };
1778
1779        if (self.idx + 1 < self.len && next_cluster == Some(cluster))
1780            || prev_cluster == Some(cluster)
1781        {
1782            // Cluster survives; do nothing.
1783            return Some(());
1784        }
1785
1786        if let Some(old_cluster) = prev_cluster {
1787            // Merge cluster backward.
1788            if cluster < old_cluster {
1789                let mut i = self.out_len;
1790                while i != 0 {
1791                    let info = self.out_info_mut().get_mut(i - 1)?;
1792                    if info.cluster != old_cluster {
1793                        break;
1794                    }
1795                    Self::set_cluster(info, cluster, mask);
1796                    i -= 1;
1797                }
1798            }
1799            return Some(());
1800        }
1801
1802        if self.idx + 1 < self.len {
1803            // Merge cluster forward.
1804            self.merge_clusters(self.idx, self.idx + 2);
1805        }
1806        Some(())
1807    }
1808
1809    pub(crate) fn delete_glyphs_inplace(&mut self, filter: impl Fn(&GlyphInfo) -> bool) {
1810        // Merge clusters and delete filtered glyphs.
1811        // NOTE! We can't use out-buffer as we have positioning data.
1812        let mut j = 0;
1813
1814        for i in 0..self.len {
1815            let Some(&info) = self.info.get(i) else {
1816                self.index_failed();
1817                break;
1818            };
1819            if filter(&info) {
1820                // Merge clusters.
1821                // Same logic as delete_glyph(), but for in-place removal
1822
1823                let GlyphInfo { cluster, mask, .. } = info;
1824                if self
1825                    .info
1826                    .get(i + 1)
1827                    .is_some_and(|next| i + 1 < self.len && next.cluster == cluster)
1828                {
1829                    // Cluster survives; do nothing.
1830                    continue;
1831                }
1832
1833                if j != 0 {
1834                    // Merge cluster backward.
1835                    let old_cluster = self.info.get(j - 1).map_or(cluster, |info| info.cluster);
1836                    if cluster < old_cluster {
1837                        for info in self.info.get_mut(..j).unwrap_or_default().iter_mut().rev() {
1838                            if info.cluster != old_cluster {
1839                                break;
1840                            }
1841                            Self::set_cluster(info, cluster, mask);
1842                        }
1843                    }
1844                    continue;
1845                }
1846
1847                if i + 1 < self.len {
1848                    // Merge cluster forward.
1849                    self.merge_clusters(i, i + 2);
1850                }
1851
1852                continue;
1853            }
1854
1855            if j != i {
1856                if let Some(slot) = self.info.get_mut(j) {
1857                    *slot = info;
1858                }
1859                if let Some(&pos) = self.pos.get(i) {
1860                    if let Some(slot) = self.pos.get_mut(j) {
1861                        *slot = pos;
1862                    }
1863                }
1864            }
1865
1866            j += 1;
1867        }
1868
1869        self.len = j;
1870    }
1871
1872    pub(crate) fn unsafe_to_break(&mut self, start: Option<usize>, end: Option<usize>) {
1873        self.set_glyph_flags(
1874            GlyphFlags::UNSAFE_TO_BREAK | GlyphFlags::UNSAFE_TO_CONCAT,
1875            start,
1876            end,
1877            Some(true),
1878            None,
1879        );
1880    }
1881
1882    pub(crate) fn safe_to_insert_tatweel(&mut self, start: Option<usize>, end: Option<usize>) {
1883        if !self
1884            .flags
1885            .contains(BufferFlags::PRODUCE_SAFE_TO_INSERT_TATWEEL)
1886        {
1887            self.unsafe_to_break(start, end);
1888            return;
1889        }
1890
1891        self.set_glyph_flags(
1892            GlyphFlags::SAFE_TO_INSERT_TATWEEL,
1893            start,
1894            end,
1895            Some(true),
1896            None,
1897        );
1898    }
1899
1900    fn _set_glyph_flags_impl(
1901        &mut self,
1902        mask: Mask,
1903        start: usize,
1904        end: usize,
1905        interior: bool,
1906        from_out_buffer: bool,
1907    ) {
1908        if !from_out_buffer || !self.have_output {
1909            if !interior {
1910                for info in self.info.get_mut(start..end).unwrap_or_default() {
1911                    info.mask |= mask;
1912                }
1913            } else {
1914                let cluster = self._infos_find_min_cluster(&self.info, start, end, None);
1915                self._infos_set_glyph_flags(false, start, end, cluster, mask);
1916            }
1917        } else {
1918            debug_assert!(start <= self.out_len);
1919            debug_assert!(self.idx <= end);
1920
1921            if !interior {
1922                let range_end = self.out_len;
1923                for info in self
1924                    .out_info_mut()
1925                    .get_mut(start..range_end)
1926                    .unwrap_or_default()
1927                {
1928                    info.mask |= mask;
1929                }
1930
1931                for info in self.info.get_mut(self.idx..end).unwrap_or_default() {
1932                    info.mask |= mask;
1933                }
1934            } else {
1935                let mut cluster = self._infos_find_min_cluster(&self.info, self.idx, end, None);
1936                cluster = self._infos_find_min_cluster(
1937                    self.out_info(),
1938                    start,
1939                    self.out_len,
1940                    Some(cluster),
1941                );
1942
1943                let out_len = self.out_len;
1944                self._infos_set_glyph_flags(true, start, out_len, cluster, mask);
1945                self._infos_set_glyph_flags(false, self.idx, end, cluster, mask);
1946            }
1947        }
1948    }
1949
1950    /// Adds glyph flags in mask to infos with clusters between start and end.
1951    /// The start index will be from out-buffer if from_out_buffer is true.
1952    /// If interior is true, then the cluster having the minimum value is skipped. */
1953    fn set_glyph_flags(
1954        &mut self,
1955        flags: GlyphFlags,
1956        start: Option<usize>,
1957        end: Option<usize>,
1958        interior: Option<bool>,
1959        from_out_buffer: Option<bool>,
1960    ) {
1961        // If the range is not specified, ie. whole buffer, allow it.
1962        // But if range *is* specified, reject if range is too large.
1963        if let (Some(start), Some(end)) = (start, end) {
1964            if end.wrapping_sub(start) > 255 {
1965                return;
1966            }
1967        }
1968
1969        let start = start.unwrap_or(0);
1970        let end = min(end.unwrap_or(self.len), self.len);
1971        let interior = interior.unwrap_or(false);
1972        let from_out_buffer = from_out_buffer.unwrap_or(false);
1973
1974        if interior && !from_out_buffer && end - start < 2 {
1975            return;
1976        }
1977
1978        self._set_glyph_flags_impl(flags.0, start, end, interior, from_out_buffer);
1979    }
1980
1981    pub(crate) fn unsafe_to_concat(&mut self, start: Option<usize>, end: Option<usize>) {
1982        if !self.flags.contains(BufferFlags::PRODUCE_UNSAFE_TO_CONCAT) {
1983            return;
1984        }
1985
1986        self.set_glyph_flags(GlyphFlags::UNSAFE_TO_CONCAT, start, end, Some(false), None);
1987    }
1988
1989    pub(crate) fn unsafe_to_break_from_outbuffer(
1990        &mut self,
1991        start: Option<usize>,
1992        end: Option<usize>,
1993    ) {
1994        self.set_glyph_flags(
1995            GlyphFlags::UNSAFE_TO_BREAK | GlyphFlags::UNSAFE_TO_CONCAT,
1996            start,
1997            end,
1998            Some(true),
1999            Some(true),
2000        );
2001    }
2002
2003    pub(crate) fn unsafe_to_concat_from_outbuffer(
2004        &mut self,
2005        start: Option<usize>,
2006        end: Option<usize>,
2007    ) {
2008        if !self.flags.contains(BufferFlags::PRODUCE_UNSAFE_TO_CONCAT) {
2009            return;
2010        }
2011
2012        self.set_glyph_flags(
2013            GlyphFlags::UNSAFE_TO_CONCAT,
2014            start,
2015            end,
2016            Some(false),
2017            Some(true),
2018        );
2019    }
2020
2021    pub(crate) fn move_to(&mut self, i: usize) -> bool {
2022        if !self.have_output {
2023            debug_assert!(i <= self.len);
2024            self.idx = i;
2025            return true;
2026        }
2027
2028        if !self.successful {
2029            return false;
2030        }
2031
2032        debug_assert!(i <= self.out_len + (self.len - self.idx));
2033
2034        if self.out_len < i {
2035            let count = i - self.out_len;
2036            if !self.make_room_for(count, count) {
2037                return false;
2038            }
2039
2040            self.max_ops -= count as i32;
2041            if self.max_ops < 0 {
2042                self.successful = false;
2043                return false;
2044            }
2045
2046            self.copy_infos_to_out(self.idx, self.out_len, count);
2047
2048            self.idx += count;
2049            self.out_len += count;
2050        } else if self.out_len > i {
2051            // Tricky part: rewinding...
2052            let count = self.out_len - i;
2053
2054            // This will blow in our face if memory allocation fails later
2055            // in this same lookup...
2056            //
2057            // We used to shift with extra 32 items.
2058            // But that would leave empty slots in the buffer in case of allocation
2059            // failures.  See comments in shift_forward().  This can cause O(N^2)
2060            // behavior more severely than adding 32 empty slots can...
2061            if self.idx < count && !self.shift_forward(count - self.idx) {
2062                return false;
2063            }
2064
2065            debug_assert!(self.idx >= count);
2066
2067            self.max_ops -= count as i32;
2068            if self.max_ops < 0 {
2069                self.successful = false;
2070                return false;
2071            }
2072
2073            self.idx -= count;
2074            self.out_len -= count;
2075
2076            self.copy_out_to_infos(self.out_len, self.idx, count);
2077        }
2078
2079        true
2080    }
2081
2082    #[must_use]
2083    #[inline(always)]
2084    pub(crate) fn ensure(&mut self, size: usize) -> bool {
2085        if size <= self.info.len() {
2086            true
2087        } else {
2088            self.enlarge(size)
2089        }
2090    }
2091
2092    #[must_use]
2093    fn enlarge(&mut self, size: usize) -> bool {
2094        if size > self.max_len {
2095            self.successful = false;
2096            return false;
2097        }
2098
2099        self.info.resize(size, GlyphInfo::default());
2100        self.pos.resize(size, GlyphPosition::default());
2101        true
2102    }
2103
2104    #[must_use]
2105    fn make_room_for(&mut self, num_in: usize, num_out: usize) -> bool {
2106        if !self.ensure(self.out_len + num_out) {
2107            return false;
2108        }
2109
2110        if !self.have_separate_output && self.out_len + num_out > self.idx + num_in {
2111            debug_assert!(self.have_output);
2112
2113            self.have_separate_output = true;
2114            self.copy_infos_to_out(0, 0, self.out_len);
2115        }
2116
2117        true
2118    }
2119
2120    fn shift_forward(&mut self, count: usize) -> bool {
2121        debug_assert!(self.have_output);
2122        if !self.ensure(self.len + count) {
2123            return false;
2124        }
2125
2126        self.max_ops -= (self.len - self.idx) as i32;
2127        if self.max_ops < 0 {
2128            self.successful = false;
2129            return false;
2130        }
2131
2132        // Destination starts past the source; the inline copy runs
2133        // descending to keep memmove semantics on overlap.
2134        let moved = self.len - self.idx;
2135        let Some(window) = self.info.get_mut(self.idx..self.len + count) else {
2136            self.index_failed();
2137            return false;
2138        };
2139        if moved < Self::SMALL_COPY {
2140            for i in (0..moved).rev() {
2141                if let Some(&info) = window.get(i) {
2142                    if let Some(slot) = window.get_mut(count + i) {
2143                        *slot = info;
2144                    }
2145                }
2146            }
2147        } else {
2148            window.copy_within(..moved, count);
2149        }
2150
2151        if let Some(gap) = window.get_mut(moved..count) {
2152            gap.fill(GlyphInfo::default());
2153        }
2154
2155        self.len += count;
2156        self.idx += count;
2157
2158        true
2159    }
2160
2161    fn clear_context(&mut self, side: usize) {
2162        self.context_len[side] = 0;
2163    }
2164
2165    pub(crate) fn sort(
2166        &mut self,
2167        start: usize,
2168        end: usize,
2169        cmp: impl Fn(&GlyphInfo, &GlyphInfo) -> bool,
2170    ) {
2171        debug_assert!(!self.have_positions);
2172
2173        for i in start + 1..end {
2174            let Some(&item) = self.info.get(i) else {
2175                self.index_failed();
2176                return;
2177            };
2178            let mut j = i;
2179            while j > start && self.info.get(j - 1).is_some_and(|prev| cmp(prev, &item)) {
2180                j -= 1;
2181            }
2182
2183            if i == j {
2184                continue;
2185            }
2186
2187            // Move item i to occupy place for item j, shift what's in between.
2188            self.merge_clusters(j, i + 1);
2189            if let Some(moved) = self.info.get_mut(j..=i) {
2190                moved.rotate_right(1);
2191            }
2192        }
2193    }
2194
2195    pub(crate) fn set_cluster(info: &mut GlyphInfo, cluster: u32, mask: Mask) {
2196        if info.cluster != cluster {
2197            info.mask = (info.mask & !GlyphFlags::DEFINED_BITS) | (mask & GlyphFlags::DEFINED_BITS);
2198        }
2199
2200        info.cluster = cluster;
2201    }
2202
2203    // Called around shape()
2204    pub(crate) fn enter(&mut self) {
2205        self.serial = 0;
2206        self.scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
2207
2208        if let Some(len) = self.len.checked_mul(Buffer::MAX_LEN_FACTOR) {
2209            self.max_len = len.max(Buffer::MAX_LEN_MIN);
2210        }
2211
2212        if let Ok(len) = i32::try_from(self.len) {
2213            if let Some(ops) = len.checked_mul(Buffer::MAX_OPS_FACTOR) {
2214                self.max_ops = ops.max(Buffer::MAX_OPS_MIN);
2215            }
2216        }
2217    }
2218
2219    // Called around shape()
2220    pub(crate) fn leave(&mut self) {
2221        self.max_len = Buffer::MAX_LEN_DEFAULT;
2222        self.max_ops = Buffer::MAX_OPS_DEFAULT;
2223        self.serial = 0;
2224    }
2225
2226    fn _infos_find_min_cluster(
2227        &self,
2228        info: &[GlyphInfo],
2229        start: usize,
2230        end: usize,
2231        cluster: Option<u32>,
2232    ) -> u32 {
2233        let mut cluster = cluster.unwrap_or(u32::MAX);
2234
2235        if start == end {
2236            return cluster;
2237        }
2238
2239        let Some(infos) = info.get(start..end) else {
2240            return cluster;
2241        };
2242        if self.cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS {
2243            for glyph_info in infos {
2244                cluster = min(cluster, glyph_info.cluster);
2245            }
2246        }
2247
2248        let first = infos.first().map_or(cluster, |info| info.cluster);
2249        let last = infos.last().map_or(cluster, |info| info.cluster);
2250        cluster.min(first.min(last))
2251    }
2252
2253    #[inline(always)]
2254    fn _infos_set_glyph_flags(
2255        &mut self,
2256        out_info: bool,
2257        start: usize,
2258        end: usize,
2259        cluster: u32,
2260        mask: Mask,
2261    ) {
2262        if start == end {
2263            return;
2264        }
2265
2266        let cluster_level = self.cluster_level;
2267
2268        let infos = if out_info {
2269            self.out_info_mut()
2270        } else {
2271            self.info.as_mut_slice()
2272        };
2273        if set_cluster_flags(infos, cluster_level, start, end, cluster, mask).is_none() {
2274            self.index_failed();
2275        }
2276    }
2277
2278    pub(crate) fn next_syllable(&self, start: usize) -> usize {
2279        if start >= self.len {
2280            return start;
2281        }
2282
2283        let end = start + min(self.len - start, MAX_SYLLABLE_LENGTH);
2284        let Some(infos) = self.info.get(start..end) else {
2285            return end;
2286        };
2287        let syllable = infos.first().map(GlyphInfo::syllable);
2288        start
2289            + infos
2290                .iter()
2291                .take_while(|info| Some(info.syllable()) == syllable)
2292                .count()
2293    }
2294
2295    #[inline]
2296    pub(crate) fn allocate_lig_id(&mut self) -> u8 {
2297        let mut lig_id = self.next_serial() & 0x07;
2298
2299        if lig_id == 0 {
2300            lig_id = self.allocate_lig_id();
2301        }
2302
2303        lig_id
2304    }
2305
2306    /// Sets the callback used to trace shaping operations, or removes it with `None`.
2307    ///
2308    /// The callback receives the current buffer, shaping font, and a HarfBuzz
2309    /// tracing message. Return `false` from a skippable `start` message to skip
2310    /// that operation. Per-glyph and recursion messages are informational and
2311    /// ignore the return value. Message text may change as shaping algorithms
2312    /// evolve.
2313    ///
2314    /// The callback can inspect [`Self::content_type`], [`Self::glyph_infos`],
2315    /// [`Self::glyph_positions`], or [`Self::serialize`]. Positions are empty
2316    /// until positioning begins. The callback is retained by [`Self::clear`]
2317    /// and [`Self::reset`]. To collect results, capture owned shared state such
2318    /// as an `Arc<Mutex<Vec<String>>>`.
2319    ///
2320    /// Requires the `tracing` feature, which also works without `std`.
2321    #[cfg(feature = "tracing")]
2322    pub fn set_message_function(&mut self, function: Option<Box<MessageFunction>>) {
2323        self.message_function = function;
2324    }
2325
2326    #[cfg(feature = "tracing")]
2327    pub(crate) fn message(&mut self, font: &crate::ShaperFont<'_, '_>, msg: &str) -> bool {
2328        // HarfBuzz formats into a 100-byte C string, including its terminator.
2329        let mut end = msg.find('\0').unwrap_or(msg.len()).min(99);
2330        while !msg.is_char_boundary(end) {
2331            end -= 1;
2332        }
2333        let msg = &msg[..end];
2334        if let Some(mut function) = self.message_function.take() {
2335            // Substitution keeps separate input/output cursors. Present the
2336            // emitted prefix followed by the remaining input without syncing
2337            // the live buffer, which could change its allocation-limit behavior.
2338            let ret = if self.have_output && (self.have_separate_output || self.out_len != self.idx)
2339            {
2340                let mut info = Vec::with_capacity(self.out_len + self.len - self.idx);
2341                info.extend_from_slice(&self.out_info()[..self.out_len]);
2342                info.extend_from_slice(&self.info[self.idx..self.len]);
2343                let snapshot = Buffer {
2344                    flags: self.flags,
2345                    cluster_level: self.cluster_level,
2346                    invisible: self.invisible,
2347                    not_found_variation_selector: self.not_found_variation_selector,
2348                    content_type: self.content_type,
2349                    direction: self.direction,
2350                    script: self.script,
2351                    language: self.language.clone(),
2352                    successful: self.successful,
2353                    len: info.len(),
2354                    info,
2355                    context: self.context,
2356                    context_len: self.context_len,
2357                    ..Buffer::new()
2358                };
2359                function(&snapshot, font, msg)
2360            } else {
2361                function(self, font, msg)
2362            };
2363            self.message_function = Some(function);
2364            ret
2365        } else {
2366            true
2367        }
2368    }
2369
2370    // The cursor position in the logical buffer shown to message callbacks.
2371    #[cfg(feature = "tracing")]
2372    pub(crate) fn message_idx(&self) -> usize {
2373        if self.have_output {
2374            self.out_len
2375        } else {
2376            self.idx
2377        }
2378    }
2379
2380    #[cfg(feature = "tracing")]
2381    #[inline]
2382    pub(crate) fn messaging(&self) -> bool {
2383        self.message_function.is_some()
2384    }
2385
2386    pub(crate) fn serialize_impl(
2387        &self,
2388        font: Option<&crate::ShaperFont<'_, '_>>,
2389        flags: SerializeFlags,
2390    ) -> Result<String, core::fmt::Error> {
2391        use core::fmt::Write;
2392
2393        let mut s = String::with_capacity(64);
2394
2395        let info = self.glyph_infos();
2396        if self.content_type == Some(ContentType::Unicode) {
2397            for info in info {
2398                s.push(if s.is_empty() { '<' } else { '|' });
2399                write!(&mut s, "U+{:04X}", info.glyph_id)?;
2400                if !flags.contains(SerializeFlags::NO_CLUSTERS) {
2401                    write!(&mut s, "={}", info.cluster)?;
2402                }
2403            }
2404            if !s.is_empty() {
2405                s.push('>');
2406            }
2407            return Ok(s);
2408        }
2409        let pos = self.glyph_positions();
2410        let default_pos = GlyphPosition::default();
2411        let mut x: i32 = 0;
2412        let mut y: i32 = 0;
2413        for (index, info) in info.iter().enumerate() {
2414            let position = pos.get(index).unwrap_or(&default_pos);
2415            s.push(if s.is_empty() { '[' } else { '|' });
2416
2417            if !flags.contains(SerializeFlags::NO_GLYPH_NAMES) {
2418                match font.and_then(|font| font.glyph_name(info.as_glyph())) {
2419                    Some(name) => s.push_str(name.as_str()),
2420                    None => write!(&mut s, "gid{}", info.glyph_id)?,
2421                }
2422            } else {
2423                write!(&mut s, "{}", info.glyph_id)?;
2424            }
2425
2426            if !flags.contains(SerializeFlags::NO_CLUSTERS) {
2427                write!(&mut s, "={}", info.cluster)?;
2428            }
2429
2430            if !pos.is_empty() && !flags.contains(SerializeFlags::NO_POSITIONS) {
2431                let dx = x.saturating_add(position.x_offset);
2432                let dy = y.saturating_add(position.y_offset);
2433                if dx != 0 || dy != 0 {
2434                    write!(&mut s, "@{dx},{dy}")?;
2435                }
2436
2437                if !flags.contains(SerializeFlags::NO_ADVANCES) {
2438                    write!(&mut s, "+{}", position.x_advance)?;
2439                    if position.y_advance != 0 {
2440                        write!(&mut s, ",{}", position.y_advance)?;
2441                    }
2442                }
2443            }
2444
2445            if flags.contains(SerializeFlags::GLYPH_FLAGS) {
2446                if info.mask & GlyphFlags::DEFINED_BITS != 0 {
2447                    write!(&mut s, "#{:X}", info.mask & GlyphFlags::DEFINED_BITS)?;
2448                }
2449            }
2450
2451            if flags.contains(SerializeFlags::GLYPH_EXTENTS) {
2452                if let Some(extents) = font.and_then(|font| font.glyph_extents(info.as_glyph())) {
2453                    write!(
2454                        &mut s,
2455                        "<{},{},{},{}>",
2456                        extents.x_bearing, extents.y_bearing, extents.width, extents.height
2457                    )?;
2458                }
2459            }
2460
2461            if flags.contains(SerializeFlags::NO_ADVANCES) {
2462                x = x.saturating_add(position.x_advance);
2463                y = y.saturating_add(position.y_advance);
2464            }
2465        }
2466
2467        if !s.is_empty() {
2468            s.push(']');
2469        }
2470
2471        Ok(s)
2472    }
2473}
2474
2475impl Default for Buffer {
2476    fn default() -> Self {
2477        Self::new()
2478    }
2479}
2480
2481impl core::fmt::Debug for Buffer {
2482    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2483        fmt.debug_struct("Buffer")
2484            .field("content_type", &self.content_type())
2485            .field("direction", &self.direction())
2486            .field("language", &self.language())
2487            .field("script", &self.script())
2488            .field("cluster_level", &self.cluster_level())
2489            .field("len", &self.len())
2490            .finish()
2491    }
2492}
2493
2494// All arguments (including formatting and font references) disappear when
2495// tracing is disabled. Allocate message text only when a callback is installed.
2496macro_rules! buffer_message {
2497    ($buffer:expr, $font:expr, $($arg:tt)*) => {{
2498        #[cfg(feature = "tracing")]
2499        {
2500            if $buffer.messaging() {
2501                let msg = alloc::format!($($arg)*);
2502                $buffer.message($font, &msg)
2503            } else {
2504                true
2505            }
2506        }
2507        #[cfg(not(feature = "tracing"))]
2508        { true }
2509    }};
2510}
2511
2512macro_rules! message {
2513    ($ctx:expr, $($arg:tt)*) => {
2514        buffer_message!($ctx.buffer, $ctx.font, $($arg)*)
2515    };
2516}
2517
2518macro_rules! message_return {
2519    ($ctx:expr, $($arg:tt)*) => {
2520        if !message!($ctx, $($arg)*) { return; }
2521    };
2522}
2523
2524macro_rules! message_continue {
2525    ($ctx:expr, $($arg:tt)*) => {
2526        if !message!($ctx, $($arg)*) { continue; }
2527    };
2528}
2529
2530pub(crate) fn _cluster_group_func(a: &GlyphInfo, b: &GlyphInfo) -> bool {
2531    a.cluster == b.cluster
2532}
2533
2534// TODO: to iter if possible
2535
2536/// Sets `mask` on the glyphs in `start..end` outside `cluster`, as far as the
2537/// cluster level allows.
2538#[inline(always)]
2539fn set_cluster_flags(
2540    infos: &mut [GlyphInfo],
2541    cluster_level: ClusterLevelCode,
2542    start: usize,
2543    end: usize,
2544    cluster: u32,
2545    mask: Mask,
2546) -> Option<()> {
2547    let cluster_first = infos.get(start)?.cluster;
2548    let cluster_last = infos.get(end - 1)?.cluster;
2549
2550    if cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS
2551        || (cluster != cluster_first && cluster != cluster_last)
2552    {
2553        for info in infos.get_mut(start..end)? {
2554            if info.cluster != cluster {
2555                info.mask |= mask;
2556            }
2557        }
2558        return Some(());
2559    }
2560
2561    // Monotone clusters
2562    if cluster == cluster_first {
2563        let mut i = end;
2564        while start < i {
2565            let info = infos.get_mut(i - 1)?;
2566            if info.cluster == cluster_first {
2567                break;
2568            }
2569            if cluster != info.cluster {
2570                info.mask |= mask;
2571            }
2572            i -= 1;
2573        }
2574    } else {
2575        let mut i = start;
2576        while i < end {
2577            let info = infos.get_mut(i)?;
2578            if info.cluster == cluster_last {
2579                break;
2580            }
2581            if cluster != info.cluster {
2582                info.mask |= mask;
2583            }
2584            i += 1;
2585        }
2586    }
2587    Some(())
2588}
2589
2590macro_rules! foreach_cluster {
2591    ($buffer:expr, $start:ident, $end:ident, $($body:tt)*) => {
2592        foreach_group!($buffer, $start, $end, $crate::buffer::_cluster_group_func, $($body)*)
2593    };
2594}
2595
2596macro_rules! foreach_group {
2597    ($buffer:expr, $start:ident, $end:ident, $group_func:expr, $($body:tt)*) => {{
2598        let count = $buffer.len;
2599        let mut $start = 0;
2600        let mut $end = if count > 0 { $buffer.group_end(0, $group_func) } else { 0 };
2601
2602        while $start < count {
2603            $($body)*;
2604            $start = $end;
2605            $end = $buffer.group_end($start, $group_func);
2606        }
2607    }};
2608}
2609
2610macro_rules! foreach_syllable {
2611    ($buffer:expr, $start:ident, $end:ident, $($body:tt)*) => {{
2612        let mut $start = 0;
2613        let mut $end = $buffer.next_syllable(0);
2614        while $start < $buffer.len {
2615            $($body)*;
2616            $start = $end;
2617            $end = $buffer.next_syllable($start);
2618        }
2619    }};
2620}
2621
2622macro_rules! foreach_grapheme {
2623    ($buffer:expr, $start:ident, $end:ident, $($body:tt)*) => {
2624        foreach_group!($buffer, $start, $end, $crate::ot::layout::grapheme_group, $($body)*)
2625    };
2626}
2627
2628bitflags::bitflags! {
2629    #[derive(Default, Debug, Clone, Copy)]
2630    pub struct UnicodeProps: u16 {
2631        const GENERAL_CATEGORY  = 0x001F;
2632        const IGNORABLE         = 0x0020;
2633        // MONGOLIAN FREE VARIATION SELECTOR 1..4, or TAG characters, or CGJ sometimes
2634        const HIDDEN            = 0x0040;
2635        const CONTINUATION      = 0x0080;
2636
2637        // If GEN_CAT=FORMAT, top byte masks:
2638        const CF_ZWJ            = 0x0100;
2639        const CF_ZWNJ           = 0x0200;
2640        const CF_VS             = 0x0400;
2641        const CF_AAT_DELETED    = 0x0800;
2642    }
2643}
2644
2645bitflags::bitflags! {
2646    #[derive(Default, Debug, Clone, Copy)]
2647    pub struct GlyphPropsFlags: u16 {
2648        // The following three match LookupFlags::Ignore* numbers.
2649        const BASE_GLYPH    = 0x02;
2650        const LIGATURE      = 0x04;
2651        const MARK          = 0x08;
2652        const CLASS_MASK    = Self::BASE_GLYPH.bits() | Self::LIGATURE.bits() | Self::MARK.bits();
2653
2654        // The following are used internally; not derived from GDEF.
2655        const SUBSTITUTED   = 0x10;
2656        const LIGATED       = 0x20;
2657        const MULTIPLIED    = 0x40;
2658
2659        const PRESERVE      = Self::SUBSTITUTED.bits() | Self::LIGATED.bits() | Self::MULTIPLIED.bits();
2660    }
2661}
2662
2663pub type ScratchFlags = u32;
2664pub const HB_BUFFER_SCRATCH_FLAG_DEFAULT: u32 = 0x0000_0000;
2665pub const HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH: u32 = 0x0000_0001;
2666pub const HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES: u32 = 0x0000_0002;
2667pub const HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK: u32 = 0x0000_0004;
2668pub const HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT: u32 = 0x0000_0008;
2669pub const HB_BUFFER_SCRATCH_FLAG_HAS_CGJ: u32 = 0x0000_0010;
2670pub const HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE: u32 = 0x0000_0020;
2671pub const HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK: u32 = 0x0000_0040;
2672pub const HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS: u32 = 0x0000_0080;
2673
2674/* Reserved for shapers' internal use. */
2675pub const HB_BUFFER_SCRATCH_FLAG_SHAPER0: u32 = 0x0100_0000;
2676// pub const HB_BUFFER_SCRATCH_FLAG_SHAPER1: u32 = 0x02000000;
2677// pub const HB_BUFFER_SCRATCH_FLAG_SHAPER2: u32 = 0x04000000;
2678// pub const HB_BUFFER_SCRATCH_FLAG_SHAPER3: u32 = 0x08000000;
2679
2680#[cfg(test)]
2681mod tests {
2682    use super::*;
2683
2684    #[test]
2685    fn next_syllable_is_bounded() {
2686        let mut buffer = Buffer::new();
2687        buffer
2688            .info
2689            .resize(MAX_SYLLABLE_LENGTH + 1, GlyphInfo::default());
2690        buffer.len = buffer.info.len();
2691        for info in &mut buffer.info {
2692            info.set_syllable(1);
2693        }
2694
2695        assert_eq!(buffer.next_syllable(0), MAX_SYLLABLE_LENGTH);
2696        assert_eq!(buffer.next_syllable(MAX_SYLLABLE_LENGTH), buffer.len);
2697    }
2698}