[GSUB] Fix context_length handling in Ligature too
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gsubgpos-private.h
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_H
28 #define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H
29
30 #include "hb-ot-layout-gdef-private.h"
31 #include "harfbuzz-buffer-private.h" /* XXX */
32
33
34 #define LOOKUP_ARGS_DEF \
35         hb_ot_layout_t *layout, \
36         hb_buffer_t    *buffer, \
37         unsigned int    context_length HB_GNUC_UNUSED, \
38         unsigned int    nesting_level_left HB_GNUC_UNUSED, \
39         unsigned int    lookup_flag, \
40         unsigned int    property HB_GNUC_UNUSED /* propety of first glyph */
41 #define LOOKUP_ARGS \
42         layout, \
43         buffer, \
44         context_length, \
45         nesting_level_left, \
46         lookup_flag, \
47         property
48
49
50 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, char *data);
51 typedef bool (*apply_lookup_func_t) (LOOKUP_ARGS_DEF, unsigned int lookup_index);
52
53 struct ContextFuncs {
54   match_func_t match;
55   apply_lookup_func_t apply;
56 };
57
58
59 static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
60   return glyph_id == value;
61 }
62
63 static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
64   const ClassDef &class_def = * (const ClassDef *) data;
65   return class_def.get_class (glyph_id) == value;
66 }
67
68 static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
69   const OffsetTo<Coverage> &coverage = * (const OffsetTo<Coverage> *) &value;
70   return (data+coverage) (glyph_id) != NOT_COVERED;
71 }
72
73
74 static inline bool match_input (LOOKUP_ARGS_DEF,
75                                 unsigned int count, /* Including the first glyph (not matched) */
76                                 const USHORT input[], /* Array of input values--start with second glyph */
77                                 match_func_t match_func,
78                                 char *match_data,
79                                 unsigned int *context_length_out)
80 {
81   unsigned int i, j;
82   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
83   if (HB_UNLIKELY (buffer->in_pos + count > end))
84     return false;
85
86   for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++) {
87     while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property)) {
88       if (HB_UNLIKELY (j + count - i == end))
89         return false;
90       j++;
91     }
92
93     if (HB_LIKELY (!match_func (IN_GLYPH(j), input[i - 1], match_data)))
94       return false;
95   }
96
97   *context_length_out = j - buffer->in_pos;
98
99   return true;
100 }
101
102 static inline bool match_backtrack (LOOKUP_ARGS_DEF,
103                                     unsigned int count,
104                                     const USHORT backtrack[],
105                                     match_func_t match_func,
106                                     char *match_data)
107 {
108   if (HB_UNLIKELY (buffer->out_pos < count))
109     return false;
110
111   for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--) {
112     while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
113       if (HB_UNLIKELY (j + 1 == count - i))
114         return false;
115       j--;
116     }
117
118     if (HB_LIKELY (!match_func (OUT_GLYPH(j), backtrack[i], match_data)))
119       return false;
120   }
121
122   return true;
123 }
124
125 static inline bool match_lookahead (LOOKUP_ARGS_DEF,
126                                     unsigned int count,
127                                     const USHORT lookahead[],
128                                     match_func_t match_func,
129                                     char *match_data,
130                                     unsigned int offset)
131 {
132   unsigned int i, j;
133   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
134   if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
135     return false;
136
137   for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++) {
138     while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
139       if (HB_UNLIKELY (j + count - i == end))
140         return false;
141       j++;
142     }
143
144     if (HB_LIKELY (!match_func (IN_GLYPH(j), lookahead[i], match_data)))
145       return false;
146   }
147
148   return true;
149 }
150
151
152 struct LookupRecord {
153
154   USHORT        sequenceIndex;          /* Index into current glyph
155                                          * sequence--first glyph = 0 */
156   USHORT        lookupListIndex;        /* Lookup to apply to that
157                                          * position--zero--based */
158 };
159 ASSERT_SIZE (LookupRecord, 4);
160
161 static inline bool apply_lookup (LOOKUP_ARGS_DEF,
162                                  unsigned int count, /* Including the first glyph */
163                                  unsigned int lookupCount,
164                                  const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
165                                  apply_lookup_func_t apply_func)
166 {
167   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
168   if (HB_UNLIKELY (buffer->in_pos + count > end))
169     return false;
170
171   /* TODO We don't support lookupRecord arrays that are not increasing:
172    *      Should be easy for in_place ones at least. */
173   for (unsigned int i = 0; i < count; i++) {
174     while (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM (), lookup_flag, &property)) {
175       if (HB_UNLIKELY (buffer->in_pos == end))
176         return true;
177       /* No lookup applied for this index */
178       _hb_buffer_next_glyph (buffer);
179     }
180
181     if (lookupCount && i == lookupRecord->sequenceIndex)
182     {
183       unsigned int old_pos = buffer->in_pos;
184
185       /* Apply a lookup */
186       bool done = apply_func (LOOKUP_ARGS, lookupRecord->lookupListIndex);
187
188       lookupRecord++;
189       lookupCount--;
190       i += buffer->in_pos - old_pos;
191
192       if (!done)
193         goto not_applied;
194     }
195     else
196     {
197     not_applied:
198       /* No lookup applied for this index */
199       _hb_buffer_next_glyph (buffer);
200       i++;
201     }
202   }
203
204   return true;
205 }
206
207
208 /* Contextual lookups */
209
210 struct ContextLookupContext {
211   ContextFuncs funcs;
212   char *match_data;
213 };
214
215 static inline bool context_lookup (LOOKUP_ARGS_DEF,
216                                    unsigned int inputCount, /* Including the first glyph (not matched) */
217                                    const USHORT input[], /* Array of input values--start with second glyph */
218                                    unsigned int lookupCount,
219                                    const LookupRecord lookupRecord[],
220                                    ContextLookupContext &context)
221 {
222   /* First guess */
223   if (HB_UNLIKELY (buffer->in_pos + inputCount > buffer->in_length ||
224                    inputCount > context_length))
225     return false;
226
227   return match_input (LOOKUP_ARGS,
228                       inputCount, input,
229                       context.funcs.match, context.match_data,
230                       &context_length) &&
231          apply_lookup (LOOKUP_ARGS,
232                        inputCount,
233                        lookupCount, lookupRecord,
234                        context.funcs.apply);
235 }
236
237 struct Rule {
238
239   friend struct RuleSet;
240
241   private:
242   inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
243     const LookupRecord *lookupRecord = (const LookupRecord *)
244                                        ((const char *) input +
245                                         sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
246     return context_lookup (LOOKUP_ARGS,
247                            inputCount,
248                            input,
249                            lookupCount,
250                            lookupRecord,
251                            context);
252   }
253
254   private:
255   USHORT        inputCount;             /* Total number of glyphs in input
256                                          * glyph sequence--includes the  first
257                                          * glyph */
258   USHORT        lookupCount;            /* Number of LookupRecords */
259   USHORT        input[];                /* Array of match inputs--start with
260                                          * second glyph */
261   LookupRecord  lookupRecordX[];        /* Array of LookupRecords--in
262                                          * design order */
263 };
264 ASSERT_SIZE (Rule, 4);
265
266 struct RuleSet {
267
268   inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
269
270     unsigned int num_rules = rule.len;
271     for (unsigned int i = 0; i < num_rules; i++) {
272       if ((this+rule[i]).apply (LOOKUP_ARGS, context))
273         return true;
274     }
275
276     return false;
277   }
278
279   private:
280   OffsetArrayOf<Rule>
281                 rule;                   /* Array of Rule tables
282                                          * ordered by preference */
283 };
284
285
286 struct ContextFormat1 {
287
288   friend struct Context;
289
290   private:
291   inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
292
293     unsigned int index = (this+coverage) (IN_CURGLYPH ());
294     if (G_LIKELY (index == NOT_COVERED))
295       return false;
296
297     const RuleSet &rule_set = this+ruleSet[index];
298     struct ContextLookupContext context = {
299       {match_glyph, apply_func},
300       NULL
301     };
302     return rule_set.apply (LOOKUP_ARGS, context);
303   }
304
305   private:
306   USHORT        format;                 /* Format identifier--format = 1 */
307   OffsetTo<Coverage>
308                 coverage;               /* Offset to Coverage table--from
309                                          * beginning of table */
310   OffsetArrayOf<RuleSet>
311                 ruleSet;                /* Array of RuleSet tables
312                                          * ordered by Coverage Index */
313 };
314 ASSERT_SIZE (ContextFormat1, 6);
315
316
317 struct ContextFormat2 {
318
319   friend struct Context;
320
321   private:
322   inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
323
324     unsigned int index = (this+coverage) (IN_CURGLYPH ());
325     if (G_LIKELY (index == NOT_COVERED))
326       return false;
327
328     const ClassDef &class_def = this+classDef;
329     index = class_def (IN_CURGLYPH ());
330     const RuleSet &rule_set = this+ruleSet[index];
331     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
332      * them across subrule lookups.  Not sure it's worth it.
333      */
334     struct ContextLookupContext context = {
335      {match_class, apply_func},
336       (char *) &class_def
337     };
338     return rule_set.apply (LOOKUP_ARGS, context);
339   }
340
341   private:
342   USHORT        format;                 /* Format identifier--format = 2 */
343   OffsetTo<Coverage>
344                 coverage;               /* Offset to Coverage table--from
345                                          * beginning of table */
346   OffsetTo<ClassDef>
347                 classDef;               /* Offset to glyph ClassDef table--from
348                                          * beginning of table */
349   OffsetArrayOf<RuleSet>
350                 ruleSet;                /* Array of RuleSet tables
351                                          * ordered by class */
352 };
353 ASSERT_SIZE (ContextFormat2, 8);
354
355
356 struct ContextFormat3 {
357
358   friend struct Context;
359
360   private:
361   inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
362
363     unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
364     if (G_LIKELY (index == NOT_COVERED))
365       return false;
366
367     const LookupRecord *lookupRecord = (const LookupRecord *)
368                                        ((const char *) coverage +
369                                         sizeof (coverage[0]) * glyphCount);
370     struct ContextLookupContext context = {
371       {match_coverage, apply_func},
372       (char *) this
373     };
374     return context_lookup (LOOKUP_ARGS,
375                            glyphCount,
376                            (const USHORT *) (coverage + 1),
377                            lookupCount,
378                            lookupRecord,
379                            context);
380   }
381
382   private:
383   USHORT        format;                 /* Format identifier--format = 3 */
384   USHORT        glyphCount;             /* Number of glyphs in the input glyph
385                                          * sequence */
386   USHORT        lookupCount;            /* Number of LookupRecords */
387   OffsetTo<Coverage>
388                 coverage[];             /* Array of offsets to Coverage
389                                          * table in glyph sequence order */
390   LookupRecord  lookupRecordX[];        /* Array of LookupRecords--in
391                                          * design order */
392 };
393 ASSERT_SIZE (ContextFormat3, 6);
394
395 struct Context {
396
397   protected:
398   bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
399     switch (u.format) {
400     case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
401     case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
402     case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
403     default:return false;
404     }
405   }
406
407   private:
408   union {
409   USHORT                format;         /* Format identifier */
410   ContextFormat1        format1[];
411   ContextFormat2        format2[];
412   ContextFormat3        format3[];
413   } u;
414 };
415 ASSERT_SIZE (Context, 2);
416
417
418 /* Chaining Contextual lookups */
419
420 struct ChainContextLookupContext {
421   ContextFuncs funcs;
422   char *match_data[3];
423 };
424
425 static inline bool chain_context_lookup (LOOKUP_ARGS_DEF,
426                                          unsigned int backtrackCount,
427                                          const USHORT backtrack[],
428                                          unsigned int inputCount, /* Including the first glyph (not matched) */
429                                          const USHORT input[], /* Array of input values--start with second glyph */
430                                          unsigned int lookaheadCount,
431                                          const USHORT lookahead[],
432                                          unsigned int lookupCount,
433                                          const LookupRecord lookupRecord[],
434                                          ChainContextLookupContext &context)
435 {
436   /* First guess */
437   if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
438                    buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
439                    inputCount + lookaheadCount > context_length))
440     return false;
441
442   unsigned int offset;
443   return match_backtrack (LOOKUP_ARGS,
444                           backtrackCount, backtrack,
445                           context.funcs.match, context.match_data[0]) &&
446          match_input (LOOKUP_ARGS,
447                       inputCount, input,
448                       context.funcs.match, context.match_data[1],
449                       &offset) &&
450          match_lookahead (LOOKUP_ARGS,
451                           lookaheadCount, lookahead,
452                           context.funcs.match, context.match_data[2],
453                           offset) &&
454          (context_length = offset, true) &&
455          apply_lookup (LOOKUP_ARGS,
456                        inputCount,
457                        lookupCount, lookupRecord,
458                        context.funcs.apply);
459 }
460
461 struct ChainRule {
462
463   friend struct ChainRuleSet;
464
465   private:
466   inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
467     const HeadlessArrayOf<USHORT> &input = * (const HeadlessArrayOf<USHORT> *)
468                                              ((const char *) &backtrack + backtrack.get_size ());
469     const ArrayOf<USHORT> &lookahead = * (const ArrayOf<USHORT> *)
470                                          ((const char *) &input + input.get_size ());
471     const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
472                                             ((const char *) &lookahead + lookahead.get_size ());
473     return chain_context_lookup (LOOKUP_ARGS,
474                                  backtrack.len,
475                                  backtrack.array,
476                                  input.len,
477                                  input.array + 1,
478                                  lookahead.len,
479                                  lookahead.array,
480                                  lookup.len,
481                                  lookup.array,
482                                  context);
483     return false;
484   }
485
486
487   private:
488   ArrayOf<USHORT>
489                 backtrack;              /* Array of backtracking values
490                                          * (to be matched before the input
491                                          * sequence) */
492   HeadlessArrayOf<USHORT>
493                 inputX;                 /* Array of input values (start with
494                                          * second glyph) */
495   ArrayOf<USHORT>
496                 lookaheadX;             /* Array of lookahead values's (to be
497                                          * matched after the input sequence) */
498   ArrayOf<LookupRecord>
499                 lookupX;                /* Array of LookupRecords--in
500                                          * design order) */
501 };
502 ASSERT_SIZE (ChainRule, 8);
503
504 struct ChainRuleSet {
505
506   inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
507
508     unsigned int num_rules = rule.len;
509     for (unsigned int i = 0; i < num_rules; i++) {
510       if ((this+rule[i]).apply (LOOKUP_ARGS, context))
511         return true;
512     }
513
514     return false;
515   }
516
517   private:
518   OffsetArrayOf<ChainRule>
519                 rule;                   /* Array of ChainRule tables
520                                          * ordered by preference */
521 };
522 ASSERT_SIZE (ChainRuleSet, 2);
523
524 struct ChainContextFormat1 {
525
526   friend struct ChainContext;
527
528   private:
529   inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
530
531     unsigned int index = (this+coverage) (IN_CURGLYPH ());
532     if (G_LIKELY (index == NOT_COVERED))
533       return false;
534
535     const ChainRuleSet &rule_set = this+ruleSet[index];
536     struct ChainContextLookupContext context = {
537       {match_glyph, apply_func},
538       {NULL, NULL, NULL}
539     };
540     return rule_set.apply (LOOKUP_ARGS, context);
541   }
542   private:
543   USHORT        format;                 /* Format identifier--format = 1 */
544   OffsetTo<Coverage>
545                 coverage;               /* Offset to Coverage table--from
546                                          * beginning of table */
547   OffsetArrayOf<ChainRuleSet>
548                 ruleSet;                /* Array of ChainRuleSet tables
549                                          * ordered by Coverage Index */
550 };
551 ASSERT_SIZE (ChainContextFormat1, 6);
552
553 struct ChainContextFormat2 {
554
555   friend struct ChainContext;
556
557   private:
558   inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
559
560     unsigned int index = (this+coverage) (IN_CURGLYPH ());
561     if (G_LIKELY (index == NOT_COVERED))
562       return false;
563
564     const ClassDef &backtrack_class_def = this+backtrackClassDef;
565     const ClassDef &input_class_def = this+inputClassDef;
566     const ClassDef &lookahead_class_def = this+lookaheadClassDef;
567
568     index = input_class_def (IN_CURGLYPH ());
569     const ChainRuleSet &rule_set = this+ruleSet[index];
570     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
571      * them across subrule lookups.  Not sure it's worth it.
572      */
573     struct ChainContextLookupContext context = {
574      {match_class, apply_func},
575      {(char *) &backtrack_class_def,
576       (char *) &input_class_def,
577       (char *) &lookahead_class_def}
578     };
579     return rule_set.apply (LOOKUP_ARGS, context);
580   }
581
582   private:
583   USHORT        format;                 /* Format identifier--format = 2 */
584   OffsetTo<Coverage>
585                 coverage;               /* Offset to Coverage table--from
586                                          * beginning of table */
587   OffsetTo<ClassDef>
588                 backtrackClassDef;      /* Offset to glyph ClassDef table
589                                          * containing backtrack sequence
590                                          * data--from beginning of table */
591   OffsetTo<ClassDef>
592                 inputClassDef;          /* Offset to glyph ClassDef
593                                          * table containing input sequence
594                                          * data--from beginning of table */
595   OffsetTo<ClassDef>
596                 lookaheadClassDef;      /* Offset to glyph ClassDef table
597                                          * containing lookahead sequence
598                                          * data--from beginning of table */
599   OffsetArrayOf<ChainRuleSet>
600                 ruleSet;                /* Array of ChainRuleSet tables
601                                          * ordered by class */
602 };
603 ASSERT_SIZE (ChainContextFormat2, 12);
604
605 struct ChainContextFormat3 {
606
607   friend struct ChainContext;
608
609   private:
610
611   inline bool apply_coverage (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
612   }
613
614   inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
615
616     const OffsetArrayOf<Coverage> &input = * (const OffsetArrayOf<Coverage> *)
617                                              ((const char *) &backtrack + backtrack.get_size ());
618
619     unsigned int index = (this+input[0]) (IN_CURGLYPH ());
620     if (G_LIKELY (index == NOT_COVERED))
621       return false;
622
623     const OffsetArrayOf<Coverage> &lookahead = * (const OffsetArrayOf<Coverage> *)
624                                          ((const char *) &input + input.get_size ());
625     const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
626                                             ((const char *) &lookahead + lookahead.get_size ());
627     struct ChainContextLookupContext context = {
628       {match_coverage, apply_func},
629       {(char *) this, (char *) this, (char *) this}
630     };
631     return chain_context_lookup (LOOKUP_ARGS,
632                                  backtrack.len,
633                                  (USHORT *) backtrack.array,
634                                  input.len,
635                                  (USHORT *) input.array,
636                                  lookahead.len,
637                                  (USHORT *) lookahead.array,
638                                  lookup.len,
639                                  lookup.array,
640                                  context);
641     return false;
642   }
643
644   private:
645   USHORT        format;                 /* Format identifier--format = 3 */
646   OffsetArrayOf<Coverage>
647                 backtrack;              /* Array of coverage tables
648                                          * in backtracking sequence, in  glyph
649                                          * sequence order */
650   OffsetArrayOf<Coverage>
651                 inputX          ;       /* Array of coverage
652                                          * tables in input sequence, in glyph
653                                          * sequence order */
654   OffsetArrayOf<Coverage>
655                 lookaheadX;             /* Array of coverage tables
656                                          * in lookahead sequence, in glyph
657                                          * sequence order */
658   ArrayOf<LookupRecord>
659                 lookupX;                /* Array of LookupRecords--in
660                                          * design order) */
661 };
662 ASSERT_SIZE (ChainContextFormat3, 10);
663
664 struct ChainContext {
665
666   protected:
667   bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
668     switch (u.format) {
669     case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
670     case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
671     case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
672     default:return false;
673     }
674   }
675
676   private:
677   union {
678   USHORT                format; /* Format identifier */
679   ChainContextFormat1   format1[];
680   ChainContextFormat2   format2[];
681   ChainContextFormat3   format3[];
682   } u;
683 };
684 ASSERT_SIZE (ChainContext, 2);
685
686
687 /*
688  * GSUB/GPOS Common
689  */
690
691 struct GSUBGPOS {
692   static const hb_tag_t GSUBTag         = HB_TAG ('G','S','U','B');
693   static const hb_tag_t GPOSTag         = HB_TAG ('G','P','O','S');
694
695   STATIC_DEFINE_GET_FOR_DATA (GSUBGPOS);
696   /* XXX check version here? */
697
698   DEFINE_TAG_LIST_INTERFACE (Script,  script ); /* get_script_count (), get_script (i), get_script_tag (i) */
699   DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
700   DEFINE_LIST_INTERFACE     (Lookup,  lookup ); /* get_lookup_count (), get_lookup (i) */
701
702   // LONGTERMTODO bsearch
703   DEFINE_TAG_FIND_INTERFACE (Script,  script ); /* find_script_index (), get_script_by_tag (tag) */
704   DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
705
706   private:
707   Fixed_Version version;        /* Version of the GSUB/GPOS table--initially set
708                                  * to 0x00010000 */
709   OffsetTo<ScriptList>
710                 scriptList;     /* ScriptList table */
711   OffsetTo<FeatureList>
712                 featureList;    /* FeatureList table */
713   OffsetTo<LookupList>
714                 lookupList;     /* LookupList table */
715 };
716 ASSERT_SIZE (GSUBGPOS, 10);
717
718
719 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */