Bug 1862332 [wpt PR 42877] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / gfx / harfbuzz / src / hb-ot-cff-common.hh
blob4fdba197ac7d98cecccc01cd9e2080dc23b2d70c
1 /*
2 * Copyright © 2018 Adobe 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
16 * DAMAGE.
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 * Adobe Author(s): Michiharu Ariza
26 #ifndef HB_OT_CFF_COMMON_HH
27 #define HB_OT_CFF_COMMON_HH
29 #include "hb-open-type.hh"
30 #include "hb-bimap.hh"
31 #include "hb-ot-layout-common.hh"
32 #include "hb-cff-interp-dict-common.hh"
33 #include "hb-subset-plan.hh"
35 namespace CFF {
37 using namespace OT;
39 #define CFF_UNDEF_CODE 0xFFFFFFFF
41 using objidx_t = hb_serialize_context_t::objidx_t;
42 using whence_t = hb_serialize_context_t::whence_t;
44 /* utility macro */
45 template<typename Type>
46 static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
47 { return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
49 struct code_pair_t
51 unsigned code;
52 hb_codepoint_t glyph;
56 using str_buff_t = hb_vector_t<unsigned char>;
57 using str_buff_vec_t = hb_vector_t<str_buff_t>;
58 using glyph_to_sid_map_t = hb_vector_t<code_pair_t>;
60 struct length_f_t
62 template <typename Iterable,
63 hb_requires (hb_is_iterable (Iterable))>
64 unsigned operator () (const Iterable &_) const { return hb_len (hb_iter (_)); }
66 unsigned operator () (unsigned _) const { return _; }
68 HB_FUNCOBJ (length_f);
70 /* CFF INDEX */
71 template <typename COUNT>
72 struct CFFIndex
74 unsigned int offset_array_size () const
75 { return offSize * (count + 1); }
77 template <typename Iterable,
78 hb_requires (hb_is_iterable (Iterable))>
79 bool serialize (hb_serialize_context_t *c,
80 const Iterable &iterable,
81 const unsigned *p_data_size = nullptr,
82 unsigned min_off_size = 0)
84 TRACE_SERIALIZE (this);
85 unsigned data_size;
86 if (p_data_size)
87 data_size = *p_data_size;
88 else
89 total_size (iterable, &data_size);
91 auto it = hb_iter (iterable);
92 if (unlikely (!serialize_header (c, +it, data_size, min_off_size))) return_trace (false);
93 unsigned char *ret = c->allocate_size<unsigned char> (data_size, false);
94 if (unlikely (!ret)) return_trace (false);
95 for (const auto &_ : +it)
97 unsigned len = _.length;
98 if (!len)
99 continue;
100 if (len <= 1)
102 *ret++ = *_.arrayZ;
103 continue;
105 hb_memcpy (ret, _.arrayZ, len);
106 ret += len;
108 return_trace (true);
111 template <typename Iterator,
112 hb_requires (hb_is_iterator (Iterator))>
113 bool serialize_header (hb_serialize_context_t *c,
114 Iterator it,
115 unsigned data_size,
116 unsigned min_off_size = 0)
118 TRACE_SERIALIZE (this);
120 unsigned off_size = (hb_bit_storage (data_size + 1) + 7) / 8;
121 off_size = hb_max(min_off_size, off_size);
123 /* serialize CFFIndex header */
124 if (unlikely (!c->extend_min (this))) return_trace (false);
125 this->count = hb_len (it);
126 if (!this->count) return_trace (true);
127 if (unlikely (!c->extend (this->offSize))) return_trace (false);
128 this->offSize = off_size;
129 if (unlikely (!c->allocate_size<HBUINT8> (off_size * (this->count + 1), false)))
130 return_trace (false);
132 /* serialize indices */
133 unsigned int offset = 1;
134 if (HB_OPTIMIZE_SIZE_VAL)
136 unsigned int i = 0;
137 for (const auto &_ : +it)
139 set_offset_at (i++, offset);
140 offset += length_f (_);
142 set_offset_at (i, offset);
144 else
145 switch (off_size)
147 case 1:
149 HBUINT8 *p = (HBUINT8 *) offsets;
150 for (const auto &_ : +it)
152 *p++ = offset;
153 offset += length_f (_);
155 *p = offset;
157 break;
158 case 2:
160 HBUINT16 *p = (HBUINT16 *) offsets;
161 for (const auto &_ : +it)
163 *p++ = offset;
164 offset += length_f (_);
166 *p = offset;
168 break;
169 case 3:
171 HBUINT24 *p = (HBUINT24 *) offsets;
172 for (const auto &_ : +it)
174 *p++ = offset;
175 offset += length_f (_);
177 *p = offset;
179 break;
180 case 4:
182 HBUINT32 *p = (HBUINT32 *) offsets;
183 for (const auto &_ : +it)
185 *p++ = offset;
186 offset += length_f (_);
188 *p = offset;
190 break;
191 default:
192 break;
195 assert (offset == data_size + 1);
196 return_trace (true);
199 template <typename Iterable,
200 hb_requires (hb_is_iterable (Iterable))>
201 static unsigned total_size (const Iterable &iterable, unsigned *data_size = nullptr, unsigned min_off_size = 0)
203 auto it = + hb_iter (iterable);
204 if (!it)
206 if (data_size) *data_size = 0;
207 return min_size;
210 unsigned total = 0;
211 for (const auto &_ : +it)
212 total += length_f (_);
214 if (data_size) *data_size = total;
216 unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
217 off_size = hb_max(min_off_size, off_size);
219 return min_size + HBUINT8::static_size + (hb_len (it) + 1) * off_size + total;
222 void set_offset_at (unsigned int index, unsigned int offset)
224 assert (index <= count);
226 unsigned int size = offSize;
227 const HBUINT8 *p = offsets;
228 switch (size)
230 case 1: ((HBUINT8 *) p)[index] = offset; break;
231 case 2: ((HBUINT16 *) p)[index] = offset; break;
232 case 3: ((HBUINT24 *) p)[index] = offset; break;
233 case 4: ((HBUINT32 *) p)[index] = offset; break;
234 default: return;
238 private:
239 unsigned int offset_at (unsigned int index) const
241 assert (index <= count);
243 unsigned int size = offSize;
244 const HBUINT8 *p = offsets;
245 switch (size)
247 case 1: return ((HBUINT8 *) p)[index];
248 case 2: return ((HBUINT16 *) p)[index];
249 case 3: return ((HBUINT24 *) p)[index];
250 case 4: return ((HBUINT32 *) p)[index];
251 default: return 0;
255 const unsigned char *data_base () const
256 { return (const unsigned char *) this + min_size + offSize.static_size - 1 + offset_array_size (); }
257 public:
259 hb_ubytes_t operator [] (unsigned int index) const
261 if (unlikely (index >= count)) return hb_ubytes_t ();
262 _hb_compiler_memory_r_barrier ();
263 unsigned offset0 = offset_at (index);
264 unsigned offset1 = offset_at (index + 1);
265 if (unlikely (offset1 < offset0 || offset1 > offset_at (count)))
266 return hb_ubytes_t ();
267 return hb_ubytes_t (data_base () + offset0, offset1 - offset0);
270 unsigned int get_size () const
272 if (count)
273 return min_size + offSize.static_size + offset_array_size () + (offset_at (count) - 1);
274 return min_size; /* empty CFFIndex contains count only */
277 bool sanitize (hb_sanitize_context_t *c) const
279 TRACE_SANITIZE (this);
280 return_trace (likely (c->check_struct (this) &&
281 hb_barrier () &&
282 (count == 0 || /* empty INDEX */
283 (count < count + 1u &&
284 hb_barrier () &&
285 c->check_struct (&offSize) && offSize >= 1 && offSize <= 4 &&
286 c->check_array (offsets, offSize, count + 1u) &&
287 c->check_array ((const HBUINT8*) data_base (), 1, offset_at (count))))));
290 public:
291 COUNT count; /* Number of object data. Note there are (count+1) offsets */
292 private:
293 HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
294 HBUINT8 offsets[HB_VAR_ARRAY];
295 /* The array of (count + 1) offsets into objects array (1-base). */
296 /* HBUINT8 data[HB_VAR_ARRAY]; Object data */
297 public:
298 DEFINE_SIZE_MIN (COUNT::static_size);
301 /* Top Dict, Font Dict, Private Dict */
302 struct Dict : UnsizedByteStr
304 template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts>
305 bool serialize (hb_serialize_context_t *c,
306 const DICTVAL &dictval,
307 OP_SERIALIZER& opszr,
308 Ts&&... ds)
310 TRACE_SERIALIZE (this);
311 for (unsigned int i = 0; i < dictval.get_count (); i++)
312 if (unlikely (!opszr.serialize (c, dictval[i], std::forward<Ts> (ds)...)))
313 return_trace (false);
315 return_trace (true);
318 template <typename T, typename V>
319 static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp)
321 if (unlikely ((!serialize_int<T, V> (c, intOp, value))))
322 return false;
324 TRACE_SERIALIZE (this);
325 /* serialize the opcode */
326 HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op), false);
327 if (unlikely (!p)) return_trace (false);
328 if (Is_OpCode_ESC (op))
330 *p = OpCode_escape;
331 op = Unmake_OpCode_ESC (op);
332 p++;
334 *p = op;
335 return_trace (true);
338 template <typename V>
339 static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value)
340 { return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); }
342 template <typename V>
343 static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value)
344 { return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); }
346 template <typename T, int int_op>
347 static bool serialize_link_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence)
349 T &ofs = *(T *) (c->head + OpCode_Size (int_op));
350 if (unlikely (!serialize_int_op<T> (c, op, 0, int_op))) return false;
351 c->add_link (ofs, link, whence);
352 return true;
355 static bool serialize_link4_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
356 { return serialize_link_op<HBINT32, OpCode_longintdict> (c, op, link, whence); }
358 static bool serialize_link2_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
359 { return serialize_link_op<HBINT16, OpCode_shortint> (c, op, link, whence); }
362 struct TopDict : Dict {};
363 struct FontDict : Dict {};
364 struct PrivateDict : Dict {};
366 struct table_info_t
368 void init () { offset = size = 0; link = 0; }
370 unsigned int offset;
371 unsigned int size;
372 objidx_t link;
375 template <typename COUNT>
376 struct FDArray : CFFIndex<COUNT>
378 template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
379 bool serialize (hb_serialize_context_t *c,
380 Iterator it,
381 OP_SERIALIZER& opszr)
383 TRACE_SERIALIZE (this);
385 /* serialize INDEX data */
386 hb_vector_t<unsigned> sizes;
387 if (it.is_random_access_iterator)
388 sizes.alloc (hb_len (it));
390 c->push ();
391 char *data_base = c->head;
392 + it
393 | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
395 FontDict *dict = c->start_embed<FontDict> ();
396 dict->serialize (c, _.first, opszr, _.second);
397 return c->head - (const char*)dict;
399 | hb_sink (sizes)
401 unsigned data_size = c->head - data_base;
402 c->pop_pack (false);
404 if (unlikely (sizes.in_error ())) return_trace (false);
406 /* It just happens that the above is packed right after the header below.
407 * Such a hack. */
409 /* serialize INDEX header */
410 return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes), data_size));
414 /* FDSelect */
415 struct FDSelect0 {
416 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
418 TRACE_SANITIZE (this);
419 if (unlikely (!(c->check_struct (this))))
420 return_trace (false);
421 hb_barrier ();
422 if (unlikely (!c->check_array (fds, c->get_num_glyphs ())))
423 return_trace (false);
425 return_trace (true);
428 unsigned get_fd (hb_codepoint_t glyph) const
429 { return fds[glyph]; }
431 hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
432 { return {fds[glyph], glyph + 1}; }
434 unsigned int get_size (unsigned int num_glyphs) const
435 { return HBUINT8::static_size * num_glyphs; }
437 HBUINT8 fds[HB_VAR_ARRAY];
439 DEFINE_SIZE_MIN (0);
442 template <typename GID_TYPE, typename FD_TYPE>
443 struct FDSelect3_4_Range
445 bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
447 TRACE_SANITIZE (this);
448 return_trace (c->check_struct (this) &&
449 hb_barrier () &&
450 first < c->get_num_glyphs () && (fd < fdcount));
453 GID_TYPE first;
454 FD_TYPE fd;
455 public:
456 DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
459 template <typename GID_TYPE, typename FD_TYPE>
460 struct FDSelect3_4
462 unsigned int get_size () const
463 { return GID_TYPE::static_size * 2 + ranges.get_size (); }
465 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
467 TRACE_SANITIZE (this);
468 if (unlikely (!(c->check_struct (this) &&
469 ranges.sanitize (c, nullptr, fdcount) &&
470 hb_barrier () &&
471 (nRanges () != 0) &&
472 ranges[0].first == 0)))
473 return_trace (false);
475 for (unsigned int i = 1; i < nRanges (); i++)
476 if (unlikely (ranges[i - 1].first >= ranges[i].first))
477 return_trace (false);
479 if (unlikely (!(sentinel().sanitize (c) &&
480 hb_barrier () &&
481 (sentinel() == c->get_num_glyphs ()))))
482 return_trace (false);
484 return_trace (true);
487 static int _cmp_range (const void *_key, const void *_item)
489 hb_codepoint_t glyph = * (hb_codepoint_t *) _key;
490 FDSelect3_4_Range<GID_TYPE, FD_TYPE> *range = (FDSelect3_4_Range<GID_TYPE, FD_TYPE> *) _item;
492 if (glyph < range[0].first) return -1;
493 if (glyph < range[1].first) return 0;
494 return +1;
497 unsigned get_fd (hb_codepoint_t glyph) const
499 auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
500 return range ? range->fd : ranges[nRanges () - 1].fd;
503 hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
505 auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
506 unsigned fd = range ? range->fd : ranges[nRanges () - 1].fd;
507 hb_codepoint_t end = range ? range[1].first : ranges[nRanges () - 1].first;
508 return {fd, end};
511 GID_TYPE &nRanges () { return ranges.len; }
512 GID_TYPE nRanges () const { return ranges.len; }
513 GID_TYPE &sentinel () { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
514 const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
516 ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
517 /* GID_TYPE sentinel */
519 DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
522 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
523 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
525 struct FDSelect
527 bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
529 TRACE_SERIALIZE (this);
530 unsigned int size = src.get_size (num_glyphs);
531 FDSelect *dest = c->allocate_size<FDSelect> (size, false);
532 if (unlikely (!dest)) return_trace (false);
533 hb_memcpy (dest, &src, size);
534 return_trace (true);
537 unsigned int get_size (unsigned int num_glyphs) const
539 switch (format)
541 case 0: return format.static_size + u.format0.get_size (num_glyphs);
542 case 3: return format.static_size + u.format3.get_size ();
543 default:return 0;
547 unsigned get_fd (hb_codepoint_t glyph) const
549 if (this == &Null (FDSelect)) return 0;
551 switch (format)
553 case 0: return u.format0.get_fd (glyph);
554 case 3: return u.format3.get_fd (glyph);
555 default:return 0;
558 /* Returns pair of fd and one after last glyph in range. */
559 hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
561 if (this == &Null (FDSelect)) return {0, 1};
563 switch (format)
565 case 0: return u.format0.get_fd_range (glyph);
566 case 3: return u.format3.get_fd_range (glyph);
567 default:return {0, 1};
571 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
573 TRACE_SANITIZE (this);
574 if (unlikely (!c->check_struct (this)))
575 return_trace (false);
576 hb_barrier ();
578 switch (format)
580 case 0: return_trace (u.format0.sanitize (c, fdcount));
581 case 3: return_trace (u.format3.sanitize (c, fdcount));
582 default:return_trace (false);
586 HBUINT8 format;
587 union {
588 FDSelect0 format0;
589 FDSelect3 format3;
590 } u;
591 public:
592 DEFINE_SIZE_MIN (1);
595 template <typename COUNT>
596 struct Subrs : CFFIndex<COUNT>
598 typedef COUNT count_type;
599 typedef CFFIndex<COUNT> SUPER;
602 } /* namespace CFF */
604 #endif /* HB_OT_CFF_COMMON_HH */