blob: b21939ac001164085c8e39744aba0acc26ea8f7c [file] [log] [blame]
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001/*
2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
3 *
4 * This is part of HarfBuzz, an OpenType Layout engine library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040027#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
28#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040029
Behdad Esfahbod88a5f5a2009-05-25 03:39:11 -040030#include "hb-buffer-private.h"
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040031#include "hb-ot-layout-gdef-private.hh"
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040032
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040033
Behdad Esfahbod0535b502009-08-28 17:14:33 -040034#ifndef HB_DEBUG_APPLY
35#define HB_DEBUG_APPLY HB_DEBUG
36#endif
37
38#if HB_DEBUG_APPLY
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -040039#define TRACE_APPLY_ARG_DEF , unsigned int apply_depth
40#define TRACE_APPLY_ARG , apply_depth + 1
41#define TRACE_APPLY_ARG_INIT , 1
42#define TRACE_APPLY() \
Behdad Esfahbod0535b502009-08-28 17:14:33 -040043 HB_STMT_START { \
44 if (apply_depth < HB_DEBUG_APPLY) \
45 fprintf (stderr, "APPLY(%p) %-*d-> %s\n", \
46 (CONST_CHARP (this) == NullPool) ? 0 : this, \
47 apply_depth, apply_depth, \
48 __PRETTY_FUNCTION__); \
49 } HB_STMT_END
50#else
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -040051#define TRACE_APPLY_ARG_DEF
52#define TRACE_APPLY_ARG
53#define TRACE_APPLY_ARG_INIT
54#define TRACE_APPLY() HB_STMT_START {} HB_STMT_END
Behdad Esfahbod0535b502009-08-28 17:14:33 -040055#endif
56
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040057#define APPLY_ARG_DEF \
Behdad Esfahbod0ead4812009-08-02 17:41:36 -040058 hb_ot_layout_context_t *context, \
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040059 hb_buffer_t *buffer, \
60 unsigned int context_length HB_GNUC_UNUSED, \
61 unsigned int nesting_level_left HB_GNUC_UNUSED, \
Behdad Esfahbodecf17e82009-05-17 09:34:41 -040062 unsigned int lookup_flag, \
Behdad Esfahbod0535b502009-08-28 17:14:33 -040063 unsigned int property HB_GNUC_UNUSED /* propety of first glyph */ \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -040064 TRACE_APPLY_ARG_DEF
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040065#define APPLY_ARG \
Behdad Esfahbod0ead4812009-08-02 17:41:36 -040066 context, \
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040067 buffer, \
68 context_length, \
69 nesting_level_left, \
Behdad Esfahbodecf17e82009-05-17 09:34:41 -040070 lookup_flag, \
Behdad Esfahbod0535b502009-08-28 17:14:33 -040071 property \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -040072 TRACE_APPLY_ARG
Behdad Esfahbod0535b502009-08-28 17:14:33 -040073#define APPLY_ARG_INIT \
74 context, \
75 buffer, \
76 context_length, \
77 nesting_level_left, \
78 lookup_flag, \
79 property \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -040080 TRACE_APPLY_ARG_INIT
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040081
82
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040083typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, char *data);
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040084typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040085
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040086struct ContextFuncs
87{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040088 match_func_t match;
89 apply_lookup_func_t apply;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040090};
91
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040092
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040093static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data)
94{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040095 return glyph_id == value;
96}
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040097
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040098static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data)
99{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400100 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400101 return class_def.get_class (glyph_id) == value;
102}
103
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400104static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data)
105{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -0400106 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400107 return (data+coverage) (glyph_id) != NOT_COVERED;
108}
109
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400110
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400111static inline bool match_input (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400112 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400113 const USHORT input[], /* Array of input values--start with second glyph */
114 match_func_t match_func,
115 char *match_data,
116 unsigned int *context_length_out)
117{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400118 unsigned int i, j;
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400119 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
120 if (HB_UNLIKELY (buffer->in_pos + count > end))
121 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400122
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400123 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
124 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400125 while (_hb_ot_layout_skip_mark (context->face, IN_INFO (j), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400126 {
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400127 if (HB_UNLIKELY (j + count - i == end))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400128 return false;
129 j++;
130 }
131
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400132 if (HB_LIKELY (!match_func (IN_GLYPH (j), input[i - 1], match_data)))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400133 return false;
134 }
135
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400136 *context_length_out = j - buffer->in_pos;
137
138 return true;
139}
140
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400141static inline bool match_backtrack (APPLY_ARG_DEF,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400142 unsigned int count,
143 const USHORT backtrack[],
144 match_func_t match_func,
145 char *match_data)
146{
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400147 if (HB_UNLIKELY (buffer->out_pos < count))
148 return false;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400149
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400150 for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
151 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400152 while (_hb_ot_layout_skip_mark (context->face, OUT_INFO (j), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400153 {
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400154 if (HB_UNLIKELY (j + 1 == count - i))
155 return false;
156 j--;
157 }
158
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400159 if (HB_LIKELY (!match_func (OUT_GLYPH (j), backtrack[i], match_data)))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400160 return false;
161 }
162
163 return true;
164}
165
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400166static inline bool match_lookahead (APPLY_ARG_DEF,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400167 unsigned int count,
168 const USHORT lookahead[],
169 match_func_t match_func,
170 char *match_data,
171 unsigned int offset)
172{
173 unsigned int i, j;
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400174 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
175 if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
176 return false;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400177
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400178 for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
179 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400180 while (_hb_ot_layout_skip_mark (context->face, OUT_INFO (j), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400181 {
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400182 if (HB_UNLIKELY (j + count - i == end))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400183 return false;
184 j++;
185 }
186
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400187 if (HB_LIKELY (!match_func (IN_GLYPH (j), lookahead[i], match_data)))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400188 return false;
189 }
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400190
191 return true;
192}
193
194
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400195struct LookupRecord
196{
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400197 public:
198 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400199 TRACE_SANITIZE ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400200 return SANITIZE_SELF ();
201 }
202
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400203 USHORT sequenceIndex; /* Index into current glyph
204 * sequence--first glyph = 0 */
205 USHORT lookupListIndex; /* Lookup to apply to that
206 * position--zero--based */
207};
208ASSERT_SIZE (LookupRecord, 4);
209
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400210static inline bool apply_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400211 unsigned int count, /* Including the first glyph */
212 unsigned int lookupCount,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400213 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
214 apply_lookup_func_t apply_func)
215{
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400216 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
217 if (HB_UNLIKELY (buffer->in_pos + count > end))
218 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400219
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400220 /* TODO We don't support lookupRecord arrays that are not increasing:
221 * Should be easy for in_place ones at least. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400222 for (unsigned int i = 0; i < count; i++)
223 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400224 while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400225 {
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400226 if (HB_UNLIKELY (buffer->in_pos == end))
227 return true;
228 /* No lookup applied for this index */
229 _hb_buffer_next_glyph (buffer);
230 }
231
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400232 if (lookupCount && i == lookupRecord->sequenceIndex)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400233 {
234 unsigned int old_pos = buffer->in_pos;
235
236 /* Apply a lookup */
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400237 bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400238
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400239 lookupRecord++;
240 lookupCount--;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400241 i += buffer->in_pos - old_pos;
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400242 if (HB_UNLIKELY (buffer->in_pos == end))
243 return true;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400244
245 if (!done)
246 goto not_applied;
247 }
248 else
249 {
250 not_applied:
251 /* No lookup applied for this index */
252 _hb_buffer_next_glyph (buffer);
253 i++;
254 }
255 }
256
257 return true;
258}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400259
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400260
261/* Contextual lookups */
262
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400263struct ContextLookupContext
264{
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400265 ContextFuncs funcs;
266 char *match_data;
267};
268
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400269static inline bool context_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400270 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400271 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400272 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400273 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400274 ContextLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400275{
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400276 return match_input (APPLY_ARG,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400277 inputCount, input,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400278 lookup_context.funcs.match, lookup_context.match_data,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400279 &context_length) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400280 apply_lookup (APPLY_ARG,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400281 inputCount,
282 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400283 lookup_context.funcs.apply);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400284}
285
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400286struct Rule
287{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400288 friend struct RuleSet;
289
290 private:
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400291 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400292 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400293 TRACE_APPLY ();
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400294 const LookupRecord *lookupRecord = &CONST_CAST (LookupRecord, input, sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400295 return context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400296 inputCount, input,
297 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400298 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400299 }
300
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400301 public:
302 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400303 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400304 if (!SANITIZE_SELF ()) return false;
305 return SANITIZE_MEM (input,
306 sizeof (input[0]) * inputCount +
307 sizeof (lookupRecordX[0]) * lookupCount);
308 }
309
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400310 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400311 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400312 * glyph sequence--includes the first
313 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400314 USHORT lookupCount; /* Number of LookupRecords */
315 USHORT input[]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400316 * second glyph */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400317 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400318 * design order */
319};
320ASSERT_SIZE (Rule, 4);
321
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400322struct RuleSet
323{
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400324 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400325 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400326 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400327 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400328 for (unsigned int i = 0; i < num_rules; i++)
329 {
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400330 if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400331 return true;
332 }
333
334 return false;
335 }
336
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400337 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400338 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400339 return SANITIZE_THIS (rule);
340 }
341
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400342 private:
343 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400344 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400345 * ordered by preference */
346};
347
348
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400349struct ContextFormat1
350{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400351 friend struct Context;
352
353 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400354 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
355 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400356 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400357 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400358 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400359 return false;
360
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400361 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400362 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400363 {match_glyph, apply_func},
364 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400365 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400366 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400367 }
368
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400369 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400370 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400371 return SANITIZE_THIS2 (coverage, ruleSet);
372 }
373
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400374 private:
375 USHORT format; /* Format identifier--format = 1 */
376 OffsetTo<Coverage>
377 coverage; /* Offset to Coverage table--from
378 * beginning of table */
379 OffsetArrayOf<RuleSet>
380 ruleSet; /* Array of RuleSet tables
381 * ordered by Coverage Index */
382};
383ASSERT_SIZE (ContextFormat1, 6);
384
385
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400386struct ContextFormat2
387{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400388 friend struct Context;
389
390 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400391 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
392 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400393 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400394 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400395 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400396 return false;
397
398 const ClassDef &class_def = this+classDef;
399 index = class_def (IN_CURGLYPH ());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400400 const RuleSet &rule_set = this+ruleSet[index];
401 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
402 * them across subrule lookups. Not sure it's worth it.
403 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400404 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400405 {match_class, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400406 DECONST_CHARP(&class_def)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400407 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400408 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400409 }
410
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400411 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400412 TRACE_SANITIZE ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400413 return SANITIZE_THIS3 (coverage, classDef, ruleSet);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400414 }
415
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400416 private:
417 USHORT format; /* Format identifier--format = 2 */
418 OffsetTo<Coverage>
419 coverage; /* Offset to Coverage table--from
420 * beginning of table */
421 OffsetTo<ClassDef>
422 classDef; /* Offset to glyph ClassDef table--from
423 * beginning of table */
424 OffsetArrayOf<RuleSet>
425 ruleSet; /* Array of RuleSet tables
426 * ordered by class */
427};
428ASSERT_SIZE (ContextFormat2, 8);
429
430
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400431struct ContextFormat3
432{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400433 friend struct Context;
434
435 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400436 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
437 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400438 TRACE_APPLY ();
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400439 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400440 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400441 return false;
442
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400443 const LookupRecord *lookupRecord = &CONST_CAST(LookupRecord, coverage, sizeof (coverage[0]) * glyphCount);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400444 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400445 {match_coverage, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400446 DECONST_CHARP(this)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400447 };
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400448 return context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400449 glyphCount, (const USHORT *) (coverage + 1),
450 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400451 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400452 }
453
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400454 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400455 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400456 if (!SANITIZE_SELF ()) return false;
457 unsigned int count = glyphCount;
458 for (unsigned int i = 0; i < count; i++)
459 if (!SANITIZE_THIS (coverage[i])) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400460 LookupRecord *lookupRecord = &CAST(LookupRecord, coverage, sizeof (coverage[0]) * glyphCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400461 return SANITIZE_MEM (lookupRecord, sizeof (lookupRecord[0]) * lookupCount);
462 }
463
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400464 private:
465 USHORT format; /* Format identifier--format = 3 */
466 USHORT glyphCount; /* Number of glyphs in the input glyph
467 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400468 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400469 OffsetTo<Coverage>
470 coverage[]; /* Array of offsets to Coverage
471 * table in glyph sequence order */
472 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400473 * design order */
474};
475ASSERT_SIZE (ContextFormat3, 6);
476
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400477struct Context
478{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400479 protected:
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400480 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400481 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400482 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400483 switch (u.format) {
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400484 case 1: return u.format1->apply (APPLY_ARG, apply_func);
485 case 2: return u.format2->apply (APPLY_ARG, apply_func);
486 case 3: return u.format3->apply (APPLY_ARG, apply_func);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400487 default:return false;
488 }
489 }
490
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400491 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400492 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400493 if (!SANITIZE (u.format)) return false;
494 switch (u.format) {
495 case 1: return u.format1->sanitize (SANITIZE_ARG);
496 case 2: return u.format2->sanitize (SANITIZE_ARG);
497 case 3: return u.format3->sanitize (SANITIZE_ARG);
498 default:return true;
499 }
500 }
501
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400502 private:
503 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400504 USHORT format; /* Format identifier */
505 ContextFormat1 format1[];
506 ContextFormat2 format2[];
507 ContextFormat3 format3[];
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400508 } u;
509};
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400510ASSERT_SIZE (Context, 2);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400511
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400512
513/* Chaining Contextual lookups */
514
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400515struct ChainContextLookupContext
516{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400517 ContextFuncs funcs;
518 char *match_data[3];
519};
520
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400521static inline bool chain_context_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400522 unsigned int backtrackCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400523 const USHORT backtrack[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400524 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400525 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400526 unsigned int lookaheadCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400527 const USHORT lookahead[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400528 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400529 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400530 ChainContextLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400531{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400532 /* First guess */
533 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
Behdad Esfahbod122f21f2009-05-18 04:21:53 -0400534 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
535 inputCount + lookaheadCount > context_length))
Behdad Esfahbode072c242009-05-18 03:47:31 -0400536 return false;
537
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400538 unsigned int offset;
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400539 return match_backtrack (APPLY_ARG,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400540 backtrackCount, backtrack,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400541 lookup_context.funcs.match, lookup_context.match_data[0]) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400542 match_input (APPLY_ARG,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400543 inputCount, input,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400544 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400545 &offset) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400546 match_lookahead (APPLY_ARG,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400547 lookaheadCount, lookahead,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400548 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400549 offset) &&
550 (context_length = offset, true) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400551 apply_lookup (APPLY_ARG,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400552 inputCount,
553 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400554 lookup_context.funcs.apply);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400555}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400556
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400557struct ChainRule
558{
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400559 friend struct ChainRuleSet;
560
561 private:
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400562 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400563 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400564 TRACE_APPLY ();
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400565 const HeadlessArrayOf<USHORT> &input = CONST_NEXT (HeadlessArrayOf<USHORT>, backtrack);
566 const ArrayOf<USHORT> &lookahead = CONST_NEXT (ArrayOf<USHORT>, input);
567 const ArrayOf<LookupRecord> &lookup = CONST_NEXT (ArrayOf<LookupRecord>, lookahead);
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400568 return chain_context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400569 backtrack.len, backtrack.array,
570 input.len, input.array + 1,
571 lookahead.len, lookahead.array,
572 lookup.len, lookup.array,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400573 lookup_context);
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400574 return false;
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400575 }
576
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400577 public:
578 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400579 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400580 if (!SANITIZE (backtrack)) return false;
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400581 HeadlessArrayOf<USHORT> &input = NEXT (HeadlessArrayOf<USHORT>, backtrack);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400582 if (!SANITIZE (input)) return false;
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400583 ArrayOf<USHORT> &lookahead = NEXT (ArrayOf<USHORT>, input);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400584 if (!SANITIZE (lookahead)) return false;
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400585 ArrayOf<LookupRecord> &lookup = NEXT (ArrayOf<LookupRecord>, lookahead);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400586 return SANITIZE (lookup);
587 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400588
589 private:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400590 ArrayOf<USHORT>
591 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400592 * (to be matched before the input
593 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400594 HeadlessArrayOf<USHORT>
595 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400596 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400597 ArrayOf<USHORT>
598 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400599 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400600 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400601 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400602 * design order) */
603};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400604ASSERT_SIZE (ChainRule, 8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400605
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400606struct ChainRuleSet
607{
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400608 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400609 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400610 TRACE_APPLY ();
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400611 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400612 for (unsigned int i = 0; i < num_rules; i++)
613 {
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400614 if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400615 return true;
616 }
617
618 return false;
619 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400620
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400621 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400622 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400623 return SANITIZE_THIS (rule);
624 }
625
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400626 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400627 OffsetArrayOf<ChainRule>
628 rule; /* Array of ChainRule tables
629 * ordered by preference */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400630};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400631ASSERT_SIZE (ChainRuleSet, 2);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400632
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400633struct ChainContextFormat1
634{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400635 friend struct ChainContext;
636
637 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400638 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
639 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400640 TRACE_APPLY ();
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400641 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400642 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400643 return false;
644
645 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400646 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400647 {match_glyph, apply_func},
648 {NULL, NULL, NULL}
649 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400650 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400651 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400652
653 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400654 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400655 return SANITIZE_THIS2 (coverage, ruleSet);
656 }
657
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400658 private:
659 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400660 OffsetTo<Coverage>
661 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400662 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400663 OffsetArrayOf<ChainRuleSet>
664 ruleSet; /* Array of ChainRuleSet tables
665 * ordered by Coverage Index */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400666};
667ASSERT_SIZE (ChainContextFormat1, 6);
668
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400669struct ChainContextFormat2
670{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400671 friend struct ChainContext;
672
673 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400674 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
675 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400676 TRACE_APPLY ();
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400677 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400678 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400679 return false;
680
681 const ClassDef &backtrack_class_def = this+backtrackClassDef;
682 const ClassDef &input_class_def = this+inputClassDef;
683 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
684
685 index = input_class_def (IN_CURGLYPH ());
686 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400687 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
688 * them across subrule lookups. Not sure it's worth it.
689 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400690 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400691 {match_class, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400692 {DECONST_CHARP(&backtrack_class_def),
693 DECONST_CHARP(&input_class_def),
694 DECONST_CHARP(&lookahead_class_def)}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400695 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400696 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400697 }
698
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400699 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400700 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400701 return SANITIZE_THIS2 (coverage, backtrackClassDef) &&
702 SANITIZE_THIS2 (inputClassDef, lookaheadClassDef) &&
703 SANITIZE_THIS (ruleSet);
704 }
705
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400706 private:
707 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400708 OffsetTo<Coverage>
709 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400710 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400711 OffsetTo<ClassDef>
712 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400713 * containing backtrack sequence
714 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400715 OffsetTo<ClassDef>
716 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400717 * table containing input sequence
718 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400719 OffsetTo<ClassDef>
720 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400721 * containing lookahead sequence
722 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400723 OffsetArrayOf<ChainRuleSet>
724 ruleSet; /* Array of ChainRuleSet tables
725 * ordered by class */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400726};
727ASSERT_SIZE (ChainContextFormat2, 12);
728
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400729struct ChainContextFormat3
730{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400731 friend struct ChainContext;
732
733 private:
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400734
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400735 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
736 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400737 TRACE_APPLY ();
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400738 const OffsetArrayOf<Coverage> &input = CONST_NEXT (OffsetArrayOf<Coverage>, backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400739
740 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400741 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400742 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400743
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400744 const OffsetArrayOf<Coverage> &lookahead = CONST_NEXT (OffsetArrayOf<Coverage>, input);
745 const ArrayOf<LookupRecord> &lookup = CONST_NEXT (ArrayOf<LookupRecord>, lookahead);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400746 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400747 {match_coverage, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400748 {DECONST_CHARP(this), DECONST_CHARP(this), DECONST_CHARP(this)}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400749 };
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400750 return chain_context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400751 backtrack.len, (USHORT *) backtrack.array,
752 input.len, (USHORT *) input.array,
753 lookahead.len, (USHORT *) lookahead.array,
754 lookup.len, lookup.array,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400755 lookup_context);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400756 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400757 }
758
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400759 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400760 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400761 if (!SANITIZE_THIS (backtrack)) return false;
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400762 OffsetArrayOf<Coverage> &input = NEXT (OffsetArrayOf<Coverage>, backtrack);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400763 if (!SANITIZE_THIS (input)) return false;
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400764 OffsetArrayOf<Coverage> &lookahead = NEXT (OffsetArrayOf<Coverage>, input);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400765 if (!SANITIZE_THIS (lookahead)) return false;
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400766 ArrayOf<LookupRecord> &lookup = NEXT (ArrayOf<LookupRecord>, lookahead);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400767 return SANITIZE (lookup);
768 }
769
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400770 private:
771 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400772 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400773 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400774 * in backtracking sequence, in glyph
775 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400776 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400777 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400778 * tables in input sequence, in glyph
779 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400780 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400781 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400782 * in lookahead sequence, in glyph
783 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400784 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400785 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400786 * design order) */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400787};
788ASSERT_SIZE (ChainContextFormat3, 10);
789
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400790struct ChainContext
791{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400792 protected:
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400793 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400794 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400795 TRACE_APPLY ();
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400796 switch (u.format) {
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400797 case 1: return u.format1->apply (APPLY_ARG, apply_func);
798 case 2: return u.format2->apply (APPLY_ARG, apply_func);
799 case 3: return u.format3->apply (APPLY_ARG, apply_func);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400800 default:return false;
801 }
802 }
803
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400804 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400805 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400806 if (!SANITIZE (u.format)) return false;
807 switch (u.format) {
808 case 1: return u.format1->sanitize (SANITIZE_ARG);
809 case 2: return u.format2->sanitize (SANITIZE_ARG);
810 case 3: return u.format3->sanitize (SANITIZE_ARG);
811 default:return true;
812 }
813 }
814
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400815 private:
816 union {
817 USHORT format; /* Format identifier */
818 ChainContextFormat1 format1[];
819 ChainContextFormat2 format2[];
820 ChainContextFormat3 format3[];
821 } u;
822};
823ASSERT_SIZE (ChainContext, 2);
824
825
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400826struct ExtensionFormat1
827{
828 friend struct Extension;
829
Behdad Esfahbod18939482009-08-04 14:27:56 -0400830 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400831 inline unsigned int get_type (void) const { return extensionLookupType; }
832 inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
833 inline const LookupSubTable& get_subtable (void) const
834 {
835 unsigned int offset = get_offset ();
836 if (HB_UNLIKELY (!offset)) return Null(LookupSubTable);
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400837 return CONST_CAST (LookupSubTable, *this, offset);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400838 }
839
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400840 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400841 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400842 return SANITIZE_SELF ();
843 }
844
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400845 private:
846 USHORT format; /* Format identifier. Set to 1. */
847 USHORT extensionLookupType; /* Lookup type of subtable referenced
848 * by ExtensionOffset (i.e. the
849 * extension subtable). */
850 USHORT extensionOffset[2]; /* Offset to the extension subtable,
851 * of lookup type subtable.
852 * Defined as two shorts to avoid
853 * alignment requirements. */
854};
855ASSERT_SIZE (ExtensionFormat1, 8);
856
857struct Extension
858{
859 inline unsigned int get_type (void) const
860 {
861 switch (u.format) {
862 case 1: return u.format1->get_type ();
863 default:return 0;
864 }
865 }
866 inline const LookupSubTable& get_subtable (void) const
867 {
868 switch (u.format) {
869 case 1: return u.format1->get_subtable ();
870 default:return Null(LookupSubTable);
871 }
872 }
873
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400874 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400875 TRACE_SANITIZE ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400876 if (!SANITIZE (u.format)) return false;
877 switch (u.format) {
878 case 1: return u.format1->sanitize (SANITIZE_ARG);
879 default:return true;
880 }
881 }
882
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400883 private:
884 union {
885 USHORT format; /* Format identifier */
886 ExtensionFormat1 format1[];
887 } u;
888};
889ASSERT_SIZE (Extension, 2);
890
891
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400892/*
893 * GSUB/GPOS Common
894 */
895
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400896struct GSUBGPOS
897{
Behdad Esfahboda328d662009-08-04 20:27:05 -0400898 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
899 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400900
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400901 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (GSUBGPOS, 1, 1);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400902
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400903 inline unsigned int get_script_count (void) const
904 { return (this+scriptList).len; }
905 inline const Tag& get_script_tag (unsigned int i) const
906 { return (this+scriptList).get_tag (i); }
907 inline bool get_script_tags (unsigned int *script_count /* IN/OUT */,
908 hb_tag_t *script_tags /* OUT */) const
909 { return (this+scriptList).get_tags (script_count, script_tags); }
910 inline const Script& get_script (unsigned int i) const
911 { return (this+scriptList)[i]; }
912 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
913 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400914
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400915 inline unsigned int get_feature_count (void) const
916 { return (this+featureList).len; }
917 inline const Tag& get_feature_tag (unsigned int i) const
918 { return (this+featureList).get_tag (i); }
919 inline bool get_feature_tags (unsigned int *feature_count /* IN/OUT */,
920 hb_tag_t *feature_tags /* OUT */) const
921 { return (this+featureList).get_tags (feature_count, feature_tags); }
922 inline const Feature& get_feature (unsigned int i) const
923 { return (this+featureList)[i]; }
924 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
925 { return (this+featureList).find_index (tag, index); }
926
927 inline unsigned int get_lookup_count (void) const
928 { return (this+lookupList).len; }
929 inline const Lookup& get_lookup (unsigned int i) const
930 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400931
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400932 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400933 TRACE_SANITIZE ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400934 if (!SANITIZE (version)) return false;
935 if (version.major != 1) return true;
936 return SANITIZE_THIS3 (scriptList, featureList, lookupList);
937 }
938
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400939 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400940 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400941 * to 0x00010000 */
942 OffsetTo<ScriptList>
943 scriptList; /* ScriptList table */
944 OffsetTo<FeatureList>
945 featureList; /* FeatureList table */
946 OffsetTo<LookupList>
947 lookupList; /* LookupList table */
948};
949ASSERT_SIZE (GSUBGPOS, 10);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400950
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400951
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400952#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */