]> code.delx.au - gnu-emacs/blobdiff - src/ftfont.c
xfns.c (Fx_create_frame): If frame height i stoo big, try
[gnu-emacs] / src / ftfont.c
index f2fa877c5543b1bce2bf50bfdf98f9202c87ea17..492d03bb8bf0201f9654a7a8d8f904e72768c3c2 100644 (file)
@@ -1,6 +1,6 @@
 /* ftfont.c -- FreeType font driver.
-   Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
-   Copyright (C) 2006, 2007, 2008, 2009
+   Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2007, 2008, 2009, 2010
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H13PRO009
 
@@ -21,6 +21,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <stdio.h>
+#include <setjmp.h>
 
 #include <fontconfig/fontconfig.h>
 #include <fontconfig/fcfreetype.h>
@@ -43,9 +44,6 @@ Lisp_Object Qfreetype;
 /* Fontconfig's generic families and their aliases.  */
 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
 
-/* Special ADSTYLE properties to avoid fonts used for Latin characters.  */
-static Lisp_Object Qja, Qko;
-
 /* Flag to tell if FcInit is already called or not.  */
 static int fc_initialized;
 
@@ -72,14 +70,25 @@ struct ftfont_info
 #endif /* HAVE_LIBOTF */
   FT_Size ft_size;
   int index;
+  FT_Matrix matrix;
 };
 
+enum ftfont_cache_for
+  {
+    FTFONT_CACHE_FOR_FACE,
+    FTFONT_CACHE_FOR_CHARSET,
+    FTFONT_CACHE_FOR_ENTITY
+  };
+
 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
 
 static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object,
                                                      FcPattern *));
-static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, int));
+static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object,
+                                           enum ftfont_cache_for));
 
+static void ftfont_filter_properties P_ ((Lisp_Object font, Lisp_Object alist));
+                                                
 Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
 
 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
@@ -95,7 +104,7 @@ static struct
   /* set on demand */
   FcCharSet *fc_charset;
 } fc_charset_table[] =
-  { { "iso8859-1", { } },      /* ftfont_get_latin1_charset handles it. */
+  { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
     { "iso8859-2", { 0x00A0, 0x010E }},
     { "iso8859-3", { 0x00A0, 0x0108 }},
     { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
@@ -135,29 +144,6 @@ static struct
     { NULL }
   };
 
-/* Return a FcCharSet for iso8859-1 from fc_charset_table[0].  If the
-   charset is not yet ready, create it. */
-static FcCharSet *
-ftfont_get_latin1_charset ()
-{
-  FcCharSet *cs;
-  FcChar32 c;
-
-  if (fc_charset_table[0].fc_charset)
-    return fc_charset_table[0].fc_charset;
-  cs = FcCharSetCreate ();
-  if (! cs)
-    return NULL;
-  for (c = 33; c <= 0xFF; c++)
-    {
-      FcCharSetAddChar (cs, c);
-      if (c == 0x7E)
-       c = 0xA0;
-    }
-  fc_charset_table[0].fc_charset = cs;
-  return cs;
-}
-
 extern Lisp_Object Qc, Qm, Qp, Qd;
 
 /* Dirty hack for handing ADSTYLE property.
@@ -195,7 +181,7 @@ get_adstyle_property (FcPattern *p)
       || xstrcasecmp (str, "Oblique") == 0
       || xstrcasecmp (str, "Italic") == 0)
     return Qnil;
-  adstyle = font_intern_prop (str, end - str, 0);
+  adstyle = font_intern_prop (str, end - str, 1);
   if (font_style_to_value (FONT_WIDTH_INDEX, adstyle, 0) >= 0)
     return Qnil;
   return adstyle;
@@ -206,7 +192,7 @@ ftfont_pattern_entity (p, extra)
      FcPattern *p;
      Lisp_Object extra;
 {
-  Lisp_Object entity;
+  Lisp_Object key, cache, entity;
   char *file, *str;
   int index;
   int numeric;
@@ -218,7 +204,21 @@ ftfont_pattern_entity (p, extra)
   if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
     return Qnil;
 
+  key = Fcons (make_unibyte_string ((char *) file, strlen ((char *) file)),
+              make_number (index));
+  cache = ftfont_lookup_cache (key, FTFONT_CACHE_FOR_ENTITY);
+  entity = XCAR (cache);
+  if (! NILP (entity))
+    {
+      Lisp_Object val = font_make_entity ();
+      int i;
+
+      for (i = 0; i < FONT_OBJLIST_INDEX; i++)
+       ASET (val, i, AREF (entity, i));
+      return val;
+    }
   entity = font_make_entity ();
+  XSETCAR (cache, entity);
 
   ASET (entity, FONT_TYPE_INDEX, Qfreetype);
   ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
@@ -244,12 +244,7 @@ ftfont_pattern_entity (p, extra)
     }
   if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
     {
-      Lisp_Object adstyle;
-
       ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
-      /* As this font has PIXEL_SIZE property, parhaps this is a BDF
-        or PCF font. */ 
-      ASET (entity, FONT_ADSTYLE_INDEX, get_adstyle_property (p));
     }
   else
     ASET (entity, FONT_SIZE_INDEX, make_number (0));
@@ -262,13 +257,31 @@ ftfont_pattern_entity (p, extra)
     }
   if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
       && b == FcTrue)
-    ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
+    {
+      ASET (entity, FONT_SIZE_INDEX, make_number (0));
+      ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
+    }
+  else
+    {
+      /* As this font is not scalable, parhaps this is a BDF or PCF
+        font. */ 
+      FT_Face ft_face;
+
+      ASET (entity, FONT_ADSTYLE_INDEX, get_adstyle_property (p));
+      if ((ft_library || FT_Init_FreeType (&ft_library) == 0)
+         && FT_New_Face (ft_library, file, index, &ft_face) == 0)
+       {
+         BDF_PropertyRec rec;
+
+         if (FT_Get_BDF_Property (ft_face, "AVERAGE_WIDTH", &rec) == 0
+             && rec.type == BDF_PROPERTY_TYPE_INTEGER)
+           ASET (entity, FONT_AVGWIDTH_INDEX, make_number (rec.u.integer));
+         FT_Done_Face (ft_face);
+       }
+    }
 
   ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
-  font_put_extra (entity, QCfont_entity,
-                 Fcons (make_unibyte_string ((char *) file,
-                                             strlen ((char *) file)),
-                        make_number (index)));
+  font_put_extra (entity, QCfont_entity, key);
   return entity;
 }
 
@@ -335,9 +348,9 @@ struct ftfont_cache_data
 };
 
 static Lisp_Object
-ftfont_lookup_cache (key, for_face)
+ftfont_lookup_cache (key, cache_for)
      Lisp_Object key;
-     int for_face;
+     enum ftfont_cache_for cache_for;
 {
   Lisp_Object cache, val, entity;
   struct ftfont_cache_data *cache_data;
@@ -352,29 +365,45 @@ ftfont_lookup_cache (key, for_face)
   else
     entity = Qnil;
 
-  cache = assoc_no_quit (key, ft_face_cache);
+  if (NILP (ft_face_cache))
+    cache = Qnil;
+  else
+    cache = Fgethash (key, ft_face_cache, Qnil);
   if (NILP (cache))
     {
+      if (NILP (ft_face_cache))
+       {
+         Lisp_Object args[2];
+
+         args[0] = QCtest;
+         args[1] = Qequal;
+         ft_face_cache = Fmake_hash_table (2, args);
+       }
       cache_data = xmalloc (sizeof (struct ftfont_cache_data));
       cache_data->ft_face = NULL;
       cache_data->fc_charset = NULL;
       val = make_save_value (NULL, 0);
       XSAVE_VALUE (val)->integer = 0;
       XSAVE_VALUE (val)->pointer = cache_data;
-      cache = Fcons (key, val);
-      ft_face_cache = Fcons (cache, ft_face_cache);
+      cache = Fcons (Qnil, val);
+      Fputhash (key, cache, ft_face_cache);
     }
   else
     {
       val = XCDR (cache);
       cache_data = XSAVE_VALUE (val)->pointer;
     }
-  if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset)
+
+  if (cache_for == FTFONT_CACHE_FOR_ENTITY)
+    return cache;
+
+  if (cache_for == FTFONT_CACHE_FOR_FACE
+      ? ! cache_data->ft_face : ! cache_data->fc_charset)
     {
       char *filename = (char *) SDATA (XCAR (key));
       int index = XINT (XCDR (key));
 
-      if (for_face)
+      if (cache_for == FTFONT_CACHE_FOR_FACE)
        {
          if (! ft_library
              && FT_Init_FreeType (&ft_library) != 0)
@@ -400,31 +429,11 @@ ftfont_lookup_cache (key, for_face)
          fontset = FcFontList (NULL, pat, objset);
          if (! fontset)
            goto finish;
-         if (fontset && fontset->nfont > 0)
-           {
-             if (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0,
+         if (fontset && fontset->nfont > 0
+             && (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0,
                                       &charset)
-                 == FcResultMatch)
-               {
-                 /* Dirty hack.  Fonts of "ja" and "ko" adstyle are
-                    not suitable for Latin characters.  */
-                 if (! NILP (entity)
-                     && (EQ (AREF (entity, FONT_ADSTYLE_INDEX), Qja)
-                         || EQ (AREF (entity, FONT_ADSTYLE_INDEX), Qko)))
-                   {
-                     FcCharSet *latin1 = ftfont_get_latin1_charset ();
-
-                     if (! latin1)
-                       goto finish;
-                     cache_data->fc_charset = FcCharSetSubtract (charset,
-                                                                 latin1);
-                   }
-                 else
-                   cache_data->fc_charset = FcCharSetCopy (charset);
-               }
-             else
-               cache_data->fc_charset = FcCharSetCreate ();
-           }
+                 == FcResultMatch))
+           cache_data->fc_charset = FcCharSetCopy (charset);
          else
            cache_data->fc_charset = FcCharSetCreate ();
 
@@ -447,7 +456,7 @@ ftfont_get_fc_charset (entity)
   Lisp_Object val, cache;
   struct ftfont_cache_data *cache_data;
 
-  cache = ftfont_lookup_cache (entity, 0);
+  cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_CHARSET);
   val = XCDR (cache);
   cache_data = XSAVE_VALUE (val)->pointer;
   return cache_data->fc_charset;
@@ -539,10 +548,12 @@ struct font_driver ftfont_driver =
     NULL,                      /* check */
 
 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
-    ftfont_variation_glyphs
+    ftfont_variation_glyphs,
 #else
-    NULL
+    NULL,
 #endif
+
+    ftfont_filter_properties, /* filter_properties */
   };
 
 extern Lisp_Object QCname;
@@ -582,23 +593,18 @@ ftfont_get_charset (registry)
     return -1;
   if (! fc_charset_table[i].fc_charset)
     {
-      if (i == 0)
-       ftfont_get_latin1_charset ();
-      else
-       {
-         FcCharSet *charset = FcCharSetCreate ();
-         int *uniquifier = fc_charset_table[i].uniquifier;
+      FcCharSet *charset = FcCharSetCreate ();
+      int *uniquifier = fc_charset_table[i].uniquifier;
 
-         if (! charset)
+      if (! charset)
+       return -1;
+      for (j = 0; uniquifier[j]; j++)
+       if (! FcCharSetAddChar (charset, uniquifier[j]))
+         {
+           FcCharSetDestroy (charset);
            return -1;
-         for (j = 0; uniquifier[j]; j++)
-           if (! FcCharSetAddChar (charset, uniquifier[j]))
-             {
-               FcCharSetDestroy (charset);
-               return -1;
-             }
-         fc_charset_table[i].fc_charset = charset;
-       }
+         }
+      fc_charset_table[i].fc_charset = charset;
     }
   return i;
 }
@@ -700,13 +706,15 @@ ftfont_get_open_type_spec (Lisp_Object otf_spec)
 }
 
 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
-                                          struct OpenTypeSpec **));
+                                          struct OpenTypeSpec **,
+                                          char **langname));
 
 static FcPattern *
-ftfont_spec_pattern (spec, otlayout, otspec)
+ftfont_spec_pattern (spec, otlayout, otspec, langname)
      Lisp_Object spec;
      char *otlayout;
      struct OpenTypeSpec **otspec;
+     char **langname;
 {
   Lisp_Object tmp, extra;
   FcPattern *pattern = NULL;
@@ -744,7 +752,8 @@ ftfont_spec_pattern (spec, otlayout, otspec)
       if (fc_charset_idx < 0)
        return NULL;
       charset = fc_charset_table[fc_charset_idx].fc_charset;
-      lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang;
+      *langname = fc_charset_table[fc_charset_idx].lang;
+      lang = (FcChar8 *) *langname;
       if (lang)
        {
          langset = FcLangSetCreate ();
@@ -876,6 +885,7 @@ ftfont_list (frame, spec)
   char otlayout[15];           /* For "otlayout:XXXX" */
   struct OpenTypeSpec *otspec = NULL;
   int spacing = -1;
+  char *langname = NULL;
 
   if (! fc_initialized)
     {
@@ -883,7 +893,7 @@ ftfont_list (frame, spec)
       fc_initialized = 1;
     }
 
-  pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
+  pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
   if (! pattern)
     return Qnil;
   if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
@@ -1023,13 +1033,18 @@ ftfont_list (frame, spec)
          if (j == ASIZE (chars))
            continue;
        }
-      if (! NILP (adstyle))
+      if (! NILP (adstyle) || langname)
        {
          Lisp_Object this_adstyle = get_adstyle_property (fontset->fonts[i]);
 
-         if (NILP (this_adstyle)
-             || xstrcasecmp (SDATA (SYMBOL_NAME (adstyle)),
-                             SDATA (SYMBOL_NAME (this_adstyle))) != 0)
+         if (! NILP (adstyle)
+             && (NILP (this_adstyle)
+                 || xstrcasecmp (SDATA (SYMBOL_NAME (adstyle)),
+                                 SDATA (SYMBOL_NAME (this_adstyle))) != 0))
+           continue;
+         if (langname
+             && ! NILP (this_adstyle)
+             && xstrcasecmp (langname, SDATA (SYMBOL_NAME (this_adstyle))))
            continue;
        }
       entity = ftfont_pattern_entity (fontset->fonts[i],
@@ -1046,7 +1061,7 @@ ftfont_list (frame, spec)
   val = Qnil;
 
  finish:
-  font_add_log ("ftfont-list", spec, val);
+  FONT_ADD_LOG ("ftfont-list", spec, val);
   if (objset) FcObjectSetDestroy (objset);
   if (fontset) FcFontSetDestroy (fontset);
   if (pattern) FcPatternDestroy (pattern);
@@ -1062,6 +1077,7 @@ ftfont_match (frame, spec)
   FcResult result;
   char otlayout[15];           /* For "otlayout:XXXX" */
   struct OpenTypeSpec *otspec = NULL;
+  char *langname = NULL;
 
   if (! fc_initialized)
     {
@@ -1069,7 +1085,7 @@ ftfont_match (frame, spec)
       fc_initialized = 1;
     }
 
-  pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
+  pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
   if (! pattern)
     return Qnil;
 
@@ -1099,7 +1115,7 @@ ftfont_match (frame, spec)
     }
   FcPatternDestroy (pattern);
 
-  font_add_log ("ftfont-match", spec, entity);
+  FONT_ADD_LOG ("ftfont-match", spec, entity);
   return entity;
 }
 
@@ -1170,7 +1186,7 @@ ftfont_open (f, entity, pixel_size)
   if (! CONSP (val))
     return Qnil;
   val = XCDR (val);
-  cache = ftfont_lookup_cache (entity, 1);
+  cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_FACE);
   if (NILP (cache))
     return Qnil;
   filename = XCAR (val);
@@ -1221,6 +1237,8 @@ ftfont_open (f, entity, pixel_size)
   ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
   ftfont_info->otf = NULL;
 #endif /* HAVE_LIBOTF */
+  /* This means that there's no need of transformation.  */
+  ftfont_info->matrix.xx = 0;
   font->pixel_size = size;
   font->driver = &ftfont_driver;
   font->encoding_charset = font->repertory_charset = -1;
@@ -1297,7 +1315,7 @@ ftfont_close (f, font)
   Lisp_Object val, cache;
 
   val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
-  cache = ftfont_lookup_cache (val, 1);
+  cache = ftfont_lookup_cache (val, FTFONT_CACHE_FOR_FACE);
   xassert (CONSP (cache));
   val = XCDR (cache);
   (XSAVE_VALUE (val)->integer)--;
@@ -1321,6 +1339,17 @@ ftfont_has_char (font, c)
      Lisp_Object font;
      int c;
 {
+  struct charset *cs = NULL;
+
+  if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja)
+      && charset_jisx0208 >= 0)
+    cs = CHARSET_FROM_ID (charset_jisx0208);
+  else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko)
+      && charset_ksc5601 >= 0)
+    cs = CHARSET_FROM_ID (charset_ksc5601);
+  if (cs)
+    return (ENCODE_CHAR (cs, c) != CHARSET_INVALID_CODE (cs));
+
   if (FONT_ENTITY_P (font))
     {
       FcCharSet *charset = ftfont_get_fc_charset (font);
@@ -1555,6 +1584,7 @@ struct MFLTFontFT
   struct font *font;
   FT_Face ft_face;
   OTF *otf;
+  FT_Matrix *matrix;
 };
 
 static int
@@ -1578,6 +1608,12 @@ ftfont_get_glyph_id (font, gstring, from, to)
   return 0;
 }
 
+/* Operators for 26.6 fixed fractional pixel format */
+
+#define FLOOR(x)    ((x) & -64)
+#define CEIL(x)            (((x)+63) & -64)
+#define ROUND(x)    (((x)+32) & -64)
+
 static int
 ftfont_get_metrics (font, gstring, from, to)
      MFLTFont *font;
@@ -1594,16 +1630,35 @@ ftfont_get_metrics (font, gstring, from, to)
        if (g->code != FONT_INVALID_CODE)
          {
            FT_Glyph_Metrics *m;
+           int lbearing, rbearing, ascent, descent, xadv;
 
            if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
              abort ();
            m = &ft_face->glyph->metrics;
-
-           g->lbearing = m->horiBearingX;
-           g->rbearing = m->horiBearingX + m->width;
-           g->ascent = m->horiBearingY;
-           g->descent = m->height - m->horiBearingY;
-           g->xadv = m->horiAdvance;
+           if (flt_font_ft->matrix)
+             {
+               FT_Vector v[4];
+               int i;
+
+               v[0].x = v[1].x = m->horiBearingX;
+               v[2].x = v[3].x = m->horiBearingX + m->width;
+               v[0].y = v[2].y = m->horiBearingY;
+               v[1].y = v[3].y = m->horiBearingY - m->height;
+               for (i = 0; i < 4; i++)
+                 FT_Vector_Transform (v + i, flt_font_ft->matrix);
+               g->lbearing = v[0].x < v[1].x ? FLOOR (v[0].x) : FLOOR (v[1].x);
+               g->rbearing = v[2].x > v[3].x ? CEIL (v[2].x) : CEIL (v[3].x);
+               g->ascent = v[0].y > v[2].y ? CEIL (v[0].y) : CEIL (v[2].y);
+               g->descent = v[1].y < v[3].y ? - FLOOR (v[1].y) : - FLOOR (v[3].y);
+             }
+           else
+             {
+               g->lbearing = FLOOR (m->horiBearingX);
+               g->rbearing = CEIL (m->horiBearingX + m->width);
+               g->ascent = CEIL (m->horiBearingY);
+               g->descent = - FLOOR (m->horiBearingY - m->height);
+             }
+           g->xadv = ROUND (ft_face->glyph->advance.x);
          }
        else
          {
@@ -1689,13 +1744,13 @@ setup_otf_gstring (int size)
 {
   if (otf_gstring.size == 0)
     {
-      otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * size);
+      otf_gstring.glyphs = (OTF_Glyph *) xmalloc (sizeof (OTF_Glyph) * size);
       otf_gstring.size = size;
     }
   else if (otf_gstring.size < size)
     {
-      otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
-                                                 sizeof (OTF_Glyph) * size);
+      otf_gstring.glyphs = xrealloc (otf_gstring.glyphs,
+                                    sizeof (OTF_Glyph) * size);
       otf_gstring.size = size;
     }
   otf_gstring.used = size;
@@ -1963,11 +2018,12 @@ static int m17n_flt_initialized;
 extern Lisp_Object QCfamily;
 
 static Lisp_Object
-ftfont_shape_by_flt (lgstring, font, ft_face, otf)
+ftfont_shape_by_flt (lgstring, font, ft_face, otf, matrix)
      Lisp_Object lgstring;
      struct font *font;
      FT_Face ft_face;
      OTF *otf;
+     FT_Matrix *matrix;
 {
   EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
   EMACS_UINT i;
@@ -2026,13 +2082,13 @@ ftfont_shape_by_flt (lgstring, font, ft_face, otf)
     {
       gstring.allocated = len * 2;
       gstring.glyph_size = sizeof (MFLTGlyph);
-      gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
+      gstring.glyphs = xmalloc (sizeof (MFLTGlyph) * gstring.allocated);
     }
   else if (gstring.allocated < len * 2)
     {
       gstring.allocated = len * 2;
-      gstring.glyphs = realloc (gstring.glyphs,
-                               sizeof (MFLTGlyph) * gstring.allocated);
+      gstring.glyphs = xrealloc (gstring.glyphs,
+                                sizeof (MFLTGlyph) * gstring.allocated);
     }
   memset (gstring.glyphs, 0, sizeof (MFLTGlyph) * len);
   for (i = 0; i < len; i++)
@@ -2069,6 +2125,7 @@ ftfont_shape_by_flt (lgstring, font, ft_face, otf)
   flt_font_ft.font = font;
   flt_font_ft.ft_face = ft_face;
   flt_font_ft.otf = otf;
+  flt_font_ft.matrix = matrix->xx != 0 ? matrix : 0;
   if (len > 1
       && gstring.glyphs[1].c >= 0x300 && gstring.glyphs[1].c <= 0x36F)
     /* A little bit ad hoc.  Perhaps, shaper must get script and
@@ -2081,8 +2138,8 @@ ftfont_shape_by_flt (lgstring, font, ft_face, otf)
       if (result != -2)
        break;
       gstring.allocated += gstring.allocated;
-      gstring.glyphs = realloc (gstring.glyphs,
-                               sizeof (MFLTGlyph) * gstring.allocated);
+      gstring.glyphs = xrealloc (gstring.glyphs,
+                                sizeof (MFLTGlyph) * gstring.allocated);
     }
   if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
     return Qnil;
@@ -2140,7 +2197,8 @@ ftfont_shape (lgstring)
   otf = ftfont_get_otf (ftfont_info);
   if (! otf)
     return make_number (0);
-  return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
+  return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf,
+                             &ftfont_info->matrix);
 }
 
 #endif /* HAVE_M17N_FLT */
@@ -2204,7 +2262,94 @@ ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
   return intern ("unknown");
 }
 
-\f
+static const char *ftfont_booleans [] = {
+  ":antialias",
+  ":hinting",
+  ":verticallayout",
+  ":autohint",
+  ":globaladvance",
+  ":outline",
+  ":scalable",
+  ":minspace",
+  ":embolden",
+  NULL,
+};
+
+static const char *ftfont_non_booleans [] = {
+  ":family",
+  ":familylang",
+  ":style",
+  ":stylelang",
+  ":fullname",
+  ":fullnamelang",
+  ":slant",
+  ":weight",
+  ":size",
+  ":width",
+  ":aspect",
+  ":pixelsize",
+  ":spacing",
+  ":foundry",
+  ":hintstyle",
+  ":file",
+  ":index",
+  ":ftface",
+  ":rasterizer",
+  ":scale",
+  ":dpi",
+  ":rgba",
+  ":lcdfilter",
+  ":charset",
+  ":lang",
+  ":fontversion",
+  ":capability",
+  NULL,
+};
+
+static void
+ftfont_filter_properties (font, alist)
+     Lisp_Object font;
+     Lisp_Object alist;
+{
+  Lisp_Object it;
+  int i;
+
+  /* Set boolean values to Qt or Qnil */
+  for (i = 0; ftfont_booleans[i] != NULL; ++i)
+    for (it = alist; ! NILP (it); it = XCDR (it))
+      {
+        Lisp_Object key = XCAR (XCAR (it));
+        Lisp_Object val = XCDR (XCAR (it));
+        char *keystr = SDATA (SYMBOL_NAME (key));
+
+        if (strcmp (ftfont_booleans[i], keystr) == 0)
+          {
+            char *str = SYMBOLP (val) ? SDATA (SYMBOL_NAME (val)) : NULL;
+            if (INTEGERP (val)) str = XINT (val) != 0 ? "true" : "false";
+            if (str == NULL) str = "true";
+
+            val = Qt;
+            if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
+                || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
+                || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
+                || strcmp ("Off", str) == 0)
+              val = Qnil;
+            Ffont_put (font, key, val);
+          }
+      }
+
+  for (i = 0; ftfont_non_booleans[i] != NULL; ++i)
+    for (it = alist; ! NILP (it); it = XCDR (it))
+      {
+        Lisp_Object key = XCAR (XCAR (it));
+        Lisp_Object val = XCDR (XCAR (it));
+        char *keystr = SDATA (SYMBOL_NAME (key));
+        if (strcmp (ftfont_non_booleans[i], keystr) == 0)
+          Ffont_put (font, key, val);
+      }
+}
+
+
 void
 syms_of_ftfont ()
 {
@@ -2215,8 +2360,6 @@ syms_of_ftfont ()
   DEFSYM (Qmono, "mono");
   DEFSYM (Qsans, "sans");
   DEFSYM (Qsans__serif, "sans serif");
-  DEFSYM (Qja, "ja");
-  DEFSYM (Qko, "ko");
 
   staticpro (&freetype_font_cache);
   freetype_font_cache = Fcons (Qt, Qnil);