blob: df9dd9b7f0d573e0b402e0ee787ee78336c8aa49 [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
27#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H
28#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H
29
30#include "hb-ot-layout-gdef-private.h"
31#include "harfbuzz-buffer-private.h" /* XXX */
32
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040033
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040034#define LOOKUP_ARGS_DEF \
35 hb_ot_layout_t *layout, \
36 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 Esfahbod66bf7ce2009-05-17 08:28:42 -040041#define LOOKUP_ARGS \
42 layout, \
43 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);
51typedef bool (*apply_lookup_func_t) (LOOKUP_ARGS_DEF, unsigned int lookup_index);
52
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040053struct ContextFuncs {
54 match_func_t match;
55 apply_lookup_func_t apply;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040056};
57
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040058
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040059static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
60 return glyph_id == value;
61}
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040062
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -040063static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
64 const ClassDef &class_def = * (const ClassDef *) data;
65 return class_def.get_class (glyph_id) == value;
66}
67
68static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
69 const OffsetTo<Coverage> &coverage = * (const OffsetTo<Coverage> *) &value;
70 return (data+coverage) (glyph_id) != NOT_COVERED;
71}
72
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040073
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040074static inline bool match_input (LOOKUP_ARGS_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -040075 unsigned int count, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -040076 const USHORT input[], /* Array of input values--start with second glyph */
77 match_func_t match_func,
78 char *match_data,
79 unsigned int *context_length_out)
80{
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040081 unsigned int i, j;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040082
83 /* XXX context_length should also be checked when skipping glyphs, right?
84 * What does context_length really mean, anyway? */
85
86 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++) {
87 while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property)) {
88 if (HB_UNLIKELY (j + count - i == buffer->in_length))
89 return false;
90 j++;
91 }
92
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -040093 if (HB_LIKELY (!match_func (IN_GLYPH(j), input[i - 1], match_data)))
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -040094 return false;
95 }
96
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -040097 *context_length_out = j - buffer->in_pos;
98
99 return true;
100}
101
102static inline bool match_backtrack (LOOKUP_ARGS_DEF,
103 unsigned int count,
104 const USHORT backtrack[],
105 match_func_t match_func,
106 char *match_data)
107{
108 unsigned int i, j;
109
110 for (i = 0, j = buffer->out_pos - 1; i < count; i++, j--) {
111 while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
112 if (HB_UNLIKELY (j + 1 == count - i))
113 return false;
114 j--;
115 }
116
117 if (HB_LIKELY (!match_func (OUT_GLYPH(j), backtrack[i], match_data)))
118 return false;
119 }
120
121 return true;
122}
123
124static inline bool match_lookahead (LOOKUP_ARGS_DEF,
125 unsigned int count,
126 const USHORT lookahead[],
127 match_func_t match_func,
128 char *match_data,
129 unsigned int offset)
130{
131 unsigned int i, j;
132
133 for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++) {
134 while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
135 if (HB_UNLIKELY (j + count - i == buffer->in_length))
136 return false;
137 j++;
138 }
139
140 if (HB_LIKELY (!match_func (IN_GLYPH(j), lookahead[i], match_data)))
141 return false;
142 }
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400143
144 return true;
145}
146
147
148struct LookupRecord {
149
150 USHORT sequenceIndex; /* Index into current glyph
151 * sequence--first glyph = 0 */
152 USHORT lookupListIndex; /* Lookup to apply to that
153 * position--zero--based */
154};
155ASSERT_SIZE (LookupRecord, 4);
156
157static inline bool apply_lookup (LOOKUP_ARGS_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400158 unsigned int count, /* Including the first glyph */
159 unsigned int lookupCount,
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400160 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
161 apply_lookup_func_t apply_func)
162{
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400163 unsigned int record_count = lookupCount;
164 const LookupRecord *record = lookupRecord;
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400165
166 /* XXX We have to jump non-matching glyphs when applying too, right? */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400167 /* XXX We don't support lookupRecord arrays that are not increasing:
168 * Should be easy for in_place ones at least. */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400169 for (unsigned int i = 0; i < count;)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400170 {
Behdad Esfahbod19fc24f2009-05-17 09:45:32 -0400171 if (record_count && i == record->sequenceIndex)
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400172 {
173 unsigned int old_pos = buffer->in_pos;
174
175 /* Apply a lookup */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400176 bool done = apply_func (LOOKUP_ARGS, record->lookupListIndex);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400177
178 record++;
179 record_count--;
180 i += buffer->in_pos - old_pos;
181
182 if (!done)
183 goto not_applied;
184 }
185 else
186 {
187 not_applied:
188 /* No lookup applied for this index */
189 _hb_buffer_next_glyph (buffer);
190 i++;
191 }
192 }
193
194 return true;
195}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400196
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400197
198/* Contextual lookups */
199
200struct ContextLookupContext {
201 ContextFuncs funcs;
202 char *match_data;
203};
204
205static inline bool context_lookup (LOOKUP_ARGS_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400206 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400207 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400208 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400209 const LookupRecord lookupRecord[],
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400210 ContextLookupContext &context)
211{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400212 /* First guess */
213 if (HB_UNLIKELY (buffer->in_pos + inputCount > buffer->in_length ||
214 context_length < inputCount))
215 return false;
216
Behdad Esfahbodf14c2b72009-05-18 02:36:18 -0400217 return match_input (LOOKUP_ARGS,
218 inputCount, input,
219 context.funcs.match, context.match_data,
220 &context_length) &&
221 apply_lookup (LOOKUP_ARGS,
222 inputCount,
223 lookupCount, lookupRecord,
224 context.funcs.apply);
225}
226
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400227struct Rule {
228
229 friend struct RuleSet;
230
231 private:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400232 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400233 const LookupRecord *lookupRecord = (const LookupRecord *)
234 ((const char *) input +
235 sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400236 return context_lookup (LOOKUP_ARGS,
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400237 inputCount,
238 input,
239 lookupCount,
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400240 lookupRecord,
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400241 context);
242 }
243
244 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400245 USHORT inputCount; /* Total number of glyphs in input
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400246 * glyph sequence--includes the first
247 * glyph */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400248 USHORT lookupCount; /* Number of LookupRecords */
249 USHORT input[]; /* Array of match inputs--start with
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400250 * second glyph */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400251 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400252 * design order */
253};
254ASSERT_SIZE (Rule, 4);
255
256struct RuleSet {
257
258 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
259
260 unsigned int num_rules = rule.len;
261 for (unsigned int i = 0; i < num_rules; i++) {
262 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
263 return true;
264 }
265
266 return false;
267 }
268
269 private:
270 OffsetArrayOf<Rule>
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400271 rule; /* Array of Rule tables
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400272 * ordered by preference */
273};
274
275
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400276struct ContextFormat1 {
277
278 friend struct Context;
279
280 private:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400281 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
282
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400283 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400284 if (G_LIKELY (index == NOT_COVERED))
285 return false;
286
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400287 const RuleSet &rule_set = this+ruleSet[index];
288 struct ContextLookupContext context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400289 {match_glyph, apply_func},
290 NULL
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400291 };
292 return rule_set.apply (LOOKUP_ARGS, context);
293 }
294
295 private:
296 USHORT format; /* Format identifier--format = 1 */
297 OffsetTo<Coverage>
298 coverage; /* Offset to Coverage table--from
299 * beginning of table */
300 OffsetArrayOf<RuleSet>
301 ruleSet; /* Array of RuleSet tables
302 * ordered by Coverage Index */
303};
304ASSERT_SIZE (ContextFormat1, 6);
305
306
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400307struct ContextFormat2 {
308
309 friend struct Context;
310
311 private:
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400312 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
313
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400314 unsigned int index = (this+coverage) (IN_CURGLYPH ());
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400315 if (G_LIKELY (index == NOT_COVERED))
316 return false;
317
318 const ClassDef &class_def = this+classDef;
319 index = class_def (IN_CURGLYPH ());
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400320 const RuleSet &rule_set = this+ruleSet[index];
321 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
322 * them across subrule lookups. Not sure it's worth it.
323 */
324 struct ContextLookupContext context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400325 {match_class, apply_func},
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400326 (char *) &class_def
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400327 };
328 return rule_set.apply (LOOKUP_ARGS, context);
329 }
330
331 private:
332 USHORT format; /* Format identifier--format = 2 */
333 OffsetTo<Coverage>
334 coverage; /* Offset to Coverage table--from
335 * beginning of table */
336 OffsetTo<ClassDef>
337 classDef; /* Offset to glyph ClassDef table--from
338 * beginning of table */
339 OffsetArrayOf<RuleSet>
340 ruleSet; /* Array of RuleSet tables
341 * ordered by class */
342};
343ASSERT_SIZE (ContextFormat2, 8);
344
345
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400346struct ContextFormat3 {
347
348 friend struct Context;
349
350 private:
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400351 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400352
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400353 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
354 if (G_LIKELY (index == NOT_COVERED))
355 return false;
356
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400357 const LookupRecord *lookupRecord = (const LookupRecord *)
358 ((const char *) coverage +
359 sizeof (coverage[0]) * glyphCount);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400360 struct ContextLookupContext context = {
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400361 {match_coverage, apply_func},
362 (char *) this
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400363 };
364 return context_lookup (LOOKUP_ARGS,
365 glyphCount,
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400366 (const USHORT *) (coverage + 1),
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400367 lookupCount,
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400368 lookupRecord,
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400369 context);
370 }
371
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400372 private:
373 USHORT format; /* Format identifier--format = 3 */
374 USHORT glyphCount; /* Number of glyphs in the input glyph
375 * sequence */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400376 USHORT lookupCount; /* Number of LookupRecords */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400377 OffsetTo<Coverage>
378 coverage[]; /* Array of offsets to Coverage
379 * table in glyph sequence order */
380 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400381 * design order */
382};
383ASSERT_SIZE (ContextFormat3, 6);
384
385struct Context {
386
387 protected:
388 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
389 switch (u.format) {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400390 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
391 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
392 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400393 default:return false;
394 }
395 }
396
397 private:
398 union {
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400399 USHORT format; /* Format identifier */
400 ContextFormat1 format1[];
401 ContextFormat2 format2[];
402 ContextFormat3 format3[];
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400403 } u;
404};
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400405ASSERT_SIZE (Context, 2);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400406
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400407
408/* Chaining Contextual lookups */
409
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400410struct ChainContextLookupContext {
411 ContextFuncs funcs;
412 char *match_data[3];
413};
414
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400415static inline bool chain_context_lookup (LOOKUP_ARGS_DEF,
Behdad Esfahbode072c242009-05-18 03:47:31 -0400416 unsigned int backtrackCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400417 const USHORT backtrack[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400418 unsigned int inputCount, /* Including the first glyph (not matched) */
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400419 const USHORT input[], /* Array of input values--start with second glyph */
Behdad Esfahbode072c242009-05-18 03:47:31 -0400420 unsigned int lookaheadCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400421 const USHORT lookahead[],
Behdad Esfahbode072c242009-05-18 03:47:31 -0400422 unsigned int lookupCount,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400423 const LookupRecord lookupRecord[],
424 ChainContextLookupContext &context)
425{
Behdad Esfahbode072c242009-05-18 03:47:31 -0400426 /* First guess */
427 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
428 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length))
429 return false;
430
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400431 unsigned int offset;
Behdad Esfahbode072c242009-05-18 03:47:31 -0400432 return match_backtrack (LOOKUP_ARGS,
433 backtrackCount, backtrack,
434 context.funcs.match, context.match_data[0]) &&
435 match_input (LOOKUP_ARGS,
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400436 inputCount, input,
437 context.funcs.match, context.match_data[1],
Behdad Esfahbodd0ba0552009-05-18 03:56:39 -0400438 &offset) &&
439 (context_length -= offset, true) &&
440 match_lookahead (LOOKUP_ARGS,
441 lookaheadCount, lookahead,
442 context.funcs.match, context.match_data[2],
443 offset) &&
444 (context_length = offset, true) &&
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400445 apply_lookup (LOOKUP_ARGS,
446 inputCount,
447 lookupCount, lookupRecord,
448 context.funcs.apply);
449}
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400450
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400451struct ChainRule {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400452
453 friend struct ChainRuleSet;
454
455 private:
456 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400457 const HeadlessArrayOf<USHORT> &input = * (const HeadlessArrayOf<USHORT> *)
458 ((const char *) &backtrack + backtrack.get_size ());
459 const ArrayOf<USHORT> &lookahead = * (const ArrayOf<USHORT> *)
460 ((const char *) &input + input.get_size ());
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400461 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
462 ((const char *) &lookahead + lookahead.get_size ());
463 return chain_context_lookup (LOOKUP_ARGS,
464 backtrack.len,
465 backtrack.array,
466 input.len,
467 input.array + 1,
468 lookahead.len,
469 lookahead.array,
470 lookup.len,
471 lookup.array,
472 context);
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400473 return false;
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400474 }
475
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400476
477 private:
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400478 ArrayOf<USHORT>
479 backtrack; /* Array of backtracking values
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400480 * (to be matched before the input
481 * sequence) */
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400482 HeadlessArrayOf<USHORT>
483 inputX; /* Array of input values (start with
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400484 * second glyph) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400485 ArrayOf<USHORT>
486 lookaheadX; /* Array of lookahead values's (to be
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400487 * matched after the input sequence) */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400488 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400489 lookupX; /* Array of LookupRecords--in
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400490 * design order) */
491};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400492ASSERT_SIZE (ChainRule, 8);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400493
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400494struct ChainRuleSet {
495
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400496 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
497
498 unsigned int num_rules = rule.len;
499 for (unsigned int i = 0; i < num_rules; i++) {
500 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
501 return true;
502 }
503
504 return false;
505 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400506
507 private:
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400508 OffsetArrayOf<ChainRule>
509 rule; /* Array of ChainRule tables
510 * ordered by preference */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400511};
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400512ASSERT_SIZE (ChainRuleSet, 2);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400513
514struct ChainContextFormat1 {
515
516 friend struct ChainContext;
517
518 private:
519 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400520
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400521 unsigned int index = (this+coverage) (IN_CURGLYPH ());
522 if (G_LIKELY (index == NOT_COVERED))
523 return false;
524
525 const ChainRuleSet &rule_set = this+ruleSet[index];
526 struct ChainContextLookupContext context = {
527 {match_glyph, apply_func},
528 {NULL, NULL, NULL}
529 };
530 return rule_set.apply (LOOKUP_ARGS, context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400531 }
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400532 private:
533 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod48f16ed2009-05-17 22:11:30 -0400534 OffsetTo<Coverage>
535 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400536 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400537 OffsetArrayOf<ChainRuleSet>
538 ruleSet; /* Array of ChainRuleSet tables
539 * ordered by Coverage Index */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400540};
541ASSERT_SIZE (ChainContextFormat1, 6);
542
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400543struct ChainContextFormat2 {
544
545 friend struct ChainContext;
546
547 private:
548 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
549
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400550 unsigned int index = (this+coverage) (IN_CURGLYPH ());
551 if (G_LIKELY (index == NOT_COVERED))
552 return false;
553
554 const ClassDef &backtrack_class_def = this+backtrackClassDef;
555 const ClassDef &input_class_def = this+inputClassDef;
556 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
557
558 index = input_class_def (IN_CURGLYPH ());
559 const ChainRuleSet &rule_set = this+ruleSet[index];
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400560 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
561 * them across subrule lookups. Not sure it's worth it.
562 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400563 struct ChainContextLookupContext context = {
564 {match_class, apply_func},
565 {(char *) &backtrack_class_def,
566 (char *) &input_class_def,
567 (char *) &lookahead_class_def}
568 };
569 return rule_set.apply (LOOKUP_ARGS, context);
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400570 }
571
572 private:
573 USHORT format; /* Format identifier--format = 2 */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400574 OffsetTo<Coverage>
575 coverage; /* Offset to Coverage table--from
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400576 * beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400577 OffsetTo<ClassDef>
578 backtrackClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400579 * containing backtrack sequence
580 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400581 OffsetTo<ClassDef>
582 inputClassDef; /* Offset to glyph ClassDef
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400583 * table containing input sequence
584 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400585 OffsetTo<ClassDef>
586 lookaheadClassDef; /* Offset to glyph ClassDef table
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400587 * containing lookahead sequence
588 * data--from beginning of table */
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400589 OffsetArrayOf<ChainRuleSet>
590 ruleSet; /* Array of ChainRuleSet tables
591 * ordered by class */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400592};
593ASSERT_SIZE (ChainContextFormat2, 12);
594
595struct ChainContextFormat3 {
596
597 friend struct ChainContext;
598
599 private:
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400600
601 inline bool apply_coverage (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400602 }
603
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400604 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
605
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400606 const OffsetArrayOf<Coverage> &input = * (const OffsetArrayOf<Coverage> *)
607 ((const char *) &backtrack + backtrack.get_size ());
608
609 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400610 if (G_LIKELY (index == NOT_COVERED))
611 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400612
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400613 const OffsetArrayOf<Coverage> &lookahead = * (const OffsetArrayOf<Coverage> *)
614 ((const char *) &input + input.get_size ());
615 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
616 ((const char *) &lookahead + lookahead.get_size ());
617 struct ChainContextLookupContext context = {
618 {match_coverage, apply_func},
619 {(char *) this, (char *) this, (char *) this}
620 };
621 return chain_context_lookup (LOOKUP_ARGS,
622 backtrack.len,
623 (USHORT *) backtrack.array,
624 input.len,
625 (USHORT *) input.array,
626 lookahead.len,
627 (USHORT *) lookahead.array,
628 lookup.len,
629 lookup.array,
630 context);
631 return false;
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400632 }
633
634 private:
635 USHORT format; /* Format identifier--format = 3 */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400636 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400637 backtrack; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400638 * in backtracking sequence, in glyph
639 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400640 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400641 inputX ; /* Array of coverage
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400642 * tables in input sequence, in glyph
643 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400644 OffsetArrayOf<Coverage>
Behdad Esfahbod13ed4402009-05-18 02:14:37 -0400645 lookaheadX; /* Array of coverage tables
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400646 * in lookahead sequence, in glyph
647 * sequence order */
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400648 ArrayOf<LookupRecord>
Behdad Esfahbod02e1e5c2009-05-18 02:47:57 -0400649 lookupX; /* Array of LookupRecords--in
Behdad Esfahboddcb6b602009-05-18 01:49:57 -0400650 * design order) */
Behdad Esfahbodca5290f2009-05-17 20:48:27 -0400651};
652ASSERT_SIZE (ChainContextFormat3, 10);
653
654struct ChainContext {
655
656 protected:
657 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
658 switch (u.format) {
659 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
660 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
661 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
662 default:return false;
663 }
664 }
665
666 private:
667 union {
668 USHORT format; /* Format identifier */
669 ChainContextFormat1 format1[];
670 ChainContextFormat2 format2[];
671 ChainContextFormat3 format3[];
672 } u;
673};
674ASSERT_SIZE (ChainContext, 2);
675
676
Behdad Esfahbodf45107f2009-05-17 20:13:02 -0400677/*
678 * GSUB/GPOS Common
679 */
680
681struct GSUBGPOS {
682 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
683 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
684
685 STATIC_DEFINE_GET_FOR_DATA (GSUBGPOS);
686 /* XXX check version here? */
687
688 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
689 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
690 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
691
692 // LONGTERMTODO bsearch
693 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
694 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
695
696 private:
697 Fixed_Version version; /* Version of the GSUB/GPOS table--initially set
698 * to 0x00010000 */
699 OffsetTo<ScriptList>
700 scriptList; /* ScriptList table */
701 OffsetTo<FeatureList>
702 featureList; /* FeatureList table */
703 OffsetTo<LookupList>
704 lookupList; /* LookupList table */
705};
706ASSERT_SIZE (GSUBGPOS, 10);
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400707
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400708
Behdad Esfahbod66bf7ce2009-05-17 08:28:42 -0400709#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */