Cosmetic
[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   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   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   hb_apply_context_t new_context = *context;
262   return match_input (context,
263                       inputCount, input,
264                       lookup_context.funcs.match, lookup_context.match_data,
265                       &new_context.context_length)
266       && apply_lookup (&new_context,
267                        inputCount,
268                        lookupCount, lookupRecord,
269                        lookup_context.funcs.apply);
270 }
271
272 struct Rule
273 {
274   friend struct RuleSet;
275
276   private:
277   inline bool apply (hb_apply_context_t *context, ContextLookupContext &lookup_context) const
278   {
279     TRACE_APPLY ();
280     const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].get_size () * (inputCount ? inputCount - 1 : 0));
281     return context_lookup (context,
282                            inputCount, input,
283                            lookupCount, lookupRecord,
284                            lookup_context);
285   }
286
287   public:
288   inline bool sanitize (hb_sanitize_context_t *context) {
289     TRACE_SANITIZE ();
290     return SANITIZE (inputCount)
291         && SANITIZE (lookupCount)
292         && SANITIZE_MEM (input,
293                          input[0].get_size () * inputCount +
294                          lookupRecordX[0].get_size () * lookupCount);
295   }
296
297   private:
298   USHORT        inputCount;             /* Total number of glyphs in input
299                                          * glyph sequence--includes the  first
300                                          * glyph */
301   USHORT        lookupCount;            /* Number of LookupRecords */
302   USHORT        input[VAR];             /* Array of match inputs--start with
303                                          * second glyph */
304   LookupRecord  lookupRecordX[VAR];     /* Array of LookupRecords--in
305                                          * design order */
306 };
307 ASSERT_SIZE_VAR2 (Rule, 4, USHORT, LookupRecord);
308
309 struct RuleSet
310 {
311   inline bool apply (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 SANITIZE_WITH_BASE (this, rule);
327   }
328
329   private:
330   OffsetArrayOf<Rule>
331                 rule;                   /* Array of Rule tables
332                                          * ordered by preference */
333 };
334
335
336 struct ContextFormat1
337 {
338   friend struct Context;
339
340   private:
341   inline bool apply (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 SANITIZE_WITH_BASE (this, coverage)
359         && SANITIZE_WITH_BASE (this, ruleSet);
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 SANITIZE_WITH_BASE (this, coverage)
402         && SANITIZE_WITH_BASE (this, classDef)
403         && SANITIZE_WITH_BASE (this, ruleSet);
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].get_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 (!SANITIZE_SELF ()) return false;
447     unsigned int count = glyphCount;
448     if (!SANITIZE_ARRAY (coverage, OffsetTo<Coverage>::get_size (), count)) return false;
449     for (unsigned int i = 0; i < count; i++)
450       if (!SANITIZE_WITH_BASE (this, coverage[i])) return false;
451     LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, OffsetTo<Coverage>::get_size () * count);
452     return SANITIZE_ARRAY (lookupRecord, LookupRecord::get_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 };
466 ASSERT_SIZE_VAR2 (ContextFormat3, 6, OffsetTo<Coverage>, LookupRecord);
467
468 struct Context
469 {
470   protected:
471   inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
472   {
473     TRACE_APPLY ();
474     switch (u.format) {
475     case 1: return u.format1->apply (context, apply_func);
476     case 2: return u.format2->apply (context, apply_func);
477     case 3: return u.format3->apply (context, apply_func);
478     default:return false;
479     }
480   }
481
482   inline bool sanitize (hb_sanitize_context_t *context) {
483     TRACE_SANITIZE ();
484     if (!SANITIZE (u.format)) return false;
485     switch (u.format) {
486     case 1: return u.format1->sanitize (context);
487     case 2: return u.format2->sanitize (context);
488     case 3: return u.format3->sanitize (context);
489     default:return true;
490     }
491   }
492
493   private:
494   union {
495   USHORT                format;         /* Format identifier */
496   ContextFormat1        format1[VAR];
497   ContextFormat2        format2[VAR];
498   ContextFormat3        format3[VAR];
499   } u;
500 };
501
502
503 /* Chaining Contextual lookups */
504
505 struct ChainContextLookupContext
506 {
507   ContextFuncs funcs;
508   const char *match_data[3];
509 };
510
511 static inline bool chain_context_lookup (hb_apply_context_t *context,
512                                          unsigned int backtrackCount,
513                                          const USHORT backtrack[],
514                                          unsigned int inputCount, /* Including the first glyph (not matched) */
515                                          const USHORT input[], /* Array of input values--start with second glyph */
516                                          unsigned int lookaheadCount,
517                                          const USHORT lookahead[],
518                                          unsigned int lookupCount,
519                                          const LookupRecord lookupRecord[],
520                                          ChainContextLookupContext &lookup_context)
521 {
522   /* First guess */
523   if (unlikely (context->buffer->out_pos < backtrackCount ||
524                 context->buffer->in_pos + inputCount + lookaheadCount > context->buffer->in_length ||
525                 inputCount + lookaheadCount > context->context_length))
526     return false;
527
528   hb_apply_context_t new_context = *context;
529   return match_backtrack (context,
530                           backtrackCount, backtrack,
531                           lookup_context.funcs.match, lookup_context.match_data[0])
532       && match_input (context,
533                       inputCount, input,
534                       lookup_context.funcs.match, lookup_context.match_data[1],
535                       &new_context.context_length)
536       && match_lookahead (context,
537                           lookaheadCount, lookahead,
538                           lookup_context.funcs.match, lookup_context.match_data[2],
539                           new_context.context_length)
540       && apply_lookup (&new_context,
541                        inputCount,
542                        lookupCount, lookupRecord,
543                        lookup_context.funcs.apply);
544 }
545
546 struct ChainRule
547 {
548   friend struct ChainRuleSet;
549
550   private:
551   inline bool apply (hb_apply_context_t *context, ChainContextLookupContext &lookup_context) const
552   {
553     TRACE_APPLY ();
554     const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
555     const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
556     const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
557     return chain_context_lookup (context,
558                                  backtrack.len, backtrack.array(),
559                                  input.len, input.array(),
560                                  lookahead.len, lookahead.array(),
561                                  lookup.len, lookup.array(),
562                                  lookup_context);
563     return false;
564   }
565
566   public:
567   inline bool sanitize (hb_sanitize_context_t *context) {
568     TRACE_SANITIZE ();
569     if (!SANITIZE (backtrack)) return false;
570     HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
571     if (!SANITIZE (input)) return false;
572     ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
573     if (!SANITIZE (lookahead)) return false;
574     ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
575     return SANITIZE (lookup);
576   }
577
578   private:
579   ArrayOf<USHORT>
580                 backtrack;              /* Array of backtracking values
581                                          * (to be matched before the input
582                                          * sequence) */
583   HeadlessArrayOf<USHORT>
584                 inputX;                 /* Array of input values (start with
585                                          * second glyph) */
586   ArrayOf<USHORT>
587                 lookaheadX;             /* Array of lookahead values's (to be
588                                          * matched after the input sequence) */
589   ArrayOf<LookupRecord>
590                 lookupX;                /* Array of LookupRecords--in
591                                          * design order) */
592 };
593 ASSERT_SIZE (ChainRule, 8);
594
595 struct ChainRuleSet
596 {
597   inline bool apply (hb_apply_context_t *context, ChainContextLookupContext &lookup_context) const
598   {
599     TRACE_APPLY ();
600     unsigned int num_rules = rule.len;
601     for (unsigned int i = 0; i < num_rules; i++)
602     {
603       if ((this+rule[i]).apply (context, lookup_context))
604         return true;
605     }
606
607     return false;
608   }
609
610   inline bool sanitize (hb_sanitize_context_t *context) {
611     TRACE_SANITIZE ();
612     return SANITIZE_WITH_BASE (this, rule);
613   }
614
615   private:
616   OffsetArrayOf<ChainRule>
617                 rule;                   /* Array of ChainRule tables
618                                          * ordered by preference */
619 };
620 ASSERT_SIZE (ChainRuleSet, 2);
621
622 struct ChainContextFormat1
623 {
624   friend struct ChainContext;
625
626   private:
627   inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
628   {
629     TRACE_APPLY ();
630     unsigned int index = (this+coverage) (IN_CURGLYPH ());
631     if (likely (index == NOT_COVERED))
632       return false;
633
634     const ChainRuleSet &rule_set = this+ruleSet[index];
635     struct ChainContextLookupContext lookup_context = {
636       {match_glyph, apply_func},
637       {NULL, NULL, NULL}
638     };
639     return rule_set.apply (context, lookup_context);
640   }
641
642   inline bool sanitize (hb_sanitize_context_t *context) {
643     TRACE_SANITIZE ();
644     return SANITIZE_WITH_BASE (this, coverage)
645         && SANITIZE_WITH_BASE (this, ruleSet);
646   }
647
648   private:
649   USHORT        format;                 /* Format identifier--format = 1 */
650   OffsetTo<Coverage>
651                 coverage;               /* Offset to Coverage table--from
652                                          * beginning of table */
653   OffsetArrayOf<ChainRuleSet>
654                 ruleSet;                /* Array of ChainRuleSet tables
655                                          * ordered by Coverage Index */
656 };
657 ASSERT_SIZE (ChainContextFormat1, 6);
658
659 struct ChainContextFormat2
660 {
661   friend struct ChainContext;
662
663   private:
664   inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
665   {
666     TRACE_APPLY ();
667     unsigned int index = (this+coverage) (IN_CURGLYPH ());
668     if (likely (index == NOT_COVERED))
669       return false;
670
671     const ClassDef &backtrack_class_def = this+backtrackClassDef;
672     const ClassDef &input_class_def = this+inputClassDef;
673     const ClassDef &lookahead_class_def = this+lookaheadClassDef;
674
675     index = input_class_def (IN_CURGLYPH ());
676     const ChainRuleSet &rule_set = this+ruleSet[index];
677     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
678      * them across subrule lookups.  Not sure it's worth it.
679      */
680     struct ChainContextLookupContext lookup_context = {
681      {match_class, apply_func},
682      {CharP(&backtrack_class_def),
683       CharP(&input_class_def),
684       CharP(&lookahead_class_def)}
685     };
686     return rule_set.apply (context, lookup_context);
687   }
688
689   inline bool sanitize (hb_sanitize_context_t *context) {
690     TRACE_SANITIZE ();
691     return SANITIZE_WITH_BASE (this, coverage)
692         && SANITIZE_WITH_BASE (this, backtrackClassDef)
693         && SANITIZE_WITH_BASE (this, inputClassDef)
694         && SANITIZE_WITH_BASE (this, lookaheadClassDef)
695         && SANITIZE_WITH_BASE (this, ruleSet);
696   }
697
698   private:
699   USHORT        format;                 /* Format identifier--format = 2 */
700   OffsetTo<Coverage>
701                 coverage;               /* Offset to Coverage table--from
702                                          * beginning of table */
703   OffsetTo<ClassDef>
704                 backtrackClassDef;      /* Offset to glyph ClassDef table
705                                          * containing backtrack sequence
706                                          * data--from beginning of table */
707   OffsetTo<ClassDef>
708                 inputClassDef;          /* Offset to glyph ClassDef
709                                          * table containing input sequence
710                                          * data--from beginning of table */
711   OffsetTo<ClassDef>
712                 lookaheadClassDef;      /* Offset to glyph ClassDef table
713                                          * containing lookahead sequence
714                                          * data--from beginning of table */
715   OffsetArrayOf<ChainRuleSet>
716                 ruleSet;                /* Array of ChainRuleSet tables
717                                          * ordered by class */
718 };
719 ASSERT_SIZE (ChainContextFormat2, 12);
720
721 struct ChainContextFormat3
722 {
723   friend struct ChainContext;
724
725   private:
726
727   inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
728   {
729     TRACE_APPLY ();
730     const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
731
732     unsigned int index = (this+input[0]) (IN_CURGLYPH ());
733     if (likely (index == NOT_COVERED))
734       return false;
735
736     const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
737     const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
738     struct ChainContextLookupContext lookup_context = {
739       {match_coverage, apply_func},
740       {CharP(this), CharP(this), CharP(this)}
741     };
742     return chain_context_lookup (context,
743                                  backtrack.len, (const USHORT *) backtrack.array(),
744                                  input.len, (const USHORT *) input.array() + 1,
745                                  lookahead.len, (const USHORT *) lookahead.array(),
746                                  lookup.len, lookup.array(),
747                                  lookup_context);
748     return false;
749   }
750
751   inline bool sanitize (hb_sanitize_context_t *context) {
752     TRACE_SANITIZE ();
753     if (!SANITIZE_WITH_BASE (this, backtrack)) return false;
754     OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
755     if (!SANITIZE_WITH_BASE (this, input)) return false;
756     OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
757     if (!SANITIZE_WITH_BASE (this, lookahead)) return false;
758     ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
759     return SANITIZE (lookup);
760   }
761
762   private:
763   USHORT        format;                 /* Format identifier--format = 3 */
764   OffsetArrayOf<Coverage>
765                 backtrack;              /* Array of coverage tables
766                                          * in backtracking sequence, in  glyph
767                                          * sequence order */
768   OffsetArrayOf<Coverage>
769                 inputX          ;       /* Array of coverage
770                                          * tables in input sequence, in glyph
771                                          * sequence order */
772   OffsetArrayOf<Coverage>
773                 lookaheadX;             /* Array of coverage tables
774                                          * in lookahead sequence, in glyph
775                                          * sequence order */
776   ArrayOf<LookupRecord>
777                 lookupX;                /* Array of LookupRecords--in
778                                          * design order) */
779 };
780 ASSERT_SIZE (ChainContextFormat3, 10);
781
782 struct ChainContext
783 {
784   protected:
785   inline bool apply (hb_apply_context_t *context, apply_lookup_func_t apply_func) const
786   {
787     TRACE_APPLY ();
788     switch (u.format) {
789     case 1: return u.format1->apply (context, apply_func);
790     case 2: return u.format2->apply (context, apply_func);
791     case 3: return u.format3->apply (context, apply_func);
792     default:return false;
793     }
794   }
795
796   inline bool sanitize (hb_sanitize_context_t *context) {
797     TRACE_SANITIZE ();
798     if (!SANITIZE (u.format)) return false;
799     switch (u.format) {
800     case 1: return u.format1->sanitize (context);
801     case 2: return u.format2->sanitize (context);
802     case 3: return u.format3->sanitize (context);
803     default:return true;
804     }
805   }
806
807   private:
808   union {
809   USHORT                format; /* Format identifier */
810   ChainContextFormat1   format1[VAR];
811   ChainContextFormat2   format2[VAR];
812   ChainContextFormat3   format3[VAR];
813   } u;
814 };
815
816
817 struct ExtensionFormat1
818 {
819   friend struct Extension;
820
821   protected:
822   inline unsigned int get_type (void) const { return extensionLookupType; }
823   inline unsigned int get_offset (void) const { return extensionOffset; }
824
825   inline bool sanitize (hb_sanitize_context_t *context) {
826     TRACE_SANITIZE ();
827     return SANITIZE_SELF ();
828   }
829
830   private:
831   USHORT        format;                 /* Format identifier. Set to 1. */
832   USHORT        extensionLookupType;    /* Lookup type of subtable referenced
833                                          * by ExtensionOffset (i.e. the
834                                          * extension subtable). */
835   ULONG         extensionOffset;        /* Offset to the extension subtable,
836                                          * of lookup type subtable. */
837 };
838 ASSERT_SIZE (ExtensionFormat1, 8);
839
840 struct Extension
841 {
842   inline unsigned int get_type (void) const
843   {
844     switch (u.format) {
845     case 1: return u.format1->get_type ();
846     default:return 0;
847     }
848   }
849   inline unsigned int get_offset (void) const
850   {
851     switch (u.format) {
852     case 1: return u.format1->get_offset ();
853     default:return 0;
854     }
855   }
856
857   inline bool sanitize (hb_sanitize_context_t *context) {
858     TRACE_SANITIZE ();
859     if (!SANITIZE (u.format)) return false;
860     switch (u.format) {
861     case 1: return u.format1->sanitize (context);
862     default:return true;
863     }
864   }
865
866   private:
867   union {
868   USHORT                format;         /* Format identifier */
869   ExtensionFormat1      format1[VAR];
870   } u;
871 };
872
873
874 /*
875  * GSUB/GPOS Common
876  */
877
878 struct GSUBGPOS
879 {
880   static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
881   static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
882
883   inline unsigned int get_script_count (void) const
884   { return (this+scriptList).len; }
885   inline const Tag& get_script_tag (unsigned int i) const
886   { return (this+scriptList).get_tag (i); }
887   inline unsigned int get_script_tags (unsigned int start_offset,
888                                        unsigned int *script_count /* IN/OUT */,
889                                        hb_tag_t     *script_tags /* OUT */) const
890   { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
891   inline const Script& get_script (unsigned int i) const
892   { return (this+scriptList)[i]; }
893   inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
894   { return (this+scriptList).find_index (tag, index); }
895
896   inline unsigned int get_feature_count (void) const
897   { return (this+featureList).len; }
898   inline const Tag& get_feature_tag (unsigned int i) const
899   { return (this+featureList).get_tag (i); }
900   inline unsigned int get_feature_tags (unsigned int start_offset,
901                                         unsigned int *feature_count /* IN/OUT */,
902                                         hb_tag_t     *feature_tags /* OUT */) const
903   { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
904   inline const Feature& get_feature (unsigned int i) const
905   { return (this+featureList)[i]; }
906   inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
907   { return (this+featureList).find_index (tag, index); }
908
909   inline unsigned int get_lookup_count (void) const
910   { return (this+lookupList).len; }
911   inline const Lookup& get_lookup (unsigned int i) const
912   { return (this+lookupList)[i]; }
913
914   inline bool sanitize (hb_sanitize_context_t *context) {
915     TRACE_SANITIZE ();
916     return SANITIZE (version) && likely (version.major == 1)
917         && SANITIZE_WITH_BASE (this, scriptList)
918         && SANITIZE_WITH_BASE (this, featureList)
919         && SANITIZE_WITH_BASE (this, lookupList);
920   }
921
922   protected:
923   FixedVersion  version;        /* Version of the GSUB/GPOS table--initially set
924                                  * to 0x00010000 */
925   OffsetTo<ScriptList>
926                 scriptList;     /* ScriptList table */
927   OffsetTo<FeatureList>
928                 featureList;    /* FeatureList table */
929   OffsetTo<LookupList>
930                 lookupList;     /* LookupList table */
931 };
932 ASSERT_SIZE (GSUBGPOS, 10);
933
934
935 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */