bb46e5b97f1a9c2a0a950102aac30aff7134dc78
[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-color-sbix-table.hh"
41 #include "hb-ot-color-colr-table.hh"
42 #include "hb-ot-color-cpal-table.hh"
43 #include "hb-ot-os2-table.hh"
44 #include "hb-ot-post-table.hh"
45 #include "hb-ot-post-table-v2subset.hh"
46 #include "hb-ot-cff1-table.hh"
47 #include "hb-ot-cff2-table.hh"
48 #include "hb-ot-vorg-table.hh"
49 #include "hb-ot-name-table.hh"
50 #include "hb-ot-color-cbdt-table.hh"
51 #include "hb-ot-layout-gsub-table.hh"
52 #include "hb-ot-layout-gpos-table.hh"
53 #include "hb-ot-var-gvar-table.hh"
54 #include "hb-ot-var-hvar-table.hh"
55 #include "hb-ot-math-table.hh"
56 #include "hb-repacker.hh"
57
58 /**
59  * SECTION:hb-subset
60  * @title: hb-subset
61  * @short_description: Subsets font files.
62  * @include: hb-subset.h
63  *
64  * Subsetting reduces the codepoint coverage of font files and removes all data
65  * that is no longer needed. A subset input describes the desired subset. The input is
66  * provided along with a font to the subsetting operation. Output is a new font file
67  * containing only the data specified in the input.
68  *
69  * Currently most outline and bitmap tables are supported: glyf, CFF, CFF2, sbix,
70  * COLR, and CBDT/CBLC. This also includes fonts with variable outlines via OpenType
71  * variations. Notably EBDT/EBLC and SVG are not supported. Layout subsetting is supported
72  * only for OpenType Layout tables (GSUB, GPOS, GDEF). Notably subsetting of graphite or AAT tables
73  * is not yet supported.
74  *
75  * Fonts with graphite or AAT tables may still be subsetted but will likely need to use the
76  * retain glyph ids option and configure the subset to pass through the layout tables untouched.
77  */
78
79 static unsigned
80 _plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
81 {
82   unsigned src_glyphs = plan->source->get_num_glyphs ();
83   unsigned dst_glyphs = plan->glyphset ()->get_population ();
84
85   if (unlikely (!src_glyphs))
86     return 512 + table_len;
87
88   return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
89 }
90
91 /*
92  * Repack the serialization buffer if any offset overflows exist.
93  */
94 static hb_blob_t*
95 _repack (hb_tag_t tag, const hb_serialize_context_t& c)
96 {
97   if (tag != HB_OT_TAG_GPOS
98       &&  tag != HB_OT_TAG_GSUB)
99   {
100     // Check for overflow in a non-handled table.
101     return c.successful () ? c.copy_blob () : nullptr;
102   }
103
104   if (!c.offset_overflow ())
105     return c.copy_blob ();
106
107   hb_blob_t* result = hb_resolve_overflows (c.object_graph (), tag);
108
109   if (unlikely (!result))
110   {
111     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c offset overflow resolution failed.",
112                HB_UNTAG (tag));
113     return nullptr;
114   }
115
116   return result;
117 }
118
119 template<typename TableType>
120 static
121 bool
122 _try_subset (const TableType *table,
123              hb_vector_t<char>* buf,
124              unsigned buf_size,
125              hb_subset_context_t* c /* OUT */)
126 {
127   c->serializer->start_serialize<TableType> ();
128   if (c->serializer->in_error ()) return false;
129
130   bool needed = table->subset (c);
131   if (!c->serializer->ran_out_of_room ())
132   {
133     c->serializer->end_serialize ();
134     return needed;
135   }
136
137   buf_size += (buf_size >> 1) + 32;
138   DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.",
139              HB_UNTAG (c->table_tag), buf_size);
140
141   if (unlikely (!buf->alloc (buf_size)))
142   {
143     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.",
144                HB_UNTAG (c->table_tag), buf_size);
145     return needed;
146   }
147
148   c->serializer->reset (buf->arrayZ, buf_size);
149   return _try_subset (table, buf, buf_size, c);
150 }
151
152 template<typename TableType>
153 static bool
154 _subset (hb_subset_plan_t *plan)
155 {
156   hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
157   const TableType *table = source_blob->as<TableType> ();
158
159   hb_tag_t tag = TableType::tableTag;
160   if (!source_blob->data)
161   {
162     DEBUG_MSG (SUBSET, nullptr,
163                "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
164     hb_blob_destroy (source_blob);
165     return false;
166   }
167
168   hb_vector_t<char> buf;
169   /* TODO Not all tables are glyph-related.  'name' table size for example should not be
170    * affected by number of glyphs.  Accommodate that. */
171   unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
172   DEBUG_MSG (SUBSET, nullptr,
173              "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
174   if (unlikely (!buf.alloc (buf_size)))
175   {
176     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
177     hb_blob_destroy (source_blob);
178     return false;
179   }
180
181   bool needed = false;
182   hb_serialize_context_t serializer (buf.arrayZ, buf_size);
183   {
184     hb_subset_context_t c (source_blob, plan, &serializer, tag);
185     needed = _try_subset (table, &buf, buf_size, &c);
186   }
187   hb_blob_destroy (source_blob);
188
189   if (serializer.in_error () && !serializer.only_offset_overflow ())
190   {
191     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset FAILED!", HB_UNTAG (tag));
192     return false;
193   }
194
195   if (!needed)
196   {
197     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
198     return true;
199   }
200
201   bool result = false;
202   hb_blob_t *dest_blob = _repack (tag, serializer);
203   if (dest_blob)
204   {
205     DEBUG_MSG (SUBSET, nullptr,
206                "OT::%c%c%c%c final subset table size: %u bytes.",
207                HB_UNTAG (tag), dest_blob->length);
208     result = plan->add_table (tag, dest_blob);
209     hb_blob_destroy (dest_blob);
210   }
211
212   DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s",
213              HB_UNTAG (tag), result ? "success" : "FAILED!");
214   return result;
215 }
216
217 static bool
218 _is_table_present (hb_face_t *source, hb_tag_t tag)
219 {
220   hb_tag_t table_tags[32];
221   unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
222   while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
223   {
224     for (unsigned i = 0; i < num_tables; ++i)
225       if (table_tags[i] == tag)
226         return true;
227     offset += num_tables;
228   }
229   return false;
230 }
231
232 static bool
233 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
234 {
235   if (plan->drop_tables->has (tag))
236     return true;
237
238   switch (tag)
239   {
240   case HB_TAG ('c','v','a','r'): /* hint table, fallthrough */
241   case HB_TAG ('c','v','t',' '): /* hint table, fallthrough */
242   case HB_TAG ('f','p','g','m'): /* hint table, fallthrough */
243   case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */
244   case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */
245   case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */
246     return plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
247
248 #ifdef HB_NO_SUBSET_LAYOUT
249     // Drop Layout Tables if requested.
250   case HB_OT_TAG_GDEF:
251   case HB_OT_TAG_GPOS:
252   case HB_OT_TAG_GSUB:
253   case HB_TAG ('m','o','r','x'):
254   case HB_TAG ('m','o','r','t'):
255   case HB_TAG ('k','e','r','x'):
256   case HB_TAG ('k','e','r','n'):
257     return true;
258 #endif
259
260   default:
261     return false;
262   }
263 }
264
265 static bool
266 _passthrough (hb_subset_plan_t *plan, hb_tag_t tag)
267 {
268   hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
269   bool result = plan->add_table (tag, source_table);
270   hb_blob_destroy (source_table);
271   return result;
272 }
273
274 static bool
275 _subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
276 {
277   if (plan->no_subset_tables->has (tag)) {
278     return _passthrough (plan, tag);
279   }
280
281   DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
282   switch (tag)
283   {
284   case HB_OT_TAG_glyf: return _subset<const OT::glyf> (plan);
285   case HB_OT_TAG_hdmx: return _subset<const OT::hdmx> (plan);
286   case HB_OT_TAG_name: return _subset<const OT::name> (plan);
287   case HB_OT_TAG_head:
288     if (_is_table_present (plan->source, HB_OT_TAG_glyf) && !_should_drop_table (plan, HB_OT_TAG_glyf))
289       return true; /* skip head, handled by glyf */
290     return _subset<const OT::head> (plan);
291   case HB_OT_TAG_hhea: return true; /* skip hhea, handled by hmtx */
292   case HB_OT_TAG_hmtx: return _subset<const OT::hmtx> (plan);
293   case HB_OT_TAG_vhea: return true; /* skip vhea, handled by vmtx */
294   case HB_OT_TAG_vmtx: return _subset<const OT::vmtx> (plan);
295   case HB_OT_TAG_maxp: return _subset<const OT::maxp> (plan);
296   case HB_OT_TAG_sbix: return _subset<const OT::sbix> (plan);
297   case HB_OT_TAG_loca: return true; /* skip loca, handled by glyf */
298   case HB_OT_TAG_cmap: return _subset<const OT::cmap> (plan);
299   case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan);
300   case HB_OT_TAG_post: return _subset<const OT::post> (plan);
301   case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan);
302   case HB_OT_TAG_CPAL: return _subset<const OT::CPAL> (plan);
303   case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan);
304   case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
305   case HB_OT_TAG_MATH: return _subset<const OT::MATH> (plan);
306
307 #ifndef HB_NO_SUBSET_CFF
308   case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan);
309   case HB_OT_TAG_cff2: return _subset<const OT::cff2> (plan);
310   case HB_OT_TAG_VORG: return _subset<const OT::VORG> (plan);
311 #endif
312
313 #ifndef HB_NO_SUBSET_LAYOUT
314   case HB_OT_TAG_GDEF: return _subset<const OT::GDEF> (plan);
315   case HB_OT_TAG_GSUB: return _subset<const OT::GSUB> (plan);
316   case HB_OT_TAG_GPOS: return _subset<const OT::GPOS> (plan);
317   case HB_OT_TAG_gvar: return _subset<const OT::gvar> (plan);
318   case HB_OT_TAG_HVAR: return _subset<const OT::HVAR> (plan);
319   case HB_OT_TAG_VVAR: return _subset<const OT::VVAR> (plan);
320 #endif
321
322   default:
323     if (plan->flags & HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED)
324       return _passthrough (plan, tag);
325
326     // Drop table
327     return true;
328   }
329 }
330
331 /**
332  * hb_subset_or_fail:
333  * @source: font face data to be subset.
334  * @input: input to use for the subsetting.
335  *
336  * Subsets a font according to provided input. Returns nullptr
337  * if the subset operation fails.
338  *
339  * Since: 2.9.0
340  **/
341 hb_face_t *
342 hb_subset_or_fail (hb_face_t *source, const hb_subset_input_t *input)
343 {
344   if (unlikely (!input || !source)) return hb_face_get_empty ();
345
346   hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
347   if (unlikely (plan->in_error ())) {
348     hb_subset_plan_destroy (plan);
349     return nullptr;
350   }
351
352   hb_set_t tags_set;
353   bool success = true;
354   hb_tag_t table_tags[32];
355   unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
356   while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
357   {
358     for (unsigned i = 0; i < num_tables; ++i)
359     {
360       hb_tag_t tag = table_tags[i];
361       if (_should_drop_table (plan, tag) && !tags_set.has (tag)) continue;
362       tags_set.add (tag);
363       success = _subset_table (plan, tag);
364       if (unlikely (!success)) goto end;
365     }
366     offset += num_tables;
367   }
368 end:
369
370   hb_face_t *result = success ? hb_face_reference (plan->dest) : nullptr;
371
372   hb_subset_plan_destroy (plan);
373   return result;
374 }