*** empty log message ***
[m17n/libotf.git] / src / otfdrive.c
index dedbec3..59b8114 100644 (file)
@@ -1,6 +1,30 @@
+/* otfdrive.c -- OpenType font driver.
+
+Copyright (C) 2003, 2004
+  National Institute of Advanced Industrial Science and Technology (AIST)
+  Registration Number H15PRO167
+
+This file is part of libotf.
+
+Libotf is free software; you can redistribute it and/or modify it
+under the terms of the GNU Lesser General Public License as published
+by the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+Libotf is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library, in a file named COPYING; if not,
+write to the Free Software Foundation, Inc., 59 Temple Place, Suite
+330, Boston, MA 02111-1307, USA.  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <config.h>
 
 #include "otf.h"
 #include "otferror.h"
@@ -87,7 +111,7 @@ get_class_def (OTF_ClassDef *class_def, OTF_GlyphID glyph_id)
 
       for (i = 0; i < class_def->f.f2.ClassRangeCount; i++)
        if (glyph_id >= class_def->f.f2.ClassRangeRecord[i].Start
-           && glyph_id >= class_def->f.f2.ClassRangeRecord[i].End)
+           && glyph_id <= class_def->f.f2.ClassRangeRecord[i].End)
          return class_def->f.f2.ClassRangeRecord[i].Class;
     }
   return 0;
@@ -97,12 +121,13 @@ static OTF_LangSys *
 get_langsys (OTF_ScriptList *script_list, char *script, char *language)
 {
 
-  OTF_Tag script_tag = otf_tag (script);
-  OTF_Tag langsys_tag = otf_tag (language);
+  OTF_Tag script_tag = OTF_tag (script);
+  OTF_Tag langsys_tag = OTF_tag (language);
   int i, j;
 
   for (i = 0; i < script_list->ScriptCount; i++)
-    if (script_list->Script[i].ScriptTag == script_tag)
+    if (! script_tag
+       || script_list->Script[i].ScriptTag == script_tag)
       {
        OTF_Script *script = script_list->Script + i;
 
@@ -118,49 +143,218 @@ get_langsys (OTF_ScriptList *script_list, char *script, char *language)
 }
 
 static int
-get_feature_index (OTF_LangSys *LangSys, OTF_FeatureList *FeatureList,
-                  char *features, int *feature_index)
+setup_lookup_indices (OTF_LookupList *LookupList, OTF_FeatureList *FeatureList,
+                     char *features, int *lookup_indices)
 {
-  int nfeatures = 0;
+  int i, j, n = 0;
+  OTF_Feature *feature;
+  int *feature_table = alloca (sizeof (int) * FeatureList->FeatureCount);
 
-  if (features)
+  for (i = 0; i < FeatureList->FeatureCount; i++)
+    feature_table[i] = 0;
+
+  while (*features)
     {
-      char *p0, *p1;
-      int len = strlen (features) + 1;
-
-      p0 = alloca (len);
-      for (p1 = p0; *p1; p1++)
-       if (*p1 == ':')
-         *p1 = '\0';
-      
-      while (len > 0)
+      char tagname[4];
+      OTF_Tag tag;
+      int negate = 0;
+
+      if (*features == '*')
        {
-         int this_len = strlen (p0) + 1;
-         OTF_Tag tag = otf_tag (p0);
+         /* 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;
+             }
+         for (i = 0; i < LookupList->LookupCount; i++)
+           if (free_table[i])
+             lookup_indices[n++] = i;
+         break;
+       }
 
-         if (tag)
+      if (*features == '~')
+       negate = 1, features++;
+      for (i = 0; *features && *features != ','; i++, features++)
+       tagname[i] = *features;
+      if (*features)
+       /* Skip ',' */
+       features++;
+      for (; i < 4; i++)
+       tagname[i] = '\0';
+      tag = OTF_tag (tagname);
+      for (i = 0; i < FeatureList->FeatureCount; i++)
+       {
+         feature = FeatureList->Feature + i;
+         if (tag == feature->FeatureTag)
            {
-             int i;
-
-             for (i = 0; i < FeatureList->FeatureCount; i++)
-               if (tag == FeatureList->Feature[i].FeatureTag)
-                 {
-                   feature_index[nfeatures++] = i;
-                   if (nfeatures == FeatureList->FeatureCount)
-                     break;
-                 }
+             if (! negate)
+               for (j = 0; j < feature->LookupCount; j++)
+                 lookup_indices[n++] = feature->LookupListIndex[j];
+             feature_table[i] = 1;
+             break;
            }
-         p0 += this_len;
-         len -= this_len;
        }
     }
-  else
+
+  return n;
+}
+
+static int
+match_ids (OTF_GlyphString *gstring, int gidx, int count, OTF_GlyphID *ids)
+{
+  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 (g->glyph_id && g->glyph_id != ids[i++])
+      return -1;
+  return (i < count ? -1 : g - gbeg);
+}
+
+static int
+match_chain_ids (OTF_GlyphString *gstring, int gidx, OTF_ChainRule *rule)
+{
+  int i = rule->BacktrackGlyphCount;
+
+  if (i > 0)
+    {
+      int j;
+
+      for (j = gidx - 1; j >= 0; j--)
+       if (gstring->glyphs[j].glyph_id
+           && --i == 0)
+         break;
+      if (i > 0)
+       return -1;
+      if (match_ids (gstring, j, rule->BacktrackGlyphCount, rule->Backtrack)
+         < 0)
+       return -1;
+    }
+  gidx++;
+  i = match_ids (gstring, gidx, rule->InputGlyphCount - 1, rule->Input);
+  if (i < 0)
+    return -1;
+  gidx += i;
+  i = match_ids (gstring, gidx, rule->LookaheadGlyphCount, rule->LookAhead);
+  if (i < 0)
+    return -1;
+  return 0;
+}
+
+static int
+match_classes (OTF_ClassDef *class_def, OTF_GlyphString *gstring, int gidx,
+              int count, unsigned *classes)
+{
+  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 (g->glyph_id
+       && get_class_def (class_def, g->glyph_id) != classes[i++])
+      return -1;
+  return (i < count ? -1 : g - gbeg);
+}
+
+static int
+match_chain_classes (OTF_GlyphString *gstring, int gidx,
+                    OTF_ClassDef *BacktrackClassDef,
+                    OTF_ClassDef *InputClassDef,
+                    OTF_ClassDef *LookaheadClassDef,
+                    OTF_ChainClassRule *rule)
+{
+  int i = rule->BacktrackGlyphCount;
+
+  if (i > 0)
     {
-      for (; nfeatures < LangSys->FeatureCount; nfeatures++)
-       feature_index[nfeatures] = LangSys->FeatureIndex[nfeatures];
+      int j;
+
+      for (j = gidx - 1; j >= 0 && i > 0; j--)
+       if (gstring->glyphs[j].glyph_id
+           && i-- == 0)
+         break;
+      if (i > 0)
+       return -1;
+      if (match_classes (BacktrackClassDef, gstring, j,
+                        rule->BacktrackGlyphCount, rule->Backtrack) < 0);
+      return -1;
     }
+  gidx++;
+  i = match_classes (InputClassDef, gstring, gidx,
+                    rule->InputGlyphCount - 1, rule->Input);
+  if (i < 0)
+    return -1;
+  gidx += i;
+  i = match_classes (LookaheadClassDef, gstring, gidx,
+                    rule->LookaheadGlyphCount, rule->LookAhead);
+  if (i < 0)
+    return -1;
+  return 0;
+}
+
+
+static int
+match_coverages (OTF_GlyphString *gstring, int gidx, int count,
+                OTF_Coverage *coverages)
+{
+  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 (g->glyph_id
+       && get_coverage_index (coverages + i++, g->glyph_id) < 0)
+      return -1;
+  return (i < count ? -1 : g - gbeg);
+}
+
+static int
+match_chain_coverages (OTF_GlyphString *gstring, int gidx,
+                      OTF_GSUB_ChainContext3 *context3)
+{
+  int i = context3->BacktrackGlyphCount;
 
-  return nfeatures;
+  if (i > 0)
+    {
+      int j;
+
+      for (j = gidx - 1; j >= 0; j--)
+       if (gstring->glyphs[j].glyph_id
+           && --i == 0)
+         break;
+      if (i > 0)
+       return -1;
+      if (match_coverages (gstring, j, context3->BacktrackGlyphCount,
+                          context3->Backtrack) < 0)
+       return -1;
+    }
+  gidx++;
+  if (context3->InputGlyphCount > 1)
+    {
+      i = match_coverages (gstring, gidx, context3->InputGlyphCount - 1,
+                          context3->Input + 1);
+      if (i < 0)
+       return -1;
+      gidx += i;
+    }
+  if (match_coverages (gstring, gidx, context3->LookaheadGlyphCount,
+                      context3->LookAhead) < 0)
+    return -1;
+  return 0;
 }
 
 static int
@@ -183,9 +377,18 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
   /* 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_LookupSubTableGSUB *subtable = lookup->SubTable.gsub + i;
       int coverage_idx;
 
+      if (lookup_type == 7)
+       {
+         OTF_GSUB_Extension1 *extension1 = &subtable->u.extension1;
+
+         lookup_type = extension1->ExtensionLookupType;
+         subtable = extension1->ExtensionSubtable;
+       }
+
       if (subtable->Coverage.offset)
        {
          coverage_idx = get_coverage_index (&subtable->Coverage,
@@ -216,29 +419,36 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
          break;
 
        case 3:
-         OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (LookupType not yet supported)");
+         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++;
+           }
+         else
+           OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (invalid SubFormat)");
+         break;
 
        case 4:
          if (subtable->Format == 1)
            {
              OTF_GSUB_Ligature1 *lig1 = &subtable->u.ligature1;
              OTF_LigatureSet *ligset = lig1->LigatureSet + coverage_idx;
+             OTF_Ligature *lig;
              int j;
 
              for (j = 0; j < ligset->LigatureCount; j++)
                {
-                 OTF_Ligature *lig = ligset->Ligature + j;
-                 int k;
+                 int n;
 
-                 if (gstring->used - gidx < lig->CompCount)
+                 lig = ligset->Ligature + j;
+                 n = match_ids (gstring, gidx + 1,
+                                lig->CompCount - 1, lig->Component);
+                 if (n < 0)
                    continue;
-                 for (k = 1; k < lig->CompCount; k++)
-                   if (gstring->glyphs[gidx + k].glyph_id
-                       != lig->Component[k - 1])
-                     break;
-                 if (k < lig->CompCount)
-                   continue;
-                 gstring_subst (gstring, gidx, gidx + lig->CompCount,
+                 gstring_subst (gstring, gidx, gidx + 1 + n,
                                 &lig->LigGlyph, 1);
                  gidx++;
                  break;
@@ -247,67 +457,200 @@ lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
          else
            OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (invalid SubFormat)");
          break;
-             
-       case 6:
+
+       case 5:
          if (subtable->Format == 1)
-           OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (not yet supported)");
+           {
+             OTF_GSUB_Context1 *context1 = &subtable->u.context1;
+             OTF_RuleSet *set = context1->RuleSet + coverage_idx;
+             OTF_Rule *rule;
+             int orig_used;
+             int j, k;
+
+             for (j = 0; j < set->RuleCount; j++)
+               {
+                 rule = set->Rule + j;
+                 if (match_ids (gstring, gidx + 1,
+                                rule->GlyphCount - 1, rule->Input) < 0)
+                   continue;
+                 orig_used = gstring->used;
+                 for (k = 0; k < rule->LookupCount; k++)
+                   lookup_gsub (lookup_list,
+                                rule->LookupRecord[k].LookupListIndex,
+                                gstring,
+                                gidx + rule->LookupRecord[k].SequenceIndex);
+                 gidx += rule->GlyphCount + (gstring->used - orig_used);
+                 break;
+               }
+           }
          else if (subtable->Format == 2)
-           OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (not yet supported)");
-         else
            {
-             OTF_GSUB_ChainContext3 *context3
-               = &subtable->u.chain_context3;
-             int back_gidx = gidx - context3->BacktrackGlyphCount;
-             int fore_gidx = gidx + context3->InputGlyphCount;
+             OTF_GSUB_Context2 *context2 = &subtable->u.context2;
+             OTF_ClassSet *set;
+             OTF_ClassRule *rule;
+             unsigned class;
+             int orig_used;
+             int j, k;
+
+             class = get_class_def (&context2->ClassDef, g->glyph_id);
+             set = context2->ClassSet + class;
+             if (set)
+               for (j = 0; j < set->ClassRuleCnt; j++)
+                 {
+                   rule = set->ClassRule + j;
+                   if (match_classes (&context2->ClassDef,
+                                      gstring, gidx + 1,
+                                      rule->GlyphCount - 1, rule->Class)
+                       < 0)
+                     continue;
+                   orig_used = gstring->used;
+                   for (k = 0; k < rule->LookupCount; k++)
+                     lookup_gsub (lookup_list,
+                                  rule->LookupRecord[k].LookupListIndex,
+                                  gstring,
+                                  gidx + rule->LookupRecord[k].SequenceIndex);
+                   gidx += rule->GlyphCount + (gstring->used - orig_used);
+                   break;
+                 }
+           }
+         else                  /* subtable->Format == 3 */
+           {
+             OTF_GSUB_Context3 *context3 = &subtable->u.context3;
              int orig_used;
              int j;
 
-             if (back_gidx < 0
-                 || fore_gidx + context3->LookaheadGlyphCount > gstring->used)
-               break;
+             if (match_coverages (gstring, gidx + 1, context3->GlyphCount - 1,
+                                  context3->Coverage + 1) < 0)
+               continue;
+             orig_used = gstring->used;
+             for (j = 0; j < context3->LookupCount; j++)
+               lookup_gsub (lookup_list,
+                            context3->LookupRecord[j].LookupListIndex,
+                            gstring,
+                            gidx + context3->LookupRecord[j].SequenceIndex);
+             gidx += context3->GlyphCount + (gstring->used - orig_used);
+           }
+         break;
 
-             for (j = 0; j < context3->BacktrackGlyphCount; j++)
-               if (get_coverage_index (context3->Backtrack + j,
-                                       gstring->glyphs[back_gidx + j].glyph_id)
-                   < 0)
-                 break;
-             /* Start from the secoding coverage_idx because the
-                first one is the same as subtable->Coverage and thus
-                already tested */
-             for (j = 1; j < context3->InputGlyphCount; j++)
-               if (get_coverage_index (context3->Input + j - 1,
-                                       gstring->glyphs[gidx + j].glyph_id)
-                   < 0)
+       case 6:
+         if (subtable->Format == 1)
+           {
+             OTF_GSUB_ChainContext1 *context1 = &subtable->u.chain_context1;
+             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;
+
+                 if (gidx < rule->BacktrackGlyphCount
+                     || (gidx + rule->InputGlyphCount
+                         + rule->LookaheadGlyphCount) > gstring->used)
+                   continue;
+                 if (match_chain_ids (gstring, gidx, rule) < 0)
+                   continue;
+                 orig_used = gstring->used;
+                 for (k = 0; k < rule->LookupCount; k++)
+                   lookup_gsub (lookup_list,
+                                rule->LookupRecord[k].LookupListIndex,
+                                gstring,
+                                gidx + rule->LookupRecord[k].SequenceIndex);
+                 gidx += rule->InputGlyphCount + (gstring->used - orig_used);
                  break;
-             for (j = 0; j < context3->LookaheadGlyphCount; j++)
-               if (get_coverage_index (context3->LookAhead + j,
-                                       gstring->glyphs[fore_gidx + j].glyph_id)
-                   < 0)
+               }
+           }
+         else if (subtable->Format == 2)
+           {
+             OTF_GSUB_ChainContext2 *context2 = &subtable->u.chain_context2;
+             OTF_ChainClassSet *set;
+             unsigned class;
+             int j;
+             int orig_used;
+
+             class = get_class_def (&context2->InputClassDef, g->glyph_id);
+             set = context2->ChainClassSet + class;
+             for (j = 0; j < set->ChainClassRuleCnt; j++)
+               {
+                 OTF_ChainClassRule *rule = set->ChainClassRule + j;
+                 int k;
+
+                 if (gidx < rule->BacktrackGlyphCount
+                     || (gidx + rule->InputGlyphCount
+                         + rule->LookaheadGlyphCount) > gstring->used)
+                   continue;
+                 if (match_chain_classes (gstring, gidx,
+                                          &context2->BacktrackClassDef,
+                                          &context2->InputClassDef,
+                                          &context2->LookaheadClassDef,
+                                          rule) < 0)
+                   continue;
+                 orig_used = gstring->used;
+                 for (k = 0; k < rule->LookupCount; k++)
+                   lookup_gsub (lookup_list,
+                                rule->LookupRecord[k].LookupListIndex,
+                                gstring,
+                                gidx + rule->LookupRecord[k].SequenceIndex);
+                 gidx += rule->InputGlyphCount + (gstring->used - orig_used);
                  break;
+               }
+           }
+         else
+           {
+             OTF_GSUB_ChainContext3 *context3 = &subtable->u.chain_context3;
+             int orig_used;
+             int j;
 
+             if (gidx < context3->BacktrackGlyphCount
+                 || (gidx + context3->InputGlyphCount
+                     + context3->LookaheadGlyphCount) > gstring->used)
+               continue;
+             if (match_chain_coverages (gstring, gidx, context3) < 0)
+               continue;
              orig_used = gstring->used;
-             for (j = 0; j < context3->SubstCount; j++)
+             for (j = 0; j < context3->LookupCount; j++)
                lookup_gsub (lookup_list,
-                            context3->SubstLookupRecord[j].LookupListIndex,
+                            context3->LookupRecord[j].LookupListIndex,
                             gstring,
-                            gidx + context3->SubstLookupRecord[j].SequenceIndex);
+                            gidx + context3->LookupRecord[j].SequenceIndex);
              gidx += context3->InputGlyphCount + (gstring->used - orig_used);
            }
          break;
 
+       case 8:
+         {
+           OTF_GSUB_ReverseChain1 *reverse = &subtable->u.reverse_chain1;
+           int back_gidx = gidx + 1 + reverse->BacktrackGlyphCount;
+           int ahead_gidx = gidx - reverse->LookaheadGlyphCount;
+           int j;
+
+           if (back_gidx > gstring->used || ahead_gidx < 0)
+             break;
+
+           for (j = 0; j < reverse->BacktrackGlyphCount; j++)
+             if (get_coverage_index (reverse->Backtrack + j,
+                                     gstring->glyphs[gidx + 1 + j].glyph_id)
+                 < 0)
+               break;
+           if (j < reverse->BacktrackGlyphCount)
+             continue;
+           for (j = 0; j < reverse->LookaheadGlyphCount; j++)
+             if (get_coverage_index (reverse->LookAhead + j,
+                                     gstring->glyphs[gidx - 1 - j].glyph_id)
+                 < 0)
+               break;
+           if (j < reverse->LookaheadGlyphCount)
+             continue;
+           g->glyph_id = reverse->Substitute[coverage_idx];
+           gidx--;
+         }
+
        default:
          continue;
        }
     }
   if (gidx == orig_gidx)
-    {
-      //printf ("not applied\n");
-      gidx++;
-    }
-  else
-    {
-      // printf ("done\n");
-    }
+    gidx++;
   return gidx;
 }
 
@@ -357,54 +700,110 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
       OTF_LookupSubTableGPOS *subtable = lookup->SubTable.gpos + i;
       int coverage_idx;
 
-      // printf ("subtype:%d ", subtable->Format);
       if (subtable->Coverage.offset)
        {
          coverage_idx = get_coverage_index (&subtable->Coverage,
                                             g->glyph_id);
          if (coverage_idx < 0)
-           {
-             // printf ("not covererd ");
-             continue;
-           }
+           continue;
        }
 
       switch (lookup->LookupType)
        {
        case 1:
-         OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
-
-       case 2:
-         if (gidx + 1 >= gstring->used)
-           continue;
+         g->positioning_type = lookup->LookupType;
          if (subtable->Format == 1)
-           OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
+           {
+             OTF_GPOS_Single1 *single1 = &subtable->u.single1;
+
+             g->f.f1.format = single1->ValueFormat;
+             g->f.f1.value = &single1->Value;
+           }
          else if (subtable->Format == 2)
            {
-             OTF_GPOS_Pair2 *pair2 = &subtable->u.pair2;
-             unsigned class1, class2;
+             OTF_GPOS_Single2 *single2 = &subtable->u.single2;
 
-             printf ("GPOS 2-2: c:0x%x g:0x%x\n", g->c, g->glyph_id);
-             gidx++;
-             class1 = get_class_def (&pair2->ClassDef1, g->glyph_id);
-             class2 = get_class_def (&pair2->ClassDef2, g[1].glyph_id);
-             g->positioning_type = lookup->LookupType;
-             g->f.f2.format = pair2->ValueFormat1;
-             g->f.f2.value
-               = &pair2->Class1Record[class1].Class2Record[class2].Value1;
-             if (pair2->ValueFormat2)
-               {
-                 g++, gidx++;
-                 g->positioning_type = lookup->LookupType;
-                 g->f.f2.format = pair2->ValueFormat2;
-                 g->f.f2.value
-                   = &pair2->Class1Record[class1].Class2Record[class2].Value2;
-               }
+             g->f.f1.format = single2->ValueFormat;
+             g->f.f1.value = single2->Value + coverage_idx;
            }
          break;
 
+       case 2:
+         {
+           int next_gidx = gidx + 1;
+           OTF_Glyph *nextg;
+
+           while (next_gidx < gstring->used
+                  && ! gstring->glyphs[next_gidx].glyph_id)
+             next_gidx++;
+
+           if (next_gidx >= gstring->used)
+             continue;
+           nextg = gstring->glyphs + next_gidx;
+           if (subtable->Format == 1)
+             {
+               OTF_GPOS_Pair1 *pair1 = &subtable->u.pair1;
+               OTF_PairSet *set = pair1->PairSet + coverage_idx;
+               int j;
+
+               for (j = 0; j < set->PairValueCount; j++)
+                 if (set->PairValueRecord[j].SecondGlyph == nextg->glyph_id)
+                   {
+                     if (pair1->ValueFormat1)
+                       {
+                         g->positioning_type = lookup->LookupType;
+                         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->f.f2.format = pair1->ValueFormat2;
+                         nextg->f.f2.value = &set->PairValueRecord[j].Value2;
+                         gidx++;
+                       }
+                     break;
+                   }
+             }
+           else if (subtable->Format == 2)
+             {
+               OTF_GPOS_Pair2 *pair2 = &subtable->u.pair2;
+               unsigned class1, class2;
+
+               class1 = get_class_def (&pair2->ClassDef1, g->glyph_id);
+               class2 = get_class_def (&pair2->ClassDef2, nextg->glyph_id);
+               if (pair2->ValueFormat1)
+                 {
+                   g->positioning_type = lookup->LookupType;
+                   g->f.f2.format = pair2->ValueFormat1;
+                   g->f.f2.value
+                     = &pair2->Class1Record[class1].Class2Record[class2].Value1;
+                 }
+               gidx = next_gidx;
+               if (pair2->ValueFormat2)
+                 {
+                   nextg->positioning_type = lookup->LookupType;
+                   nextg->f.f2.format = pair2->ValueFormat2;
+                   nextg->f.f2.value
+                     = &pair2->Class1Record[class1].Class2Record[class2].Value2;
+                   gidx++;
+                 }
+             }
+         }
+         break;
+
        case 3:
-         OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
+         {
+           OTF_GPOS_Cursive1 *cursive1 = &subtable->u.cursive1;
+         
+           g->positioning_type = lookup->LookupType;
+           g->f.f3.entry_anchor
+             = &cursive1->EntryExitRecord[coverage_idx].EntryAnchor;
+           g->f.f3.exit_anchor
+             = &cursive1->EntryExitRecord[coverage_idx].ExitAnchor;
+         }
+         break;
 
        case 4:
          if (gidx < 1)
@@ -413,30 +812,127 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
            {
              OTF_GPOS_MarkBase1 *mark_base1 = &subtable->u.mark_base1;
              OTF_MarkRecord *mark_record;
-             OTF_BaseRecord *base_record;
-             OTF_Anchor *anchor1, *anchor2;
+             OTF_AnchorRecord *base_record;
              int coverage_idx_base
                = get_coverage_index (&mark_base1->BaseCoverage,
                                      g[-1].glyph_id);
 
              if (coverage_idx_base < 0)
                continue;
-             printf ("GPOS 4-1: c:0x%x g:0x%x\n", g->c, g->glyph_id);
              mark_record = mark_base1->MarkArray.MarkRecord + coverage_idx;
              base_record
-               = mark_base1->BaseArray.BaseRecord + coverage_idx_base;
-             anchor1 = &mark_record->MarkAnchor;
-             anchor2 = &base_record->BaseAnchor[mark_record->Class];
+               = mark_base1->BaseArray.AnchorRecord + coverage_idx_base;
+             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->f.f4.mark_anchor = anchor1;
-             g->f.f4.base_anchor = anchor2;
              break;
            }
-         else
-           OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
          break;
-             
+
+       case 5:
+         if (gidx < 1)
+           continue;
+         if (subtable->Format == 1)
+           {
+             /* As the document of this lookup type is quite
+                ambiguous, and we can't know the exact procedure to
+                handle it?!?  */
+             OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
+           }
+         break;
+
        case 6:
+         if (gidx < 1)
+           continue;
+         if (subtable->Format == 1)
+           {
+             OTF_GPOS_MarkMark1 *mark_mark1 = &subtable->u.mark_mark1;
+             OTF_MarkRecord *mark1_record;
+             OTF_AnchorRecord *mark2_record;
+             int coverage_idx_base
+               = get_coverage_index (&mark_mark1->Mark2Coverage,
+                                     g[-1].glyph_id);
+
+             if (coverage_idx_base < 0)
+               continue;
+             mark1_record = mark_mark1->Mark1Array.MarkRecord + coverage_idx;
+             mark2_record
+               = mark_mark1->Mark2Array.AnchorRecord + coverage_idx_base;
+             g->f.f6.mark1_anchor = &mark1_record->MarkAnchor;
+             g->f.f6.mark2_anchor
+               = &mark2_record->Anchor[mark1_record->Class];
+             g->positioning_type = lookup->LookupType;
+             break;
+           }
+         break;
+
+       case 7:
+         OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
+         break;
+
+       case 8:
+         if (subtable->Format == 1)
+           OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
+         else if (subtable->Format == 2)
+           {
+             OTF_GPOS_ChainContext2 *context2 = &subtable->u.chain_context2;
+             OTF_ChainClassSet *set;
+             unsigned class;
+             int j;
+             int orig_used;
+
+             class = get_class_def (&context2->InputClassDef, g->glyph_id);
+             set = context2->ChainClassSet + class;
+             for (j = 0; j < set->ChainClassRuleCnt; j++)
+               {
+                 OTF_ChainClassRule *rule = set->ChainClassRule + j;
+                 int fore_idx = gidx + rule->InputGlyphCount;
+                 int k;
+
+                 if (gidx < rule->BacktrackGlyphCount
+                     || (gidx + rule->InputGlyphCount
+                         + rule->LookaheadGlyphCount) > gstring->used)
+                   continue;
+                 for (k = 0; k < rule->BacktrackGlyphCount; k++)
+                   if (get_class_def (&context2->BacktrackClassDef,
+                                      gstring->glyphs[gidx - 1 - k].glyph_id)
+                       != rule->Backtrack[k])
+                     break;
+                 if (k < rule->BacktrackGlyphCount)
+                   continue;
+                 for (k = 1; k < rule->InputGlyphCount; k++)
+                   if (get_class_def (&context2->InputClassDef,
+                                      gstring->glyphs[gidx + k].glyph_id)
+                       != rule->Input[k - 1])
+                     break;
+                 if (k < rule->InputGlyphCount)
+                   continue;
+                 for (k = 0; k < rule->LookaheadGlyphCount; k++)
+                   if (get_class_def (&context2->LookaheadClassDef,
+                                      gstring->glyphs[fore_idx + k].glyph_id)
+                       != rule->LookAhead[k])
+                     break;
+                 if (k < rule->LookaheadGlyphCount)
+                   continue;
+
+                 orig_used = gstring->used;
+                 for (k = 0; k < rule->LookupCount; k++)
+                   lookup_gpos (lookup_list,
+                                rule->LookupRecord[k].LookupListIndex,
+                                gstring,
+                                gidx + rule->LookupRecord[k].SequenceIndex);
+                 gidx += rule->InputGlyphCount + (gstring->used - orig_used);
+                 break;
+               }
+           }
+         else if (subtable->Format == 3)
+           OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
+         else
+           OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (invalid subformat)");
+         break;
+
+       case 9:
          OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
          break;
 
@@ -445,80 +941,172 @@ lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
        }
     }
   if (gidx == orig_gidx)
+    gidx++;
+  return gidx;
+}
+
+static int
+lookup_encoding_0 (OTF_EncodingSubtable0 *sub0, OTF_GlyphString *gstring)
+{
+  int i, c;
+
+  for (i = 0; i < gstring->used; i++)
     {
-      // printf ("not applied\n");
-      gidx++;
-    }
-  else
-    {
-      // printf ("done\n");
+      c = gstring->glyphs[i].c;
+      if (c < 0 || c >= 256)
+       gstring->glyphs[i].glyph_id = 0;
+      else
+       gstring->glyphs[i].glyph_id = sub0->glyphIdArray[c];
     }
-  return gidx;
+  return 0;
 }
 
 static int
-lookup_cmap (OTF_cmap *cmap, int c)
+lookup_encoding_2 (OTF_EncodingSubtable2 *sub2, OTF_GlyphString *gstring)
 {
-  int i;
+  return 0;
+}
 
-  if (! cmap || ! cmap->Unicode)
-    return 0;
+static int
+lookup_encoding_4 (OTF_EncodingSubtable4 *sub4, OTF_GlyphString *gstring)
+{
+  int i, j, c;
+  int segCount = sub4->segCountX2 / 2;
 
-  switch (cmap->Unicode->subtable.format)
+  for (i = 0; i < gstring->used; i++)
     {
-    case 0:
-      break;
+      c = gstring->glyphs[i].c;
+      if (c < 0)
+       gstring->glyphs[i].glyph_id = 0;
+      for (j = 0; j < segCount; j++)
+       {
+         OTF_cmapSegument *seg = sub4->segments + i;
 
-    case 4:
-      {
-       OTF_EncodingSubtable4 *sub4 = cmap->Unicode->subtable.f.f4;
-       int segCount = sub4->segCountX2 / 2;
-
-       for (i = 0; i < segCount; i++)
-         if (c <= sub4->segments[i].endCount)
-           break;
-       if (i == segCount || c < sub4->segments[i].startCount)
-         return 0;
-       if (sub4->segments[i].idRangeOffset == 0xFFFF)
-         return c + sub4->segments[i].idDelta;
-       return sub4->glyphIdArray[sub4->segments[i].idRangeOffset
-                                 + (c - sub4->segments[i].startCount)];
-      }
-      break;
+         if (c >= seg->startCount && c <= seg->endCount)
+           {
+             if (seg->idRangeOffset == 0xFFFF)
+               gstring->glyphs[i].glyph_id = c + seg->idDelta;
+             else
+               gstring->glyphs[i].glyph_id
+                 = sub4->glyphIdArray[seg->idRangeOffset
+                                      + (c - seg->startCount)];
+             break;
+           }
+       }
     }
+
+  return 0;
+}
+
+static int
+lookup_encoding_6 (OTF_EncodingSubtable6 *sub6, OTF_GlyphString *gstring)
+{
+  return 0;
+}
+
+static int
+lookup_encoding_8 (OTF_EncodingSubtable8 *sub8, OTF_GlyphString *gstring)
+{
+  return 0;
+}
+
+static int
+lookup_encoding_10 (OTF_EncodingSubtable10 *sub10, OTF_GlyphString *gstring)
+{
+  return 0;
+}
+
+static int
+lookup_encoding_12 (OTF_EncodingSubtable12 *sub12, OTF_GlyphString *gstring)
+{
   return 0;
 }
 
 \f
 
-/* APIs */
+/* API */
 
 int
-otf_drive_cmap (OTF *otf, OTF_GlyphString *gstring)
+OTF_drive_cmap (OTF *otf, OTF_GlyphString *gstring)
 {
   OTF_cmap *cmap;
   int i;
 
   if (! otf->cmap
-      && otf_get_table (otf, "cmap") < 0)
+      && OTF_get_table (otf, "cmap") < 0)
     return -1;
 
   cmap = otf->cmap;
   for (i = 0; i < gstring->used; i++)
-      gstring->glyphs[i].glyph_id = lookup_cmap (cmap, gstring->glyphs[i].c);
-
+    if (! gstring->glyphs[i].glyph_id)
+      {
+       int c = gstring->glyphs[i].c;
+       if (c < 32 || ! cmap->unicode_table)
+         gstring->glyphs[i].glyph_id = 0;
+       else
+         gstring->glyphs[i].glyph_id = cmap->unicode_table[c];
+      }
   return 0;
 }
 
 
 int
-otf_drive_gdef (OTF *otf, OTF_GlyphString *gstring)
+OTF_drive_cmap2 (OTF *otf, OTF_GlyphString *gstring,
+                int platform_id, int encoding_id)
+{
+  OTF_cmap *cmap;
+  int i;
+  char *errfmt = "CMAP Looking up%s";
+  int errret = -1;
+  OTF_EncodingRecord *enc;
+
+  if (! otf->cmap
+      && OTF_get_table (otf, "cmap") < 0)
+    return -1;
+
+  cmap = otf->cmap;
+  for (i = 0; i < cmap->numTables; i++)
+    if (cmap->EncodingRecord[i].platformID == platform_id
+       && cmap->EncodingRecord[i].encodingID == encoding_id)
+      break;
+  if (i == cmap->numTables)
+    OTF_ERROR (OTF_ERROR_CMAP_DRIVE, " (unknown platformID/encodingID)");
+  enc = cmap->EncodingRecord + i;
+  switch (enc->subtable.format)
+    {
+    case 0: return lookup_encoding_0 (enc->subtable.f.f0, gstring);
+    case 2: return lookup_encoding_2 (enc->subtable.f.f2, gstring);
+    case 4: return lookup_encoding_4 (enc->subtable.f.f4, gstring);
+    case 6: return lookup_encoding_6 (enc->subtable.f.f6, gstring);
+    case 8: return lookup_encoding_8 (enc->subtable.f.f8, gstring);
+    case 10: return lookup_encoding_10 (enc->subtable.f.f10, gstring);
+    case 12: return lookup_encoding_12 (enc->subtable.f.f12, gstring);
+    }
+  OTF_ERROR (OTF_ERROR_CMAP_DRIVE, " (invalid format)");
+}
+
+
+int
+OTF_get_unicode (OTF *otf, OTF_GlyphID code)
+{
+  if (! otf->cmap
+      && OTF_get_table (otf, "cmap") < 0)
+    return 0;
+  if (code == 0
+      || code > otf->cmap->max_glyph_id
+      || ! otf->cmap->decode_table)
+    return 0;
+  return otf->cmap->decode_table[code];
+}
+
+int
+OTF_drive_gdef (OTF *otf, OTF_GlyphString *gstring)
 {
   OTF_GDEF *gdef;
   int i;
 
   if (! otf->gdef
-      && otf_get_table (otf, "GDEF") < 0)
+      && OTF_get_table (otf, "GDEF") < 0)
     return -1;
   gdef = otf->gdef;
 
@@ -539,46 +1127,63 @@ otf_drive_gdef (OTF *otf, OTF_GlyphString *gstring)
 
 
 int
-otf_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
+OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
                char *script, char *language, char *features)
 {
   char *errfmt = "GSUB driving%s";
   int errret = -1;
   OTF_GSUB *gsub;
   OTF_LangSys *LangSys;
-  int nfeatures;
-  int *feature_index;
-  int i, j;
+  int *lookup_indices;
+  int i, n;
 
   if (! otf->gsub
-      && otf_get_table (otf, "GSUB") < 0)
-    return -1;
+      && OTF_get_table (otf, "GSUB") < 0)
+    return errret;
   gsub = otf->gsub;
+  if (gsub->FeatureList.FeatureCount == 0
+      || gsub->LookupList.LookupCount == 0)
+    return 0;
 
   LangSys = get_langsys (&gsub->ScriptList, script, language);
   if (! LangSys)
-    return -1;
+    return errret;
 
-  feature_index = alloca (sizeof (int) * gsub->FeatureList.FeatureCount);
-  if (! feature_index)
+  /* One lookup may be used by multiple features.  */
+  lookup_indices = alloca (sizeof (int)
+                          * gsub->LookupList.LookupCount
+                          * (gsub->FeatureList.FeatureCount + 1));
+  if (! lookup_indices)
     OTF_ERROR (OTF_ERROR_MEMORY, " feature list");
+  n = setup_lookup_indices (&gsub->LookupList, &gsub->FeatureList,
+                           features, lookup_indices);
+  if (n < 0)
+    return errret;
 
-  nfeatures = get_feature_index (LangSys, &gsub->FeatureList,
-                                features, feature_index);
-  if (nfeatures == 0)
-    OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " no feature");
-
-  for (i = 0; i < nfeatures; i++)
+  for (i = 0; i < n; i++)
     {
-      OTF_Feature *feature = gsub->FeatureList.Feature + feature_index[i];
+      int index = lookup_indices[i];
+      int gidx;
 
-      for (j = 0; j < feature->LookupCount; j++)
+      if (gsub->LookupList.Lookup[index].LookupType != 8)
        {
-         int gidx = 0;
-
+         gidx = 0;
          while (gidx < gstring->used)
-           gidx = lookup_gsub (&gsub->LookupList, feature->LookupListIndex[j],
-                               gstring, gidx);
+           {
+             gidx = lookup_gsub (&gsub->LookupList, index, gstring, gidx);
+             if (gidx < 0)
+               return errret;
+           }
+       }
+      else
+       {
+         gidx = gstring->used - 1;
+         while (gidx >= 0)
+           {
+             gidx = lookup_gsub (&gsub->LookupList, index, gstring, gidx);
+             if (gidx < 0)
+               return errret;
+           }
        }
     }
 
@@ -586,46 +1191,49 @@ otf_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
 }
 
 int
-otf_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
+OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
                char *script, char *language, char *features)
 {
   char *errfmt = "GPOS driving%s";
   int errret = -1;
   OTF_GPOS *gpos;
   OTF_LangSys *LangSys;
-  int nfeatures;
-  int *feature_index;
-  int i, j;
+  int *lookup_indices;
+  int i, n;
 
   if (! otf->gpos
-      && otf_get_table (otf, "GPOS") < 0)
-    return -1;
+      && OTF_get_table (otf, "GPOS") < 0)
+    return errret;
   gpos = otf->gpos;
+  if (gpos->FeatureList.FeatureCount == 0
+      || gpos->LookupList.LookupCount == 0)
+    return 0;
 
   LangSys = get_langsys (&gpos->ScriptList, script, language);
   if (! LangSys)
-    return -1;
+    return errret;
 
-  feature_index = alloca (sizeof (int) * gpos->FeatureList.FeatureCount);
-  if (! feature_index)
+  /* One lookup may be used by multiple features.  */
+  lookup_indices = alloca (sizeof (int)
+                          * gpos->LookupList.LookupCount
+                          * (gpos->FeatureList.FeatureCount + 1));
+  if (! lookup_indices)
     OTF_ERROR (OTF_ERROR_MEMORY, " feature list");
+  n = setup_lookup_indices (&gpos->LookupList, &gpos->FeatureList,
+                           features, lookup_indices);
+  if (n < 0)
+    return errret;
 
-  nfeatures = get_feature_index (LangSys, &gpos->FeatureList,
-                                features, feature_index);
-  if (nfeatures == 0)
-    OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " no feature");
-
-  for (i = 0; i < nfeatures; i++)
+  for (i = 0; i < n; i++)
     {
-      OTF_Feature *feature = gpos->FeatureList.Feature + feature_index[i];
+      int index = lookup_indices[i];
+      int gidx = 0;
 
-      for (j = 0; j < feature->LookupCount; j++)
+      while (gidx < gstring->used)
        {
-         int gidx = 0;
-
-         while (gidx < gstring->used)
-           gidx = lookup_gpos (&gpos->LookupList, feature->LookupListIndex[j],
-                               gstring, gidx);
+         gidx = lookup_gpos (&gpos->LookupList, index, gstring, gidx);
+         if (gidx < 0)
+           return errret;
        }
     }
 
@@ -633,16 +1241,19 @@ otf_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
 }
 
 int
-otf_drive_tables (OTF *otf, OTF_GlyphString *gstring,
-                 char *script, char *language)
+OTF_drive_tables (OTF *otf, OTF_GlyphString *gstring,
+                 char *script, char *language,
+                 char *gsub_features, char *gpos_features)
 {
-  if (otf_drive_cmap (otf, gstring) < 0)
+  if (OTF_drive_cmap (otf, gstring) < 0)
     return -1;
-  if (otf_drive_gdef (otf, gstring) < 0)
+  if (OTF_drive_gdef (otf, gstring) < 0)
     return -1;
-  if (otf_drive_gsub (otf, gstring, script, language, NULL) < 0)
+  if (gsub_features
+      && OTF_drive_gsub (otf, gstring, script, language, gsub_features) < 0)
     return -1;
-  if (otf_drive_gpos (otf, gstring, script, language, NULL) < 0)
+  if (gpos_features
+      && OTF_drive_gpos (otf, gstring, script, language, gpos_features) < 0)
     return -1;
   return 0;
 }