Coverage Report

Created: 2026-10-22 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/OT/Layout/GSUB/Sequence.hh
Line
Count
Source
1
#ifndef OT_LAYOUT_GSUB_SEQUENCE_HH
2
#define OT_LAYOUT_GSUB_SEQUENCE_HH
3
4
#include "Common.hh"
5
6
namespace OT {
7
namespace Layout {
8
namespace GSUB_impl {
9
10
template <typename Types>
11
struct Sequence
12
{
13
  protected:
14
  Array16Of<typename Types::HBGlyphID>
15
                substitute;             /* String of GlyphIDs to substitute */
16
  public:
17
  DEFINE_SIZE_ARRAY (2, substitute);
18
19
  bool sanitize (hb_sanitize_context_t *c) const
20
0
  {
21
0
    TRACE_SANITIZE (this);
22
0
    return_trace (substitute.sanitize (c));
23
0
  }
24
25
  bool intersects (const hb_set_t *glyphs) const
26
0
  { return hb_all (substitute, glyphs); }
27
28
  void closure (hb_closure_context_t *c) const
29
0
  { c->output->add_array (substitute.arrayZ, substitute.len); }
30
31
  void depend (hb_depend_context_t *c, hb_codepoint_t source) const
32
0
  {
33
0
    + hb_iter (substitute)
34
0
    | hb_apply ([&] (const hb_codepoint_t &target) { c->depend_data->add_gsub_lookup (source, c->lookup_index, target); })
35
0
    ;
36
0
  }
37
38
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
39
0
  { c->output->add_array (substitute.arrayZ, substitute.len); }
40
41
  bool apply (hb_ot_apply_context_t *c) const
42
0
  {
43
0
    TRACE_APPLY (this);
44
0
    unsigned int count = substitute.len;
45
46
    /* Special-case to make it in-place and not consider this
47
     * as a "multiplied" substitution. */
48
0
    if (unlikely (count == 1))
49
0
    {
50
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
51
0
      {
52
0
  c->buffer->sync_so_far ();
53
0
  c->buffer->message (c->font,
54
0
          "replacing glyph at %u (multiple substitution)",
55
0
          c->buffer->idx);
56
0
      }
57
58
0
      c->replace_glyph (substitute.arrayZ[0]);
59
60
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
61
0
      {
62
0
  c->buffer->message (c->font,
63
0
          "replaced glyph at %u (multiple substitution)",
64
0
          c->buffer->idx - 1u);
65
0
      }
66
67
0
      return_trace (true);
68
0
    }
69
    /* Spec disallows this, but Uniscribe allows it.
70
     * https://github.com/harfbuzz/harfbuzz/issues/253 */
71
0
    else if (unlikely (count == 0))
72
0
    {
73
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
74
0
      {
75
0
  c->buffer->sync_so_far ();
76
0
  c->buffer->message (c->font,
77
0
          "deleting glyph at %u (multiple substitution)",
78
0
          c->buffer->idx);
79
0
      }
80
81
0
      c->buffer->delete_glyph ();
82
83
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
84
0
      {
85
0
  c->buffer->sync_so_far ();
86
0
  c->buffer->message (c->font,
87
0
          "deleted glyph at %u (multiple substitution)",
88
0
          c->buffer->idx);
89
0
      }
90
91
0
      return_trace (true);
92
0
    }
93
94
0
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
95
0
    {
96
0
      c->buffer->sync_so_far ();
97
0
      c->buffer->message (c->font,
98
0
        "multiplying glyph at %u",
99
0
        c->buffer->idx);
100
0
    }
101
102
0
    unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
103
0
                         HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;
104
0
    unsigned lig_id = _hb_glyph_info_get_lig_id (&c->buffer->cur());
105
106
0
    for (unsigned int i = 0; i < count; i++)
107
0
    {
108
      /* If is attached to a ligature, don't disturb that.
109
       * https://github.com/harfbuzz/harfbuzz/issues/3069 */
110
0
      if (!lig_id)
111
0
        _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
112
0
      c->output_glyph_for_component (substitute.arrayZ[i], klass);
113
0
    }
114
0
    c->buffer->skip_glyph ();
115
116
0
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
117
0
    {
118
0
      c->buffer->sync_so_far ();
119
120
0
      char buf[HB_MAX_CONTEXT_LENGTH * 16] = {0};
121
0
      char *p = buf;
122
123
0
      for (unsigned i = c->buffer->idx - count; i < c->buffer->idx; i++)
124
0
      {
125
0
  if (buf < p && sizeof(buf) - 1u > unsigned (p - buf))
126
0
    *p++ = ',';
127
0
  snprintf (p, sizeof(buf) - (p - buf), "%u", i);
128
0
  p += strlen(p);
129
0
      }
130
131
0
      c->buffer->message (c->font,
132
0
        "multiplied glyphs at %s",
133
0
        buf);
134
0
    }
135
136
0
    return_trace (true);
137
0
  }
138
139
  template <typename Iterator,
140
            hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
141
  bool serialize (hb_serialize_context_t *c,
142
                  Iterator subst)
143
0
  {
144
0
    TRACE_SERIALIZE (this);
145
0
    return_trace (substitute.serialize (c, subst));
146
0
  }
147
148
  bool subset (hb_subset_context_t *c) const
149
0
  {
150
0
    TRACE_SUBSET (this);
151
0
    const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
152
0
    const hb_map_t &glyph_map = *c->plan->glyph_map;
153
0
154
0
    if (!intersects (&glyphset)) return_trace (false);
155
0
156
0
    auto it =
157
0
    + hb_iter (substitute)
158
0
    | hb_map (glyph_map)
159
0
    ;
160
0
161
0
    auto *out = c->serializer->start_embed (*this);
162
0
    return_trace (out->serialize (c->serializer, it));
163
0
  }
164
};
165
166
167
}
168
}
169
}
170
171
172
#endif /* OT_LAYOUT_GSUB_SEQUENCE_HH */