Further cleanup of sizeof
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gsub-private.hh
1 /*
2  * Copyright (C) 2007,2008,2009,2010  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_GSUB_PRIVATE_HH
28 #define HB_OT_LAYOUT_GSUB_PRIVATE_HH
29
30 #include "hb-ot-layout-gsubgpos-private.hh"
31
32
33 #undef BUFFER
34 #define BUFFER context->buffer
35
36
37 struct SingleSubstFormat1
38 {
39   friend struct SingleSubst;
40
41   private:
42
43   inline bool apply (hb_apply_context_t *context) const
44   {
45     TRACE_APPLY ();
46     hb_codepoint_t glyph_id = IN_CURGLYPH ();
47     unsigned int index = (this+coverage) (glyph_id);
48     if (likely (index == NOT_COVERED))
49       return false;
50
51     glyph_id += deltaGlyphID;
52     _hb_buffer_replace_glyph (context->buffer, glyph_id);
53
54     /* We inherit the old glyph class to the substituted glyph */
55     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
56       _hb_ot_layout_set_glyph_property (context->layout->face, glyph_id, context->property);
57
58     return true;
59   }
60
61   inline bool sanitize (hb_sanitize_context_t *context) {
62     TRACE_SANITIZE ();
63     return coverage.sanitize (context, this)
64         && deltaGlyphID.sanitize (context);
65   }
66
67   private:
68   USHORT        format;                 /* Format identifier--format = 1 */
69   OffsetTo<Coverage>
70                 coverage;               /* Offset to Coverage table--from
71                                          * beginning of Substitution table */
72   SHORT         deltaGlyphID;           /* Add to original GlyphID to get
73                                          * substitute GlyphID */
74   public:
75   DEFINE_SIZE_STATIC (6);
76 };
77
78 struct SingleSubstFormat2
79 {
80   friend struct SingleSubst;
81
82   private:
83
84   inline bool apply (hb_apply_context_t *context) const
85   {
86     TRACE_APPLY ();
87     hb_codepoint_t glyph_id = IN_CURGLYPH ();
88     unsigned int index = (this+coverage) (glyph_id);
89     if (likely (index == NOT_COVERED))
90       return false;
91
92     if (unlikely (index >= substitute.len))
93       return false;
94
95     glyph_id = substitute[index];
96     _hb_buffer_replace_glyph (context->buffer, glyph_id);
97
98     /* We inherit the old glyph class to the substituted glyph */
99     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
100       _hb_ot_layout_set_glyph_property (context->layout->face, glyph_id, context->property);
101
102     return true;
103   }
104
105   inline bool sanitize (hb_sanitize_context_t *context) {
106     TRACE_SANITIZE ();
107     return coverage.sanitize (context, this)
108         && substitute.sanitize (context);
109   }
110
111   private:
112   USHORT        format;                 /* Format identifier--format = 2 */
113   OffsetTo<Coverage>
114                 coverage;               /* Offset to Coverage table--from
115                                          * beginning of Substitution table */
116   ArrayOf<GlyphID>
117                 substitute;             /* Array of substitute
118                                          * GlyphIDs--ordered by Coverage Index */
119   public:
120   DEFINE_SIZE_VAR (6, GlyphID);
121 };
122
123 struct SingleSubst
124 {
125   friend struct SubstLookupSubTable;
126
127   private:
128
129   inline bool apply (hb_apply_context_t *context) const
130   {
131     TRACE_APPLY ();
132     switch (u.format) {
133     case 1: return u.format1->apply (context);
134     case 2: return u.format2->apply (context);
135     default:return false;
136     }
137   }
138
139   inline bool sanitize (hb_sanitize_context_t *context) {
140     TRACE_SANITIZE ();
141     if (!u.format.sanitize (context)) return false;
142     switch (u.format) {
143     case 1: return u.format1->sanitize (context);
144     case 2: return u.format2->sanitize (context);
145     default:return true;
146     }
147   }
148
149   private:
150   union {
151   USHORT                format;         /* Format identifier */
152   SingleSubstFormat1    format1[VAR];
153   SingleSubstFormat2    format2[VAR];
154   } u;
155 };
156
157
158 struct Sequence
159 {
160   friend struct MultipleSubstFormat1;
161
162   private:
163   inline bool apply (hb_apply_context_t *context) const
164   {
165     TRACE_APPLY ();
166     if (unlikely (!substitute.len))
167       return false;
168
169     _hb_buffer_add_output_glyphs_be16 (context->buffer, 1,
170                                        substitute.len, (const uint16_t *) substitute.array(),
171                                        0xFFFF, 0xFFFF);
172
173     /* This is a guess only ... */
174     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
175     {
176       unsigned int property = context->property;
177       if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
178         property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
179
180       unsigned int count = substitute.len;
181       for (unsigned int n = 0; n < count; n++)
182         _hb_ot_layout_set_glyph_property (context->layout->face, substitute[n], property);
183     }
184
185     return true;
186   }
187
188   public:
189   inline bool sanitize (hb_sanitize_context_t *context) {
190     TRACE_SANITIZE ();
191     return substitute.sanitize (context);
192   }
193
194   private:
195   ArrayOf<GlyphID>
196                 substitute;             /* String of GlyphIDs to substitute */
197   public:
198   DEFINE_SIZE_VAR (2, GlyphID);
199 };
200
201 struct MultipleSubstFormat1
202 {
203   friend struct MultipleSubst;
204
205   private:
206
207   inline bool apply (hb_apply_context_t *context) const
208   {
209     TRACE_APPLY ();
210
211     unsigned int index = (this+coverage) (IN_CURGLYPH ());
212     if (likely (index == NOT_COVERED))
213       return false;
214
215     return (this+sequence[index]).apply (context);
216   }
217
218   inline bool sanitize (hb_sanitize_context_t *context) {
219     TRACE_SANITIZE ();
220     return coverage.sanitize (context, this)
221         && sequence.sanitize (context, this);
222   }
223
224   private:
225   USHORT        format;                 /* Format identifier--format = 1 */
226   OffsetTo<Coverage>
227                 coverage;               /* Offset to Coverage table--from
228                                          * beginning of Substitution table */
229   OffsetArrayOf<Sequence>
230                 sequence;               /* Array of Sequence tables
231                                          * ordered by Coverage Index */
232   public:
233   DEFINE_SIZE_VAR (6, OffsetTo<Sequence>);
234 };
235
236 struct MultipleSubst
237 {
238   friend struct SubstLookupSubTable;
239
240   private:
241
242   inline bool apply (hb_apply_context_t *context) const
243   {
244     TRACE_APPLY ();
245     switch (u.format) {
246     case 1: return u.format1->apply (context);
247     default:return false;
248     }
249   }
250
251   inline bool sanitize (hb_sanitize_context_t *context) {
252     TRACE_SANITIZE ();
253     if (!u.format.sanitize (context)) return false;
254     switch (u.format) {
255     case 1: return u.format1->sanitize (context);
256     default:return true;
257     }
258   }
259
260   private:
261   union {
262   USHORT                format;         /* Format identifier */
263   MultipleSubstFormat1  format1[VAR];
264   } u;
265 };
266
267
268 typedef ArrayOf<GlyphID> AlternateSet;  /* Array of alternate GlyphIDs--in
269                                          * arbitrary order */
270
271 struct AlternateSubstFormat1
272 {
273   friend struct AlternateSubst;
274
275   private:
276
277   inline bool apply (hb_apply_context_t *context) const
278   {
279     TRACE_APPLY ();
280     hb_codepoint_t glyph_id = IN_CURGLYPH ();
281
282     unsigned int index = (this+coverage) (glyph_id);
283     if (likely (index == NOT_COVERED))
284       return false;
285
286     const AlternateSet &alt_set = this+alternateSet[index];
287
288     if (unlikely (!alt_set.len))
289       return false;
290
291     unsigned int alt_index = 0;
292
293     /* XXX callback to user to choose alternate
294     if (context->layout->face->altfunc)
295       alt_index = (context->layout->face->altfunc)(context->layout->layout, context->buffer,
296                                     context->buffer->out_pos, glyph_id,
297                                     alt_set.len, alt_set.array);
298                                    */
299
300     if (unlikely (alt_index >= alt_set.len))
301       return false;
302
303     glyph_id = alt_set[alt_index];
304
305     _hb_buffer_replace_glyph (context->buffer, glyph_id);
306
307     /* We inherit the old glyph class to the substituted glyph */
308     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
309       _hb_ot_layout_set_glyph_property (context->layout->face, glyph_id, context->property);
310
311     return true;
312   }
313
314   inline bool sanitize (hb_sanitize_context_t *context) {
315     TRACE_SANITIZE ();
316     return coverage.sanitize (context, this)
317         && alternateSet.sanitize (context, this);
318   }
319
320   private:
321   USHORT        format;                 /* Format identifier--format = 1 */
322   OffsetTo<Coverage>
323                 coverage;               /* Offset to Coverage table--from
324                                          * beginning of Substitution table */
325   OffsetArrayOf<AlternateSet>
326                 alternateSet;           /* Array of AlternateSet tables
327                                          * ordered by Coverage Index */
328   public:
329   DEFINE_SIZE_VAR (6, OffsetTo<AlternateSet>);
330 };
331
332 struct AlternateSubst
333 {
334   friend struct SubstLookupSubTable;
335
336   private:
337
338   inline bool apply (hb_apply_context_t *context) const
339   {
340     TRACE_APPLY ();
341     switch (u.format) {
342     case 1: return u.format1->apply (context);
343     default:return false;
344     }
345   }
346
347   inline bool sanitize (hb_sanitize_context_t *context) {
348     TRACE_SANITIZE ();
349     if (!u.format.sanitize (context)) return false;
350     switch (u.format) {
351     case 1: return u.format1->sanitize (context);
352     default:return true;
353     }
354   }
355
356   private:
357   union {
358   USHORT                format;         /* Format identifier */
359   AlternateSubstFormat1 format1[VAR];
360   } u;
361 };
362
363
364 struct Ligature
365 {
366   friend struct LigatureSet;
367
368   private:
369   inline bool apply (hb_apply_context_t *context, bool is_mark) const
370   {
371     TRACE_APPLY ();
372     unsigned int i, j;
373     unsigned int count = component.len;
374     unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context->context_length);
375     if (unlikely (context->buffer->in_pos + count > end))
376       return false;
377
378     for (i = 1, j = context->buffer->in_pos + 1; i < count; i++, j++)
379     {
380       unsigned int property;
381       while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, &property))
382       {
383         if (unlikely (j + count - i == end))
384           return false;
385         j++;
386       }
387
388       if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
389         is_mark = false;
390
391       if (likely (IN_GLYPH (j) != component[i]))
392         return false;
393     }
394     /* This is just a guess ... */
395     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
396       _hb_ot_layout_set_glyph_class (context->layout->face, ligGlyph,
397                                      is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
398                                              : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
399
400     if (j == context->buffer->in_pos + i) /* No input glyphs skipped */
401       /* We don't use a new ligature ID if there are no skipped
402          glyphs and the ligature already has an ID. */
403       _hb_buffer_add_output_glyphs_be16 (context->buffer, i,
404                                          1, (const uint16_t *) &ligGlyph,
405                                          0,
406                                          IN_LIGID (context->buffer->in_pos) && !IN_COMPONENT (context->buffer->in_pos) ?
407                                          0xFFFF : _hb_buffer_allocate_lig_id (context->buffer));
408     else
409     {
410       unsigned int lig_id = _hb_buffer_allocate_lig_id (context->buffer);
411       _hb_buffer_add_output_glyph (context->buffer, ligGlyph, 0xFFFF, lig_id);
412
413       /* Now we must do a second loop to copy the skipped glyphs to
414          `out' and assign component values to it.  We start with the
415          glyph after the first component.  Glyphs between component
416          i and i+1 belong to component i.  Together with the lig_id
417          value it is later possible to check whether a specific
418          component value really belongs to a given ligature. */
419
420       for ( i = 1; i < count; i++ )
421       {
422         while (_hb_ot_layout_skip_mark (context->layout->face, IN_CURINFO (), context->lookup_flag, NULL))
423           _hb_buffer_add_output_glyph (context->buffer, IN_CURGLYPH (), i, lig_id);
424
425         (context->buffer->in_pos)++;
426       }
427     }
428
429     return true;
430   }
431
432   public:
433   inline bool sanitize (hb_sanitize_context_t *context) {
434     TRACE_SANITIZE ();
435     return ligGlyph.sanitize (context)
436         && component.sanitize (context);
437   }
438
439   private:
440   GlyphID       ligGlyph;               /* GlyphID of ligature to substitute */
441   HeadlessArrayOf<GlyphID>
442                 component;              /* Array of component GlyphIDs--start
443                                          * with the second  component--ordered
444                                          * in writing direction */
445   public:
446   DEFINE_SIZE_VAR (4, GlyphID);
447 };
448
449 struct LigatureSet
450 {
451   friend struct LigatureSubstFormat1;
452
453   private:
454   inline bool apply (hb_apply_context_t *context, bool is_mark) const
455   {
456     TRACE_APPLY ();
457     unsigned int num_ligs = ligature.len;
458     for (unsigned int i = 0; i < num_ligs; i++)
459     {
460       const Ligature &lig = this+ligature[i];
461       if (lig.apply (context, is_mark))
462         return true;
463     }
464
465     return false;
466   }
467
468   public:
469   inline bool sanitize (hb_sanitize_context_t *context) {
470     TRACE_SANITIZE ();
471     return ligature.sanitize (context, this);
472   }
473
474   private:
475   OffsetArrayOf<Ligature>
476                 ligature;               /* Array LigatureSet tables
477                                          * ordered by preference */
478   public:
479   DEFINE_SIZE_VAR (2, OffsetTo<Ligature>);
480 };
481
482 struct LigatureSubstFormat1
483 {
484   friend struct LigatureSubst;
485
486   private:
487   inline bool apply (hb_apply_context_t *context) const
488   {
489     TRACE_APPLY ();
490     hb_codepoint_t glyph_id = IN_CURGLYPH ();
491
492     bool first_is_mark = !!(context->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
493
494     unsigned int index = (this+coverage) (glyph_id);
495     if (likely (index == NOT_COVERED))
496       return false;
497
498     const LigatureSet &lig_set = this+ligatureSet[index];
499     return lig_set.apply (context, first_is_mark);
500   }
501
502   inline bool sanitize (hb_sanitize_context_t *context) {
503     TRACE_SANITIZE ();
504     return coverage.sanitize (context, this)
505         && ligatureSet.sanitize (context, this);
506   }
507
508   private:
509   USHORT        format;                 /* Format identifier--format = 1 */
510   OffsetTo<Coverage>
511                 coverage;               /* Offset to Coverage table--from
512                                          * beginning of Substitution table */
513   OffsetArrayOf<LigatureSet>
514                 ligatureSet;            /* Array LigatureSet tables
515                                          * ordered by Coverage Index */
516   public:
517   DEFINE_SIZE_VAR (6, OffsetTo<LigatureSet>);
518 };
519
520 struct LigatureSubst
521 {
522   friend struct SubstLookupSubTable;
523
524   private:
525   inline bool apply (hb_apply_context_t *context) const
526   {
527     TRACE_APPLY ();
528     switch (u.format) {
529     case 1: return u.format1->apply (context);
530     default:return false;
531     }
532   }
533
534   inline bool sanitize (hb_sanitize_context_t *context) {
535     TRACE_SANITIZE ();
536     if (!u.format.sanitize (context)) return false;
537     switch (u.format) {
538     case 1: return u.format1->sanitize (context);
539     default:return true;
540     }
541   }
542
543   private:
544   union {
545   USHORT                format;         /* Format identifier */
546   LigatureSubstFormat1  format1[VAR];
547   } u;
548 };
549
550
551
552 static inline bool substitute_lookup (hb_apply_context_t *context, unsigned int lookup_index);
553
554 struct ContextSubst : Context
555 {
556   friend struct SubstLookupSubTable;
557
558   private:
559   inline bool apply (hb_apply_context_t *context) const
560   {
561     TRACE_APPLY ();
562     return Context::apply (context, substitute_lookup);
563   }
564 };
565
566 struct ChainContextSubst : ChainContext
567 {
568   friend struct SubstLookupSubTable;
569
570   private:
571   inline bool apply (hb_apply_context_t *context) const
572   {
573     TRACE_APPLY ();
574     return ChainContext::apply (context, substitute_lookup);
575   }
576 };
577
578
579 struct ExtensionSubst : Extension
580 {
581   friend struct SubstLookupSubTable;
582   friend struct SubstLookup;
583
584   private:
585   inline const struct SubstLookupSubTable& get_subtable (void) const
586   {
587     unsigned int offset = get_offset ();
588     if (unlikely (!offset)) return Null(SubstLookupSubTable);
589     return StructAtOffset<SubstLookupSubTable> (this, offset);
590   }
591
592   inline bool apply (hb_apply_context_t *context) const;
593
594   inline bool sanitize (hb_sanitize_context_t *context);
595
596   inline bool is_reverse (void) const;
597 };
598
599
600 struct ReverseChainSingleSubstFormat1
601 {
602   friend struct ReverseChainSingleSubst;
603
604   private:
605   inline bool apply (hb_apply_context_t *context) const
606   {
607     TRACE_APPLY ();
608     if (unlikely (context->context_length != NO_CONTEXT))
609       return false; /* No chaining to this type */
610
611     unsigned int index = (this+coverage) (IN_CURGLYPH ());
612     if (likely (index == NOT_COVERED))
613       return false;
614
615     const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
616     const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
617
618     if (match_backtrack (context,
619                          backtrack.len, (USHORT *) backtrack.array(),
620                          match_coverage, this) &&
621         match_lookahead (context,
622                          lookahead.len, (USHORT *) lookahead.array(),
623                          match_coverage, this,
624                          1))
625     {
626       IN_CURGLYPH () = substitute[index];
627       context->buffer->in_pos--; /* Reverse! */
628       return true;
629     }
630
631     return false;
632   }
633
634   inline bool sanitize (hb_sanitize_context_t *context) {
635     TRACE_SANITIZE ();
636     if (!(coverage.sanitize (context, this)
637        && backtrack.sanitize (context, this)))
638       return false;
639     OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
640     if (!lookahead.sanitize (context, this))
641       return false;
642     ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
643     return substitute.sanitize (context);
644   }
645
646   private:
647   USHORT        format;                 /* Format identifier--format = 1 */
648   OffsetTo<Coverage>
649                 coverage;               /* Offset to Coverage table--from
650                                          * beginning of table */
651   OffsetArrayOf<Coverage>
652                 backtrack;              /* Array of coverage tables
653                                          * in backtracking sequence, in  glyph
654                                          * sequence order */
655   OffsetArrayOf<Coverage>
656                 lookaheadX;             /* Array of coverage tables
657                                          * in lookahead sequence, in glyph
658                                          * sequence order */
659   ArrayOf<GlyphID>
660                 substituteX;            /* Array of substitute
661                                          * GlyphIDs--ordered by Coverage Index */
662   public:
663   DEFINE_SIZE_MIN (10);
664 };
665
666 struct ReverseChainSingleSubst
667 {
668   friend struct SubstLookupSubTable;
669
670   private:
671   inline bool apply (hb_apply_context_t *context) const
672   {
673     TRACE_APPLY ();
674     switch (u.format) {
675     case 1: return u.format1->apply (context);
676     default:return false;
677     }
678   }
679
680   inline bool sanitize (hb_sanitize_context_t *context) {
681     TRACE_SANITIZE ();
682     if (!u.format.sanitize (context)) return false;
683     switch (u.format) {
684     case 1: return u.format1->sanitize (context);
685     default:return true;
686     }
687   }
688
689   private:
690   union {
691   USHORT                                format;         /* Format identifier */
692   ReverseChainSingleSubstFormat1        format1[VAR];
693   } u;
694 };
695
696
697
698 /*
699  * SubstLookup
700  */
701
702 struct SubstLookupSubTable
703 {
704   friend struct SubstLookup;
705
706   enum {
707     Single              = 1,
708     Multiple            = 2,
709     Alternate           = 3,
710     Ligature            = 4,
711     Context             = 5,
712     ChainContext        = 6,
713     Extension           = 7,
714     ReverseChainSingle  = 8
715   };
716
717   inline bool apply (hb_apply_context_t *context, unsigned int lookup_type) const
718   {
719     TRACE_APPLY ();
720     switch (lookup_type) {
721     case Single:                return u.single->apply (context);
722     case Multiple:              return u.multiple->apply (context);
723     case Alternate:             return u.alternate->apply (context);
724     case Ligature:              return u.ligature->apply (context);
725     case Context:               return u.context->apply (context);
726     case ChainContext:          return u.chainContext->apply (context);
727     case Extension:             return u.extension->apply (context);
728     case ReverseChainSingle:    return u.reverseChainContextSingle->apply (context);
729     default:return false;
730     }
731   }
732
733   inline bool sanitize (hb_sanitize_context_t *context) {
734     TRACE_SANITIZE ();
735     if (!u.format.sanitize (context)) return false;
736     switch (u.format) {
737     case Single:                return u.single->sanitize (context);
738     case Multiple:              return u.multiple->sanitize (context);
739     case Alternate:             return u.alternate->sanitize (context);
740     case Ligature:              return u.ligature->sanitize (context);
741     case Context:               return u.context->sanitize (context);
742     case ChainContext:          return u.chainContext->sanitize (context);
743     case Extension:             return u.extension->sanitize (context);
744     case ReverseChainSingle:    return u.reverseChainContextSingle->sanitize (context);
745     default:return true;
746     }
747   }
748
749   private:
750   union {
751   USHORT                        format;
752   SingleSubst                   single[VAR];
753   MultipleSubst                 multiple[VAR];
754   AlternateSubst                alternate[VAR];
755   LigatureSubst                 ligature[VAR];
756   ContextSubst                  context[VAR];
757   ChainContextSubst             chainContext[VAR];
758   ExtensionSubst                extension[VAR];
759   ReverseChainSingleSubst       reverseChainContextSingle[VAR];
760   } u;
761   public:
762   DEFINE_SIZE_MIN (2);
763 };
764
765
766 struct SubstLookup : Lookup
767 {
768   inline const SubstLookupSubTable& get_subtable (unsigned int i) const
769   { return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
770
771   inline static bool lookup_type_is_reverse (unsigned int lookup_type)
772   { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
773
774   inline bool is_reverse (void) const
775   {
776     unsigned int type = get_type ();
777     if (unlikely (type == SubstLookupSubTable::Extension))
778       return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
779     return lookup_type_is_reverse (type);
780   }
781
782
783   inline bool apply_once (hb_ot_layout_context_t *layout,
784                           hb_buffer_t *buffer,
785                           unsigned int context_length,
786                           unsigned int nesting_level_left) const
787   {
788     unsigned int lookup_type = get_type ();
789     hb_apply_context_t context[1] = {{0}};
790
791     context->layout = layout;
792     context->buffer = buffer;
793     context->context_length = context_length;
794     context->nesting_level_left = nesting_level_left;
795     context->lookup_flag = get_flag ();
796
797     if (!_hb_ot_layout_check_glyph_property (context->layout->face, IN_CURINFO (), context->lookup_flag, &context->property))
798       return false;
799
800     if (unlikely (lookup_type == SubstLookupSubTable::Extension))
801     {
802       /* The spec says all subtables should have the same type.
803        * This is specially important if one has a reverse type!
804        *
805        * This is rather slow to do this here for every glyph,
806        * but it's easiest, and who uses extension lookups anyway?!*/
807       unsigned int count = get_subtable_count ();
808       unsigned int type = get_subtable(0).u.extension->get_type ();
809       for (unsigned int i = 1; i < count; i++)
810         if (get_subtable(i).u.extension->get_type () != type)
811           return false;
812     }
813
814     unsigned int count = get_subtable_count ();
815     for (unsigned int i = 0; i < count; i++)
816       if (get_subtable (i).apply (context, lookup_type))
817         return true;
818
819     return false;
820   }
821
822   inline bool apply_string (hb_ot_layout_context_t *layout,
823                             hb_buffer_t *buffer,
824                             hb_mask_t    mask) const
825   {
826 #undef BUFFER
827 #define BUFFER buffer
828     bool ret = false;
829
830     if (unlikely (!buffer->in_length))
831       return false;
832
833     if (likely (!is_reverse ()))
834     {
835         /* in/out forward substitution */
836         _hb_buffer_clear_output (buffer);
837         buffer->in_pos = 0;
838         while (buffer->in_pos < buffer->in_length)
839         {
840           if ((~IN_MASK (buffer->in_pos) & mask) &&
841               apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
842             ret = true;
843           else
844             _hb_buffer_next_glyph (buffer);
845
846         }
847         if (ret)
848           _hb_buffer_swap (buffer);
849     }
850     else
851     {
852         /* in-place backward substitution */
853         buffer->in_pos = buffer->in_length - 1;
854         do
855         {
856           if ((~IN_MASK (buffer->in_pos) & mask) &&
857               apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
858             ret = true;
859           else
860             buffer->in_pos--;
861
862         }
863         while ((int) buffer->in_pos >= 0);
864     }
865
866     return ret;
867   }
868
869   inline bool sanitize (hb_sanitize_context_t *context) {
870     TRACE_SANITIZE ();
871     if (unlikely (!Lookup::sanitize (context))) return false;
872     OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
873     return list.sanitize (context, this);
874   }
875 };
876
877 typedef OffsetListOf<SubstLookup> SubstLookupList;
878
879 /*
880  * GSUB
881  */
882
883 struct GSUB : GSUBGPOS
884 {
885   static const hb_tag_t Tag     = HB_OT_TAG_GSUB;
886
887   inline const SubstLookup& get_lookup (unsigned int i) const
888   { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
889
890   inline bool substitute_lookup (hb_ot_layout_context_t *layout,
891                                  hb_buffer_t  *buffer,
892                                  unsigned int  lookup_index,
893                                  hb_mask_t     mask) const
894   { return get_lookup (lookup_index).apply_string (layout, buffer, mask); }
895
896   inline bool sanitize (hb_sanitize_context_t *context) {
897     TRACE_SANITIZE ();
898     if (unlikely (!GSUBGPOS::sanitize (context))) return false;
899     OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
900     return list.sanitize (context, this);
901   }
902   public:
903   DEFINE_SIZE_STATIC (10);
904 };
905
906
907 /* Out-of-class implementation for methods recursing */
908
909 inline bool ExtensionSubst::apply (hb_apply_context_t *context) const
910 {
911   TRACE_APPLY ();
912   return get_subtable ().apply (context, get_type ());
913 }
914
915 inline bool ExtensionSubst::sanitize (hb_sanitize_context_t *context)
916 {
917   TRACE_SANITIZE ();
918   if (unlikely (!Extension::sanitize (context))) return false;
919   unsigned int offset = get_offset ();
920   if (unlikely (!offset)) return true;
921   return StructAtOffset<SubstLookupSubTable> (this, offset).sanitize (context);
922 }
923
924 inline bool ExtensionSubst::is_reverse (void) const
925 {
926   unsigned int type = get_type ();
927   if (unlikely (type == SubstLookupSubTable::Extension))
928     return CastR<ExtensionSubst> (get_subtable()).is_reverse ();
929   return SubstLookup::lookup_type_is_reverse (type);
930 }
931
932 static inline bool substitute_lookup (hb_apply_context_t *context, unsigned int lookup_index)
933 {
934   const GSUB &gsub = *(context->layout->face->ot_layout.gsub);
935   const SubstLookup &l = gsub.get_lookup (lookup_index);
936
937   if (unlikely (context->nesting_level_left == 0))
938     return false;
939
940   if (unlikely (context->context_length < 1))
941     return false;
942
943   return l.apply_once (context->layout, context->buffer, context->context_length, context->nesting_level_left - 1);
944 }
945
946
947 #endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */