2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Garret Rieger, Rod Sheeter, Behdad Esfahbod
28 #include "hb-open-type.hh"
30 #include "hb-subset.hh"
32 #include "hb-open-file.hh"
33 #include "hb-ot-cmap-table.hh"
34 #include "hb-ot-glyf-table.hh"
35 #include "hb-ot-hdmx-table.hh"
36 #include "hb-ot-head-table.hh"
37 #include "hb-ot-hhea-table.hh"
38 #include "hb-ot-hmtx-table.hh"
39 #include "hb-ot-maxp-table.hh"
40 #include "hb-ot-color-sbix-table.hh"
41 #include "hb-ot-color-colr-table.hh"
42 #include "hb-ot-color-cpal-table.hh"
43 #include "hb-ot-os2-table.hh"
44 #include "hb-ot-post-table.hh"
45 #include "hb-ot-post-table-v2subset.hh"
46 #include "hb-ot-cff1-table.hh"
47 #include "hb-ot-cff2-table.hh"
48 #include "hb-ot-vorg-table.hh"
49 #include "hb-ot-name-table.hh"
50 #include "hb-ot-color-cbdt-table.hh"
51 #include "hb-ot-layout-gsub-table.hh"
52 #include "hb-ot-layout-gpos-table.hh"
53 #include "hb-ot-var-gvar-table.hh"
54 #include "hb-ot-var-hvar-table.hh"
55 #include "hb-ot-math-table.hh"
56 #include "hb-repacker.hh"
61 * @short_description: Subsets font files.
62 * @include: hb-subset.h
64 * Subsetting reduces the codepoint coverage of font files and removes all data
65 * that is no longer needed. A subset input describes the desired subset. The input is
66 * provided along with a font to the subsetting operation. Output is a new font file
67 * containing only the data specified in the input.
69 * Currently most outline and bitmap tables are supported: glyf, CFF, CFF2, sbix,
70 * COLR, and CBDT/CBLC. This also includes fonts with variable outlines via OpenType
71 * variations. Notably EBDT/EBLC and SVG are not supported. Layout subsetting is supported
72 * only for OpenType Layout tables (GSUB, GPOS, GDEF). Notably subsetting of graphite or AAT tables
73 * is not yet supported.
75 * Fonts with graphite or AAT tables may still be subsetted but will likely need to use the
76 * retain glyph ids option and configure the subset to pass through the layout tables untouched.
80 _plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
82 unsigned src_glyphs = plan->source->get_num_glyphs ();
83 unsigned dst_glyphs = plan->glyphset ()->get_population ();
85 if (unlikely (!src_glyphs))
86 return 512 + table_len;
88 return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
92 * Repack the serialization buffer if any offset overflows exist.
95 _repack (hb_tag_t tag, const hb_serialize_context_t& c)
97 if (tag != HB_OT_TAG_GPOS
98 && tag != HB_OT_TAG_GSUB)
100 // Check for overflow in a non-handled table.
101 return c.successful () ? c.copy_blob () : nullptr;
104 if (!c.offset_overflow ())
105 return c.copy_blob ();
107 hb_blob_t* result = hb_resolve_overflows (c.object_graph (), tag);
109 if (unlikely (!result))
111 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c offset overflow resolution failed.",
119 template<typename TableType>
122 _try_subset (const TableType *table,
123 hb_vector_t<char>* buf,
125 hb_subset_context_t* c /* OUT */)
127 c->serializer->start_serialize<TableType> ();
128 if (c->serializer->in_error ()) return false;
130 bool needed = table->subset (c);
131 if (!c->serializer->ran_out_of_room ())
133 c->serializer->end_serialize ();
137 buf_size += (buf_size >> 1) + 32;
138 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.",
139 HB_UNTAG (c->table_tag), buf_size);
141 if (unlikely (!buf->alloc (buf_size)))
143 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.",
144 HB_UNTAG (c->table_tag), buf_size);
148 c->serializer->reset (buf->arrayZ, buf_size);
149 return _try_subset (table, buf, buf_size, c);
152 template<typename TableType>
154 _subset (hb_subset_plan_t *plan)
156 hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
157 const TableType *table = source_blob->as<TableType> ();
159 hb_tag_t tag = TableType::tableTag;
160 if (!source_blob->data)
162 DEBUG_MSG (SUBSET, nullptr,
163 "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
164 hb_blob_destroy (source_blob);
168 hb_vector_t<char> buf;
169 /* TODO Not all tables are glyph-related. 'name' table size for example should not be
170 * affected by number of glyphs. Accommodate that. */
171 unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
172 DEBUG_MSG (SUBSET, nullptr,
173 "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
174 if (unlikely (!buf.alloc (buf_size)))
176 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
177 hb_blob_destroy (source_blob);
182 hb_serialize_context_t serializer (buf.arrayZ, buf_size);
184 hb_subset_context_t c (source_blob, plan, &serializer, tag);
185 needed = _try_subset (table, &buf, buf_size, &c);
187 hb_blob_destroy (source_blob);
189 if (serializer.in_error () && !serializer.only_offset_overflow ())
191 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset FAILED!", HB_UNTAG (tag));
197 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
202 hb_blob_t *dest_blob = _repack (tag, serializer);
205 DEBUG_MSG (SUBSET, nullptr,
206 "OT::%c%c%c%c final subset table size: %u bytes.",
207 HB_UNTAG (tag), dest_blob->length);
208 result = plan->add_table (tag, dest_blob);
209 hb_blob_destroy (dest_blob);
212 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s",
213 HB_UNTAG (tag), result ? "success" : "FAILED!");
218 _is_table_present (hb_face_t *source, hb_tag_t tag)
220 hb_tag_t table_tags[32];
221 unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
222 while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
224 for (unsigned i = 0; i < num_tables; ++i)
225 if (table_tags[i] == tag)
227 offset += num_tables;
233 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
235 if (plan->drop_tables->has (tag))
240 case HB_TAG ('c','v','a','r'): /* hint table, fallthrough */
241 case HB_TAG ('c','v','t',' '): /* hint table, fallthrough */
242 case HB_TAG ('f','p','g','m'): /* hint table, fallthrough */
243 case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */
244 case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */
245 case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */
246 return plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
248 #ifdef HB_NO_SUBSET_LAYOUT
249 // Drop Layout Tables if requested.
253 case HB_TAG ('m','o','r','x'):
254 case HB_TAG ('m','o','r','t'):
255 case HB_TAG ('k','e','r','x'):
256 case HB_TAG ('k','e','r','n'):
266 _passthrough (hb_subset_plan_t *plan, hb_tag_t tag)
268 hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
269 bool result = plan->add_table (tag, source_table);
270 hb_blob_destroy (source_table);
275 _subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
277 if (plan->no_subset_tables->has (tag)) {
278 return _passthrough (plan, tag);
281 DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
284 case HB_OT_TAG_glyf: return _subset<const OT::glyf> (plan);
285 case HB_OT_TAG_hdmx: return _subset<const OT::hdmx> (plan);
286 case HB_OT_TAG_name: return _subset<const OT::name> (plan);
288 if (_is_table_present (plan->source, HB_OT_TAG_glyf) && !_should_drop_table (plan, HB_OT_TAG_glyf))
289 return true; /* skip head, handled by glyf */
290 return _subset<const OT::head> (plan);
291 case HB_OT_TAG_hhea: return true; /* skip hhea, handled by hmtx */
292 case HB_OT_TAG_hmtx: return _subset<const OT::hmtx> (plan);
293 case HB_OT_TAG_vhea: return true; /* skip vhea, handled by vmtx */
294 case HB_OT_TAG_vmtx: return _subset<const OT::vmtx> (plan);
295 case HB_OT_TAG_maxp: return _subset<const OT::maxp> (plan);
296 case HB_OT_TAG_sbix: return _subset<const OT::sbix> (plan);
297 case HB_OT_TAG_loca: return true; /* skip loca, handled by glyf */
298 case HB_OT_TAG_cmap: return _subset<const OT::cmap> (plan);
299 case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan);
300 case HB_OT_TAG_post: return _subset<const OT::post> (plan);
301 case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan);
302 case HB_OT_TAG_CPAL: return _subset<const OT::CPAL> (plan);
303 case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan);
304 case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
305 case HB_OT_TAG_MATH: return _subset<const OT::MATH> (plan);
307 #ifndef HB_NO_SUBSET_CFF
308 case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan);
309 case HB_OT_TAG_cff2: return _subset<const OT::cff2> (plan);
310 case HB_OT_TAG_VORG: return _subset<const OT::VORG> (plan);
313 #ifndef HB_NO_SUBSET_LAYOUT
314 case HB_OT_TAG_GDEF: return _subset<const OT::GDEF> (plan);
315 case HB_OT_TAG_GSUB: return _subset<const OT::GSUB> (plan);
316 case HB_OT_TAG_GPOS: return _subset<const OT::GPOS> (plan);
317 case HB_OT_TAG_gvar: return _subset<const OT::gvar> (plan);
318 case HB_OT_TAG_HVAR: return _subset<const OT::HVAR> (plan);
319 case HB_OT_TAG_VVAR: return _subset<const OT::VVAR> (plan);
323 if (plan->flags & HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED)
324 return _passthrough (plan, tag);
333 * @source: font face data to be subset.
334 * @input: input to use for the subsetting.
336 * Subsets a font according to provided input. Returns nullptr
337 * if the subset operation fails.
342 hb_subset_or_fail (hb_face_t *source, const hb_subset_input_t *input)
344 if (unlikely (!input || !source)) return hb_face_get_empty ();
346 hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
347 if (unlikely (plan->in_error ())) {
348 hb_subset_plan_destroy (plan);
354 hb_tag_t table_tags[32];
355 unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
356 while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
358 for (unsigned i = 0; i < num_tables; ++i)
360 hb_tag_t tag = table_tags[i];
361 if (_should_drop_table (plan, tag) && !tags_set.has (tag)) continue;
363 success = _subset_table (plan, tag);
364 if (unlikely (!success)) goto end;
366 offset += num_tables;
370 hb_face_t *result = success ? hb_face_reference (plan->dest) : nullptr;
372 hb_subset_plan_destroy (plan);