*** empty log message ***
[m17n/libotf.git] / src / otfdrive.c
index c236414..3dc6eda 100644 (file)
@@ -29,14 +29,14 @@ 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))
+/* Return nonzero (-1 if ID is zero, 1 otherwise) if OTF_Glyph *G
+   should be ignored according to LookupFlag FLAG.  */
+#define IGNORED_GLYPH(g, flag)                         \
+  ((g)->glyph_id == 0 ? -1                             \
+   : (((flag) & (1 << (g)->GlyphClass))                        \
+      || (((flag) & OTF_MarkAttachmentType)            \
+         && (g)->GlyphClass == OTF_GlyphClassMark      \
+         && ((flag) >> 8) != (g)->MarkAttachClass)))
 
 #define GSTRING_DELETE(gstring, from, len)                             \
   do {                                                                 \
@@ -53,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,14 +65,15 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
   } while (0)
 
 
+static unsigned get_class_def (OTF_ClassDef *, OTF_GlyphID);
+
 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;
@@ -79,11 +81,16 @@ gstring_subst (OTF_GlyphString *gstring, int from, int to, int flag,
   for (i = non_ignored_idx = to - 1; i >= from; i--)
     {
       OTF_Glyph *g = gstring->glyphs + i;
-      if (IGNORED_GLYPH (g, flag))
+
+      if (IGNORED_GLYPH (g, flag) == 1)
        {
+         /* Move this glyph to the next of the current target of
+            substitution.  */
          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--;
        }
@@ -95,8 +102,17 @@ 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;
+      if (gstring->glyphs[from + i].glyph_id != ids[i])
+       {
+         gstring->glyphs[from + i].c = 0;
+         if (otf->gdef)
+           gstring->glyphs[from + i].GlyphClass
+             = get_class_def (&otf->gdef->glyph_class_def, ids[i]);
+         else
+           gstring->glyphs[from + i].GlyphClass = 0;
+       }
       gstring->glyphs[from + i].glyph_id = ids[i];
+      gstring->glyphs[from + i].positioning_type = 0;
       gstring->glyphs[from + i].f.index.from = from_idx;
       gstring->glyphs[from + i].f.index.to = to_idx;
     }
@@ -149,7 +165,8 @@ get_class_def (OTF_ClassDef *class_def, OTF_GlyphID glyph_id)
 }
 
 static OTF_LangSys *
-get_langsys (OTF_ScriptList *script_list, char *script, char *language)
+get_langsys (OTF_ScriptList *script_list,
+            const char *script, const char *language)
 {
 
   OTF_Tag script_tag = OTF_tag (script);
@@ -187,7 +204,7 @@ get_langsys (OTF_ScriptList *script_list, char *script, char *language)
 
 static int
 setup_lookup_indices (OTF_LookupList *LookupList, OTF_FeatureList *FeatureList,
-                     char *features, int *lookup_indices)
+                     const char *features, int *lookup_indices)
 {
   int i, j, n = 0;
   OTF_Feature *feature;
@@ -398,8 +415,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;
@@ -428,6 +445,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,
@@ -447,26 +469,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, gidx + 1, flag,
+                            altset->Alternate, altset->GlyphCount);
+             gidx += altset->GlyphCount;;
            }
          else
            OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (invalid SubFormat)");
@@ -489,7 +513,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;
@@ -516,10 +540,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;
                }
@@ -546,10 +571,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;
                  }
@@ -566,10 +592,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;
@@ -594,10 +621,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;
                }
@@ -629,10 +657,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;
                }
@@ -651,10 +680,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;
@@ -733,7 +763,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
   int i;
 
   if (IGNORED_GLYPH (g, flag)
-      || g->positioning_type)
+      || (0 & g->positioning_type))
     return (gidx + 1);
 
   /* Try all subtables until one of them handles the current glyph.  */
@@ -785,7 +815,7 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
            OTF_Glyph *nextg;
 
            for (next_gidx = gidx + 1, nextg = gstring->glyphs + next_gidx;
-                next_gidx < gstring->used && ! IGNORED_GLYPH (nextg, flag);
+                next_gidx < gstring->used && IGNORED_GLYPH (nextg, flag);
                 next_gidx++, nextg++);
 
            if (next_gidx >= gstring->used
@@ -866,9 +896,10 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
              OTF_AnchorRecord *base_record;
              OTF_Glyph *baseg;
              int coverage_idx_base;
+             unsigned int this_flag = flag | OTF_IgnoreMarks;
 
              for (baseg = g - 1;
-                  baseg >= gstring->glyphs && IGNORED_GLYPH (baseg, flag);
+                  baseg >= gstring->glyphs && IGNORED_GLYPH (baseg, this_flag);
                   baseg--);
              if (baseg < gstring->glyphs)
                continue;
@@ -904,7 +935,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)
@@ -1320,10 +1353,11 @@ 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,
+                        const char *script, const char *language,
+                        const char *features,
+                        int alternate_subst)
 {
   char *errfmt = "GSUB driving%s";
   int errret = -1;
@@ -1333,10 +1367,12 @@ OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
   int i, n;
 
   for (i = 0; i < gstring->used; i++)
-    gstring->glyphs[i].f.index.from = gstring->glyphs[i].f.index.to = i;
+    {
+      gstring->glyphs[i].positioning_type = 0;
+      gstring->glyphs[i].f.index.from = gstring->glyphs[i].f.index.to = i;
+    }
 
-  if (! otf->gsub
-      && OTF_get_table (otf, "GSUB") < 0)
+  if (OTF_get_table (otf, "GSUB") < 0)
     return errret;
   gsub = otf->gsub;
   if (gsub->FeatureList.FeatureCount == 0
@@ -1368,7 +1404,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;
            }
@@ -1378,7 +1415,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;
            }
@@ -1389,8 +1427,17 @@ OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
 }
 
 int
+OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
+               const char *script, const char *language, const char *features)
+{
+  if (! otf->cmap)
+    OTF_get_table (otf, "cmap");
+  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)
+               const char *script, const char *language, const char *features)
 {
   char *errfmt = "GPOS driving%s";
   int errret = -1;
@@ -1399,8 +1446,10 @@ OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
   int *lookup_indices;
   int i, n;
 
-  if (! otf->gpos
-      && OTF_get_table (otf, "GPOS") < 0)
+  for (i = 0; i < gstring->used; i++)
+    gstring->glyphs[i].positioning_type = 0;
+
+  if (OTF_get_table (otf, "GPOS") < 0)
     return errret;
   gpos = otf->gpos;
   if (gpos->FeatureList.FeatureCount == 0
@@ -1422,9 +1471,6 @@ OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
   if (n < 0)
     return errret;
 
-  for (i = 0; i < gstring->used; i++)
-    gstring->glyphs[i].positioning_type = 0;
-
   for (i = 0; i < n; i++)
     {
       int index = lookup_indices[i];
@@ -1443,8 +1489,8 @@ OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
 
 int
 OTF_drive_tables (OTF *otf, OTF_GlyphString *gstring,
-                 char *script, char *language,
-                 char *gsub_features, char *gpos_features)
+                 const char *script, const char *language,
+                 const char *gsub_features, const char *gpos_features)
 {
   if (OTF_drive_cmap (otf, gstring) < 0)
     return -1;
@@ -1458,3 +1504,11 @@ OTF_drive_tables (OTF *otf, OTF_GlyphString *gstring,
     return -1;
   return 0;
 }
+
+int
+OTF_drive_gsub_alternate (OTF *otf, OTF_GlyphString *gstring,
+                         const char *script, const char *language,
+                         const char *features)
+{
+  return OTF_drive_gsub_internal (otf, gstring, script, language, features, 1);
+}