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