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