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-private.hh"
29 #include "hb-subset-plan.hh"
30 #include "hb-ot-cmap-table.hh"
31 #include "hb-ot-glyf-table.hh"
34 _hb_codepoint_t_cmp (const void *pa, const void *pb)
36 hb_codepoint_t a = * (hb_codepoint_t *) pa;
37 hb_codepoint_t b = * (hb_codepoint_t *) pb;
39 return a < b ? -1 : a > b ? +1 : 0;
43 hb_subset_plan_new_gid_for_codepoint (hb_subset_plan_t *plan,
44 hb_codepoint_t codepoint,
45 hb_codepoint_t *new_gid)
47 // TODO actual map, delete this garbage.
48 for (unsigned int i = 0; i < plan->codepoints.len; i++)
50 if (plan->codepoints[i] != codepoint) continue;
51 if (!hb_subset_plan_new_gid_for_old_id(plan, plan->gids_to_retain[i], new_gid))
61 hb_subset_plan_new_gid_for_old_id (hb_subset_plan_t *plan,
62 hb_codepoint_t old_gid,
63 hb_codepoint_t *new_gid)
65 // the index in old_gids is the new gid; only up to codepoints.len are valid
66 for (unsigned int i = 0; i < plan->gids_to_retain_sorted.len; i++)
68 if (plan->gids_to_retain_sorted[i] == old_gid)
78 hb_subset_plan_add_table (hb_subset_plan_t *plan,
82 hb_blob_t *source_blob = plan->source->reference_table (tag);
83 DEBUG_MSG(SUBSET, nullptr, "add table %c%c%c%c, dest %d bytes, source %d bytes", HB_UNTAG(tag), hb_blob_get_length (contents), hb_blob_get_length (source_blob));
84 hb_blob_destroy (source_blob);
85 return hb_subset_face_add_table(plan->dest, tag, contents);
89 _populate_codepoints (hb_set_t *input_codepoints,
90 hb_prealloced_array_t<hb_codepoint_t>& plan_codepoints)
92 plan_codepoints.alloc (hb_set_get_population (input_codepoints));
93 hb_codepoint_t cp = -1;
94 while (hb_set_next (input_codepoints, &cp)) {
95 hb_codepoint_t *wr = plan_codepoints.push();
98 plan_codepoints.qsort (_hb_codepoint_t_cmp);
102 _add_gid_and_children (const OT::glyf::accelerator_t &glyf,
104 hb_set_t *gids_to_retain)
106 if (hb_set_has (gids_to_retain, gid))
107 // Already visited this gid, ignore.
110 hb_set_add (gids_to_retain, gid);
112 OT::glyf::CompositeGlyphHeader::Iterator composite;
113 if (glyf.get_composite (gid, &composite))
117 _add_gid_and_children (glyf, (hb_codepoint_t) composite.current->glyphIndex, gids_to_retain);
118 } while (composite.move_to_next());
123 _populate_gids_to_retain (hb_face_t *face,
124 hb_prealloced_array_t<hb_codepoint_t>& codepoints,
125 hb_prealloced_array_t<hb_codepoint_t>& old_gids,
126 hb_prealloced_array_t<hb_codepoint_t>& old_gids_sorted)
128 OT::cmap::accelerator_t cmap;
129 OT::glyf::accelerator_t glyf;
133 hb_auto_array_t<unsigned int> bad_indices;
135 old_gids.alloc (codepoints.len);
136 for (unsigned int i = 0; i < codepoints.len; i++)
139 if (!cmap.get_nominal_glyph (codepoints[i], &gid))
142 *(bad_indices.push ()) = i;
144 *(old_gids.push ()) = gid;
147 /* Generally there shouldn't be any */
148 while (bad_indices.len > 0)
150 unsigned int i = bad_indices[bad_indices.len - 1];
152 DEBUG_MSG(SUBSET, nullptr, "Drop U+%04X; no gid", codepoints[i]);
153 codepoints.remove (i);
157 // Populate a full set of glyphs to retain by adding all referenced
159 // TODO expand with glyphs reached by G*
160 hb_set_t * all_gids_to_retain = hb_set_create ();
161 _add_gid_and_children (glyf, 0, all_gids_to_retain);
162 for (unsigned int i = 0; i < old_gids.len; i++)
163 _add_gid_and_children (glyf, old_gids[i], all_gids_to_retain);
165 // Transfer to a sorted list.
166 old_gids_sorted.alloc (hb_set_get_population (all_gids_to_retain));
167 hb_codepoint_t gid = HB_SET_VALUE_INVALID;
168 while (hb_set_next (all_gids_to_retain, &gid))
169 *(old_gids_sorted.push ()) = gid;
171 hb_set_destroy (all_gids_to_retain);
177 * hb_subset_plan_create:
178 * Computes a plan for subsetting the supplied face according
179 * to a provide profile and input. The plan describes
180 * which tables and glyphs should be retained.
182 * Return value: New subset plan.
187 hb_subset_plan_create (hb_face_t *face,
188 hb_subset_profile_t *profile,
189 hb_subset_input_t *input)
191 hb_subset_plan_t *plan = hb_object_create<hb_subset_plan_t> ();
193 plan->codepoints.init();
194 plan->gids_to_retain.init();
195 plan->gids_to_retain_sorted.init();
196 plan->source = hb_face_reference (face);
197 plan->dest = hb_subset_face_create ();
198 plan->drop_hints = input->drop_hints;
200 _populate_codepoints (input->unicodes, plan->codepoints);
201 _populate_gids_to_retain (face,
203 plan->gids_to_retain,
204 plan->gids_to_retain_sorted);
210 * hb_subset_plan_destroy:
215 hb_subset_plan_destroy (hb_subset_plan_t *plan)
217 if (!hb_object_destroy (plan)) return;
219 plan->codepoints.finish ();
220 plan->gids_to_retain.finish ();
221 plan->gids_to_retain_sorted.finish ();
223 hb_face_destroy (plan->source);
224 hb_face_destroy (plan->dest);