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_PRIVATE_HH
30 #define HB_OPEN_TYPE_PRIVATE_HH
32 #include "hb-private.hh"
33 #include "hb-face-private.hh"
44 /* Cast to struct T, reference to reference */
45 template<typename Type, typename TObject>
46 static inline const Type& CastR(const TObject &X)
47 { return reinterpret_cast<const Type&> (X); }
48 template<typename Type, typename TObject>
49 static inline Type& CastR(TObject &X)
50 { return reinterpret_cast<Type&> (X); }
52 /* Cast to struct T, pointer to pointer */
53 template<typename Type, typename TObject>
54 static inline const Type* CastP(const TObject *X)
55 { return reinterpret_cast<const Type*> (X); }
56 template<typename Type, typename TObject>
57 static inline Type* CastP(TObject *X)
58 { return reinterpret_cast<Type*> (X); }
60 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
61 * location pointed to by P plus Ofs bytes. */
62 template<typename Type>
63 static inline const Type& StructAtOffset(const void *P, unsigned int offset)
64 { return * reinterpret_cast<const Type*> ((const char *) P + offset); }
65 template<typename Type>
66 static inline Type& StructAtOffset(void *P, unsigned int offset)
67 { return * reinterpret_cast<Type*> ((char *) P + offset); }
69 /* StructAfter<T>(X) returns the struct T& that is placed after X.
70 * Works with X of variable size also. X must implement get_size() */
71 template<typename Type, typename TObject>
72 static inline const Type& StructAfter(const TObject &X)
73 { return StructAtOffset<Type>(&X, X.get_size()); }
74 template<typename Type, typename TObject>
75 static inline Type& StructAfter(TObject &X)
76 { return StructAtOffset<Type>(&X, X.get_size()); }
84 /* Check _assertion in a method environment */
85 #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
86 inline void _instance_assertion_on_line_##_line (void) const \
88 ASSERT_STATIC (_assertion); \
89 ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \
91 # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
92 # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
94 /* Check that _code compiles in a method environment */
95 #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
96 inline void _compiles_assertion_on_line_##_line (void) const \
98 # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
99 # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
102 #define DEFINE_SIZE_STATIC(size) \
103 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \
104 static const unsigned int static_size = (size); \
105 static const unsigned int min_size = (size); \
106 inline unsigned int get_size (void) const { return (size); }
108 #define DEFINE_SIZE_UNION(size, _member) \
109 DEFINE_INSTANCE_ASSERTION (0*sizeof(this->u._member.static_size) + sizeof(this->u._member) == (size)); \
110 static const unsigned int min_size = (size)
112 #define DEFINE_SIZE_MIN(size) \
113 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \
114 static const unsigned int min_size = (size)
116 #define DEFINE_SIZE_ARRAY(size, array) \
117 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
118 DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
119 static const unsigned int min_size = (size)
121 #define DEFINE_SIZE_ARRAY2(size, array1, array2) \
122 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
123 DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
124 static const unsigned int min_size = (size)
132 /* Global nul-content Null pool. Enlarge as necessary. */
133 /* TODO This really should be a extern HB_INTERNAL and defined somewhere... */
134 static const void *_NullPool[(256+8) / sizeof (void *)];
136 /* Generic nul-content Null objects. */
137 template <typename Type>
138 static inline const Type& Null (void) {
139 ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
140 return *CastP<Type> (_NullPool);
143 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
144 #define DEFINE_NULL_DATA(Type, data) \
145 static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \
147 /*static*/ inline const Type& Null<Type> (void) { \
148 return *CastP<Type> (_Null##Type); \
149 } /* The following line really exists such that we end in a place needing semicolon */ \
150 ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
152 /* Accessor macro. */
153 #define Null(Type) Null<Type>()
160 template <typename Context, typename Return, unsigned int MaxDebugDepth>
161 struct hb_dispatch_context_t
163 static const unsigned int max_debug_depth = MaxDebugDepth;
164 typedef Return return_t;
165 template <typename T, typename F>
166 inline bool may_dispatch (const T *obj, const F *format) { return true; }
167 static return_t no_dispatch_return_value (void) { return Context::default_return_value (); }
175 #ifndef HB_DEBUG_SANITIZE
176 #define HB_DEBUG_SANITIZE (HB_DEBUG+0)
180 #define TRACE_SANITIZE(this) \
181 hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \
182 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
185 /* This limits sanitizing time on really broken fonts. */
186 #ifndef HB_SANITIZE_MAX_EDITS
187 #define HB_SANITIZE_MAX_EDITS 32
190 struct hb_sanitize_context_t :
191 hb_dispatch_context_t<hb_sanitize_context_t, bool, HB_DEBUG_SANITIZE>
193 inline hb_sanitize_context_t (void) :
195 start (NULL), end (NULL),
196 writable (false), edit_count (0),
199 inline const char *get_name (void) { return "SANITIZE"; }
200 template <typename T, typename F>
201 inline bool may_dispatch (const T *obj, const F *format)
202 { return format->sanitize (this); }
203 template <typename T>
204 inline return_t dispatch (const T &obj) { return obj.sanitize (this); }
205 static return_t default_return_value (void) { return true; }
206 static return_t no_dispatch_return_value (void) { return false; }
207 bool stop_sublookup_iteration (const return_t r) const { return !r; }
209 inline void init (hb_blob_t *b)
211 this->blob = hb_blob_reference (b);
212 this->writable = false;
215 inline void start_processing (void)
217 this->start = hb_blob_get_data (this->blob, NULL);
218 this->end = this->start + hb_blob_get_length (this->blob);
219 assert (this->start <= this->end); /* Must not overflow. */
220 this->edit_count = 0;
221 this->debug_depth = 0;
223 DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1,
224 "start [%p..%p] (%lu bytes)",
225 this->start, this->end,
226 (unsigned long) (this->end - this->start));
229 inline void end_processing (void)
231 DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1,
232 "end [%p..%p] %u edit requests",
233 this->start, this->end, this->edit_count);
235 hb_blob_destroy (this->blob);
237 this->start = this->end = NULL;
240 inline bool check_range (const void *base, unsigned int len) const
242 const char *p = (const char *) base;
243 bool ok = this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len;
245 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
246 "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s",
248 this->start, this->end,
249 ok ? "OK" : "OUT-OF-RANGE");
254 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
256 const char *p = (const char *) base;
257 bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
258 unsigned int array_size = record_size * len;
259 bool ok = !overflows && this->check_range (base, array_size);
261 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
262 "check_array [%p..%p] (%d*%d=%d bytes) in [%p..%p] -> %s",
263 p, p + (record_size * len), record_size, len, (unsigned int) array_size,
264 this->start, this->end,
265 overflows ? "OVERFLOWS" : ok ? "OK" : "OUT-OF-RANGE");
270 template <typename Type>
271 inline bool check_struct (const Type *obj) const
273 return likely (this->check_range (obj, obj->min_size));
276 inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
278 if (this->edit_count >= HB_SANITIZE_MAX_EDITS)
281 const char *p = (const char *) base;
284 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
285 "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
288 this->start, this->end,
289 this->writable ? "GRANTED" : "DENIED");
291 return this->writable;
294 template <typename Type, typename ValueType>
295 inline bool try_set (const Type *obj, const ValueType &v) {
296 if (this->may_edit (obj, obj->static_size)) {
297 const_cast<Type *> (obj)->set (v);
303 mutable unsigned int debug_depth;
304 const char *start, *end;
306 unsigned int edit_count;
312 /* Template to sanitize an object. */
313 template <typename Type>
316 static hb_blob_t *sanitize (hb_blob_t *blob) {
317 hb_sanitize_context_t c[1];
320 /* TODO is_sane() stuff */
325 DEBUG_MSG_FUNC (SANITIZE, c->start, "start");
327 c->start_processing ();
329 if (unlikely (!c->start)) {
330 c->end_processing ();
334 Type *t = CastP<Type> (const_cast<char *> (c->start));
336 sane = t->sanitize (c);
339 DEBUG_MSG_FUNC (SANITIZE, c->start, "passed first round with %d edits; going for second round", c->edit_count);
341 /* sanitize again to ensure no toe-stepping */
343 sane = t->sanitize (c);
345 DEBUG_MSG_FUNC (SANITIZE, c->start, "requested %d edits in second round; FAILLING", c->edit_count);
350 unsigned int edit_count = c->edit_count;
351 if (edit_count && !c->writable) {
352 c->start = hb_blob_get_data_writable (blob, NULL);
353 c->end = c->start + hb_blob_get_length (blob);
357 /* ok, we made it writable by relocating. try again */
358 DEBUG_MSG_FUNC (SANITIZE, c->start, "retry");
364 c->end_processing ();
366 DEBUG_MSG_FUNC (SANITIZE, c->start, sane ? "PASSED" : "FAILED");
370 hb_blob_destroy (blob);
371 return hb_blob_get_empty ();
375 static const Type* lock_instance (hb_blob_t *blob) {
376 hb_blob_make_immutable (blob);
377 const char *base = hb_blob_get_data (blob, NULL);
378 return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
388 #ifndef HB_DEBUG_SERIALIZE
389 #define HB_DEBUG_SERIALIZE (HB_DEBUG+0)
393 #define TRACE_SERIALIZE(this) \
394 hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \
395 (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \
399 struct hb_serialize_context_t
401 inline hb_serialize_context_t (void *start_, unsigned int size)
403 this->start = (char *) start_;
404 this->end = this->start + size;
406 this->ran_out_of_room = false;
407 this->head = this->start;
408 this->debug_depth = 0;
411 template <typename Type>
412 inline Type *start_serialize (void)
414 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
415 "start [%p..%p] (%lu bytes)",
416 this->start, this->end,
417 (unsigned long) (this->end - this->start));
419 return start_embed<Type> ();
422 inline void end_serialize (void)
424 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
425 "end [%p..%p] serialized %d bytes; %s",
426 this->start, this->end,
427 (int) (this->head - this->start),
428 this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room");
432 template <typename Type>
433 inline Type *copy (void)
435 assert (!this->ran_out_of_room);
436 unsigned int len = this->head - this->start;
437 void *p = malloc (len);
439 memcpy (p, this->start, len);
440 return reinterpret_cast<Type *> (p);
443 template <typename Type>
444 inline Type *allocate_size (unsigned int size)
446 if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) {
447 this->ran_out_of_room = true;
450 memset (this->head, 0, size);
451 char *ret = this->head;
453 return reinterpret_cast<Type *> (ret);
456 template <typename Type>
457 inline Type *allocate_min (void)
459 return this->allocate_size<Type> (Type::min_size);
462 template <typename Type>
463 inline Type *start_embed (void)
465 Type *ret = reinterpret_cast<Type *> (this->head);
469 template <typename Type>
470 inline Type *embed (const Type &obj)
472 unsigned int size = obj.get_size ();
473 Type *ret = this->allocate_size<Type> (size);
474 if (unlikely (!ret)) return NULL;
475 memcpy (ret, obj, size);
479 template <typename Type>
480 inline Type *extend_min (Type &obj)
482 unsigned int size = obj.min_size;
483 assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head);
484 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL;
485 return reinterpret_cast<Type *> (&obj);
488 template <typename Type>
489 inline Type *extend (Type &obj)
491 unsigned int size = obj.get_size ();
492 assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head);
493 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL;
494 return reinterpret_cast<Type *> (&obj);
497 inline void truncate (void *new_head)
499 assert (this->start < new_head && new_head <= this->head);
500 this->head = (char *) new_head;
503 unsigned int debug_depth;
504 char *start, *end, *head;
505 bool ran_out_of_room;
508 template <typename Type>
511 inline Supplier (const Type *array, unsigned int len_)
516 inline const Type operator [] (unsigned int i) const
518 if (unlikely (i >= len)) return Type ();
522 inline void advance (unsigned int count)
524 if (unlikely (count > len))
531 inline Supplier (const Supplier<Type> &); /* Disallow copy */
532 inline Supplier<Type>& operator= (const Supplier<Type> &); /* Disallow copy */
543 * The OpenType Font File: Data Types
547 /* "The following data types are used in the OpenType font file.
548 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
555 template <typename Type, int Bytes> struct BEInt;
557 template <typename Type>
558 struct BEInt<Type, 1>
561 inline void set (Type V)
565 inline operator Type (void) const
571 template <typename Type>
572 struct BEInt<Type, 2>
575 inline void set (Type V)
577 v[0] = (V >> 8) & 0xFF;
580 inline operator Type (void) const
585 private: uint8_t v[2];
587 template <typename Type>
588 struct BEInt<Type, 3>
591 inline void set (Type V)
593 v[0] = (V >> 16) & 0xFF;
594 v[1] = (V >> 8) & 0xFF;
597 inline operator Type (void) const
603 private: uint8_t v[3];
605 template <typename Type>
606 struct BEInt<Type, 4>
609 inline void set (Type V)
611 v[0] = (V >> 24) & 0xFF;
612 v[1] = (V >> 16) & 0xFF;
613 v[2] = (V >> 8) & 0xFF;
616 inline operator Type (void) const
623 private: uint8_t v[4];
626 /* Integer types in big-endian order and no alignment requirement */
627 template <typename Type, unsigned int Size>
630 inline void set (Type i) { v.set (i); }
631 inline operator Type(void) const { return v; }
632 inline bool operator == (const IntType<Type,Size> &o) const { return (Type) v == (Type) o.v; }
633 inline bool operator != (const IntType<Type,Size> &o) const { return !(*this == o); }
634 static inline int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); }
635 inline int cmp (Type a) const
638 if (sizeof (Type) < sizeof (int))
639 return (int) a - (int) b;
641 return a < b ? -1 : a == b ? 0 : +1;
643 inline bool sanitize (hb_sanitize_context_t *c) const
645 TRACE_SANITIZE (this);
646 return_trace (likely (c->check_struct (this)));
651 DEFINE_SIZE_STATIC (Size);
654 typedef IntType<int8_t , 1> CHAR; /* 8-bit signed integer. */
655 typedef IntType<uint8_t , 1> BYTE; /* 8-bit unsigned integer. */
656 typedef IntType<int8_t , 1> INT8; /* 8-bit signed integer. */
657 typedef IntType<uint16_t, 2> USHORT; /* 16-bit unsigned integer. */
658 typedef IntType<int16_t, 2> SHORT; /* 16-bit signed integer. */
659 typedef IntType<uint32_t, 4> ULONG; /* 32-bit unsigned integer. */
660 typedef IntType<int32_t, 4> LONG; /* 32-bit signed integer. */
661 typedef IntType<uint32_t, 3> UINT24; /* 24-bit unsigned integer. */
663 /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
666 /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
667 typedef USHORT UFWORD;
669 /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
670 struct F2DOT14 : SHORT
672 //inline float to_float (void) const { return ???; }
673 //inline void set_float (float f) { v.set (f * ???); }
675 DEFINE_SIZE_STATIC (2);
678 /* 32-bit signed fixed-point number (16.16). */
681 //inline float to_float (void) const { return ???; }
682 //inline void set_float (float f) { v.set (f * ???); }
684 DEFINE_SIZE_STATIC (4);
687 /* Date represented in number of seconds since 12:00 midnight, January 1,
688 * 1904. The value is represented as a signed 64-bit integer. */
691 inline bool sanitize (hb_sanitize_context_t *c) const
693 TRACE_SANITIZE (this);
694 return_trace (likely (c->check_struct (this)));
700 DEFINE_SIZE_STATIC (8);
703 /* Array of four uint8s (length = 32 bits) used to identify a script, language
704 * system, feature, or baseline */
707 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
708 inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
709 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
711 DEFINE_SIZE_STATIC (4);
713 DEFINE_NULL_DATA (Tag, " ");
715 /* Glyph index number, same as uint16 (length = 16 bits) */
716 struct GlyphID : USHORT {
717 static inline int cmp (const GlyphID *a, const GlyphID *b) { return b->USHORT::cmp (*a); }
718 inline int cmp (hb_codepoint_t a) const { return (int) a - (int) *this; }
721 /* Script/language-system/feature index */
722 struct Index : USHORT {
723 static const unsigned int NOT_FOUND_INDEX = 0xFFFFu;
725 DEFINE_NULL_DATA (Index, "\xff\xff");
727 /* Offset, Null offset = 0 */
728 template <typename Type=USHORT>
731 inline bool is_null (void) const { return 0 == *this; }
733 DEFINE_SIZE_STATIC (sizeof(Type));
738 struct CheckSum : ULONG
740 /* This is reference implementation from the spec. */
741 static inline uint32_t CalcTableChecksum (const ULONG *Table, uint32_t Length)
744 const ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
746 while (Table < EndPtr)
751 /* Note: data should be 4byte aligned and have 4byte padding at the end. */
752 inline void set_for_data (const void *data, unsigned int length)
753 { set (CalcTableChecksum ((const ULONG *) data, length)); }
756 DEFINE_SIZE_STATIC (4);
764 template <typename FixedType=USHORT>
767 inline uint32_t to_int (void) const { return (major << (sizeof(FixedType) * 8)) + minor; }
769 inline bool sanitize (hb_sanitize_context_t *c) const
771 TRACE_SANITIZE (this);
772 return_trace (c->check_struct (this));
778 DEFINE_SIZE_STATIC (2 * sizeof(FixedType));
784 * Template subclasses of Offset that do the dereferencing.
788 template <typename Type, typename OffsetType=USHORT>
789 struct OffsetTo : Offset<OffsetType>
791 inline const Type& operator () (const void *base) const
793 unsigned int offset = *this;
794 if (unlikely (!offset)) return Null(Type);
795 return StructAtOffset<Type> (base, offset);
798 inline Type& serialize (hb_serialize_context_t *c, const void *base)
800 Type *t = c->start_embed<Type> ();
801 this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */
805 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
807 TRACE_SANITIZE (this);
808 if (unlikely (!c->check_struct (this))) return_trace (false);
809 unsigned int offset = *this;
810 if (unlikely (!offset)) return_trace (true);
811 if (unlikely (!c->check_range (base, offset))) return_trace (false);
812 const Type &obj = StructAtOffset<Type> (base, offset);
813 return_trace (likely (obj.sanitize (c)) || neuter (c));
815 template <typename T>
816 inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
818 TRACE_SANITIZE (this);
819 if (unlikely (!c->check_struct (this))) return_trace (false);
820 unsigned int offset = *this;
821 if (unlikely (!offset)) return_trace (true);
822 if (unlikely (!c->check_range (base, offset))) return_trace (false);
823 const Type &obj = StructAtOffset<Type> (base, offset);
824 return_trace (likely (obj.sanitize (c, user_data)) || neuter (c));
827 /* Set the offset to Null */
828 inline bool neuter (hb_sanitize_context_t *c) const {
829 return c->try_set (this, 0);
831 DEFINE_SIZE_STATIC (sizeof(OffsetType));
833 template <typename Type> struct LOffsetTo : OffsetTo<Type, ULONG> {};
834 template <typename Base, typename OffsetType, typename Type>
835 static inline const Type& operator + (const Base &base, const OffsetTo<Type, OffsetType> &offset) { return offset (base); }
836 template <typename Base, typename OffsetType, typename Type>
837 static inline Type& operator + (Base &base, OffsetTo<Type, OffsetType> &offset) { return offset (base); }
844 /* An array with a number of elements. */
845 template <typename Type, typename LenType=USHORT>
848 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
850 unsigned int count = len;
851 if (unlikely (start_offset > count))
854 count -= start_offset;
855 count = MIN (count, *pcount);
857 return array + start_offset;
860 inline const Type& operator [] (unsigned int i) const
862 if (unlikely (i >= len)) return Null(Type);
865 inline Type& operator [] (unsigned int i)
869 inline unsigned int get_size (void) const
870 { return len.static_size + len * Type::static_size; }
872 inline bool serialize (hb_serialize_context_t *c,
873 unsigned int items_len)
875 TRACE_SERIALIZE (this);
876 if (unlikely (!c->extend_min (*this))) return_trace (false);
877 len.set (items_len); /* TODO(serialize) Overflow? */
878 if (unlikely (!c->extend (*this))) return_trace (false);
882 inline bool serialize (hb_serialize_context_t *c,
883 Supplier<Type> &items,
884 unsigned int items_len)
886 TRACE_SERIALIZE (this);
887 if (unlikely (!serialize (c, items_len))) return_trace (false);
888 for (unsigned int i = 0; i < items_len; i++)
890 items.advance (items_len);
894 inline bool sanitize (hb_sanitize_context_t *c) const
896 TRACE_SANITIZE (this);
897 if (unlikely (!sanitize_shallow (c))) return_trace (false);
899 /* Note: for structs that do not reference other structs,
900 * we do not need to call their sanitize() as we already did
901 * a bound check on the aggregate array size. We just include
902 * a small unreachable expression to make sure the structs
903 * pointed to do have a simple sanitize(), ie. they do not
904 * reference other structs via offsets.
906 (void) (false && array[0].sanitize (c));
910 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
912 TRACE_SANITIZE (this);
913 if (unlikely (!sanitize_shallow (c))) return_trace (false);
914 unsigned int count = len;
915 for (unsigned int i = 0; i < count; i++)
916 if (unlikely (!array[i].sanitize (c, base)))
917 return_trace (false);
920 template <typename T>
921 inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
923 TRACE_SANITIZE (this);
924 if (unlikely (!sanitize_shallow (c))) return_trace (false);
925 unsigned int count = len;
926 for (unsigned int i = 0; i < count; i++)
927 if (unlikely (!array[i].sanitize (c, base, user_data)))
928 return_trace (false);
932 template <typename SearchType>
933 inline int lsearch (const SearchType &x) const
935 unsigned int count = len;
936 for (unsigned int i = 0; i < count; i++)
937 if (!this->array[i].cmp (x))
943 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
945 TRACE_SANITIZE (this);
946 return_trace (c->check_struct (this) && c->check_array (array, Type::static_size, len));
953 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
955 template <typename Type> struct LArrayOf : ArrayOf<Type, ULONG> {};
957 /* Array of Offset's */
958 template <typename Type, typename OffsetType=USHORT>
959 struct OffsetArrayOf : ArrayOf<OffsetTo<Type, OffsetType> > {};
961 /* Array of offsets relative to the beginning of the array itself. */
962 template <typename Type>
963 struct OffsetListOf : OffsetArrayOf<Type>
965 inline const Type& operator [] (unsigned int i) const
967 if (unlikely (i >= this->len)) return Null(Type);
968 return this+this->array[i];
971 inline bool sanitize (hb_sanitize_context_t *c) const
973 TRACE_SANITIZE (this);
974 return_trace (OffsetArrayOf<Type>::sanitize (c, this));
976 template <typename T>
977 inline bool sanitize (hb_sanitize_context_t *c, T user_data) const
979 TRACE_SANITIZE (this);
980 return_trace (OffsetArrayOf<Type>::sanitize (c, this, user_data));
985 /* An array starting at second element. */
986 template <typename Type, typename LenType=USHORT>
987 struct HeadlessArrayOf
989 inline const Type& operator [] (unsigned int i) const
991 if (unlikely (i >= len || !i)) return Null(Type);
994 inline unsigned int get_size (void) const
995 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
997 inline bool serialize (hb_serialize_context_t *c,
998 Supplier<Type> &items,
999 unsigned int items_len)
1001 TRACE_SERIALIZE (this);
1002 if (unlikely (!c->extend_min (*this))) return_trace (false);
1003 len.set (items_len); /* TODO(serialize) Overflow? */
1004 if (unlikely (!items_len)) return_trace (true);
1005 if (unlikely (!c->extend (*this))) return_trace (false);
1006 for (unsigned int i = 0; i < items_len - 1; i++)
1007 array[i] = items[i];
1008 items.advance (items_len - 1);
1009 return_trace (true);
1012 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
1014 return c->check_struct (this)
1015 && c->check_array (this, Type::static_size, len);
1018 inline bool sanitize (hb_sanitize_context_t *c) const
1020 TRACE_SANITIZE (this);
1021 if (unlikely (!sanitize_shallow (c))) return_trace (false);
1023 /* Note: for structs that do not reference other structs,
1024 * we do not need to call their sanitize() as we already did
1025 * a bound check on the aggregate array size. We just include
1026 * a small unreachable expression to make sure the structs
1027 * pointed to do have a simple sanitize(), ie. they do not
1028 * reference other structs via offsets.
1030 (void) (false && array[0].sanitize (c));
1032 return_trace (true);
1038 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
1042 /* An array with sorted elements. Supports binary searching. */
1043 template <typename Type, typename LenType=USHORT>
1044 struct SortedArrayOf : ArrayOf<Type, LenType>
1046 template <typename SearchType>
1047 inline int bsearch (const SearchType &x) const
1049 /* Hand-coded bsearch here since this is in the hot inner loop. */
1050 int min = 0, max = (int) this->len - 1;
1053 int mid = (min + max) / 2;
1054 int c = this->array[mid].cmp (x);
1067 /* Lazy struct and blob loaders. */
1069 /* Logic is shared between hb_lazy_loader_t and hb_lazy_table_loader_t */
1070 template <typename T>
1071 struct hb_lazy_loader_t
1073 inline void init (hb_face_t *face_)
1079 inline void fini (void)
1081 if (instance && instance != &OT::Null(T))
1088 inline const T* get (void) const
1091 T *p = (T *) hb_atomic_ptr_get (&instance);
1094 p = (T *) calloc (1, sizeof (T));
1096 p = const_cast<T *> (&OT::Null(T));
1099 if (unlikely (!hb_atomic_ptr_cmpexch (const_cast<T **>(&instance), NULL, p)))
1101 if (p != &OT::Null(T))
1109 inline const T* operator-> (void) const
1119 /* Logic is shared between hb_lazy_loader_t and hb_lazy_table_loader_t */
1120 template <typename T>
1121 struct hb_lazy_table_loader_t
1123 inline void init (hb_face_t *face_)
1130 inline void fini (void)
1132 hb_blob_destroy (blob);
1135 inline const T* get (void) const
1138 T *p = (T *) hb_atomic_ptr_get (&instance);
1141 hb_blob_t *blob_ = OT::Sanitizer<T>::sanitize (face->reference_table (T::tableTag));
1142 p = const_cast<T *>(OT::Sanitizer<T>::lock_instance (blob_));
1143 if (!hb_atomic_ptr_cmpexch (const_cast<T **>(&instance), NULL, p))
1145 hb_blob_destroy (blob_);
1153 inline const T* operator-> (void) const
1161 mutable hb_blob_t *blob;
1165 } /* namespace OT */
1168 #endif /* HB_OPEN_TYPE_PRIVATE_HH */