(IGNORED_GLYPH): Fix previous change.
[m17n/libotf.git] / src / otfdrive.c
index 5acdf22..c236414 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 == 3                 \
+       && ((flag) >> 8) != (g)->MarkAttachClass))
+
 #define GSTRING_DELETE(gstring, from, len)                             \
   do {                                                                 \
     memmove (gstring->glyphs + from, gstring->glyphs + from + len,     \
@@ -68,15 +77,17 @@ gstring_subst (OTF_GlyphString *gstring, int from, int to, int flag,
   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));
+         gstring->glyphs[non_ignored_idx--] = temp;
+         len--;
+       }
+    }
 
   if (len < num)
     GSTRING_INSERT (gstring, from, (num - len));
@@ -189,33 +200,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 +230,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 +254,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 +268,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 +303,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 +321,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 +356,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 +371,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;
@@ -402,14 +404,13 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
   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.  */
@@ -435,7 +436,7 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
            continue;
        }
 
-      switch (lookup->LookupType)
+      switch (lookup_type)
        {
        case 1:
          if (subtable->Format == 1)
@@ -725,23 +726,31 @@ 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.  */
   for (i = 0; i < lookup->SubTableCount && gidx == orig_gidx; i++)
     {
+      unsigned lookup_type = lookup->LookupType;
       OTF_LookupSubTableGPOS *subtable = lookup->SubTable.gpos + i;
       int coverage_idx;
 
+      if (lookup_type == 9)
+       {
+         OTF_GPOS_Extension1 *extension1 = &subtable->u.extension1;
+
+         lookup_type = extension1->ExtensionLookupType;
+         subtable = extension1->ExtensionSubtable;
+       }
+
       if (subtable->Coverage.offset)
        {
          coverage_idx = get_coverage_index (&subtable->Coverage,
@@ -750,10 +759,10 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
            continue;
        }
 
-      switch (lookup->LookupType)
+      switch (lookup_type)
        {
        case 1:
-         g->positioning_type = lookup->LookupType;
+         g->positioning_type = lookup_type;
          if (subtable->Format == 1)
            {
              OTF_GPOS_Single1 *single1 = &subtable->u.single1;
@@ -772,19 +781,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)
              {
@@ -797,14 +802,14 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                    {
                      if (pair1->ValueFormat1)
                        {
-                         g->positioning_type = lookup->LookupType;
+                         g->positioning_type = lookup_type;
                          g->f.f2.format = pair1->ValueFormat1;
                          g->f.f2.value = &set->PairValueRecord[j].Value1;
                        }
                      gidx = next_gidx;
                      if (pair1->ValueFormat2)
                        {
-                         nextg->positioning_type = lookup->LookupType;
+                         nextg->positioning_type = lookup_type;
                          nextg->f.f2.format = pair1->ValueFormat2;
                          nextg->f.f2.value = &set->PairValueRecord[j].Value2;
                          gidx++;
@@ -821,7 +826,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                class2 = get_class_def (&pair2->ClassDef2, nextg->glyph_id);
                if (pair2->ValueFormat1)
                  {
-                   g->positioning_type = lookup->LookupType;
+                   g->positioning_type = lookup_type;
                    g->f.f2.format = pair2->ValueFormat1;
                    g->f.f2.value
                      = &pair2->Class1Record[class1].Class2Record[class2].Value1;
@@ -829,7 +834,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                gidx = next_gidx;
                if (pair2->ValueFormat2)
                  {
-                   nextg->positioning_type = lookup->LookupType;
+                   nextg->positioning_type = lookup_type;
                    nextg->f.f2.format = pair2->ValueFormat2;
                    nextg->f.f2.value
                      = &pair2->Class1Record[class1].Class2Record[class2].Value2;
@@ -843,7 +848,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
          {
            OTF_GPOS_Cursive1 *cursive1 = &subtable->u.cursive1;
          
-           g->positioning_type = lookup->LookupType;
+           g->positioning_type = lookup_type;
            g->f.f3.entry_anchor
              = &cursive1->EntryExitRecord[coverage_idx].EntryAnchor;
            g->f.f3.exit_anchor
@@ -859,18 +864,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;
@@ -879,7 +883,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              g->f.f4.mark_anchor = &mark_record->MarkAnchor;
              g->f.f4.base_anchor
                = &base_record->Anchor[mark_record->Class];
-             g->positioning_type = lookup->LookupType;
+             g->positioning_type = lookup_type;
            }
          break;
 
@@ -889,11 +893,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;
@@ -901,16 +903,14 @@ 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--)
+               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);
@@ -928,7 +928,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                  if (lig_anchor[mark_record->Class].AnchorFormat
                      && num_class[mark_record->Class]-- == 0)
                    {
-                     g->positioning_type = lookup->LookupType;
+                     g->positioning_type = lookup_type;
                      g->f.f5.mark_anchor = &mark_record->MarkAnchor;
                      g->f.f5.ligature_anchor = lig_anchor + mark_record->Class;
                      break;
@@ -945,14 +945,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
@@ -966,7 +964,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              g->f.f6.mark1_anchor = &mark1_record->MarkAnchor;
              g->f.f6.mark2_anchor
                = &mark2_record->Anchor[mark1_record->Class];
-             g->positioning_type = lookup->LookupType;
+             g->positioning_type = lookup_type;
              break;
            }
          break;
@@ -1133,10 +1131,6 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
            OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (invalid subformat)");
          break;
 
-       case 9:
-         OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
-         break;
-
        default:
          continue;
        }