Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / hb-subset-plan.cc
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-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"
39
40
41 #ifndef HB_NO_SUBSET_CFF
42 static inline void
43 _add_cff_seac_components (const OT::cff1::accelerator_t &cff,
44                           hb_codepoint_t gid,
45                           hb_set_t *gids_to_retain)
46 {
47   hb_codepoint_t base_gid, accent_gid;
48   if (cff.get_seac_components (gid, &base_gid, &accent_gid))
49   {
50     gids_to_retain->add (base_gid);
51     gids_to_retain->add (accent_gid);
52   }
53 }
54 #endif
55
56 #ifndef HB_NO_SUBSET_LAYOUT
57 #ifdef HB_EXPERIMENTAL_API
58 static 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 #endif
69
70 static inline void
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)
75 {
76   hb_set_t lookup_indices;
77   hb_ot_layout_collect_lookups (face,
78                                 HB_OT_TAG_GSUB,
79                                 nullptr,
80                                 nullptr,
81                                 nullptr,
82                                 &lookup_indices);
83   hb_ot_layout_lookups_substitute_closure (face,
84                                            &lookup_indices,
85                                            gids_to_retain);
86 #ifdef HB_EXPERIMENTAL_API
87   hb_ot_layout_closure_lookups (face,
88                                 HB_OT_TAG_GSUB,
89                                 gids_to_retain,
90                                 &lookup_indices);
91   _remap_indexes (&lookup_indices, gsub_lookups);
92
93   //closure features
94   hb_set_t feature_indices;
95   hb_ot_layout_closure_features (face,
96                                  HB_OT_TAG_GSUB,
97                                  gsub_lookups,
98                                  &feature_indices);
99   _remap_indexes (&feature_indices, gsub_features);
100 #endif
101 }
102
103 static inline void
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)
108 {
109   hb_set_t lookup_indices;
110   hb_ot_layout_collect_lookups (face,
111                                 HB_OT_TAG_GPOS,
112                                 nullptr,
113                                 nullptr,
114                                 nullptr,
115                                 &lookup_indices);
116 #ifdef HB_EXPERIMENTAL_API
117   hb_ot_layout_closure_lookups (face,
118                                 HB_OT_TAG_GPOS,
119                                 gids_to_retain,
120                                 &lookup_indices);
121   _remap_indexes (&lookup_indices, gpos_lookups);
122
123   //closure features
124   hb_set_t feature_indices;
125   hb_ot_layout_closure_features (face,
126                                  HB_OT_TAG_GPOS,
127                                  gpos_lookups,
128                                  &feature_indices);
129   _remap_indexes (&feature_indices, gpos_features);
130 #endif
131 }
132 #endif
133
134 #ifndef HB_NO_VAR
135 static inline void
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)
141 {
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);
144
145   if (!gdef->has_data ())
146   {
147     gdef.destroy ();
148     gpos.destroy ();
149     return;
150   }
151   OT::hb_collect_variation_indices_context_t c (layout_variation_indices, glyphset, gpos_lookups);
152   gdef->collect_variation_indices (&c);
153
154   if (hb_ot_layout_has_positioning (face))
155     gpos->collect_variation_indices (&c);
156
157   gdef->remap_layout_variation_indices (layout_variation_indices, layout_variation_idx_map);
158   
159   gdef.destroy ();
160   gpos.destroy ();
161 }
162 #endif
163
164 static inline void
165 _cmap_closure (hb_face_t           *face,
166                const hb_set_t      *unicodes,
167                hb_set_t            *glyphset)
168 {
169   OT::cmap::accelerator_t cmap;
170   cmap.init (face);
171   cmap.table->closure_glyphs (unicodes, glyphset);
172   cmap.fini ();
173 }
174
175 static inline void
176 _remove_invalid_gids (hb_set_t *glyphs,
177                       unsigned int num_glyphs)
178 {
179   hb_codepoint_t gid = HB_SET_VALUE_INVALID;
180   while (glyphs->next (&gid))
181   {
182     if (gid >= num_glyphs)
183       glyphs->del (gid);
184   }
185 }
186
187 static void
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)
194 {
195   OT::cmap::accelerator_t cmap;
196   OT::glyf::accelerator_t glyf;
197 #ifndef HB_NO_SUBSET_CFF
198   OT::cff1::accelerator_t cff;
199 #endif
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);
205 #endif
206   colr.init (plan->source);
207
208   plan->_glyphset_gsub->add (0); // Not-def
209   hb_set_union (plan->_glyphset_gsub, input_glyphs_to_retain);
210
211   hb_codepoint_t cp = HB_SET_VALUE_INVALID;
212   while (unicodes->next (&cp))
213   {
214     hb_codepoint_t gid;
215     if (!cmap.get_nominal_glyph (cp, &gid))
216     {
217       DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
218       continue;
219     }
220     plan->unicodes->add (cp);
221     plan->codepoint_to_glyph->set (cp, gid);
222     plan->_glyphset_gsub->add (gid);
223   }
224
225   _cmap_closure (plan->source, plan->unicodes, plan->_glyphset_gsub);
226
227 #ifndef HB_NO_SUBSET_LAYOUT
228   if (close_over_gsub)
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);
231
232   if (close_over_gpos)
233     _gpos_closure_lookups_features (plan->source, plan->_glyphset_gsub, plan->gpos_lookups, plan->gpos_features);
234 #endif
235   _remove_invalid_gids (plan->_glyphset_gsub, plan->source->get_num_glyphs ());
236
237   // Populate a full set of glyphs to retain by adding all referenced
238   // composite glyphs.
239   hb_codepoint_t gid = HB_SET_VALUE_INVALID;
240   while (plan->_glyphset_gsub->next (&gid))
241   {
242     glyf.add_gid_and_children (gid, plan->_glyphset);
243 #ifndef HB_NO_SUBSET_CFF
244     if (cff.is_valid ())
245       _add_cff_seac_components (cff, gid, plan->_glyphset);
246 #endif
247     if (colr.is_valid ())
248       colr.closure_glyphs (gid, plan->_glyphset);
249   }
250
251   _remove_invalid_gids (plan->_glyphset, plan->source->get_num_glyphs ());
252
253 #ifndef HB_NO_VAR
254   if (close_over_gdef)
255     _collect_layout_variation_indices (plan->source, plan->_glyphset, plan->gpos_lookups, plan->layout_variation_indices, plan->layout_variation_idx_map);
256 #endif
257
258 #ifndef HB_NO_SUBSET_CFF
259   cff.fini ();
260 #endif
261   glyf.fini ();
262   cmap.fini ();
263 }
264
265 static void
266 _create_old_gid_to_new_gid_map (const hb_face_t *face,
267                                 bool             retain_gids,
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 */)
272 {
273   if (!retain_gids)
274   {
275     + hb_enumerate (hb_iter (all_gids_to_retain), (hb_codepoint_t) 0)
276     | hb_sink (reverse_glyph_map)
277     ;
278     *num_glyphs = reverse_glyph_map->get_population ();
279   } else {
280     + hb_iter (all_gids_to_retain)
281     | hb_map ([] (hb_codepoint_t _) {
282                 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, _);
283               })
284     | hb_sink (reverse_glyph_map)
285     ;
286
287     unsigned max_glyph =
288     + hb_iter (all_gids_to_retain)
289     | hb_reduce (hb_max, 0u)
290     ;
291     *num_glyphs = max_glyph + 1;
292   }
293
294   + reverse_glyph_map->iter ()
295   | hb_map (&hb_pair_t<hb_codepoint_t, hb_codepoint_t>::reverse)
296   | hb_sink (glyph_map)
297   ;
298 }
299
300 static void
301 _nameid_closure (hb_face_t *face,
302                  hb_set_t  *nameids)
303 {
304 #ifndef HB_NO_STAT
305   face->table.STAT->collect_name_ids (nameids);
306 #endif
307 #ifndef HB_NO_VAR
308   face->table.fvar->collect_name_ids (nameids);
309 #endif
310 }
311
312 /**
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.
317  *
318  * Return value: New subset plan.
319  *
320  * Since: 1.7.5
321  **/
322 hb_subset_plan_t *
323 hb_subset_plan_create (hb_face_t         *face,
324                        hb_subset_input_t *input)
325 {
326   hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
327
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 ();
340
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 ();
352
353   _populate_gids_to_retain (plan,
354                             input->unicodes,
355                             input->glyphs,
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));
359
360   _create_old_gid_to_new_gid_map (face,
361                                   input->retain_gids,
362                                   plan->_glyphset,
363                                   plan->glyph_map,
364                                   plan->reverse_glyph_map,
365                                   &plan->_num_output_glyphs);
366
367   return plan;
368 }
369
370 /**
371  * hb_subset_plan_destroy:
372  *
373  * Since: 1.7.5
374  **/
375 void
376 hb_subset_plan_destroy (hb_subset_plan_t *plan)
377 {
378   if (!hb_object_destroy (plan)) return;
379
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);
398
399
400   free (plan);
401 }