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-os2-table.hh"
41 #include "hb-ot-post-table.hh"
42 #include "hb-ot-cff1-table.hh"
43 #include "hb-ot-cff2-table.hh"
44 #include "hb-ot-vorg-table.hh"
45 #include "hb-ot-name-table.hh"
46 #include "hb-ot-layout-gsub-table.hh"
47 #include "hb-ot-layout-gpos-table.hh"
50 HB_UNUSED static inline unsigned int
51 _plan_estimate_subset_table_size (hb_subset_plan_t *plan,
52 unsigned int table_len);
53 static inline unsigned int
54 _plan_estimate_subset_table_size (hb_subset_plan_t *plan,
55 unsigned int table_len)
57 unsigned int src_glyphs = plan->source->get_num_glyphs ();
58 unsigned int dst_glyphs = plan->glyphset ()->get_population ();
60 if (unlikely (!src_glyphs))
61 return 512 + table_len;
63 return 512 + (unsigned int) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
66 template<typename TableType>
68 _subset2 (hb_subset_plan_t *plan)
71 hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
72 const TableType *table = source_blob->as<TableType> ();
74 hb_tag_t tag = TableType::tableTag;
75 if (source_blob->data)
77 hb_vector_t<char> buf;
78 /* TODO Not all tables are glyph-related. 'name' table size for example should not be
79 * affected by number of glyphs. Accommodate that. */
80 unsigned int buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
81 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
82 if (unlikely (!buf.alloc (buf_size)))
84 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
85 hb_blob_destroy (source_blob);
89 hb_serialize_context_t serializer ((void *) buf, buf_size);
90 serializer.start_serialize<TableType> ();
91 hb_subset_context_t c (plan, &serializer);
92 bool needed = table->subset (&c);
93 if (serializer.ran_out_of_room)
95 buf_size += (buf_size >> 1) + 32;
96 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);
97 if (unlikely (!buf.alloc (buf_size)))
99 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size);
100 hb_blob_destroy (source_blob);
105 serializer.end_serialize ();
107 result = !serializer.in_error ();
113 hb_blob_t *dest_blob = serializer.copy_blob ();
114 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length);
115 result = c.plan->add_table (tag, dest_blob);
116 hb_blob_destroy (dest_blob);
120 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
125 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
127 hb_blob_destroy (source_blob);
128 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
132 template<typename TableType>
134 _subset (hb_subset_plan_t *plan)
136 hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
137 const TableType *table = source_blob->as<TableType> ();
139 hb_tag_t tag = TableType::tableTag;
140 hb_bool_t result = false;
141 if (source_blob->data)
142 result = table->subset (plan);
144 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
146 hb_blob_destroy (source_blob);
147 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
153 _subset_table (hb_subset_plan_t *plan,
156 DEBUG_MSG(SUBSET, nullptr, "begin subset %c%c%c%c", HB_UNTAG (tag));
160 result = _subset2<const OT::glyf> (plan);
163 result = _subset2<const OT::hdmx> (plan);
166 result = _subset2<const OT::name> (plan);
169 // TODO that won't work well if there is no glyf
170 DEBUG_MSG(SUBSET, nullptr, "skip head, handled by glyf");
174 DEBUG_MSG(SUBSET, nullptr, "skip hhea handled by hmtx");
177 result = _subset2<const OT::hmtx> (plan);
180 DEBUG_MSG(SUBSET, nullptr, "skip vhea handled by vmtx");
183 result = _subset2<const OT::vmtx> (plan);
186 result = _subset2<const OT::maxp> (plan);
189 DEBUG_MSG(SUBSET, nullptr, "skip loca handled by glyf");
192 result = _subset2<const OT::cmap> (plan);
195 result = _subset2<const OT::OS2> (plan);
198 result = _subset2<const OT::post> (plan);
201 #ifndef HB_NO_SUBSET_CFF
203 result = _subset<const OT::cff1> (plan);
206 result = _subset<const OT::cff2> (plan);
209 result = _subset2<const OT::VORG> (plan);
213 #ifndef HB_NO_SUBSET_LAYOUT
215 result = _subset2<const OT::GDEF> (plan);
218 result = _subset2<const OT::GSUB> (plan);
221 result = _subset2<const OT::GPOS> (plan);
226 hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
227 if (likely (source_table))
228 result = plan->add_table (tag, source_table);
231 hb_blob_destroy (source_table);
234 DEBUG_MSG(SUBSET, nullptr, "subset %c%c%c%c %s", HB_UNTAG (tag), result ? "ok" : "FAILED");
239 _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
241 if (plan->drop_tables->has (tag))
245 case HB_TAG ('c', 'v', 'a', 'r'): /* hint table, fallthrough */
246 case HB_TAG ('c', 'v', 't', ' '): /* hint table, fallthrough */
247 case HB_TAG ('f', 'p', 'g', 'm'): /* hint table, fallthrough */
248 case HB_TAG ('p', 'r', 'e', 'p'): /* hint table, fallthrough */
249 case HB_TAG ('h', 'd', 'm', 'x'): /* hint table, fallthrough */
250 case HB_TAG ('V', 'D', 'M', 'X'): /* hint table, fallthrough */
251 return plan->drop_hints;
253 #ifdef HB_NO_SUBSET_LAYOUT
254 // Drop Layout Tables if requested.
258 case HB_TAG ('m', 'o', 'r', 'x'):
259 case HB_TAG ('m', 'o', 'r', 't'):
260 case HB_TAG ('k', 'e', 'r', 'x'):
261 case HB_TAG ('k', 'e', 'r', 'n'):
272 * @source: font face data to be subset.
273 * @input: input to use for the subsetting.
275 * Subsets a font according to provided input.
278 hb_subset (hb_face_t *source,
279 hb_subset_input_t *input)
281 if (unlikely (!input || !source)) return hb_face_get_empty ();
283 hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
285 hb_tag_t table_tags[32];
286 unsigned int offset = 0, count;
290 count = ARRAY_LENGTH (table_tags);
291 hb_face_get_table_tags (source, offset, &count, table_tags);
292 for (unsigned int i = 0; i < count; i++)
294 hb_tag_t tag = table_tags[i];
295 if (_should_drop_table (plan, tag) && !tags_set.has (tag))
297 DEBUG_MSG(SUBSET, nullptr, "drop %c%c%c%c", HB_UNTAG (tag));
301 success = success && _subset_table (plan, tag);
304 } while (success && count == ARRAY_LENGTH (table_tags));
306 hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
307 hb_subset_plan_destroy (plan);