blob: 0d4a10a368e85625192d9ca5f5947903704f396c [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 Esfahbod00f6a8e2014-12-12 20:36:49 -080041#define TRACE_DISPATCH(this, format) \
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, \
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -080044 "format %d", (int) format);
Behdad Esfahboda1733db2012-11-23 16:40:04 -050045
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040046#ifndef HB_DEBUG_CLOSURE
47#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
48#endif
49
Behdad Esfahbodbe218c62012-11-23 15:32:14 -050050#define TRACE_CLOSURE(this) \
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050051 hb_auto_trace_t<HB_DEBUG_CLOSURE, hb_void_t> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -050052 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -050053 "");
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040054
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040055struct hb_closure_context_t
56{
Behdad Esfahboda1733db2012-11-23 16:40:04 -050057 inline const char *get_name (void) { return "CLOSURE"; }
58 static const unsigned int max_debug_depth = HB_DEBUG_CLOSURE;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050059 typedef hb_void_t return_t;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050060 typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
61 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -050062 inline return_t dispatch (const T &obj) { obj.closure (this); return HB_VOID; }
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050063 static return_t default_return_value (void) { return HB_VOID; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -060064 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050065 return_t recurse (unsigned int lookup_index)
66 {
Behdad Esfahbod9b346772012-11-23 17:55:40 -050067 if (unlikely (nesting_level_left == 0 || !recurse_func))
Behdad Esfahbod2005fa52012-11-22 14:38:10 -050068 return default_return_value ();
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050069
70 nesting_level_left--;
71 recurse_func (this, lookup_index);
72 nesting_level_left++;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050073 return HB_VOID;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050074 }
75
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040076 hb_face_t *face;
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -040077 hb_set_t *glyphs;
Behdad Esfahbod44fc2372012-11-21 23:33:13 -050078 recurse_func_t recurse_func;
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040079 unsigned int nesting_level_left;
80 unsigned int debug_depth;
81
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040082 hb_closure_context_t (hb_face_t *face_,
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -040083 hb_set_t *glyphs_,
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040084 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
Behdad Esfahbode72b3602012-07-19 14:35:23 -040085 face (face_),
86 glyphs (glyphs_),
Behdad Esfahbod9b346772012-11-23 17:55:40 -050087 recurse_func (NULL),
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040088 nesting_level_left (nesting_level_left_),
89 debug_depth (0) {}
Behdad Esfahbod9b346772012-11-23 17:55:40 -050090
91 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040092};
93
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -040094
95
Behdad Esfahbod472f2292012-08-07 22:25:24 -040096#ifndef HB_DEBUG_WOULD_APPLY
97#define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
98#endif
99
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500100#define TRACE_WOULD_APPLY(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500101 hb_auto_trace_t<HB_DEBUG_WOULD_APPLY, bool> 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 "%d glyphs", c->len);
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400104
105struct hb_would_apply_context_t
106{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500107 inline const char *get_name (void) { return "WOULD_APPLY"; }
108 static const unsigned int max_debug_depth = HB_DEBUG_WOULD_APPLY;
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500109 typedef bool return_t;
110 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500111 inline return_t dispatch (const T &obj) { return obj.would_apply (this); }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500112 static return_t default_return_value (void) { return false; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600113 bool stop_sublookup_iteration (return_t r) const { return r; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500114
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400115 hb_face_t *face;
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400116 const hb_codepoint_t *glyphs;
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400117 unsigned int len;
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400118 bool zero_context;
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400119 unsigned int debug_depth;
120
121 hb_would_apply_context_t (hb_face_t *face_,
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400122 const hb_codepoint_t *glyphs_,
123 unsigned int len_,
Behdad Esfahbod2bd9fe32012-09-04 15:15:19 -0400124 bool zero_context_) :
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400125 face (face_),
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400126 glyphs (glyphs_),
127 len (len_),
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400128 zero_context (zero_context_),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500129 debug_depth (0) {}
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400130};
131
132
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800133
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800134#ifndef HB_DEBUG_COLLECT_GLYPHS
135#define HB_DEBUG_COLLECT_GLYPHS (HB_DEBUG+0)
136#endif
137
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500138#define TRACE_COLLECT_GLYPHS(this) \
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500139 hb_auto_trace_t<HB_DEBUG_COLLECT_GLYPHS, hb_void_t> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500140 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500141 "");
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800142
143struct hb_collect_glyphs_context_t
144{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500145 inline const char *get_name (void) { return "COLLECT_GLYPHS"; }
146 static const unsigned int max_debug_depth = HB_DEBUG_COLLECT_GLYPHS;
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500147 typedef hb_void_t return_t;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500148 typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500149 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500150 inline return_t dispatch (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500151 static return_t default_return_value (void) { return HB_VOID; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600152 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500153 return_t recurse (unsigned int lookup_index)
154 {
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500155 if (unlikely (nesting_level_left == 0 || !recurse_func))
156 return default_return_value ();
157
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -0500158 /* Note that GPOS sets recurse_func to NULL already, so it doesn't get
159 * past the previous check. For GSUB, we only want to collect the output
Behdad Esfahbod76ea5632013-05-04 16:01:20 -0400160 * glyphs in the recursion. If output is not requested, we can go home now.
161 *
162 * Note further, that the above is not exactly correct. A recursed lookup
163 * is allowed to match input that is not matched in the context, but that's
164 * not how most fonts are built. It's possible to relax that and recurse
165 * with all sets here if it proves to be an issue.
166 */
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500167
168 if (output == hb_set_get_empty ())
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500169 return HB_VOID;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500170
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700171 /* Return if new lookup was recursed to before. */
172 if (recursed_lookups.has (lookup_index))
173 return HB_VOID;
174
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500175 hb_set_t *old_before = before;
176 hb_set_t *old_input = input;
177 hb_set_t *old_after = after;
178 before = input = after = hb_set_get_empty ();
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -0500179
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500180 nesting_level_left--;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500181 recurse_func (this, lookup_index);
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500182 nesting_level_left++;
Behdad Esfahbod4a350d02012-12-04 17:13:09 -0500183
184 before = old_before;
185 input = old_input;
186 after = old_after;
187
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700188 recursed_lookups.add (lookup_index);
189
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500190 return HB_VOID;
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500191 }
192
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800193 hb_face_t *face;
Behdad Esfahbod83035932012-12-04 17:08:41 -0500194 hb_set_t *before;
195 hb_set_t *input;
196 hb_set_t *after;
197 hb_set_t *output;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500198 recurse_func_t recurse_func;
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700199 hb_set_t recursed_lookups;
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500200 unsigned int nesting_level_left;
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800201 unsigned int debug_depth;
202
203 hb_collect_glyphs_context_t (hb_face_t *face_,
204 hb_set_t *glyphs_before, /* OUT. May be NULL */
205 hb_set_t *glyphs_input, /* OUT. May be NULL */
206 hb_set_t *glyphs_after, /* OUT. May be NULL */
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500207 hb_set_t *glyphs_output, /* OUT. May be NULL */
208 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800209 face (face_),
Behdad Esfahbod83035932012-12-04 17:08:41 -0500210 before (glyphs_before ? glyphs_before : hb_set_get_empty ()),
211 input (glyphs_input ? glyphs_input : hb_set_get_empty ()),
212 after (glyphs_after ? glyphs_after : hb_set_get_empty ()),
213 output (glyphs_output ? glyphs_output : hb_set_get_empty ()),
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500214 recurse_func (NULL),
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700215 recursed_lookups (),
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500216 nesting_level_left (nesting_level_left_),
Behdad Esfahbodfde3e4a2014-10-29 11:23:08 -0700217 debug_depth (0)
218 {
219 recursed_lookups.init ();
220 }
221 ~hb_collect_glyphs_context_t (void)
222 {
223 recursed_lookups.fini ();
224 }
Behdad Esfahbod26514d52012-11-23 18:13:48 -0500225
226 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800227};
228
229
230
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500231struct hb_get_coverage_context_t
232{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500233 inline const char *get_name (void) { return "GET_COVERAGE"; }
234 static const unsigned int max_debug_depth = 0;
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500235 typedef const Coverage &return_t;
236 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500237 inline return_t dispatch (const T &obj) { return obj.get_coverage (); }
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500238 static return_t default_return_value (void) { return Null(Coverage); }
Behdad Esfahbod1d67ef92012-11-22 16:47:53 -0500239
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500240 hb_get_coverage_context_t (void) :
241 debug_depth (0) {}
242
243 unsigned int debug_depth;
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500244};
245
246
247
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400248#ifndef HB_DEBUG_APPLY
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400249#define HB_DEBUG_APPLY (HB_DEBUG+0)
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400250#endif
251
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500252#define TRACE_APPLY(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500253 hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500254 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500255 "idx %d codepoint %u", c->buffer->idx, c->buffer->cur().codepoint);
Behdad Esfahbod0535b502009-08-28 17:14:33 -0400256
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400257struct hb_apply_context_t
258{
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500259 inline const char *get_name (void) { return "APPLY"; }
260 static const unsigned int max_debug_depth = HB_DEBUG_APPLY;
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500261 typedef bool return_t;
262 typedef return_t (*recurse_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
263 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500264 inline return_t dispatch (const T &obj) { return obj.apply (this); }
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500265 static return_t default_return_value (void) { return false; }
Behdad Esfahbod7b912c12013-01-04 01:25:27 -0600266 bool stop_sublookup_iteration (return_t r) const { return r; }
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500267 return_t recurse (unsigned int lookup_index)
268 {
269 if (unlikely (nesting_level_left == 0 || !recurse_func))
270 return default_return_value ();
271
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -0500272 nesting_level_left--;
Behdad Esfahbodf18ff5a2012-11-30 08:07:06 +0200273 bool ret = recurse_func (this, lookup_index);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -0500274 nesting_level_left++;
Behdad Esfahbodf18ff5a2012-11-30 08:07:06 +0200275 return ret;
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500276 }
277
Behdad Esfahbod6880f7e2013-02-13 12:17:25 -0500278 unsigned int table_index; /* GSUB/GPOS */
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400279 hb_font_t *font;
280 hb_face_t *face;
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400281 hb_buffer_t *buffer;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400282 hb_direction_t direction;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100283 hb_mask_t lookup_mask;
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400284 bool auto_zwj;
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500285 recurse_func_t recurse_func;
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400286 unsigned int nesting_level_left;
Behdad Esfahbod8c69e652010-10-27 22:07:49 -0400287 unsigned int lookup_props;
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400288 const GDEF &gdef;
Behdad Esfahbod300c7302012-07-30 19:37:44 -0400289 bool has_glyph_classes;
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500290 unsigned int debug_depth;
Behdad Esfahbod98370e82010-10-27 17:39:01 -0400291
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400292
Behdad Esfahbod6880f7e2013-02-13 12:17:25 -0500293 hb_apply_context_t (unsigned int table_index_,
294 hb_font_t *font_,
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400295 hb_buffer_t *buffer_) :
Behdad Esfahbod6880f7e2013-02-13 12:17:25 -0500296 table_index (table_index_),
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400297 font (font_), face (font->face), buffer (buffer_),
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400298 direction (buffer_->props.direction),
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400299 lookup_mask (1),
300 auto_zwj (true),
Behdad Esfahbodec35a722012-11-22 16:05:59 -0500301 recurse_func (NULL),
Behdad Esfahbod6736f3c2012-05-13 15:21:06 +0200302 nesting_level_left (MAX_NESTING_LEVEL),
Behdad Esfahbod407fc122013-02-13 11:13:06 -0500303 lookup_props (0),
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400304 gdef (*hb_ot_layout_from_face (face)->gdef),
Behdad Esfahboda1733db2012-11-23 16:40:04 -0500305 has_glyph_classes (gdef.has_glyph_classes ()),
306 debug_depth (0) {}
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400307
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400308 inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
309 inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500310 inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
311 inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
312 inline void set_lookup (const Lookup &l) { lookup_props = l.get_props (); }
Behdad Esfahbod41ae6742012-04-11 17:11:05 -0400313
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500314 struct matcher_t
315 {
316 inline matcher_t (void) :
317 lookup_props (0),
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500318 ignore_zwnj (false),
319 ignore_zwj (false),
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500320 mask (-1),
321#define arg1(arg) (arg) /* Remove the macro to see why it's needed! */
322 syllable arg1(0),
323#undef arg1
324 match_func (NULL),
325 match_data (NULL) {};
326
327 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
328
329 inline void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500330 inline void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500331 inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
332 inline void set_mask (hb_mask_t mask_) { mask = mask_; }
333 inline void set_syllable (uint8_t syllable_) { syllable = syllable_; }
334 inline void set_match_func (match_func_t match_func_,
335 const void *match_data_)
336 { match_func = match_func_; match_data = match_data_; }
337
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500338 enum may_match_t {
339 MATCH_NO,
340 MATCH_YES,
341 MATCH_MAYBE
342 };
343
344 inline may_match_t may_match (const hb_glyph_info_t &info,
345 const USHORT *glyph_data) const
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500346 {
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500347 if (!(info.mask & mask) ||
348 (syllable && syllable != info.syllable ()))
349 return MATCH_NO;
350
351 if (match_func)
352 return match_func (info.codepoint, *glyph_data, match_data) ? MATCH_YES : MATCH_NO;
353
354 return MATCH_MAYBE;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500355 }
356
357 enum may_skip_t {
358 SKIP_NO,
359 SKIP_YES,
360 SKIP_MAYBE
361 };
362
363 inline may_skip_t
364 may_skip (const hb_apply_context_t *c,
365 const hb_glyph_info_t &info) const
366 {
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400367 if (!c->check_glyph_property (&info, lookup_props))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500368 return SKIP_YES;
369
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500370 if (unlikely (_hb_glyph_info_is_default_ignorable (&info) &&
371 (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
372 (ignore_zwj || !_hb_glyph_info_is_zwj (&info)) &&
Behdad Esfahboda1f7b282013-10-18 01:09:08 +0200373 !_hb_glyph_info_ligated (&info)))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500374 return SKIP_MAYBE;
375
376 return SKIP_NO;
377 }
378
379 protected:
380 unsigned int lookup_props;
381 bool ignore_zwnj;
Behdad Esfahbod0b454792013-02-14 10:46:52 -0500382 bool ignore_zwj;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500383 hb_mask_t mask;
384 uint8_t syllable;
385 match_func_t match_func;
386 const void *match_data;
387 };
388
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500389 struct skipping_forward_iterator_t
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500390 {
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500391 inline skipping_forward_iterator_t (hb_apply_context_t *c_,
392 unsigned int start_index_,
393 unsigned int num_items_,
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500394 bool context_match = false) :
395 idx (start_index_),
396 c (c_),
397 match_glyph_data (NULL),
398 num_items (num_items_),
399 end (c->buffer->len)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500400 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500401 matcher.set_lookup_props (c->lookup_props);
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400402 /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
403 matcher.set_ignore_zwnj (context_match || c->table_index == 1);
404 /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
405 matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500406 if (!context_match)
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500407 matcher.set_mask (c->lookup_mask);
Behdad Esfahbodcb90b1b2013-02-15 07:02:08 -0500408 matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500409 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500410 inline void set_lookup_props (unsigned int lookup_props) { matcher.set_lookup_props (lookup_props); }
411 inline void set_syllable (unsigned int syllable) { matcher.set_syllable (syllable); }
412 inline void set_match_func (matcher_t::match_func_t match_func,
413 const void *match_data,
414 const USHORT glyph_data[])
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500415 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500416 matcher.set_match_func (match_func, match_data);
417 match_glyph_data = glyph_data;
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500418 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500419
420 inline bool has_no_chance (void) const { return unlikely (num_items && idx + num_items >= end); }
421 inline void reject (void) { num_items++; match_glyph_data--; }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500422 inline bool next (void)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500423 {
Behdad Esfahbod506ffeb2012-01-18 16:07:53 -0500424 assert (num_items > 0);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500425 while (!has_no_chance ())
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500426 {
Behdad Esfahboda4a48fe2012-01-17 18:08:41 -0500427 idx++;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500428 const hb_glyph_info_t &info = c->buffer->info[idx];
429
Behdad Esfahbodff93ac82013-02-21 14:51:40 -0500430 matcher_t::may_skip_t skip = matcher.may_skip (c, info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500431 if (unlikely (skip == matcher_t::SKIP_YES))
432 continue;
433
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500434 matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500435 if (match == matcher_t::MATCH_YES ||
436 (match == matcher_t::MATCH_MAYBE &&
437 skip == matcher_t::SKIP_NO))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500438 {
439 num_items--;
440 match_glyph_data++;
441 return true;
442 }
443
444 if (skip == matcher_t::SKIP_NO)
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500445 return false;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500446 }
447 return false;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500448 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500449
450 unsigned int idx;
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400451 protected:
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500452 hb_apply_context_t *c;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500453 matcher_t matcher;
454 const USHORT *match_glyph_data;
455
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500456 unsigned int num_items;
457 unsigned int end;
458 };
459
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500460 struct skipping_backward_iterator_t
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500461 {
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500462 inline skipping_backward_iterator_t (hb_apply_context_t *c_,
463 unsigned int start_index_,
464 unsigned int num_items_,
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500465 bool context_match = false) :
466 idx (start_index_),
467 c (c_),
468 match_glyph_data (NULL),
469 num_items (num_items_)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500470 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500471 matcher.set_lookup_props (c->lookup_props);
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400472 /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
473 matcher.set_ignore_zwnj (context_match || c->table_index == 1);
474 /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
475 matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500476 if (!context_match)
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500477 matcher.set_mask (c->lookup_mask);
Behdad Esfahbodcb90b1b2013-02-15 07:02:08 -0500478 matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500479 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500480 inline void set_lookup_props (unsigned int lookup_props) { matcher.set_lookup_props (lookup_props); }
481 inline void set_syllable (unsigned int syllable) { matcher.set_syllable (syllable); }
482 inline void set_match_func (matcher_t::match_func_t match_func,
483 const void *match_data,
484 const USHORT glyph_data[])
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500485 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500486 matcher.set_match_func (match_func, match_data);
487 match_glyph_data = glyph_data;
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500488 }
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500489
490 inline bool has_no_chance (void) const { return unlikely (idx < num_items); }
491 inline void reject (void) { num_items++; }
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -0500492 inline bool prev (void)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500493 {
Behdad Esfahbod506ffeb2012-01-18 16:07:53 -0500494 assert (num_items > 0);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500495 while (!has_no_chance ())
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500496 {
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500497 idx--;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500498 const hb_glyph_info_t &info = c->buffer->out_info[idx];
499
Behdad Esfahbodff93ac82013-02-21 14:51:40 -0500500 matcher_t::may_skip_t skip = matcher.may_skip (c, info);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500501 if (unlikely (skip == matcher_t::SKIP_YES))
502 continue;
503
Behdad Esfahbod2b2a6e82013-02-21 15:07:03 -0500504 matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500505 if (match == matcher_t::MATCH_YES ||
506 (match == matcher_t::MATCH_MAYBE &&
507 skip == matcher_t::SKIP_NO))
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500508 {
509 num_items--;
510 match_glyph_data++;
511 return true;
512 }
513
514 if (skip == matcher_t::SKIP_NO)
Behdad Esfahbod722e8b82013-02-21 15:37:51 -0500515 return false;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500516 }
517 return false;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500518 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500519
520 unsigned int idx;
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400521 protected:
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500522 hb_apply_context_t *c;
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500523 matcher_t matcher;
524 const USHORT *match_glyph_data;
525
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500526 unsigned int num_items;
527 };
528
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400529 inline bool
530 match_properties_mark (hb_codepoint_t glyph,
531 unsigned int glyph_props,
532 unsigned int lookup_props) const
533 {
534 /* If using mark filtering sets, the high short of
535 * lookup_props has the set index.
536 */
537 if (lookup_props & LookupFlag::UseMarkFilteringSet)
538 return gdef.mark_set_covers (lookup_props >> 16, glyph);
539
540 /* The second byte of lookup_props has the meaning
541 * "ignore marks of attachment type different than
542 * the attachment type specified."
543 */
544 if (lookup_props & LookupFlag::MarkAttachmentType)
545 return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
546
547 return true;
548 }
549
550 inline bool
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400551 check_glyph_property (const hb_glyph_info_t *info,
552 unsigned int lookup_props) const
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400553 {
Behdad Esfahbodb98c5db2014-07-16 13:44:01 -0400554 hb_codepoint_t glyph = info->codepoint;
555 unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);
556
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400557 /* Not covered, if, for example, glyph class is ligature and
558 * lookup_props includes LookupFlags::IgnoreLigatures
559 */
560 if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
561 return false;
562
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800563 if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
Behdad Esfahbod03f67bc2012-07-30 19:47:53 -0400564 return match_properties_mark (glyph, glyph_props, lookup_props);
565
566 return true;
567 }
568
Behdad Esfahboda0161742013-10-18 00:06:30 +0200569 inline void _set_glyph_props (hb_codepoint_t glyph_index,
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100570 unsigned int class_guess = 0,
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400571 bool ligature = false,
572 bool component = false) const
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400573 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200574 unsigned int add_in = _hb_glyph_info_get_glyph_props (&buffer->cur()) &
575 HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
576 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
577 if (ligature)
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400578 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200579 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400580 /* In the only place that the MULTIPLIED bit is used, Uniscribe
581 * seems to only care about the "last" transformation between
582 * Ligature and Multiple substitions. Ie. if you ligate, expand,
583 * and ligate again, it forgives the multiplication and acts as
584 * if only ligation happened. As such, clear MULTIPLIED bit.
585 */
586 add_in &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
587 }
588 if (component)
589 add_in |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
Behdad Esfahbod2fca1422012-07-30 18:46:41 -0400590 if (likely (has_glyph_classes))
Behdad Esfahbod05ad6b52013-10-18 00:45:59 +0200591 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400592 else if (class_guess)
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200593 _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
Behdad Esfahbod60da7632012-07-16 16:13:32 -0400594 }
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500595
Behdad Esfahboda0161742013-10-18 00:06:30 +0200596 inline void replace_glyph (hb_codepoint_t glyph_index) const
Behdad Esfahbod3ec77d62012-06-08 21:44:06 -0400597 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200598 _set_glyph_props (glyph_index);
Behdad Esfahbod98370e82010-10-27 17:39:01 -0400599 buffer->replace_glyph (glyph_index);
600 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200601 inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400602 {
Behdad Esfahboda0161742013-10-18 00:06:30 +0200603 _set_glyph_props (glyph_index);
Behdad Esfahbod7fbbf862012-07-30 18:36:42 -0400604 buffer->cur().codepoint = glyph_index;
605 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200606 inline void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
607 unsigned int class_guess) const
608 {
Behdad Esfahbod09675a82013-10-18 01:05:58 +0200609 _set_glyph_props (glyph_index, class_guess, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200610 buffer->replace_glyph (glyph_index);
611 }
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400612 inline void output_glyph_for_component (hb_codepoint_t glyph_index,
613 unsigned int class_guess) const
Behdad Esfahboda0161742013-10-18 00:06:30 +0200614 {
Behdad Esfahbod832a6f92014-06-04 16:57:42 -0400615 _set_glyph_props (glyph_index, class_guess, false, true);
Behdad Esfahboda0161742013-10-18 00:06:30 +0200616 buffer->output_glyph (glyph_index);
617 }
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400618};
619
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400620
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400621
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400622typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500623typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400624typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400625
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400626struct ContextClosureFuncs
627{
628 intersects_func_t intersects;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400629};
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500630struct ContextCollectGlyphsFuncs
631{
632 collect_glyphs_func_t collect;
633};
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400634struct ContextApplyFuncs
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400635{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400636 match_func_t match;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400637};
638
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500639
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400640static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400641{
642 return glyphs->has (value);
643}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400644static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400645{
646 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
647 return class_def.intersects_class (glyphs, value);
648}
Behdad Esfahbod6a9be5b2012-04-23 22:23:17 -0400649static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400650{
651 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
652 return (data+coverage).intersects (glyphs);
653}
654
655static inline bool intersects_array (hb_closure_context_t *c,
656 unsigned int count,
657 const USHORT values[],
658 intersects_func_t intersects_func,
659 const void *intersects_data)
660{
661 for (unsigned int i = 0; i < count; i++)
662 if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
663 return false;
664 return true;
665}
666
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400667
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500668static inline void collect_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
669{
670 glyphs->add (value);
671}
672static inline void collect_class (hb_set_t *glyphs, const USHORT &value, const void *data)
673{
674 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
675 class_def.add_class (glyphs, value);
676}
677static inline void collect_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
678{
679 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
680 (data+coverage).add_coverage (glyphs);
681}
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500682static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500683 hb_set_t *glyphs,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500684 unsigned int count,
685 const USHORT values[],
686 collect_glyphs_func_t collect_func,
687 const void *collect_data)
688{
689 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -0500690 collect_func (glyphs, values[i], collect_data);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500691}
692
693
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400694static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400695{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400696 return glyph_id == value;
697}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400698static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400699{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400700 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400701 return class_def.get_class (glyph_id) == value;
702}
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400703static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400704{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -0400705 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400706 return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400707}
708
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400709static inline bool would_match_input (hb_would_apply_context_t *c,
710 unsigned int count, /* Including the first glyph (not matched) */
711 const USHORT input[], /* Array of input values--start with second glyph */
712 match_func_t match_func,
713 const void *match_data)
714{
715 if (count != c->len)
716 return false;
717
718 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod472f2292012-08-07 22:25:24 -0400719 if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400720 return false;
721
722 return true;
723}
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400724static inline bool match_input (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400725 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400726 const USHORT input[], /* Array of input values--start with second glyph */
727 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400728 const void *match_data,
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200729 unsigned int *end_offset,
730 unsigned int match_positions[MAX_CONTEXT_LENGTH],
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400731 bool *p_is_mark_ligature = NULL,
732 unsigned int *p_total_component_count = NULL)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400733{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500734 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400735
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200736 if (unlikely (count > MAX_CONTEXT_LENGTH)) TRACE_RETURN (false);
737
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200738 hb_buffer_t *buffer = c->buffer;
739
740 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, count - 1);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500741 skippy_iter.set_match_func (match_func, match_data, input);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400742 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
743
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400744 /*
745 * This is perhaps the trickiest part of OpenType... Remarks:
746 *
747 * - If all components of the ligature were marks, we call this a mark ligature.
748 *
749 * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
750 * it as a ligature glyph.
751 *
752 * - Ligatures cannot be formed across glyphs attached to different components
753 * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
754 * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
755 * However, it would be wrong to ligate that SHADDA,FATHA sequence.o
756 * There is an exception to this: If a ligature tries ligating with marks that
757 * belong to it itself, go ahead, assuming that the font designer knows what
758 * they are doing (otherwise it can break Indic stuff when a matra wants to
759 * ligate with a conjunct...)
760 */
761
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200762 bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400763
764 unsigned int total_component_count = 0;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200765 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400766
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200767 unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
768 unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400769
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200770 match_positions[0] = buffer->idx;
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500771 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400772 {
Behdad Esfahbod407fc122013-02-13 11:13:06 -0500773 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbod6cc136f2013-10-17 13:55:48 +0200774
775 match_positions[i] = skippy_iter.idx;
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400776
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200777 unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]);
778 unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400779
780 if (first_lig_id && first_lig_comp) {
781 /* If first component was attached to a previous ligature component,
782 * all subsequent components should be attached to the same ligature
783 * component, otherwise we shouldn't ligate them. */
784 if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
785 return TRACE_RETURN (false);
786 } else {
787 /* If first component was NOT attached to a previous ligature component,
788 * all subsequent components should also NOT be attached to any ligature
789 * component, unless they are attached to the first component itself! */
790 if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
791 return TRACE_RETURN (false);
792 }
793
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200794 is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200795 total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400796 }
797
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200798 *end_offset = skippy_iter.idx - buffer->idx + 1;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400799
Behdad Esfahbod191fa882012-08-28 22:58:55 -0400800 if (p_is_mark_ligature)
801 *p_is_mark_ligature = is_mark_ligature;
802
803 if (p_total_component_count)
804 *p_total_component_count = total_component_count;
805
Behdad Esfahbodbc513ad2012-10-29 19:03:55 -0700806 return TRACE_RETURN (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400807}
Behdad Esfahboda177d022012-08-28 23:18:22 -0400808static inline void ligate_input (hb_apply_context_t *c,
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200809 unsigned int count, /* Including the first glyph */
810 unsigned int match_positions[MAX_CONTEXT_LENGTH], /* Including the first glyph */
811 unsigned int match_length,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400812 hb_codepoint_t lig_glyph,
Behdad Esfahboda177d022012-08-28 23:18:22 -0400813 bool is_mark_ligature,
814 unsigned int total_component_count)
815{
Behdad Esfahbode714fe62013-10-17 13:49:51 +0200816 TRACE_APPLY (NULL);
817
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200818 hb_buffer_t *buffer = c->buffer;
819
820 buffer->merge_clusters (buffer->idx, buffer->idx + match_length);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500821
Behdad Esfahboda177d022012-08-28 23:18:22 -0400822 /*
823 * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
824 * the ligature to keep its old ligature id. This will allow it to attach to
825 * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
826 * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
827 * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature
828 * later, we don't want them to lose their ligature id/component, otherwise
829 * GPOS will fail to correctly position the mark ligature on top of the
830 * LAM,LAM,HEH ligature. See:
831 * https://bugzilla.gnome.org/show_bug.cgi?id=676343
832 *
833 * - If a ligature is formed of components that some of which are also ligatures
834 * themselves, and those ligature components had marks attached to *their*
835 * components, we have to attach the marks to the new ligature component
836 * positions! Now *that*'s tricky! And these marks may be following the
837 * last component of the whole sequence, so we should loop forward looking
838 * for them and update them.
839 *
840 * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
841 * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
842 * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature
843 * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to
844 * the new ligature with a component value of 2.
845 *
846 * This in fact happened to a font... See:
847 * https://bugzilla.gnome.org/show_bug.cgi?id=437633
848 */
849
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800850 unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200851 unsigned int lig_id = is_mark_ligature ? 0 : _hb_allocate_lig_id (buffer);
852 unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
853 unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400854 unsigned int components_so_far = last_num_components;
855
856 if (!is_mark_ligature)
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400857 {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200858 _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200859 if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100860 {
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200861 _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
Behdad Esfahbod3d436d32013-10-28 21:00:37 +0100862 _hb_glyph_info_set_modified_combining_class (&buffer->cur(), 0);
863 }
Behdad Esfahbod7e08f122013-05-27 14:48:34 -0400864 }
Behdad Esfahboda0161742013-10-18 00:06:30 +0200865 c->replace_glyph_with_ligature (lig_glyph, klass);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400866
867 for (unsigned int i = 1; i < count; i++)
868 {
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200869 while (buffer->idx < match_positions[i])
Behdad Esfahboda177d022012-08-28 23:18:22 -0400870 {
871 if (!is_mark_ligature) {
872 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200873 MIN (MAX (_hb_glyph_info_get_lig_comp (&buffer->cur()), 1u), last_num_components);
874 _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400875 }
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200876 buffer->next_glyph ();
Behdad Esfahboda177d022012-08-28 23:18:22 -0400877 }
878
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200879 last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
880 last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
Behdad Esfahboda177d022012-08-28 23:18:22 -0400881 components_so_far += last_num_components;
882
883 /* Skip the base glyph */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200884 buffer->idx++;
Behdad Esfahboda177d022012-08-28 23:18:22 -0400885 }
886
887 if (!is_mark_ligature && last_lig_id) {
888 /* Re-adjust components for any marks following. */
Behdad Esfahbod3c3df9c2013-10-17 13:58:31 +0200889 for (unsigned int i = buffer->idx; i < buffer->len; i++) {
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200890 if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
Behdad Esfahboda177d022012-08-28 23:18:22 -0400891 unsigned int new_lig_comp = components_so_far - last_num_components +
Behdad Esfahbod3ddf8922013-10-18 00:02:43 +0200892 MIN (MAX (_hb_glyph_info_get_lig_comp (&buffer->info[i]), 1u), last_num_components);
893 _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400894 } else
895 break;
896 }
897 }
Behdad Esfahbod04d894e2014-03-25 12:11:32 -0700898 TRACE_RETURN (true);
Behdad Esfahboda177d022012-08-28 23:18:22 -0400899}
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400900
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400901static inline bool match_backtrack (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400902 unsigned int count,
903 const USHORT backtrack[],
904 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400905 const void *match_data)
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400906{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500907 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400908
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500909 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500910 skippy_iter.set_match_func (match_func, match_data, backtrack);
911 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400912
Behdad Esfahbod4d3aeb82012-01-16 16:43:26 -0500913 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500914 if (!skippy_iter.prev ())
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400915 return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400916
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400917 return TRACE_RETURN (true);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400918}
919
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400920static inline bool match_lookahead (hb_apply_context_t *c,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400921 unsigned int count,
922 const USHORT lookahead[],
923 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400924 const void *match_data,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400925 unsigned int offset)
926{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500927 TRACE_APPLY (NULL);
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400928
Behdad Esfahbod9082efc2013-02-11 13:14:15 -0500929 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 -0500930 skippy_iter.set_match_func (match_func, match_data, lookahead);
931 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400932
Behdad Esfahbod370f03e2012-01-16 17:03:55 -0500933 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500934 if (!skippy_iter.next ())
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400935 return TRACE_RETURN (false);
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400936
Behdad Esfahbod93814ca2012-08-28 22:24:51 -0400937 return TRACE_RETURN (true);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400938}
939
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400940
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400941
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400942struct LookupRecord
943{
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400944 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500945 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200946 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400947 }
948
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400949 USHORT sequenceIndex; /* Index into current glyph
950 * sequence--first glyph = 0 */
951 USHORT lookupListIndex; /* Lookup to apply to that
952 * position--zero--based */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400953 public:
954 DEFINE_SIZE_STATIC (4);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400955};
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400956
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400957
Behdad Esfahbodd0a52332012-11-23 18:54:59 -0500958template <typename context_t>
959static inline void recurse_lookups (context_t *c,
960 unsigned int lookupCount,
961 const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400962{
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400963 for (unsigned int i = 0; i < lookupCount; i++)
Behdad Esfahbod86522e42013-07-22 19:07:53 -0400964 c->recurse (lookupRecord[i].lookupListIndex);
Behdad Esfahbod31081f72012-04-23 16:54:58 -0400965}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400966
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400967static inline bool apply_lookup (hb_apply_context_t *c,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400968 unsigned int count, /* Including the first glyph */
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200969 unsigned int match_positions[MAX_CONTEXT_LENGTH], /* Including the first glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400970 unsigned int lookupCount,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200971 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
972 unsigned int match_length)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400973{
Behdad Esfahbod73c18ae2012-11-23 15:34:11 -0500974 TRACE_APPLY (NULL);
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500975
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200976 hb_buffer_t *buffer = c->buffer;
977 unsigned int end;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400978
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200979 /* All positions are distance from beginning of *output* buffer.
980 * Adjust. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400981 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200982 unsigned int bl = buffer->backtrack_len ();
983 end = bl + match_length;
Behdad Esfahbod8751de52013-07-18 16:29:50 -0400984
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200985 int delta = bl - buffer->idx;
986 /* Convert positions to new indexing. */
987 for (unsigned int j = 0; j < count; j++)
988 match_positions[j] += delta;
Behdad Esfahbod8820bb22013-02-14 07:41:03 -0500989 }
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400990
Behdad Esfahbod6b65a762013-10-14 18:51:39 +0200991 for (unsigned int i = 0; i < lookupCount; i++)
992 {
993 unsigned int idx = lookupRecord[i].sequenceIndex;
994 if (idx >= count)
995 continue;
996
997 buffer->move_to (match_positions[idx]);
998
999 unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
1000 if (!c->recurse (lookupRecord[i].lookupListIndex))
1001 continue;
1002
1003 unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
1004 int delta = new_len - orig_len;
1005
1006 if (!delta)
1007 continue;
1008
1009 /* Recursed lookup changed buffer len. Adjust. */
1010
Behdad Esfahbodb6b304f2014-06-05 17:12:54 -04001011 /* end can't go back past the current match position.
1012 * Note: this is only true because we do NOT allow MultipleSubst
1013 * with zero sequence len. */
Behdad Esfahbodda720422013-10-17 12:01:50 +02001014 end = MAX ((int) match_positions[idx] + 1, int (end) + delta);
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001015
1016 unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */
1017
1018 if (delta > 0)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001019 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001020 if (unlikely (delta + count > MAX_CONTEXT_LENGTH))
1021 break;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001022 }
1023 else
1024 {
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001025 /* NOTE: delta is negative. */
1026 delta = MAX (delta, (int) next - (int) count);
1027 next -= delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001028 }
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001029
1030 /* Shift! */
1031 memmove (match_positions + next + delta, match_positions + next,
1032 (count - next) * sizeof (match_positions[0]));
1033 next += delta;
1034 count += delta;
1035
1036 /* Fill in new entries. */
1037 for (unsigned int j = idx + 1; j < next; j++)
1038 match_positions[j] = match_positions[j - 1] + 1;
1039
1040 /* And fixup the rest. */
1041 for (; next < count; next++)
1042 match_positions[next] += delta;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001043 }
1044
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001045 buffer->move_to (end);
1046
Behdad Esfahbod26166892012-10-29 21:51:56 -07001047 return TRACE_RETURN (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001048}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001049
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001050
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001051
1052/* Contextual lookups */
1053
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001054struct ContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001055{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001056 ContextClosureFuncs funcs;
1057 const void *intersects_data;
1058};
1059
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001060struct ContextCollectGlyphsLookupContext
1061{
1062 ContextCollectGlyphsFuncs funcs;
1063 const void *collect_data;
1064};
1065
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001066struct ContextApplyLookupContext
1067{
1068 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001069 const void *match_data;
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001070};
1071
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001072static inline void context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001073 unsigned int inputCount, /* Including the first glyph (not matched) */
1074 const USHORT input[], /* Array of input values--start with second glyph */
1075 unsigned int lookupCount,
1076 const LookupRecord lookupRecord[],
1077 ContextClosureLookupContext &lookup_context)
1078{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001079 if (intersects_array (c,
1080 inputCount ? inputCount - 1 : 0, input,
1081 lookup_context.funcs.intersects, lookup_context.intersects_data))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001082 recurse_lookups (c,
1083 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001084}
1085
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001086static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1087 unsigned int inputCount, /* Including the first glyph (not matched) */
1088 const USHORT input[], /* Array of input values--start with second glyph */
1089 unsigned int lookupCount,
1090 const LookupRecord lookupRecord[],
1091 ContextCollectGlyphsLookupContext &lookup_context)
1092{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001093 collect_array (c, c->input,
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001094 inputCount ? inputCount - 1 : 0, input,
1095 lookup_context.funcs.collect, lookup_context.collect_data);
1096 recurse_lookups (c,
1097 lookupCount, lookupRecord);
1098}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001099
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001100static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
1101 unsigned int inputCount, /* Including the first glyph (not matched) */
1102 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001103 unsigned int lookupCount HB_UNUSED,
1104 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001105 ContextApplyLookupContext &lookup_context)
1106{
1107 return would_match_input (c,
1108 inputCount, input,
1109 lookup_context.funcs.match, lookup_context.match_data);
1110}
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001111static inline bool context_apply_lookup (hb_apply_context_t *c,
1112 unsigned int inputCount, /* Including the first glyph (not matched) */
1113 const USHORT input[], /* Array of input values--start with second glyph */
1114 unsigned int lookupCount,
1115 const LookupRecord lookupRecord[],
1116 ContextApplyLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001117{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001118 unsigned int match_length = 0;
1119 unsigned int match_positions[MAX_CONTEXT_LENGTH];
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001120 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001121 inputCount, input,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001122 lookup_context.funcs.match, lookup_context.match_data,
1123 &match_length, match_positions)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001124 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001125 inputCount, match_positions,
1126 lookupCount, lookupRecord,
1127 match_length);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -04001128}
1129
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001130struct Rule
1131{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001132 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001133 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001134 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001135 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001136 context_closure_lookup (c,
1137 inputCount, input,
1138 lookupCount, lookupRecord,
1139 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001140 }
1141
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001142 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1143 {
1144 TRACE_COLLECT_GLYPHS (this);
1145 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
1146 context_collect_glyphs_lookup (c,
1147 inputCount, input,
1148 lookupCount, lookupRecord,
1149 lookup_context);
1150 }
1151
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001152 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1153 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001154 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001155 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
1156 return TRACE_RETURN (context_would_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
1157 }
1158
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001159 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001160 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001161 TRACE_APPLY (this);
Behdad Esfahbode45d3f82010-05-06 19:33:31 -04001162 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001163 return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001164 }
1165
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001166 public:
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001167 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001168 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001169 return inputCount.sanitize (c)
1170 && lookupCount.sanitize (c)
1171 && c->check_range (input,
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001172 input[0].static_size * inputCount
1173 + lookupRecordX[0].static_size * lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001174 }
1175
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001176 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001177 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001178 * glyph sequence--includes the first
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001179 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001180 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001181 USHORT input[VAR]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001182 * second glyph */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001183 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001184 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001185 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001186 DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001187};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001188
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001189struct RuleSet
1190{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001191 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001192 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001193 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001194 unsigned int num_rules = rule.len;
1195 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001196 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001197 }
1198
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001199 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1200 {
1201 TRACE_COLLECT_GLYPHS (this);
1202 unsigned int num_rules = rule.len;
1203 for (unsigned int i = 0; i < num_rules; i++)
1204 (this+rule[i]).collect_glyphs (c, lookup_context);
1205 }
1206
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001207 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1208 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001209 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001210 unsigned int num_rules = rule.len;
1211 for (unsigned int i = 0; i < num_rules; i++)
1212 {
1213 if ((this+rule[i]).would_apply (c, lookup_context))
1214 return TRACE_RETURN (true);
1215 }
1216 return TRACE_RETURN (false);
1217 }
1218
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001219 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001220 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001221 TRACE_APPLY (this);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001222 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001223 for (unsigned int i = 0; i < num_rules; i++)
1224 {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001225 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001226 return TRACE_RETURN (true);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001227 }
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001228 return TRACE_RETURN (false);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001229 }
1230
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001231 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001232 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001233 return TRACE_RETURN (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001234 }
1235
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001236 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001237 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001238 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001239 * ordered by preference */
Behdad Esfahboded074222010-05-10 18:08:46 -04001240 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001241 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001242};
1243
1244
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001245struct ContextFormat1
1246{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001247 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001248 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001249 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001250
1251 const Coverage &cov = (this+coverage);
1252
1253 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001254 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001255 NULL
1256 };
1257
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001258 unsigned int count = ruleSet.len;
1259 for (unsigned int i = 0; i < count; i++)
1260 if (cov.intersects_coverage (c->glyphs, i)) {
1261 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001262 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001263 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001264 }
1265
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001266 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1267 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001268 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001269 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001270
1271 struct ContextCollectGlyphsLookupContext lookup_context = {
1272 {collect_glyph},
1273 NULL
1274 };
1275
1276 unsigned int count = ruleSet.len;
1277 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001278 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001279 }
1280
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001281 inline bool would_apply (hb_would_apply_context_t *c) const
1282 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001283 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001284
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001285 const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001286 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001287 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001288 NULL
1289 };
1290 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1291 }
1292
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001293 inline const Coverage &get_coverage (void) const
1294 {
1295 return this+coverage;
1296 }
1297
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001298 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001299 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001300 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001301 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -04001302 if (likely (index == NOT_COVERED))
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001303 return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001304
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001305 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001306 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001307 {match_glyph},
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001308 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001309 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001310 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001311 }
1312
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001313 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001314 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001315 return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001316 }
1317
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001318 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001319 USHORT format; /* Format identifier--format = 1 */
1320 OffsetTo<Coverage>
1321 coverage; /* Offset to Coverage table--from
1322 * beginning of table */
1323 OffsetArrayOf<RuleSet>
1324 ruleSet; /* Array of RuleSet tables
1325 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001326 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001327 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001328};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001329
1330
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001331struct ContextFormat2
1332{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001333 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001334 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001335 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001336 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001337 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001338
1339 const ClassDef &class_def = this+classDef;
1340
1341 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001342 {intersects_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001343 &class_def
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001344 };
1345
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001346 unsigned int count = ruleSet.len;
1347 for (unsigned int i = 0; i < count; i++)
1348 if (class_def.intersects_class (c->glyphs, i)) {
1349 const RuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001350 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001351 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001352 }
1353
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001354 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1355 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001356 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001357 (this+coverage).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001358
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001359 const ClassDef &class_def = this+classDef;
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001360 struct ContextCollectGlyphsLookupContext lookup_context = {
1361 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001362 &class_def
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001363 };
1364
1365 unsigned int count = ruleSet.len;
1366 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001367 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001368 }
1369
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001370 inline bool would_apply (hb_would_apply_context_t *c) const
1371 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001372 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001373
1374 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001375 unsigned int index = class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001376 const RuleSet &rule_set = this+ruleSet[index];
1377 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001378 {match_class},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001379 &class_def
1380 };
1381 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1382 }
1383
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001384 inline const Coverage &get_coverage (void) const
1385 {
1386 return this+coverage;
1387 }
1388
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001389 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001390 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001391 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001392 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001393 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001394
1395 const ClassDef &class_def = this+classDef;
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001396 index = class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001397 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001398 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001399 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001400 &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001401 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001402 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001403 }
1404
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001405 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001406 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001407 return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001408 }
1409
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001410 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001411 USHORT format; /* Format identifier--format = 2 */
1412 OffsetTo<Coverage>
1413 coverage; /* Offset to Coverage table--from
1414 * beginning of table */
1415 OffsetTo<ClassDef>
1416 classDef; /* Offset to glyph ClassDef table--from
1417 * beginning of table */
1418 OffsetArrayOf<RuleSet>
1419 ruleSet; /* Array of RuleSet tables
1420 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001421 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001422 DEFINE_SIZE_ARRAY (8, ruleSet);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001423};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001424
1425
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001426struct ContextFormat3
1427{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001428 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001429 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001430 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001431 if (!(this+coverage[0]).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001432 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001433
1434 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1435 struct ContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001436 {intersects_coverage},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001437 this
1438 };
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001439 context_closure_lookup (c,
1440 glyphCount, (const USHORT *) (coverage + 1),
1441 lookupCount, lookupRecord,
1442 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001443 }
1444
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001445 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1446 {
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001447 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001448 (this+coverage[0]).add_coverage (c->input);
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001449
1450 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1451 struct ContextCollectGlyphsLookupContext lookup_context = {
1452 {collect_coverage},
Behdad Esfahbode75943d2012-11-30 08:38:24 +02001453 this
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001454 };
1455
1456 context_collect_glyphs_lookup (c,
1457 glyphCount, (const USHORT *) (coverage + 1),
1458 lookupCount, lookupRecord,
1459 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001460 }
1461
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001462 inline bool would_apply (hb_would_apply_context_t *c) const
1463 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001464 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001465
1466 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1467 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001468 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001469 this
1470 };
1471 return TRACE_RETURN (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
1472 }
1473
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001474 inline const Coverage &get_coverage (void) const
1475 {
1476 return this+coverage[0];
1477 }
1478
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001479 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001480 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001481 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001482 unsigned int index = (this+coverage[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001483 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001484
Behdad Esfahbode45d3f82010-05-06 19:33:31 -04001485 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001486 struct ContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001487 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001488 this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001489 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001490 return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001491 }
1492
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001493 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001494 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001495 if (!c->check_struct (this)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001496 unsigned int count = glyphCount;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001497 if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001498 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001499 if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode45d3f82010-05-06 19:33:31 -04001500 LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001501 return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001502 }
1503
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001504 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001505 USHORT format; /* Format identifier--format = 3 */
1506 USHORT glyphCount; /* Number of glyphs in the input glyph
1507 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001508 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001509 OffsetTo<Coverage>
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001510 coverage[VAR]; /* Array of offsets to Coverage
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001511 * table in glyph sequence order */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -05001512 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001513 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001514 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001515 DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001516};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001517
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001518struct Context
1519{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001520 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001521 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001522 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001523 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001524 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001525 case 1: return TRACE_RETURN (c->dispatch (u.format1));
1526 case 2: return TRACE_RETURN (c->dispatch (u.format2));
1527 case 3: return TRACE_RETURN (c->dispatch (u.format3));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001528 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001529 }
1530 }
1531
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001532 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001533 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001534 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001535 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001536 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1537 case 2: return TRACE_RETURN (u.format2.sanitize (c));
1538 case 3: return TRACE_RETURN (u.format3.sanitize (c));
1539 default:return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001540 }
1541 }
1542
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001543 protected:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001544 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -04001545 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001546 ContextFormat1 format1;
1547 ContextFormat2 format2;
1548 ContextFormat3 format3;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001549 } u;
1550};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001551
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001552
1553/* Chaining Contextual lookups */
1554
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001555struct ChainContextClosureLookupContext
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001556{
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001557 ContextClosureFuncs funcs;
1558 const void *intersects_data[3];
1559};
1560
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001561struct ChainContextCollectGlyphsLookupContext
1562{
1563 ContextCollectGlyphsFuncs funcs;
1564 const void *collect_data[3];
1565};
1566
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001567struct ChainContextApplyLookupContext
1568{
1569 ContextApplyFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001570 const void *match_data[3];
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001571};
1572
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001573static inline void chain_context_closure_lookup (hb_closure_context_t *c,
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001574 unsigned int backtrackCount,
1575 const USHORT backtrack[],
1576 unsigned int inputCount, /* Including the first glyph (not matched) */
1577 const USHORT input[], /* Array of input values--start with second glyph */
1578 unsigned int lookaheadCount,
1579 const USHORT lookahead[],
1580 unsigned int lookupCount,
1581 const LookupRecord lookupRecord[],
1582 ChainContextClosureLookupContext &lookup_context)
1583{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001584 if (intersects_array (c,
1585 backtrackCount, backtrack,
1586 lookup_context.funcs.intersects, lookup_context.intersects_data[0])
1587 && intersects_array (c,
1588 inputCount ? inputCount - 1 : 0, input,
1589 lookup_context.funcs.intersects, lookup_context.intersects_data[1])
Behdad Esfahbod74439d02013-07-22 19:02:29 -04001590 && intersects_array (c,
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001591 lookaheadCount, lookahead,
1592 lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
Behdad Esfahbodd0a52332012-11-23 18:54:59 -05001593 recurse_lookups (c,
1594 lookupCount, lookupRecord);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001595}
1596
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001597static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1598 unsigned int backtrackCount,
1599 const USHORT backtrack[],
1600 unsigned int inputCount, /* Including the first glyph (not matched) */
1601 const USHORT input[], /* Array of input values--start with second glyph */
1602 unsigned int lookaheadCount,
1603 const USHORT lookahead[],
1604 unsigned int lookupCount,
1605 const LookupRecord lookupRecord[],
1606 ChainContextCollectGlyphsLookupContext &lookup_context)
1607{
Behdad Esfahbod83035932012-12-04 17:08:41 -05001608 collect_array (c, c->before,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001609 backtrackCount, backtrack,
1610 lookup_context.funcs.collect, lookup_context.collect_data[0]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001611 collect_array (c, c->input,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001612 inputCount ? inputCount - 1 : 0, input,
1613 lookup_context.funcs.collect, lookup_context.collect_data[1]);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001614 collect_array (c, c->after,
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001615 lookaheadCount, lookahead,
1616 lookup_context.funcs.collect, lookup_context.collect_data[2]);
1617 recurse_lookups (c,
1618 lookupCount, lookupRecord);
1619}
1620
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001621static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
1622 unsigned int backtrackCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001623 const USHORT backtrack[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001624 unsigned int inputCount, /* Including the first glyph (not matched) */
1625 const USHORT input[], /* Array of input values--start with second glyph */
1626 unsigned int lookaheadCount,
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -05001627 const USHORT lookahead[] HB_UNUSED,
1628 unsigned int lookupCount HB_UNUSED,
1629 const LookupRecord lookupRecord[] HB_UNUSED,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001630 ChainContextApplyLookupContext &lookup_context)
1631{
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -04001632 return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
Behdad Esfahbod1f2bb172012-08-23 16:10:37 -04001633 && would_match_input (c,
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001634 inputCount, input,
1635 lookup_context.funcs.match, lookup_context.match_data[1]);
1636}
1637
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001638static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
1639 unsigned int backtrackCount,
1640 const USHORT backtrack[],
1641 unsigned int inputCount, /* Including the first glyph (not matched) */
1642 const USHORT input[], /* Array of input values--start with second glyph */
1643 unsigned int lookaheadCount,
1644 const USHORT lookahead[],
1645 unsigned int lookupCount,
1646 const LookupRecord lookupRecord[],
1647 ChainContextApplyLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001648{
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001649 unsigned int match_length = 0;
1650 unsigned int match_positions[MAX_CONTEXT_LENGTH];
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001651 return match_input (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001652 inputCount, input,
1653 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001654 &match_length, match_positions)
Behdad Esfahbodf19e0b02012-06-09 02:26:57 -04001655 && match_backtrack (c,
1656 backtrackCount, backtrack,
1657 lookup_context.funcs.match, lookup_context.match_data[0])
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001658 && match_lookahead (c,
Behdad Esfahbod41697102010-05-05 01:37:58 -04001659 lookaheadCount, lookahead,
1660 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001661 match_length)
Behdad Esfahbod5df809b2012-05-13 15:17:51 +02001662 && apply_lookup (c,
Behdad Esfahbod6b65a762013-10-14 18:51:39 +02001663 inputCount, match_positions,
1664 lookupCount, lookupRecord,
1665 match_length);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001666}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001667
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001668struct ChainRule
1669{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001670 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001671 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001672 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001673 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1674 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1675 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001676 chain_context_closure_lookup (c,
1677 backtrack.len, backtrack.array,
1678 input.len, input.array,
1679 lookahead.len, lookahead.array,
1680 lookup.len, lookup.array,
1681 lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001682 }
1683
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001684 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1685 {
1686 TRACE_COLLECT_GLYPHS (this);
1687 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1688 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1689 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1690 chain_context_collect_glyphs_lookup (c,
1691 backtrack.len, backtrack.array,
1692 input.len, input.array,
1693 lookahead.len, lookahead.array,
1694 lookup.len, lookup.array,
1695 lookup_context);
1696 }
1697
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001698 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1699 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001700 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001701 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1702 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1703 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1704 return TRACE_RETURN (chain_context_would_apply_lookup (c,
1705 backtrack.len, backtrack.array,
1706 input.len, input.array,
1707 lookahead.len, lookahead.array, lookup.len,
1708 lookup.array, lookup_context));
1709 }
1710
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001711 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001712 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001713 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001714 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1715 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1716 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001717 return TRACE_RETURN (chain_context_apply_lookup (c,
1718 backtrack.len, backtrack.array,
1719 input.len, input.array,
1720 lookahead.len, lookahead.array, lookup.len,
1721 lookup.array, lookup_context));
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001722 }
1723
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001724 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001725 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001726 if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001727 HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001728 if (!input.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001729 ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001730 if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04001731 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001732 return TRACE_RETURN (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001733 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001734
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001735 protected:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001736 ArrayOf<USHORT>
1737 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001738 * (to be matched before the input
1739 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -04001740 HeadlessArrayOf<USHORT>
1741 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001742 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001743 ArrayOf<USHORT>
1744 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001745 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04001746 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04001747 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001748 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001749 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04001750 DEFINE_SIZE_MIN (8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001751};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001752
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001753struct ChainRuleSet
1754{
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001755 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001756 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001757 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001758 unsigned int num_rules = rule.len;
1759 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001760 (this+rule[i]).closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001761 }
1762
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001763 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1764 {
1765 TRACE_COLLECT_GLYPHS (this);
1766 unsigned int num_rules = rule.len;
1767 for (unsigned int i = 0; i < num_rules; i++)
1768 (this+rule[i]).collect_glyphs (c, lookup_context);
1769 }
1770
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001771 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1772 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001773 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001774 unsigned int num_rules = rule.len;
1775 for (unsigned int i = 0; i < num_rules; i++)
1776 if ((this+rule[i]).would_apply (c, lookup_context))
1777 return TRACE_RETURN (true);
1778
1779 return TRACE_RETURN (false);
1780 }
1781
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001782 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001783 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001784 TRACE_APPLY (this);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001785 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001786 for (unsigned int i = 0; i < num_rules; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001787 if ((this+rule[i]).apply (c, lookup_context))
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001788 return TRACE_RETURN (true);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001789
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001790 return TRACE_RETURN (false);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001791 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001792
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001793 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001794 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001795 return TRACE_RETURN (rule.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001796 }
1797
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001798 protected:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001799 OffsetArrayOf<ChainRule>
1800 rule; /* Array of ChainRule tables
1801 * ordered by preference */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001802 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001803 DEFINE_SIZE_ARRAY (2, rule);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001804};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001805
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001806struct ChainContextFormat1
1807{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001808 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001809 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001810 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001811 const Coverage &cov = (this+coverage);
1812
1813 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001814 {intersects_glyph},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001815 {NULL, NULL, NULL}
1816 };
1817
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001818 unsigned int count = ruleSet.len;
1819 for (unsigned int i = 0; i < count; i++)
1820 if (cov.intersects_coverage (c->glyphs, i)) {
1821 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001822 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001823 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001824 }
1825
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001826 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1827 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001828 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001829 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001830
1831 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1832 {collect_glyph},
1833 {NULL, NULL, NULL}
1834 };
1835
1836 unsigned int count = ruleSet.len;
1837 for (unsigned int i = 0; i < count; i++)
1838 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001839 }
1840
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001841 inline bool would_apply (hb_would_apply_context_t *c) const
1842 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001843 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001844
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001845 const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001846 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001847 {match_glyph},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001848 {NULL, NULL, NULL}
1849 };
1850 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1851 }
1852
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001853 inline const Coverage &get_coverage (void) const
1854 {
1855 return this+coverage;
1856 }
1857
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001858 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001859 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001860 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001861 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001862 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001863
1864 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001865 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001866 {match_glyph},
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001867 {NULL, NULL, NULL}
1868 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001869 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001870 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001871
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001872 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001873 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001874 return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001875 }
1876
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001877 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001878 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -04001879 OffsetTo<Coverage>
1880 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001881 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001882 OffsetArrayOf<ChainRuleSet>
1883 ruleSet; /* Array of ChainRuleSet tables
1884 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001885 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001886 DEFINE_SIZE_ARRAY (6, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001887};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001888
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001889struct ChainContextFormat2
1890{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001891 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001892 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001893 TRACE_CLOSURE (this);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001894 if (!(this+coverage).intersects (c->glyphs))
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001895 return;
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001896
1897 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1898 const ClassDef &input_class_def = this+inputClassDef;
1899 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1900
1901 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001902 {intersects_class},
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001903 {&backtrack_class_def,
1904 &input_class_def,
1905 &lookahead_class_def}
1906 };
1907
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001908 unsigned int count = ruleSet.len;
1909 for (unsigned int i = 0; i < count; i++)
1910 if (input_class_def.intersects_class (c->glyphs, i)) {
1911 const ChainRuleSet &rule_set = this+ruleSet[i];
Behdad Esfahbod5caece62012-04-23 23:03:12 -04001912 rule_set.closure (c, lookup_context);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001913 }
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04001914 }
1915
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001916 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1917 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001918 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001919 (this+coverage).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001920
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001921 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1922 const ClassDef &input_class_def = this+inputClassDef;
1923 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1924
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001925 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1926 {collect_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001927 {&backtrack_class_def,
1928 &input_class_def,
1929 &lookahead_class_def}
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05001930 };
1931
1932 unsigned int count = ruleSet.len;
1933 for (unsigned int i = 0; i < count; i++)
1934 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001935 }
1936
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001937 inline bool would_apply (hb_would_apply_context_t *c) const
1938 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001939 TRACE_WOULD_APPLY (this);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001940
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001941 const ClassDef &backtrack_class_def = this+backtrackClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001942 const ClassDef &input_class_def = this+inputClassDef;
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001943 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001944
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001945 unsigned int index = input_class_def.get_class (c->glyphs[0]);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001946 const ChainRuleSet &rule_set = this+ruleSet[index];
1947 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001948 {match_class},
Behdad Esfahbod11fba792013-01-02 23:36:37 -06001949 {&backtrack_class_def,
1950 &input_class_def,
1951 &lookahead_class_def}
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001952 };
1953 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1954 }
1955
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05001956 inline const Coverage &get_coverage (void) const
1957 {
1958 return this+coverage;
1959 }
1960
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001961 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04001962 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001963 TRACE_APPLY (this);
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05001964 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001965 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001966
1967 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1968 const ClassDef &input_class_def = this+inputClassDef;
1969 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1970
Behdad Esfahbod2dc11412012-11-24 19:16:34 -05001971 index = input_class_def.get_class (c->buffer->cur().codepoint);
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001972 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod31081f72012-04-23 16:54:58 -04001973 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001974 {match_class},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04001975 {&backtrack_class_def,
1976 &input_class_def,
1977 &lookahead_class_def}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001978 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001979 return TRACE_RETURN (rule_set.apply (c, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001980 }
1981
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001982 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001983 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001984 return TRACE_RETURN (coverage.sanitize (c, this) && backtrackClassDef.sanitize (c, this) &&
1985 inputClassDef.sanitize (c, this) && lookaheadClassDef.sanitize (c, this) &&
1986 ruleSet.sanitize (c, this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04001987 }
1988
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001989 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001990 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001991 OffsetTo<Coverage>
1992 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001993 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001994 OffsetTo<ClassDef>
1995 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04001996 * containing backtrack sequence
1997 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04001998 OffsetTo<ClassDef>
1999 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002000 * table containing input sequence
2001 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002002 OffsetTo<ClassDef>
2003 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002004 * containing lookahead sequence
2005 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -04002006 OffsetArrayOf<ChainRuleSet>
2007 ruleSet; /* Array of ChainRuleSet tables
2008 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002009 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04002010 DEFINE_SIZE_ARRAY (12, ruleSet);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002011};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002012
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002013struct ChainContextFormat3
2014{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002015 inline void closure (hb_closure_context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002016 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002017 TRACE_CLOSURE (this);
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002018 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2019
2020 if (!(this+input[0]).intersects (c->glyphs))
2021 return;
2022
2023 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2024 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2025 struct ChainContextClosureLookupContext lookup_context = {
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002026 {intersects_coverage},
Behdad Esfahbod5caece62012-04-23 23:03:12 -04002027 {this, this, this}
2028 };
2029 chain_context_closure_lookup (c,
2030 backtrack.len, (const USHORT *) backtrack.array,
2031 input.len, (const USHORT *) input.array + 1,
2032 lookahead.len, (const USHORT *) lookahead.array,
2033 lookup.len, lookup.array,
2034 lookup_context);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002035 }
2036
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002037 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
2038 {
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002039 TRACE_COLLECT_GLYPHS (this);
2040 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2041
Behdad Esfahbod83035932012-12-04 17:08:41 -05002042 (this+input[0]).add_coverage (c->input);
Behdad Esfahbodf1b12782012-11-24 01:55:34 -05002043
2044 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2045 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2046 struct ChainContextCollectGlyphsLookupContext lookup_context = {
2047 {collect_coverage},
2048 {this, this, this}
2049 };
2050 chain_context_collect_glyphs_lookup (c,
2051 backtrack.len, (const USHORT *) backtrack.array,
2052 input.len, (const USHORT *) input.array + 1,
2053 lookahead.len, (const USHORT *) lookahead.array,
2054 lookup.len, lookup.array,
2055 lookup_context);
Behdad Esfahbod26514d52012-11-23 18:13:48 -05002056 }
2057
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002058 inline bool would_apply (hb_would_apply_context_t *c) const
2059 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002060 TRACE_WOULD_APPLY (this);
Behdad Esfahbode6f74792012-07-28 18:34:58 -04002061
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002062 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002063 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2064 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2065 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002066 {match_coverage},
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002067 {this, this, this}
2068 };
2069 return TRACE_RETURN (chain_context_would_apply_lookup (c,
2070 backtrack.len, (const USHORT *) backtrack.array,
2071 input.len, (const USHORT *) input.array + 1,
2072 lookahead.len, (const USHORT *) lookahead.array,
2073 lookup.len, lookup.array, lookup_context));
2074 }
2075
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002076 inline const Coverage &get_coverage (void) const
2077 {
2078 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2079 return this+input[0];
2080 }
2081
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002082 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002083 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002084 TRACE_APPLY (this);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002085 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002086
Behdad Esfahbodb67881b2012-11-24 19:13:55 -05002087 unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002088 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002089
Behdad Esfahbode961c862010-04-21 15:56:11 -04002090 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
2091 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod31081f72012-04-23 16:54:58 -04002092 struct ChainContextApplyLookupContext lookup_context = {
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002093 {match_coverage},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -04002094 {this, this, this}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002095 };
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002096 return TRACE_RETURN (chain_context_apply_lookup (c,
2097 backtrack.len, (const USHORT *) backtrack.array,
2098 input.len, (const USHORT *) input.array + 1,
2099 lookahead.len, (const USHORT *) lookahead.array,
2100 lookup.len, lookup.array, lookup_context));
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002101 }
2102
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002103 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002104 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002105 if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002106 OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002107 if (!input.sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002108 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002109 if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
Behdad Esfahbode961c862010-04-21 15:56:11 -04002110 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002111 return TRACE_RETURN (lookup.sanitize (c));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002112 }
2113
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002114 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002115 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002116 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002117 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002118 * in backtracking sequence, in glyph
2119 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002120 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002121 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002122 * tables in input sequence, in glyph
2123 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002124 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -04002125 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002126 * in lookahead sequence, in glyph
2127 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002128 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -04002129 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -04002130 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002131 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -04002132 DEFINE_SIZE_MIN (10);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002133};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002134
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002135struct ChainContext
2136{
Behdad Esfahbod44fc2372012-11-21 23:33:13 -05002137 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002138 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002139 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002140 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -04002141 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002142 case 1: return TRACE_RETURN (c->dispatch (u.format1));
2143 case 2: return TRACE_RETURN (c->dispatch (u.format2));
2144 case 3: return TRACE_RETURN (c->dispatch (u.format3));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05002145 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002146 }
2147 }
2148
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002149 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002150 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002151 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002152 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002153 case 1: return TRACE_RETURN (u.format1.sanitize (c));
2154 case 2: return TRACE_RETURN (u.format2.sanitize (c));
2155 case 3: return TRACE_RETURN (u.format3.sanitize (c));
2156 default:return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002157 }
2158 }
2159
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002160 protected:
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002161 union {
2162 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002163 ChainContextFormat1 format1;
2164 ChainContextFormat2 format2;
2165 ChainContextFormat3 format3;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002166 } u;
2167};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -04002168
2169
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002170struct ExtensionFormat1
2171{
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002172 inline unsigned int get_type (void) const { return extensionLookupType; }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -04002173 inline unsigned int get_offset (void) const { return extensionOffset; }
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002174
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002175 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002176 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002177 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002178 }
2179
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002180 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002181 USHORT format; /* Format identifier. Set to 1. */
2182 USHORT extensionLookupType; /* Lookup type of subtable referenced
2183 * by ExtensionOffset (i.e. the
2184 * extension subtable). */
Behdad Esfahbod81f2af42010-04-22 00:58:49 -04002185 ULONG extensionOffset; /* Offset to the extension subtable,
2186 * of lookup type subtable. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002187 public:
2188 DEFINE_SIZE_STATIC (8);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002189};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002190
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002191template <typename T>
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002192struct Extension
2193{
2194 inline unsigned int get_type (void) const
2195 {
2196 switch (u.format) {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002197 case 1: return u.format1.get_type ();
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002198 default:return 0;
2199 }
2200 }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -04002201 inline unsigned int get_offset (void) const
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002202 {
2203 switch (u.format) {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002204 case 1: return u.format1.get_offset ();
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -04002205 default:return 0;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002206 }
2207 }
2208
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05002209 template <typename X>
2210 inline const X& get_subtable (void) const
2211 {
2212 unsigned int offset = get_offset ();
2213 if (unlikely (!offset)) return Null(typename T::LookupSubTable);
2214 return StructAtOffset<typename T::LookupSubTable> (this, offset);
2215 }
2216
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002217 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002218 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002219 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002220 return get_subtable<typename T::LookupSubTable> ().dispatch (c, get_type ());
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002221 }
2222
Behdad Esfahboded2e1352012-11-23 17:10:40 -05002223 inline bool sanitize_self (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002224 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002225 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002226 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002227 case 1: return TRACE_RETURN (u.format1.sanitize (c));
2228 default:return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -04002229 }
2230 }
2231
Behdad Esfahboded2e1352012-11-23 17:10:40 -05002232 inline bool sanitize (hb_sanitize_context_t *c) {
2233 TRACE_SANITIZE (this);
2234 if (!sanitize_self (c)) return TRACE_RETURN (false);
2235 unsigned int offset = get_offset ();
2236 if (unlikely (!offset)) return TRACE_RETURN (true);
2237 return TRACE_RETURN (StructAtOffset<typename T::LookupSubTable> (this, offset).sanitize (c, get_type ()));
2238 }
2239
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002240 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002241 union {
2242 USHORT format; /* Format identifier */
Behdad Esfahbod6d08c7f2012-07-11 18:01:27 -04002243 ExtensionFormat1 format1;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002244 } u;
2245};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002246
2247
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002248/*
2249 * GSUB/GPOS Common
2250 */
2251
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -04002252struct GSUBGPOS
2253{
Behdad Esfahboda328d662009-08-04 20:27:05 -04002254 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
2255 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002256
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002257 inline unsigned int get_script_count (void) const
2258 { return (this+scriptList).len; }
2259 inline const Tag& get_script_tag (unsigned int i) const
2260 { return (this+scriptList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002261 inline unsigned int get_script_tags (unsigned int start_offset,
2262 unsigned int *script_count /* IN/OUT */,
2263 hb_tag_t *script_tags /* OUT */) const
2264 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002265 inline const Script& get_script (unsigned int i) const
2266 { return (this+scriptList)[i]; }
2267 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
2268 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002269
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002270 inline unsigned int get_feature_count (void) const
2271 { return (this+featureList).len; }
Jonathan Kewda132932014-04-27 14:05:24 +01002272 inline hb_tag_t get_feature_tag (unsigned int i) const
2273 { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : (this+featureList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -05002274 inline unsigned int get_feature_tags (unsigned int start_offset,
2275 unsigned int *feature_count /* IN/OUT */,
2276 hb_tag_t *feature_tags /* OUT */) const
2277 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -04002278 inline const Feature& get_feature (unsigned int i) const
2279 { return (this+featureList)[i]; }
2280 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
2281 { return (this+featureList).find_index (tag, index); }
2282
2283 inline unsigned int get_lookup_count (void) const
2284 { return (this+lookupList).len; }
2285 inline const Lookup& get_lookup (unsigned int i) const
2286 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002287
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04002288 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002289 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002290 return TRACE_RETURN (version.sanitize (c) && likely (version.major == 1) &&
2291 scriptList.sanitize (c, this) &&
2292 featureList.sanitize (c, this) &&
2293 lookupList.sanitize (c, this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -04002294 }
2295
Behdad Esfahbod212aba62009-05-24 00:50:27 -04002296 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -04002297 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbod76271002014-07-11 14:54:42 -04002298 * to 0x00010000u */
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002299 OffsetTo<ScriptList>
2300 scriptList; /* ScriptList table */
2301 OffsetTo<FeatureList>
2302 featureList; /* FeatureList table */
2303 OffsetTo<LookupList>
2304 lookupList; /* LookupList table */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002305 public:
2306 DEFINE_SIZE_STATIC (10);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -04002307};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04002308
Behdad Esfahbod6f20f722009-05-17 20:28:01 -04002309
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08002310} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04002311
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04002312
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -04002313#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */