}
 
 static int
+match_ids_backward (OTF_GlyphString *gstring, int gidx, int flag,
+                   int count, OTF_GlyphID *ids)
+{
+  OTF_Glyph *gbeg = gstring->glyphs + gidx;
+  OTF_Glyph *gend = gstring->glyphs;
+  OTF_Glyph *g;
+  int i;
+
+  for (g = gbeg, i = 0; g >= gend && i < count; g--)
+    if (! IGNORED_GLYPH (g, flag) && g->glyph_id != ids[i++])
+      return -1;
+  return (i < count ? -1 : gbeg - g);
+}
+
+static int
 match_chain_ids (OTF_GlyphString *gstring, int gidx, int flag,
                 OTF_ChainRule *rule)
 {
          break;
       if (i > 0)
        return -1;
-      if (match_ids (gstring, j, flag,
+      if (match_ids_backward (gstring, j, flag,
                     rule->BacktrackGlyphCount, rule->Backtrack)
          < 0)
        return -1;