Imported Upstream version 1.7.6
[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-private.hh"
28
29 #include "hb-subset-plan.hh"
30 #include "hb-ot-cmap-table.hh"
31 #include "hb-ot-glyf-table.hh"
32
33 static int
34 _hb_codepoint_t_cmp (const void *pa, const void *pb)
35 {
36   hb_codepoint_t a = * (hb_codepoint_t *) pa;
37   hb_codepoint_t b = * (hb_codepoint_t *) pb;
38
39   return a < b ? -1 : a > b ? +1 : 0;
40 }
41
42 hb_bool_t
43 hb_subset_plan_new_gid_for_codepoint (hb_subset_plan_t *plan,
44                                       hb_codepoint_t codepoint,
45                                       hb_codepoint_t *new_gid)
46 {
47   // TODO actual map, delete this garbage.
48   for (unsigned int i = 0; i < plan->codepoints.len; i++)
49   {
50     if (plan->codepoints[i] != codepoint) continue;
51     if (!hb_subset_plan_new_gid_for_old_id(plan, plan->gids_to_retain[i], new_gid))
52     {
53       return false;
54     }
55     return true;
56   }
57   return false;
58 }
59
60 hb_bool_t
61 hb_subset_plan_new_gid_for_old_id (hb_subset_plan_t *plan,
62                                    hb_codepoint_t old_gid,
63                                    hb_codepoint_t *new_gid)
64 {
65   // the index in old_gids is the new gid; only up to codepoints.len are valid
66   for (unsigned int i = 0; i < plan->gids_to_retain_sorted.len; i++)
67   {
68     if (plan->gids_to_retain_sorted[i] == old_gid)
69     {
70       *new_gid = i;
71       return true;
72     }
73   }
74   return false;
75 }
76
77 hb_bool_t
78 hb_subset_plan_add_table (hb_subset_plan_t *plan,
79                           hb_tag_t tag,
80                           hb_blob_t *contents)
81 {
82   hb_blob_t *source_blob = plan->source->reference_table (tag);
83   DEBUG_MSG(SUBSET, nullptr, "add table %c%c%c%c, dest %d bytes, source %d bytes", HB_UNTAG(tag), hb_blob_get_length (contents), hb_blob_get_length (source_blob));
84   hb_blob_destroy (source_blob);
85   return hb_subset_face_add_table(plan->dest, tag, contents);
86 }
87
88 static void
89 _populate_codepoints (hb_set_t *input_codepoints,
90                       hb_prealloced_array_t<hb_codepoint_t>& plan_codepoints)
91 {
92   plan_codepoints.alloc (hb_set_get_population (input_codepoints));
93   hb_codepoint_t cp = -1;
94   while (hb_set_next (input_codepoints, &cp)) {
95     hb_codepoint_t *wr = plan_codepoints.push();
96     *wr = cp;
97   }
98   plan_codepoints.qsort (_hb_codepoint_t_cmp);
99 }
100
101 static void
102 _add_gid_and_children (const OT::glyf::accelerator_t &glyf,
103                        hb_codepoint_t gid,
104                        hb_set_t *gids_to_retain)
105 {
106   if (hb_set_has (gids_to_retain, gid))
107     // Already visited this gid, ignore.
108     return;
109
110   hb_set_add (gids_to_retain, gid);
111
112   OT::glyf::CompositeGlyphHeader::Iterator composite;
113   if (glyf.get_composite (gid, &composite))
114   {
115     do
116     {
117       _add_gid_and_children (glyf, (hb_codepoint_t) composite.current->glyphIndex, gids_to_retain);
118     } while (composite.move_to_next());
119   }
120 }
121
122 static void
123 _populate_gids_to_retain (hb_face_t *face,
124                           hb_prealloced_array_t<hb_codepoint_t>& codepoints,
125                           hb_prealloced_array_t<hb_codepoint_t>& old_gids,
126                           hb_prealloced_array_t<hb_codepoint_t>& old_gids_sorted)
127 {
128   OT::cmap::accelerator_t cmap;
129   OT::glyf::accelerator_t glyf;
130   cmap.init (face);
131   glyf.init (face);
132
133   hb_auto_array_t<unsigned int> bad_indices;
134
135   old_gids.alloc (codepoints.len);
136   for (unsigned int i = 0; i < codepoints.len; i++)
137   {
138     hb_codepoint_t gid;
139     if (!cmap.get_nominal_glyph (codepoints[i], &gid))
140     {
141       gid = -1;
142       *(bad_indices.push ()) = i;
143     }
144     *(old_gids.push ()) = gid;
145   }
146
147   /* Generally there shouldn't be any */
148   while (bad_indices.len > 0)
149   {
150     unsigned int i = bad_indices[bad_indices.len - 1];
151     bad_indices.pop ();
152     DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", codepoints[i]);
153     codepoints.remove (i);
154     old_gids.remove (i);
155   }
156
157   // Populate a full set of glyphs to retain by adding all referenced
158   // composite glyphs.
159   // TODO expand with glyphs reached by G*
160   hb_set_t * all_gids_to_retain = hb_set_create ();
161   _add_gid_and_children (glyf, 0, all_gids_to_retain);
162   for (unsigned int i = 0; i < old_gids.len; i++)
163     _add_gid_and_children (glyf, old_gids[i], all_gids_to_retain);
164
165   // Transfer to a sorted list.
166   old_gids_sorted.alloc (hb_set_get_population (all_gids_to_retain));
167   hb_codepoint_t gid = HB_SET_VALUE_INVALID;
168   while (hb_set_next (all_gids_to_retain, &gid))
169     *(old_gids_sorted.push ()) = gid;
170
171   hb_set_destroy (all_gids_to_retain);
172   glyf.fini ();
173   cmap.fini ();
174 }
175
176 /**
177  * hb_subset_plan_create:
178  * Computes a plan for subsetting the supplied face according
179  * to a provide profile and input. The plan describes
180  * which tables and glyphs should be retained.
181  *
182  * Return value: New subset plan.
183  *
184  * Since: 1.7.5
185  **/
186 hb_subset_plan_t *
187 hb_subset_plan_create (hb_face_t           *face,
188                        hb_subset_profile_t *profile,
189                        hb_subset_input_t   *input)
190 {
191   hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
192
193   plan->codepoints.init();
194   plan->gids_to_retain.init();
195   plan->gids_to_retain_sorted.init();
196   plan->source = hb_face_reference (face);
197   plan->dest = hb_subset_face_create ();
198   plan->drop_hints = input->drop_hints;
199
200   _populate_codepoints (input->unicodes, plan->codepoints);
201   _populate_gids_to_retain (face,
202                             plan->codepoints,
203                             plan->gids_to_retain,
204                             plan->gids_to_retain_sorted);
205
206   return plan;
207 }
208
209 /**
210  * hb_subset_plan_destroy:
211  *
212  * Since: 1.7.5
213  **/
214 void
215 hb_subset_plan_destroy (hb_subset_plan_t *plan)
216 {
217   if (!hb_object_destroy (plan)) return;
218
219   plan->codepoints.finish ();
220   plan->gids_to_retain.finish ();
221   plan->gids_to_retain_sorted.finish ();
222
223   hb_face_destroy (plan->source);
224   hb_face_destroy (plan->dest);
225
226   free (plan);
227 }