Include <fontconfig/fcfreetype.h> if HAVE_XFT2.
[m17n/m17n-lib.git] / src / font-flt.c
index 1522d6a..b4fdc69 100644 (file)
@@ -263,6 +263,17 @@ static MSymbol Mcond, Mrange;
 
 #define GLYPH_CODE_INDEX(code) ((code) - GLYPH_CODE_MIN)
 
+#define UPDATE_CLUSTER_RANGE(ctx, g)           \
+  do {                                         \
+    if ((ctx)->cluster_begin_idx)              \
+      {                                                \
+       if (ctx->cluster_begin_pos > (g).pos)   \
+         ctx->cluster_begin_pos = (g).pos;     \
+       if (ctx->cluster_end_pos < (g).to)      \
+         ctx->cluster_end_pos = (g).to;        \
+      }                                                \
+  } while (0);
+
 enum FontLayoutCmdRuleSrcType
   {
     SRC_REGEX,
@@ -304,14 +315,6 @@ typedef struct
   int *cmd_ids;
 } FontLayoutCmdCond;
 
-typedef struct
-{
-  MSymbol script;
-  MSymbol langsys;
-  MSymbol gsub_features;
-  MSymbol gpos_features;
-} FontLayoutCmdOTF;
-
 enum FontLayoutCmdType
   {
     FontLayoutCmdTypeRule,
@@ -322,6 +325,14 @@ enum FontLayoutCmdType
 
 typedef struct
 {
+  MSymbol script;
+  MSymbol langsys;
+  MSymbol gsub_features;
+  MSymbol gpos_features;
+} FontLayoutCmdOTF;
+
+typedef struct
+{
   enum FontLayoutCmdType type;
   union {
     FontLayoutCmdRule rule;
@@ -856,6 +867,7 @@ load_flt (MSymbol layouter_name)
   MPlist *top = NULL, *plist;
   MSymbol Mcategory = msymbol ("category");
   MSymbol Mgenerator = msymbol ("generator");
+  MSymbol Mend = msymbol ("end");
   MFontLayoutTable *layouter = NULL;
   MCharTable *category = NULL;
 
@@ -871,6 +883,9 @@ load_flt (MSymbol layouter_name)
       MSymbol sym;
       MPlist *elt;
 
+      if (MPLIST_SYMBOL_P (plist)
+         && MPLIST_SYMBOL (plist) == Mend)
+       break;
       if (! MPLIST_PLIST (plist))
        MERROR_GOTO (MERROR_FONT, finish);
       elt = MPLIST_PLIST (plist);
@@ -964,7 +979,6 @@ typedef struct
   int cluster_end_pos;
   int combining_code;
   int left_padding;
-  MRealizedFont *rfont;
 } FontLayoutContext;
 
 static int run_command (int depth,
@@ -983,14 +997,6 @@ run_rule (int depth,
   int i;
   int orig_from = from;
 
-  if (ctx->cluster_begin_idx)
-    {
-      if (ctx->cluster_begin_pos > MGLYPH (from)->pos)
-       ctx->cluster_begin_pos = MGLYPH (from)->pos;
-      if (ctx->cluster_end_pos < MGLYPH (to)->pos)
-       ctx->cluster_end_pos = MGLYPH (to)->pos;
-    }
-
   if (rule->src_type == SRC_SEQ)
     {
       int len;
@@ -1004,7 +1010,8 @@ run_rule (int depth,
       if (i < len)
        return 0;
       to = from + len;
-      MDEBUG_PRINT1 (" (SEQ 0x%X", rule->src.seq.codes[0]);
+      MDEBUG_PRINT3 ("\n [FLT] %*s(SEQ 0x%X", depth, "",
+                    rule->src.seq.codes[0]);
     }
   else if (rule->src_type == SRC_RANGE)
     {
@@ -1017,7 +1024,7 @@ run_rule (int depth,
        return 0;
       ctx->code_offset = head - rule->src.range.from;
       to = from + 1;
-      MDEBUG_PRINT2 (" (RANGE 0x%X-0x%X",
+      MDEBUG_PRINT4 ("\n [FLT] %*s(RANGE 0x%X-0x%X", depth, "",
                     rule->src.range.from, rule->src.range.to);
     }
   else if (rule->src_type == SRC_REGEX)
@@ -1035,7 +1042,7 @@ run_rule (int depth,
                        NMATCH, pmatch, 0);
       if (result == 0 && pmatch[0].rm_so == 0)
        {
-         MDEBUG_PRINT3 (" (REGEX \"%s\" \"%s\" %d",
+         MDEBUG_PRINT5 ("\n [FLT] %*s(REGEX \"%s\" \"%s\" %d", depth, "",
                         rule->src.re.pattern,
                         ctx->encoded + from - ctx->encoded_offset,
                         pmatch[0].rm_eo);
@@ -1067,7 +1074,7 @@ run_rule (int depth,
       if (from < 0)
        return 0;
       to = ctx->match_indices[rule->src.match_idx * 2 + 1];
-      MDEBUG_PRINT1 (" (INDEX %d", rule->src.match_idx);
+      MDEBUG_PRINT3 ("\n [FLT] %*s(INDEX %d", depth, "", rule->src.match_idx);
     }
 
   consumed = 0;
@@ -1124,13 +1131,19 @@ run_otf (int depth,
         FontLayoutContext *ctx)
 {
 #ifdef HAVE_OTF
-  int gidx = gstring->used;
-
-  to = mfont__ft_drive_otf (gstring, from, to, ctx->rfont,
+  int from_idx = gstring->used;
+
+  MDEBUG_PRINT4 ("\n [FLT] %*s(OTF %s,%s)", depth, "",
+                (otf_cmd->gsub_features == Mnil ? ""
+                 : MSYMBOL_NAME (otf_cmd->gsub_features)),
+                (otf_cmd->gpos_features == Mnil ? ""
+                 : MSYMBOL_NAME (otf_cmd->gpos_features)));
+  to = mfont__ft_drive_otf (gstring, from, to,
                            otf_cmd->script, otf_cmd->langsys,
                            otf_cmd->gsub_features, otf_cmd->gpos_features);
-  if (gidx < gstring->used)
-    MGLYPH (gidx)->left_padding = ctx->left_padding;
+  if (ctx->cluster_begin_idx)
+    for (; from_idx < gstring->used; from_idx++)
+      UPDATE_CLUSTER_RANGE (ctx, gstring->glyphs[from_idx]);
 #endif
   return to;
 }
@@ -1153,7 +1166,7 @@ run_command (int depth, int id, MGlyphString *gstring, int from, int to,
        g = *(MGLYPH (from - 1));
       g.type = GLYPH_CHAR;
       g.code = ctx->code_offset + id;
-      MDEBUG_PRINT1 (" (DIRECT 0x%X", g.code);
+      MDEBUG_PRINT3 ("\n [FLT] %*s(DIRECT 0x%X", depth, "", g.code);
       if (ctx->combining_code)
        g.combining_code = ctx->combining_code;
       if (ctx->left_padding)
@@ -1168,6 +1181,7 @@ run_command (int depth, int id, MGlyphString *gstring, int from, int to,
            g.to = tmp->to;
        }
       APPEND_GLYPH (gstring, g);
+      UPDATE_CLUSTER_RANGE (ctx, g);
       ctx->code_offset = ctx->combining_code = ctx->left_padding = 0;
       MDEBUG_PRINT (")");
       return (from);
@@ -1211,6 +1225,8 @@ run_command (int depth, int id, MGlyphString *gstring, int from, int to,
        if (ctx->left_padding)
          g.left_padding = ctx->left_padding;
        APPEND_GLYPH (gstring, g);
+       UPDATE_CLUSTER_RANGE (ctx, g);
+       MDEBUG_PRINT3 ("\n [FLT] %*s(COPY 0x%X)", depth, "", g.code);
        ctx->code_offset = ctx->combining_code = ctx->left_padding = 0;
        return (from + 1);
       }
@@ -1218,7 +1234,7 @@ run_command (int depth, int id, MGlyphString *gstring, int from, int to,
     case CMD_ID_CLUSTER_BEGIN:
       if (! ctx->cluster_begin_idx)
        {
-         MDEBUG_PRINT1 (" <%d", MGLYPH (from)->pos);
+         MDEBUG_PRINT3 ("\n [FLT] %*s<%d", depth, "", MGLYPH (from)->pos);
          ctx->cluster_begin_idx = gstring->used;
          ctx->cluster_begin_pos = MGLYPH (from)->pos;
          ctx->cluster_end_pos = MGLYPH (from)->to;
@@ -1342,7 +1358,6 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
 
   /* Setup CTX.  */
   memset (&ctx, 0, sizeof ctx);
-  ctx.rfont = rface->rfont;
   table = MPLIST_VAL (layouter);
   layouter = MPLIST_NEXT (layouter);
   stage = (FontLayoutStage *) MPLIST_VAL (layouter);
@@ -1384,11 +1399,28 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
       int len = to - from;
       int result;
 
-      MDEBUG_PRINT1 ("\n [FLT]   (STAGE %d", stage_idx);
+      MDEBUG_PRINT2 ("\n [FLT]   (STAGE %d \"%s\"", stage_idx, ctx.encoded);
+      if (mdebug__flag & mdebug_mask
+         && ctx.encoded_offset < to)
+       {
+         if (gstring->glyphs[ctx.encoded_offset].type == GLYPH_PAD)
+           fprintf (stderr, " (|");
+         else
+           fprintf (stderr, " (%X", gstring->glyphs[ctx.encoded_offset].code);
+         for (i = ctx.encoded_offset + 1; i < to; i++)
+           {
+             if (gstring->glyphs[i].type == GLYPH_PAD)
+               fprintf (stderr, " |");
+             else
+               fprintf (stderr, " %X", gstring->glyphs[i].code);
+           }
+         fprintf (stderr, ")");
+       }
+
       gidx = gstring->used;
       ctx.stage = stage;
 
-      result = run_command (2, INDEX_TO_CMD_ID (0), gstring,
+      result = run_command (4, INDEX_TO_CMD_ID (0), gstring,
                            ctx.encoded_offset, to, &ctx);
       MDEBUG_PRINT (")");
       if (result < 0)
@@ -1418,7 +1450,7 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
            ctx.encoded[i - from] = ' ';
          else if (! g->otf_encoded)
            ctx.encoded[i - from] = (int) mchartable_lookup (table, g->code);
-#ifdef HAVE_FREETYPE
+#if defined (HAVE_FREETYPE) && defined (HAVE_OTF)
          else
            {
              int c = mfont__ft_decode_otf (g);
@@ -1431,7 +1463,7 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
                }
              ctx.encoded[i - from] = (c >= 0 ? c : 1);
            }
-#endif /* HAVE_FREETYPE */
+#endif /* HAVE_FREETYPE && HAVE_OTF */
        }
       ctx.encoded[i - from] = '\0';
       ctx.encoded_offset = from;
@@ -1439,8 +1471,6 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
       ctx.match_indices[1] = to;
     }
 
-  MDEBUG_PRINT (")\n");
-
   if (from == to)
     {
       /* Somehow there's no glyph contributing to characters between
@@ -1457,15 +1487,19 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
     }
   else
     {
-      /* Here we must check if all characters in the range is covered
-        by some glyph(s).  If not, change <pos> and <to> of glyphs to
-        cover uncovered characters.  */
+      /* Get actual glyph IDs of glyphs.  Also check if all characters
+        in the range is covered by some glyph(s).  If not, change
+        <pos> and <to> of glyphs to cover uncovered characters.  */
       int len = to_pos - from_pos;
       int pos;
       MGlyph **glyphs = alloca (sizeof (MGlyph) * len);
       MGlyph *g, *gend = MGLYPH (to);
       MGlyph *latest = gend;
 
+      for (g = MGLYPH (from); g != gend; g++)
+       if (g->type == GLYPH_CHAR && ! g->otf_encoded)
+         g->code
+           = (rface->rfont->driver->encode_char) (rface->rfont, g->code);
       for (i = 0; i < len; i++)
        glyphs[i] = NULL;
       for (g = MGLYPH (from); g != gend; g++)
@@ -1497,6 +1531,25 @@ mfont__flt_run (MGlyphString *gstring, int from, int to, MRealizedFace *rface)
            latest = glyphs[i];
        }
     }
+  MDEBUG_PRINT ("\n [FLT]   (RESULT (");
+  if (mdebug__flag & mdebug_mask
+      && ctx.encoded_offset < to)
+    {
+      if (gstring->glyphs[from].type == GLYPH_PAD)
+       fprintf (stderr, "|");
+      else
+       fprintf (stderr, "%X", gstring->glyphs[from].code);
+      for (from++; from < to; from++)
+       {
+         if (gstring->glyphs[from].type == GLYPH_PAD)
+           fprintf (stderr, " |");
+         else
+           fprintf (stderr, " %X", gstring->glyphs[from].code);
+       }
+      fprintf (stderr, "))");
+    }
+  MDEBUG_PRINT (")\n");
+
   return to;
 }