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-os2-table.hh"
43 #include "hb-ot-post-table.hh"
44 #include "hb-ot-cff1-table.hh"
45 #include "hb-ot-cff2-table.hh"
46 #include "hb-ot-vorg-table.hh"
47 #include "hb-ot-name-table.hh"
48 #include "hb-ot-color-cbdt-table.hh"
49 #include "hb-ot-layout-gsub-table.hh"
50 #include "hb-ot-layout-gpos-table.hh"
51 #include "hb-ot-var-gvar-table.hh"
52 #include "hb-ot-var-hvar-table.hh"
56 _plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
58 unsigned src_glyphs = plan->source->get_num_glyphs ();
59 unsigned dst_glyphs = plan->glyphset ()->get_population ();
61 if (unlikely (!src_glyphs))
62 return 512 + table_len;
64 return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
67 template<typename TableType>
69 _subset (hb_subset_plan_t *plan)
72 hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
73 const TableType *table = source_blob->as<TableType> ();
75 hb_tag_t tag = TableType::tableTag;
76 if (source_blob->data)
78 hb_vector_t<char> buf;
79 /* TODO Not all tables are glyph-related. 'name' table size for example should not be
80 * affected by number of glyphs. Accommodate that. */
81 unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
82 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
83 if (unlikely (!buf.alloc (buf_size)))
85 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
86 hb_blob_destroy (source_blob);
90 hb_serialize_context_t serializer ((void *) buf, buf_size);
91 serializer.start_serialize<TableType> ();
92 hb_subset_context_t c (source_blob, plan, &serializer, tag);
93 bool needed = table->subset (&c);
94 if (serializer.ran_out_of_room)
96 buf_size += (buf_size >> 1) + 32;
97 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);
98 if (unlikely (!buf.alloc (buf_size)))
100 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size);
101 hb_blob_destroy (source_blob);
106 serializer.end_serialize ();
108 result = !serializer.in_error ();
114 hb_blob_t *dest_blob = serializer.copy_blob ();
115 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length);
116 result = c.plan->add_table (tag, dest_blob);
117 hb_blob_destroy (dest_blob);
121 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
126 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
128 hb_blob_destroy (source_blob);
129 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
134 _is_table_present (hb_face_t *source, hb_tag_t tag)
136 hb_tag_t table_tags[32];
137 unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
138 while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
140 for (unsigned i = 0; i < num_tables; ++i)
141 if (table_tags[i] == tag)
143 offset += num_tables;
149 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
151 if (plan->drop_tables->has (tag))
156 case HB_TAG ('c','v','a','r'): /* hint table, fallthrough */
157 case HB_TAG ('c','v','t',' '): /* hint table, fallthrough */
158 case HB_TAG ('f','p','g','m'): /* hint table, fallthrough */
159 case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */
160 case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */
161 case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */
162 return plan->drop_hints;
164 #ifdef HB_NO_SUBSET_LAYOUT
165 // Drop Layout Tables if requested.
169 case HB_TAG ('m','o','r','x'):
170 case HB_TAG ('m','o','r','t'):
171 case HB_TAG ('k','e','r','x'):
172 case HB_TAG ('k','e','r','n'):
182 _subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
184 DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
187 case HB_OT_TAG_glyf: return _subset<const OT::glyf> (plan);
188 case HB_OT_TAG_hdmx: return _subset<const OT::hdmx> (plan);
189 case HB_OT_TAG_name: return _subset<const OT::name> (plan);
191 if (_is_table_present (plan->source, HB_OT_TAG_glyf) && !_should_drop_table (plan, HB_OT_TAG_glyf))
192 return true; /* skip head, handled by glyf */
193 return _subset<const OT::head> (plan);
194 case HB_OT_TAG_hhea: return true; /* skip hhea, handled by hmtx */
195 case HB_OT_TAG_hmtx: return _subset<const OT::hmtx> (plan);
196 case HB_OT_TAG_vhea: return true; /* skip vhea, handled by vmtx */
197 case HB_OT_TAG_vmtx: return _subset<const OT::vmtx> (plan);
198 case HB_OT_TAG_maxp: return _subset<const OT::maxp> (plan);
199 case HB_OT_TAG_sbix: return _subset<const OT::sbix> (plan);
200 case HB_OT_TAG_loca: return true; /* skip loca, handled by glyf */
201 case HB_OT_TAG_cmap: return _subset<const OT::cmap> (plan);
202 case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan);
203 case HB_OT_TAG_post: return _subset<const OT::post> (plan);
204 case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan);
205 case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan);
206 case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
208 #ifndef HB_NO_SUBSET_CFF
209 case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan);
210 case HB_OT_TAG_cff2: return _subset<const OT::cff2> (plan);
211 case HB_OT_TAG_VORG: return _subset<const OT::VORG> (plan);
214 #ifndef HB_NO_SUBSET_LAYOUT
215 case HB_OT_TAG_GDEF: return _subset<const OT::GDEF> (plan);
216 case HB_OT_TAG_GSUB: return _subset<const OT::GSUB> (plan);
217 case HB_OT_TAG_GPOS: return _subset<const OT::GPOS> (plan);
218 case HB_OT_TAG_gvar: return _subset<const OT::gvar> (plan);
219 case HB_OT_TAG_HVAR: return _subset<const OT::HVAR> (plan);
220 case HB_OT_TAG_VVAR: return _subset<const OT::VVAR> (plan);
224 hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
225 bool result = plan->add_table (tag, source_table);
226 hb_blob_destroy (source_table);
233 * @source: font face data to be subset.
234 * @input: input to use for the subsetting.
236 * Subsets a font according to provided input.
239 hb_subset (hb_face_t *source, hb_subset_input_t *input)
241 if (unlikely (!input || !source)) return hb_face_get_empty ();
243 hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
247 hb_tag_t table_tags[32];
248 unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
249 while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
251 for (unsigned i = 0; i < num_tables; ++i)
253 hb_tag_t tag = table_tags[i];
254 if (_should_drop_table (plan, tag) && !tags_set.has (tag)) continue;
256 success = _subset_table (plan, tag);
257 if (unlikely (!success)) goto end;
259 offset += num_tables;
263 hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
264 hb_subset_plan_destroy (plan);