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