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