blob: 0647ee4e75414b5c82b7801331c3bfa1bad61fac [file] [log] [blame]
Garret Rieger6a45e5d2018-02-06 16:04:09 -08001/*
2 * Copyright © 2018 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
Rod Sheeter4f074372018-02-21 22:23:05 -080024 * Google Author(s): Garret Rieger, Roderick Sheeter
Garret Rieger6a45e5d2018-02-06 16:04:09 -080025 */
26
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070027#include "hb-open-type.hh"
Garret Rieger53aa0e92018-02-06 17:05:22 -080028#include "hb-ot-glyf-table.hh"
Garret Rieger0a5d1442018-02-07 13:09:54 -080029#include "hb-set.h"
Garret Rieger6a45e5d2018-02-06 16:04:09 -080030#include "hb-subset-glyf.hh"
31
Garret Rieger4b1ac3a2019-01-18 15:11:26 -080032struct loca_data_t
33{
34 bool is_short;
35 void *data;
36 unsigned int size;
37
38 inline bool
39 _write_loca_entry (unsigned int id,
40 unsigned int offset)
41 {
42 unsigned int entry_size = is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32);
43 if ((id + 1) * entry_size <= size)
44 {
45 if (is_short) {
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -070046 ((OT::HBUINT16*) data) [id] = offset / 2;
Garret Rieger4b1ac3a2019-01-18 15:11:26 -080047 } else {
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -070048 ((OT::HBUINT32*) data) [id] = offset;
Garret Rieger4b1ac3a2019-01-18 15:11:26 -080049 }
50 return true;
51 }
52
53 // Offset was not written because the write is out of bounds.
54 DEBUG_MSG(SUBSET,
55 nullptr,
56 "WARNING: Attempted to write an out of bounds loca entry at index %d. Loca size is %d.",
57 id,
58 size);
59 return false;
60 }
Garret Rieger4b1ac3a2019-01-18 15:11:26 -080061};
62
Behdad Esfahbod938de312019-05-07 11:47:02 -070063/*
Garret Rieger4b1ac3a2019-01-18 15:11:26 -080064 * If hints are being dropped find the range which in glyf at which
65 * the hinting instructions are located. Add them to the instruction_ranges
66 * vector.
67 */
68static bool
69_add_instructions_range (const OT::glyf::accelerator_t &glyf,
70 hb_codepoint_t glyph_id,
71 unsigned int glyph_start_offset,
72 unsigned int glyph_end_offset,
73 bool drop_hints,
74 hb_vector_t<unsigned int> *instruction_ranges /* OUT */)
75{
76 if (!instruction_ranges->resize (instruction_ranges->length + 2))
77 {
78 DEBUG_MSG(SUBSET, nullptr, "Failed to resize instruction_ranges.");
79 return false;
80 }
81 unsigned int *instruction_start = &(*instruction_ranges)[instruction_ranges->length - 2];
82 *instruction_start = 0;
83 unsigned int *instruction_end = &(*instruction_ranges)[instruction_ranges->length - 1];
84 *instruction_end = 0;
85
86 if (drop_hints)
87 {
88 if (unlikely (!glyf.get_instruction_offsets (glyph_start_offset, glyph_end_offset,
89 instruction_start, instruction_end)))
90 {
91 DEBUG_MSG(SUBSET, nullptr, "Unable to get instruction offsets for %d", glyph_id);
92 return false;
93 }
94 }
95
96 return true;
97}
98
Ebrahim Byagowi203b6472018-02-11 01:00:03 +033099static bool
Garret Rieger8e9fd6f2018-02-07 19:01:21 -0800100_calculate_glyf_and_loca_prime_size (const OT::glyf::accelerator_t &glyf,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800101 const hb_subset_plan_t *plan,
102 loca_data_t *loca_data, /* OUT */
103 unsigned int *glyf_size /* OUT */,
104 hb_vector_t<unsigned int> *instruction_ranges /* OUT */)
Garret Rieger0a5d1442018-02-07 13:09:54 -0800105{
106 unsigned int total = 0;
Rod Sheeter9bd6d252018-02-23 13:05:58 -0800107
Garret Rieger74c44ff2019-01-28 16:53:01 -0800108 hb_codepoint_t next_glyph = HB_SET_VALUE_INVALID;
109 while (plan->glyphset ()->next (&next_glyph))
110 {
Behdad Esfahbod750d5af2019-05-08 12:01:55 -0700111 unsigned int start_offset = 0, end_offset = 0;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330112 if (unlikely (!(glyf.get_offsets (next_glyph, &start_offset, &end_offset) &&
113 glyf.remove_padding (start_offset, &end_offset))))
Rod Sheeter4f074372018-02-21 22:23:05 -0800114 {
115 DEBUG_MSG(SUBSET, nullptr, "Invalid gid %d", next_glyph);
Garret Rieger96b038f2019-01-18 16:41:08 -0800116 start_offset = end_offset = 0;
Rod Sheeter4f074372018-02-21 22:23:05 -0800117 }
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800118
119 bool is_zero_length = end_offset - start_offset < OT::glyf::GlyphHeader::static_size;
120 if (!_add_instructions_range (glyf,
121 next_glyph,
122 start_offset,
123 end_offset,
124 plan->drop_hints && !is_zero_length,
125 instruction_ranges))
126 return false;
127
128 if (is_zero_length)
Rod Sheeter9bd6d252018-02-23 13:05:58 -0800129 continue; /* 0-length glyph */
130
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800131 total += end_offset - start_offset
132 - ((*instruction_ranges)[instruction_ranges->length - 1]
133 - (*instruction_ranges)[instruction_ranges->length - 2]);
Rod Sheeter9bd6d252018-02-23 13:05:58 -0800134 /* round2 so short loca will work */
135 total += total % 2;
Garret Rieger0a5d1442018-02-07 13:09:54 -0800136 }
137
Garret Rieger8e9fd6f2018-02-07 19:01:21 -0800138 *glyf_size = total;
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800139 loca_data->is_short = (total <= 131070);
Garret Rieger74c44ff2019-01-28 16:53:01 -0800140 loca_data->size = (plan->num_output_glyphs () + 1)
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800141 * (loca_data->is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32));
Garret Riegerd18decd2018-02-09 18:41:21 -0800142
143 DEBUG_MSG(SUBSET, nullptr, "preparing to subset glyf: final size %d, loca size %d, using %s loca",
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330144 total,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800145 loca_data->size,
146 loca_data->is_short ? "short" : "long");
Garret Rieger0a5d1442018-02-07 13:09:54 -0800147 return true;
148}
149
Garret Rieger21303922018-02-16 17:01:00 -0700150static void
Garret Riegerb7f97182019-01-17 18:55:56 -0800151_update_components (const hb_subset_plan_t *plan,
152 char *glyph_start,
153 unsigned int length)
Garret Rieger21303922018-02-16 17:01:00 -0700154{
155 OT::glyf::CompositeGlyphHeader::Iterator iterator;
156 if (OT::glyf::CompositeGlyphHeader::get_iterator (glyph_start,
157 length,
158 &iterator))
159 {
160 do
161 {
162 hb_codepoint_t new_gid;
Garret Rieger251cc972018-05-30 12:23:51 -0700163 if (!plan->new_gid_for_old_gid (iterator.current->glyphIndex,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330164 &new_gid))
Garret Rieger21303922018-02-16 17:01:00 -0700165 continue;
166
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700167 ((OT::glyf::CompositeGlyphHeader *) iterator.current)->glyphIndex = new_gid;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330168 } while (iterator.move_to_next ());
Garret Rieger21303922018-02-16 17:01:00 -0700169 }
170}
171
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330172static bool _remove_composite_instruction_flag (char *glyf_prime, unsigned int length)
Rod Sheeter20d57392018-02-28 11:15:08 -0800173{
174 /* remove WE_HAVE_INSTRUCTIONS from flags in dest */
175 OT::glyf::CompositeGlyphHeader::Iterator composite_it;
176 if (unlikely (!OT::glyf::CompositeGlyphHeader::get_iterator (glyf_prime, length, &composite_it))) return false;
177 const OT::glyf::CompositeGlyphHeader *glyph;
178 do {
179 glyph = composite_it.current;
180 OT::HBUINT16 *flags = const_cast<OT::HBUINT16 *> (&glyph->flags);
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700181 *flags = (uint16_t) *flags & ~OT::glyf::CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330182 } while (composite_it.move_to_next ());
Rod Sheeter20d57392018-02-28 11:15:08 -0800183 return true;
184}
185
Ebrahim Byagowi203b6472018-02-11 01:00:03 +0330186static bool
Garret Riegerb7f97182019-01-17 18:55:56 -0800187_write_glyf_and_loca_prime (const hb_subset_plan_t *plan,
Garret Rieger21303922018-02-16 17:01:00 -0700188 const OT::glyf::accelerator_t &glyf,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330189 const char *glyf_data,
Garret Riegerb7f97182019-01-17 18:55:56 -0800190 hb_vector_t<unsigned int> &instruction_ranges,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330191 unsigned int glyf_prime_size,
192 char *glyf_prime_data /* OUT */,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800193 loca_data_t *loca_prime /* OUT */)
Garret Rieger0a5d1442018-02-07 13:09:54 -0800194{
195 char *glyf_prime_data_next = glyf_prime_data;
196
Garret Rieger0ab73e52018-02-20 15:33:03 -0800197 bool success = true;
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800198
Garret Rieger74c44ff2019-01-28 16:53:01 -0800199
200 unsigned int i = 0;
201 hb_codepoint_t new_gid;
202 for (new_gid = 0; new_gid < plan->num_output_glyphs (); new_gid++)
203 {
204 hb_codepoint_t old_gid;
205 if (!plan->old_gid_for_new_gid (new_gid, &old_gid))
206 {
207 // Empty glyph, add a loca entry and carry on.
208 loca_prime->_write_loca_entry (new_gid,
209 glyf_prime_data_next - glyf_prime_data);
210 continue;
211 }
212
Garret Riegerb7f97182019-01-17 18:55:56 -0800213
Behdad Esfahbod750d5af2019-05-08 12:01:55 -0700214 unsigned int start_offset = 0, end_offset = 0;
Garret Rieger74c44ff2019-01-28 16:53:01 -0800215 if (unlikely (!(glyf.get_offsets (old_gid, &start_offset, &end_offset) &&
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330216 glyf.remove_padding (start_offset, &end_offset))))
Behdad Esfahbodfd0bde62018-02-13 16:35:30 -0800217 end_offset = start_offset = 0;
Garret Rieger07851aa2018-03-26 20:56:56 -0600218
Rod Sheeter4f074372018-02-21 22:23:05 -0800219 unsigned int instruction_start = instruction_ranges[i * 2];
220 unsigned int instruction_end = instruction_ranges[i * 2 + 1];
Garret Rieger0a5d1442018-02-07 13:09:54 -0800221
Rod Sheeter4f074372018-02-21 22:23:05 -0800222 int length = end_offset - start_offset - (instruction_end - instruction_start);
Garret Riegera998eee2018-02-20 16:48:52 -0800223
224 if (glyf_prime_data_next + length > glyf_prime_data + glyf_prime_size)
225 {
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330226 DEBUG_MSG(SUBSET,
Garret Rieger96b038f2019-01-18 16:41:08 -0800227 nullptr,
228 "WARNING: Attempted to write an out of bounds glyph entry for gid %d (length %d)",
229 i, length);
Garret Riegera998eee2018-02-20 16:48:52 -0800230 return false;
231 }
Rod Sheeter4f074372018-02-21 22:23:05 -0800232
233 if (instruction_start == instruction_end)
Rod Sheeter4f074372018-02-21 22:23:05 -0800234 memcpy (glyf_prime_data_next, glyf_data + start_offset, length);
Rod Sheeter4f074372018-02-21 22:23:05 -0800235 else
236 {
Rod Sheeter4f074372018-02-21 22:23:05 -0800237 memcpy (glyf_prime_data_next, glyf_data + start_offset, instruction_start - start_offset);
238 memcpy (glyf_prime_data_next + instruction_start - start_offset, glyf_data + instruction_end, end_offset - instruction_end);
Rod Sheeterb3f1a042018-02-28 11:41:24 -0800239 /* if the instructions end at the end this was a composite glyph, else simple */
Rod Sheeter4f074372018-02-21 22:23:05 -0800240 if (instruction_end == end_offset)
Rod Sheeter6836a822018-02-27 20:51:12 -0800241 {
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330242 if (unlikely (!_remove_composite_instruction_flag (glyf_prime_data_next, length))) return false;
Rod Sheeter6836a822018-02-27 20:51:12 -0800243 }
Rod Sheeter4f074372018-02-21 22:23:05 -0800244 else
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330245 /* zero instruction length, which is just before instruction_start */
246 memset (glyf_prime_data_next + instruction_start - start_offset - 2, 0, 2);
Rod Sheeter4f074372018-02-21 22:23:05 -0800247 }
Garret Riegerd18decd2018-02-09 18:41:21 -0800248
Garret Rieger74c44ff2019-01-28 16:53:01 -0800249 success = success && loca_prime->_write_loca_entry (new_gid,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800250 glyf_prime_data_next - glyf_prime_data);
Rod Sheeter4f074372018-02-21 22:23:05 -0800251 _update_components (plan, glyf_prime_data_next, length);
Garret Rieger8e9fd6f2018-02-07 19:01:21 -0800252
Garret Rieger07851aa2018-03-26 20:56:56 -0600253 // TODO: don't align to two bytes if using long loca.
254 glyf_prime_data_next += length + (length % 2); // Align to 2 bytes for short loca.
Garret Rieger74c44ff2019-01-28 16:53:01 -0800255
256 i++;
Garret Rieger0a5d1442018-02-07 13:09:54 -0800257 }
258
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800259 // loca table has n+1 entries where the last entry signifies the end location of the last
260 // glyph.
Garret Rieger74c44ff2019-01-28 16:53:01 -0800261 success = success && loca_prime->_write_loca_entry (new_gid,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800262 glyf_prime_data_next - glyf_prime_data);
Garret Rieger0ab73e52018-02-20 15:33:03 -0800263 return success;
Garret Rieger0a5d1442018-02-07 13:09:54 -0800264}
265
Ebrahim Byagowi203b6472018-02-11 01:00:03 +0330266static bool
Garret Rieger8e9fd6f2018-02-07 19:01:21 -0800267_hb_subset_glyf_and_loca (const OT::glyf::accelerator_t &glyf,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330268 const char *glyf_data,
269 hb_subset_plan_t *plan,
270 bool *use_short_loca,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800271 hb_blob_t **glyf_prime_blob /* OUT */,
272 hb_blob_t **loca_prime_blob /* OUT */)
Garret Rieger4e1abe22018-02-07 13:28:11 -0800273{
Garret Rieger4e1abe22018-02-07 13:28:11 -0800274 // TODO(grieger): Sanity check allocation size for the new table.
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800275 loca_data_t loca_prime;
Garret Rieger4e1abe22018-02-07 13:28:11 -0800276 unsigned int glyf_prime_size;
Behdad Esfahbod5c3112a2018-05-01 19:07:04 -0400277 hb_vector_t<unsigned int> instruction_ranges;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330278 instruction_ranges.init ();
Garret Riegerd18decd2018-02-09 18:41:21 -0800279
Garret Rieger8e9fd6f2018-02-07 19:01:21 -0800280 if (unlikely (!_calculate_glyf_and_loca_prime_size (glyf,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800281 plan,
282 &loca_prime,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330283 &glyf_prime_size,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330284 &instruction_ranges))) {
285 instruction_ranges.fini ();
Garret Rieger4e1abe22018-02-07 13:28:11 -0800286 return false;
287 }
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800288 *use_short_loca = loca_prime.is_short;
Garret Rieger4e1abe22018-02-07 13:28:11 -0800289
Rod Sheeter0ac8c0c2018-02-23 17:43:00 -0800290 char *glyf_prime_data = (char *) calloc (1, glyf_prime_size);
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800291 loca_prime.data = (void *) calloc (1, loca_prime.size);
Garret Rieger21303922018-02-16 17:01:00 -0700292 if (unlikely (!_write_glyf_and_loca_prime (plan, glyf, glyf_data,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330293 instruction_ranges,
294 glyf_prime_size, glyf_prime_data,
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800295 &loca_prime))) {
Garret Rieger4e1abe22018-02-07 13:28:11 -0800296 free (glyf_prime_data);
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800297 free (loca_prime.data);
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330298 instruction_ranges.fini ();
Garret Rieger4e1abe22018-02-07 13:28:11 -0800299 return false;
300 }
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330301 instruction_ranges.fini ();
Garret Rieger4e1abe22018-02-07 13:28:11 -0800302
Garret Rieger4b1ac3a2019-01-18 15:11:26 -0800303 *glyf_prime_blob = hb_blob_create (glyf_prime_data,
304 glyf_prime_size,
305 HB_MEMORY_MODE_READONLY,
306 glyf_prime_data,
307 free);
308 *loca_prime_blob = hb_blob_create ((char *) loca_prime.data,
309 loca_prime.size,
310 HB_MEMORY_MODE_READONLY,
311 loca_prime.data,
312 free);
Garret Rieger4e1abe22018-02-07 13:28:11 -0800313 return true;
314}
315
Garret Rieger6a45e5d2018-02-06 16:04:09 -0800316/**
317 * hb_subset_glyf:
318 * Subsets the glyph table according to a provided plan.
319 *
320 * Return value: subsetted glyf table.
321 *
322 * Since: 1.7.5
323 **/
324bool
Garret Riegerf9c665f2018-02-07 16:53:18 -0800325hb_subset_glyf_and_loca (hb_subset_plan_t *plan,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330326 bool *use_short_loca, /* OUT */
327 hb_blob_t **glyf_prime, /* OUT */
328 hb_blob_t **loca_prime /* OUT */)
Garret Rieger6a45e5d2018-02-06 16:04:09 -0800329{
Behdad Esfahboded7b2e52018-08-01 23:59:09 -0700330 hb_blob_t *glyf_blob = hb_sanitize_context_t ().reference_table<OT::glyf> (plan->source);
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330331 const char *glyf_data = hb_blob_get_data (glyf_blob, nullptr);
Garret Rieger53aa0e92018-02-06 17:05:22 -0800332
Garret Rieger0a5d1442018-02-07 13:09:54 -0800333 OT::glyf::accelerator_t glyf;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330334 glyf.init (plan->source);
Garret Riegerd18decd2018-02-09 18:41:21 -0800335 bool result = _hb_subset_glyf_and_loca (glyf,
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330336 glyf_data,
337 plan,
338 use_short_loca,
339 glyf_prime,
340 loca_prime);
Garret Rieger99967e22018-02-23 15:45:45 -0800341
342 hb_blob_destroy (glyf_blob);
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330343 glyf.fini ();
Garret Rieger4e1abe22018-02-07 13:28:11 -0800344
345 return result;
Garret Rieger6a45e5d2018-02-06 16:04:09 -0800346}