blob: a8c4b7637627c0d2d5ca1becceb384d4ee11c964 [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"
Ebrahim Byagowi47cf9a92018-12-08 10:20:25 +033031#include "hb-open-type.hh"
Behdad Esfahboda0175e72017-08-17 16:55:54 -070032
33
34namespace AAT {
35
36using namespace OT;
37
38
39/*
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010040 * Lookup Table
41 */
42
43template <typename T> struct Lookup;
44
45template <typename T>
46struct LookupFormat0
47{
48 friend struct Lookup<T>;
49
50 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033051 const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010052 {
Behdad Esfahbod748b9892018-01-09 17:55:17 +010053 if (unlikely (glyph_id >= num_glyphs)) return nullptr;
54 return &arrayZ[glyph_id];
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010055 }
56
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033057 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010058 {
59 TRACE_SANITIZE (this);
Behdad Esfahbodbe707382018-07-17 18:45:25 +020060 return_trace (arrayZ.sanitize (c, c->get_num_glyphs ()));
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010061 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033062 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +033063 {
64 TRACE_SANITIZE (this);
65 return_trace (arrayZ.sanitize (c, c->get_num_glyphs (), base));
66 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010067
68 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +010069 HBUINT16 format; /* Format identifier--format = 0 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010070 UnsizedArrayOf<T>
71 arrayZ; /* Array of lookup values, indexed by glyph index. */
72 public:
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -050073 DEFINE_SIZE_UNBOUNDED (2);
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010074};
75
76
77template <typename T>
78struct LookupSegmentSingle
79{
Behdad Esfahbod5d4b0372019-01-22 12:11:24 +010080 static constexpr unsigned TerminationWordCount = 2u;
Behdad Esfahbod3d309722018-11-24 23:12:28 -050081
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033082 int cmp (hb_codepoint_t g) const
83 { return g < first ? -1 : g <= last ? 0 : +1 ; }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010084
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033085 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010086 {
87 TRACE_SANITIZE (this);
88 return_trace (c->check_struct (this) && value.sanitize (c));
89 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033090 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +033091 {
92 TRACE_SANITIZE (this);
93 return_trace (c->check_struct (this) && value.sanitize (c, base));
94 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +010095
96 GlyphID last; /* Last GlyphID in this segment */
97 GlyphID first; /* First GlyphID in this segment */
98 T value; /* The lookup value (only one) */
99 public:
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100100 DEFINE_SIZE_STATIC (4 + T::static_size);
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100101};
102
103template <typename T>
104struct LookupFormat2
105{
106 friend struct Lookup<T>;
107
108 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330109 const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100110 {
111 const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100112 return v ? &v->value : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100113 }
114
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330115 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100116 {
117 TRACE_SANITIZE (this);
118 return_trace (segments.sanitize (c));
119 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330120 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330121 {
122 TRACE_SANITIZE (this);
123 return_trace (segments.sanitize (c, base));
124 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100125
126 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100127 HBUINT16 format; /* Format identifier--format = 2 */
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700128 VarSizedBinSearchArrayOf<LookupSegmentSingle<T>>
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100129 segments; /* The actual segments. These must already be sorted,
130 * according to the first word in each one (the last
131 * glyph in each segment). */
132 public:
133 DEFINE_SIZE_ARRAY (8, segments);
134};
135
136template <typename T>
137struct LookupSegmentArray
138{
Behdad Esfahbod5d4b0372019-01-22 12:11:24 +0100139 static constexpr unsigned TerminationWordCount = 2u;
Behdad Esfahbod3d309722018-11-24 23:12:28 -0500140
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330141 const T* get_value (hb_codepoint_t glyph_id, const void *base) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100142 {
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100143 return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100144 }
145
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330146 int cmp (hb_codepoint_t g) const
147 { return g < first ? -1 : g <= last ? 0 : +1; }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100148
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330149 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100150 {
151 TRACE_SANITIZE (this);
152 return_trace (c->check_struct (this) &&
153 first <= last &&
154 valuesZ.sanitize (c, base, last - first + 1));
155 }
Behdad Esfahbod175bdad2019-04-23 23:57:11 -0400156 template <typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700157 bool sanitize (hb_sanitize_context_t *c, const void *base, Ts&&... ds) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330158 {
159 TRACE_SANITIZE (this);
160 return_trace (c->check_struct (this) &&
161 first <= last &&
Behdad Esfahbod175bdad2019-04-23 23:57:11 -0400162 valuesZ.sanitize (c, base, last - first + 1, hb_forward<Ts> (ds)...));
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330163 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100164
165 GlyphID last; /* Last GlyphID in this segment */
166 GlyphID first; /* First GlyphID in this segment */
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700167 NNOffsetTo<UnsizedArrayOf<T>>
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100168 valuesZ; /* A 16-bit offset from the start of
169 * the table to the data. */
170 public:
171 DEFINE_SIZE_STATIC (6);
172};
173
174template <typename T>
175struct LookupFormat4
176{
177 friend struct Lookup<T>;
178
179 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330180 const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100181 {
182 const LookupSegmentArray<T> *v = segments.bsearch (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100183 return v ? v->get_value (glyph_id, this) : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100184 }
185
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330186 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100187 {
188 TRACE_SANITIZE (this);
189 return_trace (segments.sanitize (c, this));
190 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330191 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330192 {
193 TRACE_SANITIZE (this);
194 return_trace (segments.sanitize (c, this, base));
195 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100196
197 protected:
Behdad Esfahbod2c824d32018-10-11 16:41:01 -0400198 HBUINT16 format; /* Format identifier--format = 4 */
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700199 VarSizedBinSearchArrayOf<LookupSegmentArray<T>>
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100200 segments; /* The actual segments. These must already be sorted,
201 * according to the first word in each one (the last
202 * glyph in each segment). */
203 public:
204 DEFINE_SIZE_ARRAY (8, segments);
205};
206
207template <typename T>
208struct LookupSingle
209{
Behdad Esfahbod5d4b0372019-01-22 12:11:24 +0100210 static constexpr unsigned TerminationWordCount = 1u;
Behdad Esfahbod3d309722018-11-24 23:12:28 -0500211
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330212 int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100213
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330214 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100215 {
216 TRACE_SANITIZE (this);
217 return_trace (c->check_struct (this) && value.sanitize (c));
218 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330219 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330220 {
221 TRACE_SANITIZE (this);
222 return_trace (c->check_struct (this) && value.sanitize (c, base));
223 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100224
225 GlyphID glyph; /* Last GlyphID */
226 T value; /* The lookup value (only one) */
227 public:
Behdad Esfahbod2c824d32018-10-11 16:41:01 -0400228 DEFINE_SIZE_STATIC (2 + T::static_size);
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100229};
230
231template <typename T>
232struct LookupFormat6
233{
234 friend struct Lookup<T>;
235
236 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330237 const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100238 {
239 const LookupSingle<T> *v = entries.bsearch (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100240 return v ? &v->value : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100241 }
242
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330243 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100244 {
245 TRACE_SANITIZE (this);
246 return_trace (entries.sanitize (c));
247 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330248 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330249 {
250 TRACE_SANITIZE (this);
251 return_trace (entries.sanitize (c, base));
252 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100253
254 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100255 HBUINT16 format; /* Format identifier--format = 6 */
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700256 VarSizedBinSearchArrayOf<LookupSingle<T>>
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100257 entries; /* The actual entries, sorted by glyph index. */
258 public:
259 DEFINE_SIZE_ARRAY (8, entries);
260};
261
262template <typename T>
263struct LookupFormat8
264{
265 friend struct Lookup<T>;
266
267 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330268 const T* get_value (hb_codepoint_t glyph_id) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100269 {
Behdad Esfahbod44af1f92018-10-14 14:52:17 -0700270 return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ?
271 &valueArrayZ[glyph_id - firstGlyph] : nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100272 }
273
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330274 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100275 {
276 TRACE_SANITIZE (this);
277 return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount));
278 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330279 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330280 {
281 TRACE_SANITIZE (this);
282 return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount, base));
283 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100284
285 protected:
Behdad Esfahbod44af1f92018-10-14 14:52:17 -0700286 HBUINT16 format; /* Format identifier--format = 8 */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100287 GlyphID firstGlyph; /* First glyph index included in the trimmed array. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100288 HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100289 * glyph minus the value of firstGlyph plus 1). */
290 UnsizedArrayOf<T>
291 valueArrayZ; /* The lookup values (indexed by the glyph index
292 * minus the value of firstGlyph). */
293 public:
294 DEFINE_SIZE_ARRAY (6, valueArrayZ);
295};
296
297template <typename T>
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700298struct LookupFormat10
299{
300 friend struct Lookup<T>;
301
302 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330303 const typename T::type get_value_or_null (hb_codepoint_t glyph_id) const
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700304 {
305 if (!(firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount))
306 return Null(T);
307
308 const HBUINT8 *p = &valueArrayZ[(glyph_id - firstGlyph) * valueSize];
309
310 unsigned int v = 0;
311 unsigned int count = valueSize;
312 for (unsigned int i = 0; i < count; i++)
313 v = (v << 8) | *p++;
314
315 return v;
316 }
317
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330318 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700319 {
320 TRACE_SANITIZE (this);
321 return_trace (c->check_struct (this) &&
322 valueSize <= 4 &&
323 valueArrayZ.sanitize (c, glyphCount * valueSize));
324 }
325
326 protected:
327 HBUINT16 format; /* Format identifier--format = 8 */
328 HBUINT16 valueSize; /* Byte size of each value. */
329 GlyphID firstGlyph; /* First glyph index included in the trimmed array. */
330 HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last
331 * glyph minus the value of firstGlyph plus 1). */
332 UnsizedArrayOf<HBUINT8>
333 valueArrayZ; /* The lookup values (indexed by the glyph index
334 * minus the value of firstGlyph). */
335 public:
Behdad Esfahbod00fdbca2018-10-20 12:04:51 -0700336 DEFINE_SIZE_ARRAY (8, valueArrayZ);
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700337};
338
339template <typename T>
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100340struct Lookup
341{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330342 const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100343 {
344 switch (u.format) {
345 case 0: return u.format0.get_value (glyph_id, num_glyphs);
346 case 2: return u.format2.get_value (glyph_id);
347 case 4: return u.format4.get_value (glyph_id);
348 case 6: return u.format6.get_value (glyph_id);
349 case 8: return u.format8.get_value (glyph_id);
Behdad Esfahbod748b9892018-01-09 17:55:17 +0100350 default:return nullptr;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100351 }
352 }
353
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330354 const typename T::type get_value_or_null (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod7727e732018-10-10 13:24:51 -0400355 {
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700356 switch (u.format) {
357 /* Format 10 cannot return a pointer. */
358 case 10: return u.format10.get_value_or_null (glyph_id);
359 default:
360 const T *v = get_value (glyph_id, num_glyphs);
361 return v ? *v : Null(T);
362 }
Behdad Esfahbod7727e732018-10-10 13:24:51 -0400363 }
364
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330365 typename T::type get_class (hb_codepoint_t glyph_id,
366 unsigned int num_glyphs,
367 unsigned int outOfRange) const
Behdad Esfahbod5b4a7892018-11-28 14:46:26 -0500368 {
369 const T *v = get_value (glyph_id, num_glyphs);
370 return v ? *v : outOfRange;
371 }
372
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330373 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100374 {
375 TRACE_SANITIZE (this);
376 if (!u.format.sanitize (c)) return_trace (false);
377 switch (u.format) {
378 case 0: return_trace (u.format0.sanitize (c));
379 case 2: return_trace (u.format2.sanitize (c));
380 case 4: return_trace (u.format4.sanitize (c));
381 case 6: return_trace (u.format6.sanitize (c));
382 case 8: return_trace (u.format8.sanitize (c));
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700383 case 10: return_trace (u.format10.sanitize (c));
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100384 default:return_trace (true);
385 }
386 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330387 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330388 {
389 TRACE_SANITIZE (this);
390 if (!u.format.sanitize (c)) return_trace (false);
391 switch (u.format) {
392 case 0: return_trace (u.format0.sanitize (c, base));
393 case 2: return_trace (u.format2.sanitize (c, base));
394 case 4: return_trace (u.format4.sanitize (c, base));
395 case 6: return_trace (u.format6.sanitize (c, base));
396 case 8: return_trace (u.format8.sanitize (c, base));
Behdad Esfahbodc99d13d2019-01-17 17:56:27 -0500397 case 10: return_trace (false); /* We don't support format10 here currently. */
Ebrahim Byagowib8b00fb2018-11-08 18:53:14 +0330398 default:return_trace (true);
399 }
400 }
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100401
402 protected:
403 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100404 HBUINT16 format; /* Format identifier */
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100405 LookupFormat0<T> format0;
406 LookupFormat2<T> format2;
407 LookupFormat4<T> format4;
408 LookupFormat6<T> format6;
409 LookupFormat8<T> format8;
Behdad Esfahbodf7c0b432018-10-19 15:23:49 -0700410 LookupFormat10<T> format10;
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100411 } u;
412 public:
Behdad Esfahbod9e8a9b82018-10-17 21:41:25 -0700413 DEFINE_SIZE_UNION (2, format);
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100414};
Behdad Esfahbod9e8a9b82018-10-17 21:41:25 -0700415/* Lookup 0 has unbounded size (dependant on num_glyphs). So we need to defined
416 * special NULL objects for Lookup<> objects, but since it's template our macros
417 * don't work. So we have to hand-code them here. UGLY. */
418} /* Close namespace. */
419/* Ugly hand-coded null objects for template Lookup<> :(. */
420extern HB_INTERNAL const unsigned char _hb_Null_AAT_Lookup[2];
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700421template <typename T>
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700422struct Null<AAT::Lookup<T>> {
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700423 static AAT::Lookup<T> const & get_null ()
424 { return *reinterpret_cast<const AAT::Lookup<T> *> (_hb_Null_AAT_Lookup); }
425};
Behdad Esfahbod9e8a9b82018-10-17 21:41:25 -0700426namespace AAT {
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100427
Behdad Esfahbod102af612018-10-25 17:29:32 -0700428enum { DELETED_GLYPH = 0xFFFF };
Behdad Esfahbod470fe5b2018-01-09 15:48:51 +0100429
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100430/*
Behdad Esfahbodc0383c62018-11-06 15:07:19 -0500431 * (Extended) State Table
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100432 */
433
434template <typename T>
435struct Entry
436{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330437 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100438 {
439 TRACE_SANITIZE (this);
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100440 /* Note, we don't recurse-sanitize data because we don't access it.
441 * That said, in our DEFINE_SIZE_STATIC we access T::static_size,
442 * which ensures that data has a simple sanitize(). To be determined
Behdad Esfahbod39bd07a2018-10-26 21:01:11 -0700443 * if I need to remove that as well.
444 *
Behdad Esfahbodba383782018-11-24 00:27:57 -0500445 * HOWEVER! Because we are a template, our DEFINE_SIZE_STATIC
446 * assertion wouldn't be checked, hence the line below. */
447 static_assert (T::static_size, "");
448
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100449 return_trace (c->check_struct (this));
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100450 }
451
452 public:
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100453 HBUINT16 newState; /* Byte offset from beginning of state table
454 * to the new state. Really?!?! Or just state
455 * number? The latter in morx for sure. */
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100456 HBUINT16 flags; /* Table specific. */
457 T data; /* Optional offsets to per-glyph tables. */
458 public:
459 DEFINE_SIZE_STATIC (4 + T::static_size);
460};
461
462template <>
463struct Entry<void>
464{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330465 bool sanitize (hb_sanitize_context_t *c, unsigned int count /*XXX Unused?*/) const
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100466 {
467 TRACE_SANITIZE (this);
468 return_trace (c->check_struct (this));
469 }
470
471 public:
472 HBUINT16 newState; /* Byte offset from beginning of state table to the new state. */
473 HBUINT16 flags; /* Table specific. */
474 public:
475 DEFINE_SIZE_STATIC (4);
476};
477
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330478template <typename Types, typename Extra>
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100479struct StateTable
480{
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330481 typedef typename Types::HBUINT HBUINT;
482 typedef typename Types::HBUSHORT HBUSHORT;
Behdad Esfahbodf9a9c0f2018-11-28 14:51:56 -0500483 typedef typename Types::ClassTypeNarrow ClassType;
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330484
Behdad Esfahbod4653e6c2018-09-14 11:31:33 +0200485 enum State
486 {
487 STATE_START_OF_TEXT = 0,
488 STATE_START_OF_LINE = 1,
489 };
490 enum Class
491 {
492 CLASS_END_OF_TEXT = 0,
493 CLASS_OUT_OF_BOUNDS = 1,
494 CLASS_DELETED_GLYPH = 2,
495 CLASS_END_OF_LINE = 3,
496 };
497
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330498 int new_state (unsigned int newState) const
Behdad Esfahbod84efe042018-12-02 12:38:53 -0500499 { return Types::extended ? newState : ((int) newState - (int) stateArrayTable) / (int) nClasses; }
Behdad Esfahbod0cf282a2018-10-30 20:51:44 -0700500
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330501 unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100502 {
Behdad Esfahbod102af612018-10-25 17:29:32 -0700503 if (unlikely (glyph_id == DELETED_GLYPH)) return CLASS_DELETED_GLYPH;
Behdad Esfahbodd5c0ca22018-11-07 12:08:44 -0500504 return (this+classTable).get_class (glyph_id, num_glyphs, 1);
Behdad Esfahbod1f1c85a2018-01-11 22:43:57 +0100505 }
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100506
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330507 const Entry<Extra> *get_entries () const
Ebrahim Byagowi7ee5c522018-12-12 15:14:37 +0330508 { return (this+entryTable).arrayZ; }
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100509
Behdad Esfahbod299eca02019-01-24 17:17:00 +0100510 const Entry<Extra> &get_entry (int state, unsigned int klass) const
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100511 {
Behdad Esfahbod299eca02019-01-24 17:17:00 +0100512 if (unlikely (klass >= nClasses))
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700513 klass = StateTable<Types, Entry<Extra>>::CLASS_OUT_OF_BOUNDS;
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100514
Behdad Esfahbodc2527a12018-10-30 19:26:16 -0700515 const HBUSHORT *states = (this+stateArrayTable).arrayZ;
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100516 const Entry<Extra> *entries = (this+entryTable).arrayZ;
517
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100518 unsigned int entry = states[state * nClasses + klass];
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500519 DEBUG_MSG (APPLY, nullptr, "e%u", entry);
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100520
Behdad Esfahbod299eca02019-01-24 17:17:00 +0100521 return entries[entry];
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100522 }
523
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330524 bool sanitize (hb_sanitize_context_t *c,
525 unsigned int *num_entries_out = nullptr) const
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100526 {
527 TRACE_SANITIZE (this);
Behdad Esfahbod12fffce2018-01-15 15:41:51 -0500528 if (unlikely (!(c->check_struct (this) &&
Behdad Esfahbodc4e36f92019-01-24 17:06:16 +0100529 nClasses >= 4 /* Ensure pre-defined classes fit. */ &&
Behdad Esfahbod12fffce2018-01-15 15:41:51 -0500530 classTable.sanitize (c, this)))) return_trace (false);
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100531
Behdad Esfahbodc2527a12018-10-30 19:26:16 -0700532 const HBUSHORT *states = (this+stateArrayTable).arrayZ;
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100533 const Entry<Extra> *entries = (this+entryTable).arrayZ;
534
Behdad Esfahbode9405302018-10-11 15:56:17 -0400535 unsigned int num_classes = nClasses;
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500536 if (unlikely (hb_unsigned_mul_overflows (num_classes, states[0].static_size)))
537 return_trace (false);
538 unsigned int row_stride = num_classes * states[0].static_size;
Behdad Esfahbode9405302018-10-11 15:56:17 -0400539
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500540 /* Apple 'kern' table has this peculiarity:
541 *
542 * "Because the stateTableOffset in the state table header is (strictly
543 * speaking) redundant, some 'kern' tables use it to record an initial
544 * state where that should not be StartOfText. To determine if this is
545 * done, calculate what the stateTableOffset should be. If it's different
546 * from the actual stateTableOffset, use it as the initial state."
547 *
548 * We implement this by calling the initial state zero, but allow *negative*
549 * states if the start state indeed was not the first state. Since the code
550 * is shared, this will also apply to 'mort' table. The 'kerx' / 'morx'
551 * tables are not affected since those address states by index, not offset.
552 */
553
554 int min_state = 0;
555 int max_state = 0;
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100556 unsigned int num_entries = 0;
557
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500558 int state_pos = 0;
559 int state_neg = 0;
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100560 unsigned int entry = 0;
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500561 while (min_state < state_neg || state_pos <= max_state)
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100562 {
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500563 if (min_state < state_neg)
564 {
565 /* Negative states. */
566 if (unlikely (hb_unsigned_mul_overflows (min_state, num_classes)))
567 return_trace (false);
Behdad Esfahbode0144052018-11-12 14:23:31 -0500568 if (unlikely (!c->check_range (&states[min_state * num_classes],
569 -min_state,
570 row_stride)))
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500571 return_trace (false);
Behdad Esfahbodc4623db2019-01-24 17:10:12 +0100572 if ((c->max_ops -= state_neg - min_state) <= 0)
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500573 return_trace (false);
574 { /* Sweep new states. */
575 const HBUSHORT *stop = &states[min_state * num_classes];
576 if (unlikely (stop > states))
577 return_trace (false);
578 for (const HBUSHORT *p = states; stop < p; p--)
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700579 num_entries = hb_max (num_entries, *(p - 1) + 1);
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500580 state_neg = min_state;
581 }
582 }
Behdad Esfahbode9405302018-10-11 15:56:17 -0400583
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500584 if (state_pos <= max_state)
585 {
586 /* Positive states. */
Behdad Esfahbode0144052018-11-12 14:23:31 -0500587 if (unlikely (!c->check_range (states,
588 max_state + 1,
589 row_stride)))
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500590 return_trace (false);
Behdad Esfahbodc4623db2019-01-24 17:10:12 +0100591 if ((c->max_ops -= max_state - state_pos + 1) <= 0)
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500592 return_trace (false);
593 { /* Sweep new states. */
594 if (unlikely (hb_unsigned_mul_overflows ((max_state + 1), num_classes)))
595 return_trace (false);
596 const HBUSHORT *stop = &states[(max_state + 1) * num_classes];
597 if (unlikely (stop < states))
598 return_trace (false);
599 for (const HBUSHORT *p = &states[state_pos * num_classes]; p < stop; p++)
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700600 num_entries = hb_max (num_entries, *p + 1);
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500601 state_pos = max_state + 1;
602 }
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100603 }
604
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200605 if (unlikely (!c->check_array (entries, num_entries)))
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100606 return_trace (false);
Behdad Esfahbodc4623db2019-01-24 17:10:12 +0100607 if ((c->max_ops -= num_entries - entry) <= 0)
Behdad Esfahbod83780302018-10-17 22:34:16 -0700608 return_trace (false);
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100609 { /* Sweep new entries. */
610 const Entry<Extra> *stop = &entries[num_entries];
611 for (const Entry<Extra> *p = &entries[entry]; p < stop; p++)
Behdad Esfahbod0cf282a2018-10-30 20:51:44 -0700612 {
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500613 int newState = new_state (p->newState);
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700614 min_state = hb_min (min_state, newState);
615 max_state = hb_max (max_state, newState);
Behdad Esfahbod0cf282a2018-10-30 20:51:44 -0700616 }
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100617 entry = num_entries;
618 }
619 }
620
Behdad Esfahbod17f01af2018-01-11 18:54:49 +0100621 if (num_entries_out)
622 *num_entries_out = num_entries;
623
Behdad Esfahbod680cbc22018-01-11 18:15:53 +0100624 return_trace (true);
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100625 }
626
627 protected:
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330628 HBUINT nClasses; /* Number of classes, which is the number of indices
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100629 * in a single line in the state array. */
Behdad Esfahbod7e6bd512019-01-17 18:24:18 -0500630 NNOffsetTo<ClassType, HBUINT>
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100631 classTable; /* Offset to the class table. */
Behdad Esfahbod7e6bd512019-01-17 18:24:18 -0500632 NNOffsetTo<UnsizedArrayOf<HBUSHORT>, HBUINT>
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100633 stateArrayTable;/* Offset to the state array. */
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700634 NNOffsetTo<UnsizedArrayOf<Entry<Extra>>, HBUINT>
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100635 entryTable; /* Offset to the entry array. */
636
637 public:
Behdad Esfahbod11dbf0f2018-10-30 21:49:59 -0700638 DEFINE_SIZE_STATIC (4 * sizeof (HBUINT));
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100639};
640
Behdad Esfahbod5b4a7892018-11-28 14:46:26 -0500641template <typename HBUCHAR>
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330642struct ClassTable
643{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330644 unsigned int get_class (hb_codepoint_t glyph_id, unsigned int outOfRange) const
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330645 {
Behdad Esfahbodb11830c2018-11-06 15:23:18 -0500646 unsigned int i = glyph_id - firstGlyph;
647 return i >= classArray.len ? outOfRange : classArray.arrayZ[i];
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330648 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330649 unsigned int get_class (hb_codepoint_t glyph_id,
650 unsigned int num_glyphs HB_UNUSED,
651 unsigned int outOfRange) const
Behdad Esfahbod5b4a7892018-11-28 14:46:26 -0500652 {
653 return get_class (glyph_id, outOfRange);
654 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330655 bool sanitize (hb_sanitize_context_t *c) const
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330656 {
657 TRACE_SANITIZE (this);
Behdad Esfahbodb11830c2018-11-06 15:23:18 -0500658 return_trace (c->check_struct (this) && classArray.sanitize (c));
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330659 }
660 protected:
Behdad Esfahbodb11830c2018-11-06 15:23:18 -0500661 GlyphID firstGlyph; /* First glyph index included in the trimmed array. */
Behdad Esfahbod5b4a7892018-11-28 14:46:26 -0500662 ArrayOf<HBUCHAR> classArray; /* The class codes (indexed by glyph index minus
Behdad Esfahbodb11830c2018-11-06 15:23:18 -0500663 * firstGlyph). */
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330664 public:
Behdad Esfahbodb11830c2018-11-06 15:23:18 -0500665 DEFINE_SIZE_ARRAY (4, classArray);
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330666};
667
Behdad Esfahbod241ba7d2018-11-07 11:51:40 -0500668struct ObsoleteTypes
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330669{
Behdad Esfahbod39e1b6d2019-01-22 12:07:43 +0100670 static constexpr bool extended = false;
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330671 typedef HBUINT16 HBUINT;
672 typedef HBUINT8 HBUSHORT;
Behdad Esfahbodf9a9c0f2018-11-28 14:51:56 -0500673 typedef ClassTable<HBUINT8> ClassTypeNarrow;
674 typedef ClassTable<HBUINT16> ClassTypeWide;
Behdad Esfahbod5b4a7892018-11-28 14:46:26 -0500675
Behdad Esfahbod36e90ef2018-10-31 15:09:09 -0700676 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330677 static unsigned int offsetToIndex (unsigned int offset,
678 const void *base,
679 const T *array)
Behdad Esfahbod36e90ef2018-10-31 15:09:09 -0700680 {
681 return (offset - ((const char *) array - (const char *) base)) / sizeof (T);
682 }
683 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330684 static unsigned int byteOffsetToIndex (unsigned int offset,
685 const void *base,
686 const T *array)
Behdad Esfahbod42a2b492018-11-28 15:24:30 -0500687 {
688 return offsetToIndex (offset, base, array);
689 }
690 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330691 static unsigned int wordOffsetToIndex (unsigned int offset,
692 const void *base,
693 const T *array)
Behdad Esfahbod36e90ef2018-10-31 15:09:09 -0700694 {
695 return offsetToIndex (2 * offset, base, array);
696 }
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330697};
Behdad Esfahbod241ba7d2018-11-07 11:51:40 -0500698struct ExtendedTypes
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330699{
Behdad Esfahbod39e1b6d2019-01-22 12:07:43 +0100700 static constexpr bool extended = true;
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330701 typedef HBUINT32 HBUINT;
702 typedef HBUINT16 HBUSHORT;
Behdad Esfahbodf9a9c0f2018-11-28 14:51:56 -0500703 typedef Lookup<HBUINT16> ClassTypeNarrow;
704 typedef Lookup<HBUINT16> ClassTypeWide;
Behdad Esfahbod5b4a7892018-11-28 14:46:26 -0500705
Behdad Esfahbod36e90ef2018-10-31 15:09:09 -0700706 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330707 static unsigned int offsetToIndex (unsigned int offset,
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500708 const void *base HB_UNUSED,
709 const T *array HB_UNUSED)
Behdad Esfahbod36e90ef2018-10-31 15:09:09 -0700710 {
711 return offset;
712 }
713 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330714 static unsigned int byteOffsetToIndex (unsigned int offset,
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500715 const void *base HB_UNUSED,
716 const T *array HB_UNUSED)
Behdad Esfahbod42a2b492018-11-28 15:24:30 -0500717 {
718 return offset / 2;
719 }
720 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330721 static unsigned int wordOffsetToIndex (unsigned int offset,
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500722 const void *base HB_UNUSED,
723 const T *array HB_UNUSED)
Behdad Esfahbod36e90ef2018-10-31 15:09:09 -0700724 {
725 return offset;
726 }
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330727};
728
729template <typename Types, typename EntryData>
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100730struct StateTableDriver
731{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330732 StateTableDriver (const StateTable<Types, EntryData> &machine_,
733 hb_buffer_t *buffer_,
734 hb_face_t *face_) :
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100735 machine (machine_),
736 buffer (buffer_),
Behdad Esfahbod54e6efa2018-02-04 14:58:02 -0500737 num_glyphs (face_->get_num_glyphs ()) {}
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100738
739 template <typename context_t>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330740 void drive (context_t *c)
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100741 {
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800742 if (!c->in_place)
743 buffer->clear_output ();
744
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500745 int state = StateTable<Types, EntryData>::STATE_START_OF_TEXT;
Behdad Esfahbod1bb8ed82018-10-25 17:33:48 -0700746 for (buffer->idx = 0; buffer->successful;)
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100747 {
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800748 unsigned int klass = buffer->idx < buffer->len ?
Behdad Esfahbod4831e612018-10-05 18:14:13 +0200749 machine.get_class (buffer->info[buffer->idx].codepoint, num_glyphs) :
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330750 (unsigned) StateTable<Types, EntryData>::CLASS_END_OF_TEXT;
Behdad Esfahbod006386b2018-11-07 18:04:53 -0500751 DEBUG_MSG (APPLY, nullptr, "c%u at %u", klass, buffer->idx);
Behdad Esfahbod1ec90512019-01-24 17:21:41 +0100752 const Entry<EntryData> &entry = machine.get_entry (state, klass);
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100753
Behdad Esfahbod54e6efa2018-02-04 14:58:02 -0500754 /* Unsafe-to-break before this if not in state 0, as things might
Behdad Esfahbod7ee50af2018-10-06 21:31:44 +0200755 * go differently if we start from state 0 here.
756 *
757 * Ugh. The indexing here is ugly... */
758 if (state && buffer->backtrack_len () && buffer->idx < buffer->len)
Behdad Esfahbod54e6efa2018-02-04 14:58:02 -0500759 {
Behdad Esfahbod2971e9d2018-02-06 11:48:04 -0500760 /* If there's no action and we're just epsilon-transitioning to state 0,
761 * safe to break. */
762 if (c->is_actionable (this, entry) ||
Behdad Esfahbod1ec90512019-01-24 17:21:41 +0100763 !(entry.newState == StateTable<Types, EntryData>::STATE_START_OF_TEXT &&
764 entry.flags == context_t::DontAdvance))
Behdad Esfahbod7ee50af2018-10-06 21:31:44 +0200765 buffer->unsafe_to_break_from_outbuffer (buffer->backtrack_len () - 1, buffer->idx + 1);
Behdad Esfahbod54e6efa2018-02-04 14:58:02 -0500766 }
767
768 /* Unsafe-to-break if end-of-text would kick in here. */
769 if (buffer->idx + 2 <= buffer->len)
770 {
Behdad Esfahbod1ec90512019-01-24 17:21:41 +0100771 const Entry<EntryData> &end_entry = machine.get_entry (state, StateTable<Types, EntryData>::CLASS_END_OF_TEXT);
Behdad Esfahbod54e6efa2018-02-04 14:58:02 -0500772 if (c->is_actionable (this, end_entry))
773 buffer->unsafe_to_break (buffer->idx, buffer->idx + 2);
774 }
775
Behdad Esfahbodb9769402019-01-24 18:01:07 +0100776 c->transition (this, entry);
Behdad Esfahbodf7600222018-01-12 11:09:21 +0100777
Behdad Esfahbod1ec90512019-01-24 17:21:41 +0100778 state = machine.new_state (entry.newState);
Behdad Esfahbod19090722018-11-07 16:42:16 -0500779 DEBUG_MSG (APPLY, nullptr, "s%d", state);
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800780
781 if (buffer->idx == buffer->len)
Behdad Esfahbod0cf282a2018-10-30 20:51:44 -0700782 break;
Behdad Esfahbode6f283e2018-01-19 18:08:56 -0800783
Behdad Esfahbod1ec90512019-01-24 17:21:41 +0100784 if (!(entry.flags & context_t::DontAdvance) || buffer->max_ops-- <= 0)
Behdad Esfahbod0cf282a2018-10-30 20:51:44 -0700785 buffer->next_glyph ();
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100786 }
787
Behdad Esfahbod57051b42018-01-12 11:42:25 +0100788 if (!c->in_place)
789 {
Behdad Esfahbod7efa3822018-10-14 19:30:44 -0700790 for (; buffer->successful && buffer->idx < buffer->len;)
791 buffer->next_glyph ();
Behdad Esfahbod17335a82018-11-04 02:25:07 -0500792 buffer->swap_buffers ();
Behdad Esfahbod57051b42018-01-12 11:42:25 +0100793 }
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100794 }
795
796 public:
Ebrahim Byagowib053cab2018-10-30 18:41:34 +0330797 const StateTable<Types, EntryData> &machine;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100798 hb_buffer_t *buffer;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100799 unsigned int num_glyphs;
Behdad Esfahbod117cfe72018-01-12 00:01:36 +0100800};
801
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100802
Behdad Esfahbod7bb4da72018-10-11 00:52:07 -0400803struct ankr;
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800804
805struct hb_aat_apply_context_t :
806 hb_dispatch_context_t<hb_aat_apply_context_t, bool, HB_DEBUG_APPLY>
807{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330808 const char *get_name () { return "APPLY"; }
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800809 template <typename T>
Behdad Esfahbodc14efb82019-05-05 09:54:58 -0700810 return_t dispatch (const T &obj) { return obj.apply (this); }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330811 static return_t default_return_value () { return false; }
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800812 bool stop_sublookup_iteration (return_t r) const { return r; }
813
Behdad Esfahbodc221dc02018-11-14 14:49:34 -0500814 const hb_ot_shape_plan_t *plan;
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800815 hb_font_t *font;
816 hb_face_t *face;
817 hb_buffer_t *buffer;
Behdad Esfahbodf07ce662018-01-19 16:52:01 -0800818 hb_sanitize_context_t sanitizer;
Behdad Esfahbodb605db22018-11-04 12:58:02 -0500819 const ankr *ankr_table;
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800820
Behdad Esfahbod126ffdb2018-02-07 12:26:41 -0500821 /* Unused. For debug tracing only. */
822 unsigned int lookup_index;
823 unsigned int debug_depth;
824
Behdad Esfahbodc221dc02018-11-14 14:49:34 -0500825 HB_INTERNAL hb_aat_apply_context_t (const hb_ot_shape_plan_t *plan_,
Behdad Esfahbodb605db22018-11-04 12:58:02 -0500826 hb_font_t *font_,
827 hb_buffer_t *buffer_,
828 hb_blob_t *blob = const_cast<hb_blob_t *> (&Null(hb_blob_t)));
829
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330830 HB_INTERNAL ~hb_aat_apply_context_t ();
Behdad Esfahbodb605db22018-11-04 12:58:02 -0500831
Behdad Esfahbod347ad452019-01-17 17:47:29 -0500832 HB_INTERNAL void set_ankr_table (const AAT::ankr *ankr_table_);
Behdad Esfahbodf07ce662018-01-19 16:52:01 -0800833
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330834 void set_lookup_index (unsigned int i) { lookup_index = i; }
Behdad Esfahbod046690a2018-01-17 16:59:55 -0800835};
836
837
Behdad Esfahboda0175e72017-08-17 16:55:54 -0700838} /* namespace AAT */
839
840
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700841#endif /* HB_AAT_LAYOUT_COMMON_HH */