(OTF_check_features): If gsubp is zero, check gpos
[m17n/libotf.git] / src / otfdrive.c
index a857e68..51053b3 100644 (file)
@@ -35,7 +35,8 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
   ((g)->glyph_id == 0                          \
    || ((flag) & (1 << (g)->GlyphClass))                \
    || (((flag) & OTF_MarkAttachmentType)       \
-       && ((flag) >> 8) == (g)->MarkAttachClass))
+       && (g)->GlyphClass == OTF_GlyphClassMark        \
+       && ((flag) >> 8) != (g)->MarkAttachClass))
 
 #define GSTRING_DELETE(gstring, from, len)                             \
   do {                                                                 \
@@ -52,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, "");                             \
       }                                                                        \
@@ -64,13 +66,12 @@ 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;
@@ -83,6 +84,8 @@ gstring_subst (OTF_GlyphString *gstring, int from, int to, int flag,
          OTF_Glyph temp = *g;
 
          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--;
        }
@@ -94,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;
@@ -199,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)
@@ -239,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;
            }
        }
@@ -405,8 +400,8 @@ 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;
@@ -435,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,
@@ -454,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)");
@@ -496,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;
@@ -523,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;
                }
@@ -553,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;
                  }
@@ -573,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;
@@ -601,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;
                }
@@ -636,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;
                }
@@ -658,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;
@@ -900,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;
              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;
@@ -913,7 +919,9 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
                num_class[j] = 0;
 
              for (ligg = g - 1;
-                  ligg >= gstring->glyphs && IGNORED_GLYPH (ligg, flag);
+                  (ligg >= gstring->glyphs
+                   && (IGNORED_GLYPH (ligg, flag)
+                       || ligg->GlyphClass > OTF_GlyphClassLigature));
                   ligg--)
                if (ligg->positioning_type == 5
                    && ligg->MarkAttachClass < mark_lig1->ClassCount)
@@ -1329,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;
@@ -1377,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;
            }
@@ -1387,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;
            }
@@ -1398,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)
 {
@@ -1467,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);
+}