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_vector_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_vector_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 if (!instruction_ranges->resize (instruction_ranges->len + 2))
48 DEBUG_MSG(SUBSET, nullptr, "Failed to resize instruction_ranges.");
51 unsigned int *instruction_start = &(*instruction_ranges)[instruction_ranges->len - 2];
52 *instruction_start = 0;
53 unsigned int *instruction_end = &(*instruction_ranges)[instruction_ranges->len - 1];
56 unsigned int start_offset, end_offset;
57 if (unlikely (!(glyf.get_offsets(next_glyph, &start_offset, &end_offset)
58 && glyf.remove_padding(start_offset, &end_offset))))
60 DEBUG_MSG(SUBSET, nullptr, "Invalid gid %d", next_glyph);
63 if (end_offset - start_offset < OT::glyf::GlyphHeader::static_size)
64 continue; /* 0-length glyph */
68 if (unlikely (!glyf.get_instruction_offsets(start_offset, end_offset,
69 instruction_start, instruction_end)))
71 DEBUG_MSG(SUBSET, nullptr, "Unable to get instruction offsets for %d", next_glyph);
76 total += end_offset - start_offset - (*instruction_end - *instruction_start);
77 /* round2 so short loca will work */
82 *use_short_loca = (total <= 131070);
83 *loca_size = (glyph_ids.len + 1)
84 * (*use_short_loca ? sizeof(OT::HBUINT16) : sizeof(OT::HBUINT32));
86 DEBUG_MSG(SUBSET, nullptr, "preparing to subset glyf: final size %d, loca size %d, using %s loca",
89 *use_short_loca ? "short" : "long");
94 _write_loca_entry (unsigned int id,
98 unsigned int loca_size)
100 unsigned int entry_size = is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32);
101 if ((id + 1) * entry_size <= loca_size)
104 ((OT::HBUINT16*) loca_prime) [id].set (offset / 2);
106 ((OT::HBUINT32*) loca_prime) [id].set (offset);
111 // Offset was not written because the write is out of bounds.
114 "WARNING: Attempted to write an out of bounds loca entry at index %d. Loca size is %d.",
121 _update_components (hb_subset_plan_t * plan,
125 OT::glyf::CompositeGlyphHeader::Iterator iterator;
126 if (OT::glyf::CompositeGlyphHeader::get_iterator (glyph_start,
132 hb_codepoint_t new_gid;
133 if (!plan->new_gid_for_old_gid (iterator.current->glyphIndex,
137 ((OT::glyf::CompositeGlyphHeader *) iterator.current)->glyphIndex.set (new_gid);
138 } while (iterator.move_to_next());
142 static bool _remove_composite_instruction_flag(char *glyf_prime, unsigned int length)
144 /* remove WE_HAVE_INSTRUCTIONS from flags in dest */
145 OT::glyf::CompositeGlyphHeader::Iterator composite_it;
146 if (unlikely (!OT::glyf::CompositeGlyphHeader::get_iterator (glyf_prime, length, &composite_it))) return false;
147 const OT::glyf::CompositeGlyphHeader *glyph;
149 glyph = composite_it.current;
150 OT::HBUINT16 *flags = const_cast<OT::HBUINT16 *> (&glyph->flags);
151 flags->set ( (uint16_t) *flags & ~OT::glyf::CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS);
152 } while (composite_it.move_to_next());
157 _write_glyf_and_loca_prime (hb_subset_plan_t *plan,
158 const OT::glyf::accelerator_t &glyf,
159 const char *glyf_data,
161 hb_vector_t<unsigned int> &instruction_ranges,
162 unsigned int glyf_prime_size,
163 char *glyf_prime_data /* OUT */,
164 unsigned int loca_prime_size,
165 char *loca_prime_data /* OUT */)
167 hb_vector_t<hb_codepoint_t> &glyph_ids = plan->glyphs;
168 char *glyf_prime_data_next = glyf_prime_data;
171 for (unsigned int i = 0; i < glyph_ids.len; i++)
173 unsigned int start_offset, end_offset;
174 if (unlikely (!(glyf.get_offsets (glyph_ids[i], &start_offset, &end_offset)
175 && glyf.remove_padding(start_offset, &end_offset))))
176 end_offset = start_offset = 0;
178 unsigned int instruction_start = instruction_ranges[i * 2];
179 unsigned int instruction_end = instruction_ranges[i * 2 + 1];
181 int length = end_offset - start_offset - (instruction_end - instruction_start);
183 if (glyf_prime_data_next + length > glyf_prime_data + glyf_prime_size)
187 "WARNING: Attempted to write an out of bounds glyph entry for gid %d (length %d)",
192 if (instruction_start == instruction_end)
193 memcpy (glyf_prime_data_next, glyf_data + start_offset, length);
196 memcpy (glyf_prime_data_next, glyf_data + start_offset, instruction_start - start_offset);
197 memcpy (glyf_prime_data_next + instruction_start - start_offset, glyf_data + instruction_end, end_offset - instruction_end);
198 /* if the instructions end at the end this was a composite glyph, else simple */
199 if (instruction_end == end_offset)
201 if (unlikely (!_remove_composite_instruction_flag (glyf_prime_data_next, length))) return false;
204 /* zero instruction length, which is just before instruction_start */
205 memset (glyf_prime_data_next + instruction_start - start_offset - 2, 0, 2);
208 success = success && _write_loca_entry (i,
209 glyf_prime_data_next - glyf_prime_data,
213 _update_components (plan, glyf_prime_data_next, length);
215 // TODO: don't align to two bytes if using long loca.
216 glyf_prime_data_next += length + (length % 2); // Align to 2 bytes for short loca.
219 success = success && _write_loca_entry (glyph_ids.len,
220 glyf_prime_data_next - glyf_prime_data,
228 _hb_subset_glyf_and_loca (const OT::glyf::accelerator_t &glyf,
229 const char *glyf_data,
230 hb_subset_plan_t *plan,
231 bool *use_short_loca,
232 hb_blob_t **glyf_prime /* OUT */,
233 hb_blob_t **loca_prime /* OUT */)
235 // TODO(grieger): Sanity check allocation size for the new table.
236 hb_vector_t<hb_codepoint_t> &glyphs_to_retain = plan->glyphs;
238 unsigned int glyf_prime_size;
239 unsigned int loca_prime_size;
240 hb_vector_t<unsigned int> instruction_ranges;
241 instruction_ranges.init();
243 if (unlikely (!_calculate_glyf_and_loca_prime_size (glyf,
249 &instruction_ranges))) {
250 instruction_ranges.fini();
254 char *glyf_prime_data = (char *) calloc (1, glyf_prime_size);
255 char *loca_prime_data = (char *) calloc (1, loca_prime_size);
256 if (unlikely (!_write_glyf_and_loca_prime (plan, glyf, glyf_data,
259 glyf_prime_size, glyf_prime_data,
260 loca_prime_size, loca_prime_data))) {
261 free (glyf_prime_data);
262 free (loca_prime_data);
263 instruction_ranges.fini();
266 instruction_ranges.fini();
268 *glyf_prime = hb_blob_create (glyf_prime_data,
270 HB_MEMORY_MODE_READONLY,
273 *loca_prime = hb_blob_create (loca_prime_data,
275 HB_MEMORY_MODE_READONLY,
283 * Subsets the glyph table according to a provided plan.
285 * Return value: subsetted glyf table.
290 hb_subset_glyf_and_loca (hb_subset_plan_t *plan,
291 bool *use_short_loca, /* OUT */
292 hb_blob_t **glyf_prime, /* OUT */
293 hb_blob_t **loca_prime /* OUT */)
295 hb_blob_t *glyf_blob = OT::Sanitizer<OT::glyf>().sanitize (plan->source->reference_table (HB_OT_TAG_glyf));
296 const char *glyf_data = hb_blob_get_data(glyf_blob, nullptr);
298 OT::glyf::accelerator_t glyf;
299 glyf.init(plan->source);
300 bool result = _hb_subset_glyf_and_loca (glyf,
307 hb_blob_destroy (glyf_blob);