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