Imported Upstream version 2.4.0
[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-cff1-table.hh"
34
35 static void
36 _add_gid_and_children (const OT::glyf::accelerator_t &glyf,
37                        hb_codepoint_t gid,
38                        hb_set_t *gids_to_retain)
39 {
40   if (hb_set_has (gids_to_retain, gid))
41     // Already visited this gid, ignore.
42     return;
43
44   hb_set_add (gids_to_retain, gid);
45
46   OT::glyf::CompositeGlyphHeader::Iterator composite;
47   if (glyf.get_composite (gid, &composite))
48   {
49     do
50     {
51       _add_gid_and_children (glyf, (hb_codepoint_t) composite.current->glyphIndex, gids_to_retain);
52     } while (composite.move_to_next());
53   }
54 }
55
56 static void
57 _add_cff_seac_components (const OT::cff1::accelerator_t &cff,
58            hb_codepoint_t gid,
59            hb_set_t *gids_to_retain)
60 {
61   hb_codepoint_t base_gid, accent_gid;
62   if (cff.get_seac_components (gid, &base_gid, &accent_gid))
63   {
64     hb_set_add (gids_to_retain, base_gid);
65     hb_set_add (gids_to_retain, accent_gid);
66   }
67 }
68
69 static void
70 _gsub_closure (hb_face_t *face, hb_set_t *gids_to_retain)
71 {
72   hb_set_t lookup_indices;
73   hb_ot_layout_collect_lookups (face,
74                                 HB_OT_TAG_GSUB,
75                                 nullptr,
76                                 nullptr,
77                                 nullptr,
78                                 &lookup_indices);
79   hb_ot_layout_lookups_substitute_closure (face,
80                                            &lookup_indices,
81                                            gids_to_retain);
82 }
83
84 static void
85 _remove_invalid_gids (hb_set_t *glyphs,
86                       unsigned int num_glyphs)
87 {
88   hb_codepoint_t gid = HB_SET_VALUE_INVALID;
89   while (glyphs->next (&gid))
90   {
91     if (gid >= num_glyphs)
92       glyphs->del (gid);
93   }
94 }
95
96 static hb_set_t *
97 _populate_gids_to_retain (hb_face_t *face,
98                           const hb_set_t *unicodes,
99                           const hb_set_t *input_glyphs_to_retain,
100                           bool close_over_gsub,
101                           hb_set_t *unicodes_to_retain,
102                           hb_map_t *codepoint_to_glyph)
103 {
104   OT::cmap::accelerator_t cmap;
105   OT::glyf::accelerator_t glyf;
106   OT::cff1::accelerator_t cff;
107   cmap.init (face);
108   glyf.init (face);
109   cff.init (face);
110
111   hb_set_t *initial_gids_to_retain = hb_set_create ();
112   initial_gids_to_retain->add (0); // Not-def
113   hb_set_union (initial_gids_to_retain, input_glyphs_to_retain);
114
115   hb_codepoint_t cp = HB_SET_VALUE_INVALID;
116   while (unicodes->next (&cp))
117   {
118     hb_codepoint_t gid;
119     if (!cmap.get_nominal_glyph (cp, &gid))
120     {
121       DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
122       continue;
123     }
124     unicodes_to_retain->add (cp);
125     codepoint_to_glyph->set (cp, gid);
126     initial_gids_to_retain->add (gid);
127   }
128
129   if (close_over_gsub)
130     // Add all glyphs needed for GSUB substitutions.
131     _gsub_closure (face, initial_gids_to_retain);
132
133   // Populate a full set of glyphs to retain by adding all referenced
134   // composite glyphs.
135   hb_codepoint_t gid = HB_SET_VALUE_INVALID;
136   hb_set_t *all_gids_to_retain = hb_set_create ();
137   while (initial_gids_to_retain->next (&gid))
138   {
139     _add_gid_and_children (glyf, gid, all_gids_to_retain);
140     if (cff.is_valid ())
141       _add_cff_seac_components (cff, gid, all_gids_to_retain);
142   }
143   hb_set_destroy (initial_gids_to_retain);
144
145   _remove_invalid_gids (all_gids_to_retain, face->get_num_glyphs ());
146
147
148   cff.fini ();
149   glyf.fini ();
150   cmap.fini ();
151
152   return all_gids_to_retain;
153 }
154
155 static void
156 _create_old_gid_to_new_gid_map (const hb_face_t                   *face,
157                                 bool                               retain_gids,
158                                 hb_set_t                          *all_gids_to_retain,
159                                 hb_map_t                          *glyph_map, /* OUT */
160                                 hb_map_t                          *reverse_glyph_map, /* OUT */
161                                 unsigned int                      *num_glyphs /* OUT */)
162 {
163   hb_codepoint_t gid = HB_SET_VALUE_INVALID;
164   unsigned int length = 0;
165   for (unsigned int i = 0; all_gids_to_retain->next (&gid); i++) {
166     if (!retain_gids)
167     {
168       glyph_map->set (gid, i);
169       reverse_glyph_map->set (i, gid);
170     }
171     else
172     {
173       glyph_map->set (gid, gid);
174       reverse_glyph_map->set (gid, gid);
175     }
176     ++length;
177   }
178   if (!retain_gids || length == 0)
179   {
180     *num_glyphs = length;
181   }
182   else
183   {
184     *num_glyphs = face->get_num_glyphs ();
185   }
186 }
187
188 /**
189  * hb_subset_plan_create:
190  * Computes a plan for subsetting the supplied face according
191  * to a provided input. The plan describes
192  * which tables and glyphs should be retained.
193  *
194  * Return value: New subset plan.
195  *
196  * Since: 1.7.5
197  **/
198 hb_subset_plan_t *
199 hb_subset_plan_create (hb_face_t           *face,
200                        hb_subset_input_t   *input)
201 {
202   hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
203
204   plan->drop_hints = input->drop_hints;
205   plan->drop_layout = input->drop_layout;
206   plan->desubroutinize = input->desubroutinize;
207   plan->unicodes = hb_set_create();
208   plan->source = hb_face_reference (face);
209   plan->dest = hb_face_builder_create ();
210   plan->codepoint_to_glyph = hb_map_create();
211   plan->glyph_map = hb_map_create();
212   plan->reverse_glyph_map = hb_map_create();
213   plan->_glyphset = _populate_gids_to_retain (face,
214                                               input->unicodes,
215                                               input->glyphs,
216                                               !plan->drop_layout,
217                                               plan->unicodes,
218                                               plan->codepoint_to_glyph);
219
220   _create_old_gid_to_new_gid_map (face,
221                                   input->retain_gids,
222                                   plan->_glyphset,
223                                   plan->glyph_map,
224                                   plan->reverse_glyph_map,
225                                   &plan->_num_output_glyphs);
226
227   return plan;
228 }
229
230 /**
231  * hb_subset_plan_destroy:
232  *
233  * Since: 1.7.5
234  **/
235 void
236 hb_subset_plan_destroy (hb_subset_plan_t *plan)
237 {
238   if (!hb_object_destroy (plan)) return;
239
240   hb_set_destroy (plan->unicodes);
241   hb_face_destroy (plan->source);
242   hb_face_destroy (plan->dest);
243   hb_map_destroy (plan->codepoint_to_glyph);
244   hb_map_destroy (plan->glyph_map);
245   hb_map_destroy (plan->reverse_glyph_map);
246   hb_set_destroy (plan->_glyphset);
247
248   free (plan);
249 }