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
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"
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;
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); }
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>;
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);
71 template <typename COUNT>
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)
83 TRACE_SERIALIZE (this);
86 data_size = *p_data_size;
88 total_size (iterable, &data_size);
90 auto it = hb_iter (iterable);
91 if (unlikely (!serialize_header (c, +it, data_size))) return_trace (false);
92 unsigned char *ret = c->allocate_size<unsigned char> (data_size, false);
93 if (unlikely (!ret)) return_trace (false);
94 for (const auto &_ : +it)
96 unsigned len = _.length;
104 hb_memcpy (ret, _.arrayZ, len);
110 template <typename Iterator,
111 hb_requires (hb_is_iterator (Iterator))>
112 bool serialize_header (hb_serialize_context_t *c,
116 TRACE_SERIALIZE (this);
118 unsigned off_size = (hb_bit_storage (data_size + 1) + 7) / 8;
120 /* serialize CFFIndex header */
121 if (unlikely (!c->extend_min (this))) return_trace (false);
122 this->count = hb_len (it);
123 if (!this->count) return_trace (true);
124 if (unlikely (!c->extend (this->offSize))) return_trace (false);
125 this->offSize = off_size;
126 if (unlikely (!c->allocate_size<HBUINT8> (off_size * (this->count + 1), false)))
127 return_trace (false);
129 /* serialize indices */
130 unsigned int offset = 1;
131 if (HB_OPTIMIZE_SIZE_VAL)
134 for (const auto &_ : +it)
136 set_offset_at (i++, offset);
137 offset += length_f (_);
139 set_offset_at (i, offset);
146 HBUINT8 *p = (HBUINT8 *) offsets;
147 for (const auto &_ : +it)
150 offset += length_f (_);
157 HBUINT16 *p = (HBUINT16 *) offsets;
158 for (const auto &_ : +it)
161 offset += length_f (_);
168 HBUINT24 *p = (HBUINT24 *) offsets;
169 for (const auto &_ : +it)
172 offset += length_f (_);
179 HBUINT32 *p = (HBUINT32 *) offsets;
180 for (const auto &_ : +it)
183 offset += length_f (_);
192 assert (offset == data_size + 1);
196 template <typename Iterable,
197 hb_requires (hb_is_iterable (Iterable))>
198 static unsigned total_size (const Iterable &iterable, unsigned *data_size = nullptr)
200 auto it = + hb_iter (iterable);
203 if (data_size) *data_size = 0;
208 for (const auto &_ : +it)
209 total += length_f (_);
211 if (data_size) *data_size = total;
213 unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
215 return min_size + HBUINT8::static_size + (hb_len (it) + 1) * off_size + total;
218 void set_offset_at (unsigned int index, unsigned int offset)
220 assert (index <= count);
222 unsigned int size = offSize;
223 const HBUINT8 *p = offsets;
226 case 1: ((HBUINT8 *) p)[index] = offset; break;
227 case 2: ((HBUINT16 *) p)[index] = offset; break;
228 case 3: ((HBUINT24 *) p)[index] = offset; break;
229 case 4: ((HBUINT32 *) p)[index] = offset; break;
235 unsigned int offset_at (unsigned int index) const
237 assert (index <= count);
239 unsigned int size = offSize;
240 const HBUINT8 *p = offsets;
243 case 1: return ((HBUINT8 *) p)[index];
244 case 2: return ((HBUINT16 *) p)[index];
245 case 3: return ((HBUINT24 *) p)[index];
246 case 4: return ((HBUINT32 *) p)[index];
251 const unsigned char *data_base () const
252 { return (const unsigned char *) this + min_size + offSize.static_size - 1 + offset_array_size (); }
255 hb_ubytes_t operator [] (unsigned int index) const
257 if (unlikely (index >= count)) return hb_ubytes_t ();
258 _hb_compiler_memory_r_barrier ();
259 unsigned offset0 = offset_at (index);
260 unsigned offset1 = offset_at (index + 1);
261 if (unlikely (offset1 < offset0 || offset1 > offset_at (count)))
262 return hb_ubytes_t ();
263 return hb_ubytes_t (data_base () + offset0, offset1 - offset0);
266 unsigned int get_size () const
269 return min_size + offSize.static_size + offset_array_size () + (offset_at (count) - 1);
270 return min_size; /* empty CFFIndex contains count only */
273 bool sanitize (hb_sanitize_context_t *c) const
275 TRACE_SANITIZE (this);
276 return_trace (likely (c->check_struct (this) &&
277 (count == 0 || /* empty INDEX */
278 (count < count + 1u &&
279 c->check_struct (&offSize) && offSize >= 1 && offSize <= 4 &&
280 c->check_array (offsets, offSize, count + 1u) &&
281 c->check_array ((const HBUINT8*) data_base (), 1, offset_at (count))))));
285 COUNT count; /* Number of object data. Note there are (count+1) offsets */
287 HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
288 HBUINT8 offsets[HB_VAR_ARRAY];
289 /* The array of (count + 1) offsets into objects array (1-base). */
290 /* HBUINT8 data[HB_VAR_ARRAY]; Object data */
292 DEFINE_SIZE_MIN (COUNT::static_size);
295 /* Top Dict, Font Dict, Private Dict */
296 struct Dict : UnsizedByteStr
298 template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts>
299 bool serialize (hb_serialize_context_t *c,
300 const DICTVAL &dictval,
301 OP_SERIALIZER& opszr,
304 TRACE_SERIALIZE (this);
305 for (unsigned int i = 0; i < dictval.get_count (); i++)
306 if (unlikely (!opszr.serialize (c, dictval[i], std::forward<Ts> (ds)...)))
307 return_trace (false);
312 template <typename T, typename V>
313 static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp)
315 if (unlikely ((!serialize_int<T, V> (c, intOp, value))))
318 TRACE_SERIALIZE (this);
319 /* serialize the opcode */
320 HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op), false);
321 if (unlikely (!p)) return_trace (false);
322 if (Is_OpCode_ESC (op))
325 op = Unmake_OpCode_ESC (op);
332 template <typename V>
333 static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value)
334 { return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); }
336 template <typename V>
337 static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value)
338 { return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); }
340 template <typename T, int int_op>
341 static bool serialize_link_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence)
343 T &ofs = *(T *) (c->head + OpCode_Size (int_op));
344 if (unlikely (!serialize_int_op<T> (c, op, 0, int_op))) return false;
345 c->add_link (ofs, link, whence);
349 static bool serialize_link4_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
350 { return serialize_link_op<HBINT32, OpCode_longintdict> (c, op, link, whence); }
352 static bool serialize_link2_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
353 { return serialize_link_op<HBINT16, OpCode_shortint> (c, op, link, whence); }
356 struct TopDict : Dict {};
357 struct FontDict : Dict {};
358 struct PrivateDict : Dict {};
362 void init () { offset = size = 0; link = 0; }
369 template <typename COUNT>
370 struct FDArray : CFFIndex<COUNT>
372 template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
373 bool serialize (hb_serialize_context_t *c,
375 OP_SERIALIZER& opszr)
377 TRACE_SERIALIZE (this);
379 /* serialize INDEX data */
380 hb_vector_t<unsigned> sizes;
381 if (it.is_random_access_iterator)
382 sizes.alloc (hb_len (it));
385 char *data_base = c->head;
387 | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
389 FontDict *dict = c->start_embed<FontDict> ();
390 dict->serialize (c, _.first, opszr, _.second);
391 return c->head - (const char*)dict;
395 unsigned data_size = c->head - data_base;
398 if (unlikely (sizes.in_error ())) return_trace (false);
400 /* It just happens that the above is packed right after the header below.
403 /* serialize INDEX header */
404 return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes), data_size));
410 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
412 TRACE_SANITIZE (this);
413 if (unlikely (!(c->check_struct (this))))
414 return_trace (false);
415 if (unlikely (!c->check_array (fds, c->get_num_glyphs ())))
416 return_trace (false);
421 unsigned get_fd (hb_codepoint_t glyph) const
422 { return fds[glyph]; }
424 hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
425 { return {fds[glyph], glyph + 1}; }
427 unsigned int get_size (unsigned int num_glyphs) const
428 { return HBUINT8::static_size * num_glyphs; }
430 HBUINT8 fds[HB_VAR_ARRAY];
435 template <typename GID_TYPE, typename FD_TYPE>
436 struct FDSelect3_4_Range
438 bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
440 TRACE_SANITIZE (this);
441 return_trace (first < c->get_num_glyphs () && (fd < fdcount));
447 DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
450 template <typename GID_TYPE, typename FD_TYPE>
453 unsigned int get_size () const
454 { return GID_TYPE::static_size * 2 + ranges.get_size (); }
456 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
458 TRACE_SANITIZE (this);
459 if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) ||
460 (nRanges () == 0) || ranges[0].first != 0))
461 return_trace (false);
463 for (unsigned int i = 1; i < nRanges (); i++)
464 if (unlikely (ranges[i - 1].first >= ranges[i].first))
465 return_trace (false);
467 if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
468 return_trace (false);
473 static int _cmp_range (const void *_key, const void *_item)
475 hb_codepoint_t glyph = * (hb_codepoint_t *) _key;
476 FDSelect3_4_Range<GID_TYPE, FD_TYPE> *range = (FDSelect3_4_Range<GID_TYPE, FD_TYPE> *) _item;
478 if (glyph < range[0].first) return -1;
479 if (glyph < range[1].first) return 0;
483 unsigned get_fd (hb_codepoint_t glyph) const
485 auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
486 return range ? range->fd : ranges[nRanges () - 1].fd;
489 hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
491 auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
492 unsigned fd = range ? range->fd : ranges[nRanges () - 1].fd;
493 hb_codepoint_t end = range ? range[1].first : ranges[nRanges () - 1].first;
497 GID_TYPE &nRanges () { return ranges.len; }
498 GID_TYPE nRanges () const { return ranges.len; }
499 GID_TYPE &sentinel () { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
500 const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
502 ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
503 /* GID_TYPE sentinel */
505 DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
508 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
509 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
513 bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
515 TRACE_SERIALIZE (this);
516 unsigned int size = src.get_size (num_glyphs);
517 FDSelect *dest = c->allocate_size<FDSelect> (size, false);
518 if (unlikely (!dest)) return_trace (false);
519 hb_memcpy (dest, &src, size);
523 unsigned int get_size (unsigned int num_glyphs) const
527 case 0: return format.static_size + u.format0.get_size (num_glyphs);
528 case 3: return format.static_size + u.format3.get_size ();
533 unsigned get_fd (hb_codepoint_t glyph) const
535 if (this == &Null (FDSelect)) return 0;
539 case 0: return u.format0.get_fd (glyph);
540 case 3: return u.format3.get_fd (glyph);
544 /* Returns pair of fd and one after last glyph in range. */
545 hb_pair_t<unsigned, hb_codepoint_t> get_fd_range (hb_codepoint_t glyph) const
547 if (this == &Null (FDSelect)) return {0, 1};
551 case 0: return u.format0.get_fd_range (glyph);
552 case 3: return u.format3.get_fd_range (glyph);
553 default:return {0, 1};
557 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
559 TRACE_SANITIZE (this);
560 if (unlikely (!c->check_struct (this)))
561 return_trace (false);
565 case 0: return_trace (u.format0.sanitize (c, fdcount));
566 case 3: return_trace (u.format3.sanitize (c, fdcount));
567 default:return_trace (false);
580 template <typename COUNT>
581 struct Subrs : CFFIndex<COUNT>
583 typedef COUNT count_type;
584 typedef CFFIndex<COUNT> SUPER;
587 } /* namespace CFF */
589 #endif /* HB_OT_CFF_COMMON_HH */