Oops, actually set global mask
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gsub-private.hh
index 31de65c..f09b4d2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007,2008,2009  Red Hat, Inc.
+ * Copyright (C) 2007,2008,2009,2010  Red Hat, Inc.
  *
  *  This is part of HarfBuzz, a text shaping library.
  *
@@ -29,6 +29,8 @@
 
 #include "hb-ot-layout-gsubgpos-private.hh"
 
+HB_BEGIN_DECLS
+
 
 struct SingleSubstFormat1
 {
@@ -36,27 +38,28 @@ struct SingleSubstFormat1
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) 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 (HB_LIKELY (index == NOT_COVERED))
+    if (likely (index == NOT_COVERED))
       return false;
 
     glyph_id += deltaGlyphID;
-    _hb_buffer_replace_glyph (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 (layout_context->face))
-      _hb_ot_layout_set_glyph_property (layout_context->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 (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE_THIS (coverage) && SANITIZE (deltaGlyphID);
+    return coverage.sanitize (c, this)
+       && deltaGlyphID.sanitize (c);
   }
 
   private:
@@ -66,8 +69,9 @@ struct SingleSubstFormat1
                                         * beginning of Substitution table */
   SHORT                deltaGlyphID;           /* Add to original GlyphID to get
                                         * substitute GlyphID */
+  public:
+  DEFINE_SIZE_STATIC (6);
 };
-ASSERT_SIZE (SingleSubstFormat1, 6);
 
 struct SingleSubstFormat2
 {
@@ -75,30 +79,31 @@ struct SingleSubstFormat2
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) 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 (HB_LIKELY (index == NOT_COVERED))
+    if (likely (index == NOT_COVERED))
       return false;
 
-    if (HB_UNLIKELY (index >= substitute.len))
+    if (unlikely (index >= substitute.len))
       return false;
 
     glyph_id = substitute[index];
-    _hb_buffer_replace_glyph (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 (layout_context->face))
-      _hb_ot_layout_set_glyph_property (layout_context->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 (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE_THIS (coverage) && SANITIZE (substitute);
+    return coverage.sanitize (c, this)
+       && substitute.sanitize (c);
   }
 
   private:
@@ -109,8 +114,9 @@ struct SingleSubstFormat2
   ArrayOf<GlyphID>
                substitute;             /* Array of substitute
                                         * GlyphIDs--ordered by Coverage Index */
+  public:
+  DEFINE_SIZE_ARRAY (6, substitute);
 };
-ASSERT_SIZE (SingleSubstFormat2, 6);
 
 struct SingleSubst
 {
@@ -118,22 +124,22 @@ struct SingleSubst
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (APPLY_ARG);
-    case 2: return u.format2->apply (APPLY_ARG);
+    case 1: return u.format1.apply (c);
+    case 2: return u.format2.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!SANITIZE (u.format)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (SANITIZE_ARG);
-    case 2: return u.format2->sanitize (SANITIZE_ARG);
+    case 1: return u.format1.sanitize (c);
+    case 2: return u.format2.sanitize (c);
     default:return true;
     }
   }
@@ -141,8 +147,8 @@ struct SingleSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  SingleSubstFormat1   format1[VAR];
-  SingleSubstFormat2   format2[VAR];
+  SingleSubstFormat1   format1;
+  SingleSubstFormat2   format2;
   } u;
 };
 
@@ -152,42 +158,43 @@ struct Sequence
   friend struct MultipleSubstFormat1;
 
   private:
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    if (HB_UNLIKELY (!substitute.len))
+    if (unlikely (!substitute.len))
       return false;
 
-    _hb_buffer_add_output_glyphs_be16 (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 (layout_context->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 (layout_context->face, substitute[n], property);
+       _hb_ot_layout_set_glyph_property (c->layout->face, substitute[n], property);
     }
 
     return true;
   }
 
   public:
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE (substitute);
+    return substitute.sanitize (c);
   }
 
   private:
   ArrayOf<GlyphID>
                substitute;             /* String of GlyphIDs to substitute */
+  public:
+  DEFINE_SIZE_ARRAY (2, substitute);
 };
-ASSERT_SIZE (Sequence, 2);
 
 struct MultipleSubstFormat1
 {
@@ -195,20 +202,21 @@ struct MultipleSubstFormat1
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
 
-    unsigned int index = (this+coverage) (IN_CURGLYPH ());
-    if (HB_LIKELY (index == NOT_COVERED))
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
+    if (likely (index == NOT_COVERED))
       return false;
 
-    return (this+sequence[index]).apply (APPLY_ARG);
+    return (this+sequence[index]).apply (c);
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE_THIS2 (coverage, sequence);
+    return coverage.sanitize (c, this)
+       && sequence.sanitize (c, this);
   }
 
   private:
@@ -219,8 +227,9 @@ struct MultipleSubstFormat1
   OffsetArrayOf<Sequence>
                sequence;               /* Array of Sequence tables
                                         * ordered by Coverage Index */
+  public:
+  DEFINE_SIZE_ARRAY (6, sequence);
 };
-ASSERT_SIZE (MultipleSubstFormat1, 6);
 
 struct MultipleSubst
 {
@@ -228,20 +237,20 @@ struct MultipleSubst
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (APPLY_ARG);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!SANITIZE (u.format)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (SANITIZE_ARG);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -249,14 +258,13 @@ struct MultipleSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  MultipleSubstFormat1 format1[VAR];
+  MultipleSubstFormat1 format1;
   } u;
 };
 
 
 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
                                         * arbitrary order */
-ASSERT_SIZE (AlternateSet, 2);
 
 struct AlternateSubstFormat1
 {
@@ -264,46 +272,44 @@ struct AlternateSubstFormat1
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) 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 (HB_LIKELY (index == NOT_COVERED))
+    if (likely (index == NOT_COVERED))
       return false;
 
     const AlternateSet &alt_set = this+alternateSet[index];
 
-    if (HB_UNLIKELY (!alt_set.len))
+    if (unlikely (!alt_set.len))
       return false;
 
-    unsigned int alt_index = 0;
+    /* 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);
 
-    /* XXX callback to user to choose alternate
-    if (layout_context->face->altfunc)
-      alt_index = (layout_context->face->altfunc)(layout_context->layout, buffer,
-                                   buffer->out_pos, glyph_id,
-                                   alt_set.len, alt_set.array);
-                                  */
-
-    if (HB_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 (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 (layout_context->face))
-      _hb_ot_layout_set_glyph_property (layout_context->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 (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE_THIS2 (coverage, alternateSet);
+    return coverage.sanitize (c, this)
+       && alternateSet.sanitize (c, this);
   }
 
   private:
@@ -314,8 +320,9 @@ struct AlternateSubstFormat1
   OffsetArrayOf<AlternateSet>
                alternateSet;           /* Array of AlternateSet tables
                                         * ordered by Coverage Index */
+  public:
+  DEFINE_SIZE_ARRAY (6, alternateSet);
 };
-ASSERT_SIZE (AlternateSubstFormat1, 6);
 
 struct AlternateSubst
 {
@@ -323,20 +330,20 @@ struct AlternateSubst
 
   private:
 
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (APPLY_ARG);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!SANITIZE (u.format)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (SANITIZE_ARG);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -344,7 +351,7 @@ struct AlternateSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  AlternateSubstFormat1        format1[VAR];
+  AlternateSubstFormat1        format1;
   } u;
 };
 
@@ -354,21 +361,21 @@ struct Ligature
   friend struct LigatureSet;
 
   private:
-  inline bool apply (APPLY_ARG_DEF, 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 (buffer->in_length, buffer->in_pos + context_length);
-    if (HB_UNLIKELY (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 = 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 (layout_context->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 (HB_UNLIKELY (j + count - i == end))
+       if (unlikely (j + count - i == end))
          return false;
        j++;
       }
@@ -376,27 +383,27 @@ struct Ligature
       if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
        is_mark = false;
 
-      if (HB_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 (layout_context->face))
-      _hb_ot_layout_set_glyph_class (layout_context->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 == 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 (buffer, i,
+      c->buffer->add_output_glyphs_be16 (i,
                                         1, (const uint16_t *) &ligGlyph,
                                         0,
-                                        IN_LIGID (buffer->in_pos) && !IN_COMPONENT (buffer->in_pos) ?
-                                        0xFFFF : _hb_buffer_allocate_lig_id (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 (buffer);
-      _hb_buffer_add_output_glyph (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
@@ -407,10 +414,10 @@ struct Ligature
 
       for ( i = 1; i < count; i++ )
       {
-       while (_hb_ot_layout_skip_mark (layout_context->face, IN_CURINFO (), context->lookup_flag, NULL))
-         _hb_buffer_add_output_glyph (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);
 
-       (buffer->in_pos)++;
+       (c->buffer->i)++;
       }
     }
 
@@ -418,9 +425,10 @@ struct Ligature
   }
 
   public:
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE2 (ligGlyph, component);
+    return ligGlyph.sanitize (c)
+        && component.sanitize (c);
   }
 
   private:
@@ -429,22 +437,23 @@ struct Ligature
                component;              /* Array of component GlyphIDs--start
                                         * with the second  component--ordered
                                         * in writing direction */
+  public:
+  DEFINE_SIZE_ARRAY (4, component);
 };
-ASSERT_SIZE (Ligature, 4);
 
 struct LigatureSet
 {
   friend struct LigatureSubstFormat1;
 
   private:
-  inline bool apply (APPLY_ARG_DEF, 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 (APPLY_ARG, is_mark))
+      if (lig.apply (c, is_mark))
         return true;
     }
 
@@ -452,41 +461,43 @@ struct LigatureSet
   }
 
   public:
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE_THIS (ligature);
+    return ligature.sanitize (c, this);
   }
 
   private:
   OffsetArrayOf<Ligature>
                ligature;               /* Array LigatureSet tables
                                         * ordered by preference */
+  public:
+  DEFINE_SIZE_ARRAY (2, ligature);
 };
-ASSERT_SIZE (LigatureSet, 2);
 
 struct LigatureSubstFormat1
 {
   friend struct LigatureSubst;
 
   private:
-  inline bool apply (APPLY_ARG_DEF) 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 (HB_LIKELY (index == NOT_COVERED))
+    if (likely (index == NOT_COVERED))
       return false;
 
     const LigatureSet &lig_set = this+ligatureSet[index];
-    return lig_set.apply (APPLY_ARG, first_is_mark);
+    return lig_set.apply (c, first_is_mark);
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    return SANITIZE_THIS2 (coverage, ligatureSet);
+    return coverage.sanitize (c, this)
+       && ligatureSet.sanitize (c, this);
   }
 
   private:
@@ -497,28 +508,29 @@ struct LigatureSubstFormat1
   OffsetArrayOf<LigatureSet>
                ligatureSet;            /* Array LigatureSet tables
                                         * ordered by Coverage Index */
+  public:
+  DEFINE_SIZE_ARRAY (6, ligatureSet);
 };
-ASSERT_SIZE (LigatureSubstFormat1, 6);
 
 struct LigatureSubst
 {
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (APPLY_ARG);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!SANITIZE (u.format)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (SANITIZE_ARG);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -526,23 +538,24 @@ struct LigatureSubst
   private:
   union {
   USHORT               format;         /* Format identifier */
-  LigatureSubstFormat1 format1[VAR];
+  LigatureSubstFormat1 format1;
   } u;
 };
 
 
-
-static inline bool substitute_lookup (APPLY_ARG_DEF, 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 (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    return Context::apply (APPLY_ARG, substitute_lookup);
+    return Context::apply (c, substitute_lookup);
   }
 };
 
@@ -551,10 +564,10 @@ struct ChainContextSubst : ChainContext
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    return ChainContext::apply (APPLY_ARG, substitute_lookup);
+    return ChainContext::apply (c, substitute_lookup);
   }
 };
 
@@ -568,13 +581,13 @@ struct ExtensionSubst : Extension
   inline const struct SubstLookupSubTable& get_subtable (void) const
   {
     unsigned int offset = get_offset ();
-    if (HB_UNLIKELY (!offset)) return Null(SubstLookupSubTable);
-    return StructAtOffset<SubstLookupSubTable> (*this, offset);
+    if (unlikely (!offset)) return Null(SubstLookupSubTable);
+    return StructAtOffset<SubstLookupSubTable> (this, offset);
   }
 
-  inline bool apply (APPLY_ARG_DEF) const;
+  inline bool apply (hb_apply_context_t *c) const;
 
-  inline bool sanitize (SANITIZE_ARG_DEF);
+  inline bool sanitize (hb_sanitize_context_t *c);
 
   inline bool is_reverse (void) const;
 };
@@ -585,44 +598,45 @@ struct ReverseChainSingleSubstFormat1
   friend struct ReverseChainSingleSubst;
 
   private:
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    if (HB_UNLIKELY (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 ());
-    if (HB_LIKELY (index == NOT_COVERED))
+    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 (APPLY_ARG,
-                        backtrack.len, (USHORT *) backtrack.array(),
-                        match_coverage, CharP(this)) &&
-        match_lookahead (APPLY_ARG,
-                        lookahead.len, (USHORT *) lookahead.array(),
-                        match_coverage, CharP(this),
+    if (match_backtrack (c,
+                        backtrack.len, (USHORT *) backtrack.array,
+                        match_coverage, this) &&
+        match_lookahead (c,
+                        lookahead.len, (USHORT *) lookahead.array,
+                        match_coverage, this,
                         1))
     {
-      IN_CURGLYPH () = substitute[index];
-      buffer->in_pos--; /* Reverse! */
+      c->buffer->info[c->buffer->i].codepoint = substitute[index];
+      c->buffer->i--; /* Reverse! */
       return true;
     }
 
     return false;
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!SANITIZE_THIS2 (coverage, backtrack))
+    if (!(coverage.sanitize (c, this)
+       && backtrack.sanitize (c, this)))
       return false;
     OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
-    if (!SANITIZE_THIS (lookahead))
+    if (!lookahead.sanitize (c, this))
       return false;
     ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
-    return SANITIZE (substitute);
+    return substitute.sanitize (c);
   }
 
   private:
@@ -641,28 +655,29 @@ struct ReverseChainSingleSubstFormat1
   ArrayOf<GlyphID>
                substituteX;            /* Array of substitute
                                         * GlyphIDs--ordered by Coverage Index */
+  public:
+  DEFINE_SIZE_MIN (10);
 };
-ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);
 
 struct ReverseChainSingleSubst
 {
   friend struct SubstLookupSubTable;
 
   private:
-  inline bool apply (APPLY_ARG_DEF) const
+  inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
     switch (u.format) {
-    case 1: return u.format1->apply (APPLY_ARG);
+    case 1: return u.format1.apply (c);
     default:return false;
     }
   }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (!SANITIZE (u.format)) return false;
+    if (!u.format.sanitize (c)) return false;
     switch (u.format) {
-    case 1: return u.format1->sanitize (SANITIZE_ARG);
+    case 1: return u.format1.sanitize (c);
     default:return true;
     }
   }
@@ -670,7 +685,7 @@ struct ReverseChainSingleSubst
   private:
   union {
   USHORT                               format;         /* Format identifier */
-  ReverseChainSingleSubstFormat1       format1[VAR];
+  ReverseChainSingleSubstFormat1       format1;
   } u;
 };
 
@@ -695,50 +710,51 @@ struct SubstLookupSubTable
     ReverseChainSingle = 8
   };
 
-  inline bool apply (APPLY_ARG_DEF, 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 (APPLY_ARG);
-    case Multiple:             return u.multiple->apply (APPLY_ARG);
-    case Alternate:            return u.alternate->apply (APPLY_ARG);
-    case Ligature:             return u.ligature->apply (APPLY_ARG);
-    case Context:              return u.context->apply (APPLY_ARG);
-    case ChainContext:         return u.chainContext->apply (APPLY_ARG);
-    case Extension:            return u.extension->apply (APPLY_ARG);
-    case ReverseChainSingle:   return u.reverseChainContextSingle->apply (APPLY_ARG);
+    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 (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
     TRACE_SANITIZE ();
-    if (!SANITIZE (u.format)) return false;
-    switch (u.format) {
-    case Single:               return u.single->sanitize (SANITIZE_ARG);
-    case Multiple:             return u.multiple->sanitize (SANITIZE_ARG);
-    case Alternate:            return u.alternate->sanitize (SANITIZE_ARG);
-    case Ligature:             return u.ligature->sanitize (SANITIZE_ARG);
-    case Context:              return u.context->sanitize (SANITIZE_ARG);
-    case ChainContext:         return u.chainContext->sanitize (SANITIZE_ARG);
-    case Extension:            return u.extension->sanitize (SANITIZE_ARG);
-    case ReverseChainSingle:   return u.reverseChainContextSingle->sanitize (SANITIZE_ARG);
+    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_UNION (2, sub_format);
 };
 
 
@@ -753,28 +769,32 @@ struct SubstLookup : Lookup
   inline bool is_reverse (void) const
   {
     unsigned int type = get_type ();
-    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
+    if (unlikely (type == SubstLookupSubTable::Extension))
       return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
     return lookup_type_is_reverse (type);
   }
 
 
-  inline bool apply_once ( hb_ot_layout_context_t *layout_context,
+  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,
-                         unsigned int apply_depth) const
+                         unsigned int nesting_level_left) const
   {
     unsigned int lookup_type = get_type ();
-    hb_apply_context_t context[1];
+    hb_apply_context_t c[1] = {{0}};
 
-    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 (layout_context->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 (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
+    if (unlikely (lookup_type == SubstLookupSubTable::Extension))
     {
       /* The spec says all subtables should have the same type.
        * This is specially important if one has a reverse type!
@@ -782,75 +802,74 @@ 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 (APPLY_ARG, lookup_type))
+      if (get_subtable (i).apply (c, lookup_type))
        return true;
 
     return false;
   }
 
-  inline bool apply_string (hb_ot_layout_context_t *layout_context,
+  inline bool apply_string (hb_ot_layout_context_t *layout,
                            hb_buffer_t *buffer,
                            hb_mask_t    mask) const
   {
     bool ret = false;
 
-    if (HB_UNLIKELY (!buffer->in_length))
+    if (unlikely (!buffer->len))
       return false;
 
-    if (HB_LIKELY (!is_reverse ()))
+    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_context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL, 0))
+         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_context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL, 0))
+         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 (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (HB_UNLIKELY (!Lookup::sanitize (SANITIZE_ARG))) return false;
+    if (unlikely (!Lookup::sanitize (c))) return false;
     OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
-    return SANITIZE_THIS (list);
+    return list.sanitize (c, this, get_type ());
   }
 };
 
 typedef OffsetListOf<SubstLookup> SubstLookupList;
-ASSERT_SIZE (SubstLookupList, 2);
 
 /*
  * GSUB
@@ -863,61 +882,63 @@ struct GSUB : GSUBGPOS
   inline const SubstLookup& get_lookup (unsigned int i) const
   { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
 
-  inline bool substitute_lookup (hb_ot_layout_context_t *layout_context,
+  inline bool substitute_lookup (hb_ot_layout_context_t *layout,
                                 hb_buffer_t  *buffer,
                                 unsigned int  lookup_index,
                                 hb_mask_t     mask) const
-  { return get_lookup (lookup_index).apply_string (layout_context, buffer, mask); }
-
+  { return get_lookup (lookup_index).apply_string (layout, buffer, mask); }
 
-  inline bool sanitize (SANITIZE_ARG_DEF) {
+  inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
-    if (HB_UNLIKELY (!GSUBGPOS::sanitize (SANITIZE_ARG))) return false;
+    if (unlikely (!GSUBGPOS::sanitize (c))) return false;
     OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
-    return SANITIZE_THIS (list);
+    return list.sanitize (c, this);
   }
+  public:
+  DEFINE_SIZE_STATIC (10);
 };
-ASSERT_SIZE (GSUB, 10);
 
 
 /* Out-of-class implementation for methods recursing */
 
-inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
+inline bool ExtensionSubst::apply (hb_apply_context_t *c) const
 {
   TRACE_APPLY ();
-  return get_subtable ().apply (APPLY_ARG, get_type ());
+  return get_subtable ().apply (c, get_type ());
 }
 
-inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
+inline bool ExtensionSubst::sanitize (hb_sanitize_context_t *c)
 {
   TRACE_SANITIZE ();
-  if (HB_UNLIKELY (!Extension::sanitize (SANITIZE_ARG))) return false;
+  if (unlikely (!Extension::sanitize (c))) return false;
   unsigned int offset = get_offset ();
-  if (HB_UNLIKELY (!offset)) return true;
-  return SANITIZE (StructAtOffset<SubstLookupSubTable> (*this, offset));
+  if (unlikely (!offset)) return true;
+  return StructAtOffset<SubstLookupSubTable> (this, offset).sanitize (c, get_type ());
 }
 
 inline bool ExtensionSubst::is_reverse (void) const
 {
   unsigned int type = get_type ();
-  if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
+  if (unlikely (type == SubstLookupSubTable::Extension))
     return CastR<ExtensionSubst> (get_subtable()).is_reverse ();
   return SubstLookup::lookup_type_is_reverse (type);
 }
 
-static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
+static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index)
 {
-  const GSUB &gsub = *(layout_context->face->ot_layout.gsub);
+  const GSUB &gsub = *(c->layout->face->ot_layout->gsub);
   const SubstLookup &l = gsub.get_lookup (lookup_index);
 
-  if (HB_UNLIKELY (context->nesting_level_left == 0))
+  if (unlikely (c->nesting_level_left == 0))
     return false;
 
-  if (HB_UNLIKELY (context_length < 1))
+  if (unlikely (c->context_length < 1))
     return false;
 
-  return l.apply_once (layout_context, buffer, context_length, context->nesting_level_left - 1, apply_depth + 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 */