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