1 | /* |
2 | * Copyright © 2007,2008,2009,2010 Red Hat, Inc. |
3 | * Copyright © 2012 Google, Inc. |
4 | * |
5 | * This is part of HarfBuzz, a text shaping library. |
6 | * |
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. |
12 | * |
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 |
17 | * DAMAGE. |
18 | * |
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. |
24 | * |
25 | * Red Hat Author(s): Behdad Esfahbod |
26 | * Google Author(s): Behdad Esfahbod |
27 | */ |
28 | |
29 | #ifndef HB_OPEN_TYPE_HH |
30 | #define HB_OPEN_TYPE_HH |
31 | |
32 | #include "hb.hh" |
33 | #include "hb-blob.hh" |
34 | #include "hb-face.hh" |
35 | #include "hb-machinery.hh" |
36 | #include "hb-subset.hh" |
37 | |
38 | |
39 | namespace OT { |
40 | |
41 | |
42 | /* |
43 | * |
44 | * The OpenType Font File: Data Types |
45 | */ |
46 | |
47 | |
48 | /* "The following data types are used in the OpenType font file. |
49 | * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
50 | |
51 | /* |
52 | * Int types |
53 | */ |
54 | |
55 | /* Integer types in big-endian order and no alignment requirement */ |
56 | template <typename Type, unsigned int Size> |
57 | struct IntType |
58 | { |
59 | inline void set (Type i) { v.set (i); } |
60 | inline operator Type(void) const { return v; } |
61 | inline bool operator == (const IntType<Type,Size> &o) const { return (Type) v == (Type) o.v; } |
62 | inline bool operator != (const IntType<Type,Size> &o) const { return !(*this == o); } |
63 | static inline int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); } |
64 | template <typename Type2> |
65 | inline int cmp (Type2 a) const |
66 | { |
67 | Type b = v; |
68 | if (sizeof (Type) < sizeof (int) && sizeof (Type2) < sizeof (int)) |
69 | return (int) a - (int) b; |
70 | else |
71 | return a < b ? -1 : a == b ? 0 : +1; |
72 | } |
73 | inline bool sanitize (hb_sanitize_context_t *c) const |
74 | { |
75 | TRACE_SANITIZE (this); |
76 | return_trace (likely (c->check_struct (this))); |
77 | } |
78 | protected: |
79 | BEInt<Type, Size> v; |
80 | public: |
81 | DEFINE_SIZE_STATIC (Size); |
82 | }; |
83 | |
84 | typedef IntType<uint8_t, 1> HBUINT8; /* 8-bit unsigned integer. */ |
85 | typedef IntType<int8_t, 1> HBINT8; /* 8-bit signed integer. */ |
86 | typedef IntType<uint16_t, 2> HBUINT16; /* 16-bit unsigned integer. */ |
87 | typedef IntType<int16_t, 2> HBINT16; /* 16-bit signed integer. */ |
88 | typedef IntType<uint32_t, 4> HBUINT32; /* 32-bit unsigned integer. */ |
89 | typedef IntType<int32_t, 4> HBINT32; /* 32-bit signed integer. */ |
90 | typedef IntType<uint32_t, 3> HBUINT24; /* 24-bit unsigned integer. */ |
91 | |
92 | /* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */ |
93 | typedef HBINT16 FWORD; |
94 | |
95 | /* 16-bit unsigned integer (HBUINT16) that describes a quantity in FUnits. */ |
96 | typedef HBUINT16 UFWORD; |
97 | |
98 | /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */ |
99 | struct F2DOT14 : HBINT16 |
100 | { |
101 | // 16384 means 1<<14 |
102 | inline float to_float (void) const { return ((int32_t) v) / 16384.f; } |
103 | inline void set_float (float f) { v.set (round (f * 16384.f)); } |
104 | public: |
105 | DEFINE_SIZE_STATIC (2); |
106 | }; |
107 | |
108 | /* 32-bit signed fixed-point number (16.16). */ |
109 | struct Fixed : HBINT32 |
110 | { |
111 | // 65536 means 1<<16 |
112 | inline float to_float (void) const { return ((int32_t) v) / 65536.f; } |
113 | inline void set_float (float f) { v.set (round (f * 65536.f)); } |
114 | public: |
115 | DEFINE_SIZE_STATIC (4); |
116 | }; |
117 | |
118 | /* Date represented in number of seconds since 12:00 midnight, January 1, |
119 | * 1904. The value is represented as a signed 64-bit integer. */ |
120 | struct LONGDATETIME |
121 | { |
122 | inline bool sanitize (hb_sanitize_context_t *c) const |
123 | { |
124 | TRACE_SANITIZE (this); |
125 | return_trace (likely (c->check_struct (this))); |
126 | } |
127 | protected: |
128 | HBINT32 major; |
129 | HBUINT32 minor; |
130 | public: |
131 | DEFINE_SIZE_STATIC (8); |
132 | }; |
133 | |
134 | /* Array of four uint8s (length = 32 bits) used to identify a script, language |
135 | * system, feature, or baseline */ |
136 | struct Tag : HBUINT32 |
137 | { |
138 | /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ |
139 | inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); } |
140 | inline operator char* (void) { return reinterpret_cast<char *> (&this->v); } |
141 | public: |
142 | DEFINE_SIZE_STATIC (4); |
143 | }; |
144 | |
145 | /* Glyph index number, same as uint16 (length = 16 bits) */ |
146 | typedef HBUINT16 GlyphID; |
147 | |
148 | /* Name-table index, same as uint16 (length = 16 bits) */ |
149 | typedef HBUINT16 NameID; |
150 | |
151 | /* Script/language-system/feature index */ |
152 | struct Index : HBUINT16 { |
153 | enum { NOT_FOUND_INDEX = 0xFFFFu }; |
154 | }; |
155 | DECLARE_NULL_NAMESPACE_BYTES (OT, Index); |
156 | |
157 | /* Offset, Null offset = 0 */ |
158 | template <typename Type> |
159 | struct Offset : Type |
160 | { |
161 | inline bool is_null (void) const { return 0 == *this; } |
162 | |
163 | inline void *serialize (hb_serialize_context_t *c, const void *base) |
164 | { |
165 | void *t = c->start_embed<void> (); |
166 | this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */ |
167 | return t; |
168 | } |
169 | |
170 | public: |
171 | DEFINE_SIZE_STATIC (sizeof(Type)); |
172 | }; |
173 | |
174 | typedef Offset<HBUINT16> Offset16; |
175 | typedef Offset<HBUINT32> Offset32; |
176 | |
177 | |
178 | /* CheckSum */ |
179 | struct CheckSum : HBUINT32 |
180 | { |
181 | /* This is reference implementation from the spec. */ |
182 | static inline uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length) |
183 | { |
184 | uint32_t Sum = 0L; |
185 | assert (0 == (Length & 3)); |
186 | const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size; |
187 | |
188 | while (Table < EndPtr) |
189 | Sum += *Table++; |
190 | return Sum; |
191 | } |
192 | |
193 | /* Note: data should be 4byte aligned and have 4byte padding at the end. */ |
194 | inline void set_for_data (const void *data, unsigned int length) |
195 | { set (CalcTableChecksum ((const HBUINT32 *) data, length)); } |
196 | |
197 | public: |
198 | DEFINE_SIZE_STATIC (4); |
199 | }; |
200 | |
201 | |
202 | /* |
203 | * Version Numbers |
204 | */ |
205 | |
206 | template <typename FixedType=HBUINT16> |
207 | struct FixedVersion |
208 | { |
209 | inline uint32_t to_int (void) const { return (major << (sizeof(FixedType) * 8)) + minor; } |
210 | |
211 | inline bool sanitize (hb_sanitize_context_t *c) const |
212 | { |
213 | TRACE_SANITIZE (this); |
214 | return_trace (c->check_struct (this)); |
215 | } |
216 | |
217 | FixedType major; |
218 | FixedType minor; |
219 | public: |
220 | DEFINE_SIZE_STATIC (2 * sizeof(FixedType)); |
221 | }; |
222 | |
223 | |
224 | /* |
225 | * Template subclasses of Offset that do the dereferencing. |
226 | * Use: (base+offset) |
227 | */ |
228 | |
229 | template <typename Type, typename OffsetType=HBUINT16> |
230 | struct OffsetTo : Offset<OffsetType> |
231 | { |
232 | inline const Type& operator () (const void *base) const |
233 | { |
234 | unsigned int offset = *this; |
235 | if (unlikely (!offset)) return Null(Type); |
236 | return StructAtOffset<const Type> (base, offset); |
237 | } |
238 | inline Type& operator () (void *base) const |
239 | { |
240 | unsigned int offset = *this; |
241 | if (unlikely (!offset)) return Crap(Type); |
242 | return StructAtOffset<Type> (base, offset); |
243 | } |
244 | |
245 | inline Type& serialize (hb_serialize_context_t *c, const void *base) |
246 | { |
247 | return * (Type *) Offset<OffsetType>::serialize (c, base); |
248 | } |
249 | |
250 | template <typename T> |
251 | inline void serialize_subset (hb_subset_context_t *c, const T &src, const void *base) |
252 | { |
253 | if (&src == &Null(T)) |
254 | { |
255 | this->set (0); |
256 | return; |
257 | } |
258 | serialize (c->serializer, base); |
259 | if (!src.subset (c)) |
260 | this->set (0); |
261 | } |
262 | |
263 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
264 | { |
265 | TRACE_SANITIZE (this); |
266 | if (unlikely (!c->check_struct (this))) return_trace (false); |
267 | unsigned int offset = *this; |
268 | if (unlikely (!offset)) return_trace (true); |
269 | if (unlikely (!c->check_range (base, offset))) return_trace (false); |
270 | const Type &obj = StructAtOffset<Type> (base, offset); |
271 | return_trace (likely (obj.sanitize (c)) || neuter (c)); |
272 | } |
273 | template <typename T> |
274 | inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const |
275 | { |
276 | TRACE_SANITIZE (this); |
277 | if (unlikely (!c->check_struct (this))) return_trace (false); |
278 | unsigned int offset = *this; |
279 | if (unlikely (!offset)) return_trace (true); |
280 | if (unlikely (!c->check_range (base, offset))) return_trace (false); |
281 | const Type &obj = StructAtOffset<Type> (base, offset); |
282 | return_trace (likely (obj.sanitize (c, user_data)) || neuter (c)); |
283 | } |
284 | |
285 | /* Set the offset to Null */ |
286 | inline bool neuter (hb_sanitize_context_t *c) const { |
287 | return c->try_set (this, 0); |
288 | } |
289 | DEFINE_SIZE_STATIC (sizeof(OffsetType)); |
290 | }; |
291 | template <typename Type> struct LOffsetTo : OffsetTo<Type, HBUINT32> {}; |
292 | template <typename Base, typename OffsetType, typename Type> |
293 | static inline const Type& operator + (const Base &base, const OffsetTo<Type, OffsetType> &offset) { return offset (base); } |
294 | template <typename Base, typename OffsetType, typename Type> |
295 | static inline Type& operator + (Base &base, OffsetTo<Type, OffsetType> &offset) { return offset (base); } |
296 | |
297 | |
298 | /* |
299 | * Array Types |
300 | */ |
301 | |
302 | /* TODO Use it in ArrayOf, HeadlessArrayOf, and other places around the code base?? */ |
303 | template <typename Type> |
304 | struct UnsizedArrayOf |
305 | { |
306 | inline const Type& operator [] (unsigned int i) const { return arrayZ[i]; } |
307 | inline Type& operator [] (unsigned int i) { return arrayZ[i]; } |
308 | |
309 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const |
310 | { |
311 | TRACE_SANITIZE (this); |
312 | if (unlikely (!sanitize_shallow (c, count))) return_trace (false); |
313 | |
314 | /* Note: for structs that do not reference other structs, |
315 | * we do not need to call their sanitize() as we already did |
316 | * a bound check on the aggregate array size. We just include |
317 | * a small unreachable expression to make sure the structs |
318 | * pointed to do have a simple sanitize(), ie. they do not |
319 | * reference other structs via offsets. |
320 | */ |
321 | (void) (false && arrayZ[0].sanitize (c)); |
322 | |
323 | return_trace (true); |
324 | } |
325 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count, const void *base) const |
326 | { |
327 | TRACE_SANITIZE (this); |
328 | if (unlikely (!sanitize_shallow (c, count))) return_trace (false); |
329 | for (unsigned int i = 0; i < count; i++) |
330 | if (unlikely (!arrayZ[i].sanitize (c, base))) |
331 | return_trace (false); |
332 | return_trace (true); |
333 | } |
334 | template <typename T> |
335 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count, const void *base, T user_data) const |
336 | { |
337 | TRACE_SANITIZE (this); |
338 | if (unlikely (!sanitize_shallow (c, count))) return_trace (false); |
339 | for (unsigned int i = 0; i < count; i++) |
340 | if (unlikely (!arrayZ[i].sanitize (c, base, user_data))) |
341 | return_trace (false); |
342 | return_trace (true); |
343 | } |
344 | |
345 | inline bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const |
346 | { |
347 | TRACE_SANITIZE (this); |
348 | return_trace (c->check_array (arrayZ, arrayZ[0].static_size, count)); |
349 | } |
350 | |
351 | public: |
352 | Type arrayZ[VAR]; |
353 | public: |
354 | DEFINE_SIZE_ARRAY (0, arrayZ); |
355 | }; |
356 | |
357 | /* Unsized array of offset's */ |
358 | template <typename Type, typename OffsetType> |
359 | struct UnsizedOffsetArrayOf : UnsizedArrayOf<OffsetTo<Type, OffsetType> > {}; |
360 | |
361 | /* Unsized array of offsets relative to the beginning of the array itself. */ |
362 | template <typename Type, typename OffsetType> |
363 | struct UnsizedOffsetListOf : UnsizedOffsetArrayOf<Type, OffsetType> |
364 | { |
365 | inline const Type& operator [] (unsigned int i) const |
366 | { |
367 | return this+this->arrayZ[i]; |
368 | } |
369 | |
370 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const |
371 | { |
372 | TRACE_SANITIZE (this); |
373 | return_trace ((UnsizedOffsetArrayOf<Type, OffsetType>::sanitize (c, count, this))); |
374 | } |
375 | template <typename T> |
376 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count, T user_data) const |
377 | { |
378 | TRACE_SANITIZE (this); |
379 | return_trace ((UnsizedOffsetArrayOf<Type, OffsetType>::sanitize (c, count, this, user_data))); |
380 | } |
381 | }; |
382 | |
383 | /* An array with a number of elements. */ |
384 | template <typename Type, typename LenType=HBUINT16> |
385 | struct ArrayOf |
386 | { |
387 | const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const |
388 | { |
389 | unsigned int count = len; |
390 | if (unlikely (start_offset > count)) |
391 | count = 0; |
392 | else |
393 | count -= start_offset; |
394 | count = MIN (count, *pcount); |
395 | *pcount = count; |
396 | return arrayZ + start_offset; |
397 | } |
398 | |
399 | inline const Type& operator [] (unsigned int i) const |
400 | { |
401 | if (unlikely (i >= len)) return Null(Type); |
402 | return arrayZ[i]; |
403 | } |
404 | inline Type& operator [] (unsigned int i) |
405 | { |
406 | if (unlikely (i >= len)) return Crap(Type); |
407 | return arrayZ[i]; |
408 | } |
409 | inline unsigned int get_size (void) const |
410 | { return len.static_size + len * Type::static_size; } |
411 | |
412 | inline bool serialize (hb_serialize_context_t *c, |
413 | unsigned int items_len) |
414 | { |
415 | TRACE_SERIALIZE (this); |
416 | if (unlikely (!c->extend_min (*this))) return_trace (false); |
417 | len.set (items_len); /* TODO(serialize) Overflow? */ |
418 | if (unlikely (!c->extend (*this))) return_trace (false); |
419 | return_trace (true); |
420 | } |
421 | inline bool serialize (hb_serialize_context_t *c, |
422 | Supplier<Type> &items, |
423 | unsigned int items_len) |
424 | { |
425 | TRACE_SERIALIZE (this); |
426 | if (unlikely (!serialize (c, items_len))) return_trace (false); |
427 | for (unsigned int i = 0; i < items_len; i++) |
428 | arrayZ[i] = items[i]; |
429 | items += items_len; |
430 | return_trace (true); |
431 | } |
432 | |
433 | inline bool sanitize (hb_sanitize_context_t *c) const |
434 | { |
435 | TRACE_SANITIZE (this); |
436 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
437 | |
438 | /* Note: for structs that do not reference other structs, |
439 | * we do not need to call their sanitize() as we already did |
440 | * a bound check on the aggregate array size. We just include |
441 | * a small unreachable expression to make sure the structs |
442 | * pointed to do have a simple sanitize(), ie. they do not |
443 | * reference other structs via offsets. |
444 | */ |
445 | (void) (false && arrayZ[0].sanitize (c)); |
446 | |
447 | return_trace (true); |
448 | } |
449 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
450 | { |
451 | TRACE_SANITIZE (this); |
452 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
453 | unsigned int count = len; |
454 | for (unsigned int i = 0; i < count; i++) |
455 | if (unlikely (!arrayZ[i].sanitize (c, base))) |
456 | return_trace (false); |
457 | return_trace (true); |
458 | } |
459 | template <typename T> |
460 | inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const |
461 | { |
462 | TRACE_SANITIZE (this); |
463 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
464 | unsigned int count = len; |
465 | for (unsigned int i = 0; i < count; i++) |
466 | if (unlikely (!arrayZ[i].sanitize (c, base, user_data))) |
467 | return_trace (false); |
468 | return_trace (true); |
469 | } |
470 | |
471 | template <typename SearchType> |
472 | inline int lsearch (const SearchType &x) const |
473 | { |
474 | unsigned int count = len; |
475 | for (unsigned int i = 0; i < count; i++) |
476 | if (!this->arrayZ[i].cmp (x)) |
477 | return i; |
478 | return -1; |
479 | } |
480 | |
481 | inline void qsort (void) |
482 | { |
483 | ::qsort (arrayZ, len, sizeof (Type), Type::cmp); |
484 | } |
485 | |
486 | private: |
487 | inline bool sanitize_shallow (hb_sanitize_context_t *c) const |
488 | { |
489 | TRACE_SANITIZE (this); |
490 | return_trace (len.sanitize (c) && c->check_array (arrayZ, Type::static_size, len)); |
491 | } |
492 | |
493 | public: |
494 | LenType len; |
495 | Type arrayZ[VAR]; |
496 | public: |
497 | DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ); |
498 | }; |
499 | template <typename Type> struct LArrayOf : ArrayOf<Type, HBUINT32> {}; |
500 | typedef ArrayOf<HBUINT8, HBUINT8> PString; |
501 | |
502 | /* Array of Offset's */ |
503 | template <typename Type, typename OffsetType=HBUINT16> |
504 | struct OffsetArrayOf : ArrayOf<OffsetTo<Type, OffsetType> > {}; |
505 | |
506 | /* Array of offsets relative to the beginning of the array itself. */ |
507 | template <typename Type> |
508 | struct OffsetListOf : OffsetArrayOf<Type> |
509 | { |
510 | inline const Type& operator [] (unsigned int i) const |
511 | { |
512 | if (unlikely (i >= this->len)) return Null(Type); |
513 | return this+this->arrayZ[i]; |
514 | } |
515 | inline const Type& operator [] (unsigned int i) |
516 | { |
517 | if (unlikely (i >= this->len)) return Crap(Type); |
518 | return this+this->arrayZ[i]; |
519 | } |
520 | |
521 | inline bool subset (hb_subset_context_t *c) const |
522 | { |
523 | TRACE_SUBSET (this); |
524 | struct OffsetListOf<Type> *out = c->serializer->embed (*this); |
525 | if (unlikely (!out)) return_trace (false); |
526 | unsigned int count = this->len; |
527 | for (unsigned int i = 0; i < count; i++) |
528 | out->arrayZ[i].serialize_subset (c, (*this)[i], out); |
529 | return_trace (true); |
530 | } |
531 | |
532 | inline bool sanitize (hb_sanitize_context_t *c) const |
533 | { |
534 | TRACE_SANITIZE (this); |
535 | return_trace (OffsetArrayOf<Type>::sanitize (c, this)); |
536 | } |
537 | template <typename T> |
538 | inline bool sanitize (hb_sanitize_context_t *c, T user_data) const |
539 | { |
540 | TRACE_SANITIZE (this); |
541 | return_trace (OffsetArrayOf<Type>::sanitize (c, this, user_data)); |
542 | } |
543 | }; |
544 | |
545 | /* An array starting at second element. */ |
546 | template <typename Type, typename LenType=HBUINT16> |
547 | struct HeadlessArrayOf |
548 | { |
549 | inline const Type& operator [] (unsigned int i) const |
550 | { |
551 | if (unlikely (i >= len || !i)) return Null(Type); |
552 | return arrayZ[i-1]; |
553 | } |
554 | inline Type& operator [] (unsigned int i) |
555 | { |
556 | if (unlikely (i >= len || !i)) return Crap(Type); |
557 | return arrayZ[i-1]; |
558 | } |
559 | inline unsigned int get_size (void) const |
560 | { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } |
561 | |
562 | inline bool serialize (hb_serialize_context_t *c, |
563 | Supplier<Type> &items, |
564 | unsigned int items_len) |
565 | { |
566 | TRACE_SERIALIZE (this); |
567 | if (unlikely (!c->extend_min (*this))) return_trace (false); |
568 | len.set (items_len); /* TODO(serialize) Overflow? */ |
569 | if (unlikely (!items_len)) return_trace (true); |
570 | if (unlikely (!c->extend (*this))) return_trace (false); |
571 | for (unsigned int i = 0; i < items_len - 1; i++) |
572 | arrayZ[i] = items[i]; |
573 | items += items_len - 1; |
574 | return_trace (true); |
575 | } |
576 | |
577 | inline bool sanitize (hb_sanitize_context_t *c) const |
578 | { |
579 | TRACE_SANITIZE (this); |
580 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
581 | |
582 | /* Note: for structs that do not reference other structs, |
583 | * we do not need to call their sanitize() as we already did |
584 | * a bound check on the aggregate array size. We just include |
585 | * a small unreachable expression to make sure the structs |
586 | * pointed to do have a simple sanitize(), ie. they do not |
587 | * reference other structs via offsets. |
588 | */ |
589 | (void) (false && arrayZ[0].sanitize (c)); |
590 | |
591 | return_trace (true); |
592 | } |
593 | |
594 | private: |
595 | inline bool sanitize_shallow (hb_sanitize_context_t *c) const |
596 | { |
597 | TRACE_SANITIZE (this); |
598 | return_trace (len.sanitize (c) && |
599 | (!len || c->check_array (arrayZ, Type::static_size, len - 1))); |
600 | } |
601 | |
602 | public: |
603 | LenType len; |
604 | Type arrayZ[VAR]; |
605 | public: |
606 | DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ); |
607 | }; |
608 | |
609 | /* An array with sorted elements. Supports binary searching. */ |
610 | template <typename Type, typename LenType=HBUINT16> |
611 | struct SortedArrayOf : ArrayOf<Type, LenType> |
612 | { |
613 | template <typename SearchType> |
614 | inline int bsearch (const SearchType &x) const |
615 | { |
616 | /* Hand-coded bsearch here since this is in the hot inner loop. */ |
617 | const Type *arr = this->arrayZ; |
618 | int min = 0, max = (int) this->len - 1; |
619 | while (min <= max) |
620 | { |
621 | int mid = (min + max) / 2; |
622 | int c = arr[mid].cmp (x); |
623 | if (c < 0) |
624 | max = mid - 1; |
625 | else if (c > 0) |
626 | min = mid + 1; |
627 | else |
628 | return mid; |
629 | } |
630 | return -1; |
631 | } |
632 | }; |
633 | |
634 | /* Binary-search arrays */ |
635 | struct |
636 | { |
637 | inline (void) const { return len; } |
638 | |
639 | inline bool (hb_sanitize_context_t *c) const |
640 | { |
641 | TRACE_SANITIZE (this); |
642 | return_trace (c->check_struct (this)); |
643 | } |
644 | |
645 | inline void (unsigned int v) |
646 | { |
647 | len.set (v); |
648 | assert (len == v); |
649 | entrySelector.set (MAX (1u, hb_bit_storage (v)) - 1); |
650 | searchRange.set (16 * (1u << entrySelector)); |
651 | rangeShift.set (v * 16 > searchRange |
652 | ? 16 * v - searchRange |
653 | : 0); |
654 | } |
655 | |
656 | protected: |
657 | HBUINT16 ; |
658 | HBUINT16 ; |
659 | HBUINT16 ; |
660 | HBUINT16 ; |
661 | |
662 | public: |
663 | DEFINE_SIZE_STATIC (8); |
664 | }; |
665 | |
666 | template <typename Type> |
667 | struct BinSearchArrayOf : SortedArrayOf<Type, BinSearchHeader> {}; |
668 | |
669 | |
670 | } /* namespace OT */ |
671 | |
672 | |
673 | #endif /* HB_OPEN_TYPE_HH */ |
674 | |