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