blob: a14166c7a8b960ce2f617f7b048ac030395310c0 [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 Esfahbod4497af02009-05-25 03:20:18 -040067 const ClassDef &class_def = *(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 Esfahbodf14c2b72009-05-18 02:36:18 -0400164 USHORT sequenceIndex; /* Index into current glyph
165 * sequence--first glyph = 0 */
166 USHORT lookupListIndex; /* Lookup to apply to that
167 * position--zero--based */
168};
169ASSERT_SIZE (LookupRecord, 4);
170
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400171static inline bool apply_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400172 unsigned int count, /* Including the first glyph */
173 unsigned int lookupCount,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400174 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
175 apply_lookup_func_t apply_func)
176{
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400177 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
178 if (HB_UNLIKELY (buffer->in_pos + count > end))
179 return false;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400180
Behdad Esfahbod7cff75b2009-05-18 04:09:05 -0400181 /* TODO We don't support lookupRecord arrays that are not increasing:
182 * Should be easy for in_place ones at least. */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400183 for (unsigned int i = 0; i < count; i++)
184 {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400185 while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400186 {
Behdad Esfahbode73a0c22009-05-18 04:15:25 -0400187 if (HB_UNLIKELY (buffer->in_pos == end))
188 return true;
189 /* No lookup applied for this index */
190 _hb_buffer_next_glyph (buffer);
191 }
192
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400193 if (lookupCount && i == lookupRecord->sequenceIndex)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400194 {
195 unsigned int old_pos = buffer->in_pos;
196
197 /* Apply a lookup */
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400198 bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400199
Behdad Esfahbod47958de2009-05-18 04:17:47 -0400200 lookupRecord++;
201 lookupCount--;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400202 i += buffer->in_pos - old_pos;
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400203 if (HB_UNLIKELY (buffer->in_pos == end))
204 return true;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400205
206 if (!done)
207 goto not_applied;
208 }
209 else
210 {
211 not_applied:
212 /* No lookup applied for this index */
213 _hb_buffer_next_glyph (buffer);
214 i++;
215 }
216 }
217
218 return true;
219}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400220
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400221
222/* Contextual lookups */
223
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400224struct ContextLookupContext
225{
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400226 ContextFuncs funcs;
227 char *match_data;
228};
229
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400230static inline bool context_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400231 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400232 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400233 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400234 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400235 ContextLookupContext &lookup_context)
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400236{
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400237 return match_input (APPLY_ARG,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400238 inputCount, input,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400239 lookup_context.funcs.match, lookup_context.match_data,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400240 &context_length) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400241 apply_lookup (APPLY_ARG,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400242 inputCount,
243 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400244 lookup_context.funcs.apply);
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400245}
246
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400247struct Rule
248{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400249 friend struct RuleSet;
250
251 private:
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400252 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400253 {
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400254 const LookupRecord *lookupRecord = (const LookupRecord *)
255 ((const char *) input +
256 sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400257 return context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400258 inputCount, input,
259 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400260 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400261 }
262
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400263 public:
264 inline bool sanitize (SANITIZE_ARG_DEF) {
265 if (!SANITIZE_SELF ()) return false;
266 return SANITIZE_MEM (input,
267 sizeof (input[0]) * inputCount +
268 sizeof (lookupRecordX[0]) * lookupCount);
269 }
270
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400271 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400272 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400273 * glyph sequence--includes the first
274 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400275 USHORT lookupCount; /* Number of LookupRecords */
276 USHORT input[]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400277 * second glyph */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400278 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400279 * design order */
280};
281ASSERT_SIZE (Rule, 4);
282
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400283struct RuleSet
284{
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400285 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400286 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400287 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400288 for (unsigned int i = 0; i < num_rules; i++)
289 {
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400290 if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400291 return true;
292 }
293
294 return false;
295 }
296
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400297 inline bool sanitize (SANITIZE_ARG_DEF) {
298 return SANITIZE_THIS (rule);
299 }
300
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400301 private:
302 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400303 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400304 * ordered by preference */
305};
306
307
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400308struct ContextFormat1
309{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400310 friend struct Context;
311
312 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400313 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
314 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400315 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400316 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400317 return false;
318
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400319 const RuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400320 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400321 {match_glyph, apply_func},
322 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400323 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400324 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400325 }
326
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400327 inline bool sanitize (SANITIZE_ARG_DEF) {
328 return SANITIZE_THIS2 (coverage, ruleSet);
329 }
330
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400331 private:
332 USHORT format; /* Format identifier--format = 1 */
333 OffsetTo<Coverage>
334 coverage; /* Offset to Coverage table--from
335 * beginning of table */
336 OffsetArrayOf<RuleSet>
337 ruleSet; /* Array of RuleSet tables
338 * ordered by Coverage Index */
339};
340ASSERT_SIZE (ContextFormat1, 6);
341
342
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400343struct ContextFormat2
344{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400345 friend struct Context;
346
347 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400348 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
349 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400350 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400351 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400352 return false;
353
354 const ClassDef &class_def = this+classDef;
355 index = class_def (IN_CURGLYPH ());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400356 const RuleSet &rule_set = this+ruleSet[index];
357 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
358 * them across subrule lookups. Not sure it's worth it.
359 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400360 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400361 {match_class, apply_func},
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400362 (char *) &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400363 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400364 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400365 }
366
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400367 inline bool sanitize (SANITIZE_ARG_DEF) {
368 return SANITIZE_THIS2 (coverage, classDef) && SANITIZE_THIS (ruleSet);
369 }
370
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400371 private:
372 USHORT format; /* Format identifier--format = 2 */
373 OffsetTo<Coverage>
374 coverage; /* Offset to Coverage table--from
375 * beginning of table */
376 OffsetTo<ClassDef>
377 classDef; /* Offset to glyph ClassDef table--from
378 * beginning of table */
379 OffsetArrayOf<RuleSet>
380 ruleSet; /* Array of RuleSet tables
381 * ordered by class */
382};
383ASSERT_SIZE (ContextFormat2, 8);
384
385
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400386struct ContextFormat3
387{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400388 friend struct Context;
389
390 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400391 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
392 {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400393 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400394 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400395 return false;
396
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400397 const LookupRecord *lookupRecord = (const LookupRecord *)
398 ((const char *) coverage +
399 sizeof (coverage[0]) * glyphCount);
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400400 struct ContextLookupContext lookup_context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400401 {match_coverage, apply_func},
402 (char *) this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400403 };
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400404 return context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400405 glyphCount, (const USHORT *) (coverage + 1),
406 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400407 lookup_context);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400408 }
409
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400410 inline bool sanitize (SANITIZE_ARG_DEF) {
411 if (!SANITIZE_SELF ()) return false;
412 unsigned int count = glyphCount;
413 for (unsigned int i = 0; i < count; i++)
414 if (!SANITIZE_THIS (coverage[i])) return false;
415 LookupRecord *lookupRecord = (LookupRecord *)
416 ((char *) coverage +
417 sizeof (coverage[0]) * glyphCount);
418 return SANITIZE_MEM (lookupRecord, sizeof (lookupRecord[0]) * lookupCount);
419 }
420
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400421 private:
422 USHORT format; /* Format identifier--format = 3 */
423 USHORT glyphCount; /* Number of glyphs in the input glyph
424 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400425 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400426 OffsetTo<Coverage>
427 coverage[]; /* Array of offsets to Coverage
428 * table in glyph sequence order */
429 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400430 * design order */
431};
432ASSERT_SIZE (ContextFormat3, 6);
433
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400434struct Context
435{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400436 protected:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400437 bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
438 {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400439 switch (u.format) {
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400440 case 1: return u.format1->apply (APPLY_ARG, apply_func);
441 case 2: return u.format2->apply (APPLY_ARG, apply_func);
442 case 3: return u.format3->apply (APPLY_ARG, apply_func);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400443 default:return false;
444 }
445 }
446
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400447 inline bool sanitize (SANITIZE_ARG_DEF) {
448 if (!SANITIZE (u.format)) return false;
449 switch (u.format) {
450 case 1: return u.format1->sanitize (SANITIZE_ARG);
451 case 2: return u.format2->sanitize (SANITIZE_ARG);
452 case 3: return u.format3->sanitize (SANITIZE_ARG);
453 default:return true;
454 }
455 }
456
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400457 private:
458 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400459 USHORT format; /* Format identifier */
460 ContextFormat1 format1[];
461 ContextFormat2 format2[];
462 ContextFormat3 format3[];
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400463 } u;
464};
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400465ASSERT_SIZE (Context, 2);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400466
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400467
468/* Chaining Contextual lookups */
469
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400470struct ChainContextLookupContext
471{
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400472 ContextFuncs funcs;
473 char *match_data[3];
474};
475
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400476static inline bool chain_context_lookup (APPLY_ARG_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400477 unsigned int backtrackCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400478 const USHORT backtrack[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400479 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400480 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400481 unsigned int lookaheadCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400482 const USHORT lookahead[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400483 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400484 const LookupRecord lookupRecord[],
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400485 ChainContextLookupContext &lookup_context)
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400486{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400487 /* First guess */
488 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
Behdad Esfahbod122f21f2009-05-18 04:21:53 -0400489 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
490 inputCount + lookaheadCount > context_length))
Behdad Esfahbode072c242009-05-18 03:47:31 -0400491 return false;
492
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400493 unsigned int offset;
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400494 return match_backtrack (APPLY_ARG,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400495 backtrackCount, backtrack,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400496 lookup_context.funcs.match, lookup_context.match_data[0]) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400497 match_input (APPLY_ARG,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400498 inputCount, input,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400499 lookup_context.funcs.match, lookup_context.match_data[1],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400500 &offset) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400501 match_lookahead (APPLY_ARG,
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400502 lookaheadCount, lookahead,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400503 lookup_context.funcs.match, lookup_context.match_data[2],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400504 offset) &&
505 (context_length = offset, true) &&
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400506 apply_lookup (APPLY_ARG,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400507 inputCount,
508 lookupCount, lookupRecord,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400509 lookup_context.funcs.apply);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400510}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400511
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400512struct ChainRule
513{
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400514 friend struct ChainRuleSet;
515
516 private:
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400517 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400518 {
Behdad Esfahbod515ce4c2009-05-26 13:08:00 -0400519 const HeadlessArrayOf<USHORT> &input = *(const HeadlessArrayOf<USHORT>*)
520 ((const char *) &backtrack + backtrack.get_size ());
521 const ArrayOf<USHORT> &lookahead = *(const ArrayOf<USHORT>*)
522 ((const char *) &input + input.get_size ());
523 const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*)
524 ((const char *) &lookahead + lookahead.get_size ());
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400525 return chain_context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400526 backtrack.len, backtrack.array,
527 input.len, input.array + 1,
528 lookahead.len, lookahead.array,
529 lookup.len, lookup.array,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400530 lookup_context);
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400531 return false;
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400532 }
533
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400534 public:
535 inline bool sanitize (SANITIZE_ARG_DEF) {
536 if (!SANITIZE (backtrack)) return false;
537 HeadlessArrayOf<USHORT> &input = *(HeadlessArrayOf<USHORT>*)
538 ((char *) &backtrack + backtrack.get_size ());
539 if (!SANITIZE (input)) return false;
540 ArrayOf<USHORT> &lookahead = *(ArrayOf<USHORT>*)
541 ((char *) &input + input.get_size ());
542 if (!SANITIZE (lookahead)) return false;
543 ArrayOf<LookupRecord> &lookup = *(ArrayOf<LookupRecord>*)
544 ((char *) &lookahead + lookahead.get_size ());
545 return SANITIZE (lookup);
546 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400547
548 private:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400549 ArrayOf<USHORT>
550 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400551 * (to be matched before the input
552 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400553 HeadlessArrayOf<USHORT>
554 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400555 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400556 ArrayOf<USHORT>
557 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400558 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400559 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400560 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400561 * design order) */
562};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400563ASSERT_SIZE (ChainRule, 8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400564
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400565struct ChainRuleSet
566{
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400567 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400568 {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400569 unsigned int num_rules = rule.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400570 for (unsigned int i = 0; i < num_rules; i++)
571 {
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400572 if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400573 return true;
574 }
575
576 return false;
577 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400578
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400579 inline bool sanitize (SANITIZE_ARG_DEF) {
580 return SANITIZE_THIS (rule);
581 }
582
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400583 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400584 OffsetArrayOf<ChainRule>
585 rule; /* Array of ChainRule tables
586 * ordered by preference */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400587};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400588ASSERT_SIZE (ChainRuleSet, 2);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400589
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400590struct ChainContextFormat1
591{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400592 friend struct ChainContext;
593
594 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400595 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
596 {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400597 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400598 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400599 return false;
600
601 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400602 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400603 {match_glyph, apply_func},
604 {NULL, NULL, NULL}
605 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400606 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400607 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400608
609 inline bool sanitize (SANITIZE_ARG_DEF) {
610 return SANITIZE_THIS2 (coverage, ruleSet);
611 }
612
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400613 private:
614 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400615 OffsetTo<Coverage>
616 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400617 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400618 OffsetArrayOf<ChainRuleSet>
619 ruleSet; /* Array of ChainRuleSet tables
620 * ordered by Coverage Index */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400621};
622ASSERT_SIZE (ChainContextFormat1, 6);
623
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400624struct ChainContextFormat2
625{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400626 friend struct ChainContext;
627
628 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400629 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
630 {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400631 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400632 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400633 return false;
634
635 const ClassDef &backtrack_class_def = this+backtrackClassDef;
636 const ClassDef &input_class_def = this+inputClassDef;
637 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
638
639 index = input_class_def (IN_CURGLYPH ());
640 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400641 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
642 * them across subrule lookups. Not sure it's worth it.
643 */
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400644 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400645 {match_class, apply_func},
646 {(char *) &backtrack_class_def,
647 (char *) &input_class_def,
648 (char *) &lookahead_class_def}
649 };
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400650 return rule_set.apply (APPLY_ARG, lookup_context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400651 }
652
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400653 inline bool sanitize (SANITIZE_ARG_DEF) {
654 return SANITIZE_THIS2 (coverage, backtrackClassDef) &&
655 SANITIZE_THIS2 (inputClassDef, lookaheadClassDef) &&
656 SANITIZE_THIS (ruleSet);
657 }
658
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400659 private:
660 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400661 OffsetTo<Coverage>
662 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400663 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400664 OffsetTo<ClassDef>
665 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400666 * containing backtrack sequence
667 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400668 OffsetTo<ClassDef>
669 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400670 * table containing input sequence
671 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400672 OffsetTo<ClassDef>
673 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400674 * containing lookahead sequence
675 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400676 OffsetArrayOf<ChainRuleSet>
677 ruleSet; /* Array of ChainRuleSet tables
678 * ordered by class */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400679};
680ASSERT_SIZE (ChainContextFormat2, 12);
681
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400682struct ChainContextFormat3
683{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400684 friend struct ChainContext;
685
686 private:
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400687
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400688 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
689 {
Behdad Esfahbod515ce4c2009-05-26 13:08:00 -0400690 const OffsetArrayOf<Coverage> &input = *(const OffsetArrayOf<Coverage>*)
691 ((const char *) &backtrack + backtrack.get_size ());
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400692
693 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400694 if (HB_LIKELY (index == NOT_COVERED))
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400695 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400696
Behdad Esfahbod515ce4c2009-05-26 13:08:00 -0400697 const OffsetArrayOf<Coverage> &lookahead = *(const OffsetArrayOf<Coverage>*)
698 ((const char *) &input + input.get_size ());
699 const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*)
700 ((const char *) &lookahead + lookahead.get_size ());
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400701 struct ChainContextLookupContext lookup_context = {
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400702 {match_coverage, apply_func},
703 {(char *) this, (char *) this, (char *) this}
704 };
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400705 return chain_context_lookup (APPLY_ARG,
Behdad Esfahbod969afd72009-05-18 05:47:47 -0400706 backtrack.len, (USHORT *) backtrack.array,
707 input.len, (USHORT *) input.array,
708 lookahead.len, (USHORT *) lookahead.array,
709 lookup.len, lookup.array,
Behdad Esfahbod7cda6fa2009-07-29 18:37:57 -0400710 lookup_context);
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400711 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400712 }
713
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400714 inline bool sanitize (SANITIZE_ARG_DEF) {
715 if (!SANITIZE_THIS (backtrack)) return false;
716 OffsetArrayOf<Coverage> &input = *(OffsetArrayOf<Coverage>*)
717 ((char *) &backtrack + backtrack.get_size ());
718 if (!SANITIZE_THIS (input)) return false;
719 OffsetArrayOf<Coverage> &lookahead = *(OffsetArrayOf<Coverage>*)
720 ((char *) &input + input.get_size ());
721 if (!SANITIZE_THIS (lookahead)) return false;
722 ArrayOf<LookupRecord> &lookup = *(ArrayOf<LookupRecord>*)
723 ((char *) &lookahead + lookahead.get_size ());
724 return SANITIZE (lookup);
725 }
726
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400727 private:
728 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400729 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400730 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400731 * in backtracking sequence, in glyph
732 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400733 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400734 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400735 * tables in input sequence, in glyph
736 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400737 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400738 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400739 * in lookahead sequence, in glyph
740 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400741 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400742 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400743 * design order) */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400744};
745ASSERT_SIZE (ChainContextFormat3, 10);
746
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400747struct ChainContext
748{
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400749 protected:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400750 bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
751 {
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400752 switch (u.format) {
Behdad Esfahbodeb0dfc82009-05-18 18:22:44 -0400753 case 1: return u.format1->apply (APPLY_ARG, apply_func);
754 case 2: return u.format2->apply (APPLY_ARG, apply_func);
755 case 3: return u.format3->apply (APPLY_ARG, apply_func);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400756 default:return false;
757 }
758 }
759
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400760 inline bool sanitize (SANITIZE_ARG_DEF) {
761 if (!SANITIZE (u.format)) return false;
762 switch (u.format) {
763 case 1: return u.format1->sanitize (SANITIZE_ARG);
764 case 2: return u.format2->sanitize (SANITIZE_ARG);
765 case 3: return u.format3->sanitize (SANITIZE_ARG);
766 default:return true;
767 }
768 }
769
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400770 private:
771 union {
772 USHORT format; /* Format identifier */
773 ChainContextFormat1 format1[];
774 ChainContextFormat2 format2[];
775 ChainContextFormat3 format3[];
776 } u;
777};
778ASSERT_SIZE (ChainContext, 2);
779
780
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400781struct ExtensionFormat1
782{
783 friend struct Extension;
784
785 private:
786 inline unsigned int get_type (void) const { return extensionLookupType; }
787 inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
788 inline const LookupSubTable& get_subtable (void) const
789 {
790 unsigned int offset = get_offset ();
791 if (HB_UNLIKELY (!offset)) return Null(LookupSubTable);
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400792 return *(LookupSubTable*)(((char *) this) + offset);
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400793 }
794
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400795 inline bool sanitize (SANITIZE_ARG_DEF) {
796 return SANITIZE_SELF ();
797 }
798
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400799 private:
800 USHORT format; /* Format identifier. Set to 1. */
801 USHORT extensionLookupType; /* Lookup type of subtable referenced
802 * by ExtensionOffset (i.e. the
803 * extension subtable). */
804 USHORT extensionOffset[2]; /* Offset to the extension subtable,
805 * of lookup type subtable.
806 * Defined as two shorts to avoid
807 * alignment requirements. */
808};
809ASSERT_SIZE (ExtensionFormat1, 8);
810
811struct Extension
812{
813 inline unsigned int get_type (void) const
814 {
815 switch (u.format) {
816 case 1: return u.format1->get_type ();
817 default:return 0;
818 }
819 }
820 inline const LookupSubTable& get_subtable (void) const
821 {
822 switch (u.format) {
823 case 1: return u.format1->get_subtable ();
824 default:return Null(LookupSubTable);
825 }
826 }
827
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400828 inline bool sanitize (SANITIZE_ARG_DEF) {
829 if (!SANITIZE (u.format)) return false;
830 switch (u.format) {
831 case 1: return u.format1->sanitize (SANITIZE_ARG);
832 default:return true;
833 }
834 }
835
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -0400836 private:
837 union {
838 USHORT format; /* Format identifier */
839 ExtensionFormat1 format1[];
840 } u;
841};
842ASSERT_SIZE (Extension, 2);
843
844
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400845/*
846 * GSUB/GPOS Common
847 */
848
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400849struct GSUBGPOS
850{
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400851 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
852 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
853
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400854 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (GSUBGPOS, 1);
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400855
856 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
857 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
858 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
859
860 // LONGTERMTODO bsearch
861 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
862 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
863
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400864 protected:
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400865 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400866 * to 0x00010000 */
867 OffsetTo<ScriptList>
868 scriptList; /* ScriptList table */
869 OffsetTo<FeatureList>
870 featureList; /* FeatureList table */
871 OffsetTo<LookupList>
872 lookupList; /* LookupList table */
873};
874ASSERT_SIZE (GSUBGPOS, 10);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400875
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400876
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400877#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */