Coverage Report

Created: 2026-12-01 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/OT/Layout/GSUB/Ligature.hh
Line
Count
Source
1
#ifndef OT_LAYOUT_GSUB_LIGATURE_HH
2
#define OT_LAYOUT_GSUB_LIGATURE_HH
3
4
#include "Common.hh"
5
6
namespace OT {
7
namespace Layout {
8
namespace GSUB_impl {
9
10
template <typename Types>
11
struct Ligature
12
{
13
  public:
14
  typename Types::HBGlyphID
15
    ligGlyph;               /* GlyphID of ligature to substitute */
16
  HeadlessArray16Of<typename Types::HBGlyphID>
17
    component;              /* Array of component GlyphIDs--start
18
                                         * with the second  component--ordered
19
                                         * in writing direction */
20
  public:
21
  DEFINE_SIZE_ARRAY (Types::size + 2, component);
22
23
  bool sanitize (hb_sanitize_context_t *c) const
24
190k
  {
25
190k
    TRACE_SANITIZE (this);
26
190k
    return_trace (ligGlyph.sanitize (c) && component.sanitize (c));
27
190k
  }
OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::sanitize(hb_sanitize_context_t*) const
Line
Count
Source
24
190k
  {
25
190k
    TRACE_SANITIZE (this);
26
190k
    return_trace (ligGlyph.sanitize (c) && component.sanitize (c));
27
190k
  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::sanitize(hb_sanitize_context_t*) const
28
29
  bool intersects (const hb_set_t *glyphs) const
30
0
  { return hb_all (component, glyphs); }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::intersects(hb_set_t const*) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::intersects(hb_set_t const*) const
31
32
  bool intersects_lig_glyph (const hb_set_t *glyphs) const
33
0
  { return glyphs->has(ligGlyph); }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::intersects_lig_glyph(hb_set_t const*) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::intersects_lig_glyph(hb_set_t const*) const
34
35
  void closure (hb_closure_context_t *c) const
36
0
  {
37
0
    if (unlikely (!c->spend_budget (HB_BUDGET_1, component.lenP1))) return;
38
0
    if (!intersects (c->glyphs)) return;
39
0
    c->add_output (ligGlyph);
40
0
  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::closure(OT::hb_closure_context_t*) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::closure(OT::hb_closure_context_t*) const
41
42
  void depend (hb_depend_context_t *c, hb_codepoint_t first) const
43
0
  {
44
0
    // Build the complete ligature set upfront before adding any edges
45
0
    hb_set_t complete_ligset;
46
0
    complete_ligset.add (first);
47
0
    hb_set_builder_t<> components (complete_ligset);
48
0
    for (const auto &gid : component) components.add (gid);
49
0
    if (unlikely (!components.flush ()))
50
0
    {
51
0
      c->depend_data->fail ();
52
0
      return;
53
0
    }
54
0
55
0
    bool ligset_created;
56
0
    hb_codepoint_t ligset_idx = c->depend_data->find_or_create_set (complete_ligset,
57
0
                                                                    &ligset_created);
58
0
    if (unlikely (ligset_idx == HB_CODEPOINT_INVALID))
59
0
      return;
60
0
61
0
    // Track whether any edge using this ligset_idx was actually added
62
0
    bool any_added = false;
63
0
64
0
    // Now add one edge for each glyph in the complete, immutable set
65
0
    + hb_iter (complete_ligset)
66
0
    | hb_apply ([&] (hb_codepoint_t gid) {
67
0
        if (c->depend_data->add_gsub_lookup (gid, c->lookup_index, ligGlyph, ligset_idx))
68
0
          any_added = true;
69
0
      })
70
0
    ;
71
0
72
0
    // If no edges were added, a newly allocated set is unused - free it for reuse
73
0
    if (!any_added && ligset_created)
74
0
      c->depend_data->discard_set (ligset_idx);
75
0
  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::depend(OT::hb_depend_context_t*, unsigned int) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::depend(OT::hb_depend_context_t*, unsigned int) const
76
77
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
78
0
  {
79
0
    c->input->add_array (component.arrayZ, component.get_length ());
80
0
    c->output->add (ligGlyph);
81
0
  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::collect_glyphs(OT::hb_collect_glyphs_context_t*) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::collect_glyphs(OT::hb_collect_glyphs_context_t*) const
82
83
  template <typename set_t>
84
  void collect_second (set_t &s) const
85
49.0k
  {
86
49.0k
    if (unlikely (!component.get_length ()))
87
0
    {
88
      // A ligature without any components. Anything matches.
89
0
      s = set_t::full ();
90
0
      return;
91
0
    }
92
49.0k
    s.add (component.arrayZ[0]);
93
49.0k
  }
void OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::collect_second<hb_set_digest_t>(hb_set_digest_t&) const
Line
Count
Source
85
49.0k
  {
86
49.0k
    if (unlikely (!component.get_length ()))
87
0
    {
88
      // A ligature without any components. Anything matches.
89
0
      s = set_t::full ();
90
0
      return;
91
0
    }
92
49.0k
    s.add (component.arrayZ[0]);
93
49.0k
  }
Unexecuted instantiation: void OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::collect_second<hb_set_digest_t>(hb_set_digest_t&) const
94
95
  bool would_apply (hb_would_apply_context_t *c) const
96
0
  {
97
0
    if (c->len != component.lenP1)
98
0
      return false;
99
100
0
    for (unsigned int i = 1; i < c->len; i++)
101
0
      if (likely (c->glyphs[i] != component[i]))
102
0
        return false;
103
104
0
    return true;
105
0
  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::would_apply(OT::hb_would_apply_context_t*) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::would_apply(OT::hb_would_apply_context_t*) const
106
107
  bool apply (hb_ot_apply_context_t *c) const
108
74.4k
  {
109
74.4k
    TRACE_APPLY (this);
110
111
74.4k
    if (unlikely (!c->buffer->successful)) return_trace (false);
112
113
74.4k
    unsigned int count = component.lenP1;
114
115
74.4k
    if (unlikely (!count)) return_trace (false);
116
117
    /* Special-case to make it in-place and not consider this
118
     * as a "ligated" substitution. */
119
74.4k
    if (unlikely (count == 1))
120
0
    {
121
122
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
123
0
      {
124
0
  c->buffer->sync_so_far ();
125
0
  c->buffer->message (c->font,
126
0
          "replacing glyph at %u (ligature substitution)",
127
0
          c->buffer->idx);
128
0
      }
129
130
0
      c->replace_glyph (ligGlyph);
131
132
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
133
0
      {
134
0
  c->buffer->message (c->font,
135
0
          "replaced glyph at %u (ligature substitution)",
136
0
          c->buffer->idx - 1u);
137
0
      }
138
139
0
      return_trace (true);
140
0
    }
141
142
74.4k
    unsigned int total_component_count = 0;
143
144
74.4k
    if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return false;
145
74.4k
    unsigned int match_end = 0;
146
147
74.4k
    if (likely (!match_input (c, count,
148
74.4k
                              &component[1],
149
74.4k
                              match_glyph,
150
74.4k
                              nullptr,
151
74.4k
                              &match_end,
152
74.4k
                              &total_component_count)))
153
61.3k
    {
154
61.3k
      c->buffer->unsafe_to_concat (c->buffer->idx, match_end);
155
61.3k
      return_trace (false);
156
61.3k
    }
157
158
13.1k
    unsigned pos = 0;
159
13.1k
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
160
0
    {
161
0
      unsigned delta = c->buffer->sync_so_far ();
162
163
0
      pos = c->buffer->idx;
164
165
0
      char buf[HB_MAX_CONTEXT_LENGTH * 16] = {0};
166
0
      char *p = buf;
167
168
0
      match_end += delta;
169
0
      for (unsigned i = 0; i < count; i++)
170
0
      {
171
0
  c->match_positions[i] += delta;
172
0
  if (i)
173
0
    *p++ = ',';
174
0
  snprintf (p, sizeof(buf) - (p - buf), "%u", c->match_positions[i]);
175
0
  p += strlen(p);
176
0
      }
177
178
0
      c->buffer->message (c->font,
179
0
        "ligating glyphs at %s",
180
0
        buf);
181
0
    }
182
183
13.1k
    ligate_input (c,
184
13.1k
                  count,
185
13.1k
                  match_end,
186
13.1k
                  ligGlyph,
187
13.1k
                  total_component_count);
188
189
13.1k
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
190
0
    {
191
0
      c->buffer->sync_so_far ();
192
0
      c->buffer->message (c->font,
193
0
        "ligated glyph at %u",
194
0
        pos);
195
0
    }
196
197
13.1k
    return_trace (true);
198
74.4k
  }
OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::apply(OT::hb_ot_apply_context_t*) const
Line
Count
Source
108
74.4k
  {
109
74.4k
    TRACE_APPLY (this);
110
111
74.4k
    if (unlikely (!c->buffer->successful)) return_trace (false);
112
113
74.4k
    unsigned int count = component.lenP1;
114
115
74.4k
    if (unlikely (!count)) return_trace (false);
116
117
    /* Special-case to make it in-place and not consider this
118
     * as a "ligated" substitution. */
119
74.4k
    if (unlikely (count == 1))
120
0
    {
121
122
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
123
0
      {
124
0
  c->buffer->sync_so_far ();
125
0
  c->buffer->message (c->font,
126
0
          "replacing glyph at %u (ligature substitution)",
127
0
          c->buffer->idx);
128
0
      }
129
130
0
      c->replace_glyph (ligGlyph);
131
132
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
133
0
      {
134
0
  c->buffer->message (c->font,
135
0
          "replaced glyph at %u (ligature substitution)",
136
0
          c->buffer->idx - 1u);
137
0
      }
138
139
0
      return_trace (true);
140
0
    }
141
142
74.4k
    unsigned int total_component_count = 0;
143
144
74.4k
    if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return false;
145
74.4k
    unsigned int match_end = 0;
146
147
74.4k
    if (likely (!match_input (c, count,
148
74.4k
                              &component[1],
149
74.4k
                              match_glyph,
150
74.4k
                              nullptr,
151
74.4k
                              &match_end,
152
74.4k
                              &total_component_count)))
153
61.3k
    {
154
61.3k
      c->buffer->unsafe_to_concat (c->buffer->idx, match_end);
155
61.3k
      return_trace (false);
156
61.3k
    }
157
158
13.1k
    unsigned pos = 0;
159
13.1k
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
160
0
    {
161
0
      unsigned delta = c->buffer->sync_so_far ();
162
163
0
      pos = c->buffer->idx;
164
165
0
      char buf[HB_MAX_CONTEXT_LENGTH * 16] = {0};
166
0
      char *p = buf;
167
168
0
      match_end += delta;
169
0
      for (unsigned i = 0; i < count; i++)
170
0
      {
171
0
  c->match_positions[i] += delta;
172
0
  if (i)
173
0
    *p++ = ',';
174
0
  snprintf (p, sizeof(buf) - (p - buf), "%u", c->match_positions[i]);
175
0
  p += strlen(p);
176
0
      }
177
178
0
      c->buffer->message (c->font,
179
0
        "ligating glyphs at %s",
180
0
        buf);
181
0
    }
182
183
13.1k
    ligate_input (c,
184
13.1k
                  count,
185
13.1k
                  match_end,
186
13.1k
                  ligGlyph,
187
13.1k
                  total_component_count);
188
189
13.1k
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
190
0
    {
191
0
      c->buffer->sync_so_far ();
192
0
      c->buffer->message (c->font,
193
0
        "ligated glyph at %u",
194
0
        pos);
195
0
    }
196
197
13.1k
    return_trace (true);
198
74.4k
  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::apply(OT::hb_ot_apply_context_t*) const
199
200
  template <typename Iterator,
201
            hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
202
  bool serialize (hb_serialize_context_t *c,
203
                  hb_codepoint_t ligature,
204
                  Iterator components /* Starting from second */)
205
0
  {
206
0
    TRACE_SERIALIZE (this);
207
0
    if (unlikely (!c->extend_min (this))) return_trace (false);
208
0
    ligGlyph = ligature;
209
0
    if (unlikely (!component.serialize (c, components))) return_trace (false);
210
0
    return_trace (true);
211
0
  }
Unexecuted instantiation: _ZN2OT6Layout9GSUB_impl8LigatureINS0_10SmallTypesEE9serializeI13hb_map_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_map_tL24hb_function_sortedness_t0ELPv0EETnPNSt3__19enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSF_0EEEbP22hb_serialize_context_tjSJ_
Unexecuted instantiation: _ZN2OT6Layout9GSUB_impl8LigatureINS0_11MediumTypesEE9serializeI13hb_map_iter_tI10hb_array_tIKNS_11HBGlyphID24EERK8hb_map_tL24hb_function_sortedness_t0ELPv0EETnPNSt3__19enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSF_0EEEbP22hb_serialize_context_tjSJ_
Unexecuted instantiation: _ZN2OT6Layout9GSUB_impl8LigatureINS0_10SmallTypesEE9serializeI10hb_array_tINS_11HBGlyphID16EETnPNSt3__19enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELPv0EEEbP22hb_serialize_context_tjSB_
212
213
  bool subset (hb_subset_context_t *c, unsigned coverage_idx) const
214
0
  {
215
0
    TRACE_SUBSET (this);
216
0
    const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
217
0
    const hb_map_t &glyph_map = *c->plan->glyph_map;
218
0
219
0
    if (!intersects (&glyphset) || !glyphset.has (ligGlyph)) return_trace (false);
220
0
    // Ensure Coverage table is always packed after this.
221
0
    c->serializer->add_virtual_link (coverage_idx);
222
0
223
0
    auto it =
224
0
      + hb_iter (component)
225
0
      | hb_map (glyph_map)
226
0
      ;
227
0
228
0
    auto *out = c->serializer->start_embed (*this);
229
0
    return_trace (out->serialize (c->serializer,
230
0
                                  glyph_map[ligGlyph],
231
0
                                  it));  }
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::SmallTypes>::subset(hb_subset_context_t*, unsigned int) const
Unexecuted instantiation: OT::Layout::GSUB_impl::Ligature<OT::Layout::MediumTypes>::subset(hb_subset_context_t*, unsigned int) const
232
};
233
234
235
}
236
}
237
}
238
239
#endif  /* OT_LAYOUT_GSUB_LIGATURE_HH */