ec2f8892fde1201f683a92fdfa09cf348605cdc1
[platform/upstream/harfbuzz.git] / src / hb-subset.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, Rod Sheeter, Behdad Esfahbod
25  */
26
27 #include "hb.hh"
28 #include "hb-open-type.hh"
29
30 #include "hb-subset.hh"
31
32 #include "hb-open-file.hh"
33 #include "hb-ot-cmap-table.hh"
34 #include "hb-ot-glyf-table.hh"
35 #include "hb-ot-hdmx-table.hh"
36 #include "hb-ot-head-table.hh"
37 #include "hb-ot-hhea-table.hh"
38 #include "hb-ot-hmtx-table.hh"
39 #include "hb-ot-maxp-table.hh"
40 #include "hb-ot-os2-table.hh"
41 #include "hb-ot-post-table.hh"
42 #include "hb-ot-cff1-table.hh"
43 #include "hb-ot-cff2-table.hh"
44 #include "hb-ot-vorg-table.hh"
45 #include "hb-ot-name-table.hh"
46 #include "hb-ot-layout-gsub-table.hh"
47 #include "hb-ot-layout-gpos-table.hh"
48
49
50 HB_UNUSED static inline unsigned int
51 _plan_estimate_subset_table_size (hb_subset_plan_t *plan,
52                                   unsigned int table_len);
53 static inline unsigned int
54 _plan_estimate_subset_table_size (hb_subset_plan_t *plan,
55                                   unsigned int table_len)
56 {
57   unsigned int src_glyphs = plan->source->get_num_glyphs ();
58   unsigned int dst_glyphs = plan->glyphset ()->get_population ();
59
60   if (unlikely (!src_glyphs))
61     return 512 + table_len;
62
63   return 512 + (unsigned int) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
64 }
65
66 template<typename TableType>
67 static bool
68 _subset2 (hb_subset_plan_t *plan)
69 {
70   bool result = false;
71   hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
72   const TableType *table = source_blob->as<TableType> ();
73
74   hb_tag_t tag = TableType::tableTag;
75   if (source_blob->data)
76   {
77     hb_vector_t<char> buf;
78     /* TODO Not all tables are glyph-related.  'name' table size for example should not be
79      * affected by number of glyphs.  Accommodate that. */
80     unsigned int buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
81     DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
82     if (unlikely (!buf.alloc (buf_size)))
83     {
84       DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
85       hb_blob_destroy (source_blob);
86       return false;
87     }
88   retry:
89     hb_serialize_context_t serializer ((void *) buf, buf_size);
90     serializer.start_serialize<TableType> ();
91     hb_subset_context_t c (plan, &serializer);
92     bool needed = table->subset (&c);
93     if (serializer.ran_out_of_room)
94     {
95       buf_size += (buf_size >> 1) + 32;
96       DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);
97       if (unlikely (!buf.alloc (buf_size)))
98       {
99         DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size);
100         hb_blob_destroy (source_blob);
101         return false;
102       }
103       goto retry;
104     }
105     serializer.end_serialize ();
106
107     result = !serializer.in_error ();
108
109     if (result)
110     {
111       if (needed)
112       {
113         hb_blob_t *dest_blob = serializer.copy_blob ();
114         DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length);
115         result = c.plan->add_table (tag, dest_blob);
116         hb_blob_destroy (dest_blob);
117       }
118       else
119       {
120         DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
121       }
122     }
123   }
124   else
125     DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
126
127   hb_blob_destroy (source_blob);
128   DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
129   return result;
130 }
131
132 template<typename TableType>
133 static bool
134 _subset (hb_subset_plan_t *plan)
135 {
136   hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
137   const TableType *table = source_blob->as<TableType> ();
138
139   hb_tag_t tag = TableType::tableTag;
140   hb_bool_t result = false;
141   if (source_blob->data)
142     result = table->subset (plan);
143   else
144     DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
145
146   hb_blob_destroy (source_blob);
147   DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
148   return result;
149 }
150
151
152 static bool
153 _subset_table (hb_subset_plan_t *plan,
154                hb_tag_t          tag)
155 {
156   DEBUG_MSG(SUBSET, nullptr, "begin subset %c%c%c%c", HB_UNTAG (tag));
157   bool result = true;
158   switch (tag) {
159     case HB_OT_TAG_glyf:
160       result = _subset2<const OT::glyf> (plan);
161       break;
162     case HB_OT_TAG_hdmx:
163       result = _subset2<const OT::hdmx> (plan);
164       break;
165     case HB_OT_TAG_name:
166       result = _subset2<const OT::name> (plan);
167       break;
168     case HB_OT_TAG_head:
169       // TODO that won't work well if there is no glyf
170       DEBUG_MSG(SUBSET, nullptr, "skip head, handled by glyf");
171       result = true;
172       break;
173     case HB_OT_TAG_hhea:
174       DEBUG_MSG(SUBSET, nullptr, "skip hhea handled by hmtx");
175       return true;
176     case HB_OT_TAG_hmtx:
177       result = _subset2<const OT::hmtx> (plan);
178       break;
179     case HB_OT_TAG_vhea:
180       DEBUG_MSG(SUBSET, nullptr, "skip vhea handled by vmtx");
181       return true;
182     case HB_OT_TAG_vmtx:
183       result = _subset2<const OT::vmtx> (plan);
184       break;
185     case HB_OT_TAG_maxp:
186       result = _subset2<const OT::maxp> (plan);
187       break;
188     case HB_OT_TAG_loca:
189       DEBUG_MSG(SUBSET, nullptr, "skip loca handled by glyf");
190       return true;
191     case HB_OT_TAG_cmap:
192       result = _subset2<const OT::cmap> (plan);
193       break;
194     case HB_OT_TAG_OS2:
195       result = _subset2<const OT::OS2> (plan);
196       break;
197     case HB_OT_TAG_post:
198       result = _subset2<const OT::post> (plan);
199       break;
200
201 #ifndef HB_NO_SUBSET_CFF
202     case HB_OT_TAG_cff1:
203       result = _subset<const OT::cff1> (plan);
204       break;
205     case HB_OT_TAG_cff2:
206       result = _subset<const OT::cff2> (plan);
207       break;
208     case HB_OT_TAG_VORG:
209       result = _subset2<const OT::VORG> (plan);
210       break;
211 #endif
212
213 #ifndef HB_NO_SUBSET_LAYOUT
214     case HB_OT_TAG_GDEF:
215       result = _subset2<const OT::GDEF> (plan);
216       break;
217     case HB_OT_TAG_GSUB:
218       result = _subset2<const OT::GSUB> (plan);
219       break;
220     case HB_OT_TAG_GPOS:
221       result = _subset2<const OT::GPOS> (plan);
222       break;
223 #endif
224
225     default:
226       hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
227       if (likely (source_table))
228         result = plan->add_table (tag, source_table);
229       else
230         result = false;
231       hb_blob_destroy (source_table);
232       break;
233   }
234   DEBUG_MSG(SUBSET, nullptr, "subset %c%c%c%c %s", HB_UNTAG (tag), result ? "ok" : "FAILED");
235   return result;
236 }
237
238 static bool
239 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
240 {
241   if (plan->drop_tables->has (tag))
242     return true;
243
244   switch (tag) {
245     case HB_TAG ('c', 'v', 'a', 'r'): /* hint table, fallthrough */
246     case HB_TAG ('c', 'v', 't', ' '): /* hint table, fallthrough */
247     case HB_TAG ('f', 'p', 'g', 'm'): /* hint table, fallthrough */
248     case HB_TAG ('p', 'r', 'e', 'p'): /* hint table, fallthrough */
249     case HB_TAG ('h', 'd', 'm', 'x'): /* hint table, fallthrough */
250     case HB_TAG ('V', 'D', 'M', 'X'): /* hint table, fallthrough */
251       return plan->drop_hints;
252
253 #ifdef HB_NO_SUBSET_LAYOUT
254     // Drop Layout Tables if requested.
255     case HB_OT_TAG_GDEF:
256     case HB_OT_TAG_GPOS:
257     case HB_OT_TAG_GSUB:
258     case HB_TAG ('m', 'o', 'r', 'x'):
259     case HB_TAG ('m', 'o', 'r', 't'):
260     case HB_TAG ('k', 'e', 'r', 'x'):
261     case HB_TAG ('k', 'e', 'r', 'n'):
262       return true;
263 #endif
264
265     default:
266       return false;
267   }
268 }
269
270 /**
271  * hb_subset:
272  * @source: font face data to be subset.
273  * @input: input to use for the subsetting.
274  *
275  * Subsets a font according to provided input.
276  **/
277 hb_face_t *
278 hb_subset (hb_face_t *source,
279            hb_subset_input_t *input)
280 {
281   if (unlikely (!input || !source)) return hb_face_get_empty ();
282
283   hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
284
285   hb_tag_t table_tags[32];
286   unsigned int offset = 0, count;
287   bool success = true;
288   hb_set_t tags_set;
289   do {
290     count = ARRAY_LENGTH (table_tags);
291     hb_face_get_table_tags (source, offset, &count, table_tags);
292     for (unsigned int i = 0; i < count; i++)
293     {
294       hb_tag_t tag = table_tags[i];
295       if (_should_drop_table (plan, tag) && !tags_set.has (tag))
296       {
297         DEBUG_MSG(SUBSET, nullptr, "drop %c%c%c%c", HB_UNTAG (tag));
298         continue;
299       }
300       tags_set.add (tag);
301       success = success && _subset_table (plan, tag);
302     }
303     offset += count;
304   } while (success && count == ARRAY_LENGTH (table_tags));
305
306   hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
307   hb_subset_plan_destroy (plan);
308   return result;
309 }