blob: 92521c5441802a23ea4851d62172591c3964f53c [file] [log] [blame]
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001/*
Behdad Esfahbod41a93d22010-05-06 12:55:14 -04002 * Copyright (C) 2007,2008,2009,2010 Red Hat, Inc.
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04003 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04004 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04005 *
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
Behdad Esfahbod74e313c2010-04-28 15:15:09 -040035#define HB_DEBUG_APPLY HB_DEBUG+0
Behdad Esfahbod0535b502009-08-28 17:14:33 -040036#endif
37
Behdad Esfahbodbc200452010-04-29 01:40:26 -040038#define TRACE_APPLY() \
Behdad Esfahbod6c42cdd2010-05-05 01:30:48 -040039 hb_trace_t<HB_DEBUG_APPLY> trace (&context->debug_depth); \
40 trace.log ("APPLY", HB_FUNC, this);
Behdad Esfahbod74e313c2010-04-28 15:15:09 -040041
Behdad Esfahbod0535b502009-08-28 17:14:33 -040042
Behdad Esfahbod1376fb72010-04-29 02:19:21 -040043struct hb_apply_context_t
44{
Behdad Esfahbod705e2152010-05-05 01:40:25 -040045 unsigned int debug_depth;
Behdad Esfahbod63493f92010-05-05 01:01:05 -040046 hb_ot_layout_context_t *layout;
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -040047 hb_buffer_t *buffer;
Behdad Esfahbod969c9702010-05-05 01:23:44 -040048 unsigned int context_length;
Behdad Esfahbod1376fb72010-04-29 02:19:21 -040049 unsigned int nesting_level_left;
50 unsigned int lookup_flag;
51 unsigned int property; /* propety of first glyph (TODO remove) */
52};
53
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040054
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -040055
56
57#undef BUFFER
58#define BUFFER context->buffer
59
60
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -040061typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -040062typedef bool (*apply_lookup_func_t) (hb_apply_context_t *context, unsigned int lookup_index);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040063
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040064struct ContextFuncs
65{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040066 match_func_t match;
67 apply_lookup_func_t apply;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040068};
69
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040070
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -040071static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040072{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040073 return glyph_id == value;
74}
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040075
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -040076static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040077{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -040078 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040079 return class_def.get_class (glyph_id) == value;
80}
81
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -040082static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040083{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -040084 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040085 return (data+coverage) (glyph_id) != NOT_COVERED;
86}
87
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040088
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -040089static inline bool match_input (hb_apply_context_t *context,
Behdad Esfahbode072c242009-05-18 03:47:31 -040090 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040091 const USHORT input[], /* Array of input values--start with second glyph */
92 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -040093 const void *match_data,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040094 unsigned int *context_length_out)
95{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040096 unsigned int i, j;
Behdad Esfahbod969c9702010-05-05 01:23:44 -040097 unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context->context_length);
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -040098 if (unlikely (context->buffer->in_pos + count > end))
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -040099 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400100
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400101 for (i = 1, j = context->buffer->in_pos + 1; i < count; i++, j++)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400102 {
Behdad Esfahbod63493f92010-05-05 01:01:05 -0400103 while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400104 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400105 if (unlikely (j + count - i == end))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400106 return false;
107 j++;
108 }
109
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400110 if (likely (!match_func (IN_GLYPH (j), input[i - 1], match_data)))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400111 return false;
112 }
113
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400114 *context_length_out = j - context->buffer->in_pos;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400115
116 return true;
117}
118
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400119static inline bool match_backtrack (hb_apply_context_t *context,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400120 unsigned int count,
121 const USHORT backtrack[],
122 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400123 const void *match_data)
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400124{
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400125 if (unlikely (context->buffer->out_pos < count))
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400126 return false;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400127
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400128 for (unsigned int i = 0, j = context->buffer->out_pos - 1; i < count; i++, j--)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400129 {
Behdad Esfahbod63493f92010-05-05 01:01:05 -0400130 while (_hb_ot_layout_skip_mark (context->layout->face, OUT_INFO (j), context->lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400131 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400132 if (unlikely (j + 1 == count - i))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400133 return false;
134 j--;
135 }
136
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400137 if (likely (!match_func (OUT_GLYPH (j), backtrack[i], match_data)))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400138 return false;
139 }
140
141 return true;
142}
143
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400144static inline bool match_lookahead (hb_apply_context_t *context,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400145 unsigned int count,
146 const USHORT lookahead[],
147 match_func_t match_func,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400148 const void *match_data,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400149 unsigned int offset)
150{
151 unsigned int i, j;
Behdad Esfahbod969c9702010-05-05 01:23:44 -0400152 unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context->context_length);
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400153 if (unlikely (context->buffer->in_pos + offset + count > end))
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400154 return false;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400155
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400156 for (i = 0, j = context->buffer->in_pos + offset; i < count; i++, j++)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400157 {
Behdad Esfahbod63493f92010-05-05 01:01:05 -0400158 while (_hb_ot_layout_skip_mark (context->layout->face, OUT_INFO (j), context->lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400159 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400160 if (unlikely (j + count - i == end))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400161 return false;
162 j++;
163 }
164
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400165 if (likely (!match_func (IN_GLYPH (j), lookahead[i], match_data)))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400166 return false;
167 }
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400168
169 return true;
170}
171
172
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400173struct LookupRecord
174{
Behdad Esfahbod39840472010-05-05 00:23:19 -0400175 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400176 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400177 return context->check_struct (this);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400178 }
179
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400180 USHORT sequenceIndex; /* Index into current glyph
181 * sequence--first glyph = 0 */
182 USHORT lookupListIndex; /* Lookup to apply to that
183 * position--zero--based */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400184 public:
185 DEFINE_SIZE_STATIC (4);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400186};
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400187
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400188static inline bool apply_lookup (hb_apply_context_t *context,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400189 unsigned int count, /* Including the first glyph */
190 unsigned int lookupCount,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400191 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
192 apply_lookup_func_t apply_func)
193{
Behdad Esfahbod969c9702010-05-05 01:23:44 -0400194 unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context->context_length);
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400195 if (unlikely (context->buffer->in_pos + count > end))
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400196 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400197
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400198 /* TODO We don't support lookupRecord arrays that are not increasing:
199 * Should be easy for in_place ones at least. */
Behdad Esfahbod52e9a712009-09-21 13:58:56 -0400200
201 /* Note: If sublookup is reverse, i will underflow after the first loop
202 * and we jump out of it. Not entirely disastrous. So we don't check
203 * for reverse lookup here.
204 */
Behdad Esfahbodf9c0a2d2009-09-21 13:43:54 -0400205 for (unsigned int i = 0; i < count; /* NOP */)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400206 {
Behdad Esfahbod63493f92010-05-05 01:01:05 -0400207 while (_hb_ot_layout_skip_mark (context->layout->face, IN_CURINFO (), context->lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400208 {
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400209 if (unlikely (context->buffer->in_pos == end))
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400210 return true;
211 /* No lookup applied for this index */
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400212 _hb_buffer_next_glyph (context->buffer);
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400213 }
214
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400215 if (lookupCount && i == lookupRecord->sequenceIndex)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400216 {
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400217 unsigned int old_pos = context->buffer->in_pos;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400218
219 /* Apply a lookup */
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400220 bool done = apply_func (context, lookupRecord->lookupListIndex);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400221
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400222 lookupRecord++;
223 lookupCount--;
Behdad Esfahbodf9c0a2d2009-09-21 13:43:54 -0400224 /* Err, this is wrong if the lookup jumped over some glyphs */
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400225 i += context->buffer->in_pos - old_pos;
226 if (unlikely (context->buffer->in_pos == end))
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400227 return true;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400228
229 if (!done)
230 goto not_applied;
231 }
232 else
233 {
234 not_applied:
235 /* No lookup applied for this index */
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400236 _hb_buffer_next_glyph (context->buffer);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400237 i++;
238 }
239 }
240
241 return true;
242}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400243
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400244
245/* Contextual lookups */
246
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400247struct ContextLookupContext
248{
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400249 ContextFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400250 const void *match_data;
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400251};
252
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400253static inline bool context_lookup (hb_apply_context_t *context,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400254 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400255 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400256 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400257 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400258 ContextLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400259{
Behdad Esfahbod41697102010-05-05 01:37:58 -0400260 hb_apply_context_t new_context = *context;
261 return match_input (context,
262 inputCount, input,
263 lookup_context.funcs.match, lookup_context.match_data,
264 &new_context.context_length)
265 && apply_lookup (&new_context,
266 inputCount,
267 lookupCount, lookupRecord,
268 lookup_context.funcs.apply);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400269}
270
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400271struct Rule
272{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400273 friend struct RuleSet;
274
275 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400276 inline bool apply (hb_apply_context_t *context, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400277 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400278 TRACE_APPLY ();
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400279 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400280 return context_lookup (context,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400281 inputCount, input,
Behdad Esfahbodfff9aa22010-05-05 00:32:21 -0400282 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400283 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400284 }
285
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400286 public:
Behdad Esfahbod39840472010-05-05 00:23:19 -0400287 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400288 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400289 return inputCount.sanitize (context)
290 && lookupCount.sanitize (context)
Behdad Esfahbodf5fab0c2010-05-06 10:26:52 -0400291 && context->check_range (input,
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400292 input[0].static_size * inputCount
293 + lookupRecordX[0].static_size * lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400294 }
295
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400296 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400297 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400298 * glyph sequence--includes the first
299 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400300 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500301 USHORT input[VAR]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400302 * second glyph */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500303 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400304 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400305 public:
306 DEFINE_SIZE_VAR2 (4, USHORT, LookupRecord);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400307};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400308
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400309struct RuleSet
310{
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400311 inline bool apply (hb_apply_context_t *context, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400312 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400313 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400314 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400315 for (unsigned int i = 0; i < num_rules; i++)
316 {
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400317 if ((this+rule[i]).apply (context, lookup_context))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400318 return true;
319 }
320
321 return false;
322 }
323
Behdad Esfahbod39840472010-05-05 00:23:19 -0400324 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400325 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400326 return rule.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400327 }
328
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400329 private:
330 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400331 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400332 * ordered by preference */
Behdad Esfahboded074222010-05-10 18:08:46 -0400333 public:
334 DEFINE_SIZE_VAR (2, OffsetTo<Rule>);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400335};
336
337
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400338struct ContextFormat1
339{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400340 friend struct Context;
341
342 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400343 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400344 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400345 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400346 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400347 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400348 return false;
349
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400350 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400351 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400352 {match_glyph, apply_func},
353 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400354 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400355 return rule_set.apply (context, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400356 }
357
Behdad Esfahbod39840472010-05-05 00:23:19 -0400358 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400359 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400360 return coverage.sanitize (context, this)
361 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400362 }
363
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400364 private:
365 USHORT format; /* Format identifier--format = 1 */
366 OffsetTo<Coverage>
367 coverage; /* Offset to Coverage table--from
368 * beginning of table */
369 OffsetArrayOf<RuleSet>
370 ruleSet; /* Array of RuleSet tables
371 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400372 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400373 DEFINE_SIZE_VAR (6, OffsetTo<RuleSet>);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400374};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400375
376
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400377struct ContextFormat2
378{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400379 friend struct Context;
380
381 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400382 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400383 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400384 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400385 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400386 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400387 return false;
388
389 const ClassDef &class_def = this+classDef;
390 index = class_def (IN_CURGLYPH ());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400391 const RuleSet &rule_set = this+ruleSet[index];
392 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
393 * them across subrule lookups. Not sure it's worth it.
394 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400395 struct ContextLookupContext lookup_context = {
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400396 {match_class, apply_func},
397 &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400398 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400399 return rule_set.apply (context, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400400 }
401
Behdad Esfahbod39840472010-05-05 00:23:19 -0400402 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400403 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400404 return coverage.sanitize (context, this)
405 && classDef.sanitize (context, this)
406 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400407 }
408
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400409 private:
410 USHORT format; /* Format identifier--format = 2 */
411 OffsetTo<Coverage>
412 coverage; /* Offset to Coverage table--from
413 * beginning of table */
414 OffsetTo<ClassDef>
415 classDef; /* Offset to glyph ClassDef table--from
416 * beginning of table */
417 OffsetArrayOf<RuleSet>
418 ruleSet; /* Array of RuleSet tables
419 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400420 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400421 DEFINE_SIZE_VAR (8, OffsetTo<RuleSet>);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400422};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400423
424
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400425struct ContextFormat3
426{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400427 friend struct Context;
428
429 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400430 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400431 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400432 TRACE_APPLY ();
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400433 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400434 if (likely (index == NOT_COVERED))
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400435 return false;
436
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400437 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400438 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400439 {match_coverage, apply_func},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400440 this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400441 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400442 return context_lookup (context,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400443 glyphCount, (const USHORT *) (coverage + 1),
Behdad Esfahbodfff9aa22010-05-05 00:32:21 -0400444 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400445 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400446 }
447
Behdad Esfahbod39840472010-05-05 00:23:19 -0400448 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400449 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400450 if (!context->check_struct (this)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400451 unsigned int count = glyphCount;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400452 if (!context->check_array (coverage, coverage[0].static_size, count)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400453 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400454 if (!coverage[i].sanitize (context, this)) return false;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400455 LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
456 return context->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400457 }
458
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400459 private:
460 USHORT format; /* Format identifier--format = 3 */
461 USHORT glyphCount; /* Number of glyphs in the input glyph
462 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400463 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400464 OffsetTo<Coverage>
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500465 coverage[VAR]; /* Array of offsets to Coverage
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400466 * table in glyph sequence order */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500467 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400468 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400469 public:
470 DEFINE_SIZE_VAR2 (6, OffsetTo<Coverage>, LookupRecord);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400471};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400472
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400473struct Context
474{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400475 protected:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400476 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400477 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400478 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400479 switch (u.format) {
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400480 case 1: return u.format1->apply (context, apply_func);
481 case 2: return u.format2->apply (context, apply_func);
482 case 3: return u.format3->apply (context, apply_func);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400483 default:return false;
484 }
485 }
486
Behdad Esfahbod39840472010-05-05 00:23:19 -0400487 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400488 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400489 if (!u.format.sanitize (context)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400490 switch (u.format) {
Behdad Esfahbod39840472010-05-05 00:23:19 -0400491 case 1: return u.format1->sanitize (context);
492 case 2: return u.format2->sanitize (context);
493 case 3: return u.format3->sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400494 default:return true;
495 }
496 }
497
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400498 private:
499 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400500 USHORT format; /* Format identifier */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500501 ContextFormat1 format1[VAR];
502 ContextFormat2 format2[VAR];
503 ContextFormat3 format3[VAR];
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400504 } u;
505};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400506
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400507
508/* Chaining Contextual lookups */
509
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400510struct ChainContextLookupContext
511{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400512 ContextFuncs funcs;
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400513 const void *match_data[3];
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400514};
515
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400516static inline bool chain_context_lookup (hb_apply_context_t *context,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400517 unsigned int backtrackCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400518 const USHORT backtrack[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400519 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400520 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400521 unsigned int lookaheadCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400522 const USHORT lookahead[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400523 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400524 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400525 ChainContextLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400526{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400527 /* First guess */
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400528 if (unlikely (context->buffer->out_pos < backtrackCount ||
529 context->buffer->in_pos + inputCount + lookaheadCount > context->buffer->in_length ||
Behdad Esfahbod969c9702010-05-05 01:23:44 -0400530 inputCount + lookaheadCount > context->context_length))
Behdad Esfahbode072c242009-05-18 03:47:31 -0400531 return false;
532
Behdad Esfahbod41697102010-05-05 01:37:58 -0400533 hb_apply_context_t new_context = *context;
534 return match_backtrack (context,
535 backtrackCount, backtrack,
536 lookup_context.funcs.match, lookup_context.match_data[0])
537 && match_input (context,
538 inputCount, input,
539 lookup_context.funcs.match, lookup_context.match_data[1],
540 &new_context.context_length)
541 && match_lookahead (context,
542 lookaheadCount, lookahead,
543 lookup_context.funcs.match, lookup_context.match_data[2],
544 new_context.context_length)
545 && apply_lookup (&new_context,
546 inputCount,
547 lookupCount, lookupRecord,
548 lookup_context.funcs.apply);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400549}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400550
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400551struct ChainRule
552{
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400553 friend struct ChainRuleSet;
554
555 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400556 inline bool apply (hb_apply_context_t *context, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400557 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400558 TRACE_APPLY ();
Behdad Esfahbode961c862010-04-21 15:56:11 -0400559 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
560 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
561 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400562 return chain_context_lookup (context,
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400563 backtrack.len, backtrack.array(),
564 input.len, input.array(),
565 lookahead.len, lookahead.array(),
566 lookup.len, lookup.array(),
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400567 lookup_context);
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400568 return false;
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400569 }
570
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400571 public:
Behdad Esfahbod39840472010-05-05 00:23:19 -0400572 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400573 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400574 if (!backtrack.sanitize (context)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400575 HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400576 if (!input.sanitize (context)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400577 ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400578 if (!lookahead.sanitize (context)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400579 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400580 return lookup.sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400581 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400582
583 private:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400584 ArrayOf<USHORT>
585 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400586 * (to be matched before the input
587 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400588 HeadlessArrayOf<USHORT>
589 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400590 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400591 ArrayOf<USHORT>
592 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400593 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400594 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400595 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400596 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400597 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400598 DEFINE_SIZE_MIN (8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400599};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400600
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400601struct ChainRuleSet
602{
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400603 inline bool apply (hb_apply_context_t *context, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400604 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400605 TRACE_APPLY ();
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400606 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400607 for (unsigned int i = 0; i < num_rules; i++)
608 {
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400609 if ((this+rule[i]).apply (context, lookup_context))
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400610 return true;
611 }
612
613 return false;
614 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400615
Behdad Esfahbod39840472010-05-05 00:23:19 -0400616 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400617 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400618 return rule.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400619 }
620
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400621 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400622 OffsetArrayOf<ChainRule>
623 rule; /* Array of ChainRule tables
624 * ordered by preference */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400625 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400626 DEFINE_SIZE_VAR (2, OffsetTo<ChainRule>);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400627};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400628
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400629struct ChainContextFormat1
630{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400631 friend struct ChainContext;
632
633 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400634 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400635 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400636 TRACE_APPLY ();
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400637 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400638 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400639 return false;
640
641 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400642 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400643 {match_glyph, apply_func},
644 {NULL, NULL, NULL}
645 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400646 return rule_set.apply (context, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400647 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400648
Behdad Esfahbod39840472010-05-05 00:23:19 -0400649 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400650 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400651 return coverage.sanitize (context, this)
652 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400653 }
654
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400655 private:
656 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400657 OffsetTo<Coverage>
658 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400659 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400660 OffsetArrayOf<ChainRuleSet>
661 ruleSet; /* Array of ChainRuleSet tables
662 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400663 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400664 DEFINE_SIZE_VAR (6, OffsetTo<ChainRuleSet>);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400665};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400666
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400667struct ChainContextFormat2
668{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400669 friend struct ChainContext;
670
671 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400672 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400673 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400674 TRACE_APPLY ();
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400675 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400676 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400677 return false;
678
679 const ClassDef &backtrack_class_def = this+backtrackClassDef;
680 const ClassDef &input_class_def = this+inputClassDef;
681 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
682
683 index = input_class_def (IN_CURGLYPH ());
684 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400685 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
686 * them across subrule lookups. Not sure it's worth it.
687 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400688 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400689 {match_class, apply_func},
690 {&backtrack_class_def,
691 &input_class_def,
692 &lookahead_class_def}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400693 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400694 return rule_set.apply (context, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400695 }
696
Behdad Esfahbod39840472010-05-05 00:23:19 -0400697 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400698 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400699 return coverage.sanitize (context, this)
700 && backtrackClassDef.sanitize (context, this)
701 && inputClassDef.sanitize (context, this)
702 && lookaheadClassDef.sanitize (context, this)
703 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400704 }
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 Esfahbodb3651232010-05-10 16:57:29 -0400726 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400727 DEFINE_SIZE_VAR (12, OffsetTo<ChainRuleSet>);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400728};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400729
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400730struct ChainContextFormat3
731{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400732 friend struct ChainContext;
733
734 private:
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400735
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400736 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400737 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400738 TRACE_APPLY ();
Behdad Esfahbode961c862010-04-21 15:56:11 -0400739 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400740
741 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400742 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400743 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400744
Behdad Esfahbode961c862010-04-21 15:56:11 -0400745 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
746 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400747 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400748 {match_coverage, apply_func},
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400749 {this, this, this}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400750 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400751 return chain_context_lookup (context,
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400752 backtrack.len, (const USHORT *) backtrack.array(),
753 input.len, (const USHORT *) input.array() + 1,
754 lookahead.len, (const USHORT *) lookahead.array(),
755 lookup.len, lookup.array(),
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400756 lookup_context);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400757 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400758 }
759
Behdad Esfahbod39840472010-05-05 00:23:19 -0400760 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400761 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400762 if (!backtrack.sanitize (context, this)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400763 OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400764 if (!input.sanitize (context, this)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400765 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400766 if (!lookahead.sanitize (context, this)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400767 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400768 return lookup.sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400769 }
770
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400771 private:
772 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400773 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400774 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400775 * in backtracking sequence, in glyph
776 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400777 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400778 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400779 * tables in input sequence, in glyph
780 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400781 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400782 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400783 * in lookahead sequence, in glyph
784 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400785 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400786 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400787 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400788 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400789 DEFINE_SIZE_MIN (10);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400790};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400791
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400792struct ChainContext
793{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400794 protected:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400795 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400796 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400797 TRACE_APPLY ();
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400798 switch (u.format) {
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400799 case 1: return u.format1->apply (context, apply_func);
800 case 2: return u.format2->apply (context, apply_func);
801 case 3: return u.format3->apply (context, apply_func);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400802 default:return false;
803 }
804 }
805
Behdad Esfahbod39840472010-05-05 00:23:19 -0400806 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400807 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400808 if (!u.format.sanitize (context)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400809 switch (u.format) {
Behdad Esfahbod39840472010-05-05 00:23:19 -0400810 case 1: return u.format1->sanitize (context);
811 case 2: return u.format2->sanitize (context);
812 case 3: return u.format3->sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400813 default:return true;
814 }
815 }
816
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400817 private:
818 union {
819 USHORT format; /* Format identifier */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500820 ChainContextFormat1 format1[VAR];
821 ChainContextFormat2 format2[VAR];
822 ChainContextFormat3 format3[VAR];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400823 } u;
824};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400825
826
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400827struct ExtensionFormat1
828{
829 friend struct Extension;
830
Behdad Esfahbod18939482009-08-04 14:27:56 -0400831 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400832 inline unsigned int get_type (void) const { return extensionLookupType; }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -0400833 inline unsigned int get_offset (void) const { return extensionOffset; }
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400834
Behdad Esfahbod39840472010-05-05 00:23:19 -0400835 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400836 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400837 return context->check_struct (this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400838 }
839
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400840 private:
841 USHORT format; /* Format identifier. Set to 1. */
842 USHORT extensionLookupType; /* Lookup type of subtable referenced
843 * by ExtensionOffset (i.e. the
844 * extension subtable). */
Behdad Esfahbod81f2af42010-04-22 00:58:49 -0400845 ULONG extensionOffset; /* Offset to the extension subtable,
846 * of lookup type subtable. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400847 public:
848 DEFINE_SIZE_STATIC (8);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400849};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400850
851struct Extension
852{
853 inline unsigned int get_type (void) const
854 {
855 switch (u.format) {
856 case 1: return u.format1->get_type ();
857 default:return 0;
858 }
859 }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -0400860 inline unsigned int get_offset (void) const
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400861 {
862 switch (u.format) {
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -0400863 case 1: return u.format1->get_offset ();
864 default:return 0;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400865 }
866 }
867
Behdad Esfahbod39840472010-05-05 00:23:19 -0400868 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400869 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400870 if (!u.format.sanitize (context)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400871 switch (u.format) {
Behdad Esfahbod39840472010-05-05 00:23:19 -0400872 case 1: return u.format1->sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400873 default:return true;
874 }
875 }
876
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400877 private:
878 union {
879 USHORT format; /* Format identifier */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500880 ExtensionFormat1 format1[VAR];
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400881 } u;
882};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400883
884
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400885/*
886 * GSUB/GPOS Common
887 */
888
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400889struct GSUBGPOS
890{
Behdad Esfahboda328d662009-08-04 20:27:05 -0400891 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
892 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400893
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400894 inline unsigned int get_script_count (void) const
895 { return (this+scriptList).len; }
896 inline const Tag& get_script_tag (unsigned int i) const
897 { return (this+scriptList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500898 inline unsigned int get_script_tags (unsigned int start_offset,
899 unsigned int *script_count /* IN/OUT */,
900 hb_tag_t *script_tags /* OUT */) const
901 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400902 inline const Script& get_script (unsigned int i) const
903 { return (this+scriptList)[i]; }
904 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
905 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400906
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400907 inline unsigned int get_feature_count (void) const
908 { return (this+featureList).len; }
909 inline const Tag& get_feature_tag (unsigned int i) const
910 { return (this+featureList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500911 inline unsigned int get_feature_tags (unsigned int start_offset,
912 unsigned int *feature_count /* IN/OUT */,
913 hb_tag_t *feature_tags /* OUT */) const
914 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400915 inline const Feature& get_feature (unsigned int i) const
916 { return (this+featureList)[i]; }
917 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
918 { return (this+featureList).find_index (tag, index); }
919
920 inline unsigned int get_lookup_count (void) const
921 { return (this+lookupList).len; }
922 inline const Lookup& get_lookup (unsigned int i) const
923 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400924
Behdad Esfahbod39840472010-05-05 00:23:19 -0400925 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400926 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400927 return version.sanitize (context) && likely (version.major == 1)
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400928 && scriptList.sanitize (context, this)
929 && featureList.sanitize (context, this)
930 && lookupList.sanitize (context, this);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400931 }
932
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400933 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400934 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400935 * to 0x00010000 */
936 OffsetTo<ScriptList>
937 scriptList; /* ScriptList table */
938 OffsetTo<FeatureList>
939 featureList; /* FeatureList table */
940 OffsetTo<LookupList>
941 lookupList; /* LookupList table */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400942 public:
943 DEFINE_SIZE_STATIC (10);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400944};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400945
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400946
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400947#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */