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
27 #include "hb-private.hh"
28 #include "hb-object-private.hh"
29 #include "hb-open-type-private.hh"
31 #include "hb-subset-glyf.hh"
32 #include "hb-subset-private.hh"
33 #include "hb-subset-plan.hh"
35 #include "hb-open-file-private.hh"
36 #include "hb-ot-cmap-table.hh"
37 #include "hb-ot-glyf-table.hh"
38 #include "hb-ot-hdmx-table.hh"
39 #include "hb-ot-head-table.hh"
40 #include "hb-ot-hhea-table.hh"
41 #include "hb-ot-hmtx-table.hh"
42 #include "hb-ot-maxp-table.hh"
43 #include "hb-ot-os2-table.hh"
46 #ifndef HB_NO_VISIBILITY
47 const void * const OT::_hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)] = {};
51 struct hb_subset_profile_t {
52 hb_object_header_t header;
57 * hb_subset_profile_create:
59 * Return value: New profile with default settings.
64 hb_subset_profile_create ()
66 return hb_object_create<hb_subset_profile_t>();
70 * hb_subset_profile_destroy:
75 hb_subset_profile_destroy (hb_subset_profile_t *profile)
77 if (!hb_object_destroy (profile)) return;
82 template<typename TableType>
84 _subset (hb_subset_plan_t *plan)
86 OT::Sanitizer<TableType> sanitizer;
88 hb_blob_t *source_blob = sanitizer.sanitize (plan->source->reference_table (TableType::tableTag));
89 const TableType *table = OT::Sanitizer<TableType>::lock_instance (source_blob);
91 hb_bool_t result = false;
92 if (table != &OT::Null(TableType))
93 result = table->subset(plan);
95 hb_blob_destroy (source_blob);
96 hb_tag_t tag = TableType::tableTag;
97 DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG(tag), result ? "success" : "FAILED!");
103 * A face that has add_table().
106 struct hb_subset_face_data_t
110 inline int cmp (const hb_tag_t *t) const
112 if (*t < tag) return -1;
113 if (*t > tag) return -1;
121 hb_prealloced_array_t<table_entry_t, 32> tables;
124 static hb_subset_face_data_t *
125 _hb_subset_face_data_create (void)
127 hb_subset_face_data_t *data = (hb_subset_face_data_t *) calloc (1, sizeof (hb_subset_face_data_t));
128 if (unlikely (!data))
135 _hb_subset_face_data_destroy (void *user_data)
137 hb_subset_face_data_t *data = (hb_subset_face_data_t *) user_data;
139 for (unsigned int i = 0; i < data->tables.len; i++)
140 hb_blob_destroy (data->tables[i].blob);
142 data->tables.finish ();
148 _hb_subset_face_data_reference_blob (hb_subset_face_data_t *data)
151 unsigned int table_count = data->tables.len;
152 unsigned int face_length = table_count * 16 + 12;
154 for (unsigned int i = 0; i < table_count; i++)
155 face_length += _hb_ceil_to_4 (hb_blob_get_length (data->tables.array[i].blob));
157 char *buf = (char *) malloc (face_length);
161 OT::hb_serialize_context_t c (buf, face_length);
162 OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();
164 bool is_cff = data->tables.lsearch (HB_TAG ('C','F','F',' ')) || data->tables.lsearch (HB_TAG ('C','F','F','2'));
165 hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag;
167 OT::Supplier<hb_tag_t> tags_supplier (&data->tables[0].tag, table_count, sizeof (data->tables[0]));
168 OT::Supplier<hb_blob_t *> blobs_supplier (&data->tables[0].blob, table_count, sizeof (data->tables[0]));
169 bool ret = f->serialize_single (&c,
183 return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, free);
187 _hb_subset_face_reference_table (hb_face_t *face, hb_tag_t tag, void *user_data)
189 hb_subset_face_data_t *data = (hb_subset_face_data_t *) user_data;
192 return _hb_subset_face_data_reference_blob (data);
194 hb_subset_face_data_t::table_entry_t *entry = data->tables.lsearch (tag);
196 return hb_blob_reference (entry->blob);
201 /* TODO: Move this to hb-face.h and rename to hb_face_builder_create()
202 * with hb_face_builder_add_table(). */
204 hb_subset_face_create (void)
206 hb_subset_face_data_t *data = _hb_subset_face_data_create ();
207 if (unlikely (!data)) return hb_face_get_empty ();
209 return hb_face_create_for_tables (_hb_subset_face_reference_table,
211 _hb_subset_face_data_destroy);
215 hb_subset_face_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
217 if (unlikely (face->destroy != _hb_subset_face_data_destroy))
220 hb_subset_face_data_t *data = (hb_subset_face_data_t *) face->user_data;
221 hb_subset_face_data_t::table_entry_t *entry = data->tables.push ();
222 if (unlikely (!entry))
226 entry->blob = hb_blob_reference (blob);
232 _subset_table (hb_subset_plan_t *plan,
235 DEBUG_MSG(SUBSET, nullptr, "begin subset %c%c%c%c", HB_UNTAG(tag));
239 result = _subset<const OT::glyf> (plan);
242 result = _subset<const OT::hdmx> (plan);
245 // TODO that won't work well if there is no glyf
246 DEBUG_MSG(SUBSET, nullptr, "skip head, handled by glyf");
250 DEBUG_MSG(SUBSET, nullptr, "skip hhea handled by hmtx");
253 result = _subset<const OT::hmtx> (plan);
256 result = _subset<const OT::maxp> (plan);
259 DEBUG_MSG(SUBSET, nullptr, "skip loca handled by glyf");
262 result = _subset<const OT::cmap> (plan);
265 result = _subset<const OT::os2> (plan);
268 hb_blob_t *source_table = hb_face_reference_table(plan->source, tag);
269 if (likely (source_table))
270 result = hb_subset_plan_add_table(plan, tag, source_table);
273 hb_blob_destroy (source_table);
276 DEBUG_MSG(SUBSET, nullptr, "subset %c%c%c%c %s", HB_UNTAG(tag), result ? "ok" : "FAILED");
281 _should_drop_table(hb_subset_plan_t *plan, hb_tag_t tag)
284 case HB_TAG ('c', 'v', 'a', 'r'): /* hint table, fallthrough */
285 case HB_TAG ('c', 'v', 't', ' '): /* hint table, fallthrough */
286 case HB_TAG ('f', 'p', 'g', 'm'): /* hint table, fallthrough */
287 case HB_TAG ('p', 'r', 'e', 'p'): /* hint table, fallthrough */
288 case HB_TAG ('h', 'd', 'm', 'x'): /* hint table, fallthrough */
289 case HB_TAG ('V', 'D', 'M', 'X'): /* hint table, fallthrough */
290 return plan->drop_hints;
291 case HB_TAG ('G', 'D', 'E', 'F'): /* temporary */
292 case HB_TAG ('G', 'P', 'O', 'S'): /* temporary */
293 case HB_TAG ('G', 'S', 'U', 'B'): /* temporary */
294 case HB_TAG ('D', 'S', 'I', 'G'):
303 * @source: font face data to be subset.
304 * @profile: profile to use for the subsetting.
305 * @input: input to use for the subsetting.
307 * Subsets a font according to provided profile and input.
310 hb_subset (hb_face_t *source,
311 hb_subset_profile_t *profile,
312 hb_subset_input_t *input)
314 if (unlikely (!profile || !input || !source)) return hb_face_get_empty();
316 hb_subset_plan_t *plan = hb_subset_plan_create (source, profile, input);
318 hb_tag_t table_tags[32];
319 unsigned int offset = 0, count;
322 count = ARRAY_LENGTH (table_tags);
323 hb_face_get_table_tags (source, offset, &count, table_tags);
324 for (unsigned int i = 0; i < count; i++)
326 hb_tag_t tag = table_tags[i];
327 if (_should_drop_table(plan, tag))
329 DEBUG_MSG(SUBSET, nullptr, "drop %c%c%c%c", HB_UNTAG(tag));
332 success = success && _subset_table (plan, tag);
334 } while (count == ARRAY_LENGTH (table_tags));
336 hb_face_t *result = success ? hb_face_reference(plan->dest) : hb_face_get_empty();
337 hb_subset_plan_destroy (plan);