Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-subset-plan.cc
Line
Count
Source
1
/*
2
 * Copyright © 2018  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Garret Rieger, Roderick Sheeter
25
 */
26
27
#include "hb-subset-plan.hh"
28
#include "hb-subset-accelerator.hh"
29
#include "hb-map.hh"
30
#include "hb-multimap.hh"
31
#include "hb-set.hh"
32
#include "hb-subset.h"
33
#include "hb-unicode.h"
34
35
#include "hb-ot-cmap-table.hh"
36
#include "hb-ot-glyf-table.hh"
37
#include "hb-ot-layout-base-table.hh"
38
#include "hb-ot-cff1-table.hh"
39
#include "hb-ot-cff2-table.hh"
40
#include "OT/Color/COLR/COLR.hh"
41
#include "OT/Color/COLR/colrv1-closure.hh"
42
#include "OT/Color/CPAL/CPAL.hh"
43
#include "hb-ot-var-fvar-table.hh"
44
#include "hb-ot-var-varc-table.hh"
45
#include "hb-ot-stat-table.hh"
46
#include "hb-ot-math-table.hh"
47
48
hb_subset_accelerator_t::~hb_subset_accelerator_t ()
49
0
{
50
0
  if (cmap_cache && destroy_cmap_cache)
51
0
    destroy_cmap_cache ((void*) cmap_cache);
52
53
0
#ifndef HB_NO_SUBSET_CFF
54
0
  cff1_accel.fini ();
55
0
  cff2_accel.fini ();
56
0
#endif
57
0
  hb_face_destroy (source);
58
0
}
59
60
61
#ifndef HB_NO_SUBSET_CFF
62
static inline bool
63
_add_cff_seac_components (const OT::cff1::accelerator_subset_t &cff,
64
        hb_codepoint_t gid,
65
        hb_set_t *gids_to_retain)
66
0
{
67
0
  hb_codepoint_t base_gid, accent_gid;
68
0
  if (cff.get_seac_components (gid, &base_gid, &accent_gid))
69
0
  {
70
0
    gids_to_retain->add (base_gid);
71
0
    gids_to_retain->add (accent_gid);
72
0
    return true;
73
0
  }
74
0
  return false;
75
0
}
76
#endif
77
78
static void
79
_remap_palette_indexes (const hb_set_t *palette_indexes,
80
      hb_map_t       *mapping /* OUT */)
81
0
{
82
0
  unsigned new_idx = 0;
83
0
  for (unsigned palette_index : palette_indexes->iter ())
84
0
  {
85
0
    if (palette_index == 0xFFFF)
86
0
    {
87
0
      mapping->set (palette_index, palette_index);
88
0
      continue;
89
0
    }
90
0
    mapping->set (palette_index, new_idx);
91
0
    new_idx++;
92
0
  }
93
0
}
94
95
void
96
remap_indexes (const hb_set_t *indexes,
97
               hb_map_t       *mapping /* OUT */)
98
0
{
99
0
  for (auto _ : + hb_enumerate (indexes->iter ()))
100
0
    mapping->set (_.second, _.first);
101
0
}
102
103
static inline void
104
_cmap_closure (hb_face_t     *face,
105
         const hb_set_t    *unicodes,
106
         hb_set_t      *glyphset)
107
15.5k
{
108
15.5k
  OT::cmap::accelerator_t cmap (face);
109
15.5k
  cmap.table->closure_glyphs (unicodes, glyphset);
110
15.5k
}
111
112
static void _colr_closure (hb_subset_plan_t* plan,
113
                           hb_set_t *glyphs_colred)
114
0
{
115
0
  OT::COLR::accelerator_t colr (plan->source);
116
0
  if (!colr.is_valid ()) return;
117
118
0
  hb_set_t palette_indices, layer_indices;
119
  // Collect all glyphs referenced by COLRv0
120
0
  hb_set_t glyphset_colrv0;
121
0
  for (hb_codepoint_t gid : *glyphs_colred)
122
0
    colr.closure_glyphs (gid, &glyphset_colrv0);
123
124
0
  glyphs_colred->union_ (glyphset_colrv0);
125
126
  //closure for COLRv1
127
0
  hb_set_t variation_indices, delta_set_indices;
128
0
  colr.closure_forV1 (glyphs_colred, &layer_indices, &palette_indices, &variation_indices, &delta_set_indices);
129
130
0
  colr.closure_V0palette_indices (glyphs_colred, &palette_indices);
131
0
  remap_indexes (&layer_indices, &plan->colrv1_layers);
132
0
  _remap_palette_indexes (&palette_indices, &plan->colr_palettes);
133
134
0
#ifndef HB_NO_VAR
135
0
  if (!colr.has_var_store () || !variation_indices) return;
136
137
0
  const OT::ItemVariationStore &var_store = colr.get_var_store ();
138
  // generated inner_maps is used by ItemVariationStore serialize(), which is subset only
139
0
  unsigned subtable_count = var_store.get_sub_table_count ();
140
0
  generate_varstore_inner_maps (variation_indices, subtable_count, plan->colrv1_varstore_inner_maps);
141
142
  /* colr variation indices mapping during planning phase:
143
   * generate colrv1_variation_idx_delta_map. When delta set index map is not
144
   * included, it's a mapping from varIdx-> (new varIdx,delta). Otherwise, it's
145
   * a mapping from old delta set idx-> (new delta set idx, delta). Mapping
146
   * delta set indices is the same as gid mapping.
147
   * Besides, we need to generate a delta set idx-> new var_idx map for updating
148
   * delta set index map if exists. This map will be updated again after
149
   * instancing. */
150
0
  if (!plan->all_axes_pinned)
151
0
  {
152
0
    remap_variation_indices (var_store,
153
0
                              variation_indices,
154
0
                              plan->normalized_coords,
155
0
                              false, /* no need to calculate delta for COLR during planning */
156
0
                              plan->all_axes_pinned,
157
0
                              plan->colrv1_variation_idx_delta_map);
158
159
0
    if (colr.has_delta_set_index_map ())
160
0
      remap_colrv1_delta_set_index_indices (colr.get_delta_set_index_map (),
161
0
                                             delta_set_indices,
162
0
                                             plan->colrv1_variation_idx_delta_map,
163
0
                                             plan->colrv1_new_deltaset_idx_varidx_map);
164
0
  }
165
0
#endif
166
0
}
167
168
static inline void
169
_math_closure (hb_subset_plan_t *plan,
170
               hb_set_t         *glyphset)
171
0
{
172
0
  hb_blob_ptr_t<OT::MATH> math = plan->source_table<OT::MATH> ();
173
0
  if (math->has_data ())
174
0
    math->closure_glyphs (glyphset);
175
0
  math.destroy ();
176
0
}
177
178
static inline void
179
_remove_invalid_gids (hb_set_t *glyphs,
180
          unsigned int num_glyphs)
181
31.1k
{
182
31.1k
  glyphs->del_range (num_glyphs, HB_SET_VALUE_INVALID);
183
31.1k
}
184
185
template<bool GID_ALWAYS_EXISTS = false, typename I, typename F, typename G, hb_requires (hb_is_iterator (I))>
186
static void
187
_fill_unicode_and_glyph_map(hb_subset_plan_t *plan,
188
                            I unicode_iterator,
189
                            F unicode_to_gid_for_iterator,
190
                            G unicode_to_gid_general)
191
1.94M
{
192
1.94M
  for (hb_codepoint_t cp : unicode_iterator)
193
36.2M
  {
194
36.2M
    hb_codepoint_t gid = unicode_to_gid_for_iterator(cp);
195
36.2M
    if (!GID_ALWAYS_EXISTS && gid == HB_MAP_VALUE_INVALID)
196
35.7M
    {
197
35.7M
      DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
198
35.7M
      continue;
199
35.7M
    }
200
201
461k
    plan->codepoint_to_glyph->set (cp, gid);
202
461k
    plan->unicode_to_new_gid_list.push (hb_pair (cp, gid));
203
461k
  }
204
1.94M
}
Unexecuted instantiation: hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb0EN23hb_bit_set_invertible_t6iter_tEZL28_populate_unicodes_to_retainPK8hb_set_tS4_P16hb_subset_plan_tE3$_0S7_TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NS9_6item_tEEE5valueEvE4typeELPv0EEvS6_S9_T1_T2_
Unexecuted instantiation: hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb0EN23hb_bit_set_invertible_t6iter_tEZL28_populate_unicodes_to_retainPK8hb_set_tS4_P16hb_subset_plan_tE3$_1S7_TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NS9_6item_tEEE5valueEvE4typeELPv0EEvS6_S9_T1_T2_
Unexecuted instantiation: hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb1E10hb_array_tIKjEZL28_populate_unicodes_to_retainPK8hb_set_tS5_P16hb_subset_plan_tE3$_2ZL28_populate_unicodes_to_retainS5_S5_S7_E3$_3TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NSB_6item_tEEE5valueEvE4typeELPv0EEvS7_SB_T1_T2_
Unexecuted instantiation: hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb0EN23hb_bit_set_invertible_t6iter_tEZL28_populate_unicodes_to_retainPK8hb_set_tS4_P16hb_subset_plan_tE3$_4ZL28_populate_unicodes_to_retainS4_S4_S6_E3$_5TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NSA_6item_tEEE5valueEvE4typeELPv0EEvS6_SA_T1_T2_
hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb0E15hb_range_iter_tIjjEZL28_populate_unicodes_to_retainPK8hb_set_tS4_P16hb_subset_plan_tE3$_6ZL28_populate_unicodes_to_retainS4_S4_S6_E3$_7TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NSA_6item_tEEE5valueEvE4typeELPv0EEvS6_SA_T1_T2_
Line
Count
Source
191
1.94M
{
192
1.94M
  for (hb_codepoint_t cp : unicode_iterator)
193
36.2M
  {
194
36.2M
    hb_codepoint_t gid = unicode_to_gid_for_iterator(cp);
195
36.2M
    if (!GID_ALWAYS_EXISTS && gid == HB_MAP_VALUE_INVALID)
196
35.7M
    {
197
35.7M
      DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
198
35.7M
      continue;
199
35.7M
    }
200
201
461k
    plan->codepoint_to_glyph->set (cp, gid);
202
461k
    plan->unicode_to_new_gid_list.push (hb_pair (cp, gid));
203
461k
  }
204
1.94M
}
205
206
template<bool GID_ALWAYS_EXISTS = false, typename I, typename F, hb_requires (hb_is_iterator (I))>
207
static void
208
_fill_unicode_and_glyph_map(hb_subset_plan_t *plan,
209
                            I unicode_iterator,
210
                            F unicode_to_gid_for_iterator)
211
0
{
212
0
  _fill_unicode_and_glyph_map(plan, unicode_iterator, unicode_to_gid_for_iterator, unicode_to_gid_for_iterator);
213
0
}
Unexecuted instantiation: hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb0EN23hb_bit_set_invertible_t6iter_tEZL28_populate_unicodes_to_retainPK8hb_set_tS4_P16hb_subset_plan_tE3$_0TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NS9_6item_tEEE5valueEvE4typeELPv0EEvS6_S9_T1_
Unexecuted instantiation: hb-subset-plan.cc:_ZL27_fill_unicode_and_glyph_mapILb0EN23hb_bit_set_invertible_t6iter_tEZL28_populate_unicodes_to_retainPK8hb_set_tS4_P16hb_subset_plan_tE3$_1TnPN12hb_enable_ifIXsr17hb_is_iterator_ofIT0_NS9_6item_tEEE5valueEvE4typeELPv0EEvS6_S9_T1_
214
215
/*
216
 * Finds additional unicode codepoints which are reachable from the input unicode set.
217
 * Currently this adds in mirrored variants (needed for bidi) of any input unicodes.
218
 */
219
static hb_set_t
220
15.5k
_unicode_closure (const hb_set_t* unicodes, bool bidi_closure) {
221
  // TODO: we may want to also consider pulling in reachable unicode composition and decompositions.
222
  //       see: https://github.com/harfbuzz/harfbuzz/issues/2283
223
15.5k
  hb_set_t out = *unicodes;
224
15.5k
  if (!bidi_closure) return out;
225
226
15.5k
  if (out.is_inverted()) {
227
    // don't closure inverted sets, they are asking to specifically exclude certain codepoints.
228
    // otherwise everything is already included.
229
0
    return out;
230
0
  }
231
232
15.5k
  auto unicode_funcs = hb_unicode_funcs_get_default ();
233
15.5k
  for (hb_codepoint_t cp : *unicodes) {
234
0
   hb_codepoint_t mirror = hb_unicode_mirroring(unicode_funcs, cp);
235
0
   if (unlikely (mirror != cp)) {
236
0
     out.add(mirror);
237
0
   }
238
0
  }
239
240
15.5k
  return out;
241
15.5k
}
242
243
static void
244
_populate_unicodes_to_retain (const hb_set_t *unicodes_in,
245
                              const hb_set_t *glyphs,
246
                              hb_subset_plan_t *plan)
247
15.5k
{
248
15.5k
  hb_set_t unicodes = _unicode_closure(unicodes_in,
249
15.5k
    !(plan->flags & HB_SUBSET_FLAGS_NO_BIDI_CLOSURE));
250
251
15.5k
  OT::cmap::accelerator_t cmap (plan->source);
252
15.5k
  unsigned size_threshold = plan->source->get_num_glyphs ();
253
254
15.5k
  if (glyphs->is_empty () && unicodes.get_population () < size_threshold)
255
0
  {
256
257
0
    const hb_map_t* unicode_to_gid = nullptr;
258
0
    if (plan->accelerator)
259
0
      unicode_to_gid = &plan->accelerator->unicode_to_gid;
260
261
    // This is approach to collection is faster, but can only be used  if glyphs
262
    // are not being explicitly added to the subset and the input unicodes set is
263
    // not excessively large (eg. an inverted set).
264
0
    plan->unicode_to_new_gid_list.alloc (unicodes.get_population ());
265
0
    if (!unicode_to_gid) {
266
0
      _fill_unicode_and_glyph_map(plan, unicodes.iter(), [&] (hb_codepoint_t cp) {
267
0
        hb_codepoint_t gid;
268
0
        if (!cmap.get_nominal_glyph (cp, &gid)) {
269
0
          return HB_MAP_VALUE_INVALID;
270
0
        }
271
0
        return gid;
272
0
      });
273
0
    } else {
274
      // Use in memory unicode to gid map it's faster then looking up from
275
      // the map. This code is mostly duplicated from above to avoid doing
276
      // conditionals on the presence of the unicode_to_gid map each
277
      // iteration.
278
0
      _fill_unicode_and_glyph_map(plan, unicodes.iter(), [&] (hb_codepoint_t cp) {
279
0
        return unicode_to_gid->get (cp);
280
0
      });
281
0
    }
282
0
  }
283
15.5k
  else
284
15.5k
  {
285
    // This approach is slower, but can handle adding in glyphs to the subset and will match
286
    // them with cmap entries.
287
288
15.5k
    hb_map_t unicode_glyphid_map_storage;
289
15.5k
    hb_set_t cmap_unicodes_storage;
290
15.5k
    const hb_map_t* unicode_glyphid_map = &unicode_glyphid_map_storage;
291
15.5k
    const hb_set_t* cmap_unicodes = &cmap_unicodes_storage;
292
293
15.5k
    if (!plan->accelerator) {
294
15.5k
      cmap.collect_mapping (&cmap_unicodes_storage, &unicode_glyphid_map_storage);
295
15.5k
      plan->unicode_to_new_gid_list.alloc (hb_min(unicodes.get_population ()
296
15.5k
                                                  + glyphs->get_population (),
297
15.5k
                                                  cmap_unicodes->get_population ()));
298
15.5k
    } else {
299
0
      unicode_glyphid_map = &plan->accelerator->unicode_to_gid;
300
0
      cmap_unicodes = &plan->accelerator->unicodes;
301
0
    }
302
303
15.5k
    if (plan->accelerator &&
304
0
  unicodes.get_population () < cmap_unicodes->get_population () &&
305
0
  glyphs->get_population () < cmap_unicodes->get_population ())
306
0
    {
307
0
      plan->codepoint_to_glyph->alloc (unicodes.get_population () + glyphs->get_population ());
308
309
0
      auto &gid_to_unicodes = plan->accelerator->gid_to_unicodes;
310
311
0
      for (hb_codepoint_t gid : *glyphs)
312
0
      {
313
0
        auto unicodes = gid_to_unicodes.get (gid);
314
0
        _fill_unicode_and_glyph_map<true>(plan, unicodes, [&] (hb_codepoint_t cp) {
315
0
          return gid;
316
0
        },
317
0
        [&] (hb_codepoint_t cp) {
318
0
          return unicode_glyphid_map->get(cp);
319
0
        });
320
0
      }
321
322
0
      _fill_unicode_and_glyph_map(plan, unicodes.iter(), [&] (hb_codepoint_t cp) {
323
          /* Don't double-add entry. */
324
0
  if (plan->codepoint_to_glyph->has (cp))
325
0
          return HB_MAP_VALUE_INVALID;
326
327
0
        return unicode_glyphid_map->get(cp);
328
0
      },
329
0
      [&] (hb_codepoint_t cp) {
330
0
          return unicode_glyphid_map->get(cp);
331
0
      });
332
333
0
      plan->unicode_to_new_gid_list.qsort ();
334
0
    }
335
15.5k
    else
336
15.5k
    {
337
15.5k
      plan->codepoint_to_glyph->alloc (cmap_unicodes->get_population ());
338
15.5k
      hb_codepoint_t first = HB_SET_VALUE_INVALID, last = HB_SET_VALUE_INVALID;
339
1.96M
      for (; cmap_unicodes->next_range (&first, &last); )
340
1.94M
      {
341
36.2M
        _fill_unicode_and_glyph_map(plan, hb_range(first, last + 1), [&] (hb_codepoint_t cp) {
342
36.2M
          hb_codepoint_t gid = (*unicode_glyphid_map)[cp];
343
36.2M
    if (!unicodes.has (cp) && !glyphs->has (gid))
344
35.7M
      return HB_MAP_VALUE_INVALID;
345
461k
          return gid;
346
36.2M
        },
347
1.94M
        [&] (hb_codepoint_t cp) {
348
1.94M
          return unicode_glyphid_map->get(cp);
349
1.94M
        });
350
1.94M
      }
351
15.5k
    }
352
353
    /* Add gids which where requested, but not mapped in cmap */
354
15.5k
    unsigned num_glyphs = plan->source->get_num_glyphs ();
355
15.5k
    hb_codepoint_t first = HB_SET_VALUE_INVALID, last = HB_SET_VALUE_INVALID;
356
244k
    for (; glyphs->next_range (&first, &last); )
357
228k
    {
358
228k
      if (first >= num_glyphs)
359
0
  break;
360
228k
      if (last >= num_glyphs)
361
0
        last = num_glyphs - 1;
362
228k
      plan->_glyphset_gsub.add_range (first, last);
363
228k
    }
364
15.5k
  }
365
366
15.5k
  auto &arr = plan->unicode_to_new_gid_list;
367
15.5k
  if (arr.length)
368
14.9k
  {
369
14.9k
    plan->unicodes.add_sorted_array (&arr.arrayZ->first, arr.length, sizeof (*arr.arrayZ));
370
14.9k
    plan->_glyphset_gsub.add_array (&arr.arrayZ->second, arr.length, sizeof (*arr.arrayZ));
371
14.9k
  }
372
373
  // Variation selectors don't have glyphs associated with them in the cmap so they will have been filtered out above
374
  // but should still be retained. Add them back here.
375
376
  // However, the min and max codepoints for OS/2 should be calculated without considering variation selectors,
377
  // so record those first.
378
15.5k
  plan->os2_info.min_cmap_codepoint = plan->unicodes.get_min();
379
15.5k
  plan->os2_info.max_cmap_codepoint = plan->unicodes.get_max();
380
381
15.5k
  hb_set_t variation_selectors_to_retain;
382
15.5k
  cmap.collect_variation_selectors(&variation_selectors_to_retain);
383
15.5k
  + variation_selectors_to_retain.iter()
384
15.5k
  | hb_filter(unicodes)
385
15.5k
  | hb_sink(&plan->unicodes)
386
15.5k
  ;
387
15.5k
}
388
389
static unsigned
390
_glyf_add_gid_and_children (const OT::glyf_accelerator_t &glyf,
391
          hb_codepoint_t gid,
392
          hb_set_t *gids_to_retain,
393
          int operation_count,
394
          unsigned depth = 0)
395
544k
{
396
  /* Check if is already visited */
397
544k
  if (gids_to_retain->has (gid)) return operation_count;
398
399
511k
  gids_to_retain->add (gid);
400
401
511k
  if (unlikely (depth++ > HB_MAX_NESTING_LEVEL)) return operation_count;
402
511k
  if (unlikely (--operation_count < 0)) return operation_count;
403
404
511k
  auto glyph = glyf.glyph_for_gid (gid);
405
406
511k
  for (auto &item : glyph.get_composite_iterator ())
407
67.2k
    operation_count =
408
67.2k
      _glyf_add_gid_and_children (glyf,
409
67.2k
          item.get_gid (),
410
67.2k
          gids_to_retain,
411
67.2k
          operation_count,
412
67.2k
          depth);
413
414
511k
  return operation_count;
415
511k
}
416
417
static void
418
_nameid_closure (hb_subset_plan_t* plan,
419
     hb_set_t* drop_tables)
420
15.5k
{
421
15.5k
#ifndef HB_NO_STYLE
422
15.5k
  if (!drop_tables->has (HB_OT_TAG_STAT))
423
0
    plan->source->table.STAT->collect_name_ids (&plan->user_axes_location, &plan->name_ids);
424
15.5k
#endif
425
15.5k
#ifndef HB_NO_VAR
426
15.5k
  if (!plan->all_axes_pinned)
427
15.5k
    plan->source->table.fvar->collect_name_ids (&plan->user_axes_location, &plan->axes_old_index_tag_map, &plan->name_ids);
428
15.5k
#endif
429
15.5k
#ifndef HB_NO_COLOR
430
15.5k
  if (!drop_tables->has (HB_OT_TAG_CPAL))
431
0
    plan->source->table.CPAL->collect_name_ids (&plan->colr_palettes, &plan->name_ids);
432
15.5k
#endif
433
434
15.5k
#ifndef HB_NO_SUBSET_LAYOUT
435
15.5k
  layout_nameid_closure(plan, drop_tables);
436
15.5k
#endif
437
15.5k
}
438
439
static void
440
_populate_gids_to_retain (hb_subset_plan_t* plan,
441
              hb_set_t* drop_tables)
442
15.5k
{
443
15.5k
  OT::glyf_accelerator_t glyf (plan->source);
444
15.5k
#ifndef HB_NO_SUBSET_CFF
445
  // Note: we cannot use inprogress_accelerator here, since it has not been
446
  // created yet. So in case of preprocessed-face (and otherwise), we do an
447
  // extra sanitize pass here, which is not ideal.
448
15.5k
  OT::cff1::accelerator_subset_t stack_cff (plan->accelerator ? nullptr : plan->source);
449
15.5k
  const OT::cff1::accelerator_subset_t *cff (plan->accelerator ? plan->accelerator->cff1_accel.get () : &stack_cff);
450
15.5k
#endif
451
452
15.5k
  plan->_glyphset_gsub.add (0); // Not-def
453
454
15.5k
  _cmap_closure (plan->source, &plan->unicodes, &plan->_glyphset_gsub);
455
15.5k
  plan->_glyphset_cmaped = plan->_glyphset_gsub;
456
457
15.5k
  if (!drop_tables->has (HB_OT_TAG_MATH))
458
0
  {
459
0
    _math_closure (plan, &plan->_glyphset_gsub);
460
0
    _remove_invalid_gids (&plan->_glyphset_gsub, plan->source->get_num_glyphs ());
461
0
  }
462
15.5k
  plan->_glyphset_mathed = plan->_glyphset_gsub;
463
464
15.5k
#ifndef HB_NO_SUBSET_LAYOUT
465
15.5k
  layout_populate_gids_to_retain(plan, drop_tables);
466
15.5k
#endif
467
468
15.5k
  _remove_invalid_gids (&plan->_glyphset_gsub, plan->source->get_num_glyphs ());
469
470
15.5k
  hb_set_t cur_glyphset = plan->_glyphset_gsub;
471
15.5k
  if (!drop_tables->has (HB_OT_TAG_COLR))
472
0
  {
473
0
    _colr_closure (plan, &cur_glyphset);
474
0
    _remove_invalid_gids (&cur_glyphset, plan->source->get_num_glyphs ());
475
0
  }
476
477
15.5k
  plan->_glyphset_colred = cur_glyphset;
478
479
15.5k
#ifndef HB_NO_VAR_COMPOSITES
480
15.5k
  if (!drop_tables->has (OT::VARC::tableTag))
481
0
  {
482
0
    if (unlikely (!plan->source->table.VARC->closure_glyphs (&cur_glyphset)))
483
0
    {
484
0
      plan->check_success (false);
485
0
      return;
486
0
    }
487
0
    _remove_invalid_gids (&cur_glyphset, plan->source->get_num_glyphs ());
488
0
  }
489
15.5k
#endif
490
491
15.5k
  plan->_glyphset_varced = cur_glyphset;
492
493
15.5k
  _nameid_closure (plan, drop_tables);
494
  /* Populate a full set of glyphs to retain by adding all referenced
495
   * composite glyphs. */
496
15.5k
  if (glyf.has_data ())
497
15.5k
    for (hb_codepoint_t gid : cur_glyphset)
498
477k
      _glyf_add_gid_and_children (glyf, gid, &plan->_glyphset,
499
477k
          cur_glyphset.get_population () * HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH);
500
0
  else
501
0
    plan->_glyphset.union_ (cur_glyphset);
502
15.5k
#ifndef HB_NO_SUBSET_CFF
503
15.5k
  if (!plan->accelerator || plan->accelerator->has_seac)
504
15.5k
  {
505
15.5k
    bool has_seac = false;
506
15.5k
    if (cff->is_valid ())
507
0
      for (hb_codepoint_t gid : cur_glyphset)
508
0
  if (_add_cff_seac_components (*cff, gid, &plan->_glyphset))
509
0
    has_seac = true;
510
15.5k
    plan->has_seac = has_seac;
511
15.5k
  }
512
15.5k
#endif
513
514
15.5k
  _remove_invalid_gids (&plan->_glyphset, plan->source->get_num_glyphs ());
515
516
15.5k
#ifndef HB_NO_VAR
517
15.5k
#ifndef HB_NO_SUBSET_LAYOUT
518
15.5k
  if (!drop_tables->has (HB_OT_TAG_GDEF))
519
0
    collect_layout_variation_indices (plan);
520
15.5k
#endif
521
15.5k
#endif
522
15.5k
}
523
524
static bool
525
_create_glyph_map_gsub (const hb_set_t* glyph_set_gsub,
526
                        const hb_map_t* glyph_map,
527
                        hb_map_t* out,
528
                        hb_vector_t<hb_codepoint_t>* out_flat)
529
15.5k
{
530
15.5k
  hb_codepoint_t max_gid = HB_SET_VALUE_INVALID;
531
15.5k
  hb_set_previous (glyph_set_gsub, &max_gid);
532
15.5k
  unsigned flat_size = max_gid == HB_SET_VALUE_INVALID ? 0 : max_gid + 1;
533
15.5k
  if (unlikely (!out_flat->resize_dirty (flat_size)))
534
0
    return false;
535
15.5k
  hb_memset (out_flat->arrayZ, 0xFF, flat_size * sizeof (hb_codepoint_t));
536
537
15.5k
  out->alloc (glyph_set_gsub->get_population ());
538
15.5k
  for (auto gid : *glyph_set_gsub)
539
477k
  {
540
477k
    hb_codepoint_t new_gid = glyph_map->get (gid);
541
477k
    out->set (gid, new_gid);
542
477k
    out_flat->arrayZ[gid] = new_gid;
543
477k
  }
544
15.5k
  return !out->in_error ();
545
15.5k
}
546
547
static bool
548
_create_old_gid_to_new_gid_map (const hb_face_t *face,
549
        bool     retain_gids,
550
        const hb_set_t  *all_gids_to_retain,
551
                                const hb_map_t  *requested_glyph_map,
552
        hb_map_t  *glyph_map, /* OUT */
553
        hb_map_t  *reverse_glyph_map, /* OUT */
554
        hb_sorted_vector_t<hb_codepoint_pair_t> *new_to_old_gid_list /* OUT */,
555
        unsigned int  *num_glyphs /* OUT */)
556
15.5k
{
557
15.5k
  unsigned pop = all_gids_to_retain->get_population ();
558
15.5k
  reverse_glyph_map->alloc (pop);
559
15.5k
  glyph_map->alloc (pop);
560
15.5k
  new_to_old_gid_list->alloc (pop);
561
562
15.5k
  if (*requested_glyph_map)
563
15.5k
  {
564
15.5k
    hb_set_t new_gids(requested_glyph_map->values());
565
15.5k
    if (new_gids.get_population() != requested_glyph_map->get_population())
566
0
    {
567
0
      DEBUG_MSG (SUBSET, nullptr, "The provided custom glyph mapping is not unique.");
568
0
      return false;
569
0
    }
570
571
15.5k
    if (retain_gids)
572
0
    {
573
0
      DEBUG_MSG (SUBSET, nullptr,
574
0
        "HB_SUBSET_FLAGS_RETAIN_GIDS cannot be set if "
575
0
        "a custom glyph mapping has been provided.");
576
0
      return false;
577
0
    }
578
579
15.5k
    hb_codepoint_t max_glyph = 0;
580
15.5k
    hb_set_t remaining;
581
15.5k
    for (auto old_gid : all_gids_to_retain->iter ())
582
511k
    {
583
511k
      if (old_gid == 0) {
584
15.5k
  new_to_old_gid_list->push (hb_pair<hb_codepoint_t, hb_codepoint_t> (0u, 0u));
585
15.5k
        continue;
586
15.5k
      }
587
588
496k
      hb_codepoint_t* new_gid;
589
496k
      if (!requested_glyph_map->has (old_gid, &new_gid))
590
34.6k
      {
591
34.6k
        remaining.add(old_gid);
592
34.6k
        continue;
593
34.6k
      }
594
595
461k
      if (*new_gid > max_glyph)
596
56.6k
        max_glyph = *new_gid;
597
461k
      new_to_old_gid_list->push (hb_pair (*new_gid, old_gid));
598
461k
    }
599
15.5k
    new_to_old_gid_list->qsort ();
600
601
    // Anything that wasn't mapped by the requested mapping should
602
    // be placed after the requested mapping.
603
15.5k
    for (auto old_gid : remaining)
604
34.6k
      new_to_old_gid_list->push (hb_pair (++max_glyph, old_gid));
605
606
15.5k
    *num_glyphs = max_glyph + 1;
607
15.5k
  }
608
0
  else if (!retain_gids)
609
0
  {
610
0
    + hb_enumerate (hb_iter (all_gids_to_retain), (hb_codepoint_t) 0)
611
0
    | hb_sink (new_to_old_gid_list)
612
0
    ;
613
0
    *num_glyphs = new_to_old_gid_list->length;
614
0
  }
615
0
  else
616
0
  {
617
0
    + hb_iter (all_gids_to_retain)
618
0
    | hb_map ([] (hb_codepoint_t _) {
619
0
    return hb_codepoint_pair_t (_, _);
620
0
        })
621
0
    | hb_sink (new_to_old_gid_list)
622
0
    ;
623
624
0
    hb_codepoint_t max_glyph = HB_SET_VALUE_INVALID;
625
0
    hb_set_previous (all_gids_to_retain, &max_glyph);
626
627
0
    *num_glyphs = max_glyph + 1;
628
0
  }
629
630
15.5k
  reverse_glyph_map->alloc (reverse_glyph_map->get_population () + new_to_old_gid_list->length);
631
15.5k
  + hb_iter (new_to_old_gid_list)
632
15.5k
  | hb_sink (reverse_glyph_map)
633
15.5k
  ;
634
15.5k
  glyph_map->alloc (glyph_map->get_population () + new_to_old_gid_list->length);
635
15.5k
  + hb_iter (new_to_old_gid_list)
636
15.5k
  | hb_map (&hb_codepoint_pair_t::reverse)
637
15.5k
  | hb_sink (glyph_map)
638
15.5k
  ;
639
640
15.5k
  return true;
641
15.5k
}
642
643
hb_subset_plan_t::hb_subset_plan_t (hb_face_t *face,
644
            const hb_subset_input_t *input)
645
15.5k
{
646
15.5k
  successful = true;
647
15.5k
  flags = input->flags;
648
649
15.5k
  unicode_to_new_gid_list.init ();
650
651
15.5k
  name_ids = *input->sets.name_ids;
652
15.5k
  name_languages = *input->sets.name_languages;
653
15.5k
  layout_features = *input->sets.layout_features;
654
15.5k
  layout_scripts = *input->sets.layout_scripts;
655
15.5k
  glyphs_requested = *input->sets.glyphs;
656
15.5k
  drop_tables = *input->sets.drop_tables;
657
15.5k
  no_subset_tables = *input->sets.no_subset_tables;
658
15.5k
  source = hb_face_reference (face);
659
15.5k
  dest = hb_face_builder_create ();
660
661
15.5k
  codepoint_to_glyph = hb_map_create ();
662
15.5k
  glyph_map = hb_map_create ();
663
15.5k
  reverse_glyph_map = hb_map_create ();
664
665
15.5k
  gsub_insert_catch_all_feature_variation_rec = false;
666
15.5k
  gpos_insert_catch_all_feature_variation_rec = false;
667
15.5k
  gdef_varstore_inner_maps.init ();
668
669
15.5k
  user_axes_location = input->axes_location;
670
15.5k
  all_axes_pinned = false;
671
15.5k
  pinned_at_default = true;
672
15.5k
  has_gdef_varstore = false;
673
15.5k
  has_avar2 = false;
674
675
#ifdef HB_EXPERIMENTAL_API
676
  for (auto _ : input->name_table_overrides)
677
  {
678
    hb_bytes_t name_bytes = _.second;
679
    unsigned len = name_bytes.length;
680
    char *name_str = (char *) hb_malloc (len);
681
    if (unlikely (!check_success (name_str)))
682
      break;
683
684
    hb_memcpy (name_str, name_bytes.arrayZ, len);
685
    name_table_overrides.set (_.first, hb_bytes_t (name_str, len));
686
  }
687
#endif
688
689
15.5k
  void* accel = hb_face_get_user_data(face, hb_subset_accelerator_t::user_data_key());
690
691
15.5k
  attach_accelerator_data = input->attach_accelerator_data;
692
15.5k
  force_long_loca = input->force_long_loca;
693
#ifdef HB_EXPERIMENTAL_API
694
  force_long_loca = force_long_loca || (flags & HB_SUBSET_FLAGS_IFTB_REQUIREMENTS);
695
#endif
696
697
15.5k
  if (accel)
698
0
    accelerator = (hb_subset_accelerator_t*) accel;
699
700
15.5k
  if (unlikely (in_error ()))
701
0
    return;
702
703
15.5k
#ifndef HB_NO_VAR_COMPOSITES
704
15.5k
  if (!input->sets.drop_tables->has (OT::VARC::tableTag) &&
705
0
      input->sets.no_subset_tables->has (OT::VARC::tableTag) &&
706
0
      !(input->flags & HB_SUBSET_FLAGS_RETAIN_GIDS) &&
707
0
      face->table.VARC->has_data ())
708
0
  {
709
0
    check_success (false);
710
0
    return;
711
0
  }
712
15.5k
#endif
713
714
15.5k
#ifndef HB_NO_VAR
715
15.5k
  if (!check_success (normalize_axes_location (face, this)))
716
0
      return;
717
15.5k
#endif
718
15.5k
#ifndef HB_NO_VAR_COMPOSITES
719
15.5k
  if (!user_axes_location.is_empty () &&
720
0
      !input->sets.drop_tables->has (OT::VARC::tableTag) &&
721
0
      face->table.VARC->has_data ())
722
0
  {
723
0
    check_success (false);
724
0
    return;
725
0
  }
726
15.5k
#endif
727
728
15.5k
  _populate_unicodes_to_retain (input->sets.unicodes, input->sets.glyphs, this);
729
730
15.5k
  _populate_gids_to_retain (this, input->sets.drop_tables);
731
15.5k
  if (unlikely (in_error ()))
732
0
    return;
733
734
15.5k
  if (!check_success(_create_old_gid_to_new_gid_map(
735
15.5k
          face,
736
15.5k
          input->flags & HB_SUBSET_FLAGS_RETAIN_GIDS,
737
15.5k
          &_glyphset,
738
15.5k
          &input->glyph_map,
739
15.5k
          glyph_map,
740
15.5k
          reverse_glyph_map,
741
15.5k
    &new_to_old_gid_list,
742
15.5k
          &_num_output_glyphs))) {
743
0
    return;
744
0
  }
745
746
#ifdef HB_EXPERIMENTAL_API  
747
  if ((input->flags & HB_SUBSET_FLAGS_RETAIN_GIDS) &&
748
      (input->flags & HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS)) {
749
    // We've been requested to maintain the num glyphs count from the
750
    // input face.
751
    _num_output_glyphs = source->get_num_glyphs ();
752
  }
753
#endif
754
755
15.5k
  if (!check_success (_create_glyph_map_gsub (
756
15.5k
      &_glyphset_gsub,
757
15.5k
      glyph_map,
758
15.5k
      &glyph_map_gsub,
759
15.5k
      &glyph_map_gsub_flat)))
760
0
    return;
761
762
  // Now that we have old to new gid map update the unicode to new gid list.
763
477k
  for (unsigned i = 0; i < unicode_to_new_gid_list.length; i++)
764
461k
  {
765
    // Use raw array access for performance.
766
461k
    unicode_to_new_gid_list.arrayZ[i].second =
767
461k
        glyph_map->get(unicode_to_new_gid_list.arrayZ[i].second);
768
461k
  }
769
770
15.5k
  bounds_width_vec.resize_dirty  (_num_output_glyphs);
771
15.5k
  for (auto &v : bounds_width_vec)
772
515k
    v = 0xFFFFFFFF;
773
15.5k
  bounds_height_vec.resize_dirty  (_num_output_glyphs);
774
15.5k
  for (auto &v : bounds_height_vec)
775
515k
    v = 0xFFFFFFFF;
776
777
15.5k
#ifndef HB_NO_SUBSET_LAYOUT    
778
15.5k
  if (!drop_tables.has (HB_OT_TAG_GDEF))
779
0
    remap_used_mark_sets (this, used_mark_sets_map);
780
15.5k
#endif
781
782
15.5k
#ifndef HB_NO_VAR
783
15.5k
#ifndef HB_NO_BASE
784
15.5k
  if (!drop_tables.has (HB_OT_TAG_BASE))
785
0
    collect_base_variation_indices (this);
786
15.5k
#endif
787
15.5k
#endif
788
789
15.5k
  if (unlikely (in_error ()))
790
0
    return;
791
792
15.5k
#if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_CFF)
793
15.5k
  update_instance_metrics_map_from_cff2 (this);
794
15.5k
#endif
795
15.5k
#ifndef HB_NO_VAR
796
15.5k
  if (!check_success (get_instance_glyphs_contour_points (this)))
797
0
      return;
798
15.5k
#endif
799
800
15.5k
  if (attach_accelerator_data)
801
0
  {
802
0
    inprogress_accelerator =
803
0
      hb_subset_accelerator_t::create (source,
804
0
               *codepoint_to_glyph,
805
0
                                       unicodes,
806
0
               has_seac);
807
808
0
    check_success (inprogress_accelerator);
809
0
  }
810
811
950k
#define HB_SUBSET_PLAN_MEMBER(Type, Name) check_success (!Name.in_error ());
812
15.5k
#include "hb-subset-plan-member-list.hh"
813
15.5k
#undef HB_SUBSET_PLAN_MEMBER
814
15.5k
}
815
816
hb_subset_plan_t::~hb_subset_plan_t()
817
15.5k
{
818
15.5k
  hb_face_destroy (dest);
819
820
15.5k
  hb_map_destroy (codepoint_to_glyph);
821
15.5k
  hb_map_destroy (glyph_map);
822
15.5k
  hb_map_destroy (reverse_glyph_map);
823
15.5k
#ifndef HB_NO_SUBSET_CFF
824
15.5k
  cff1_accel.fini ();
825
15.5k
  cff2_accel.fini ();
826
15.5k
#endif
827
15.5k
  hb_face_destroy (source);
828
829
#ifdef HB_EXPERIMENTAL_API
830
  for (auto _ : name_table_overrides.iter_ref ())
831
    _.second.fini ();
832
#endif
833
834
15.5k
  if (inprogress_accelerator)
835
0
    hb_subset_accelerator_t::destroy ((void*) inprogress_accelerator);
836
15.5k
}
837
838
839
/**
840
 * hb_subset_plan_create_or_fail:
841
 * @face: font face to create the plan for.
842
 * @input: a #hb_subset_input_t input.
843
 *
844
 * Computes a plan for subsetting the supplied face according
845
 * to a provided input. The plan describes
846
 * which tables and glyphs should be retained.
847
 *
848
 * Return value: (transfer full): New subset plan. Destroy with
849
 * hb_subset_plan_destroy(). If there is a failure creating the plan
850
 * nullptr will be returned.
851
 *
852
 * Since: 4.0.0
853
 **/
854
hb_subset_plan_t *
855
hb_subset_plan_create_or_fail (hb_face_t   *face,
856
                               const hb_subset_input_t *input)
857
15.5k
{
858
15.5k
  hb_subset_plan_t *plan;
859
15.5k
  if (unlikely (!(plan = hb_object_create<hb_subset_plan_t> (face, input))))
860
0
    return nullptr;
861
862
15.5k
  if (unlikely (plan->in_error ()))
863
0
  {
864
0
    hb_subset_plan_destroy (plan);
865
0
    return nullptr;
866
0
  }
867
868
15.5k
  return plan;
869
15.5k
}
870
871
/**
872
 * hb_subset_plan_destroy:
873
 * @plan: a #hb_subset_plan_t
874
 *
875
 * Decreases the reference count on @plan, and if it reaches zero, destroys
876
 * @plan, freeing all memory.
877
 *
878
 * Since: 4.0.0
879
 **/
880
void
881
hb_subset_plan_destroy (hb_subset_plan_t *plan)
882
15.5k
{
883
15.5k
  if (!hb_object_destroy (plan)) return;
884
885
15.5k
  hb_free (plan);
886
15.5k
}
887
888
/**
889
 * hb_subset_plan_old_to_new_glyph_mapping:
890
 * @plan: a subsetting plan.
891
 *
892
 * Returns the mapping between glyphs in the original font to glyphs in the
893
 * subset that will be produced by @plan
894
 *
895
 * Return value: (transfer none):
896
 * A pointer to the #hb_map_t of the mapping.
897
 *
898
 * Since: 4.0.0
899
 **/
900
hb_map_t *
901
hb_subset_plan_old_to_new_glyph_mapping (const hb_subset_plan_t *plan)
902
0
{
903
0
  return plan->glyph_map;
904
0
}
905
906
/**
907
 * hb_subset_plan_new_to_old_glyph_mapping:
908
 * @plan: a subsetting plan.
909
 *
910
 * Returns the mapping between glyphs in the subset that will be produced by
911
 * @plan and the glyph in the original font.
912
 *
913
 * Return value: (transfer none):
914
 * A pointer to the #hb_map_t of the mapping.
915
 *
916
 * Since: 4.0.0
917
 **/
918
hb_map_t *
919
hb_subset_plan_new_to_old_glyph_mapping (const hb_subset_plan_t *plan)
920
0
{
921
0
  return plan->reverse_glyph_map;
922
0
}
923
924
/**
925
 * hb_subset_plan_unicode_to_old_glyph_mapping:
926
 * @plan: a subsetting plan.
927
 *
928
 * Returns the mapping between codepoints in the original font and the
929
 * associated glyph id in the original font.
930
 *
931
 * Return value: (transfer none):
932
 * A pointer to the #hb_map_t of the mapping.
933
 *
934
 * Since: 4.0.0
935
 **/
936
hb_map_t *
937
hb_subset_plan_unicode_to_old_glyph_mapping (const hb_subset_plan_t *plan)
938
0
{
939
0
  return plan->codepoint_to_glyph;
940
0
}
941
942
/**
943
 * hb_subset_plan_reference: (skip)
944
 * @plan: a #hb_subset_plan_t object.
945
 *
946
 * Increases the reference count on @plan.
947
 *
948
 * Return value: @plan.
949
 *
950
 * Since: 4.0.0
951
 **/
952
hb_subset_plan_t *
953
hb_subset_plan_reference (hb_subset_plan_t *plan)
954
0
{
955
0
  return hb_object_reference (plan);
956
0
}
957
958
/**
959
 * hb_subset_plan_set_user_data: (skip)
960
 * @plan: a #hb_subset_plan_t object.
961
 * @key: The user-data key to set
962
 * @data: A pointer to the user data
963
 * @destroy: (nullable): A callback to call when @data is not needed anymore
964
 * @replace: Whether to replace an existing data with the same key
965
 *
966
 * Attaches a user-data key/data pair to the given subset plan object.
967
 *
968
 * Return value: `true` if success, `false` otherwise
969
 *
970
 * Since: 4.0.0
971
 **/
972
hb_bool_t
973
hb_subset_plan_set_user_data (hb_subset_plan_t   *plan,
974
                              hb_user_data_key_t *key,
975
                              void               *data,
976
                              hb_destroy_func_t   destroy,
977
                              hb_bool_t           replace)
978
0
{
979
0
  return hb_object_set_user_data (plan, key, data, destroy, replace);
980
0
}
981
982
/**
983
 * hb_subset_plan_get_user_data: (skip)
984
 * @plan: a #hb_subset_plan_t object.
985
 * @key: The user-data key to query
986
 *
987
 * Fetches the user data associated with the specified key,
988
 * attached to the specified subset plan object.
989
 *
990
 * Return value: (transfer none): A pointer to the user data
991
 *
992
 * Since: 4.0.0
993
 **/
994
void *
995
hb_subset_plan_get_user_data (const hb_subset_plan_t *plan,
996
                              hb_user_data_key_t     *key)
997
0
{
998
0
  return hb_object_get_user_data (plan, key);
999
0
}