Imported Upstream version 0.9.35
[platform/upstream/harfbuzz.git] / src / hb-ot-layout-gsubgpos-private.hh
index 0b00005..546ff4b 100644 (file)
@@ -38,12 +38,11 @@ namespace OT {
 
 
 
-#define TRACE_PROCESS(this) \
+#define TRACE_DISPATCH(this) \
        hb_auto_trace_t<context_t::max_debug_depth, typename context_t::return_t> trace \
        (&c->debug_depth, c->get_name (), this, HB_FUNC, \
         "");
 
-
 #ifndef HB_DEBUG_CLOSURE
 #define HB_DEBUG_CLOSURE (HB_DEBUG+0)
 #endif
@@ -60,7 +59,7 @@ struct hb_closure_context_t
   typedef hb_void_t return_t;
   typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
   template <typename T>
-  inline return_t process (const T &obj) { obj.closure (this); return HB_VOID; }
+  inline return_t dispatch (const T &obj) { obj.closure (this); return HB_VOID; }
   static return_t default_return_value (void) { return HB_VOID; }
   bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
   return_t recurse (unsigned int lookup_index)
@@ -109,7 +108,7 @@ struct hb_would_apply_context_t
   static const unsigned int max_debug_depth = HB_DEBUG_WOULD_APPLY;
   typedef bool return_t;
   template <typename T>
-  inline return_t process (const T &obj) { return obj.would_apply (this); }
+  inline return_t dispatch (const T &obj) { return obj.would_apply (this); }
   static return_t default_return_value (void) { return false; }
   bool stop_sublookup_iteration (return_t r) const { return r; }
 
@@ -148,7 +147,7 @@ struct hb_collect_glyphs_context_t
   typedef hb_void_t return_t;
   typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
   template <typename T>
-  inline return_t process (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
+  inline return_t dispatch (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
   static return_t default_return_value (void) { return HB_VOID; }
   bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
   return_t recurse (unsigned int lookup_index)
@@ -158,7 +157,13 @@ struct hb_collect_glyphs_context_t
 
     /* Note that GPOS sets recurse_func to NULL already, so it doesn't get
      * past the previous check.  For GSUB, we only want to collect the output
-     * glyphs in the recursion.  If output is not requested, we can go home now. */
+     * glyphs in the recursion.  If output is not requested, we can go home now.
+     *
+     * Note further, that the above is not exactly correct.  A recursed lookup
+     * is allowed to match input that is not matched in the context, but that's
+     * not how most fonts are built.  It's possible to relax that and recurse
+     * with all sets here if it proves to be an issue.
+     */
 
     if (output == hb_set_get_empty ())
       return HB_VOID;
@@ -214,7 +219,7 @@ struct hb_get_coverage_context_t
   static const unsigned int max_debug_depth = 0;
   typedef const Coverage &return_t;
   template <typename T>
-  inline return_t process (const T &obj) { return obj.get_coverage (); }
+  inline return_t dispatch (const T &obj) { return obj.get_coverage (); }
   static return_t default_return_value (void) { return Null(Coverage); }
 
   hb_get_coverage_context_t (void) :
@@ -241,7 +246,7 @@ struct hb_apply_context_t
   typedef bool return_t;
   typedef return_t (*recurse_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
   template <typename T>
-  inline return_t process (const T &obj) { return obj.apply (this); }
+  inline return_t dispatch (const T &obj) { return obj.apply (this); }
   static return_t default_return_value (void) { return false; }
   bool stop_sublookup_iteration (return_t r) const { return r; }
   return_t recurse (unsigned int lookup_index)
@@ -255,132 +260,255 @@ struct hb_apply_context_t
     return ret;
   }
 
+  unsigned int table_index; /* GSUB/GPOS */
   hb_font_t *font;
   hb_face_t *face;
   hb_buffer_t *buffer;
   hb_direction_t direction;
   hb_mask_t lookup_mask;
+  bool auto_zwj;
   recurse_func_t recurse_func;
   unsigned int nesting_level_left;
   unsigned int lookup_props;
-  unsigned int property; /* propety of first glyph */
   const GDEF &gdef;
   bool has_glyph_classes;
   unsigned int debug_depth;
 
 
-  hb_apply_context_t (hb_font_t *font_,
-                     hb_buffer_t *buffer_,
-                     hb_mask_t lookup_mask_) :
+  hb_apply_context_t (unsigned int table_index_,
+                     hb_font_t *font_,
+                     hb_buffer_t *buffer_) :
+                       table_index (table_index_),
                        font (font_), face (font->face), buffer (buffer_),
                        direction (buffer_->props.direction),
-                       lookup_mask (lookup_mask_),
+                       lookup_mask (1),
+                       auto_zwj (true),
                        recurse_func (NULL),
                        nesting_level_left (MAX_NESTING_LEVEL),
-                       lookup_props (0), property (0),
+                       lookup_props (0),
                        gdef (*hb_ot_layout_from_face (face)->gdef),
                        has_glyph_classes (gdef.has_glyph_classes ()),
                        debug_depth (0) {}
 
-  void set_recurse_func (recurse_func_t func) { recurse_func = func; }
-  void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
-  void set_lookup (const Lookup &l) { lookup_props = l.get_props (); }
+  inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
+  inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
+  inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
+  inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
+  inline void set_lookup (const Lookup &l) { lookup_props = l.get_props (); }
+
+  struct matcher_t
+  {
+    inline matcher_t (void) :
+            lookup_props (0),
+            ignore_zwnj (false),
+            ignore_zwj (false),
+            mask (-1),
+#define arg1(arg) (arg) /* Remove the macro to see why it's needed! */
+            syllable arg1(0),
+#undef arg1
+            match_func (NULL),
+            match_data (NULL) {};
+
+    typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
+
+    inline void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
+    inline void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
+    inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
+    inline void set_mask (hb_mask_t mask_) { mask = mask_; }
+    inline void set_syllable (uint8_t syllable_)  { syllable = syllable_; }
+    inline void set_match_func (match_func_t match_func_,
+                               const void *match_data_)
+    { match_func = match_func_; match_data = match_data_; }
+
+    enum may_match_t {
+      MATCH_NO,
+      MATCH_YES,
+      MATCH_MAYBE
+    };
 
-  struct mark_skipping_forward_iterator_t
-  {
-    inline mark_skipping_forward_iterator_t (hb_apply_context_t *c_,
-                                            unsigned int start_index_,
-                                            unsigned int num_items_,
-                                            bool context_match = false)
+    inline may_match_t may_match (const hb_glyph_info_t &info,
+                                 const USHORT          *glyph_data) const
     {
-      c = c_;
-      idx = start_index_;
-      num_items = num_items_;
-      mask = context_match ? -1 : c->lookup_mask;
-      syllable = context_match ? 0 : c->buffer->cur().syllable ();
-      end = c->buffer->len;
+      if (!(info.mask & mask) ||
+         (syllable && syllable != info.syllable ()))
+       return MATCH_NO;
+
+      if (match_func)
+        return match_func (info.codepoint, *glyph_data, match_data) ? MATCH_YES : MATCH_NO;
+
+      return MATCH_MAYBE;
     }
-    inline bool has_no_chance (void) const
+
+    enum may_skip_t {
+      SKIP_NO,
+      SKIP_YES,
+      SKIP_MAYBE
+    };
+
+    inline may_skip_t
+    may_skip (const hb_apply_context_t *c,
+             const hb_glyph_info_t    &info) const
     {
-      return unlikely (num_items && idx + num_items >= end);
+      if (!c->check_glyph_property (&info, lookup_props))
+       return SKIP_YES;
+
+      if (unlikely (_hb_glyph_info_is_default_ignorable (&info) &&
+                   (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
+                   (ignore_zwj || !_hb_glyph_info_is_zwj (&info)) &&
+                   !_hb_glyph_info_ligated (&info)))
+       return SKIP_MAYBE;
+
+      return SKIP_NO;
     }
-    inline void reject (void)
+
+    protected:
+    unsigned int lookup_props;
+    bool ignore_zwnj;
+    bool ignore_zwj;
+    hb_mask_t mask;
+    uint8_t syllable;
+    match_func_t match_func;
+    const void *match_data;
+  };
+
+  struct skipping_forward_iterator_t
+  {
+    inline skipping_forward_iterator_t (hb_apply_context_t *c_,
+                                       unsigned int start_index_,
+                                       unsigned int num_items_,
+                                       bool context_match = false) :
+                                        idx (start_index_),
+                                        c (c_),
+                                        match_glyph_data (NULL),
+                                        num_items (num_items_),
+                                        end (c->buffer->len)
     {
-      num_items++;
+      matcher.set_lookup_props (c->lookup_props);
+      /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
+      matcher.set_ignore_zwnj (context_match || c->table_index == 1);
+      /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
+      matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
+      if (!context_match)
+       matcher.set_mask (c->lookup_mask);
+      matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
     }
-    inline bool next (unsigned int *property_out,
-                     unsigned int  lookup_props)
+    inline void set_lookup_props (unsigned int lookup_props) { matcher.set_lookup_props (lookup_props); }
+    inline void set_syllable (unsigned int syllable) { matcher.set_syllable (syllable); }
+    inline void set_match_func (matcher_t::match_func_t match_func,
+                               const void *match_data,
+                               const USHORT glyph_data[])
+    {
+      matcher.set_match_func (match_func, match_data);
+      match_glyph_data = glyph_data;
+    }
+
+    inline bool has_no_chance (void) const { return unlikely (num_items && idx + num_items >= end); }
+    inline void reject (void) { num_items++; match_glyph_data--; }
+    inline bool next (void)
     {
       assert (num_items > 0);
-      do
+      while (!has_no_chance ())
       {
-       if (has_no_chance ())
-         return false;
        idx++;
-      } while (c->should_skip_mark (&c->buffer->info[idx], lookup_props, property_out));
-      num_items--;
-      return (c->buffer->info[idx].mask & mask) && (!syllable || syllable == c->buffer->info[idx].syllable ());
-    }
-    inline bool next (unsigned int *property_out = NULL)
-    {
-      return next (property_out, c->lookup_props);
+       const hb_glyph_info_t &info = c->buffer->info[idx];
+
+       matcher_t::may_skip_t skip = matcher.may_skip (c, info);
+       if (unlikely (skip == matcher_t::SKIP_YES))
+         continue;
+
+       matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
+       if (match == matcher_t::MATCH_YES ||
+           (match == matcher_t::MATCH_MAYBE &&
+            skip == matcher_t::SKIP_NO))
+       {
+         num_items--;
+         match_glyph_data++;
+         return true;
+       }
+
+       if (skip == matcher_t::SKIP_NO)
+         return false;
+      }
+      return false;
     }
 
     unsigned int idx;
     protected:
     hb_apply_context_t *c;
+    matcher_t matcher;
+    const USHORT *match_glyph_data;
+
     unsigned int num_items;
-    hb_mask_t mask;
-    uint8_t syllable;
     unsigned int end;
   };
 
-  struct mark_skipping_backward_iterator_t
+  struct skipping_backward_iterator_t
   {
-    inline mark_skipping_backward_iterator_t (hb_apply_context_t *c_,
-                                             unsigned int start_index_,
-                                             unsigned int num_items_,
-                                             hb_mask_t mask_ = 0,
-                                             bool match_syllable_ = true)
-    {
-      c = c_;
-      idx = start_index_;
-      num_items = num_items_;
-      mask = mask_ ? mask_ : c->lookup_mask;
-      syllable = match_syllable_ ? c->buffer->cur().syllable () : 0;
-    }
-    inline bool has_no_chance (void) const
+    inline skipping_backward_iterator_t (hb_apply_context_t *c_,
+                                        unsigned int start_index_,
+                                        unsigned int num_items_,
+                                        bool context_match = false) :
+                                         idx (start_index_),
+                                         c (c_),
+                                         match_glyph_data (NULL),
+                                         num_items (num_items_)
     {
-      return unlikely (idx < num_items);
+      matcher.set_lookup_props (c->lookup_props);
+      /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
+      matcher.set_ignore_zwnj (context_match || c->table_index == 1);
+      /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
+      matcher.set_ignore_zwj (context_match || c->table_index == 1 || c->auto_zwj);
+      if (!context_match)
+       matcher.set_mask (c->lookup_mask);
+      matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
     }
-    inline void reject (void)
+    inline void set_lookup_props (unsigned int lookup_props) { matcher.set_lookup_props (lookup_props); }
+    inline void set_syllable (unsigned int syllable) { matcher.set_syllable (syllable); }
+    inline void set_match_func (matcher_t::match_func_t match_func,
+                               const void *match_data,
+                               const USHORT glyph_data[])
     {
-      num_items++;
+      matcher.set_match_func (match_func, match_data);
+      match_glyph_data = glyph_data;
     }
-    inline bool prev (unsigned int *property_out,
-                     unsigned int  lookup_props)
+
+    inline bool has_no_chance (void) const { return unlikely (idx < num_items); }
+    inline void reject (void) { num_items++; }
+    inline bool prev (void)
     {
       assert (num_items > 0);
-      do
+      while (!has_no_chance ())
       {
-       if (has_no_chance ())
-         return false;
        idx--;
-      } while (c->should_skip_mark (&c->buffer->out_info[idx], lookup_props, property_out));
-      num_items--;
-      return (c->buffer->out_info[idx].mask & mask) && (!syllable || syllable == c->buffer->out_info[idx].syllable ());
-    }
-    inline bool prev (unsigned int *property_out = NULL)
-    {
-      return prev (property_out, c->lookup_props);
+       const hb_glyph_info_t &info = c->buffer->out_info[idx];
+
+       matcher_t::may_skip_t skip = matcher.may_skip (c, info);
+       if (unlikely (skip == matcher_t::SKIP_YES))
+         continue;
+
+       matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
+       if (match == matcher_t::MATCH_YES ||
+           (match == matcher_t::MATCH_MAYBE &&
+            skip == matcher_t::SKIP_NO))
+       {
+         num_items--;
+         match_glyph_data++;
+         return true;
+       }
+
+       if (skip == matcher_t::SKIP_NO)
+         return false;
+      }
+      return false;
     }
 
     unsigned int idx;
     protected:
     hb_apply_context_t *c;
+    matcher_t matcher;
+    const USHORT *match_glyph_data;
+
     unsigned int num_items;
-    hb_mask_t mask;
-    uint8_t syllable;
   };
 
   inline bool
@@ -405,10 +533,12 @@ struct hb_apply_context_t
   }
 
   inline bool
-  match_properties (hb_codepoint_t  glyph,
-                   unsigned int    glyph_props,
-                   unsigned int    lookup_props) const
+  check_glyph_property (const hb_glyph_info_t *info,
+                       unsigned int  lookup_props) const
   {
+    hb_codepoint_t glyph = info->codepoint;
+    unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);
+
     /* Not covered, if, for example, glyph class is ligature and
      * lookup_props includes LookupFlags::IgnoreLigatures
      */
@@ -421,69 +551,54 @@ struct hb_apply_context_t
     return true;
   }
 
-  inline bool
-  check_glyph_property (hb_glyph_info_t *info,
-                       unsigned int  lookup_props,
-                       unsigned int *property_out) const
-  {
-    unsigned int property;
-
-    property = info->glyph_props();
-    *property_out = property;
-
-    return match_properties (info->codepoint, property, lookup_props);
-  }
-
-  inline bool
-  should_skip_mark (hb_glyph_info_t *info,
-                  unsigned int  lookup_props,
-                  unsigned int *property_out) const
-  {
-    unsigned int property;
-
-    property = info->glyph_props();
-    if (property_out)
-      *property_out = property;
-
-    /* If it's a mark, skip it if we don't accept it. */
-    if (unlikely (property & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
-      return !match_properties (info->codepoint, property, lookup_props);
-
-    /* If not a mark, don't skip. */
-    return false;
-  }
-
-
-  inline bool should_mark_skip_current_glyph (void) const
-  {
-    return should_skip_mark (&buffer->cur(), lookup_props, NULL);
-  }
-
-  inline void set_class (hb_codepoint_t glyph_index, unsigned int class_guess) const
+  inline void _set_glyph_props (hb_codepoint_t glyph_index,
+                         unsigned int class_guess = 0,
+                         bool ligature = false,
+                         bool component = false) const
   {
+    unsigned int add_in = _hb_glyph_info_get_glyph_props (&buffer->cur()) &
+                         HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
+    add_in |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
+    if (ligature)
+    {
+      add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
+      /* In the only place that the MULTIPLIED bit is used, Uniscribe
+       * seems to only care about the "last" transformation between
+       * Ligature and Multiple substitions.  Ie. if you ligate, expand,
+       * and ligate again, it forgives the multiplication and acts as
+       * if only ligation happened.  As such, clear MULTIPLIED bit.
+       */
+      add_in &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
+    }
+    if (component)
+      add_in |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
     if (likely (has_glyph_classes))
-      buffer->cur().glyph_props() = gdef.get_glyph_props (glyph_index);
+      _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
     else if (class_guess)
-      buffer->cur().glyph_props() = class_guess;
+      _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
   }
 
-  inline void output_glyph (hb_codepoint_t glyph_index,
-                           unsigned int class_guess = 0) const
+  inline void replace_glyph (hb_codepoint_t glyph_index) const
   {
-    set_class (glyph_index, class_guess);
-    buffer->output_glyph (glyph_index);
+    _set_glyph_props (glyph_index);
+    buffer->replace_glyph (glyph_index);
+  }
+  inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
+  {
+    _set_glyph_props (glyph_index);
+    buffer->cur().codepoint = glyph_index;
   }
-  inline void replace_glyph (hb_codepoint_t glyph_index,
-                            unsigned int class_guess = 0) const
+  inline void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
+                                          unsigned int class_guess) const
   {
-    set_class (glyph_index, class_guess);
+    _set_glyph_props (glyph_index, class_guess, true);
     buffer->replace_glyph (glyph_index);
   }
-  inline void replace_glyph_inplace (hb_codepoint_t glyph_index,
-                                    unsigned int class_guess = 0) const
+  inline void output_glyph_for_component (hb_codepoint_t glyph_index,
+                                         unsigned int class_guess) const
   {
-    set_class (glyph_index, class_guess);
-    buffer->cur().codepoint = glyph_index;
+    _set_glyph_props (glyph_index, class_guess, false, true);
+    buffer->output_glyph (glyph_index);
   }
 };
 
@@ -596,13 +711,19 @@ static inline bool match_input (hb_apply_context_t *c,
                                const USHORT input[], /* Array of input values--start with second glyph */
                                match_func_t match_func,
                                const void *match_data,
-                               unsigned int *end_offset = NULL,
+                               unsigned int *end_offset,
+                               unsigned int match_positions[MAX_CONTEXT_LENGTH],
                                bool *p_is_mark_ligature = NULL,
                                unsigned int *p_total_component_count = NULL)
 {
   TRACE_APPLY (NULL);
 
-  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx, count - 1);
+  if (unlikely (count > MAX_CONTEXT_LENGTH)) TRACE_RETURN (false);
+
+  hb_buffer_t *buffer = c->buffer;
+
+  hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, count - 1);
+  skippy_iter.set_match_func (match_func, match_data, input);
   if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
 
   /*
@@ -623,24 +744,23 @@ static inline bool match_input (hb_apply_context_t *c,
    *   ligate with a conjunct...)
    */
 
-  bool is_mark_ligature = !!(c->property & HB_OT_LAYOUT_GLYPH_PROPS_MARK);
+  bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
 
   unsigned int total_component_count = 0;
-  total_component_count += get_lig_num_comps (c->buffer->cur());
+  total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur());
 
-  unsigned int first_lig_id = get_lig_id (c->buffer->cur());
-  unsigned int first_lig_comp = get_lig_comp (c->buffer->cur());
+  unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
+  unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
 
+  match_positions[0] = buffer->idx;
   for (unsigned int i = 1; i < count; i++)
   {
-    unsigned int property;
-
-    if (!skippy_iter.next (&property)) return TRACE_RETURN (false);
+    if (!skippy_iter.next ()) return TRACE_RETURN (false);
 
-    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, input[i - 1], match_data))) return TRACE_RETURN (false);
+    match_positions[i] = skippy_iter.idx;
 
-    unsigned int this_lig_id = get_lig_id (c->buffer->info[skippy_iter.idx]);
-    unsigned int this_lig_comp = get_lig_comp (c->buffer->info[skippy_iter.idx]);
+    unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]);
+    unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]);
 
     if (first_lig_id && first_lig_comp) {
       /* If first component was attached to a previous ligature component,
@@ -656,12 +776,11 @@ static inline bool match_input (hb_apply_context_t *c,
        return TRACE_RETURN (false);
     }
 
-    is_mark_ligature = is_mark_ligature && (property & HB_OT_LAYOUT_GLYPH_PROPS_MARK);
-    total_component_count += get_lig_num_comps (c->buffer->info[skippy_iter.idx]);
+    is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
+    total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
   }
 
-  if (end_offset)
-    *end_offset = skippy_iter.idx - c->buffer->idx + 1;
+  *end_offset = skippy_iter.idx - buffer->idx + 1;
 
   if (p_is_mark_ligature)
     *p_is_mark_ligature = is_mark_ligature;
@@ -672,14 +791,19 @@ static inline bool match_input (hb_apply_context_t *c,
   return TRACE_RETURN (true);
 }
 static inline void ligate_input (hb_apply_context_t *c,
-                                unsigned int count, /* Including the first glyph (not matched) */
-                                const USHORT input[] HB_UNUSED, /* Array of input values--start with second glyph */
+                                unsigned int count, /* Including the first glyph */
+                                unsigned int match_positions[MAX_CONTEXT_LENGTH], /* Including the first glyph */
+                                unsigned int match_length,
                                 hb_codepoint_t lig_glyph,
-                                match_func_t match_func HB_UNUSED,
-                                const void *match_data HB_UNUSED,
                                 bool is_mark_ligature,
                                 unsigned int total_component_count)
 {
+  TRACE_APPLY (NULL);
+
+  hb_buffer_t *buffer = c->buffer;
+
+  buffer->merge_clusters (buffer->idx, buffer->idx + match_length);
+
   /*
    * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
    *   the ligature to keep its old ligature id.  This will allow it to attach to
@@ -709,46 +833,54 @@ static inline void ligate_input (hb_apply_context_t *c,
    */
 
   unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
-  unsigned int lig_id = is_mark_ligature ? 0 : allocate_lig_id (c->buffer);
-  unsigned int last_lig_id = get_lig_id (c->buffer->cur());
-  unsigned int last_num_components = get_lig_num_comps (c->buffer->cur());
+  unsigned int lig_id = is_mark_ligature ? 0 : _hb_allocate_lig_id (buffer);
+  unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
+  unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
   unsigned int components_so_far = last_num_components;
 
   if (!is_mark_ligature)
-    set_lig_props_for_ligature (c->buffer->cur(), lig_id, total_component_count);
-  c->replace_glyph (lig_glyph, klass);
+  {
+    _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
+    if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
+    {
+      _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
+      _hb_glyph_info_set_modified_combining_class (&buffer->cur(), 0);
+    }
+  }
+  c->replace_glyph_with_ligature (lig_glyph, klass);
 
   for (unsigned int i = 1; i < count; i++)
   {
-    while (c->should_mark_skip_current_glyph ())
+    while (buffer->idx < match_positions[i])
     {
       if (!is_mark_ligature) {
        unsigned int new_lig_comp = components_so_far - last_num_components +
-                                   MIN (MAX (get_lig_comp (c->buffer->cur()), 1u), last_num_components);
-       set_lig_props_for_mark (c->buffer->cur(), lig_id, new_lig_comp);
+                                   MIN (MAX (_hb_glyph_info_get_lig_comp (&buffer->cur()), 1u), last_num_components);
+       _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
       }
-      c->buffer->next_glyph ();
+      buffer->next_glyph ();
     }
 
-    last_lig_id = get_lig_id (c->buffer->cur());
-    last_num_components = get_lig_num_comps (c->buffer->cur());
+    last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
+    last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
     components_so_far += last_num_components;
 
     /* Skip the base glyph */
-    c->buffer->idx++;
+    buffer->idx++;
   }
 
   if (!is_mark_ligature && last_lig_id) {
     /* Re-adjust components for any marks following. */
-    for (unsigned int i = c->buffer->idx; i < c->buffer->len; i++) {
-      if (last_lig_id == get_lig_id (c->buffer->info[i])) {
+    for (unsigned int i = buffer->idx; i < buffer->len; i++) {
+      if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
        unsigned int new_lig_comp = components_so_far - last_num_components +
-                                   MIN (MAX (get_lig_comp (c->buffer->info[i]), 1u), last_num_components);
-       set_lig_props_for_mark (c->buffer->info[i], lig_id, new_lig_comp);
+                                   MIN (MAX (_hb_glyph_info_get_lig_comp (&buffer->info[i]), 1u), last_num_components);
+       _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
       } else
        break;
     }
   }
+  TRACE_RETURN (true);
 }
 
 static inline bool match_backtrack (hb_apply_context_t *c,
@@ -759,19 +891,14 @@ static inline bool match_backtrack (hb_apply_context_t *c,
 {
   TRACE_APPLY (NULL);
 
-  hb_apply_context_t::mark_skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
-  if (skippy_iter.has_no_chance ())
-    return TRACE_RETURN (false);
+  hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
+  skippy_iter.set_match_func (match_func, match_data, backtrack);
+  if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
 
   for (unsigned int i = 0; i < count; i++)
-  {
     if (!skippy_iter.prev ())
       return TRACE_RETURN (false);
 
-    if (likely (!match_func (c->buffer->out_info[skippy_iter.idx].codepoint, backtrack[i], match_data)))
-      return TRACE_RETURN (false);
-  }
-
   return TRACE_RETURN (true);
 }
 
@@ -784,19 +911,14 @@ static inline bool match_lookahead (hb_apply_context_t *c,
 {
   TRACE_APPLY (NULL);
 
-  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
-  if (skippy_iter.has_no_chance ())
-    return TRACE_RETURN (false);
+  hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
+  skippy_iter.set_match_func (match_func, match_data, lookahead);
+  if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
 
   for (unsigned int i = 0; i < count; i++)
-  {
     if (!skippy_iter.next ())
       return TRACE_RETURN (false);
 
-    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, lookahead[i], match_data)))
-      return TRACE_RETURN (false);
-  }
-
   return TRACE_RETURN (true);
 }
 
@@ -824,65 +946,89 @@ static inline void recurse_lookups (context_t *c,
                                    const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
 {
   for (unsigned int i = 0; i < lookupCount; i++)
-    c->recurse (lookupRecord->lookupListIndex);
+    c->recurse (lookupRecord[i].lookupListIndex);
 }
 
 static inline bool apply_lookup (hb_apply_context_t *c,
                                 unsigned int count, /* Including the first glyph */
+                                unsigned int match_positions[MAX_CONTEXT_LENGTH], /* Including the first glyph */
                                 unsigned int lookupCount,
-                                const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
+                                const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
+                                unsigned int match_length)
 {
   TRACE_APPLY (NULL);
 
-  unsigned int end = c->buffer->len;
-  if (unlikely (count == 0 || c->buffer->idx + count > end))
-    return TRACE_RETURN (false);
+  hb_buffer_t *buffer = c->buffer;
+  unsigned int end;
 
-  /* TODO We don't support lookupRecord arrays that are not increasing:
-   *      Should be easy for in_place ones at least. */
+  /* All positions are distance from beginning of *output* buffer.
+   * Adjust. */
+  {
+    unsigned int bl = buffer->backtrack_len ();
+    end = bl + match_length;
 
-  /* Note: If sublookup is reverse, it will underflow after the first loop
-   * and we jump out of it.  Not entirely disastrous.  So we don't check
-   * for reverse lookup here.
-   */
-  for (unsigned int i = 0; i < count; /* NOP */)
+    int delta = bl - buffer->idx;
+    /* Convert positions to new indexing. */
+    for (unsigned int j = 0; j < count; j++)
+      match_positions[j] += delta;
+  }
+
+  for (unsigned int i = 0; i < lookupCount; i++)
   {
-    if (unlikely (c->buffer->idx == end))
-      return TRACE_RETURN (true);
-    while (c->should_mark_skip_current_glyph ())
-    {
-      /* No lookup applied for this index */
-      c->buffer->next_glyph ();
-      if (unlikely (c->buffer->idx == end))
-       return TRACE_RETURN (true);
-    }
+    unsigned int idx = lookupRecord[i].sequenceIndex;
+    if (idx >= count)
+      continue;
 
-    if (lookupCount && i == lookupRecord->sequenceIndex)
-    {
-      unsigned int old_pos = c->buffer->idx;
+    buffer->move_to (match_positions[idx]);
+
+    unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
+    if (!c->recurse (lookupRecord[i].lookupListIndex))
+      continue;
+
+    unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
+    int delta = new_len - orig_len;
+
+    if (!delta)
+        continue;
+
+    /* Recursed lookup changed buffer len.  Adjust. */
 
-      /* Apply a lookup */
-      bool done = c->recurse (lookupRecord->lookupListIndex);
+    /* end can't go back past the current match position.
+     * Note: this is only true because we do NOT allow MultipleSubst
+     * with zero sequence len. */
+    end = MAX ((int) match_positions[idx] + 1, int (end) + delta);
 
-      lookupRecord++;
-      lookupCount--;
-      /* Err, this is wrong if the lookup jumped over some glyphs */
-      i += c->buffer->idx - old_pos;
-      if (unlikely (c->buffer->idx == end))
-       return TRACE_RETURN (true);
+    unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */
 
-      if (!done)
-       goto not_applied;
+    if (delta > 0)
+    {
+      if (unlikely (delta + count > MAX_CONTEXT_LENGTH))
+       break;
     }
     else
     {
-    not_applied:
-      /* No lookup applied for this index */
-      c->buffer->next_glyph ();
-      i++;
+      /* NOTE: delta is negative. */
+      delta = MAX (delta, (int) next - (int) count);
+      next -= delta;
     }
+
+    /* Shift! */
+    memmove (match_positions + next + delta, match_positions + next,
+            (count - next) * sizeof (match_positions[0]));
+    next += delta;
+    count += delta;
+
+    /* Fill in new entries. */
+    for (unsigned int j = idx + 1; j < next; j++)
+      match_positions[j] = match_positions[j - 1] + 1;
+
+    /* And fixup the rest. */
+    for (; next < count; next++)
+      match_positions[next] += delta;
   }
 
+  buffer->move_to (end);
+
   return TRACE_RETURN (true);
 }
 
@@ -954,12 +1100,16 @@ static inline bool context_apply_lookup (hb_apply_context_t *c,
                                         const LookupRecord lookupRecord[],
                                         ContextApplyLookupContext &lookup_context)
 {
+  unsigned int match_length = 0;
+  unsigned int match_positions[MAX_CONTEXT_LENGTH];
   return match_input (c,
                      inputCount, input,
-                     lookup_context.funcs.match, lookup_context.match_data)
+                     lookup_context.funcs.match, lookup_context.match_data,
+                     &match_length, match_positions)
       && apply_lookup (c,
-                      inputCount,
-                      lookupCount, lookupRecord);
+                      inputCount, match_positions,
+                      lookupCount, lookupRecord,
+                      match_length);
 }
 
 struct Rule
@@ -1353,13 +1503,13 @@ struct ContextFormat3
 struct Context
 {
   template <typename context_t>
-  inline typename context_t::return_t process (context_t *c) const
+  inline typename context_t::return_t dispatch (context_t *c) const
   {
-    TRACE_PROCESS (this);
+    TRACE_DISPATCH (this);
     switch (u.format) {
-    case 1: return TRACE_RETURN (c->process (u.format1));
-    case 2: return TRACE_RETURN (c->process (u.format2));
-    case 3: return TRACE_RETURN (c->process (u.format3));
+    case 1: return TRACE_RETURN (c->dispatch (u.format1));
+    case 2: return TRACE_RETURN (c->dispatch (u.format2));
+    case 3: return TRACE_RETURN (c->dispatch (u.format3));
     default:return TRACE_RETURN (c->default_return_value ());
     }
   }
@@ -1422,7 +1572,7 @@ static inline void chain_context_closure_lookup (hb_closure_context_t *c,
    && intersects_array (c,
                        inputCount ? inputCount - 1 : 0, input,
                        lookup_context.funcs.intersects, lookup_context.intersects_data[1])
-  && intersects_array (c,
+   && intersects_array (c,
                       lookaheadCount, lookahead,
                       lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
     recurse_lookups (c,
@@ -1481,21 +1631,23 @@ static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
                                               const LookupRecord lookupRecord[],
                                               ChainContextApplyLookupContext &lookup_context)
 {
-  unsigned int lookahead_offset = 0;
+  unsigned int match_length = 0;
+  unsigned int match_positions[MAX_CONTEXT_LENGTH];
   return match_input (c,
                      inputCount, input,
                      lookup_context.funcs.match, lookup_context.match_data[1],
-                     &lookahead_offset)
+                     &match_length, match_positions)
       && match_backtrack (c,
                          backtrackCount, backtrack,
                          lookup_context.funcs.match, lookup_context.match_data[0])
       && match_lookahead (c,
                          lookaheadCount, lookahead,
                          lookup_context.funcs.match, lookup_context.match_data[2],
-                         lookahead_offset)
+                         match_length)
       && apply_lookup (c,
-                      inputCount,
-                      lookupCount, lookupRecord);
+                      inputCount, match_positions,
+                      lookupCount, lookupRecord,
+                      match_length);
 }
 
 struct ChainRule
@@ -1968,13 +2120,13 @@ struct ChainContextFormat3
 struct ChainContext
 {
   template <typename context_t>
-  inline typename context_t::return_t process (context_t *c) const
+  inline typename context_t::return_t dispatch (context_t *c) const
   {
-    TRACE_PROCESS (this);
+    TRACE_DISPATCH (this);
     switch (u.format) {
-    case 1: return TRACE_RETURN (c->process (u.format1));
-    case 2: return TRACE_RETURN (c->process (u.format2));
-    case 3: return TRACE_RETURN (c->process (u.format3));
+    case 1: return TRACE_RETURN (c->dispatch (u.format1));
+    case 2: return TRACE_RETURN (c->dispatch (u.format2));
+    case 3: return TRACE_RETURN (c->dispatch (u.format3));
     default:return TRACE_RETURN (c->default_return_value ());
     }
   }
@@ -2048,9 +2200,9 @@ struct Extension
   }
 
   template <typename context_t>
-  inline typename context_t::return_t process (context_t *c) const
+  inline typename context_t::return_t dispatch (context_t *c) const
   {
-    return get_subtable<typename T::LookupSubTable> ().process (c, get_type ());
+    return get_subtable<typename T::LookupSubTable> ().dispatch (c, get_type ());
   }
 
   inline bool sanitize_self (hb_sanitize_context_t *c) {
@@ -2102,8 +2254,8 @@ struct GSUBGPOS
 
   inline unsigned int get_feature_count (void) const
   { return (this+featureList).len; }
-  inline const Tag& get_feature_tag (unsigned int i) const
-  { return (this+featureList).get_tag (i); }
+  inline hb_tag_t get_feature_tag (unsigned int i) const
+  { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : (this+featureList).get_tag (i); }
   inline unsigned int get_feature_tags (unsigned int start_offset,
                                        unsigned int *feature_count /* IN/OUT */,
                                        hb_tag_t     *feature_tags /* OUT */) const
@@ -2128,7 +2280,7 @@ struct GSUBGPOS
 
   protected:
   FixedVersion version;        /* Version of the GSUB/GPOS table--initially set
-                                * to 0x00010000 */
+                                * to 0x00010000u */
   OffsetTo<ScriptList>
                scriptList;     /* ScriptList table */
   OffsetTo<FeatureList>