Coverage Report

Created: 2025-10-10 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/OT/Layout/GPOS/PairPosFormat2.hh
Line
Count
Source
1
#ifndef OT_LAYOUT_GPOS_PAIRPOSFORMAT2_HH
2
#define OT_LAYOUT_GPOS_PAIRPOSFORMAT2_HH
3
4
#include "ValueFormat.hh"
5
6
namespace OT {
7
namespace Layout {
8
namespace GPOS_impl {
9
10
template <typename Types>
11
struct PairPosFormat2_4 : ValueBase
12
{
13
  protected:
14
  HBUINT16      format;                 /* Format identifier--format = 2 */
15
  typename Types::template OffsetTo<Coverage>
16
                coverage;               /* Offset to Coverage table--from
17
                                         * beginning of subtable */
18
  ValueFormat   valueFormat1;           /* ValueRecord definition--for the
19
                                         * first glyph of the pair--may be zero
20
                                         * (0) */
21
  ValueFormat   valueFormat2;           /* ValueRecord definition--for the
22
                                         * second glyph of the pair--may be
23
                                         * zero (0) */
24
  typename Types::template OffsetTo<ClassDef>
25
                classDef1;              /* Offset to ClassDef table--from
26
                                         * beginning of PairPos subtable--for
27
                                         * the first glyph of the pair */
28
  typename Types::template OffsetTo<ClassDef>
29
                classDef2;              /* Offset to ClassDef table--from
30
                                         * beginning of PairPos subtable--for
31
                                         * the second glyph of the pair */
32
  HBUINT16      class1Count;            /* Number of classes in ClassDef1
33
                                         * table--includes Class0 */
34
  HBUINT16      class2Count;            /* Number of classes in ClassDef2
35
                                         * table--includes Class0 */
36
  ValueRecord   values;                 /* Matrix of value pairs:
37
                                         * class1-major, class2-minor,
38
                                         * Each entry has value1 and value2 */
39
  public:
40
  DEFINE_SIZE_ARRAY (10 + 3 * Types::size, values);
41
42
  bool sanitize (hb_sanitize_context_t *c) const
43
17
  {
44
17
    TRACE_SANITIZE (this);
45
17
    if (!(c->check_struct (this)
46
17
       && coverage.sanitize (c, this)
47
17
       && classDef1.sanitize (c, this)
48
17
       && classDef2.sanitize (c, this))) return_trace (false);
49
50
17
    unsigned int len1 = valueFormat1.get_len ();
51
17
    unsigned int len2 = valueFormat2.get_len ();
52
17
    unsigned int stride = HBUINT16::static_size * (len1 + len2);
53
17
    unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
54
17
    return_trace (c->check_range ((const void *) values,
55
17
                                  count,
56
17
                                  stride) &&
57
17
      (c->lazy_some_gpos ||
58
17
       (valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
59
17
        valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride))));
60
17
  }
61
62
  bool intersects (const hb_set_t *glyphs) const
63
0
  {
64
0
    return (this+coverage).intersects (glyphs) &&
65
0
           (this+classDef2).intersects (glyphs);
66
0
  }
67
68
0
  void closure_lookups (hb_closure_lookups_context_t *c) const {}
69
  void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
70
0
  {
71
0
    if (!intersects (c->glyph_set)) return;
72
0
    if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return;
73
0
74
0
    hb_set_t klass1_glyphs, klass2_glyphs;
75
0
    if (!(this+classDef1).collect_coverage (&klass1_glyphs)) return;
76
0
    if (!(this+classDef2).collect_coverage (&klass2_glyphs)) return;
77
0
78
0
    hb_set_t class1_set, class2_set;
79
0
    for (const unsigned cp : + c->glyph_set->iter () | hb_filter (this + coverage))
80
0
    {
81
0
      if (!klass1_glyphs.has (cp)) class1_set.add (0);
82
0
      else
83
0
      {
84
0
        unsigned klass1 = (this+classDef1).get (cp);
85
0
        class1_set.add (klass1);
86
0
      }
87
0
    }
88
0
89
0
    class2_set.add (0);
90
0
    for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs))
91
0
    {
92
0
      unsigned klass2 = (this+classDef2).get (cp);
93
0
      class2_set.add (klass2);
94
0
    }
95
0
96
0
    if (class1_set.is_empty ()
97
0
        || class2_set.is_empty ()
98
0
        || (class2_set.get_population() == 1 && class2_set.has(0)))
99
0
      return;
100
0
101
0
    unsigned len1 = valueFormat1.get_len ();
102
0
    unsigned len2 = valueFormat2.get_len ();
103
0
    const hb_array_t<const Value> values_array = values.as_array ((unsigned)class1Count * (unsigned) class2Count * (len1 + len2));
104
0
    for (const unsigned class1_idx : class1_set.iter ())
105
0
    {
106
0
      for (const unsigned class2_idx : class2_set.iter ())
107
0
      {
108
0
        unsigned start_offset = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2);
109
0
        if (valueFormat1.has_device ())
110
0
          valueFormat1.collect_variation_indices (c, this, values_array.sub_array (start_offset, len1));
111
0
112
0
        if (valueFormat2.has_device ())
113
0
          valueFormat2.collect_variation_indices (c, this, values_array.sub_array (start_offset+len1, len2));
114
0
      }
115
0
    }
116
0
  }
117
118
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
119
0
  {
120
0
    if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
121
0
    if (unlikely (!(this+classDef2).collect_coverage (c->input))) return;
122
0
  }
123
124
0
  const Coverage &get_coverage () const { return this+coverage; }
125
126
  struct external_cache_t
127
  {
128
    hb_ot_layout_mapping_cache_t coverage;
129
    hb_ot_layout_mapping_cache_t first;
130
    hb_ot_layout_mapping_cache_t second;
131
  };
132
  void *external_cache_create () const
133
0
  {
134
0
    external_cache_t *cache = (external_cache_t *) hb_malloc (sizeof (external_cache_t));
135
0
    if (likely (cache))
136
0
    {
137
0
      cache->coverage.clear ();
138
0
      cache->first.clear ();
139
0
      cache->second.clear ();
140
0
    }
141
0
    return cache;
142
0
  }
143
144
  bool apply (hb_ot_apply_context_t *c, void *external_cache) const
145
0
  {
146
0
    TRACE_APPLY (this);
147
148
0
    hb_buffer_t *buffer = c->buffer;
149
150
0
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
151
0
    external_cache_t *cache = (external_cache_t *) external_cache;
152
0
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint, cache ? &cache->coverage : nullptr);
153
#else
154
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
155
#endif
156
0
    if (index == NOT_COVERED) return_trace (false);
157
158
0
    auto &skippy_iter = c->iter_input;
159
0
    skippy_iter.reset_fast (buffer->idx);
160
0
    unsigned unsafe_to;
161
0
    if (unlikely (!skippy_iter.next (&unsafe_to)))
162
0
    {
163
0
      buffer->unsafe_to_concat (buffer->idx, unsafe_to);
164
0
      return_trace (false);
165
0
    }
166
167
0
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
168
0
    unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint, cache ? &cache->first : nullptr);
169
0
    unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint, cache ? &cache->second : nullptr);
170
#else
171
    unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
172
    unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
173
#endif
174
0
    if (unlikely (klass1 >= class1Count || klass2 >= class2Count))
175
0
    {
176
0
      buffer->unsafe_to_concat (buffer->idx, skippy_iter.idx + 1);
177
0
      return_trace (false);
178
0
    }
179
180
0
    unsigned int len1 = valueFormat1.get_len ();
181
0
    unsigned int len2 = valueFormat2.get_len ();
182
0
    unsigned int record_len = len1 + len2;
183
184
0
    const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
185
186
0
    bool applied_first = false, applied_second = false;
187
188
189
    /* Isolate simple kerning and apply it half to each side.
190
     * Results in better cursor positioning / underline drawing.
191
     *
192
     * Disabled, because causes issues... :-(
193
     * https://github.com/harfbuzz/harfbuzz/issues/3408
194
     * https://github.com/harfbuzz/harfbuzz/pull/3235#issuecomment-1029814978
195
     */
196
0
#ifndef HB_SPLIT_KERN
197
0
    if (false)
198
0
#endif
199
0
    {
200
0
      if (!len2)
201
0
      {
202
0
        const hb_direction_t dir = buffer->props.direction;
203
0
        const bool horizontal = HB_DIRECTION_IS_HORIZONTAL (dir);
204
0
        const bool backward = HB_DIRECTION_IS_BACKWARD (dir);
205
0
        unsigned mask = horizontal ? ValueFormat::xAdvance : ValueFormat::yAdvance;
206
0
        if (backward)
207
0
          mask |= mask >> 2; /* Add eg. xPlacement in RTL. */
208
        /* Add Devices. */
209
0
        mask |= mask << 4;
210
211
0
        if (valueFormat1 & ~mask)
212
0
          goto bail;
213
214
        /* Is simple kern. Apply value on an empty position slot,
215
         * then split it between sides. */
216
217
0
        hb_glyph_position_t pos{};
218
0
        if (valueFormat1.apply_value (c, this, v, pos))
219
0
        {
220
0
          hb_position_t *src  = &pos.x_advance;
221
0
          hb_position_t *dst1 = &buffer->cur_pos().x_advance;
222
0
          hb_position_t *dst2 = &buffer->pos[skippy_iter.idx].x_advance;
223
0
          unsigned i = horizontal ? 0 : 1;
224
225
0
          hb_position_t kern  = src[i];
226
0
          hb_position_t kern1 = kern >> 1;
227
0
          hb_position_t kern2 = kern - kern1;
228
229
0
          if (!backward)
230
0
          {
231
0
            dst1[i] += kern1;
232
0
            dst2[i] += kern2;
233
0
            dst2[i + 2] += kern2;
234
0
          }
235
0
          else
236
0
          {
237
0
            dst1[i] += kern1;
238
0
            dst1[i + 2] += src[i + 2] - kern2;
239
0
            dst2[i] += kern2;
240
0
          }
241
242
0
          applied_first = applied_second = kern != 0;
243
0
          goto success;
244
0
        }
245
0
        goto boring;
246
0
      }
247
0
    }
248
0
    bail:
249
250
0
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
251
0
    {
252
0
      c->buffer->message (c->font,
253
0
        "try kerning glyphs at %u,%u",
254
0
        c->buffer->idx, skippy_iter.idx);
255
0
    }
256
257
0
    applied_first = len1 && valueFormat1.apply_value (c, this, v, buffer->cur_pos());
258
0
    applied_second = len2 && valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]);
259
260
0
    if (applied_first || applied_second)
261
0
      if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
262
0
      {
263
0
  c->buffer->message (c->font,
264
0
          "kerned glyphs at %u,%u",
265
0
          c->buffer->idx, skippy_iter.idx);
266
0
      }
267
268
0
    if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
269
0
    {
270
0
      c->buffer->message (c->font,
271
0
        "tried kerning glyphs at %u,%u",
272
0
        c->buffer->idx, skippy_iter.idx);
273
0
    }
274
275
0
    success:
276
0
    if (applied_first || applied_second)
277
0
      buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
278
0
    else
279
0
    boring:
280
0
      buffer->unsafe_to_concat (buffer->idx, skippy_iter.idx + 1);
281
282
0
    if (len2)
283
0
    {
284
0
      skippy_iter.idx++;
285
      // https://github.com/harfbuzz/harfbuzz/issues/3824
286
      // https://github.com/harfbuzz/harfbuzz/issues/3888#issuecomment-1326781116
287
0
      buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
288
0
    }
289
290
0
    buffer->idx = skippy_iter.idx;
291
292
0
    return_trace (true);
293
0
  }
294
295
  bool subset (hb_subset_context_t *c) const
296
0
  {
297
0
    TRACE_SUBSET (this);
298
0
    auto *out = c->serializer->start_embed (*this);
299
0
    if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
300
0
    out->format = format;
301
0
302
0
    hb_map_t klass1_map;
303
0
    out->classDef1.serialize_subset (c, classDef1, this, &klass1_map, true, true, &(this + coverage));
304
0
    out->class1Count = klass1_map.get_population ();
305
0
306
0
    hb_map_t klass2_map;
307
0
    out->classDef2.serialize_subset (c, classDef2, this, &klass2_map, true, false);
308
0
    out->class2Count = klass2_map.get_population ();
309
0
310
0
    unsigned len1 = valueFormat1.get_len ();
311
0
    unsigned len2 = valueFormat2.get_len ();
312
0
313
0
    hb_pair_t<unsigned, unsigned> newFormats = hb_pair (valueFormat1, valueFormat2);
314
0
315
0
    if (c->plan->normalized_coords)
316
0
    {
317
0
      /* in case of full instancing, all var device flags will be dropped so no
318
0
       * need to strip hints here */
319
0
      newFormats = compute_effective_value_formats (klass1_map, klass2_map, false, false, &c->plan->layout_variation_idx_delta_map);
320
0
    }
321
0
    /* do not strip hints for VF */
322
0
    else if (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
323
0
    {
324
0
      hb_blob_t* blob = hb_face_reference_table (c->plan->source, HB_TAG ('f','v','a','r'));
325
0
      bool has_fvar = (blob != hb_blob_get_empty ());
326
0
      hb_blob_destroy (blob);
327
0
328
0
      bool strip = !has_fvar;
329
0
      /* special case: strip hints when a VF has no GDEF varstore after
330
0
       * subsetting*/
331
0
      if (has_fvar && !c->plan->has_gdef_varstore)
332
0
        strip = true;
333
0
      newFormats = compute_effective_value_formats (klass1_map, klass2_map, strip, true);
334
0
    }
335
0
336
0
    out->valueFormat1 = newFormats.first;
337
0
    out->valueFormat2 = newFormats.second;
338
0
339
0
    unsigned total_len = len1 + len2;
340
0
    hb_vector_t<unsigned> class2_idxs (+ hb_range ((unsigned) class2Count) | hb_filter (klass2_map));
341
0
    for (unsigned class1_idx : + hb_range ((unsigned) class1Count) | hb_filter (klass1_map))
342
0
    {
343
0
      for (unsigned class2_idx : class2_idxs)
344
0
      {
345
0
        unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * total_len;
346
0
        valueFormat1.copy_values (c->serializer, out->valueFormat1, this, &values[idx], &c->plan->layout_variation_idx_delta_map);
347
0
        valueFormat2.copy_values (c->serializer, out->valueFormat2, this, &values[idx + len1], &c->plan->layout_variation_idx_delta_map);
348
0
      }
349
0
    }
350
0
351
0
    bool ret = out->coverage.serialize_subset(c, coverage, this);
352
0
    return_trace (out->class1Count && out->class2Count && ret);
353
0
  }
354
355
356
  hb_pair_t<unsigned, unsigned> compute_effective_value_formats (const hb_map_t& klass1_map,
357
                                                                 const hb_map_t& klass2_map,
358
                                                                 bool strip_hints, bool strip_empty,
359
                                                                 const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *varidx_delta_map = nullptr) const
360
0
  {
361
0
    unsigned len1 = valueFormat1.get_len ();
362
0
    unsigned len2 = valueFormat2.get_len ();
363
0
    unsigned record_size = len1 + len2;
364
0
365
0
    unsigned format1 = 0;
366
0
    unsigned format2 = 0;
367
0
368
0
    for (unsigned class1_idx : + hb_range ((unsigned) class1Count) | hb_filter (klass1_map))
369
0
    {
370
0
      for (unsigned class2_idx : + hb_range ((unsigned) class2Count) | hb_filter (klass2_map))
371
0
      {
372
0
        unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * record_size;
373
0
        format1 = format1 | valueFormat1.get_effective_format (&values[idx], strip_hints, strip_empty, this, varidx_delta_map);
374
0
        format2 = format2 | valueFormat2.get_effective_format (&values[idx + len1], strip_hints, strip_empty, this, varidx_delta_map);
375
0
      }
376
0
377
0
      if (format1 == valueFormat1 && format2 == valueFormat2)
378
0
        break;
379
0
    }
380
0
381
0
    return hb_pair (format1, format2);
382
0
  }
383
};
384
385
}
386
}
387
}
388
389
#endif  // OT_LAYOUT_GPOS_PAIRPOSFORMAT2_HH