blob: 32ece001c29d99703ced84f3df5eff629f2b514c [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 Esfahbod1922ffe2010-04-21 04:19:51 -040061typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const char *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 Esfahbod33d13fd2010-04-29 13:56:44 -040071static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const char *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 Esfahbod1922ffe2010-04-21 04:19:51 -040076static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const char *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 Esfahbod1922ffe2010-04-21 04:19:51 -040082static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const char *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 Esfahbod1922ffe2010-04-21 04:19:51 -040093 const char *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 Esfahbod1922ffe2010-04-21 04:19:51 -0400123 const char *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 Esfahbod1922ffe2010-04-21 04:19:51 -0400148 const char *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 Esfahbod1922ffe2010-04-21 04:19:51 -0400250 const char *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 */
333};
334
335
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400336struct ContextFormat1
337{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400338 friend struct Context;
339
340 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400341 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400342 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400343 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400344 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400345 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400346 return false;
347
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400348 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400349 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400350 {match_glyph, apply_func},
351 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400352 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400353 return rule_set.apply (context, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400354 }
355
Behdad Esfahbod39840472010-05-05 00:23:19 -0400356 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400357 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400358 return coverage.sanitize (context, this)
359 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400360 }
361
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400362 private:
363 USHORT format; /* Format identifier--format = 1 */
364 OffsetTo<Coverage>
365 coverage; /* Offset to Coverage table--from
366 * beginning of table */
367 OffsetArrayOf<RuleSet>
368 ruleSet; /* Array of RuleSet tables
369 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400370 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400371 DEFINE_SIZE_VAR (6, OffsetTo<RuleSet>);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400372};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400373
374
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400375struct ContextFormat2
376{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400377 friend struct Context;
378
379 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400380 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400381 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400382 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400383 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400384 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400385 return false;
386
387 const ClassDef &class_def = this+classDef;
388 index = class_def (IN_CURGLYPH ());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400389 const RuleSet &rule_set = this+ruleSet[index];
390 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
391 * them across subrule lookups. Not sure it's worth it.
392 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400393 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400394 {match_class, apply_func},
Behdad Esfahbod0dfcc132010-04-21 23:41:26 -0400395 CharP(&class_def)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400396 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400397 return rule_set.apply (context, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400398 }
399
Behdad Esfahbod39840472010-05-05 00:23:19 -0400400 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400401 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400402 return coverage.sanitize (context, this)
403 && classDef.sanitize (context, this)
404 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400405 }
406
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400407 private:
408 USHORT format; /* Format identifier--format = 2 */
409 OffsetTo<Coverage>
410 coverage; /* Offset to Coverage table--from
411 * beginning of table */
412 OffsetTo<ClassDef>
413 classDef; /* Offset to glyph ClassDef table--from
414 * beginning of table */
415 OffsetArrayOf<RuleSet>
416 ruleSet; /* Array of RuleSet tables
417 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400418 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400419 DEFINE_SIZE_VAR (8, OffsetTo<RuleSet>);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400420};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400421
422
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400423struct ContextFormat3
424{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400425 friend struct Context;
426
427 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400428 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400429 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400430 TRACE_APPLY ();
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400431 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400432 if (likely (index == NOT_COVERED))
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400433 return false;
434
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400435 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400436 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400437 {match_coverage, apply_func},
Behdad Esfahbod0dfcc132010-04-21 23:41:26 -0400438 CharP(this)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400439 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400440 return context_lookup (context,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400441 glyphCount, (const USHORT *) (coverage + 1),
Behdad Esfahbodfff9aa22010-05-05 00:32:21 -0400442 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400443 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400444 }
445
Behdad Esfahbod39840472010-05-05 00:23:19 -0400446 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400447 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400448 if (!context->check_struct (this)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400449 unsigned int count = glyphCount;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400450 if (!context->check_array (coverage, coverage[0].static_size, count)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400451 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400452 if (!coverage[i].sanitize (context, this)) return false;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400453 LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
454 return context->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400455 }
456
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400457 private:
458 USHORT format; /* Format identifier--format = 3 */
459 USHORT glyphCount; /* Number of glyphs in the input glyph
460 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400461 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400462 OffsetTo<Coverage>
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500463 coverage[VAR]; /* Array of offsets to Coverage
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400464 * table in glyph sequence order */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500465 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400466 * design order */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400467 public:
468 DEFINE_SIZE_VAR2 (6, OffsetTo<Coverage>, LookupRecord);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400469};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400470
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400471struct Context
472{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400473 protected:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400474 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400475 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400476 TRACE_APPLY ();
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400477 switch (u.format) {
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400478 case 1: return u.format1->apply (context, apply_func);
479 case 2: return u.format2->apply (context, apply_func);
480 case 3: return u.format3->apply (context, apply_func);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400481 default:return false;
482 }
483 }
484
Behdad Esfahbod39840472010-05-05 00:23:19 -0400485 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400486 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400487 if (!u.format.sanitize (context)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400488 switch (u.format) {
Behdad Esfahbod39840472010-05-05 00:23:19 -0400489 case 1: return u.format1->sanitize (context);
490 case 2: return u.format2->sanitize (context);
491 case 3: return u.format3->sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400492 default:return true;
493 }
494 }
495
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400496 private:
497 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400498 USHORT format; /* Format identifier */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500499 ContextFormat1 format1[VAR];
500 ContextFormat2 format2[VAR];
501 ContextFormat3 format3[VAR];
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400502 } u;
503};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400504
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400505
506/* Chaining Contextual lookups */
507
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400508struct ChainContextLookupContext
509{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400510 ContextFuncs funcs;
Behdad Esfahbod1922ffe2010-04-21 04:19:51 -0400511 const char *match_data[3];
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400512};
513
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400514static inline bool chain_context_lookup (hb_apply_context_t *context,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400515 unsigned int backtrackCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400516 const USHORT backtrack[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400517 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400518 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400519 unsigned int lookaheadCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400520 const USHORT lookahead[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400521 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400522 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400523 ChainContextLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400524{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400525 /* First guess */
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -0400526 if (unlikely (context->buffer->out_pos < backtrackCount ||
527 context->buffer->in_pos + inputCount + lookaheadCount > context->buffer->in_length ||
Behdad Esfahbod969c9702010-05-05 01:23:44 -0400528 inputCount + lookaheadCount > context->context_length))
Behdad Esfahbode072c242009-05-18 03:47:31 -0400529 return false;
530
Behdad Esfahbod41697102010-05-05 01:37:58 -0400531 hb_apply_context_t new_context = *context;
532 return match_backtrack (context,
533 backtrackCount, backtrack,
534 lookup_context.funcs.match, lookup_context.match_data[0])
535 && match_input (context,
536 inputCount, input,
537 lookup_context.funcs.match, lookup_context.match_data[1],
538 &new_context.context_length)
539 && match_lookahead (context,
540 lookaheadCount, lookahead,
541 lookup_context.funcs.match, lookup_context.match_data[2],
542 new_context.context_length)
543 && apply_lookup (&new_context,
544 inputCount,
545 lookupCount, lookupRecord,
546 lookup_context.funcs.apply);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400547}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400548
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400549struct ChainRule
550{
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400551 friend struct ChainRuleSet;
552
553 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400554 inline bool apply (hb_apply_context_t *context, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400555 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400556 TRACE_APPLY ();
Behdad Esfahbode961c862010-04-21 15:56:11 -0400557 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
558 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
559 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400560 return chain_context_lookup (context,
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400561 backtrack.len, backtrack.array(),
562 input.len, input.array(),
563 lookahead.len, lookahead.array(),
564 lookup.len, lookup.array(),
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400565 lookup_context);
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400566 return false;
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400567 }
568
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400569 public:
Behdad Esfahbod39840472010-05-05 00:23:19 -0400570 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400571 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400572 if (!backtrack.sanitize (context)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400573 HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400574 if (!input.sanitize (context)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400575 ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400576 if (!lookahead.sanitize (context)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400577 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400578 return lookup.sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400579 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400580
581 private:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400582 ArrayOf<USHORT>
583 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400584 * (to be matched before the input
585 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400586 HeadlessArrayOf<USHORT>
587 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400588 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400589 ArrayOf<USHORT>
590 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400591 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400592 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400593 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400594 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400595 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400596 DEFINE_SIZE_MIN (8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400597};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400598
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400599struct ChainRuleSet
600{
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400601 inline bool apply (hb_apply_context_t *context, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400602 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400603 TRACE_APPLY ();
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400604 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400605 for (unsigned int i = 0; i < num_rules; i++)
606 {
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400607 if ((this+rule[i]).apply (context, lookup_context))
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400608 return true;
609 }
610
611 return false;
612 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400613
Behdad Esfahbod39840472010-05-05 00:23:19 -0400614 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400615 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400616 return rule.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400617 }
618
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400619 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400620 OffsetArrayOf<ChainRule>
621 rule; /* Array of ChainRule tables
622 * ordered by preference */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400623 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400624 DEFINE_SIZE_VAR (2, OffsetTo<ChainRule>);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400625};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400626
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400627struct ChainContextFormat1
628{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400629 friend struct ChainContext;
630
631 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400632 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400633 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400634 TRACE_APPLY ();
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400635 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400636 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400637 return false;
638
639 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400640 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400641 {match_glyph, apply_func},
642 {NULL, NULL, NULL}
643 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400644 return rule_set.apply (context, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400645 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400646
Behdad Esfahbod39840472010-05-05 00:23:19 -0400647 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400648 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400649 return coverage.sanitize (context, this)
650 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400651 }
652
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400653 private:
654 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400655 OffsetTo<Coverage>
656 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400657 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400658 OffsetArrayOf<ChainRuleSet>
659 ruleSet; /* Array of ChainRuleSet tables
660 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400661 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400662 DEFINE_SIZE_VAR (6, OffsetTo<ChainRuleSet>);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400663};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400664
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400665struct ChainContextFormat2
666{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400667 friend struct ChainContext;
668
669 private:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400670 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400671 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400672 TRACE_APPLY ();
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400673 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400674 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400675 return false;
676
677 const ClassDef &backtrack_class_def = this+backtrackClassDef;
678 const ClassDef &input_class_def = this+inputClassDef;
679 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
680
681 index = input_class_def (IN_CURGLYPH ());
682 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400683 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
684 * them across subrule lookups. Not sure it's worth it.
685 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400686 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400687 {match_class, apply_func},
Behdad Esfahbod0dfcc132010-04-21 23:41:26 -0400688 {CharP(&backtrack_class_def),
689 CharP(&input_class_def),
690 CharP(&lookahead_class_def)}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400691 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400692 return rule_set.apply (context, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400693 }
694
Behdad Esfahbod39840472010-05-05 00:23:19 -0400695 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400696 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400697 return coverage.sanitize (context, this)
698 && backtrackClassDef.sanitize (context, this)
699 && inputClassDef.sanitize (context, this)
700 && lookaheadClassDef.sanitize (context, this)
701 && ruleSet.sanitize (context, this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400702 }
703
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400704 private:
705 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400706 OffsetTo<Coverage>
707 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400708 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400709 OffsetTo<ClassDef>
710 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400711 * containing backtrack sequence
712 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400713 OffsetTo<ClassDef>
714 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400715 * table containing input sequence
716 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400717 OffsetTo<ClassDef>
718 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400719 * containing lookahead sequence
720 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400721 OffsetArrayOf<ChainRuleSet>
722 ruleSet; /* Array of ChainRuleSet tables
723 * ordered by class */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400724 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400725 DEFINE_SIZE_VAR (12, OffsetTo<ChainRuleSet>);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400726};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400727
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400728struct ChainContextFormat3
729{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400730 friend struct ChainContext;
731
732 private:
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400733
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400734 inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400735 {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400736 TRACE_APPLY ();
Behdad Esfahbode961c862010-04-21 15:56:11 -0400737 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400738
739 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400740 if (likely (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400741 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400742
Behdad Esfahbode961c862010-04-21 15:56:11 -0400743 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
744 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400745 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400746 {match_coverage, apply_func},
Behdad Esfahbod0dfcc132010-04-21 23:41:26 -0400747 {CharP(this), CharP(this), CharP(this)}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400748 };
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400749 return chain_context_lookup (context,
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400750 backtrack.len, (const USHORT *) backtrack.array(),
751 input.len, (const USHORT *) input.array() + 1,
752 lookahead.len, (const USHORT *) lookahead.array(),
753 lookup.len, lookup.array(),
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400754 lookup_context);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400755 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400756 }
757
Behdad Esfahbod39840472010-05-05 00:23:19 -0400758 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400759 TRACE_SANITIZE ();
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400760 if (!backtrack.sanitize (context, this)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400761 OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400762 if (!input.sanitize (context, this)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400763 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400764 if (!lookahead.sanitize (context, this)) return false;
Behdad Esfahbode961c862010-04-21 15:56:11 -0400765 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400766 return lookup.sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400767 }
768
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400769 private:
770 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400771 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400772 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400773 * in backtracking sequence, in glyph
774 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400775 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400776 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400777 * tables in input sequence, in glyph
778 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400779 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400780 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400781 * in lookahead sequence, in glyph
782 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400783 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400784 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400785 * design order) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400786 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400787 DEFINE_SIZE_MIN (10);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400788};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400789
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400790struct ChainContext
791{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400792 protected:
Behdad Esfahbod1911b9d2010-05-05 01:32:04 -0400793 inline bool apply (hb_apply_context_t *context, 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 Esfahbod1911b9d2010-05-05 01:32:04 -0400797 case 1: return u.format1->apply (context, apply_func);
798 case 2: return u.format2->apply (context, apply_func);
799 case 3: return u.format3->apply (context, apply_func);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400800 default:return false;
801 }
802 }
803
Behdad Esfahbod39840472010-05-05 00:23:19 -0400804 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400805 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400806 if (!u.format.sanitize (context)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400807 switch (u.format) {
Behdad Esfahbod39840472010-05-05 00:23:19 -0400808 case 1: return u.format1->sanitize (context);
809 case 2: return u.format2->sanitize (context);
810 case 3: return u.format3->sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400811 default:return true;
812 }
813 }
814
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400815 private:
816 union {
817 USHORT format; /* Format identifier */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500818 ChainContextFormat1 format1[VAR];
819 ChainContextFormat2 format2[VAR];
820 ChainContextFormat3 format3[VAR];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400821 } u;
822};
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400823
824
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400825struct ExtensionFormat1
826{
827 friend struct Extension;
828
Behdad Esfahbod18939482009-08-04 14:27:56 -0400829 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400830 inline unsigned int get_type (void) const { return extensionLookupType; }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -0400831 inline unsigned int get_offset (void) const { return extensionOffset; }
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400832
Behdad Esfahbod39840472010-05-05 00:23:19 -0400833 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400834 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400835 return context->check_struct (this);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400836 }
837
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400838 private:
839 USHORT format; /* Format identifier. Set to 1. */
840 USHORT extensionLookupType; /* Lookup type of subtable referenced
841 * by ExtensionOffset (i.e. the
842 * extension subtable). */
Behdad Esfahbod81f2af42010-04-22 00:58:49 -0400843 ULONG extensionOffset; /* Offset to the extension subtable,
844 * of lookup type subtable. */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400845 public:
846 DEFINE_SIZE_STATIC (8);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400847};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400848
849struct Extension
850{
851 inline unsigned int get_type (void) const
852 {
853 switch (u.format) {
854 case 1: return u.format1->get_type ();
855 default:return 0;
856 }
857 }
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -0400858 inline unsigned int get_offset (void) const
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400859 {
860 switch (u.format) {
Behdad Esfahbod3b2c2df2010-04-22 16:51:42 -0400861 case 1: return u.format1->get_offset ();
862 default:return 0;
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400863 }
864 }
865
Behdad Esfahbod39840472010-05-05 00:23:19 -0400866 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400867 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400868 if (!u.format.sanitize (context)) return false;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400869 switch (u.format) {
Behdad Esfahbod39840472010-05-05 00:23:19 -0400870 case 1: return u.format1->sanitize (context);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400871 default:return true;
872 }
873 }
874
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400875 private:
876 union {
877 USHORT format; /* Format identifier */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500878 ExtensionFormat1 format1[VAR];
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400879 } u;
880};
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400881
882
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400883/*
884 * GSUB/GPOS Common
885 */
886
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400887struct GSUBGPOS
888{
Behdad Esfahboda328d662009-08-04 20:27:05 -0400889 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
890 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400891
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400892 inline unsigned int get_script_count (void) const
893 { return (this+scriptList).len; }
894 inline const Tag& get_script_tag (unsigned int i) const
895 { return (this+scriptList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500896 inline unsigned int get_script_tags (unsigned int start_offset,
897 unsigned int *script_count /* IN/OUT */,
898 hb_tag_t *script_tags /* OUT */) const
899 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400900 inline const Script& get_script (unsigned int i) const
901 { return (this+scriptList)[i]; }
902 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
903 { return (this+scriptList).find_index (tag, index); }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400904
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400905 inline unsigned int get_feature_count (void) const
906 { return (this+featureList).len; }
907 inline const Tag& get_feature_tag (unsigned int i) const
908 { return (this+featureList).get_tag (i); }
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500909 inline unsigned int get_feature_tags (unsigned int start_offset,
910 unsigned int *feature_count /* IN/OUT */,
911 hb_tag_t *feature_tags /* OUT */) const
912 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400913 inline const Feature& get_feature (unsigned int i) const
914 { return (this+featureList)[i]; }
915 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
916 { return (this+featureList).find_index (tag, index); }
917
918 inline unsigned int get_lookup_count (void) const
919 { return (this+lookupList).len; }
920 inline const Lookup& get_lookup (unsigned int i) const
921 { return (this+lookupList)[i]; }
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400922
Behdad Esfahbod39840472010-05-05 00:23:19 -0400923 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400924 TRACE_SANITIZE ();
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400925 return version.sanitize (context) && likely (version.major == 1)
Behdad Esfahbod41a93d22010-05-06 12:55:14 -0400926 && scriptList.sanitize (context, this)
927 && featureList.sanitize (context, this)
928 && lookupList.sanitize (context, this);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400929 }
930
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400931 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400932 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400933 * to 0x00010000 */
934 OffsetTo<ScriptList>
935 scriptList; /* ScriptList table */
936 OffsetTo<FeatureList>
937 featureList; /* FeatureList table */
938 OffsetTo<LookupList>
939 lookupList; /* LookupList table */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400940 public:
941 DEFINE_SIZE_STATIC (10);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400942};
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400943
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400944
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400945#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */