Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2017 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 27 | #ifndef HB_AAT_LAYOUT_COMMON_HH |
| 28 | #define HB_AAT_LAYOUT_COMMON_HH |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 29 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb-aat-layout.hh" |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | namespace AAT { |
| 34 | |
| 35 | using namespace OT; |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * Binary Searching Tables |
| 40 | */ |
| 41 | |
| 42 | struct BinSearchHeader |
| 43 | { |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 44 | |
| 45 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 46 | { |
| 47 | TRACE_SANITIZE (this); |
| 48 | return_trace (c->check_struct (this)); |
| 49 | } |
| 50 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 51 | HBUINT16 unitSize; /* Size of a lookup unit for this search in bytes. */ |
| 52 | HBUINT16 nUnits; /* Number of units of the preceding size to be searched. */ |
| 53 | HBUINT16 searchRange; /* The value of unitSize times the largest power of 2 |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 54 | * that is less than or equal to the value of nUnits. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 55 | HBUINT16 entrySelector; /* The log base 2 of the largest power of 2 less than |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 56 | * or equal to the value of nUnits. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 57 | HBUINT16 rangeShift; /* The value of unitSize times the difference of the |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 58 | * value of nUnits minus the largest power of 2 less |
| 59 | * than or equal to the value of nUnits. */ |
| 60 | public: |
| 61 | DEFINE_SIZE_STATIC (10); |
| 62 | }; |
| 63 | |
| 64 | template <typename Type> |
| 65 | struct BinSearchArrayOf |
| 66 | { |
| 67 | inline const Type& operator [] (unsigned int i) const |
| 68 | { |
| 69 | if (unlikely (i >= header.nUnits)) return Null(Type); |
Behdad Esfahbod | dff2c45 | 2018-09-10 23:29:26 +0200 | [diff] [blame] | 70 | return StructAtOffset<Type> (&bytesZ, i * header.unitSize); |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 71 | } |
| 72 | inline Type& operator [] (unsigned int i) |
| 73 | { |
Behdad Esfahbod | dff2c45 | 2018-09-10 23:29:26 +0200 | [diff] [blame] | 74 | return StructAtOffset<Type> (&bytesZ, i * header.unitSize); |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 75 | } |
| 76 | inline unsigned int get_size (void) const |
| 77 | { return header.static_size + header.nUnits * header.unitSize; } |
| 78 | |
| 79 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 80 | { |
| 81 | TRACE_SANITIZE (this); |
| 82 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
| 83 | |
| 84 | /* Note: for structs that do not reference other structs, |
| 85 | * we do not need to call their sanitize() as we already did |
| 86 | * a bound check on the aggregate array size. We just include |
| 87 | * a small unreachable expression to make sure the structs |
| 88 | * pointed to do have a simple sanitize(), ie. they do not |
| 89 | * reference other structs via offsets. |
| 90 | */ |
Behdad Esfahbod | dff2c45 | 2018-09-10 23:29:26 +0200 | [diff] [blame] | 91 | (void) (false && StructAtOffset<Type> (&bytesZ, 0).sanitize (c)); |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 92 | |
| 93 | return_trace (true); |
| 94 | } |
| 95 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 96 | { |
| 97 | TRACE_SANITIZE (this); |
| 98 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
| 99 | unsigned int count = header.nUnits; |
| 100 | for (unsigned int i = 0; i < count; i++) |
| 101 | if (unlikely (!(*this)[i].sanitize (c, base))) |
| 102 | return_trace (false); |
| 103 | return_trace (true); |
| 104 | } |
| 105 | |
| 106 | template <typename T> |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 107 | inline const Type *bsearch (const T &key) const |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 108 | { |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 109 | unsigned int size = header.unitSize; |
| 110 | int min = 0, max = (int) header.nUnits - 1; |
| 111 | while (min <= max) |
| 112 | { |
| 113 | int mid = (min + max) / 2; |
Behdad Esfahbod | dff2c45 | 2018-09-10 23:29:26 +0200 | [diff] [blame] | 114 | const Type *p = (const Type *) (((const char *) &bytesZ) + (mid * size)); |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 115 | int c = p->cmp (key); |
| 116 | if (c < 0) |
| 117 | max = mid - 1; |
| 118 | else if (c > 0) |
| 119 | min = mid + 1; |
| 120 | else |
| 121 | return p; |
| 122 | } |
Bruce Mitchener | a895737 | 2018-02-04 01:31:53 +0700 | [diff] [blame] | 123 | return nullptr; |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private: |
| 127 | inline bool sanitize_shallow (hb_sanitize_context_t *c) const |
| 128 | { |
| 129 | TRACE_SANITIZE (this); |
| 130 | return_trace (header.sanitize (c) && |
| 131 | Type::static_size >= header.unitSize && |
Behdad Esfahbod | dff2c45 | 2018-09-10 23:29:26 +0200 | [diff] [blame] | 132 | c->check_array (bytesZ.arrayZ, header.nUnits, header.unitSize)); |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | protected: |
Behdad Esfahbod | dff2c45 | 2018-09-10 23:29:26 +0200 | [diff] [blame] | 136 | BinSearchHeader header; |
| 137 | UnsizedArrayOf<HBUINT8> bytesZ; |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 138 | public: |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 139 | DEFINE_SIZE_ARRAY (10, bytesZ); |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 143 | /* |
| 144 | * Lookup Table |
| 145 | */ |
| 146 | |
| 147 | template <typename T> struct Lookup; |
| 148 | |
| 149 | template <typename T> |
| 150 | struct LookupFormat0 |
| 151 | { |
| 152 | friend struct Lookup<T>; |
| 153 | |
| 154 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 155 | inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 156 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 157 | if (unlikely (glyph_id >= num_glyphs)) return nullptr; |
| 158 | return &arrayZ[glyph_id]; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 162 | { |
| 163 | TRACE_SANITIZE (this); |
Behdad Esfahbod | be70738 | 2018-07-17 18:45:25 +0200 | [diff] [blame] | 164 | return_trace (arrayZ.sanitize (c, c->get_num_glyphs ())); |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 168 | HBUINT16 format; /* Format identifier--format = 0 */ |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 169 | UnsizedArrayOf<T> |
| 170 | arrayZ; /* Array of lookup values, indexed by glyph index. */ |
| 171 | public: |
| 172 | DEFINE_SIZE_ARRAY (2, arrayZ); |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | template <typename T> |
| 177 | struct LookupSegmentSingle |
| 178 | { |
| 179 | inline int cmp (hb_codepoint_t g) const { |
| 180 | return g < first ? -1 : g <= last ? 0 : +1 ; |
| 181 | } |
| 182 | |
| 183 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 184 | { |
| 185 | TRACE_SANITIZE (this); |
| 186 | return_trace (c->check_struct (this) && value.sanitize (c)); |
| 187 | } |
| 188 | |
| 189 | GlyphID last; /* Last GlyphID in this segment */ |
| 190 | GlyphID first; /* First GlyphID in this segment */ |
| 191 | T value; /* The lookup value (only one) */ |
| 192 | public: |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 193 | DEFINE_SIZE_STATIC (4 + T::static_size); |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | template <typename T> |
| 197 | struct LookupFormat2 |
| 198 | { |
| 199 | friend struct Lookup<T>; |
| 200 | |
| 201 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 202 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 203 | { |
| 204 | const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 205 | return v ? &v->value : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 209 | { |
| 210 | TRACE_SANITIZE (this); |
| 211 | return_trace (segments.sanitize (c)); |
| 212 | } |
| 213 | |
| 214 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 215 | HBUINT16 format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 216 | BinSearchArrayOf<LookupSegmentSingle<T> > |
| 217 | segments; /* The actual segments. These must already be sorted, |
| 218 | * according to the first word in each one (the last |
| 219 | * glyph in each segment). */ |
| 220 | public: |
| 221 | DEFINE_SIZE_ARRAY (8, segments); |
| 222 | }; |
| 223 | |
| 224 | template <typename T> |
| 225 | struct LookupSegmentArray |
| 226 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 227 | inline const T* get_value (hb_codepoint_t glyph_id, const void *base) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 228 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 229 | return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | inline int cmp (hb_codepoint_t g) const { |
| 233 | return g < first ? -1 : g <= last ? 0 : +1 ; |
| 234 | } |
| 235 | |
| 236 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 237 | { |
| 238 | TRACE_SANITIZE (this); |
| 239 | return_trace (c->check_struct (this) && |
| 240 | first <= last && |
| 241 | valuesZ.sanitize (c, base, last - first + 1)); |
| 242 | } |
| 243 | |
| 244 | GlyphID last; /* Last GlyphID in this segment */ |
| 245 | GlyphID first; /* First GlyphID in this segment */ |
| 246 | OffsetTo<UnsizedArrayOf<T> > |
| 247 | valuesZ; /* A 16-bit offset from the start of |
| 248 | * the table to the data. */ |
| 249 | public: |
| 250 | DEFINE_SIZE_STATIC (6); |
| 251 | }; |
| 252 | |
| 253 | template <typename T> |
| 254 | struct LookupFormat4 |
| 255 | { |
| 256 | friend struct Lookup<T>; |
| 257 | |
| 258 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 259 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 260 | { |
| 261 | const LookupSegmentArray<T> *v = segments.bsearch (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 262 | return v ? v->get_value (glyph_id, this) : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 266 | { |
| 267 | TRACE_SANITIZE (this); |
| 268 | return_trace (segments.sanitize (c, this)); |
| 269 | } |
| 270 | |
| 271 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 272 | HBUINT16 format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 273 | BinSearchArrayOf<LookupSegmentArray<T> > |
| 274 | segments; /* The actual segments. These must already be sorted, |
| 275 | * according to the first word in each one (the last |
| 276 | * glyph in each segment). */ |
| 277 | public: |
| 278 | DEFINE_SIZE_ARRAY (8, segments); |
| 279 | }; |
| 280 | |
| 281 | template <typename T> |
| 282 | struct LookupSingle |
| 283 | { |
| 284 | inline int cmp (hb_codepoint_t g) const { return glyph.cmp (g); } |
| 285 | |
| 286 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 287 | { |
| 288 | TRACE_SANITIZE (this); |
| 289 | return_trace (c->check_struct (this) && value.sanitize (c)); |
| 290 | } |
| 291 | |
| 292 | GlyphID glyph; /* Last GlyphID */ |
| 293 | T value; /* The lookup value (only one) */ |
| 294 | public: |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 295 | DEFINE_SIZE_STATIC (4 + T::static_size); |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | template <typename T> |
| 299 | struct LookupFormat6 |
| 300 | { |
| 301 | friend struct Lookup<T>; |
| 302 | |
| 303 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 304 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 305 | { |
| 306 | const LookupSingle<T> *v = entries.bsearch (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 307 | return v ? &v->value : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 311 | { |
| 312 | TRACE_SANITIZE (this); |
| 313 | return_trace (entries.sanitize (c)); |
| 314 | } |
| 315 | |
| 316 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 317 | HBUINT16 format; /* Format identifier--format = 6 */ |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 318 | BinSearchArrayOf<LookupSingle<T> > |
| 319 | entries; /* The actual entries, sorted by glyph index. */ |
| 320 | public: |
| 321 | DEFINE_SIZE_ARRAY (8, entries); |
| 322 | }; |
| 323 | |
| 324 | template <typename T> |
| 325 | struct LookupFormat8 |
| 326 | { |
| 327 | friend struct Lookup<T>; |
| 328 | |
| 329 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 330 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 331 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 332 | return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ? &valueArrayZ[glyph_id - firstGlyph] : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 336 | { |
| 337 | TRACE_SANITIZE (this); |
| 338 | return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount)); |
| 339 | } |
| 340 | |
| 341 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 342 | HBUINT16 format; /* Format identifier--format = 6 */ |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 343 | GlyphID firstGlyph; /* First glyph index included in the trimmed array. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 344 | HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 345 | * glyph minus the value of firstGlyph plus 1). */ |
| 346 | UnsizedArrayOf<T> |
| 347 | valueArrayZ; /* The lookup values (indexed by the glyph index |
| 348 | * minus the value of firstGlyph). */ |
| 349 | public: |
| 350 | DEFINE_SIZE_ARRAY (6, valueArrayZ); |
| 351 | }; |
| 352 | |
| 353 | template <typename T> |
| 354 | struct Lookup |
| 355 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 356 | inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 357 | { |
| 358 | switch (u.format) { |
| 359 | case 0: return u.format0.get_value (glyph_id, num_glyphs); |
| 360 | case 2: return u.format2.get_value (glyph_id); |
| 361 | case 4: return u.format4.get_value (glyph_id); |
| 362 | case 6: return u.format6.get_value (glyph_id); |
| 363 | case 8: return u.format8.get_value (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame] | 364 | default:return nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 369 | { |
| 370 | TRACE_SANITIZE (this); |
| 371 | if (!u.format.sanitize (c)) return_trace (false); |
| 372 | switch (u.format) { |
| 373 | case 0: return_trace (u.format0.sanitize (c)); |
| 374 | case 2: return_trace (u.format2.sanitize (c)); |
| 375 | case 4: return_trace (u.format4.sanitize (c)); |
| 376 | case 6: return_trace (u.format6.sanitize (c)); |
| 377 | case 8: return_trace (u.format8.sanitize (c)); |
| 378 | default:return_trace (true); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | protected: |
| 383 | union { |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 384 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 385 | LookupFormat0<T> format0; |
| 386 | LookupFormat2<T> format2; |
| 387 | LookupFormat4<T> format4; |
| 388 | LookupFormat6<T> format6; |
| 389 | LookupFormat8<T> format8; |
| 390 | } u; |
| 391 | public: |
| 392 | DEFINE_SIZE_UNION (2, format); |
| 393 | }; |
| 394 | |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 395 | |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 396 | /* |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 397 | * Extended State Table |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 398 | */ |
| 399 | |
| 400 | template <typename T> |
| 401 | struct Entry |
| 402 | { |
| 403 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const |
| 404 | { |
| 405 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 17f01af | 2018-01-11 18:54:49 +0100 | [diff] [blame] | 406 | /* Note, we don't recurse-sanitize data because we don't access it. |
| 407 | * That said, in our DEFINE_SIZE_STATIC we access T::static_size, |
| 408 | * which ensures that data has a simple sanitize(). To be determined |
| 409 | * if I need to remove that as well. */ |
| 410 | return_trace (c->check_struct (this)); |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | public: |
Behdad Esfahbod | 17f01af | 2018-01-11 18:54:49 +0100 | [diff] [blame] | 414 | HBUINT16 newState; /* Byte offset from beginning of state table |
| 415 | * to the new state. Really?!?! Or just state |
| 416 | * number? The latter in morx for sure. */ |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 417 | HBUINT16 flags; /* Table specific. */ |
| 418 | T data; /* Optional offsets to per-glyph tables. */ |
| 419 | public: |
| 420 | DEFINE_SIZE_STATIC (4 + T::static_size); |
| 421 | }; |
| 422 | |
| 423 | template <> |
| 424 | struct Entry<void> |
| 425 | { |
| 426 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const |
| 427 | { |
| 428 | TRACE_SANITIZE (this); |
| 429 | return_trace (c->check_struct (this)); |
| 430 | } |
| 431 | |
| 432 | public: |
| 433 | HBUINT16 newState; /* Byte offset from beginning of state table to the new state. */ |
| 434 | HBUINT16 flags; /* Table specific. */ |
| 435 | public: |
| 436 | DEFINE_SIZE_STATIC (4); |
| 437 | }; |
| 438 | |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 439 | template <typename Extra> |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 440 | struct StateTable |
| 441 | { |
Behdad Esfahbod | 4653e6c | 2018-09-14 11:31:33 +0200 | [diff] [blame] | 442 | enum State |
| 443 | { |
| 444 | STATE_START_OF_TEXT = 0, |
| 445 | STATE_START_OF_LINE = 1, |
| 446 | }; |
| 447 | enum Class |
| 448 | { |
| 449 | CLASS_END_OF_TEXT = 0, |
| 450 | CLASS_OUT_OF_BOUNDS = 1, |
| 451 | CLASS_DELETED_GLYPH = 2, |
| 452 | CLASS_END_OF_LINE = 3, |
| 453 | }; |
| 454 | |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 455 | inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 456 | { |
| 457 | const HBUINT16 *v = (this+classTable).get_value (glyph_id, num_glyphs); |
Behdad Esfahbod | 957dbed | 2018-09-14 12:14:42 +0200 | [diff] [blame^] | 458 | return v ? (unsigned) *v : (unsigned) CLASS_OUT_OF_BOUNDS; |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 459 | } |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 460 | |
Behdad Esfahbod | 17f01af | 2018-01-11 18:54:49 +0100 | [diff] [blame] | 461 | inline const Entry<Extra> *get_entries () const |
| 462 | { |
| 463 | return (this+entryTable).arrayZ; |
| 464 | } |
| 465 | |
| 466 | inline const Entry<Extra> *get_entryZ (unsigned int state, unsigned int klass) const |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 467 | { |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 468 | if (unlikely (klass >= nClasses)) return nullptr; |
| 469 | |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 470 | const HBUINT16 *states = (this+stateArrayTable).arrayZ; |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 471 | const Entry<Extra> *entries = (this+entryTable).arrayZ; |
| 472 | |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 473 | unsigned int entry = states[state * nClasses + klass]; |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 474 | |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 475 | return &entries[entry]; |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Behdad Esfahbod | 17f01af | 2018-01-11 18:54:49 +0100 | [diff] [blame] | 478 | inline bool sanitize (hb_sanitize_context_t *c, |
| 479 | unsigned int *num_entries_out = nullptr) const |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 480 | { |
| 481 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 12fffce | 2018-01-15 15:41:51 -0500 | [diff] [blame] | 482 | if (unlikely (!(c->check_struct (this) && |
| 483 | classTable.sanitize (c, this)))) return_trace (false); |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 484 | |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 485 | const HBUINT16 *states = (this+stateArrayTable).arrayZ; |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 486 | const Entry<Extra> *entries = (this+entryTable).arrayZ; |
| 487 | |
| 488 | unsigned int num_states = 1; |
| 489 | unsigned int num_entries = 0; |
| 490 | |
| 491 | unsigned int state = 0; |
| 492 | unsigned int entry = 0; |
| 493 | while (state < num_states) |
| 494 | { |
Behdad Esfahbod | 5021ba2 | 2018-02-08 15:11:28 -0600 | [diff] [blame] | 495 | if (unlikely (!c->check_array (states, |
Behdad Esfahbod | 9507b05 | 2018-09-10 23:18:07 +0200 | [diff] [blame] | 496 | num_states, |
| 497 | states[0].static_size * nClasses))) |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 498 | return_trace (false); |
| 499 | { /* Sweep new states. */ |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 500 | const HBUINT16 *stop = &states[num_states * nClasses]; |
| 501 | for (const HBUINT16 *p = &states[state * nClasses]; p < stop; p++) |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 502 | num_entries = MAX<unsigned int> (num_entries, *p + 1); |
| 503 | state = num_states; |
| 504 | } |
| 505 | |
Behdad Esfahbod | 9507b05 | 2018-09-10 23:18:07 +0200 | [diff] [blame] | 506 | if (unlikely (!c->check_array (entries, num_entries))) |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 507 | return_trace (false); |
| 508 | { /* Sweep new entries. */ |
| 509 | const Entry<Extra> *stop = &entries[num_entries]; |
| 510 | for (const Entry<Extra> *p = &entries[entry]; p < stop; p++) |
| 511 | num_states = MAX<unsigned int> (num_states, p->newState + 1); |
| 512 | entry = num_entries; |
| 513 | } |
| 514 | } |
| 515 | |
Behdad Esfahbod | 17f01af | 2018-01-11 18:54:49 +0100 | [diff] [blame] | 516 | if (num_entries_out) |
| 517 | *num_entries_out = num_entries; |
| 518 | |
Behdad Esfahbod | 680cbc2 | 2018-01-11 18:15:53 +0100 | [diff] [blame] | 519 | return_trace (true); |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | protected: |
Behdad Esfahbod | 1f1c85a | 2018-01-11 22:43:57 +0100 | [diff] [blame] | 523 | HBUINT32 nClasses; /* Number of classes, which is the number of indices |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 524 | * in a single line in the state array. */ |
Ebrahim Byagowi | 211da5e | 2018-04-11 17:41:24 +0430 | [diff] [blame] | 525 | LOffsetTo<Lookup<HBUINT16> > |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 526 | classTable; /* Offset to the class table. */ |
Ebrahim Byagowi | 211da5e | 2018-04-11 17:41:24 +0430 | [diff] [blame] | 527 | LOffsetTo<UnsizedArrayOf<HBUINT16> > |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 528 | stateArrayTable;/* Offset to the state array. */ |
Ebrahim Byagowi | 211da5e | 2018-04-11 17:41:24 +0430 | [diff] [blame] | 529 | LOffsetTo<UnsizedArrayOf<Entry<Extra> > > |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 530 | entryTable; /* Offset to the entry array. */ |
| 531 | |
| 532 | public: |
Behdad Esfahbod | 9009b34 | 2018-01-12 12:04:53 +0100 | [diff] [blame] | 533 | DEFINE_SIZE_STATIC (16); |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 534 | }; |
| 535 | |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 536 | template <typename EntryData> |
| 537 | struct StateTableDriver |
| 538 | { |
| 539 | inline StateTableDriver (const StateTable<EntryData> &machine_, |
| 540 | hb_buffer_t *buffer_, |
| 541 | hb_face_t *face_) : |
| 542 | machine (machine_), |
| 543 | buffer (buffer_), |
Behdad Esfahbod | 54e6efa | 2018-02-04 14:58:02 -0500 | [diff] [blame] | 544 | num_glyphs (face_->get_num_glyphs ()) {} |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 545 | |
| 546 | template <typename context_t> |
| 547 | inline void drive (context_t *c) |
| 548 | { |
| 549 | hb_glyph_info_t *info = buffer->info; |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 550 | |
| 551 | if (!c->in_place) |
| 552 | buffer->clear_output (); |
| 553 | |
Behdad Esfahbod | 4653e6c | 2018-09-14 11:31:33 +0200 | [diff] [blame] | 554 | unsigned int state = StateTable<EntryData>::STATE_START_OF_TEXT; |
Behdad Esfahbod | f760022 | 2018-01-12 11:09:21 +0100 | [diff] [blame] | 555 | bool last_was_dont_advance = false; |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 556 | for (buffer->idx = 0;;) |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 557 | { |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 558 | unsigned int klass = buffer->idx < buffer->len ? |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 559 | machine.get_class (info[buffer->idx].codepoint, num_glyphs) : |
Behdad Esfahbod | 4653e6c | 2018-09-14 11:31:33 +0200 | [diff] [blame] | 560 | (unsigned) StateTable<EntryData>::CLASS_END_OF_TEXT; |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 561 | const Entry<EntryData> *entry = machine.get_entryZ (state, klass); |
| 562 | if (unlikely (!entry)) |
| 563 | break; |
| 564 | |
Behdad Esfahbod | 54e6efa | 2018-02-04 14:58:02 -0500 | [diff] [blame] | 565 | /* Unsafe-to-break before this if not in state 0, as things might |
| 566 | * go differently if we start from state 0 here. */ |
| 567 | if (state && buffer->idx) |
| 568 | { |
Behdad Esfahbod | 2971e9d | 2018-02-06 11:48:04 -0500 | [diff] [blame] | 569 | /* If there's no action and we're just epsilon-transitioning to state 0, |
| 570 | * safe to break. */ |
| 571 | if (c->is_actionable (this, entry) || |
| 572 | !(entry->newState == 0 && entry->flags == context_t::DontAdvance)) |
Behdad Esfahbod | 54e6efa | 2018-02-04 14:58:02 -0500 | [diff] [blame] | 573 | buffer->unsafe_to_break (buffer->idx - 1, buffer->idx + 1); |
| 574 | } |
| 575 | |
| 576 | /* Unsafe-to-break if end-of-text would kick in here. */ |
| 577 | if (buffer->idx + 2 <= buffer->len) |
| 578 | { |
| 579 | const Entry<EntryData> *end_entry = machine.get_entryZ (state, 0); |
| 580 | if (c->is_actionable (this, end_entry)) |
| 581 | buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); |
| 582 | } |
| 583 | |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 584 | if (unlikely (!c->transition (this, entry))) |
| 585 | break; |
Behdad Esfahbod | f760022 | 2018-01-12 11:09:21 +0100 | [diff] [blame] | 586 | |
Behdad Esfahbod | 7033fe5 | 2018-02-18 17:12:04 -0800 | [diff] [blame] | 587 | last_was_dont_advance = (entry->flags & context_t::DontAdvance) && buffer->max_ops-- > 0; |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 588 | |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 589 | state = entry->newState; |
| 590 | |
| 591 | if (buffer->idx == buffer->len) |
| 592 | break; |
| 593 | |
Behdad Esfahbod | 57051b4 | 2018-01-12 11:42:25 +0100 | [diff] [blame] | 594 | if (!last_was_dont_advance) |
| 595 | buffer->next_glyph (); |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Behdad Esfahbod | 57051b4 | 2018-01-12 11:42:25 +0100 | [diff] [blame] | 598 | if (!c->in_place) |
| 599 | { |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 600 | for (; buffer->idx < buffer->len;) |
Behdad Esfahbod | 57051b4 | 2018-01-12 11:42:25 +0100 | [diff] [blame] | 601 | buffer->next_glyph (); |
Behdad Esfahbod | e6f283e | 2018-01-19 18:08:56 -0800 | [diff] [blame] | 602 | buffer->swap_buffers (); |
Behdad Esfahbod | 57051b4 | 2018-01-12 11:42:25 +0100 | [diff] [blame] | 603 | } |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | public: |
| 607 | const StateTable<EntryData> &machine; |
| 608 | hb_buffer_t *buffer; |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 609 | unsigned int num_glyphs; |
Behdad Esfahbod | 117cfe7 | 2018-01-12 00:01:36 +0100 | [diff] [blame] | 610 | }; |
| 611 | |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 612 | |
Behdad Esfahbod | 046690a | 2018-01-17 16:59:55 -0800 | [diff] [blame] | 613 | |
| 614 | struct hb_aat_apply_context_t : |
| 615 | hb_dispatch_context_t<hb_aat_apply_context_t, bool, HB_DEBUG_APPLY> |
| 616 | { |
| 617 | inline const char *get_name (void) { return "APPLY"; } |
| 618 | template <typename T> |
| 619 | inline return_t dispatch (const T &obj) { return obj.apply (this); } |
| 620 | static return_t default_return_value (void) { return false; } |
| 621 | bool stop_sublookup_iteration (return_t r) const { return r; } |
| 622 | |
| 623 | hb_font_t *font; |
| 624 | hb_face_t *face; |
| 625 | hb_buffer_t *buffer; |
Behdad Esfahbod | f07ce66 | 2018-01-19 16:52:01 -0800 | [diff] [blame] | 626 | hb_sanitize_context_t sanitizer; |
Behdad Esfahbod | 046690a | 2018-01-17 16:59:55 -0800 | [diff] [blame] | 627 | |
Behdad Esfahbod | 126ffdb | 2018-02-07 12:26:41 -0500 | [diff] [blame] | 628 | /* Unused. For debug tracing only. */ |
| 629 | unsigned int lookup_index; |
| 630 | unsigned int debug_depth; |
| 631 | |
Behdad Esfahbod | f07ce66 | 2018-01-19 16:52:01 -0800 | [diff] [blame] | 632 | inline hb_aat_apply_context_t (hb_font_t *font_, |
| 633 | hb_buffer_t *buffer_, |
| 634 | hb_blob_t *table) : |
| 635 | font (font_), face (font->face), buffer (buffer_), |
Behdad Esfahbod | 126ffdb | 2018-02-07 12:26:41 -0500 | [diff] [blame] | 636 | sanitizer (), lookup_index (0), debug_depth (0) |
Behdad Esfahbod | f07ce66 | 2018-01-19 16:52:01 -0800 | [diff] [blame] | 637 | { |
| 638 | sanitizer.init (table); |
Behdad Esfahbod | be70738 | 2018-07-17 18:45:25 +0200 | [diff] [blame] | 639 | sanitizer.set_num_glyphs (face->get_num_glyphs ()); |
Behdad Esfahbod | f07ce66 | 2018-01-19 16:52:01 -0800 | [diff] [blame] | 640 | sanitizer.start_processing (); |
| 641 | } |
| 642 | |
Behdad Esfahbod | ee433d3 | 2018-02-07 12:30:18 -0500 | [diff] [blame] | 643 | inline void set_lookup_index (unsigned int i) { lookup_index = i; } |
| 644 | |
Behdad Esfahbod | f07ce66 | 2018-01-19 16:52:01 -0800 | [diff] [blame] | 645 | inline ~hb_aat_apply_context_t (void) |
| 646 | { |
| 647 | sanitizer.end_processing (); |
| 648 | } |
Behdad Esfahbod | 046690a | 2018-01-17 16:59:55 -0800 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 652 | } /* namespace AAT */ |
| 653 | |
| 654 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 655 | #endif /* HB_AAT_LAYOUT_COMMON_HH */ |