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.hh"
28 #include "hb-ot-glyf-table.hh"
30 #include "hb-subset-glyf.hh"
33 _calculate_glyf_and_loca_prime_size (const OT::glyf::accelerator_t &glyf,
34 hb_vector_t<hb_codepoint_t> &glyph_ids,
36 bool *use_short_loca /* OUT */,
37 unsigned int *glyf_size /* OUT */,
38 unsigned int *loca_size /* OUT */,
39 hb_vector_t<unsigned int> *instruction_ranges /* OUT */)
41 unsigned int total = 0;
42 for (unsigned int i = 0; i < glyph_ids.length; i++)
44 hb_codepoint_t next_glyph = glyph_ids[i];
45 if (!instruction_ranges->resize (instruction_ranges->length + 2))
47 DEBUG_MSG(SUBSET, nullptr, "Failed to resize instruction_ranges.");
50 unsigned int *instruction_start = &(*instruction_ranges)[instruction_ranges->length - 2];
51 *instruction_start = 0;
52 unsigned int *instruction_end = &(*instruction_ranges)[instruction_ranges->length - 1];
55 unsigned int start_offset, end_offset;
56 if (unlikely (!(glyf.get_offsets (next_glyph, &start_offset, &end_offset) &&
57 glyf.remove_padding (start_offset, &end_offset))))
59 DEBUG_MSG(SUBSET, nullptr, "Invalid gid %d", next_glyph);
62 if (end_offset - start_offset < OT::glyf::GlyphHeader::static_size)
63 continue; /* 0-length glyph */
67 if (unlikely (!glyf.get_instruction_offsets (start_offset, end_offset,
68 instruction_start, instruction_end)))
70 DEBUG_MSG(SUBSET, nullptr, "Unable to get instruction offsets for %d", next_glyph);
75 total += end_offset - start_offset - (*instruction_end - *instruction_start);
76 /* round2 so short loca will work */
81 *use_short_loca = (total <= 131070);
82 *loca_size = (glyph_ids.length + 1)
83 * (*use_short_loca ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32));
85 DEBUG_MSG(SUBSET, nullptr, "preparing to subset glyf: final size %d, loca size %d, using %s loca",
88 *use_short_loca ? "short" : "long");
93 _write_loca_entry (unsigned int id,
97 unsigned int loca_size)
99 unsigned int entry_size = is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32);
100 if ((id + 1) * entry_size <= loca_size)
103 ((OT::HBUINT16*) loca_prime) [id].set (offset / 2);
105 ((OT::HBUINT32*) loca_prime) [id].set (offset);
110 // Offset was not written because the write is out of bounds.
113 "WARNING: Attempted to write an out of bounds loca entry at index %d. Loca size is %d.",
120 _update_components (hb_subset_plan_t * plan,
124 OT::glyf::CompositeGlyphHeader::Iterator iterator;
125 if (OT::glyf::CompositeGlyphHeader::get_iterator (glyph_start,
131 hb_codepoint_t new_gid;
132 if (!plan->new_gid_for_old_gid (iterator.current->glyphIndex,
136 ((OT::glyf::CompositeGlyphHeader *) iterator.current)->glyphIndex.set (new_gid);
137 } while (iterator.move_to_next ());
141 static bool _remove_composite_instruction_flag (char *glyf_prime, unsigned int length)
143 /* remove WE_HAVE_INSTRUCTIONS from flags in dest */
144 OT::glyf::CompositeGlyphHeader::Iterator composite_it;
145 if (unlikely (!OT::glyf::CompositeGlyphHeader::get_iterator (glyf_prime, length, &composite_it))) return false;
146 const OT::glyf::CompositeGlyphHeader *glyph;
148 glyph = composite_it.current;
149 OT::HBUINT16 *flags = const_cast<OT::HBUINT16 *> (&glyph->flags);
150 flags->set ( (uint16_t) *flags & ~OT::glyf::CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS);
151 } while (composite_it.move_to_next ());
156 _write_glyf_and_loca_prime (hb_subset_plan_t *plan,
157 const OT::glyf::accelerator_t &glyf,
158 const char *glyf_data,
160 hb_vector_t<unsigned int> &instruction_ranges,
161 unsigned int glyf_prime_size,
162 char *glyf_prime_data /* OUT */,
163 unsigned int loca_prime_size,
164 char *loca_prime_data /* OUT */)
166 hb_vector_t<hb_codepoint_t> &glyph_ids = plan->glyphs;
167 char *glyf_prime_data_next = glyf_prime_data;
170 for (unsigned int i = 0; i < glyph_ids.length; i++)
172 unsigned int start_offset, end_offset;
173 if (unlikely (!(glyf.get_offsets (glyph_ids[i], &start_offset, &end_offset) &&
174 glyf.remove_padding (start_offset, &end_offset))))
175 end_offset = start_offset = 0;
177 unsigned int instruction_start = instruction_ranges[i * 2];
178 unsigned int instruction_end = instruction_ranges[i * 2 + 1];
180 int length = end_offset - start_offset - (instruction_end - instruction_start);
182 if (glyf_prime_data_next + length > glyf_prime_data + glyf_prime_size)
186 "WARNING: Attempted to write an out of bounds glyph entry for gid %d (length %d)",
191 if (instruction_start == instruction_end)
192 memcpy (glyf_prime_data_next, glyf_data + start_offset, length);
195 memcpy (glyf_prime_data_next, glyf_data + start_offset, instruction_start - start_offset);
196 memcpy (glyf_prime_data_next + instruction_start - start_offset, glyf_data + instruction_end, end_offset - instruction_end);
197 /* if the instructions end at the end this was a composite glyph, else simple */
198 if (instruction_end == end_offset)
200 if (unlikely (!_remove_composite_instruction_flag (glyf_prime_data_next, length))) return false;
203 /* zero instruction length, which is just before instruction_start */
204 memset (glyf_prime_data_next + instruction_start - start_offset - 2, 0, 2);
207 success = success && _write_loca_entry (i,
208 glyf_prime_data_next - glyf_prime_data,
212 _update_components (plan, glyf_prime_data_next, length);
214 // TODO: don't align to two bytes if using long loca.
215 glyf_prime_data_next += length + (length % 2); // Align to 2 bytes for short loca.
218 success = success && _write_loca_entry (glyph_ids.length,
219 glyf_prime_data_next - glyf_prime_data,
227 _hb_subset_glyf_and_loca (const OT::glyf::accelerator_t &glyf,
228 const char *glyf_data,
229 hb_subset_plan_t *plan,
230 bool *use_short_loca,
231 hb_blob_t **glyf_prime /* OUT */,
232 hb_blob_t **loca_prime /* OUT */)
234 // TODO(grieger): Sanity check allocation size for the new table.
235 hb_vector_t<hb_codepoint_t> &glyphs_to_retain = plan->glyphs;
237 unsigned int glyf_prime_size;
238 unsigned int loca_prime_size;
239 hb_vector_t<unsigned int> instruction_ranges;
240 instruction_ranges.init ();
242 if (unlikely (!_calculate_glyf_and_loca_prime_size (glyf,
248 &instruction_ranges))) {
249 instruction_ranges.fini ();
253 char *glyf_prime_data = (char *) calloc (1, glyf_prime_size);
254 char *loca_prime_data = (char *) calloc (1, loca_prime_size);
255 if (unlikely (!_write_glyf_and_loca_prime (plan, glyf, glyf_data,
258 glyf_prime_size, glyf_prime_data,
259 loca_prime_size, loca_prime_data))) {
260 free (glyf_prime_data);
261 free (loca_prime_data);
262 instruction_ranges.fini ();
265 instruction_ranges.fini ();
267 *glyf_prime = hb_blob_create (glyf_prime_data,
269 HB_MEMORY_MODE_READONLY,
272 *loca_prime = hb_blob_create (loca_prime_data,
274 HB_MEMORY_MODE_READONLY,
282 * Subsets the glyph table according to a provided plan.
284 * Return value: subsetted glyf table.
289 hb_subset_glyf_and_loca (hb_subset_plan_t *plan,
290 bool *use_short_loca, /* OUT */
291 hb_blob_t **glyf_prime, /* OUT */
292 hb_blob_t **loca_prime /* OUT */)
294 hb_blob_t *glyf_blob = hb_sanitize_context_t ().reference_table<OT::glyf> (plan->source);
295 const char *glyf_data = hb_blob_get_data (glyf_blob, nullptr);
297 OT::glyf::accelerator_t glyf;
298 glyf.init (plan->source);
299 bool result = _hb_subset_glyf_and_loca (glyf,
306 hb_blob_destroy (glyf_blob);