blob: b87046ab2dc18aec2f4938fad54626c51c02de00 [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"
Thomas Devoogdtc657c4e2022-05-10 10:00:06 +020036#include "hb-meta.hh"
Behdad Esfahbodd1f29902018-08-31 16:31:00 -070037#include "hb-subset.hh"
Behdad Esfahbod12c45682006-12-28 06:10:59 -050038
Behdad Esfahboda16ecbf2008-01-23 17:01:55 -050039
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040040namespace OT {
41
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040042
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -050043/*
44 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040045 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -050046 */
47
48
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -050049/* "The following data types are used in the OpenType font file.
50 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -050051
Behdad Esfahbod5f810362009-05-17 00:54:25 -040052/*
53 * Int types
54 */
55
Behdad Esfahbod2467c662010-04-21 23:11:45 -040056/* Integer types in big-endian order and no alignment requirement */
Behdad Esfahbodcc16b262021-02-22 17:55:47 -070057template <typename Type,
Behdad Esfahbod567cedc2021-02-22 22:09:15 -070058 unsigned int Size = sizeof (Type)>
Behdad Esfahbode032ed92010-04-21 03:11:46 -040059struct IntType
60{
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -070061 typedef Type type;
Behdad Esfahbod11d2f492018-12-01 13:12:21 -050062
Behdad Esfahbodcc16b262021-02-22 17:55:47 -070063 IntType () = default;
Behdad Esfahbod567cedc2021-02-22 22:09:15 -070064 explicit constexpr IntType (Type V) : v {V} {}
65 IntType& operator = (Type i) { v = i; return *this; }
Behdad Esfahbod486da352021-02-23 13:58:14 -070066 /* For reason we define cast out operator for signed/unsigned, instead of Type, see:
67 * https://github.com/harfbuzz/harfbuzz/pull/2875/commits/09836013995cab2b9f07577a179ad7b024130467 */
Behdad Esfahbod7a078c32021-11-02 00:44:51 -060068 operator typename std::conditional<std::is_signed<Type>::value, signed, unsigned>::type () const { return v; }
Behdad Esfahbod09836012021-02-22 22:33:17 -070069
Behdad Esfahbod8938dd22019-06-17 14:12:11 -070070 bool operator == (const IntType &o) const { return (Type) v == (Type) o.v; }
71 bool operator != (const IntType &o) const { return !(*this == o); }
Behdad Esfahbod307bd6d2019-08-28 13:49:17 -070072
73 IntType& operator += (unsigned count) { *this = *this + count; return *this; }
74 IntType& operator -= (unsigned count) { *this = *this - count; return *this; }
75 IntType& operator ++ () { *this += 1; return *this; }
76 IntType& operator -- () { *this -= 1; return *this; }
77 IntType operator ++ (int) { IntType c (*this); ++*this; return c; }
78 IntType operator -- (int) { IntType c (*this); --*this; return c; }
79
Behdad Esfahbod8938dd22019-06-17 14:12:11 -070080 HB_INTERNAL static int cmp (const IntType *a, const IntType *b)
Behdad Esfahbod95df00a2019-04-12 17:50:03 -040081 { return b->cmp (*a); }
Qunxin Liu82afc752020-02-04 13:24:37 -080082 HB_INTERNAL static int cmp (const void *a, const void *b)
83 {
84 IntType *pa = (IntType *) a;
85 IntType *pb = (IntType *) b;
86
87 return pb->cmp (*pa);
88 }
Behdad Esfahbod98374ce2021-02-05 13:40:10 -050089 template <typename Type2,
Behdad Esfahbod943921c2021-11-02 00:26:46 -060090 hb_enable_if (std::is_integral<Type2>::value &&
Behdad Esfahbod98374ce2021-02-05 13:40:10 -050091 sizeof (Type2) < sizeof (int) &&
92 sizeof (Type) < sizeof (int))>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033093 int cmp (Type2 a) const
Behdad Esfahbod88a399a2015-02-19 16:57:12 +030094 {
95 Type b = v;
Behdad Esfahbod98374ce2021-02-05 13:40:10 -050096 return (int) a - (int) b;
97 }
98 template <typename Type2,
99 hb_enable_if (hb_is_convertible (Type2, Type))>
100 int cmp (Type2 a) const
101 {
102 Type b = v;
103 return a < b ? -1 : a == b ? 0 : +1;
Behdad Esfahbod88a399a2015-02-19 16:57:12 +0300104 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330105 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300106 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500107 TRACE_SANITIZE (this);
Behdad Esfahbod7c4e9082022-07-11 14:01:52 -0600108 return_trace (c->check_struct (this));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400109 }
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400110 protected:
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500111 BEInt<Type, Size> v;
Behdad Esfahbod569da922010-05-10 16:38:32 -0400112 public:
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500113 DEFINE_SIZE_STATIC (Size);
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400114};
115
Behdad Esfahbode5b7bc42020-06-29 01:24:02 -0700116typedef IntType<uint8_t> HBUINT8; /* 8-bit unsigned integer. */
117typedef IntType<int8_t> HBINT8; /* 8-bit signed integer. */
118typedef IntType<uint16_t> HBUINT16; /* 16-bit unsigned integer. */
119typedef IntType<int16_t> HBINT16; /* 16-bit signed integer. */
120typedef IntType<uint32_t> HBUINT32; /* 32-bit unsigned integer. */
121typedef IntType<int32_t> HBINT32; /* 32-bit signed integer. */
Behdad Esfahbod11d2f492018-12-01 13:12:21 -0500122/* Note: we cannot defined a signed HBINT24 because there's no corresponding C type.
123 * Works for unsigned, but not signed, since we rely on compiler for sign-extension. */
Behdad Esfahbodc3a8b042018-12-01 00:14:48 -0500124typedef IntType<uint32_t, 3> HBUINT24; /* 24-bit unsigned integer. */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400125
Behdad Esfahbod14a2df72021-09-19 23:06:09 -0400126/* 15-bit unsigned number; top bit used for extension. */
127struct HBUINT15 : HBUINT16
128{
129 /* TODO Flesh out; actually mask top bit. */
130 HBUINT15& operator = (uint16_t i ) { HBUINT16::operator= (i); return *this; }
131 public:
132 DEFINE_SIZE_STATIC (2);
133};
134
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100135/* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */
136typedef HBINT16 FWORD;
Behdad Esfahbodae9877d2011-08-17 14:43:45 +0200137
Behdad Esfahbod22955b22018-10-10 19:58:20 -0400138/* 32-bit signed integer (HBINT32) that describes a quantity in FUnits. */
139typedef HBINT32 FWORD32;
140
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100141/* 16-bit unsigned integer (HBUINT16) that describes a quantity in FUnits. */
142typedef HBUINT16 UFWORD;
Behdad Esfahbodae9877d2011-08-17 14:43:45 +0200143
Behdad Esfahbod68b62962016-03-01 16:41:53 +0900144/* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100145struct F2DOT14 : HBINT16
Behdad Esfahbod68b62962016-03-01 16:41:53 +0900146{
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700147 F2DOT14& operator = (uint16_t i ) { HBINT16::operator= (i); return *this; }
Ebrahim Byagowice99dd02018-04-15 22:08:50 +0430148 // 16384 means 1<<14
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330149 float to_float () const { return ((int32_t) v) / 16384.f; }
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700150 void set_float (float f) { v = roundf (f * 16384.f); }
Behdad Esfahbod68b62962016-03-01 16:41:53 +0900151 public:
152 DEFINE_SIZE_STATIC (2);
153};
154
Behdad Esfahbod587d4622016-04-30 19:20:56 +0200155/* 32-bit signed fixed-point number (16.16). */
Behdad Esfahbod229ef1d2019-09-10 10:31:07 -0700156struct HBFixed : HBINT32
Behdad Esfahbod587d4622016-04-30 19:20:56 +0200157{
Behdad Esfahbod229ef1d2019-09-10 10:31:07 -0700158 HBFixed& operator = (uint32_t i) { HBINT32::operator= (i); return *this; }
Ebrahim Byagowice99dd02018-04-15 22:08:50 +0430159 // 65536 means 1<<16
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330160 float to_float () const { return ((int32_t) v) / 65536.f; }
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700161 void set_float (float f) { v = roundf (f * 65536.f); }
Behdad Esfahbod587d4622016-04-30 19:20:56 +0200162 public:
163 DEFINE_SIZE_STATIC (4);
164};
165
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400166/* Date represented in number of seconds since 12:00 midnight, January 1,
167 * 1904. The value is represented as a signed 64-bit integer. */
168struct LONGDATETIME
169{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330170 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300171 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500172 TRACE_SANITIZE (this);
Behdad Esfahbod7c4e9082022-07-11 14:01:52 -0600173 return_trace (c->check_struct (this));
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400174 }
Behdad Esfahbod6775da32014-01-23 14:18:49 -0500175 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100176 HBINT32 major;
177 HBUINT32 minor;
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400178 public:
179 DEFINE_SIZE_STATIC (8);
180};
181
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500182/* Array of four uint8s (length = 32 bits) used to identify a script, language
183 * system, feature, or baseline */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100184struct Tag : HBUINT32
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400185{
Behdad Esfahbod6492b232019-06-17 14:19:13 -0700186 Tag& operator = (hb_tag_t i) { HBUINT32::operator= (i); return *this; }
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500187 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahbod8b2f9ad2021-02-22 17:42:24 -0700188 operator const char* () const { return reinterpret_cast<const char *> (this); }
189 operator char* () { return reinterpret_cast<char *> (this); }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400190 public:
191 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500192};
193
194/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbodc852b862021-09-19 16:30:12 -0400195struct HBGlyphID16 : HBUINT16
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700196{
Behdad Esfahbodc852b862021-09-19 16:30:12 -0400197 HBGlyphID16& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700198};
Behdad Esfahbod2c672612022-07-06 12:26:16 -0600199struct HBGlyphID24 : HBUINT24
200{
201 HBGlyphID24& operator = (uint32_t i) { HBUINT24::operator= (i); return *this; }
202};
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500203
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400204/* Script/language-system/feature index */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100205struct Index : HBUINT16 {
Behdad Esfahbod5d4b0372019-01-22 12:11:24 +0100206 static constexpr unsigned NOT_FOUND_INDEX = 0xFFFFu;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700207 Index& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400208};
Behdad Esfahbod92806ee2018-08-05 21:41:52 -0700209DECLARE_NULL_NAMESPACE_BYTES (OT, Index);
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400210
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700211typedef Index NameID;
212
Behdad Esfahbod9ffc46b2021-03-31 11:26:18 -0600213struct VarIdx : HBUINT32 {
214 static constexpr unsigned NO_VARIATION = 0xFFFFFFFFu;
215 VarIdx& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; }
216};
217DECLARE_NULL_NAMESPACE_BYTES (OT, VarIdx);
218
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400219/* Offset, Null offset = 0 */
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200220template <typename Type, bool has_null=true>
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400221struct Offset : Type
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600222{
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700223 Offset& operator = (typename Type::type i) { Type::operator= (i); return *this; }
224
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700225 typedef Type type;
226
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330227 bool is_null () const { return has_null && 0 == *this; }
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600228
Behdad Esfahboddf1c7d52018-02-25 19:06:25 -0800229 public:
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330230 DEFINE_SIZE_STATIC (sizeof (Type));
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600231};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400232
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100233typedef Offset<HBUINT16> Offset16;
Behdad Esfahbod21792812021-03-31 11:20:21 -0600234typedef Offset<HBUINT24> Offset24;
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100235typedef Offset<HBUINT32> Offset32;
Behdad Esfahbodc6173a32017-11-14 21:09:03 -0800236
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500237
238/* CheckSum */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100239struct CheckSum : HBUINT32
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400240{
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700241 CheckSum& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; }
242
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400243 /* This is reference implementation from the spec. */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330244 static uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400245 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500246 uint32_t Sum = 0L;
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800247 assert (0 == (Length & 3));
248 const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500249
250 while (Table < EndPtr)
251 Sum += *Table++;
252 return Sum;
253 }
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400254
255 /* Note: data should be 4byte aligned and have 4byte padding at the end. */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330256 void set_for_data (const void *data, unsigned int length)
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700257 { *this = CalcTableChecksum ((const HBUINT32 *) data, length); }
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400258
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400259 public:
260 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500261};
262
263
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500264/*
265 * Version Numbers
266 */
267
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100268template <typename FixedType=HBUINT16>
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400269struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400270{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330271 uint32_t to_int () const { return (major << (sizeof (FixedType) * 8)) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400272
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330273 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300274 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500275 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100276 return_trace (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400277 }
278
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +0900279 FixedType major;
280 FixedType minor;
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400281 public:
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330282 DEFINE_SIZE_STATIC (2 * sizeof (FixedType));
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500283};
284
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400285
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400286/*
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400287 * Template subclasses of Offset that do the dereferencing.
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400288 * Use: (base+offset)
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400289 */
290
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500291template <typename Type, bool has_null>
292struct _hb_has_null
293{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330294 static const Type *get_null () { return nullptr; }
295 static Type *get_crap () { return nullptr; }
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500296};
297template <typename Type>
298struct _hb_has_null<Type, true>
299{
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330300 static const Type *get_null () { return &Null (Type); }
301 static Type *get_crap () { return &Crap (Type); }
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500302};
303
Behdad Esfahbod9b4b5842021-03-31 13:27:21 -0600304template <typename Type, typename OffsetType, bool has_null=true>
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200305struct OffsetTo : Offset<OffsetType, has_null>
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400306{
Behdad Esfahbod07776b62019-04-15 16:43:34 -0400307 HB_DELETE_COPY_ASSIGN (OffsetTo);
308 OffsetTo () = default;
309
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700310 OffsetTo& operator = (typename OffsetType::type i) { OffsetType::operator= (i); return *this; }
311
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330312 const Type& operator () (const void *base) const
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400313 {
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500314 if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_null ();
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200315 return StructAtOffset<const Type> (base, *this);
Behdad Esfahboddcd1b072018-05-31 17:58:40 -0700316 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330317 Type& operator () (void *base) const
Behdad Esfahboddcd1b072018-05-31 17:58:40 -0700318 {
Behdad Esfahbod4d4fd642018-11-22 18:07:36 -0500319 if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_crap ();
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200320 return StructAtOffset<Type> (base, *this);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400321 }
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400322
Behdad Esfahbod203ea582019-05-15 16:14:40 -0700323 template <typename Base,
324 hb_enable_if (hb_is_convertible (const Base, const void *))>
325 friend const Type& operator + (const Base &base, const OffsetTo &offset) { return offset ((const void *) base); }
326 template <typename Base,
Behdad Esfahboddfa5e422019-05-15 21:18:14 -0700327 hb_enable_if (hb_is_convertible (const Base, const void *))>
328 friend const Type& operator + (const OffsetTo &offset, const Base &base) { return offset ((const void *) base); }
329 template <typename Base,
Behdad Esfahbod203ea582019-05-15 16:14:40 -0700330 hb_enable_if (hb_is_convertible (Base, void *))>
331 friend Type& operator + (Base &&base, OffsetTo &offset) { return offset ((void *) base); }
Behdad Esfahboddfa5e422019-05-15 21:18:14 -0700332 template <typename Base,
333 hb_enable_if (hb_is_convertible (Base, void *))>
334 friend Type& operator + (OffsetTo &offset, Base &&base) { return offset ((void *) base); }
Behdad Esfahbod763ea422019-05-15 01:15:11 -0700335
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400336
Behdad Esfahbod88a41472019-05-02 14:22:31 -0700337 template <typename ...Ts>
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330338 bool serialize_subset (hb_subset_context_t *c, const OffsetTo& src,
339 const void *src_base, Ts&&... ds)
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700340 {
Behdad Esfahbodbfa02be2019-04-01 21:36:13 -0700341 *this = 0;
Behdad Esfahbod1834cf82019-05-31 14:39:32 -0700342 if (src.is_null ())
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700343 return false;
Behdad Esfahbodbfa02be2019-04-01 21:36:13 -0700344
Behdad Esfahbodaa2293a2019-04-02 17:42:10 -0700345 auto *s = c->serializer;
346
347 s->push ();
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700348
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600349 bool ret = c->dispatch (src_base+src, std::forward<Ts> (ds)...);
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700350
351 if (ret || !has_null)
ariza188a0a42020-03-07 11:02:36 -0800352 s->add_link (*this, s->pop_pack ());
Behdad Esfahbod7f73c972019-04-02 17:12:24 -0700353 else
Behdad Esfahbodaa2293a2019-04-02 17:42:10 -0700354 s->pop_discard ();
Behdad Esfahbode42b82c2019-04-02 17:21:54 -0700355
356 return ret;
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700357 }
358
Garret Rieger35458b62021-06-11 13:14:51 -0700359
360 template <typename ...Ts>
361 bool serialize_serialize (hb_serialize_context_t *c, Ts&&... ds)
362 {
363 *this = 0;
364
365 Type* obj = c->push<Type> ();
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600366 bool ret = obj->serialize (c, std::forward<Ts> (ds)...);
Garret Rieger35458b62021-06-11 13:14:51 -0700367
368 if (ret)
369 c->add_link (*this, c->pop_pack ());
370 else
371 c->pop_discard ();
372
373 return ret;
374 }
375
Behdad Esfahbod95426ea2019-05-07 15:56:51 -0700376 /* TODO: Somehow merge this with previous function into a serialize_dispatch(). */
ariza4ca8e0d2020-02-19 12:52:18 -0800377 /* Workaround clang bug: https://bugs.llvm.org/show_bug.cgi?id=23029
378 * Can't compile: whence = hb_serialize_context_t::Head followed by Ts&&...
379 */
380 template <typename ...Ts>
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330381 bool serialize_copy (hb_serialize_context_t *c, const OffsetTo& src,
382 const void *src_base, unsigned dst_bias,
ariza4ca8e0d2020-02-19 12:52:18 -0800383 hb_serialize_context_t::whence_t whence,
384 Ts&&... ds)
385 {
386 *this = 0;
387 if (src.is_null ())
388 return false;
389
390 c->push ();
391
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600392 bool ret = c->copy (src_base+src, std::forward<Ts> (ds)...);
ariza4ca8e0d2020-02-19 12:52:18 -0800393
ariza188a0a42020-03-07 11:02:36 -0800394 c->add_link (*this, c->pop_pack (), whence, dst_bias);
ariza4ca8e0d2020-02-19 12:52:18 -0800395
396 return ret;
397 }
398
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330399 bool serialize_copy (hb_serialize_context_t *c, const OffsetTo& src,
400 const void *src_base, unsigned dst_bias = 0)
ariza188a0a42020-03-07 11:02:36 -0800401 { return serialize_copy (c, src, src_base, dst_bias, hb_serialize_context_t::Head); }
Behdad Esfahbod273ed612019-05-02 14:04:51 -0700402
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330403 bool sanitize_shallow (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300404 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500405 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100406 if (unlikely (!c->check_struct (this))) return_trace (false);
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200407 if (unlikely (this->is_null ())) return_trace (true);
Behdad Esfahbod70110f62021-03-31 17:04:02 -0600408 if (unlikely ((const char *) base + (unsigned) *this < (const char *) base)) return_trace (false);
Behdad Esfahbodb482e522018-09-13 16:29:49 +0200409 return_trace (true);
410 }
411
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400412 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700413 bool sanitize (hb_sanitize_context_t *c, const void *base, Ts&&... ds) const
Behdad Esfahbodb482e522018-09-13 16:29:49 +0200414 {
415 TRACE_SANITIZE (this);
Behdad Esfahboda73bea62018-09-13 16:31:31 +0200416 return_trace (sanitize_shallow (c, base) &&
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200417 (this->is_null () ||
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600418 c->dispatch (StructAtOffset<Type> (base, *this), std::forward<Ts> (ds)...) ||
Behdad Esfahboda73bea62018-09-13 16:31:31 +0200419 neuter (c)));
Behdad Esfahbodb482e522018-09-13 16:29:49 +0200420 }
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400421
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400422 /* Set the offset to Null */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330423 bool neuter (hb_sanitize_context_t *c) const
Behdad Esfahbod29faebe2018-09-13 18:45:35 +0200424 {
425 if (!has_null) return false;
Behdad Esfahbod51f56352014-06-04 18:42:32 -0400426 return c->try_set (this, 0);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400427 }
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330428 DEFINE_SIZE_STATIC (sizeof (OffsetType));
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400429};
Behdad Esfahbod205d72a2019-01-17 18:10:38 -0500430/* Partial specializations. */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600431template <typename Type, bool has_null=true> using Offset16To = OffsetTo<Type, HBUINT16, has_null>;
432template <typename Type, bool has_null=true> using Offset24To = OffsetTo<Type, HBUINT24, has_null>;
433template <typename Type, bool has_null=true> using Offset32To = OffsetTo<Type, HBUINT32, has_null>;
434
435template <typename Type, typename OffsetType> using NNOffsetTo = OffsetTo<Type, OffsetType, false>;
436template <typename Type> using NNOffset16To = Offset16To<Type, false>;
437template <typename Type> using NNOffset24To = Offset24To<Type, false>;
438template <typename Type> using NNOffset32To = Offset32To<Type, false>;
Behdad Esfahbodf47a60a2018-11-22 17:53:29 -0500439
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400440
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400441/*
442 * Array Types
443 */
444
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100445template <typename Type>
446struct UnsizedArrayOf
447{
Behdad Esfahbod879faa22018-12-21 01:57:40 -0500448 typedef Type item_t;
Behdad Esfahbod70a52d62019-01-22 12:15:23 +0100449 static constexpr unsigned item_size = hb_static_size (Type);
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500450
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400451 HB_DELETE_CREATE_COPY_ASSIGN (UnsizedArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700452
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330453 const Type& operator [] (int i_) const
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800454 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500455 unsigned int i = (unsigned int) i_;
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800456 const Type *p = &arrayZ[i];
Behdad Esfahbod0328a1c2018-11-16 16:48:28 -0800457 if (unlikely (p < arrayZ)) return Null (Type); /* Overflowed. */
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800458 return *p;
459 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330460 Type& operator [] (int i_)
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800461 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500462 unsigned int i = (unsigned int) i_;
Behdad Esfahbod9714e112018-11-16 16:52:42 -0800463 Type *p = &arrayZ[i];
Behdad Esfahbod0328a1c2018-11-16 16:48:28 -0800464 if (unlikely (p < arrayZ)) return Crap (Type); /* Overflowed. */
Behdad Esfahbod52f61cd2018-11-16 16:41:59 -0800465 return *p;
466 }
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100467
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330468 unsigned int get_size (unsigned int len) const
Behdad Esfahbod1cf075e2018-11-02 11:38:00 -0400469 { return len * Type::static_size; }
470
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330471 template <typename T> operator T * () { return arrayZ; }
472 template <typename T> operator const T * () const { return arrayZ; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330473 hb_array_t<Type> as_array (unsigned int len)
Behdad Esfahbode6043062018-11-24 01:24:48 -0500474 { return hb_array (arrayZ, len); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330475 hb_array_t<const Type> as_array (unsigned int len) const
Behdad Esfahbode6043062018-11-24 01:24:48 -0500476 { return hb_array (arrayZ, len); }
Behdad Esfahbod72462eb2018-11-02 11:46:24 -0400477
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500478 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330479 Type &lsearch (unsigned int len, const T &x, Type &not_found = Crap (Type))
Behdad Esfahbod52ae9862018-11-24 10:46:56 -0500480 { return *as_array (len).lsearch (x, &not_found); }
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500481 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330482 const Type &lsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
Behdad Esfahbod52ae9862018-11-24 10:46:56 -0500483 { return *as_array (len).lsearch (x, &not_found); }
Ebrahim Byagowi08d57d92020-06-28 13:13:25 +0430484 template <typename T>
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700485 bool lfind (unsigned int len, const T &x, unsigned int *i = nullptr,
486 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
487 unsigned int to_store = (unsigned int) -1) const
488 { return as_array (len).lfind (x, i, not_found, to_store); }
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500489
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330490 void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1)
Behdad Esfahbod70d80c92018-11-24 01:59:50 -0500491 { as_array (len).qsort (start, end); }
492
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700493 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
494 {
495 TRACE_SERIALIZE (this);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600496 if (unlikely (!c->extend (this, items_len))) return_trace (false);
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700497 return_trace (true);
498 }
499 template <typename Iterator,
Behdad Esfahboded972d52019-05-09 16:58:28 -0700500 hb_requires (hb_is_source_of (Iterator, Type))>
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700501 bool serialize (hb_serialize_context_t *c, Iterator items)
502 {
503 TRACE_SERIALIZE (this);
504 unsigned count = items.len ();
505 if (unlikely (!serialize (c, count))) return_trace (false);
506 /* TODO Umm. Just exhaust the iterator instead? Being extra
507 * cautious right now.. */
Behdad Esfahbod7166bd52019-05-08 14:24:57 -0700508 for (unsigned i = 0; i < count; i++, ++items)
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700509 arrayZ[i] = *items;
510 return_trace (true);
511 }
512
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700513 UnsizedArrayOf* copy (hb_serialize_context_t *c, unsigned count) const
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700514 {
515 TRACE_SERIALIZE (this);
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700516 auto *out = c->start_embed (this);
Behdad Esfahbode8b45c12019-05-08 16:37:38 -0700517 if (unlikely (!as_array (count).copy (c))) return_trace (nullptr);
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700518 return_trace (out);
519 }
520
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400521 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700522 bool sanitize (hb_sanitize_context_t *c, unsigned int count, Ts&&... ds) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100523 {
524 TRACE_SANITIZE (this);
525 if (unlikely (!sanitize_shallow (c, count))) return_trace (false);
Thomas Devoogdtc657c4e2022-05-10 10:00:06 +0200526 if (!sizeof... (Ts) && hb_is_trivially_copyable(Type)) return_trace (true);
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100527 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600528 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330529 return_trace (false);
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100530 return_trace (true);
531 }
532
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330533 bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100534 {
535 TRACE_SANITIZE (this);
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200536 return_trace (c->check_array (arrayZ, count));
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100537 }
538
539 public:
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400540 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100541 public:
Behdad Esfahbodf47a60a2018-11-22 17:53:29 -0500542 DEFINE_SIZE_UNBOUNDED (0);
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100543};
544
545/* Unsized array of offset's */
Behdad Esfahbod87205ef2018-10-16 15:40:44 -0700546template <typename Type, typename OffsetType, bool has_null=true>
Behdad Esfahbod1fc6b692021-03-31 15:30:35 -0600547using UnsizedArray16OfOffsetTo = UnsizedArrayOf<OffsetTo<Type, OffsetType, has_null>>;
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100548
549/* Unsized array of offsets relative to the beginning of the array itself. */
Behdad Esfahbod87205ef2018-10-16 15:40:44 -0700550template <typename Type, typename OffsetType, bool has_null=true>
Behdad Esfahbod5efe3602021-03-31 15:33:22 -0600551struct UnsizedListOfOffset16To : UnsizedArray16OfOffsetTo<Type, OffsetType, has_null>
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100552{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330553 const Type& operator [] (int i_) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100554 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500555 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -0500556 const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
557 if (unlikely (p < this->arrayZ)) return Null (Type); /* Overflowed. */
558 return this+*p;
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100559 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330560 Type& operator [] (int i_)
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -0500561 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500562 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -0500563 const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
564 if (unlikely (p < this->arrayZ)) return Crap (Type); /* Overflowed. */
565 return this+*p;
566 }
567
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400568 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700569 bool sanitize (hb_sanitize_context_t *c, unsigned int count, Ts&&... ds) const
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100570 {
571 TRACE_SANITIZE (this);
Behdad Esfahbod1fc6b692021-03-31 15:30:35 -0600572 return_trace ((UnsizedArray16OfOffsetTo<Type, OffsetType, has_null>
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600573 ::sanitize (c, count, this, std::forward<Ts> (ds)...)));
Behdad Esfahbod6418ae42018-03-14 16:18:42 +0100574 }
575};
576
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500577/* An array with sorted elements. Supports binary searching. */
578template <typename Type>
579struct SortedUnsizedArrayOf : UnsizedArrayOf<Type>
580{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330581 hb_sorted_array_t<Type> as_array (unsigned int len)
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500582 { return hb_sorted_array (this->arrayZ, len); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330583 hb_sorted_array_t<const Type> as_array (unsigned int len) const
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500584 { return hb_sorted_array (this->arrayZ, len); }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330585 operator hb_sorted_array_t<Type> () { return as_array (); }
586 operator hb_sorted_array_t<const Type> () const { return as_array (); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500587
588 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330589 Type &bsearch (unsigned int len, const T &x, Type &not_found = Crap (Type))
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500590 { return *as_array (len).bsearch (x, &not_found); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500591 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330592 const Type &bsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500593 { return *as_array (len).bsearch (x, &not_found); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500594 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330595 bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr,
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700596 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
Ebrahim Byagowi7e3edfa2020-07-18 19:03:36 +0430597 unsigned int to_store = (unsigned int) -1) const
Behdad Esfahbodd77a0982018-11-24 10:06:13 -0500598 { return as_array (len).bfind (x, i, not_found, to_store); }
Behdad Esfahbod7c1600d2018-11-24 01:37:11 -0500599};
600
601
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400602/* An array with a number of elements. */
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600603template <typename Type, typename LenType>
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400604struct ArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400605{
Behdad Esfahbod879faa22018-12-21 01:57:40 -0500606 typedef Type item_t;
Behdad Esfahbod70a52d62019-01-22 12:15:23 +0100607 static constexpr unsigned item_size = hb_static_size (Type);
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500608
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400609 HB_DELETE_CREATE_COPY_ASSIGN (ArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700610
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330611 const Type& operator [] (int i_) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400612 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500613 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330614 if (unlikely (i >= len)) return Null (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700615 return arrayZ[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400616 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330617 Type& operator [] (int i_)
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400618 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500619 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330620 if (unlikely (i >= len)) return Crap (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700621 return arrayZ[i];
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400622 }
Behdad Esfahbod28b68cf2018-10-30 23:33:30 -0700623
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330624 unsigned int get_size () const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400625 { return len.static_size + len * Type::static_size; }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400626
Behdad Esfahbod3f36c892019-03-29 15:22:46 -0700627 explicit operator bool () const { return len; }
Behdad Esfahbod362d4e72019-01-08 13:41:30 -0800628
Behdad Esfahbod3ca809e2019-08-28 13:49:35 -0700629 void pop () { len--; }
630
Behdad Esfahbod362d4e72019-01-08 13:41:30 -0800631 hb_array_t< Type> as_array () { return hb_array (arrayZ, len); }
632 hb_array_t<const Type> as_array () const { return hb_array (arrayZ, len); }
633
634 /* Iterator. */
Behdad Esfahboda4ea0d32019-01-09 00:32:11 -0800635 typedef hb_array_t<const Type> iter_t;
636 typedef hb_array_t< Type> writer_t;
637 iter_t iter () const { return as_array (); }
638 writer_t writer () { return as_array (); }
639 operator iter_t () const { return iter (); }
640 operator writer_t () { return writer (); }
Behdad Esfahbodc514f652018-11-23 16:04:56 -0500641
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330642 hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430643 { return as_array ().sub_array (start_offset, count); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330644 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 +0430645 { return as_array ().sub_array (start_offset, count); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330646 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430647 { return as_array ().sub_array (start_offset, count); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330648 hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430649 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod3246a8e2018-11-24 21:32:00 -0500650
Behdad Esfahbod7a2eda72021-03-29 17:32:29 -0600651 template <typename T>
652 Type &lsearch (const T &x, Type &not_found = Crap (Type))
653 { return *as_array ().lsearch (x, &not_found); }
654 template <typename T>
655 const Type &lsearch (const T &x, const Type &not_found = Null (Type)) const
656 { return *as_array ().lsearch (x, &not_found); }
657 template <typename T>
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700658 bool lfind (const T &x, unsigned int *i = nullptr,
659 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
660 unsigned int to_store = (unsigned int) -1) const
661 { return as_array ().lfind (x, i, not_found, to_store); }
Behdad Esfahbod7a2eda72021-03-29 17:32:29 -0600662
663 void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
664 { as_array ().qsort (start, end); }
665
Behdad Esfahbod23976892021-03-29 17:34:23 -0600666 HB_NODISCARD bool serialize (hb_serialize_context_t *c, unsigned items_len)
Behdad Esfahbod1f07e332012-09-03 23:28:34 -0400667 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500668 TRACE_SERIALIZE (this);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600669 if (unlikely (!c->extend_min (this))) return_trace (false);
Garret Riegerb14475d2021-03-18 10:51:26 -0700670 c->check_assign (len, items_len, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600671 if (unlikely (!c->extend (this))) return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100672 return_trace (true);
Behdad Esfahbod1f07e332012-09-03 23:28:34 -0400673 }
Behdad Esfahbod79870952019-01-09 01:02:38 -0800674 template <typename Iterator,
Behdad Esfahboded972d52019-05-09 16:58:28 -0700675 hb_requires (hb_is_source_of (Iterator, Type))>
Behdad Esfahbod23976892021-03-29 17:34:23 -0600676 HB_NODISCARD bool serialize (hb_serialize_context_t *c, Iterator items)
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400677 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500678 TRACE_SERIALIZE (this);
Behdad Esfahbod49161d42018-12-26 22:50:33 -0500679 unsigned count = items.len ();
680 if (unlikely (!serialize (c, count))) return_trace (false);
Behdad Esfahboddf138da2018-12-28 16:29:48 -0500681 /* TODO Umm. Just exhaust the iterator instead? Being extra
682 * cautious right now.. */
Behdad Esfahbod7166bd52019-05-08 14:24:57 -0700683 for (unsigned i = 0; i < count; i++, ++items)
Behdad Esfahbod4c38a9f2019-03-29 20:23:07 -0700684 arrayZ[i] = *items;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100685 return_trace (true);
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400686 }
687
Behdad Esfahbod062cad52019-08-28 13:33:08 -0700688 Type* serialize_append (hb_serialize_context_t *c)
689 {
690 TRACE_SERIALIZE (this);
691 len++;
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600692 if (unlikely (!len || !c->extend (this)))
Behdad Esfahbod062cad52019-08-28 13:33:08 -0700693 {
694 len--;
695 return_trace (nullptr);
696 }
697 return_trace (&arrayZ[len - 1]);
698 }
699
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700700 ArrayOf* copy (hb_serialize_context_t *c) const
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700701 {
702 TRACE_SERIALIZE (this);
Behdad Esfahbod8a32c9e2019-05-02 16:20:18 -0700703 auto *out = c->start_embed (this);
Behdad Esfahbode8b45c12019-05-08 16:37:38 -0700704 if (unlikely (!c->extend_min (out))) return_trace (nullptr);
Garret Riegerb14475d2021-03-18 10:51:26 -0700705 c->check_assign (out->len, len, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
Behdad Esfahbode8b45c12019-05-08 16:37:38 -0700706 if (unlikely (!as_array ().copy (c))) return_trace (nullptr);
Behdad Esfahbod998b0b62019-05-02 14:39:52 -0700707 return_trace (out);
708 }
709
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400710 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700711 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300712 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500713 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100714 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Thomas Devoogdtc657c4e2022-05-10 10:00:06 +0200715 if (!sizeof... (Ts) && hb_is_trivially_copyable(Type)) return_trace (true);
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400716 unsigned int count = len;
717 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600718 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330719 return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100720 return_trace (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400721 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400722
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330723 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300724 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500725 TRACE_SANITIZE (this);
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200726 return_trace (len.sanitize (c) && c->check_array (arrayZ, len));
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400727 }
728
729 public:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200730 LenType len;
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400731 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400732 public:
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700733 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400734};
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600735template <typename Type> using Array16Of = ArrayOf<Type, HBUINT16>;
Behdad Esfahbod486555c2022-07-05 17:12:59 -0600736template <typename Type> using Array24Of = ArrayOf<Type, HBUINT24>;
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600737template <typename Type> using Array32Of = ArrayOf<Type, HBUINT32>;
Behdad Esfahbod489faf82019-03-29 20:01:37 -0700738using PString = ArrayOf<HBUINT8, HBUINT8>;
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400739
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400740/* Array of Offset's */
Behdad Esfahbod6c4e0492021-03-31 15:31:32 -0600741template <typename Type> using Array16OfOffset16To = ArrayOf<OffsetTo<Type, HBUINT16>, HBUINT16>;
Behdad Esfahbod2520a822021-03-31 15:34:26 -0600742template <typename Type> using Array16OfOffset32To = ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT16>;
Behdad Esfahbod2a54c9f2021-03-31 15:26:42 -0600743template <typename Type> using Array32OfOffset32To = ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT32>;
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400744
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400745/* Array of offsets relative to the beginning of the array itself. */
Behdad Esfahbodf6c2aae2022-07-11 13:06:48 -0600746template <typename Type, typename OffsetType>
747struct List16OfOffsetTo : ArrayOf<OffsetTo<Type, OffsetType>, HBUINT16>
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400748{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330749 const Type& operator [] (int i_) const
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400750 {
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 Null (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700753 return this+this->arrayZ[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400754 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330755 const Type& operator [] (int i_)
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700756 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500757 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330758 if (unlikely (i >= this->len)) return Crap (Type);
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700759 return this+this->arrayZ[i];
760 }
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400761
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330762 bool subset (hb_subset_context_t *c) const
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700763 {
764 TRACE_SUBSET (this);
Behdad Esfahbodf6c2aae2022-07-11 13:06:48 -0600765 struct List16OfOffsetTo *out = c->serializer->embed (*this);
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700766 if (unlikely (!out)) return_trace (false);
767 unsigned int count = this->len;
768 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod1834cf82019-05-31 14:39:32 -0700769 out->arrayZ[i].serialize_subset (c, this->arrayZ[i], this, out);
Behdad Esfahbodbfa72a92018-09-01 18:34:50 -0700770 return_trace (true);
771 }
772
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400773 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700774 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300775 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500776 TRACE_SANITIZE (this);
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600777 return_trace (Array16OfOffset16To<Type>::sanitize (c, this, std::forward<Ts> (ds)...));
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400778 }
779};
780
Behdad Esfahbodf6c2aae2022-07-11 13:06:48 -0600781template <typename Type>
782using List16OfOffset16To = List16OfOffsetTo<Type, HBUINT16>;
783
Behdad Esfahbod51d9ba02014-06-27 15:27:15 -0400784/* An array starting at second element. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100785template <typename Type, typename LenType=HBUINT16>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400786struct HeadlessArrayOf
787{
Behdad Esfahbod70a52d62019-01-22 12:15:23 +0100788 static constexpr unsigned item_size = Type::static_size;
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500789
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400790 HB_DELETE_CREATE_COPY_ASSIGN (HeadlessArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700791
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330792 const Type& operator [] (int i_) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400793 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500794 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330795 if (unlikely (i >= lenP1 || !i)) return Null (Type);
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700796 return arrayZ[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400797 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330798 Type& operator [] (int i_)
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700799 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500800 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330801 if (unlikely (i >= lenP1 || !i)) return Crap (Type);
Behdad Esfahbod5d801292018-05-24 11:33:15 -0700802 return arrayZ[i-1];
803 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330804 unsigned int get_size () const
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700805 { return lenP1.static_size + get_length () * Type::static_size; }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400806
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700807 unsigned get_length () const { return lenP1 ? lenP1 - 1 : 0; }
Behdad Esfahbod42d887b2019-08-28 14:47:14 -0700808
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700809 hb_array_t< Type> as_array () { return hb_array (arrayZ, get_length ()); }
810 hb_array_t<const Type> as_array () const { return hb_array (arrayZ, get_length ()); }
Behdad Esfahbod42d887b2019-08-28 14:47:14 -0700811
812 /* Iterator. */
813 typedef hb_array_t<const Type> iter_t;
814 typedef hb_array_t< Type> writer_t;
815 iter_t iter () const { return as_array (); }
816 writer_t writer () { return as_array (); }
817 operator iter_t () const { return iter (); }
818 operator writer_t () { return writer (); }
819
820 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
Behdad Esfahboda930c682012-09-04 18:17:57 -0400821 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500822 TRACE_SERIALIZE (this);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600823 if (unlikely (!c->extend_min (this))) return_trace (false);
Garret Riegerb14475d2021-03-18 10:51:26 -0700824 c->check_assign (lenP1, items_len + 1, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600825 if (unlikely (!c->extend (this))) return_trace (false);
Behdad Esfahbod42d887b2019-08-28 14:47:14 -0700826 return_trace (true);
827 }
828 template <typename Iterator,
829 hb_requires (hb_is_source_of (Iterator, Type))>
830 bool serialize (hb_serialize_context_t *c, Iterator items)
831 {
832 TRACE_SERIALIZE (this);
833 unsigned count = items.len ();
834 if (unlikely (!serialize (c, count))) return_trace (false);
835 /* TODO Umm. Just exhaust the iterator instead? Being extra
836 * cautious right now.. */
837 for (unsigned i = 0; i < count; i++, ++items)
838 arrayZ[i] = *items;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100839 return_trace (true);
Behdad Esfahboda930c682012-09-04 18:17:57 -0400840 }
841
Behdad Esfahbod4dcf6532019-05-10 22:23:24 -0700842 template <typename ...Ts>
843 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300844 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500845 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100846 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Thomas Devoogdtc657c4e2022-05-10 10:00:06 +0200847 if (!sizeof... (Ts) && hb_is_trivially_copyable(Type)) return_trace (true);
Behdad Esfahboddc2c9aa2019-08-28 15:05:49 -0700848 unsigned int count = get_length ();
Behdad Esfahbod4dcf6532019-05-10 22:23:24 -0700849 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600850 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Behdad Esfahbod4dcf6532019-05-10 22:23:24 -0700851 return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100852 return_trace (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400853 }
854
Behdad Esfahbod5f047112017-10-31 18:10:40 -0600855 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330856 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbod5f047112017-10-31 18:10:40 -0600857 {
858 TRACE_SANITIZE (this);
Behdad Esfahbodeffc7ce2018-09-13 20:21:54 +0200859 return_trace (lenP1.sanitize (c) &&
860 (!lenP1 || c->check_array (arrayZ, lenP1 - 1)));
Behdad Esfahbod5f047112017-10-31 18:10:40 -0600861 }
862
863 public:
Behdad Esfahbodeffc7ce2018-09-13 20:21:54 +0200864 LenType lenP1;
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400865 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahboded074222010-05-10 18:08:46 -0400866 public:
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700867 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400868};
869
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200870/* An array storing length-1. */
871template <typename Type, typename LenType=HBUINT16>
872struct ArrayOfM1
873{
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -0400874 HB_DELETE_CREATE_COPY_ASSIGN (ArrayOfM1);
Behdad Esfahboda256a922018-10-29 11:25:35 -0700875
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330876 const Type& operator [] (int i_) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200877 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500878 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330879 if (unlikely (i > lenM1)) return Null (Type);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200880 return arrayZ[i];
881 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330882 Type& operator [] (int i_)
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200883 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -0500884 unsigned int i = (unsigned int) i_;
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330885 if (unlikely (i > lenM1)) return Crap (Type);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200886 return arrayZ[i];
887 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330888 unsigned int get_size () const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200889 { return lenM1.static_size + (lenM1 + 1) * Type::static_size; }
890
Behdad Esfahbod20f31342019-04-23 12:58:52 -0400891 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700892 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200893 {
894 TRACE_SANITIZE (this);
895 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Thomas Devoogdtc657c4e2022-05-10 10:00:06 +0200896 if (!sizeof... (Ts) && hb_is_trivially_copyable(Type)) return_trace (true);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200897 unsigned int count = lenM1 + 1;
898 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -0600899 if (unlikely (!c->dispatch (arrayZ[i], std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330900 return_trace (false);
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200901 return_trace (true);
902 }
903
904 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330905 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200906 {
907 TRACE_SANITIZE (this);
908 return_trace (lenM1.sanitize (c) &&
909 (c->check_array (arrayZ, lenM1 + 1)));
910 }
911
912 public:
913 LenType lenM1;
Behdad Esfahbod0e294c42019-09-06 16:54:27 -0400914 Type arrayZ[HB_VAR_ARRAY];
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200915 public:
916 DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
917};
918
Behdad Esfahbod92b1e022018-07-25 16:58:47 -0700919/* An array with sorted elements. Supports binary searching. */
Behdad Esfahbod4dba7492021-03-31 16:09:39 -0600920template <typename Type, typename LenType>
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400921struct SortedArrayOf : ArrayOf<Type, LenType>
Behdad Esfahbod40a47972014-05-08 18:21:04 -0400922{
Behdad Esfahbod362d4e72019-01-08 13:41:30 -0800923 hb_sorted_array_t< Type> as_array () { return hb_sorted_array (this->arrayZ, this->len); }
924 hb_sorted_array_t<const Type> as_array () const { return hb_sorted_array (this->arrayZ, this->len); }
925
926 /* Iterator. */
Behdad Esfahboda4ea0d32019-01-09 00:32:11 -0800927 typedef hb_sorted_array_t<const Type> iter_t;
928 typedef hb_sorted_array_t< Type> writer_t;
929 iter_t iter () const { return as_array (); }
930 writer_t writer () { return as_array (); }
931 operator iter_t () const { return iter (); }
932 operator writer_t () { return writer (); }
Behdad Esfahbode7003922018-11-24 01:31:00 -0500933
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800934 hb_sorted_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430935 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800936 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 +0430937 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800938 hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430939 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod2f837a32019-01-08 13:05:01 -0800940 hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
Ebrahim Byagowiaab8e082019-07-26 02:19:22 +0430941 { return as_array ().sub_array (start_offset, count); }
Behdad Esfahbod3246a8e2018-11-24 21:32:00 -0500942
Behdad Esfahbod82378092019-01-07 22:00:45 -0500943 bool serialize (hb_serialize_context_t *c, unsigned int items_len)
944 {
945 TRACE_SERIALIZE (this);
946 bool ret = ArrayOf<Type, LenType>::serialize (c, items_len);
947 return_trace (ret);
948 }
Behdad Esfahbod79870952019-01-09 01:02:38 -0800949 template <typename Iterator,
Behdad Esfahboded972d52019-05-09 16:58:28 -0700950 hb_requires (hb_is_sorted_source_of (Iterator, Type))>
Behdad Esfahbod79870952019-01-09 01:02:38 -0800951 bool serialize (hb_serialize_context_t *c, Iterator items)
Behdad Esfahbod82378092019-01-07 22:00:45 -0500952 {
953 TRACE_SERIALIZE (this);
954 bool ret = ArrayOf<Type, LenType>::serialize (c, items);
955 return_trace (ret);
956 }
957
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500958 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330959 Type &bsearch (const T &x, Type &not_found = Crap (Type))
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500960 { return *as_array ().bsearch (x, &not_found); }
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500961 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330962 const Type &bsearch (const T &x, const Type &not_found = Null (Type)) const
Behdad Esfahbod918b1ee2018-11-24 10:09:17 -0500963 { return *as_array ().bsearch (x, &not_found); }
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500964 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330965 bool bfind (const T &x, unsigned int *i = nullptr,
Behdad Esfahbod03cd9c52021-07-22 11:27:33 -0700966 hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330967 unsigned int to_store = (unsigned int) -1) const
Behdad Esfahbodd77a0982018-11-24 10:06:13 -0500968 { return as_array ().bfind (x, i, not_found, to_store); }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400969};
970
Behdad Esfahbod4dba7492021-03-31 16:09:39 -0600971template <typename Type> using SortedArray16Of = SortedArrayOf<Type, HBUINT16>;
Behdad Esfahbod486555c2022-07-05 17:12:59 -0600972template <typename Type> using SortedArray24Of = SortedArrayOf<Type, HBUINT24>;
Behdad Esfahbod4dba7492021-03-31 16:09:39 -0600973template <typename Type> using SortedArray32Of = SortedArrayOf<Type, HBUINT32>;
974
Behdad Esfahbod456a68c2018-10-07 22:28:45 -0400975/*
976 * Binary-search arrays
977 */
978
Behdad Esfahbod4c3b19d2018-10-07 22:30:42 -0400979template <typename LenType=HBUINT16>
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600980struct BinSearchHeader
981{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330982 operator uint32_t () const { return len; }
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600983
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330984 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600985 {
986 TRACE_SANITIZE (this);
987 return_trace (c->check_struct (this));
988 }
989
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700990 BinSearchHeader& operator = (unsigned int v)
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600991 {
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700992 len = v;
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600993 assert (len == v);
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700994 entrySelector = hb_max (1u, hb_bit_storage (v)) - 1;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700995 searchRange = 16 * (1u << entrySelector);
996 rangeShift = v * 16 > searchRange
997 ? 16 * v - searchRange
998 : 0;
999 return *this;
Behdad Esfahbodc479a592018-02-07 21:13:10 -06001000 }
1001
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -06001002 protected:
Behdad Esfahbod4c3b19d2018-10-07 22:30:42 -04001003 LenType len;
1004 LenType searchRange;
1005 LenType entrySelector;
1006 LenType rangeShift;
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -06001007
1008 public:
1009 DEFINE_SIZE_STATIC (8);
1010};
1011
Behdad Esfahbod4c3b19d2018-10-07 22:30:42 -04001012template <typename Type, typename LenType=HBUINT16>
Ebrahim Byagowi92588782019-04-30 13:05:10 -07001013using BinSearchArrayOf = SortedArrayOf<Type, BinSearchHeader<LenType>>;
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -06001014
Behdad Esfahboda256a922018-10-29 11:25:35 -07001015
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001016struct VarSizedBinSearchHeader
1017{
1018
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301019 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001020 {
1021 TRACE_SANITIZE (this);
1022 return_trace (c->check_struct (this));
1023 }
1024
1025 HBUINT16 unitSize; /* Size of a lookup unit for this search in bytes. */
1026 HBUINT16 nUnits; /* Number of units of the preceding size to be searched. */
1027 HBUINT16 searchRange; /* The value of unitSize times the largest power of 2
1028 * that is less than or equal to the value of nUnits. */
1029 HBUINT16 entrySelector; /* The log base 2 of the largest power of 2 less than
1030 * or equal to the value of nUnits. */
1031 HBUINT16 rangeShift; /* The value of unitSize times the difference of the
1032 * value of nUnits minus the largest power of 2 less
1033 * than or equal to the value of nUnits. */
1034 public:
1035 DEFINE_SIZE_STATIC (10);
1036};
1037
1038template <typename Type>
1039struct VarSizedBinSearchArrayOf
1040{
Behdad Esfahbod70a52d62019-01-22 12:15:23 +01001041 static constexpr unsigned item_size = Type::static_size;
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -05001042
Behdad Esfahbodb52c0e52019-04-11 11:20:10 -04001043 HB_DELETE_CREATE_COPY_ASSIGN (VarSizedBinSearchArrayOf);
Behdad Esfahboda256a922018-10-29 11:25:35 -07001044
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301045 bool last_is_terminator () const
Behdad Esfahbod3d309722018-11-24 23:12:28 -05001046 {
1047 if (unlikely (!header.nUnits)) return false;
1048
1049 /* Gah.
1050 *
1051 * "The number of termination values that need to be included is table-specific.
1052 * The value that indicates binary search termination is 0xFFFF." */
1053 const HBUINT16 *words = &StructAtOffset<HBUINT16> (&bytesZ, (header.nUnits - 1) * header.unitSize);
1054 unsigned int count = Type::TerminationWordCount;
1055 for (unsigned int i = 0; i < count; i++)
1056 if (words[i] != 0xFFFFu)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301057 return false;
Behdad Esfahbod3d309722018-11-24 23:12:28 -05001058 return true;
1059 }
1060
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301061 const Type& operator [] (int i_) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001062 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -05001063 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -05001064 if (unlikely (i >= get_length ())) return Null (Type);
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001065 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
1066 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301067 Type& operator [] (int i_)
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001068 {
Behdad Esfahboddfad19a2018-11-30 19:57:12 -05001069 unsigned int i = (unsigned int) i_;
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -05001070 if (unlikely (i >= get_length ())) return Crap (Type);
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001071 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
1072 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301073 unsigned int get_length () const
1074 { return header.nUnits - last_is_terminator (); }
1075 unsigned int get_size () const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001076 { return header.static_size + header.nUnits * header.unitSize; }
1077
Behdad Esfahbod20f31342019-04-23 12:58:52 -04001078 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001079 bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001080 {
1081 TRACE_SANITIZE (this);
1082 if (unlikely (!sanitize_shallow (c))) return_trace (false);
Thomas Devoogdtc657c4e2022-05-10 10:00:06 +02001083 if (!sizeof... (Ts) && hb_is_trivially_copyable(Type)) return_trace (true);
Behdad Esfahbod4202a3c2018-11-24 22:48:34 -05001084 unsigned int count = get_length ();
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001085 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06001086 if (unlikely (!(*this)[i].sanitize (c, std::forward<Ts> (ds)...)))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301087 return_trace (false);
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +03301088 return_trace (true);
1089 }
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001090
1091 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301092 const Type *bsearch (const T &key) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001093 {
Behdad Esfahbodb1dc6762019-12-10 11:41:24 -06001094 unsigned pos;
1095 return hb_bsearch_impl (&pos,
1096 key,
1097 (const void *) bytesZ,
1098 get_length (),
1099 header.unitSize,
1100 _hb_cmp_method<T, Type>)
1101 ? (const Type *) (((const char *) &bytesZ) + (pos * header.unitSize))
1102 : nullptr;
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001103 }
1104
1105 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301106 bool sanitize_shallow (hb_sanitize_context_t *c) const
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001107 {
1108 TRACE_SANITIZE (this);
1109 return_trace (header.sanitize (c) &&
Behdad Esfahbod2c824d32018-10-11 16:41:01 -04001110 Type::static_size <= header.unitSize &&
Behdad Esfahbode0144052018-11-12 14:23:31 -05001111 c->check_range (bytesZ.arrayZ,
1112 header.nUnits,
1113 header.unitSize));
Behdad Esfahbod456a68c2018-10-07 22:28:45 -04001114 }
1115
1116 protected:
1117 VarSizedBinSearchHeader header;
1118 UnsizedArrayOf<HBUINT8> bytesZ;
1119 public:
1120 DEFINE_SIZE_ARRAY (10, bytesZ);
1121};
1122
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -04001123
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08001124} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001125
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001126
Behdad Esfahbodc77ae402018-08-25 22:36:36 -07001127#endif /* HB_OPEN_TYPE_HH */