blob: 4a1ab24ce55c6ed4855b8f26fb4c7bb2f14d5b07 [file] [log] [blame]
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -04001/*
2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
3 *
4 * This is part of HarfBuzz, an OpenType Layout engine library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040027#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
28#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040029
Behdad Esfahbod88a5f5a2009-05-25 03:39:11 -040030#include "hb-buffer-private.h"
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040031#include "hb-ot-layout-gdef-private.hh"
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040032
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040033
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040034#define APPLY_ARG_DEF \
Behdad Esfahbod0ead4812009-08-02 17:41:36 -040035 hb_ot_layout_context_t *context, \
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040036 hb_buffer_t *buffer, \
37 unsigned int context_length HB_GNUC_UNUSED, \
38 unsigned int nesting_level_left HB_GNUC_UNUSED, \
Behdad Esfahbodecf17e82009-05-17 09:34:41 -040039 unsigned int lookup_flag, \
40 unsigned int property HB_GNUC_UNUSED /* propety of first glyph */
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040041#define APPLY_ARG \
Behdad Esfahbod0ead4812009-08-02 17:41:36 -040042 context, \
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040043 buffer, \
44 context_length, \
45 nesting_level_left, \
Behdad Esfahbodecf17e82009-05-17 09:34:41 -040046 lookup_flag, \
47 property
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040048
49
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040050typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, char *data);
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040051typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040052
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040053struct ContextFuncs
54{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040055 match_func_t match;
56 apply_lookup_func_t apply;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040057};
58
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040059
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040060static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data)
61{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040062 return glyph_id == value;
63}
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040064
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040065static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data)
66{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -040067 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040068 return class_def.get_class (glyph_id) == value;
69}
70
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040071static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data)
72{
Behdad Esfahbod6b54c5d2009-05-18 18:30:25 -040073 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040074 return (data+coverage) (glyph_id) != NOT_COVERED;
75}
76
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040077
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -040078static inline bool match_input (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -040079 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040080 const USHORT input[], /* Array of input values--start with second glyph */
81 match_func_t match_func,
82 char *match_data,
83 unsigned int *context_length_out)
84{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040085 unsigned int i, j;
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -040086 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
87 if (HB_UNLIKELY (buffer->in_pos + count > end))
88 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040089
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040090 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
91 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040092 while (_hb_ot_layout_skip_mark (context->face, IN_INFO (j), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040093 {
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -040094 if (HB_UNLIKELY (j + count - i == end))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040095 return false;
96 j++;
97 }
98
Behdad Esfahbod4189b922009-05-26 17:31:56 -040099 if (HB_LIKELY (!match_func (IN_GLYPH (j), input[i - 1], match_data)))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400100 return false;
101 }
102
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400103 *context_length_out = j - buffer->in_pos;
104
105 return true;
106}
107
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400108static inline bool match_backtrack (APPLY_ARG_DEF,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400109 unsigned int count,
110 const USHORT backtrack[],
111 match_func_t match_func,
112 char *match_data)
113{
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400114 if (HB_UNLIKELY (buffer->out_pos < count))
115 return false;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400116
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400117 for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
118 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400119 while (_hb_ot_layout_skip_mark (context->face, OUT_INFO (j), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400120 {
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400121 if (HB_UNLIKELY (j + 1 == count - i))
122 return false;
123 j--;
124 }
125
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400126 if (HB_LIKELY (!match_func (OUT_GLYPH (j), backtrack[i], match_data)))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400127 return false;
128 }
129
130 return true;
131}
132
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400133static inline bool match_lookahead (APPLY_ARG_DEF,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400134 unsigned int count,
135 const USHORT lookahead[],
136 match_func_t match_func,
137 char *match_data,
138 unsigned int offset)
139{
140 unsigned int i, j;
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400141 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
142 if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
143 return false;
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400144
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400145 for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
146 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400147 while (_hb_ot_layout_skip_mark (context->face, OUT_INFO (j), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400148 {
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400149 if (HB_UNLIKELY (j + count - i == end))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400150 return false;
151 j++;
152 }
153
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400154 if (HB_LIKELY (!match_func (IN_GLYPH (j), lookahead[i], match_data)))
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400155 return false;
156 }
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400157
158 return true;
159}
160
161
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400162struct LookupRecord
163{
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400164 public:
165 inline bool sanitize (SANITIZE_ARG_DEF) {
166 return SANITIZE_SELF ();
167 }
168
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400169 USHORT sequenceIndex; /* Index into current glyph
170 * sequence--first glyph = 0 */
171 USHORT lookupListIndex; /* Lookup to apply to that
172 * position--zero--based */
173};
174ASSERT_SIZE (LookupRecord, 4);
175
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400176static inline bool apply_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400177 unsigned int count, /* Including the first glyph */
178 unsigned int lookupCount,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400179 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
180 apply_lookup_func_t apply_func)
181{
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400182 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
183 if (HB_UNLIKELY (buffer->in_pos + count > end))
184 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400185
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400186 /* TODO We don't support lookupRecord arrays that are not increasing:
187 * Should be easy for in_place ones at least. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400188 for (unsigned int i = 0; i < count; i++)
189 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400190 while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400191 {
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400192 if (HB_UNLIKELY (buffer->in_pos == end))
193 return true;
194 /* No lookup applied for this index */
195 _hb_buffer_next_glyph (buffer);
196 }
197
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400198 if (lookupCount && i == lookupRecord->sequenceIndex)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400199 {
200 unsigned int old_pos = buffer->in_pos;
201
202 /* Apply a lookup */
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400203 bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400204
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400205 lookupRecord++;
206 lookupCount--;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400207 i += buffer->in_pos - old_pos;
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400208 if (HB_UNLIKELY (buffer->in_pos == end))
209 return true;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400210
211 if (!done)
212 goto not_applied;
213 }
214 else
215 {
216 not_applied:
217 /* No lookup applied for this index */
218 _hb_buffer_next_glyph (buffer);
219 i++;
220 }
221 }
222
223 return true;
224}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400225
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400226
227/* Contextual lookups */
228
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400229struct ContextLookupContext
230{
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400231 ContextFuncs funcs;
232 char *match_data;
233};
234
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400235static inline bool context_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400236 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400237 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400238 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400239 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400240 ContextLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400241{
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400242 return match_input (APPLY_ARG,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400243 inputCount, input,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400244 lookup_context.funcs.match, lookup_context.match_data,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400245 &context_length) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400246 apply_lookup (APPLY_ARG,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400247 inputCount,
248 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400249 lookup_context.funcs.apply);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400250}
251
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400252struct Rule
253{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400254 friend struct RuleSet;
255
256 private:
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400257 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400258 {
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400259 const LookupRecord *lookupRecord = &CONST_CAST (LookupRecord, input, sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400260 return context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400261 inputCount, input,
262 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400263 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400264 }
265
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400266 public:
267 inline bool sanitize (SANITIZE_ARG_DEF) {
268 if (!SANITIZE_SELF ()) return false;
269 return SANITIZE_MEM (input,
270 sizeof (input[0]) * inputCount +
271 sizeof (lookupRecordX[0]) * lookupCount);
272 }
273
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400274 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400275 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400276 * glyph sequence--includes the first
277 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400278 USHORT lookupCount; /* Number of LookupRecords */
279 USHORT input[]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400280 * second glyph */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400281 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400282 * design order */
283};
284ASSERT_SIZE (Rule, 4);
285
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400286struct RuleSet
287{
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400288 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400289 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400290 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400291 for (unsigned int i = 0; i < num_rules; i++)
292 {
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400293 if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400294 return true;
295 }
296
297 return false;
298 }
299
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400300 inline bool sanitize (SANITIZE_ARG_DEF) {
301 return SANITIZE_THIS (rule);
302 }
303
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400304 private:
305 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400306 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400307 * ordered by preference */
308};
309
310
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400311struct ContextFormat1
312{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400313 friend struct Context;
314
315 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400316 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
317 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400318 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400319 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400320 return false;
321
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400322 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400323 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400324 {match_glyph, apply_func},
325 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400326 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400327 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400328 }
329
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400330 inline bool sanitize (SANITIZE_ARG_DEF) {
331 return SANITIZE_THIS2 (coverage, ruleSet);
332 }
333
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400334 private:
335 USHORT format; /* Format identifier--format = 1 */
336 OffsetTo<Coverage>
337 coverage; /* Offset to Coverage table--from
338 * beginning of table */
339 OffsetArrayOf<RuleSet>
340 ruleSet; /* Array of RuleSet tables
341 * ordered by Coverage Index */
342};
343ASSERT_SIZE (ContextFormat1, 6);
344
345
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400346struct ContextFormat2
347{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400348 friend struct Context;
349
350 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400351 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
352 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400353 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400354 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400355 return false;
356
357 const ClassDef &class_def = this+classDef;
358 index = class_def (IN_CURGLYPH ());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400359 const RuleSet &rule_set = this+ruleSet[index];
360 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
361 * them across subrule lookups. Not sure it's worth it.
362 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400363 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400364 {match_class, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400365 DECONST_CHARP(&class_def)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400366 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400367 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400368 }
369
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400370 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400371 return SANITIZE_THIS3 (coverage, classDef, ruleSet);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400372 }
373
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400374 private:
375 USHORT format; /* Format identifier--format = 2 */
376 OffsetTo<Coverage>
377 coverage; /* Offset to Coverage table--from
378 * beginning of table */
379 OffsetTo<ClassDef>
380 classDef; /* Offset to glyph ClassDef table--from
381 * beginning of table */
382 OffsetArrayOf<RuleSet>
383 ruleSet; /* Array of RuleSet tables
384 * ordered by class */
385};
386ASSERT_SIZE (ContextFormat2, 8);
387
388
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400389struct ContextFormat3
390{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400391 friend struct Context;
392
393 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400394 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
395 {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400396 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400397 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400398 return false;
399
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400400 const LookupRecord *lookupRecord = &CONST_CAST(LookupRecord, coverage, sizeof (coverage[0]) * glyphCount);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400401 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400402 {match_coverage, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400403 DECONST_CHARP(this)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400404 };
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400405 return context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400406 glyphCount, (const USHORT *) (coverage + 1),
407 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400408 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400409 }
410
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400411 inline bool sanitize (SANITIZE_ARG_DEF) {
412 if (!SANITIZE_SELF ()) return false;
413 unsigned int count = glyphCount;
414 for (unsigned int i = 0; i < count; i++)
415 if (!SANITIZE_THIS (coverage[i])) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400416 LookupRecord *lookupRecord = &CAST(LookupRecord, coverage, sizeof (coverage[0]) * glyphCount);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400417 return SANITIZE_MEM (lookupRecord, sizeof (lookupRecord[0]) * lookupCount);
418 }
419
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400420 private:
421 USHORT format; /* Format identifier--format = 3 */
422 USHORT glyphCount; /* Number of glyphs in the input glyph
423 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400424 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400425 OffsetTo<Coverage>
426 coverage[]; /* Array of offsets to Coverage
427 * table in glyph sequence order */
428 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400429 * design order */
430};
431ASSERT_SIZE (ContextFormat3, 6);
432
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400433struct Context
434{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400435 protected:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400436 bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
437 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400438 switch (u.format) {
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400439 case 1: return u.format1->apply (APPLY_ARG, apply_func);
440 case 2: return u.format2->apply (APPLY_ARG, apply_func);
441 case 3: return u.format3->apply (APPLY_ARG, apply_func);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400442 default:return false;
443 }
444 }
445
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400446 inline bool sanitize (SANITIZE_ARG_DEF) {
447 if (!SANITIZE (u.format)) return false;
448 switch (u.format) {
449 case 1: return u.format1->sanitize (SANITIZE_ARG);
450 case 2: return u.format2->sanitize (SANITIZE_ARG);
451 case 3: return u.format3->sanitize (SANITIZE_ARG);
452 default:return true;
453 }
454 }
455
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400456 private:
457 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400458 USHORT format; /* Format identifier */
459 ContextFormat1 format1[];
460 ContextFormat2 format2[];
461 ContextFormat3 format3[];
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400462 } u;
463};
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400464ASSERT_SIZE (Context, 2);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400465
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400466
467/* Chaining Contextual lookups */
468
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400469struct ChainContextLookupContext
470{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400471 ContextFuncs funcs;
472 char *match_data[3];
473};
474
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400475static inline bool chain_context_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400476 unsigned int backtrackCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400477 const USHORT backtrack[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400478 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400479 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400480 unsigned int lookaheadCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400481 const USHORT lookahead[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400482 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400483 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400484 ChainContextLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400485{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400486 /* First guess */
487 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
Behdad Esfahbod122f21f2009-05-18 04:21:53 -0400488 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
489 inputCount + lookaheadCount > context_length))
Behdad Esfahbode072c242009-05-18 03:47:31 -0400490 return false;
491
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400492 unsigned int offset;
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400493 return match_backtrack (APPLY_ARG,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400494 backtrackCount, backtrack,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400495 lookup_context.funcs.match, lookup_context.match_data[0]) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400496 match_input (APPLY_ARG,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400497 inputCount, input,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400498 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400499 &offset) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400500 match_lookahead (APPLY_ARG,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400501 lookaheadCount, lookahead,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400502 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400503 offset) &&
504 (context_length = offset, true) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400505 apply_lookup (APPLY_ARG,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400506 inputCount,
507 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400508 lookup_context.funcs.apply);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400509}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400510
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400511struct ChainRule
512{
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400513 friend struct ChainRuleSet;
514
515 private:
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400516 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400517 {
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400518 const HeadlessArrayOf<USHORT> &input = CONST_CAST (HeadlessArrayOf<USHORT>, backtrack, backtrack.get_size ());
519 const ArrayOf<USHORT> &lookahead = CONST_CAST (ArrayOf<USHORT>, input, input.get_size ());
520 const ArrayOf<LookupRecord> &lookup = CONST_CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400521 return chain_context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400522 backtrack.len, backtrack.array,
523 input.len, input.array + 1,
524 lookahead.len, lookahead.array,
525 lookup.len, lookup.array,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400526 lookup_context);
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400527 return false;
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400528 }
529
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400530 public:
531 inline bool sanitize (SANITIZE_ARG_DEF) {
532 if (!SANITIZE (backtrack)) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400533 HeadlessArrayOf<USHORT> &input = CAST (HeadlessArrayOf<USHORT>, backtrack, backtrack.get_size ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400534 if (!SANITIZE (input)) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400535 ArrayOf<USHORT> &lookahead = CAST (ArrayOf<USHORT>, input, input.get_size ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400536 if (!SANITIZE (lookahead)) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400537 ArrayOf<LookupRecord> &lookup = CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400538 return SANITIZE (lookup);
539 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400540
541 private:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400542 ArrayOf<USHORT>
543 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400544 * (to be matched before the input
545 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400546 HeadlessArrayOf<USHORT>
547 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400548 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400549 ArrayOf<USHORT>
550 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400551 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400552 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400553 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400554 * design order) */
555};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400556ASSERT_SIZE (ChainRule, 8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400557
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400558struct ChainRuleSet
559{
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400560 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400561 {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400562 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400563 for (unsigned int i = 0; i < num_rules; i++)
564 {
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400565 if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400566 return true;
567 }
568
569 return false;
570 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400571
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400572 inline bool sanitize (SANITIZE_ARG_DEF) {
573 return SANITIZE_THIS (rule);
574 }
575
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400576 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400577 OffsetArrayOf<ChainRule>
578 rule; /* Array of ChainRule tables
579 * ordered by preference */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400580};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400581ASSERT_SIZE (ChainRuleSet, 2);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400582
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400583struct ChainContextFormat1
584{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400585 friend struct ChainContext;
586
587 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400588 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
589 {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400590 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400591 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400592 return false;
593
594 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400595 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400596 {match_glyph, apply_func},
597 {NULL, NULL, NULL}
598 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400599 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400600 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400601
602 inline bool sanitize (SANITIZE_ARG_DEF) {
603 return SANITIZE_THIS2 (coverage, ruleSet);
604 }
605
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400606 private:
607 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400608 OffsetTo<Coverage>
609 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400610 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400611 OffsetArrayOf<ChainRuleSet>
612 ruleSet; /* Array of ChainRuleSet tables
613 * ordered by Coverage Index */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400614};
615ASSERT_SIZE (ChainContextFormat1, 6);
616
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400617struct ChainContextFormat2
618{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400619 friend struct ChainContext;
620
621 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400622 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
623 {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400624 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400625 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400626 return false;
627
628 const ClassDef &backtrack_class_def = this+backtrackClassDef;
629 const ClassDef &input_class_def = this+inputClassDef;
630 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
631
632 index = input_class_def (IN_CURGLYPH ());
633 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400634 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
635 * them across subrule lookups. Not sure it's worth it.
636 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400637 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400638 {match_class, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400639 {DECONST_CHARP(&backtrack_class_def),
640 DECONST_CHARP(&input_class_def),
641 DECONST_CHARP(&lookahead_class_def)}
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400642 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400643 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400644 }
645
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400646 inline bool sanitize (SANITIZE_ARG_DEF) {
647 return SANITIZE_THIS2 (coverage, backtrackClassDef) &&
648 SANITIZE_THIS2 (inputClassDef, lookaheadClassDef) &&
649 SANITIZE_THIS (ruleSet);
650 }
651
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400652 private:
653 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400654 OffsetTo<Coverage>
655 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400656 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400657 OffsetTo<ClassDef>
658 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400659 * containing backtrack sequence
660 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400661 OffsetTo<ClassDef>
662 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400663 * table containing input sequence
664 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400665 OffsetTo<ClassDef>
666 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400667 * containing lookahead sequence
668 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400669 OffsetArrayOf<ChainRuleSet>
670 ruleSet; /* Array of ChainRuleSet tables
671 * ordered by class */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400672};
673ASSERT_SIZE (ChainContextFormat2, 12);
674
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400675struct ChainContextFormat3
676{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400677 friend struct ChainContext;
678
679 private:
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400680
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400681 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
682 {
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400683 const OffsetArrayOf<Coverage> &input = CONST_CAST (OffsetArrayOf<Coverage>, backtrack, backtrack.get_size ());
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400684
685 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400686 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400687 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400688
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400689 const OffsetArrayOf<Coverage> &lookahead = CONST_CAST (OffsetArrayOf<Coverage>, input, input.get_size ());
690 const ArrayOf<LookupRecord> &lookup = CONST_CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400691 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400692 {match_coverage, apply_func},
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400693 {DECONST_CHARP(this), DECONST_CHARP(this), DECONST_CHARP(this)}
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400694 };
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400695 return chain_context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400696 backtrack.len, (USHORT *) backtrack.array,
697 input.len, (USHORT *) input.array,
698 lookahead.len, (USHORT *) lookahead.array,
699 lookup.len, lookup.array,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400700 lookup_context);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400701 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400702 }
703
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400704 inline bool sanitize (SANITIZE_ARG_DEF) {
705 if (!SANITIZE_THIS (backtrack)) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400706 OffsetArrayOf<Coverage> &input = CAST (OffsetArrayOf<Coverage>, backtrack, backtrack.get_size ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400707 if (!SANITIZE_THIS (input)) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400708 OffsetArrayOf<Coverage> &lookahead = CAST (OffsetArrayOf<Coverage>, input, input.get_size ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400709 if (!SANITIZE_THIS (lookahead)) return false;
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400710 ArrayOf<LookupRecord> &lookup = CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400711 return SANITIZE (lookup);
712 }
713
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400714 private:
715 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400716 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400717 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400718 * in backtracking sequence, in glyph
719 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400720 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400721 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400722 * tables in input sequence, in glyph
723 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400724 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400725 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400726 * in lookahead sequence, in glyph
727 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400728 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400729 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400730 * design order) */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400731};
732ASSERT_SIZE (ChainContextFormat3, 10);
733
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400734struct ChainContext
735{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400736 protected:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400737 bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
738 {
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400739 switch (u.format) {
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400740 case 1: return u.format1->apply (APPLY_ARG, apply_func);
741 case 2: return u.format2->apply (APPLY_ARG, apply_func);
742 case 3: return u.format3->apply (APPLY_ARG, apply_func);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400743 default:return false;
744 }
745 }
746
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400747 inline bool sanitize (SANITIZE_ARG_DEF) {
748 if (!SANITIZE (u.format)) return false;
749 switch (u.format) {
750 case 1: return u.format1->sanitize (SANITIZE_ARG);
751 case 2: return u.format2->sanitize (SANITIZE_ARG);
752 case 3: return u.format3->sanitize (SANITIZE_ARG);
753 default:return true;
754 }
755 }
756
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400757 private:
758 union {
759 USHORT format; /* Format identifier */
760 ChainContextFormat1 format1[];
761 ChainContextFormat2 format2[];
762 ChainContextFormat3 format3[];
763 } u;
764};
765ASSERT_SIZE (ChainContext, 2);
766
767
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400768struct ExtensionFormat1
769{
770 friend struct Extension;
771
Behdad Esfahbod18939482009-08-04 14:27:56 -0400772 protected:
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400773 inline unsigned int get_type (void) const { return extensionLookupType; }
774 inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
775 inline const LookupSubTable& get_subtable (void) const
776 {
777 unsigned int offset = get_offset ();
778 if (HB_UNLIKELY (!offset)) return Null(LookupSubTable);
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400779 return CONST_CAST (LookupSubTable, *this, offset);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400780 }
781
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400782 inline bool sanitize (SANITIZE_ARG_DEF) {
783 return SANITIZE_SELF ();
784 }
785
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400786 private:
787 USHORT format; /* Format identifier. Set to 1. */
788 USHORT extensionLookupType; /* Lookup type of subtable referenced
789 * by ExtensionOffset (i.e. the
790 * extension subtable). */
791 USHORT extensionOffset[2]; /* Offset to the extension subtable,
792 * of lookup type subtable.
793 * Defined as two shorts to avoid
794 * alignment requirements. */
795};
796ASSERT_SIZE (ExtensionFormat1, 8);
797
798struct Extension
799{
800 inline unsigned int get_type (void) const
801 {
802 switch (u.format) {
803 case 1: return u.format1->get_type ();
804 default:return 0;
805 }
806 }
807 inline const LookupSubTable& get_subtable (void) const
808 {
809 switch (u.format) {
810 case 1: return u.format1->get_subtable ();
811 default:return Null(LookupSubTable);
812 }
813 }
814
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400815 inline bool sanitize (SANITIZE_ARG_DEF) {
816 if (!SANITIZE (u.format)) return false;
817 switch (u.format) {
818 case 1: return u.format1->sanitize (SANITIZE_ARG);
819 default:return true;
820 }
821 }
822
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400823 private:
824 union {
825 USHORT format; /* Format identifier */
826 ExtensionFormat1 format1[];
827 } u;
828};
829ASSERT_SIZE (Extension, 2);
830
831
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400832/*
833 * GSUB/GPOS Common
834 */
835
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400836struct GSUBGPOS
837{
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400838 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
839 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
840
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400841 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (GSUBGPOS, 1, 1);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400842
843 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
844 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
845 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
846
847 // LONGTERMTODO bsearch
848 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
849 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
850
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400851 inline bool sanitize (SANITIZE_ARG_DEF) {
852 if (!SANITIZE (version)) return false;
853 if (version.major != 1) return true;
854 return SANITIZE_THIS3 (scriptList, featureList, lookupList);
855 }
856
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400857 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400858 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400859 * to 0x00010000 */
860 OffsetTo<ScriptList>
861 scriptList; /* ScriptList table */
862 OffsetTo<FeatureList>
863 featureList; /* FeatureList table */
864 OffsetTo<LookupList>
865 lookupList; /* LookupList table */
866};
867ASSERT_SIZE (GSUBGPOS, 10);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400868
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400869
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400870#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */