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