Move buffer into apply_context
diff --git a/src/hb-buffer-private.h b/src/hb-buffer-private.h
index c2bd885..d4a1641 100644
--- a/src/hb-buffer-private.h
+++ b/src/hb-buffer-private.h
@@ -132,20 +132,24 @@
 _hb_buffer_allocate_lig_id (hb_buffer_t *buffer);
 
 
+#ifndef BUFFER
+#define BUFFER buffer
+#endif
+
 /* convenience macros */
-#define IN_GLYPH(pos)		(buffer->in_string[(pos)].codepoint)
-#define IN_INFO(pos)		(&buffer->in_string[(pos)])
-#define IN_CURGLYPH()		(buffer->in_string[buffer->in_pos].codepoint)
-#define IN_NEXTGLYPH()		(buffer->in_string[buffer->in_pos + 1].codepoint)
-#define IN_CURINFO()		(&buffer->in_string[buffer->in_pos])
-#define IN_MASK(pos)		(buffer->in_string[(pos)].mask)
-#define IN_CLUSTER(pos)		(buffer->in_string[(pos)].cluster)
-#define IN_LIGID(pos)		(buffer->in_string[(pos)].lig_id)
-#define IN_COMPONENT(pos)	(buffer->in_string[(pos)].component)
-#define POSITION(pos)		(&buffer->positions[(pos)])
-#define CURPOSITION()		(&buffer->positions[buffer->in_pos])
-#define OUT_GLYPH(pos)		(buffer->out_string[(pos)].codepoint)
-#define OUT_INFO(pos)		(&buffer->out_string[(pos)])
+#define IN_GLYPH(pos)		(BUFFER->in_string[(pos)].codepoint)
+#define IN_INFO(pos)		(&BUFFER->in_string[(pos)])
+#define IN_CURGLYPH()		(BUFFER->in_string[BUFFER->in_pos].codepoint)
+#define IN_NEXTGLYPH()		(BUFFER->in_string[BUFFER->in_pos + 1].codepoint)
+#define IN_CURINFO()		(&BUFFER->in_string[BUFFER->in_pos])
+#define IN_MASK(pos)		(BUFFER->in_string[(pos)].mask)
+#define IN_CLUSTER(pos)		(BUFFER->in_string[(pos)].cluster)
+#define IN_LIGID(pos)		(BUFFER->in_string[(pos)].lig_id)
+#define IN_COMPONENT(pos)	(BUFFER->in_string[(pos)].component)
+#define POSITION(pos)		(&BUFFER->positions[(pos)])
+#define CURPOSITION()		(&BUFFER->positions[BUFFER->in_pos])
+#define OUT_GLYPH(pos)		(BUFFER->out_string[(pos)].codepoint)
+#define OUT_INFO(pos)		(&BUFFER->out_string[(pos)])
 
 HB_END_DECLS
 
diff --git a/src/hb-ot-layout-gpos-private.hh b/src/hb-ot-layout-gpos-private.hh
index 8fe93eb..605102d 100644
--- a/src/hb-ot-layout-gpos-private.hh
+++ b/src/hb-ot-layout-gpos-private.hh
@@ -29,6 +29,11 @@
 
 #include "hb-ot-layout-gsubgpos-private.hh"
 
+
+#undef BUFFER
+#define BUFFER context->buffer
+
+
 #define HB_OT_LAYOUT_GPOS_NO_LAST ((unsigned int) -1)
 
 /* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
@@ -391,14 +396,14 @@
     mark_anchor.get_anchor (context->layout, IN_CURGLYPH (), &mark_x, &mark_y);
     glyph_anchor.get_anchor (context->layout, IN_GLYPH (glyph_pos), &base_x, &base_y);
 
-    hb_internal_glyph_position_t *o = POSITION (buffer->in_pos);
+    hb_internal_glyph_position_t *o = POSITION (context->buffer->in_pos);
     o->x_advance = 0;
     o->y_advance = 0;
     o->x_offset  = base_x - mark_x;
     o->y_offset  = base_y - mark_y;
-    o->back      = buffer->in_pos - glyph_pos;
+    o->back      = context->buffer->in_pos - glyph_pos;
 
-    buffer->in_pos++;
+    context->buffer->in_pos++;
     return true;
   }
 
@@ -430,7 +435,7 @@
 
     valueFormat.apply_value (context->layout, CharP(this), values, CURPOSITION ());
 
-    buffer->in_pos++;
+    context->buffer->in_pos++;
     return true;
   }
 
@@ -473,7 +478,7 @@
 			     &values[index * valueFormat.get_len ()],
 			     CURPOSITION ());
 
-    buffer->in_pos++;
+    context->buffer->in_pos++;
     return true;
   }
 
@@ -572,15 +577,15 @@
   inline bool apply (APPLY_ARG_DEF) const
   {
     TRACE_APPLY ();
-    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
-    if (unlikely (buffer->in_pos + 2 > end))
+    unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context_length);
+    if (unlikely (context->buffer->in_pos + 2 > end))
       return false;
 
     unsigned int index = (this+coverage) (IN_CURGLYPH ());
     if (likely (index == NOT_COVERED))
       return false;
 
-    unsigned int j = buffer->in_pos + 1;
+    unsigned int j = context->buffer->in_pos + 1;
     while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, NULL))
     {
       if (unlikely (j == end))
@@ -603,7 +608,7 @@
 	valueFormat2.apply_value (context->layout, CharP(this), &record->values[len1], POSITION (j));
 	if (len2)
 	  j++;
-	buffer->in_pos = j;
+	context->buffer->in_pos = j;
 	return true;
       }
       record = &StructAtOffset<PairValueRecord> (*record, record_size);
@@ -665,15 +670,15 @@
   inline bool apply (APPLY_ARG_DEF) const
   {
     TRACE_APPLY ();
-    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
-    if (unlikely (buffer->in_pos + 2 > end))
+    unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context_length);
+    if (unlikely (context->buffer->in_pos + 2 > end))
       return false;
 
     unsigned int index = (this+coverage) (IN_CURGLYPH ());
     if (likely (index == NOT_COVERED))
       return false;
 
-    unsigned int j = buffer->in_pos + 1;
+    unsigned int j = context->buffer->in_pos + 1;
     while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, NULL))
     {
       if (unlikely (j == end))
@@ -696,7 +701,7 @@
 
     if (len2)
       j++;
-    buffer->in_pos = j;
+    context->buffer->in_pos = j;
 
     return true;
   }
@@ -949,10 +954,10 @@
 
     /* TODO vertical */
 
-    if (buffer->direction == HB_DIRECTION_RTL)
+    if (context->buffer->direction == HB_DIRECTION_RTL)
     {
       /* advance is absolute, not relative */
-      POSITION (buffer->in_pos)->x_advance = entry_x - gpi->anchor_x;
+      POSITION (context->buffer->in_pos)->x_advance = entry_x - gpi->anchor_x;
     }
     else
     {
@@ -962,23 +967,23 @@
 
     if  (context->lookup_flag & LookupFlag::RightToLeft)
     {
-      POSITION (last_pos)->cursive_chain = last_pos - buffer->in_pos;
+      POSITION (last_pos)->cursive_chain = last_pos - context->buffer->in_pos;
       POSITION (last_pos)->y_offset = entry_y - gpi->anchor_y;
     }
     else
     {
-      POSITION (buffer->in_pos)->cursive_chain = buffer->in_pos - last_pos;
-      POSITION (buffer->in_pos)->y_offset = gpi->anchor_y - entry_y;
+      POSITION (context->buffer->in_pos)->cursive_chain = context->buffer->in_pos - last_pos;
+      POSITION (context->buffer->in_pos)->y_offset = gpi->anchor_y - entry_y;
     }
 
   end:
     if (record.exitAnchor)
     {
-      gpi->last = buffer->in_pos;
+      gpi->last = context->buffer->in_pos;
       (this+record.exitAnchor).get_anchor (context->layout, IN_CURGLYPH (), &gpi->anchor_x, &gpi->anchor_y);
     }
 
-    buffer->in_pos++;
+    context->buffer->in_pos++;
     return true;
   }
 
@@ -1049,7 +1054,7 @@
 
     /* now we search backwards for a non-mark glyph */
     unsigned int property;
-    unsigned int j = buffer->in_pos;
+    unsigned int j = context->buffer->in_pos;
     do
     {
       if (unlikely (!j))
@@ -1152,7 +1157,7 @@
 
     /* now we search backwards for a non-mark glyph */
     unsigned int property;
-    unsigned int j = buffer->in_pos;
+    unsigned int j = context->buffer->in_pos;
     do
     {
       if (unlikely (!j))
@@ -1182,9 +1187,9 @@
      * is identical to the ligature ID of the found ligature.  If yes, we
      * can directly use the component index.  If not, we attach the mark
      * glyph to the last component of the ligature. */
-    if (IN_LIGID (j) && IN_LIGID (j) == IN_LIGID (buffer->in_pos) && IN_COMPONENT (buffer->in_pos))
+    if (IN_LIGID (j) && IN_LIGID (j) == IN_LIGID (context->buffer->in_pos) && IN_COMPONENT (context->buffer->in_pos))
     {
-      comp_index = IN_COMPONENT (buffer->in_pos) - 1;
+      comp_index = IN_COMPONENT (context->buffer->in_pos) - 1;
       if (comp_index >= comp_count)
 	comp_index = comp_count - 1;
     }
@@ -1272,7 +1277,7 @@
 
     /* now we search backwards for a suitable mark glyph until a non-mark glyph */
     unsigned int property;
-    unsigned int j = buffer->in_pos;
+    unsigned int j = context->buffer->in_pos;
     do
     {
       if (unlikely (!j))
@@ -1286,8 +1291,8 @@
     /* Two marks match only if they belong to the same base, or same component
      * of the same ligature.  That is, the component numbers must match, and
      * if those are non-zero, the ligid number should also match. */
-    if ((IN_COMPONENT (j) != IN_COMPONENT (buffer->in_pos)) ||
-	(IN_COMPONENT (j) && IN_LIGID (j) != IN_LIGID (buffer->in_pos)))
+    if ((IN_COMPONENT (j) != IN_COMPONENT (context->buffer->in_pos)) ||
+	(IN_COMPONENT (j) && IN_LIGID (j) != IN_LIGID (context->buffer->in_pos)))
       return false;
 
     unsigned int mark2_index = (this+mark2Coverage) (IN_GLYPH (j));
@@ -1489,6 +1494,7 @@
     hb_apply_context_t context[1];
 
     context->layout = layout;
+    context->buffer = buffer;
     context->nesting_level_left = nesting_level_left;
     context->lookup_flag = get_flag ();
 
@@ -1506,6 +1512,8 @@
 			     hb_buffer_t *buffer,
 			     hb_mask_t    mask) const
   {
+#undef BUFFER
+#define BUFFER buffer
     bool ret = false;
 
     if (unlikely (!buffer->in_length))
@@ -1603,7 +1611,7 @@
   if (unlikely (context_length < 1))
     return false;
 
-  return l.apply_once (context->layout, buffer, context_length, context->nesting_level_left - 1, apply_depth + 1);
+  return l.apply_once (context->layout, context->buffer, context_length, context->nesting_level_left - 1, apply_depth + 1);
 }
 
 
diff --git a/src/hb-ot-layout-gsub-private.hh b/src/hb-ot-layout-gsub-private.hh
index f2f8954..f0306a3 100644
--- a/src/hb-ot-layout-gsub-private.hh
+++ b/src/hb-ot-layout-gsub-private.hh
@@ -30,6 +30,10 @@
 #include "hb-ot-layout-gsubgpos-private.hh"
 
 
+#undef BUFFER
+#define BUFFER context->buffer
+
+
 struct SingleSubstFormat1
 {
   friend struct SingleSubst;
@@ -45,7 +49,7 @@
       return false;
 
     glyph_id += deltaGlyphID;
-    _hb_buffer_replace_glyph (buffer, glyph_id);
+    _hb_buffer_replace_glyph (context->buffer, glyph_id);
 
     /* We inherit the old glyph class to the substituted glyph */
     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
@@ -88,7 +92,7 @@
       return false;
 
     glyph_id = substitute[index];
-    _hb_buffer_replace_glyph (buffer, glyph_id);
+    _hb_buffer_replace_glyph (context->buffer, glyph_id);
 
     /* We inherit the old glyph class to the substituted glyph */
     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
@@ -160,7 +164,7 @@
     if (unlikely (!substitute.len))
       return false;
 
-    _hb_buffer_add_output_glyphs_be16 (buffer, 1,
+    _hb_buffer_add_output_glyphs_be16 (context->buffer, 1,
 				       substitute.len, (const uint16_t *) substitute.array(),
 				       0xFFFF, 0xFFFF);
 
@@ -285,8 +289,8 @@
 
     /* XXX callback to user to choose alternate
     if (context->layout->face->altfunc)
-      alt_index = (context->layout->face->altfunc)(context->layout->layout, buffer,
-				    buffer->out_pos, glyph_id,
+      alt_index = (context->layout->face->altfunc)(context->layout->layout, context->buffer,
+				    context->buffer->out_pos, glyph_id,
 				    alt_set.len, alt_set.array);
 				   */
 
@@ -295,7 +299,7 @@
 
     glyph_id = alt_set[alt_index];
 
-    _hb_buffer_replace_glyph (buffer, glyph_id);
+    _hb_buffer_replace_glyph (context->buffer, glyph_id);
 
     /* We inherit the old glyph class to the substituted glyph */
     if (_hb_ot_layout_has_new_glyph_classes (context->layout->face))
@@ -363,11 +367,11 @@
     TRACE_APPLY ();
     unsigned int i, j;
     unsigned int count = component.len;
-    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
-    if (unlikely (buffer->in_pos + count > end))
+    unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context_length);
+    if (unlikely (context->buffer->in_pos + count > end))
       return false;
 
-    for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
+    for (i = 1, j = context->buffer->in_pos + 1; i < count; i++, j++)
     {
       unsigned int property;
       while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, &property))
@@ -389,18 +393,18 @@
 				     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 == context->buffer->in_pos + 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,
+      _hb_buffer_add_output_glyphs_be16 (context->buffer, i,
 					 1, (const uint16_t *) &ligGlyph,
 					 0,
-					 IN_LIGID (buffer->in_pos) && !IN_COMPONENT (buffer->in_pos) ?
-					 0xFFFF : _hb_buffer_allocate_lig_id (buffer));
+					 IN_LIGID (context->buffer->in_pos) && !IN_COMPONENT (context->buffer->in_pos) ?
+					 0xFFFF : _hb_buffer_allocate_lig_id (context->buffer));
     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 = _hb_buffer_allocate_lig_id (context->buffer);
+      _hb_buffer_add_output_glyph (context->buffer, 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
@@ -412,9 +416,9 @@
       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 (buffer, IN_CURGLYPH (), i, lig_id);
+	  _hb_buffer_add_output_glyph (context->buffer, IN_CURGLYPH (), i, lig_id);
 
-	(buffer->in_pos)++;
+	(context->buffer->in_pos)++;
       }
     }
 
@@ -612,7 +616,7 @@
 			 1))
     {
       IN_CURGLYPH () = substitute[index];
-      buffer->in_pos--; /* Reverse! */
+      context->buffer->in_pos--; /* Reverse! */
       return true;
     }
 
@@ -775,6 +779,7 @@
     hb_apply_context_t context[1];
 
     context->layout = layout;
+    context->buffer = buffer;
     context->nesting_level_left = nesting_level_left;
     context->lookup_flag = get_flag ();
 
@@ -807,6 +812,8 @@
 			    hb_buffer_t *buffer,
 			    hb_mask_t    mask) const
   {
+#undef BUFFER
+#define BUFFER buffer
     bool ret = false;
 
     if (unlikely (!buffer->in_length))
@@ -923,7 +930,7 @@
   if (unlikely (context_length < 1))
     return false;
 
-  return l.apply_once (context->layout, buffer, context_length, context->nesting_level_left - 1, apply_depth + 1);
+  return l.apply_once (context->layout, context->buffer, context_length, context->nesting_level_left - 1, apply_depth + 1);
 }
 
 
diff --git a/src/hb-ot-layout-gsubgpos-private.hh b/src/hb-ot-layout-gsubgpos-private.hh
index e91bae5..5398b8a 100644
--- a/src/hb-ot-layout-gsubgpos-private.hh
+++ b/src/hb-ot-layout-gsubgpos-private.hh
@@ -44,24 +44,29 @@
 
 #define APPLY_ARG_DEF \
 	hb_apply_context_t *context, \
-	hb_buffer_t *buffer, \
 	unsigned int context_length HB_UNUSED, \
 	unsigned int apply_depth HB_UNUSED
 #define APPLY_ARG \
 	context, \
-	buffer, \
 	context_length, \
 	(HB_DEBUG_APPLY ? apply_depth + 1 : 0)
 
 struct hb_apply_context_t
 {
   hb_ot_layout_context_t *layout;
+  hb_buffer_t *buffer;
   unsigned int nesting_level_left;
   unsigned int lookup_flag;
   unsigned int property; /* propety of first glyph (TODO remove) */
 };
 
 
+
+
+#undef BUFFER
+#define BUFFER context->buffer
+
+
 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const char *data);
 typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
 
@@ -98,11 +103,11 @@
 				unsigned int *context_length_out)
 {
   unsigned int i, j;
-  unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
-  if (unlikely (buffer->in_pos + count > end))
+  unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context_length);
+  if (unlikely (context->buffer->in_pos + count > end))
     return false;
 
-  for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
+  for (i = 1, j = context->buffer->in_pos + 1; i < count; i++, j++)
   {
     while (_hb_ot_layout_skip_mark (context->layout->face, IN_INFO (j), context->lookup_flag, NULL))
     {
@@ -115,7 +120,7 @@
       return false;
   }
 
-  *context_length_out = j - buffer->in_pos;
+  *context_length_out = j - context->buffer->in_pos;
 
   return true;
 }
@@ -126,10 +131,10 @@
 				    match_func_t match_func,
 				    const char *match_data)
 {
-  if (unlikely (buffer->out_pos < count))
+  if (unlikely (context->buffer->out_pos < count))
     return false;
 
-  for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
+  for (unsigned int i = 0, j = context->buffer->out_pos - 1; i < count; i++, j--)
   {
     while (_hb_ot_layout_skip_mark (context->layout->face, OUT_INFO (j), context->lookup_flag, NULL))
     {
@@ -153,11 +158,11 @@
 				    unsigned int offset)
 {
   unsigned int i, j;
-  unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
-  if (unlikely (buffer->in_pos + offset + count > end))
+  unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context_length);
+  if (unlikely (context->buffer->in_pos + offset + count > end))
     return false;
 
-  for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
+  for (i = 0, j = context->buffer->in_pos + offset; i < count; i++, j++)
   {
     while (_hb_ot_layout_skip_mark (context->layout->face, OUT_INFO (j), context->lookup_flag, NULL))
     {
@@ -196,8 +201,8 @@
 				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
 				 apply_lookup_func_t apply_func)
 {
-  unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
-  if (unlikely (buffer->in_pos + count > end))
+  unsigned int end = MIN (context->buffer->in_length, context->buffer->in_pos + context_length);
+  if (unlikely (context->buffer->in_pos + count > end))
     return false;
 
   /* TODO We don't support lookupRecord arrays that are not increasing:
@@ -211,15 +216,15 @@
   {
     while (_hb_ot_layout_skip_mark (context->layout->face, IN_CURINFO (), context->lookup_flag, NULL))
     {
-      if (unlikely (buffer->in_pos == end))
+      if (unlikely (context->buffer->in_pos == end))
 	return true;
       /* No lookup applied for this index */
-      _hb_buffer_next_glyph (buffer);
+      _hb_buffer_next_glyph (context->buffer);
     }
 
     if (lookupCount && i == lookupRecord->sequenceIndex)
     {
-      unsigned int old_pos = buffer->in_pos;
+      unsigned int old_pos = context->buffer->in_pos;
 
       /* Apply a lookup */
       bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
@@ -227,8 +232,8 @@
       lookupRecord++;
       lookupCount--;
       /* Err, this is wrong if the lookup jumped over some glyphs */
-      i += buffer->in_pos - old_pos;
-      if (unlikely (buffer->in_pos == end))
+      i += context->buffer->in_pos - old_pos;
+      if (unlikely (context->buffer->in_pos == end))
 	return true;
 
       if (!done)
@@ -238,7 +243,7 @@
     {
     not_applied:
       /* No lookup applied for this index */
-      _hb_buffer_next_glyph (buffer);
+      _hb_buffer_next_glyph (context->buffer);
       i++;
     }
   }
@@ -522,9 +527,9 @@
 					 ChainContextLookupContext &lookup_context)
 {
   /* First guess */
-  if (unlikely (buffer->out_pos < backtrackCount ||
-		   buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
-		   inputCount + lookaheadCount > context_length))
+  if (unlikely (context->buffer->out_pos < backtrackCount ||
+		context->buffer->in_pos + inputCount + lookaheadCount > context->buffer->in_length ||
+		inputCount + lookaheadCount > context_length))
     return false;
 
   unsigned int offset;