blob: bfe8ffa3f33aff1e73017c18c892eaf520117b3f [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 Esfahbod902cc8a2012-11-23 15:06:59 -050040
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -050041#define TRACE_DISPATCH(this) \
Behdad Esfahboda1733db2012-11-23 16:40:04 -050042 hb_auto_trace_t<context_t::max_debug_depth, typename context_t::return_t> trace \
43 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
44 "");
45
46
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -040047
48#ifndef HB_DEBUG_IS_INPLACE
49#define HB_DEBUG_IS_INPLACE (HB_DEBUG+0)
50#endif
51
52#define TRACE_IS_INPLACE(this) \
53 hb_auto_trace_t<HB_DEBUG_IS_INPLACE, bool> trace \
54 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
55 "");
56
57struct hb_is_inplace_context_t
58{
59 inline const char *get_name (void) { return "IS_INPLACE"; }
60 static const unsigned int max_debug_depth = HB_DEBUG_IS_INPLACE;
61 typedef bool return_t;
62 typedef return_t (*recurse_func_t) (hb_is_inplace_context_t *c, unsigned int lookup_index);
63 template <typename T>
64 inline return_t dispatch (const T &obj) { return obj.is_inplace (this); }
65 static return_t default_return_value (void) { return true; }
66 bool stop_sublookup_iteration (return_t r) const { return !r; }
67
68 return_t recurse (unsigned int lookup_index)
69 {
70 if (unlikely (nesting_level_left == 0 || !recurse_func))
71 return default_return_value ();
72
73 nesting_level_left--;
74 bool ret = recurse_func (this, lookup_index);
75 nesting_level_left++;
76 return ret;
77 }
78
79 hb_face_t *face;
80 recurse_func_t recurse_func;
81 unsigned int nesting_level_left;
82 unsigned int debug_depth;
83
84 hb_is_inplace_context_t (hb_face_t *face_,
85 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
86 face (face_),
87 recurse_func (NULL),
88 nesting_level_left (nesting_level_left_),
89 debug_depth (0) {}
90
91 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
92};
93
94
95
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040096#ifndef HB_DEBUG_CLOSURE
97#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
98#endif
99
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500100#define TRACE_CLOSURE(this) \
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500101 hb_auto_trace_t<HB_DEBUG_CLOSURE, hb_void_t> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500102 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500103 "");
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400104
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400105struct hb_closure_context_t
106{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500107 inline const char *get_name (void) { return "CLOSURE"; }
108 static const unsigned int max_debug_depth = HB_DEBUG_CLOSURE;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500109 typedef hb_void_t return_t;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -0500110 typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
111 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500112 inline return_t dispatch (const T &obj) { obj.closure (this); return HB_VOID; }
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500113 static return_t default_return_value (void) { return HB_VOID; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600114 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
Behdad Esfahbod44fc2372012-11-21 23:33:13 -0500115 return_t recurse (unsigned int lookup_index)
116 {
Behdad Esfahbod9b346772012-11-23 17:55:40 -0500117 if (unlikely (nesting_level_left == 0 || !recurse_func))
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500118 return default_return_value ();
Behdad Esfahbod44fc2372012-11-21 23:33:13 -0500119
120 nesting_level_left--;
121 recurse_func (this, lookup_index);
122 nesting_level_left++;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500123 return HB_VOID;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -0500124 }
125
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400126 hb_face_t *face;
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400127 hb_set_t *glyphs;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -0500128 recurse_func_t recurse_func;
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400129 unsigned int nesting_level_left;
130 unsigned int debug_depth;
131
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400132 hb_closure_context_t (hb_face_t *face_,
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400133 hb_set_t *glyphs_,
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400134 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400135 face (face_),
136 glyphs (glyphs_),
Behdad Esfahbod9b346772012-11-23 17:55:40 -0500137 recurse_func (NULL),
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400138 nesting_level_left (nesting_level_left_),
139 debug_depth (0) {}
Behdad Esfahbod9b346772012-11-23 17:55:40 -0500140
141 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400142};
143
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400144
145
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400146#ifndef HB_DEBUG_WOULD_APPLY
147#define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
148#endif
149
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500150#define TRACE_WOULD_APPLY(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500151 hb_auto_trace_t<HB_DEBUG_WOULD_APPLY, bool> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500152 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500153 "%d glyphs", c->len);
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400154
155struct hb_would_apply_context_t
156{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500157 inline const char *get_name (void) { return "WOULD_APPLY"; }
158 static const unsigned int max_debug_depth = HB_DEBUG_WOULD_APPLY;
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500159 typedef bool return_t;
160 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500161 inline return_t dispatch (const T &obj) { return obj.would_apply (this); }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500162 static return_t default_return_value (void) { return false; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600163 bool stop_sublookup_iteration (return_t r) const { return r; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500164
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400165 hb_face_t *face;
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400166 const hb_codepoint_t *glyphs;
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400167 unsigned int len;
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400168 bool zero_context;
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400169 unsigned int debug_depth;
170
171 hb_would_apply_context_t (hb_face_t *face_,
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400172 const hb_codepoint_t *glyphs_,
173 unsigned int len_,
Behdad Esfahbod2bd9fe32012-09-04 15:15:19 -0400174 bool zero_context_) :
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400175 face (face_),
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400176 glyphs (glyphs_),
177 len (len_),
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400178 zero_context (zero_context_),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500179 debug_depth (0) {}
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400180};
181
182
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800183
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800184#ifndef HB_DEBUG_COLLECT_GLYPHS
185#define HB_DEBUG_COLLECT_GLYPHS (HB_DEBUG+0)
186#endif
187
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500188#define TRACE_COLLECT_GLYPHS(this) \
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500189 hb_auto_trace_t<HB_DEBUG_COLLECT_GLYPHS, hb_void_t> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500190 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500191 "");
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800192
193struct hb_collect_glyphs_context_t
194{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500195 inline const char *get_name (void) { return "COLLECT_GLYPHS"; }
196 static const unsigned int max_debug_depth = HB_DEBUG_COLLECT_GLYPHS;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500197 typedef hb_void_t return_t;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500198 typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500199 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500200 inline return_t dispatch (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500201 static return_t default_return_value (void) { return HB_VOID; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600202 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500203 return_t recurse (unsigned int lookup_index)
204 {
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500205 if (unlikely (nesting_level_left == 0 || !recurse_func))
206 return default_return_value ();
207
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -0500208 /* Note that GPOS sets recurse_func to NULL already, so it doesn't get
209 * past the previous check. For GSUB, we only want to collect the output
Behdad Esfahbod76ea5632013-05-04 16:01:20 -0400210 * glyphs in the recursion. If output is not requested, we can go home now.
211 *
212 * Note further, that the above is not exactly correct. A recursed lookup
213 * is allowed to match input that is not matched in the context, but that's
214 * not how most fonts are built. It's possible to relax that and recurse
215 * with all sets here if it proves to be an issue.
216 */
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500217
218 if (output == hb_set_get_empty ())
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500219 return HB_VOID;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500220
221 hb_set_t *old_before = before;
222 hb_set_t *old_input = input;
223 hb_set_t *old_after = after;
224 before = input = after = hb_set_get_empty ();
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -0500225
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500226 nesting_level_left--;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500227 recurse_func (this, lookup_index);
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500228 nesting_level_left++;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500229
230 before = old_before;
231 input = old_input;
232 after = old_after;
233
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500234 return HB_VOID;
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500235 }
236
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800237 hb_face_t *face;
Behdad Esfahbod83035932012-12-04 17:08:41 -0500238 hb_set_t *before;
239 hb_set_t *input;
240 hb_set_t *after;
241 hb_set_t *output;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500242 recurse_func_t recurse_func;
243 unsigned int nesting_level_left;
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800244 unsigned int debug_depth;
245
246 hb_collect_glyphs_context_t (hb_face_t *face_,
247 hb_set_t *glyphs_before, /* OUT. May be NULL */
248 hb_set_t *glyphs_input, /* OUT. May be NULL */
249 hb_set_t *glyphs_after, /* OUT. May be NULL */
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500250 hb_set_t *glyphs_output, /* OUT. May be NULL */
251 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800252 face (face_),
Behdad Esfahbod83035932012-12-04 17:08:41 -0500253 before (glyphs_before ? glyphs_before : hb_set_get_empty ()),
254 input (glyphs_input ? glyphs_input : hb_set_get_empty ()),
255 after (glyphs_after ? glyphs_after : hb_set_get_empty ()),
256 output (glyphs_output ? glyphs_output : hb_set_get_empty ()),
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500257 recurse_func (NULL),
258 nesting_level_left (nesting_level_left_),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500259 debug_depth (0) {}
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500260
261 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800262};
263
264
265
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500266struct hb_get_coverage_context_t
267{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500268 inline const char *get_name (void) { return "GET_COVERAGE"; }
269 static const unsigned int max_debug_depth = 0;
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500270 typedef const Coverage &return_t;
271 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500272 inline return_t dispatch (const T &obj) { return obj.get_coverage (); }
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500273 static return_t default_return_value (void) { return Null(Coverage); }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500274
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500275 hb_get_coverage_context_t (void) :
276 debug_depth (0) {}
277
278 unsigned int debug_depth;
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500279};
280
281
282
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400283#ifndef HB_DEBUG_APPLY
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400284#define HB_DEBUG_APPLY (HB_DEBUG+0)
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400285#endif
286
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500287#define TRACE_APPLY(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500288 hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500289 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500290 "idx %d codepoint %u", c->buffer->idx, c->buffer->cur().codepoint);
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400291
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400292struct hb_apply_context_t
293{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500294 inline const char *get_name (void) { return "APPLY"; }
295 static const unsigned int max_debug_depth = HB_DEBUG_APPLY;
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500296 typedef bool return_t;
297 typedef return_t (*recurse_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
298 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500299 inline return_t dispatch (const T &obj) { return obj.apply (this); }
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500300 static return_t default_return_value (void) { return false; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600301 bool stop_sublookup_iteration (return_t r) const { return r; }
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500302 return_t recurse (unsigned int lookup_index)
303 {
304 if (unlikely (nesting_level_left == 0 || !recurse_func))
305 return default_return_value ();
306
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -0500307 nesting_level_left--;
Behdad Esfahbodf18ff5a2012-11-30 08:07:06 +0200308 bool ret = recurse_func (this, lookup_index);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -0500309 nesting_level_left++;
Behdad Esfahbodf18ff5a2012-11-30 08:07:06 +0200310 return ret;
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500311 }
312
Behdad Esfahbod6880f7e2013-02-13 12:17:25 -0500313 unsigned int table_index; /* GSUB/GPOS */
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400314 hb_font_t *font;
315 hb_face_t *face;
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400316 hb_buffer_t *buffer;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400317 hb_direction_t direction;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100318 hb_mask_t lookup_mask;
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400319 bool auto_zwj;
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500320 recurse_func_t recurse_func;
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400321 unsigned int nesting_level_left;
Behdad Esfahbod8c69e652010-10-27 22:07:49 -0400322 unsigned int lookup_props;
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400323 const GDEF &gdef;
Behdad Esfahbod300c7302012-07-30 19:37:44 -0400324 bool has_glyph_classes;
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500325 unsigned int debug_depth;
Behdad Esfahbod98370e82010-10-27 17:39:01 -0400326
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400327
Behdad Esfahbod6880f7e2013-02-13 12:17:25 -0500328 hb_apply_context_t (unsigned int table_index_,
329 hb_font_t *font_,
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400330 hb_buffer_t *buffer_) :
Behdad Esfahbod6880f7e2013-02-13 12:17:25 -0500331 table_index (table_index_),
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400332 font (font_), face (font->face), buffer (buffer_),
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400333 direction (buffer_->props.direction),
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400334 lookup_mask (1),
335 auto_zwj (true),
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500336 recurse_func (NULL),
Behdad Esfahbod6736f3c2012-05-13 15:21:06 +0200337 nesting_level_left (MAX_NESTING_LEVEL),
Behdad Esfahbod407fc122013-02-13 11:13:06 -0500338 lookup_props (0),
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400339 gdef (*hb_ot_layout_from_face (face)->gdef),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500340 has_glyph_classes (gdef.has_glyph_classes ()),
341 debug_depth (0) {}
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400342
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400343 inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
344 inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500345 inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
346 inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
347 inline void set_lookup (const Lookup &l) { lookup_props = l.get_props (); }
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400348
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500349 struct matcher_t
350 {
351 inline matcher_t (void) :
352 lookup_props (0),
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500353 ignore_zwnj (false),
354 ignore_zwj (false),
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500355 mask (-1),
356#define arg1(arg) (arg) /* Remove the macro to see why it's needed! */
357 syllable arg1(0),
358#undef arg1
359 match_func (NULL),
360 match_data (NULL) {};
361
362 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
363
364 inline void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500365 inline void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500366 inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
367 inline void set_mask (hb_mask_t mask_) { mask = mask_; }
368 inline void set_syllable (uint8_t syllable_) { syllable = syllable_; }
369 inline void set_match_func (match_func_t match_func_,
370 const void *match_data_)
371 { match_func = match_func_; match_data = match_data_; }
372
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500373 enum may_match_t {
374 MATCH_NO,
375 MATCH_YES,
376 MATCH_MAYBE
377 };
378
379 inline may_match_t may_match (const hb_glyph_info_t &info,
380 const USHORT *glyph_data) const
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500381 {
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500382 if (!(info.mask & mask) ||
383 (syllable && syllable != info.syllable ()))
384 return MATCH_NO;
385
386 if (match_func)
387 return match_func (info.codepoint, *glyph_data, match_data) ? MATCH_YES : MATCH_NO;
388
389 return MATCH_MAYBE;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500390 }
391
392 enum may_skip_t {
393 SKIP_NO,
394 SKIP_YES,
395 SKIP_MAYBE
396 };
397
398 inline may_skip_t
399 may_skip (const hb_apply_context_t *c,
400 const hb_glyph_info_t &info) const
401 {
402 unsigned int property;
403
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200404 property = _hb_glyph_info_get_glyph_props (&info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500405
406 if (!c->match_properties (info.codepoint, property, lookup_props))
407 return SKIP_YES;
408
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500409 if (unlikely (_hb_glyph_info_is_default_ignorable (&info) &&
410 (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
411 (ignore_zwj || !_hb_glyph_info_is_zwj (&info)) &&
Behdad Esfahboda1f7b282013-10-18 01:09:08 +0200412 !_hb_glyph_info_ligated (&info)))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500413 return SKIP_MAYBE;
414
415 return SKIP_NO;
416 }
417
418 protected:
419 unsigned int lookup_props;
420 bool ignore_zwnj;
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500421 bool ignore_zwj;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500422 hb_mask_t mask;
423 uint8_t syllable;
424 match_func_t match_func;
425 const void *match_data;
426 };
427
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500428 struct skipping_forward_iterator_t
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500429 {
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500430 inline skipping_forward_iterator_t (hb_apply_context_t *c_,
431 unsigned int start_index_,
432 unsigned int num_items_,
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500433 bool context_match = false) :
434 idx (start_index_),
435 c (c_),
436 match_glyph_data (NULL),
437 num_items (num_items_),
438 end (c->buffer->len)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500439 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500440 matcher.set_lookup_props (c->lookup_props);
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400441 /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
442 matcher.set_ignore_zwnj (context_match || c->table_index == 1);
443 /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
444 matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500445 if (!context_match)
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500446 matcher.set_mask (c->lookup_mask);
Behdad Esfahbodcb90b1b2013-02-15 07:02:08 -0500447 matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500448 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500449 inline void set_lookup_props (unsigned int lookup_props) { matcher.set_lookup_props (lookup_props); }
450 inline void set_syllable (unsigned int syllable) { matcher.set_syllable (syllable); }
451 inline void set_match_func (matcher_t::match_func_t match_func,
452 const void *match_data,
453 const USHORT glyph_data[])
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500454 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500455 matcher.set_match_func (match_func, match_data);
456 match_glyph_data = glyph_data;
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500457 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500458
459 inline bool has_no_chance (void) const { return unlikely (num_items && idx + num_items >= end); }
460 inline void reject (void) { num_items++; match_glyph_data--; }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500461 inline bool next (void)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500462 {
Behdad Esfahbod506ffeb2012-01-18 16:07:53 -0500463 assert (num_items > 0);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500464 while (!has_no_chance ())
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500465 {
Behdad Esfahboda4a48fe2012-01-17 18:08:41 -0500466 idx++;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500467 const hb_glyph_info_t &info = c->buffer->info[idx];
468
Behdad Esfahbodff93ac82013-02-21 14:51:40 -0500469 matcher_t::may_skip_t skip = matcher.may_skip (c, info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500470 if (unlikely (skip == matcher_t::SKIP_YES))
471 continue;
472
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500473 matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500474 if (match == matcher_t::MATCH_YES ||
475 (match == matcher_t::MATCH_MAYBE &&
476 skip == matcher_t::SKIP_NO))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500477 {
478 num_items--;
479 match_glyph_data++;
480 return true;
481 }
482
483 if (skip == matcher_t::SKIP_NO)
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500484 return false;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500485 }
486 return false;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500487 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500488
489 unsigned int idx;
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400490 protected:
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500491 hb_apply_context_t *c;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500492 matcher_t matcher;
493 const USHORT *match_glyph_data;
494
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500495 unsigned int num_items;
496 unsigned int end;
497 };
498
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500499 struct skipping_backward_iterator_t
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500500 {
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500501 inline skipping_backward_iterator_t (hb_apply_context_t *c_,
502 unsigned int start_index_,
503 unsigned int num_items_,
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500504 bool context_match = false) :
505 idx (start_index_),
506 c (c_),
507 match_glyph_data (NULL),
508 num_items (num_items_)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500509 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500510 matcher.set_lookup_props (c->lookup_props);
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400511 /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
512 matcher.set_ignore_zwnj (context_match || c->table_index == 1);
513 /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
514 matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500515 if (!context_match)
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500516 matcher.set_mask (c->lookup_mask);
Behdad Esfahbodcb90b1b2013-02-15 07:02:08 -0500517 matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500518 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500519 inline void set_lookup_props (unsigned int lookup_props) { matcher.set_lookup_props (lookup_props); }
520 inline void set_syllable (unsigned int syllable) { matcher.set_syllable (syllable); }
521 inline void set_match_func (matcher_t::match_func_t match_func,
522 const void *match_data,
523 const USHORT glyph_data[])
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500524 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500525 matcher.set_match_func (match_func, match_data);
526 match_glyph_data = glyph_data;
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500527 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500528
529 inline bool has_no_chance (void) const { return unlikely (idx < num_items); }
530 inline void reject (void) { num_items++; }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500531 inline bool prev (void)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500532 {
Behdad Esfahbod506ffeb2012-01-18 16:07:53 -0500533 assert (num_items > 0);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500534 while (!has_no_chance ())
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500535 {
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500536 idx--;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500537 const hb_glyph_info_t &info = c->buffer->out_info[idx];
538
Behdad Esfahbodff93ac82013-02-21 14:51:40 -0500539 matcher_t::may_skip_t skip = matcher.may_skip (c, info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500540
541 if (unlikely (skip == matcher_t::SKIP_YES))
542 continue;
543
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500544 matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500545 if (match == matcher_t::MATCH_YES ||
546 (match == matcher_t::MATCH_MAYBE &&
547 skip == matcher_t::SKIP_NO))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500548 {
549 num_items--;
550 match_glyph_data++;
551 return true;
552 }
553
554 if (skip == matcher_t::SKIP_NO)
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500555 return false;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500556 }
557 return false;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500558 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500559
560 unsigned int idx;
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400561 protected:
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500562 hb_apply_context_t *c;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500563 matcher_t matcher;
564 const USHORT *match_glyph_data;
565
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500566 unsigned int num_items;
567 };
568
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400569 inline bool
570 match_properties_mark (hb_codepoint_t glyph,
571 unsigned int glyph_props,
572 unsigned int lookup_props) const
573 {
574 /* If using mark filtering sets, the high short of
575 * lookup_props has the set index.
576 */
577 if (lookup_props & LookupFlag::UseMarkFilteringSet)
578 return gdef.mark_set_covers (lookup_props >> 16, glyph);
579
580 /* The second byte of lookup_props has the meaning
581 * "ignore marks of attachment type different than
582 * the attachment type specified."
583 */
584 if (lookup_props & LookupFlag::MarkAttachmentType)
585 return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
586
587 return true;
588 }
589
590 inline bool
591 match_properties (hb_codepoint_t glyph,
592 unsigned int glyph_props,
593 unsigned int lookup_props) const
594 {
595 /* Not covered, if, for example, glyph class is ligature and
596 * lookup_props includes LookupFlags::IgnoreLigatures
597 */
598 if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
599 return false;
600
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800601 if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400602 return match_properties_mark (glyph, glyph_props, lookup_props);
603
604 return true;
605 }
606
607 inline bool
608 check_glyph_property (hb_glyph_info_t *info,
Behdad Esfahbod407fc122013-02-13 11:13:06 -0500609 unsigned int lookup_props) const
Behdad Esfahbod7d479902012-01-18 21:19:32 -0500610 {
Behdad Esfahbodce476132012-06-09 01:10:26 -0400611 unsigned int property;
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400612
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200613 property = _hb_glyph_info_get_glyph_props (info);
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400614
615 return match_properties (info->codepoint, property, lookup_props);
616 }
617
Behdad Esfahboda0161742013-10-18 00:06:30 +0200618 inline void _set_glyph_props (hb_codepoint_t glyph_index,
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100619 unsigned int class_guess = 0,
620 bool ligature = false) const
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400621 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200622 unsigned int add_in = _hb_glyph_info_get_glyph_props (&buffer->cur()) &
623 HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
624 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
625 if (ligature)
626 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
Behdad Esfahbod2fca1422012-07-30 18:46:41 -0400627 if (likely (has_glyph_classes))
Behdad Esfahbod05ad6b52013-10-18 00:45:59 +0200628 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400629 else if (class_guess)
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200630 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400631 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500632
Behdad Esfahboda0161742013-10-18 00:06:30 +0200633 inline void replace_glyph (hb_codepoint_t glyph_index) const
Behdad Esfahbod3ec77d62012-06-08 21:44:06 -0400634 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200635 _set_glyph_props (glyph_index);
Behdad Esfahbod98370e82010-10-27 17:39:01 -0400636 buffer->replace_glyph (glyph_index);
637 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200638 inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400639 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200640 _set_glyph_props (glyph_index);
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400641 buffer->cur().codepoint = glyph_index;
642 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200643 inline void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
644 unsigned int class_guess) const
645 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200646 _set_glyph_props (glyph_index, class_guess, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200647 buffer->replace_glyph (glyph_index);
648 }
649 inline void output_glyph (hb_codepoint_t glyph_index,
650 unsigned int class_guess) const
651 {
652 _set_glyph_props (glyph_index, class_guess);
653 buffer->output_glyph (glyph_index);
654 }
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400655};
656
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400657
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400658
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400659typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500660typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400661typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400662
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400663struct ContextClosureFuncs
664{
665 intersects_func_t intersects;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400666};
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500667struct ContextCollectGlyphsFuncs
668{
669 collect_glyphs_func_t collect;
670};
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400671struct ContextApplyFuncs
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400672{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400673 match_func_t match;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400674};
675
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500676
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400677static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400678{
679 return glyphs->has (value);
680}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400681static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400682{
683 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
684 return class_def.intersects_class (glyphs, value);
685}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400686static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400687{
688 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
689 return (data+coverage).intersects (glyphs);
690}
691
692static inline bool intersects_array (hb_closure_context_t *c,
693 unsigned int count,
694 const USHORT values[],
695 intersects_func_t intersects_func,
696 const void *intersects_data)
697{
698 for (unsigned int i = 0; i < count; i++)
699 if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
700 return false;
701 return true;
702}
703
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400704
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500705static inline void collect_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
706{
707 glyphs->add (value);
708}
709static inline void collect_class (hb_set_t *glyphs, const USHORT &value, const void *data)
710{
711 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
712 class_def.add_class (glyphs, value);
713}
714static inline void collect_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
715{
716 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
717 (data+coverage).add_coverage (glyphs);
718}
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500719static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500720 hb_set_t *glyphs,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500721 unsigned int count,
722 const USHORT values[],
723 collect_glyphs_func_t collect_func,
724 const void *collect_data)
725{
726 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500727 collect_func (glyphs, values[i], collect_data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500728}
729
730
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400731static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400732{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400733 return glyph_id == value;
734}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400735static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400736{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400737 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400738 return class_def.get_class (glyph_id) == value;
739}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400740static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400741{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -0400742 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400743 return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400744}
745
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400746static inline bool would_match_input (hb_would_apply_context_t *c,
747 unsigned int count, /* Including the first glyph (not matched) */
748 const USHORT input[], /* Array of input values--start with second glyph */
749 match_func_t match_func,
750 const void *match_data)
751{
752 if (count != c->len)
753 return false;
754
755 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400756 if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400757 return false;
758
759 return true;
760}
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400761static inline bool match_input (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400762 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400763 const USHORT input[], /* Array of input values--start with second glyph */
764 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400765 const void *match_data,
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200766 unsigned int *end_offset,
767 unsigned int match_positions[MAX_CONTEXT_LENGTH],
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400768 bool *p_is_mark_ligature = NULL,
769 unsigned int *p_total_component_count = NULL)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400770{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500771 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400772
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200773 if (unlikely (count > MAX_CONTEXT_LENGTH)) TRACE_RETURN (false);
774
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200775 hb_buffer_t *buffer = c->buffer;
776
777 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, count - 1);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500778 skippy_iter.set_match_func (match_func, match_data, input);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400779 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
780
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400781 /*
782 * This is perhaps the trickiest part of OpenType... Remarks:
783 *
784 * - If all components of the ligature were marks, we call this a mark ligature.
785 *
786 * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
787 * it as a ligature glyph.
788 *
789 * - Ligatures cannot be formed across glyphs attached to different components
790 * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
791 * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
792 * However, it would be wrong to ligate that SHADDA,FATHA sequence.o
793 * There is an exception to this: If a ligature tries ligating with marks that
794 * belong to it itself, go ahead, assuming that the font designer knows what
795 * they are doing (otherwise it can break Indic stuff when a matra wants to
796 * ligate with a conjunct...)
797 */
798
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200799 bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400800
801 unsigned int total_component_count = 0;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200802 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400803
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200804 unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
805 unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400806
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200807 match_positions[0] = buffer->idx;
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500808 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400809 {
Behdad Esfahbod407fc122013-02-13 11:13:06 -0500810 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200811
812 match_positions[i] = skippy_iter.idx;
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400813
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200814 unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]);
815 unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400816
817 if (first_lig_id && first_lig_comp) {
818 /* If first component was attached to a previous ligature component,
819 * all subsequent components should be attached to the same ligature
820 * component, otherwise we shouldn't ligate them. */
821 if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
822 return TRACE_RETURN (false);
823 } else {
824 /* If first component was NOT attached to a previous ligature component,
825 * all subsequent components should also NOT be attached to any ligature
826 * component, unless they are attached to the first component itself! */
827 if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
828 return TRACE_RETURN (false);
829 }
830
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200831 is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200832 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400833 }
834
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200835 *end_offset = skippy_iter.idx - buffer->idx + 1;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400836
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400837 if (p_is_mark_ligature)
838 *p_is_mark_ligature = is_mark_ligature;
839
840 if (p_total_component_count)
841 *p_total_component_count = total_component_count;
842
Behdad Esfahbodbc513ad2012-10-29 19:03:55 -0700843 return TRACE_RETURN (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400844}
Behdad Esfahboda177d022012-08-28 23:18:22 -0400845static inline void ligate_input (hb_apply_context_t *c,
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200846 unsigned int count, /* Including the first glyph */
847 unsigned int match_positions[MAX_CONTEXT_LENGTH], /* Including the first glyph */
848 unsigned int match_length,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400849 hb_codepoint_t lig_glyph,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400850 bool is_mark_ligature,
851 unsigned int total_component_count)
852{
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200853 TRACE_APPLY (NULL);
854
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200855 hb_buffer_t *buffer = c->buffer;
856
857 buffer->merge_clusters (buffer->idx, buffer->idx + match_length);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500858
Behdad Esfahboda177d022012-08-28 23:18:22 -0400859 /*
860 * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
861 * the ligature to keep its old ligature id. This will allow it to attach to
862 * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
863 * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
864 * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature
865 * later, we don't want them to lose their ligature id/component, otherwise
866 * GPOS will fail to correctly position the mark ligature on top of the
867 * LAM,LAM,HEH ligature. See:
868 * https://bugzilla.gnome.org/show_bug.cgi?id=676343
869 *
870 * - If a ligature is formed of components that some of which are also ligatures
871 * themselves, and those ligature components had marks attached to *their*
872 * components, we have to attach the marks to the new ligature component
873 * positions! Now *that*'s tricky! And these marks may be following the
874 * last component of the whole sequence, so we should loop forward looking
875 * for them and update them.
876 *
877 * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
878 * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
879 * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature
880 * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to
881 * the new ligature with a component value of 2.
882 *
883 * This in fact happened to a font... See:
884 * https://bugzilla.gnome.org/show_bug.cgi?id=437633
885 */
886
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800887 unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200888 unsigned int lig_id = is_mark_ligature ? 0 : _hb_allocate_lig_id (buffer);
889 unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
890 unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400891 unsigned int components_so_far = last_num_components;
892
893 if (!is_mark_ligature)
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400894 {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200895 _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200896 if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100897 {
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200898 _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100899 _hb_glyph_info_set_modified_combining_class (&buffer->cur(), 0);
900 }
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400901 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200902 c->replace_glyph_with_ligature (lig_glyph, klass);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400903
904 for (unsigned int i = 1; i < count; i++)
905 {
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200906 while (buffer->idx < match_positions[i])
Behdad Esfahboda177d022012-08-28 23:18:22 -0400907 {
908 if (!is_mark_ligature) {
909 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200910 MIN (MAX (_hb_glyph_info_get_lig_comp (&buffer->cur()), 1u), last_num_components);
911 _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400912 }
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200913 buffer->next_glyph ();
Behdad Esfahboda177d022012-08-28 23:18:22 -0400914 }
915
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200916 last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
917 last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400918 components_so_far += last_num_components;
919
920 /* Skip the base glyph */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200921 buffer->idx++;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400922 }
923
924 if (!is_mark_ligature && last_lig_id) {
925 /* Re-adjust components for any marks following. */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200926 for (unsigned int i = buffer->idx; i < buffer->len; i++) {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200927 if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
Behdad Esfahboda177d022012-08-28 23:18:22 -0400928 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200929 MIN (MAX (_hb_glyph_info_get_lig_comp (&buffer->info[i]), 1u), last_num_components);
930 _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400931 } else
932 break;
933 }
934 }
935}
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400936
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400937static inline bool match_backtrack (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400938 unsigned int count,
939 const USHORT backtrack[],
940 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400941 const void *match_data)
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400942{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500943 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400944
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500945 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500946 skippy_iter.set_match_func (match_func, match_data, backtrack);
947 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400948
Behdad Esfahbod4d3aeb82012-01-16 16:43:26 -0500949 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500950 if (!skippy_iter.prev ())
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400951 return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400952
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400953 return TRACE_RETURN (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400954}
955
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400956static inline bool match_lookahead (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400957 unsigned int count,
958 const USHORT lookahead[],
959 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400960 const void *match_data,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400961 unsigned int offset)
962{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500963 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400964
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500965 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500966 skippy_iter.set_match_func (match_func, match_data, lookahead);
967 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400968
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500969 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500970 if (!skippy_iter.next ())
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400971 return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400972
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400973 return TRACE_RETURN (true);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400974}
975
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400976
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400977
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400978struct LookupRecord
979{
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400980 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500981 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200982 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400983 }
984
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400985 USHORT sequenceIndex; /* Index into current glyph
986 * sequence--first glyph = 0 */
987 USHORT lookupListIndex; /* Lookup to apply to that
988 * position--zero--based */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400989 public:
990 DEFINE_SIZE_STATIC (4);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400991};
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400992
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400993
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500994template <typename context_t>
995static inline void recurse_lookups (context_t *c,
996 unsigned int lookupCount,
997 const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400998{
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400999 for (unsigned int i = 0; i < lookupCount; i++)
Behdad Esfahbod86522e42013-07-22 19:07:53 -04001000 c->recurse (lookupRecord[i].lookupListIndex);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001001}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001002
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001003static inline bool apply_lookup (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -04001004 unsigned int count, /* Including the first glyph */
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001005 unsigned int match_positions[MAX_CONTEXT_LENGTH], /* Including the first glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -04001006 unsigned int lookupCount,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001007 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
1008 unsigned int match_length)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001009{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -05001010 TRACE_APPLY (NULL);
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -05001011
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001012 hb_buffer_t *buffer = c->buffer;
1013 unsigned int end;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001014
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001015 /* All positions are distance from beginning of *output* buffer.
1016 * Adjust. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001017 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001018 unsigned int bl = buffer->backtrack_len ();
1019 end = bl + match_length;
Behdad Esfahbod8751de52013-07-18 16:29:50 -04001020
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001021 int delta = bl - buffer->idx;
1022 /* Convert positions to new indexing. */
1023 for (unsigned int j = 0; j < count; j++)
1024 match_positions[j] += delta;
Behdad Esfahbod8820bb22013-02-14 07:41:03 -05001025 }
Behdad Esfahbode73a0c22009-05-18 04:15:25 -04001026
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001027 for (unsigned int i = 0; i < lookupCount; i++)
1028 {
1029 unsigned int idx = lookupRecord[i].sequenceIndex;
1030 if (idx >= count)
1031 continue;
1032
1033 buffer->move_to (match_positions[idx]);
1034
1035 unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
1036 if (!c->recurse (lookupRecord[i].lookupListIndex))
1037 continue;
1038
1039 unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
1040 int delta = new_len - orig_len;
1041
1042 if (!delta)
1043 continue;
1044
1045 /* Recursed lookup changed buffer len. Adjust. */
1046
Behdad Esfahbodda720422013-10-17 12:01:50 +02001047 /* end can't go back past the current match position. */
1048 end = MAX ((int) match_positions[idx] + 1, int (end) + delta);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001049
1050 unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */
1051
1052 if (delta > 0)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001053 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001054 if (unlikely (delta + count > MAX_CONTEXT_LENGTH))
1055 break;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001056 }
1057 else
1058 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001059 /* NOTE: delta is negative. */
1060 delta = MAX (delta, (int) next - (int) count);
1061 next -= delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001062 }
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001063
1064 /* Shift! */
1065 memmove (match_positions + next + delta, match_positions + next,
1066 (count - next) * sizeof (match_positions[0]));
1067 next += delta;
1068 count += delta;
1069
1070 /* Fill in new entries. */
1071 for (unsigned int j = idx + 1; j < next; j++)
1072 match_positions[j] = match_positions[j - 1] + 1;
1073
1074 /* And fixup the rest. */
1075 for (; next < count; next++)
1076 match_positions[next] += delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001077 }
1078
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001079 buffer->move_to (end);
1080
Behdad Esfahbod26166892012-10-29 21:51:56 -07001081 return TRACE_RETURN (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001082}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001083
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001084
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001085
1086/* Contextual lookups */
1087
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001088struct ContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001089{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001090 ContextClosureFuncs funcs;
1091 const void *intersects_data;
1092};
1093
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001094struct ContextCollectGlyphsLookupContext
1095{
1096 ContextCollectGlyphsFuncs funcs;
1097 const void *collect_data;
1098};
1099
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001100struct ContextApplyLookupContext
1101{
1102 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001103 const void *match_data;
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001104};
1105
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001106static inline void context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001107 unsigned int inputCount, /* Including the first glyph (not matched) */
1108 const USHORT input[], /* Array of input values--start with second glyph */
1109 unsigned int lookupCount,
1110 const LookupRecord lookupRecord[],
1111 ContextClosureLookupContext &lookup_context)
1112{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001113 if (intersects_array (c,
1114 inputCount ? inputCount - 1 : 0, input,
1115 lookup_context.funcs.intersects, lookup_context.intersects_data))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001116 recurse_lookups (c,
1117 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001118}
1119
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001120static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1121 unsigned int inputCount, /* Including the first glyph (not matched) */
1122 const USHORT input[], /* Array of input values--start with second glyph */
1123 unsigned int lookupCount,
1124 const LookupRecord lookupRecord[],
1125 ContextCollectGlyphsLookupContext &lookup_context)
1126{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001127 collect_array (c, c->input,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001128 inputCount ? inputCount - 1 : 0, input,
1129 lookup_context.funcs.collect, lookup_context.collect_data);
1130 recurse_lookups (c,
1131 lookupCount, lookupRecord);
1132}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001133
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001134static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
1135 unsigned int inputCount, /* Including the first glyph (not matched) */
1136 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001137 unsigned int lookupCount HB_UNUSED,
1138 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001139 ContextApplyLookupContext &lookup_context)
1140{
1141 return would_match_input (c,
1142 inputCount, input,
1143 lookup_context.funcs.match, lookup_context.match_data);
1144}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001145static inline bool context_apply_lookup (hb_apply_context_t *c,
1146 unsigned int inputCount, /* Including the first glyph (not matched) */
1147 const USHORT input[], /* Array of input values--start with second glyph */
1148 unsigned int lookupCount,
1149 const LookupRecord lookupRecord[],
1150 ContextApplyLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001151{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001152 unsigned int match_length = 0;
1153 unsigned int match_positions[MAX_CONTEXT_LENGTH];
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001154 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001155 inputCount, input,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001156 lookup_context.funcs.match, lookup_context.match_data,
1157 &match_length, match_positions)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001158 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001159 inputCount, match_positions,
1160 lookupCount, lookupRecord,
1161 match_length);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001162}
1163
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001164struct Rule
1165{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001166 inline bool is_inplace (hb_is_inplace_context_t *c) const
1167 {
1168 TRACE_IS_INPLACE (this);
1169 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
1170 unsigned int count = lookupCount;
1171 for (unsigned int i = 0; i < count; i++)
1172 if (!c->recurse (lookupRecord[i].lookupListIndex))
1173 return TRACE_RETURN (false);
1174 return TRACE_RETURN (true);
1175 }
1176
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001177 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001178 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001179 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001180 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001181 context_closure_lookup (c,
1182 inputCount, input,
1183 lookupCount, lookupRecord,
1184 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001185 }
1186
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001187 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1188 {
1189 TRACE_COLLECT_GLYPHS (this);
1190 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
1191 context_collect_glyphs_lookup (c,
1192 inputCount, input,
1193 lookupCount, lookupRecord,
1194 lookup_context);
1195 }
1196
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001197 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1198 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001199 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001200 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
1201 return TRACE_RETURN (context_would_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
1202 }
1203
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001204 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001205 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001206 TRACE_APPLY (this);
Behdad Esfahbode45d3f82010-05-06 19:33:31 -04001207 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001208 return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001209 }
1210
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001211 public:
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001212 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001213 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001214 return inputCount.sanitize (c)
1215 && lookupCount.sanitize (c)
1216 && c->check_range (input,
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001217 input[0].static_size * inputCount
1218 + lookupRecordX[0].static_size * lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001219 }
1220
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001221 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001222 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001223 * glyph sequence--includes the first
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001224 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001225 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001226 USHORT input[VAR]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001227 * second glyph */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001228 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001229 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001230 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001231 DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001232};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001233
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001234struct RuleSet
1235{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001236 inline bool is_inplace (hb_is_inplace_context_t *c) const
1237 {
1238 TRACE_IS_INPLACE (this);
1239 unsigned int num_rules = rule.len;
1240 for (unsigned int i = 0; i < num_rules; i++)
1241 if (!(this+rule[i]).is_inplace (c))
1242 return TRACE_RETURN (false);
1243 return TRACE_RETURN (true);
1244 }
1245
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001246 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001247 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001248 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001249 unsigned int num_rules = rule.len;
1250 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001251 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001252 }
1253
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001254 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1255 {
1256 TRACE_COLLECT_GLYPHS (this);
1257 unsigned int num_rules = rule.len;
1258 for (unsigned int i = 0; i < num_rules; i++)
1259 (this+rule[i]).collect_glyphs (c, lookup_context);
1260 }
1261
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001262 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1263 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001264 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001265 unsigned int num_rules = rule.len;
1266 for (unsigned int i = 0; i < num_rules; i++)
1267 {
1268 if ((this+rule[i]).would_apply (c, lookup_context))
1269 return TRACE_RETURN (true);
1270 }
1271 return TRACE_RETURN (false);
1272 }
1273
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001274 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001275 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001276 TRACE_APPLY (this);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001277 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001278 for (unsigned int i = 0; i < num_rules; i++)
1279 {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001280 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001281 return TRACE_RETURN (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001282 }
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001283 return TRACE_RETURN (false);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001284 }
1285
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001286 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001287 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001288 return TRACE_RETURN (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001289 }
1290
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001291 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001292 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001293 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001294 * ordered by preference */
Behdad Esfahboded074222010-05-10 18:08:46 -04001295 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001296 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001297};
1298
1299
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001300struct ContextFormat1
1301{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001302 inline bool is_inplace (hb_is_inplace_context_t *c) const
1303 {
1304 TRACE_IS_INPLACE (this);
1305 unsigned int count = ruleSet.len;
1306 for (unsigned int i = 0; i < count; i++)
1307 if (!(this+ruleSet[i]).is_inplace (c))
1308 return TRACE_RETURN (false);
1309 return TRACE_RETURN (true);
1310 }
1311
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001312 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001313 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001314 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001315
1316 const Coverage &cov = (this+coverage);
1317
1318 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001319 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001320 NULL
1321 };
1322
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001323 unsigned int count = ruleSet.len;
1324 for (unsigned int i = 0; i < count; i++)
1325 if (cov.intersects_coverage (c->glyphs, i)) {
1326 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001327 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001328 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001329 }
1330
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001331 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1332 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001333 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001334 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001335
1336 struct ContextCollectGlyphsLookupContext lookup_context = {
1337 {collect_glyph},
1338 NULL
1339 };
1340
1341 unsigned int count = ruleSet.len;
1342 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001343 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001344 }
1345
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001346 inline bool would_apply (hb_would_apply_context_t *c) const
1347 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001348 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001349
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001350 const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001351 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001352 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001353 NULL
1354 };
1355 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1356 }
1357
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001358 inline const Coverage &get_coverage (void) const
1359 {
1360 return this+coverage;
1361 }
1362
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001363 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001364 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001365 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001366 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -04001367 if (likely (index == NOT_COVERED))
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001368 return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001369
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001370 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001371 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001372 {match_glyph},
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001373 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001374 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001375 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001376 }
1377
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001378 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001379 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001380 return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001381 }
1382
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001383 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001384 USHORT format; /* Format identifier--format = 1 */
1385 OffsetTo<Coverage>
1386 coverage; /* Offset to Coverage table--from
1387 * beginning of table */
1388 OffsetArrayOf<RuleSet>
1389 ruleSet; /* Array of RuleSet tables
1390 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001391 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001392 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001393};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001394
1395
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001396struct ContextFormat2
1397{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001398 inline bool is_inplace (hb_is_inplace_context_t *c) const
1399 {
1400 TRACE_IS_INPLACE (this);
1401 unsigned int count = ruleSet.len;
1402 for (unsigned int i = 0; i < count; i++)
1403 if (!(this+ruleSet[i]).is_inplace (c))
1404 return TRACE_RETURN (false);
1405 return TRACE_RETURN (true);
1406 }
1407
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001408 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001409 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001410 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001411 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001412 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001413
1414 const ClassDef &class_def = this+classDef;
1415
1416 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001417 {intersects_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001418 &class_def
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001419 };
1420
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001421 unsigned int count = ruleSet.len;
1422 for (unsigned int i = 0; i < count; i++)
1423 if (class_def.intersects_class (c->glyphs, i)) {
1424 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001425 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001426 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001427 }
1428
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001429 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1430 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001431 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001432 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001433
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001434 const ClassDef &class_def = this+classDef;
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001435 struct ContextCollectGlyphsLookupContext lookup_context = {
1436 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001437 &class_def
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001438 };
1439
1440 unsigned int count = ruleSet.len;
1441 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001442 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001443 }
1444
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001445 inline bool would_apply (hb_would_apply_context_t *c) const
1446 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001447 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001448
1449 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001450 unsigned int index = class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001451 const RuleSet &rule_set = this+ruleSet[index];
1452 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001453 {match_class},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001454 &class_def
1455 };
1456 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1457 }
1458
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001459 inline const Coverage &get_coverage (void) const
1460 {
1461 return this+coverage;
1462 }
1463
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001464 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001465 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001466 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001467 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001468 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001469
1470 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001471 index = class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001472 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001473 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001474 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001475 &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001476 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001477 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001478 }
1479
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001480 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001481 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001482 return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001483 }
1484
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001485 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001486 USHORT format; /* Format identifier--format = 2 */
1487 OffsetTo<Coverage>
1488 coverage; /* Offset to Coverage table--from
1489 * beginning of table */
1490 OffsetTo<ClassDef>
1491 classDef; /* Offset to glyph ClassDef table--from
1492 * beginning of table */
1493 OffsetArrayOf<RuleSet>
1494 ruleSet; /* Array of RuleSet tables
1495 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001496 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001497 DEFINE_SIZE_ARRAY (8, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001498};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001499
1500
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001501struct ContextFormat3
1502{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001503 inline bool is_inplace (hb_is_inplace_context_t *c) const
1504 {
1505 TRACE_IS_INPLACE (this);
1506 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1507 unsigned int count = lookupCount;
1508 for (unsigned int i = 0; i < count; i++)
1509 if (!c->recurse (lookupRecord[i].lookupListIndex))
1510 return TRACE_RETURN (false);
1511 return TRACE_RETURN (true);
1512 }
1513
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001514 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001515 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001516 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001517 if (!(this+coverage[0]).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001518 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001519
1520 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1521 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001522 {intersects_coverage},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001523 this
1524 };
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001525 context_closure_lookup (c,
1526 glyphCount, (const USHORT *) (coverage + 1),
1527 lookupCount, lookupRecord,
1528 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001529 }
1530
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001531 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1532 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001533 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001534 (this+coverage[0]).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001535
1536 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1537 struct ContextCollectGlyphsLookupContext lookup_context = {
1538 {collect_coverage},
Behdad Esfahbode75943d2012-11-30 08:38:24 +02001539 this
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001540 };
1541
1542 context_collect_glyphs_lookup (c,
1543 glyphCount, (const USHORT *) (coverage + 1),
1544 lookupCount, lookupRecord,
1545 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001546 }
1547
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001548 inline bool would_apply (hb_would_apply_context_t *c) const
1549 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001550 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001551
1552 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1553 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001554 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001555 this
1556 };
1557 return TRACE_RETURN (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
1558 }
1559
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001560 inline const Coverage &get_coverage (void) const
1561 {
1562 return this+coverage[0];
1563 }
1564
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001565 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001566 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001567 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001568 unsigned int index = (this+coverage[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001569 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001570
Behdad Esfahbode45d3f82010-05-06 19:33:31 -04001571 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001572 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001573 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001574 this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001575 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001576 return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001577 }
1578
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001579 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001580 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001581 if (!c->check_struct (this)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001582 unsigned int count = glyphCount;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001583 if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001584 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001585 if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode45d3f82010-05-06 19:33:31 -04001586 LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001587 return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001588 }
1589
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001590 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001591 USHORT format; /* Format identifier--format = 3 */
1592 USHORT glyphCount; /* Number of glyphs in the input glyph
1593 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001594 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001595 OffsetTo<Coverage>
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001596 coverage[VAR]; /* Array of offsets to Coverage
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001597 * table in glyph sequence order */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001598 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001599 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001600 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001601 DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001602};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001603
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001604struct Context
1605{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001606 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001607 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001608 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001609 TRACE_DISPATCH (this);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001610 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001611 case 1: return TRACE_RETURN (c->dispatch (u.format1));
1612 case 2: return TRACE_RETURN (c->dispatch (u.format2));
1613 case 3: return TRACE_RETURN (c->dispatch (u.format3));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001614 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001615 }
1616 }
1617
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001618 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001619 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001620 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001621 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001622 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1623 case 2: return TRACE_RETURN (u.format2.sanitize (c));
1624 case 3: return TRACE_RETURN (u.format3.sanitize (c));
1625 default:return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001626 }
1627 }
1628
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001629 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001630 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -04001631 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001632 ContextFormat1 format1;
1633 ContextFormat2 format2;
1634 ContextFormat3 format3;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001635 } u;
1636};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001637
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001638
1639/* Chaining Contextual lookups */
1640
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001641struct ChainContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001642{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001643 ContextClosureFuncs funcs;
1644 const void *intersects_data[3];
1645};
1646
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001647struct ChainContextCollectGlyphsLookupContext
1648{
1649 ContextCollectGlyphsFuncs funcs;
1650 const void *collect_data[3];
1651};
1652
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001653struct ChainContextApplyLookupContext
1654{
1655 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001656 const void *match_data[3];
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001657};
1658
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001659static inline void chain_context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001660 unsigned int backtrackCount,
1661 const USHORT backtrack[],
1662 unsigned int inputCount, /* Including the first glyph (not matched) */
1663 const USHORT input[], /* Array of input values--start with second glyph */
1664 unsigned int lookaheadCount,
1665 const USHORT lookahead[],
1666 unsigned int lookupCount,
1667 const LookupRecord lookupRecord[],
1668 ChainContextClosureLookupContext &lookup_context)
1669{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001670 if (intersects_array (c,
1671 backtrackCount, backtrack,
1672 lookup_context.funcs.intersects, lookup_context.intersects_data[0])
1673 && intersects_array (c,
1674 inputCount ? inputCount - 1 : 0, input,
1675 lookup_context.funcs.intersects, lookup_context.intersects_data[1])
Behdad Esfahbod74439d02013-07-22 19:02:29 -04001676 && intersects_array (c,
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001677 lookaheadCount, lookahead,
1678 lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001679 recurse_lookups (c,
1680 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001681}
1682
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001683static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1684 unsigned int backtrackCount,
1685 const USHORT backtrack[],
1686 unsigned int inputCount, /* Including the first glyph (not matched) */
1687 const USHORT input[], /* Array of input values--start with second glyph */
1688 unsigned int lookaheadCount,
1689 const USHORT lookahead[],
1690 unsigned int lookupCount,
1691 const LookupRecord lookupRecord[],
1692 ChainContextCollectGlyphsLookupContext &lookup_context)
1693{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001694 collect_array (c, c->before,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001695 backtrackCount, backtrack,
1696 lookup_context.funcs.collect, lookup_context.collect_data[0]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001697 collect_array (c, c->input,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001698 inputCount ? inputCount - 1 : 0, input,
1699 lookup_context.funcs.collect, lookup_context.collect_data[1]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001700 collect_array (c, c->after,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001701 lookaheadCount, lookahead,
1702 lookup_context.funcs.collect, lookup_context.collect_data[2]);
1703 recurse_lookups (c,
1704 lookupCount, lookupRecord);
1705}
1706
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001707static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
1708 unsigned int backtrackCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001709 const USHORT backtrack[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001710 unsigned int inputCount, /* Including the first glyph (not matched) */
1711 const USHORT input[], /* Array of input values--start with second glyph */
1712 unsigned int lookaheadCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001713 const USHORT lookahead[] HB_UNUSED,
1714 unsigned int lookupCount HB_UNUSED,
1715 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001716 ChainContextApplyLookupContext &lookup_context)
1717{
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -04001718 return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
Behdad Esfahbod1f2bb172012-08-23 16:10:37 -04001719 && would_match_input (c,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001720 inputCount, input,
1721 lookup_context.funcs.match, lookup_context.match_data[1]);
1722}
1723
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001724static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
1725 unsigned int backtrackCount,
1726 const USHORT backtrack[],
1727 unsigned int inputCount, /* Including the first glyph (not matched) */
1728 const USHORT input[], /* Array of input values--start with second glyph */
1729 unsigned int lookaheadCount,
1730 const USHORT lookahead[],
1731 unsigned int lookupCount,
1732 const LookupRecord lookupRecord[],
1733 ChainContextApplyLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001734{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001735 unsigned int match_length = 0;
1736 unsigned int match_positions[MAX_CONTEXT_LENGTH];
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001737 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001738 inputCount, input,
1739 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001740 &match_length, match_positions)
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001741 && match_backtrack (c,
1742 backtrackCount, backtrack,
1743 lookup_context.funcs.match, lookup_context.match_data[0])
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001744 && match_lookahead (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001745 lookaheadCount, lookahead,
1746 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001747 match_length)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001748 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001749 inputCount, match_positions,
1750 lookupCount, lookupRecord,
1751 match_length);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001752}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001753
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001754struct ChainRule
1755{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001756 inline bool is_inplace (hb_is_inplace_context_t *c) const
1757 {
1758 TRACE_IS_INPLACE (this);
1759 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1760 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1761 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1762 unsigned int count = lookup.len;
1763 for (unsigned int i = 0; i < count; i++)
1764 if (!c->recurse (lookup.array[i].lookupListIndex))
1765 return TRACE_RETURN (false);
1766 return TRACE_RETURN (true);
1767 }
1768
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001769 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001770 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001771 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001772 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1773 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1774 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001775 chain_context_closure_lookup (c,
1776 backtrack.len, backtrack.array,
1777 input.len, input.array,
1778 lookahead.len, lookahead.array,
1779 lookup.len, lookup.array,
1780 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001781 }
1782
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001783 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1784 {
1785 TRACE_COLLECT_GLYPHS (this);
1786 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1787 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1788 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1789 chain_context_collect_glyphs_lookup (c,
1790 backtrack.len, backtrack.array,
1791 input.len, input.array,
1792 lookahead.len, lookahead.array,
1793 lookup.len, lookup.array,
1794 lookup_context);
1795 }
1796
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001797 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1798 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001799 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001800 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1801 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1802 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1803 return TRACE_RETURN (chain_context_would_apply_lookup (c,
1804 backtrack.len, backtrack.array,
1805 input.len, input.array,
1806 lookahead.len, lookahead.array, lookup.len,
1807 lookup.array, lookup_context));
1808 }
1809
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001810 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001811 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001812 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001813 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1814 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1815 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001816 return TRACE_RETURN (chain_context_apply_lookup (c,
1817 backtrack.len, backtrack.array,
1818 input.len, input.array,
1819 lookahead.len, lookahead.array, lookup.len,
1820 lookup.array, lookup_context));
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001821 }
1822
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001823 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001824 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001825 if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001826 HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001827 if (!input.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001828 ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001829 if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001830 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001831 return TRACE_RETURN (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001832 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001833
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001834 protected:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001835 ArrayOf<USHORT>
1836 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001837 * (to be matched before the input
1838 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -04001839 HeadlessArrayOf<USHORT>
1840 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001841 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001842 ArrayOf<USHORT>
1843 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001844 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001845 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001846 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001847 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001848 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04001849 DEFINE_SIZE_MIN (8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001850};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001851
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001852struct ChainRuleSet
1853{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001854 inline bool is_inplace (hb_is_inplace_context_t *c) const
1855 {
1856 TRACE_IS_INPLACE (this);
1857 unsigned int num_rules = rule.len;
1858 for (unsigned int i = 0; i < num_rules; i++)
1859 if (!(this+rule[i]).is_inplace (c))
1860 return TRACE_RETURN (false);
1861 return TRACE_RETURN (true);
1862 }
1863
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001864 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001865 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001866 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001867 unsigned int num_rules = rule.len;
1868 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001869 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001870 }
1871
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001872 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1873 {
1874 TRACE_COLLECT_GLYPHS (this);
1875 unsigned int num_rules = rule.len;
1876 for (unsigned int i = 0; i < num_rules; i++)
1877 (this+rule[i]).collect_glyphs (c, lookup_context);
1878 }
1879
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001880 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1881 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001882 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001883 unsigned int num_rules = rule.len;
1884 for (unsigned int i = 0; i < num_rules; i++)
1885 if ((this+rule[i]).would_apply (c, lookup_context))
1886 return TRACE_RETURN (true);
1887
1888 return TRACE_RETURN (false);
1889 }
1890
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001891 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001892 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001893 TRACE_APPLY (this);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001894 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001895 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001896 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001897 return TRACE_RETURN (true);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001898
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001899 return TRACE_RETURN (false);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001900 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001901
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001902 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001903 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001904 return TRACE_RETURN (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001905 }
1906
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001907 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001908 OffsetArrayOf<ChainRule>
1909 rule; /* Array of ChainRule tables
1910 * ordered by preference */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001911 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001912 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001913};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001914
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001915struct ChainContextFormat1
1916{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001917 inline bool is_inplace (hb_is_inplace_context_t *c) const
1918 {
1919 TRACE_IS_INPLACE (this);
1920 unsigned int count = ruleSet.len;
1921 for (unsigned int i = 0; i < count; i++)
1922 if (!(this+ruleSet[i]).is_inplace (c))
1923 return TRACE_RETURN (false);
1924 return TRACE_RETURN (true);
1925 }
1926
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001927 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001928 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001929 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001930 const Coverage &cov = (this+coverage);
1931
1932 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001933 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001934 {NULL, NULL, NULL}
1935 };
1936
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001937 unsigned int count = ruleSet.len;
1938 for (unsigned int i = 0; i < count; i++)
1939 if (cov.intersects_coverage (c->glyphs, i)) {
1940 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001941 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001942 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001943 }
1944
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001945 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1946 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001947 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001948 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001949
1950 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1951 {collect_glyph},
1952 {NULL, NULL, NULL}
1953 };
1954
1955 unsigned int count = ruleSet.len;
1956 for (unsigned int i = 0; i < count; i++)
1957 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001958 }
1959
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001960 inline bool would_apply (hb_would_apply_context_t *c) const
1961 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001962 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001963
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001964 const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001965 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001966 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001967 {NULL, NULL, NULL}
1968 };
1969 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1970 }
1971
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001972 inline const Coverage &get_coverage (void) const
1973 {
1974 return this+coverage;
1975 }
1976
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001977 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001978 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001979 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001980 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001981 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001982
1983 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001984 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001985 {match_glyph},
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001986 {NULL, NULL, NULL}
1987 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001988 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001989 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001990
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001991 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001992 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001993 return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001994 }
1995
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001996 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001997 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001998 OffsetTo<Coverage>
1999 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002000 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002001 OffsetArrayOf<ChainRuleSet>
2002 ruleSet; /* Array of ChainRuleSet tables
2003 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002004 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04002005 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002006};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002007
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002008struct ChainContextFormat2
2009{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04002010 inline bool is_inplace (hb_is_inplace_context_t *c) const
2011 {
2012 TRACE_IS_INPLACE (this);
2013 unsigned int count = ruleSet.len;
2014 for (unsigned int i = 0; i < count; i++)
2015 if (!(this+ruleSet[i]).is_inplace (c))
2016 return TRACE_RETURN (false);
2017 return TRACE_RETURN (true);
2018 }
2019
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002020 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002021 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002022 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002023 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002024 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002025
2026 const ClassDef &backtrack_class_def = this+backtrackClassDef;
2027 const ClassDef &input_class_def = this+inputClassDef;
2028 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
2029
2030 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002031 {intersects_class},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002032 {&backtrack_class_def,
2033 &input_class_def,
2034 &lookahead_class_def}
2035 };
2036
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002037 unsigned int count = ruleSet.len;
2038 for (unsigned int i = 0; i < count; i++)
2039 if (input_class_def.intersects_class (c->glyphs, i)) {
2040 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002041 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002042 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002043 }
2044
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002045 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
2046 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002047 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05002048 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002049
Behdad Esfahbod11fba792013-01-02 23:36:37 -06002050 const ClassDef &backtrack_class_def = this+backtrackClassDef;
2051 const ClassDef &input_class_def = this+inputClassDef;
2052 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
2053
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002054 struct ChainContextCollectGlyphsLookupContext lookup_context = {
2055 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06002056 {&backtrack_class_def,
2057 &input_class_def,
2058 &lookahead_class_def}
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002059 };
2060
2061 unsigned int count = ruleSet.len;
2062 for (unsigned int i = 0; i < count; i++)
2063 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002064 }
2065
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002066 inline bool would_apply (hb_would_apply_context_t *c) const
2067 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002068 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002069
Behdad Esfahbod11fba792013-01-02 23:36:37 -06002070 const ClassDef &backtrack_class_def = this+backtrackClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002071 const ClassDef &input_class_def = this+inputClassDef;
Behdad Esfahbod11fba792013-01-02 23:36:37 -06002072 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002073
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05002074 unsigned int index = input_class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002075 const ChainRuleSet &rule_set = this+ruleSet[index];
2076 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002077 {match_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06002078 {&backtrack_class_def,
2079 &input_class_def,
2080 &lookahead_class_def}
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002081 };
2082 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
2083 }
2084
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002085 inline const Coverage &get_coverage (void) const
2086 {
2087 return this+coverage;
2088 }
2089
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002090 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002091 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002092 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05002093 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002094 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002095
2096 const ClassDef &backtrack_class_def = this+backtrackClassDef;
2097 const ClassDef &input_class_def = this+inputClassDef;
2098 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
2099
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05002100 index = input_class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002101 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002102 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002103 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04002104 {&backtrack_class_def,
2105 &input_class_def,
2106 &lookahead_class_def}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002107 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002108 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002109 }
2110
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002111 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002112 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002113 return TRACE_RETURN (coverage.sanitize (c, this) && backtrackClassDef.sanitize (c, this) &&
2114 inputClassDef.sanitize (c, this) && lookaheadClassDef.sanitize (c, this) &&
2115 ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002116 }
2117
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002118 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002119 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002120 OffsetTo<Coverage>
2121 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002122 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002123 OffsetTo<ClassDef>
2124 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002125 * containing backtrack sequence
2126 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002127 OffsetTo<ClassDef>
2128 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002129 * table containing input sequence
2130 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002131 OffsetTo<ClassDef>
2132 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002133 * containing lookahead sequence
2134 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002135 OffsetArrayOf<ChainRuleSet>
2136 ruleSet; /* Array of ChainRuleSet tables
2137 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002138 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04002139 DEFINE_SIZE_ARRAY (12, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002140};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002141
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002142struct ChainContextFormat3
2143{
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04002144 inline bool is_inplace (hb_is_inplace_context_t *c) const
2145 {
2146 TRACE_IS_INPLACE (this);
2147 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2148 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2149 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2150
2151 unsigned int count = lookup.len;
2152 for (unsigned int i = 0; i < count; i++)
2153 if (!c->recurse (lookup.array[i].lookupListIndex))
2154 return TRACE_RETURN (false);
2155 return TRACE_RETURN (true);
2156 }
2157
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002158 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002159 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002160 TRACE_CLOSURE (this);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002161 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2162
2163 if (!(this+input[0]).intersects (c->glyphs))
2164 return;
2165
2166 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2167 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2168 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002169 {intersects_coverage},
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002170 {this, this, this}
2171 };
2172 chain_context_closure_lookup (c,
2173 backtrack.len, (const USHORT *) backtrack.array,
2174 input.len, (const USHORT *) input.array + 1,
2175 lookahead.len, (const USHORT *) lookahead.array,
2176 lookup.len, lookup.array,
2177 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002178 }
2179
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002180 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
2181 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002182 TRACE_COLLECT_GLYPHS (this);
2183 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2184
Behdad Esfahbod83035932012-12-04 17:08:41 -05002185 (this+input[0]).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002186
2187 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2188 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2189 struct ChainContextCollectGlyphsLookupContext lookup_context = {
2190 {collect_coverage},
2191 {this, this, this}
2192 };
2193 chain_context_collect_glyphs_lookup (c,
2194 backtrack.len, (const USHORT *) backtrack.array,
2195 input.len, (const USHORT *) input.array + 1,
2196 lookahead.len, (const USHORT *) lookahead.array,
2197 lookup.len, lookup.array,
2198 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002199 }
2200
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002201 inline bool would_apply (hb_would_apply_context_t *c) const
2202 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002203 TRACE_WOULD_APPLY (this);
Behdad Esfahbode6f74792012-07-28 18:34:58 -04002204
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002205 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002206 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2207 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2208 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002209 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002210 {this, this, this}
2211 };
2212 return TRACE_RETURN (chain_context_would_apply_lookup (c,
2213 backtrack.len, (const USHORT *) backtrack.array,
2214 input.len, (const USHORT *) input.array + 1,
2215 lookahead.len, (const USHORT *) lookahead.array,
2216 lookup.len, lookup.array, lookup_context));
2217 }
2218
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002219 inline const Coverage &get_coverage (void) const
2220 {
2221 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2222 return this+input[0];
2223 }
2224
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002225 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002226 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002227 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002228 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002229
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05002230 unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002231 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002232
Behdad Esfahbode961c862010-04-21 15:56:11 -04002233 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2234 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002235 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002236 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04002237 {this, this, this}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002238 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002239 return TRACE_RETURN (chain_context_apply_lookup (c,
2240 backtrack.len, (const USHORT *) backtrack.array,
2241 input.len, (const USHORT *) input.array + 1,
2242 lookahead.len, (const USHORT *) lookahead.array,
2243 lookup.len, lookup.array, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002244 }
2245
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002246 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002247 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002248 if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002249 OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002250 if (!input.sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002251 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002252 if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002253 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002254 return TRACE_RETURN (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002255 }
2256
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002257 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002258 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002259 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002260 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002261 * in backtracking sequence, in glyph
2262 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002263 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002264 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002265 * tables in input sequence, in glyph
2266 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002267 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002268 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002269 * in lookahead sequence, in glyph
2270 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002271 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002272 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002273 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002274 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04002275 DEFINE_SIZE_MIN (10);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002276};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002277
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002278struct ChainContext
2279{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002280 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002281 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002282 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002283 TRACE_DISPATCH (this);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002284 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002285 case 1: return TRACE_RETURN (c->dispatch (u.format1));
2286 case 2: return TRACE_RETURN (c->dispatch (u.format2));
2287 case 3: return TRACE_RETURN (c->dispatch (u.format3));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05002288 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002289 }
2290 }
2291
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002292 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002293 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002294 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002295 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002296 case 1: return TRACE_RETURN (u.format1.sanitize (c));
2297 case 2: return TRACE_RETURN (u.format2.sanitize (c));
2298 case 3: return TRACE_RETURN (u.format3.sanitize (c));
2299 default:return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002300 }
2301 }
2302
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002303 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002304 union {
2305 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002306 ChainContextFormat1 format1;
2307 ChainContextFormat2 format2;
2308 ChainContextFormat3 format3;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002309 } u;
2310};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002311
2312
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002313struct ExtensionFormat1
2314{
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002315 inline unsigned int get_type (void) const { return extensionLookupType; }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -04002316 inline unsigned int get_offset (void) const { return extensionOffset; }
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002317
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002318 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002319 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002320 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002321 }
2322
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002323 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002324 USHORT format; /* Format identifier. Set to 1. */
2325 USHORT extensionLookupType; /* Lookup type of subtable referenced
2326 * by ExtensionOffset (i.e. the
2327 * extension subtable). */
Behdad Esfahbod81f2af42010-04-22 00:58:49 -04002328 ULONG extensionOffset; /* Offset to the extension subtable,
2329 * of lookup type subtable. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002330 public:
2331 DEFINE_SIZE_STATIC (8);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002332};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002333
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002334template <typename T>
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002335struct Extension
2336{
2337 inline unsigned int get_type (void) const
2338 {
2339 switch (u.format) {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002340 case 1: return u.format1.get_type ();
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002341 default:return 0;
2342 }
2343 }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -04002344 inline unsigned int get_offset (void) const
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002345 {
2346 switch (u.format) {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002347 case 1: return u.format1.get_offset ();
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -04002348 default:return 0;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002349 }
2350 }
2351
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05002352 template <typename X>
2353 inline const X& get_subtable (void) const
2354 {
2355 unsigned int offset = get_offset ();
2356 if (unlikely (!offset)) return Null(typename T::LookupSubTable);
2357 return StructAtOffset<typename T::LookupSubTable> (this, offset);
2358 }
2359
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002360 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002361 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002362 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002363 return get_subtable<typename T::LookupSubTable> ().dispatch (c, get_type ());
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002364 }
2365
Behdad Esfahboded2e1352012-11-23 17:10:40 -05002366 inline bool sanitize_self (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002367 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002368 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002369 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002370 case 1: return TRACE_RETURN (u.format1.sanitize (c));
2371 default:return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002372 }
2373 }
2374
Behdad Esfahboded2e1352012-11-23 17:10:40 -05002375 inline bool sanitize (hb_sanitize_context_t *c) {
2376 TRACE_SANITIZE (this);
2377 if (!sanitize_self (c)) return TRACE_RETURN (false);
2378 unsigned int offset = get_offset ();
2379 if (unlikely (!offset)) return TRACE_RETURN (true);
2380 return TRACE_RETURN (StructAtOffset<typename T::LookupSubTable> (this, offset).sanitize (c, get_type ()));
2381 }
2382
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002383 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002384 union {
2385 USHORT format; /* Format identifier */
Behdad Esfahbod6d08c7f2012-07-11 18:01:27 -04002386 ExtensionFormat1 format1;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002387 } u;
2388};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002389
2390
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002391/*
2392 * GSUB/GPOS Common
2393 */
2394
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002395struct GSUBGPOS
2396{
Behdad Esfahboda328d662009-08-04 20:27:05 -04002397 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
2398 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002399
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002400 inline unsigned int get_script_count (void) const
2401 { return (this+scriptList).len; }
2402 inline const Tag& get_script_tag (unsigned int i) const
2403 { return (this+scriptList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002404 inline unsigned int get_script_tags (unsigned int start_offset,
2405 unsigned int *script_count /* IN/OUT */,
2406 hb_tag_t *script_tags /* OUT */) const
2407 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002408 inline const Script& get_script (unsigned int i) const
2409 { return (this+scriptList)[i]; }
2410 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
2411 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002412
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002413 inline unsigned int get_feature_count (void) const
2414 { return (this+featureList).len; }
2415 inline const Tag& get_feature_tag (unsigned int i) const
2416 { return (this+featureList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002417 inline unsigned int get_feature_tags (unsigned int start_offset,
2418 unsigned int *feature_count /* IN/OUT */,
2419 hb_tag_t *feature_tags /* OUT */) const
2420 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002421 inline const Feature& get_feature (unsigned int i) const
2422 { return (this+featureList)[i]; }
2423 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
2424 { return (this+featureList).find_index (tag, index); }
2425
2426 inline unsigned int get_lookup_count (void) const
2427 { return (this+lookupList).len; }
2428 inline const Lookup& get_lookup (unsigned int i) const
2429 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002430
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002431 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002432 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002433 return TRACE_RETURN (version.sanitize (c) && likely (version.major == 1) &&
2434 scriptList.sanitize (c, this) &&
2435 featureList.sanitize (c, this) &&
2436 lookupList.sanitize (c, this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -04002437 }
2438
Behdad Esfahbod212aba62009-05-24 00:50:27 -04002439 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -04002440 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002441 * to 0x00010000 */
2442 OffsetTo<ScriptList>
2443 scriptList; /* ScriptList table */
2444 OffsetTo<FeatureList>
2445 featureList; /* FeatureList table */
2446 OffsetTo<LookupList>
2447 lookupList; /* LookupList table */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002448 public:
2449 DEFINE_SIZE_STATIC (10);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002450};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04002451
Behdad Esfahbod6f20f722009-05-17 20:28:01 -04002452
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08002453} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04002454
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04002455
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -04002456#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */