blob: af71ebd385f04db5abc7ba9718c5d56920d9d044 [file] [log] [blame]
Behdad Esfahboda0175e72017-08-17 16:55:54 -07001/*
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 Esfahbodc77ae402018-08-25 22:36:36 -070027#ifndef HB_AAT_LAYOUT_COMMON_HH
28#define HB_AAT_LAYOUT_COMMON_HH
Behdad Esfahboda0175e72017-08-17 16:55:54 -070029
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-aat-layout.hh"
Behdad Esfahboda0175e72017-08-17 16:55:54 -070031
32
33namespace AAT {
34
35using namespace OT;
36
37
38/*
39 * Binary Searching Tables
40 */
41
42struct BinSearchHeader
43{
Behdad Esfahboda0175e72017-08-17 16:55:54 -070044
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 Esfahbod6b191782018-01-10 03:07:30 +010051 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 Esfahboda0175e72017-08-17 16:55:54 -070054 * that is less than or equal to the value of nUnits. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +010055 HBUINT16 entrySelector; /* The log base 2 of the largest power of 2 less than
Behdad Esfahboda0175e72017-08-17 16:55:54 -070056 * or equal to the value of nUnits. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +010057 HBUINT16 rangeShift; /* The value of unitSize times the difference of the
Behdad Esfahboda0175e72017-08-17 16:55:54 -070058 * 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
64template <typename Type>
65struct BinSearchArrayOf
66{
67 inline const Type& operator [] (unsigned int i) const
68 {
69 if (unlikely (i >= header.nUnits)) return Null(Type);
Behdad Esfahboddff2c452018-09-10 23:29:26 +020070 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
Behdad Esfahboda0175e72017-08-17 16:55:54 -070071 }
72 inline Type& operator [] (unsigned int i)
73 {
Behdad Esfahboddff2c452018-09-10 23:29:26 +020074 return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
Behdad Esfahboda0175e72017-08-17 16:55:54 -070075 }
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 Esfahboddff2c452018-09-10 23:29:26 +020091 (void) (false && StructAtOffset<Type> (&bytesZ, 0).sanitize (c));
Behdad Esfahboda0175e72017-08-17 16:55:54 -070092
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 Esfahbod470fe5b2018-01-09 15:48:51 +0100107 inline const Type *bsearch (const T &key) const
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700108 {
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100109 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 Esfahboddff2c452018-09-10 23:29:26 +0200114 const Type *p = (const Type *) (((const char *) &bytesZ) + (mid * size));
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100115 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 Mitchenera8957372018-02-04 01:31:53 +0700123 return nullptr;
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700124 }
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 Esfahboddff2c452018-09-10 23:29:26 +0200132 c->check_array (bytesZ.arrayZ, header.nUnits, header.unitSize));
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700133 }
134
135 protected:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200136 BinSearchHeader header;
137 UnsizedArrayOf<HBUINT8> bytesZ;
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700138 public:
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700139 DEFINE_SIZE_ARRAY (10, bytesZ);
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700140};
141
142
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100143/*
144 * Lookup Table
145 */
146
147template <typename T> struct Lookup;
148
149template <typename T>
150struct LookupFormat0
151{
152 friend struct Lookup<T>;
153
154 private:
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100155 inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100156 {
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100157 if (unlikely (glyph_id >= num_glyphs)) return nullptr;
158 return &arrayZ[glyph_id];
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100159 }
160
161 inline bool sanitize (hb_sanitize_context_t *c) const
162 {
163 TRACE_SANITIZE (this);
Behdad Esfahbodbe707382018-07-17 18:45:25 +0200164 return_trace (arrayZ.sanitize (c, c->get_num_glyphs ()));
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100165 }
166
167 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100168 HBUINT16 format; /* Format identifier--format = 0 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100169 UnsizedArrayOf<T>
170 arrayZ; /* Array of lookup values, indexed by glyph index. */
171 public:
172 DEFINE_SIZE_ARRAY (2, arrayZ);
173};
174
175
176template <typename T>
177struct 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 Esfahbodca42d962018-01-11 09:15:34 +0100193 DEFINE_SIZE_STATIC (4 + T::static_size);
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100194};
195
196template <typename T>
197struct LookupFormat2
198{
199 friend struct Lookup<T>;
200
201 private:
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100202 inline const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100203 {
204 const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100205 return v ? &v->value : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100206 }
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 Esfahbod6b191782018-01-10 03:07:30 +0100215 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100216 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
224template <typename T>
225struct LookupSegmentArray
226{
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100227 inline const T* get_value (hb_codepoint_t glyph_id, const void *base) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100228 {
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100229 return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100230 }
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
253template <typename T>
254struct LookupFormat4
255{
256 friend struct Lookup<T>;
257
258 private:
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100259 inline const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100260 {
261 const LookupSegmentArray<T> *v = segments.bsearch (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100262 return v ? v->get_value (glyph_id, this) : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100263 }
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 Esfahbod6b191782018-01-10 03:07:30 +0100272 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100273 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
281template <typename T>
282struct 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 Esfahbodca42d962018-01-11 09:15:34 +0100295 DEFINE_SIZE_STATIC (4 + T::static_size);
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100296};
297
298template <typename T>
299struct LookupFormat6
300{
301 friend struct Lookup<T>;
302
303 private:
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100304 inline const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100305 {
306 const LookupSingle<T> *v = entries.bsearch (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100307 return v ? &v->value : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100308 }
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 Esfahbod6b191782018-01-10 03:07:30 +0100317 HBUINT16 format; /* Format identifier--format = 6 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100318 BinSearchArrayOf<LookupSingle<T> >
319 entries; /* The actual entries, sorted by glyph index. */
320 public:
321 DEFINE_SIZE_ARRAY (8, entries);
322};
323
324template <typename T>
325struct LookupFormat8
326{
327 friend struct Lookup<T>;
328
329 private:
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100330 inline const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100331 {
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100332 return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ? &valueArrayZ[glyph_id - firstGlyph] : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100333 }
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 Esfahbod6b191782018-01-10 03:07:30 +0100342 HBUINT16 format; /* Format identifier--format = 6 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100343 GlyphID firstGlyph; /* First glyph index included in the trimmed array. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100344 HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100345 * 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
353template <typename T>
354struct Lookup
355{
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100356 inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100357 {
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 Esfahbod748b9892018-01-09 17:55:17 +0100364 default:return nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100365 }
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 Esfahbod6b191782018-01-10 03:07:30 +0100384 HBUINT16 format; /* Format identifier */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100385 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 Esfahbod470fe5b2018-01-09 15:48:51 +0100395
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100396/*
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100397 * Extended State Table
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100398 */
399
400template <typename T>
401struct Entry
402{
403 inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
404 {
405 TRACE_SANITIZE (this);
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100406 /* 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 Esfahbodca42d962018-01-11 09:15:34 +0100411 }
412
413 public:
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100414 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 Esfahbodca42d962018-01-11 09:15:34 +0100417 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
423template <>
424struct 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 Esfahbod1f1c85a2018-01-11 22:43:57 +0100439template <typename Extra>
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100440struct StateTable
441{
Behdad Esfahbod4653e6c2018-09-14 11:31:33 +0200442 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 Esfahbodca42d962018-01-11 09:15:34 +0100455 inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100456 {
457 const HBUINT16 *v = (this+classTable).get_value (glyph_id, num_glyphs);
Behdad Esfahbod957dbed2018-09-14 12:14:42 +0200458 return v ? (unsigned) *v : (unsigned) CLASS_OUT_OF_BOUNDS;
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100459 }
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100460
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100461 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 Esfahbodca42d962018-01-11 09:15:34 +0100467 {
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100468 if (unlikely (klass >= nClasses)) return nullptr;
469
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100470 const HBUINT16 *states = (this+stateArrayTable).arrayZ;
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100471 const Entry<Extra> *entries = (this+entryTable).arrayZ;
472
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100473 unsigned int entry = states[state * nClasses + klass];
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100474
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100475 return &entries[entry];
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100476 }
477
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100478 inline bool sanitize (hb_sanitize_context_t *c,
479 unsigned int *num_entries_out = nullptr) const
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100480 {
481 TRACE_SANITIZE (this);
Behdad Esfahbod12fffce2018-01-15 15:41:51 -0500482 if (unlikely (!(c->check_struct (this) &&
483 classTable.sanitize (c, this)))) return_trace (false);
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100484
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100485 const HBUINT16 *states = (this+stateArrayTable).arrayZ;
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100486 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 Esfahbod5021ba22018-02-08 15:11:28 -0600495 if (unlikely (!c->check_array (states,
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200496 num_states,
497 states[0].static_size * nClasses)))
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100498 return_trace (false);
499 { /* Sweep new states. */
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100500 const HBUINT16 *stop = &states[num_states * nClasses];
501 for (const HBUINT16 *p = &states[state * nClasses]; p < stop; p++)
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100502 num_entries = MAX<unsigned int> (num_entries, *p + 1);
503 state = num_states;
504 }
505
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200506 if (unlikely (!c->check_array (entries, num_entries)))
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100507 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 Esfahbod17f01af2018-01-11 18:54:49 +0100516 if (num_entries_out)
517 *num_entries_out = num_entries;
518
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100519 return_trace (true);
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100520 }
521
522 protected:
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100523 HBUINT32 nClasses; /* Number of classes, which is the number of indices
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100524 * in a single line in the state array. */
Ebrahim Byagowi211da5e2018-04-11 17:41:24 +0430525 LOffsetTo<Lookup<HBUINT16> >
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100526 classTable; /* Offset to the class table. */
Ebrahim Byagowi211da5e2018-04-11 17:41:24 +0430527 LOffsetTo<UnsizedArrayOf<HBUINT16> >
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100528 stateArrayTable;/* Offset to the state array. */
Ebrahim Byagowi211da5e2018-04-11 17:41:24 +0430529 LOffsetTo<UnsizedArrayOf<Entry<Extra> > >
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100530 entryTable; /* Offset to the entry array. */
531
532 public:
Behdad Esfahbod9009b342018-01-12 12:04:53 +0100533 DEFINE_SIZE_STATIC (16);
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100534};
535
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100536template <typename EntryData>
537struct 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 Esfahbod54e6efa2018-02-04 14:58:02 -0500544 num_glyphs (face_->get_num_glyphs ()) {}
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100545
546 template <typename context_t>
547 inline void drive (context_t *c)
548 {
549 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800550
551 if (!c->in_place)
552 buffer->clear_output ();
553
Behdad Esfahbod4653e6c2018-09-14 11:31:33 +0200554 unsigned int state = StateTable<EntryData>::STATE_START_OF_TEXT;
Behdad Esfahbodf7600222018-01-12 11:09:21 +0100555 bool last_was_dont_advance = false;
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800556 for (buffer->idx = 0;;)
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100557 {
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800558 unsigned int klass = buffer->idx < buffer->len ?
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100559 machine.get_class (info[buffer->idx].codepoint, num_glyphs) :
Behdad Esfahbod4653e6c2018-09-14 11:31:33 +0200560 (unsigned) StateTable<EntryData>::CLASS_END_OF_TEXT;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100561 const Entry<EntryData> *entry = machine.get_entryZ (state, klass);
562 if (unlikely (!entry))
563 break;
564
Behdad Esfahbod54e6efa2018-02-04 14:58:02 -0500565 /* 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 Esfahbod2971e9d2018-02-06 11:48:04 -0500569 /* 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 Esfahbod54e6efa2018-02-04 14:58:02 -0500573 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 Esfahbode6f283e2018-01-19 18:08:56 -0800584 if (unlikely (!c->transition (this, entry)))
585 break;
Behdad Esfahbodf7600222018-01-12 11:09:21 +0100586
Behdad Esfahbod7033fe52018-02-18 17:12:04 -0800587 last_was_dont_advance = (entry->flags & context_t::DontAdvance) && buffer->max_ops-- > 0;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100588
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800589 state = entry->newState;
590
591 if (buffer->idx == buffer->len)
592 break;
593
Behdad Esfahbod57051b42018-01-12 11:42:25 +0100594 if (!last_was_dont_advance)
595 buffer->next_glyph ();
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100596 }
597
Behdad Esfahbod57051b42018-01-12 11:42:25 +0100598 if (!c->in_place)
599 {
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800600 for (; buffer->idx < buffer->len;)
Behdad Esfahbod57051b42018-01-12 11:42:25 +0100601 buffer->next_glyph ();
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800602 buffer->swap_buffers ();
Behdad Esfahbod57051b42018-01-12 11:42:25 +0100603 }
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100604 }
605
606 public:
607 const StateTable<EntryData> &machine;
608 hb_buffer_t *buffer;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100609 unsigned int num_glyphs;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100610};
611
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100612
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800613
614struct 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 Esfahbodf07ce662018-01-19 16:52:01 -0800626 hb_sanitize_context_t sanitizer;
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800627
Behdad Esfahbod126ffdb2018-02-07 12:26:41 -0500628 /* Unused. For debug tracing only. */
629 unsigned int lookup_index;
630 unsigned int debug_depth;
631
Behdad Esfahbodf07ce662018-01-19 16:52:01 -0800632 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 Esfahbod126ffdb2018-02-07 12:26:41 -0500636 sanitizer (), lookup_index (0), debug_depth (0)
Behdad Esfahbodf07ce662018-01-19 16:52:01 -0800637 {
638 sanitizer.init (table);
Behdad Esfahbodbe707382018-07-17 18:45:25 +0200639 sanitizer.set_num_glyphs (face->get_num_glyphs ());
Behdad Esfahbodf07ce662018-01-19 16:52:01 -0800640 sanitizer.start_processing ();
641 }
642
Behdad Esfahbodee433d32018-02-07 12:30:18 -0500643 inline void set_lookup_index (unsigned int i) { lookup_index = i; }
644
Behdad Esfahbodf07ce662018-01-19 16:52:01 -0800645 inline ~hb_aat_apply_context_t (void)
646 {
647 sanitizer.end_processing ();
648 }
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800649};
650
651
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700652} /* namespace AAT */
653
654
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700655#endif /* HB_AAT_LAYOUT_COMMON_HH */