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