blob: d6db00566da51862d45aef733c9d5d9765fbf496 [file] [log] [blame]
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
Behdad Esfahbod5b93e8d2012-04-23 22:26:13 -04003 * Copyright © 2010,2012 Google, Inc.
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04006 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
Behdad Esfahbod98370e82010-10-27 17:39:01 -040026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040027 */
28
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040029#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
30#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040031
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -040032#include "hb-buffer-private.hh"
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020033#include "hb-ot-layout-gdef-table.hh"
Behdad Esfahbod1336ecd2012-08-01 21:46:36 -040034#include "hb-set-private.hh"
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040035
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040036
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040037namespace OT {
38
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040039
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040040#ifndef HB_DEBUG_CLOSURE
41#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
42#endif
43
Behdad Esfahbodbe218c62012-11-23 15:32:14 -050044#define TRACE_CLOSURE(this) \
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050045 hb_auto_trace_t<HB_DEBUG_CLOSURE, hb_void_t> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -050046 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -050047 "");
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040048
Behdad Esfahbod77a1a2b2015-10-09 12:20:58 -040049struct hb_closure_context_t :
50 hb_dispatch_context_t<hb_closure_context_t, hb_void_t, HB_DEBUG_CLOSURE>
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040051{
Behdad Esfahboda1733db2012-11-23 16:40:04 -050052 inline const char *get_name (void) { return "CLOSURE"; }
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050053 typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
54 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -050055 inline return_t dispatch (const T &obj) { obj.closure (this); return HB_VOID; }
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050056 static return_t default_return_value (void) { return HB_VOID; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -060057 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050058 return_t recurse (unsigned int lookup_index)
59 {
Behdad Esfahbod9b346772012-11-23 17:55:40 -050060 if (unlikely (nesting_level_left == 0 || !recurse_func))
Behdad Esfahbod2005fa52012-11-22 14:38:10 -050061 return default_return_value ();
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050062
63 nesting_level_left--;
64 recurse_func (this, lookup_index);
65 nesting_level_left++;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050066 return HB_VOID;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050067 }
68
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040069 hb_face_t *face;
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -040070 hb_set_t *glyphs;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050071 recurse_func_t recurse_func;
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040072 unsigned int nesting_level_left;
73 unsigned int debug_depth;
74
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040075 hb_closure_context_t (hb_face_t *face_,
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -040076 hb_set_t *glyphs_,
Behdad Esfahbod5ba45042015-11-02 15:43:08 -080077 unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
Behdad Esfahbode72b3602012-07-19 14:35:23 -040078 face (face_),
79 glyphs (glyphs_),
Behdad Esfahbod9b346772012-11-23 17:55:40 -050080 recurse_func (NULL),
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040081 nesting_level_left (nesting_level_left_),
82 debug_depth (0) {}
Behdad Esfahbod9b346772012-11-23 17:55:40 -050083
84 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040085};
86
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040087
88
Behdad Esfahbod472f2292012-08-07 22:25:24 -040089#ifndef HB_DEBUG_WOULD_APPLY
90#define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
91#endif
92
Behdad Esfahbodbe218c62012-11-23 15:32:14 -050093#define TRACE_WOULD_APPLY(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -050094 hb_auto_trace_t<HB_DEBUG_WOULD_APPLY, bool> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -050095 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -050096 "%d glyphs", c->len);
Behdad Esfahbode72b3602012-07-19 14:35:23 -040097
Behdad Esfahbod77a1a2b2015-10-09 12:20:58 -040098struct hb_would_apply_context_t :
99 hb_dispatch_context_t<hb_would_apply_context_t, bool, HB_DEBUG_WOULD_APPLY>
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400100{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500101 inline const char *get_name (void) { return "WOULD_APPLY"; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500102 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500103 inline return_t dispatch (const T &obj) { return obj.would_apply (this); }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500104 static return_t default_return_value (void) { return false; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600105 bool stop_sublookup_iteration (return_t r) const { return r; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500106
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400107 hb_face_t *face;
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400108 const hb_codepoint_t *glyphs;
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400109 unsigned int len;
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400110 bool zero_context;
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400111 unsigned int debug_depth;
112
113 hb_would_apply_context_t (hb_face_t *face_,
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400114 const hb_codepoint_t *glyphs_,
115 unsigned int len_,
Behdad Esfahbod2bd9fe32012-09-04 15:15:19 -0400116 bool zero_context_) :
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400117 face (face_),
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400118 glyphs (glyphs_),
119 len (len_),
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400120 zero_context (zero_context_),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500121 debug_depth (0) {}
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400122};
123
124
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800125
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800126#ifndef HB_DEBUG_COLLECT_GLYPHS
127#define HB_DEBUG_COLLECT_GLYPHS (HB_DEBUG+0)
128#endif
129
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500130#define TRACE_COLLECT_GLYPHS(this) \
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500131 hb_auto_trace_t<HB_DEBUG_COLLECT_GLYPHS, hb_void_t> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500132 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500133 "");
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800134
Behdad Esfahbod77a1a2b2015-10-09 12:20:58 -0400135struct hb_collect_glyphs_context_t :
136 hb_dispatch_context_t<hb_collect_glyphs_context_t, hb_void_t, HB_DEBUG_COLLECT_GLYPHS>
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800137{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500138 inline const char *get_name (void) { return "COLLECT_GLYPHS"; }
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500139 typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500140 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500141 inline return_t dispatch (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500142 static return_t default_return_value (void) { return HB_VOID; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600143 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500144 return_t recurse (unsigned int lookup_index)
145 {
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500146 if (unlikely (nesting_level_left == 0 || !recurse_func))
147 return default_return_value ();
148
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -0500149 /* Note that GPOS sets recurse_func to NULL already, so it doesn't get
150 * past the previous check. For GSUB, we only want to collect the output
Behdad Esfahbod76ea5632013-05-04 16:01:20 -0400151 * glyphs in the recursion. If output is not requested, we can go home now.
152 *
153 * Note further, that the above is not exactly correct. A recursed lookup
154 * is allowed to match input that is not matched in the context, but that's
155 * not how most fonts are built. It's possible to relax that and recurse
156 * with all sets here if it proves to be an issue.
157 */
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500158
159 if (output == hb_set_get_empty ())
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500160 return HB_VOID;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500161
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700162 /* Return if new lookup was recursed to before. */
163 if (recursed_lookups.has (lookup_index))
164 return HB_VOID;
165
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500166 hb_set_t *old_before = before;
167 hb_set_t *old_input = input;
168 hb_set_t *old_after = after;
169 before = input = after = hb_set_get_empty ();
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -0500170
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500171 nesting_level_left--;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500172 recurse_func (this, lookup_index);
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500173 nesting_level_left++;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500174
175 before = old_before;
176 input = old_input;
177 after = old_after;
178
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700179 recursed_lookups.add (lookup_index);
180
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500181 return HB_VOID;
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500182 }
183
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800184 hb_face_t *face;
Behdad Esfahbod83035932012-12-04 17:08:41 -0500185 hb_set_t *before;
186 hb_set_t *input;
187 hb_set_t *after;
188 hb_set_t *output;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500189 recurse_func_t recurse_func;
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700190 hb_set_t recursed_lookups;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500191 unsigned int nesting_level_left;
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800192 unsigned int debug_depth;
193
194 hb_collect_glyphs_context_t (hb_face_t *face_,
195 hb_set_t *glyphs_before, /* OUT. May be NULL */
196 hb_set_t *glyphs_input, /* OUT. May be NULL */
197 hb_set_t *glyphs_after, /* OUT. May be NULL */
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500198 hb_set_t *glyphs_output, /* OUT. May be NULL */
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800199 unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800200 face (face_),
Behdad Esfahbod83035932012-12-04 17:08:41 -0500201 before (glyphs_before ? glyphs_before : hb_set_get_empty ()),
202 input (glyphs_input ? glyphs_input : hb_set_get_empty ()),
203 after (glyphs_after ? glyphs_after : hb_set_get_empty ()),
204 output (glyphs_output ? glyphs_output : hb_set_get_empty ()),
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500205 recurse_func (NULL),
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700206 recursed_lookups (),
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500207 nesting_level_left (nesting_level_left_),
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700208 debug_depth (0)
209 {
210 recursed_lookups.init ();
211 }
212 ~hb_collect_glyphs_context_t (void)
213 {
214 recursed_lookups.fini ();
215 }
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500216
217 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800218};
219
220
221
Behdad Esfahbod295ef1d2014-12-12 20:43:18 -0800222#ifndef HB_DEBUG_GET_COVERAGE
223#define HB_DEBUG_GET_COVERAGE (HB_DEBUG+0)
224#endif
225
Behdad Esfahbod77a1a2b2015-10-09 12:20:58 -0400226/* XXX Can we remove this? */
227
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +0300228template <typename set_t>
Behdad Esfahbod77a1a2b2015-10-09 12:20:58 -0400229struct hb_add_coverage_context_t :
230 hb_dispatch_context_t<hb_add_coverage_context_t<set_t>, const Coverage &, HB_DEBUG_GET_COVERAGE>
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500231{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500232 inline const char *get_name (void) { return "GET_COVERAGE"; }
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500233 typedef const Coverage &return_t;
234 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500235 inline return_t dispatch (const T &obj) { return obj.get_coverage (); }
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500236 static return_t default_return_value (void) { return Null(Coverage); }
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +0300237 bool stop_sublookup_iteration (return_t r) const
238 {
239 r.add_coverage (set);
240 return false;
241 }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500242
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +0300243 hb_add_coverage_context_t (set_t *set_) :
244 set (set_),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500245 debug_depth (0) {}
246
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +0300247 set_t *set;
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500248 unsigned int debug_depth;
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500249};
250
251
252
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400253#ifndef HB_DEBUG_APPLY
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400254#define HB_DEBUG_APPLY (HB_DEBUG+0)
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400255#endif
256
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500257#define TRACE_APPLY(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500258 hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500259 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100260 "idx %d gid %u lookup %d", \
261 c->buffer->idx, c->buffer->cur().codepoint, (int) c->lookup_index);
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400262
Behdad Esfahbod77a1a2b2015-10-09 12:20:58 -0400263struct hb_apply_context_t :
264 hb_dispatch_context_t<hb_apply_context_t, bool, HB_DEBUG_APPLY>
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400265{
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500266 struct matcher_t
267 {
268 inline matcher_t (void) :
269 lookup_props (0),
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500270 ignore_zwnj (false),
271 ignore_zwj (false),
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500272 mask (-1),
273#define arg1(arg) (arg) /* Remove the macro to see why it's needed! */
274 syllable arg1(0),
275#undef arg1
276 match_func (NULL),
277 match_data (NULL) {};
278
279 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
280
281 inline void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500282 inline void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500283 inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
284 inline void set_mask (hb_mask_t mask_) { mask = mask_; }
285 inline void set_syllable (uint8_t syllable_) { syllable = syllable_; }
286 inline void set_match_func (match_func_t match_func_,
287 const void *match_data_)
288 { match_func = match_func_; match_data = match_data_; }
289
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500290 enum may_match_t {
291 MATCH_NO,
292 MATCH_YES,
293 MATCH_MAYBE
294 };
295
296 inline may_match_t may_match (const hb_glyph_info_t &info,
297 const USHORT *glyph_data) const
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500298 {
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500299 if (!(info.mask & mask) ||
300 (syllable && syllable != info.syllable ()))
301 return MATCH_NO;
302
303 if (match_func)
304 return match_func (info.codepoint, *glyph_data, match_data) ? MATCH_YES : MATCH_NO;
305
306 return MATCH_MAYBE;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500307 }
308
309 enum may_skip_t {
310 SKIP_NO,
311 SKIP_YES,
312 SKIP_MAYBE
313 };
314
315 inline may_skip_t
316 may_skip (const hb_apply_context_t *c,
317 const hb_glyph_info_t &info) const
318 {
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400319 if (!c->check_glyph_property (&info, lookup_props))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500320 return SKIP_YES;
321
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500322 if (unlikely (_hb_glyph_info_is_default_ignorable (&info) &&
323 (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
Behdad Esfahbod4ba796b2015-07-22 17:41:31 +0100324 (ignore_zwj || !_hb_glyph_info_is_zwj (&info))))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500325 return SKIP_MAYBE;
326
327 return SKIP_NO;
328 }
329
330 protected:
331 unsigned int lookup_props;
332 bool ignore_zwnj;
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500333 bool ignore_zwj;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500334 hb_mask_t mask;
335 uint8_t syllable;
336 match_func_t match_func;
337 const void *match_data;
338 };
339
Behdad Esfahbod69626692015-01-29 13:08:41 +0100340 struct skipping_iterator_t
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500341 {
Behdad Esfahbod514564f2015-01-29 13:48:48 +0100342 inline void init (hb_apply_context_t *c_, bool context_match = false)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500343 {
Behdad Esfahbod514564f2015-01-29 13:48:48 +0100344 c = c_;
345 match_glyph_data = NULL,
346 matcher.set_match_func (NULL, NULL);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500347 matcher.set_lookup_props (c->lookup_props);
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400348 /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
349 matcher.set_ignore_zwnj (context_match || c->table_index == 1);
350 /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
351 matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
Behdad Esfahbod514564f2015-01-29 13:48:48 +0100352 matcher.set_mask (context_match ? -1 : c->lookup_mask);
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500353 }
Behdad Esfahbod514564f2015-01-29 13:48:48 +0100354 inline void set_lookup_props (unsigned int lookup_props)
355 {
356 matcher.set_lookup_props (lookup_props);
357 }
Behdad Esfahbod4a6b1ee2015-10-21 11:20:55 -0200358 inline void set_match_func (matcher_t::match_func_t match_func_,
359 const void *match_data_,
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500360 const USHORT glyph_data[])
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500361 {
Behdad Esfahbod4a6b1ee2015-10-21 11:20:55 -0200362 matcher.set_match_func (match_func_, match_data_);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500363 match_glyph_data = glyph_data;
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500364 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500365
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100366 inline void reset (unsigned int start_index_,
367 unsigned int num_items_)
368 {
369 idx = start_index_;
370 num_items = num_items_;
371 end = c->buffer->len;
372 matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
373 }
374
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500375 inline void reject (void) { num_items++; match_glyph_data--; }
Behdad Esfahbod69626692015-01-29 13:08:41 +0100376
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500377 inline bool next (void)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500378 {
Behdad Esfahbod506ffeb2012-01-18 16:07:53 -0500379 assert (num_items > 0);
Behdad Esfahbod37d13ac2015-01-29 11:38:01 +0100380 while (idx + num_items < end)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500381 {
Behdad Esfahboda4a48fe2012-01-17 18:08:41 -0500382 idx++;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500383 const hb_glyph_info_t &info = c->buffer->info[idx];
384
Behdad Esfahbodff93ac82013-02-21 14:51:40 -0500385 matcher_t::may_skip_t skip = matcher.may_skip (c, info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500386 if (unlikely (skip == matcher_t::SKIP_YES))
387 continue;
388
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500389 matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500390 if (match == matcher_t::MATCH_YES ||
391 (match == matcher_t::MATCH_MAYBE &&
392 skip == matcher_t::SKIP_NO))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500393 {
394 num_items--;
395 match_glyph_data++;
396 return true;
397 }
398
399 if (skip == matcher_t::SKIP_NO)
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500400 return false;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500401 }
402 return false;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500403 }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500404 inline bool prev (void)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500405 {
Behdad Esfahbod506ffeb2012-01-18 16:07:53 -0500406 assert (num_items > 0);
Behdad Esfahbod37d13ac2015-01-29 11:38:01 +0100407 while (idx >= num_items)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500408 {
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500409 idx--;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500410 const hb_glyph_info_t &info = c->buffer->out_info[idx];
411
Behdad Esfahbodff93ac82013-02-21 14:51:40 -0500412 matcher_t::may_skip_t skip = matcher.may_skip (c, info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500413 if (unlikely (skip == matcher_t::SKIP_YES))
414 continue;
415
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500416 matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500417 if (match == matcher_t::MATCH_YES ||
418 (match == matcher_t::MATCH_MAYBE &&
419 skip == matcher_t::SKIP_NO))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500420 {
421 num_items--;
422 match_glyph_data++;
423 return true;
424 }
425
426 if (skip == matcher_t::SKIP_NO)
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500427 return false;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500428 }
429 return false;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500430 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500431
432 unsigned int idx;
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400433 protected:
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500434 hb_apply_context_t *c;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500435 matcher_t matcher;
436 const USHORT *match_glyph_data;
437
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500438 unsigned int num_items;
Behdad Esfahbod69626692015-01-29 13:08:41 +0100439 unsigned int end;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500440 };
441
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100442
443 inline const char *get_name (void) { return "APPLY"; }
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100444 typedef return_t (*recurse_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
445 template <typename T>
446 inline return_t dispatch (const T &obj) { return obj.apply (this); }
447 static return_t default_return_value (void) { return false; }
448 bool stop_sublookup_iteration (return_t r) const { return r; }
449 return_t recurse (unsigned int lookup_index)
450 {
451 if (unlikely (nesting_level_left == 0 || !recurse_func))
452 return default_return_value ();
453
454 nesting_level_left--;
455 bool ret = recurse_func (this, lookup_index);
456 nesting_level_left++;
457 return ret;
458 }
459
460 unsigned int table_index; /* GSUB/GPOS */
461 hb_font_t *font;
462 hb_face_t *face;
463 hb_buffer_t *buffer;
464 hb_direction_t direction;
465 hb_mask_t lookup_mask;
466 bool auto_zwj;
467 recurse_func_t recurse_func;
468 unsigned int nesting_level_left;
469 unsigned int lookup_props;
470 const GDEF &gdef;
471 bool has_glyph_classes;
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100472 skipping_iterator_t iter_input, iter_context;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100473 unsigned int lookup_index;
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100474 unsigned int debug_depth;
475
476
477 hb_apply_context_t (unsigned int table_index_,
478 hb_font_t *font_,
479 hb_buffer_t *buffer_) :
480 table_index (table_index_),
481 font (font_), face (font->face), buffer (buffer_),
482 direction (buffer_->props.direction),
483 lookup_mask (1),
484 auto_zwj (true),
485 recurse_func (NULL),
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800486 nesting_level_left (HB_MAX_NESTING_LEVEL),
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100487 lookup_props (0),
488 gdef (*hb_ot_layout_from_face (face)->gdef),
489 has_glyph_classes (gdef.has_glyph_classes ()),
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100490 iter_input (),
491 iter_context (),
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100492 lookup_index ((unsigned int) -1),
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100493 debug_depth (0) {}
494
495 inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
496 inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
497 inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100498 inline void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; }
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100499 inline void set_lookup_props (unsigned int lookup_props_)
500 {
501 lookup_props = lookup_props_;
502 iter_input.init (this, false);
503 iter_context.init (this, true);
504 }
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100505
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400506 inline bool
507 match_properties_mark (hb_codepoint_t glyph,
508 unsigned int glyph_props,
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700509 unsigned int match_props) const
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400510 {
511 /* If using mark filtering sets, the high short of
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700512 * match_props has the set index.
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400513 */
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700514 if (match_props & LookupFlag::UseMarkFilteringSet)
515 return gdef.mark_set_covers (match_props >> 16, glyph);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400516
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700517 /* The second byte of match_props has the meaning
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400518 * "ignore marks of attachment type different than
519 * the attachment type specified."
520 */
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700521 if (match_props & LookupFlag::MarkAttachmentType)
522 return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400523
524 return true;
525 }
526
527 inline bool
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400528 check_glyph_property (const hb_glyph_info_t *info,
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700529 unsigned int match_props) const
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400530 {
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400531 hb_codepoint_t glyph = info->codepoint;
532 unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);
533
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400534 /* Not covered, if, for example, glyph class is ligature and
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700535 * match_props includes LookupFlags::IgnoreLigatures
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400536 */
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700537 if (glyph_props & match_props & LookupFlag::IgnoreFlags)
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400538 return false;
539
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800540 if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700541 return match_properties_mark (glyph, glyph_props, match_props);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400542
543 return true;
544 }
545
Behdad Esfahboda0161742013-10-18 00:06:30 +0200546 inline void _set_glyph_props (hb_codepoint_t glyph_index,
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100547 unsigned int class_guess = 0,
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400548 bool ligature = false,
549 bool component = false) const
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400550 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200551 unsigned int add_in = _hb_glyph_info_get_glyph_props (&buffer->cur()) &
552 HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
553 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
554 if (ligature)
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400555 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200556 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400557 /* In the only place that the MULTIPLIED bit is used, Uniscribe
558 * seems to only care about the "last" transformation between
559 * Ligature and Multiple substitions. Ie. if you ligate, expand,
560 * and ligate again, it forgives the multiplication and acts as
561 * if only ligation happened. As such, clear MULTIPLIED bit.
562 */
563 add_in &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
564 }
565 if (component)
566 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
Behdad Esfahbod2fca1422012-07-30 18:46:41 -0400567 if (likely (has_glyph_classes))
Behdad Esfahbod05ad6b52013-10-18 00:45:59 +0200568 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400569 else if (class_guess)
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200570 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400571 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500572
Behdad Esfahboda0161742013-10-18 00:06:30 +0200573 inline void replace_glyph (hb_codepoint_t glyph_index) const
Behdad Esfahbod3ec77d62012-06-08 21:44:06 -0400574 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200575 _set_glyph_props (glyph_index);
Behdad Esfahbod98370e82010-10-27 17:39:01 -0400576 buffer->replace_glyph (glyph_index);
577 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200578 inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400579 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200580 _set_glyph_props (glyph_index);
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400581 buffer->cur().codepoint = glyph_index;
582 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200583 inline void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
584 unsigned int class_guess) const
585 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200586 _set_glyph_props (glyph_index, class_guess, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200587 buffer->replace_glyph (glyph_index);
588 }
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400589 inline void output_glyph_for_component (hb_codepoint_t glyph_index,
590 unsigned int class_guess) const
Behdad Esfahboda0161742013-10-18 00:06:30 +0200591 {
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400592 _set_glyph_props (glyph_index, class_guess, false, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200593 buffer->output_glyph (glyph_index);
594 }
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400595};
596
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400597
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400598
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400599typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500600typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400601typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400602
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400603struct ContextClosureFuncs
604{
605 intersects_func_t intersects;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400606};
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500607struct ContextCollectGlyphsFuncs
608{
609 collect_glyphs_func_t collect;
610};
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400611struct ContextApplyFuncs
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400612{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400613 match_func_t match;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400614};
615
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500616
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400617static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400618{
619 return glyphs->has (value);
620}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400621static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400622{
623 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
624 return class_def.intersects_class (glyphs, value);
625}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400626static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400627{
628 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
629 return (data+coverage).intersects (glyphs);
630}
631
632static inline bool intersects_array (hb_closure_context_t *c,
633 unsigned int count,
634 const USHORT values[],
635 intersects_func_t intersects_func,
636 const void *intersects_data)
637{
638 for (unsigned int i = 0; i < count; i++)
639 if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
640 return false;
641 return true;
642}
643
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400644
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500645static inline void collect_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
646{
647 glyphs->add (value);
648}
649static inline void collect_class (hb_set_t *glyphs, const USHORT &value, const void *data)
650{
651 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
652 class_def.add_class (glyphs, value);
653}
654static inline void collect_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
655{
656 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
657 (data+coverage).add_coverage (glyphs);
658}
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500659static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500660 hb_set_t *glyphs,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500661 unsigned int count,
662 const USHORT values[],
663 collect_glyphs_func_t collect_func,
664 const void *collect_data)
665{
666 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500667 collect_func (glyphs, values[i], collect_data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500668}
669
670
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400671static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400672{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400673 return glyph_id == value;
674}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400675static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400676{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400677 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400678 return class_def.get_class (glyph_id) == value;
679}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400680static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400681{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -0400682 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400683 return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400684}
685
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400686static inline bool would_match_input (hb_would_apply_context_t *c,
687 unsigned int count, /* Including the first glyph (not matched) */
688 const USHORT input[], /* Array of input values--start with second glyph */
689 match_func_t match_func,
690 const void *match_data)
691{
692 if (count != c->len)
693 return false;
694
695 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400696 if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400697 return false;
698
699 return true;
700}
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400701static inline bool match_input (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400702 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400703 const USHORT input[], /* Array of input values--start with second glyph */
704 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400705 const void *match_data,
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200706 unsigned int *end_offset,
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800707 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH],
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400708 bool *p_is_mark_ligature = NULL,
709 unsigned int *p_total_component_count = NULL)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400710{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500711 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400712
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800713 if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return_trace (false);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200714
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200715 hb_buffer_t *buffer = c->buffer;
716
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100717 hb_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100718 skippy_iter.reset (buffer->idx, count - 1);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500719 skippy_iter.set_match_func (match_func, match_data, input);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400720
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400721 /*
722 * This is perhaps the trickiest part of OpenType... Remarks:
723 *
724 * - If all components of the ligature were marks, we call this a mark ligature.
725 *
726 * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
727 * it as a ligature glyph.
728 *
729 * - Ligatures cannot be formed across glyphs attached to different components
730 * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
731 * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
732 * However, it would be wrong to ligate that SHADDA,FATHA sequence.o
733 * There is an exception to this: If a ligature tries ligating with marks that
734 * belong to it itself, go ahead, assuming that the font designer knows what
735 * they are doing (otherwise it can break Indic stuff when a matra wants to
736 * ligate with a conjunct...)
737 */
738
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200739 bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400740
741 unsigned int total_component_count = 0;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200742 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400743
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200744 unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
745 unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400746
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200747 match_positions[0] = buffer->idx;
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500748 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400749 {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100750 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200751
752 match_positions[i] = skippy_iter.idx;
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400753
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200754 unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]);
755 unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400756
757 if (first_lig_id && first_lig_comp) {
758 /* If first component was attached to a previous ligature component,
759 * all subsequent components should be attached to the same ligature
760 * component, otherwise we shouldn't ligate them. */
761 if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100762 return_trace (false);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400763 } else {
764 /* If first component was NOT attached to a previous ligature component,
765 * all subsequent components should also NOT be attached to any ligature
766 * component, unless they are attached to the first component itself! */
767 if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100768 return_trace (false);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400769 }
770
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200771 is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200772 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400773 }
774
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200775 *end_offset = skippy_iter.idx - buffer->idx + 1;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400776
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400777 if (p_is_mark_ligature)
778 *p_is_mark_ligature = is_mark_ligature;
779
780 if (p_total_component_count)
781 *p_total_component_count = total_component_count;
782
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100783 return_trace (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400784}
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100785static inline bool ligate_input (hb_apply_context_t *c,
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200786 unsigned int count, /* Including the first glyph */
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800787 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200788 unsigned int match_length,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400789 hb_codepoint_t lig_glyph,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400790 bool is_mark_ligature,
791 unsigned int total_component_count)
792{
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200793 TRACE_APPLY (NULL);
794
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200795 hb_buffer_t *buffer = c->buffer;
796
797 buffer->merge_clusters (buffer->idx, buffer->idx + match_length);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500798
Behdad Esfahboda177d022012-08-28 23:18:22 -0400799 /*
800 * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
801 * the ligature to keep its old ligature id. This will allow it to attach to
802 * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
803 * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
804 * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature
805 * later, we don't want them to lose their ligature id/component, otherwise
806 * GPOS will fail to correctly position the mark ligature on top of the
807 * LAM,LAM,HEH ligature. See:
808 * https://bugzilla.gnome.org/show_bug.cgi?id=676343
809 *
810 * - If a ligature is formed of components that some of which are also ligatures
811 * themselves, and those ligature components had marks attached to *their*
812 * components, we have to attach the marks to the new ligature component
813 * positions! Now *that*'s tricky! And these marks may be following the
814 * last component of the whole sequence, so we should loop forward looking
815 * for them and update them.
816 *
817 * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
818 * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
819 * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature
820 * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to
821 * the new ligature with a component value of 2.
822 *
823 * This in fact happened to a font... See:
824 * https://bugzilla.gnome.org/show_bug.cgi?id=437633
825 */
826
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800827 unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200828 unsigned int lig_id = is_mark_ligature ? 0 : _hb_allocate_lig_id (buffer);
829 unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
830 unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400831 unsigned int components_so_far = last_num_components;
832
833 if (!is_mark_ligature)
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400834 {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200835 _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200836 if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100837 {
Behdad Esfahbod82596692015-11-02 17:44:05 -0800838 _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100839 }
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400840 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200841 c->replace_glyph_with_ligature (lig_glyph, klass);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400842
843 for (unsigned int i = 1; i < count; i++)
844 {
Behdad Esfahbodabadc172015-11-18 17:52:08 -0800845 while (buffer->idx < match_positions[i] && !buffer->in_error)
Behdad Esfahboda177d022012-08-28 23:18:22 -0400846 {
847 if (!is_mark_ligature) {
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000848 unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
849 if (this_comp == 0)
Behdad Esfahbod100fbea2015-12-17 15:23:09 +0000850 this_comp = last_num_components;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400851 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000852 MIN (this_comp, last_num_components);
853 _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400854 }
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200855 buffer->next_glyph ();
Behdad Esfahboda177d022012-08-28 23:18:22 -0400856 }
857
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200858 last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
859 last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400860 components_so_far += last_num_components;
861
862 /* Skip the base glyph */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200863 buffer->idx++;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400864 }
865
866 if (!is_mark_ligature && last_lig_id) {
867 /* Re-adjust components for any marks following. */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200868 for (unsigned int i = buffer->idx; i < buffer->len; i++) {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200869 if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000870 unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->info[i]);
871 if (!this_comp)
872 break;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400873 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000874 MIN (this_comp, last_num_components);
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200875 _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400876 } else
877 break;
878 }
879 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100880 return_trace (true);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400881}
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400882
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400883static inline bool match_backtrack (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400884 unsigned int count,
885 const USHORT backtrack[],
886 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400887 const void *match_data)
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400888{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500889 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400890
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100891 hb_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100892 skippy_iter.reset (c->buffer->backtrack_len (), count);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500893 skippy_iter.set_match_func (match_func, match_data, backtrack);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400894
Behdad Esfahbod4d3aeb82012-01-16 16:43:26 -0500895 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500896 if (!skippy_iter.prev ())
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100897 return_trace (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400898
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100899 return_trace (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400900}
901
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400902static inline bool match_lookahead (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400903 unsigned int count,
904 const USHORT lookahead[],
905 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400906 const void *match_data,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400907 unsigned int offset)
908{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500909 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400910
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100911 hb_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100912 skippy_iter.reset (c->buffer->idx + offset - 1, count);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500913 skippy_iter.set_match_func (match_func, match_data, lookahead);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400914
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500915 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500916 if (!skippy_iter.next ())
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100917 return_trace (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400918
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100919 return_trace (true);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400920}
921
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400922
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400923
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400924struct LookupRecord
925{
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300926 inline bool sanitize (hb_sanitize_context_t *c) const
927 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500928 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100929 return_trace (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400930 }
931
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400932 USHORT sequenceIndex; /* Index into current glyph
933 * sequence--first glyph = 0 */
934 USHORT lookupListIndex; /* Lookup to apply to that
935 * position--zero--based */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400936 public:
937 DEFINE_SIZE_STATIC (4);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400938};
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400939
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400940
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500941template <typename context_t>
942static inline void recurse_lookups (context_t *c,
943 unsigned int lookupCount,
944 const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400945{
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400946 for (unsigned int i = 0; i < lookupCount; i++)
Behdad Esfahbod86522e42013-07-22 19:07:53 -0400947 c->recurse (lookupRecord[i].lookupListIndex);
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400948}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400949
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400950static inline bool apply_lookup (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400951 unsigned int count, /* Including the first glyph */
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800952 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400953 unsigned int lookupCount,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200954 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
955 unsigned int match_length)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400956{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500957 TRACE_APPLY (NULL);
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500958
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200959 hb_buffer_t *buffer = c->buffer;
960 unsigned int end;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400961
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200962 /* All positions are distance from beginning of *output* buffer.
963 * Adjust. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400964 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200965 unsigned int bl = buffer->backtrack_len ();
966 end = bl + match_length;
Behdad Esfahbod8751de52013-07-18 16:29:50 -0400967
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200968 int delta = bl - buffer->idx;
969 /* Convert positions to new indexing. */
970 for (unsigned int j = 0; j < count; j++)
971 match_positions[j] += delta;
Behdad Esfahbod8820bb22013-02-14 07:41:03 -0500972 }
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400973
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200974 for (unsigned int i = 0; i < lookupCount; i++)
975 {
976 unsigned int idx = lookupRecord[i].sequenceIndex;
977 if (idx >= count)
978 continue;
979
Behdad Esfahbod9cc1ed42015-11-19 12:39:09 -0800980 /* Don't recurse to ourself at same position.
981 * Note that this test is too naive, it doesn't catch longer loops. */
982 if (idx == 0 && lookupRecord[i].lookupListIndex == c->lookup_index)
983 continue;
984
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200985 buffer->move_to (match_positions[idx]);
986
987 unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
988 if (!c->recurse (lookupRecord[i].lookupListIndex))
989 continue;
990
991 unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
992 int delta = new_len - orig_len;
993
994 if (!delta)
995 continue;
996
997 /* Recursed lookup changed buffer len. Adjust. */
998
Behdad Esfahbodb6b304f2014-06-05 17:12:54 -0400999 /* end can't go back past the current match position.
1000 * Note: this is only true because we do NOT allow MultipleSubst
1001 * with zero sequence len. */
Behdad Esfahbod37b40cd2015-11-18 23:04:45 -08001002 end = MAX (MIN((int) match_positions[idx] + 1, (int) new_len), int (end) + delta);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001003
1004 unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */
1005
1006 if (delta > 0)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001007 {
Behdad Esfahbod5ba45042015-11-02 15:43:08 -08001008 if (unlikely (delta + count > HB_MAX_CONTEXT_LENGTH))
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001009 break;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001010 }
1011 else
1012 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001013 /* NOTE: delta is negative. */
1014 delta = MAX (delta, (int) next - (int) count);
1015 next -= delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001016 }
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001017
1018 /* Shift! */
1019 memmove (match_positions + next + delta, match_positions + next,
1020 (count - next) * sizeof (match_positions[0]));
1021 next += delta;
1022 count += delta;
1023
1024 /* Fill in new entries. */
1025 for (unsigned int j = idx + 1; j < next; j++)
1026 match_positions[j] = match_positions[j - 1] + 1;
1027
1028 /* And fixup the rest. */
1029 for (; next < count; next++)
1030 match_positions[next] += delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001031 }
1032
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001033 buffer->move_to (end);
1034
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001035 return_trace (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001036}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001037
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001038
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001039
1040/* Contextual lookups */
1041
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001042struct ContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001043{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001044 ContextClosureFuncs funcs;
1045 const void *intersects_data;
1046};
1047
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001048struct ContextCollectGlyphsLookupContext
1049{
1050 ContextCollectGlyphsFuncs funcs;
1051 const void *collect_data;
1052};
1053
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001054struct ContextApplyLookupContext
1055{
1056 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001057 const void *match_data;
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001058};
1059
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001060static inline void context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001061 unsigned int inputCount, /* Including the first glyph (not matched) */
1062 const USHORT input[], /* Array of input values--start with second glyph */
1063 unsigned int lookupCount,
1064 const LookupRecord lookupRecord[],
1065 ContextClosureLookupContext &lookup_context)
1066{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001067 if (intersects_array (c,
1068 inputCount ? inputCount - 1 : 0, input,
1069 lookup_context.funcs.intersects, lookup_context.intersects_data))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001070 recurse_lookups (c,
1071 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001072}
1073
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001074static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1075 unsigned int inputCount, /* Including the first glyph (not matched) */
1076 const USHORT input[], /* Array of input values--start with second glyph */
1077 unsigned int lookupCount,
1078 const LookupRecord lookupRecord[],
1079 ContextCollectGlyphsLookupContext &lookup_context)
1080{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001081 collect_array (c, c->input,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001082 inputCount ? inputCount - 1 : 0, input,
1083 lookup_context.funcs.collect, lookup_context.collect_data);
1084 recurse_lookups (c,
1085 lookupCount, lookupRecord);
1086}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001087
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001088static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
1089 unsigned int inputCount, /* Including the first glyph (not matched) */
1090 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001091 unsigned int lookupCount HB_UNUSED,
1092 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001093 ContextApplyLookupContext &lookup_context)
1094{
1095 return would_match_input (c,
1096 inputCount, input,
1097 lookup_context.funcs.match, lookup_context.match_data);
1098}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001099static inline bool context_apply_lookup (hb_apply_context_t *c,
1100 unsigned int inputCount, /* Including the first glyph (not matched) */
1101 const USHORT input[], /* Array of input values--start with second glyph */
1102 unsigned int lookupCount,
1103 const LookupRecord lookupRecord[],
1104 ContextApplyLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001105{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001106 unsigned int match_length = 0;
Behdad Esfahbod5ba45042015-11-02 15:43:08 -08001107 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001108 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001109 inputCount, input,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001110 lookup_context.funcs.match, lookup_context.match_data,
1111 &match_length, match_positions)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001112 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001113 inputCount, match_positions,
1114 lookupCount, lookupRecord,
1115 match_length);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001116}
1117
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001118struct Rule
1119{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001120 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001121 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001122 TRACE_CLOSURE (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001123 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001124 context_closure_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001125 inputCount, inputZ,
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001126 lookupCount, lookupRecord,
1127 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001128 }
1129
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001130 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1131 {
1132 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001133 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001134 context_collect_glyphs_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001135 inputCount, inputZ,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001136 lookupCount, lookupRecord,
1137 lookup_context);
1138 }
1139
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001140 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1141 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001142 TRACE_WOULD_APPLY (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001143 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001144 return_trace (context_would_apply_lookup (c, inputCount, inputZ, lookupCount, lookupRecord, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001145 }
1146
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001147 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001148 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001149 TRACE_APPLY (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001150 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001151 return_trace (context_apply_lookup (c, inputCount, inputZ, lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001152 }
1153
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001154 public:
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001155 inline bool sanitize (hb_sanitize_context_t *c) const
1156 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001157 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001158 return inputCount.sanitize (c)
1159 && lookupCount.sanitize (c)
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001160 && c->check_range (inputZ,
1161 inputZ[0].static_size * inputCount
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001162 + lookupRecordX[0].static_size * lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001163 }
1164
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001165 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001166 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001167 * glyph sequence--includes the first
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001168 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001169 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001170 USHORT inputZ[VAR]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001171 * second glyph */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001172 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001173 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001174 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001175 DEFINE_SIZE_ARRAY2 (4, inputZ, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001176};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001177
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001178struct RuleSet
1179{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001180 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001181 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001182 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001183 unsigned int num_rules = rule.len;
1184 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001185 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001186 }
1187
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001188 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1189 {
1190 TRACE_COLLECT_GLYPHS (this);
1191 unsigned int num_rules = rule.len;
1192 for (unsigned int i = 0; i < num_rules; i++)
1193 (this+rule[i]).collect_glyphs (c, lookup_context);
1194 }
1195
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001196 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1197 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001198 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001199 unsigned int num_rules = rule.len;
1200 for (unsigned int i = 0; i < num_rules; i++)
1201 {
1202 if ((this+rule[i]).would_apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001203 return_trace (true);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001204 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001205 return_trace (false);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001206 }
1207
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001208 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001209 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001210 TRACE_APPLY (this);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001211 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001212 for (unsigned int i = 0; i < num_rules; i++)
1213 {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001214 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001215 return_trace (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001216 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001217 return_trace (false);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001218 }
1219
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001220 inline bool sanitize (hb_sanitize_context_t *c) const
1221 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001222 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001223 return_trace (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001224 }
1225
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001226 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001227 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001228 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001229 * ordered by preference */
Behdad Esfahboded074222010-05-10 18:08:46 -04001230 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001231 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001232};
1233
1234
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001235struct ContextFormat1
1236{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001237 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001238 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001239 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001240
1241 const Coverage &cov = (this+coverage);
1242
1243 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001244 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001245 NULL
1246 };
1247
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001248 unsigned int count = ruleSet.len;
1249 for (unsigned int i = 0; i < count; i++)
1250 if (cov.intersects_coverage (c->glyphs, i)) {
1251 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001252 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001253 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001254 }
1255
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001256 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1257 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001258 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001259 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001260
1261 struct ContextCollectGlyphsLookupContext lookup_context = {
1262 {collect_glyph},
1263 NULL
1264 };
1265
1266 unsigned int count = ruleSet.len;
1267 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001268 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001269 }
1270
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001271 inline bool would_apply (hb_would_apply_context_t *c) const
1272 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001273 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001274
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001275 const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001276 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001277 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001278 NULL
1279 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001280 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001281 }
1282
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001283 inline const Coverage &get_coverage (void) const
1284 {
1285 return this+coverage;
1286 }
1287
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001288 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001289 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001290 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001291 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -04001292 if (likely (index == NOT_COVERED))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001293 return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001294
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001295 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001296 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001297 {match_glyph},
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001298 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001299 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001300 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001301 }
1302
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001303 inline bool sanitize (hb_sanitize_context_t *c) const
1304 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001305 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001306 return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001307 }
1308
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001309 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001310 USHORT format; /* Format identifier--format = 1 */
1311 OffsetTo<Coverage>
1312 coverage; /* Offset to Coverage table--from
1313 * beginning of table */
1314 OffsetArrayOf<RuleSet>
1315 ruleSet; /* Array of RuleSet tables
1316 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001317 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001318 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001319};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001320
1321
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001322struct ContextFormat2
1323{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001324 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001325 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001326 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001327 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001328 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001329
1330 const ClassDef &class_def = this+classDef;
1331
1332 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001333 {intersects_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001334 &class_def
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001335 };
1336
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001337 unsigned int count = ruleSet.len;
1338 for (unsigned int i = 0; i < count; i++)
1339 if (class_def.intersects_class (c->glyphs, i)) {
1340 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001341 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001342 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001343 }
1344
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001345 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1346 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001347 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001348 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001349
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001350 const ClassDef &class_def = this+classDef;
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001351 struct ContextCollectGlyphsLookupContext lookup_context = {
1352 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001353 &class_def
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001354 };
1355
1356 unsigned int count = ruleSet.len;
1357 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001358 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001359 }
1360
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001361 inline bool would_apply (hb_would_apply_context_t *c) const
1362 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001363 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001364
1365 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001366 unsigned int index = class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001367 const RuleSet &rule_set = this+ruleSet[index];
1368 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001369 {match_class},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001370 &class_def
1371 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001372 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001373 }
1374
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001375 inline const Coverage &get_coverage (void) const
1376 {
1377 return this+coverage;
1378 }
1379
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001380 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001381 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001382 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001383 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001384 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001385
1386 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001387 index = class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001388 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001389 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001390 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001391 &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001392 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001393 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001394 }
1395
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001396 inline bool sanitize (hb_sanitize_context_t *c) const
1397 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001398 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001399 return_trace (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001400 }
1401
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001402 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001403 USHORT format; /* Format identifier--format = 2 */
1404 OffsetTo<Coverage>
1405 coverage; /* Offset to Coverage table--from
1406 * beginning of table */
1407 OffsetTo<ClassDef>
1408 classDef; /* Offset to glyph ClassDef table--from
1409 * beginning of table */
1410 OffsetArrayOf<RuleSet>
1411 ruleSet; /* Array of RuleSet tables
1412 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001413 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001414 DEFINE_SIZE_ARRAY (8, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001415};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001416
1417
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001418struct ContextFormat3
1419{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001420 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001421 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001422 TRACE_CLOSURE (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001423 if (!(this+coverageZ[0]).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001424 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001425
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001426 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001427 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001428 {intersects_coverage},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001429 this
1430 };
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001431 context_closure_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001432 glyphCount, (const USHORT *) (coverageZ + 1),
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001433 lookupCount, lookupRecord,
1434 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001435 }
1436
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001437 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1438 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001439 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001440 (this+coverageZ[0]).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001441
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001442 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001443 struct ContextCollectGlyphsLookupContext lookup_context = {
1444 {collect_coverage},
Behdad Esfahbode75943d2012-11-30 08:38:24 +02001445 this
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001446 };
1447
1448 context_collect_glyphs_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001449 glyphCount, (const USHORT *) (coverageZ + 1),
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001450 lookupCount, lookupRecord,
1451 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001452 }
1453
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001454 inline bool would_apply (hb_would_apply_context_t *c) const
1455 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001456 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001457
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001458 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001459 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001460 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001461 this
1462 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001463 return_trace (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverageZ + 1), lookupCount, lookupRecord, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001464 }
1465
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001466 inline const Coverage &get_coverage (void) const
1467 {
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001468 return this+coverageZ[0];
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001469 }
1470
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001471 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001472 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001473 TRACE_APPLY (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001474 unsigned int index = (this+coverageZ[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001475 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001476
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001477 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001478 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001479 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001480 this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001481 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001482 return_trace (context_apply_lookup (c, glyphCount, (const USHORT *) (coverageZ + 1), lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001483 }
1484
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001485 inline bool sanitize (hb_sanitize_context_t *c) const
1486 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001487 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001488 if (!c->check_struct (this)) return_trace (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001489 unsigned int count = glyphCount;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001490 if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */
1491 if (!c->check_array (coverageZ, coverageZ[0].static_size, count)) return_trace (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001492 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001493 if (!coverageZ[i].sanitize (c, this)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001494 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * count);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001495 return_trace (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001496 }
1497
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001498 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001499 USHORT format; /* Format identifier--format = 3 */
1500 USHORT glyphCount; /* Number of glyphs in the input glyph
1501 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001502 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001503 OffsetTo<Coverage>
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001504 coverageZ[VAR]; /* Array of offsets to Coverage
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001505 * table in glyph sequence order */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001506 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001507 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001508 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001509 DEFINE_SIZE_ARRAY2 (6, coverageZ, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001510};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001511
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001512struct Context
1513{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001514 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001515 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001516 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001517 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001518 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001519 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001520 case 1: return_trace (c->dispatch (u.format1));
1521 case 2: return_trace (c->dispatch (u.format2));
1522 case 3: return_trace (c->dispatch (u.format3));
1523 default:return_trace (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001524 }
1525 }
1526
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001527 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001528 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -04001529 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001530 ContextFormat1 format1;
1531 ContextFormat2 format2;
1532 ContextFormat3 format3;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001533 } u;
1534};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001535
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001536
1537/* Chaining Contextual lookups */
1538
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001539struct ChainContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001540{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001541 ContextClosureFuncs funcs;
1542 const void *intersects_data[3];
1543};
1544
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001545struct ChainContextCollectGlyphsLookupContext
1546{
1547 ContextCollectGlyphsFuncs funcs;
1548 const void *collect_data[3];
1549};
1550
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001551struct ChainContextApplyLookupContext
1552{
1553 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001554 const void *match_data[3];
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001555};
1556
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001557static inline void chain_context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001558 unsigned int backtrackCount,
1559 const USHORT backtrack[],
1560 unsigned int inputCount, /* Including the first glyph (not matched) */
1561 const USHORT input[], /* Array of input values--start with second glyph */
1562 unsigned int lookaheadCount,
1563 const USHORT lookahead[],
1564 unsigned int lookupCount,
1565 const LookupRecord lookupRecord[],
1566 ChainContextClosureLookupContext &lookup_context)
1567{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001568 if (intersects_array (c,
1569 backtrackCount, backtrack,
1570 lookup_context.funcs.intersects, lookup_context.intersects_data[0])
1571 && intersects_array (c,
1572 inputCount ? inputCount - 1 : 0, input,
1573 lookup_context.funcs.intersects, lookup_context.intersects_data[1])
Behdad Esfahbod74439d02013-07-22 19:02:29 -04001574 && intersects_array (c,
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001575 lookaheadCount, lookahead,
1576 lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001577 recurse_lookups (c,
1578 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001579}
1580
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001581static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1582 unsigned int backtrackCount,
1583 const USHORT backtrack[],
1584 unsigned int inputCount, /* Including the first glyph (not matched) */
1585 const USHORT input[], /* Array of input values--start with second glyph */
1586 unsigned int lookaheadCount,
1587 const USHORT lookahead[],
1588 unsigned int lookupCount,
1589 const LookupRecord lookupRecord[],
1590 ChainContextCollectGlyphsLookupContext &lookup_context)
1591{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001592 collect_array (c, c->before,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001593 backtrackCount, backtrack,
1594 lookup_context.funcs.collect, lookup_context.collect_data[0]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001595 collect_array (c, c->input,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001596 inputCount ? inputCount - 1 : 0, input,
1597 lookup_context.funcs.collect, lookup_context.collect_data[1]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001598 collect_array (c, c->after,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001599 lookaheadCount, lookahead,
1600 lookup_context.funcs.collect, lookup_context.collect_data[2]);
1601 recurse_lookups (c,
1602 lookupCount, lookupRecord);
1603}
1604
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001605static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
1606 unsigned int backtrackCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001607 const USHORT backtrack[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001608 unsigned int inputCount, /* Including the first glyph (not matched) */
1609 const USHORT input[], /* Array of input values--start with second glyph */
1610 unsigned int lookaheadCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001611 const USHORT lookahead[] HB_UNUSED,
1612 unsigned int lookupCount HB_UNUSED,
1613 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001614 ChainContextApplyLookupContext &lookup_context)
1615{
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -04001616 return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
Behdad Esfahbod1f2bb172012-08-23 16:10:37 -04001617 && would_match_input (c,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001618 inputCount, input,
1619 lookup_context.funcs.match, lookup_context.match_data[1]);
1620}
1621
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001622static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
1623 unsigned int backtrackCount,
1624 const USHORT backtrack[],
1625 unsigned int inputCount, /* Including the first glyph (not matched) */
1626 const USHORT input[], /* Array of input values--start with second glyph */
1627 unsigned int lookaheadCount,
1628 const USHORT lookahead[],
1629 unsigned int lookupCount,
1630 const LookupRecord lookupRecord[],
1631 ChainContextApplyLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001632{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001633 unsigned int match_length = 0;
Behdad Esfahbod5ba45042015-11-02 15:43:08 -08001634 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001635 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001636 inputCount, input,
1637 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001638 &match_length, match_positions)
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001639 && match_backtrack (c,
1640 backtrackCount, backtrack,
1641 lookup_context.funcs.match, lookup_context.match_data[0])
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001642 && match_lookahead (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001643 lookaheadCount, lookahead,
1644 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001645 match_length)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001646 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001647 inputCount, match_positions,
1648 lookupCount, lookupRecord,
1649 match_length);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001650}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001651
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001652struct ChainRule
1653{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001654 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001655 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001656 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001657 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1658 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1659 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001660 chain_context_closure_lookup (c,
1661 backtrack.len, backtrack.array,
1662 input.len, input.array,
1663 lookahead.len, lookahead.array,
1664 lookup.len, lookup.array,
1665 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001666 }
1667
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001668 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1669 {
1670 TRACE_COLLECT_GLYPHS (this);
1671 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1672 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1673 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1674 chain_context_collect_glyphs_lookup (c,
1675 backtrack.len, backtrack.array,
1676 input.len, input.array,
1677 lookahead.len, lookahead.array,
1678 lookup.len, lookup.array,
1679 lookup_context);
1680 }
1681
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001682 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1683 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001684 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001685 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1686 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1687 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001688 return_trace (chain_context_would_apply_lookup (c,
1689 backtrack.len, backtrack.array,
1690 input.len, input.array,
1691 lookahead.len, lookahead.array, lookup.len,
1692 lookup.array, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001693 }
1694
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001695 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001696 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001697 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001698 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1699 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1700 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001701 return_trace (chain_context_apply_lookup (c,
1702 backtrack.len, backtrack.array,
1703 input.len, input.array,
1704 lookahead.len, lookahead.array, lookup.len,
1705 lookup.array, lookup_context));
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001706 }
1707
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001708 inline bool sanitize (hb_sanitize_context_t *c) const
1709 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001710 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001711 if (!backtrack.sanitize (c)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001712 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001713 if (!input.sanitize (c)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001714 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001715 if (!lookahead.sanitize (c)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001716 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001717 return_trace (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001718 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001719
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001720 protected:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001721 ArrayOf<USHORT>
1722 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001723 * (to be matched before the input
1724 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -04001725 HeadlessArrayOf<USHORT>
1726 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001727 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001728 ArrayOf<USHORT>
1729 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001730 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001731 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001732 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001733 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001734 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04001735 DEFINE_SIZE_MIN (8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001736};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001737
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001738struct ChainRuleSet
1739{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001740 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001741 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001742 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001743 unsigned int num_rules = rule.len;
1744 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001745 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001746 }
1747
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001748 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1749 {
1750 TRACE_COLLECT_GLYPHS (this);
1751 unsigned int num_rules = rule.len;
1752 for (unsigned int i = 0; i < num_rules; i++)
1753 (this+rule[i]).collect_glyphs (c, lookup_context);
1754 }
1755
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001756 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1757 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001758 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001759 unsigned int num_rules = rule.len;
1760 for (unsigned int i = 0; i < num_rules; i++)
1761 if ((this+rule[i]).would_apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001762 return_trace (true);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001763
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001764 return_trace (false);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001765 }
1766
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001767 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001768 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001769 TRACE_APPLY (this);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001770 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001771 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001772 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001773 return_trace (true);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001774
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001775 return_trace (false);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001776 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001777
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001778 inline bool sanitize (hb_sanitize_context_t *c) const
1779 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001780 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001781 return_trace (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001782 }
1783
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001784 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001785 OffsetArrayOf<ChainRule>
1786 rule; /* Array of ChainRule tables
1787 * ordered by preference */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001788 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001789 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001790};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001791
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001792struct ChainContextFormat1
1793{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001794 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001795 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001796 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001797 const Coverage &cov = (this+coverage);
1798
1799 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001800 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001801 {NULL, NULL, NULL}
1802 };
1803
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001804 unsigned int count = ruleSet.len;
1805 for (unsigned int i = 0; i < count; i++)
1806 if (cov.intersects_coverage (c->glyphs, i)) {
1807 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001808 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001809 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001810 }
1811
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001812 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1813 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001814 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001815 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001816
1817 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1818 {collect_glyph},
1819 {NULL, NULL, NULL}
1820 };
1821
1822 unsigned int count = ruleSet.len;
1823 for (unsigned int i = 0; i < count; i++)
1824 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001825 }
1826
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001827 inline bool would_apply (hb_would_apply_context_t *c) const
1828 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001829 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001830
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001831 const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001832 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001833 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001834 {NULL, NULL, NULL}
1835 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001836 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001837 }
1838
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001839 inline const Coverage &get_coverage (void) const
1840 {
1841 return this+coverage;
1842 }
1843
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001844 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001845 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001846 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001847 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001848 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001849
1850 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001851 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001852 {match_glyph},
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001853 {NULL, NULL, NULL}
1854 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001855 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001856 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001857
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001858 inline bool sanitize (hb_sanitize_context_t *c) const
1859 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001860 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001861 return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001862 }
1863
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001864 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001865 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001866 OffsetTo<Coverage>
1867 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001868 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001869 OffsetArrayOf<ChainRuleSet>
1870 ruleSet; /* Array of ChainRuleSet tables
1871 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001872 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001873 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001874};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001875
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001876struct ChainContextFormat2
1877{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001878 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001879 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001880 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001881 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001882 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001883
1884 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1885 const ClassDef &input_class_def = this+inputClassDef;
1886 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1887
1888 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001889 {intersects_class},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001890 {&backtrack_class_def,
1891 &input_class_def,
1892 &lookahead_class_def}
1893 };
1894
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001895 unsigned int count = ruleSet.len;
1896 for (unsigned int i = 0; i < count; i++)
1897 if (input_class_def.intersects_class (c->glyphs, i)) {
1898 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001899 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001900 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001901 }
1902
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001903 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1904 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001905 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001906 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001907
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001908 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1909 const ClassDef &input_class_def = this+inputClassDef;
1910 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1911
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001912 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1913 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001914 {&backtrack_class_def,
1915 &input_class_def,
1916 &lookahead_class_def}
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001917 };
1918
1919 unsigned int count = ruleSet.len;
1920 for (unsigned int i = 0; i < count; i++)
1921 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001922 }
1923
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001924 inline bool would_apply (hb_would_apply_context_t *c) const
1925 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001926 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001927
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001928 const ClassDef &backtrack_class_def = this+backtrackClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001929 const ClassDef &input_class_def = this+inputClassDef;
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001930 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001931
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001932 unsigned int index = input_class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001933 const ChainRuleSet &rule_set = this+ruleSet[index];
1934 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001935 {match_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001936 {&backtrack_class_def,
1937 &input_class_def,
1938 &lookahead_class_def}
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001939 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001940 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001941 }
1942
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001943 inline const Coverage &get_coverage (void) const
1944 {
1945 return this+coverage;
1946 }
1947
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001948 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001949 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001950 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001951 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001952 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001953
1954 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1955 const ClassDef &input_class_def = this+inputClassDef;
1956 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1957
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001958 index = input_class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001959 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001960 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001961 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001962 {&backtrack_class_def,
1963 &input_class_def,
1964 &lookahead_class_def}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001965 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001966 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001967 }
1968
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001969 inline bool sanitize (hb_sanitize_context_t *c) const
1970 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001971 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001972 return_trace (coverage.sanitize (c, this) &&
1973 backtrackClassDef.sanitize (c, this) &&
1974 inputClassDef.sanitize (c, this) &&
1975 lookaheadClassDef.sanitize (c, this) &&
1976 ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001977 }
1978
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001979 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001980 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001981 OffsetTo<Coverage>
1982 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001983 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001984 OffsetTo<ClassDef>
1985 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001986 * containing backtrack sequence
1987 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001988 OffsetTo<ClassDef>
1989 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001990 * table containing input sequence
1991 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001992 OffsetTo<ClassDef>
1993 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001994 * containing lookahead sequence
1995 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001996 OffsetArrayOf<ChainRuleSet>
1997 ruleSet; /* Array of ChainRuleSet tables
1998 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001999 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04002000 DEFINE_SIZE_ARRAY (12, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002001};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002002
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002003struct ChainContextFormat3
2004{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002005 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002006 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002007 TRACE_CLOSURE (this);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002008 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2009
2010 if (!(this+input[0]).intersects (c->glyphs))
2011 return;
2012
2013 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2014 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2015 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002016 {intersects_coverage},
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002017 {this, this, this}
2018 };
2019 chain_context_closure_lookup (c,
2020 backtrack.len, (const USHORT *) backtrack.array,
2021 input.len, (const USHORT *) input.array + 1,
2022 lookahead.len, (const USHORT *) lookahead.array,
2023 lookup.len, lookup.array,
2024 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002025 }
2026
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002027 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
2028 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002029 TRACE_COLLECT_GLYPHS (this);
2030 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2031
Behdad Esfahbod83035932012-12-04 17:08:41 -05002032 (this+input[0]).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002033
2034 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2035 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2036 struct ChainContextCollectGlyphsLookupContext lookup_context = {
2037 {collect_coverage},
2038 {this, this, this}
2039 };
2040 chain_context_collect_glyphs_lookup (c,
2041 backtrack.len, (const USHORT *) backtrack.array,
2042 input.len, (const USHORT *) input.array + 1,
2043 lookahead.len, (const USHORT *) lookahead.array,
2044 lookup.len, lookup.array,
2045 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002046 }
2047
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002048 inline bool would_apply (hb_would_apply_context_t *c) const
2049 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002050 TRACE_WOULD_APPLY (this);
Behdad Esfahbode6f74792012-07-28 18:34:58 -04002051
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002052 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002053 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2054 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2055 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002056 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002057 {this, this, this}
2058 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002059 return_trace (chain_context_would_apply_lookup (c,
2060 backtrack.len, (const USHORT *) backtrack.array,
2061 input.len, (const USHORT *) input.array + 1,
2062 lookahead.len, (const USHORT *) lookahead.array,
2063 lookup.len, lookup.array, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002064 }
2065
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002066 inline const Coverage &get_coverage (void) const
2067 {
2068 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2069 return this+input[0];
2070 }
2071
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002072 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002073 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002074 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002075 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002076
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05002077 unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002078 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002079
Behdad Esfahbode961c862010-04-21 15:56:11 -04002080 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2081 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002082 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002083 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04002084 {this, this, this}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002085 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002086 return_trace (chain_context_apply_lookup (c,
2087 backtrack.len, (const USHORT *) backtrack.array,
2088 input.len, (const USHORT *) input.array + 1,
2089 lookahead.len, (const USHORT *) lookahead.array,
2090 lookup.len, lookup.array, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002091 }
2092
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002093 inline bool sanitize (hb_sanitize_context_t *c) const
2094 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002095 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002096 if (!backtrack.sanitize (c, this)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002097 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002098 if (!input.sanitize (c, this)) return_trace (false);
2099 if (!input.len) return_trace (false); /* To be consistent with Context. */
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002100 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002101 if (!lookahead.sanitize (c, this)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002102 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002103 return_trace (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002104 }
2105
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002106 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002107 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002108 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002109 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002110 * in backtracking sequence, in glyph
2111 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002112 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002113 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002114 * tables in input sequence, in glyph
2115 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002116 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002117 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002118 * in lookahead sequence, in glyph
2119 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002120 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002121 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002122 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002123 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04002124 DEFINE_SIZE_MIN (10);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002125};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002126
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002127struct ChainContext
2128{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002129 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002130 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002131 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002132 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002133 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002134 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002135 case 1: return_trace (c->dispatch (u.format1));
2136 case 2: return_trace (c->dispatch (u.format2));
2137 case 3: return_trace (c->dispatch (u.format3));
2138 default:return_trace (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002139 }
2140 }
2141
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002142 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002143 union {
2144 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002145 ChainContextFormat1 format1;
2146 ChainContextFormat2 format2;
2147 ChainContextFormat3 format3;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002148 } u;
2149};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002150
2151
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002152template <typename T>
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002153struct ExtensionFormat1
2154{
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002155 inline unsigned int get_type (void) const { return extensionLookupType; }
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002156
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002157 template <typename X>
2158 inline const X& get_subtable (void) const
2159 {
2160 unsigned int offset = extensionOffset;
2161 if (unlikely (!offset)) return Null(typename T::LookupSubTable);
2162 return StructAtOffset<typename T::LookupSubTable> (this, offset);
2163 }
2164
2165 template <typename context_t>
2166 inline typename context_t::return_t dispatch (context_t *c) const
2167 {
2168 TRACE_DISPATCH (this, format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002169 if (unlikely (!c->may_dispatch (this, this))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002170 return_trace (get_subtable<typename T::LookupSubTable> ().dispatch (c, get_type ()));
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002171 }
2172
2173 /* This is called from may_dispatch() above with hb_sanitize_context_t. */
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002174 inline bool sanitize (hb_sanitize_context_t *c) const
2175 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002176 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002177 return_trace (c->check_struct (this) && extensionOffset != 0);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002178 }
2179
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002180 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002181 USHORT format; /* Format identifier. Set to 1. */
2182 USHORT extensionLookupType; /* Lookup type of subtable referenced
2183 * by ExtensionOffset (i.e. the
2184 * extension subtable). */
Behdad Esfahbod81f2af42010-04-22 00:58:49 -04002185 ULONG extensionOffset; /* Offset to the extension subtable,
2186 * of lookup type subtable. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002187 public:
2188 DEFINE_SIZE_STATIC (8);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002189};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002190
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002191template <typename T>
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002192struct Extension
2193{
2194 inline unsigned int get_type (void) const
2195 {
2196 switch (u.format) {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002197 case 1: return u.format1.get_type ();
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002198 default:return 0;
2199 }
2200 }
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05002201 template <typename X>
2202 inline const X& get_subtable (void) const
2203 {
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002204 switch (u.format) {
2205 case 1: return u.format1.template get_subtable<typename T::LookupSubTable> ();
2206 default:return Null(typename T::LookupSubTable);
2207 }
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05002208 }
2209
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002210 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002211 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002212 {
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002213 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002214 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002215 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002216 case 1: return_trace (u.format1.dispatch (c));
2217 default:return_trace (c->default_return_value ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002218 }
2219 }
2220
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002221 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002222 union {
2223 USHORT format; /* Format identifier */
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002224 ExtensionFormat1<T> format1;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002225 } u;
2226};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002227
2228
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002229/*
2230 * GSUB/GPOS Common
2231 */
2232
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002233struct GSUBGPOS
2234{
Behdad Esfahboda328d662009-08-04 20:27:05 -04002235 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
2236 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002237
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002238 inline unsigned int get_script_count (void) const
2239 { return (this+scriptList).len; }
2240 inline const Tag& get_script_tag (unsigned int i) const
2241 { return (this+scriptList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002242 inline unsigned int get_script_tags (unsigned int start_offset,
2243 unsigned int *script_count /* IN/OUT */,
2244 hb_tag_t *script_tags /* OUT */) const
2245 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002246 inline const Script& get_script (unsigned int i) const
2247 { return (this+scriptList)[i]; }
2248 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
2249 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002250
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002251 inline unsigned int get_feature_count (void) const
2252 { return (this+featureList).len; }
Jonathan Kewda132932014-04-27 14:05:24 +01002253 inline hb_tag_t get_feature_tag (unsigned int i) const
2254 { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : (this+featureList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002255 inline unsigned int get_feature_tags (unsigned int start_offset,
2256 unsigned int *feature_count /* IN/OUT */,
2257 hb_tag_t *feature_tags /* OUT */) const
2258 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002259 inline const Feature& get_feature (unsigned int i) const
2260 { return (this+featureList)[i]; }
2261 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
2262 { return (this+featureList).find_index (tag, index); }
2263
2264 inline unsigned int get_lookup_count (void) const
2265 { return (this+lookupList).len; }
2266 inline const Lookup& get_lookup (unsigned int i) const
2267 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002268
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002269 inline bool sanitize (hb_sanitize_context_t *c) const
2270 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002271 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002272 return_trace (version.sanitize (c) &&
2273 likely (version.major == 1) &&
2274 scriptList.sanitize (c, this) &&
2275 featureList.sanitize (c, this) &&
2276 lookupList.sanitize (c, this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -04002277 }
2278
Behdad Esfahbod212aba62009-05-24 00:50:27 -04002279 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -04002280 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbod76271002014-07-11 14:54:42 -04002281 * to 0x00010000u */
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002282 OffsetTo<ScriptList>
2283 scriptList; /* ScriptList table */
2284 OffsetTo<FeatureList>
2285 featureList; /* FeatureList table */
2286 OffsetTo<LookupList>
2287 lookupList; /* LookupList table */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002288 public:
2289 DEFINE_SIZE_STATIC (10);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002290};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04002291
Behdad Esfahbod6f20f722009-05-17 20:28:01 -04002292
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08002293} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04002294
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04002295
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -04002296#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */