blob: 1bdf1339f31b9542416cfaf2d36dbb65234a1ab2 [file] [log] [blame]
Garret Rieger1cfe6502018-02-05 15:22:30 -08001/*
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 *
Rod Sheeterd0ce3c62018-02-06 16:58:35 -080024 * Google Author(s): Garret Rieger, Roderick Sheeter
Garret Rieger1cfe6502018-02-05 15:22:30 -080025 */
26
Behdad Esfahbodaba0a942018-08-31 13:25:19 -070027#include "hb-subset-plan.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070028#include "hb-map.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070029#include "hb-set.hh"
Garret Rieger1cfe6502018-02-05 15:22:30 -080030
Behdad Esfahbodaf028122018-02-07 13:07:46 -050031#include "hb-ot-cmap-table.hh"
Garret Riegerdcac9fe2018-02-16 11:27:03 -070032#include "hb-ot-glyf-table.hh"
Qunxin Liud7c012a2020-02-26 13:11:42 -080033#include "hb-ot-layout-gdef-table.hh"
34#include "hb-ot-layout-gpos-table.hh"
Qunxin Liu973c47f2020-06-11 11:27:57 -070035#include "hb-ot-layout-gsub-table.hh"
Michiharu Ariza3787c072018-11-14 13:38:03 -080036#include "hb-ot-cff1-table.hh"
ckitagawaed857c42020-01-24 08:52:23 -050037#include "hb-ot-color-colr-table.hh"
Qunxin Liue59ffe52021-04-01 12:01:19 -070038#include "hb-ot-color-colrv1-closure.hh"
Qunxin Liu02e5e5d2019-05-13 09:38:42 -070039#include "hb-ot-var-fvar-table.hh"
40#include "hb-ot-stat-table.hh"
Qunxin Liuca7b9da2021-09-20 14:42:51 -070041#include "hb-ot-math-table.hh"
Behdad Esfahbodaf028122018-02-07 13:07:46 -050042
Qunxin Liu8200e482020-02-26 13:11:42 -080043
Qunxin Liu56ca4352021-01-28 15:21:26 -080044typedef hb_hashmap_t<unsigned, hb_set_t *, (unsigned)-1, nullptr> script_langsys_map;
Behdad Esfahboda20db492019-05-12 11:08:45 -070045#ifndef HB_NO_SUBSET_CFF
Behdad Esfahbodd43af332019-05-10 23:46:22 -070046static inline void
Michiharu Ariza3787c072018-11-14 13:38:03 -080047_add_cff_seac_components (const OT::cff1::accelerator_t &cff,
Ebrahim Byagowic7621cf2019-10-08 13:24:26 +033048 hb_codepoint_t gid,
49 hb_set_t *gids_to_retain)
Michiharu Ariza3787c072018-11-14 13:38:03 -080050{
51 hb_codepoint_t base_gid, accent_gid;
52 if (cff.get_seac_components (gid, &base_gid, &accent_gid))
53 {
Ebrahim Byagowic7621cf2019-10-08 13:24:26 +033054 gids_to_retain->add (base_gid);
55 gids_to_retain->add (accent_gid);
Michiharu Ariza3787c072018-11-14 13:38:03 -080056 }
57}
Behdad Esfahboda20db492019-05-12 11:08:45 -070058#endif
Michiharu Ariza3787c072018-11-14 13:38:03 -080059
Qunxin Liu0b39c482019-10-22 16:00:43 -070060static void
Qunxin Liuf739e1d2021-05-11 11:44:32 -070061_remap_palette_indexes (const hb_set_t *palette_indexes,
Garret Rieger9fb3a252021-06-09 13:46:35 -070062 hb_map_t *mapping /* OUT */)
Qunxin Liuf739e1d2021-05-11 11:44:32 -070063{
64 unsigned new_idx = 0;
65 for (unsigned palette_index : palette_indexes->iter ())
66 {
67 if (palette_index == 0xFFFF)
68 {
69 mapping->set (palette_index, palette_index);
70 continue;
71 }
72 mapping->set (palette_index, new_idx);
73 new_idx++;
74 }
75}
76
77static void
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -070078_remap_indexes (const hb_set_t *indexes,
79 hb_map_t *mapping /* OUT */)
Qunxin Liu0b39c482019-10-22 16:00:43 -070080{
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -070081 unsigned count = indexes->get_population ();
Qunxin Liu0b39c482019-10-22 16:00:43 -070082
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -070083 for (auto _ : + hb_zip (indexes->iter (), hb_range (count)))
84 mapping->set (_.first, _.second);
Qunxin Liu0b39c482019-10-22 16:00:43 -070085
86}
87
Dominik Röttsches1dffb552021-05-18 12:31:14 +030088#ifndef HB_NO_SUBSET_LAYOUT
Qunxin Liucb5a6b52021-05-19 17:33:46 -070089typedef void (*layout_collect_func_t) (hb_face_t *face, hb_tag_t table_tag, const hb_tag_t *scripts, const hb_tag_t *languages, const hb_tag_t *features, hb_set_t *lookup_indexes /* OUT */);
90
Garret Rieger59deb752021-08-24 17:06:14 -070091
Garret Riegerf2441a42021-08-24 15:53:32 -070092template <typename T>
Garret Rieger59deb752021-08-24 17:06:14 -070093static void _collect_layout_indices (hb_face_t *face,
94 const T& table,
95 const hb_set_t *layout_features_to_retain,
96 layout_collect_func_t layout_collect_func,
Behdad Esfahbode9e6d662021-08-24 23:22:49 -060097 hb_set_t *indices /* OUT */)
Garret Riegerfeb23892018-06-07 14:32:34 -070098{
Garret Riegerf2441a42021-08-24 15:53:32 -070099 hb_vector_t<hb_tag_t> features;
100 if (!features.alloc (table.get_feature_count () + 1))
101 return;
102
103 for (unsigned i = 0; i < table.get_feature_count (); i++)
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700104 {
Garret Riegerf2441a42021-08-24 15:53:32 -0700105 hb_tag_t tag = table.get_feature_tag (i);
106 if (tag && layout_features_to_retain->has (tag))
107 features.push (tag);
108 }
109
110 if (!features)
111 return;
112
113 // The collect function needs a null element to signal end of the array.
114 features.push (0);
115
116 if (features.get_size () == table.get_feature_count () + 1)
117 {
118 // Looking for all features, trigger the faster collection method.
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700119 layout_collect_func (face,
Garret Riegerf2441a42021-08-24 15:53:32 -0700120 T::tableTag,
121 nullptr,
122 nullptr,
123 nullptr,
Behdad Esfahbode9e6d662021-08-24 23:22:49 -0600124 indices);
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700125 return;
126 }
127
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700128 layout_collect_func (face,
Garret Riegerf2441a42021-08-24 15:53:32 -0700129 T::tableTag,
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700130 nullptr,
131 nullptr,
Garret Riegerf2441a42021-08-24 15:53:32 -0700132 features.arrayZ,
Behdad Esfahbode9e6d662021-08-24 23:22:49 -0600133 indices);
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700134}
135
136template <typename T>
137static inline void
Garret Rieger9fb3a252021-06-09 13:46:35 -0700138_closure_glyphs_lookups_features (hb_face_t *face,
139 hb_set_t *gids_to_retain,
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700140 const hb_set_t *layout_features_to_retain,
Garret Rieger9fb3a252021-06-09 13:46:35 -0700141 hb_map_t *lookups,
142 hb_map_t *features,
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700143 script_langsys_map *langsys_map)
144{
145 hb_blob_ptr_t<T> table = hb_sanitize_context_t ().reference_table<T> (face);
146 hb_tag_t table_tag = table->tableTag;
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700147 hb_set_t lookup_indices;
Garret Rieger59deb752021-08-24 17:06:14 -0700148 _collect_layout_indices<T> (face,
149 *table,
150 layout_features_to_retain,
151 hb_ot_layout_collect_lookups,
152 &lookup_indices);
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700153
154 if (table_tag == HB_OT_TAG_GSUB)
155 hb_ot_layout_lookups_substitute_closure (face,
156 &lookup_indices,
157 gids_to_retain);
158 table->closure_lookups (face,
159 gids_to_retain,
Qunxin Liu973c47f2020-06-11 11:27:57 -0700160 &lookup_indices);
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700161 _remap_indexes (&lookup_indices, lookups);
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -0700162
Garret Rieger718bf5a2020-09-29 13:16:01 -0700163 // Collect and prune features
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -0700164 hb_set_t feature_indices;
Garret Rieger59deb752021-08-24 17:06:14 -0700165 _collect_layout_indices<T> (face,
166 *table,
167 layout_features_to_retain,
168 hb_ot_layout_collect_features,
169 &feature_indices);
Qunxin Liu56ca4352021-01-28 15:21:26 -0800170
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700171 table->prune_features (lookups, &feature_indices);
Qunxin Liu56ca4352021-01-28 15:21:26 -0800172 hb_map_t duplicate_feature_map;
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700173 table->find_duplicate_features (lookups, &feature_indices, &duplicate_feature_map);
Qunxin Liu56ca4352021-01-28 15:21:26 -0800174
175 feature_indices.clear ();
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700176 table->prune_langsys (&duplicate_feature_map, langsys_map, &feature_indices);
177 _remap_indexes (&feature_indices, features);
Garret Rieger718bf5a2020-09-29 13:16:01 -0700178
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700179 table.destroy ();
Qunxin Liu0b39c482019-10-22 16:00:43 -0700180}
181
Dominik Röttschesc73d7ba2019-05-14 13:26:18 +0300182#endif
Garret Riegerfeb23892018-06-07 14:32:34 -0700183
Qunxin Liu8200e482020-02-26 13:11:42 -0800184#ifndef HB_NO_VAR
185static inline void
186 _collect_layout_variation_indices (hb_face_t *face,
187 const hb_set_t *glyphset,
188 const hb_map_t *gpos_lookups,
189 hb_set_t *layout_variation_indices,
190 hb_map_t *layout_variation_idx_map)
191{
Qunxin Liud7c012a2020-02-26 13:11:42 -0800192 hb_blob_ptr_t<OT::GDEF> gdef = hb_sanitize_context_t ().reference_table<OT::GDEF> (face);
193 hb_blob_ptr_t<OT::GPOS> gpos = hb_sanitize_context_t ().reference_table<OT::GPOS> (face);
194
195 if (!gdef->has_data ())
196 {
197 gdef.destroy ();
198 gpos.destroy ();
199 return;
200 }
201 OT::hb_collect_variation_indices_context_t c (layout_variation_indices, glyphset, gpos_lookups);
202 gdef->collect_variation_indices (&c);
203
204 if (hb_ot_layout_has_positioning (face))
205 gpos->collect_variation_indices (&c);
206
207 gdef->remap_layout_variation_indices (layout_variation_indices, layout_variation_idx_map);
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430208
Qunxin Liud7c012a2020-02-26 13:11:42 -0800209 gdef.destroy ();
210 gpos.destroy ();
Qunxin Liu8200e482020-02-26 13:11:42 -0800211}
212#endif
213
Behdad Esfahbodd43af332019-05-10 23:46:22 -0700214static inline void
Garret Rieger9fb3a252021-06-09 13:46:35 -0700215_cmap_closure (hb_face_t *face,
216 const hb_set_t *unicodes,
217 hb_set_t *glyphset)
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700218{
Behdad Esfahbod7152ac32019-10-28 17:12:55 -0700219 OT::cmap::accelerator_t cmap;
220 cmap.init (face);
221 cmap.table->closure_glyphs (unicodes, glyphset);
222 cmap.fini ();
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700223}
224
225static inline void
Qunxin Liuca7b9da2021-09-20 14:42:51 -0700226_math_closure (hb_face_t *face,
227 hb_set_t *glyphset)
228{
229 hb_blob_ptr_t<OT::MATH> math = hb_sanitize_context_t ().reference_table<OT::MATH> (face);
230 if (math->has_data ())
231 math->closure_glyphs (glyphset);
232 math.destroy ();
233}
234
235
236static inline void
Behdad Esfahbod9e9f16c2018-11-16 01:48:26 -0500237_remove_invalid_gids (hb_set_t *glyphs,
238 unsigned int num_glyphs)
239{
240 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
241 while (glyphs->next (&gid))
242 {
243 if (gid >= num_glyphs)
244 glyphs->del (gid);
245 }
246}
Garret Riegerfeb23892018-06-07 14:32:34 -0700247
Garret Rieger0af9de12019-05-20 15:04:20 -0700248static void
Garret Rieger9aa0ece2021-07-14 17:27:14 -0700249_populate_unicodes_to_retain (const hb_set_t *unicodes,
250 const hb_set_t *glyphs,
251 hb_subset_plan_t *plan)
Garret Rieger25e91732018-02-05 17:26:25 -0800252{
Behdad Esfahbodaf028122018-02-07 13:07:46 -0500253 OT::cmap::accelerator_t cmap;
Garret Rieger0af9de12019-05-20 15:04:20 -0700254 cmap.init (plan->source);
Rod Sheeter59c658c2018-02-08 19:22:47 -0800255
Behdad Esfahbod16206982021-08-25 16:25:08 -0600256 constexpr static const int size_threshold = 4096;
257
258 if (glyphs->is_empty () && unicodes->get_population () < size_threshold)
Behdad Esfahbode5ab34f2018-02-16 16:58:17 -0800259 {
Behdad Esfahbod16206982021-08-25 16:25:08 -0600260 /* This is the fast path if it's anticipated that size of unicodes
261 * is << than the number of codepoints in the font. */
Garret Rieger8f4f47d2021-08-25 13:34:05 -0700262 for (hb_codepoint_t cp : *unicodes)
Behdad Esfahbode5ab34f2018-02-16 16:58:17 -0800263 {
Garret Rieger8f4f47d2021-08-25 13:34:05 -0700264 hb_codepoint_t gid;
265 if (!cmap.get_nominal_glyph (cp, &gid))
266 {
267 DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
268 continue;
269 }
Garret Riegercd070702021-08-25 14:42:00 -0700270
Garret Rieger8f4f47d2021-08-25 13:34:05 -0700271 plan->codepoint_to_glyph->set (cp, gid);
Rod Sheeterd0ce3c62018-02-06 16:58:35 -0800272 }
Behdad Esfahbod16206982021-08-25 16:25:08 -0600273 }
274 else
275 {
Garret Riegercd070702021-08-25 14:42:00 -0700276 hb_map_t unicode_glyphid_map;
277 cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map);
Garret Riegercd070702021-08-25 14:42:00 -0700278
279 for (hb_pair_t<hb_codepoint_t, hb_codepoint_t> cp_gid :
Behdad Esfahbod16206982021-08-25 16:25:08 -0600280 + unicode_glyphid_map.iter ())
Garret Riegercd070702021-08-25 14:42:00 -0700281 {
282 if (!unicodes->has (cp_gid.first) && !glyphs->has (cp_gid.second))
Behdad Esfahbod16206982021-08-25 16:25:08 -0600283 continue;
Garret Riegercd070702021-08-25 14:42:00 -0700284
285 plan->codepoint_to_glyph->set (cp_gid.first, cp_gid.second);
286 }
287
Behdad Esfahbod16206982021-08-25 16:25:08 -0600288 /* Add gids which where requested, but not mapped in cmap */
289 // TODO(garretrieger):
290 // Once https://github.com/harfbuzz/harfbuzz/issues/3169
291 // is implemented, this can be done with union and del_range
Garret Riegercd070702021-08-25 14:42:00 -0700292 for (hb_codepoint_t gid : glyphs->iter ())
293 {
294 if (gid >= plan->source->get_num_glyphs ())
Behdad Esfahbod16206982021-08-25 16:25:08 -0600295 break;
Garret Riegercd070702021-08-25 14:42:00 -0700296 plan->_glyphset_gsub->add (gid);
297 }
Garret Rieger9aa0ece2021-07-14 17:27:14 -0700298 }
299
Behdad Esfahbod16206982021-08-25 16:25:08 -0600300 + plan->codepoint_to_glyph->keys () | hb_sink (plan->unicodes);
Garret Riegercd070702021-08-25 14:42:00 -0700301 + plan->codepoint_to_glyph->values () | hb_sink (plan->_glyphset_gsub);
Behdad Esfahbod16206982021-08-25 16:25:08 -0600302
303 cmap.fini ();
Garret Rieger9aa0ece2021-07-14 17:27:14 -0700304}
305
306static void
307_populate_gids_to_retain (hb_subset_plan_t* plan,
Garret Rieger9aa0ece2021-07-14 17:27:14 -0700308 bool close_over_gsub,
309 bool close_over_gpos,
310 bool close_over_gdef)
311{
312 OT::glyf::accelerator_t glyf;
313#ifndef HB_NO_SUBSET_CFF
314 OT::cff1::accelerator_t cff;
315#endif
316 OT::COLR::accelerator_t colr;
317 glyf.init (plan->source);
318#ifndef HB_NO_SUBSET_CFF
319 cff.init (plan->source);
320#endif
321 colr.init (plan->source);
322
323 plan->_glyphset_gsub->add (0); // Not-def
Garret Rieger9aa0ece2021-07-14 17:27:14 -0700324
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700325 _cmap_closure (plan->source, plan->unicodes, plan->_glyphset_gsub);
Rod Sheeter59c658c2018-02-08 19:22:47 -0800326
Behdad Esfahbodd43af332019-05-10 23:46:22 -0700327#ifndef HB_NO_SUBSET_LAYOUT
Garret Riegerfeb23892018-06-07 14:32:34 -0700328 if (close_over_gsub)
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -0700329 // closure all glyphs/lookups/features needed for GSUB substitutions.
Garret Rieger46d4a5e2021-07-29 15:07:13 -0700330 _closure_glyphs_lookups_features<OT::GSUB> (
331 plan->source,
332 plan->_glyphset_gsub,
333 plan->layout_features,
Garret Rieger46d4a5e2021-07-29 15:07:13 -0700334 plan->gsub_lookups,
335 plan->gsub_features,
336 plan->gsub_langsys);
Qunxin Liu0b39c482019-10-22 16:00:43 -0700337
338 if (close_over_gpos)
Garret Rieger46d4a5e2021-07-29 15:07:13 -0700339 _closure_glyphs_lookups_features<OT::GPOS> (
340 plan->source,
341 plan->_glyphset_gsub,
342 plan->layout_features,
Garret Rieger46d4a5e2021-07-29 15:07:13 -0700343 plan->gpos_lookups,
344 plan->gpos_features,
345 plan->gpos_langsys);
Behdad Esfahbodd43af332019-05-10 23:46:22 -0700346#endif
Garret Rieger0af9de12019-05-20 15:04:20 -0700347 _remove_invalid_gids (plan->_glyphset_gsub, plan->source->get_num_glyphs ());
Garret Riegerfeb23892018-06-07 14:32:34 -0700348
Qunxin Liuca7b9da2021-09-20 14:42:51 -0700349 hb_set_set (plan->_glyphset_mathed, plan->_glyphset_gsub);
350 _math_closure (plan->source, plan->_glyphset_mathed);
351 _remove_invalid_gids (plan->_glyphset_mathed, plan->source->get_num_glyphs ());
352
Garret Riegera08900b2021-05-04 16:48:41 -0700353 // Collect all glyphs referenced by COLRv0
Qunxin Liuca7b9da2021-09-20 14:42:51 -0700354 hb_set_t* cur_glyphset = plan->_glyphset_mathed;
Garret Riegera08900b2021-05-04 16:48:41 -0700355 hb_set_t glyphset_colrv0;
356 if (colr.is_valid ())
357 {
Behdad Esfahbodbc33b872021-06-09 11:51:32 -0600358 glyphset_colrv0.union_ (*cur_glyphset);
Garret Riegera08900b2021-05-04 16:48:41 -0700359 for (hb_codepoint_t gid : cur_glyphset->iter ())
360 colr.closure_glyphs (gid, &glyphset_colrv0);
361 cur_glyphset = &glyphset_colrv0;
362 }
363
Garret Rieger26c80ad2021-06-18 14:14:20 -0700364 hb_set_t palette_indices;
365 colr.closure_V0palette_indices (cur_glyphset, &palette_indices);
366
367 hb_set_t layer_indices;
368 colr.closure_forV1 (cur_glyphset, &layer_indices, &palette_indices);
369 _remap_indexes (&layer_indices, plan->colrv1_layers);
370 _remap_palette_indexes (&palette_indices, plan->colr_palettes);
371 colr.fini ();
372 _remove_invalid_gids (cur_glyphset, plan->source->get_num_glyphs ());
373
Garret Riegerdcac9fe2018-02-16 11:27:03 -0700374 // Populate a full set of glyphs to retain by adding all referenced
375 // composite glyphs.
Garret Riegera08900b2021-05-04 16:48:41 -0700376 for (hb_codepoint_t gid : cur_glyphset->iter ())
Garret Rieger251cc972018-05-30 12:23:51 -0700377 {
Ebrahim Byagowibe0eddd2019-10-08 13:46:55 +0330378 glyf.add_gid_and_children (gid, plan->_glyphset);
Behdad Esfahbodd43af332019-05-10 23:46:22 -0700379#ifndef HB_NO_SUBSET_CFF
Michiharu Ariza3787c072018-11-14 13:38:03 -0800380 if (cff.is_valid ())
Garret Rieger0af9de12019-05-20 15:04:20 -0700381 _add_cff_seac_components (cff, gid, plan->_glyphset);
Behdad Esfahbodd43af332019-05-10 23:46:22 -0700382#endif
Garret Rieger251cc972018-05-30 12:23:51 -0700383 }
Garret Rieger251cc972018-05-30 12:23:51 -0700384
Garret Rieger0af9de12019-05-20 15:04:20 -0700385 _remove_invalid_gids (plan->_glyphset, plan->source->get_num_glyphs ());
Qunxin Liuf739e1d2021-05-11 11:44:32 -0700386
Behdad Esfahbod9e9f16c2018-11-16 01:48:26 -0500387
Qunxin Liu8200e482020-02-26 13:11:42 -0800388#ifndef HB_NO_VAR
389 if (close_over_gdef)
Garret Riegeraace09a2020-10-06 10:26:17 -0700390 _collect_layout_variation_indices (plan->source,
Garret Rieger9fb3a252021-06-09 13:46:35 -0700391 plan->_glyphset_gsub,
392 plan->gpos_lookups,
393 plan->layout_variation_indices,
394 plan->layout_variation_idx_map);
Qunxin Liu8200e482020-02-26 13:11:42 -0800395#endif
396
blueshade7ab525ac2019-11-19 20:36:56 -0800397#ifndef HB_NO_SUBSET_CFF
Michiharu Ariza3787c072018-11-14 13:38:03 -0800398 cff.fini ();
blueshade7ab525ac2019-11-19 20:36:56 -0800399#endif
Garret Riegerdcac9fe2018-02-16 11:27:03 -0700400 glyf.fini ();
Garret Rieger25e91732018-02-05 17:26:25 -0800401}
Garret Rieger1cfe6502018-02-05 15:22:30 -0800402
Garret Rieger251cc972018-05-30 12:23:51 -0700403static void
Garret Rieger5e3cbed2019-05-08 16:33:03 -0700404_create_old_gid_to_new_gid_map (const hb_face_t *face,
Garret Rieger9fb3a252021-06-09 13:46:35 -0700405 bool retain_gids,
406 const hb_set_t *all_gids_to_retain,
407 hb_map_t *glyph_map, /* OUT */
408 hb_map_t *reverse_glyph_map, /* OUT */
409 unsigned int *num_glyphs /* OUT */)
Garret Rieger251cc972018-05-30 12:23:51 -0700410{
Garret Rieger5e3cbed2019-05-08 16:33:03 -0700411 if (!retain_gids)
Garret Rieger2da16542019-01-18 17:49:35 -0800412 {
Garret Rieger5e3cbed2019-05-08 16:33:03 -0700413 + hb_enumerate (hb_iter (all_gids_to_retain), (hb_codepoint_t) 0)
414 | hb_sink (reverse_glyph_map)
415 ;
416 *num_glyphs = reverse_glyph_map->get_population ();
417 } else {
418 + hb_iter (all_gids_to_retain)
Behdad Esfahbod78d35f02019-05-15 18:15:05 -0700419 | hb_map ([] (hb_codepoint_t _) {
Garret Rieger5e3cbed2019-05-08 16:33:03 -0700420 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, _);
421 })
Behdad Esfahbod70fe9e72019-05-13 17:35:02 -0700422 | hb_sink (reverse_glyph_map)
Garret Rieger5e3cbed2019-05-08 16:33:03 -0700423 ;
424
Garret Rieger6555f202019-05-15 09:42:38 -0700425 unsigned max_glyph =
426 + hb_iter (all_gids_to_retain)
Behdad Esfahbod6157bbe2019-07-17 11:20:08 -0700427 | hb_reduce (hb_max, 0u)
Garret Rieger6555f202019-05-15 09:42:38 -0700428 ;
429 *num_glyphs = max_glyph + 1;
Garret Rieger2da16542019-01-18 17:49:35 -0800430 }
Garret Rieger5e3cbed2019-05-08 16:33:03 -0700431
432 + reverse_glyph_map->iter ()
433 | hb_map (&hb_pair_t<hb_codepoint_t, hb_codepoint_t>::reverse)
434 | hb_sink (glyph_map)
435 ;
Garret Rieger251cc972018-05-30 12:23:51 -0700436}
437
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700438static void
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430439_nameid_closure (hb_face_t *face,
440 hb_set_t *nameids)
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700441{
Ebrahim Byagowi22037492019-08-04 11:31:31 +0430442#ifndef HB_NO_STYLE
Behdad Esfahbodb0debc32019-06-19 18:42:39 -0700443 face->table.STAT->collect_name_ids (nameids);
Behdad Esfahboda5897462019-06-19 18:36:35 -0700444#endif
Behdad Esfahboda8498732019-06-19 19:26:22 -0700445#ifndef HB_NO_VAR
Behdad Esfahbodb0debc32019-06-19 18:42:39 -0700446 face->table.fvar->collect_name_ids (nameids);
Behdad Esfahboda8498732019-06-19 19:26:22 -0700447#endif
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700448}
449
Garret Rieger1cfe6502018-02-05 15:22:30 -0800450/**
451 * hb_subset_plan_create:
Garret Rieger1b6c1aa2021-06-28 12:57:39 -0700452 * @face: font face to create the plan for.
Khaled Hosnyacc71002021-07-26 02:13:49 +0200453 * @input: a #hb_subset_input_t input.
Garret Rieger1b6c1aa2021-06-28 12:57:39 -0700454 *
Garret Rieger1cfe6502018-02-05 15:22:30 -0800455 * Computes a plan for subsetting the supplied face according
Behdad Esfahbodf39166f2018-08-29 18:09:55 -0700456 * to a provided input. The plan describes
Garret Rieger1cfe6502018-02-05 15:22:30 -0800457 * which tables and glyphs should be retained.
458 *
Khaled Hosnyacc71002021-07-26 02:13:49 +0200459 * Return value: (transfer full): New subset plan. Destroy with
460 * hb_subset_plan_destroy().
Garret Rieger1cfe6502018-02-05 15:22:30 -0800461 *
462 * Since: 1.7.5
463 **/
464hb_subset_plan_t *
Garret Rieger9fb3a252021-06-09 13:46:35 -0700465hb_subset_plan_create (hb_face_t *face,
Garret Riegeredb57a82021-06-09 10:42:48 -0700466 const hb_subset_input_t *input)
Garret Rieger1cfe6502018-02-05 15:22:30 -0800467{
Garret Riegerdae32b42020-07-28 18:31:46 -0700468 hb_subset_plan_t *plan;
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430469 if (unlikely (!(plan = hb_object_create<hb_subset_plan_t> ())))
Garret Riegerdae32b42020-07-28 18:31:46 -0700470 return const_cast<hb_subset_plan_t *> (&Null (hb_subset_plan_t));
Behdad Esfahbodc31fcf42018-02-10 14:20:10 -0600471
Garret Riegerdae32b42020-07-28 18:31:46 -0700472 plan->successful = true;
Garret Rieger46d4a5e2021-07-29 15:07:13 -0700473 plan->flags = input->flags;
Qunxin Liue501ea12019-04-05 10:05:55 -0700474 plan->unicodes = hb_set_create ();
Garret Rieger3844e582021-08-30 11:16:51 -0700475 plan->name_ids = hb_set_copy (input->sets.name_ids);
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700476 _nameid_closure (face, plan->name_ids);
Garret Rieger3844e582021-08-30 11:16:51 -0700477 plan->name_languages = hb_set_copy (input->sets.name_languages);
478 plan->layout_features = hb_set_copy (input->sets.layout_features);
479 plan->glyphs_requested = hb_set_copy (input->sets.glyphs);
480 plan->drop_tables = hb_set_copy (input->sets.drop_tables);
481 plan->no_subset_tables = hb_set_copy (input->sets.no_subset_tables);
Garret Riegerb56c9382018-02-14 16:05:39 -0800482 plan->source = hb_face_reference (face);
Behdad Esfahbodaadb2a92018-08-25 08:18:53 -0700483 plan->dest = hb_face_builder_create ();
Garret Rieger0af9de12019-05-20 15:04:20 -0700484
485 plan->_glyphset = hb_set_create ();
486 plan->_glyphset_gsub = hb_set_create ();
Qunxin Liuca7b9da2021-09-20 14:42:51 -0700487 plan->_glyphset_mathed = hb_set_create ();
Qunxin Liue501ea12019-04-05 10:05:55 -0700488 plan->codepoint_to_glyph = hb_map_create ();
489 plan->glyph_map = hb_map_create ();
490 plan->reverse_glyph_map = hb_map_create ();
Qunxin Liu0b39c482019-10-22 16:00:43 -0700491 plan->gsub_lookups = hb_map_create ();
492 plan->gpos_lookups = hb_map_create ();
Qunxin Liu56ca4352021-01-28 15:21:26 -0800493
Garret Rieger3c8273a2021-03-25 11:39:57 -0700494 if (plan->check_success (plan->gsub_langsys = hb_object_create<script_langsys_map> ()))
495 plan->gsub_langsys->init_shallow ();
496 if (plan->check_success (plan->gpos_langsys = hb_object_create<script_langsys_map> ()))
497 plan->gpos_langsys->init_shallow ();
498
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -0700499 plan->gsub_features = hb_map_create ();
500 plan->gpos_features = hb_map_create ();
Qunxin Liue59ffe52021-04-01 12:01:19 -0700501 plan->colrv1_layers = hb_map_create ();
Qunxin Liuf739e1d2021-05-11 11:44:32 -0700502 plan->colr_palettes = hb_map_create ();
Qunxin Liu8200e482020-02-26 13:11:42 -0800503 plan->layout_variation_indices = hb_set_create ();
504 plan->layout_variation_idx_map = hb_map_create ();
Garret Rieger0af9de12019-05-20 15:04:20 -0700505
Garret Rieger3c8273a2021-03-25 11:39:57 -0700506 if (plan->in_error ()) {
507 return plan;
508 }
509
Garret Rieger3844e582021-08-30 11:16:51 -0700510 _populate_unicodes_to_retain (input->sets.unicodes, input->sets.glyphs, plan);
Garret Rieger9aa0ece2021-07-14 17:27:14 -0700511
Garret Rieger0af9de12019-05-20 15:04:20 -0700512 _populate_gids_to_retain (plan,
Garret Rieger3844e582021-08-30 11:16:51 -0700513 !input->sets.drop_tables->has (HB_OT_TAG_GSUB),
514 !input->sets.drop_tables->has (HB_OT_TAG_GPOS),
515 !input->sets.drop_tables->has (HB_OT_TAG_GDEF));
Garret Riegerb7f97182019-01-17 18:55:56 -0800516
Garret Rieger198859b2019-01-28 18:10:56 -0800517 _create_old_gid_to_new_gid_map (face,
Garret Rieger3d534b12021-07-29 11:52:14 -0700518 input->flags & HB_SUBSET_FLAGS_RETAIN_GIDS,
Qunxin Liu31471332019-03-26 09:15:56 -0700519 plan->_glyphset,
Garret Rieger2da16542019-01-18 17:49:35 -0800520 plan->glyph_map,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430521 plan->reverse_glyph_map,
522 &plan->_num_output_glyphs);
Rod Sheeterfa877702018-02-14 14:16:25 -0800523
Garret Rieger25e91732018-02-05 17:26:25 -0800524 return plan;
Garret Rieger1cfe6502018-02-05 15:22:30 -0800525}
526
Garret Rieger1cfe6502018-02-05 15:22:30 -0800527/**
528 * hb_subset_plan_destroy:
Khaled Hosnyacc71002021-07-26 02:13:49 +0200529 * @plan: a #hb_subset_plan_t
530 *
531 * Decreases the reference count on @plan, and if it reaches zero, destroys
532 * @plan, freeing all memory.
Garret Rieger1cfe6502018-02-05 15:22:30 -0800533 *
534 * Since: 1.7.5
535 **/
536void
537hb_subset_plan_destroy (hb_subset_plan_t *plan)
538{
539 if (!hb_object_destroy (plan)) return;
540
Garret Rieger251cc972018-05-30 12:23:51 -0700541 hb_set_destroy (plan->unicodes);
Qunxin Liue501ea12019-04-05 10:05:55 -0700542 hb_set_destroy (plan->name_ids);
Qunxin Liu36a5c042020-01-21 13:37:28 -0800543 hb_set_destroy (plan->name_languages);
Qunxin Liucb5a6b52021-05-19 17:33:46 -0700544 hb_set_destroy (plan->layout_features);
Qunxin Liub2a965d2020-04-22 15:58:41 -0700545 hb_set_destroy (plan->glyphs_requested);
Garret Rieger3be0ffe2019-05-16 11:29:15 -0700546 hb_set_destroy (plan->drop_tables);
Garret Rieger2b9cb292021-07-22 13:26:03 -0700547 hb_set_destroy (plan->no_subset_tables);
Garret Riegerb56c9382018-02-14 16:05:39 -0800548 hb_face_destroy (plan->source);
549 hb_face_destroy (plan->dest);
Garret Rieger251cc972018-05-30 12:23:51 -0700550 hb_map_destroy (plan->codepoint_to_glyph);
551 hb_map_destroy (plan->glyph_map);
Garret Rieger23f36442019-01-18 18:33:21 -0800552 hb_map_destroy (plan->reverse_glyph_map);
Garret Riegerbdbe0472019-01-28 16:59:15 -0800553 hb_set_destroy (plan->_glyphset);
Garret Rieger0af9de12019-05-20 15:04:20 -0700554 hb_set_destroy (plan->_glyphset_gsub);
Qunxin Liuca7b9da2021-09-20 14:42:51 -0700555 hb_set_destroy (plan->_glyphset_mathed);
Qunxin Liu0b39c482019-10-22 16:00:43 -0700556 hb_map_destroy (plan->gsub_lookups);
557 hb_map_destroy (plan->gpos_lookups);
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -0700558 hb_map_destroy (plan->gsub_features);
559 hb_map_destroy (plan->gpos_features);
Qunxin Liue59ffe52021-04-01 12:01:19 -0700560 hb_map_destroy (plan->colrv1_layers);
Qunxin Liuf739e1d2021-05-11 11:44:32 -0700561 hb_map_destroy (plan->colr_palettes);
Qunxin Liu8200e482020-02-26 13:11:42 -0800562 hb_set_destroy (plan->layout_variation_indices);
563 hb_map_destroy (plan->layout_variation_idx_map);
Qunxin Liu8ffc9ad2019-10-31 15:59:02 -0700564
Garret Rieger3c8273a2021-03-25 11:39:57 -0700565 if (plan->gsub_langsys)
566 {
567 for (auto _ : plan->gsub_langsys->iter ())
568 hb_set_destroy (_.second);
Qunxin Liu56ca4352021-01-28 15:21:26 -0800569
Garret Rieger3c8273a2021-03-25 11:39:57 -0700570 hb_object_destroy (plan->gsub_langsys);
571 plan->gsub_langsys->fini_shallow ();
Behdad Esfahbod2337f0d2021-07-08 10:58:50 -0600572 hb_free (plan->gsub_langsys);
Garret Rieger3c8273a2021-03-25 11:39:57 -0700573 }
574
575 if (plan->gpos_langsys)
576 {
577 for (auto _ : plan->gpos_langsys->iter ())
578 hb_set_destroy (_.second);
579
580 hb_object_destroy (plan->gpos_langsys);
581 plan->gpos_langsys->fini_shallow ();
Behdad Esfahbod2337f0d2021-07-08 10:58:50 -0600582 hb_free (plan->gpos_langsys);
Garret Rieger3c8273a2021-03-25 11:39:57 -0700583 }
Garret Riegerb56c9382018-02-14 16:05:39 -0800584
Behdad Esfahbod2337f0d2021-07-08 10:58:50 -0600585 hb_free (plan);
Garret Rieger1cfe6502018-02-05 15:22:30 -0800586}