Coverage Report

Created: 2023-06-07 06:14

/src/harfbuzz/src/OT/Layout/GPOS/PairSet.hh
Line
Count
Source (jump to first uncovered line)
1
#ifndef OT_LAYOUT_GPOS_PAIRSET_HH
2
#define OT_LAYOUT_GPOS_PAIRSET_HH
3
4
#include "PairValueRecord.hh"
5
6
namespace OT {
7
namespace Layout {
8
namespace GPOS_impl {
9
10
11
template <typename Types>
12
struct PairSet
13
{
14
  template <typename Types2>
15
  friend struct PairPosFormat1_3;
16
17
  using PairValueRecord = GPOS_impl::PairValueRecord<Types>;
18
19
  protected:
20
  HBUINT16              len;    /* Number of PairValueRecords */
21
  PairValueRecord       firstPairValueRecord;
22
                                /* Array of PairValueRecords--ordered
23
                                 * by GlyphID of the second glyph */
24
  public:
25
  DEFINE_SIZE_MIN (2);
26
27
  static unsigned get_size (unsigned len1, unsigned len2)
28
0
  {
29
0
    return Types::HBGlyphID::static_size + Value::static_size * (len1 + len2);
30
0
  }
31
  static unsigned get_size (const ValueFormat valueFormats[2])
32
0
  {
33
0
    unsigned len1 = valueFormats[0].get_len ();
34
0
    unsigned len2 = valueFormats[1].get_len ();
35
0
    return get_size (len1, len2);
36
0
  }
37
38
  struct sanitize_closure_t
39
  {
40
    const ValueFormat *valueFormats;
41
    unsigned int len1; /* valueFormats[0].get_len() */
42
    unsigned int stride; /* bytes */
43
  };
44
45
  bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
46
0
  {
47
0
    TRACE_SANITIZE (this);
48
0
    if (!(c->check_struct (this)
49
0
       && c->check_range (&firstPairValueRecord,
50
0
                          len,
51
0
                          closure->stride))) return_trace (false);
52
53
0
    unsigned int count = len;
54
0
    const PairValueRecord *record = &firstPairValueRecord;
55
0
    return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, this, &record->values[0], count, closure->stride) &&
56
0
                  closure->valueFormats[1].sanitize_values_stride_unsafe (c, this, &record->values[closure->len1], count, closure->stride));
57
0
  }
58
59
  bool intersects (const hb_set_t *glyphs,
60
                   const ValueFormat *valueFormats) const
61
0
  {
62
0
    unsigned record_size = get_size (valueFormats);
63
0
64
0
    const PairValueRecord *record = &firstPairValueRecord;
65
0
    unsigned int count = len;
66
0
    for (unsigned int i = 0; i < count; i++)
67
0
    {
68
0
      if (glyphs->has (record->secondGlyph))
69
0
        return true;
70
0
      record = &StructAtOffset<const PairValueRecord> (record, record_size);
71
0
    }
72
0
    return false;
73
0
  }
74
75
  void collect_glyphs (hb_collect_glyphs_context_t *c,
76
                       const ValueFormat *valueFormats) const
77
0
  {
78
0
    unsigned record_size = get_size (valueFormats);
79
80
0
    const PairValueRecord *record = &firstPairValueRecord;
81
0
    c->input->add_array (&record->secondGlyph, len, record_size);
82
0
  }
83
84
  void collect_variation_indices (hb_collect_variation_indices_context_t *c,
85
                                  const ValueFormat *valueFormats) const
86
0
  {
87
0
    unsigned record_size = get_size (valueFormats);
88
0
89
0
    const PairValueRecord *record = &firstPairValueRecord;
90
0
    unsigned count = len;
91
0
    for (unsigned i = 0; i < count; i++)
92
0
    {
93
0
      if (c->glyph_set->has (record->secondGlyph))
94
0
      { record->collect_variation_indices (c, valueFormats, this); }
95
0
96
0
      record = &StructAtOffset<const PairValueRecord> (record, record_size);
97
0
    }
98
0
  }
99
100
  bool apply (hb_ot_apply_context_t *c,
101
              const ValueFormat *valueFormats,
102
              unsigned int pos) const
103
0
  {
104
0
    TRACE_APPLY (this);
105
0
    hb_buffer_t *buffer = c->buffer;
106
0
    unsigned int len1 = valueFormats[0].get_len ();
107
0
    unsigned int len2 = valueFormats[1].get_len ();
108
0
    unsigned record_size = get_size (len1, len2);
109
110
0
    const PairValueRecord *record = hb_bsearch (buffer->info[pos].codepoint,
111
0
                                                &firstPairValueRecord,
112
0
                                                len,
113
0
                                                record_size);
114
0
    if (record)
115
0
    {
116
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
117
0
      {
118
0
  c->buffer->message (c->font,
119
0
          "try kerning glyphs at %u,%u",
120
0
          c->buffer->idx, pos);
121
0
      }
122
123
0
      bool applied_first = len1 && valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos());
124
0
      bool applied_second = len2 && valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]);
125
126
0
      if (applied_first || applied_second)
127
0
  if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
128
0
  {
129
0
    c->buffer->message (c->font,
130
0
            "kerned glyphs at %u,%u",
131
0
            c->buffer->idx, pos);
132
0
  }
133
134
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
135
0
      {
136
0
  c->buffer->message (c->font,
137
0
          "tried kerning glyphs at %u,%u",
138
0
          c->buffer->idx, pos);
139
0
      }
140
141
0
      if (applied_first || applied_second)
142
0
        buffer->unsafe_to_break (buffer->idx, pos + 1);
143
144
0
      if (len2)
145
0
      {
146
0
  pos++;
147
      // https://github.com/harfbuzz/harfbuzz/issues/3824
148
      // https://github.com/harfbuzz/harfbuzz/issues/3888#issuecomment-1326781116
149
0
      buffer->unsafe_to_break (buffer->idx, pos + 1);
150
0
      }
151
152
0
      buffer->idx = pos;
153
0
      return_trace (true);
154
0
    }
155
0
    buffer->unsafe_to_concat (buffer->idx, pos + 1);
156
0
    return_trace (false);
157
0
  }
158
159
  bool subset (hb_subset_context_t *c,
160
               const ValueFormat valueFormats[2],
161
               const ValueFormat newFormats[2]) const
162
0
  {
163
0
    TRACE_SUBSET (this);
164
0
    auto snap = c->serializer->snapshot ();
165
0
166
0
    auto *out = c->serializer->start_embed (*this);
167
0
    if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
168
0
    out->len = 0;
169
0
170
0
    const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
171
0
    const hb_map_t &glyph_map = *c->plan->glyph_map;
172
0
173
0
    unsigned len1 = valueFormats[0].get_len ();
174
0
    unsigned len2 = valueFormats[1].get_len ();
175
0
    unsigned record_size = get_size (len1, len2);
176
0
177
0
    typename PairValueRecord::context_t context =
178
0
    {
179
0
      this,
180
0
      valueFormats,
181
0
      newFormats,
182
0
      len1,
183
0
      &glyph_map,
184
0
      &c->plan->layout_variation_idx_delta_map
185
0
    };
186
0
187
0
    const PairValueRecord *record = &firstPairValueRecord;
188
0
    unsigned count = len, num = 0;
189
0
    for (unsigned i = 0; i < count; i++)
190
0
    {
191
0
      if (glyphset.has (record->secondGlyph)
192
0
         && record->subset (c, &context)) num++;
193
0
      record = &StructAtOffset<const PairValueRecord> (record, record_size);
194
0
    }
195
0
196
0
    out->len = num;
197
0
    if (!num) c->serializer->revert (snap);
198
0
    return_trace (num);
199
0
  }
200
};
201
202
203
}
204
}
205
}
206
207
#endif  // OT_LAYOUT_GPOS_PAIRSET_HH