• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-fvar-table.hh"
54 #include "hb-ot-var-gvar-table.hh"
55 #include "hb-ot-var-hvar-table.hh"
56 #include "hb-ot-math-table.hh"
57 #include "hb-ot-stat-table.hh"
58 #include "hb-repacker.hh"
59 #include "hb-subset-accelerator.hh"
60 
61 using OT::Layout::GSUB;
62 using OT::Layout::GPOS;
63 
64 /**
65  * SECTION:hb-subset
66  * @title: hb-subset
67  * @short_description: Subsets font files.
68  * @include: hb-subset.h
69  *
70  * Subsetting reduces the codepoint coverage of font files and removes all data
71  * that is no longer needed. A subset input describes the desired subset. The input is
72  * provided along with a font to the subsetting operation. Output is a new font file
73  * containing only the data specified in the input.
74  *
75  * Currently most outline and bitmap tables are supported: glyf, CFF, CFF2, sbix,
76  * COLR, and CBDT/CBLC. This also includes fonts with variable outlines via OpenType
77  * variations. Notably EBDT/EBLC and SVG are not supported. Layout subsetting is supported
78  * only for OpenType Layout tables (GSUB, GPOS, GDEF). Notably subsetting of graphite or AAT tables
79  * is not yet supported.
80  *
81  * Fonts with graphite or AAT tables may still be subsetted but will likely need to use the
82  * retain glyph ids option and configure the subset to pass through the layout tables untouched.
83  */
84 
85 
86 hb_user_data_key_t _hb_subset_accelerator_user_data_key = {};
87 
88 
89 /*
90  * The list of tables in the open type spec. Used to check for tables that may need handling
91  * if we are unable to list the tables in a face.
92  */
93 static hb_tag_t known_tables[] {
94   HB_TAG ('a', 'v', 'a', 'r'),
95   HB_OT_TAG_BASE,
96   HB_OT_TAG_CBDT,
97   HB_OT_TAG_CBLC,
98   HB_OT_TAG_cff1,
99   HB_OT_TAG_cff2,
100   HB_OT_TAG_cmap,
101   HB_OT_TAG_COLR,
102   HB_OT_TAG_CPAL,
103   HB_TAG ('c', 'v', 'a', 'r'),
104   HB_TAG ('c', 'v', 't', ' '),
105   HB_TAG ('D', 'S', 'I', 'G'),
106   HB_TAG ('E', 'B', 'D', 'T'),
107   HB_TAG ('E', 'B', 'L', 'C'),
108   HB_TAG ('E', 'B', 'S', 'C'),
109   HB_TAG ('f', 'p', 'g', 'm'),
110   HB_TAG ('f', 'v', 'a', 'r'),
111   HB_TAG ('g', 'a', 's', 'p'),
112   HB_OT_TAG_GDEF,
113   HB_OT_TAG_glyf,
114   HB_OT_TAG_GPOS,
115   HB_OT_TAG_GSUB,
116   HB_OT_TAG_gvar,
117   HB_OT_TAG_hdmx,
118   HB_OT_TAG_head,
119   HB_OT_TAG_hhea,
120   HB_OT_TAG_hmtx,
121   HB_OT_TAG_HVAR,
122   HB_OT_TAG_JSTF,
123   HB_TAG ('k', 'e', 'r', 'n'),
124   HB_OT_TAG_loca,
125   HB_TAG ('L', 'T', 'S', 'H'),
126   HB_OT_TAG_MATH,
127   HB_OT_TAG_maxp,
128   HB_TAG ('M', 'E', 'R', 'G'),
129   HB_TAG ('m', 'e', 't', 'a'),
130   HB_TAG ('M', 'V', 'A', 'R'),
131   HB_TAG ('P', 'C', 'L', 'T'),
132   HB_OT_TAG_post,
133   HB_TAG ('p', 'r', 'e', 'p'),
134   HB_OT_TAG_sbix,
135   HB_TAG ('S', 'T', 'A', 'T'),
136   HB_TAG ('S', 'V', 'G', ' '),
137   HB_TAG ('V', 'D', 'M', 'X'),
138   HB_OT_TAG_vhea,
139   HB_OT_TAG_vmtx,
140   HB_OT_TAG_VORG,
141   HB_OT_TAG_VVAR,
142   HB_OT_TAG_name,
143   HB_OT_TAG_OS2
144 };
145 
_table_is_empty(const hb_face_t * face,hb_tag_t tag)146 static bool _table_is_empty (const hb_face_t *face, hb_tag_t tag)
147 {
148   hb_blob_t* blob = hb_face_reference_table (face, tag);
149   bool result = (blob == hb_blob_get_empty ());
150   hb_blob_destroy (blob);
151   return result;
152 }
153 
154 static unsigned int
_get_table_tags(const hb_subset_plan_t * plan,unsigned int start_offset,unsigned int * table_count,hb_tag_t * table_tags)155 _get_table_tags (const hb_subset_plan_t* plan,
156                  unsigned int  start_offset,
157                  unsigned int *table_count, /* IN/OUT */
158                  hb_tag_t     *table_tags /* OUT */)
159 {
160   unsigned num_tables = hb_face_get_table_tags (plan->source, 0, nullptr, nullptr);
161   if (num_tables)
162     return hb_face_get_table_tags (plan->source, start_offset, table_count, table_tags);
163 
164   // If face has 0 tables associated with it, assume that it was built from
165   // hb_face_create_tables and thus is unable to list its tables. Fallback to
166   // checking each table type we can handle for existence instead.
167   auto it =
168       hb_concat (
169           + hb_array (known_tables)
170           | hb_filter ([&] (hb_tag_t tag) {
171             return !_table_is_empty (plan->source, tag) && !plan->no_subset_tables->has (tag);
172           })
173           | hb_map ([] (hb_tag_t tag) -> hb_tag_t { return tag; }),
174 
175           plan->no_subset_tables->iter ()
176           | hb_filter([&] (hb_tag_t tag) {
177             return !_table_is_empty (plan->source, tag);
178           }));
179 
180   it += start_offset;
181 
182   unsigned num_written = 0;
183   while (bool (it) && num_written < *table_count)
184     table_tags[num_written++] = *it++;
185 
186   *table_count = num_written;
187   return num_written;
188 }
189 
190 
191 static unsigned
_plan_estimate_subset_table_size(hb_subset_plan_t * plan,unsigned table_len,bool same_size)192 _plan_estimate_subset_table_size (hb_subset_plan_t *plan,
193 				  unsigned table_len,
194 				  bool same_size)
195 {
196   unsigned src_glyphs = plan->source->get_num_glyphs ();
197   unsigned dst_glyphs = plan->glyphset ()->get_population ();
198 
199   if (unlikely (!src_glyphs) || same_size)
200     return 512 + table_len;
201 
202   return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
203 }
204 
205 /*
206  * Repack the serialization buffer if any offset overflows exist.
207  */
208 static hb_blob_t*
_repack(hb_tag_t tag,const hb_serialize_context_t & c)209 _repack (hb_tag_t tag, const hb_serialize_context_t& c)
210 {
211   if (tag != HB_OT_TAG_GPOS
212       &&  tag != HB_OT_TAG_GSUB)
213   {
214     // Check for overflow in a non-handled table.
215     return c.successful () ? c.copy_blob () : nullptr;
216   }
217 
218   if (!c.offset_overflow ())
219     return c.copy_blob ();
220 
221   hb_blob_t* result = hb_resolve_overflows (c.object_graph (), tag);
222 
223   if (unlikely (!result))
224   {
225     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c offset overflow resolution failed.",
226                HB_UNTAG (tag));
227     return nullptr;
228   }
229 
230   return result;
231 }
232 
233 template<typename TableType>
234 static
235 bool
_try_subset(const TableType * table,hb_vector_t<char> * buf,hb_subset_context_t * c)236 _try_subset (const TableType *table,
237              hb_vector_t<char>* buf,
238              hb_subset_context_t* c /* OUT */)
239 {
240   c->serializer->start_serialize<TableType> ();
241   if (c->serializer->in_error ()) return false;
242 
243   bool needed = table->subset (c);
244   if (!c->serializer->ran_out_of_room ())
245   {
246     c->serializer->end_serialize ();
247     return needed;
248   }
249 
250   unsigned buf_size = buf->allocated;
251   buf_size = buf_size * 2 + 16;
252 
253 
254 
255 
256   DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.",
257              HB_UNTAG (c->table_tag), buf_size);
258 
259   if (unlikely (buf_size > c->source_blob->length * 16 ||
260 		!buf->alloc (buf_size)))
261   {
262     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.",
263                HB_UNTAG (c->table_tag), buf_size);
264     return needed;
265   }
266 
267   c->serializer->reset (buf->arrayZ, buf->allocated);
268   return _try_subset (table, buf, c);
269 }
270 
271 template<typename TableType>
272 static bool
_subset(hb_subset_plan_t * plan,hb_vector_t<char> & buf)273 _subset (hb_subset_plan_t *plan, hb_vector_t<char> &buf)
274 {
275   hb_blob_ptr_t<TableType> source_blob = plan->source_table<TableType> ();
276   const TableType *table = source_blob.get ();
277 
278   hb_tag_t tag = TableType::tableTag;
279   if (!source_blob.get_blob()->data)
280   {
281     DEBUG_MSG (SUBSET, nullptr,
282                "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
283     source_blob.destroy ();
284     return false;
285   }
286 
287   /* Tables that we want to allocate same space as the source table. For GSUB/GPOS it's
288    * because those are expensive to subset, so giving them more room is fine. */
289   bool same_size_table = TableType::tableTag == HB_OT_TAG_GSUB ||
290 			 TableType::tableTag == HB_OT_TAG_GPOS ||
291 			 TableType::tableTag == HB_OT_TAG_name;
292 
293   unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob.get_length (), same_size_table);
294   DEBUG_MSG (SUBSET, nullptr,
295              "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
296   if (unlikely (!buf.alloc (buf_size)))
297   {
298     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
299     source_blob.destroy ();
300     return false;
301   }
302 
303   bool needed = false;
304   hb_serialize_context_t serializer (buf.arrayZ, buf.allocated);
305   {
306     hb_subset_context_t c (source_blob.get_blob (), plan, &serializer, tag);
307     needed = _try_subset (table, &buf, &c);
308   }
309   source_blob.destroy ();
310 
311   if (serializer.in_error () && !serializer.only_offset_overflow ())
312   {
313     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset FAILED!", HB_UNTAG (tag));
314     return false;
315   }
316 
317   if (!needed)
318   {
319     DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
320     return true;
321   }
322 
323   bool result = false;
324   hb_blob_t *dest_blob = _repack (tag, serializer);
325   if (dest_blob)
326   {
327     DEBUG_MSG (SUBSET, nullptr,
328                "OT::%c%c%c%c final subset table size: %u bytes.",
329                HB_UNTAG (tag), dest_blob->length);
330     result = plan->add_table (tag, dest_blob);
331     hb_blob_destroy (dest_blob);
332   }
333 
334   DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s",
335              HB_UNTAG (tag), result ? "success" : "FAILED!");
336   return result;
337 }
338 
339 static bool
_is_table_present(hb_face_t * source,hb_tag_t tag)340 _is_table_present (hb_face_t *source, hb_tag_t tag)
341 {
342 
343   if (!hb_face_get_table_tags (source, 0, nullptr, nullptr)) {
344     // If face has 0 tables associated with it, assume that it was built from
345     // hb_face_create_tables and thus is unable to list its tables. Fallback to
346     // checking if the blob associated with tag is empty.
347     return !_table_is_empty (source, tag);
348   }
349 
350   hb_tag_t table_tags[32];
351   unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
352   while (((void) hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
353   {
354     for (unsigned i = 0; i < num_tables; ++i)
355       if (table_tags[i] == tag)
356 	return true;
357     offset += num_tables;
358   }
359   return false;
360 }
361 
362 static bool
_should_drop_table(hb_subset_plan_t * plan,hb_tag_t tag)363 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
364 {
365   if (plan->drop_tables->has (tag))
366     return true;
367 
368   switch (tag)
369   {
370   case HB_TAG ('c','v','a','r'): /* hint table, fallthrough */
371     return plan->all_axes_pinned || (plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
372 
373   case HB_TAG ('c','v','t',' '): /* hint table, fallthrough */
374   case HB_TAG ('f','p','g','m'): /* hint table, fallthrough */
375   case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */
376   case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */
377   case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */
378     return plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
379 
380 #ifdef HB_NO_SUBSET_LAYOUT
381     // Drop Layout Tables if requested.
382   case HB_OT_TAG_GDEF:
383   case HB_OT_TAG_GPOS:
384   case HB_OT_TAG_GSUB:
385   case HB_TAG ('m','o','r','x'):
386   case HB_TAG ('m','o','r','t'):
387   case HB_TAG ('k','e','r','x'):
388   case HB_TAG ('k','e','r','n'):
389     return true;
390 #endif
391 
392   case HB_TAG ('a','v','a','r'):
393   case HB_TAG ('f','v','a','r'):
394   case HB_TAG ('g','v','a','r'):
395   case HB_OT_TAG_HVAR:
396   case HB_OT_TAG_VVAR:
397   case HB_TAG ('M','V','A','R'):
398     return plan->all_axes_pinned;
399 
400   default:
401     return false;
402   }
403 }
404 
405 static bool
_passthrough(hb_subset_plan_t * plan,hb_tag_t tag)406 _passthrough (hb_subset_plan_t *plan, hb_tag_t tag)
407 {
408   hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
409   bool result = plan->add_table (tag, source_table);
410   hb_blob_destroy (source_table);
411   return result;
412 }
413 
414 static bool
_dependencies_satisfied(hb_subset_plan_t * plan,hb_tag_t tag,const hb_set_t & subsetted_tags,const hb_set_t & pending_subset_tags)415 _dependencies_satisfied (hb_subset_plan_t *plan, hb_tag_t tag,
416                          const hb_set_t &subsetted_tags,
417                          const hb_set_t &pending_subset_tags)
418 {
419   switch (tag)
420   {
421   case HB_OT_TAG_hmtx:
422   case HB_OT_TAG_vmtx:
423     return plan->pinned_at_default || !pending_subset_tags.has (HB_OT_TAG_glyf);
424   default:
425     return true;
426   }
427 }
428 
429 static bool
_subset_table(hb_subset_plan_t * plan,hb_vector_t<char> & buf,hb_tag_t tag)430 _subset_table (hb_subset_plan_t *plan,
431 	       hb_vector_t<char> &buf,
432 	       hb_tag_t tag)
433 {
434   if (plan->no_subset_tables->has (tag)) {
435     return _passthrough (plan, tag);
436   }
437 
438   DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
439   switch (tag)
440   {
441   case HB_OT_TAG_glyf: return _subset<const OT::glyf> (plan, buf);
442   case HB_OT_TAG_hdmx: return _subset<const OT::hdmx> (plan, buf);
443   case HB_OT_TAG_name: return _subset<const OT::name> (plan, buf);
444   case HB_OT_TAG_head:
445     if (_is_table_present (plan->source, HB_OT_TAG_glyf) && !_should_drop_table (plan, HB_OT_TAG_glyf))
446       return true; /* skip head, handled by glyf */
447     return _subset<const OT::head> (plan, buf);
448   case HB_OT_TAG_hhea: return true; /* skip hhea, handled by hmtx */
449   case HB_OT_TAG_hmtx: return _subset<const OT::hmtx> (plan, buf);
450   case HB_OT_TAG_vhea: return true; /* skip vhea, handled by vmtx */
451   case HB_OT_TAG_vmtx: return _subset<const OT::vmtx> (plan, buf);
452   case HB_OT_TAG_maxp: return _subset<const OT::maxp> (plan, buf);
453   case HB_OT_TAG_sbix: return _subset<const OT::sbix> (plan, buf);
454   case HB_OT_TAG_loca: return true; /* skip loca, handled by glyf */
455   case HB_OT_TAG_cmap: return _subset<const OT::cmap> (plan, buf);
456   case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan, buf);
457   case HB_OT_TAG_post: return _subset<const OT::post> (plan, buf);
458   case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan, buf);
459   case HB_OT_TAG_CPAL: return _subset<const OT::CPAL> (plan, buf);
460   case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan, buf);
461   case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
462   case HB_OT_TAG_MATH: return _subset<const OT::MATH> (plan, buf);
463 
464 #ifndef HB_NO_SUBSET_CFF
465   case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan, buf);
466   case HB_OT_TAG_cff2: return _subset<const OT::cff2> (plan, buf);
467   case HB_OT_TAG_VORG: return _subset<const OT::VORG> (plan, buf);
468 #endif
469 
470 #ifndef HB_NO_SUBSET_LAYOUT
471   case HB_OT_TAG_GDEF: return _subset<const OT::GDEF> (plan, buf);
472   case HB_OT_TAG_GSUB: return _subset<const GSUB> (plan, buf);
473   case HB_OT_TAG_GPOS: return _subset<const GPOS> (plan, buf);
474   case HB_OT_TAG_gvar: return _subset<const OT::gvar> (plan, buf);
475   case HB_OT_TAG_HVAR: return _subset<const OT::HVAR> (plan, buf);
476   case HB_OT_TAG_VVAR: return _subset<const OT::VVAR> (plan, buf);
477 #endif
478   case HB_OT_TAG_fvar:
479     if (plan->user_axes_location->is_empty ()) return _passthrough (plan, tag);
480     return _subset<const OT::fvar> (plan, buf);
481   case HB_OT_TAG_STAT:
482     /*TODO(qxliu): change the condition as we support more complex
483      * instancing operation*/
484     if (plan->all_axes_pinned) return _subset<const OT::STAT> (plan, buf);
485     else return _passthrough (plan, tag);
486 
487   default:
488     if (plan->flags & HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED)
489       return _passthrough (plan, tag);
490 
491     // Drop table
492     return true;
493   }
494 }
495 
_attach_accelerator_data(hb_subset_plan_t * plan,hb_face_t * face)496 static void _attach_accelerator_data (hb_subset_plan_t* plan,
497                                       hb_face_t* face /* IN/OUT */)
498 {
499   if (!plan->inprogress_accelerator) return;
500 
501   // Transfer the accelerator from the plan to us.
502   hb_subset_accelerator_t* accel = plan->inprogress_accelerator;
503   plan->inprogress_accelerator = nullptr;
504 
505   if (accel->in_error ())
506   {
507     hb_subset_accelerator_t::destroy (accel);
508     return;
509   }
510 
511   // Populate caches that need access to the final tables.
512   hb_blob_ptr_t<OT::cmap> cmap_ptr (hb_sanitize_context_t ().reference_table<OT::cmap> (face));
513   accel->cmap_cache = OT::cmap::create_filled_cache (cmap_ptr);
514   accel->destroy_cmap_cache = OT::SubtableUnicodesCache::destroy;
515 
516   if (!hb_face_set_user_data(face,
517                              hb_subset_accelerator_t::user_data_key(),
518                              accel,
519                              hb_subset_accelerator_t::destroy,
520                              true))
521     hb_subset_accelerator_t::destroy (accel);
522 }
523 
524 /**
525  * hb_subset_or_fail:
526  * @source: font face data to be subset.
527  * @input: input to use for the subsetting.
528  *
529  * Subsets a font according to provided input. Returns nullptr
530  * if the subset operation fails.
531  *
532  * Since: 2.9.0
533  **/
534 hb_face_t *
hb_subset_or_fail(hb_face_t * source,const hb_subset_input_t * input)535 hb_subset_or_fail (hb_face_t *source, const hb_subset_input_t *input)
536 {
537   if (unlikely (!input || !source)) return hb_face_get_empty ();
538 
539   hb_subset_plan_t *plan = hb_subset_plan_create_or_fail (source, input);
540   if (unlikely (!plan)) {
541     return nullptr;
542   }
543 
544   hb_face_t * result = hb_subset_plan_execute_or_fail (plan);
545   hb_subset_plan_destroy (plan);
546   return result;
547 }
548 
549 
550 /**
551  * hb_subset_plan_execute_or_fail:
552  * @plan: a subsetting plan.
553  *
554  * Executes the provided subsetting @plan.
555  *
556  * Return value:
557  * on success returns a reference to generated font subset. If the subsetting operation fails
558  * returns nullptr.
559  *
560  * Since: 4.0.0
561  **/
562 hb_face_t *
hb_subset_plan_execute_or_fail(hb_subset_plan_t * plan)563 hb_subset_plan_execute_or_fail (hb_subset_plan_t *plan)
564 {
565   if (unlikely (!plan || plan->in_error ())) {
566     return nullptr;
567   }
568 
569   hb_tag_t table_tags[32];
570   unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
571 
572   hb_set_t subsetted_tags, pending_subset_tags;
573   while (((void) _get_table_tags (plan, offset, &num_tables, table_tags), num_tables))
574   {
575     for (unsigned i = 0; i < num_tables; ++i)
576     {
577       hb_tag_t tag = table_tags[i];
578       if (_should_drop_table (plan, tag)) continue;
579       pending_subset_tags.add (tag);
580     }
581 
582     offset += num_tables;
583   }
584 
585   hb_vector_t<char> buf;
586   buf.alloc (4096 - 16);
587 
588 
589   bool success = true;
590 
591   while (!pending_subset_tags.is_empty ())
592   {
593     if (subsetted_tags.in_error ()
594         || pending_subset_tags.in_error ()) {
595       success = false;
596       goto end;
597     }
598 
599     bool made_changes = false;
600     for (hb_tag_t tag : pending_subset_tags)
601     {
602       if (!_dependencies_satisfied (plan, tag,
603                                     subsetted_tags,
604                                     pending_subset_tags))
605       {
606         // delayed subsetting for some tables since they might have dependency on other tables
607         // in some cases: e.g: during instantiating glyf tables, hmetrics/vmetrics are updated
608         // and saved in subset plan, hmtx/vmtx subsetting need to use these updated metrics values
609         continue;
610       }
611 
612       pending_subset_tags.del (tag);
613       subsetted_tags.add (tag);
614       made_changes = true;
615 
616       success = _subset_table (plan, buf, tag);
617       if (unlikely (!success)) goto end;
618     }
619 
620     if (!made_changes)
621     {
622       DEBUG_MSG (SUBSET, nullptr, "Table dependencies unable to be satisfied. Subset failed.");
623       success = false;
624       goto end;
625     }
626   }
627 
628   if (success && plan->attach_accelerator_data) {
629     _attach_accelerator_data (plan, plan->dest);
630   }
631 
632 end:
633   return success ? hb_face_reference (plan->dest) : nullptr;
634 }
635