Further simplify tracing
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gsubgpos-private.hh
1 /*
2  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping 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_HH
28 #define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
29
30 #include "hb-buffer-private.h"
31 #include "hb-ot-layout-gdef-private.hh"
32
33
34 #ifndef HB_DEBUG_APPLY
35 #define HB_DEBUG_APPLY HB_DEBUG+0
36 #endif
37
38 #define TRACE_APPLY() \
39         HB_STMT_START { \
40           if (HB_DEBUG_APPLY) \
41                   _hb_trace ("APPLY", __PRETTY_FUNCTION__, this, apply_depth, HB_DEBUG_APPLY); \
42         } HB_STMT_END
43
44
45 #define APPLY_ARG_DEF \
46         hb_ot_layout_context_t *context, \
47         hb_buffer_t    *buffer, \
48         unsigned int    context_length HB_GNUC_UNUSED, \
49         unsigned int    nesting_level_left HB_GNUC_UNUSED, \
50         unsigned int    lookup_flag HB_GNUC_UNUSED, \
51         unsigned int    property HB_GNUC_UNUSED, /* propety of first glyph */ \
52         unsigned int    apply_depth HB_GNUC_UNUSED
53 #define APPLY_ARG \
54         context, \
55         buffer, \
56         context_length, \
57         nesting_level_left, \
58         lookup_flag, \
59         property, \
60         (HB_DEBUG_APPLY ? apply_depth + 1 : 0)
61
62
63 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const char *data);
64 typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
65
66 struct ContextFuncs
67 {
68   match_func_t match;
69   apply_lookup_func_t apply;
70 };
71
72
73 static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const char *data HB_GNUC_UNUSED)
74 {
75   return glyph_id == value;
76 }
77
78 static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const char *data)
79 {
80   const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
81   return class_def.get_class (glyph_id) == value;
82 }
83
84 static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const char *data)
85 {
86   const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
87   return (data+coverage) (glyph_id) != NOT_COVERED;
88 }
89
90
91 static inline bool match_input (APPLY_ARG_DEF,
92                                 unsigned int count, /* Including the first glyph (not matched) */
93                                 const USHORT input[], /* Array of input values--start with second glyph */
94                                 match_func_t match_func,
95                                 const char *match_data,
96                                 unsigned int *context_length_out)
97 {
98   unsigned int i, j;
99   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
100   if (HB_UNLIKELY (buffer->in_pos + count > end))
101     return false;
102
103   for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
104   {
105     while (_hb_ot_layout_skip_mark (context->face, IN_INFO (j), lookup_flag, NULL))
106     {
107       if (HB_UNLIKELY (j + count - i == end))
108         return false;
109       j++;
110     }
111
112     if (HB_LIKELY (!match_func (IN_GLYPH (j), input[i - 1], match_data)))
113       return false;
114   }
115
116   *context_length_out = j - buffer->in_pos;
117
118   return true;
119 }
120
121 static inline bool match_backtrack (APPLY_ARG_DEF,
122                                     unsigned int count,
123                                     const USHORT backtrack[],
124                                     match_func_t match_func,
125                                     const char *match_data)
126 {
127   if (HB_UNLIKELY (buffer->out_pos < count))
128     return false;
129
130   for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
131   {
132     while (_hb_ot_layout_skip_mark (context->face, OUT_INFO (j), lookup_flag, NULL))
133     {
134       if (HB_UNLIKELY (j + 1 == count - i))
135         return false;
136       j--;
137     }
138
139     if (HB_LIKELY (!match_func (OUT_GLYPH (j), backtrack[i], match_data)))
140       return false;
141   }
142
143   return true;
144 }
145
146 static inline bool match_lookahead (APPLY_ARG_DEF,
147                                     unsigned int count,
148                                     const USHORT lookahead[],
149                                     match_func_t match_func,
150                                     const char *match_data,
151                                     unsigned int offset)
152 {
153   unsigned int i, j;
154   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
155   if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
156     return false;
157
158   for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
159   {
160     while (_hb_ot_layout_skip_mark (context->face, OUT_INFO (j), lookup_flag, NULL))
161     {
162       if (HB_UNLIKELY (j + count - i == end))
163         return false;
164       j++;
165     }
166
167     if (HB_LIKELY (!match_func (IN_GLYPH (j), lookahead[i], match_data)))
168       return false;
169   }
170
171   return true;
172 }
173
174
175 struct LookupRecord
176 {
177   static inline unsigned int get_size () { return sizeof (LookupRecord); }
178
179   inline bool sanitize (SANITIZE_ARG_DEF) {
180     TRACE_SANITIZE ();
181     return SANITIZE_SELF ();
182   }
183
184   USHORT        sequenceIndex;          /* Index into current glyph
185                                          * sequence--first glyph = 0 */
186   USHORT        lookupListIndex;        /* Lookup to apply to that
187                                          * position--zero--based */
188 };
189 ASSERT_SIZE (LookupRecord, 4);
190
191 static inline bool apply_lookup (APPLY_ARG_DEF,
192                                  unsigned int count, /* Including the first glyph */
193                                  unsigned int lookupCount,
194                                  const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
195                                  apply_lookup_func_t apply_func)
196 {
197   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
198   if (HB_UNLIKELY (buffer->in_pos + count > end))
199     return false;
200
201   /* TODO We don't support lookupRecord arrays that are not increasing:
202    *      Should be easy for in_place ones at least. */
203
204   /* Note: If sublookup is reverse, i will underflow after the first loop
205    * and we jump out of it.  Not entirely disastrous.  So we don't check
206    * for reverse lookup here.
207    */
208   for (unsigned int i = 0; i < count; /* NOP */)
209   {
210     while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
211     {
212       if (HB_UNLIKELY (buffer->in_pos == end))
213         return true;
214       /* No lookup applied for this index */
215       _hb_buffer_next_glyph (buffer);
216     }
217
218     if (lookupCount && i == lookupRecord->sequenceIndex)
219     {
220       unsigned int old_pos = buffer->in_pos;
221
222       /* Apply a lookup */
223       bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
224
225       lookupRecord++;
226       lookupCount--;
227       /* Err, this is wrong if the lookup jumped over some glyphs */
228       i += buffer->in_pos - old_pos;
229       if (HB_UNLIKELY (buffer->in_pos == end))
230         return true;
231
232       if (!done)
233         goto not_applied;
234     }
235     else
236     {
237     not_applied:
238       /* No lookup applied for this index */
239       _hb_buffer_next_glyph (buffer);
240       i++;
241     }
242   }
243
244   return true;
245 }
246
247
248 /* Contextual lookups */
249
250 struct ContextLookupContext
251 {
252   ContextFuncs funcs;
253   const char *match_data;
254 };
255
256 static inline bool context_lookup (APPLY_ARG_DEF,
257                                    unsigned int inputCount, /* Including the first glyph (not matched) */
258                                    const USHORT input[], /* Array of input values--start with second glyph */
259                                    unsigned int lookupCount,
260                                    const LookupRecord lookupRecord[],
261                                    ContextLookupContext &lookup_context)
262 {
263   return match_input (APPLY_ARG,
264                       inputCount, input,
265                       lookup_context.funcs.match, lookup_context.match_data,
266                       &context_length) &&
267          apply_lookup (APPLY_ARG,
268                        inputCount,
269                        lookupCount, lookupRecord,
270                        lookup_context.funcs.apply);
271 }
272
273 struct Rule
274 {
275   friend struct RuleSet;
276
277   private:
278   inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
279   {
280     TRACE_APPLY ();
281     const LookupRecord &lookupRecord = StructAtOffset<LookupRecord> (input, input[0].get_size () * (inputCount ? inputCount - 1 : 0));
282     return context_lookup (APPLY_ARG,
283                            inputCount, input,
284                            lookupCount, &lookupRecord,
285                            lookup_context);
286   }
287
288   public:
289   inline bool sanitize (SANITIZE_ARG_DEF) {
290     TRACE_SANITIZE ();
291     if (!(SANITIZE (inputCount) && SANITIZE (lookupCount))) return false;
292     return SANITIZE_MEM (input,
293                          input[0].get_size () * inputCount +
294                          lookupRecordX[0].get_size () * lookupCount);
295   }
296
297   private:
298   USHORT        inputCount;             /* Total number of glyphs in input
299                                          * glyph sequence--includes the  first
300                                          * glyph */
301   USHORT        lookupCount;            /* Number of LookupRecords */
302   USHORT        input[VAR];             /* Array of match inputs--start with
303                                          * second glyph */
304   LookupRecord  lookupRecordX[VAR];     /* Array of LookupRecords--in
305                                          * design order */
306 };
307 ASSERT_SIZE_VAR2 (Rule, 4, USHORT, LookupRecord);
308
309 struct RuleSet
310 {
311   inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
312   {
313     TRACE_APPLY ();
314     unsigned int num_rules = rule.len;
315     for (unsigned int i = 0; i < num_rules; i++)
316     {
317       if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
318         return true;
319     }
320
321     return false;
322   }
323
324   inline bool sanitize (SANITIZE_ARG_DEF) {
325     TRACE_SANITIZE ();
326     return SANITIZE_THIS (rule);
327   }
328
329   private:
330   OffsetArrayOf<Rule>
331                 rule;                   /* Array of Rule tables
332                                          * ordered by preference */
333 };
334
335
336 struct ContextFormat1
337 {
338   friend struct Context;
339
340   private:
341   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
342   {
343     TRACE_APPLY ();
344     unsigned int index = (this+coverage) (IN_CURGLYPH ());
345     if (HB_LIKELY (index == NOT_COVERED))
346       return false;
347
348     const RuleSet &rule_set = this+ruleSet[index];
349     struct ContextLookupContext lookup_context = {
350       {match_glyph, apply_func},
351       NULL
352     };
353     return rule_set.apply (APPLY_ARG, lookup_context);
354   }
355
356   inline bool sanitize (SANITIZE_ARG_DEF) {
357     TRACE_SANITIZE ();
358     return SANITIZE_THIS2 (coverage, ruleSet);
359   }
360
361   private:
362   USHORT        format;                 /* Format identifier--format = 1 */
363   OffsetTo<Coverage>
364                 coverage;               /* Offset to Coverage table--from
365                                          * beginning of table */
366   OffsetArrayOf<RuleSet>
367                 ruleSet;                /* Array of RuleSet tables
368                                          * ordered by Coverage Index */
369 };
370 ASSERT_SIZE (ContextFormat1, 6);
371
372
373 struct ContextFormat2
374 {
375   friend struct Context;
376
377   private:
378   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
379   {
380     TRACE_APPLY ();
381     unsigned int index = (this+coverage) (IN_CURGLYPH ());
382     if (HB_LIKELY (index == NOT_COVERED))
383       return false;
384
385     const ClassDef &class_def = this+classDef;
386     index = class_def (IN_CURGLYPH ());
387     const RuleSet &rule_set = this+ruleSet[index];
388     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
389      * them across subrule lookups.  Not sure it's worth it.
390      */
391     struct ContextLookupContext lookup_context = {
392      {match_class, apply_func},
393       CharP(&class_def)
394     };
395     return rule_set.apply (APPLY_ARG, lookup_context);
396   }
397
398   inline bool sanitize (SANITIZE_ARG_DEF) {
399     TRACE_SANITIZE ();
400     return SANITIZE_THIS3 (coverage, classDef, ruleSet);
401   }
402
403   private:
404   USHORT        format;                 /* Format identifier--format = 2 */
405   OffsetTo<Coverage>
406                 coverage;               /* Offset to Coverage table--from
407                                          * beginning of table */
408   OffsetTo<ClassDef>
409                 classDef;               /* Offset to glyph ClassDef table--from
410                                          * beginning of table */
411   OffsetArrayOf<RuleSet>
412                 ruleSet;                /* Array of RuleSet tables
413                                          * ordered by class */
414 };
415 ASSERT_SIZE (ContextFormat2, 8);
416
417
418 struct ContextFormat3
419 {
420   friend struct Context;
421
422   private:
423   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
424   {
425     TRACE_APPLY ();
426     unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
427     if (HB_LIKELY (index == NOT_COVERED))
428       return false;
429
430     const LookupRecord &lookupRecord = StructAtOffset<LookupRecord> (coverage, coverage[0].get_size () * glyphCount);
431     struct ContextLookupContext lookup_context = {
432       {match_coverage, apply_func},
433        CharP(this)
434     };
435     return context_lookup (APPLY_ARG,
436                            glyphCount, (const USHORT *) (coverage + 1),
437                            lookupCount, &lookupRecord,
438                            lookup_context);
439   }
440
441   inline bool sanitize (SANITIZE_ARG_DEF) {
442     TRACE_SANITIZE ();
443     if (!SANITIZE_SELF ()) return false;
444     unsigned int count = glyphCount;
445     if (!SANITIZE_ARRAY (coverage, OffsetTo<Coverage>::get_size (), glyphCount)) return false;
446     for (unsigned int i = 0; i < count; i++)
447       if (!SANITIZE_THIS (coverage[i])) return false;
448     LookupRecord &lookupRecord = StructAtOffset<LookupRecord> (coverage, OffsetTo<Coverage>::get_size () * glyphCount);
449     return SANITIZE_ARRAY (&lookupRecord, LookupRecord::get_size (), lookupCount);
450   }
451
452   private:
453   USHORT        format;                 /* Format identifier--format = 3 */
454   USHORT        glyphCount;             /* Number of glyphs in the input glyph
455                                          * sequence */
456   USHORT        lookupCount;            /* Number of LookupRecords */
457   OffsetTo<Coverage>
458                 coverage[VAR];          /* Array of offsets to Coverage
459                                          * table in glyph sequence order */
460   LookupRecord  lookupRecordX[VAR];     /* Array of LookupRecords--in
461                                          * design order */
462 };
463 ASSERT_SIZE_VAR2 (ContextFormat3, 6, OffsetTo<Coverage>, LookupRecord);
464
465 struct Context
466 {
467   protected:
468   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
469   {
470     TRACE_APPLY ();
471     switch (u.format) {
472     case 1: return u.format1->apply (APPLY_ARG, apply_func);
473     case 2: return u.format2->apply (APPLY_ARG, apply_func);
474     case 3: return u.format3->apply (APPLY_ARG, apply_func);
475     default:return false;
476     }
477   }
478
479   inline bool sanitize (SANITIZE_ARG_DEF) {
480     TRACE_SANITIZE ();
481     if (!SANITIZE (u.format)) return false;
482     switch (u.format) {
483     case 1: return u.format1->sanitize (SANITIZE_ARG);
484     case 2: return u.format2->sanitize (SANITIZE_ARG);
485     case 3: return u.format3->sanitize (SANITIZE_ARG);
486     default:return true;
487     }
488   }
489
490   private:
491   union {
492   USHORT                format;         /* Format identifier */
493   ContextFormat1        format1[VAR];
494   ContextFormat2        format2[VAR];
495   ContextFormat3        format3[VAR];
496   } u;
497 };
498
499
500 /* Chaining Contextual lookups */
501
502 struct ChainContextLookupContext
503 {
504   ContextFuncs funcs;
505   const char *match_data[3];
506 };
507
508 static inline bool chain_context_lookup (APPLY_ARG_DEF,
509                                          unsigned int backtrackCount,
510                                          const USHORT backtrack[],
511                                          unsigned int inputCount, /* Including the first glyph (not matched) */
512                                          const USHORT input[], /* Array of input values--start with second glyph */
513                                          unsigned int lookaheadCount,
514                                          const USHORT lookahead[],
515                                          unsigned int lookupCount,
516                                          const LookupRecord lookupRecord[],
517                                          ChainContextLookupContext &lookup_context)
518 {
519   /* First guess */
520   if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
521                    buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
522                    inputCount + lookaheadCount > context_length))
523     return false;
524
525   unsigned int offset;
526   return match_backtrack (APPLY_ARG,
527                           backtrackCount, backtrack,
528                           lookup_context.funcs.match, lookup_context.match_data[0]) &&
529          match_input (APPLY_ARG,
530                       inputCount, input,
531                       lookup_context.funcs.match, lookup_context.match_data[1],
532                       &offset) &&
533          match_lookahead (APPLY_ARG,
534                           lookaheadCount, lookahead,
535                           lookup_context.funcs.match, lookup_context.match_data[2],
536                           offset) &&
537          (context_length = offset, true) &&
538          apply_lookup (APPLY_ARG,
539                        inputCount,
540                        lookupCount, lookupRecord,
541                        lookup_context.funcs.apply);
542 }
543
544 struct ChainRule
545 {
546   friend struct ChainRuleSet;
547
548   private:
549   inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
550   {
551     TRACE_APPLY ();
552     const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
553     const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
554     const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
555     return chain_context_lookup (APPLY_ARG,
556                                  backtrack.len, backtrack.array(),
557                                  input.len, input.array(),
558                                  lookahead.len, lookahead.array(),
559                                  lookup.len, lookup.array(),
560                                  lookup_context);
561     return false;
562   }
563
564   public:
565   inline bool sanitize (SANITIZE_ARG_DEF) {
566     TRACE_SANITIZE ();
567     if (!SANITIZE (backtrack)) return false;
568     HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
569     if (!SANITIZE (input)) return false;
570     ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
571     if (!SANITIZE (lookahead)) return false;
572     ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
573     return SANITIZE (lookup);
574   }
575
576   private:
577   ArrayOf<USHORT>
578                 backtrack;              /* Array of backtracking values
579                                          * (to be matched before the input
580                                          * sequence) */
581   HeadlessArrayOf<USHORT>
582                 inputX;                 /* Array of input values (start with
583                                          * second glyph) */
584   ArrayOf<USHORT>
585                 lookaheadX;             /* Array of lookahead values's (to be
586                                          * matched after the input sequence) */
587   ArrayOf<LookupRecord>
588                 lookupX;                /* Array of LookupRecords--in
589                                          * design order) */
590 };
591 ASSERT_SIZE (ChainRule, 8);
592
593 struct ChainRuleSet
594 {
595   inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
596   {
597     TRACE_APPLY ();
598     unsigned int num_rules = rule.len;
599     for (unsigned int i = 0; i < num_rules; i++)
600     {
601       if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
602         return true;
603     }
604
605     return false;
606   }
607
608   inline bool sanitize (SANITIZE_ARG_DEF) {
609     TRACE_SANITIZE ();
610     return SANITIZE_THIS (rule);
611   }
612
613   private:
614   OffsetArrayOf<ChainRule>
615                 rule;                   /* Array of ChainRule tables
616                                          * ordered by preference */
617 };
618 ASSERT_SIZE (ChainRuleSet, 2);
619
620 struct ChainContextFormat1
621 {
622   friend struct ChainContext;
623
624   private:
625   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
626   {
627     TRACE_APPLY ();
628     unsigned int index = (this+coverage) (IN_CURGLYPH ());
629     if (HB_LIKELY (index == NOT_COVERED))
630       return false;
631
632     const ChainRuleSet &rule_set = this+ruleSet[index];
633     struct ChainContextLookupContext lookup_context = {
634       {match_glyph, apply_func},
635       {NULL, NULL, NULL}
636     };
637     return rule_set.apply (APPLY_ARG, lookup_context);
638   }
639
640   inline bool sanitize (SANITIZE_ARG_DEF) {
641     TRACE_SANITIZE ();
642     return SANITIZE_THIS2 (coverage, ruleSet);
643   }
644
645   private:
646   USHORT        format;                 /* Format identifier--format = 1 */
647   OffsetTo<Coverage>
648                 coverage;               /* Offset to Coverage table--from
649                                          * beginning of table */
650   OffsetArrayOf<ChainRuleSet>
651                 ruleSet;                /* Array of ChainRuleSet tables
652                                          * ordered by Coverage Index */
653 };
654 ASSERT_SIZE (ChainContextFormat1, 6);
655
656 struct ChainContextFormat2
657 {
658   friend struct ChainContext;
659
660   private:
661   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
662   {
663     TRACE_APPLY ();
664     unsigned int index = (this+coverage) (IN_CURGLYPH ());
665     if (HB_LIKELY (index == NOT_COVERED))
666       return false;
667
668     const ClassDef &backtrack_class_def = this+backtrackClassDef;
669     const ClassDef &input_class_def = this+inputClassDef;
670     const ClassDef &lookahead_class_def = this+lookaheadClassDef;
671
672     index = input_class_def (IN_CURGLYPH ());
673     const ChainRuleSet &rule_set = this+ruleSet[index];
674     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
675      * them across subrule lookups.  Not sure it's worth it.
676      */
677     struct ChainContextLookupContext lookup_context = {
678      {match_class, apply_func},
679      {CharP(&backtrack_class_def),
680       CharP(&input_class_def),
681       CharP(&lookahead_class_def)}
682     };
683     return rule_set.apply (APPLY_ARG, lookup_context);
684   }
685
686   inline bool sanitize (SANITIZE_ARG_DEF) {
687     TRACE_SANITIZE ();
688     return SANITIZE_THIS2 (coverage, backtrackClassDef) &&
689            SANITIZE_THIS2 (inputClassDef, lookaheadClassDef) &&
690            SANITIZE_THIS (ruleSet);
691   }
692
693   private:
694   USHORT        format;                 /* Format identifier--format = 2 */
695   OffsetTo<Coverage>
696                 coverage;               /* Offset to Coverage table--from
697                                          * beginning of table */
698   OffsetTo<ClassDef>
699                 backtrackClassDef;      /* Offset to glyph ClassDef table
700                                          * containing backtrack sequence
701                                          * data--from beginning of table */
702   OffsetTo<ClassDef>
703                 inputClassDef;          /* Offset to glyph ClassDef
704                                          * table containing input sequence
705                                          * data--from beginning of table */
706   OffsetTo<ClassDef>
707                 lookaheadClassDef;      /* Offset to glyph ClassDef table
708                                          * containing lookahead sequence
709                                          * data--from beginning of table */
710   OffsetArrayOf<ChainRuleSet>
711                 ruleSet;                /* Array of ChainRuleSet tables
712                                          * ordered by class */
713 };
714 ASSERT_SIZE (ChainContextFormat2, 12);
715
716 struct ChainContextFormat3
717 {
718   friend struct ChainContext;
719
720   private:
721
722   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
723   {
724     TRACE_APPLY ();
725     const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
726
727     unsigned int index = (this+input[0]) (IN_CURGLYPH ());
728     if (HB_LIKELY (index == NOT_COVERED))
729       return false;
730
731     const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
732     const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
733     struct ChainContextLookupContext lookup_context = {
734       {match_coverage, apply_func},
735       {CharP(this), CharP(this), CharP(this)}
736     };
737     return chain_context_lookup (APPLY_ARG,
738                                  backtrack.len, (const USHORT *) backtrack.array(),
739                                  input.len, (const USHORT *) input.array() + 1,
740                                  lookahead.len, (const USHORT *) lookahead.array(),
741                                  lookup.len, lookup.array(),
742                                  lookup_context);
743     return false;
744   }
745
746   inline bool sanitize (SANITIZE_ARG_DEF) {
747     TRACE_SANITIZE ();
748     if (!SANITIZE_THIS (backtrack)) return false;
749     OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
750     if (!SANITIZE_THIS (input)) return false;
751     OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
752     if (!SANITIZE_THIS (lookahead)) return false;
753     ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
754     return SANITIZE (lookup);
755   }
756
757   private:
758   USHORT        format;                 /* Format identifier--format = 3 */
759   OffsetArrayOf<Coverage>
760                 backtrack;              /* Array of coverage tables
761                                          * in backtracking sequence, in  glyph
762                                          * sequence order */
763   OffsetArrayOf<Coverage>
764                 inputX          ;       /* Array of coverage
765                                          * tables in input sequence, in glyph
766                                          * sequence order */
767   OffsetArrayOf<Coverage>
768                 lookaheadX;             /* Array of coverage tables
769                                          * in lookahead sequence, in glyph
770                                          * sequence order */
771   ArrayOf<LookupRecord>
772                 lookupX;                /* Array of LookupRecords--in
773                                          * design order) */
774 };
775 ASSERT_SIZE (ChainContextFormat3, 10);
776
777 struct ChainContext
778 {
779   protected:
780   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
781   {
782     TRACE_APPLY ();
783     switch (u.format) {
784     case 1: return u.format1->apply (APPLY_ARG, apply_func);
785     case 2: return u.format2->apply (APPLY_ARG, apply_func);
786     case 3: return u.format3->apply (APPLY_ARG, apply_func);
787     default:return false;
788     }
789   }
790
791   inline bool sanitize (SANITIZE_ARG_DEF) {
792     TRACE_SANITIZE ();
793     if (!SANITIZE (u.format)) return false;
794     switch (u.format) {
795     case 1: return u.format1->sanitize (SANITIZE_ARG);
796     case 2: return u.format2->sanitize (SANITIZE_ARG);
797     case 3: return u.format3->sanitize (SANITIZE_ARG);
798     default:return true;
799     }
800   }
801
802   private:
803   union {
804   USHORT                format; /* Format identifier */
805   ChainContextFormat1   format1[VAR];
806   ChainContextFormat2   format2[VAR];
807   ChainContextFormat3   format3[VAR];
808   } u;
809 };
810
811
812 struct ExtensionFormat1
813 {
814   friend struct Extension;
815
816   protected:
817   inline unsigned int get_type (void) const { return extensionLookupType; }
818   inline unsigned int get_offset (void) const { return extensionOffset; }
819
820   inline bool sanitize (SANITIZE_ARG_DEF) {
821     TRACE_SANITIZE ();
822     return SANITIZE_SELF ();
823   }
824
825   private:
826   USHORT        format;                 /* Format identifier. Set to 1. */
827   USHORT        extensionLookupType;    /* Lookup type of subtable referenced
828                                          * by ExtensionOffset (i.e. the
829                                          * extension subtable). */
830   ULONG         extensionOffset;        /* Offset to the extension subtable,
831                                          * of lookup type subtable. */
832 };
833 ASSERT_SIZE (ExtensionFormat1, 8);
834
835 struct Extension
836 {
837   inline unsigned int get_type (void) const
838   {
839     switch (u.format) {
840     case 1: return u.format1->get_type ();
841     default:return 0;
842     }
843   }
844   inline unsigned int get_offset (void) const
845   {
846     switch (u.format) {
847     case 1: return u.format1->get_offset ();
848     default:return 0;
849     }
850   }
851
852   inline bool sanitize (SANITIZE_ARG_DEF) {
853     TRACE_SANITIZE ();
854     if (!SANITIZE (u.format)) return false;
855     switch (u.format) {
856     case 1: return u.format1->sanitize (SANITIZE_ARG);
857     default:return true;
858     }
859   }
860
861   private:
862   union {
863   USHORT                format;         /* Format identifier */
864   ExtensionFormat1      format1[VAR];
865   } u;
866 };
867
868
869 /*
870  * GSUB/GPOS Common
871  */
872
873 struct GSUBGPOS
874 {
875   static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
876   static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
877
878   inline unsigned int get_script_count (void) const
879   { return (this+scriptList).len; }
880   inline const Tag& get_script_tag (unsigned int i) const
881   { return (this+scriptList).get_tag (i); }
882   inline unsigned int get_script_tags (unsigned int start_offset,
883                                        unsigned int *script_count /* IN/OUT */,
884                                        hb_tag_t     *script_tags /* OUT */) const
885   { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
886   inline const Script& get_script (unsigned int i) const
887   { return (this+scriptList)[i]; }
888   inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
889   { return (this+scriptList).find_index (tag, index); }
890
891   inline unsigned int get_feature_count (void) const
892   { return (this+featureList).len; }
893   inline const Tag& get_feature_tag (unsigned int i) const
894   { return (this+featureList).get_tag (i); }
895   inline unsigned int get_feature_tags (unsigned int start_offset,
896                                         unsigned int *feature_count /* IN/OUT */,
897                                         hb_tag_t     *feature_tags /* OUT */) const
898   { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
899   inline const Feature& get_feature (unsigned int i) const
900   { return (this+featureList)[i]; }
901   inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
902   { return (this+featureList).find_index (tag, index); }
903
904   inline unsigned int get_lookup_count (void) const
905   { return (this+lookupList).len; }
906   inline const Lookup& get_lookup (unsigned int i) const
907   { return (this+lookupList)[i]; }
908
909   inline bool sanitize (SANITIZE_ARG_DEF) {
910     TRACE_SANITIZE ();
911     if (!SANITIZE (version)) return false;
912     if (HB_UNLIKELY (version.major != 1)) return false;
913     return SANITIZE_THIS3 (scriptList, featureList, lookupList);
914   }
915
916   protected:
917   FixedVersion  version;        /* Version of the GSUB/GPOS table--initially set
918                                  * to 0x00010000 */
919   OffsetTo<ScriptList>
920                 scriptList;     /* ScriptList table */
921   OffsetTo<FeatureList>
922                 featureList;    /* FeatureList table */
923   OffsetTo<LookupList>
924                 lookupList;     /* LookupList table */
925 };
926 ASSERT_SIZE (GSUBGPOS, 10);
927
928
929 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */