Oops, actually set global mask
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gsub-private.hh
index c562dea..f09b4d2 100644 (file)
@@ -29,9 +29,7 @@
 
 #include "hb-ot-layout-gsubgpos-private.hh"
 
-
-#undef BUFFER
-#define BUFFER context->buffer
+HB_BEGIN_DECLS
 
 
 struct SingleSubstFormat1
@@ -40,28 +38,28 @@ struct SingleSubstFormat1
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    hb_codepoint_t glyph_id = IN_CURGLYPH ();
+    hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
     unsigned int index = (this+coverage) (glyph_id);
     if (likely (index == NOT_COVERED))
       return false;
 
     glyph_id += deltaGlyphID;
-    _hb_buffer_replace_glyph (context->buffer, glyph_id);
+    c->buffer->replace_glyph (glyph_id);
 
     /* We inherit the old glyph class to the substituted glyph */
-    if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
-      _hb_ot_layout_set_glyph_property (context->layout->face, glyph_id, context->property);
+    if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
+      _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
 
     return true;
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return coverage.sanitize (context, this)
-       && deltaGlyphID.sanitize (context);
+    return coverage.sanitize (c, this)
+       && deltaGlyphID.sanitize (c);
   }
 
   private:
@@ -81,10 +79,10 @@ struct SingleSubstFormat2
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    hb_codepoint_t glyph_id = IN_CURGLYPH ();
+    hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
     unsigned int index = (this+coverage) (glyph_id);
     if (likely (index == NOT_COVERED))
       return false;
@@ -93,19 +91,19 @@ struct SingleSubstFormat2
       return false;
 
     glyph_id = substitute[index];
-    _hb_buffer_replace_glyph (context->buffer, glyph_id);
+    c->buffer->replace_glyph (glyph_id);
 
     /* We inherit the old glyph class to the substituted glyph */
-    if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
-      _hb_ot_layout_set_glyph_property (context->layout->face, glyph_id, context->property);
+    if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
+      _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
 
     return true;
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return coverage.sanitize (context, this)
-       && substitute.sanitize (context);
+    return coverage.sanitize (c, this)
+       && substitute.sanitize (c);
   }
 
   private:
@@ -117,7 +115,7 @@ struct SingleSubstFormat2
                substitute;             /* Array of substitute
                                         * GlyphIDs--ordered by Coverage Index */
   public:
-  DEFINE_SIZE_VAR (6, GlyphID);
+  DEFINE_SIZE_ARRAY (6, substitute);
 };
 
 struct SingleSubst
@@ -126,22 +124,22 @@ struct SingleSubst
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (context);
-    case 2: return u.format2->apply (context);
+    case 1: return u.format1.apply (c);
+    case 2: return u.format2.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!u.format.sanitize (context)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (context);
-    case 2: return u.format2->sanitize (context);
+    case 1: return u.format1.sanitize (c);
+    case 2: return u.format2.sanitize (c);
     default:return true;
     }
   }
@@ -149,8 +147,8 @@ struct SingleSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  SingleSubstFormat1   format1[VAR];
-  SingleSubstFormat2   format2[VAR];
+  SingleSubstFormat1   format1;
+  SingleSubstFormat2   format2;
   } u;
 };
 
@@ -160,42 +158,42 @@ struct Sequence
   friend struct MultipleSubstFormat1;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     if (unlikely (!substitute.len))
       return false;
 
-    _hb_buffer_add_output_glyphs_be16 (context->buffer, 1,
-                                      substitute.len, (const uint16_t *) substitute.array(),
+    c->buffer->add_output_glyphs_be16 (1,
+                                      substitute.len, (const uint16_t *) substitute.array,
                                       0xFFFF, 0xFFFF);
 
     /* This is a guess only ... */
-    if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
+    if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
     {
-      unsigned int property = context->property;
+      unsigned int property = c->property;
       if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
         property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
 
       unsigned int count = substitute.len;
       for (unsigned int n = 0; n < count; n++)
-       _hb_ot_layout_set_glyph_property (context->layout->face, substitute[n], property);
+       _hb_ot_layout_set_glyph_property (c->layout->face, substitute[n], property);
     }
 
     return true;
   }
 
   public:
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return substitute.sanitize (context);
+    return substitute.sanitize (c);
   }
 
   private:
   ArrayOf<GlyphID>
                substitute;             /* String of GlyphIDs to substitute */
   public:
-  DEFINE_SIZE_VAR (2, GlyphID);
+  DEFINE_SIZE_ARRAY (2, substitute);
 };
 
 struct MultipleSubstFormat1
@@ -204,21 +202,21 @@ struct MultipleSubstFormat1
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
 
-    unsigned int index = (this+coverage) (IN_CURGLYPH ());
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (index == NOT_COVERED))
       return false;
 
-    return (this+sequence[index]).apply (context);
+    return (this+sequence[index]).apply (c);
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return coverage.sanitize (context, this)
-       && sequence.sanitize (context, this);
+    return coverage.sanitize (c, this)
+       && sequence.sanitize (c, this);
   }
 
   private:
@@ -230,7 +228,7 @@ struct MultipleSubstFormat1
                sequence;               /* Array of Sequence tables
                                         * ordered by Coverage Index */
   public:
-  DEFINE_SIZE_VAR (6, OffsetTo<Sequence>);
+  DEFINE_SIZE_ARRAY (6, sequence);
 };
 
 struct MultipleSubst
@@ -239,20 +237,20 @@ struct MultipleSubst
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (context);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!u.format.sanitize (context)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (context);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -260,7 +258,7 @@ struct MultipleSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  MultipleSubstFormat1 format1[VAR];
+  MultipleSubstFormat1 format1;
   } u;
 };
 
@@ -274,10 +272,12 @@ struct AlternateSubstFormat1
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    hb_codepoint_t glyph_id = IN_CURGLYPH ();
+    hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
+    hb_mask_t glyph_mask = c->buffer->info[c->buffer->i].mask;
+    hb_mask_t lookup_mask = c->lookup_mask;
 
     unsigned int index = (this+coverage) (glyph_id);
     if (likely (index == NOT_COVERED))
@@ -288,33 +288,28 @@ struct AlternateSubstFormat1
     if (unlikely (!alt_set.len))
       return false;
 
-    unsigned int alt_index = 0;
-
-    /* XXX callback to user to choose alternate
-    if (context->layout->face->altfunc)
-      alt_index = (context->layout->face->altfunc)(context->layout->layout, context->buffer,
-                                   context->buffer->out_pos, glyph_id,
-                                   alt_set.len, alt_set.array);
-                                  */
+    /* Note: This breaks badly if two features enabled this lookup together. */
+    unsigned int shift = _hb_ctz (lookup_mask);
+    unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
 
-    if (unlikely (alt_index >= alt_set.len))
+    if (unlikely (alt_index > alt_set.len || alt_index == 0))
       return false;
 
-    glyph_id = alt_set[alt_index];
+    glyph_id = alt_set[alt_index - 1];
 
-    _hb_buffer_replace_glyph (context->buffer, glyph_id);
+    c->buffer->replace_glyph (glyph_id);
 
     /* We inherit the old glyph class to the substituted glyph */
-    if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
-      _hb_ot_layout_set_glyph_property (context->layout->face, glyph_id, context->property);
+    if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
+      _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
 
     return true;
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return coverage.sanitize (context, this)
-       && alternateSet.sanitize (context, this);
+    return coverage.sanitize (c, this)
+       && alternateSet.sanitize (c, this);
   }
 
   private:
@@ -326,7 +321,7 @@ struct AlternateSubstFormat1
                alternateSet;           /* Array of AlternateSet tables
                                         * ordered by Coverage Index */
   public:
-  DEFINE_SIZE_VAR (6, OffsetTo<AlternateSet>);
+  DEFINE_SIZE_ARRAY (6, alternateSet);
 };
 
 struct AlternateSubst
@@ -335,20 +330,20 @@ struct AlternateSubst
 
   private:
 
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (context);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!u.format.sanitize (context)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (context);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -356,7 +351,7 @@ struct AlternateSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  AlternateSubstFormat1        format1[VAR];
+  AlternateSubstFormat1        format1;
   } u;
 };
 
@@ -366,19 +361,19 @@ struct Ligature
   friend struct LigatureSet;
 
   private:
-  inline bool apply (hb_apply_context_t *context, bool is_mark) const
+  inline bool apply (hb_apply_context_t *c, bool is_mark) const
   {
     TRACE_APPLY ();
     unsigned int i, j;
     unsigned int count = component.len;
-    unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context->context_length);
-    if (unlikely (context->buffer->in_pos + count > end))
+    unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
+    if (unlikely (c->buffer->i + count > end))
       return false;
 
-    for (i = 1, j = context->buffer->in_pos + 1; i < count; i++, j++)
+    for (i = 1, j = c->buffer->i + 1; i < count; i++, j++)
     {
       unsigned int property;
-      while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, &property))
+      while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_flag, &property))
       {
        if (unlikely (j + count - i == end))
          return false;
@@ -388,27 +383,27 @@ struct Ligature
       if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
        is_mark = false;
 
-      if (likely (IN_GLYPH (j) != component[i]))
+      if (likely (c->buffer->info[j].codepoint != component[i]))
         return false;
     }
     /* This is just a guess ... */
-    if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
-      _hb_ot_layout_set_glyph_class (context->layout->face, ligGlyph,
+    if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
+      _hb_ot_layout_set_glyph_class (c->layout->face, ligGlyph,
                                     is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
                                             : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
 
-    if (j == context->buffer->in_pos + i) /* No input glyphs skipped */
+    if (j == c->buffer->i + i) /* No input glyphs skipped */
       /* We don't use a new ligature ID if there are no skipped
         glyphs and the ligature already has an ID. */
-      _hb_buffer_add_output_glyphs_be16 (context->buffer, i,
+      c->buffer->add_output_glyphs_be16 (i,
                                         1, (const uint16_t *) &ligGlyph,
                                         0,
-                                        IN_LIGID (context->buffer->in_pos) && !IN_COMPONENT (context->buffer->in_pos) ?
-                                        0xFFFF : _hb_buffer_allocate_lig_id (context->buffer));
+                                        c->buffer->info[c->buffer->i].lig_id && !c->buffer->info[c->buffer->i].component ?
+                                        0xFFFF : c->buffer->allocate_lig_id ());
     else
     {
-      unsigned int lig_id = _hb_buffer_allocate_lig_id (context->buffer);
-      _hb_buffer_add_output_glyph (context->buffer, ligGlyph, 0xFFFF, lig_id);
+      unsigned int lig_id = c->buffer->allocate_lig_id ();
+      c->buffer->add_output_glyph (ligGlyph, 0xFFFF, lig_id);
 
       /* Now we must do a second loop to copy the skipped glyphs to
         `out' and assign component values to it.  We start with the
@@ -419,10 +414,10 @@ struct Ligature
 
       for ( i = 1; i < count; i++ )
       {
-       while (_hb_ot_layout_skip_mark (context->layout->face, IN_CURINFO (), context->lookup_flag, NULL))
-         _hb_buffer_add_output_glyph (context->buffer, IN_CURGLYPH (), i, lig_id);
+       while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[c->buffer->i], c->lookup_flag, NULL))
+         c->buffer->add_output_glyph (c->buffer->info[c->buffer->i].codepoint, i, lig_id);
 
-       (context->buffer->in_pos)++;
+       (c->buffer->i)++;
       }
     }
 
@@ -430,10 +425,10 @@ struct Ligature
   }
 
   public:
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return ligGlyph.sanitize (context)
-        && component.sanitize (context);
+    return ligGlyph.sanitize (c)
+        && component.sanitize (c);
   }
 
   private:
@@ -443,7 +438,7 @@ struct Ligature
                                         * with the second  component--ordered
                                         * in writing direction */
   public:
-  DEFINE_SIZE_VAR (4, GlyphID);
+  DEFINE_SIZE_ARRAY (4, component);
 };
 
 struct LigatureSet
@@ -451,14 +446,14 @@ struct LigatureSet
   friend struct LigatureSubstFormat1;
 
   private:
-  inline bool apply (hb_apply_context_t *context, bool is_mark) const
+  inline bool apply (hb_apply_context_t *c, bool is_mark) const
   {
     TRACE_APPLY ();
     unsigned int num_ligs = ligature.len;
     for (unsigned int i = 0; i < num_ligs; i++)
     {
       const Ligature &lig = this+ligature[i];
-      if (lig.apply (context, is_mark))
+      if (lig.apply (c, is_mark))
         return true;
     }
 
@@ -466,9 +461,9 @@ struct LigatureSet
   }
 
   public:
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return ligature.sanitize (context, this);
+    return ligature.sanitize (c, this);
   }
 
   private:
@@ -476,7 +471,7 @@ struct LigatureSet
                ligature;               /* Array LigatureSet tables
                                         * ordered by preference */
   public:
-  DEFINE_SIZE_VAR (2, OffsetTo<Ligature>);
+  DEFINE_SIZE_ARRAY (2, ligature);
 };
 
 struct LigatureSubstFormat1
@@ -484,25 +479,25 @@ struct LigatureSubstFormat1
   friend struct LigatureSubst;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    hb_codepoint_t glyph_id = IN_CURGLYPH ();
+    hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
 
-    bool first_is_mark = !!(context->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
+    bool first_is_mark = !!(c->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
 
     unsigned int index = (this+coverage) (glyph_id);
     if (likely (index == NOT_COVERED))
       return false;
 
     const LigatureSet &lig_set = this+ligatureSet[index];
-    return lig_set.apply (context, first_is_mark);
+    return lig_set.apply (c, first_is_mark);
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return coverage.sanitize (context, this)
-       && ligatureSet.sanitize (context, this);
+    return coverage.sanitize (c, this)
+       && ligatureSet.sanitize (c, this);
   }
 
   private:
@@ -514,7 +509,7 @@ struct LigatureSubstFormat1
                ligatureSet;            /* Array LigatureSet tables
                                         * ordered by Coverage Index */
   public:
-  DEFINE_SIZE_VAR (6, OffsetTo<LigatureSet>);
+  DEFINE_SIZE_ARRAY (6, ligatureSet);
 };
 
 struct LigatureSubst
@@ -522,20 +517,20 @@ struct LigatureSubst
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (context);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!u.format.sanitize (context)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (context);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -543,23 +538,24 @@ struct LigatureSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  LigatureSubstFormat1 format1[VAR];
+  LigatureSubstFormat1 format1;
   } u;
 };
 
 
-
-static inline bool substitute_lookup (hb_apply_context_t *context, unsigned int lookup_index);
+HB_BEGIN_DECLS
+static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index);
+HB_END_DECLS
 
 struct ContextSubst : Context
 {
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    return Context::apply (context, substitute_lookup);
+    return Context::apply (c, substitute_lookup);
   }
 };
 
@@ -568,10 +564,10 @@ struct ChainContextSubst : ChainContext
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    return ChainContext::apply (context, substitute_lookup);
+    return ChainContext::apply (c, substitute_lookup);
   }
 };
 
@@ -589,9 +585,9 @@ struct ExtensionSubst : Extension
     return StructAtOffset<SubstLookupSubTable> (this, offset);
   }
 
-  inline bool apply (hb_apply_context_t *context) const;
+  inline bool apply (hb_apply_context_t *c) const;
 
-  inline bool sanitize (hb_sanitize_context_t *context);
+  inline bool sanitize (hb_sanitize_context_t *c);
 
   inline bool is_reverse (void) const;
 };
@@ -602,45 +598,45 @@ struct ReverseChainSingleSubstFormat1
   friend struct ReverseChainSingleSubst;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    if (unlikely (context->context_length != NO_CONTEXT))
+    if (unlikely (c->context_length != NO_CONTEXT))
       return false; /* No chaining to this type */
 
-    unsigned int index = (this+coverage) (IN_CURGLYPH ());
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (index == NOT_COVERED))
       return false;
 
     const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
     const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
 
-    if (match_backtrack (context,
-                        backtrack.len, (USHORT *) backtrack.array(),
+    if (match_backtrack (c,
+                        backtrack.len, (USHORT *) backtrack.array,
                         match_coverage, this) &&
-        match_lookahead (context,
-                        lookahead.len, (USHORT *) lookahead.array(),
+        match_lookahead (c,
+                        lookahead.len, (USHORT *) lookahead.array,
                         match_coverage, this,
                         1))
     {
-      IN_CURGLYPH () = substitute[index];
-      context->buffer->in_pos--; /* Reverse! */
+      c->buffer->info[c->buffer->i].codepoint = substitute[index];
+      c->buffer->i--; /* Reverse! */
       return true;
     }
 
     return false;
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!(coverage.sanitize (context, this)
-       && backtrack.sanitize (context, this)))
+    if (!(coverage.sanitize (c, this)
+       && backtrack.sanitize (c, this)))
       return false;
     OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
-    if (!lookahead.sanitize (context, this))
+    if (!lookahead.sanitize (c, this))
       return false;
     ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
-    return substitute.sanitize (context);
+    return substitute.sanitize (c);
   }
 
   private:
@@ -668,20 +664,20 @@ struct ReverseChainSingleSubst
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (hb_apply_context_t *context) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (context);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!u.format.sanitize (context)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (context);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -689,7 +685,7 @@ struct ReverseChainSingleSubst
   private:
   union {
   USHORT                               format;         /* Format identifier */
-  ReverseChainSingleSubstFormat1       format1[VAR];
+  ReverseChainSingleSubstFormat1       format1;
   } u;
 };
 
@@ -714,52 +710,51 @@ struct SubstLookupSubTable
     ReverseChainSingle = 8
   };
 
-  inline bool apply (hb_apply_context_t *context, unsigned int lookup_type) const
+  inline bool apply (hb_apply_context_t *c, unsigned int lookup_type) const
   {
     TRACE_APPLY ();
     switch (lookup_type) {
-    case Single:               return u.single->apply (context);
-    case Multiple:             return u.multiple->apply (context);
-    case Alternate:            return u.alternate->apply (context);
-    case Ligature:             return u.ligature->apply (context);
-    case Context:              return u.context->apply (context);
-    case ChainContext:         return u.chainContext->apply (context);
-    case Extension:            return u.extension->apply (context);
-    case ReverseChainSingle:   return u.reverseChainContextSingle->apply (context);
+    case Single:               return u.single.apply (c);
+    case Multiple:             return u.multiple.apply (c);
+    case Alternate:            return u.alternate.apply (c);
+    case Ligature:             return u.ligature.apply (c);
+    case Context:              return u.c.apply (c);
+    case ChainContext:         return u.chainContext.apply (c);
+    case Extension:            return u.extension.apply (c);
+    case ReverseChainSingle:   return u.reverseChainContextSingle.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
     TRACE_SANITIZE ();
-    if (!u.format.sanitize (context)) return false;
-    switch (u.format) {
-    case Single:               return u.single->sanitize (context);
-    case Multiple:             return u.multiple->sanitize (context);
-    case Alternate:            return u.alternate->sanitize (context);
-    case Ligature:             return u.ligature->sanitize (context);
-    case Context:              return u.context->sanitize (context);
-    case ChainContext:         return u.chainContext->sanitize (context);
-    case Extension:            return u.extension->sanitize (context);
-    case ReverseChainSingle:   return u.reverseChainContextSingle->sanitize (context);
+    switch (lookup_type) {
+    case Single:               return u.single.sanitize (c);
+    case Multiple:             return u.multiple.sanitize (c);
+    case Alternate:            return u.alternate.sanitize (c);
+    case Ligature:             return u.ligature.sanitize (c);
+    case Context:              return u.c.sanitize (c);
+    case ChainContext:         return u.chainContext.sanitize (c);
+    case Extension:            return u.extension.sanitize (c);
+    case ReverseChainSingle:   return u.reverseChainContextSingle.sanitize (c);
     default:return true;
     }
   }
 
   private:
   union {
-  USHORT                       format;
-  SingleSubst                  single[VAR];
-  MultipleSubst                        multiple[VAR];
-  AlternateSubst               alternate[VAR];
-  LigatureSubst                        ligature[VAR];
-  ContextSubst                 context[VAR];
-  ChainContextSubst            chainContext[VAR];
-  ExtensionSubst               extension[VAR];
-  ReverseChainSingleSubst      reverseChainContextSingle[VAR];
+  USHORT                       sub_format;
+  SingleSubst                  single;
+  MultipleSubst                        multiple;
+  AlternateSubst               alternate;
+  LigatureSubst                        ligature;
+  ContextSubst                 c;
+  ChainContextSubst            chainContext;
+  ExtensionSubst               extension;
+  ReverseChainSingleSubst      reverseChainContextSingle;
   } u;
   public:
-  DEFINE_SIZE_MIN (2);
+  DEFINE_SIZE_UNION (2, sub_format);
 };
 
 
@@ -782,19 +777,21 @@ struct SubstLookup : Lookup
 
   inline bool apply_once (hb_ot_layout_context_t *layout,
                          hb_buffer_t *buffer,
+                         hb_mask_t lookup_mask,
                          unsigned int context_length,
                          unsigned int nesting_level_left) const
   {
     unsigned int lookup_type = get_type ();
-    hb_apply_context_t context[1] = {{0}};
+    hb_apply_context_t c[1] = {{0}};
 
-    context->layout = layout;
-    context->buffer = buffer;
-    context->context_length = context_length;
-    context->nesting_level_left = nesting_level_left;
-    context->lookup_flag = get_flag ();
+    c->layout = layout;
+    c->buffer = buffer;
+    c->lookup_mask = lookup_mask;
+    c->context_length = context_length;
+    c->nesting_level_left = nesting_level_left;
+    c->lookup_flag = get_flag ();
 
-    if (!_hb_ot_layout_check_glyph_property (context->layout->face, IN_CURINFO (), context->lookup_flag, &context->property))
+    if (!_hb_ot_layout_check_glyph_property (c->layout->face, &c->buffer->info[c->buffer->i], c->lookup_flag, &c->property))
       return false;
 
     if (unlikely (lookup_type == SubstLookupSubTable::Extension))
@@ -805,15 +802,15 @@ struct SubstLookup : Lookup
        * This is rather slow to do this here for every glyph,
        * but it's easiest, and who uses extension lookups anyway?!*/
       unsigned int count = get_subtable_count ();
-      unsigned int type = get_subtable(0).u.extension->get_type ();
+      unsigned int type = get_subtable(0).u.extension.get_type ();
       for (unsigned int i = 1; i < count; i++)
-        if (get_subtable(i).u.extension->get_type () != type)
+        if (get_subtable(i).u.extension.get_type () != type)
          return false;
     }
 
     unsigned int count = get_subtable_count ();
     for (unsigned int i = 0; i < count; i++)
-      if (get_subtable (i).apply (context, lookup_type))
+      if (get_subtable (i).apply (c, lookup_type))
        return true;
 
     return false;
@@ -823,54 +820,52 @@ struct SubstLookup : Lookup
                            hb_buffer_t *buffer,
                            hb_mask_t    mask) const
   {
-#undef BUFFER
-#define BUFFER buffer
     bool ret = false;
 
-    if (unlikely (!buffer->in_length))
+    if (unlikely (!buffer->len))
       return false;
 
     if (likely (!is_reverse ()))
     {
        /* in/out forward substitution */
-       _hb_buffer_clear_output (buffer);
-       buffer->in_pos = 0;
-       while (buffer->in_pos < buffer->in_length)
+       buffer->clear_output ();
+       buffer->i = 0;
+       while (buffer->i < buffer->len)
        {
-         if ((~IN_MASK (buffer->in_pos) & mask) &&
-             apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
+         if ((buffer->info[buffer->i].mask & mask) &&
+             apply_once (layout, buffer, mask, NO_CONTEXT, MAX_NESTING_LEVEL))
            ret = true;
          else
-           _hb_buffer_next_glyph (buffer);
+           buffer->next_glyph ();
 
        }
        if (ret)
-         _hb_buffer_swap (buffer);
+         buffer->swap ();
     }
     else
     {
        /* in-place backward substitution */
-       buffer->in_pos = buffer->in_length - 1;
+       buffer->i = buffer->len - 1;
        do
        {
-         if ((~IN_MASK (buffer->in_pos) & mask) &&
-             apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
+         if ((buffer->info[buffer->i].mask & mask) &&
+             apply_once (layout, buffer, mask, NO_CONTEXT, MAX_NESTING_LEVEL))
            ret = true;
          else
-           buffer->in_pos--;
+           buffer->i--;
 
        }
-       while ((int) buffer->in_pos >= 0);
+       while ((int) buffer->i >= 0);
     }
 
     return ret;
   }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (unlikely (!Lookup::sanitize (context))) return false;
+    if (unlikely (!Lookup::sanitize (c))) return false;
     OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
-    return list.sanitize (context, this);
+    return list.sanitize (c, this, get_type ());
   }
 };
 
@@ -893,11 +888,11 @@ struct GSUB : GSUBGPOS
                                 hb_mask_t     mask) const
   { return get_lookup (lookup_index).apply_string (layout, buffer, mask); }
 
-  inline bool sanitize (hb_sanitize_context_t *context) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (unlikely (!GSUBGPOS::sanitize (context))) return false;
+    if (unlikely (!GSUBGPOS::sanitize (c))) return false;
     OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
-    return list.sanitize (context, this);
+    return list.sanitize (c, this);
   }
   public:
   DEFINE_SIZE_STATIC (10);
@@ -906,19 +901,19 @@ struct GSUB : GSUBGPOS
 
 /* Out-of-class implementation for methods recursing */
 
-inline bool ExtensionSubst::apply (hb_apply_context_t *context) const
+inline bool ExtensionSubst::apply (hb_apply_context_t *c) const
 {
   TRACE_APPLY ();
-  return get_subtable ().apply (context, get_type ());
+  return get_subtable ().apply (c, get_type ());
 }
 
-inline bool ExtensionSubst::sanitize (hb_sanitize_context_t *context)
+inline bool ExtensionSubst::sanitize (hb_sanitize_context_t *c)
 {
   TRACE_SANITIZE ();
-  if (unlikely (!Extension::sanitize (context))) return false;
+  if (unlikely (!Extension::sanitize (c))) return false;
   unsigned int offset = get_offset ();
   if (unlikely (!offset)) return true;
-  return StructAtOffset<SubstLookupSubTable> (this, offset).sanitize (context);
+  return StructAtOffset<SubstLookupSubTable> (this, offset).sanitize (c, get_type ());
 }
 
 inline bool ExtensionSubst::is_reverse (void) const
@@ -929,19 +924,21 @@ inline bool ExtensionSubst::is_reverse (void) const
   return SubstLookup::lookup_type_is_reverse (type);
 }
 
-static inline bool substitute_lookup (hb_apply_context_t *context, unsigned int lookup_index)
+static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index)
 {
-  const GSUB &gsub = *(context->layout->face->ot_layout.gsub);
+  const GSUB &gsub = *(c->layout->face->ot_layout->gsub);
   const SubstLookup &l = gsub.get_lookup (lookup_index);
 
-  if (unlikely (context->nesting_level_left == 0))
+  if (unlikely (c->nesting_level_left == 0))
     return false;
 
-  if (unlikely (context->context_length < 1))
+  if (unlikely (c->context_length < 1))
     return false;
 
-  return l.apply_once (context->layout, context->buffer, context->context_length, context->nesting_level_left - 1);
+  return l.apply_once (c->layout, c->buffer, c->lookup_mask, c->context_length, c->nesting_level_left - 1);
 }
 
 
+HB_END_DECLS
+
 #endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */