(match_ids): New arg "direction". Callers changed.
[m17n/libotf.git] / src / otfdrive.c
index d22e658..44cca1c 100644 (file)
@@ -190,7 +190,7 @@ get_langsys (OTF_ScriptList *script_list,
          for (j = 0; j < script->LangSysCount; j++)
            if (script->LangSysRecord[j].LangSysTag == langsys_tag)
              return script->LangSys + j;
-         return &script->DefaultLangSys;       
+         return &script->DefaultLangSys;
        }
     }
 
@@ -201,7 +201,7 @@ get_langsys (OTF_ScriptList *script_list,
   for (j = 0; j < dflt->LangSysCount; j++)
     if (dflt->LangSysRecord[j].LangSysTag == langsys_tag)
       return dflt->LangSys + j;
-  return &dflt->DefaultLangSys;        
+  return &dflt->DefaultLangSys;
 }
 
 static int
@@ -228,7 +228,7 @@ setup_lookup_flags (OTF_LookupList *LookupList, OTF_FeatureList *FeatureList,
       if (*features == '*')
        {
          /* Consume all remaining features.  */
-         for (i = 0; i < LangSys->FeatureCount; i++) 
+         for (i = 0; i < LangSys->FeatureCount; i++)
            {
              int index = LangSys->FeatureIndex[i];
 
@@ -272,32 +272,17 @@ setup_lookup_flags (OTF_LookupList *LookupList, OTF_FeatureList *FeatureList,
 
 static int
 match_ids (OTF_GlyphString *gstring, int gidx, int flag,
-          int count, OTF_GlyphID *ids)
+          int count, OTF_GlyphID *ids, int direction)
 {
-  OTF_Glyph *gbeg = gstring->glyphs + gidx;
-  OTF_Glyph *gend = gstring->glyphs + gstring->used;
-  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 : g - gbeg);
-}
-
-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;
+  OTF_Glyph *g = gstring->glyphs + gidx;
+  OTF_Glyph *gend = gstring->glyphs + (direction > 0 ? gstring->used : -1);
+  int i, j;
 
-  for (g = gbeg, i = 0; g >= gend && i < count; g--)
-    if (! IGNORED_GLYPH (g, flag) && g->glyph_id != ids[i++])
+  for (i = j = 0; i < count && g != gend; g += direction)
+    if (! IGNORED_GLYPH (g, flag)
+       && g->glyph_id != ids[i++])
       return -1;
-  return (i < count ? -1 : gbeg - g);
+  return (i < count ? -1 : j);
 }
 
 static int
@@ -306,29 +291,18 @@ match_chain_ids (OTF_GlyphString *gstring, int gidx, int flag,
 {
   int i = rule->BacktrackGlyphCount;
 
-  if (i > 0)
-    {
-      int j;
-      OTF_Glyph *g;
-
-      for (j = gidx - 1, g = gstring->glyphs + j; j >= 0; j--, g--)
-       if (! IGNORED_GLYPH (g, flag) && --i == 0)
-         break;
-      if (i > 0)
-       return -1;
-      if (match_ids_backward (gstring, j, flag,
-                    rule->BacktrackGlyphCount, rule->Backtrack)
-         < 0)
-       return -1;
-    }
+  if (i > 0
+      && (gidx < i
+         || match_ids (gstring, gidx - 1, flag, i, rule->Backtrack, -1) < 0))
+    return -1;
   gidx++;
   i = match_ids (gstring, gidx, flag,
-                rule->InputGlyphCount - 1, rule->Input);
+                rule->InputGlyphCount - 1, rule->Input, 1);
   if (i < 0)
     return -1;
   gidx += i;
   i = match_ids (gstring, gidx, flag,
-                rule->LookaheadGlyphCount, rule->LookAhead);
+                rule->LookaheadGlyphCount, rule->LookAhead, 1);
   if (i < 0)
     return -1;
   return 0;
@@ -336,18 +310,17 @@ match_chain_ids (OTF_GlyphString *gstring, int gidx, int flag,
 
 static int
 match_classes (OTF_ClassDef *class_def, OTF_GlyphString *gstring, int gidx,
-              int flag, int count, unsigned *classes)
+              int flag, int count, unsigned *classes, int direction)
 {
-  OTF_Glyph *gbeg = gstring->glyphs + gidx;
-  OTF_Glyph *gend = gstring->glyphs + gstring->used;
-  OTF_Glyph *g;
-  int i;
+  OTF_Glyph *g = gstring->glyphs + gidx;
+  OTF_Glyph *gend = gstring->glyphs + (direction > 0 ? gstring->used : -1);
+  int i, j;
 
-  for (g = gbeg, i = 0; g < gend && i < count; g++)
+  for (i = j = 0; i < count && g != gend; g += direction)
     if (! IGNORED_GLYPH (g, flag)
        && get_class_def (class_def, g->glyph_id) != classes[i++])
       return -1;
-  return (i < count ? -1 : g - gbeg);
+  return (i < count ? -1 : j);
 }
 
 static int
@@ -359,28 +332,19 @@ match_chain_classes (OTF_GlyphString *gstring, int gidx, int flag,
 {
   int i = rule->BacktrackGlyphCount;
 
-  if (i > 0)
-    {
-      int j;
-      OTF_Glyph *g;
-
-      for (j = gidx - 1, g = gstring->glyphs + j; j >= 0; j--, g--)
-       if (! IGNORED_GLYPH (g, flag) && i-- == 0)
-         break;
-      if (i > 0)
-       return -1;
-      if (match_classes (BacktrackClassDef, gstring, j, flag,
-                        rule->BacktrackGlyphCount, rule->Backtrack) < 0);
-      return -1;
-    }
+  if (i > 0
+      && (gidx < i
+         || match_classes (BacktrackClassDef, gstring, gidx - 1, flag, i,
+                           rule->Backtrack, -1) < 0))
+    return -1;
   gidx++;
   i = match_classes (InputClassDef, gstring, gidx, flag,
-                    rule->InputGlyphCount - 1, rule->Input);
+                    rule->InputGlyphCount - 1, rule->Input, 1);
   if (i < 0)
     return -1;
   gidx += i;
   i = match_classes (LookaheadClassDef, gstring, gidx, flag,
-                    rule->LookaheadGlyphCount, rule->LookAhead);
+                    rule->LookaheadGlyphCount, rule->LookAhead, 1);
   if (i < 0)
     return -1;
   return 0;
@@ -389,18 +353,17 @@ match_chain_classes (OTF_GlyphString *gstring, int gidx, int flag,
 
 static int
 match_coverages (OTF_GlyphString *gstring, int gidx, int flag, int count,
-                OTF_Coverage *coverages)
+                OTF_Coverage *coverages, int direction)
 {
-  OTF_Glyph *gbeg = gstring->glyphs + gidx;
-  OTF_Glyph *gend = gstring->glyphs + gstring->used;
-  OTF_Glyph *g;
-  int i;
+  OTF_Glyph *g = gstring->glyphs + gidx;
+  OTF_Glyph *gend = gstring->glyphs + (direction > 0 ? gstring->used : - 1);
+  int i, j;
 
-  for (g = gbeg, i = 0; g < gend && i < count; g++)
+  for (i = j = 0; i < count && g != gend; g += direction, j++)
     if (! IGNORED_GLYPH (g, flag)
        && get_coverage_index (coverages + i++, g->glyph_id) < 0)
       return -1;
-  return (i < count ? -1 : g - gbeg);
+  return (i < count ? -1 : j);
 }
 
 static int
@@ -409,31 +372,22 @@ match_chain_coverages (OTF_GlyphString *gstring, int gidx, int flag,
 {
   int i = context3->BacktrackGlyphCount;
 
-  if (i > 0)
-    {
-      int j;
-      OTF_Glyph *g;
-
-      for (j = gidx - 1, g= gstring->glyphs +j; j >= 0; j--, g--)
-       if (! IGNORED_GLYPH (g, flag) && --i == 0)
-         break;
-      if (i > 0)
-       return -1;
-      if (match_coverages (gstring, j, flag, context3->BacktrackGlyphCount,
-                          context3->Backtrack) < 0)
-       return -1;
-    }
+  if (i > 0
+      && (gidx < i
+         || match_coverages (gstring, gidx - 1, flag, i,
+                             context3->Backtrack, -1) < 0))
+    return -1;
   gidx++;
   if (context3->InputGlyphCount > 1)
     {
       i = match_coverages (gstring, gidx, flag, context3->InputGlyphCount - 1,
-                          context3->Input + 1);
+                          context3->Input + 1, 1);
       if (i < 0)
        return -1;
       gidx += i;
     }
   if (match_coverages (gstring, gidx, flag, context3->LookaheadGlyphCount,
-                      context3->LookAhead) < 0)
+                      context3->LookAhead, 1) < 0)
     return -1;
   return 0;
 }
@@ -534,7 +488,7 @@ lookup_gsub (OTF *otf, OTF_LookupList *lookup_list, unsigned lookup_list_index,
 
                  lig = ligset->Ligature + j;
                  n = match_ids (gstring, gidx + 1, flag,
-                                lig->CompCount - 1, lig->Component);
+                                lig->CompCount - 1, lig->Component, 1);
                  if (n < 0)
                    continue;
                  gstring_subst (otf, gstring, gidx, gidx + 1 + n, flag,
@@ -560,7 +514,7 @@ lookup_gsub (OTF *otf, OTF_LookupList *lookup_list, unsigned lookup_list_index,
                {
                  rule = set->Rule + j;
                  if (match_ids (gstring, gidx + 1, flag,
-                                rule->GlyphCount - 1, rule->Input) < 0)
+                                rule->GlyphCount - 1, rule->Input, 1) < 0)
                    continue;
                  orig_used = gstring->used;
                  for (k = 0; k < rule->LookupCount; k++)
@@ -590,7 +544,7 @@ lookup_gsub (OTF *otf, OTF_LookupList *lookup_list, unsigned lookup_list_index,
                    rule = set->ClassRule + j;
                    if (match_classes (&context2->ClassDef,
                                       gstring, gidx + 1, flag,
-                                      rule->GlyphCount - 1, rule->Class)
+                                      rule->GlyphCount - 1, rule->Class, 1)
                        < 0)
                      continue;
                    orig_used = gstring->used;
@@ -612,7 +566,7 @@ lookup_gsub (OTF *otf, OTF_LookupList *lookup_list, unsigned lookup_list_index,
 
              if (match_coverages (gstring, gidx + 1, flag,
                                   context3->GlyphCount - 1,
-                                  context3->Coverage + 1) < 0)
+                                  context3->Coverage + 1, 1) < 0)
                continue;
              orig_used = gstring->used;
              for (j = 0; j < context3->LookupCount; j++)
@@ -632,7 +586,7 @@ lookup_gsub (OTF *otf, OTF_LookupList *lookup_list, unsigned lookup_list_index,
              OTF_ChainRuleSet *set = context1->ChainRuleSet + coverage_idx;
              int orig_used;
              int j, k;
-             
+
              for (j = 0; j < set->ChainRuleCount; j++)
                {
                  OTF_ChainRule *rule = set->ChainRule + j;
@@ -901,7 +855,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
            }
          if (accumulate && g->positioning_type)
            {
-             gidx = gstring_insert_for_gpos (gstring, gidx);         
+             gidx = gstring_insert_for_gpos (gstring, gidx);
              g = gstring->glyphs + gidx;
            }
          g->positioning_type = positioning_type;
@@ -1011,7 +965,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
        case 3:
          {
            OTF_GPOS_Cursive1 *cursive1 = &subtable->u.cursive1;
-         
+
            g->positioning_type = lookup_type;
            g->f.f3.entry_anchor
              = &cursive1->EntryExitRecord[coverage_idx].EntryAnchor;
@@ -1070,7 +1024,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              OTF_LigatureAttach *attach;
              int *num_class = alloca (sizeof (int) * mark_lig1->ClassCount);
              int j;
-                                      
+
              for (j = 0; j < mark_lig1->ClassCount; j++)
                num_class[j] = 0;
 
@@ -1161,7 +1115,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                {
                  rule = set->Rule + j;
                  if (match_ids (gstring, gidx + 1, flag,
-                                rule->GlyphCount - 1, rule->Input) < 0)
+                                rule->GlyphCount - 1, rule->Input, 1) < 0)
                    continue;
                  orig_used = gstring->used;
                  for (k = 0; k < rule->LookupCount; k++)
@@ -1191,7 +1145,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                    rule = set->ClassRule + j;
                    if (match_classes (&context2->ClassDef,
                                       gstring, gidx + 1, flag,
-                                      rule->GlyphCount - 1, rule->Class)
+                                      rule->GlyphCount - 1, rule->Class, 1)
                        < 0)
                      continue;
                    orig_used = gstring->used;
@@ -1213,7 +1167,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
 
              if (match_coverages (gstring, gidx + 1, flag,
                                   context3->GlyphCount - 1,
-                                  context3->Coverage + 1) < 0)
+                                  context3->Coverage + 1, 1) < 0)
                continue;
              orig_used = gstring->used;
              for (j = 0; j < context3->LookupCount; j++)
@@ -1233,7 +1187,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              OTF_ChainRuleSet *set = context1->ChainRuleSet + coverage_idx;
              int orig_used;
              int j, k;
-             
+
              for (j = 0; j < set->ChainRuleCount; j++)
                {
                  OTF_ChainRule *rule = set->ChainRule + j;
@@ -1500,7 +1454,7 @@ get_uvs_glyph (OTF_cmap *cmap, OTF_EncodingSubtable14 *sub14, int c1, int c2)
 
 static void
 check_cmap_uvs (OTF_cmap *cmap, OTF_GlyphString *gstring, int idx)
-{  
+{
   OTF_EncodingSubtable14 *sub14;
   int c1 = gstring->glyphs[idx - 1].c;
   int c2 = gstring->glyphs[idx].c;
@@ -1920,7 +1874,7 @@ OTF_drive_gsub_alternate (OTF *otf, OTF_GlyphString *gstring,
 }
 
 static int
-iterate_coverage (OTF *otf, const char *feature, 
+iterate_coverage (OTF *otf, const char *feature,
                  OTF_Feature_Callback callback,
                  OTF_Coverage *coverage)
 {