*** empty log message ***
[m17n/m17n-test.git] / flt.c
diff --git a/flt.c b/flt.c
index 70e6abe..884782a 100644 (file)
--- a/flt.c
+++ b/flt.c
@@ -20,7 +20,7 @@
 #elif defined (FLT_HB)
 
 #include <m17n-flt.h>
-#include <harfbuzz.h>
+#include <hb.h>
 #define PROGNAME "flt-hb"
 
 #else  /* (defined (FLT_PANGO)) */
 #include <m17n-flt.h>
 #define PANGO_ENABLE_ENGINE
 #define PANGO_ENABLE_BACKEND
-#include <pango/pango.h>
+#include <pango/pangcairo.h>
 #include <pango/pango-ot.h>
-#include <pango/pangoft2.h>
-#include <pango/pangofc-font.h>
+
 #endif
 
+#include <m17n-misc.h>
+
 static FT_Library ft_library;
 
 int
@@ -119,7 +120,7 @@ struct _MFLTFont
 {
   int x_ppem, y_ppem;
   void (*get_glyph_id) (void);
-  void (*get_metric) (void);
+  void (*get_metrics) (void);
   void (*suitable_p) (void);
   void (*drive_otf) (void);
   MFont *font;
@@ -127,7 +128,7 @@ struct _MFLTFont
 };
 
 void get_glyph_id (void) {}
-void get_metric (void) {}
+void get_metrics (void) {}
 void suitable_p (void) {}
 void drive_otf (void) {}
 
@@ -194,7 +195,7 @@ get_glyph_id (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
 }
 
 int 
-get_metric (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
+get_metrics (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
 {
   FT_Face face = ((FontInfo *) font)->face;
 
@@ -300,61 +301,74 @@ encode_features (char *str, int count, unsigned *features)
   *str = '\0';
 }
 
+static OTF_GlyphString otf_gstring;
+
+static void
+setup_otf_gstring (int size)
+{
+  if (otf_gstring.size == 0)
+    {
+      otf_gstring.glyphs = (OTF_Glyph *) xmalloc (sizeof (OTF_Glyph) * size);
+      otf_gstring.size = size;
+    }
+  else if (otf_gstring.size < size)
+    {
+      otf_gstring.glyphs = xrealloc (otf_gstring.glyphs,
+                                    sizeof (OTF_Glyph) * size);
+      otf_gstring.size = size;
+    }
+  otf_gstring.used = size;
+  memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * size);
+}
+
 int
 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
           MFLTGlyphString *in, int from, int to,
           MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
 {
-  int len = to - from;
-  int i, gidx;
   OTF *otf = ((FontInfoOTF *) font)->otf;
-  OTF_GlyphString otf_gstring;
+  int len = to - from;
+  int i, j, gidx;
   OTF_Glyph *otfg;
-  char script[4], langsys[4];
+  char script[5], *langsys = NULL;
   char *gsub_features = NULL, *gpos_features = NULL;
 
   if (len == 0)
     return from;
 
-  if (! otf)
-    goto simple_copy;
-  otf_gstring.glyphs = NULL;
-  if (OTF_get_table (otf, "head") < 0)
-    {
-      OTF_close (otf);
-      ((FontInfoOTF *) font)->otf = NULL;
-      goto simple_copy;
-    }
-
-  tag_name (script, spec->script);
-  tag_name (langsys, spec->langsys);
-
-  if (spec->gsub_count > 0)
+  OTF_tag_name (spec->script, script);
+  if (spec->langsys)
     {
-      gsub_features = alloca (6 * spec->gsub_count);
-      if (gsub_features)
-       {
-         if (OTF_check_table (otf, "GSUB") < 0)
-           gsub_features = NULL;
-         else
-           encode_features (gsub_features, spec->gsub_count, spec->gsub);
-       }
+      langsys = alloca (5);
+      OTF_tag_name (spec->langsys, langsys);
     }
-  if (spec->gpos_count)
+  for (i = 0; i < 2; i++)
     {
-      gpos_features = alloca (6 * spec->gpos_count);
-      if (gpos_features)
+      char *p;
+
+      if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
        {
-         if (OTF_check_table (otf, "GPOS") < 0)
-           gpos_features = NULL;
+         for (j = 0; spec->features[i][j]; j++);
+         if (i == 0)
+           p = gsub_features = alloca (6 * j);
          else
-           encode_features (gpos_features, spec->gpos_count, spec->gpos);
+           p = gpos_features = alloca (6 * j);
+         for (j = 0; spec->features[i][j]; j++)
+           {
+             if (spec->features[i][j] == 0xFFFFFFFF)
+               *p++ = '*', *p++ = ',';
+             else
+               {
+                 OTF_tag_name (spec->features[i][j], p);
+                 p[4] = ',';
+                 p += 5;
+               }
+           }
+         *--p = '\0';
        }
     }
 
-  otf_gstring.size = otf_gstring.used = len;
-  otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
-  memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
+  setup_otf_gstring (len);
   for (i = 0; i < len; i++)
     {
       otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
@@ -384,7 +398,11 @@ drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
                g->c = in->glyphs[j].c;
                break;
              }
-         g->code = otfg->glyph_id;
+         if (g->code != otfg->glyph_id)
+           {
+             g->code = otfg->glyph_id;
+             g->measured = 0;
+           }
          out->used++;
        }
     }
@@ -396,8 +414,6 @@ drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
        out->glyphs[out->used++] = in->glyphs[from + i];
     }
 
-  font->get_metric (font, out, gidx, out->used);
-
   if (gpos_features)
     {
       FT_Face face;
@@ -502,7 +518,7 @@ drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
   return to;
 
  simple_copy:
-  font->get_metric (font, in, from, to);
+  font->get_metrics (font, in, from, to);
   for (i = 0; i < len; i++)
     {
       MFLTGlyph *g = in->glyphs + (from + i);
@@ -525,6 +541,13 @@ open_font (char *fontname, char **err)
   font_info = malloc (sizeof (FontInfoOTF));
   font_info->face = face;
   font_info->otf = OTF_open_ft_face (face);
+  if (OTF_get_table (font_info->otf, "head") < 0)
+    {
+      OTF_close (font_info->otf);
+      FT_Done_Face (font_info->face);
+      free (font_info);
+      return NULL;
+    }
   return ((MFLTFont *) font_info);
 }
 
@@ -912,7 +935,11 @@ drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
       out->glyphs[out->used] = in->glyphs[hg->cluster];
       g = out->glyphs + out->used++;
       if (g->code != hg->gindex)
-       g->c = 0, g->code = hg->gindex;
+       {
+         g->c = 0;
+         g->code = hg->gindex;
+         g->measured = 0;
+       }
       adjustment[i].set = gpos_applied;
       if (gpos_applied)
        {
@@ -1029,7 +1056,7 @@ get_glyph_id (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
 #define PANGO_SCALE_TO_26_6 (PANGO_SCALE / (1<<6))
 
 int
-get_metric (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
+get_metrics (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
 {
   FontInfoPango *font_info = (FontInfoPango *) font;
   int i;
@@ -1199,7 +1226,11 @@ drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
       out->glyphs[out->used] = in->glyphs[glyphs->log_clusters[i]];
       g = out->glyphs + out->used++;
       if (g->code != glyph_info->glyph)
-       g->c = 0, g->code = glyph_info->glyph;
+       {
+         g->c = 0;
+         g->code = glyph_info->glyph;
+         g->measured = 0;
+       }
       g->xoff = glyph_info->geometry.x_offset / PANGO_SCALE_TO_26_6;
       g->yoff = glyph_info->geometry.y_offset / PANGO_SCALE_TO_26_6;
       g->xadv = glyph_info->geometry.width / PANGO_SCALE_TO_26_6;
@@ -1347,6 +1378,7 @@ main (int argc, char **argv)
   MText *mt;
   MFLTFont *font;
   int len, i, j;
+  unsigned char *buf;
 
   if (argc < 3)
     {
@@ -1366,10 +1398,17 @@ main (int argc, char **argv)
       exit (1);
     }
   font->get_glyph_id = get_glyph_id;
-  font->get_metric = get_metric;
+  font->get_metrics = get_metrics;
   font->drive_otf = drive_otf;
 
-  mt = mconv_decode_stream (msymbol ("utf-8"), stdin);
+  i = 0;
+  buf = malloc (4096);
+  while ((j = fread (buf + i, 1, 4096, stdin)) == 4096)
+    {
+      i += 4096;
+      buf = realloc (buf, i + 4096);
+    }
+  mt = mtext_from_data (buf, i, MTEXT_FORMAT_UTF_8);
   len = mtext_len (mt);
   for (i = 0, j = mtext_character (mt, i, len, '\n'); i < len;
        i = j + 1, j = mtext_character (mt, i, len, '\n'))
@@ -1387,6 +1426,7 @@ main (int argc, char **argv)
       i = j;
     }
   m17n_object_unref (mt);
+  free (buf);
   close_font (font);
   M17N_FINI ();
   return 0;