blob: b7a0122a3b30f51877773c15103c6cce318ee00d [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 Esfahbodb2030502016-04-26 16:41:17 -0700322 if (unlikely (_hb_glyph_info_is_default_ignorable_and_not_fvs (&info) &&
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500323 (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 Esfahbodcf3de4d2016-09-10 00:22:24 -0700472 const VariationStore &var_store;
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100473 skipping_iterator_t iter_input, iter_context;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100474 unsigned int lookup_index;
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100475 unsigned int debug_depth;
476
477
478 hb_apply_context_t (unsigned int table_index_,
479 hb_font_t *font_,
480 hb_buffer_t *buffer_) :
481 table_index (table_index_),
482 font (font_), face (font->face), buffer (buffer_),
483 direction (buffer_->props.direction),
484 lookup_mask (1),
485 auto_zwj (true),
486 recurse_func (NULL),
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800487 nesting_level_left (HB_MAX_NESTING_LEVEL),
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100488 lookup_props (0),
489 gdef (*hb_ot_layout_from_face (face)->gdef),
490 has_glyph_classes (gdef.has_glyph_classes ()),
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700491 var_store (gdef.get_var_store ()),
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100492 iter_input (),
493 iter_context (),
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100494 lookup_index ((unsigned int) -1),
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100495 debug_depth (0) {}
496
497 inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
498 inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
499 inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +0100500 inline void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; }
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100501 inline void set_lookup_props (unsigned int lookup_props_)
502 {
503 lookup_props = lookup_props_;
504 iter_input.init (this, false);
505 iter_context.init (this, true);
506 }
Behdad Esfahbod2cecc382015-01-29 13:32:05 +0100507
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400508 inline bool
509 match_properties_mark (hb_codepoint_t glyph,
510 unsigned int glyph_props,
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700511 unsigned int match_props) const
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400512 {
513 /* If using mark filtering sets, the high short of
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700514 * match_props has the set index.
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400515 */
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700516 if (match_props & LookupFlag::UseMarkFilteringSet)
517 return gdef.mark_set_covers (match_props >> 16, glyph);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400518
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700519 /* The second byte of match_props has the meaning
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400520 * "ignore marks of attachment type different than
521 * the attachment type specified."
522 */
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700523 if (match_props & LookupFlag::MarkAttachmentType)
524 return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400525
526 return true;
527 }
528
529 inline bool
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400530 check_glyph_property (const hb_glyph_info_t *info,
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700531 unsigned int match_props) const
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400532 {
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400533 hb_codepoint_t glyph = info->codepoint;
534 unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);
535
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400536 /* Not covered, if, for example, glyph class is ligature and
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700537 * match_props includes LookupFlags::IgnoreLigatures
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400538 */
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700539 if (glyph_props & match_props & LookupFlag::IgnoreFlags)
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400540 return false;
541
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800542 if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
Behdad Esfahbodb931e0b2015-04-08 14:39:00 -0700543 return match_properties_mark (glyph, glyph_props, match_props);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400544
545 return true;
546 }
547
Behdad Esfahboda0161742013-10-18 00:06:30 +0200548 inline void _set_glyph_props (hb_codepoint_t glyph_index,
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100549 unsigned int class_guess = 0,
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400550 bool ligature = false,
551 bool component = false) const
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400552 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200553 unsigned int add_in = _hb_glyph_info_get_glyph_props (&buffer->cur()) &
554 HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
555 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
556 if (ligature)
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400557 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200558 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400559 /* In the only place that the MULTIPLIED bit is used, Uniscribe
560 * seems to only care about the "last" transformation between
561 * Ligature and Multiple substitions. Ie. if you ligate, expand,
562 * and ligate again, it forgives the multiplication and acts as
563 * if only ligation happened. As such, clear MULTIPLIED bit.
564 */
565 add_in &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
566 }
567 if (component)
568 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
Behdad Esfahbod2fca1422012-07-30 18:46:41 -0400569 if (likely (has_glyph_classes))
Behdad Esfahbod05ad6b52013-10-18 00:45:59 +0200570 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400571 else if (class_guess)
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200572 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400573 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500574
Behdad Esfahboda0161742013-10-18 00:06:30 +0200575 inline void replace_glyph (hb_codepoint_t glyph_index) const
Behdad Esfahbod3ec77d62012-06-08 21:44:06 -0400576 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200577 _set_glyph_props (glyph_index);
Behdad Esfahbod98370e82010-10-27 17:39:01 -0400578 buffer->replace_glyph (glyph_index);
579 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200580 inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400581 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200582 _set_glyph_props (glyph_index);
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400583 buffer->cur().codepoint = glyph_index;
584 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200585 inline void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
586 unsigned int class_guess) const
587 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200588 _set_glyph_props (glyph_index, class_guess, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200589 buffer->replace_glyph (glyph_index);
590 }
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400591 inline void output_glyph_for_component (hb_codepoint_t glyph_index,
592 unsigned int class_guess) const
Behdad Esfahboda0161742013-10-18 00:06:30 +0200593 {
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400594 _set_glyph_props (glyph_index, class_guess, false, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200595 buffer->output_glyph (glyph_index);
596 }
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400597};
598
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400599
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400600
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400601typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500602typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400603typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400604
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400605struct ContextClosureFuncs
606{
607 intersects_func_t intersects;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400608};
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500609struct ContextCollectGlyphsFuncs
610{
611 collect_glyphs_func_t collect;
612};
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400613struct ContextApplyFuncs
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400614{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400615 match_func_t match;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400616};
617
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500618
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400619static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400620{
621 return glyphs->has (value);
622}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400623static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400624{
625 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
626 return class_def.intersects_class (glyphs, value);
627}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400628static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400629{
630 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
631 return (data+coverage).intersects (glyphs);
632}
633
634static inline bool intersects_array (hb_closure_context_t *c,
635 unsigned int count,
636 const USHORT values[],
637 intersects_func_t intersects_func,
638 const void *intersects_data)
639{
640 for (unsigned int i = 0; i < count; i++)
641 if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
642 return false;
643 return true;
644}
645
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400646
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500647static inline void collect_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
648{
649 glyphs->add (value);
650}
651static inline void collect_class (hb_set_t *glyphs, const USHORT &value, const void *data)
652{
653 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
654 class_def.add_class (glyphs, value);
655}
656static inline void collect_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
657{
658 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
659 (data+coverage).add_coverage (glyphs);
660}
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500661static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500662 hb_set_t *glyphs,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500663 unsigned int count,
664 const USHORT values[],
665 collect_glyphs_func_t collect_func,
666 const void *collect_data)
667{
668 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500669 collect_func (glyphs, values[i], collect_data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500670}
671
672
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400673static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400674{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400675 return glyph_id == value;
676}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400677static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400678{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400679 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400680 return class_def.get_class (glyph_id) == value;
681}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400682static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400683{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -0400684 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400685 return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400686}
687
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400688static inline bool would_match_input (hb_would_apply_context_t *c,
689 unsigned int count, /* Including the first glyph (not matched) */
690 const USHORT input[], /* Array of input values--start with second glyph */
691 match_func_t match_func,
692 const void *match_data)
693{
694 if (count != c->len)
695 return false;
696
697 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400698 if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400699 return false;
700
701 return true;
702}
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400703static inline bool match_input (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400704 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400705 const USHORT input[], /* Array of input values--start with second glyph */
706 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400707 const void *match_data,
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200708 unsigned int *end_offset,
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800709 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH],
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400710 bool *p_is_mark_ligature = NULL,
711 unsigned int *p_total_component_count = NULL)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400712{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500713 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400714
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800715 if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return_trace (false);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200716
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200717 hb_buffer_t *buffer = c->buffer;
718
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100719 hb_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100720 skippy_iter.reset (buffer->idx, count - 1);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500721 skippy_iter.set_match_func (match_func, match_data, input);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400722
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400723 /*
724 * This is perhaps the trickiest part of OpenType... Remarks:
725 *
726 * - If all components of the ligature were marks, we call this a mark ligature.
727 *
728 * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
729 * it as a ligature glyph.
730 *
731 * - Ligatures cannot be formed across glyphs attached to different components
732 * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
733 * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
734 * However, it would be wrong to ligate that SHADDA,FATHA sequence.o
735 * There is an exception to this: If a ligature tries ligating with marks that
736 * belong to it itself, go ahead, assuming that the font designer knows what
737 * they are doing (otherwise it can break Indic stuff when a matra wants to
738 * ligate with a conjunct...)
739 */
740
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200741 bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400742
743 unsigned int total_component_count = 0;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200744 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400745
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200746 unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
747 unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400748
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200749 match_positions[0] = buffer->idx;
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500750 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400751 {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100752 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200753
754 match_positions[i] = skippy_iter.idx;
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400755
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200756 unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]);
757 unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400758
759 if (first_lig_id && first_lig_comp) {
760 /* If first component was attached to a previous ligature component,
761 * all subsequent components should be attached to the same ligature
762 * component, otherwise we shouldn't ligate them. */
763 if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100764 return_trace (false);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400765 } else {
766 /* If first component was NOT attached to a previous ligature component,
767 * all subsequent components should also NOT be attached to any ligature
768 * component, unless they are attached to the first component itself! */
769 if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100770 return_trace (false);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400771 }
772
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200773 is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200774 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400775 }
776
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200777 *end_offset = skippy_iter.idx - buffer->idx + 1;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400778
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400779 if (p_is_mark_ligature)
780 *p_is_mark_ligature = is_mark_ligature;
781
782 if (p_total_component_count)
783 *p_total_component_count = total_component_count;
784
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100785 return_trace (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400786}
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100787static inline bool ligate_input (hb_apply_context_t *c,
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200788 unsigned int count, /* Including the first glyph */
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800789 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200790 unsigned int match_length,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400791 hb_codepoint_t lig_glyph,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400792 bool is_mark_ligature,
793 unsigned int total_component_count)
794{
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200795 TRACE_APPLY (NULL);
796
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200797 hb_buffer_t *buffer = c->buffer;
798
799 buffer->merge_clusters (buffer->idx, buffer->idx + match_length);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500800
Behdad Esfahboda177d022012-08-28 23:18:22 -0400801 /*
802 * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
803 * the ligature to keep its old ligature id. This will allow it to attach to
804 * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
805 * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
806 * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature
807 * later, we don't want them to lose their ligature id/component, otherwise
808 * GPOS will fail to correctly position the mark ligature on top of the
809 * LAM,LAM,HEH ligature. See:
810 * https://bugzilla.gnome.org/show_bug.cgi?id=676343
811 *
812 * - If a ligature is formed of components that some of which are also ligatures
813 * themselves, and those ligature components had marks attached to *their*
814 * components, we have to attach the marks to the new ligature component
815 * positions! Now *that*'s tricky! And these marks may be following the
816 * last component of the whole sequence, so we should loop forward looking
817 * for them and update them.
818 *
819 * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
820 * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
821 * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature
822 * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to
823 * the new ligature with a component value of 2.
824 *
825 * This in fact happened to a font... See:
826 * https://bugzilla.gnome.org/show_bug.cgi?id=437633
827 */
828
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800829 unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200830 unsigned int lig_id = is_mark_ligature ? 0 : _hb_allocate_lig_id (buffer);
831 unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
832 unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400833 unsigned int components_so_far = last_num_components;
834
835 if (!is_mark_ligature)
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400836 {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200837 _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200838 if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100839 {
Behdad Esfahbod82596692015-11-02 17:44:05 -0800840 _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100841 }
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400842 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200843 c->replace_glyph_with_ligature (lig_glyph, klass);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400844
845 for (unsigned int i = 1; i < count; i++)
846 {
Behdad Esfahbodabadc172015-11-18 17:52:08 -0800847 while (buffer->idx < match_positions[i] && !buffer->in_error)
Behdad Esfahboda177d022012-08-28 23:18:22 -0400848 {
849 if (!is_mark_ligature) {
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000850 unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
851 if (this_comp == 0)
Behdad Esfahbod100fbea2015-12-17 15:23:09 +0000852 this_comp = last_num_components;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400853 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000854 MIN (this_comp, last_num_components);
855 _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400856 }
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200857 buffer->next_glyph ();
Behdad Esfahboda177d022012-08-28 23:18:22 -0400858 }
859
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200860 last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
861 last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400862 components_so_far += last_num_components;
863
864 /* Skip the base glyph */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200865 buffer->idx++;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400866 }
867
868 if (!is_mark_ligature && last_lig_id) {
869 /* Re-adjust components for any marks following. */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200870 for (unsigned int i = buffer->idx; i < buffer->len; i++) {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200871 if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000872 unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->info[i]);
873 if (!this_comp)
874 break;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400875 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod2f02fc72015-12-17 15:21:14 +0000876 MIN (this_comp, last_num_components);
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200877 _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400878 } else
879 break;
880 }
881 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100882 return_trace (true);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400883}
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400884
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400885static inline bool match_backtrack (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400886 unsigned int count,
887 const USHORT backtrack[],
888 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400889 const void *match_data)
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400890{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500891 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400892
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100893 hb_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100894 skippy_iter.reset (c->buffer->backtrack_len (), count);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500895 skippy_iter.set_match_func (match_func, match_data, backtrack);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400896
Behdad Esfahbod4d3aeb82012-01-16 16:43:26 -0500897 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500898 if (!skippy_iter.prev ())
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100899 return_trace (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400900
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100901 return_trace (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400902}
903
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400904static inline bool match_lookahead (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400905 unsigned int count,
906 const USHORT lookahead[],
907 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400908 const void *match_data,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400909 unsigned int offset)
910{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500911 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400912
Behdad Esfahbod365576d2015-01-29 13:59:42 +0100913 hb_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100914 skippy_iter.reset (c->buffer->idx + offset - 1, count);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500915 skippy_iter.set_match_func (match_func, match_data, lookahead);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400916
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500917 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500918 if (!skippy_iter.next ())
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100919 return_trace (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400920
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100921 return_trace (true);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400922}
923
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400924
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400925
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400926struct LookupRecord
927{
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300928 inline bool sanitize (hb_sanitize_context_t *c) const
929 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500930 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100931 return_trace (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400932 }
933
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400934 USHORT sequenceIndex; /* Index into current glyph
935 * sequence--first glyph = 0 */
936 USHORT lookupListIndex; /* Lookup to apply to that
937 * position--zero--based */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400938 public:
939 DEFINE_SIZE_STATIC (4);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400940};
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400941
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400942
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500943template <typename context_t>
944static inline void recurse_lookups (context_t *c,
945 unsigned int lookupCount,
946 const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400947{
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400948 for (unsigned int i = 0; i < lookupCount; i++)
Behdad Esfahbod86522e42013-07-22 19:07:53 -0400949 c->recurse (lookupRecord[i].lookupListIndex);
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400950}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400951
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400952static inline bool apply_lookup (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400953 unsigned int count, /* Including the first glyph */
Behdad Esfahbod5ba45042015-11-02 15:43:08 -0800954 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400955 unsigned int lookupCount,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200956 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
957 unsigned int match_length)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400958{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500959 TRACE_APPLY (NULL);
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500960
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200961 hb_buffer_t *buffer = c->buffer;
962 unsigned int end;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400963
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200964 /* All positions are distance from beginning of *output* buffer.
965 * Adjust. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400966 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200967 unsigned int bl = buffer->backtrack_len ();
968 end = bl + match_length;
Behdad Esfahbod8751de52013-07-18 16:29:50 -0400969
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200970 int delta = bl - buffer->idx;
971 /* Convert positions to new indexing. */
972 for (unsigned int j = 0; j < count; j++)
973 match_positions[j] += delta;
Behdad Esfahbod8820bb22013-02-14 07:41:03 -0500974 }
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400975
Behdad Esfahbodb87e36f2016-02-19 14:52:31 +0700976 for (unsigned int i = 0; i < lookupCount && !buffer->in_error; i++)
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200977 {
978 unsigned int idx = lookupRecord[i].sequenceIndex;
979 if (idx >= count)
980 continue;
981
Behdad Esfahbod9cc1ed42015-11-19 12:39:09 -0800982 /* Don't recurse to ourself at same position.
983 * Note that this test is too naive, it doesn't catch longer loops. */
984 if (idx == 0 && lookupRecord[i].lookupListIndex == c->lookup_index)
985 continue;
986
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200987 buffer->move_to (match_positions[idx]);
988
989 unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
990 if (!c->recurse (lookupRecord[i].lookupListIndex))
991 continue;
992
993 unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
994 int delta = new_len - orig_len;
995
996 if (!delta)
997 continue;
998
999 /* Recursed lookup changed buffer len. Adjust. */
1000
Behdad Esfahbod359dead2016-05-06 16:19:19 +01001001 end = int (end) + delta;
1002 if (end <= match_positions[idx])
1003 {
Behdad Esfahbod4b4a1b92016-12-21 23:10:43 -06001004 /* End might end up being smaller than match_positions[idx] if the recursed
1005 * lookup ended up removing many items, more than we have had matched.
1006 * Just never rewind end back and get out of here.
1007 * https://bugs.chromium.org/p/chromium/issues/detail?id=659496 */
1008 end = match_positions[idx];
Behdad Esfahbod359dead2016-05-06 16:19:19 +01001009 /* There can't be any further changes. */
Behdad Esfahbod359dead2016-05-06 16:19:19 +01001010 break;
1011 }
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001012
1013 unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */
1014
1015 if (delta > 0)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001016 {
Behdad Esfahbod5ba45042015-11-02 15:43:08 -08001017 if (unlikely (delta + count > HB_MAX_CONTEXT_LENGTH))
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001018 break;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001019 }
1020 else
1021 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001022 /* NOTE: delta is negative. */
1023 delta = MAX (delta, (int) next - (int) count);
1024 next -= delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001025 }
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001026
1027 /* Shift! */
1028 memmove (match_positions + next + delta, match_positions + next,
1029 (count - next) * sizeof (match_positions[0]));
1030 next += delta;
1031 count += delta;
1032
1033 /* Fill in new entries. */
1034 for (unsigned int j = idx + 1; j < next; j++)
1035 match_positions[j] = match_positions[j - 1] + 1;
1036
1037 /* And fixup the rest. */
1038 for (; next < count; next++)
1039 match_positions[next] += delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001040 }
1041
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001042 buffer->move_to (end);
1043
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001044 return_trace (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001045}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001046
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001047
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001048
1049/* Contextual lookups */
1050
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001051struct ContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001052{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001053 ContextClosureFuncs funcs;
1054 const void *intersects_data;
1055};
1056
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001057struct ContextCollectGlyphsLookupContext
1058{
1059 ContextCollectGlyphsFuncs funcs;
1060 const void *collect_data;
1061};
1062
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001063struct ContextApplyLookupContext
1064{
1065 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001066 const void *match_data;
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001067};
1068
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001069static inline void context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001070 unsigned int inputCount, /* Including the first glyph (not matched) */
1071 const USHORT input[], /* Array of input values--start with second glyph */
1072 unsigned int lookupCount,
1073 const LookupRecord lookupRecord[],
1074 ContextClosureLookupContext &lookup_context)
1075{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001076 if (intersects_array (c,
1077 inputCount ? inputCount - 1 : 0, input,
1078 lookup_context.funcs.intersects, lookup_context.intersects_data))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001079 recurse_lookups (c,
1080 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001081}
1082
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001083static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1084 unsigned int inputCount, /* Including the first glyph (not matched) */
1085 const USHORT input[], /* Array of input values--start with second glyph */
1086 unsigned int lookupCount,
1087 const LookupRecord lookupRecord[],
1088 ContextCollectGlyphsLookupContext &lookup_context)
1089{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001090 collect_array (c, c->input,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001091 inputCount ? inputCount - 1 : 0, input,
1092 lookup_context.funcs.collect, lookup_context.collect_data);
1093 recurse_lookups (c,
1094 lookupCount, lookupRecord);
1095}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001096
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001097static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
1098 unsigned int inputCount, /* Including the first glyph (not matched) */
1099 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001100 unsigned int lookupCount HB_UNUSED,
1101 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001102 ContextApplyLookupContext &lookup_context)
1103{
1104 return would_match_input (c,
1105 inputCount, input,
1106 lookup_context.funcs.match, lookup_context.match_data);
1107}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001108static inline bool context_apply_lookup (hb_apply_context_t *c,
1109 unsigned int inputCount, /* Including the first glyph (not matched) */
1110 const USHORT input[], /* Array of input values--start with second glyph */
1111 unsigned int lookupCount,
1112 const LookupRecord lookupRecord[],
1113 ContextApplyLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001114{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001115 unsigned int match_length = 0;
Behdad Esfahbod5ba45042015-11-02 15:43:08 -08001116 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001117 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001118 inputCount, input,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001119 lookup_context.funcs.match, lookup_context.match_data,
1120 &match_length, match_positions)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001121 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001122 inputCount, match_positions,
1123 lookupCount, lookupRecord,
1124 match_length);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001125}
1126
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001127struct Rule
1128{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001129 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001130 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001131 TRACE_CLOSURE (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001132 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001133 context_closure_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001134 inputCount, inputZ,
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001135 lookupCount, lookupRecord,
1136 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001137 }
1138
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001139 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1140 {
1141 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001142 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001143 context_collect_glyphs_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001144 inputCount, inputZ,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001145 lookupCount, lookupRecord,
1146 lookup_context);
1147 }
1148
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001149 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1150 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001151 TRACE_WOULD_APPLY (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001152 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001153 return_trace (context_would_apply_lookup (c, inputCount, inputZ, lookupCount, lookupRecord, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001154 }
1155
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001156 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001157 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001158 TRACE_APPLY (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001159 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001160 return_trace (context_apply_lookup (c, inputCount, inputZ, lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001161 }
1162
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001163 public:
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001164 inline bool sanitize (hb_sanitize_context_t *c) const
1165 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001166 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001167 return inputCount.sanitize (c)
1168 && lookupCount.sanitize (c)
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001169 && c->check_range (inputZ,
1170 inputZ[0].static_size * inputCount
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001171 + lookupRecordX[0].static_size * lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001172 }
1173
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001174 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001175 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001176 * glyph sequence--includes the first
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001177 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001178 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001179 USHORT inputZ[VAR]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001180 * second glyph */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001181 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001182 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001183 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001184 DEFINE_SIZE_ARRAY2 (4, inputZ, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001185};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001186
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001187struct RuleSet
1188{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001189 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001190 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001191 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001192 unsigned int num_rules = rule.len;
1193 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001194 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001195 }
1196
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001197 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1198 {
1199 TRACE_COLLECT_GLYPHS (this);
1200 unsigned int num_rules = rule.len;
1201 for (unsigned int i = 0; i < num_rules; i++)
1202 (this+rule[i]).collect_glyphs (c, lookup_context);
1203 }
1204
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001205 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1206 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001207 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001208 unsigned int num_rules = rule.len;
1209 for (unsigned int i = 0; i < num_rules; i++)
1210 {
1211 if ((this+rule[i]).would_apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001212 return_trace (true);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001213 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001214 return_trace (false);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001215 }
1216
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001217 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001218 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001219 TRACE_APPLY (this);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001220 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001221 for (unsigned int i = 0; i < num_rules; i++)
1222 {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001223 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001224 return_trace (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001225 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001226 return_trace (false);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001227 }
1228
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001229 inline bool sanitize (hb_sanitize_context_t *c) const
1230 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001231 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001232 return_trace (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001233 }
1234
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001235 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001236 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001237 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001238 * ordered by preference */
Behdad Esfahboded074222010-05-10 18:08:46 -04001239 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001240 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001241};
1242
1243
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001244struct ContextFormat1
1245{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001246 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001247 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001248 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001249
1250 const Coverage &cov = (this+coverage);
1251
1252 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001253 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001254 NULL
1255 };
1256
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001257 unsigned int count = ruleSet.len;
1258 for (unsigned int i = 0; i < count; i++)
1259 if (cov.intersects_coverage (c->glyphs, i)) {
1260 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001261 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001262 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001263 }
1264
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001265 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1266 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001267 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001268 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001269
1270 struct ContextCollectGlyphsLookupContext lookup_context = {
1271 {collect_glyph},
1272 NULL
1273 };
1274
1275 unsigned int count = ruleSet.len;
1276 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001277 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001278 }
1279
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001280 inline bool would_apply (hb_would_apply_context_t *c) const
1281 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001282 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001283
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001284 const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001285 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001286 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001287 NULL
1288 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001289 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001290 }
1291
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001292 inline const Coverage &get_coverage (void) const
1293 {
1294 return this+coverage;
1295 }
1296
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001297 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001298 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001299 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001300 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -04001301 if (likely (index == NOT_COVERED))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001302 return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001303
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001304 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001305 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001306 {match_glyph},
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001307 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001308 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001309 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001310 }
1311
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001312 inline bool sanitize (hb_sanitize_context_t *c) const
1313 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001314 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001315 return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001316 }
1317
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001318 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001319 USHORT format; /* Format identifier--format = 1 */
1320 OffsetTo<Coverage>
1321 coverage; /* Offset to Coverage table--from
1322 * beginning of table */
1323 OffsetArrayOf<RuleSet>
1324 ruleSet; /* Array of RuleSet tables
1325 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001326 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001327 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001328};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001329
1330
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001331struct ContextFormat2
1332{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001333 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001334 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001335 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001336 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001337 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001338
1339 const ClassDef &class_def = this+classDef;
1340
1341 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001342 {intersects_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001343 &class_def
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001344 };
1345
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001346 unsigned int count = ruleSet.len;
1347 for (unsigned int i = 0; i < count; i++)
1348 if (class_def.intersects_class (c->glyphs, i)) {
1349 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001350 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001351 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001352 }
1353
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001354 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1355 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001356 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001357 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001358
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001359 const ClassDef &class_def = this+classDef;
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001360 struct ContextCollectGlyphsLookupContext lookup_context = {
1361 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001362 &class_def
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001363 };
1364
1365 unsigned int count = ruleSet.len;
1366 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001367 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001368 }
1369
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001370 inline bool would_apply (hb_would_apply_context_t *c) const
1371 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001372 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001373
1374 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001375 unsigned int index = class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001376 const RuleSet &rule_set = this+ruleSet[index];
1377 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001378 {match_class},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001379 &class_def
1380 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001381 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001382 }
1383
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001384 inline const Coverage &get_coverage (void) const
1385 {
1386 return this+coverage;
1387 }
1388
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001389 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001390 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001391 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001392 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001393 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001394
1395 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001396 index = class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001397 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001398 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001399 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001400 &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001401 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001402 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001403 }
1404
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001405 inline bool sanitize (hb_sanitize_context_t *c) const
1406 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001407 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001408 return_trace (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001409 }
1410
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001411 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001412 USHORT format; /* Format identifier--format = 2 */
1413 OffsetTo<Coverage>
1414 coverage; /* Offset to Coverage table--from
1415 * beginning of table */
1416 OffsetTo<ClassDef>
1417 classDef; /* Offset to glyph ClassDef table--from
1418 * beginning of table */
1419 OffsetArrayOf<RuleSet>
1420 ruleSet; /* Array of RuleSet tables
1421 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001422 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001423 DEFINE_SIZE_ARRAY (8, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001424};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001425
1426
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001427struct ContextFormat3
1428{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001429 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001430 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001431 TRACE_CLOSURE (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001432 if (!(this+coverageZ[0]).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001433 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001434
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001435 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001436 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001437 {intersects_coverage},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001438 this
1439 };
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001440 context_closure_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001441 glyphCount, (const USHORT *) (coverageZ + 1),
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001442 lookupCount, lookupRecord,
1443 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001444 }
1445
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001446 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1447 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001448 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001449 (this+coverageZ[0]).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001450
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001451 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001452 struct ContextCollectGlyphsLookupContext lookup_context = {
1453 {collect_coverage},
Behdad Esfahbode75943d2012-11-30 08:38:24 +02001454 this
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001455 };
1456
1457 context_collect_glyphs_lookup (c,
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001458 glyphCount, (const USHORT *) (coverageZ + 1),
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001459 lookupCount, lookupRecord,
1460 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001461 }
1462
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001463 inline bool would_apply (hb_would_apply_context_t *c) const
1464 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001465 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001466
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001467 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001468 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001469 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001470 this
1471 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001472 return_trace (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverageZ + 1), lookupCount, lookupRecord, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001473 }
1474
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001475 inline const Coverage &get_coverage (void) const
1476 {
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001477 return this+coverageZ[0];
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001478 }
1479
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001480 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001481 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001482 TRACE_APPLY (this);
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001483 unsigned int index = (this+coverageZ[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001484 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001485
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001486 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001487 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001488 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001489 this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001490 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001491 return_trace (context_apply_lookup (c, glyphCount, (const USHORT *) (coverageZ + 1), lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001492 }
1493
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001494 inline bool sanitize (hb_sanitize_context_t *c) const
1495 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001496 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001497 if (!c->check_struct (this)) return_trace (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001498 unsigned int count = glyphCount;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001499 if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */
1500 if (!c->check_array (coverageZ, coverageZ[0].static_size, count)) return_trace (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001501 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001502 if (!coverageZ[i].sanitize (c, this)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001503 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * count);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001504 return_trace (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001505 }
1506
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001507 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001508 USHORT format; /* Format identifier--format = 3 */
1509 USHORT glyphCount; /* Number of glyphs in the input glyph
1510 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001511 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001512 OffsetTo<Coverage>
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001513 coverageZ[VAR]; /* Array of offsets to Coverage
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001514 * table in glyph sequence order */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001515 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001516 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001517 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -08001518 DEFINE_SIZE_ARRAY2 (6, coverageZ, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001519};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001520
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001521struct Context
1522{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001523 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001524 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001525 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001526 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001527 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001528 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001529 case 1: return_trace (c->dispatch (u.format1));
1530 case 2: return_trace (c->dispatch (u.format2));
1531 case 3: return_trace (c->dispatch (u.format3));
1532 default:return_trace (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001533 }
1534 }
1535
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001536 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001537 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -04001538 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001539 ContextFormat1 format1;
1540 ContextFormat2 format2;
1541 ContextFormat3 format3;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001542 } u;
1543};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001544
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001545
1546/* Chaining Contextual lookups */
1547
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001548struct ChainContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001549{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001550 ContextClosureFuncs funcs;
1551 const void *intersects_data[3];
1552};
1553
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001554struct ChainContextCollectGlyphsLookupContext
1555{
1556 ContextCollectGlyphsFuncs funcs;
1557 const void *collect_data[3];
1558};
1559
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001560struct ChainContextApplyLookupContext
1561{
1562 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001563 const void *match_data[3];
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001564};
1565
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001566static inline void chain_context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001567 unsigned int backtrackCount,
1568 const USHORT backtrack[],
1569 unsigned int inputCount, /* Including the first glyph (not matched) */
1570 const USHORT input[], /* Array of input values--start with second glyph */
1571 unsigned int lookaheadCount,
1572 const USHORT lookahead[],
1573 unsigned int lookupCount,
1574 const LookupRecord lookupRecord[],
1575 ChainContextClosureLookupContext &lookup_context)
1576{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001577 if (intersects_array (c,
1578 backtrackCount, backtrack,
1579 lookup_context.funcs.intersects, lookup_context.intersects_data[0])
1580 && intersects_array (c,
1581 inputCount ? inputCount - 1 : 0, input,
1582 lookup_context.funcs.intersects, lookup_context.intersects_data[1])
Behdad Esfahbod74439d02013-07-22 19:02:29 -04001583 && intersects_array (c,
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001584 lookaheadCount, lookahead,
1585 lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001586 recurse_lookups (c,
1587 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001588}
1589
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001590static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1591 unsigned int backtrackCount,
1592 const USHORT backtrack[],
1593 unsigned int inputCount, /* Including the first glyph (not matched) */
1594 const USHORT input[], /* Array of input values--start with second glyph */
1595 unsigned int lookaheadCount,
1596 const USHORT lookahead[],
1597 unsigned int lookupCount,
1598 const LookupRecord lookupRecord[],
1599 ChainContextCollectGlyphsLookupContext &lookup_context)
1600{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001601 collect_array (c, c->before,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001602 backtrackCount, backtrack,
1603 lookup_context.funcs.collect, lookup_context.collect_data[0]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001604 collect_array (c, c->input,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001605 inputCount ? inputCount - 1 : 0, input,
1606 lookup_context.funcs.collect, lookup_context.collect_data[1]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001607 collect_array (c, c->after,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001608 lookaheadCount, lookahead,
1609 lookup_context.funcs.collect, lookup_context.collect_data[2]);
1610 recurse_lookups (c,
1611 lookupCount, lookupRecord);
1612}
1613
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001614static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
1615 unsigned int backtrackCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001616 const USHORT backtrack[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001617 unsigned int inputCount, /* Including the first glyph (not matched) */
1618 const USHORT input[], /* Array of input values--start with second glyph */
1619 unsigned int lookaheadCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001620 const USHORT lookahead[] HB_UNUSED,
1621 unsigned int lookupCount HB_UNUSED,
1622 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001623 ChainContextApplyLookupContext &lookup_context)
1624{
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -04001625 return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
Behdad Esfahbod1f2bb172012-08-23 16:10:37 -04001626 && would_match_input (c,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001627 inputCount, input,
1628 lookup_context.funcs.match, lookup_context.match_data[1]);
1629}
1630
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001631static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
1632 unsigned int backtrackCount,
1633 const USHORT backtrack[],
1634 unsigned int inputCount, /* Including the first glyph (not matched) */
1635 const USHORT input[], /* Array of input values--start with second glyph */
1636 unsigned int lookaheadCount,
1637 const USHORT lookahead[],
1638 unsigned int lookupCount,
1639 const LookupRecord lookupRecord[],
1640 ChainContextApplyLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001641{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001642 unsigned int match_length = 0;
Behdad Esfahbod5ba45042015-11-02 15:43:08 -08001643 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001644 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001645 inputCount, input,
1646 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001647 &match_length, match_positions)
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001648 && match_backtrack (c,
1649 backtrackCount, backtrack,
1650 lookup_context.funcs.match, lookup_context.match_data[0])
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001651 && match_lookahead (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001652 lookaheadCount, lookahead,
1653 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001654 match_length)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001655 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001656 inputCount, match_positions,
1657 lookupCount, lookupRecord,
1658 match_length);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001659}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001660
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001661struct ChainRule
1662{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001663 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001664 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001665 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001666 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1667 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1668 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001669 chain_context_closure_lookup (c,
1670 backtrack.len, backtrack.array,
1671 input.len, input.array,
1672 lookahead.len, lookahead.array,
1673 lookup.len, lookup.array,
1674 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001675 }
1676
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001677 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1678 {
1679 TRACE_COLLECT_GLYPHS (this);
1680 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1681 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1682 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1683 chain_context_collect_glyphs_lookup (c,
1684 backtrack.len, backtrack.array,
1685 input.len, input.array,
1686 lookahead.len, lookahead.array,
1687 lookup.len, lookup.array,
1688 lookup_context);
1689 }
1690
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001691 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1692 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001693 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001694 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1695 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1696 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001697 return_trace (chain_context_would_apply_lookup (c,
1698 backtrack.len, backtrack.array,
1699 input.len, input.array,
1700 lookahead.len, lookahead.array, lookup.len,
1701 lookup.array, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001702 }
1703
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001704 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001705 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001706 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001707 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1708 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1709 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001710 return_trace (chain_context_apply_lookup (c,
1711 backtrack.len, backtrack.array,
1712 input.len, input.array,
1713 lookahead.len, lookahead.array, lookup.len,
1714 lookup.array, lookup_context));
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001715 }
1716
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001717 inline bool sanitize (hb_sanitize_context_t *c) const
1718 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001719 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001720 if (!backtrack.sanitize (c)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001721 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001722 if (!input.sanitize (c)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001723 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001724 if (!lookahead.sanitize (c)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001725 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001726 return_trace (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001727 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001728
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001729 protected:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001730 ArrayOf<USHORT>
1731 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001732 * (to be matched before the input
1733 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -04001734 HeadlessArrayOf<USHORT>
1735 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001736 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001737 ArrayOf<USHORT>
1738 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001739 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001740 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001741 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001742 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001743 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04001744 DEFINE_SIZE_MIN (8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001745};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001746
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001747struct ChainRuleSet
1748{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001749 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001750 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001751 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001752 unsigned int num_rules = rule.len;
1753 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001754 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001755 }
1756
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001757 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1758 {
1759 TRACE_COLLECT_GLYPHS (this);
1760 unsigned int num_rules = rule.len;
1761 for (unsigned int i = 0; i < num_rules; i++)
1762 (this+rule[i]).collect_glyphs (c, lookup_context);
1763 }
1764
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001765 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1766 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001767 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001768 unsigned int num_rules = rule.len;
1769 for (unsigned int i = 0; i < num_rules; i++)
1770 if ((this+rule[i]).would_apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001771 return_trace (true);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001772
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001773 return_trace (false);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001774 }
1775
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001776 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001777 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001778 TRACE_APPLY (this);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001779 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001780 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001781 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001782 return_trace (true);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001783
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001784 return_trace (false);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001785 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001786
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001787 inline bool sanitize (hb_sanitize_context_t *c) const
1788 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001789 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001790 return_trace (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001791 }
1792
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001793 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001794 OffsetArrayOf<ChainRule>
1795 rule; /* Array of ChainRule tables
1796 * ordered by preference */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001797 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001798 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001799};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001800
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001801struct ChainContextFormat1
1802{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001803 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001804 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001805 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001806 const Coverage &cov = (this+coverage);
1807
1808 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001809 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001810 {NULL, NULL, NULL}
1811 };
1812
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001813 unsigned int count = ruleSet.len;
1814 for (unsigned int i = 0; i < count; i++)
1815 if (cov.intersects_coverage (c->glyphs, i)) {
1816 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001817 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001818 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001819 }
1820
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001821 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1822 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001823 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001824 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001825
1826 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1827 {collect_glyph},
1828 {NULL, NULL, NULL}
1829 };
1830
1831 unsigned int count = ruleSet.len;
1832 for (unsigned int i = 0; i < count; i++)
1833 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001834 }
1835
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001836 inline bool would_apply (hb_would_apply_context_t *c) const
1837 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001838 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001839
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001840 const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001841 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001842 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001843 {NULL, NULL, NULL}
1844 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001845 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001846 }
1847
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001848 inline const Coverage &get_coverage (void) const
1849 {
1850 return this+coverage;
1851 }
1852
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001853 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001854 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001855 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001856 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001857 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001858
1859 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001860 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001861 {match_glyph},
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001862 {NULL, NULL, NULL}
1863 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001864 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001865 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001866
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001867 inline bool sanitize (hb_sanitize_context_t *c) const
1868 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001869 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001870 return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001871 }
1872
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001873 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001874 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001875 OffsetTo<Coverage>
1876 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001877 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001878 OffsetArrayOf<ChainRuleSet>
1879 ruleSet; /* Array of ChainRuleSet tables
1880 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001881 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001882 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001883};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001884
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001885struct ChainContextFormat2
1886{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001887 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001888 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001889 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001890 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001891 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001892
1893 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1894 const ClassDef &input_class_def = this+inputClassDef;
1895 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1896
1897 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001898 {intersects_class},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001899 {&backtrack_class_def,
1900 &input_class_def,
1901 &lookahead_class_def}
1902 };
1903
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001904 unsigned int count = ruleSet.len;
1905 for (unsigned int i = 0; i < count; i++)
1906 if (input_class_def.intersects_class (c->glyphs, i)) {
1907 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001908 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001909 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001910 }
1911
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001912 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1913 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001914 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001915 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001916
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001917 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1918 const ClassDef &input_class_def = this+inputClassDef;
1919 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1920
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001921 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1922 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001923 {&backtrack_class_def,
1924 &input_class_def,
1925 &lookahead_class_def}
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001926 };
1927
1928 unsigned int count = ruleSet.len;
1929 for (unsigned int i = 0; i < count; i++)
1930 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001931 }
1932
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001933 inline bool would_apply (hb_would_apply_context_t *c) const
1934 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001935 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001936
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001937 const ClassDef &backtrack_class_def = this+backtrackClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001938 const ClassDef &input_class_def = this+inputClassDef;
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001939 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001940
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001941 unsigned int index = input_class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001942 const ChainRuleSet &rule_set = this+ruleSet[index];
1943 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001944 {match_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001945 {&backtrack_class_def,
1946 &input_class_def,
1947 &lookahead_class_def}
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001948 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001949 return_trace (rule_set.would_apply (c, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001950 }
1951
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001952 inline const Coverage &get_coverage (void) const
1953 {
1954 return this+coverage;
1955 }
1956
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001957 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001958 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001959 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001960 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001961 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001962
1963 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1964 const ClassDef &input_class_def = this+inputClassDef;
1965 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1966
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001967 index = input_class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001968 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001969 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001970 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001971 {&backtrack_class_def,
1972 &input_class_def,
1973 &lookahead_class_def}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001974 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001975 return_trace (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001976 }
1977
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001978 inline bool sanitize (hb_sanitize_context_t *c) const
1979 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001980 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001981 return_trace (coverage.sanitize (c, this) &&
1982 backtrackClassDef.sanitize (c, this) &&
1983 inputClassDef.sanitize (c, this) &&
1984 lookaheadClassDef.sanitize (c, this) &&
1985 ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001986 }
1987
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001988 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001989 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001990 OffsetTo<Coverage>
1991 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001992 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001993 OffsetTo<ClassDef>
1994 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001995 * containing backtrack sequence
1996 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001997 OffsetTo<ClassDef>
1998 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001999 * table containing input sequence
2000 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002001 OffsetTo<ClassDef>
2002 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002003 * containing lookahead sequence
2004 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002005 OffsetArrayOf<ChainRuleSet>
2006 ruleSet; /* Array of ChainRuleSet tables
2007 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002008 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04002009 DEFINE_SIZE_ARRAY (12, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002010};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002011
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002012struct ChainContextFormat3
2013{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002014 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002015 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002016 TRACE_CLOSURE (this);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002017 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2018
2019 if (!(this+input[0]).intersects (c->glyphs))
2020 return;
2021
2022 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2023 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2024 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002025 {intersects_coverage},
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002026 {this, this, this}
2027 };
2028 chain_context_closure_lookup (c,
2029 backtrack.len, (const USHORT *) backtrack.array,
2030 input.len, (const USHORT *) input.array + 1,
2031 lookahead.len, (const USHORT *) lookahead.array,
2032 lookup.len, lookup.array,
2033 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002034 }
2035
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002036 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
2037 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002038 TRACE_COLLECT_GLYPHS (this);
2039 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2040
Behdad Esfahbod83035932012-12-04 17:08:41 -05002041 (this+input[0]).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002042
2043 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2044 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2045 struct ChainContextCollectGlyphsLookupContext lookup_context = {
2046 {collect_coverage},
2047 {this, this, this}
2048 };
2049 chain_context_collect_glyphs_lookup (c,
2050 backtrack.len, (const USHORT *) backtrack.array,
2051 input.len, (const USHORT *) input.array + 1,
2052 lookahead.len, (const USHORT *) lookahead.array,
2053 lookup.len, lookup.array,
2054 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002055 }
2056
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002057 inline bool would_apply (hb_would_apply_context_t *c) const
2058 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002059 TRACE_WOULD_APPLY (this);
Behdad Esfahbode6f74792012-07-28 18:34:58 -04002060
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002061 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002062 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2063 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2064 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002065 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002066 {this, this, this}
2067 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002068 return_trace (chain_context_would_apply_lookup (c,
2069 backtrack.len, (const USHORT *) backtrack.array,
2070 input.len, (const USHORT *) input.array + 1,
2071 lookahead.len, (const USHORT *) lookahead.array,
2072 lookup.len, lookup.array, lookup_context));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002073 }
2074
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002075 inline const Coverage &get_coverage (void) const
2076 {
2077 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2078 return this+input[0];
2079 }
2080
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002081 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002082 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002083 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002084 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002085
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05002086 unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002087 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002088
Behdad Esfahbode961c862010-04-21 15:56:11 -04002089 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2090 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002091 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002092 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04002093 {this, this, this}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002094 };
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002095 return_trace (chain_context_apply_lookup (c,
2096 backtrack.len, (const USHORT *) backtrack.array,
2097 input.len, (const USHORT *) input.array + 1,
2098 lookahead.len, (const USHORT *) lookahead.array,
2099 lookup.len, lookup.array, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002100 }
2101
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002102 inline bool sanitize (hb_sanitize_context_t *c) const
2103 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002104 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002105 if (!backtrack.sanitize (c, this)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002106 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002107 if (!input.sanitize (c, this)) return_trace (false);
2108 if (!input.len) return_trace (false); /* To be consistent with Context. */
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002109 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002110 if (!lookahead.sanitize (c, this)) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002111 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002112 return_trace (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002113 }
2114
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002115 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002116 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002117 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002118 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002119 * in backtracking sequence, in glyph
2120 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002121 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002122 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002123 * tables in input sequence, in glyph
2124 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002125 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002126 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002127 * in lookahead sequence, in glyph
2128 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002129 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002130 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002131 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002132 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04002133 DEFINE_SIZE_MIN (10);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002134};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002135
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002136struct ChainContext
2137{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002138 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002139 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002140 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002141 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002142 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002143 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002144 case 1: return_trace (c->dispatch (u.format1));
2145 case 2: return_trace (c->dispatch (u.format2));
2146 case 3: return_trace (c->dispatch (u.format3));
2147 default:return_trace (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002148 }
2149 }
2150
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002151 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002152 union {
2153 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002154 ChainContextFormat1 format1;
2155 ChainContextFormat2 format2;
2156 ChainContextFormat3 format3;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002157 } u;
2158};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002159
2160
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002161template <typename T>
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002162struct ExtensionFormat1
2163{
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002164 inline unsigned int get_type (void) const { return extensionLookupType; }
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002165
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002166 template <typename X>
2167 inline const X& get_subtable (void) const
2168 {
2169 unsigned int offset = extensionOffset;
2170 if (unlikely (!offset)) return Null(typename T::LookupSubTable);
2171 return StructAtOffset<typename T::LookupSubTable> (this, offset);
2172 }
2173
2174 template <typename context_t>
2175 inline typename context_t::return_t dispatch (context_t *c) const
2176 {
2177 TRACE_DISPATCH (this, format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002178 if (unlikely (!c->may_dispatch (this, this))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002179 return_trace (get_subtable<typename T::LookupSubTable> ().dispatch (c, get_type ()));
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002180 }
2181
2182 /* This is called from may_dispatch() above with hb_sanitize_context_t. */
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002183 inline bool sanitize (hb_sanitize_context_t *c) const
2184 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002185 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002186 return_trace (c->check_struct (this) && extensionOffset != 0);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002187 }
2188
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002189 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002190 USHORT format; /* Format identifier. Set to 1. */
2191 USHORT extensionLookupType; /* Lookup type of subtable referenced
2192 * by ExtensionOffset (i.e. the
2193 * extension subtable). */
Behdad Esfahbod81f2af42010-04-22 00:58:49 -04002194 ULONG extensionOffset; /* Offset to the extension subtable,
2195 * of lookup type subtable. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002196 public:
2197 DEFINE_SIZE_STATIC (8);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002198};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002199
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002200template <typename T>
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002201struct Extension
2202{
2203 inline unsigned int get_type (void) const
2204 {
2205 switch (u.format) {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002206 case 1: return u.format1.get_type ();
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002207 default:return 0;
2208 }
2209 }
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05002210 template <typename X>
2211 inline const X& get_subtable (void) const
2212 {
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002213 switch (u.format) {
2214 case 1: return u.format1.template get_subtable<typename T::LookupSubTable> ();
2215 default:return Null(typename T::LookupSubTable);
2216 }
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05002217 }
2218
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002219 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002220 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002221 {
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002222 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002223 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002224 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002225 case 1: return_trace (u.format1.dispatch (c));
2226 default:return_trace (c->default_return_value ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002227 }
2228 }
2229
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002230 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002231 union {
2232 USHORT format; /* Format identifier */
Behdad Esfahbod095a1252015-02-19 10:29:41 +03002233 ExtensionFormat1<T> format1;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002234 } u;
2235};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002236
2237
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002238/*
2239 * GSUB/GPOS Common
2240 */
2241
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002242struct GSUBGPOS
2243{
Behdad Esfahboda328d662009-08-04 20:27:05 -04002244 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
2245 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002246
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002247 inline unsigned int get_script_count (void) const
2248 { return (this+scriptList).len; }
2249 inline const Tag& get_script_tag (unsigned int i) const
2250 { return (this+scriptList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002251 inline unsigned int get_script_tags (unsigned int start_offset,
2252 unsigned int *script_count /* IN/OUT */,
2253 hb_tag_t *script_tags /* OUT */) const
2254 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002255 inline const Script& get_script (unsigned int i) const
2256 { return (this+scriptList)[i]; }
2257 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
2258 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002259
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002260 inline unsigned int get_feature_count (void) const
2261 { return (this+featureList).len; }
Jonathan Kewda132932014-04-27 14:05:24 +01002262 inline hb_tag_t get_feature_tag (unsigned int i) const
2263 { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : (this+featureList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002264 inline unsigned int get_feature_tags (unsigned int start_offset,
2265 unsigned int *feature_count /* IN/OUT */,
2266 hb_tag_t *feature_tags /* OUT */) const
2267 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002268 inline const Feature& get_feature (unsigned int i) const
2269 { return (this+featureList)[i]; }
2270 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
2271 { return (this+featureList).find_index (tag, index); }
2272
2273 inline unsigned int get_lookup_count (void) const
2274 { return (this+lookupList).len; }
2275 inline const Lookup& get_lookup (unsigned int i) const
2276 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002277
Behdad Esfahbod30c42b62016-09-10 03:32:39 -07002278 inline bool find_variations_index (const int *coords, unsigned int num_coords,
2279 unsigned int *index) const
Behdad Esfahbod59055b52016-09-10 01:24:28 -07002280 { return (version.to_int () >= 0x00010001u ? this+featureVars : Null(FeatureVariations))
Behdad Esfahbod30c42b62016-09-10 03:32:39 -07002281 .find_index (coords, num_coords, index); }
Behdad Esfahbodec87ba92016-09-10 03:53:11 -07002282 inline const Feature& get_feature_variation (unsigned int feature_index,
2283 unsigned int variations_index) const
2284 {
2285 if (FeatureVariations::NOT_FOUND_INDEX != variations_index &&
2286 version.to_int () >= 0x00010001u)
2287 {
Behdad Esfahbod4ebbeb72016-09-10 04:52:34 -07002288 const Feature *feature = (this+featureVars).find_substitute (variations_index,
2289 feature_index);
Behdad Esfahbodec87ba92016-09-10 03:53:11 -07002290 if (feature)
2291 return *feature;
2292 }
2293 return get_feature (feature_index);
2294 }
Behdad Esfahbod59055b52016-09-10 01:24:28 -07002295
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002296 inline bool sanitize (hb_sanitize_context_t *c) const
2297 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002298 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002299 return_trace (version.sanitize (c) &&
2300 likely (version.major == 1) &&
2301 scriptList.sanitize (c, this) &&
2302 featureList.sanitize (c, this) &&
Behdad Esfahbod59055b52016-09-10 01:24:28 -07002303 lookupList.sanitize (c, this) &&
2304 (version.to_int () < 0x00010001u || featureVars.sanitize (c, this)));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -04002305 }
2306
Behdad Esfahbod212aba62009-05-24 00:50:27 -04002307 protected:
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +09002308 FixedVersion<>version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbod76271002014-07-11 14:54:42 -04002309 * to 0x00010000u */
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002310 OffsetTo<ScriptList>
2311 scriptList; /* ScriptList table */
2312 OffsetTo<FeatureList>
2313 featureList; /* FeatureList table */
2314 OffsetTo<LookupList>
2315 lookupList; /* LookupList table */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -08002316 LOffsetTo<FeatureVariations>
Behdad Esfahbod59055b52016-09-10 01:24:28 -07002317 featureVars; /* Offset to Feature Variations
2318 table--from beginning of table
2319 * (may be NULL). Introduced
2320 * in version 0x00010001. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002321 public:
Behdad Esfahbod59055b52016-09-10 01:24:28 -07002322 DEFINE_SIZE_MIN (10);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002323};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04002324
Behdad Esfahbod6f20f722009-05-17 20:28:01 -04002325
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08002326} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04002327
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04002328
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -04002329#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */