2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Garret Rieger, Roderick Sheeter
27 #include "hb-subset-plan.hh"
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-cff1-table.hh"
36 #include "hb-ot-color-colr-table.hh"
37 #include "hb-ot-var-fvar-table.hh"
38 #include "hb-ot-stat-table.hh"
41 #ifndef HB_NO_SUBSET_CFF
43 _add_cff_seac_components (const OT::cff1::accelerator_t &cff,
45 hb_set_t *gids_to_retain)
47 hb_codepoint_t base_gid, accent_gid;
48 if (cff.get_seac_components (gid, &base_gid, &accent_gid))
50 gids_to_retain->add (base_gid);
51 gids_to_retain->add (accent_gid);
56 #ifndef HB_NO_SUBSET_LAYOUT
57 #ifdef HB_EXPERIMENTAL_API
59 _remap_indexes (const hb_set_t *indexes,
60 hb_map_t *mapping /* OUT */)
62 unsigned count = indexes->get_population ();
64 for (auto _ : + hb_zip (indexes->iter (), hb_range (count)))
65 mapping->set (_.first, _.second);
71 _gsub_closure_glyphs_lookups_features (hb_face_t *face,
72 hb_set_t *gids_to_retain,
73 hb_map_t *gsub_lookups,
74 hb_map_t *gsub_features)
76 hb_set_t lookup_indices;
77 hb_ot_layout_collect_lookups (face,
83 hb_ot_layout_lookups_substitute_closure (face,
86 #ifdef HB_EXPERIMENTAL_API
87 hb_ot_layout_closure_lookups (face,
91 _remap_indexes (&lookup_indices, gsub_lookups);
94 hb_set_t feature_indices;
95 hb_ot_layout_closure_features (face,
99 _remap_indexes (&feature_indices, gsub_features);
104 _gpos_closure_lookups_features (hb_face_t *face,
105 const hb_set_t *gids_to_retain,
106 hb_map_t *gpos_lookups,
107 hb_map_t *gpos_features)
109 hb_set_t lookup_indices;
110 hb_ot_layout_collect_lookups (face,
116 #ifdef HB_EXPERIMENTAL_API
117 hb_ot_layout_closure_lookups (face,
121 _remap_indexes (&lookup_indices, gpos_lookups);
124 hb_set_t feature_indices;
125 hb_ot_layout_closure_features (face,
129 _remap_indexes (&feature_indices, gpos_features);
136 _collect_layout_variation_indices (hb_face_t *face,
137 const hb_set_t *glyphset,
138 const hb_map_t *gpos_lookups,
139 hb_set_t *layout_variation_indices,
140 hb_map_t *layout_variation_idx_map)
142 hb_blob_ptr_t<OT::GDEF> gdef = hb_sanitize_context_t ().reference_table<OT::GDEF> (face);
143 hb_blob_ptr_t<OT::GPOS> gpos = hb_sanitize_context_t ().reference_table<OT::GPOS> (face);
145 if (!gdef->has_data ())
151 OT::hb_collect_variation_indices_context_t c (layout_variation_indices, glyphset, gpos_lookups);
152 gdef->collect_variation_indices (&c);
154 if (hb_ot_layout_has_positioning (face))
155 gpos->collect_variation_indices (&c);
157 gdef->remap_layout_variation_indices (layout_variation_indices, layout_variation_idx_map);
165 _cmap_closure (hb_face_t *face,
166 const hb_set_t *unicodes,
169 OT::cmap::accelerator_t cmap;
171 cmap.table->closure_glyphs (unicodes, glyphset);
176 _remove_invalid_gids (hb_set_t *glyphs,
177 unsigned int num_glyphs)
179 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
180 while (glyphs->next (&gid))
182 if (gid >= num_glyphs)
188 _populate_gids_to_retain (hb_subset_plan_t* plan,
189 const hb_set_t *unicodes,
190 const hb_set_t *input_glyphs_to_retain,
191 bool close_over_gsub,
192 bool close_over_gpos,
193 bool close_over_gdef)
195 OT::cmap::accelerator_t cmap;
196 OT::glyf::accelerator_t glyf;
197 #ifndef HB_NO_SUBSET_CFF
198 OT::cff1::accelerator_t cff;
200 OT::COLR::accelerator_t colr;
201 cmap.init (plan->source);
202 glyf.init (plan->source);
203 #ifndef HB_NO_SUBSET_CFF
204 cff.init (plan->source);
206 colr.init (plan->source);
208 plan->_glyphset_gsub->add (0); // Not-def
209 hb_set_union (plan->_glyphset_gsub, input_glyphs_to_retain);
211 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
212 while (unicodes->next (&cp))
215 if (!cmap.get_nominal_glyph (cp, &gid))
217 DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
220 plan->unicodes->add (cp);
221 plan->codepoint_to_glyph->set (cp, gid);
222 plan->_glyphset_gsub->add (gid);
225 _cmap_closure (plan->source, plan->unicodes, plan->_glyphset_gsub);
227 #ifndef HB_NO_SUBSET_LAYOUT
229 // closure all glyphs/lookups/features needed for GSUB substitutions.
230 _gsub_closure_glyphs_lookups_features (plan->source, plan->_glyphset_gsub, plan->gsub_lookups, plan->gsub_features);
233 _gpos_closure_lookups_features (plan->source, plan->_glyphset_gsub, plan->gpos_lookups, plan->gpos_features);
235 _remove_invalid_gids (plan->_glyphset_gsub, plan->source->get_num_glyphs ());
237 // Populate a full set of glyphs to retain by adding all referenced
239 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
240 while (plan->_glyphset_gsub->next (&gid))
242 glyf.add_gid_and_children (gid, plan->_glyphset);
243 #ifndef HB_NO_SUBSET_CFF
245 _add_cff_seac_components (cff, gid, plan->_glyphset);
247 if (colr.is_valid ())
248 colr.closure_glyphs (gid, plan->_glyphset);
251 _remove_invalid_gids (plan->_glyphset, plan->source->get_num_glyphs ());
255 _collect_layout_variation_indices (plan->source, plan->_glyphset, plan->gpos_lookups, plan->layout_variation_indices, plan->layout_variation_idx_map);
258 #ifndef HB_NO_SUBSET_CFF
266 _create_old_gid_to_new_gid_map (const hb_face_t *face,
268 const hb_set_t *all_gids_to_retain,
269 hb_map_t *glyph_map, /* OUT */
270 hb_map_t *reverse_glyph_map, /* OUT */
271 unsigned int *num_glyphs /* OUT */)
275 + hb_enumerate (hb_iter (all_gids_to_retain), (hb_codepoint_t) 0)
276 | hb_sink (reverse_glyph_map)
278 *num_glyphs = reverse_glyph_map->get_population ();
280 + hb_iter (all_gids_to_retain)
281 | hb_map ([] (hb_codepoint_t _) {
282 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, _);
284 | hb_sink (reverse_glyph_map)
288 + hb_iter (all_gids_to_retain)
289 | hb_reduce (hb_max, 0u)
291 *num_glyphs = max_glyph + 1;
294 + reverse_glyph_map->iter ()
295 | hb_map (&hb_pair_t<hb_codepoint_t, hb_codepoint_t>::reverse)
296 | hb_sink (glyph_map)
301 _nameid_closure (hb_face_t *face,
305 face->table.STAT->collect_name_ids (nameids);
308 face->table.fvar->collect_name_ids (nameids);
313 * hb_subset_plan_create:
314 * Computes a plan for subsetting the supplied face according
315 * to a provided input. The plan describes
316 * which tables and glyphs should be retained.
318 * Return value: New subset plan.
323 hb_subset_plan_create (hb_face_t *face,
324 hb_subset_input_t *input)
326 hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
328 plan->drop_hints = input->drop_hints;
329 plan->desubroutinize = input->desubroutinize;
330 plan->retain_gids = input->retain_gids;
331 plan->name_legacy = input->name_legacy;
332 plan->unicodes = hb_set_create ();
333 plan->name_ids = hb_set_reference (input->name_ids);
334 _nameid_closure (face, plan->name_ids);
335 plan->name_languages = hb_set_reference (input->name_languages);
336 plan->glyphs_requested = hb_set_reference (input->glyphs);
337 plan->drop_tables = hb_set_reference (input->drop_tables);
338 plan->source = hb_face_reference (face);
339 plan->dest = hb_face_builder_create ();
341 plan->_glyphset = hb_set_create ();
342 plan->_glyphset_gsub = hb_set_create ();
343 plan->codepoint_to_glyph = hb_map_create ();
344 plan->glyph_map = hb_map_create ();
345 plan->reverse_glyph_map = hb_map_create ();
346 plan->gsub_lookups = hb_map_create ();
347 plan->gpos_lookups = hb_map_create ();
348 plan->gsub_features = hb_map_create ();
349 plan->gpos_features = hb_map_create ();
350 plan->layout_variation_indices = hb_set_create ();
351 plan->layout_variation_idx_map = hb_map_create ();
353 _populate_gids_to_retain (plan,
356 !input->drop_tables->has (HB_OT_TAG_GSUB),
357 !input->drop_tables->has (HB_OT_TAG_GPOS),
358 !input->drop_tables->has (HB_OT_TAG_GDEF));
360 _create_old_gid_to_new_gid_map (face,
364 plan->reverse_glyph_map,
365 &plan->_num_output_glyphs);
371 * hb_subset_plan_destroy:
376 hb_subset_plan_destroy (hb_subset_plan_t *plan)
378 if (!hb_object_destroy (plan)) return;
380 hb_set_destroy (plan->unicodes);
381 hb_set_destroy (plan->name_ids);
382 hb_set_destroy (plan->name_languages);
383 hb_set_destroy (plan->glyphs_requested);
384 hb_set_destroy (plan->drop_tables);
385 hb_face_destroy (plan->source);
386 hb_face_destroy (plan->dest);
387 hb_map_destroy (plan->codepoint_to_glyph);
388 hb_map_destroy (plan->glyph_map);
389 hb_map_destroy (plan->reverse_glyph_map);
390 hb_set_destroy (plan->_glyphset);
391 hb_set_destroy (plan->_glyphset_gsub);
392 hb_map_destroy (plan->gsub_lookups);
393 hb_map_destroy (plan->gpos_lookups);
394 hb_map_destroy (plan->gsub_features);
395 hb_map_destroy (plan->gpos_features);
396 hb_set_destroy (plan->layout_variation_indices);
397 hb_map_destroy (plan->layout_variation_idx_map);