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, Roderick Sheeter
27 #include "hb-subset-plan.hh"
31 #include "hb-ot-cmap-table.hh"
32 #include "hb-ot-glyf-table.hh"
33 #include "hb-ot-cff1-table.hh"
36 _add_gid_and_children (const OT::glyf::accelerator_t &glyf,
38 hb_set_t *gids_to_retain)
40 if (hb_set_has (gids_to_retain, gid))
41 // Already visited this gid, ignore.
44 hb_set_add (gids_to_retain, gid);
46 OT::glyf::CompositeGlyphHeader::Iterator composite;
47 if (glyf.get_composite (gid, &composite))
51 _add_gid_and_children (glyf, (hb_codepoint_t) composite.current->glyphIndex, gids_to_retain);
52 } while (composite.move_to_next());
57 _add_cff_seac_components (const OT::cff1::accelerator_t &cff,
59 hb_set_t *gids_to_retain)
61 hb_codepoint_t base_gid, accent_gid;
62 if (cff.get_seac_components (gid, &base_gid, &accent_gid))
64 hb_set_add (gids_to_retain, base_gid);
65 hb_set_add (gids_to_retain, accent_gid);
70 _gsub_closure (hb_face_t *face, hb_set_t *gids_to_retain)
72 hb_set_t lookup_indices;
73 hb_ot_layout_collect_lookups (face,
79 hb_ot_layout_lookups_substitute_closure (face,
85 _remove_invalid_gids (hb_set_t *glyphs,
86 unsigned int num_glyphs)
88 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
89 while (glyphs->next (&gid))
91 if (gid >= num_glyphs)
97 _populate_gids_to_retain (hb_face_t *face,
98 const hb_set_t *unicodes,
100 hb_set_t *unicodes_to_retain,
101 hb_map_t *codepoint_to_glyph,
102 hb_vector_t<hb_codepoint_t> *glyphs)
104 OT::cmap::accelerator_t cmap;
105 OT::glyf::accelerator_t glyf;
106 OT::cff1::accelerator_t cff;
111 hb_set_t *initial_gids_to_retain = hb_set_create ();
112 initial_gids_to_retain->add (0); // Not-def
114 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
115 while (unicodes->next (&cp))
118 if (!cmap.get_nominal_glyph (cp, &gid))
120 DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", cp);
123 unicodes_to_retain->add (cp);
124 codepoint_to_glyph->set (cp, gid);
125 initial_gids_to_retain->add (gid);
129 // Add all glyphs needed for GSUB substitutions.
130 _gsub_closure (face, initial_gids_to_retain);
132 // Populate a full set of glyphs to retain by adding all referenced
134 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
135 hb_set_t *all_gids_to_retain = hb_set_create ();
136 while (initial_gids_to_retain->next (&gid))
138 _add_gid_and_children (glyf, gid, all_gids_to_retain);
140 _add_cff_seac_components (cff, gid, all_gids_to_retain);
142 hb_set_destroy (initial_gids_to_retain);
144 _remove_invalid_gids (all_gids_to_retain, face->get_num_glyphs ());
146 glyphs->alloc (all_gids_to_retain->get_population ());
147 gid = HB_SET_VALUE_INVALID;
148 while (all_gids_to_retain->next (&gid))
155 return all_gids_to_retain;
159 _create_old_gid_to_new_gid_map (const hb_vector_t<hb_codepoint_t> &glyphs,
162 for (unsigned int i = 0; i < glyphs.length; i++) {
163 glyph_map->set (glyphs[i], i);
168 * hb_subset_plan_create:
169 * Computes a plan for subsetting the supplied face according
170 * to a provided input. The plan describes
171 * which tables and glyphs should be retained.
173 * Return value: New subset plan.
178 hb_subset_plan_create (hb_face_t *face,
179 hb_subset_input_t *input)
181 hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
183 plan->drop_hints = input->drop_hints;
184 plan->drop_layout = input->drop_layout;
185 plan->desubroutinize = input->desubroutinize;
186 plan->unicodes = hb_set_create();
188 plan->source = hb_face_reference (face);
189 plan->dest = hb_face_builder_create ();
190 plan->codepoint_to_glyph = hb_map_create();
191 plan->glyph_map = hb_map_create();
192 plan->glyphset = _populate_gids_to_retain (face,
196 plan->codepoint_to_glyph,
198 _create_old_gid_to_new_gid_map (plan->glyphs,
205 * hb_subset_plan_destroy:
210 hb_subset_plan_destroy (hb_subset_plan_t *plan)
212 if (!hb_object_destroy (plan)) return;
214 hb_set_destroy (plan->unicodes);
215 plan->glyphs.fini ();
216 hb_face_destroy (plan->source);
217 hb_face_destroy (plan->dest);
218 hb_map_destroy (plan->codepoint_to_glyph);
219 hb_map_destroy (plan->glyph_map);
220 hb_set_destroy (plan->glyphset);