Imported Upstream version 1.7.6
[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_prealloced_array_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_prealloced_array_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     unsigned int *instruction_start = instruction_ranges->push();
47     unsigned int *instruction_end = instruction_ranges->push();
48     *instruction_start = 0;
49     *instruction_end = 0;
50
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))))
54     {
55       DEBUG_MSG(SUBSET, nullptr, "Invalid gid %d", next_glyph);
56       continue;
57     }
58     if (end_offset - start_offset < OT::glyf::GlyphHeader::static_size)
59       continue; /* 0-length glyph */
60
61     if (drop_hints)
62     {
63       if (unlikely (!glyf.get_instruction_offsets(start_offset, end_offset,
64                                                   instruction_start, instruction_end)))
65       {
66         DEBUG_MSG(SUBSET, nullptr, "Unable to get instruction offsets for %d", next_glyph);
67         return false;
68       }
69     }
70
71     total += end_offset - start_offset - (*instruction_end - *instruction_start);
72     /* round2 so short loca will work */
73     total += total % 2;
74   }
75
76   *glyf_size = total;
77   *use_short_loca = (total <= 131070);
78   *loca_size = (glyph_ids.len + 1)
79       * (*use_short_loca ? sizeof(OT::HBUINT16) : sizeof(OT::HBUINT32));
80
81   DEBUG_MSG(SUBSET, nullptr, "preparing to subset glyf: final size %d, loca size %d, using %s loca",
82             total,
83             *loca_size,
84             *use_short_loca ? "short" : "long");
85   return true;
86 }
87
88 static bool
89 _write_loca_entry (unsigned int  id,
90                    unsigned int  offset,
91                    bool          is_short,
92                    void         *loca_prime,
93                    unsigned int  loca_size)
94 {
95   unsigned int entry_size = is_short ? sizeof (OT::HBUINT16) : sizeof (OT::HBUINT32);
96   if ((id + 1) * entry_size <= loca_size)
97   {
98     if (is_short) {
99       ((OT::HBUINT16*) loca_prime) [id].set (offset / 2);
100     } else {
101       ((OT::HBUINT32*) loca_prime) [id].set (offset);
102     }
103     return true;
104   }
105
106   // Offset was not written because the write is out of bounds.
107   DEBUG_MSG (SUBSET,
108              nullptr,
109              "WARNING: Attempted to write an out of bounds loca entry at index %d. Loca size is %d.",
110              id,
111              loca_size);
112   return false;
113 }
114
115 static void
116 _update_components (hb_subset_plan_t * plan,
117                     char * glyph_start,
118                     unsigned int length)
119
120 {
121   OT::glyf::CompositeGlyphHeader::Iterator iterator;
122   if (OT::glyf::CompositeGlyphHeader::get_iterator (glyph_start,
123                                                     length,
124                                                     &iterator))
125   {
126     do
127     {
128       hb_codepoint_t new_gid;
129       if (!hb_subset_plan_new_gid_for_old_id (plan,
130                                               iterator.current->glyphIndex,
131                                               &new_gid))
132         continue;
133
134       ((OT::glyf::CompositeGlyphHeader *) iterator.current)->glyphIndex.set (new_gid);
135     } while (iterator.move_to_next());
136   }
137 }
138
139 static bool _remove_composite_instruction_flag(char *glyf_prime, unsigned int length)
140 {
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;
145   do {
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());
150   return true;
151 }
152
153 static bool
154 _write_glyf_and_loca_prime (hb_subset_plan_t              *plan,
155                             const OT::glyf::accelerator_t &glyf,
156                             const char                    *glyf_data,
157                             bool                           use_short_loca,
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 */)
163 {
164   hb_prealloced_array_t<hb_codepoint_t> &glyph_ids = plan->gids_to_retain_sorted;
165   char *glyf_prime_data_next = glyf_prime_data;
166
167   bool success = true;
168   for (unsigned int i = 0; i < glyph_ids.len; i++)
169   {
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];
176
177     int length = end_offset - start_offset - (instruction_end - instruction_start);
178     length += length % 2;
179
180     if (glyf_prime_data_next + length > glyf_prime_data + glyf_prime_size)
181     {
182       DEBUG_MSG (SUBSET,
183                  nullptr,
184                  "WARNING: Attempted to write an out of bounds glyph entry for gid %d (length %d)",
185                  i, length);
186       return false;
187     }
188
189     if (instruction_start == instruction_end)
190       memcpy (glyf_prime_data_next, glyf_data + start_offset, length);
191     else
192     {
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)
197       {
198         if (unlikely (!_remove_composite_instruction_flag (glyf_prime_data_next, length))) return false;
199       }
200       else
201         /* zero instruction length, which is just before instruction_start */
202         memset (glyf_prime_data_next + instruction_start - start_offset - 2, 0, 2);
203     }
204
205     success = success && _write_loca_entry (i,
206                                             glyf_prime_data_next - glyf_prime_data,
207                                             use_short_loca,
208                                             loca_prime_data,
209                                             loca_prime_size);
210     _update_components (plan, glyf_prime_data_next, length);
211
212     glyf_prime_data_next += length;
213   }
214
215   success = success && _write_loca_entry (glyph_ids.len,
216                                           glyf_prime_data_next - glyf_prime_data,
217                                           use_short_loca,
218                                           loca_prime_data,
219                                           loca_prime_size);
220   return success;
221 }
222
223 static bool
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 */)
230 {
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;
233
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();
238
239   if (unlikely (!_calculate_glyf_and_loca_prime_size (glyf,
240                                                       glyphs_to_retain,
241                                                       plan->drop_hints,
242                                                       use_short_loca,
243                                                       &glyf_prime_size,
244                                                       &loca_prime_size,
245                                                       &instruction_ranges))) {
246     instruction_ranges.finish();
247     return false;
248   }
249
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,
253                                              *use_short_loca,
254                                              instruction_ranges,
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();
260     return false;
261   }
262   instruction_ranges.finish();
263
264   *glyf_prime = hb_blob_create (glyf_prime_data,
265                                 glyf_prime_size,
266                                 HB_MEMORY_MODE_READONLY,
267                                 glyf_prime_data,
268                                 free);
269   *loca_prime = hb_blob_create (loca_prime_data,
270                                 loca_prime_size,
271                                 HB_MEMORY_MODE_READONLY,
272                                 loca_prime_data,
273                                 free);
274   return true;
275 }
276
277 /**
278  * hb_subset_glyf:
279  * Subsets the glyph table according to a provided plan.
280  *
281  * Return value: subsetted glyf table.
282  *
283  * Since: 1.7.5
284  **/
285 bool
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 */)
290 {
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);
293
294   OT::glyf::accelerator_t glyf;
295   glyf.init(plan->source);
296   bool result = _hb_subset_glyf_and_loca (glyf,
297                                           glyf_data,
298                                           plan,
299                                           use_short_loca,
300                                           glyf_prime,
301                                           loca_prime);
302
303   hb_blob_destroy (glyf_blob);
304   glyf.fini();
305
306   return result;
307 }