c8fa39be807bf2a98dea3f31599a1972bc4703b7
[platform/upstream/harfbuzz.git] / src / hb-subset-glyf.cc
1 /*
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  *
24  * Google Author(s): Garret Rieger, Roderick Sheeter
25  */
26
27 #include "hb-open-type-private.hh"
28 #include "hb-ot-glyf-table.hh"
29 #include "hb-set.h"
30 #include "hb-subset-glyf.hh"
31 #include "hb-subset-plan.hh"
32
33 static bool
34 _calculate_glyf_and_loca_prime_size (const OT::glyf::accelerator_t &glyf,
35                                      hb_vector_t<hb_codepoint_t> &glyph_ids,
36                                      hb_bool_t drop_hints,
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 */)
41 {
42   unsigned int total = 0;
43   for (unsigned int i = 0; i < glyph_ids.len; i++)
44   {
45     hb_codepoint_t next_glyph = glyph_ids[i];
46     if (!instruction_ranges->resize (instruction_ranges->len + 2))
47     {
48       DEBUG_MSG(SUBSET, nullptr, "Failed to resize instruction_ranges.");
49       return false;
50     }
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];
54     *instruction_end = 0;
55
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))))
59     {
60       DEBUG_MSG(SUBSET, nullptr, "Invalid gid %d", next_glyph);
61       continue;
62     }
63     if (end_offset - start_offset < OT::glyf::GlyphHeader::static_size)
64       continue; /* 0-length glyph */
65
66     if (drop_hints)
67     {
68       if (unlikely (!glyf.get_instruction_offsets(start_offset, end_offset,
69                                                   instruction_start, instruction_end)))
70       {
71         DEBUG_MSG(SUBSET, nullptr, "Unable to get instruction offsets for %d", next_glyph);
72         return false;
73       }
74     }
75
76     total += end_offset - start_offset - (*instruction_end - *instruction_start);
77     /* round2 so short loca will work */
78     total += total % 2;
79   }
80
81   *glyf_size = total;
82   *use_short_loca = (total <= 131070);
83   *loca_size = (glyph_ids.len + 1)
84       * (*use_short_loca ? sizeof(OT::HBUINT16) : sizeof(OT::HBUINT32));
85
86   DEBUG_MSG(SUBSET, nullptr, "preparing to subset glyf: final size %d, loca size %d, using %s loca",
87             total,
88             *loca_size,
89             *use_short_loca ? "short" : "long");
90   return true;
91 }
92
93 static bool
94 _write_loca_entry (unsigned int  id,
95                    unsigned int  offset,
96                    bool          is_short,
97                    void         *loca_prime,
98                    unsigned int  loca_size)
99 {
100   unsigned int entry_size = is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32);
101   if ((id + 1) * entry_size <= loca_size)
102   {
103     if (is_short) {
104       ((OT::HBUINT16*) loca_prime) [id].set (offset / 2);
105     } else {
106       ((OT::HBUINT32*) loca_prime) [id].set (offset);
107     }
108     return true;
109   }
110
111   // Offset was not written because the write is out of bounds.
112   DEBUG_MSG (SUBSET,
113              nullptr,
114              "WARNING: Attempted to write an out of bounds loca entry at index %d. Loca size is %d.",
115              id,
116              loca_size);
117   return false;
118 }
119
120 static void
121 _update_components (hb_subset_plan_t * plan,
122                     char * glyph_start,
123                     unsigned int length)
124 {
125   OT::glyf::CompositeGlyphHeader::Iterator iterator;
126   if (OT::glyf::CompositeGlyphHeader::get_iterator (glyph_start,
127                                                     length,
128                                                     &iterator))
129   {
130     do
131     {
132       hb_codepoint_t new_gid;
133       if (!plan->new_gid_for_old_gid (iterator.current->glyphIndex,
134                                       &new_gid))
135         continue;
136
137       ((OT::glyf::CompositeGlyphHeader *) iterator.current)->glyphIndex.set (new_gid);
138     } while (iterator.move_to_next());
139   }
140 }
141
142 static bool _remove_composite_instruction_flag(char *glyf_prime, unsigned int length)
143 {
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;
148   do {
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());
153   return true;
154 }
155
156 static bool
157 _write_glyf_and_loca_prime (hb_subset_plan_t              *plan,
158                             const OT::glyf::accelerator_t &glyf,
159                             const char                    *glyf_data,
160                             bool                           use_short_loca,
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 */)
166 {
167   hb_vector_t<hb_codepoint_t> &glyph_ids = plan->glyphs;
168   char *glyf_prime_data_next = glyf_prime_data;
169
170   bool success = true;
171   for (unsigned int i = 0; i < glyph_ids.len; i++)
172   {
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;
177
178     unsigned int instruction_start = instruction_ranges[i * 2];
179     unsigned int instruction_end = instruction_ranges[i * 2 + 1];
180
181     int length = end_offset - start_offset - (instruction_end - instruction_start);
182
183     if (glyf_prime_data_next + length > glyf_prime_data + glyf_prime_size)
184     {
185       DEBUG_MSG (SUBSET,
186                  nullptr,
187                  "WARNING: Attempted to write an out of bounds glyph entry for gid %d (length %d)",
188                  i, length);
189       return false;
190     }
191
192     if (instruction_start == instruction_end)
193       memcpy (glyf_prime_data_next, glyf_data + start_offset, length);
194     else
195     {
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)
200       {
201         if (unlikely (!_remove_composite_instruction_flag (glyf_prime_data_next, length))) return false;
202       }
203       else
204         /* zero instruction length, which is just before instruction_start */
205         memset (glyf_prime_data_next + instruction_start - start_offset - 2, 0, 2);
206     }
207
208     success = success && _write_loca_entry (i,
209                                             glyf_prime_data_next - glyf_prime_data,
210                                             use_short_loca,
211                                             loca_prime_data,
212                                             loca_prime_size);
213     _update_components (plan, glyf_prime_data_next, length);
214
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.
217   }
218
219   success = success && _write_loca_entry (glyph_ids.len,
220                                           glyf_prime_data_next - glyf_prime_data,
221                                           use_short_loca,
222                                           loca_prime_data,
223                                           loca_prime_size);
224   return success;
225 }
226
227 static bool
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 */)
234 {
235   // TODO(grieger): Sanity check allocation size for the new table.
236   hb_vector_t<hb_codepoint_t> &glyphs_to_retain = plan->glyphs;
237
238   unsigned int glyf_prime_size;
239   unsigned int loca_prime_size;
240   hb_vector_t<unsigned int> instruction_ranges;
241   instruction_ranges.init();
242
243   if (unlikely (!_calculate_glyf_and_loca_prime_size (glyf,
244                                                       glyphs_to_retain,
245                                                       plan->drop_hints,
246                                                       use_short_loca,
247                                                       &glyf_prime_size,
248                                                       &loca_prime_size,
249                                                       &instruction_ranges))) {
250     instruction_ranges.fini();
251     return false;
252   }
253
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,
257                                              *use_short_loca,
258                                              instruction_ranges,
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();
264     return false;
265   }
266   instruction_ranges.fini();
267
268   *glyf_prime = hb_blob_create (glyf_prime_data,
269                                 glyf_prime_size,
270                                 HB_MEMORY_MODE_READONLY,
271                                 glyf_prime_data,
272                                 free);
273   *loca_prime = hb_blob_create (loca_prime_data,
274                                 loca_prime_size,
275                                 HB_MEMORY_MODE_READONLY,
276                                 loca_prime_data,
277                                 free);
278   return true;
279 }
280
281 /**
282  * hb_subset_glyf:
283  * Subsets the glyph table according to a provided plan.
284  *
285  * Return value: subsetted glyf table.
286  *
287  * Since: 1.7.5
288  **/
289 bool
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 */)
294 {
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);
297
298   OT::glyf::accelerator_t glyf;
299   glyf.init(plan->source);
300   bool result = _hb_subset_glyf_and_loca (glyf,
301                                           glyf_data,
302                                           plan,
303                                           use_short_loca,
304                                           glyf_prime,
305                                           loca_prime);
306
307   hb_blob_destroy (glyf_blob);
308   glyf.fini();
309
310   return result;
311 }