blob: 7e524177f6705158982e0c84d3d7d451d8e159b1 [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02003 * Copyright © 2012 Google, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05006 *
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
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +020026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -050027 */
28
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070029#ifndef HB_OPEN_TYPE_HH
30#define HB_OPEN_TYPE_HH
Behdad Esfahbod12c45682006-12-28 06:10:59 -050031
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070032#include "hb.hh"
33#include "hb-blob.hh"
34#include "hb-face.hh"
35#include "hb-machinery.hh"
Behdad Esfahbodd1f29902018-08-31 16:31:00 -070036#include "hb-subset.hh"
Behdad Esfahbod12c45682006-12-28 06:10:59 -050037
Behdad Esfahboda16ecbf2008-01-23 17:01:55 -050038
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040039namespace OT {
40
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040041
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -050042/*
43 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040044 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -050045 */
46
47
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -050048/* "The following data types are used in the OpenType font file.
49 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -050050
Behdad Esfahbod5f810362009-05-17 00:54:25 -040051/*
52 * Int types
53 */
54
Behdad Esfahbod2467c662010-04-21 23:11:45 -040055/* Integer types in big-endian order and no alignment requirement */
Behdad Esfahbodcc16b262021-02-22 17:55:47 -070056template <typename Type,
Behdad Esfahbod567cedc2021-02-22 22:09:15 -070057 unsigned int Size = sizeof (Type)>
Behdad Esfahbode032ed92010-04-21 03:11:46 -040058struct IntType
59{
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -070060 typedef Type type;
Behdad Esfahbod11d2f492018-12-01 13:12:21 -050061
Behdad Esfahbodcc16b262021-02-22 17:55:47 -070062 IntType () = default;
Behdad Esfahbod567cedc2021-02-22 22:09:15 -070063 explicit constexpr IntType (Type V) : v {V} {}
64 IntType& operator = (Type i) { v = i; return *this; }
Behdad Esfahbod486da352021-02-23 13:58:14 -070065 /* For reason we define cast out operator for signed/unsigned, instead of Type, see:
66 * https://github.com/harfbuzz/harfbuzz/pull/2875/commits/09836013995cab2b9f07577a179ad7b024130467 */
Behdad Esfahbod7a078c32021-11-02 00:44:51 -060067 operator typename std::conditional<std::is_signed<Type>::value, signed, unsigned>::type () const { return v; }
Behdad Esfahbod09836012021-02-22 22:33:17 -070068
Behdad Esfahbod8938dd22019-06-17 14:12:11 -070069 bool operator == (const IntType &o) const { return (Type) v == (Type) o.v; }
70 bool operator != (const IntType &o) const { return !(*this == o); }
Behdad Esfahbod307bd6d2019-08-28 13:49:17 -070071
72 IntType& operator += (unsigned count) { *this = *this + count; return *this; }
73 IntType& operator -= (unsigned count) { *this = *this - count; return *this; }
74 IntType& operator ++ () { *this += 1; return *this; }
75 IntType& operator -- () { *this -= 1; return *this; }
76 IntType operator ++ (int) { IntType c (*this); ++*this; return c; }
77 IntType operator -- (int) { IntType c (*this); --*this; return c; }
78
Behdad Esfahbod8938dd22019-06-17 14:12:11 -070079 HB_INTERNAL static int cmp (const IntType *a, const IntType *b)
Behdad Esfahbod95df00a2019-04-12 17:50:03 -040080 { return b->cmp (*a); }
Qunxin Liu82afc752020-02-04 13:24:37 -080081 HB_INTERNAL static int cmp (const void *a, const void *b)
82 {
83 IntType *pa = (IntType *) a;
84 IntType *pb = (IntType *) b;
85
86 return pb->cmp (*pa);
87 }
Behdad Esfahbod98374ce2021-02-05 13:40:10 -050088 template <typename Type2,
Behdad Esfahbod943921c2021-11-02 00:26:46 -060089 hb_enable_if (std::is_integral<Type2>::value &&
Behdad Esfahbod98374ce2021-02-05 13:40:10 -050090 sizeof (Type2) < sizeof (int) &&
91 sizeof (Type) < sizeof (int))>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033092 int cmp (Type2 a) const
Behdad Esfahbod88a399a2015-02-19 16:57:12 +030093 {
94 Type b = v;
Behdad Esfahbod98374ce2021-02-05 13:40:10 -050095 return (int) a - (int) b;
96 }
97 template <typename Type2,
98 hb_enable_if (hb_is_convertible (Type2, Type))>
99 int cmp (Type2 a) const
100 {
101 Type b = v;
102 return a < b ? -1 : a == b ? 0 : +1;
Behdad Esfahbod88a399a2015-02-19 16:57:12 +0300103 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330104 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300105 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500106 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100107 return_trace (likely (c->check_struct (this)));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400108 }
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400109 protected:
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500110 BEInt<Type, Size> v;
Behdad Esfahbod569da922010-05-10 16:38:32 -0400111 public:
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500112 DEFINE_SIZE_STATIC (Size);
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400113};
114
Behdad Esfahbode5b7bc42020-06-29 01:24:02 -0700115typedef IntType<uint8_t> HBUINT8; /* 8-bit unsigned integer. */
116typedef IntType<int8_t> HBINT8; /* 8-bit signed integer. */
117typedef IntType<uint16_t> HBUINT16; /* 16-bit unsigned integer. */
118typedef IntType<int16_t> HBINT16; /* 16-bit signed integer. */
119typedef IntType<uint32_t> HBUINT32; /* 32-bit unsigned integer. */
120typedef IntType<int32_t> HBINT32; /* 32-bit signed integer. */
Behdad Esfahbod11d2f492018-12-01 13:12:21 -0500121/* Note: we cannot defined a signed HBINT24 because there's no corresponding C type.
122 * Works for unsigned, but not signed, since we rely on compiler for sign-extension. */
Behdad Esfahbodc3a8b042018-12-01 00:14:48 -0500123typedef IntType<uint32_t, 3> HBUINT24; /* 24-bit unsigned integer. */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400124
Behdad Esfahbod14a2df72021-09-19 23:06:09 -0400125/* 15-bit unsigned number; top bit used for extension. */
126struct HBUINT15 : HBUINT16
127{
128 /* TODO Flesh out; actually mask top bit. */
129 HBUINT15& operator = (uint16_t i ) { HBUINT16::operator= (i); return *this; }
130 public:
131 DEFINE_SIZE_STATIC (2);
132};
133
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100134/* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */
135typedef HBINT16 FWORD;
Behdad Esfahbodae9877d2011-08-17 14:43:45 +0200136
Behdad Esfahbod22955b22018-10-10 19:58:20 -0400137/* 32-bit signed integer (HBINT32) that describes a quantity in FUnits. */
138typedef HBINT32 FWORD32;
139
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100140/* 16-bit unsigned integer (HBUINT16) that describes a quantity in FUnits. */
141typedef HBUINT16 UFWORD;
Behdad Esfahbodae9877d2011-08-17 14:43:45 +0200142
Behdad Esfahbod68b62962016-03-01 16:41:53 +0900143/* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100144struct F2DOT14 : HBINT16
Behdad Esfahbod68b62962016-03-01 16:41:53 +0900145{
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700146 F2DOT14& operator = (uint16_t i ) { HBINT16::operator= (i); return *this; }
Ebrahim Byagowice99dd02018-04-15 22:08:50 +0430147 // 16384 means 1<<14
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330148 float to_float () const { return ((int32_t) v) / 16384.f; }
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700149 void set_float (float f) { v = roundf (f * 16384.f); }
Behdad Esfahbod68b62962016-03-01 16:41:53 +0900150 public:
151 DEFINE_SIZE_STATIC (2);
152};
153
Behdad Esfahbod587d4622016-04-30 19:20:56 +0200154/* 32-bit signed fixed-point number (16.16). */
Behdad Esfahbod229ef1d2019-09-10 10:31:07 -0700155struct HBFixed : HBINT32
Behdad Esfahbod587d4622016-04-30 19:20:56 +0200156{
Behdad Esfahbod229ef1d2019-09-10 10:31:07 -0700157 HBFixed& operator = (uint32_t i) { HBINT32::operator= (i); return *this; }
Ebrahim Byagowice99dd02018-04-15 22:08:50 +0430158 // 65536 means 1<<16
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330159 float to_float () const { return ((int32_t) v) / 65536.f; }
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700160 void set_float (float f) { v = roundf (f * 65536.f); }
Behdad Esfahbod587d4622016-04-30 19:20:56 +0200161 public:
162 DEFINE_SIZE_STATIC (4);
163};
164
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400165/* Date represented in number of seconds since 12:00 midnight, January 1,
166 * 1904. The value is represented as a signed 64-bit integer. */
167struct LONGDATETIME
168{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330169 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300170 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500171 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100172 return_trace (likely (c->check_struct (this)));
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400173 }
Behdad Esfahbod6775da32014-01-23 14:18:49 -0500174 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100175 HBINT32 major;
176 HBUINT32 minor;
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400177 public:
178 DEFINE_SIZE_STATIC (8);
179};
180
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500181/* Array of four uint8s (length = 32 bits) used to identify a script, language
182 * system, feature, or baseline */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100183struct Tag : HBUINT32
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400184{
Behdad Esfahbod6492b232019-06-17 14:19:13 -0700185 Tag& operator = (hb_tag_t i) { HBUINT32::operator= (i); return *this; }
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500186 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahbod8b2f9ad2021-02-22 17:42:24 -0700187 operator const char* () const { return reinterpret_cast<const char *> (this); }
188 operator char* () { return reinterpret_cast<char *> (this); }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400189 public:
190 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500191};
192
193/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbodc852b862021-09-19 16:30:12 -0400194struct HBGlyphID16 : HBUINT16
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700195{
Behdad Esfahbodc852b862021-09-19 16:30:12 -0400196 HBGlyphID16& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700197};
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500198
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400199/* Script/language-system/feature index */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100200struct Index : HBUINT16 {
Behdad Esfahbod5d4b0372019-01-22 12:11:24 +0100201 static constexpr unsigned NOT_FOUND_INDEX = 0xFFFFu;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700202 Index& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400203};
Behdad Esfahbod92806ee2018-08-05 21:41:52 -0700204DECLARE_NULL_NAMESPACE_BYTES (OT, Index);
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400205
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700206typedef Index NameID;
207
Behdad Esfahbod9ffc46b2021-03-31 11:26:18 -0600208struct VarIdx : HBUINT32 {
209 static constexpr unsigned NO_VARIATION = 0xFFFFFFFFu;
210 VarIdx& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; }
211};
212DECLARE_NULL_NAMESPACE_BYTES (OT, VarIdx);
213
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400214/* Offset, Null offset = 0 */
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200215template <typename Type, bool has_null=true>
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400216struct Offset : Type
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600217{
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700218 Offset& operator = (typename Type::type i) { Type::operator= (i); return *this; }
219
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700220 typedef Type type;
221
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330222 bool is_null () const { return has_null && 0 == *this; }
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600223
Behdad Esfahboddf1c7d52018-02-25 19:06:25 -0800224 public:
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330225 DEFINE_SIZE_STATIC (sizeof (Type));
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600226};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400227
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100228typedef Offset<HBUINT16> Offset16;
Behdad Esfahbod21792812021-03-31 11:20:21 -0600229typedef Offset<HBUINT24> Offset24;
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100230typedef Offset<HBUINT32> Offset32;
Behdad Esfahbodc6173a32017-11-14 21:09:03 -0800231
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500232
233/* CheckSum */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100234struct CheckSum : HBUINT32
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400235{
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700236 CheckSum& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; }
237
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400238 /* This is reference implementation from the spec. */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330239 static uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400240 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500241 uint32_t Sum = 0L;
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800242 assert (0 == (Length & 3));
243 const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500244
245 while (Table < EndPtr)
246 Sum += *Table++;
247 return Sum;
248 }
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400249
250 /* Note: data should be 4byte aligned and have 4byte padding at the end. */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330251 void set_for_data (const void *data, unsigned int length)
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700252 { *this = CalcTableChecksum ((const HBUINT32 *) data, length); }
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400253
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400254 public:
255 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500256};
257
258
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500259/*
260 * Version Numbers
261 */
262
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100263template <typename FixedType=HBUINT16>
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400264struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400265{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330266 uint32_t to_int () const { return (major << (sizeof (FixedType) * 8)) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400267
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330268 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300269 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500270 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100271 return_trace (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400272 }
273
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +0900274 FixedType major;
275 FixedType minor;
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400276 public:
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330277 DEFINE_SIZE_STATIC (2 * sizeof (FixedType));
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500278};
279
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400280
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400281/*
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400282 * Template subclasses of Offset that do the dereferencing.
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400283 * Use: (base+offset)
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400284 */
285
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500286template <typename Type, bool has_null>
287struct _hb_has_null
288{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330289 static const Type *get_null () { return nullptr; }
290 static Type *get_crap () { return nullptr; }
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500291};
292template <typename Type>
293struct _hb_has_null<Type, true>
294{
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330295 static const Type *get_null () { return &Null (Type); }
296 static Type *get_crap () { return &Crap (Type); }
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500297};
298
Behdad Esfahbod9b4b5842021-03-31 13:27:21 -0600299template <typename Type, typename OffsetType, bool has_null=true>
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200300struct OffsetTo : Offset<OffsetType, has_null>
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400301{
Behdad Esfahbod07776b62019-04-15 16:43:34 -0400302 HB_DELETE_COPY_ASSIGN (OffsetTo);
303 OffsetTo () = default;
304
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700305 OffsetTo& operator = (typename OffsetType::type i) { OffsetType::operator= (i); return *this; }
306
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330307 const Type& operator () (const void *base) const
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400308 {
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500309 if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_null ();
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200310 return StructAtOffset<const Type> (base, *this);
Behdad Esfahboddcd1b072018-05-31 17:58:40 -0700311 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330312 Type& operator () (void *base) const
Behdad Esfahboddcd1b072018-05-31 17:58:40 -0700313 {
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500314 if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_crap ();
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200315 return StructAtOffset<Type> (base, *this);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400316 }
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400317
Behdad Esfahbod203ea582019-05-15 16:14:40 -0700318 template <typename Base,
319 hb_enable_if (hb_is_convertible (const Base, const void *))>
320 friend const Type& operator + (const Base &base, const OffsetTo &offset) { return offset ((const void *) base); }
321 template <typename Base,
Behdad Esfahboddfa5e422019-05-15 21:18:14 -0700322 hb_enable_if (hb_is_convertible (const Base, const void *))>
323 friend const Type& operator + (const OffsetTo &offset, const Base &base) { return offset ((const void *) base); }
324 template <typename Base,
Behdad Esfahbod203ea582019-05-15 16:14:40 -0700325 hb_enable_if (hb_is_convertible (Base, void *))>
326 friend Type& operator + (Base &&base, OffsetTo &offset) { return offset ((void *) base); }
Behdad Esfahboddfa5e422019-05-15 21:18:14 -0700327 template <typename Base,
328 hb_enable_if (hb_is_convertible (Base, void *))>
329 friend Type& operator + (OffsetTo &offset, Base &&base) { return offset ((void *) base); }
Behdad Esfahbod763ea422019-05-15 01:15:11 -0700330
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400331
Behdad Esfahbod88a41472019-05-02 14:22:31 -0700332 template <typename ...Ts>
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330333 bool serialize_subset (hb_subset_context_t *c, const OffsetTo& src,
334 const void *src_base, Ts&&... ds)
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700335 {
Behdad Esfahbodbfa02be2019-04-01 21:36:13 -0700336 *this = 0;
Behdad Esfahbod1834cf82019-05-31 14:39:32 -0700337 if (src.is_null ())
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700338 return false;
Behdad Esfahbodbfa02be2019-04-01 21:36:13 -0700339
Behdad Esfahbodaa2293a2019-04-02 17:42:10 -0700340 auto *s = c->serializer;
341
342 s->push ();
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700343
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600344 bool ret = c->dispatch (src_base+src, std::forward<Ts> (ds)...);
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700345
346 if (ret || !has_null)
ariza188a0a42020-03-07 11:02:36 -0800347 s->add_link (*this, s->pop_pack ());
Behdad Esfahbod7f73c972019-04-02 17:12:24 -0700348 else
Behdad Esfahbodaa2293a2019-04-02 17:42:10 -0700349 s->pop_discard ();
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700350
351 return ret;
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700352 }
353
Garret Rieger35458b62021-06-11 13:14:51 -0700354
355 template <typename ...Ts>
356 bool serialize_serialize (hb_serialize_context_t *c, Ts&&... ds)
357 {
358 *this = 0;
359
360 Type* obj = c->push<Type> ();
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600361 bool ret = obj->serialize (c, std::forward<Ts> (ds)...);
Garret Rieger35458b62021-06-11 13:14:51 -0700362
363 if (ret)
364 c->add_link (*this, c->pop_pack ());
365 else
366 c->pop_discard ();
367
368 return ret;
369 }
370
Behdad Esfahbod95426ea2019-05-07 15:56:51 -0700371 /* TODO: Somehow merge this with previous function into a serialize_dispatch(). */
ariza4ca8e0d2020-02-19 12:52:18 -0800372 /* Workaround clang bug: https://bugs.llvm.org/show_bug.cgi?id=23029
373 * Can't compile: whence = hb_serialize_context_t::Head followed by Ts&&...
374 */
375 template <typename ...Ts>
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330376 bool serialize_copy (hb_serialize_context_t *c, const OffsetTo& src,
377 const void *src_base, unsigned dst_bias,
ariza4ca8e0d2020-02-19 12:52:18 -0800378 hb_serialize_context_t::whence_t whence,
379 Ts&&... ds)
380 {
381 *this = 0;
382 if (src.is_null ())
383 return false;
384
385 c->push ();
386
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600387 bool ret = c->copy (src_base+src, std::forward<Ts> (ds)...);
ariza4ca8e0d2020-02-19 12:52:18 -0800388
ariza188a0a42020-03-07 11:02:36 -0800389 c->add_link (*this, c->pop_pack (), whence, dst_bias);
ariza4ca8e0d2020-02-19 12:52:18 -0800390
391 return ret;
392 }
393
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330394 bool serialize_copy (hb_serialize_context_t *c, const OffsetTo& src,
395 const void *src_base, unsigned dst_bias = 0)
ariza188a0a42020-03-07 11:02:36 -0800396 { return serialize_copy (c, src, src_base, dst_bias, hb_serialize_context_t::Head); }
Behdad Esfahbod273ed612019-05-02 14:04:51 -0700397
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330398 bool sanitize_shallow (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300399 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500400 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100401 if (unlikely (!c->check_struct (this))) return_trace (false);
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200402 if (unlikely (this->is_null ())) return_trace (true);
Behdad Esfahbod70110f62021-03-31 17:04:02 -0600403 if (unlikely ((const char *) base + (unsigned) *this < (const char *) base)) return_trace (false);
Behdad Esfahbodb482e522018-09-13 16:29:49 +0200404 return_trace (true);
405 }
406
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400407 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700408 bool sanitize (hb_sanitize_context_t *c, const void *base, Ts&&... ds) const
Behdad Esfahbodb482e522018-09-13 16:29:49 +0200409 {
410 TRACE_SANITIZE (this);
Behdad Esfahboda73bea62018-09-13 16:31:31 +0200411 return_trace (sanitize_shallow (c, base) &&
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200412 (this->is_null () ||
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600413 c->dispatch (StructAtOffset<Type> (base, *this), std::forward<Ts> (ds)...) ||
Behdad Esfahboda73bea62018-09-13 16:31:31 +0200414 neuter (c)));
Behdad Esfahbodb482e522018-09-13 16:29:49 +0200415 }
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400416
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400417 /* Set the offset to Null */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330418 bool neuter (hb_sanitize_context_t *c) const
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200419 {
420 if (!has_null) return false;
Behdad Esfahbod51f56352014-06-04 18:42:32 -0400421 return c->try_set (this, 0);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400422 }
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330423 DEFINE_SIZE_STATIC (sizeof (OffsetType));
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400424};
Behdad Esfahbod205d72a2019-01-17 18:10:38 -0500425/* Partial specializations. */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600426template <typename Type, bool has_null=true> using Offset16To = OffsetTo<Type, HBUINT16, has_null>;
427template <typename Type, bool has_null=true> using Offset24To = OffsetTo<Type, HBUINT24, has_null>;
428template <typename Type, bool has_null=true> using Offset32To = OffsetTo<Type, HBUINT32, has_null>;
429
430template <typename Type, typename OffsetType> using NNOffsetTo = OffsetTo<Type, OffsetType, false>;
431template <typename Type> using NNOffset16To = Offset16To<Type, false>;
432template <typename Type> using NNOffset24To = Offset24To<Type, false>;
433template <typename Type> using NNOffset32To = Offset32To<Type, false>;
Behdad Esfahbodf47a60a2018-11-22 17:53:29 -0500434
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400435
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400436/*
437 * Array Types
438 */
439
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100440template <typename Type>
441struct UnsizedArrayOf
442{
Behdad Esfahbod879faa22018-12-21 01:57:40 -0500443 typedef Type item_t;
Behdad Esfahbod70a52d62019-01-22 12:15:23 +0100444 static constexpr unsigned item_size = hb_static_size (Type);
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500445
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400446 HB_DELETE_CREATE_COPY_ASSIGN (UnsizedArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700447
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330448 const Type& operator [] (int i_) const
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800449 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500450 unsigned int i = (unsigned int) i_;
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800451 const Type *p = &arrayZ[i];
Behdad Esfahbod0328a1c2018-11-16 16:48:28 -0800452 if (unlikely (p < arrayZ)) return Null (Type); /* Overflowed. */
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800453 return *p;
454 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330455 Type& operator [] (int i_)
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800456 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500457 unsigned int i = (unsigned int) i_;
Behdad Esfahbod9714e112018-11-16 16:52:42 -0800458 Type *p = &arrayZ[i];
Behdad Esfahbod0328a1c2018-11-16 16:48:28 -0800459 if (unlikely (p < arrayZ)) return Crap (Type); /* Overflowed. */
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800460 return *p;
461 }
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100462
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330463 unsigned int get_size (unsigned int len) const
Behdad Esfahbod1cf075e2018-11-02 11:38:00 -0400464 { return len * Type::static_size; }
465
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330466 template <typename T> operator T * () { return arrayZ; }
467 template <typename T> operator const T * () const { return arrayZ; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330468 hb_array_t<Type> as_array (unsigned int len)
Behdad Esfahbode6043062018-11-24 01:24:48 -0500469 { return hb_array (arrayZ, len); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330470 hb_array_t<const Type> as_array (unsigned int len) const
Behdad Esfahbode6043062018-11-24 01:24:48 -0500471 { return hb_array (arrayZ, len); }
Behdad Esfahbod72462eb2018-11-02 11:46:24 -0400472
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500473 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330474 Type &lsearch (unsigned int len, const T &x, Type &not_found = Crap (Type))
Behdad Esfahbod52ae9862018-11-24 10:46:56 -0500475 { return *as_array (len).lsearch (x, &not_found); }
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500476 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330477 const Type &lsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
Behdad Esfahbod52ae9862018-11-24 10:46:56 -0500478 { return *as_array (len).lsearch (x, &not_found); }
Ebrahim Byagowi08d57d92020-06-28 13:13:25 +0430479 template <typename T>
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700480 bool lfind (unsigned int len, const T &x, unsigned int *i = nullptr,
481 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
482 unsigned int to_store = (unsigned int) -1) const
483 { return as_array (len).lfind (x, i, not_found, to_store); }
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500484
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330485 void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1)
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500486 { as_array (len).qsort (start, end); }
487
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700488 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
489 {
490 TRACE_SERIALIZE (this);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600491 if (unlikely (!c->extend (this, items_len))) return_trace (false);
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700492 return_trace (true);
493 }
494 template <typename Iterator,
Behdad Esfahboded972d52019-05-09 16:58:28 -0700495 hb_requires (hb_is_source_of (Iterator, Type))>
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700496 bool serialize (hb_serialize_context_t *c, Iterator items)
497 {
498 TRACE_SERIALIZE (this);
499 unsigned count = items.len ();
500 if (unlikely (!serialize (c, count))) return_trace (false);
501 /* TODO Umm. Just exhaust the iterator instead? Being extra
502 * cautious right now.. */
Behdad Esfahbod7166bd52019-05-08 14:24:57 -0700503 for (unsigned i = 0; i < count; i++, ++items)
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700504 arrayZ[i] = *items;
505 return_trace (true);
506 }
507
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700508 UnsizedArrayOf* copy (hb_serialize_context_t *c, unsigned count) const
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700509 {
510 TRACE_SERIALIZE (this);
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700511 auto *out = c->start_embed (this);
Behdad Esfahbode8b45c12019-05-08 16:37:38 -0700512 if (unlikely (!as_array (count).copy (c))) return_trace (nullptr);
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700513 return_trace (out);
514 }
515
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400516 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700517 bool sanitize (hb_sanitize_context_t *c, unsigned int count, Ts&&... ds) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100518 {
519 TRACE_SANITIZE (this);
520 if (unlikely (!sanitize_shallow (c, count))) return_trace (false);
Behdad Esfahbodbe428002021-11-02 00:04:18 -0600521 if (!sizeof... (Ts) && std::is_trivially_copyable<Type>::value) return_trace (true);
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100522 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600523 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330524 return_trace (false);
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100525 return_trace (true);
526 }
527
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330528 bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100529 {
530 TRACE_SANITIZE (this);
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200531 return_trace (c->check_array (arrayZ, count));
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100532 }
533
534 public:
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400535 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100536 public:
Behdad Esfahbodf47a60a2018-11-22 17:53:29 -0500537 DEFINE_SIZE_UNBOUNDED (0);
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100538};
539
540/* Unsized array of offset's */
Behdad Esfahbod87205ef2018-10-16 15:40:44 -0700541template <typename Type, typename OffsetType, bool has_null=true>
Behdad Esfahbod1fc6b692021-03-31 15:30:35 -0600542using UnsizedArray16OfOffsetTo = UnsizedArrayOf<OffsetTo<Type, OffsetType, has_null>>;
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100543
544/* Unsized array of offsets relative to the beginning of the array itself. */
Behdad Esfahbod87205ef2018-10-16 15:40:44 -0700545template <typename Type, typename OffsetType, bool has_null=true>
Behdad Esfahbod5efe3602021-03-31 15:33:22 -0600546struct UnsizedListOfOffset16To : UnsizedArray16OfOffsetTo<Type, OffsetType, has_null>
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100547{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330548 const Type& operator [] (int i_) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100549 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500550 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -0500551 const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
552 if (unlikely (p < this->arrayZ)) return Null (Type); /* Overflowed. */
553 return this+*p;
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100554 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330555 Type& operator [] (int i_)
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -0500556 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500557 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -0500558 const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
559 if (unlikely (p < this->arrayZ)) return Crap (Type); /* Overflowed. */
560 return this+*p;
561 }
562
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400563 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700564 bool sanitize (hb_sanitize_context_t *c, unsigned int count, Ts&&... ds) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100565 {
566 TRACE_SANITIZE (this);
Behdad Esfahbod1fc6b692021-03-31 15:30:35 -0600567 return_trace ((UnsizedArray16OfOffsetTo<Type, OffsetType, has_null>
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600568 ::sanitize (c, count, this, std::forward<Ts> (ds)...)));
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100569 }
570};
571
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500572/* An array with sorted elements. Supports binary searching. */
573template <typename Type>
574struct SortedUnsizedArrayOf : UnsizedArrayOf<Type>
575{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330576 hb_sorted_array_t<Type> as_array (unsigned int len)
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500577 { return hb_sorted_array (this->arrayZ, len); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330578 hb_sorted_array_t<const Type> as_array (unsigned int len) const
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500579 { return hb_sorted_array (this->arrayZ, len); }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330580 operator hb_sorted_array_t<Type> () { return as_array (); }
581 operator hb_sorted_array_t<const Type> () const { return as_array (); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500582
583 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330584 Type &bsearch (unsigned int len, const T &x, Type &not_found = Crap (Type))
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500585 { return *as_array (len).bsearch (x, &not_found); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500586 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330587 const Type &bsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500588 { return *as_array (len).bsearch (x, &not_found); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500589 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330590 bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr,
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700591 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
Ebrahim Byagowi7e3edfa2020-07-18 19:03:36 +0430592 unsigned int to_store = (unsigned int) -1) const
Behdad Esfahbodd77a0982018-11-24 10:06:13 -0500593 { return as_array (len).bfind (x, i, not_found, to_store); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500594};
595
596
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400597/* An array with a number of elements. */
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600598template <typename Type, typename LenType>
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400599struct ArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400600{
Behdad Esfahbod879faa22018-12-21 01:57:40 -0500601 typedef Type item_t;
Behdad Esfahbod70a52d62019-01-22 12:15:23 +0100602 static constexpr unsigned item_size = hb_static_size (Type);
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500603
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400604 HB_DELETE_CREATE_COPY_ASSIGN (ArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700605
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330606 const Type& operator [] (int i_) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400607 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500608 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330609 if (unlikely (i >= len)) return Null (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700610 return arrayZ[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400611 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330612 Type& operator [] (int i_)
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400613 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500614 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330615 if (unlikely (i >= len)) return Crap (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700616 return arrayZ[i];
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400617 }
Behdad Esfahbod28b68cf2018-10-30 23:33:30 -0700618
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330619 unsigned int get_size () const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400620 { return len.static_size + len * Type::static_size; }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400621
Behdad Esfahbod3f36c892019-03-29 15:22:46 -0700622 explicit operator bool () const { return len; }
Behdad Esfahbod362d4e72019-01-08 13:41:30 -0800623
Behdad Esfahbod3ca809e2019-08-28 13:49:35 -0700624 void pop () { len--; }
625
Behdad Esfahbod362d4e72019-01-08 13:41:30 -0800626 hb_array_t< Type> as_array () { return hb_array (arrayZ, len); }
627 hb_array_t<const Type> as_array () const { return hb_array (arrayZ, len); }
628
629 /* Iterator. */
Behdad Esfahboda4ea0d32019-01-09 00:32:11 -0800630 typedef hb_array_t<const Type> iter_t;
631 typedef hb_array_t< Type> writer_t;
632 iter_t iter () const { return as_array (); }
633 writer_t writer () { return as_array (); }
634 operator iter_t () const { return iter (); }
635 operator writer_t () { return writer (); }
Behdad Esfahbodc514f652018-11-23 16:04:56 -0500636
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330637 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430638 { return as_array ().sub_array (start_offset, count); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330639 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430640 { return as_array ().sub_array (start_offset, count); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330641 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430642 { return as_array ().sub_array (start_offset, count); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330643 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430644 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod3246a8e2018-11-24 21:32:00 -0500645
Behdad Esfahbod7a2eda72021-03-29 17:32:29 -0600646 template <typename T>
647 Type &lsearch (const T &x, Type &not_found = Crap (Type))
648 { return *as_array ().lsearch (x, &not_found); }
649 template <typename T>
650 const Type &lsearch (const T &x, const Type &not_found = Null (Type)) const
651 { return *as_array ().lsearch (x, &not_found); }
652 template <typename T>
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700653 bool lfind (const T &x, unsigned int *i = nullptr,
654 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
655 unsigned int to_store = (unsigned int) -1) const
656 { return as_array ().lfind (x, i, not_found, to_store); }
Behdad Esfahbod7a2eda72021-03-29 17:32:29 -0600657
658 void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
659 { as_array ().qsort (start, end); }
660
Behdad Esfahbod23976892021-03-29 17:34:23 -0600661 HB_NODISCARD bool serialize (hb_serialize_context_t *c, unsigned items_len)
Behdad Esfahbod1f07e332012-09-03 23:28:34 -0400662 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500663 TRACE_SERIALIZE (this);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600664 if (unlikely (!c->extend_min (this))) return_trace (false);
Garret Riegerb14475d2021-03-18 10:51:26 -0700665 c->check_assign (len, items_len, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600666 if (unlikely (!c->extend (this))) return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100667 return_trace (true);
Behdad Esfahbod1f07e332012-09-03 23:28:34 -0400668 }
Behdad Esfahbod79870952019-01-09 01:02:38 -0800669 template <typename Iterator,
Behdad Esfahboded972d52019-05-09 16:58:28 -0700670 hb_requires (hb_is_source_of (Iterator, Type))>
Behdad Esfahbod23976892021-03-29 17:34:23 -0600671 HB_NODISCARD bool serialize (hb_serialize_context_t *c, Iterator items)
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400672 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500673 TRACE_SERIALIZE (this);
Behdad Esfahbod49161d42018-12-26 22:50:33 -0500674 unsigned count = items.len ();
675 if (unlikely (!serialize (c, count))) return_trace (false);
Behdad Esfahboddf138da2018-12-28 16:29:48 -0500676 /* TODO Umm. Just exhaust the iterator instead? Being extra
677 * cautious right now.. */
Behdad Esfahbod7166bd52019-05-08 14:24:57 -0700678 for (unsigned i = 0; i < count; i++, ++items)
Behdad Esfahbod4c38a9f2019-03-29 20:23:07 -0700679 arrayZ[i] = *items;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100680 return_trace (true);
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400681 }
682
Behdad Esfahbod062cad52019-08-28 13:33:08 -0700683 Type* serialize_append (hb_serialize_context_t *c)
684 {
685 TRACE_SERIALIZE (this);
686 len++;
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600687 if (unlikely (!len || !c->extend (this)))
Behdad Esfahbod062cad52019-08-28 13:33:08 -0700688 {
689 len--;
690 return_trace (nullptr);
691 }
692 return_trace (&arrayZ[len - 1]);
693 }
694
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700695 ArrayOf* copy (hb_serialize_context_t *c) const
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700696 {
697 TRACE_SERIALIZE (this);
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700698 auto *out = c->start_embed (this);
Behdad Esfahbode8b45c12019-05-08 16:37:38 -0700699 if (unlikely (!c->extend_min (out))) return_trace (nullptr);
Garret Riegerb14475d2021-03-18 10:51:26 -0700700 c->check_assign (out->len, len, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
Behdad Esfahbode8b45c12019-05-08 16:37:38 -0700701 if (unlikely (!as_array ().copy (c))) return_trace (nullptr);
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700702 return_trace (out);
703 }
704
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400705 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700706 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300707 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500708 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100709 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Behdad Esfahbodbe428002021-11-02 00:04:18 -0600710 if (!sizeof... (Ts) && std::is_trivially_copyable<Type>::value) return_trace (true);
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400711 unsigned int count = len;
712 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600713 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330714 return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100715 return_trace (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400716 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400717
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330718 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300719 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500720 TRACE_SANITIZE (this);
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200721 return_trace (len.sanitize (c) && c->check_array (arrayZ, len));
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400722 }
723
724 public:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200725 LenType len;
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400726 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400727 public:
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700728 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400729};
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600730template <typename Type> using Array16Of = ArrayOf<Type, HBUINT16>;
731template <typename Type> using Array32Of = ArrayOf<Type, HBUINT32>;
Behdad Esfahbod489faf82019-03-29 20:01:37 -0700732using PString = ArrayOf<HBUINT8, HBUINT8>;
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400733
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400734/* Array of Offset's */
Behdad Esfahbod6c4e0492021-03-31 15:31:32 -0600735template <typename Type> using Array16OfOffset16To = ArrayOf<OffsetTo<Type, HBUINT16>, HBUINT16>;
Behdad Esfahbod2520a822021-03-31 15:34:26 -0600736template <typename Type> using Array16OfOffset32To = ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT16>;
Behdad Esfahbod2a54c9f2021-03-31 15:26:42 -0600737template <typename Type> using Array32OfOffset32To = ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT32>;
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400738
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400739/* Array of offsets relative to the beginning of the array itself. */
740template <typename Type>
Behdad Esfahbod5efe3602021-03-31 15:33:22 -0600741struct List16OfOffset16To : Array16OfOffset16To<Type>
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400742{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330743 const Type& operator [] (int i_) const
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400744 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500745 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330746 if (unlikely (i >= this->len)) return Null (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700747 return this+this->arrayZ[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400748 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330749 const Type& operator [] (int i_)
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700750 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500751 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330752 if (unlikely (i >= this->len)) return Crap (Type);
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700753 return this+this->arrayZ[i];
754 }
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400755
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330756 bool subset (hb_subset_context_t *c) const
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700757 {
758 TRACE_SUBSET (this);
Behdad Esfahbod5efe3602021-03-31 15:33:22 -0600759 struct List16OfOffset16To<Type> *out = c->serializer->embed (*this);
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700760 if (unlikely (!out)) return_trace (false);
761 unsigned int count = this->len;
762 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod1834cf82019-05-31 14:39:32 -0700763 out->arrayZ[i].serialize_subset (c, this->arrayZ[i], this, out);
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700764 return_trace (true);
765 }
766
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400767 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700768 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300769 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500770 TRACE_SANITIZE (this);
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600771 return_trace (Array16OfOffset16To<Type>::sanitize (c, this, std::forward<Ts> (ds)...));
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400772 }
773};
774
Behdad Esfahbod51d9ba02014-06-27 15:27:15 -0400775/* An array starting at second element. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100776template <typename Type, typename LenType=HBUINT16>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400777struct HeadlessArrayOf
778{
Behdad Esfahbod70a52d62019-01-22 12:15:23 +0100779 static constexpr unsigned item_size = Type::static_size;
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500780
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400781 HB_DELETE_CREATE_COPY_ASSIGN (HeadlessArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700782
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330783 const Type& operator [] (int i_) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400784 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500785 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330786 if (unlikely (i >= lenP1 || !i)) return Null (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700787 return arrayZ[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400788 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330789 Type& operator [] (int i_)
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700790 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500791 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330792 if (unlikely (i >= lenP1 || !i)) return Crap (Type);
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700793 return arrayZ[i-1];
794 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330795 unsigned int get_size () const
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700796 { return lenP1.static_size + get_length () * Type::static_size; }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400797
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700798 unsigned get_length () const { return lenP1 ? lenP1 - 1 : 0; }
Behdad Esfahbod42d887b2019-08-28 14:47:14 -0700799
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700800 hb_array_t< Type> as_array () { return hb_array (arrayZ, get_length ()); }
801 hb_array_t<const Type> as_array () const { return hb_array (arrayZ, get_length ()); }
Behdad Esfahbod42d887b2019-08-28 14:47:14 -0700802
803 /* Iterator. */
804 typedef hb_array_t<const Type> iter_t;
805 typedef hb_array_t< Type> writer_t;
806 iter_t iter () const { return as_array (); }
807 writer_t writer () { return as_array (); }
808 operator iter_t () const { return iter (); }
809 operator writer_t () { return writer (); }
810
811 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
Behdad Esfahboda930c682012-09-04 18:17:57 -0400812 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500813 TRACE_SERIALIZE (this);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600814 if (unlikely (!c->extend_min (this))) return_trace (false);
Garret Riegerb14475d2021-03-18 10:51:26 -0700815 c->check_assign (lenP1, items_len + 1, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600816 if (unlikely (!c->extend (this))) return_trace (false);
Behdad Esfahbod42d887b2019-08-28 14:47:14 -0700817 return_trace (true);
818 }
819 template <typename Iterator,
820 hb_requires (hb_is_source_of (Iterator, Type))>
821 bool serialize (hb_serialize_context_t *c, Iterator items)
822 {
823 TRACE_SERIALIZE (this);
824 unsigned count = items.len ();
825 if (unlikely (!serialize (c, count))) return_trace (false);
826 /* TODO Umm. Just exhaust the iterator instead? Being extra
827 * cautious right now.. */
828 for (unsigned i = 0; i < count; i++, ++items)
829 arrayZ[i] = *items;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100830 return_trace (true);
Behdad Esfahboda930c682012-09-04 18:17:57 -0400831 }
832
Behdad Esfahbod4dcf6532019-05-10 22:23:24 -0700833 template <typename ...Ts>
834 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300835 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500836 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100837 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Behdad Esfahbodbe428002021-11-02 00:04:18 -0600838 if (!sizeof... (Ts) && std::is_trivially_copyable<Type>::value) return_trace (true);
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700839 unsigned int count = get_length ();
Behdad Esfahbod4dcf6532019-05-10 22:23:24 -0700840 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600841 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Behdad Esfahbod4dcf6532019-05-10 22:23:24 -0700842 return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100843 return_trace (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400844 }
845
Behdad Esfahbod5f047112017-10-31 18:10:40 -0600846 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330847 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbod5f047112017-10-31 18:10:40 -0600848 {
849 TRACE_SANITIZE (this);
Behdad Esfahbodeffc7ce2018-09-13 20:21:54 +0200850 return_trace (lenP1.sanitize (c) &&
851 (!lenP1 || c->check_array (arrayZ, lenP1 - 1)));
Behdad Esfahbod5f047112017-10-31 18:10:40 -0600852 }
853
854 public:
Behdad Esfahbodeffc7ce2018-09-13 20:21:54 +0200855 LenType lenP1;
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400856 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahboded074222010-05-10 18:08:46 -0400857 public:
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700858 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400859};
860
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200861/* An array storing length-1. */
862template <typename Type, typename LenType=HBUINT16>
863struct ArrayOfM1
864{
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400865 HB_DELETE_CREATE_COPY_ASSIGN (ArrayOfM1);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700866
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330867 const Type& operator [] (int i_) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200868 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500869 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330870 if (unlikely (i > lenM1)) return Null (Type);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200871 return arrayZ[i];
872 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330873 Type& operator [] (int i_)
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200874 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500875 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330876 if (unlikely (i > lenM1)) return Crap (Type);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200877 return arrayZ[i];
878 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330879 unsigned int get_size () const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200880 { return lenM1.static_size + (lenM1 + 1) * Type::static_size; }
881
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400882 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700883 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200884 {
885 TRACE_SANITIZE (this);
886 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Behdad Esfahbodbe428002021-11-02 00:04:18 -0600887 if (!sizeof... (Ts) && std::is_trivially_copyable<Type>::value) return_trace (true);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200888 unsigned int count = lenM1 + 1;
889 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600890 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330891 return_trace (false);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200892 return_trace (true);
893 }
894
895 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330896 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200897 {
898 TRACE_SANITIZE (this);
899 return_trace (lenM1.sanitize (c) &&
900 (c->check_array (arrayZ, lenM1 + 1)));
901 }
902
903 public:
904 LenType lenM1;
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400905 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200906 public:
907 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
908};
909
Behdad Esfahbod92b1e022018-07-25 16:58:47 -0700910/* An array with sorted elements. Supports binary searching. */
Behdad Esfahbod4dba7492021-03-31 16:09:39 -0600911template <typename Type, typename LenType>
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400912struct SortedArrayOf : ArrayOf<Type, LenType>
Behdad Esfahbod40a47972014-05-08 18:21:04 -0400913{
Behdad Esfahbod362d4e72019-01-08 13:41:30 -0800914 hb_sorted_array_t< Type> as_array () { return hb_sorted_array (this->arrayZ, this->len); }
915 hb_sorted_array_t<const Type> as_array () const { return hb_sorted_array (this->arrayZ, this->len); }
916
917 /* Iterator. */
Behdad Esfahboda4ea0d32019-01-09 00:32:11 -0800918 typedef hb_sorted_array_t<const Type> iter_t;
919 typedef hb_sorted_array_t< Type> writer_t;
920 iter_t iter () const { return as_array (); }
921 writer_t writer () { return as_array (); }
922 operator iter_t () const { return iter (); }
923 operator writer_t () { return writer (); }
Behdad Esfahbode7003922018-11-24 01:31:00 -0500924
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800925 hb_sorted_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430926 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800927 hb_sorted_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430928 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800929 hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430930 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800931 hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430932 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod3246a8e2018-11-24 21:32:00 -0500933
Behdad Esfahbod82378092019-01-07 22:00:45 -0500934 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
935 {
936 TRACE_SERIALIZE (this);
937 bool ret = ArrayOf<Type, LenType>::serialize (c, items_len);
938 return_trace (ret);
939 }
Behdad Esfahbod79870952019-01-09 01:02:38 -0800940 template <typename Iterator,
Behdad Esfahboded972d52019-05-09 16:58:28 -0700941 hb_requires (hb_is_sorted_source_of (Iterator, Type))>
Behdad Esfahbod79870952019-01-09 01:02:38 -0800942 bool serialize (hb_serialize_context_t *c, Iterator items)
Behdad Esfahbod82378092019-01-07 22:00:45 -0500943 {
944 TRACE_SERIALIZE (this);
945 bool ret = ArrayOf<Type, LenType>::serialize (c, items);
946 return_trace (ret);
947 }
948
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500949 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330950 Type &bsearch (const T &x, Type &not_found = Crap (Type))
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500951 { return *as_array ().bsearch (x, &not_found); }
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500952 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330953 const Type &bsearch (const T &x, const Type &not_found = Null (Type)) const
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500954 { return *as_array ().bsearch (x, &not_found); }
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500955 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330956 bool bfind (const T &x, unsigned int *i = nullptr,
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700957 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330958 unsigned int to_store = (unsigned int) -1) const
Behdad Esfahbodd77a0982018-11-24 10:06:13 -0500959 { return as_array ().bfind (x, i, not_found, to_store); }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400960};
961
Behdad Esfahbod4dba7492021-03-31 16:09:39 -0600962template <typename Type> using SortedArray16Of = SortedArrayOf<Type, HBUINT16>;
963template <typename Type> using SortedArray32Of = SortedArrayOf<Type, HBUINT32>;
964
Behdad Esfahbod456a68c2018-10-07 22:28:45 -0400965/*
966 * Binary-search arrays
967 */
968
Behdad Esfahbod4c3b19d2018-10-07 22:30:42 -0400969template <typename LenType=HBUINT16>
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600970struct BinSearchHeader
971{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330972 operator uint32_t () const { return len; }
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600973
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330974 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600975 {
976 TRACE_SANITIZE (this);
977 return_trace (c->check_struct (this));
978 }
979
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700980 BinSearchHeader& operator = (unsigned int v)
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600981 {
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700982 len = v;
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600983 assert (len == v);
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700984 entrySelector = hb_max (1u, hb_bit_storage (v)) - 1;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700985 searchRange = 16 * (1u << entrySelector);
986 rangeShift = v * 16 > searchRange
987 ? 16 * v - searchRange
988 : 0;
989 return *this;
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600990 }
991
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600992 protected:
Behdad Esfahbod4c3b19d2018-10-07 22:30:42 -0400993 LenType len;
994 LenType searchRange;
995 LenType entrySelector;
996 LenType rangeShift;
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600997
998 public:
999 DEFINE_SIZE_STATIC (8);
1000};
1001
Behdad Esfahbod4c3b19d2018-10-07 22:30:42 -04001002template <typename Type, typename LenType=HBUINT16>
Ebrahim Byagowi92588782019-04-30 13:05:10 -07001003using BinSearchArrayOf = SortedArrayOf<Type, BinSearchHeader<LenType>>;
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -06001004
Behdad Esfahboda256a922018-10-29 11:25:35 -07001005
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001006struct VarSizedBinSearchHeader
1007{
1008
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301009 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001010 {
1011 TRACE_SANITIZE (this);
1012 return_trace (c->check_struct (this));
1013 }
1014
1015 HBUINT16 unitSize; /* Size of a lookup unit for this search in bytes. */
1016 HBUINT16 nUnits; /* Number of units of the preceding size to be searched. */
1017 HBUINT16 searchRange; /* The value of unitSize times the largest power of 2
1018 * that is less than or equal to the value of nUnits. */
1019 HBUINT16 entrySelector; /* The log base 2 of the largest power of 2 less than
1020 * or equal to the value of nUnits. */
1021 HBUINT16 rangeShift; /* The value of unitSize times the difference of the
1022 * value of nUnits minus the largest power of 2 less
1023 * than or equal to the value of nUnits. */
1024 public:
1025 DEFINE_SIZE_STATIC (10);
1026};
1027
1028template <typename Type>
1029struct VarSizedBinSearchArrayOf
1030{
Behdad Esfahbod70a52d62019-01-22 12:15:23 +01001031 static constexpr unsigned item_size = Type::static_size;
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -05001032
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -04001033 HB_DELETE_CREATE_COPY_ASSIGN (VarSizedBinSearchArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -07001034
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301035 bool last_is_terminator () const
Behdad Esfahbod3d309722018-11-24 23:12:28 -05001036 {
1037 if (unlikely (!header.nUnits)) return false;
1038
1039 /* Gah.
1040 *
1041 * "The number of termination values that need to be included is table-specific.
1042 * The value that indicates binary search termination is 0xFFFF." */
1043 const HBUINT16 *words = &StructAtOffset<HBUINT16> (&bytesZ, (header.nUnits - 1) * header.unitSize);
1044 unsigned int count = Type::TerminationWordCount;
1045 for (unsigned int i = 0; i < count; i++)
1046 if (words[i] != 0xFFFFu)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301047 return false;
Behdad Esfahbod3d309722018-11-24 23:12:28 -05001048 return true;
1049 }
1050
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301051 const Type& operator [] (int i_) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001052 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -05001053 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -05001054 if (unlikely (i >= get_length ())) return Null (Type);
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001055 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
1056 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301057 Type& operator [] (int i_)
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001058 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -05001059 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -05001060 if (unlikely (i >= get_length ())) return Crap (Type);
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001061 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
1062 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301063 unsigned int get_length () const
1064 { return header.nUnits - last_is_terminator (); }
1065 unsigned int get_size () const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001066 { return header.static_size + header.nUnits * header.unitSize; }
1067
Behdad Esfahbod20f31342019-04-23 12:58:52 -04001068 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001069 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001070 {
1071 TRACE_SANITIZE (this);
1072 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Behdad Esfahbodbe428002021-11-02 00:04:18 -06001073 if (!sizeof... (Ts) && std::is_trivially_copyable<Type>::value) return_trace (true);
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -05001074 unsigned int count = get_length ();
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001075 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06001076 if (unlikely (!(*this)[i].sanitize (c, std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301077 return_trace (false);
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +03301078 return_trace (true);
1079 }
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001080
1081 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301082 const Type *bsearch (const T &key) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001083 {
Behdad Esfahbodb1dc6762019-12-10 11:41:24 -06001084 unsigned pos;
1085 return hb_bsearch_impl (&pos,
1086 key,
1087 (const void *) bytesZ,
1088 get_length (),
1089 header.unitSize,
1090 _hb_cmp_method<T, Type>)
1091 ? (const Type *) (((const char *) &bytesZ) + (pos * header.unitSize))
1092 : nullptr;
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001093 }
1094
1095 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301096 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001097 {
1098 TRACE_SANITIZE (this);
1099 return_trace (header.sanitize (c) &&
Behdad Esfahbod2c824d32018-10-11 16:41:01 -04001100 Type::static_size <= header.unitSize &&
Behdad Esfahbode0144052018-11-12 14:23:31 -05001101 c->check_range (bytesZ.arrayZ,
1102 header.nUnits,
1103 header.unitSize));
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001104 }
1105
1106 protected:
1107 VarSizedBinSearchHeader header;
1108 UnsizedArrayOf<HBUINT8> bytesZ;
1109 public:
1110 DEFINE_SIZE_ARRAY (10, bytesZ);
1111};
1112
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -04001113
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08001114} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001115
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001116
Behdad Esfahbodc77ae402018-08-25 22:36:36 -07001117#endif /* HB_OPEN_TYPE_HH */