blob: 6bcc0b0234bb4a0e9b0fccd51567fa743a333e6f [file] [log] [blame]
Garret Riegera2965f22018-01-31 14:53:09 -08001/*
Behdad Esfahbodbfa87162018-02-10 13:37:28 -06002 * Copyright © 2018 Google, Inc.
Garret Riegera2965f22018-01-31 14:53:09 -08003 *
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 *
Rod Sheeterd463e9f2018-02-14 15:04:15 -080024 * Google Author(s): Garret Rieger, Rod Sheeter, Behdad Esfahbod
Garret Riegera2965f22018-01-31 14:53:09 -080025 */
26
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070027#include "hb.hh"
28#include "hb-open-type.hh"
Rod Sheeter0859a002018-02-07 15:59:36 -080029
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-subset.hh"
Garret Riegera2965f22018-01-31 14:53:09 -080031
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070032#include "hb-open-file.hh"
Rod Sheeter0859a002018-02-07 15:59:36 -080033#include "hb-ot-cmap-table.hh"
Garret Rieger6a45e5d2018-02-06 16:04:09 -080034#include "hb-ot-glyf-table.hh"
Garret Riegerfe428622018-02-21 14:18:49 -080035#include "hb-ot-hdmx-table.hh"
Garret Rieger83e1ef92018-02-12 11:22:32 -080036#include "hb-ot-head-table.hh"
Rod Sheeterfa877702018-02-14 14:16:25 -080037#include "hb-ot-hhea-table.hh"
38#include "hb-ot-hmtx-table.hh"
Garret Rieger83e1ef92018-02-12 11:22:32 -080039#include "hb-ot-maxp-table.hh"
ckitagawa43b6c862020-01-09 10:44:20 -050040#include "hb-ot-color-sbix-table.hh"
ckitagawaed857c42020-01-24 08:52:23 -050041#include "hb-ot-color-colr-table.hh"
Garret Rieger24904382018-02-12 17:31:01 -080042#include "hb-ot-os2-table.hh"
Garret Riegerdd107692018-03-06 18:33:39 -080043#include "hb-ot-post-table.hh"
Michiharu Arizafdbfa182018-08-16 00:13:09 -070044#include "hb-ot-cff1-table.hh"
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -070045#include "hb-ot-cff2-table.hh"
Michiharu Arizaf212c052018-09-20 09:45:43 -070046#include "hb-ot-vorg-table.hh"
Qunxin Liu408c1da2019-03-29 10:34:32 -070047#include "hb-ot-name-table.hh"
ckitagawae128f802020-01-21 13:35:43 -050048#include "hb-ot-color-cbdt-table.hh"
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070049#include "hb-ot-layout-gsub-table.hh"
50#include "hb-ot-layout-gpos-table.hh"
Michiharu Ariza2c5ed7f2019-02-26 11:11:50 -080051#include "hb-ot-var-gvar-table.hh"
Michiharu Ariza09df17e2019-02-20 15:48:29 -080052#include "hb-ot-var-hvar-table.hh"
Garret Rieger6a45e5d2018-02-06 16:04:09 -080053
Behdad Esfahbod2da0d872018-02-07 12:44:35 -050054
Ebrahim Byagowi43016712020-02-05 15:02:20 +033055static unsigned
56_plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
Garret Riegera2965f22018-01-31 14:53:09 -080057{
Ebrahim Byagowi43016712020-02-05 15:02:20 +033058 unsigned src_glyphs = plan->source->get_num_glyphs ();
59 unsigned dst_glyphs = plan->glyphset ()->get_population ();
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070060
Behdad Esfahbod5f17dbc2018-09-07 10:24:22 -040061 if (unlikely (!src_glyphs))
62 return 512 + table_len;
63
Ebrahim Byagowi43016712020-02-05 15:02:20 +033064 return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
Garret Riegera2965f22018-01-31 14:53:09 -080065}
66
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070067template<typename TableType>
68static bool
Ebrahim Byagowi5b436032020-02-04 21:29:19 +033069_subset (hb_subset_plan_t *plan)
Garret Riegera2965f22018-01-31 14:53:09 -080070{
Qunxin Liu209491f2019-05-29 14:03:17 -070071 bool result = false;
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070072 hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
73 const TableType *table = source_blob->as<TableType> ();
Garret Riegera2965f22018-01-31 14:53:09 -080074
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070075 hb_tag_t tag = TableType::tableTag;
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070076 if (source_blob->data)
77 {
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -070078 hb_vector_t<char> buf;
Behdad Esfahbod521262b2019-05-07 11:08:08 -070079 /* TODO Not all tables are glyph-related. 'name' table size for example should not be
80 * affected by number of glyphs. Accommodate that. */
Ebrahim Byagowi43016712020-02-05 15:02:20 +033081 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);
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070083 if (unlikely (!buf.alloc (buf_size)))
84 {
Ebrahim Byagowi43016712020-02-05 15:02:20 +033085 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
Ebrahim Byagowi28aba782019-08-23 16:47:15 +043086 hb_blob_destroy (source_blob);
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070087 return false;
88 }
89 retry:
Behdad Esfahbod861bc752018-11-29 14:34:44 -050090 hb_serialize_context_t serializer ((void *) buf, buf_size);
Behdad Esfahbod2e675cc2019-04-01 16:45:50 -070091 serializer.start_serialize<TableType> ();
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070092 hb_subset_context_t c (plan, &serializer);
Behdad Esfahbod6977a952019-04-27 10:05:25 -070093 bool needed = table->subset (&c);
Behdad Esfahbodfe05e482019-03-30 15:06:25 -070094 if (serializer.ran_out_of_room)
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070095 {
96 buf_size += (buf_size >> 1) + 32;
Ebrahim Byagowi43016712020-02-05 15:02:20 +033097 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);
Behdad Esfahboda1e5e072018-09-05 16:24:28 -070098 if (unlikely (!buf.alloc (buf_size)))
99 {
Ebrahim Byagowi43016712020-02-05 15:02:20 +0330100 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size);
Ebrahim Byagowi28aba782019-08-23 16:47:15 +0430101 hb_blob_destroy (source_blob);
Behdad Esfahboda1e5e072018-09-05 16:24:28 -0700102 return false;
103 }
104 goto retry;
105 }
Qunxin Liu19afd252019-04-24 14:02:29 -0700106 serializer.end_serialize ();
Behdad Esfahbod2b051e72019-04-27 10:01:11 -0700107
Behdad Esfahbod6977a952019-04-27 10:05:25 -0700108 result = !serializer.in_error ();
Behdad Esfahbodfe05e482019-03-30 15:06:25 -0700109
Behdad Esfahboda1e5e072018-09-05 16:24:28 -0700110 if (result)
111 {
Behdad Esfahbod6977a952019-04-27 10:05:25 -0700112 if (needed)
113 {
114 hb_blob_t *dest_blob = serializer.copy_blob ();
Ebrahim Byagowi43016712020-02-05 15:02:20 +0330115 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length);
Behdad Esfahbod6977a952019-04-27 10:05:25 -0700116 result = c.plan->add_table (tag, dest_blob);
117 hb_blob_destroy (dest_blob);
118 }
119 else
120 {
Ebrahim Byagowi43016712020-02-05 15:02:20 +0330121 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
Behdad Esfahbod6977a952019-04-27 10:05:25 -0700122 }
Behdad Esfahboda1e5e072018-09-05 16:24:28 -0700123 }
124 }
125 else
Ebrahim Byagowi43016712020-02-05 15:02:20 +0330126 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
Behdad Esfahboda1e5e072018-09-05 16:24:28 -0700127
128 hb_blob_destroy (source_blob);
Ebrahim Byagowi43016712020-02-05 15:02:20 +0330129 DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
Behdad Esfahboda1e5e072018-09-05 16:24:28 -0700130 return result;
Garret Riegera2965f22018-01-31 14:53:09 -0800131}
132
Behdad Esfahboda981d792018-02-10 15:17:28 -0600133static bool
Ebrahim Byagowib1f63102020-02-05 00:28:21 +0330134_is_table_present (hb_face_t *source, hb_tag_t tag)
ckitagawa0d619262020-02-04 13:36:51 -0500135{
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330136 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))
Ebrahim Byagowib1f63102020-02-05 00:28:21 +0330139 {
140 for (unsigned i = 0; i < num_tables; ++i)
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330141 if (table_tags[i] == tag)
Ebrahim Byagowib1f63102020-02-05 00:28:21 +0330142 return true;
143 offset += num_tables;
ckitagawa0d619262020-02-04 13:36:51 -0500144 }
145 return false;
146}
147
148static bool
149_should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
150{
151 if (plan->drop_tables->has (tag))
152 return true;
153
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330154 switch (tag)
155 {
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;
ckitagawa0d619262020-02-04 13:36:51 -0500163
164#ifdef HB_NO_SUBSET_LAYOUT
165 // Drop Layout Tables if requested.
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330166 case HB_OT_TAG_GDEF:
167 case HB_OT_TAG_GPOS:
168 case HB_OT_TAG_GSUB:
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'):
173 return true;
ckitagawa0d619262020-02-04 13:36:51 -0500174#endif
175
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330176 default:
177 return false;
ckitagawa0d619262020-02-04 13:36:51 -0500178 }
179}
180
181static bool
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330182_subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
Garret Riegeraac7d962018-02-08 18:18:16 -0800183{
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330184 DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
185 switch (tag)
186 {
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);
190 case HB_OT_TAG_head:
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);
Ebrahim Byagowia7f694d2020-02-05 16:31:21 +0330205 case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan);
206 case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
Behdad Esfahbod5ea8ad52019-05-10 23:36:42 -0700207
208#ifndef HB_NO_SUBSET_CFF
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330209 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);
Behdad Esfahbod5ea8ad52019-05-10 23:36:42 -0700212#endif
Behdad Esfahbod47e538a2019-04-12 22:50:22 -0400213
Behdad Esfahbod227d85e2019-05-10 23:15:58 -0700214#ifndef HB_NO_SUBSET_LAYOUT
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330215 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);
Behdad Esfahbod47e538a2019-04-12 22:50:22 -0400221#endif
Behdad Esfahboda1e5e072018-09-05 16:24:28 -0700222
Ebrahim Byagowia8593332020-02-05 15:00:25 +0330223 default:
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);
227 return result;
Garret Rieger83e1ef92018-02-12 11:22:32 -0800228 }
Garret Riegeraac7d962018-02-08 18:18:16 -0800229}
230
Garret Riegera2965f22018-01-31 14:53:09 -0800231/**
232 * hb_subset:
Behdad Esfahbod7fd0b612018-02-07 16:44:52 -0600233 * @source: font face data to be subset.
Garret Riegera2965f22018-01-31 14:53:09 -0800234 * @input: input to use for the subsetting.
Garret Riegera2965f22018-01-31 14:53:09 -0800235 *
Behdad Esfahbodf39166f2018-08-29 18:09:55 -0700236 * Subsets a font according to provided input.
Garret Riegera2965f22018-01-31 14:53:09 -0800237 **/
Behdad Esfahbodaf028122018-02-07 13:07:46 -0500238hb_face_t *
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330239hb_subset (hb_face_t *source, hb_subset_input_t *input)
Garret Riegera2965f22018-01-31 14:53:09 -0800240{
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330241 if (unlikely (!input || !source)) return hb_face_get_empty ();
Garret Riegera2965f22018-01-31 14:53:09 -0800242
Behdad Esfahbodf39166f2018-08-29 18:09:55 -0700243 hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
Garret Rieger31ec3c22018-02-06 15:37:34 -0800244
Ebrahim Byagowi41d6e952019-09-02 01:41:48 +0430245 hb_set_t tags_set;
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330246 bool success = true;
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))
250 {
251 for (unsigned i = 0; i < num_tables; ++i)
Garret Riegeraac7d962018-02-08 18:18:16 -0800252 {
253 hb_tag_t tag = table_tags[i];
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330254 if (_should_drop_table (plan, tag) && !tags_set.has (tag)) continue;
Ebrahim Byagowi41d6e952019-09-02 01:41:48 +0430255 tags_set.add (tag);
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330256 success = _subset_table (plan, tag);
257 if (unlikely (!success)) goto end;
Garret Riegeraac7d962018-02-08 18:18:16 -0800258 }
Ebrahim Byagowidcb5dfc2020-02-05 15:01:25 +0330259 offset += num_tables;
260 }
261end:
Rod Sheeter13193a92018-02-07 16:09:52 -0800262
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330263 hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
Garret Rieger5df080b2018-02-12 10:15:59 -0800264 hb_subset_plan_destroy (plan);
Rod Sheeterfa877702018-02-14 14:16:25 -0800265 return result;
Garret Riegera2965f22018-01-31 14:53:09 -0800266}