Skip to main content

harfrust/ot/
layout.rs

1//! OpenType layout.
2
3use core::ops::{Index, IndexMut};
4
5use super::apply::ApplyContext;
6use super::buffer::*;
7use super::set_digest::SetDigest;
8use super::GlyphInfo;
9use crate::aat::AatData;
10use crate::ot::apply::check_glyph_property;
11use crate::ot::lookup::CachedLookup;
12use crate::ot::OtData;
13use crate::plan::ShapePlan;
14use crate::unicode::{space_fallback, GeneralCategory};
15use crate::BufferFlags;
16use crate::ShaperFont;
17
18impl GlyphInfo {
19    declare_buffer_var!(u16, 1, 0, GLYPH_PROPS_VAR, glyph_props, set_glyph_props);
20    declare_buffer_var!(u8, 1, 2, LIG_PROPS_VAR, lig_props, set_lig_props);
21    declare_buffer_var!(u8, 1, 3, SYLLABLE_VAR, syllable, set_syllable);
22    declare_buffer_var!(
23        u16,
24        2,
25        0,
26        UNICODE_PROPS_VAR,
27        unicode_props,
28        set_unicode_props
29    );
30}
31
32impl Buffer {
33    pub(crate) fn allocate_unicode_vars(&mut self) {
34        self.allocate_var(GlyphInfo::UNICODE_PROPS_VAR);
35    }
36    pub(crate) fn deallocate_unicode_vars(&mut self) {
37        self.deallocate_var(GlyphInfo::UNICODE_PROPS_VAR);
38    }
39    pub(crate) fn assert_unicode_vars(&mut self) {
40        self.assert_var(GlyphInfo::UNICODE_PROPS_VAR);
41    }
42
43    pub(crate) fn allocate_gsubgpos_vars(&mut self) {
44        self.allocate_var(GlyphInfo::LIG_PROPS_VAR);
45        self.allocate_var(GlyphInfo::GLYPH_PROPS_VAR);
46    }
47    pub(crate) fn deallocate_gsubgpos_vars(&mut self) {
48        self.deallocate_var(GlyphInfo::LIG_PROPS_VAR);
49        self.deallocate_var(GlyphInfo::GLYPH_PROPS_VAR);
50    }
51    pub(crate) fn assert_gsubgpos_vars(&mut self) {
52        self.assert_var(GlyphInfo::LIG_PROPS_VAR);
53        self.assert_var(GlyphInfo::GLYPH_PROPS_VAR);
54    }
55}
56
57pub const MAX_NESTING_LEVEL: usize = 64;
58pub const MAX_CONTEXT_LENGTH: usize = 64;
59pub const MAX_SYLLABLE_LENGTH: usize = 64;
60
61pub fn has_kerning(aat: &AatData) -> bool {
62    aat.kern.is_some()
63}
64
65pub fn has_machine_kerning(aat: &AatData) -> bool {
66    match aat.kern {
67        Some(ref kern) => kern
68            .subtables()
69            .filter_map(Result::ok)
70            .any(|s| s.is_state_machine()),
71        None => false,
72    }
73}
74
75pub fn has_cross_kerning(aat: &AatData) -> bool {
76    match aat.kern {
77        Some(ref kern) => kern
78            .subtables()
79            .filter_map(Result::ok)
80            .any(|s| s.is_cross_stream()),
81        None => false,
82    }
83}
84
85// apply_kern
86
87pub fn set_glyph_props<const SET_DIGEST: bool>(ot: &OtData, buffer: &mut Buffer) {
88    buffer.assert_gsubgpos_vars();
89
90    let len = buffer.len;
91    for info in buffer.info.get_mut(..len).unwrap_or_default() {
92        if SET_DIGEST {
93            buffer.digest.add(info.glyph_id);
94        }
95        info.set_glyph_props(ot.glyph_props(info.as_glyph()));
96        info.set_lig_props(0);
97    }
98}
99
100pub fn has_glyph_classes(ot: &OtData) -> bool {
101    ot.has_glyph_classes()
102}
103
104// get_gsubgpos_table
105
106#[derive(Clone, Copy, Debug, PartialEq, Eq)]
107pub enum LayoutTableKind {
108    Gsub = 0,
109    Gpos = 1,
110}
111
112impl LayoutTableKind {
113    pub fn iter() -> impl Iterator<Item = LayoutTableKind> {
114        [Self::Gsub, Self::Gpos].iter().copied()
115    }
116}
117
118impl<T> Index<LayoutTableKind> for [T] {
119    type Output = T;
120
121    fn index(&self, table_index: LayoutTableKind) -> &Self::Output {
122        &self[table_index as usize]
123    }
124}
125
126impl<T> IndexMut<LayoutTableKind> for [T] {
127    fn index_mut(&mut self, table_index: LayoutTableKind) -> &mut Self::Output {
128        &mut self[table_index as usize]
129    }
130}
131
132/// A lookup-based layout table (GSUB or GPOS).
133pub trait LayoutTable {
134    /// The kind of layout table.
135    const KIND: LayoutTableKind;
136
137    /// Whether lookups in this table can be applied to the buffer in-place.
138    const IN_PLACE: bool;
139
140    /// Get the lookup at the specified index.
141    fn get_lookup(&self, index: u16) -> Option<&CachedLookup<'_>>;
142}
143
144/// Called before substitution lookups are performed, to ensure that glyph
145/// class and other properties are set on the glyphs in the buffer.
146pub fn substitute_start(ot: &OtData, buffer: &mut Buffer) {
147    set_glyph_props::<false>(ot, buffer);
148}
149
150pub fn substitute_start_with_digest(ot: &OtData, buffer: &mut Buffer) {
151    buffer.digest = SetDigest::new();
152    set_glyph_props::<true>(ot, buffer);
153}
154
155/// Applies the lookups in the given GSUB or GPOS table.
156pub fn apply_layout_table<T: LayoutTable>(
157    plan: &ShapePlan,
158    font: &ShaperFont<'_, '_>,
159    buffer: &mut Buffer,
160    table: Option<&T>,
161) {
162    let mut ctx = ApplyContext::new(
163        T::KIND,
164        font.layout(),
165        font.scale,
166        buffer,
167        #[cfg(feature = "tracing")]
168        font,
169    );
170
171    for (stage_index, stage) in plan.ot_map.stages(T::KIND).iter().enumerate() {
172        if let Some(table) = table {
173            for lookup_map in plan.ot_map.stage_lookups(T::KIND, stage_index) {
174                let Some(lookup) = table.get_lookup(lookup_map.index) else {
175                    continue;
176                };
177
178                message_continue!(
179                    ctx,
180                    "start lookup {} feature '{}'",
181                    lookup_map.index,
182                    crate::ot::map::MessageTag(Some(lookup_map.feature_tag))
183                );
184
185                if lookup.digest().may_intersect(&ctx.buffer.digest)
186                    && (ctx
187                        .buffer
188                        .flags
189                        .contains(BufferFlags::PRODUCE_UNSAFE_TO_CONCAT)
190                        || lookup.digest_second().may_intersect(&ctx.buffer.digest))
191                {
192                    ctx.lookup_index = lookup_map.index;
193                    ctx.set_lookup_mask(lookup_map.mask);
194                    ctx.auto_zwj = lookup_map.auto_zwj;
195                    ctx.auto_zwnj = lookup_map.auto_zwnj;
196
197                    ctx.random = lookup_map.random;
198                    ctx.per_syllable = lookup_map.per_syllable;
199
200                    apply_string::<T>(&mut ctx, lookup);
201                } else {
202                    message!(
203                        ctx,
204                        "skipped lookup {} feature '{}' because no glyph matches",
205                        lookup_map.index,
206                        crate::ot::map::MessageTag(Some(lookup_map.feature_tag))
207                    );
208                }
209                message!(
210                    ctx,
211                    "end lookup {} feature '{}'",
212                    lookup_map.index,
213                    crate::ot::map::MessageTag(Some(lookup_map.feature_tag))
214                );
215            }
216        }
217
218        if let Some(func) = stage.pause_func {
219            let changed = func(plan, font, ctx.buffer);
220            if changed.is_none() {
221                ctx.buffer.index_failed();
222            }
223            // A pause that stopped early may still have changed the buffer.
224            if changed.unwrap_or(true) {
225                ctx.buffer.update_digest();
226            }
227        }
228    }
229}
230
231fn apply_string<T: LayoutTable>(ctx: &mut ApplyContext, lookup: &CachedLookup) -> bool {
232    if ctx.buffer.is_empty() || ctx.lookup_mask() == 0 {
233        return false;
234    }
235
236    let ret;
237    ctx.lookup_props = lookup.props();
238    ctx.update_matchers();
239
240    if !lookup.is_reverse() {
241        // in/out forward substitution/positioning
242        if !T::IN_PLACE {
243            ctx.buffer.clear_output();
244        }
245        ctx.buffer.idx = 0;
246        ret = apply_forward(ctx, lookup);
247
248        if !T::IN_PLACE {
249            ctx.buffer.sync();
250        }
251    } else {
252        // in-place backward substitution/positioning
253        debug_assert!(!ctx.buffer.have_output);
254
255        ctx.buffer.idx = ctx.buffer.len - 1;
256        ret = apply_backward(ctx, lookup);
257    }
258    ret
259}
260
261fn apply_forward(ctx: &mut ApplyContext, lookup: &CachedLookup) -> bool {
262    let mut ret = false;
263
264    let use_hot_subtable_cache = lookup.cache_enter(ctx);
265
266    // Loop-invariant: nested lookups save and restore lookup_props and the
267    // lookup mask (see ApplyContext::recurse), so these can be
268    // hoisted, letting the candidate scan below run over a slice without
269    // per-glyph bounds checks.
270    let ot = ctx.layout.ot;
271    let lookup_mask = ctx.lookup_mask();
272    let lookup_props = ctx.lookup_props;
273
274    while ctx.buffer.successful {
275        let idx = ctx.buffer.idx;
276        let infos = ctx.buffer.glyph_infos();
277        let mut j = idx;
278        while let Some(info) = infos.get(j) {
279            if lookup.digest.may_have(info.glyph_id)
280                && (info.mask & lookup_mask) != 0
281                && check_glyph_property(ot, info, lookup_props)
282            {
283                break;
284            }
285            j += 1;
286        }
287
288        if j > idx {
289            ctx.buffer.next_glyphs(j - idx);
290        }
291        if ctx.buffer.idx >= ctx.buffer.len {
292            break;
293        }
294
295        if lookup.apply(ctx, use_hot_subtable_cache).is_some() {
296            ret = true;
297        } else {
298            ctx.buffer.next_glyph();
299        }
300    }
301
302    if use_hot_subtable_cache {
303        lookup.cache_leave(ctx);
304    }
305
306    ret
307}
308
309pub(crate) fn apply_synthesized_subst_lookup(ctx: &mut ApplyContext, lookup: &CachedLookup) {
310    if ctx.buffer.is_empty() || ctx.lookup_mask() == 0 {
311        return;
312    }
313
314    debug_assert!(lookup.is_subst);
315    debug_assert!(!lookup.is_reverse());
316    ctx.lookup_props = lookup.props();
317    ctx.update_matchers();
318    ctx.buffer.clear_output();
319    ctx.buffer.idx = 0;
320    apply_forward(ctx, lookup);
321    ctx.buffer.sync();
322}
323
324fn apply_backward(ctx: &mut ApplyContext, lookup: &CachedLookup) -> bool {
325    let mut ret = false;
326    // Loop-invariant hoisting as in apply_forward; reverse lookups don't
327    // recurse, and in-place application cannot change the buffer length.
328    let ot = ctx.layout.ot;
329    let lookup_mask = ctx.lookup_mask();
330    let lookup_props = ctx.lookup_props;
331
332    loop {
333        let candidate = ctx
334            .buffer
335            .info
336            .get(..=ctx.buffer.idx)
337            .unwrap_or_default()
338            .iter()
339            .rposition(|info| {
340                lookup.digest.may_have(info.glyph_id)
341                    && (info.mask & lookup_mask) != 0
342                    && check_glyph_property(ot, info, lookup_props)
343            });
344
345        let Some(idx) = candidate else {
346            ctx.buffer.idx = 0;
347            break;
348        };
349
350        ctx.buffer.idx = idx;
351        ret |= lookup.apply(ctx, false).is_some();
352
353        if idx == 0 {
354            break;
355        }
356        ctx.buffer.idx = idx - 1;
357    }
358    ret
359}
360
361/* unicode_props */
362
363/* Design:
364 * unicode_props() is a two-byte number.  The low byte includes:
365 * - Modified General_Category: 5 bits.
366 * - A bit each for:
367 *   * Is it Default_Ignorable(); we have a modified Default_Ignorable().
368 *   * Whether it's one of the four Mongolian Free Variation Selectors,
369 *     CGJ, or other characters that are hidden but should not be ignored
370 *     like most other Default_Ignorable()s do during GSUB matching.
371 *   * Whether it's a grapheme continuation.
372 *
373 * The high-byte has different meanings, switched by the Gen-Cat:
374 * - For Mn,Mc,Me: the modified Combining_Class.
375 * - For Cf: whether it's ZWJ, ZWNJ, or something else.
376 * - For Ws: index of which space character this is, if space fallback
377 *   is needed, ie. we don't set this by default, only if asked to.
378 *
379 * Above I said "modified" General_Category. This is because we need to
380 * remember Variation Selectors, and we don't have bits left. So we
381 * change their Gen_Cat from Mn to Cf, and use a bit of the high byte to
382 * remember them.
383 */
384
385//  enum hb_unicode_props_flags_t {
386//     UPROPS_MASK_GEN_CAT	= 0x001Fu,
387//     UPROPS_MASK_IGNORABLE	= 0x0020u,
388//     UPROPS_MASK_HIDDEN	= 0x0040u, /* MONGOLIAN FREE VARIATION SELECTOR 1..4, or TAG characters */
389//     UPROPS_MASK_CONTINUATION=0x0080u,
390
391//     /* If GEN_CAT=FORMAT, top byte masks: */
392//     UPROPS_MASK_Cf_ZWJ	= 0x0100u,
393//     UPROPS_MASK_Cf_ZWNJ	= 0x0200u
394//   };
395//   HB_MARK_AS_FLAG_T (hb_unicode_props_flags_t);
396
397//   static inline void
398//   _hb_glyph_info_set_unicode_props (hb_glyph_info_t *info, Buffer *buffer)
399//   {
400//     space_fallback *unicode = buffer->unicode;
401//     unsigned int u = info->codepoint;
402//     unsigned int gen_cat = (unsigned int) unicode->general_category (u);
403//     unsigned int props = gen_cat;
404
405//     if (u >= 0x80u)
406//     {
407//       if (unlikely (unicode->is_default_ignorable (u)))
408//       {
409//         buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES;
410//         props |=  UPROPS_MASK_IGNORABLE;
411//         if (u == 0x200Cu) props |= UPROPS_MASK_Cf_ZWNJ;
412//         else if (u == 0x200Du) props |= UPROPS_MASK_Cf_ZWJ;
413//         /* Mongolian Free Variation Selectors need to be remembered
414//          * because although we need to hide them like default-ignorables,
415//          * they need to non-ignorable during shaping.  This is similar to
416//          * what we do for joiners in Indic-like shapers, but since the
417//          * FVSes are GC=Mn, we have use a separate bit to remember them.
418//          * Fixes:
419//          * https://github.com/harfbuzz/harfbuzz/issues/234 */
420//         else if (unlikely (hb_in_ranges<hb_codepoint_t> (u, 0x180Bu, 0x180Du, 0x180Fu, 0x180Fu))) props |= UPROPS_MASK_HIDDEN;
421//         /* TAG characters need similar treatment. Fixes:
422//          * https://github.com/harfbuzz/harfbuzz/issues/463 */
423//         else if (unlikely (hb_in_range<hb_codepoint_t> (u, 0xE0020u, 0xE007Fu))) props |= UPROPS_MASK_HIDDEN;
424//         /* COMBINING GRAPHEME JOINER should not be skipped; at least some times.
425//          * https://github.com/harfbuzz/harfbuzz/issues/554 */
426//         else if (unlikely (u == 0x034Fu))
427//         {
428//       buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_CGJ;
429//       props |= UPROPS_MASK_HIDDEN;
430//         }
431//       }
432
433//       if (unlikely (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (gen_cat)))
434//       {
435//         props |= UPROPS_MASK_CONTINUATION;
436//         props |= unicode->modified_combining_class (u)<<8;
437//       }
438//     }
439
440//     info->unicode_props() = props;
441//   }
442
443impl GlyphInfo {
444    /// HB: _hb_glyph_info_set_general_category
445    ///
446    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L260>
447    #[doc(alias = "_hb_glyph_info_set_general_category")]
448    #[inline]
449    pub(crate) fn set_general_category(&mut self, gen_cat: GeneralCategory) {
450        /* Clears top-byte. */
451        let gen_cat = gen_cat.0 as u32;
452        let n = (gen_cat as u16)
453            | (self.unicode_props() & (0xFF & !UnicodeProps::GENERAL_CATEGORY.bits()));
454        self.set_unicode_props(n);
455    }
456
457    /// HB: _hb_glyph_info_get_general_category
458    ///
459    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L268>
460    #[doc(alias = "_hb_glyph_info_get_general_category")]
461    #[inline]
462    pub(crate) fn general_category(&self) -> GeneralCategory {
463        let n = self.unicode_props() & UnicodeProps::GENERAL_CATEGORY.bits();
464        GeneralCategory(n as u8)
465    }
466
467    /// HB: _hb_glyph_info_is_unicode_mark
468    ///
469    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L274>
470    #[doc(alias = "_hb_glyph_info_is_unicode_mark")]
471    #[inline]
472    pub(crate) fn is_unicode_mark(&self) -> bool {
473        self.general_category().is_mark()
474    }
475
476    /// HB: _hb_glyph_info_set_modified_combining_class
477    ///
478    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L279>
479    #[doc(alias = "_hb_glyph_info_set_modified_combining_class")]
480    #[inline]
481    pub(crate) fn set_modified_combining_class(&mut self, modified_class: u8) {
482        if !self.is_unicode_mark() {
483            return;
484        }
485        let n = ((modified_class as u16) << 8) | (self.unicode_props() & 0xFF);
486        self.set_unicode_props(n);
487    }
488
489    /// HB: _hb_glyph_info_get_modified_combining_class
490    ///
491    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L287>
492    #[doc(alias = "_hb_glyph_info_get_modified_combining_class")]
493    #[inline]
494    pub(crate) fn modified_combining_class(&self) -> u8 {
495        if self.is_unicode_mark() {
496            (self.unicode_props() >> 8) as u8
497        } else {
498            0
499        }
500    }
501
502    // TODO: use
503    // #[inline]
504    // pub fn info_cc(info: &hb_glyph_info_t) -> u8 {
505    //     _hb_glyph_info_get_modified_combining_class(info)
506    // }
507
508    /// HB: _hb_glyph_info_is_unicode_space
509    ///
510    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L294>
511    #[doc(alias = "_hb_glyph_info_is_unicode_space")]
512    #[inline]
513    pub(crate) fn is_unicode_space(&self) -> bool {
514        self.general_category() == GeneralCategory::SPACE_SEPARATOR
515    }
516
517    /// HB: _hb_glyph_info_set_unicode_space_fallback_type
518    ///
519    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L300>
520    #[doc(alias = "_hb_glyph_info_set_unicode_space_fallback_type")]
521    #[inline]
522    pub(crate) fn set_unicode_space_fallback_type(&mut self, s: space_fallback::SpaceCode) {
523        if !self.is_unicode_space() {
524            return;
525        }
526        let n = ((s as u16) << 8) | (self.unicode_props() & 0xFF);
527        self.set_unicode_props(n);
528    }
529
530    /// HB: _hb_glyph_info_get_unicode_space_fallback_type
531    ///
532    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L307>
533    #[doc(alias = "_hb_glyph_info_get_unicode_space_fallback_type")]
534    #[inline]
535    pub(crate) fn unicode_space_fallback_type(&self) -> space_fallback::SpaceCode {
536        if self.is_unicode_space() {
537            (self.unicode_props() >> 8) as u8
538        } else {
539            space_fallback::NOT_SPACE
540        }
541    }
542
543    /// HB: _hb_glyph_info_is_variation_selector
544    ///
545    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L314>
546    #[doc(alias = "_hb_glyph_info_is_variation_selector")]
547    #[inline]
548    pub(crate) fn is_variation_selector(&self) -> bool {
549        let a = self.general_category() == GeneralCategory::FORMAT;
550        let b = (self.unicode_props() & UnicodeProps::CF_VS.bits()) != 0;
551        a && b
552    }
553
554    /// HB: _hb_glyph_info_set_variation_selector
555    ///
556    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L321>
557    #[doc(alias = "_hb_glyph_info_set_variation_selector")]
558    #[inline]
559    pub(crate) fn set_variation_selector(&mut self, customize: bool) {
560        if customize {
561            self.set_general_category(GeneralCategory::FORMAT);
562            self.set_unicode_props(self.unicode_props() | UnicodeProps::CF_VS.bits());
563        } else {
564            // Reset to their original condition
565            self.set_general_category(GeneralCategory::NON_SPACING_MARK);
566        }
567    }
568
569    /// HB: _hb_glyph_info_is_default_ignorable
570    ///
571    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L338>
572    #[doc(alias = "_hb_glyph_info_is_default_ignorable")]
573    #[inline]
574    pub(crate) fn is_default_ignorable(&self) -> bool {
575        let n = self.unicode_props() & UnicodeProps::IGNORABLE.bits();
576        n != 0 && !self.substituted()
577    }
578
579    /// HB: _hb_glyph_info_set_default_ignorable
580    ///
581    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L344>
582    #[doc(alias = "_hb_glyph_info_set_default_ignorable")]
583    #[inline]
584    pub(crate) fn _set_default_ignorable(&mut self) {
585        self.set_unicode_props(self.unicode_props() | UnicodeProps::IGNORABLE.bits());
586    }
587
588    /// HB: _hb_glyph_info_clear_default_ignorable
589    ///
590    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L349>
591    #[doc(alias = "_hb_glyph_info_clear_default_ignorable")]
592    #[inline]
593    pub(crate) fn clear_default_ignorable(&mut self) {
594        let mut n = self.unicode_props();
595        n &= !UnicodeProps::IGNORABLE.bits();
596        self.set_unicode_props(n);
597    }
598
599    /// HB: _hb_glyph_info_is_hidden
600    ///
601    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L354>
602    #[doc(alias = "_hb_glyph_info_is_hidden")]
603    #[inline]
604    pub(crate) fn is_hidden(&self) -> bool {
605        (self.unicode_props() & UnicodeProps::HIDDEN.bits()) != 0
606    }
607
608    //   static inline void
609    //   _hb_glyph_info_unhide (hb_glyph_info_t *info)
610    //   {
611    //     info->unicode_props() &= ~ UPROPS_MASK_HIDDEN;
612    //   }
613
614    /// HB: _hb_glyph_info_set_continuation
615    ///
616    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L365>
617    #[doc(alias = "_hb_glyph_info_set_continuation")]
618    #[inline]
619    pub(crate) fn set_continuation(&mut self, scratch_flags: &mut ScratchFlags) {
620        *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS;
621        let mut n = self.unicode_props();
622        n |= UnicodeProps::CONTINUATION.bits();
623        self.set_unicode_props(n);
624    }
625
626    /// HB: _hb_glyph_info_clear_continuation
627    ///
628    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L370>
629    #[doc(alias = "_hb_glyph_info_clear_continuation")]
630    #[inline]
631    pub(crate) fn clear_continuation(&mut self) {
632        let mut n = self.unicode_props();
633        n &= !UnicodeProps::CONTINUATION.bits();
634        self.set_unicode_props(n);
635    }
636
637    /// HB: _hb_glyph_info_is_continuation
638    ///
639    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L375>
640    #[doc(alias = "_hb_glyph_info_is_continuation")]
641    #[inline]
642    pub(crate) fn is_continuation(&self) -> bool {
643        self.unicode_props() & UnicodeProps::CONTINUATION.bits() != 0
644    }
645}
646
647pub(crate) fn grapheme_group(_: &GlyphInfo, b: &GlyphInfo) -> bool {
648    b.is_continuation()
649}
650
651pub fn reverse_graphemes(buffer: &mut Buffer) {
652    // MONOTONE_GRAPHEMES was already applied and is taken care of by grapheme_group.
653    // So we just check for MONOTONE_CHARACTERS here.
654    buffer.reverse_groups(
655        grapheme_group,
656        buffer.cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS,
657    );
658}
659
660impl GlyphInfo {
661    /// HB: _hb_glyph_info_is_unicode_format
662    ///
663    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L398>
664    #[doc(alias = "_hb_glyph_info_is_unicode_format")]
665    #[inline]
666    pub(crate) fn is_unicode_format(&self) -> bool {
667        self.general_category() == GeneralCategory::FORMAT
668    }
669
670    /// HB: _hb_glyph_info_is_zwnj
671    ///
672    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L404>
673    #[doc(alias = "_hb_glyph_info_is_zwnj")]
674    #[inline]
675    pub(crate) fn is_zwnj(&self) -> bool {
676        self.is_unicode_format() && (self.unicode_props() & UnicodeProps::CF_ZWNJ.bits() != 0)
677    }
678
679    /// HB: _hb_glyph_info_is_zwj
680    ///
681    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L409>
682    #[doc(alias = "_hb_glyph_info_is_zwnj")]
683    #[inline]
684    pub(crate) fn is_zwj(&self) -> bool {
685        self.is_unicode_format() && (self.unicode_props() & UnicodeProps::CF_ZWJ.bits() != 0)
686    }
687
688    /// HB: _hb_glyph_info_is_aat_deleted
689    ///
690    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L426>
691    #[doc(alias = "_hb_glyph_info_is_aat_deleted")]
692    #[inline]
693    pub(crate) fn is_aat_deleted(&self) -> bool {
694        self.is_unicode_format()
695            && (self.unicode_props() & UnicodeProps::CF_AAT_DELETED.bits() != 0)
696    }
697
698    /// HB: _hb_glyph_info_set_aat_deleted
699    ///
700    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L431>
701    #[doc(alias = "_hb_glyph_info_set_aat_deleted")]
702    #[inline]
703    pub(crate) fn set_aat_deleted(&mut self) {
704        self.set_general_category(GeneralCategory::FORMAT);
705        self.set_unicode_props(
706            self.unicode_props()
707                | UnicodeProps::CF_AAT_DELETED.bits()
708                | UnicodeProps::HIDDEN.bits(),
709        );
710    }
711
712    //   /* lig_props: aka lig_id / lig_comp
713    //    *
714    //    * When a ligature is formed:
715    //    *
716    //    *   - The ligature glyph and any marks in between all the same newly allocated
717    //    *     lig_id,
718    //    *   - The ligature glyph will get lig_num_comps set to the number of components
719    //    *   - The marks get lig_comp > 0, reflecting which component of the ligature
720    //    *     they were applied to.
721    //    *   - This is used in GPOS to attach marks to the right component of a ligature
722    //    *     in MarkLigPos,
723    //    *   - Note that when marks are ligated together, much of the above is skipped
724    //    *     and the current lig_id reused.
725    //    *
726    //    * When a multiple-substitution is done:
727    //    *
728    //    *   - All resulting glyphs will have lig_id = 0,
729    //    *   - The resulting glyphs will have lig_comp = 0, 1, 2, ... respectively.
730    //    *   - This is used in GPOS to attach marks to the first component of a
731    //    *     multiple substitution in MarkBasePos.
732    //    *
733    //    * The numbers are also used in GPOS to do mark-to-mark positioning only
734    //    * to marks that belong to the same component of the same ligature.
735    //    */
736    //   static inline void
737    //   _hb_glyph_info_clear_lig_props (hb_glyph_info_t *info)
738    //   {
739    //     info->lig_props() = 0;
740    //   }
741
742    const IS_LIG_BASE: u8 = 0x10;
743
744    /// HB: _hb_glyph_info_set_lig_props_for_ligature
745    ///
746    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L472>
747    #[doc(alias = "_hb_glyph_info_set_lig_props_for_ligature")]
748    #[inline]
749    pub(crate) fn set_lig_props_for_ligature(&mut self, lig_id: u8, lig_num_comps: u8) {
750        self.set_lig_props((lig_id << 5) | Self::IS_LIG_BASE | (lig_num_comps & 0x0F));
751    }
752
753    /// HB: _hb_glyph_info_set_lig_props_for_mark
754    ///
755    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L480>
756    #[doc(alias = "_hb_glyph_info_set_lig_props_for_mark")]
757    #[inline]
758    pub(crate) fn set_lig_props_for_mark(&mut self, lig_id: u8, lig_comp: u8) {
759        self.set_lig_props((lig_id << 5) | (lig_comp & 0x0F));
760    }
761
762    /// HB: _hb_glyph_info_set_lig_props_for_component
763    ///
764    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L488>
765    #[doc(alias = "_hb_glyph_info_set_lig_props_for_component")]
766    #[inline]
767    pub(crate) fn set_lig_props_for_component(&mut self, comp: u8) {
768        self.set_lig_props_for_mark(0, comp);
769    }
770
771    /// HB: _hb_glyph_info_get_lig_id
772    ///
773    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L494>
774    #[doc(alias = "_hb_glyph_info_get_lig_id")]
775    #[inline]
776    pub(crate) fn lig_id(&self) -> u8 {
777        self.lig_props() >> 5
778    }
779
780    /// HB: _hb_glyph_info_ligated_internal
781    ///
782    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L500>
783    #[doc(alias = "_hb_glyph_info_ligated_internal")]
784    #[inline]
785    pub(crate) fn ligated_internal(&self) -> bool {
786        self.lig_props() & Self::IS_LIG_BASE != 0
787    }
788
789    /// HB: _hb_glyph_info_get_lig_comp
790    ///
791    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L506>
792    #[doc(alias = "_hb_glyph_info_get_lig_comp")]
793    #[inline]
794    pub(crate) fn lig_comp(&self) -> u8 {
795        if self.ligated_internal() {
796            0
797        } else {
798            self.lig_props() & 0x0F
799        }
800    }
801
802    /// HB: _hb_glyph_info_get_lig_num_comps
803    ///
804    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L515>
805    #[doc(alias = "_hb_glyph_info_get_lig_num_comps")]
806    #[inline]
807    pub(crate) fn lig_num_comps(&self) -> u8 {
808        if self.glyph_props() & GlyphPropsFlags::LIGATURE.bits() != 0 && self.ligated_internal() {
809            self.lig_props() & 0x0F
810        } else {
811            1
812        }
813    }
814
815    //   /* glyph_props: */
816    //   static inline void
817    //   _hb_glyph_info_set_glyph_props (hb_glyph_info_t *info, unsigned int props)
818    //   {
819    //     info->glyph_props() = props;
820    //   }
821
822    //   static inline unsigned int
823    //   _hb_glyph_info_get_glyph_props (const hb_glyph_info_t *info)
824    //   {
825    //     return info->glyph_props();
826    //   }
827
828    /// HB: _hb_glyph_info_is_base_glyph
829    ///
830    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L548>
831    #[doc(alias = "_hb_glyph_info_is_base_glyph")]
832    #[inline]
833    pub(crate) fn is_base_glyph(&self) -> bool {
834        self.glyph_props() & GlyphPropsFlags::BASE_GLYPH.bits() != 0
835    }
836
837    /// HB: _hb_glyph_info_is_ligature
838    ///
839    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L554>
840    #[doc(alias = "_hb_glyph_info_is_ligature")]
841    #[inline]
842    pub(crate) fn is_ligature(&self) -> bool {
843        self.glyph_props() & GlyphPropsFlags::LIGATURE.bits() != 0
844    }
845
846    /// HB: _hb_glyph_info_is_mark
847    ///
848    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L560>
849    #[doc(alias = "_hb_glyph_info_is_mark")]
850    #[inline]
851    pub(crate) fn is_mark(&self) -> bool {
852        self.glyph_props() & GlyphPropsFlags::MARK.bits() != 0
853    }
854
855    /// HB: _hb_glyph_info_substituted
856    ///
857    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L566>
858    #[doc(alias = "_hb_glyph_info_substituted")]
859    #[inline]
860    pub(crate) fn substituted(&self) -> bool {
861        self.glyph_props() & GlyphPropsFlags::SUBSTITUTED.bits() != 0
862    }
863
864    /// HB: _hb_glyph_info_ligated
865    ///
866    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L572>
867    #[doc(alias = "_hb_glyph_info_ligated")]
868    #[inline]
869    pub(crate) fn ligated(&self) -> bool {
870        self.glyph_props() & GlyphPropsFlags::LIGATED.bits() != 0
871    }
872
873    /// HB: _hb_glyph_info_multiplied
874    ///
875    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L578>
876    #[doc(alias = "_hb_glyph_info_multiplied")]
877    #[inline]
878    pub(crate) fn multiplied(&self) -> bool {
879        self.glyph_props() & GlyphPropsFlags::MULTIPLIED.bits() != 0
880    }
881
882    /// Returns the number of components this glyph contributes when forming a ligature.
883    #[inline]
884    pub(crate) fn lig_num_comps_in_ligation(&self) -> u8 {
885        // When a glyph is decomposed by a MultipleSubst and its pieces later become
886        // components of a ligature, the pieces belong to the same ligature component
887        // as the first piece, matching how MarkBasePos attaches marks only to the
888        // first piece. So the non-first pieces contribute no extra component.
889        // https://github.com/harfbuzz/harfbuzz/issues/4969
890        if self.multiplied() && self.lig_comp() != 0 {
891            0
892        } else {
893            self.lig_num_comps()
894        }
895    }
896
897    /// HB: _hb_glyph_info_ligated_and_didnt_multiply
898    ///
899    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L584>
900    #[doc(alias = "_hb_glyph_info_ligated_and_didnt_multiply")]
901    #[inline]
902    pub(crate) fn ligated_and_didnt_multiply(&self) -> bool {
903        self.ligated() && !self.multiplied()
904    }
905
906    /// HB: _hb_glyph_info_clear_ligated_and_multiplied
907    ///
908    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L590>
909    #[doc(alias = "_hb_glyph_info_clear_ligated_and_multiplied")]
910    #[inline]
911    pub(crate) fn clear_ligated_and_multiplied(&mut self) {
912        let mut n = self.glyph_props();
913        n &= !(GlyphPropsFlags::LIGATED | GlyphPropsFlags::MULTIPLIED).bits();
914        self.set_glyph_props(n);
915    }
916
917    /// HB: _hb_glyph_info_clear_substituted
918    ///
919    /// See <https://github.com/harfbuzz/harfbuzz/blob/368598b5bd9c37a15cb0fd5438b8e617e254609b/src/hb-ot-layout.hh#L597>
920    #[doc(alias = "_hb_glyph_info_clear_substituted")]
921    #[inline]
922    pub(crate) fn clear_substituted(&mut self) {
923        let mut n = self.glyph_props();
924        n &= !GlyphPropsFlags::SUBSTITUTED.bits();
925        self.set_glyph_props(n);
926    }
927}
928
929pub fn clear_substitution_flags(
930    _: &ShapePlan,
931    _: &ShaperFont<'_, '_>,
932    buffer: &mut Buffer,
933) -> Option<bool> {
934    for info in buffer.info_slice_mut() {
935        info.clear_substituted();
936    }
937
938    Some(false)
939}