Coverage Report

Created: 2023-09-28 22:17

/src/harfbuzz/src/hb-ot-map.cc
Line
Count
Source (jump to first uncovered line)
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
754k
{
48
754k
  hb_memset (this, 0, sizeof (*this));
49
50
754k
  feature_infos.init ();
51
2.26M
  for (unsigned int table_index = 0; table_index < 2; table_index++)
52
1.50M
    stages[table_index].init ();
53
54
754k
  face = face_;
55
754k
  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
754k
  unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
61
754k
  unsigned int language_count = HB_OT_MAX_TAGS_PER_LANGUAGE;
62
754k
  hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
63
754k
  hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
64
65
754k
  hb_ot_tags_from_script_and_language (props.script,
66
754k
               props.language,
67
754k
               &script_count,
68
754k
               script_tags,
69
754k
               &language_count,
70
754k
               language_tags);
71
72
2.26M
  for (unsigned int table_index = 0; table_index < 2; table_index++)
73
1.50M
  {
74
1.50M
    hb_tag_t table_tag = table_tags[table_index];
75
1.50M
    found_script[table_index] = (bool) hb_ot_layout_table_select_script (face,
76
1.50M
                   table_tag,
77
1.50M
                   script_count,
78
1.50M
                   script_tags,
79
1.50M
                   &script_index[table_index],
80
1.50M
                   &chosen_script[table_index]);
81
1.50M
    hb_ot_layout_script_select_language (face,
82
1.50M
           table_tag,
83
1.50M
           script_index[table_index],
84
1.50M
           language_count,
85
1.50M
           language_tags,
86
1.50M
           &language_index[table_index]);
87
1.50M
  }
88
754k
}
89
90
hb_ot_map_builder_t::~hb_ot_map_builder_t ()
91
754k
{
92
754k
  feature_infos.fini ();
93
2.26M
  for (unsigned int table_index = 0; table_index < 2; table_index++)
94
1.50M
    stages[table_index].fini ();
95
754k
}
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
20.8M
{
101
20.8M
  if (unlikely (!tag)) return;
102
20.8M
  feature_info_t *info = feature_infos.push();
103
20.8M
  info->tag = tag;
104
20.8M
  info->seq = feature_infos.length;
105
20.8M
  info->max_value = value;
106
20.8M
  info->flags = flags;
107
20.8M
  info->default_value = (flags & F_GLOBAL) ? value : 0;
108
20.8M
  info->stage[0] = current_stage[0];
109
20.8M
  info->stage[1] = current_stage[1];
110
20.8M
}
111
112
bool hb_ot_map_builder_t::has_feature (hb_tag_t tag)
113
12.3k
{
114
36.8k
  for (unsigned int table_index = 0; table_index < 2; table_index++)
115
24.5k
  {
116
24.5k
    if (hb_ot_layout_language_find_feature (face,
117
24.5k
              table_tags[table_index],
118
24.5k
              script_index[table_index],
119
24.5k
              language_index[table_index],
120
24.5k
              tag,
121
24.5k
              nullptr))
122
86
      return true;
123
24.5k
  }
124
12.2k
  return false;
125
12.3k
}
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
          unsigned int  variations_index,
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
3.37M
{
139
3.37M
  unsigned int lookup_indices[32];
140
3.37M
  unsigned int offset, len;
141
3.37M
  unsigned int table_lookup_count;
142
143
3.37M
  table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
144
145
3.37M
  offset = 0;
146
4.26M
  do {
147
4.26M
    len = ARRAY_LENGTH (lookup_indices);
148
4.26M
    hb_ot_layout_feature_with_variations_get_lookups (face,
149
4.26M
                  table_tags[table_index],
150
4.26M
                  feature_index,
151
4.26M
                  variations_index,
152
4.26M
                  offset, &len,
153
4.26M
                  lookup_indices);
154
155
33.8M
    for (unsigned int i = 0; i < len; i++)
156
29.6M
    {
157
29.6M
      if (lookup_indices[i] >= table_lookup_count)
158
16.6M
  continue;
159
12.9M
      hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
160
12.9M
      lookup->mask = mask;
161
12.9M
      lookup->index = lookup_indices[i];
162
12.9M
      lookup->auto_zwnj = auto_zwnj;
163
12.9M
      lookup->auto_zwj = auto_zwj;
164
12.9M
      lookup->random = random;
165
12.9M
      lookup->per_syllable = per_syllable;
166
12.9M
      lookup->feature_tag = feature_tag;
167
12.9M
    }
168
169
4.26M
    offset += len;
170
4.26M
  } while (len == ARRAY_LENGTH (lookup_indices));
171
3.37M
}
172
173
174
void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
175
3.00M
{
176
3.00M
  stage_info_t *s = stages[table_index].push ();
177
3.00M
  s->index = current_stage[table_index];
178
3.00M
  s->pause_func = pause_func;
179
180
3.00M
  current_stage[table_index]++;
181
3.00M
}
182
183
void
184
hb_ot_map_builder_t::compile (hb_ot_map_t                  &m,
185
            const hb_ot_shape_plan_key_t &key)
186
754k
{
187
754k
  unsigned int global_bit_shift = 8 * sizeof (hb_mask_t) - 1;
188
754k
  unsigned int global_bit_mask = 1u << global_bit_shift;
189
190
754k
  m.global_mask = global_bit_mask;
191
192
754k
  unsigned int required_feature_index[2];
193
754k
  hb_tag_t required_feature_tag[2];
194
  /* We default to applying required feature in stage 0.  If the required
195
   * feature has a tag that is known to the shaper, we apply required feature
196
   * in the stage for that tag.
197
   */
198
754k
  unsigned int required_feature_stage[2] = {0, 0};
199
200
2.26M
  for (unsigned int table_index = 0; table_index < 2; table_index++)
201
1.50M
  {
202
1.50M
    m.chosen_script[table_index] = chosen_script[table_index];
203
1.50M
    m.found_script[table_index] = found_script[table_index];
204
205
1.50M
    hb_ot_layout_language_get_required_feature (face,
206
1.50M
            table_tags[table_index],
207
1.50M
            script_index[table_index],
208
1.50M
            language_index[table_index],
209
1.50M
            &required_feature_index[table_index],
210
1.50M
            &required_feature_tag[table_index]);
211
1.50M
  }
212
213
  /* Sort features and merge duplicates */
214
754k
  if (feature_infos.length)
215
751k
  {
216
751k
    feature_infos.qsort ();
217
751k
    auto *f = feature_infos.arrayZ;
218
751k
    unsigned int j = 0;
219
751k
    unsigned count = feature_infos.length;
220
20.6M
    for (unsigned int i = 1; i < count; i++)
221
19.9M
      if (f[i].tag != f[j].tag)
222
19.7M
  f[++j] = f[i];
223
225k
      else {
224
225k
  if (f[i].flags & F_GLOBAL) {
225
225k
    f[j].flags |= F_GLOBAL;
226
225k
    f[j].max_value = f[i].max_value;
227
225k
    f[j].default_value = f[i].default_value;
228
225k
  } else {
229
0
    if (f[j].flags & F_GLOBAL)
230
0
      f[j].flags ^= F_GLOBAL;
231
0
    f[j].max_value = hb_max (f[j].max_value, f[i].max_value);
232
    /* Inherit default_value from j */
233
0
  }
234
225k
  f[j].flags |= (f[i].flags & F_HAS_FALLBACK);
235
225k
  f[j].stage[0] = hb_min (f[j].stage[0], f[i].stage[0]);
236
225k
  f[j].stage[1] = hb_min (f[j].stage[1], f[i].stage[1]);
237
225k
      }
238
751k
    feature_infos.shrink (j + 1);
239
751k
  }
240
241
242
  /* Allocate bits now */
243
754k
  static_assert ((!(HB_GLYPH_FLAG_DEFINED & (HB_GLYPH_FLAG_DEFINED + 1))), "");
244
754k
  unsigned int next_bit = hb_popcount (HB_GLYPH_FLAG_DEFINED) + 1;
245
246
754k
  unsigned count = feature_infos.length;
247
21.2M
  for (unsigned int i = 0; i < count; i++)
248
20.4M
  {
249
20.4M
    const feature_info_t *info = &feature_infos[i];
250
251
20.4M
    unsigned int bits_needed;
252
253
20.4M
    if ((info->flags & F_GLOBAL) && info->max_value == 1)
254
      /* Uses the global bit */
255
17.0M
      bits_needed = 0;
256
3.45M
    else
257
      /* Limit bits per feature. */
258
3.45M
      bits_needed = hb_min (HB_OT_MAP_MAX_BITS, hb_bit_storage (info->max_value));
259
260
20.4M
    if (!info->max_value || next_bit + bits_needed >= global_bit_shift)
261
35.7k
      continue; /* Feature disabled, or not enough bits. */
262
263
264
20.4M
    bool found = false;
265
20.4M
    unsigned int feature_index[2];
266
61.2M
    for (unsigned int table_index = 0; table_index < 2; table_index++)
267
40.8M
    {
268
40.8M
      if (required_feature_tag[table_index] == info->tag)
269
2.56k
  required_feature_stage[table_index] = info->stage[table_index];
270
271
40.8M
      found |= (bool) hb_ot_layout_language_find_feature (face,
272
40.8M
                table_tags[table_index],
273
40.8M
                script_index[table_index],
274
40.8M
                language_index[table_index],
275
40.8M
                info->tag,
276
40.8M
                &feature_index[table_index]);
277
40.8M
    }
278
20.4M
    if (!found && (info->flags & F_GLOBAL_SEARCH))
279
0
    {
280
0
      for (unsigned int table_index = 0; table_index < 2; table_index++)
281
0
      {
282
0
  found |= (bool) hb_ot_layout_table_find_feature (face,
283
0
               table_tags[table_index],
284
0
               info->tag,
285
0
               &feature_index[table_index]);
286
0
      }
287
0
    }
288
20.4M
    if (!found && !(info->flags & F_HAS_FALLBACK))
289
18.7M
      continue;
290
291
292
1.67M
    hb_ot_map_t::feature_map_t *map = m.features.push ();
293
294
1.67M
    map->tag = info->tag;
295
1.67M
    map->index[0] = feature_index[0];
296
1.67M
    map->index[1] = feature_index[1];
297
1.67M
    map->stage[0] = info->stage[0];
298
1.67M
    map->stage[1] = info->stage[1];
299
1.67M
    map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ);
300
1.67M
    map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
301
1.67M
    map->random = !!(info->flags & F_RANDOM);
302
1.67M
    map->per_syllable = !!(info->flags & F_PER_SYLLABLE);
303
1.67M
    if ((info->flags & F_GLOBAL) && info->max_value == 1) {
304
      /* Uses the global bit */
305
1.60M
      map->shift = global_bit_shift;
306
1.60M
      map->mask = global_bit_mask;
307
1.60M
    } else {
308
73.4k
      map->shift = next_bit;
309
73.4k
      map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit);
310
73.4k
      next_bit += bits_needed;
311
73.4k
      m.global_mask |= (info->default_value << map->shift) & map->mask;
312
73.4k
    }
313
1.67M
    map->_1_mask = (1u << map->shift) & map->mask;
314
1.67M
    map->needs_fallback = !found;
315
1.67M
  }
316
  //feature_infos.shrink (0); /* Done with these */
317
318
319
754k
  add_gsub_pause (nullptr);
320
754k
  add_gpos_pause (nullptr);
321
322
2.26M
  for (unsigned int table_index = 0; table_index < 2; table_index++)
323
1.50M
  {
324
    /* Collect lookup indices for features */
325
1.50M
    auto &lookups = m.lookups[table_index];
326
327
1.50M
    unsigned int stage_index = 0;
328
1.50M
    unsigned int last_num_lookups = 0;
329
4.50M
    for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
330
3.00M
    {
331
3.00M
      if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX &&
332
3.00M
    required_feature_stage[table_index] == stage)
333
35.0k
  add_lookups (m, table_index,
334
35.0k
         required_feature_index[table_index],
335
35.0k
         key.variations_index[table_index],
336
35.0k
         global_bit_mask);
337
338
3.00M
      for (auto &feature : m.features)
339
7.78M
      {
340
7.78M
  if (feature.stage[table_index] == stage)
341
3.33M
    add_lookups (m, table_index,
342
3.33M
           feature.index[table_index],
343
3.33M
           key.variations_index[table_index],
344
3.33M
           feature.mask,
345
3.33M
           feature.auto_zwnj,
346
3.33M
           feature.auto_zwj,
347
3.33M
           feature.random,
348
3.33M
           feature.per_syllable,
349
3.33M
           feature.tag);
350
7.78M
      }
351
352
      /* Sort lookups and merge duplicates */
353
3.00M
      if (last_num_lookups < lookups.length)
354
125k
      {
355
125k
  lookups.as_array ().sub_array (last_num_lookups, lookups.length - last_num_lookups).qsort ();
356
357
125k
  unsigned int j = last_num_lookups;
358
12.7M
  for (unsigned int i = j + 1; i < lookups.length; i++)
359
12.6M
    if (lookups.arrayZ[i].index != lookups.arrayZ[j].index)
360
697k
      lookups.arrayZ[++j] = lookups.arrayZ[i];
361
11.9M
    else
362
11.9M
    {
363
11.9M
      lookups.arrayZ[j].mask |= lookups.arrayZ[i].mask;
364
11.9M
      lookups.arrayZ[j].auto_zwnj &= lookups.arrayZ[i].auto_zwnj;
365
11.9M
      lookups.arrayZ[j].auto_zwj &= lookups.arrayZ[i].auto_zwj;
366
11.9M
    }
367
125k
  lookups.shrink (j + 1);
368
125k
      }
369
370
3.00M
      last_num_lookups = lookups.length;
371
372
3.00M
      if (stage_index < stages[table_index].length && stages[table_index][stage_index].index == stage) {
373
2.98M
  hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
374
2.98M
  stage_map->last_lookup = last_num_lookups;
375
2.98M
  stage_map->pause_func = stages[table_index][stage_index].pause_func;
376
377
2.98M
  stage_index++;
378
2.98M
      }
379
3.00M
    }
380
1.50M
  }
381
754k
}
382
383
384
#endif