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.hh"
28 #include "hb-set.hh"
29 #include "hb-utf.hh"
30
31
hb_subset_input_t()32 hb_subset_input_t::hb_subset_input_t ()
33 {
34 for (auto& set : sets_iter ())
35 set = hb::shared_ptr<hb_set_t> (hb_set_create ());
36
37 if (in_error ())
38 return;
39
40 flags = HB_SUBSET_FLAGS_DEFAULT;
41
42 hb_set_add_range (sets.name_ids, 0, 6);
43 hb_set_add (sets.name_languages, 0x0409);
44
45 hb_tag_t default_drop_tables[] = {
46 // Layout disabled by default
47 HB_TAG ('m', 'o', 'r', 'x'),
48 HB_TAG ('m', 'o', 'r', 't'),
49 HB_TAG ('k', 'e', 'r', 'x'),
50 HB_TAG ('k', 'e', 'r', 'n'),
51
52 // Copied from fontTools:
53 HB_TAG ('B', 'A', 'S', 'E'),
54 HB_TAG ('J', 'S', 'T', 'F'),
55 HB_TAG ('D', 'S', 'I', 'G'),
56 HB_TAG ('E', 'B', 'D', 'T'),
57 HB_TAG ('E', 'B', 'L', 'C'),
58 HB_TAG ('E', 'B', 'S', 'C'),
59 HB_TAG ('S', 'V', 'G', ' '),
60 HB_TAG ('P', 'C', 'L', 'T'),
61 HB_TAG ('L', 'T', 'S', 'H'),
62 // Graphite tables
63 HB_TAG ('F', 'e', 'a', 't'),
64 HB_TAG ('G', 'l', 'a', 't'),
65 HB_TAG ('G', 'l', 'o', 'c'),
66 HB_TAG ('S', 'i', 'l', 'f'),
67 HB_TAG ('S', 'i', 'l', 'l'),
68 };
69 sets.drop_tables->add_array (default_drop_tables, ARRAY_LENGTH (default_drop_tables));
70
71 hb_tag_t default_no_subset_tables[] = {
72 HB_TAG ('g', 'a', 's', 'p'),
73 HB_TAG ('f', 'p', 'g', 'm'),
74 HB_TAG ('p', 'r', 'e', 'p'),
75 HB_TAG ('V', 'D', 'M', 'X'),
76 HB_TAG ('D', 'S', 'I', 'G'),
77 };
78 sets.no_subset_tables->add_array (default_no_subset_tables,
79 ARRAY_LENGTH (default_no_subset_tables));
80
81 //copied from _layout_features_groups in fonttools
82 hb_tag_t default_layout_features[] = {
83 // default shaper
84 // common
85 HB_TAG ('r', 'v', 'r', 'n'),
86 HB_TAG ('c', 'c', 'm', 'p'),
87 HB_TAG ('l', 'i', 'g', 'a'),
88 HB_TAG ('l', 'o', 'c', 'l'),
89 HB_TAG ('m', 'a', 'r', 'k'),
90 HB_TAG ('m', 'k', 'm', 'k'),
91 HB_TAG ('r', 'l', 'i', 'g'),
92
93 //fractions
94 HB_TAG ('f', 'r', 'a', 'c'),
95 HB_TAG ('n', 'u', 'm', 'r'),
96 HB_TAG ('d', 'n', 'o', 'm'),
97
98 //horizontal
99 HB_TAG ('c', 'a', 'l', 't'),
100 HB_TAG ('c', 'l', 'i', 'g'),
101 HB_TAG ('c', 'u', 'r', 's'),
102 HB_TAG ('k', 'e', 'r', 'n'),
103 HB_TAG ('r', 'c', 'l', 't'),
104
105 //vertical
106 HB_TAG ('v', 'a', 'l', 't'),
107 HB_TAG ('v', 'e', 'r', 't'),
108 HB_TAG ('v', 'k', 'r', 'n'),
109 HB_TAG ('v', 'p', 'a', 'l'),
110 HB_TAG ('v', 'r', 't', '2'),
111
112 //ltr
113 HB_TAG ('l', 't', 'r', 'a'),
114 HB_TAG ('l', 't', 'r', 'm'),
115
116 //rtl
117 HB_TAG ('r', 't', 'l', 'a'),
118 HB_TAG ('r', 't', 'l', 'm'),
119
120 //random
121 HB_TAG ('r', 'a', 'n', 'd'),
122
123 //justify
124 HB_TAG ('j', 'a', 'l', 't'), // HarfBuzz doesn't use; others might
125
126 //private
127 HB_TAG ('H', 'a', 'r', 'f'),
128 HB_TAG ('H', 'A', 'R', 'F'),
129 HB_TAG ('B', 'u', 'z', 'z'),
130 HB_TAG ('B', 'U', 'Z', 'Z'),
131
132 //shapers
133
134 //arabic
135 HB_TAG ('i', 'n', 'i', 't'),
136 HB_TAG ('m', 'e', 'd', 'i'),
137 HB_TAG ('f', 'i', 'n', 'a'),
138 HB_TAG ('i', 's', 'o', 'l'),
139 HB_TAG ('m', 'e', 'd', '2'),
140 HB_TAG ('f', 'i', 'n', '2'),
141 HB_TAG ('f', 'i', 'n', '3'),
142 HB_TAG ('c', 's', 'w', 'h'),
143 HB_TAG ('m', 's', 'e', 't'),
144 HB_TAG ('s', 't', 'c', 'h'),
145
146 //hangul
147 HB_TAG ('l', 'j', 'm', 'o'),
148 HB_TAG ('v', 'j', 'm', 'o'),
149 HB_TAG ('t', 'j', 'm', 'o'),
150
151 //tibetan
152 HB_TAG ('a', 'b', 'v', 's'),
153 HB_TAG ('b', 'l', 'w', 's'),
154 HB_TAG ('a', 'b', 'v', 'm'),
155 HB_TAG ('b', 'l', 'w', 'm'),
156
157 //indic
158 HB_TAG ('n', 'u', 'k', 't'),
159 HB_TAG ('a', 'k', 'h', 'n'),
160 HB_TAG ('r', 'p', 'h', 'f'),
161 HB_TAG ('r', 'k', 'r', 'f'),
162 HB_TAG ('p', 'r', 'e', 'f'),
163 HB_TAG ('b', 'l', 'w', 'f'),
164 HB_TAG ('h', 'a', 'l', 'f'),
165 HB_TAG ('a', 'b', 'v', 'f'),
166 HB_TAG ('p', 's', 't', 'f'),
167 HB_TAG ('c', 'f', 'a', 'r'),
168 HB_TAG ('v', 'a', 't', 'u'),
169 HB_TAG ('c', 'j', 'c', 't'),
170 HB_TAG ('i', 'n', 'i', 't'),
171 HB_TAG ('p', 'r', 'e', 's'),
172 HB_TAG ('a', 'b', 'v', 's'),
173 HB_TAG ('b', 'l', 'w', 's'),
174 HB_TAG ('p', 's', 't', 's'),
175 HB_TAG ('h', 'a', 'l', 'n'),
176 HB_TAG ('d', 'i', 's', 't'),
177 HB_TAG ('a', 'b', 'v', 'm'),
178 HB_TAG ('b', 'l', 'w', 'm'),
179 };
180
181 sets.layout_features->add_array (default_layout_features, ARRAY_LENGTH (default_layout_features));
182
183 sets.layout_scripts->invert (); // Default to all scripts.
184 }
185
186 /**
187 * hb_subset_input_create_or_fail:
188 *
189 * Creates a new subset input object.
190 *
191 * Return value: (transfer full): New subset input, or `NULL` if failed. Destroy
192 * with hb_subset_input_destroy().
193 *
194 * Since: 1.8.0
195 **/
196 hb_subset_input_t *
hb_subset_input_create_or_fail(void)197 hb_subset_input_create_or_fail (void)
198 {
199 hb_subset_input_t *input = hb_object_create<hb_subset_input_t>();
200
201 if (unlikely (!input))
202 return nullptr;
203
204 if (input->in_error ())
205 {
206 hb_subset_input_destroy (input);
207 return nullptr;
208 }
209
210 return input;
211 }
212
213 /**
214 * hb_subset_input_reference: (skip)
215 * @input: a #hb_subset_input_t object.
216 *
217 * Increases the reference count on @input.
218 *
219 * Return value: @input.
220 *
221 * Since: 1.8.0
222 **/
223 hb_subset_input_t *
hb_subset_input_reference(hb_subset_input_t * input)224 hb_subset_input_reference (hb_subset_input_t *input)
225 {
226 return hb_object_reference (input);
227 }
228
229 /**
230 * hb_subset_input_destroy:
231 * @input: a #hb_subset_input_t object.
232 *
233 * Decreases the reference count on @input, and if it reaches zero, destroys
234 * @input, freeing all memory.
235 *
236 * Since: 1.8.0
237 **/
238 void
hb_subset_input_destroy(hb_subset_input_t * input)239 hb_subset_input_destroy (hb_subset_input_t *input)
240 {
241 if (!hb_object_destroy (input)) return;
242
243 hb_free (input);
244 }
245
246 /**
247 * hb_subset_input_unicode_set:
248 * @input: a #hb_subset_input_t object.
249 *
250 * Gets the set of Unicode code points to retain, the caller should modify the
251 * set as needed.
252 *
253 * Return value: (transfer none): pointer to the #hb_set_t of Unicode code
254 * points.
255 *
256 * Since: 1.8.0
257 **/
258 HB_EXTERN hb_set_t *
hb_subset_input_unicode_set(hb_subset_input_t * input)259 hb_subset_input_unicode_set (hb_subset_input_t *input)
260 {
261 return input->sets.unicodes;
262 }
263
264 /**
265 * hb_subset_input_glyph_set:
266 * @input: a #hb_subset_input_t object.
267 *
268 * Gets the set of glyph IDs to retain, the caller should modify the set as
269 * needed.
270 *
271 * Return value: (transfer none): pointer to the #hb_set_t of glyph IDs.
272 *
273 * Since: 1.8.0
274 **/
275 HB_EXTERN hb_set_t *
hb_subset_input_glyph_set(hb_subset_input_t * input)276 hb_subset_input_glyph_set (hb_subset_input_t *input)
277 {
278 return input->sets.glyphs;
279 }
280
281 /**
282 * hb_subset_input_set:
283 * @input: a #hb_subset_input_t object.
284 * @set_type: a #hb_subset_sets_t set type.
285 *
286 * Gets the set of the specified type.
287 *
288 * Return value: (transfer none): pointer to the #hb_set_t of the specified type.
289 *
290 * Since: 2.9.1
291 **/
292 HB_EXTERN hb_set_t *
hb_subset_input_set(hb_subset_input_t * input,hb_subset_sets_t set_type)293 hb_subset_input_set (hb_subset_input_t *input, hb_subset_sets_t set_type)
294 {
295 return input->sets_iter () [set_type];
296 }
297
298 /**
299 * hb_subset_input_get_flags:
300 * @input: a #hb_subset_input_t object.
301 *
302 * Gets all of the subsetting flags in the input object.
303 *
304 * Return value: the subsetting flags bit field.
305 *
306 * Since: 2.9.0
307 **/
308 HB_EXTERN hb_subset_flags_t
hb_subset_input_get_flags(hb_subset_input_t * input)309 hb_subset_input_get_flags (hb_subset_input_t *input)
310 {
311 return (hb_subset_flags_t) input->flags;
312 }
313
314 /**
315 * hb_subset_input_set_flags:
316 * @input: a #hb_subset_input_t object.
317 * @value: bit field of flags
318 *
319 * Sets all of the flags in the input object to the values specified by the bit
320 * field.
321 *
322 * Since: 2.9.0
323 **/
324 HB_EXTERN void
hb_subset_input_set_flags(hb_subset_input_t * input,unsigned value)325 hb_subset_input_set_flags (hb_subset_input_t *input,
326 unsigned value)
327 {
328 input->flags = (hb_subset_flags_t) value;
329 }
330
331 /**
332 * hb_subset_input_set_user_data: (skip)
333 * @input: a #hb_subset_input_t object.
334 * @key: The user-data key to set
335 * @data: A pointer to the user data
336 * @destroy: (nullable): A callback to call when @data is not needed anymore
337 * @replace: Whether to replace an existing data with the same key
338 *
339 * Attaches a user-data key/data pair to the given subset input object.
340 *
341 * Return value: `true` if success, `false` otherwise
342 *
343 * Since: 2.9.0
344 **/
345 hb_bool_t
hb_subset_input_set_user_data(hb_subset_input_t * input,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)346 hb_subset_input_set_user_data (hb_subset_input_t *input,
347 hb_user_data_key_t *key,
348 void * data,
349 hb_destroy_func_t destroy,
350 hb_bool_t replace)
351 {
352 return hb_object_set_user_data (input, key, data, destroy, replace);
353 }
354
355 /**
356 * hb_subset_input_get_user_data: (skip)
357 * @input: a #hb_subset_input_t object.
358 * @key: The user-data key to query
359 *
360 * Fetches the user data associated with the specified key,
361 * attached to the specified subset input object.
362 *
363 * Return value: (transfer none): A pointer to the user data
364 *
365 * Since: 2.9.0
366 **/
367 void *
hb_subset_input_get_user_data(const hb_subset_input_t * input,hb_user_data_key_t * key)368 hb_subset_input_get_user_data (const hb_subset_input_t *input,
369 hb_user_data_key_t *key)
370 {
371 return hb_object_get_user_data (input, key);
372 }
373
374 /**
375 * hb_subset_input_keep_everything:
376 * @input: a #hb_subset_input_t object
377 *
378 * Configure input object to keep everything in the font face.
379 * That is, all Unicodes, glyphs, names, layout items,
380 * glyph names, etc.
381 *
382 * The input can be tailored afterwards by the caller.
383 *
384 * Since: 7.0.0
385 */
386 void
hb_subset_input_keep_everything(hb_subset_input_t * input)387 hb_subset_input_keep_everything (hb_subset_input_t *input)
388 {
389 const hb_subset_sets_t indices[] = {HB_SUBSET_SETS_UNICODE,
390 HB_SUBSET_SETS_GLYPH_INDEX,
391 HB_SUBSET_SETS_NAME_ID,
392 HB_SUBSET_SETS_NAME_LANG_ID,
393 HB_SUBSET_SETS_LAYOUT_FEATURE_TAG,
394 HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG};
395
396 for (auto idx : hb_iter (indices))
397 {
398 hb_set_t *set = hb_subset_input_set (input, idx);
399 hb_set_clear (set);
400 hb_set_invert (set);
401 }
402
403 // Don't drop any tables
404 hb_set_clear (hb_subset_input_set (input, HB_SUBSET_SETS_DROP_TABLE_TAG));
405
406 hb_subset_input_set_flags (input,
407 HB_SUBSET_FLAGS_NOTDEF_OUTLINE |
408 HB_SUBSET_FLAGS_GLYPH_NAMES |
409 HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES |
410 HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED);
411 }
412
413 #ifndef HB_NO_VAR
414 /**
415 * hb_subset_input_pin_axis_to_default: (skip)
416 * @input: a #hb_subset_input_t object.
417 * @face: a #hb_face_t object.
418 * @axis_tag: Tag of the axis to be pinned
419 *
420 * Pin an axis to its default location in the given subset input object.
421 *
422 * All axes in a font must be pinned. Additionally, `CFF2` table, if present,
423 * will be de-subroutinized.
424 *
425 * Return value: `true` if success, `false` otherwise
426 *
427 * Since: 6.0.0
428 **/
429 HB_EXTERN hb_bool_t
hb_subset_input_pin_axis_to_default(hb_subset_input_t * input,hb_face_t * face,hb_tag_t axis_tag)430 hb_subset_input_pin_axis_to_default (hb_subset_input_t *input,
431 hb_face_t *face,
432 hb_tag_t axis_tag)
433 {
434 hb_ot_var_axis_info_t axis_info;
435 if (!hb_ot_var_find_axis_info (face, axis_tag, &axis_info))
436 return false;
437
438 float default_val = axis_info.default_value;
439 return input->axes_location.set (axis_tag, Triple (default_val, default_val, default_val));
440 }
441
442 /**
443 * hb_subset_input_pin_axis_location: (skip)
444 * @input: a #hb_subset_input_t object.
445 * @face: a #hb_face_t object.
446 * @axis_tag: Tag of the axis to be pinned
447 * @axis_value: Location on the axis to be pinned at
448 *
449 * Pin an axis to a fixed location in the given subset input object.
450 *
451 * All axes in a font must be pinned. Additionally, `CFF2` table, if present,
452 * will be de-subroutinized.
453 *
454 * Return value: `true` if success, `false` otherwise
455 *
456 * Since: 6.0.0
457 **/
458 HB_EXTERN hb_bool_t
hb_subset_input_pin_axis_location(hb_subset_input_t * input,hb_face_t * face,hb_tag_t axis_tag,float axis_value)459 hb_subset_input_pin_axis_location (hb_subset_input_t *input,
460 hb_face_t *face,
461 hb_tag_t axis_tag,
462 float axis_value)
463 {
464 hb_ot_var_axis_info_t axis_info;
465 if (!hb_ot_var_find_axis_info (face, axis_tag, &axis_info))
466 return false;
467
468 float val = hb_clamp(axis_value, axis_info.min_value, axis_info.max_value);
469 return input->axes_location.set (axis_tag, Triple (val, val, val));
470 }
471
472 #ifdef HB_EXPERIMENTAL_API
473 /**
474 * hb_subset_input_set_axis_range: (skip)
475 * @input: a #hb_subset_input_t object.
476 * @face: a #hb_face_t object.
477 * @axis_tag: Tag of the axis
478 * @axis_min_value: Minimum value of the axis variation range to set
479 * @axis_max_value: Maximum value of the axis variation range to set
480 * @axis_def_value: Default value of the axis variation range to set, in case of
481 * null, it'll be determined automatically
482 *
483 * Restricting the range of variation on an axis in the given subset input object.
484 * New min/default/max values will be clamped if they're not within the fvar axis range.
485 * If the new default value is null:
486 * If the fvar axis default value is within the new range, then new default
487 * value is the same as original default value.
488 * If the fvar axis default value is not within the new range, the new default
489 * value will be changed to the new min or max value, whichever is closer to the fvar
490 * axis default.
491 *
492 * Note: input min value can not be bigger than input max value. If the input
493 * default value is not within the new min/max range, it'll be clamped.
494 * Note: currently it supports gvar and cvar tables only.
495 *
496 * Return value: `true` if success, `false` otherwise
497 *
498 * XSince: EXPERIMENTAL
499 **/
500 HB_EXTERN hb_bool_t
hb_subset_input_set_axis_range(hb_subset_input_t * input,hb_face_t * face,hb_tag_t axis_tag,float axis_min_value,float axis_max_value,float * axis_def_value)501 hb_subset_input_set_axis_range (hb_subset_input_t *input,
502 hb_face_t *face,
503 hb_tag_t axis_tag,
504 float axis_min_value,
505 float axis_max_value,
506 float *axis_def_value /* IN, maybe NULL */)
507 {
508 if (axis_min_value > axis_max_value)
509 return false;
510
511 hb_ot_var_axis_info_t axis_info;
512 if (!hb_ot_var_find_axis_info (face, axis_tag, &axis_info))
513 return false;
514
515 float new_min_val = hb_clamp(axis_min_value, axis_info.min_value, axis_info.max_value);
516 float new_max_val = hb_clamp(axis_max_value, axis_info.min_value, axis_info.max_value);
517 float new_default_val = axis_def_value ? *axis_def_value : axis_info.default_value;
518 new_default_val = hb_clamp(new_default_val, new_min_val, new_max_val);
519 return input->axes_location.set (axis_tag, Triple (new_min_val, new_default_val, new_max_val));
520 }
521 #endif
522 #endif
523
524 /**
525 * hb_subset_preprocess:
526 * @source: a #hb_face_t object.
527 *
528 * Preprocesses the face and attaches data that will be needed by the
529 * subsetter. Future subsetting operations can then use the precomputed data
530 * to speed up the subsetting operation.
531 *
532 * See [subset-preprocessing](https://github.com/harfbuzz/harfbuzz/blob/main/docs/subset-preprocessing.md)
533 * for more information.
534 *
535 * Note: the preprocessed face may contain sub-blobs that reference the memory
536 * backing the source #hb_face_t. Therefore in the case that this memory is not
537 * owned by the source face you will need to ensure that memory lives
538 * as long as the returned #hb_face_t.
539 *
540 * Returns: a new #hb_face_t.
541 *
542 * Since: 6.0.0
543 **/
544
545 HB_EXTERN hb_face_t *
hb_subset_preprocess(hb_face_t * source)546 hb_subset_preprocess (hb_face_t *source)
547 {
548 hb_subset_input_t* input = hb_subset_input_create_or_fail ();
549 if (!input)
550 return hb_face_reference (source);
551
552 hb_subset_input_keep_everything (input);
553
554 input->attach_accelerator_data = true;
555
556 // Always use long loca in the preprocessed version. This allows
557 // us to store the glyph bytes unpadded which allows the future subset
558 // operation to run faster by skipping the trim padding step.
559 input->force_long_loca = true;
560
561 hb_face_t* new_source = hb_subset_or_fail (source, input);
562 hb_subset_input_destroy (input);
563
564 if (!new_source) {
565 DEBUG_MSG (SUBSET, nullptr, "Preprocessing failed due to subset failure.");
566 return hb_face_reference (source);
567 }
568
569 return new_source;
570 }
571
572 /**
573 * hb_subset_input_old_to_new_glyph_mapping:
574 * @input: a #hb_subset_input_t object.
575 *
576 * Returns a map which can be used to provide an explicit mapping from old to new glyph
577 * id's in the produced subset. The caller should populate the map as desired.
578 * If this map is left empty then glyph ids will be automatically mapped to new
579 * values by the subsetter. If populated, the mapping must be unique. That
580 * is no two original glyph ids can be mapped to the same new id.
581 * Additionally, if a mapping is provided then the retain gids option cannot
582 * be enabled.
583 *
584 * Any glyphs that are retained in the subset which are not specified
585 * in this mapping will be assigned glyph ids after the highest glyph
586 * id in the mapping.
587 *
588 * Note: this will accept and apply non-monotonic mappings, however this
589 * may result in unsorted Coverage tables. Such fonts may not work for all
590 * use cases (for example ots will reject unsorted coverage tables). So it's
591 * recommended, if possible, to supply a monotonic mapping.
592 *
593 * Return value: (transfer none): pointer to the #hb_map_t of the custom glyphs ID map.
594 *
595 * Since: 7.3.0
596 **/
597 HB_EXTERN hb_map_t*
hb_subset_input_old_to_new_glyph_mapping(hb_subset_input_t * input)598 hb_subset_input_old_to_new_glyph_mapping (hb_subset_input_t *input)
599 {
600 return &input->glyph_map;
601 }
602
603 #ifdef HB_EXPERIMENTAL_API
604 /**
605 * hb_subset_input_override_name_table:
606 * @input: a #hb_subset_input_t object.
607 * @name_id: name_id of a nameRecord
608 * @platform_id: platform ID of a nameRecord
609 * @encoding_id: encoding ID of a nameRecord
610 * @language_id: language ID of a nameRecord
611 * @name_str: pointer to name string new value or null to indicate should remove
612 * @str_len: the size of @name_str, or -1 if it is `NULL`-terminated
613 *
614 * Override the name string of the NameRecord identified by name_id,
615 * platform_id, encoding_id and language_id. If a record with that name_id
616 * doesn't exist, create it and insert to the name table.
617 *
618 * Note: for mac platform, we only support name_str with all ascii characters,
619 * name_str with non-ascii characters will be ignored.
620 *
621 * XSince: EXPERIMENTAL
622 **/
623 HB_EXTERN hb_bool_t
hb_subset_input_override_name_table(hb_subset_input_t * input,hb_ot_name_id_t name_id,unsigned platform_id,unsigned encoding_id,unsigned language_id,const char * name_str,int str_len)624 hb_subset_input_override_name_table (hb_subset_input_t *input,
625 hb_ot_name_id_t name_id,
626 unsigned platform_id,
627 unsigned encoding_id,
628 unsigned language_id,
629 const char *name_str,
630 int str_len /* -1 means nul-terminated */)
631 {
632 if (!name_str)
633 {
634 str_len = 0;
635 }
636 else if (str_len == -1)
637 {
638 str_len = strlen (name_str);
639 }
640
641 hb_bytes_t name_bytes (nullptr, 0);
642 if (str_len)
643 {
644 if (platform_id == 1)
645 {
646 const uint8_t *src = reinterpret_cast<const uint8_t*> (name_str);
647 const uint8_t *src_end = src + str_len;
648
649 hb_codepoint_t unicode;
650 const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
651 while (src < src_end)
652 {
653 src = hb_utf8_t::next (src, src_end, &unicode, replacement);
654 if (unicode >= 0x0080u)
655 {
656 printf ("Non-ascii character detected, ignored...This API supports acsii characters only for mac platform\n");
657 return false;
658 }
659 }
660 }
661 char *override_name = (char *) hb_malloc (str_len);
662 if (unlikely (!override_name)) return false;
663
664 hb_memcpy (override_name, name_str, str_len);
665 name_bytes = hb_bytes_t (override_name, str_len);
666 }
667 input->name_table_overrides.set (hb_ot_name_record_ids_t (platform_id, encoding_id, language_id, name_id), name_bytes);
668 return true;
669 }
670
671 #endif
672