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