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-subset-input.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, Rod Sheeter, Behdad Esfahbod
25
 */
26
27
#include "hb-subset-instancer-solver.hh"
28
#include "hb-subset.hh"
29
#include "hb-set.hh"
30
#include "hb-utf.hh"
31
32
33
hb_subset_input_t::hb_subset_input_t ()
34
20.3k
{
35
20.3k
  for (auto& set : sets_iter ())
36
162k
    set = hb::shared_ptr<hb_set_t> (hb_set_create ());
37
38
20.3k
  if (in_error ())
39
0
    return;
40
41
20.3k
  flags = HB_SUBSET_FLAGS_DEFAULT;
42
43
20.3k
  hb_set_add_range (sets.name_ids, 0, 6);
44
20.3k
  hb_set_add (sets.name_languages, 0x0409);
45
46
20.3k
  hb_tag_t default_drop_tables[] = {
47
    // Layout disabled by default
48
20.3k
    HB_TAG ('m', 'o', 'r', 'x'),
49
20.3k
    HB_TAG ('m', 'o', 'r', 't'),
50
20.3k
    HB_TAG ('k', 'e', 'r', 'x'),
51
20.3k
    HB_TAG ('k', 'e', 'r', 'n'),
52
53
    // Copied from fontTools:
54
20.3k
    HB_TAG ('J', 'S', 'T', 'F'),
55
20.3k
    HB_TAG ('D', 'S', 'I', 'G'),
56
20.3k
    HB_TAG ('E', 'B', 'D', 'T'),
57
20.3k
    HB_TAG ('E', 'B', 'L', 'C'),
58
20.3k
    HB_TAG ('E', 'B', 'S', 'C'),
59
20.3k
    HB_TAG ('S', 'V', 'G', ' '),
60
20.3k
    HB_TAG ('P', 'C', 'L', 'T'),
61
20.3k
    HB_TAG ('L', 'T', 'S', 'H'),
62
    // Graphite tables
63
20.3k
    HB_TAG ('F', 'e', 'a', 't'),
64
20.3k
    HB_TAG ('G', 'l', 'a', 't'),
65
20.3k
    HB_TAG ('G', 'l', 'o', 'c'),
66
20.3k
    HB_TAG ('S', 'i', 'l', 'f'),
67
20.3k
    HB_TAG ('S', 'i', 'l', 'l'),
68
20.3k
  };
69
20.3k
  sets.drop_tables->add_array (default_drop_tables, ARRAY_LENGTH (default_drop_tables));
70
71
20.3k
  hb_tag_t default_no_subset_tables[] = {
72
20.3k
    HB_TAG ('g', 'a', 's', 'p'),
73
20.3k
    HB_TAG ('f', 'p', 'g', 'm'),
74
20.3k
    HB_TAG ('p', 'r', 'e', 'p'),
75
20.3k
    HB_TAG ('V', 'D', 'M', 'X'),
76
20.3k
    HB_TAG ('D', 'S', 'I', 'G'),
77
20.3k
  };
78
20.3k
  sets.no_subset_tables->add_array (default_no_subset_tables,
79
20.3k
           ARRAY_LENGTH (default_no_subset_tables));
80
81
  //copied from _layout_features_groups in fonttools
82
20.3k
  hb_tag_t default_layout_features[] = {
83
    // default shaper
84
    // common
85
20.3k
    HB_TAG ('r', 'v', 'r', 'n'),
86
20.3k
    HB_TAG ('c', 'c', 'm', 'p'),
87
20.3k
    HB_TAG ('l', 'i', 'g', 'a'),
88
20.3k
    HB_TAG ('l', 'o', 'c', 'l'),
89
20.3k
    HB_TAG ('m', 'a', 'r', 'k'),
90
20.3k
    HB_TAG ('m', 'k', 'm', 'k'),
91
20.3k
    HB_TAG ('r', 'l', 'i', 'g'),
92
93
    //fractions
94
20.3k
    HB_TAG ('f', 'r', 'a', 'c'),
95
20.3k
    HB_TAG ('n', 'u', 'm', 'r'),
96
20.3k
    HB_TAG ('d', 'n', 'o', 'm'),
97
98
    //horizontal
99
20.3k
    HB_TAG ('c', 'a', 'l', 't'),
100
20.3k
    HB_TAG ('c', 'l', 'i', 'g'),
101
20.3k
    HB_TAG ('c', 'u', 'r', 's'),
102
20.3k
    HB_TAG ('k', 'e', 'r', 'n'),
103
20.3k
    HB_TAG ('r', 'c', 'l', 't'),
104
105
    //vertical
106
20.3k
    HB_TAG ('v', 'a', 'l', 't'),
107
20.3k
    HB_TAG ('v', 'e', 'r', 't'),
108
20.3k
    HB_TAG ('v', 'k', 'r', 'n'),
109
20.3k
    HB_TAG ('v', 'p', 'a', 'l'),
110
20.3k
    HB_TAG ('v', 'r', 't', '2'),
111
112
    //ltr
113
20.3k
    HB_TAG ('l', 't', 'r', 'a'),
114
20.3k
    HB_TAG ('l', 't', 'r', 'm'),
115
116
    //rtl
117
20.3k
    HB_TAG ('r', 't', 'l', 'a'),
118
20.3k
    HB_TAG ('r', 't', 'l', 'm'),
119
120
    //random
121
20.3k
    HB_TAG ('r', 'a', 'n', 'd'),
122
123
    //justify
124
20.3k
    HB_TAG ('j', 'a', 'l', 't'), // HarfBuzz doesn't use; others might
125
126
    //East Asian spacing
127
20.3k
    HB_TAG ('c', 'h', 'w', 's'),
128
20.3k
    HB_TAG ('v', 'c', 'h', 'w'),
129
20.3k
    HB_TAG ('h', 'a', 'l', 't'),
130
20.3k
    HB_TAG ('v', 'h', 'a', 'l'),
131
20.3k
    HB_TAG ('p', 'a', 'l', 't'),
132
133
    //private
134
20.3k
    HB_TAG ('H', 'a', 'r', 'f'),
135
20.3k
    HB_TAG ('H', 'A', 'R', 'F'),
136
20.3k
    HB_TAG ('B', 'u', 'z', 'z'),
137
20.3k
    HB_TAG ('B', 'U', 'Z', 'Z'),
138
139
    //shapers
140
141
    //arabic
142
20.3k
    HB_TAG ('i', 'n', 'i', 't'),
143
20.3k
    HB_TAG ('m', 'e', 'd', 'i'),
144
20.3k
    HB_TAG ('f', 'i', 'n', 'a'),
145
20.3k
    HB_TAG ('i', 's', 'o', 'l'),
146
20.3k
    HB_TAG ('m', 'e', 'd', '2'),
147
20.3k
    HB_TAG ('f', 'i', 'n', '2'),
148
20.3k
    HB_TAG ('f', 'i', 'n', '3'),
149
20.3k
    HB_TAG ('c', 's', 'w', 'h'),
150
20.3k
    HB_TAG ('m', 's', 'e', 't'),
151
20.3k
    HB_TAG ('s', 't', 'c', 'h'),
152
153
    //hangul
154
20.3k
    HB_TAG ('l', 'j', 'm', 'o'),
155
20.3k
    HB_TAG ('v', 'j', 'm', 'o'),
156
20.3k
    HB_TAG ('t', 'j', 'm', 'o'),
157
158
    //tibetan
159
20.3k
    HB_TAG ('a', 'b', 'v', 's'),
160
20.3k
    HB_TAG ('b', 'l', 'w', 's'),
161
20.3k
    HB_TAG ('a', 'b', 'v', 'm'),
162
20.3k
    HB_TAG ('b', 'l', 'w', 'm'),
163
164
    //indic
165
20.3k
    HB_TAG ('n', 'u', 'k', 't'),
166
20.3k
    HB_TAG ('a', 'k', 'h', 'n'),
167
20.3k
    HB_TAG ('r', 'p', 'h', 'f'),
168
20.3k
    HB_TAG ('r', 'k', 'r', 'f'),
169
20.3k
    HB_TAG ('p', 'r', 'e', 'f'),
170
20.3k
    HB_TAG ('b', 'l', 'w', 'f'),
171
20.3k
    HB_TAG ('h', 'a', 'l', 'f'),
172
20.3k
    HB_TAG ('a', 'b', 'v', 'f'),
173
20.3k
    HB_TAG ('p', 's', 't', 'f'),
174
20.3k
    HB_TAG ('c', 'f', 'a', 'r'),
175
20.3k
    HB_TAG ('v', 'a', 't', 'u'),
176
20.3k
    HB_TAG ('c', 'j', 'c', 't'),
177
20.3k
    HB_TAG ('i', 'n', 'i', 't'),
178
20.3k
    HB_TAG ('p', 'r', 'e', 's'),
179
20.3k
    HB_TAG ('a', 'b', 'v', 's'),
180
20.3k
    HB_TAG ('b', 'l', 'w', 's'),
181
20.3k
    HB_TAG ('p', 's', 't', 's'),
182
20.3k
    HB_TAG ('h', 'a', 'l', 'n'),
183
20.3k
    HB_TAG ('d', 'i', 's', 't'),
184
20.3k
    HB_TAG ('a', 'b', 'v', 'm'),
185
20.3k
    HB_TAG ('b', 'l', 'w', 'm'),
186
20.3k
  };
187
188
20.3k
  sets.layout_features->add_array (default_layout_features, ARRAY_LENGTH (default_layout_features));
189
190
20.3k
  sets.layout_scripts->invert (); // Default to all scripts.
191
20.3k
}
192
193
/**
194
 * hb_subset_input_create_or_fail:
195
 *
196
 * Creates a new subset input object.
197
 *
198
 * Return value: (transfer full): New subset input, or `NULL` if failed. Destroy
199
 * with hb_subset_input_destroy().
200
 *
201
 * Since: 1.8.0
202
 **/
203
hb_subset_input_t *
204
hb_subset_input_create_or_fail (void)
205
20.3k
{
206
20.3k
  hb_subset_input_t *input = hb_object_create<hb_subset_input_t>();
207
208
20.3k
  if (unlikely (!input))
209
0
    return nullptr;
210
211
20.3k
  if (input->in_error ())
212
0
  {
213
0
    hb_subset_input_destroy (input);
214
0
    return nullptr;
215
0
  }
216
217
20.3k
  return input;
218
20.3k
}
219
220
/**
221
 * hb_subset_input_reference: (skip)
222
 * @input: a #hb_subset_input_t object.
223
 *
224
 * Increases the reference count on @input.
225
 *
226
 * Return value: @input.
227
 *
228
 * Since: 1.8.0
229
 **/
230
hb_subset_input_t *
231
hb_subset_input_reference (hb_subset_input_t *input)
232
0
{
233
0
  return hb_object_reference (input);
234
0
}
235
236
/**
237
 * hb_subset_input_destroy:
238
 * @input: a #hb_subset_input_t object.
239
 *
240
 * Decreases the reference count on @input, and if it reaches zero, destroys
241
 * @input, freeing all memory.
242
 *
243
 * Since: 1.8.0
244
 **/
245
void
246
hb_subset_input_destroy (hb_subset_input_t *input)
247
20.3k
{
248
20.3k
  if (!hb_object_destroy (input)) return;
249
250
20.3k
  hb_free (input);
251
20.3k
}
252
253
/**
254
 * hb_subset_input_unicode_set:
255
 * @input: a #hb_subset_input_t object.
256
 *
257
 * Gets the set of Unicode code points to retain, the caller should modify the
258
 * set as needed.
259
 *
260
 * Return value: (transfer none): pointer to the #hb_set_t of Unicode code
261
 * points.
262
 *
263
 * Since: 1.8.0
264
 **/
265
HB_EXTERN hb_set_t *
266
hb_subset_input_unicode_set (hb_subset_input_t *input)
267
0
{
268
0
  return input->sets.unicodes;
269
0
}
270
271
/**
272
 * hb_subset_input_glyph_set:
273
 * @input: a #hb_subset_input_t object.
274
 *
275
 * Gets the set of glyph IDs to retain, the caller should modify the set as
276
 * needed.
277
 *
278
 * Return value: (transfer none): pointer to the #hb_set_t of glyph IDs.
279
 *
280
 * Since: 1.8.0
281
 **/
282
HB_EXTERN hb_set_t *
283
hb_subset_input_glyph_set (hb_subset_input_t *input)
284
20.3k
{
285
20.3k
  return input->sets.glyphs;
286
20.3k
}
287
288
/**
289
 * hb_subset_input_set:
290
 * @input: a #hb_subset_input_t object.
291
 * @set_type: a #hb_subset_sets_t set type.
292
 *
293
 * Gets the set of the specified type.
294
 *
295
 * Return value: (transfer none): pointer to the #hb_set_t of the specified type.
296
 *
297
 * Since: 2.9.1
298
 **/
299
HB_EXTERN hb_set_t *
300
hb_subset_input_set (hb_subset_input_t *input, hb_subset_sets_t set_type)
301
20.3k
{
302
20.3k
  return input->sets_iter () [set_type];
303
20.3k
}
304
305
/**
306
 * hb_subset_input_get_flags:
307
 * @input: a #hb_subset_input_t object.
308
 *
309
 * Gets all of the subsetting flags in the input object.
310
 *
311
 * Return value: the subsetting flags bit field.
312
 *
313
 * Since: 2.9.0
314
 **/
315
HB_EXTERN hb_subset_flags_t
316
hb_subset_input_get_flags (hb_subset_input_t *input)
317
0
{
318
0
  return (hb_subset_flags_t) input->flags;
319
0
}
320
321
/**
322
 * hb_subset_input_set_flags:
323
 * @input: a #hb_subset_input_t object.
324
 * @value: bit field of flags
325
 *
326
 * Sets all of the flags in the input object to the values specified by the bit
327
 * field.
328
 *
329
 * Since: 2.9.0
330
 **/
331
HB_EXTERN void
332
hb_subset_input_set_flags (hb_subset_input_t *input,
333
         unsigned value)
334
20.3k
{
335
20.3k
  input->flags = (hb_subset_flags_t) value;
336
20.3k
}
337
338
/**
339
 * hb_subset_input_set_user_data: (skip)
340
 * @input: a #hb_subset_input_t object.
341
 * @key: The user-data key to set
342
 * @data: A pointer to the user data
343
 * @destroy: (nullable): A callback to call when @data is not needed anymore
344
 * @replace: Whether to replace an existing data with the same key
345
 *
346
 * Attaches a user-data key/data pair to the given subset input object.
347
 *
348
 * Return value: `true` if success, `false` otherwise
349
 *
350
 * Since: 2.9.0
351
 **/
352
hb_bool_t
353
hb_subset_input_set_user_data (hb_subset_input_t  *input,
354
             hb_user_data_key_t *key,
355
             void *      data,
356
             hb_destroy_func_t   destroy,
357
             hb_bool_t     replace)
358
0
{
359
0
  return hb_object_set_user_data (input, key, data, destroy, replace);
360
0
}
361
362
/**
363
 * hb_subset_input_get_user_data: (skip)
364
 * @input: a #hb_subset_input_t object.
365
 * @key: The user-data key to query
366
 *
367
 * Fetches the user data associated with the specified key,
368
 * attached to the specified subset input object.
369
 *
370
 * Return value: (transfer none): A pointer to the user data
371
 *
372
 * Since: 2.9.0
373
 **/
374
void *
375
hb_subset_input_get_user_data (const hb_subset_input_t *input,
376
             hb_user_data_key_t     *key)
377
0
{
378
0
  return hb_object_get_user_data (input, key);
379
0
}
380
381
/**
382
 * hb_subset_input_keep_everything:
383
 * @input: a #hb_subset_input_t object
384
 *
385
 * Configure input object to keep everything in the font face.
386
 * That is, all Unicodes, glyphs, names, layout items,
387
 * glyph names, etc.
388
 *
389
 * The input can be tailored afterwards by the caller.
390
 *
391
 * Since: 7.0.0
392
 */
393
void
394
hb_subset_input_keep_everything (hb_subset_input_t *input)
395
0
{
396
0
  const hb_subset_sets_t indices[] = {HB_SUBSET_SETS_UNICODE,
397
0
              HB_SUBSET_SETS_GLYPH_INDEX,
398
0
              HB_SUBSET_SETS_NAME_ID,
399
0
              HB_SUBSET_SETS_NAME_LANG_ID,
400
0
              HB_SUBSET_SETS_LAYOUT_FEATURE_TAG,
401
0
              HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG};
402
403
0
  for (auto idx : hb_iter (indices))
404
0
  {
405
0
    hb_set_t *set = hb_subset_input_set (input, idx);
406
0
    hb_set_clear (set);
407
0
    hb_set_invert (set);
408
0
  }
409
410
  // Don't drop any tables
411
0
  hb_set_clear (hb_subset_input_set (input, HB_SUBSET_SETS_DROP_TABLE_TAG));
412
413
0
  hb_subset_input_set_flags (input,
414
0
           HB_SUBSET_FLAGS_NOTDEF_OUTLINE |
415
0
           HB_SUBSET_FLAGS_GLYPH_NAMES |
416
0
           HB_SUBSET_FLAGS_NAME_LEGACY |
417
0
           HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES |
418
0
                             HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED);
419
0
}
420
421
#ifndef HB_NO_VAR
422
/**
423
 * hb_subset_input_pin_all_axes_to_default: (skip)
424
 * @input: a #hb_subset_input_t object.
425
 * @face: a #hb_face_t object.
426
 *
427
 * Pin all axes to default locations in the given subset input object.
428
 *
429
 * The `CFF2` table, if present, will be de-subroutinized.
430
 *
431
 * Return value: `true` if success, `false` otherwise
432
 *
433
 * Since: 8.3.1
434
 **/
435
HB_EXTERN hb_bool_t
436
hb_subset_input_pin_all_axes_to_default (hb_subset_input_t  *input,
437
                                         hb_face_t          *face)
438
0
{
439
0
  unsigned axis_count = hb_ot_var_get_axis_count (face);
440
0
  if (!axis_count) return false;
441
442
0
  hb_ot_var_axis_info_t *axis_infos = (hb_ot_var_axis_info_t *) hb_calloc (axis_count, sizeof (hb_ot_var_axis_info_t));
443
0
  if (unlikely (!axis_infos)) return false;
444
445
0
  (void) hb_ot_var_get_axis_infos (face, 0, &axis_count, axis_infos);
446
447
0
  for (unsigned i = 0; i < axis_count; i++)
448
0
  {
449
0
    hb_tag_t axis_tag = axis_infos[i].tag;
450
0
    double default_val = (double) axis_infos[i].default_value;
451
0
    if (!input->axes_location.set (axis_tag, Triple (default_val, default_val, default_val)))
452
0
    {
453
0
      hb_free (axis_infos);
454
0
      return false;
455
0
    }
456
0
  }
457
0
  hb_free (axis_infos);
458
0
  return true;
459
0
}
460
461
/**
462
 * hb_subset_input_pin_axis_to_default: (skip)
463
 * @input: a #hb_subset_input_t object.
464
 * @face: a #hb_face_t object.
465
 * @axis_tag: Tag of the axis to be pinned
466
 *
467
 * Pin an axis to its default location in the given subset input object.
468
 *
469
 * The `CFF2` table, if present, will be de-subroutinized.
470
 *
471
 * Return value: `true` if success, `false` otherwise
472
 *
473
 * Since: 6.0.0
474
 **/
475
HB_EXTERN hb_bool_t
476
hb_subset_input_pin_axis_to_default (hb_subset_input_t  *input,
477
                                     hb_face_t          *face,
478
                                     hb_tag_t            axis_tag)
479
0
{
480
0
  hb_ot_var_axis_info_t axis_info;
481
0
  if (!hb_ot_var_find_axis_info (face, axis_tag, &axis_info))
482
0
    return false;
483
484
0
  double default_val = (double) axis_info.default_value;
485
0
  return input->axes_location.set (axis_tag, Triple (default_val, default_val, default_val));
486
0
}
487
488
/**
489
 * hb_subset_input_pin_axis_location: (skip)
490
 * @input: a #hb_subset_input_t object.
491
 * @face: a #hb_face_t object.
492
 * @axis_tag: Tag of the axis to be pinned
493
 * @axis_value: Location on the axis to be pinned at
494
 *
495
 * Pin an axis to a fixed location in the given subset input object.
496
 *
497
 * The `CFF2` table, if present, will be de-subroutinized.
498
 *
499
 * Return value: `true` if success, `false` otherwise
500
 *
501
 * Since: 6.0.0
502
 **/
503
HB_EXTERN hb_bool_t
504
hb_subset_input_pin_axis_location (hb_subset_input_t  *input,
505
                                   hb_face_t          *face,
506
                                   hb_tag_t            axis_tag,
507
                                   float               axis_value)
508
0
{
509
0
  hb_ot_var_axis_info_t axis_info;
510
0
  if (!hb_ot_var_find_axis_info (face, axis_tag, &axis_info))
511
0
    return false;
512
513
0
  double val = hb_clamp((double) axis_value, (double) axis_info.min_value, (double) axis_info.max_value);
514
0
  return input->axes_location.set (axis_tag, Triple (val, val, val));
515
0
}
516
517
/**
518
 * hb_subset_input_set_axis_range: (skip)
519
 * @input: a #hb_subset_input_t object.
520
 * @face: a #hb_face_t object.
521
 * @axis_tag: Tag of the axis
522
 * @axis_min_value: Minimum value of the axis variation range to set, if NaN the existing min will be used.
523
 * @axis_max_value: Maximum value of the axis variation range to set  if NaN the existing max will be used.
524
 * @axis_def_value: Default value of the axis variation range to set, if NaN the existing default will be used.
525
 *
526
 * Restricting the range of variation on an axis in the given subset input object.
527
 * New min/default/max values will be clamped if they're not within the fvar axis range.
528
 *
529
 * If the fvar axis default value is not within the new range, the new default
530
 * value will be changed to the new min or max value, whichever is closer to the fvar
531
 * axis default.
532
 *
533
 * Note: input min value can not be bigger than input max value. If the input
534
 * default value is not within the new min/max range, it'll be clamped.
535
 *
536
 * Return value: `true` if success, `false` otherwise
537
 *
538
 * Since: 8.5.0
539
 **/
540
HB_EXTERN hb_bool_t
541
hb_subset_input_set_axis_range (hb_subset_input_t  *input,
542
                                hb_face_t          *face,
543
                                hb_tag_t            axis_tag,
544
                                float               axis_min_value,
545
                                float               axis_max_value,
546
                                float               axis_def_value)
547
0
{
548
0
  hb_ot_var_axis_info_t axis_info;
549
0
  if (!hb_ot_var_find_axis_info (face, axis_tag, &axis_info))
550
0
    return false;
551
552
0
  float min = !std::isnan(axis_min_value) ? axis_min_value : axis_info.min_value;
553
0
  float max = !std::isnan(axis_max_value) ? axis_max_value : axis_info.max_value;
554
0
  float def = !std::isnan(axis_def_value) ? axis_def_value : axis_info.default_value;
555
556
0
  if (min > max)
557
0
    return false;
558
559
0
  float new_min_val = hb_clamp(min, axis_info.min_value, axis_info.max_value);
560
0
  float new_max_val = hb_clamp(max, axis_info.min_value, axis_info.max_value);
561
0
  float new_default_val = hb_clamp(def, new_min_val, new_max_val);
562
0
  return input->axes_location.set (axis_tag, Triple ((double) new_min_val, (double) new_default_val, (double) new_max_val));
563
0
}
564
565
/**
566
 * hb_subset_input_get_axis_range: (skip)
567
 * @input: a #hb_subset_input_t object.
568
 * @axis_tag: Tag of the axis
569
 * @axis_min_value: Set to the previously configured minimum value of the axis variation range.
570
 * @axis_max_value: Set to the previously configured maximum value of the axis variation range.
571
 * @axis_def_value: Set to the previously configured default value of the axis variation range.
572
 *
573
 * Gets the axis range assigned by previous calls to hb_subset_input_set_axis_range.
574
 *
575
 * Return value: `true` if a range has been set for this axis tag, `false` otherwise.
576
 *
577
 * Since: 8.5.0
578
 **/
579
HB_EXTERN hb_bool_t
580
hb_subset_input_get_axis_range (hb_subset_input_t  *input,
581
        hb_tag_t            axis_tag,
582
        float              *axis_min_value,
583
        float              *axis_max_value,
584
        float              *axis_def_value)
585
586
0
{
587
0
  Triple* triple;
588
0
  if (!input->axes_location.has(axis_tag, &triple)) {
589
0
    return false;
590
0
  }
591
592
0
  *axis_min_value = triple->minimum;
593
0
  *axis_def_value = triple->middle;
594
0
  *axis_max_value = triple->maximum;
595
0
  return true;
596
0
}
597
598
/**
599
 * hb_subset_axis_range_from_string:
600
 * @str: a string to parse
601
 * @len: length of @str, or -1 if str is NULL terminated
602
 * @axis_min_value: (out): the axis min value to initialize with the parsed value
603
 * @axis_max_value: (out): the axis max value to initialize with the parsed value
604
 * @axis_def_value: (out): the axis default value to initialize with the parse
605
 * value
606
 *
607
 * Parses a string into a subset axis range(min, def, max).
608
 * Axis positions string is in the format of min:def:max or min:max
609
 * When parsing axis positions, empty values as meaning the existing value for that part
610
 * E.g: :300:500
611
 * Specifies min = existing, def = 300, max = 500
612
 * In the output axis_range, if a value should be set to it's default value,
613
 * then it will be set to NaN
614
 *
615
 * Return value:
616
 * `true` if @str is successfully parsed, `false` otherwise
617
 *
618
 * Since: 10.2.0
619
 */
620
HB_EXTERN hb_bool_t
621
hb_subset_axis_range_from_string (const char *str, int len,
622
                                  float *axis_min_value,
623
                                  float *axis_max_value,
624
                                  float *axis_def_value)
625
0
{
626
0
  if (len < 0)
627
0
    len = strlen (str);
628
629
0
  const char *end = str + len;
630
0
  const char* part = strpbrk (str, ":");
631
0
  if (!part)
632
0
  {
633
    // Single value.
634
0
    if (strcmp (str, "drop") == 0)
635
0
    {
636
0
      *axis_min_value = NAN;
637
0
      *axis_def_value = NAN;
638
0
      *axis_max_value = NAN;
639
0
      return true;
640
0
    }
641
642
0
    double v;
643
0
    if (!hb_parse_double (&str, end, &v)) return false;
644
645
0
    *axis_min_value = v;
646
0
    *axis_def_value = v;
647
0
    *axis_max_value = v;
648
0
    return true;
649
0
  }
650
651
0
  float values[3];
652
0
  int count = 0;
653
0
  for (int i = 0; i < 3; i++) {
654
0
    count++;
655
0
    if (!*str || part == str)
656
0
    {
657
0
      values[i] = NAN;
658
659
0
      if (part == NULL) break;
660
0
      str = part + 1;
661
0
      part = strpbrk (str, ":");
662
0
      continue;
663
0
    }
664
665
0
    double v;
666
0
    const char *piece_end = part ? part : end;
667
0
    if (!hb_parse_double (&str, piece_end, &v)) return false;
668
0
    values[i] = v;
669
670
0
    if (part == NULL) break;
671
0
    str = part + 1;
672
0
    part = strpbrk (str, ":");
673
0
  }
674
675
0
  if (count == 2)
676
0
  {
677
0
    *axis_min_value = values[0];
678
0
    *axis_def_value = NAN;
679
0
    *axis_max_value = values[1];
680
0
    return true;
681
0
  }
682
0
  else if (count == 3)
683
0
  {
684
0
    *axis_min_value = values[0];
685
0
    *axis_def_value = values[1];
686
0
    *axis_max_value = values[2];
687
0
    return true;
688
0
  }
689
0
  return false;
690
0
}
691
692
/**
693
 * hb_subset_axis_range_to_string:
694
 * @input: a #hb_subset_input_t object.
695
 * @axis_tag: an axis to convert
696
 * @buf: (array length=size) (out caller-allocates): output string
697
 * @size: the allocated size of @buf
698
 *
699
 * Converts an axis range into a `NULL`-terminated string in the format
700
 * understood by hb_subset_axis_range_from_string(). The client in responsible for
701
 * allocating big enough size for @buf, 128 bytes is more than enough.
702
 *
703
 * Since: 10.2.0
704
 */
705
HB_EXTERN void
706
hb_subset_axis_range_to_string (hb_subset_input_t *input,
707
                                hb_tag_t axis_tag,
708
                                char *buf, unsigned size)
709
0
{
710
0
  if (unlikely (!size)) return;
711
0
  Triple* triple;
712
0
  if (!input->axes_location.has(axis_tag, &triple)) {
713
0
    return;
714
0
  }
715
716
0
  char s[128];
717
0
  unsigned len = 0;
718
719
0
  hb_locale_t clocale HB_UNUSED;
720
0
  hb_locale_t oldlocale HB_UNUSED;
721
0
  oldlocale = hb_uselocale (clocale = newlocale (LC_ALL_MASK, "C", NULL));
722
0
  len += hb_max (0, snprintf (s, ARRAY_LENGTH (s) - len, "%g", (double) triple->minimum));
723
0
  s[len++] = ':';
724
725
0
  len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) triple->middle));
726
0
  s[len++] = ':';
727
728
0
  len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) triple->maximum));
729
0
  (void) hb_uselocale (((void) freelocale (clocale), oldlocale));
730
731
0
  assert (len < ARRAY_LENGTH (s));
732
0
  len = hb_min (len, size - 1);
733
0
  hb_memcpy (buf, s, len);
734
0
  buf[len] = '\0';
735
0
}
736
#endif
737
738
#ifdef HB_EXPERIMENTAL_API
739
static void
740
_append_arg (hb_vector_t<char> &sb, const char *arg)
741
{
742
  if (sb.in_error ()) return;
743
  if (sb.length)
744
    sb.push (' ');
745
  unsigned len = strlen (arg);
746
  sb.extend (hb_bytes_t (arg, len));
747
}
748
static void
749
_format_number_set (const hb_set_t *set, hb_vector_t<char> &out, unsigned base = 10)
750
{
751
  assert (base == 10 || base == 16);
752
  if (unlikely (base != 10 && base != 16)) return;
753
754
  hb_codepoint_t first = HB_SET_VALUE_INVALID, last = HB_SET_VALUE_INVALID;
755
  bool first_range = true;
756
  while (hb_set_next_range (set, &first, &last))
757
  {
758
    if (!first_range)
759
      out.push (',');
760
    first_range = false;
761
762
    char buf[64];
763
    int len;
764
    if (first == last)
765
    {
766
      if (base == 16)
767
        len = snprintf (buf, sizeof (buf), "%X", first);
768
      else
769
        len = snprintf (buf, sizeof (buf), "%u", first);
770
    }
771
    else
772
    {
773
      if (base == 16)
774
        len = snprintf (buf, sizeof (buf), "%X-%X", first, last);
775
      else
776
        len = snprintf (buf, sizeof (buf), "%u-%u", first, last);
777
    }
778
    if (unlikely (len < 0 || (size_t) len >= sizeof (buf))) return;
779
    out.extend (hb_bytes_t (buf, len));
780
  }
781
}
782
783
static void
784
_format_tag (hb_tag_t tag, hb_vector_t<char> &out)
785
{
786
  char buf[4];
787
  hb_tag_to_string (tag, buf);
788
789
  unsigned len = 4;
790
  while (len > 0 && buf[len - 1] == ' ')
791
    len--;
792
793
  for (unsigned i = 0; i < len; i++)
794
  {
795
    char c = buf[i];
796
    if (!((c >= 'a' && c <= 'z') ||
797
          (c >= 'A' && c <= 'Z') ||
798
          (c >= '0' && c <= '9') ||
799
          c == '-' || c == '_'))
800
    {
801
      c = '_';
802
    }
803
    out.push (c);
804
  }
805
}
806
807
static void
808
_format_tag_set (const hb_set_t *set, hb_vector_t<char> &out)
809
{
810
  bool first_tag = true;
811
  for (hb_codepoint_t tag : *set)
812
  {
813
    if (!first_tag)
814
      out.push (',');
815
    first_tag = false;
816
817
    _format_tag (tag, out);
818
  }
819
}
820
821
static bool
822
_is_set_default (const hb_set_t *set, const hb_set_t *default_set)
823
{
824
  return hb_set_is_inverted (set) == hb_set_is_inverted (default_set) &&
825
         hb_set_is_equal (set, default_set);
826
}
827
828
template <typename Formatter>
829
static void
830
_format_set_arg (const hb_set_t *set,
831
                 const char *opt_name,
832
                 Formatter &&formatter,
833
                 hb_vector_t<char> &sb)
834
{
835
  if (hb_set_is_inverted (set))
836
  {
837
    hb_set_t *excluded = hb_set_copy (set);
838
    hb_set_invert (excluded);
839
840
    hb_vector_t<char> opt;
841
    opt.extend (hb_bytes_t ("--", 2));
842
    opt.extend (hb_bytes_t (opt_name, strlen (opt_name)));
843
    opt.extend (hb_bytes_t ("=*", 2));
844
    opt.push ('\0');
845
    if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
846
847
    if (!hb_set_is_empty (excluded))
848
    {
849
      hb_vector_t<char> formatted;
850
      formatter (excluded, formatted);
851
      if (formatted.length)
852
      {
853
        hb_vector_t<char> opt2;
854
        opt2.extend (hb_bytes_t ("--", 2));
855
        opt2.extend (hb_bytes_t (opt_name, strlen (opt_name)));
856
        opt2.extend (hb_bytes_t ("-=", 2));
857
        opt2.extend (formatted.as_array ());
858
        opt2.push ('\0');
859
        if (opt2.arrayZ) _append_arg (sb, opt2.arrayZ);
860
      }
861
    }
862
    hb_set_destroy (excluded);
863
  }
864
  else if (hb_set_is_empty (set))
865
  {
866
    hb_vector_t<char> opt;
867
    opt.extend (hb_bytes_t ("--", 2));
868
    opt.extend (hb_bytes_t (opt_name, strlen (opt_name)));
869
    opt.extend (hb_bytes_t ("-=*", 3));
870
    opt.push ('\0');
871
    if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
872
  }
873
  else
874
  {
875
    hb_vector_t<char> formatted;
876
    formatter (set, formatted);
877
    if (formatted.length)
878
    {
879
      hb_vector_t<char> opt;
880
      opt.extend (hb_bytes_t ("--", 2));
881
      opt.extend (hb_bytes_t (opt_name, strlen (opt_name)));
882
      opt.extend (hb_bytes_t ("=", 1));
883
      opt.extend (formatted.as_array ());
884
      opt.push ('\0');
885
      if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
886
    }
887
  }
888
}
889
890
static void
891
_format_number_set_arg (const hb_set_t *set,
892
                        const char *opt_name,
893
                        hb_vector_t<char> &sb,
894
                        unsigned base = 10)
895
{
896
  _format_set_arg (set, opt_name, [base] (const hb_set_t *s, hb_vector_t<char> &out) {
897
    _format_number_set (s, out, base);
898
  }, sb);
899
}
900
901
static void
902
_format_tag_set_arg (const hb_set_t *set,
903
                     const char *opt_name,
904
                     hb_vector_t<char> &sb)
905
{
906
  _format_set_arg (set, opt_name, _format_tag_set, sb);
907
}
908
909
static bool
910
_format_glyph_map_arg (const hb_map_t &glyph_map,
911
                       hb_vector_t<char> &sb)
912
{
913
  if (glyph_map.is_empty ())
914
    return true;
915
916
  hb_set_t keys;
917
  hb_map_keys (&glyph_map, &keys);
918
  hb_vector_t<char> pairs;
919
  bool first = true;
920
  for (hb_codepoint_t key : keys)
921
  {
922
    if (!first)
923
      pairs.push (',');
924
    first = false;
925
    hb_codepoint_t val = hb_map_get (&glyph_map, key);
926
    char buf[64];
927
    int len = snprintf (buf, sizeof (buf), "%u:%u", key, val);
928
    if (unlikely (len < 0 || (size_t) len >= sizeof (buf))) return false;
929
    pairs.extend (hb_bytes_t (buf, len));
930
  }
931
  if (pairs.length)
932
  {
933
    hb_vector_t<char> opt;
934
    opt.extend (hb_bytes_t ("--gid-map=", 10));
935
    opt.extend (pairs.as_array ());
936
    opt.push ('\0');
937
    if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
938
  }
939
  return true;
940
}
941
942
static bool
943
_format_axes_location_arg (const hb_hashmap_t<hb_tag_t, Triple> &axes_location,
944
                           hb_vector_t<char> &sb)
945
{
946
  if (axes_location.is_empty ())
947
    return true;
948
949
  hb_vector_t<hb_tag_t> axis_tags;
950
  for (auto tag : axes_location.keys ())
951
    axis_tags.push (tag);
952
  axis_tags.qsort ([] (const hb_tag_t &a, const hb_tag_t &b) { return a < b ? -1 : a > b ? 1 : 0; });
953
954
  hb_vector_t<char> specs;
955
  bool first = true;
956
  for (hb_tag_t tag : axis_tags)
957
  {
958
    Triple triple = axes_location.get (tag);
959
    if (!first)
960
      specs.push (',');
961
    first = false;
962
963
    hb_vector_t<char> tag_buf;
964
    _format_tag (tag, tag_buf);
965
966
    char buf[128];
967
    int len;
968
    if (triple.minimum == triple.middle && triple.middle == triple.maximum)
969
    {
970
      len = snprintf (buf, sizeof (buf), "%.*s=%g", (int) tag_buf.length, tag_buf.arrayZ, (double) triple.middle);
971
    }
972
    else if (std::isnan (triple.minimum) && std::isnan (triple.middle) && std::isnan (triple.maximum))
973
    {
974
      len = snprintf (buf, sizeof (buf), "%.*s=drop", (int) tag_buf.length, tag_buf.arrayZ);
975
    }
976
    else
977
    {
978
      len = snprintf (buf, sizeof (buf), "%.*s=%g:%g:%g", (int) tag_buf.length, tag_buf.arrayZ,
979
                      (double) triple.minimum, (double) triple.middle, (double) triple.maximum);
980
    }
981
    if (unlikely (len < 0 || (size_t) len >= sizeof (buf))) return false;
982
    specs.extend (hb_bytes_t (buf, len));
983
  }
984
  if (specs.length)
985
  {
986
    hb_vector_t<char> opt;
987
    opt.extend (hb_bytes_t ("--variations=", 13));
988
    opt.extend (specs.as_array ());
989
    opt.push ('\0');
990
    if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
991
  }
992
  return true;
993
}
994
995
/**
996
 * hb_subset_input_to_string_or_fail:
997
 * @input: a #hb_subset_input_t object.
998
 *
999
 * Produces a command line string representation of the given subset input
1000
 * suitable for use with the `hb-subset` command line tool.
1001
 *
1002
 * Return value: (transfer full): A new #hb_blob_t containing the command line
1003
 * string, or `NULL` if failed. Destroy with hb_blob_destroy().
1004
 *
1005
 * XSince: EXPERIMENTAL
1006
 **/
1007
hb_blob_t *
1008
hb_subset_input_to_string_or_fail (hb_subset_input_t *input)
1009
{
1010
  if (unlikely (!input || input->in_error ()))
1011
    return nullptr;
1012
1013
  hb_vector_t<char> sb;
1014
1015
  struct flag_option_t {
1016
    hb_subset_flags_t flag;
1017
    char option[32];
1018
  };
1019
  static const flag_option_t flag_options[] = {
1020
    { HB_SUBSET_FLAGS_NO_HINTING, "no-hinting" },
1021
    { HB_SUBSET_FLAGS_RETAIN_GIDS, "retain-gids" },
1022
    { HB_SUBSET_FLAGS_DESUBROUTINIZE, "desubroutinize" },
1023
    { HB_SUBSET_FLAGS_NAME_LEGACY, "name-legacy" },
1024
    { HB_SUBSET_FLAGS_SET_OVERLAPS_FLAG, "set-overlaps-flag" },
1025
    { HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED, "passthrough-tables" },
1026
    { HB_SUBSET_FLAGS_NOTDEF_OUTLINE, "notdef-outline" },
1027
    { HB_SUBSET_FLAGS_GLYPH_NAMES, "glyph-names" },
1028
    { HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES, "no-prune-unicode-ranges" },
1029
    { HB_SUBSET_FLAGS_NO_LAYOUT_CLOSURE, "no-layout-closure" },
1030
    { HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS, "optimize" },
1031
    { HB_SUBSET_FLAGS_NO_BIDI_CLOSURE, "no-bidi-closure" },
1032
#ifdef HB_EXPERIMENTAL_API
1033
    { HB_SUBSET_FLAGS_IFTB_REQUIREMENTS, "iftb-requirements" },
1034
    { HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS, "retain-num-glyphs" },
1035
#endif
1036
    { HB_SUBSET_FLAGS_DOWNGRADE_CFF2, "downgrade-cff2" },
1037
  };
1038
1039
#ifdef HB_EXPERIMENTAL_API
1040
  static_assert (sizeof (flag_options) / sizeof (flag_options[0]) == 15,
1041
                 "Check all flags in hb_subset_flags_t are handled here.");
1042
#else
1043
  static_assert (sizeof (flag_options) / sizeof (flag_options[0]) == 13,
1044
                 "Check all flags in hb_subset_flags_t are handled here.");
1045
#endif
1046
1047
  for (const auto &fo : flag_options)
1048
  {
1049
    if (input->flags & fo.flag)
1050
    {
1051
      char buf[36];
1052
      snprintf (buf, sizeof (buf), "--%s", fo.option);
1053
      _append_arg (sb, buf);
1054
    }
1055
  }
1056
1057
  hb_subset_input_t *default_input = hb_subset_input_create_or_fail ();
1058
  if (unlikely (!default_input))
1059
    return nullptr;
1060
1061
  // Serialize all sets if they are not equal to their default setting:
1062
#define PROCESS_NUMBER_SET(field, opt_name, ...) \
1063
  if (!_is_set_default (input->sets.field, default_input->sets.field)) \
1064
    _format_number_set_arg (input->sets.field, opt_name, sb, ##__VA_ARGS__)
1065
1066
#define PROCESS_TAG_SET(field, opt_name) \
1067
  if (!_is_set_default (input->sets.field, default_input->sets.field)) \
1068
    _format_tag_set_arg (input->sets.field, opt_name, sb)
1069
1070
  PROCESS_NUMBER_SET (unicodes, "unicodes", 16);
1071
  PROCESS_NUMBER_SET (glyphs, "gids");
1072
  PROCESS_NUMBER_SET (name_ids, "name-IDs");
1073
  PROCESS_NUMBER_SET (name_languages, "name-languages");
1074
  PROCESS_TAG_SET (layout_features, "layout-features");
1075
  PROCESS_TAG_SET (layout_scripts, "layout-scripts");
1076
  PROCESS_TAG_SET (drop_tables, "drop-tables");
1077
  PROCESS_TAG_SET (no_subset_tables, "passthrough-tables");
1078
1079
#undef PROCESS_NUMBER_SET
1080
#undef PROCESS_TAG_SET
1081
1082
  hb_subset_input_destroy (default_input);
1083
1084
  static_assert (sizeof (input->sets) / sizeof (hb_set_t*) == 8,
1085
                 "Check all sets in hb_subset_input_t::sets_t are handled here.");
1086
1087
  if (unlikely (!_format_glyph_map_arg (input->glyph_map, sb)))
1088
    return nullptr;
1089
1090
  if (unlikely (!_format_axes_location_arg (input->axes_location, sb)))
1091
    return nullptr;
1092
1093
  sb.push ('\0');
1094
  if (unlikely (sb.in_error ()))
1095
    return nullptr;
1096
1097
  unsigned len = 0;
1098
  char *buf = sb.steal (&len);
1099
  if (unlikely (!buf))
1100
    return nullptr;
1101
1102
  return hb_blob_create_or_fail (buf, len, HB_MEMORY_MODE_WRITABLE, buf, hb_free);
1103
}
1104
#endif
1105
1106
/**
1107
 * hb_subset_preprocess:
1108
 * @source: a #hb_face_t object.
1109
 *
1110
 * Preprocesses the face and attaches data that will be needed by the
1111
 * subsetter. Future subsetting operations can then use the precomputed data
1112
 * to speed up the subsetting operation.
1113
 *
1114
 * See [subset-preprocessing](https://github.com/harfbuzz/harfbuzz/blob/main/docs/subset-preprocessing.md)
1115
 * for more information.
1116
 *
1117
 * Note: the preprocessed face may contain sub-blobs that reference the memory
1118
 * backing the source #hb_face_t. Therefore in the case that this memory is not
1119
 * owned by the source face you will need to ensure that memory lives
1120
 * as long as the returned #hb_face_t.
1121
 *
1122
 * Returns: a new #hb_face_t.
1123
 *
1124
 * Since: 6.0.0
1125
 **/
1126
1127
HB_EXTERN hb_face_t *
1128
hb_subset_preprocess (hb_face_t *source)
1129
0
{
1130
0
  hb_subset_input_t* input = hb_subset_input_create_or_fail ();
1131
0
  if (!input)
1132
0
    return hb_face_reference (source);
1133
1134
0
  hb_subset_input_keep_everything (input);
1135
1136
0
  input->attach_accelerator_data = true;
1137
1138
  // Always use long loca in the preprocessed version. This allows
1139
  // us to store the glyph bytes unpadded which allows the future subset
1140
  // operation to run faster by skipping the trim padding step.
1141
0
  input->force_long_loca = true;
1142
1143
0
  hb_face_t* new_source = hb_subset_or_fail (source, input);
1144
0
  hb_subset_input_destroy (input);
1145
1146
0
  if (!new_source) {
1147
0
    DEBUG_MSG (SUBSET, nullptr, "Preprocessing failed due to subset failure.");
1148
0
    return hb_face_reference (source);
1149
0
  }
1150
1151
0
  return new_source;
1152
0
}
1153
1154
/**
1155
 * hb_subset_input_old_to_new_glyph_mapping:
1156
 * @input: a #hb_subset_input_t object.
1157
 *
1158
 * Returns a map which can be used to provide an explicit mapping from old to new glyph
1159
 * id's in the produced subset. The caller should populate the map as desired.
1160
 * If this map is left empty then glyph ids will be automatically mapped to new
1161
 * values by the subsetter. If populated, the mapping must be unique. That
1162
 * is no two original glyph ids can be mapped to the same new id.
1163
 * Additionally, if a mapping is provided then the retain gids option cannot
1164
 * be enabled.
1165
 *
1166
 * Any glyphs that are retained in the subset which are not specified
1167
 * in this mapping will be assigned glyph ids after the highest glyph
1168
 * id in the mapping.
1169
 *
1170
 * Note: this will accept and apply non-monotonic mappings, however this
1171
 * may result in unsorted Coverage tables. Such fonts may not work for all
1172
 * use cases (for example ots will reject unsorted coverage tables). So it's
1173
 * recommended, if possible, to supply a monotonic mapping.
1174
 *
1175
 * Return value: (transfer none): pointer to the #hb_map_t of the custom glyphs ID map.
1176
 *
1177
 * Since: 7.3.0
1178
 **/
1179
HB_EXTERN hb_map_t*
1180
hb_subset_input_old_to_new_glyph_mapping (hb_subset_input_t *input)
1181
20.3k
{
1182
20.3k
  return &input->glyph_map;
1183
20.3k
}
1184
1185
#ifdef HB_EXPERIMENTAL_API
1186
/**
1187
 * hb_subset_input_override_name_table:
1188
 * @input: a #hb_subset_input_t object.
1189
 * @name_id: name_id of a nameRecord
1190
 * @platform_id: platform ID of a nameRecord
1191
 * @encoding_id: encoding ID of a nameRecord
1192
 * @language_id: language ID of a nameRecord
1193
 * @name_str: pointer to name string new value or null to indicate should remove
1194
 * @str_len: the size of @name_str, or -1 if it is `NULL`-terminated
1195
 *
1196
 * Override the name string of the NameRecord identified by name_id,
1197
 * platform_id, encoding_id and language_id. If a record with that name_id
1198
 * doesn't exist, create it and insert to the name table.
1199
 *
1200
 * Note: for mac platform, we only support name_str with all ascii characters,
1201
 * name_str with non-ascii characters will be ignored.
1202
 *
1203
 * XSince: EXPERIMENTAL
1204
 **/
1205
HB_EXTERN hb_bool_t
1206
hb_subset_input_override_name_table (hb_subset_input_t  *input,
1207
                                     hb_ot_name_id_t     name_id,
1208
                                     unsigned            platform_id,
1209
                                     unsigned            encoding_id,
1210
                                     unsigned            language_id,
1211
                                     const char         *name_str,
1212
                                     int                 str_len /* -1 means nul-terminated */)
1213
{
1214
  if (!name_str)
1215
  {
1216
    str_len = 0;
1217
  }
1218
  else if (str_len == -1)
1219
  {
1220
      str_len = strlen (name_str);
1221
  }
1222
1223
  hb_bytes_t name_bytes (nullptr, 0);
1224
  if (str_len)
1225
  {
1226
    if (platform_id == 1)
1227
    {
1228
      const uint8_t *src = reinterpret_cast<const uint8_t*> (name_str);
1229
      const uint8_t *src_end = src + str_len;
1230
1231
      hb_codepoint_t unicode;
1232
      const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
1233
      while (src < src_end)
1234
      {
1235
        src = hb_utf8_t::next (src, src_end, &unicode, replacement);
1236
        if (unicode >= 0x0080u)
1237
        {
1238
          // Non-ascii character detected, ignored...
1239
          return false;
1240
        }
1241
      }
1242
    }
1243
    char *override_name = (char *) hb_malloc (str_len);
1244
    if (unlikely (!override_name)) return false;
1245
1246
    hb_memcpy (override_name, name_str, str_len);
1247
    name_bytes = hb_bytes_t (override_name, str_len);
1248
  }
1249
  input->name_table_overrides.set (hb_ot_name_record_ids_t (platform_id, encoding_id, language_id, name_id), name_bytes);
1250
  return true;
1251
}
1252
#endif