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