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 "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"
53 #include "hb-repacker.hh"
54
55
56 static unsigned
_plan_estimate_subset_table_size(hb_subset_plan_t * plan,unsigned table_len)57 _plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
58 {
59 unsigned src_glyphs = plan->source->get_num_glyphs ();
60 unsigned dst_glyphs = plan->glyphset ()->get_population ();
61
62 if (unlikely (!src_glyphs))
63 return 512 + table_len;
64
65 return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
66 }
67
68 /*
69 * Repack the serialization buffer if any offset overflows exist.
70 */
71 static hb_blob_t*
_repack(hb_tag_t tag,const hb_serialize_context_t & c)72 _repack (hb_tag_t tag, const hb_serialize_context_t& c)
73 {
74 if (tag != HB_OT_TAG_GPOS
75 && tag != HB_OT_TAG_GSUB)
76 {
77 // Check for overflow in a non-handled table.
78 return c.successful () ? c.copy_blob () : nullptr;
79 }
80
81 if (!c.offset_overflow ())
82 return c.copy_blob ();
83
84 hb_vector_t<char> buf;
85 int buf_size = c.end - c.start;
86 if (unlikely (!buf.alloc (buf_size)))
87 return nullptr;
88
89 hb_serialize_context_t repacked ((void *) buf, buf_size);
90 hb_resolve_overflows (c.object_graph (), &repacked);
91
92 if (unlikely (repacked.in_error ()))
93 // TODO(garretrieger): refactor so we can share the resize/retry logic with the subset
94 // portion.
95 return nullptr;
96
97 return repacked.copy_blob ();
98 }
99
100 template<typename TableType>
101 static
102 bool
_try_subset(const TableType * table,hb_vector_t<char> * buf,unsigned buf_size,hb_subset_context_t * c)103 _try_subset (const TableType *table,
104 hb_vector_t<char>* buf,
105 unsigned buf_size,
106 hb_subset_context_t* c /* OUT */)
107 {
108 c->serializer->start_serialize<TableType> ();
109
110 bool needed = table->subset (c);
111 if (!c->serializer->ran_out_of_room ())
112 {
113 c->serializer->end_serialize ();
114 return needed;
115 }
116
117 buf_size += (buf_size >> 1) + 32;
118 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.",
119 HB_UNTAG (c->table_tag), buf_size);
120
121 if (unlikely (!buf->alloc (buf_size)))
122 {
123 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.",
124 HB_UNTAG (c->table_tag), buf_size);
125 return needed;
126 }
127
128 c->serializer->reset (buf->arrayZ, buf_size);
129 return _try_subset (table, buf, buf_size, c);
130 }
131
132 template<typename TableType>
133 static bool
_subset(hb_subset_plan_t * plan)134 _subset (hb_subset_plan_t *plan)
135 {
136 hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
137 const TableType *table = source_blob->as<TableType> ();
138
139 hb_tag_t tag = TableType::tableTag;
140 if (!source_blob->data)
141 {
142 DEBUG_MSG (SUBSET, nullptr,
143 "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
144 hb_blob_destroy (source_blob);
145 return false;
146 }
147
148 hb_vector_t<char> buf;
149 /* TODO Not all tables are glyph-related. 'name' table size for example should not be
150 * affected by number of glyphs. Accommodate that. */
151 unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
152 DEBUG_MSG (SUBSET, nullptr,
153 "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
154 if (unlikely (!buf.alloc (buf_size)))
155 {
156 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
157 hb_blob_destroy (source_blob);
158 return false;
159 }
160
161 bool needed = false;
162 hb_serialize_context_t serializer (buf.arrayZ, buf_size);
163 {
164 hb_subset_context_t c (source_blob, plan, &serializer, tag);
165 needed = _try_subset (table, &buf, buf_size, &c);
166 }
167 hb_blob_destroy (source_blob);
168
169 if (serializer.in_error () && !serializer.only_offset_overflow ())
170 {
171 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset FAILED!", HB_UNTAG (tag));
172 return false;
173 }
174
175 if (!needed)
176 {
177 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
178 return true;
179 }
180
181 bool result = false;
182 hb_blob_t *dest_blob = _repack (tag, serializer);
183 if (dest_blob)
184 {
185 DEBUG_MSG (SUBSET, nullptr,
186 "OT::%c%c%c%c final subset table size: %u bytes.",
187 HB_UNTAG (tag), dest_blob->length);
188 result = plan->add_table (tag, dest_blob);
189 hb_blob_destroy (dest_blob);
190 }
191
192 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s",
193 HB_UNTAG (tag), result ? "success" : "FAILED!");
194 return result;
195 }
196
197 static bool
_is_table_present(hb_face_t * source,hb_tag_t tag)198 _is_table_present (hb_face_t *source, hb_tag_t tag)
199 {
200 hb_tag_t table_tags[32];
201 unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
202 while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
203 {
204 for (unsigned i = 0; i < num_tables; ++i)
205 if (table_tags[i] == tag)
206 return true;
207 offset += num_tables;
208 }
209 return false;
210 }
211
212 static bool
_should_drop_table(hb_subset_plan_t * plan,hb_tag_t tag)213 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
214 {
215 if (plan->drop_tables->has (tag))
216 return true;
217
218 switch (tag)
219 {
220 case HB_TAG ('c','v','a','r'): /* hint table, fallthrough */
221 case HB_TAG ('c','v','t',' '): /* hint table, fallthrough */
222 case HB_TAG ('f','p','g','m'): /* hint table, fallthrough */
223 case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */
224 case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */
225 case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */
226 return plan->drop_hints;
227
228 #ifdef HB_NO_SUBSET_LAYOUT
229 // Drop Layout Tables if requested.
230 case HB_OT_TAG_GDEF:
231 case HB_OT_TAG_GPOS:
232 case HB_OT_TAG_GSUB:
233 case HB_TAG ('m','o','r','x'):
234 case HB_TAG ('m','o','r','t'):
235 case HB_TAG ('k','e','r','x'):
236 case HB_TAG ('k','e','r','n'):
237 return true;
238 #endif
239
240 default:
241 return false;
242 }
243 }
244
245 static bool
_subset_table(hb_subset_plan_t * plan,hb_tag_t tag)246 _subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
247 {
248 DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
249 switch (tag)
250 {
251 case HB_OT_TAG_glyf: return _subset<const OT::glyf> (plan);
252 case HB_OT_TAG_hdmx: return _subset<const OT::hdmx> (plan);
253 case HB_OT_TAG_name: return _subset<const OT::name> (plan);
254 case HB_OT_TAG_head:
255 if (_is_table_present (plan->source, HB_OT_TAG_glyf) && !_should_drop_table (plan, HB_OT_TAG_glyf))
256 return true; /* skip head, handled by glyf */
257 return _subset<const OT::head> (plan);
258 case HB_OT_TAG_hhea: return true; /* skip hhea, handled by hmtx */
259 case HB_OT_TAG_hmtx: return _subset<const OT::hmtx> (plan);
260 case HB_OT_TAG_vhea: return true; /* skip vhea, handled by vmtx */
261 case HB_OT_TAG_vmtx: return _subset<const OT::vmtx> (plan);
262 case HB_OT_TAG_maxp: return _subset<const OT::maxp> (plan);
263 case HB_OT_TAG_sbix: return _subset<const OT::sbix> (plan);
264 case HB_OT_TAG_loca: return true; /* skip loca, handled by glyf */
265 case HB_OT_TAG_cmap: return _subset<const OT::cmap> (plan);
266 case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan);
267 case HB_OT_TAG_post: return _subset<const OT::post> (plan);
268 case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan);
269 case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan);
270 case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
271
272 #ifndef HB_NO_SUBSET_CFF
273 case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan);
274 case HB_OT_TAG_cff2: return _subset<const OT::cff2> (plan);
275 case HB_OT_TAG_VORG: return _subset<const OT::VORG> (plan);
276 #endif
277
278 #ifndef HB_NO_SUBSET_LAYOUT
279 case HB_OT_TAG_GDEF: return _subset<const OT::GDEF> (plan);
280 case HB_OT_TAG_GSUB: return _subset<const OT::GSUB> (plan);
281 case HB_OT_TAG_GPOS: return _subset<const OT::GPOS> (plan);
282 case HB_OT_TAG_gvar: return _subset<const OT::gvar> (plan);
283 case HB_OT_TAG_HVAR: return _subset<const OT::HVAR> (plan);
284 case HB_OT_TAG_VVAR: return _subset<const OT::VVAR> (plan);
285 #endif
286
287 default:
288 hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
289 bool result = plan->add_table (tag, source_table);
290 hb_blob_destroy (source_table);
291 return result;
292 }
293 }
294
295 /**
296 * hb_subset:
297 * @source: font face data to be subset.
298 * @input: input to use for the subsetting.
299 *
300 * Subsets a font according to provided input.
301 **/
302 hb_face_t *
hb_subset(hb_face_t * source,hb_subset_input_t * input)303 hb_subset (hb_face_t *source, hb_subset_input_t *input)
304 {
305 if (unlikely (!input || !source)) return hb_face_get_empty ();
306
307 hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
308 if (unlikely (plan->in_error ())) {
309 hb_subset_plan_destroy (plan);
310 return hb_face_get_empty ();
311 }
312
313 hb_set_t tags_set;
314 bool success = true;
315 hb_tag_t table_tags[32];
316 unsigned offset = 0, num_tables = ARRAY_LENGTH (table_tags);
317 while ((hb_face_get_table_tags (source, offset, &num_tables, table_tags), num_tables))
318 {
319 for (unsigned i = 0; i < num_tables; ++i)
320 {
321 hb_tag_t tag = table_tags[i];
322 if (_should_drop_table (plan, tag) && !tags_set.has (tag)) continue;
323 tags_set.add (tag);
324 success = _subset_table (plan, tag);
325 if (unlikely (!success)) goto end;
326 }
327 offset += num_tables;
328 }
329 end:
330
331 hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
332
333 hb_subset_plan_destroy (plan);
334 return result;
335 }
336