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