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-open-type-private.hh"
28 #include "hb-ot-glyf-table.hh"
30 #include "hb-subset-glyf.hh"
31 #include "hb-subset-plan.hh"
34 _calculate_glyf_and_loca_prime_size (const OT::glyf::accelerator_t &glyf,
35 hb_prealloced_array_t<hb_codepoint_t> &glyph_ids,
37 bool *use_short_loca /* OUT */,
38 unsigned int *glyf_size /* OUT */,
39 unsigned int *loca_size /* OUT */,
40 hb_prealloced_array_t<unsigned int> *instruction_ranges /* OUT */)
42 unsigned int total = 0;
43 for (unsigned int i = 0; i < glyph_ids.len; i++)
45 hb_codepoint_t next_glyph = glyph_ids[i];
46 unsigned int *instruction_start = instruction_ranges->push();
47 unsigned int *instruction_end = instruction_ranges->push();
48 *instruction_start = 0;
51 unsigned int start_offset, end_offset;
52 if (unlikely (!(glyf.get_offsets(next_glyph, &start_offset, &end_offset)
53 && glyf.remove_padding(start_offset, &end_offset))))
55 DEBUG_MSG(SUBSET, nullptr, "Invalid gid %d", next_glyph);
58 if (end_offset - start_offset < OT::glyf::GlyphHeader::static_size)
59 continue; /* 0-length glyph */
63 if (unlikely (!glyf.get_instruction_offsets(start_offset, end_offset,
64 instruction_start, instruction_end)))
66 DEBUG_MSG(SUBSET, nullptr, "Unable to get instruction offsets for %d", next_glyph);
71 total += end_offset - start_offset - (*instruction_end - *instruction_start);
72 /* round2 so short loca will work */
77 *use_short_loca = (total <= 131070);
78 *loca_size = (glyph_ids.len + 1)
79 * (*use_short_loca ? sizeof(OT::HBUINT16) : sizeof(OT::HBUINT32));
81 DEBUG_MSG(SUBSET, nullptr, "preparing to subset glyf: final size %d, loca size %d, using %s loca",
84 *use_short_loca ? "short" : "long");
89 _write_loca_entry (unsigned int id,
93 unsigned int loca_size)
95 unsigned int entry_size = is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32);
96 if ((id + 1) * entry_size <= loca_size)
99 ((OT::HBUINT16*) loca_prime) [id].set (offset / 2);
101 ((OT::HBUINT32*) loca_prime) [id].set (offset);
106 // Offset was not written because the write is out of bounds.
109 "WARNING: Attempted to write an out of bounds loca entry at index %d. Loca size is %d.",
116 _update_components (hb_subset_plan_t * plan,
121 OT::glyf::CompositeGlyphHeader::Iterator iterator;
122 if (OT::glyf::CompositeGlyphHeader::get_iterator (glyph_start,
128 hb_codepoint_t new_gid;
129 if (!hb_subset_plan_new_gid_for_old_id (plan,
130 iterator.current->glyphIndex,
134 ((OT::glyf::CompositeGlyphHeader *) iterator.current)->glyphIndex.set (new_gid);
135 } while (iterator.move_to_next());
139 static bool _remove_composite_instruction_flag(char *glyf_prime, unsigned int length)
141 /* remove WE_HAVE_INSTRUCTIONS from flags in dest */
142 OT::glyf::CompositeGlyphHeader::Iterator composite_it;
143 if (unlikely (!OT::glyf::CompositeGlyphHeader::get_iterator (glyf_prime, length, &composite_it))) return false;
144 const OT::glyf::CompositeGlyphHeader *glyph;
146 glyph = composite_it.current;
147 OT::HBUINT16 *flags = const_cast<OT::HBUINT16 *> (&glyph->flags);
148 flags->set ( (uint16_t) *flags & ~OT::glyf::CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS);
149 } while (composite_it.move_to_next());
154 _write_glyf_and_loca_prime (hb_subset_plan_t *plan,
155 const OT::glyf::accelerator_t &glyf,
156 const char *glyf_data,
158 hb_prealloced_array_t<unsigned int> &instruction_ranges,
159 unsigned int glyf_prime_size,
160 char *glyf_prime_data /* OUT */,
161 unsigned int loca_prime_size,
162 char *loca_prime_data /* OUT */)
164 hb_prealloced_array_t<hb_codepoint_t> &glyph_ids = plan->gids_to_retain_sorted;
165 char *glyf_prime_data_next = glyf_prime_data;
168 for (unsigned int i = 0; i < glyph_ids.len; i++)
170 unsigned int start_offset, end_offset;
171 if (unlikely (!(glyf.get_offsets (glyph_ids[i], &start_offset, &end_offset)
172 && glyf.remove_padding(start_offset, &end_offset))))
173 end_offset = start_offset = 0;
174 unsigned int instruction_start = instruction_ranges[i * 2];
175 unsigned int instruction_end = instruction_ranges[i * 2 + 1];
177 int length = end_offset - start_offset - (instruction_end - instruction_start);
178 length += length % 2;
180 if (glyf_prime_data_next + length > glyf_prime_data + glyf_prime_size)
184 "WARNING: Attempted to write an out of bounds glyph entry for gid %d (length %d)",
189 if (instruction_start == instruction_end)
190 memcpy (glyf_prime_data_next, glyf_data + start_offset, length);
193 memcpy (glyf_prime_data_next, glyf_data + start_offset, instruction_start - start_offset);
194 memcpy (glyf_prime_data_next + instruction_start - start_offset, glyf_data + instruction_end, end_offset - instruction_end);
195 /* if the instructions end at the end this was a composite glyph, else simple */
196 if (instruction_end == end_offset)
198 if (unlikely (!_remove_composite_instruction_flag (glyf_prime_data_next, length))) return false;
201 /* zero instruction length, which is just before instruction_start */
202 memset (glyf_prime_data_next + instruction_start - start_offset - 2, 0, 2);
205 success = success && _write_loca_entry (i,
206 glyf_prime_data_next - glyf_prime_data,
210 _update_components (plan, glyf_prime_data_next, length);
212 glyf_prime_data_next += length;
215 success = success && _write_loca_entry (glyph_ids.len,
216 glyf_prime_data_next - glyf_prime_data,
224 _hb_subset_glyf_and_loca (const OT::glyf::accelerator_t &glyf,
225 const char *glyf_data,
226 hb_subset_plan_t *plan,
227 bool *use_short_loca,
228 hb_blob_t **glyf_prime /* OUT */,
229 hb_blob_t **loca_prime /* OUT */)
231 // TODO(grieger): Sanity check allocation size for the new table.
232 hb_prealloced_array_t<hb_codepoint_t> &glyphs_to_retain = plan->gids_to_retain_sorted;
234 unsigned int glyf_prime_size;
235 unsigned int loca_prime_size;
236 hb_prealloced_array_t<unsigned int> instruction_ranges;
237 instruction_ranges.init();
239 if (unlikely (!_calculate_glyf_and_loca_prime_size (glyf,
245 &instruction_ranges))) {
246 instruction_ranges.finish();
250 char *glyf_prime_data = (char *) calloc (1, glyf_prime_size);
251 char *loca_prime_data = (char *) calloc (1, loca_prime_size);
252 if (unlikely (!_write_glyf_and_loca_prime (plan, glyf, glyf_data,
255 glyf_prime_size, glyf_prime_data,
256 loca_prime_size, loca_prime_data))) {
257 free (glyf_prime_data);
258 free (loca_prime_data);
259 instruction_ranges.finish();
262 instruction_ranges.finish();
264 *glyf_prime = hb_blob_create (glyf_prime_data,
266 HB_MEMORY_MODE_READONLY,
269 *loca_prime = hb_blob_create (loca_prime_data,
271 HB_MEMORY_MODE_READONLY,
279 * Subsets the glyph table according to a provided plan.
281 * Return value: subsetted glyf table.
286 hb_subset_glyf_and_loca (hb_subset_plan_t *plan,
287 bool *use_short_loca, /* OUT */
288 hb_blob_t **glyf_prime, /* OUT */
289 hb_blob_t **loca_prime /* OUT */)
291 hb_blob_t *glyf_blob = OT::Sanitizer<OT::glyf>().sanitize (plan->source->reference_table (HB_OT_TAG_glyf));
292 const char *glyf_data = hb_blob_get_data(glyf_blob, nullptr);
294 OT::glyf::accelerator_t glyf;
295 glyf.init(plan->source);
296 bool result = _hb_subset_glyf_and_loca (glyf,
303 hb_blob_destroy (glyf_blob);