(OTF_check_features): Fix indexing to
[m17n/libotf.git] / src / otfdrive.c
index cd50897..51053b3 100644 (file)
@@ -29,6 +29,15 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
 #include "otf.h"
 #include "otferror.h"
 
+/* Return nonzero if OTF_Glyph *G should be ignored according to
+   LookupFlag FLAG.  */
+#define IGNORED_GLYPH(g, flag)                 \
+  ((g)->glyph_id == 0                          \
+   || ((flag) & (1 << (g)->GlyphClass))                \
+   || (((flag) & OTF_MarkAttachmentType)       \
+       && (g)->GlyphClass == OTF_GlyphClassMark        \
+       && ((flag) >> 8) != (g)->MarkAttachClass))
+
 #define GSTRING_DELETE(gstring, from, len)                             \
   do {                                                                 \
     memmove (gstring->glyphs + from, gstring->glyphs + from + len,     \
@@ -44,8 +53,9 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
        char *errfmt = "GSTRING%s";                                     \
                                                                        \
        gstring->size = gstring->used + len;                            \
-       gstring->glyphs = (OTF_Glyph *) realloc (gstring->glyphs,       \
-                                                gstring->size);        \
+       gstring->glyphs                                                 \
+         = (OTF_Glyph *) realloc (gstring->glyphs,                     \
+                                  sizeof (OTF_Glyph) * gstring->size); \
        if (! gstring->glyphs)                                          \
          OTF_ERROR (OTF_ERROR_MEMORY, "");                             \
       }                                                                        \
@@ -56,27 +66,30 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
 
 
 static int
-gstring_subst (OTF_GlyphString *gstring, int from, int to, int flag,
+gstring_subst (OTF *otf, OTF_GlyphString *gstring, int from, int to, int flag,
               OTF_GlyphID *ids, int num)
 {
   int errret = -1;
   int len = to - from;
   int i;
-  int c = gstring->glyphs[from].c;
   int from_idx = gstring->glyphs[from].f.index.from;
   int to_idx = gstring->glyphs[to - 1].f.index.to;
   int non_ignored_idx;
 
   for (i = non_ignored_idx = to - 1; i >= from; i--)
-    if (flag & (1 << gstring->glyphs[i].GlyphClass))
-      {
-       OTF_Glyph temp = gstring->glyphs[i];
+    {
+      OTF_Glyph *g = gstring->glyphs + i;
+      if (IGNORED_GLYPH (g, flag))
+       {
+         OTF_Glyph temp = *g;
 
-       memmove (gstring->glyphs + i, gstring->glyphs + i + 1,
-                sizeof (OTF_Glyph) * (non_ignored_idx - i));
-       gstring->glyphs[non_ignored_idx--] = temp;
-       len--;
-      }
+         memmove (g, g + 1, sizeof (OTF_Glyph) * (non_ignored_idx - i));
+         temp.f.index.from = from_idx;
+         temp.f.index.to = to_idx;
+         gstring->glyphs[non_ignored_idx--] = temp;
+         len--;
+       }
+    }
 
   if (len < num)
     GSTRING_INSERT (gstring, from, (num - len));
@@ -84,7 +97,7 @@ gstring_subst (OTF_GlyphString *gstring, int from, int to, int flag,
     GSTRING_DELETE (gstring, from, (len - num));
   for (i = 0; i < num; i++)
     {
-      gstring->glyphs[from + i].c = c;
+      gstring->glyphs[from + i].c = otf->cmap->decode_table[ids[i]];
       gstring->glyphs[from + i].glyph_id = ids[i];
       gstring->glyphs[from + i].f.index.from = from_idx;
       gstring->glyphs[from + i].f.index.to = to_idx;
@@ -189,33 +202,23 @@ setup_lookup_indices (OTF_LookupList *LookupList, OTF_FeatureList *FeatureList,
     {
       char tagname[4];
       OTF_Tag tag;
-      int negate = 0;
+      int use_it = 1;
 
       if (*features == '*')
        {
          /* Consume all remaining features.  */
-         /* We are sure that the last LookupCount elements of
-            lookup_indices are free to be used for this work.  */
-         int *free_table = (lookup_indices + (LookupList->LookupCount
-                                              * FeatureList->FeatureCount));
-
-         for (i = 0; i < LookupList->LookupCount; i++)
-           free_table[i] = 0;
          for (i = 0; i < FeatureList->FeatureCount; i++)
            if (! feature_table[i])
              {
                feature = FeatureList->Feature + i;
                for (j = 0; j < feature->LookupCount; j++)
-                 free_table[feature->LookupListIndex[j]] = 1;
+                 lookup_indices[n++] = feature->LookupListIndex[j];
              }
-         for (i = 0; i < LookupList->LookupCount; i++)
-           if (free_table[i])
-             lookup_indices[n++] = i;
          break;
        }
 
       if (*features == '~')
-       negate = 1, features++;
+       use_it = -1, features++;
       for (i = 0; *features && *features != ','; i++, features++)
        tagname[i] = *features;
       if (*features)
@@ -229,10 +232,12 @@ setup_lookup_indices (OTF_LookupList *LookupList, OTF_FeatureList *FeatureList,
          feature = FeatureList->Feature + i;
          if (tag == feature->FeatureTag)
            {
-             if (! negate)
+             if (feature_table[i])
+               break;
+             if (use_it > 0)
                for (j = 0; j < feature->LookupCount; j++)
                  lookup_indices[n++] = feature->LookupListIndex[j];
-             feature_table[i] = 1;
+             feature_table[i] = use_it;
              break;
            }
        }
@@ -251,8 +256,7 @@ match_ids (OTF_GlyphString *gstring, int gidx, int flag,
   int i;
 
   for (g = gbeg, i = 0; g < gend && i < count; g++)
-    if (g->glyph_id && ! (flag & (1 << g->GlyphClass))
-       && g->glyph_id != ids[i++])
+    if (! IGNORED_GLYPH (g, flag) && g->glyph_id != ids[i++])
       return -1;
   return (i < count ? -1 : g - gbeg);
 }
@@ -266,10 +270,10 @@ match_chain_ids (OTF_GlyphString *gstring, int gidx, int flag,
   if (i > 0)
     {
       int j;
+      OTF_Glyph *g;
 
-      for (j = gidx - 1; j >= 0; j--)
-       if (gstring->glyphs[j].glyph_id
-           && --i == 0)
+      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;
@@ -301,7 +305,7 @@ match_classes (OTF_ClassDef *class_def, OTF_GlyphString *gstring, int gidx,
   int i;
 
   for (g = gbeg, i = 0; g < gend && i < count; g++)
-    if (g->glyph_id && ! (flag & (1 << g->GlyphClass))
+    if (! IGNORED_GLYPH (g, flag)
        && get_class_def (class_def, g->glyph_id) != classes[i++])
       return -1;
   return (i < count ? -1 : g - gbeg);
@@ -319,10 +323,10 @@ match_chain_classes (OTF_GlyphString *gstring, int gidx, int flag,
   if (i > 0)
     {
       int j;
+      OTF_Glyph *g;
 
-      for (j = gidx - 1; j >= 0 && i > 0; j--)
-       if (gstring->glyphs[j].glyph_id
-           && i-- == 0)
+      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;
@@ -354,7 +358,7 @@ match_coverages (OTF_GlyphString *gstring, int gidx, int flag, int count,
   int i;
 
   for (g = gbeg, i = 0; g < gend && i < count; g++)
-    if (g->glyph_id && ! (flag & (1 << g->GlyphClass))
+    if (! IGNORED_GLYPH (g, flag)
        && get_coverage_index (coverages + i++, g->glyph_id) < 0)
       return -1;
   return (i < count ? -1 : g - gbeg);
@@ -369,10 +373,10 @@ match_chain_coverages (OTF_GlyphString *gstring, int gidx, int flag,
   if (i > 0)
     {
       int j;
+      OTF_Glyph *g;
 
-      for (j = gidx - 1; j >= 0; j--)
-       if (gstring->glyphs[j].glyph_id
-           && --i == 0)
+      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;
@@ -396,20 +400,19 @@ match_chain_coverages (OTF_GlyphString *gstring, int gidx, int flag,
 }
 
 static int
-lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
-            OTF_GlyphString *gstring, int gidx)
+lookup_gsub (OTF *otf, OTF_LookupList *lookup_list, unsigned lookup_list_index,
+            OTF_GlyphString *gstring, int gidx, int alternate_subst)
 {
   char *errfmt = "GSUB Looking up%s";
   int errret = -1;
   OTF_Lookup *lookup = lookup_list->Lookup + lookup_list_index;
-  unsigned int flag = lookup->LookupFlag & OTF_LookupFlagIgnoreMask;
+  unsigned int flag = (lookup->LookupFlag
+                      & (OTF_LookupFlagIgnoreMask | OTF_MarkAttachmentType));
   int orig_gidx = gidx;
   OTF_Glyph *g = gstring->glyphs + gidx;
   int i;
 
-  if (! g->glyph_id
-      || (g->GlyphClass
-         && (flag & (1 << g->GlyphClass))))
+  if (IGNORED_GLYPH (g, flag))
     return (gidx + 1);
 
   /* Try all subtables until one of them handles the current glyph.  */
@@ -427,6 +430,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
          subtable = extension1->ExtensionSubtable;
        }
 
+      if (alternate_subst
+         ? (lookup_type != 3 && lookup_type != 5 && lookup_type != 6)
+         : (lookup_type == 3))
+       continue;
+
       if (subtable->Coverage.offset)
        {
          coverage_idx = get_coverage_index (&subtable->Coverage,
@@ -446,26 +454,28 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
          break;
 
        case 2:
-         {
-           OTF_GSUB_Multiple1 *multiple1 = &subtable->u.multiple1;
-           OTF_Sequence *seq = multiple1->Sequence + coverage_idx;
+         if (subtable->Format == 1)
+           {
+             OTF_GSUB_Multiple1 *multiple1 = &subtable->u.multiple1;
+             OTF_Sequence *seq = multiple1->Sequence + coverage_idx;
 
-           gstring_subst (gstring, gidx, gidx + 1, flag,
-                          seq->Substitute, seq->GlyphCount);
-           gidx += seq->GlyphCount;
-         }
+             gstring_subst (otf, gstring, gidx, gidx + 1, flag,
+                            seq->Substitute, seq->GlyphCount);
+             gidx += seq->GlyphCount;
+           }
+         else
+           OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (invalid SubFormat)");
          break;
 
        case 3:
-         /* For the moment, we always ignore this feature.  */
-         break;
          if (subtable->Format == 1)
            {
              OTF_GSUB_Alternate1 *alt1 = &subtable->u.alternate1;
              OTF_AlternateSet *altset = alt1->AlternateSet + coverage_idx;
 
-             g->glyph_id = altset->Alternate[0];
-             gidx++;
+             gstring_subst (otf, gstring, gidx + 1, gidx + 1, flag,
+                            altset->Alternate, altset->GlyphCount);
+             gidx += altset->GlyphCount;;
            }
          else
            OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (invalid SubFormat)");
@@ -488,7 +498,7 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                                 lig->CompCount - 1, lig->Component);
                  if (n < 0)
                    continue;
-                 gstring_subst (gstring, gidx, gidx + 1 + n, flag,
+                 gstring_subst (otf, gstring, gidx, gidx + 1 + n, flag,
                                 &lig->LigGlyph, 1);
                  gidx++;
                  break;
@@ -515,10 +525,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                    continue;
                  orig_used = gstring->used;
                  for (k = 0; k < rule->LookupCount; k++)
-                   lookup_gsub (lookup_list,
+                   lookup_gsub (otf, lookup_list,
                                 rule->LookupRecord[k].LookupListIndex,
                                 gstring,
-                                gidx + rule->LookupRecord[k].SequenceIndex);
+                                gidx + rule->LookupRecord[k].SequenceIndex,
+                                alternate_subst);
                  gidx += rule->GlyphCount + (gstring->used - orig_used);
                  break;
                }
@@ -545,10 +556,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                      continue;
                    orig_used = gstring->used;
                    for (k = 0; k < rule->LookupCount; k++)
-                     lookup_gsub (lookup_list,
+                     lookup_gsub (otf, lookup_list,
                                   rule->LookupRecord[k].LookupListIndex,
                                   gstring,
-                                  gidx + rule->LookupRecord[k].SequenceIndex);
+                                  gidx + rule->LookupRecord[k].SequenceIndex,
+                                  alternate_subst);
                    gidx += rule->GlyphCount + (gstring->used - orig_used);
                    break;
                  }
@@ -565,10 +577,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                continue;
              orig_used = gstring->used;
              for (j = 0; j < context3->LookupCount; j++)
-               lookup_gsub (lookup_list,
+               lookup_gsub (otf, lookup_list,
                             context3->LookupRecord[j].LookupListIndex,
                             gstring,
-                            gidx + context3->LookupRecord[j].SequenceIndex);
+                            gidx + context3->LookupRecord[j].SequenceIndex,
+                            alternate_subst);
              gidx += context3->GlyphCount + (gstring->used - orig_used);
            }
          break;
@@ -593,10 +606,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                    continue;
                  orig_used = gstring->used;
                  for (k = 0; k < rule->LookupCount; k++)
-                   lookup_gsub (lookup_list,
+                   lookup_gsub (otf, lookup_list,
                                 rule->LookupRecord[k].LookupListIndex,
                                 gstring,
-                                gidx + rule->LookupRecord[k].SequenceIndex);
+                                gidx + rule->LookupRecord[k].SequenceIndex,
+                                alternate_subst);
                  gidx += rule->InputGlyphCount + (gstring->used - orig_used);
                  break;
                }
@@ -628,10 +642,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                    continue;
                  orig_used = gstring->used;
                  for (k = 0; k < rule->LookupCount; k++)
-                   lookup_gsub (lookup_list,
+                   lookup_gsub (otf, lookup_list,
                                 rule->LookupRecord[k].LookupListIndex,
                                 gstring,
-                                gidx + rule->LookupRecord[k].SequenceIndex);
+                                gidx + rule->LookupRecord[k].SequenceIndex,
+                                alternate_subst);
                  gidx += rule->InputGlyphCount + (gstring->used - orig_used);
                  break;
                }
@@ -650,10 +665,11 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                continue;
              orig_used = gstring->used;
              for (j = 0; j < context3->LookupCount; j++)
-               lookup_gsub (lookup_list,
+               lookup_gsub (otf, lookup_list,
                             context3->LookupRecord[j].LookupListIndex,
                             gstring,
-                            gidx + context3->LookupRecord[j].SequenceIndex);
+                            gidx + context3->LookupRecord[j].SequenceIndex,
+                            alternate_subst);
              gidx += context3->InputGlyphCount + (gstring->used - orig_used);
            }
          break;
@@ -725,15 +741,14 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
   char *errfmt = "GPOS Looking up%s";
   int errret = -1;
   OTF_Lookup *lookup = lookup_list->Lookup + lookup_list_index;
-  unsigned int flag = lookup->LookupFlag & OTF_LookupFlagIgnoreMask;
+  unsigned int flag = (lookup->LookupFlag
+                      & (OTF_LookupFlagIgnoreMask | OTF_MarkAttachmentType));
   int orig_gidx = gidx;
   OTF_Glyph *g = gstring->glyphs + gidx;
   int i;
 
-  if (! g->glyph_id
-      || g->positioning_type
-      || (g->GlyphClass
-         && (flag & (1 << g->GlyphClass))))
+  if (IGNORED_GLYPH (g, flag)
+      || g->positioning_type)
     return (gidx + 1);
 
   /* Try all subtables until one of them handles the current glyph.  */
@@ -781,19 +796,15 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
 
        case 2:
          {
-           int next_gidx = gidx + 1;
+           int next_gidx;
            OTF_Glyph *nextg;
 
-           while (next_gidx < gstring->used
-                  && (! gstring->glyphs[next_gidx].glyph_id
-                      || ! (flag
-                            & (1 << gstring->glyphs[next_gidx].GlyphClass))))
-             next_gidx++;
+           for (next_gidx = gidx + 1, nextg = gstring->glyphs + next_gidx;
+                next_gidx < gstring->used && ! IGNORED_GLYPH (nextg, flag);
+                next_gidx++, nextg++);
 
-           if (next_gidx >= gstring->used)
-             continue;
-           nextg = gstring->glyphs + next_gidx;
-           if (nextg->positioning_type)
+           if (next_gidx >= gstring->used
+               || nextg->positioning_type)
              continue;
            if (subtable->Format == 1)
              {
@@ -868,18 +879,17 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              OTF_GPOS_MarkBase1 *mark_base1 = &subtable->u.mark_base1;
              OTF_MarkRecord *mark_record;
              OTF_AnchorRecord *base_record;
-             OTF_Glyph *baseg = g - 1;
+             OTF_Glyph *baseg;
              int coverage_idx_base;
 
-             while (baseg >= gstring->glyphs
-                    && (! baseg->glyph_id
-                        || (baseg->GlyphClass
-                            && (flag & (1 << baseg->GlyphClass)))))
-               baseg--;
+             for (baseg = g - 1;
+                  baseg >= gstring->glyphs && IGNORED_GLYPH (baseg, flag);
+                  baseg--);
+             if (baseg < gstring->glyphs)
+               continue;
              coverage_idx_base
                = get_coverage_index (&mark_base1->BaseCoverage,
                                      baseg->glyph_id);
-
              if (coverage_idx_base < 0)
                continue;
              mark_record = mark_base1->MarkArray.MarkRecord + coverage_idx;
@@ -898,11 +908,9 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
          if (subtable->Format == 1)
            {
              OTF_GPOS_MarkLig1 *mark_lig1 = &subtable->u.mark_lig1;
-             unsigned class = g->MarkAttachClass;
-             OTF_Glyph *ligg = g - 1;
+             OTF_Glyph *ligg;
              int coverage_idx_lig;
              OTF_MarkRecord *mark_record;
-             OTF_ComponentRecord *cmp_record;
              OTF_LigatureAttach *attach;
              int *num_class = alloca (sizeof (int) * mark_lig1->ClassCount);
              int j;
@@ -910,16 +918,16 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              for (j = 0; j < mark_lig1->ClassCount; j++)
                num_class[j] = 0;
 
-             while (ligg >= gstring->glyphs
-                    && (! ligg->glyph_id
-                        || (ligg->GlyphClass
-                            && (flag & (1 << ligg->GlyphClass)))))
-               {
-                 if (ligg->positioning_type == 5
-                     && ligg->MarkAttachClass < mark_lig1->ClassCount)
-                   num_class[ligg->MarkAttachClass]++;
-                 ligg--;
-               }
+             for (ligg = g - 1;
+                  (ligg >= gstring->glyphs
+                   && (IGNORED_GLYPH (ligg, flag)
+                       || ligg->GlyphClass > OTF_GlyphClassLigature));
+                  ligg--)
+               if (ligg->positioning_type == 5
+                   && ligg->MarkAttachClass < mark_lig1->ClassCount)
+                 num_class[ligg->MarkAttachClass]++;
+             if (ligg < gstring->glyphs)
+               continue;
              coverage_idx_lig
                = get_coverage_index (&mark_lig1->LigatureCoverage,
                                      ligg->glyph_id);
@@ -954,14 +962,12 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              OTF_GPOS_MarkMark1 *mark_mark1 = &subtable->u.mark_mark1;
              OTF_MarkRecord *mark1_record;
              OTF_AnchorRecord *mark2_record;
-             OTF_Glyph *prevg = g - 1;
+             OTF_Glyph *prevg;
              int coverage_idx_base;
 
-             while (prevg >= gstring->glyphs
-                    && (! prevg->glyph_id
-                        || (prevg->GlyphClass
-                            && (flag & (1 << prevg->GlyphClass)))))
-               prevg--;
+             for (prevg = g - 1;
+                  prevg >= gstring->glyphs && IGNORED_GLYPH (prevg, flag);
+                  prevg--);
              if (prevg < gstring->glyphs)
                continue;
              coverage_idx_base
@@ -1331,10 +1337,10 @@ OTF_drive_gdef (OTF *otf, OTF_GlyphString *gstring)
   return 0;
 }
 
-
-int
-OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
-               char *script, char *language, char *features)
+static int
+OTF_drive_gsub_internal (OTF *otf, OTF_GlyphString *gstring,
+                        char *script, char *language, char *features,
+                        int alternate_subst)
 {
   char *errfmt = "GSUB driving%s";
   int errret = -1;
@@ -1379,7 +1385,8 @@ OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
          gidx = 0;
          while (gidx < gstring->used)
            {
-             gidx = lookup_gsub (&gsub->LookupList, index, gstring, gidx);
+             gidx = lookup_gsub (otf, &gsub->LookupList, index, gstring, gidx,
+                                 alternate_subst);
              if (gidx < 0)
                return errret;
            }
@@ -1389,7 +1396,8 @@ OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
          gidx = gstring->used - 1;
          while (gidx >= 0)
            {
-             gidx = lookup_gsub (&gsub->LookupList, index, gstring, gidx);
+             gidx = lookup_gsub (otf, &gsub->LookupList, index, gstring, gidx,
+                                 alternate_subst);
              if (gidx < 0)
                return errret;
            }
@@ -1400,6 +1408,13 @@ OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
 }
 
 int
+OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
+               char *script, char *language, char *features)
+{
+  return OTF_drive_gsub_internal (otf, gstring, script, language, features, 0);
+}
+
+int
 OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
                char *script, char *language, char *features)
 {
@@ -1469,3 +1484,10 @@ OTF_drive_tables (OTF *otf, OTF_GlyphString *gstring,
     return -1;
   return 0;
 }
+
+int
+OTF_drive_gsub_alternate (OTF *otf, OTF_GlyphString *gstring,
+                         char *script, char *language, char *features)
+{
+  return OTF_drive_gsub_internal (otf, gstring, script, language, features, 1);
+}