2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OPEN_TYPE_HH
30 #define HB_OPEN_TYPE_HH
35 #include "hb-machinery.hh"
36 #include "hb-subset.hh"
44 * The OpenType Font File: Data Types
48 /* "The following data types are used in the OpenType font file.
49 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
55 template <bool is_signed> struct hb_signedness_int;
56 template <> struct hb_signedness_int<false> { typedef unsigned int value; };
57 template <> struct hb_signedness_int<true> { typedef signed int value; };
59 /* Integer types in big-endian order and no alignment requirement */
60 template <typename Type, unsigned int Size>
64 typedef typename hb_signedness_int<hb_is_signed<Type>::value>::value wide_type;
66 void set (wide_type i) { v.set (i); }
67 operator wide_type () const { return v; }
68 bool operator == (const IntType<Type,Size> &o) const { return (Type) v == (Type) o.v; }
69 bool operator != (const IntType<Type,Size> &o) const { return !(*this == o); }
70 static int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); }
71 template <typename Type2>
72 int cmp (Type2 a) const
75 if (sizeof (Type) < sizeof (int) && sizeof (Type2) < sizeof (int))
76 return (int) a - (int) b;
78 return a < b ? -1 : a == b ? 0 : +1;
80 bool sanitize (hb_sanitize_context_t *c) const
82 TRACE_SANITIZE (this);
83 return_trace (likely (c->check_struct (this)));
88 DEFINE_SIZE_STATIC (Size);
91 typedef IntType<uint8_t, 1> HBUINT8; /* 8-bit unsigned integer. */
92 typedef IntType<int8_t, 1> HBINT8; /* 8-bit signed integer. */
93 typedef IntType<uint16_t, 2> HBUINT16; /* 16-bit unsigned integer. */
94 typedef IntType<int16_t, 2> HBINT16; /* 16-bit signed integer. */
95 typedef IntType<uint32_t, 4> HBUINT32; /* 32-bit unsigned integer. */
96 typedef IntType<int32_t, 4> HBINT32; /* 32-bit signed integer. */
97 /* Note: we cannot defined a signed HBINT24 because there's no corresponding C type.
98 * Works for unsigned, but not signed, since we rely on compiler for sign-extension. */
99 typedef IntType<uint32_t, 3> HBUINT24; /* 24-bit unsigned integer. */
101 /* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */
102 typedef HBINT16 FWORD;
104 /* 32-bit signed integer (HBINT32) that describes a quantity in FUnits. */
105 typedef HBINT32 FWORD32;
107 /* 16-bit unsigned integer (HBUINT16) that describes a quantity in FUnits. */
108 typedef HBUINT16 UFWORD;
110 /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
111 struct F2DOT14 : HBINT16
114 float to_float () const { return ((int32_t) v) / 16384.f; }
115 void set_float (float f) { v.set (round (f * 16384.f)); }
117 DEFINE_SIZE_STATIC (2);
120 /* 32-bit signed fixed-point number (16.16). */
121 struct Fixed : HBINT32
124 float to_float () const { return ((int32_t) v) / 65536.f; }
125 void set_float (float f) { v.set (round (f * 65536.f)); }
127 DEFINE_SIZE_STATIC (4);
130 /* Date represented in number of seconds since 12:00 midnight, January 1,
131 * 1904. The value is represented as a signed 64-bit integer. */
134 bool sanitize (hb_sanitize_context_t *c) const
136 TRACE_SANITIZE (this);
137 return_trace (likely (c->check_struct (this)));
143 DEFINE_SIZE_STATIC (8);
146 /* Array of four uint8s (length = 32 bits) used to identify a script, language
147 * system, feature, or baseline */
148 struct Tag : HBUINT32
150 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
151 operator const char* () const { return reinterpret_cast<const char *> (&this->v); }
152 operator char* () { return reinterpret_cast<char *> (&this->v); }
154 DEFINE_SIZE_STATIC (4);
157 /* Glyph index number, same as uint16 (length = 16 bits) */
158 typedef HBUINT16 GlyphID;
160 /* Script/language-system/feature index */
161 struct Index : HBUINT16 {
162 static constexpr unsigned NOT_FOUND_INDEX = 0xFFFFu;
164 DECLARE_NULL_NAMESPACE_BYTES (OT, Index);
166 typedef Index NameID;
168 /* Offset, Null offset = 0 */
169 template <typename Type, bool has_null=true>
174 bool is_null () const { return has_null && 0 == *this; }
176 void *serialize (hb_serialize_context_t *c, const void *base)
178 void *t = c->start_embed<void> ();
179 this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */
184 DEFINE_SIZE_STATIC (sizeof (Type));
187 typedef Offset<HBUINT16> Offset16;
188 typedef Offset<HBUINT32> Offset32;
192 struct CheckSum : HBUINT32
194 /* This is reference implementation from the spec. */
195 static uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length)
198 assert (0 == (Length & 3));
199 const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size;
201 while (Table < EndPtr)
206 /* Note: data should be 4byte aligned and have 4byte padding at the end. */
207 void set_for_data (const void *data, unsigned int length)
208 { set (CalcTableChecksum ((const HBUINT32 *) data, length)); }
211 DEFINE_SIZE_STATIC (4);
219 template <typename FixedType=HBUINT16>
222 uint32_t to_int () const { return (major << (sizeof (FixedType) * 8)) + minor; }
224 bool sanitize (hb_sanitize_context_t *c) const
226 TRACE_SANITIZE (this);
227 return_trace (c->check_struct (this));
233 DEFINE_SIZE_STATIC (2 * sizeof (FixedType));
238 * Template subclasses of Offset that do the dereferencing.
242 template <typename Type, bool has_null>
245 static const Type *get_null () { return nullptr; }
246 static Type *get_crap () { return nullptr; }
248 template <typename Type>
249 struct _hb_has_null<Type, true>
251 static const Type *get_null () { return &Null(Type); }
252 static Type *get_crap () { return &Crap(Type); }
255 template <typename Type, typename OffsetType=HBUINT16, bool has_null=true>
256 struct OffsetTo : Offset<OffsetType, has_null>
258 const Type& operator () (const void *base) const
260 if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_null ();
261 return StructAtOffset<const Type> (base, *this);
263 Type& operator () (void *base) const
265 if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_crap ();
266 return StructAtOffset<Type> (base, *this);
269 Type& serialize (hb_serialize_context_t *c, const void *base)
271 return * (Type *) Offset<OffsetType>::serialize (c, base);
274 template <typename T>
275 void serialize_subset (hb_subset_context_t *c, const T &src, const void *base)
277 if (&src == &Null (T))
282 serialize (c->serializer, base);
287 bool sanitize_shallow (hb_sanitize_context_t *c, const void *base) const
289 TRACE_SANITIZE (this);
290 if (unlikely (!c->check_struct (this))) return_trace (false);
291 if (unlikely (this->is_null ())) return_trace (true);
292 if (unlikely (!c->check_range (base, *this))) return_trace (false);
296 bool sanitize (hb_sanitize_context_t *c, const void *base) const
298 TRACE_SANITIZE (this);
299 return_trace (sanitize_shallow (c, base) &&
301 StructAtOffset<Type> (base, *this).sanitize (c) ||
304 template <typename T1>
305 bool sanitize (hb_sanitize_context_t *c, const void *base, T1 d1) const
307 TRACE_SANITIZE (this);
308 return_trace (sanitize_shallow (c, base) &&
310 StructAtOffset<Type> (base, *this).sanitize (c, d1) ||
313 template <typename T1, typename T2>
314 bool sanitize (hb_sanitize_context_t *c, const void *base, T1 d1, T2 d2) const
316 TRACE_SANITIZE (this);
317 return_trace (sanitize_shallow (c, base) &&
319 StructAtOffset<Type> (base, *this).sanitize (c, d1, d2) ||
322 template <typename T1, typename T2, typename T3>
323 bool sanitize (hb_sanitize_context_t *c, const void *base, T1 d1, T2 d2, T3 d3) const
325 TRACE_SANITIZE (this);
326 return_trace (sanitize_shallow (c, base) &&
328 StructAtOffset<Type> (base, *this).sanitize (c, d1, d2, d3) ||
332 /* Set the offset to Null */
333 bool neuter (hb_sanitize_context_t *c) const
335 if (!has_null) return false;
336 return c->try_set (this, 0);
338 DEFINE_SIZE_STATIC (sizeof (OffsetType));
340 /* Partial specializations. */
341 template <typename Type, bool has_null=true> struct LOffsetTo : OffsetTo<Type, HBUINT32, has_null> {};
342 template <typename Type, typename OffsetType=HBUINT16 > struct NNOffsetTo : OffsetTo<Type, OffsetType, false> {};
343 template <typename Type > struct LNNOffsetTo : OffsetTo<Type, HBUINT32, false> {};
345 template <typename Base, typename OffsetType, bool has_null, typename Type>
346 static inline const Type& operator + (const Base &base, const OffsetTo<Type, OffsetType, has_null> &offset) { return offset (base); }
347 template <typename Base, typename OffsetType, bool has_null, typename Type>
348 static inline Type& operator + (Base &base, OffsetTo<Type, OffsetType, has_null> &offset) { return offset (base); }
355 template <typename Type>
356 struct UnsizedArrayOf
359 static constexpr unsigned item_size = hb_static_size (Type);
361 HB_NO_CREATE_COPY_ASSIGN_TEMPLATE (UnsizedArrayOf, Type);
363 const Type& operator [] (int i_) const
365 unsigned int i = (unsigned int) i_;
366 const Type *p = &arrayZ[i];
367 if (unlikely (p < arrayZ)) return Null (Type); /* Overflowed. */
370 Type& operator [] (int i_)
372 unsigned int i = (unsigned int) i_;
373 Type *p = &arrayZ[i];
374 if (unlikely (p < arrayZ)) return Crap (Type); /* Overflowed. */
378 unsigned int get_size (unsigned int len) const
379 { return len * Type::static_size; }
381 template <typename T> operator T * () { return arrayZ; }
382 template <typename T> operator const T * () const { return arrayZ; }
383 hb_array_t<Type> as_array (unsigned int len)
384 { return hb_array (arrayZ, len); }
385 hb_array_t<const Type> as_array (unsigned int len) const
386 { return hb_array (arrayZ, len); }
387 operator hb_array_t<Type> () { return as_array (); }
388 operator hb_array_t<const Type> () const { return as_array (); }
390 template <typename T>
391 Type &lsearch (unsigned int len, const T &x, Type ¬_found = Crap (Type))
392 { return *as_array (len).lsearch (x, ¬_found); }
393 template <typename T>
394 const Type &lsearch (unsigned int len, const T &x, const Type ¬_found = Null (Type)) const
395 { return *as_array (len).lsearch (x, ¬_found); }
397 void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1)
398 { as_array (len).qsort (start, end); }
400 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
402 TRACE_SANITIZE (this);
403 if (unlikely (!sanitize_shallow (c, count))) return_trace (false);
405 /* Note: for structs that do not reference other structs,
406 * we do not need to call their sanitize() as we already did
407 * a bound check on the aggregate array size. We just include
408 * a small unreachable expression to make sure the structs
409 * pointed to do have a simple sanitize(), ie. they do not
410 * reference other structs via offsets.
412 (void) (false && arrayZ[0].sanitize (c));
416 bool sanitize (hb_sanitize_context_t *c, unsigned int count, const void *base) const
418 TRACE_SANITIZE (this);
419 if (unlikely (!sanitize_shallow (c, count))) return_trace (false);
420 for (unsigned int i = 0; i < count; i++)
421 if (unlikely (!arrayZ[i].sanitize (c, base)))
422 return_trace (false);
425 template <typename T>
426 bool sanitize (hb_sanitize_context_t *c, unsigned int count, const void *base, T user_data) const
428 TRACE_SANITIZE (this);
429 if (unlikely (!sanitize_shallow (c, count))) return_trace (false);
430 for (unsigned int i = 0; i < count; i++)
431 if (unlikely (!arrayZ[i].sanitize (c, base, user_data)))
432 return_trace (false);
436 bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const
438 TRACE_SANITIZE (this);
439 return_trace (c->check_array (arrayZ, count));
445 DEFINE_SIZE_UNBOUNDED (0);
448 /* Unsized array of offset's */
449 template <typename Type, typename OffsetType, bool has_null=true>
450 struct UnsizedOffsetArrayOf : UnsizedArrayOf<OffsetTo<Type, OffsetType, has_null> > {};
452 /* Unsized array of offsets relative to the beginning of the array itself. */
453 template <typename Type, typename OffsetType, bool has_null=true>
454 struct UnsizedOffsetListOf : UnsizedOffsetArrayOf<Type, OffsetType, has_null>
456 const Type& operator [] (int i_) const
458 unsigned int i = (unsigned int) i_;
459 const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
460 if (unlikely (p < this->arrayZ)) return Null (Type); /* Overflowed. */
463 Type& operator [] (int i_)
465 unsigned int i = (unsigned int) i_;
466 const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
467 if (unlikely (p < this->arrayZ)) return Crap (Type); /* Overflowed. */
472 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
474 TRACE_SANITIZE (this);
475 return_trace ((UnsizedOffsetArrayOf<Type, OffsetType, has_null>::sanitize (c, count, this)));
477 template <typename T>
478 bool sanitize (hb_sanitize_context_t *c, unsigned int count, T user_data) const
480 TRACE_SANITIZE (this);
481 return_trace ((UnsizedOffsetArrayOf<Type, OffsetType, has_null>::sanitize (c, count, this, user_data)));
485 /* An array with sorted elements. Supports binary searching. */
486 template <typename Type>
487 struct SortedUnsizedArrayOf : UnsizedArrayOf<Type>
489 hb_sorted_array_t<Type> as_array (unsigned int len)
490 { return hb_sorted_array (this->arrayZ, len); }
491 hb_sorted_array_t<const Type> as_array (unsigned int len) const
492 { return hb_sorted_array (this->arrayZ, len); }
493 operator hb_sorted_array_t<Type> () { return as_array (); }
494 operator hb_sorted_array_t<const Type> () const { return as_array (); }
496 template <typename T>
497 Type &bsearch (unsigned int len, const T &x, Type ¬_found = Crap (Type))
498 { return *as_array (len).bsearch (x, ¬_found); }
499 template <typename T>
500 const Type &bsearch (unsigned int len, const T &x, const Type ¬_found = Null (Type)) const
501 { return *as_array (len).bsearch (x, ¬_found); }
502 template <typename T>
503 bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr,
504 hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
505 unsigned int to_store = (unsigned int) -1) const
506 { return as_array (len).bfind (x, i, not_found, to_store); }
510 /* An array with a number of elements. */
511 template <typename Type, typename LenType=HBUINT16>
515 static constexpr unsigned item_size = hb_static_size (Type);
517 HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2 (ArrayOf, Type, LenType);
519 const Type& operator [] (int i_) const
521 unsigned int i = (unsigned int) i_;
522 if (unlikely (i >= len)) return Null (Type);
525 Type& operator [] (int i_)
527 unsigned int i = (unsigned int) i_;
528 if (unlikely (i >= len)) return Crap (Type);
532 unsigned int get_size () const
533 { return len.static_size + len * Type::static_size; }
535 hb_array_t<Type> as_array ()
536 { return hb_array (arrayZ, len); }
537 hb_array_t<const Type> as_array () const
538 { return hb_array (arrayZ, len); }
539 operator hb_array_t<Type> (void) { return as_array (); }
540 operator hb_array_t<const Type> (void) const { return as_array (); }
542 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
543 { return as_array ().sub_array (start_offset, count);}
544 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
545 { return as_array ().sub_array (start_offset, count);}
546 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
547 { return as_array ().sub_array (start_offset, count);}
548 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
549 { return as_array ().sub_array (start_offset, count);}
551 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
553 TRACE_SERIALIZE (this);
554 if (unlikely (!c->extend_min (*this))) return_trace (false);
555 len.set (items_len); /* TODO(serialize) Overflow? */
556 if (unlikely (!c->extend (*this))) return_trace (false);
559 template <typename T>
560 bool serialize (hb_serialize_context_t *c, hb_array_t<const T> items)
562 TRACE_SERIALIZE (this);
563 if (unlikely (!serialize (c, items.length))) return_trace (false);
564 for (unsigned int i = 0; i < items.length; i++)
565 hb_assign (arrayZ[i], items[i]);
569 bool sanitize (hb_sanitize_context_t *c) const
571 TRACE_SANITIZE (this);
572 if (unlikely (!sanitize_shallow (c))) return_trace (false);
574 /* Note: for structs that do not reference other structs,
575 * we do not need to call their sanitize() as we already did
576 * a bound check on the aggregate array size. We just include
577 * a small unreachable expression to make sure the structs
578 * pointed to do have a simple sanitize(), ie. they do not
579 * reference other structs via offsets.
581 (void) (false && arrayZ[0].sanitize (c));
585 bool sanitize (hb_sanitize_context_t *c, const void *base) const
587 TRACE_SANITIZE (this);
588 if (unlikely (!sanitize_shallow (c))) return_trace (false);
589 unsigned int count = len;
590 for (unsigned int i = 0; i < count; i++)
591 if (unlikely (!arrayZ[i].sanitize (c, base)))
592 return_trace (false);
595 template <typename T>
596 bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
598 TRACE_SANITIZE (this);
599 if (unlikely (!sanitize_shallow (c))) return_trace (false);
600 unsigned int count = len;
601 for (unsigned int i = 0; i < count; i++)
602 if (unlikely (!arrayZ[i].sanitize (c, base, user_data)))
603 return_trace (false);
607 template <typename T>
608 Type &lsearch (const T &x, Type ¬_found = Crap (Type))
609 { return *as_array ().lsearch (x, ¬_found); }
610 template <typename T>
611 const Type &lsearch (const T &x, const Type ¬_found = Null (Type)) const
612 { return *as_array ().lsearch (x, ¬_found); }
614 void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
615 { as_array ().qsort (start, end); }
617 bool sanitize_shallow (hb_sanitize_context_t *c) const
619 TRACE_SANITIZE (this);
620 return_trace (len.sanitize (c) && c->check_array (arrayZ, len));
627 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
629 template <typename Type> struct LArrayOf : ArrayOf<Type, HBUINT32> {};
630 typedef ArrayOf<HBUINT8, HBUINT8> PString;
632 /* Array of Offset's */
633 template <typename Type>
634 struct OffsetArrayOf : ArrayOf<OffsetTo<Type, HBUINT16> > {};
635 template <typename Type>
636 struct LOffsetArrayOf : ArrayOf<OffsetTo<Type, HBUINT32> > {};
637 template <typename Type>
638 struct LOffsetLArrayOf : ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT32> {};
640 /* Array of offsets relative to the beginning of the array itself. */
641 template <typename Type>
642 struct OffsetListOf : OffsetArrayOf<Type>
644 const Type& operator [] (int i_) const
646 unsigned int i = (unsigned int) i_;
647 if (unlikely (i >= this->len)) return Null (Type);
648 return this+this->arrayZ[i];
650 const Type& operator [] (int i_)
652 unsigned int i = (unsigned int) i_;
653 if (unlikely (i >= this->len)) return Crap (Type);
654 return this+this->arrayZ[i];
657 bool subset (hb_subset_context_t *c) const
660 struct OffsetListOf<Type> *out = c->serializer->embed (*this);
661 if (unlikely (!out)) return_trace (false);
662 unsigned int count = this->len;
663 for (unsigned int i = 0; i < count; i++)
664 out->arrayZ[i].serialize_subset (c, (*this)[i], out);
668 bool sanitize (hb_sanitize_context_t *c) const
670 TRACE_SANITIZE (this);
671 return_trace (OffsetArrayOf<Type>::sanitize (c, this));
673 template <typename T>
674 bool sanitize (hb_sanitize_context_t *c, T user_data) const
676 TRACE_SANITIZE (this);
677 return_trace (OffsetArrayOf<Type>::sanitize (c, this, user_data));
681 /* An array starting at second element. */
682 template <typename Type, typename LenType=HBUINT16>
683 struct HeadlessArrayOf
685 static constexpr unsigned item_size = Type::static_size;
687 HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2 (HeadlessArrayOf, Type, LenType);
689 const Type& operator [] (int i_) const
691 unsigned int i = (unsigned int) i_;
692 if (unlikely (i >= lenP1 || !i)) return Null (Type);
695 Type& operator [] (int i_)
697 unsigned int i = (unsigned int) i_;
698 if (unlikely (i >= lenP1 || !i)) return Crap (Type);
701 unsigned int get_size () const
702 { return lenP1.static_size + (lenP1 ? lenP1 - 1 : 0) * Type::static_size; }
704 bool serialize (hb_serialize_context_t *c,
705 hb_array_t<const Type> items)
707 TRACE_SERIALIZE (this);
708 if (unlikely (!c->extend_min (*this))) return_trace (false);
709 lenP1.set (items.length + 1); /* TODO(serialize) Overflow? */
710 if (unlikely (!c->extend (*this))) return_trace (false);
711 for (unsigned int i = 0; i < items.length; i++)
712 arrayZ[i] = items[i];
716 bool sanitize (hb_sanitize_context_t *c) const
718 TRACE_SANITIZE (this);
719 if (unlikely (!sanitize_shallow (c))) return_trace (false);
721 /* Note: for structs that do not reference other structs,
722 * we do not need to call their sanitize() as we already did
723 * a bound check on the aggregate array size. We just include
724 * a small unreachable expression to make sure the structs
725 * pointed to do have a simple sanitize(), ie. they do not
726 * reference other structs via offsets.
728 (void) (false && arrayZ[0].sanitize (c));
734 bool sanitize_shallow (hb_sanitize_context_t *c) const
736 TRACE_SANITIZE (this);
737 return_trace (lenP1.sanitize (c) &&
738 (!lenP1 || c->check_array (arrayZ, lenP1 - 1)));
745 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
748 /* An array storing length-1. */
749 template <typename Type, typename LenType=HBUINT16>
752 HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2 (ArrayOfM1, Type, LenType);
754 const Type& operator [] (int i_) const
756 unsigned int i = (unsigned int) i_;
757 if (unlikely (i > lenM1)) return Null (Type);
760 Type& operator [] (int i_)
762 unsigned int i = (unsigned int) i_;
763 if (unlikely (i > lenM1)) return Crap (Type);
766 unsigned int get_size () const
767 { return lenM1.static_size + (lenM1 + 1) * Type::static_size; }
769 template <typename T>
770 bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
772 TRACE_SANITIZE (this);
773 if (unlikely (!sanitize_shallow (c))) return_trace (false);
774 unsigned int count = lenM1 + 1;
775 for (unsigned int i = 0; i < count; i++)
776 if (unlikely (!arrayZ[i].sanitize (c, base, user_data)))
777 return_trace (false);
782 bool sanitize_shallow (hb_sanitize_context_t *c) const
784 TRACE_SANITIZE (this);
785 return_trace (lenM1.sanitize (c) &&
786 (c->check_array (arrayZ, lenM1 + 1)));
793 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
796 /* An array with sorted elements. Supports binary searching. */
797 template <typename Type, typename LenType=HBUINT16>
798 struct SortedArrayOf : ArrayOf<Type, LenType>
800 hb_sorted_array_t<Type> as_array ()
801 { return hb_sorted_array (this->arrayZ, this->len); }
802 hb_sorted_array_t<const Type> as_array () const
803 { return hb_sorted_array (this->arrayZ, this->len); }
804 operator hb_sorted_array_t<Type> () { return as_array (); }
805 operator hb_sorted_array_t<const Type> () const { return as_array (); }
807 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
808 { return as_array ().sub_array (start_offset, count);}
809 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
810 { return as_array ().sub_array (start_offset, count);}
811 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
812 { return as_array ().sub_array (start_offset, count);}
813 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
814 { return as_array ().sub_array (start_offset, count);}
816 template <typename T>
817 Type &bsearch (const T &x, Type ¬_found = Crap (Type))
818 { return *as_array ().bsearch (x, ¬_found); }
819 template <typename T>
820 const Type &bsearch (const T &x, const Type ¬_found = Null (Type)) const
821 { return *as_array ().bsearch (x, ¬_found); }
822 template <typename T>
823 bool bfind (const T &x, unsigned int *i = nullptr,
824 hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
825 unsigned int to_store = (unsigned int) -1) const
826 { return as_array ().bfind (x, i, not_found, to_store); }
830 * Binary-search arrays
833 template <typename LenType=HBUINT16>
834 struct BinSearchHeader
836 operator uint32_t () const { return len; }
838 bool sanitize (hb_sanitize_context_t *c) const
840 TRACE_SANITIZE (this);
841 return_trace (c->check_struct (this));
844 void set (unsigned int v)
848 entrySelector.set (MAX (1u, hb_bit_storage (v)) - 1);
849 searchRange.set (16 * (1u << entrySelector));
850 rangeShift.set (v * 16 > searchRange
851 ? 16 * v - searchRange
858 LenType entrySelector;
862 DEFINE_SIZE_STATIC (8);
865 template <typename Type, typename LenType=HBUINT16>
866 struct BinSearchArrayOf : SortedArrayOf<Type, BinSearchHeader<LenType> > {};
869 struct VarSizedBinSearchHeader
872 bool sanitize (hb_sanitize_context_t *c) const
874 TRACE_SANITIZE (this);
875 return_trace (c->check_struct (this));
878 HBUINT16 unitSize; /* Size of a lookup unit for this search in bytes. */
879 HBUINT16 nUnits; /* Number of units of the preceding size to be searched. */
880 HBUINT16 searchRange; /* The value of unitSize times the largest power of 2
881 * that is less than or equal to the value of nUnits. */
882 HBUINT16 entrySelector; /* The log base 2 of the largest power of 2 less than
883 * or equal to the value of nUnits. */
884 HBUINT16 rangeShift; /* The value of unitSize times the difference of the
885 * value of nUnits minus the largest power of 2 less
886 * than or equal to the value of nUnits. */
888 DEFINE_SIZE_STATIC (10);
891 template <typename Type>
892 struct VarSizedBinSearchArrayOf
894 static constexpr unsigned item_size = Type::static_size;
896 HB_NO_CREATE_COPY_ASSIGN_TEMPLATE (VarSizedBinSearchArrayOf, Type);
898 bool last_is_terminator () const
900 if (unlikely (!header.nUnits)) return false;
904 * "The number of termination values that need to be included is table-specific.
905 * The value that indicates binary search termination is 0xFFFF." */
906 const HBUINT16 *words = &StructAtOffset<HBUINT16> (&bytesZ, (header.nUnits - 1) * header.unitSize);
907 unsigned int count = Type::TerminationWordCount;
908 for (unsigned int i = 0; i < count; i++)
909 if (words[i] != 0xFFFFu)
914 const Type& operator [] (int i_) const
916 unsigned int i = (unsigned int) i_;
917 if (unlikely (i >= get_length ())) return Null (Type);
918 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
920 Type& operator [] (int i_)
922 unsigned int i = (unsigned int) i_;
923 if (unlikely (i >= get_length ())) return Crap (Type);
924 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
926 unsigned int get_length () const
927 { return header.nUnits - last_is_terminator (); }
928 unsigned int get_size () const
929 { return header.static_size + header.nUnits * header.unitSize; }
931 bool sanitize (hb_sanitize_context_t *c) const
933 TRACE_SANITIZE (this);
934 if (unlikely (!sanitize_shallow (c))) return_trace (false);
936 /* Note: for structs that do not reference other structs,
937 * we do not need to call their sanitize() as we already did
938 * a bound check on the aggregate array size. We just include
939 * a small unreachable expression to make sure the structs
940 * pointed to do have a simple sanitize(), ie. they do not
941 * reference other structs via offsets.
943 (void) (false && StructAtOffset<Type> (&bytesZ, 0).sanitize (c));
947 bool sanitize (hb_sanitize_context_t *c, const void *base) const
949 TRACE_SANITIZE (this);
950 if (unlikely (!sanitize_shallow (c))) return_trace (false);
951 unsigned int count = get_length ();
952 for (unsigned int i = 0; i < count; i++)
953 if (unlikely (!(*this)[i].sanitize (c, base)))
954 return_trace (false);
957 template <typename T>
958 bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
960 TRACE_SANITIZE (this);
961 if (unlikely (!sanitize_shallow (c))) return_trace (false);
962 unsigned int count = get_length ();
963 for (unsigned int i = 0; i < count; i++)
964 if (unlikely (!(*this)[i].sanitize (c, base, user_data)))
965 return_trace (false);
969 template <typename T>
970 const Type *bsearch (const T &key) const
972 unsigned int size = header.unitSize;
973 int min = 0, max = (int) get_length () - 1;
976 int mid = ((unsigned int) min + (unsigned int) max) / 2;
977 const Type *p = (const Type *) (((const char *) &bytesZ) + (mid * size));
978 int c = p->cmp (key);
979 if (c < 0) max = mid - 1;
980 else if (c > 0) min = mid + 1;
987 bool sanitize_shallow (hb_sanitize_context_t *c) const
989 TRACE_SANITIZE (this);
990 return_trace (header.sanitize (c) &&
991 Type::static_size <= header.unitSize &&
992 c->check_range (bytesZ.arrayZ,
998 VarSizedBinSearchHeader header;
999 UnsizedArrayOf<HBUINT8> bytesZ;
1001 DEFINE_SIZE_ARRAY (10, bytesZ);
1005 } /* namespace OT */
1008 #endif /* HB_OPEN_TYPE_HH */