Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-map.cc
Line
Count
Source
1
/*
2
 * Copyright © 2009,2010  Red Hat, Inc.
3
 * Copyright © 2010,2011,2013  Google, Inc.
4
 *
5
 *  This is part of HarfBuzz, a text shaping library.
6
 *
7
 * Permission is hereby granted, without written agreement and without
8
 * license or royalty fees, to use, copy, modify, and distribute this
9
 * software and its documentation for any purpose, provided that the
10
 * above copyright notice and the following two paragraphs appear in
11
 * all copies of this software.
12
 *
13
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17
 * DAMAGE.
18
 *
19
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24
 *
25
 * Red Hat Author(s): Behdad Esfahbod
26
 * Google Author(s): Behdad Esfahbod
27
 */
28
29
#include "hb.hh"
30
31
#ifndef HB_NO_OT_SHAPE
32
33
#include "hb-ot-map.hh"
34
#include "hb-ot-shape.hh"
35
#include "hb-ot-layout.hh"
36
37
38
void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
39
0
{
40
0
  for (unsigned int i = 0; i < lookups[table_index].length; i++)
41
0
    lookups_out->add (lookups[table_index][i].index);
42
0
}
43
44
45
hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
46
            const hb_segment_properties_t &props_)
47
18.0k
{
48
18.0k
  hb_memset (this, 0, sizeof (*this));
49
50
18.0k
  feature_infos.init ();
51
54.1k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
52
36.0k
    stages[table_index].init ();
53
54
18.0k
  face = face_;
55
18.0k
  props = props_;
56
57
  /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
58
   * features not available in either table and not waste precious bits for them. */
59
60
18.0k
  unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
61
18.0k
  unsigned int language_count = HB_OT_MAX_TAGS_PER_LANGUAGE;
62
18.0k
  hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
63
18.0k
  hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
64
65
18.0k
  hb_ot_tags_from_script_and_language (props.script,
66
18.0k
               props.language,
67
18.0k
               &script_count,
68
18.0k
               script_tags,
69
18.0k
               &language_count,
70
18.0k
               language_tags);
71
72
54.1k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
73
36.0k
  {
74
36.0k
    hb_tag_t table_tag = table_tags[table_index];
75
36.0k
    found_script[table_index] = (bool) hb_ot_layout_table_select_script (face,
76
36.0k
                   table_tag,
77
36.0k
                   script_count,
78
36.0k
                   script_tags,
79
36.0k
                   &script_index[table_index],
80
36.0k
                   &chosen_script[table_index]);
81
36.0k
    hb_ot_layout_script_select_language (face,
82
36.0k
           table_tag,
83
36.0k
           script_index[table_index],
84
36.0k
           language_count,
85
36.0k
           language_tags,
86
36.0k
           &language_index[table_index]);
87
36.0k
  }
88
18.0k
}
89
90
hb_ot_map_builder_t::~hb_ot_map_builder_t ()
91
18.0k
{
92
18.0k
  feature_infos.fini ();
93
54.1k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
94
36.0k
    stages[table_index].fini ();
95
18.0k
}
96
97
void hb_ot_map_builder_t::add_feature (hb_tag_t tag,
98
               hb_ot_map_feature_flags_t flags,
99
               unsigned int value)
100
756k
{
101
756k
  if (unlikely (!tag)) return;
102
756k
  feature_info_t *info = feature_infos.push();
103
756k
  info->tag = tag;
104
756k
  info->seq = feature_infos.length;
105
756k
  info->max_value = value;
106
756k
  info->flags = flags;
107
756k
  info->default_value = (flags & F_GLOBAL) ? value : 0;
108
756k
  info->stage[0] = current_stage[0];
109
756k
  info->stage[1] = current_stage[1];
110
756k
}
111
112
bool hb_ot_map_builder_t::has_feature (hb_tag_t tag)
113
153
{
114
459
  for (unsigned int table_index = 0; table_index < 2; table_index++)
115
306
  {
116
306
    if (hb_ot_layout_language_find_feature (face,
117
306
              table_tags[table_index],
118
306
              script_index[table_index],
119
306
              language_index[table_index],
120
306
              tag,
121
306
              nullptr))
122
0
      return true;
123
306
  }
124
153
  return false;
125
153
}
126
127
void
128
hb_ot_map_builder_t::add_lookups (hb_ot_map_t  &m,
129
          unsigned int  table_index,
130
          unsigned int  feature_index,
131
          const hb_ot_shape_plan_key_t &key,
132
          hb_mask_t     mask,
133
          bool          auto_zwnj,
134
          bool          auto_zwj,
135
          bool          random,
136
          bool          per_syllable,
137
          hb_tag_t      feature_tag)
138
45.9k
{
139
45.9k
  unsigned int lookup_indices[32];
140
45.9k
  unsigned int offset, len;
141
45.9k
  unsigned int table_lookup_count;
142
143
45.9k
  table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
144
145
45.9k
  hb_set_t varied_lookups;
146
45.9k
  if (key.get_lookup_variations (face, table_index, feature_index, &varied_lookups))
147
0
  {
148
0
    if (unlikely (varied_lookups.in_error ()))
149
0
    {
150
0
      m.lookups[table_index].ensure_error ();
151
0
      return;
152
0
    }
153
0
    for (unsigned int lookup_index : varied_lookups)
154
0
    {
155
0
      if (lookup_index >= table_lookup_count)
156
0
  continue;
157
0
      hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
158
0
      lookup->mask = mask;
159
0
      lookup->index = lookup_index;
160
0
      lookup->auto_zwnj = auto_zwnj;
161
0
      lookup->auto_zwj = auto_zwj;
162
0
      lookup->random = random;
163
0
      lookup->per_syllable = per_syllable;
164
0
      lookup->feature_tag = feature_tag;
165
0
    }
166
0
    return;
167
0
  }
168
169
45.9k
  offset = 0;
170
60.1k
  do {
171
60.1k
    len = ARRAY_LENGTH (lookup_indices);
172
60.1k
    hb_ot_layout_feature_with_variations_get_lookups (face,
173
60.1k
                table_tags[table_index],
174
60.1k
                feature_index,
175
60.1k
                key.variations_index[table_index],
176
60.1k
                  offset, &len,
177
60.1k
                  lookup_indices);
178
179
542k
    for (unsigned int i = 0; i < len; i++)
180
482k
    {
181
482k
      if (lookup_indices[i] >= table_lookup_count)
182
317k
  continue;
183
164k
      hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
184
164k
      lookup->mask = mask;
185
164k
      lookup->index = lookup_indices[i];
186
164k
      lookup->auto_zwnj = auto_zwnj;
187
164k
      lookup->auto_zwj = auto_zwj;
188
164k
      lookup->random = random;
189
164k
      lookup->per_syllable = per_syllable;
190
164k
      lookup->feature_tag = feature_tag;
191
164k
    }
192
193
60.1k
    offset += len;
194
60.1k
  } while (len == ARRAY_LENGTH (lookup_indices));
195
45.9k
}
196
197
198
void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
199
56.0k
{
200
56.0k
  stage_info_t *s = stages[table_index].push ();
201
56.0k
  s->index = current_stage[table_index];
202
56.0k
  s->pause_func = pause_func;
203
204
56.0k
  current_stage[table_index]++;
205
56.0k
}
206
207
void
208
hb_ot_map_builder_t::compile (hb_ot_map_t                  &m,
209
            const hb_ot_shape_plan_key_t &key)
210
18.0k
{
211
18.0k
  unsigned int global_bit_shift = 8 * sizeof (hb_mask_t) - 1;
212
18.0k
  unsigned int global_bit_mask = 1u << global_bit_shift;
213
214
18.0k
  m.global_mask = global_bit_mask;
215
216
18.0k
  unsigned int required_feature_index[2];
217
18.0k
  hb_tag_t required_feature_tag[2];
218
  /* We default to applying required feature in stage 0.  If the required
219
   * feature has a tag that is known to the shaper, we apply required feature
220
   * in the stage for that tag.
221
   */
222
18.0k
  unsigned int required_feature_stage[2] = {0, 0};
223
224
54.1k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
225
36.0k
  {
226
36.0k
    m.chosen_script[table_index] = chosen_script[table_index];
227
36.0k
    m.found_script[table_index] = found_script[table_index];
228
229
36.0k
    hb_ot_layout_language_get_required_feature (face,
230
36.0k
            table_tags[table_index],
231
36.0k
            script_index[table_index],
232
36.0k
            language_index[table_index],
233
36.0k
            &required_feature_index[table_index],
234
36.0k
            &required_feature_tag[table_index]);
235
36.0k
  }
236
237
  /* Sort features and merge duplicates */
238
18.0k
  if (feature_infos.length)
239
18.0k
  {
240
18.0k
    if (!is_simple)
241
5.41k
      feature_infos.qsort ();
242
18.0k
    auto *f = feature_infos.arrayZ;
243
18.0k
    unsigned int j = 0;
244
18.0k
    unsigned count = feature_infos.length;
245
756k
    for (unsigned int i = 1; i < count; i++)
246
738k
      if (f[i].tag != f[j].tag)
247
448k
  f[++j] = f[i];
248
289k
      else {
249
289k
  if (f[i].flags & F_GLOBAL) {
250
289k
    f[j].flags |= F_GLOBAL;
251
289k
    f[j].max_value = f[i].max_value;
252
289k
    f[j].default_value = f[i].default_value;
253
289k
  } else {
254
169
    if (f[j].flags & F_GLOBAL)
255
159
      f[j].flags ^= F_GLOBAL;
256
169
    f[j].max_value = hb_max (f[j].max_value, f[i].max_value);
257
    /* Inherit default_value from j */
258
169
  }
259
289k
  f[j].flags |= (f[i].flags & F_HAS_FALLBACK);
260
289k
  f[j].stage[0] = hb_min (f[j].stage[0], f[i].stage[0]);
261
289k
  f[j].stage[1] = hb_min (f[j].stage[1], f[i].stage[1]);
262
289k
      }
263
18.0k
    feature_infos.shrink (j + 1);
264
18.0k
  }
265
266
18.0k
  hb_map_t feature_indices[2];
267
54.1k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
268
36.0k
    hb_ot_layout_collect_features_map (face,
269
36.0k
               table_tags[table_index],
270
36.0k
               script_index[table_index],
271
36.0k
               language_index[table_index],
272
36.0k
               &feature_indices[table_index]);
273
274
  /* Allocate bits now */
275
18.0k
  static_assert ((!(HB_GLYPH_FLAG_DEFINED & (HB_GLYPH_FLAG_DEFINED + 1))), "");
276
18.0k
  unsigned int next_bit = hb_popcount (HB_GLYPH_FLAG_DEFINED) + 1;
277
278
18.0k
  unsigned count = feature_infos.length;
279
484k
  for (unsigned int i = 0; i < count; i++)
280
466k
  {
281
466k
    const feature_info_t *info = &feature_infos[i];
282
283
466k
    unsigned int bits_needed;
284
285
466k
    if ((info->flags & F_GLOBAL) && info->max_value == 1)
286
      /* Uses the global bit */
287
384k
      bits_needed = 0;
288
82.1k
    else
289
      /* Limit bits per feature. */
290
82.1k
      bits_needed = hb_min (HB_OT_MAP_MAX_BITS, hb_bit_storage (info->max_value));
291
292
466k
    if (!info->max_value || next_bit + bits_needed >= global_bit_shift)
293
6.65k
      continue; /* Feature disabled, or not enough bits. */
294
295
460k
    bool found = false;
296
460k
    unsigned int feature_index[2];
297
1.38M
    for (unsigned int table_index = 0; table_index < 2; table_index++)
298
920k
    {
299
920k
      if (required_feature_tag[table_index] == info->tag)
300
8
  required_feature_stage[table_index] = info->stage[table_index];
301
302
920k
      hb_codepoint_t *index;
303
920k
      if (feature_indices[table_index].has (info->tag, &index))
304
6.84k
      {
305
6.84k
        feature_index[table_index] = *index;
306
6.84k
        found = true;
307
6.84k
      }
308
913k
      else
309
913k
        feature_index[table_index] = HB_OT_LAYOUT_NO_FEATURE_INDEX;
310
920k
    }
311
460k
    if (!found && (info->flags & F_GLOBAL_SEARCH))
312
209
    {
313
627
      for (unsigned int table_index = 0; table_index < 2; table_index++)
314
418
      {
315
418
  found |= (bool) hb_ot_layout_table_find_feature (face,
316
418
               table_tags[table_index],
317
418
               info->tag,
318
418
               &feature_index[table_index]);
319
418
      }
320
209
    }
321
460k
    if (!found && !(info->flags & F_HAS_FALLBACK))
322
437k
      continue;
323
324
325
22.9k
    hb_ot_map_t::feature_map_t *map = m.features.push ();
326
327
22.9k
    map->tag = info->tag;
328
22.9k
    map->index[0] = feature_index[0];
329
22.9k
    map->index[1] = feature_index[1];
330
22.9k
    map->stage[0] = info->stage[0];
331
22.9k
    map->stage[1] = info->stage[1];
332
22.9k
    map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ);
333
22.9k
    map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
334
22.9k
    map->random = !!(info->flags & F_RANDOM);
335
22.9k
    map->per_syllable = !!(info->flags & F_PER_SYLLABLE);
336
22.9k
    if ((info->flags & F_GLOBAL) && info->max_value == 1) {
337
      /* Uses the global bit */
338
22.3k
      map->shift = global_bit_shift;
339
22.3k
      map->mask = global_bit_mask;
340
22.3k
    } else {
341
612
      map->shift = next_bit;
342
612
      map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit);
343
612
      next_bit += bits_needed;
344
612
      m.global_mask |= (info->default_value << map->shift) & map->mask;
345
612
    }
346
22.9k
    map->_1_mask = (1u << map->shift) & map->mask;
347
22.9k
    map->needs_fallback = !found;
348
22.9k
  }
349
  //feature_infos.shrink (0); /* Done with these */
350
18.0k
  if (is_simple)
351
12.6k
    m.features.qsort ();
352
353
18.0k
  add_gsub_pause (nullptr);
354
18.0k
  add_gpos_pause (nullptr);
355
356
54.1k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
357
36.0k
  {
358
    /* Collect lookup indices for features */
359
36.0k
    auto &lookups = m.lookups[table_index];
360
361
36.0k
    unsigned int stage_index = 0;
362
36.0k
    unsigned int last_num_lookups = 0;
363
92.1k
    for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
364
56.0k
    {
365
56.0k
      if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX &&
366
68
    required_feature_stage[table_index] == stage)
367
36
  add_lookups (m, table_index,
368
36
         required_feature_index[table_index],
369
36
         key,
370
36
         global_bit_mask);
371
372
56.0k
      for (auto &feature : m.features)
373
79.5k
      {
374
79.5k
  if (feature.stage[table_index] == stage)
375
45.9k
    add_lookups (m, table_index,
376
45.9k
           feature.index[table_index],
377
45.9k
           key,
378
45.9k
           feature.mask,
379
45.9k
           feature.auto_zwnj,
380
45.9k
           feature.auto_zwj,
381
45.9k
           feature.random,
382
45.9k
           feature.per_syllable,
383
45.9k
           feature.tag);
384
79.5k
      }
385
386
      /* Sort lookups and merge duplicates */
387
56.0k
      if (last_num_lookups + 1 < lookups.length)
388
2.14k
      {
389
2.14k
  lookups.as_array ().sub_array (last_num_lookups, lookups.length - last_num_lookups).qsort ();
390
391
2.14k
  unsigned int j = last_num_lookups;
392
163k
  for (unsigned int i = j + 1; i < lookups.length; i++)
393
161k
    if (lookups.arrayZ[i].index != lookups.arrayZ[j].index)
394
17.0k
      lookups.arrayZ[++j] = lookups.arrayZ[i];
395
144k
    else
396
144k
    {
397
144k
      lookups.arrayZ[j].mask |= lookups.arrayZ[i].mask;
398
144k
      lookups.arrayZ[j].auto_zwnj &= lookups.arrayZ[i].auto_zwnj;
399
144k
      lookups.arrayZ[j].auto_zwj &= lookups.arrayZ[i].auto_zwj;
400
144k
    }
401
2.14k
  lookups.shrink (j + 1);
402
2.14k
      }
403
404
56.0k
      last_num_lookups = lookups.length;
405
406
56.0k
      if (stage_index < stages[table_index].length && stages[table_index][stage_index].index == stage) {
407
56.0k
  hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
408
56.0k
  stage_map->last_lookup = last_num_lookups;
409
56.0k
  stage_map->pause_func = stages[table_index][stage_index].pause_func;
410
411
56.0k
  stage_index++;
412
56.0k
      }
413
56.0k
    }
414
36.0k
  }
415
18.0k
}
416
417
unsigned int hb_ot_map_t::get_feature_tags (unsigned int start_offset, unsigned int *tag_count, hb_tag_t *tags) const
418
0
{
419
0
  if (tag_count)
420
0
  {
421
0
    auto sub_features = features.as_array ().sub_array (start_offset, tag_count);
422
0
    if (tags)
423
0
    {
424
0
      for (unsigned int i = 0; i < sub_features.length; i++)
425
0
        tags[i] = sub_features[i].tag;
426
0
    }
427
0
  }
428
429
0
  return features.length;
430
0
}
431
432
#endif