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-var-fvar-table.hh"
39#include "hb-ot-stat-table.hh"
40
41
42#ifndef HB_NO_SUBSET_CFF
43static inline void
44_add_cff_seac_components (const OT::cff1::accelerator_t &cff,
45 hb_codepoint_t gid,
46 hb_set_t *gids_to_retain)
47{
48 hb_codepoint_t base_gid, accent_gid;
49 if (cff.get_seac_components (gid, &base_gid, &accent_gid))
50 {
51 gids_to_retain->add (base_gid);
52 gids_to_retain->add (accent_gid);
53 }
54}
55#endif
56
57#ifndef HB_NO_SUBSET_LAYOUT
58static void
59_remap_indexes (const hb_set_t *indexes,
60 hb_map_t *mapping /* OUT */)
61{
62 unsigned count = indexes->get_population ();
63
64 for (auto _ : + hb_zip (indexes->iter (), hb_range (count)))
65 mapping->set (_.first, _.second);
66
67}
68
69static inline void
70_gsub_closure_glyphs_lookups_features (hb_face_t *face,
71 hb_set_t *gids_to_retain,
72 hb_map_t *gsub_lookups,
73 hb_map_t *gsub_features)
74{
75 hb_set_t lookup_indices;
76 hb_ot_layout_collect_lookups (face,
77 HB_OT_TAG_GSUB,
78 nullptr,
79 nullptr,
80 nullptr,
81 &lookup_indices);
82 hb_ot_layout_lookups_substitute_closure (face,
83 &lookup_indices,
84 gids_to_retain);
85 hb_blob_ptr_t<OT::GSUB> gsub = hb_sanitize_context_t ().reference_table<OT::GSUB> (face);
86 gsub->closure_lookups (face,
87 gids_to_retain,
88 &lookup_indices);
89 _remap_indexes (&lookup_indices, gsub_lookups);
90
91 //closure features
92 hb_set_t feature_indices;
93 gsub->closure_features (gsub_lookups, &feature_indices);
94 _remap_indexes (&feature_indices, gsub_features);
95 gsub.destroy ();
96}
97
98static inline void
99_gpos_closure_lookups_features (hb_face_t *face,
100 const hb_set_t *gids_to_retain,
101 hb_map_t *gpos_lookups,
102 hb_map_t *gpos_features)
103{
104 hb_set_t lookup_indices;
105 hb_ot_layout_collect_lookups (face,
106 HB_OT_TAG_GPOS,
107 nullptr,
108 nullptr,
109 nullptr,
110 &lookup_indices);
111 hb_blob_ptr_t<OT::GPOS> gpos = hb_sanitize_context_t ().reference_table<OT::GPOS> (face);
112 gpos->closure_lookups (face,
113 gids_to_retain,
114 &lookup_indices);
115 _remap_indexes (&lookup_indices, gpos_lookups);
116
117 //closure features
118 hb_set_t feature_indices;
119 gpos->closure_features (gpos_lookups, &feature_indices);
120 _remap_indexes (&feature_indices, gpos_features);
121 gpos.destroy ();
122}
123#endif
124
125#ifndef HB_NO_VAR
126static inline void
127 _collect_layout_variation_indices (hb_face_t *face,
128 const hb_set_t *glyphset,
129 const hb_map_t *gpos_lookups,
130 hb_set_t *layout_variation_indices,
131 hb_map_t *layout_variation_idx_map)
132{
133 hb_blob_ptr_t<OT::GDEF> gdef = hb_sanitize_context_t ().reference_table<OT::GDEF> (face);
134 hb_blob_ptr_t<OT::GPOS> gpos = hb_sanitize_context_t ().reference_table<OT::GPOS> (face);
135
136 if (!gdef->has_data ())
137 {
138 gdef.destroy ();
139 gpos.destroy ();
140 return;
141 }
142 OT::hb_collect_variation_indices_context_t c (layout_variation_indices, glyphset, gpos_lookups);
143 gdef->collect_variation_indices (&c);
144
145 if (hb_ot_layout_has_positioning (face))
146 gpos->collect_variation_indices (&c);
147
148 gdef->remap_layout_variation_indices (layout_variation_indices, layout_variation_idx_map);
149
150 gdef.destroy ();
151 gpos.destroy ();
152}
153#endif
154
155static inline void
156_cmap_closure (hb_face_t *face,
157 const hb_set_t *unicodes,
158 hb_set_t *glyphset)
159{
160 OT::cmap::accelerator_t cmap;
161 cmap.init (face);
162 cmap.table->closure_glyphs (unicodes, glyphset);
163 cmap.fini ();
164}
165
166static inline void
167_remove_invalid_gids (hb_set_t *glyphs,
168 unsigned int num_glyphs)
169{
170 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
171 while (glyphs->next (&gid))
172 {
173 if (gid >= num_glyphs)
174 glyphs->del (gid);
175 }
176}
177
178static void
179_populate_gids_to_retain (hb_subset_plan_t* plan,
180 const hb_set_t *unicodes,
181 const hb_set_t *input_glyphs_to_retain,
182 bool close_over_gsub,
183 bool close_over_gpos,
184 bool close_over_gdef)
185{
186 OT::cmap::accelerator_t cmap;
187 OT::glyf::accelerator_t glyf;
188#ifndef HB_NO_SUBSET_CFF
189 OT::cff1::accelerator_t cff;
190#endif
191 OT::COLR::accelerator_t colr;
192 cmap.init (plan->source);
193 glyf.init (plan->source);
194#ifndef HB_NO_SUBSET_CFF
195 cff.init (plan->source);
196#endif
197 colr.init (plan->source);
198
199 plan->_glyphset_gsub->add (0); // Not-def
200 hb_set_union (plan->_glyphset_gsub, input_glyphs_to_retain);
201
202 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
203 while (unicodes->next (&cp))
204 {
205 hb_codepoint_t gid;
206 if (!cmap.get_nominal_glyph (cp, &gid))
207 {
208 DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
209 continue;
210 }
211 plan->unicodes->add (cp);
212 plan->codepoint_to_glyph->set (cp, gid);
213 plan->_glyphset_gsub->add (gid);
214 }
215
216 _cmap_closure (plan->source, plan->unicodes, plan->_glyphset_gsub);
217
218#ifndef HB_NO_SUBSET_LAYOUT
219 if (close_over_gsub)
220 // closure all glyphs/lookups/features needed for GSUB substitutions.
221 _gsub_closure_glyphs_lookups_features (plan->source, plan->_glyphset_gsub, plan->gsub_lookups, plan->gsub_features);
222
223 if (close_over_gpos)
224 _gpos_closure_lookups_features (plan->source, plan->_glyphset_gsub, plan->gpos_lookups, plan->gpos_features);
225#endif
226 _remove_invalid_gids (plan->_glyphset_gsub, plan->source->get_num_glyphs ());
227
228 // Populate a full set of glyphs to retain by adding all referenced
229 // composite glyphs.
230 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
231 while (plan->_glyphset_gsub->next (&gid))
232 {
233 glyf.add_gid_and_children (gid, plan->_glyphset);
234#ifndef HB_NO_SUBSET_CFF
235 if (cff.is_valid ())
236 _add_cff_seac_components (cff, gid, plan->_glyphset);
237#endif
238 if (colr.is_valid ())
239 colr.closure_glyphs (gid, plan->_glyphset);
240 }
241
242 _remove_invalid_gids (plan->_glyphset, plan->source->get_num_glyphs ());
243
244#ifndef HB_NO_VAR
245 if (close_over_gdef)
246 _collect_layout_variation_indices (plan->source, plan->_glyphset, plan->gpos_lookups, plan->layout_variation_indices, plan->layout_variation_idx_map);
247#endif
248
249#ifndef HB_NO_SUBSET_CFF
250 cff.fini ();
251#endif
252 glyf.fini ();
253 cmap.fini ();
254}
255
256static void
257_create_old_gid_to_new_gid_map (const hb_face_t *face,
258 bool retain_gids,
259 const hb_set_t *all_gids_to_retain,
260 hb_map_t *glyph_map, /* OUT */
261 hb_map_t *reverse_glyph_map, /* OUT */
262 unsigned int *num_glyphs /* OUT */)
263{
264 if (!retain_gids)
265 {
266 + hb_enumerate (hb_iter (all_gids_to_retain), (hb_codepoint_t) 0)
267 | hb_sink (reverse_glyph_map)
268 ;
269 *num_glyphs = reverse_glyph_map->get_population ();
270 } else {
271 + hb_iter (all_gids_to_retain)
272 | hb_map ([] (hb_codepoint_t _) {
273 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, _);
274 })
275 | hb_sink (reverse_glyph_map)
276 ;
277
278 unsigned max_glyph =
279 + hb_iter (all_gids_to_retain)
280 | hb_reduce (hb_max, 0u)
281 ;
282 *num_glyphs = max_glyph + 1;
283 }
284
285 + reverse_glyph_map->iter ()
286 | hb_map (&hb_pair_t<hb_codepoint_t, hb_codepoint_t>::reverse)
287 | hb_sink (glyph_map)
288 ;
289}
290
291static void
292_nameid_closure (hb_face_t *face,
293 hb_set_t *nameids)
294{
295#ifndef HB_NO_STYLE
296 face->table.STAT->collect_name_ids (nameids);
297#endif
298#ifndef HB_NO_VAR
299 face->table.fvar->collect_name_ids (nameids);
300#endif
301}
302
303/**
304 * hb_subset_plan_create:
305 * Computes a plan for subsetting the supplied face according
306 * to a provided input. The plan describes
307 * which tables and glyphs should be retained.
308 *
309 * Return value: New subset plan.
310 *
311 * Since: 1.7.5
312 **/
313hb_subset_plan_t *
314hb_subset_plan_create (hb_face_t *face,
315 hb_subset_input_t *input)
316{
317 hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
318
319 plan->drop_hints = input->drop_hints;
320 plan->desubroutinize = input->desubroutinize;
321 plan->retain_gids = input->retain_gids;
322 plan->name_legacy = input->name_legacy;
323 plan->unicodes = hb_set_create ();
324 plan->name_ids = hb_set_reference (input->name_ids);
325 _nameid_closure (face, plan->name_ids);
326 plan->name_languages = hb_set_reference (input->name_languages);
327 plan->glyphs_requested = hb_set_reference (input->glyphs);
328 plan->drop_tables = hb_set_reference (input->drop_tables);
329 plan->source = hb_face_reference (face);
330 plan->dest = hb_face_builder_create ();
331
332 plan->_glyphset = hb_set_create ();
333 plan->_glyphset_gsub = hb_set_create ();
334 plan->codepoint_to_glyph = hb_map_create ();
335 plan->glyph_map = hb_map_create ();
336 plan->reverse_glyph_map = hb_map_create ();
337 plan->gsub_lookups = hb_map_create ();
338 plan->gpos_lookups = hb_map_create ();
339 plan->gsub_features = hb_map_create ();
340 plan->gpos_features = hb_map_create ();
341 plan->layout_variation_indices = hb_set_create ();
342 plan->layout_variation_idx_map = hb_map_create ();
343
344 _populate_gids_to_retain (plan,
345 input->unicodes,
346 input->glyphs,
347 !input->drop_tables->has (HB_OT_TAG_GSUB),
348 !input->drop_tables->has (HB_OT_TAG_GPOS),
349 !input->drop_tables->has (HB_OT_TAG_GDEF));
350
351 _create_old_gid_to_new_gid_map (face,
352 input->retain_gids,
353 plan->_glyphset,
354 plan->glyph_map,
355 plan->reverse_glyph_map,
356 &plan->_num_output_glyphs);
357
358 return plan;
359}
360
361/**
362 * hb_subset_plan_destroy:
363 *
364 * Since: 1.7.5
365 **/
366void
367hb_subset_plan_destroy (hb_subset_plan_t *plan)
368{
369 if (!hb_object_destroy (plan)) return;
370
371 hb_set_destroy (plan->unicodes);
372 hb_set_destroy (plan->name_ids);
373 hb_set_destroy (plan->name_languages);
374 hb_set_destroy (plan->glyphs_requested);
375 hb_set_destroy (plan->drop_tables);
376 hb_face_destroy (plan->source);
377 hb_face_destroy (plan->dest);
378 hb_map_destroy (plan->codepoint_to_glyph);
379 hb_map_destroy (plan->glyph_map);
380 hb_map_destroy (plan->reverse_glyph_map);
381 hb_set_destroy (plan->_glyphset);
382 hb_set_destroy (plan->_glyphset_gsub);
383 hb_map_destroy (plan->gsub_lookups);
384 hb_map_destroy (plan->gpos_lookups);
385 hb_map_destroy (plan->gsub_features);
386 hb_map_destroy (plan->gpos_features);
387 hb_set_destroy (plan->layout_variation_indices);
388 hb_map_destroy (plan->layout_variation_idx_map);
389
390
391 free (plan);
392}
393