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-map.hh"
29 #include "hb-set.hh"
30
31 #include "hb-ot-cmap-table.hh"
32 #include "hb-ot-glyf-table.hh"
33 #include "hb-ot-layout-gdef-table.hh"
34 #include "hb-ot-layout-gpos-table.hh"
35 #include "hb-ot-layout-gsub-table.hh"
36 #include "hb-ot-cff1-table.hh"
37 #include "hb-ot-color-colr-table.hh"
38 #include "hb-ot-color-colrv1-closure.hh"
39 #include "hb-ot-var-fvar-table.hh"
40 #include "hb-ot-stat-table.hh"
41 #include "hb-ot-math-table.hh"
42
43
44 typedef hb_hashmap_t<unsigned, hb_set_t *> script_langsys_map;
45 #ifndef HB_NO_SUBSET_CFF
46 static inline void
_add_cff_seac_components(const OT::cff1::accelerator_t & cff,hb_codepoint_t gid,hb_set_t * gids_to_retain)47 _add_cff_seac_components (const OT::cff1::accelerator_t &cff,
48 hb_codepoint_t gid,
49 hb_set_t *gids_to_retain)
50 {
51 hb_codepoint_t base_gid, accent_gid;
52 if (cff.get_seac_components (gid, &base_gid, &accent_gid))
53 {
54 gids_to_retain->add (base_gid);
55 gids_to_retain->add (accent_gid);
56 }
57 }
58 #endif
59
60 static void
_remap_palette_indexes(const hb_set_t * palette_indexes,hb_map_t * mapping)61 _remap_palette_indexes (const hb_set_t *palette_indexes,
62 hb_map_t *mapping /* OUT */)
63 {
64 unsigned new_idx = 0;
65 for (unsigned palette_index : palette_indexes->iter ())
66 {
67 if (palette_index == 0xFFFF)
68 {
69 mapping->set (palette_index, palette_index);
70 continue;
71 }
72 mapping->set (palette_index, new_idx);
73 new_idx++;
74 }
75 }
76
77 static void
_remap_indexes(const hb_set_t * indexes,hb_map_t * mapping)78 _remap_indexes (const hb_set_t *indexes,
79 hb_map_t *mapping /* OUT */)
80 {
81 unsigned count = indexes->get_population ();
82
83 for (auto _ : + hb_zip (indexes->iter (), hb_range (count)))
84 mapping->set (_.first, _.second);
85
86 }
87
88 #ifndef HB_NO_SUBSET_LAYOUT
89 typedef void (*layout_collect_func_t) (hb_face_t *face, hb_tag_t table_tag, const hb_tag_t *scripts, const hb_tag_t *languages, const hb_tag_t *features, hb_set_t *lookup_indexes /* OUT */);
90
91
92 template <typename T>
_collect_layout_indices(hb_face_t * face,const T & table,const hb_set_t * layout_features_to_retain,layout_collect_func_t layout_collect_func,hb_set_t * indices)93 static void _collect_layout_indices (hb_face_t *face,
94 const T& table,
95 const hb_set_t *layout_features_to_retain,
96 layout_collect_func_t layout_collect_func,
97 hb_set_t *indices /* OUT */)
98 {
99 hb_vector_t<hb_tag_t> features;
100 if (!features.alloc (table.get_feature_count () + 1))
101 return;
102
103 for (unsigned i = 0; i < table.get_feature_count (); i++)
104 {
105 hb_tag_t tag = table.get_feature_tag (i);
106 if (tag && layout_features_to_retain->has (tag))
107 features.push (tag);
108 }
109
110 if (!features)
111 return;
112
113 // The collect function needs a null element to signal end of the array.
114 features.push (0);
115
116 if (features.get_size () == table.get_feature_count () + 1)
117 {
118 // Looking for all features, trigger the faster collection method.
119 layout_collect_func (face,
120 T::tableTag,
121 nullptr,
122 nullptr,
123 nullptr,
124 indices);
125 return;
126 }
127
128 layout_collect_func (face,
129 T::tableTag,
130 nullptr,
131 nullptr,
132 features.arrayZ,
133 indices);
134 }
135
136 template <typename T>
137 static inline void
_closure_glyphs_lookups_features(hb_face_t * face,hb_set_t * gids_to_retain,const hb_set_t * layout_features_to_retain,hb_map_t * lookups,hb_map_t * features,script_langsys_map * langsys_map)138 _closure_glyphs_lookups_features (hb_face_t *face,
139 hb_set_t *gids_to_retain,
140 const hb_set_t *layout_features_to_retain,
141 hb_map_t *lookups,
142 hb_map_t *features,
143 script_langsys_map *langsys_map)
144 {
145 hb_blob_ptr_t<T> table = hb_sanitize_context_t ().reference_table<T> (face);
146 hb_tag_t table_tag = table->tableTag;
147 hb_set_t lookup_indices;
148 _collect_layout_indices<T> (face,
149 *table,
150 layout_features_to_retain,
151 hb_ot_layout_collect_lookups,
152 &lookup_indices);
153
154 if (table_tag == HB_OT_TAG_GSUB)
155 hb_ot_layout_lookups_substitute_closure (face,
156 &lookup_indices,
157 gids_to_retain);
158 table->closure_lookups (face,
159 gids_to_retain,
160 &lookup_indices);
161 _remap_indexes (&lookup_indices, lookups);
162
163 // Collect and prune features
164 hb_set_t feature_indices;
165 _collect_layout_indices<T> (face,
166 *table,
167 layout_features_to_retain,
168 hb_ot_layout_collect_features,
169 &feature_indices);
170
171 table->prune_features (lookups, &feature_indices);
172 hb_map_t duplicate_feature_map;
173 table->find_duplicate_features (lookups, &feature_indices, &duplicate_feature_map);
174
175 feature_indices.clear ();
176 table->prune_langsys (&duplicate_feature_map, langsys_map, &feature_indices);
177 _remap_indexes (&feature_indices, features);
178
179 table.destroy ();
180 }
181
182 #endif
183
184 #ifndef HB_NO_VAR
185 static inline void
_collect_layout_variation_indices(hb_face_t * face,const hb_set_t * glyphset,const hb_map_t * gpos_lookups,hb_set_t * layout_variation_indices,hb_map_t * layout_variation_idx_map)186 _collect_layout_variation_indices (hb_face_t *face,
187 const hb_set_t *glyphset,
188 const hb_map_t *gpos_lookups,
189 hb_set_t *layout_variation_indices,
190 hb_map_t *layout_variation_idx_map)
191 {
192 hb_blob_ptr_t<OT::GDEF> gdef = hb_sanitize_context_t ().reference_table<OT::GDEF> (face);
193 hb_blob_ptr_t<OT::GPOS> gpos = hb_sanitize_context_t ().reference_table<OT::GPOS> (face);
194
195 if (!gdef->has_data ())
196 {
197 gdef.destroy ();
198 gpos.destroy ();
199 return;
200 }
201 OT::hb_collect_variation_indices_context_t c (layout_variation_indices, glyphset, gpos_lookups);
202 gdef->collect_variation_indices (&c);
203
204 if (hb_ot_layout_has_positioning (face))
205 gpos->collect_variation_indices (&c);
206
207 gdef->remap_layout_variation_indices (layout_variation_indices, layout_variation_idx_map);
208
209 gdef.destroy ();
210 gpos.destroy ();
211 }
212 #endif
213
214 static inline void
_cmap_closure(hb_face_t * face,const hb_set_t * unicodes,hb_set_t * glyphset)215 _cmap_closure (hb_face_t *face,
216 const hb_set_t *unicodes,
217 hb_set_t *glyphset)
218 {
219 OT::cmap::accelerator_t cmap;
220 cmap.init (face);
221 cmap.table->closure_glyphs (unicodes, glyphset);
222 cmap.fini ();
223 }
224
_colr_closure(hb_face_t * face,hb_map_t * layers_map,hb_map_t * palettes_map,hb_set_t * glyphs_colred)225 static void _colr_closure (hb_face_t *face,
226 hb_map_t *layers_map,
227 hb_map_t *palettes_map,
228 hb_set_t *glyphs_colred)
229 {
230 OT::COLR::accelerator_t colr;
231 colr.init (face);
232 if (!colr.is_valid ()) return;
233
234 unsigned iteration_count = 0;
235 hb_set_t palette_indices, layer_indices;
236 unsigned glyphs_num;
237 {
238 glyphs_num = glyphs_colred->get_population ();
239
240 // Collect all glyphs referenced by COLRv0
241 hb_set_t glyphset_colrv0;
242 for (hb_codepoint_t gid : glyphs_colred->iter ())
243 colr.closure_glyphs (gid, &glyphset_colrv0);
244
245 glyphs_colred->union_ (glyphset_colrv0);
246
247 //closure for COLRv1
248 colr.closure_forV1 (glyphs_colred, &layer_indices, &palette_indices);
249 } while (iteration_count++ <= HB_CLOSURE_MAX_STAGES &&
250 glyphs_num != glyphs_colred->get_population ());
251
252 colr.closure_V0palette_indices (glyphs_colred, &palette_indices);
253 _remap_indexes (&layer_indices, layers_map);
254 _remap_palette_indexes (&palette_indices, palettes_map);
255 colr.fini ();
256 }
257
258 static inline void
_math_closure(hb_face_t * face,hb_set_t * glyphset)259 _math_closure (hb_face_t *face,
260 hb_set_t *glyphset)
261 {
262 hb_blob_ptr_t<OT::MATH> math = hb_sanitize_context_t ().reference_table<OT::MATH> (face);
263 if (math->has_data ())
264 math->closure_glyphs (glyphset);
265 math.destroy ();
266 }
267
268
269 static inline void
_remove_invalid_gids(hb_set_t * glyphs,unsigned int num_glyphs)270 _remove_invalid_gids (hb_set_t *glyphs,
271 unsigned int num_glyphs)
272 {
273 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
274 while (glyphs->next (&gid))
275 {
276 if (gid >= num_glyphs)
277 glyphs->del (gid);
278 }
279 }
280
281 static void
_populate_unicodes_to_retain(const hb_set_t * unicodes,const hb_set_t * glyphs,hb_subset_plan_t * plan)282 _populate_unicodes_to_retain (const hb_set_t *unicodes,
283 const hb_set_t *glyphs,
284 hb_subset_plan_t *plan)
285 {
286 OT::cmap::accelerator_t cmap;
287 cmap.init (plan->source);
288
289 constexpr static const int size_threshold = 4096;
290
291 if (glyphs->is_empty () && unicodes->get_population () < size_threshold)
292 {
293 /* This is the fast path if it's anticipated that size of unicodes
294 * is << than the number of codepoints in the font. */
295 for (hb_codepoint_t cp : *unicodes)
296 {
297 hb_codepoint_t gid;
298 if (!cmap.get_nominal_glyph (cp, &gid))
299 {
300 DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
301 continue;
302 }
303
304 plan->codepoint_to_glyph->set (cp, gid);
305 }
306 }
307 else
308 {
309 hb_map_t unicode_glyphid_map;
310 cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map);
311
312 for (hb_pair_t<hb_codepoint_t, hb_codepoint_t> cp_gid :
313 + unicode_glyphid_map.iter ())
314 {
315 if (!unicodes->has (cp_gid.first) && !glyphs->has (cp_gid.second))
316 continue;
317
318 plan->codepoint_to_glyph->set (cp_gid.first, cp_gid.second);
319 }
320
321 /* Add gids which where requested, but not mapped in cmap */
322 // TODO(garretrieger):
323 // Once https://github.com/harfbuzz/harfbuzz/issues/3169
324 // is implemented, this can be done with union and del_range
325 for (hb_codepoint_t gid : glyphs->iter ())
326 {
327 if (gid >= plan->source->get_num_glyphs ())
328 break;
329 plan->_glyphset_gsub->add (gid);
330 }
331 }
332
333 + plan->codepoint_to_glyph->keys () | hb_sink (plan->unicodes);
334 + plan->codepoint_to_glyph->values () | hb_sink (plan->_glyphset_gsub);
335
336 cmap.fini ();
337 }
338
339 static void
_populate_gids_to_retain(hb_subset_plan_t * plan,bool close_over_gsub,bool close_over_gpos,bool close_over_gdef)340 _populate_gids_to_retain (hb_subset_plan_t* plan,
341 bool close_over_gsub,
342 bool close_over_gpos,
343 bool close_over_gdef)
344 {
345 OT::glyf::accelerator_t glyf;
346 #ifndef HB_NO_SUBSET_CFF
347 OT::cff1::accelerator_t cff;
348 #endif
349 glyf.init (plan->source);
350 #ifndef HB_NO_SUBSET_CFF
351 cff.init (plan->source);
352 #endif
353
354 plan->_glyphset_gsub->add (0); // Not-def
355
356 _cmap_closure (plan->source, plan->unicodes, plan->_glyphset_gsub);
357
358 #ifndef HB_NO_SUBSET_LAYOUT
359 if (close_over_gsub)
360 // closure all glyphs/lookups/features needed for GSUB substitutions.
361 _closure_glyphs_lookups_features<OT::GSUB> (
362 plan->source,
363 plan->_glyphset_gsub,
364 plan->layout_features,
365 plan->gsub_lookups,
366 plan->gsub_features,
367 plan->gsub_langsys);
368
369 if (close_over_gpos)
370 _closure_glyphs_lookups_features<OT::GPOS> (
371 plan->source,
372 plan->_glyphset_gsub,
373 plan->layout_features,
374 plan->gpos_lookups,
375 plan->gpos_features,
376 plan->gpos_langsys);
377 #endif
378 _remove_invalid_gids (plan->_glyphset_gsub, plan->source->get_num_glyphs ());
379
380 hb_set_set (plan->_glyphset_mathed, plan->_glyphset_gsub);
381 _math_closure (plan->source, plan->_glyphset_mathed);
382 _remove_invalid_gids (plan->_glyphset_mathed, plan->source->get_num_glyphs ());
383
384 hb_set_t cur_glyphset = *plan->_glyphset_mathed;
385 _colr_closure (plan->source, plan->colrv1_layers, plan->colr_palettes, &cur_glyphset);
386 _remove_invalid_gids (&cur_glyphset, plan->source->get_num_glyphs ());
387
388 // Populate a full set of glyphs to retain by adding all referenced
389 // composite glyphs.
390 for (hb_codepoint_t gid : cur_glyphset.iter ())
391 {
392 glyf.add_gid_and_children (gid, plan->_glyphset);
393 #ifndef HB_NO_SUBSET_CFF
394 if (cff.is_valid ())
395 _add_cff_seac_components (cff, gid, plan->_glyphset);
396 #endif
397 }
398
399 _remove_invalid_gids (plan->_glyphset, plan->source->get_num_glyphs ());
400
401
402 #ifndef HB_NO_VAR
403 if (close_over_gdef)
404 _collect_layout_variation_indices (plan->source,
405 plan->_glyphset_gsub,
406 plan->gpos_lookups,
407 plan->layout_variation_indices,
408 plan->layout_variation_idx_map);
409 #endif
410
411 #ifndef HB_NO_SUBSET_CFF
412 cff.fini ();
413 #endif
414 glyf.fini ();
415 }
416
417 static void
_create_old_gid_to_new_gid_map(const hb_face_t * face,bool retain_gids,const hb_set_t * all_gids_to_retain,hb_map_t * glyph_map,hb_map_t * reverse_glyph_map,unsigned int * num_glyphs)418 _create_old_gid_to_new_gid_map (const hb_face_t *face,
419 bool retain_gids,
420 const hb_set_t *all_gids_to_retain,
421 hb_map_t *glyph_map, /* OUT */
422 hb_map_t *reverse_glyph_map, /* OUT */
423 unsigned int *num_glyphs /* OUT */)
424 {
425 if (!retain_gids)
426 {
427 + hb_enumerate (hb_iter (all_gids_to_retain), (hb_codepoint_t) 0)
428 | hb_sink (reverse_glyph_map)
429 ;
430 *num_glyphs = reverse_glyph_map->get_population ();
431 } else {
432 + hb_iter (all_gids_to_retain)
433 | hb_map ([] (hb_codepoint_t _) {
434 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, _);
435 })
436 | hb_sink (reverse_glyph_map)
437 ;
438
439 unsigned max_glyph =
440 + hb_iter (all_gids_to_retain)
441 | hb_reduce (hb_max, 0u)
442 ;
443 *num_glyphs = max_glyph + 1;
444 }
445
446 + reverse_glyph_map->iter ()
447 | hb_map (&hb_pair_t<hb_codepoint_t, hb_codepoint_t>::reverse)
448 | hb_sink (glyph_map)
449 ;
450 }
451
452 static void
_nameid_closure(hb_face_t * face,hb_set_t * nameids)453 _nameid_closure (hb_face_t *face,
454 hb_set_t *nameids)
455 {
456 #ifndef HB_NO_STYLE
457 face->table.STAT->collect_name_ids (nameids);
458 #endif
459 #ifndef HB_NO_VAR
460 face->table.fvar->collect_name_ids (nameids);
461 #endif
462 }
463
464 /**
465 * hb_subset_plan_create:
466 * @face: font face to create the plan for.
467 * @input: a #hb_subset_input_t input.
468 *
469 * Computes a plan for subsetting the supplied face according
470 * to a provided input. The plan describes
471 * which tables and glyphs should be retained.
472 *
473 * Return value: (transfer full): New subset plan. Destroy with
474 * hb_subset_plan_destroy().
475 *
476 * Since: 1.7.5
477 **/
478 hb_subset_plan_t *
hb_subset_plan_create(hb_face_t * face,const hb_subset_input_t * input)479 hb_subset_plan_create (hb_face_t *face,
480 const hb_subset_input_t *input)
481 {
482 hb_subset_plan_t *plan;
483 if (unlikely (!(plan = hb_object_create<hb_subset_plan_t> ())))
484 return const_cast<hb_subset_plan_t *> (&Null (hb_subset_plan_t));
485
486 plan->successful = true;
487 plan->flags = input->flags;
488 plan->unicodes = hb_set_create ();
489 plan->name_ids = hb_set_copy (input->sets.name_ids);
490 _nameid_closure (face, plan->name_ids);
491 plan->name_languages = hb_set_copy (input->sets.name_languages);
492 plan->layout_features = hb_set_copy (input->sets.layout_features);
493 plan->glyphs_requested = hb_set_copy (input->sets.glyphs);
494 plan->drop_tables = hb_set_copy (input->sets.drop_tables);
495 plan->no_subset_tables = hb_set_copy (input->sets.no_subset_tables);
496 plan->source = hb_face_reference (face);
497 plan->dest = hb_face_builder_create ();
498
499 plan->_glyphset = hb_set_create ();
500 plan->_glyphset_gsub = hb_set_create ();
501 plan->_glyphset_mathed = hb_set_create ();
502 plan->codepoint_to_glyph = hb_map_create ();
503 plan->glyph_map = hb_map_create ();
504 plan->reverse_glyph_map = hb_map_create ();
505 plan->gsub_lookups = hb_map_create ();
506 plan->gpos_lookups = hb_map_create ();
507
508 if (plan->check_success (plan->gsub_langsys = hb_object_create<script_langsys_map> ()))
509 plan->gsub_langsys->init_shallow ();
510 if (plan->check_success (plan->gpos_langsys = hb_object_create<script_langsys_map> ()))
511 plan->gpos_langsys->init_shallow ();
512
513 plan->gsub_features = hb_map_create ();
514 plan->gpos_features = hb_map_create ();
515 plan->colrv1_layers = hb_map_create ();
516 plan->colr_palettes = hb_map_create ();
517 plan->layout_variation_indices = hb_set_create ();
518 plan->layout_variation_idx_map = hb_map_create ();
519
520 if (plan->in_error ()) {
521 return plan;
522 }
523
524 _populate_unicodes_to_retain (input->sets.unicodes, input->sets.glyphs, plan);
525
526 _populate_gids_to_retain (plan,
527 !input->sets.drop_tables->has (HB_OT_TAG_GSUB),
528 !input->sets.drop_tables->has (HB_OT_TAG_GPOS),
529 !input->sets.drop_tables->has (HB_OT_TAG_GDEF));
530
531 _create_old_gid_to_new_gid_map (face,
532 input->flags & HB_SUBSET_FLAGS_RETAIN_GIDS,
533 plan->_glyphset,
534 plan->glyph_map,
535 plan->reverse_glyph_map,
536 &plan->_num_output_glyphs);
537
538 return plan;
539 }
540
541 /**
542 * hb_subset_plan_destroy:
543 * @plan: a #hb_subset_plan_t
544 *
545 * Decreases the reference count on @plan, and if it reaches zero, destroys
546 * @plan, freeing all memory.
547 *
548 * Since: 1.7.5
549 **/
550 void
hb_subset_plan_destroy(hb_subset_plan_t * plan)551 hb_subset_plan_destroy (hb_subset_plan_t *plan)
552 {
553 if (!hb_object_destroy (plan)) return;
554
555 hb_set_destroy (plan->unicodes);
556 hb_set_destroy (plan->name_ids);
557 hb_set_destroy (plan->name_languages);
558 hb_set_destroy (plan->layout_features);
559 hb_set_destroy (plan->glyphs_requested);
560 hb_set_destroy (plan->drop_tables);
561 hb_set_destroy (plan->no_subset_tables);
562 hb_face_destroy (plan->source);
563 hb_face_destroy (plan->dest);
564 hb_map_destroy (plan->codepoint_to_glyph);
565 hb_map_destroy (plan->glyph_map);
566 hb_map_destroy (plan->reverse_glyph_map);
567 hb_set_destroy (plan->_glyphset);
568 hb_set_destroy (plan->_glyphset_gsub);
569 hb_set_destroy (plan->_glyphset_mathed);
570 hb_map_destroy (plan->gsub_lookups);
571 hb_map_destroy (plan->gpos_lookups);
572 hb_map_destroy (plan->gsub_features);
573 hb_map_destroy (plan->gpos_features);
574 hb_map_destroy (plan->colrv1_layers);
575 hb_map_destroy (plan->colr_palettes);
576 hb_set_destroy (plan->layout_variation_indices);
577 hb_map_destroy (plan->layout_variation_idx_map);
578
579 if (plan->gsub_langsys)
580 {
581 for (auto _ : plan->gsub_langsys->iter ())
582 hb_set_destroy (_.second);
583
584 hb_object_destroy (plan->gsub_langsys);
585 plan->gsub_langsys->fini_shallow ();
586 hb_free (plan->gsub_langsys);
587 }
588
589 if (plan->gpos_langsys)
590 {
591 for (auto _ : plan->gpos_langsys->iter ())
592 hb_set_destroy (_.second);
593
594 hb_object_destroy (plan->gpos_langsys);
595 plan->gpos_langsys->fini_shallow ();
596 hb_free (plan->gpos_langsys);
597 }
598
599 hb_free (plan);
600 }
601