Coverage Report

Created: 2025-11-04 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/OT/Layout/GSUB/AlternateSet.hh
Line
Count
Source
1
#ifndef OT_LAYOUT_GSUB_ALTERNATESET_HH
2
#define OT_LAYOUT_GSUB_ALTERNATESET_HH
3
4
#include "Common.hh"
5
6
namespace OT {
7
namespace Layout {
8
namespace GSUB_impl {
9
10
template <typename Types>
11
struct AlternateSet
12
{
13
  protected:
14
  Array16Of<typename Types::HBGlyphID>
15
                alternates;             /* Array of alternate GlyphIDs--in
16
                                         * arbitrary order */
17
  public:
18
  DEFINE_SIZE_ARRAY (2, alternates);
19
20
  bool sanitize (hb_sanitize_context_t *c) const
21
2.45M
  {
22
2.45M
    TRACE_SANITIZE (this);
23
2.45M
    return_trace (alternates.sanitize (c));
24
2.45M
  }
25
26
  bool intersects (const hb_set_t *glyphs) const
27
  { return hb_any (alternates, glyphs); }
28
29
  void closure (hb_closure_context_t *c) const
30
0
  { c->output->add_array (alternates.arrayZ, alternates.len); }
31
32
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
33
0
  { c->output->add_array (alternates.arrayZ, alternates.len); }
34
35
  bool apply (hb_ot_apply_context_t *c) const
36
0
  {
37
0
    TRACE_APPLY (this);
38
0
    unsigned int count = alternates.len;
39
40
0
    if (unlikely (!count)) return_trace (false);
41
42
0
    hb_mask_t glyph_mask = c->buffer->cur().mask;
43
0
    hb_mask_t lookup_mask = c->lookup_mask;
44
45
    /* Note: This breaks badly if two features enabled this lookup together. */
46
0
    unsigned int shift = hb_ctz (lookup_mask);
47
0
    unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
48
49
    /* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */
50
0
    if (alt_index == HB_OT_MAP_MAX_VALUE && c->random)
51
0
    {
52
      /* Maybe we can do better than unsafe-to-break all; but since we are
53
       * changing random state, it would be hard to track that.  Good 'nough. */
54
0
      c->buffer->unsafe_to_break (0, c->buffer->len);
55
0
      alt_index = c->random_number () % count + 1;
56
0
    }
57
58
0
    if (unlikely (alt_index > count || alt_index == 0)) return_trace (false);
59
60
0
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
61
0
    {
62
0
      c->buffer->sync_so_far ();
63
0
      c->buffer->message (c->font,
64
0
        "replacing glyph at %u (alternate substitution)",
65
0
        c->buffer->idx);
66
0
    }
67
68
0
    c->replace_glyph (alternates[alt_index - 1]);
69
70
0
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
71
0
    {
72
0
      c->buffer->message (c->font,
73
0
        "replaced glyph at %u (alternate substitution)",
74
0
        c->buffer->idx - 1u);
75
0
    }
76
77
0
    return_trace (true);
78
0
  }
79
80
  unsigned
81
  get_alternates (unsigned        start_offset,
82
                  unsigned       *alternate_count  /* IN/OUT.  May be NULL. */,
83
                  hb_codepoint_t *alternate_glyphs /* OUT.     May be NULL. */) const
84
0
  {
85
0
    if (alternates.len && alternate_count)
86
0
    {
87
0
      + alternates.as_array ().sub_array (start_offset, alternate_count)
88
0
      | hb_sink (hb_array (alternate_glyphs, *alternate_count))
89
0
      ;
90
0
    }
91
0
    return alternates.len;
92
0
  }
93
94
  void
95
  collect_alternates (hb_codepoint_t gid,
96
          hb_map_t  *alternate_count /* IN/OUT */,
97
          hb_map_t  *alternate_glyphs /* IN/OUT */) const
98
0
  {
99
0
    + hb_enumerate (alternates)
100
0
    | hb_map ([gid] (hb_pair_t<unsigned, hb_codepoint_t> _) { return hb_pair (gid + (_.first << 24), _.second); })
101
0
    | hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void
102
0
    { _hb_collect_glyph_alternates_add (p.first, p.second,
103
0
                alternate_count, alternate_glyphs); })
104
0
    ;
105
0
  }
106
107
  template <typename Iterator,
108
            hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
109
  bool serialize (hb_serialize_context_t *c,
110
                  Iterator alts)
111
0
  {
112
0
    TRACE_SERIALIZE (this);
113
0
    return_trace (alternates.serialize (c, alts));
114
0
  }
Unexecuted instantiation: _ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI10hb_array_tIKNS_11HBGlyphID16EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELPv0EEEbP22hb_serialize_context_tSB_
Unexecuted instantiation: hb-ot-face.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_
Unexecuted instantiation: hb-aat-layout.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_
Unexecuted instantiation: hb-ot-layout.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_
Unexecuted instantiation: hb-ot-shaper-arabic.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_
115
116
  bool subset (hb_subset_context_t *c) const
117
0
  {
118
0
    TRACE_SUBSET (this);
119
0
    const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
120
0
    const hb_map_t &glyph_map = *c->plan->glyph_map;
121
0
122
0
    auto it =
123
0
      + hb_iter (alternates)
124
0
      | hb_filter (glyphset)
125
0
      | hb_map (glyph_map)
126
0
      ;
127
0
128
0
    auto *out = c->serializer->start_embed (*this);
129
0
    return_trace (out->serialize (c->serializer, it) &&
130
0
                  out->alternates);
131
0
  }
132
};
133
134
}
135
}
136
}
137
138
139
#endif /* OT_LAYOUT_GSUB_ALTERNATESET_HH */