*** empty log message ***
[bpt/emacs.git] / src / fontset.c
index 36ac055..2bac86c 100644 (file)
@@ -1,20 +1,20 @@
 /* Fontset handler.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
      Free Software Foundation, Inc.
    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-     2005, 2006, 2007, 2008
+     2005, 2006, 2007, 2008, 2009
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H14PRO021
    Copyright (C) 2003, 2006
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H13PRO009
+
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -22,17 +22,12 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* #define FONTSET_DEBUG */
 
 #include <config.h>
-
-#ifdef FONTSET_DEBUG
 #include <stdio.h>
-#endif
 
 #include "lisp.h"
 #include "blockinput.h"
@@ -52,8 +47,8 @@ Boston, MA 02110-1301, USA.  */
 #ifdef WINDOWSNT
 #include "w32term.h"
 #endif
-#ifdef MAC_OS
-#include "macterm.h"
+#ifdef HAVE_NS
+#include "nsterm.h"
 #endif
 #include "termhooks.h"
 
@@ -94,36 +89,17 @@ EXFUN (Fclear_face_cache, 1);
    An element of a base fontset is a vector of FONT-DEFs which itself
    is a vector [ FONT-SPEC ENCODING REPERTORY ].
 
-   FONT-SPEC is a font-spec created by `font-spec' or
-       ( FAMILY . REGISTRY )
-   or
-       FONT-NAME
-   where FAMILY, REGISTRY, and FONT-NAME are strings.
-
-   ENCODING is a charset ID that can convert characters to glyph codes
-   of the corresponding font.
-
-   REPERTORY is a charset ID, a char-table, or nil.  If REPERTORY is a
-   charset ID, the repertory of the charset exactly matches with that
-   of the font.  If REPERTORY is a char-table, all characters who have
-   a non-nil value in the table are supported.  If REPERTORY is nil,
-   we consult with the font itself to get the repertory.
-
-   ENCODING and REPERTORY are extracted from the variable
-   Vfont_encoding_alist by using a font name generated from FONT-SPEC
-   (if it is a vector) or FONT-NAME as a matching target.
+   An element of a realized fontset is nil, t, 0, or a vector of this
+   form:
 
-
-   An element of a realized fontset is nil or t, or has this form:
-
-       [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-FAMILY
-        RFONT-DEF0 RFONT-DEF1 ...].
+       [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
+         RFONT-DEF0 RFONT-DEF1 ... ]
 
    RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
 
-       [ FACE-ID FONT-INDEX FONT-DEF OPENED-FONT-NAME ]
+       [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
 
-   RFONT-DEFn is automatically reordered by the current charset
+   RFONT-DEFn are automatically reordered by the current charset
    priority list.
 
    The value nil means that we have not yet generated the above vector
@@ -132,6 +108,10 @@ EXFUN (Fclear_face_cache, 1);
    The value t means that no font is available for the corresponding
    range of characters.
 
+   The value 0 means that no font is available for the corresponding
+   range of characters in this fontset, but may be available in the
+   default fontset.
+
 
    A fontset has 9 extra slots.
 
@@ -162,7 +142,7 @@ EXFUN (Fclear_face_cache, 1);
        base: nil
        realized: Alist of font index vs the corresponding repertory
        char-table.
-       
+
    The 8th slot:
        base: nil
        realized: If the base is not the default fontset, a fontset
@@ -200,7 +180,7 @@ extern Lisp_Object Qfont;
 static Lisp_Object Qfontset;
 static Lisp_Object Qfontset_info;
 static Lisp_Object Qprepend, Qappend;
-static Lisp_Object Qlatin;
+Lisp_Object Qlatin;
 
 /* Vector containing all fontsets.  */
 static Lisp_Object Vfontset_table;
@@ -213,7 +193,6 @@ static int next_fontset_id;
    font for each character.  */
 static Lisp_Object Vdefault_fontset;
 
-Lisp_Object Vfont_encoding_alist;
 Lisp_Object Vfont_encoding_charset_alist;
 Lisp_Object Vuse_default_ascent;
 Lisp_Object Vignore_relative_composition;
@@ -222,40 +201,6 @@ Lisp_Object Vfontset_alias_alist;
 Lisp_Object Vvertical_centering_font_regexp;
 Lisp_Object Votf_script_alist;
 
-/* The following six are declarations of callback functions depending
-   on window system.  See the comments in src/fontset.h for more
-   detail.  */
-
-/* Return a pointer to struct font_info of font FONT_IDX of frame F.  */
-struct font_info *(*get_font_info_func) P_ ((FRAME_PTR f, int font_idx));
-
-/* Return a list of font names which matches PATTERN.  See the documentation
-   of `x-list-fonts' for more details.  */
-Lisp_Object (*list_fonts_func) P_ ((struct frame *f,
-                                   Lisp_Object pattern,
-                                   int size,
-                                   int maxnames));
-
-/* Load a font named NAME for frame F and return a pointer to the
-   information of the loaded font.  If loading is failed, return 0.  */
-struct font_info *(*load_font_func) P_ ((FRAME_PTR f, char *name, int));
-
-/* Return a pointer to struct font_info of a font named NAME for frame F.  */
-struct font_info *(*query_font_func) P_ ((FRAME_PTR f, char *name));
-
-/* Additional function for setting fontset or changing fontset
-   contents of frame F.  */
-void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
-                                   Lisp_Object oldval));
-
-/* To find a CCL program, fs_load_font calls this function.
-   The argument is a pointer to the struct font_info.
-   This function set the member `encoder' of the structure.  */
-void (*find_ccl_program_func) P_ ((struct font_info *));
-
-Lisp_Object (*get_font_repertory_func) P_ ((struct frame *,
-                                           struct font_info *));
-
 /* Check if any window system is used now.  */
 void (*check_window_system_func) P_ ((void));
 
@@ -263,7 +208,9 @@ void (*check_window_system_func) P_ ((void));
 /* Prototype declarations for static functions.  */
 static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
                                    Lisp_Object));
-static void reorder_font_vector P_ ((Lisp_Object, int, Lisp_Object));
+static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
+                                         int, int));
+static void reorder_font_vector P_ ((Lisp_Object, struct font *));
 static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
@@ -299,10 +246,12 @@ fontset_id_valid_p (id)
 /* Macros to access special values of (base) FONTSET.  */
 #define FONTSET_NAME(fontset)          XCHAR_TABLE (fontset)->extras[1]
 #define FONTSET_ASCII(fontset)         XCHAR_TABLE (fontset)->extras[4]
+#define FONTSET_SPEC(fontset)          XCHAR_TABLE (fontset)->extras[5]
 
 /* Macros to access special values of (realized) FONTSET.  */
 #define FONTSET_BASE(fontset)          XCHAR_TABLE (fontset)->extras[2]
 #define FONTSET_FRAME(fontset)         XCHAR_TABLE (fontset)->extras[3]
+#define FONTSET_OBJLIST(fontset)       XCHAR_TABLE (fontset)->extras[4]
 #define FONTSET_NOFONT_FACE(fontset)   XCHAR_TABLE (fontset)->extras[5]
 #define FONTSET_REPERTORY(fontset)     XCHAR_TABLE (fontset)->extras[6]
 #define FONTSET_DEFAULT(fontset)       XCHAR_TABLE (fontset)->extras[7]
@@ -313,6 +262,38 @@ fontset_id_valid_p (id)
 #define BASE_FONTSET_P(fontset)                (NILP (FONTSET_BASE (fontset)))
 
 
+/* Macros for FONT-DEF and RFONT-DEF of fontset.  */
+#define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
+  do {                                                         \
+    (font_def) = Fmake_vector (make_number (3), (font_spec));  \
+    ASET ((font_def), 1, encoding);                            \
+    ASET ((font_def), 2, repertory);                           \
+  } while (0)
+
+#define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
+#define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
+#define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
+
+#define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
+#define RFONT_DEF_SET_FACE(rfont_def, face_id) \
+  ASET ((rfont_def), 0, make_number (face_id))
+#define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
+#define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
+#define RFONT_DEF_REPERTORY(rfont_def) FONT_DEF_REPERTORY (AREF (rfont_def, 1))
+#define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
+#define RFONT_DEF_SET_OBJECT(rfont_def, object)        \
+  ASET ((rfont_def), 2, (object))
+#define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
+#define RFONT_DEF_SET_SCORE(rfont_def, score) \
+  ASET ((rfont_def), 3, make_number (score))
+#define RFONT_DEF_NEW(rfont_def, font_def)             \
+  do {                                                 \
+    (rfont_def) = Fmake_vector (make_number (4), Qnil);        \
+    ASET ((rfont_def), 1, (font_def));         \
+    RFONT_DEF_SET_SCORE ((rfont_def), 0);              \
+  } while (0)
+
+
 /* Return the element of FONTSET for the character C.  If FONTSET is a
    base fontset other then the default fontset and FONTSET doesn't
    contain information for C, return the information in the default
@@ -376,9 +357,8 @@ fontset_add (fontset, range, elt, add)
       int from1, to1;
 
       do {
+       from1 = from, to1 = to;
        args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
-       if (to < to1)
-         to1 = to;
        char_table_set_range (fontset, from, to1,
                              NILP (args[idx]) ? args[1 - idx]
                              : Fvconcat (2, args));
@@ -394,152 +374,142 @@ fontset_add (fontset, range, elt, add)
   return Qnil;
 }
 
+static int
+fontset_compare_rfontdef (val1, val2)
+     const void *val1, *val2;
+{
+  return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
+         - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
+}
 
 /* Update FONT-GROUP which has this form:
-       [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-FAMILY
-        RFONT-DEF0 RFONT-DEF1 ...].
-   Reorder RFONT-DEFs according to the current order of charset
-   (Vcharset_ordered_list), and update CHARSET-ORDERED-LIST-TICK to
-   the latest value.  */
+       [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
+         RFONT-DEF0 RFONT-DEF1 ... ]
+   Reorder RFONT-DEFs according to the current language, and update
+   CHARSET-ORDERED-LIST-TICK.
+
+   If PREFERRED_FAMILY is not nil, that family has the higher priority
+   if the encoding charsets or languages in font-specs are the same.  */
+
+extern Lisp_Object Fassoc_string ();
 
 static void
-reorder_font_vector (font_group, charset_id, family)
+reorder_font_vector (font_group, font)
      Lisp_Object font_group;
-     int charset_id;
-     Lisp_Object family;
+     struct font *font;
 {
-  Lisp_Object list, *new_vec;
+  Lisp_Object vec, font_object;
   int size;
-  int *charset_id_table;
-  int i, idx;
-  Lisp_Object preferred_by_charset, preferred_by_family;
-
-  size = ASIZE (font_group) - 3;
-  /* Exclude the tailing nil elements from the reordering.  */
-  while (NILP (AREF (font_group, size - 1))) size--;
-  charset_id_table = (int *) alloca (sizeof (int) * size);
-  new_vec = (Lisp_Object *) alloca (sizeof (Lisp_Object) * size);
-
-  /* At first, extract ENCODING (a chaset ID) from RFONT_DEF which
-     has this form:
-       [FACE-ID FONT-INDEX [ FONT-SPEC ENCODING REPERTORY ]]
-     In addtion, if RFONT_DEF is preferred by family or charset, store
-     it from the start of new_vec.  */
-  for (i = 0, idx = 0; i < size; i++)
-    {
-      Lisp_Object rfont_def = AREF (font_group, i + 3);
-      Lisp_Object font_spec = AREF (AREF (rfont_def, 2), 0);
-      Lisp_Object this_family = AREF (font_spec, FONT_FAMILY_INDEX);
-      int id = XINT (AREF (AREF (rfont_def, 2), 1));
-      struct charset *charset = CHARSET_FROM_ID (id);
-
-      charset_id_table[i] = -1;
-      if (! NILP (this_family)
-         && (fast_string_match_ignore_case (family, SYMBOL_NAME (this_family))
-             >= 0))
-       {
-         if (idx > 0)
-           memmove (new_vec + 1, new_vec, sizeof (Lisp_Object) * idx);
-         new_vec[0] = rfont_def;
-         idx++;
-         ASET (font_group, i + 3, Qnil);
-       }
-      else if (id == charset_id)
-       {
-         new_vec[idx++] = rfont_def;
-         ASET (font_group, i + 3, Qnil);
-       }
-      else if (! charset->supplementary_p)
-       charset_id_table[i] = id;
-    }
+  int i;
+  int score_changed = 0;
 
-  if (idx == 0
-      && (XINT (AREF (font_group, 0)) == charset_ordered_list_tick))
-    /* No need of reordering.  */
-    return;
+  if (font)
+    XSETFONT (font_object, font);
+  else
+    font_object = Qnil;
 
-  ASET (font_group, 0, make_number (charset_ordered_list_tick));
-  ASET (font_group, 1, make_number (charset_id));
-  ASET (font_group, 2, family);
+  vec = XCDR (font_group);
+  size = ASIZE (vec);
+  /* Exclude the tailing nil element from the reordering.  */
+  if (NILP (AREF (vec, size - 1)))
+    size--;
 
-  /* Then, store the remaining RFONT-DEFs in NEW_VEC in the correct
-     order.  */
-  for (list = Vcharset_ordered_list; idx < size; list = XCDR (list))
+  for (i = 0; i < size; i++)
     {
-      int id = XINT (XCAR (list));
-      struct charset *charset = CHARSET_FROM_ID (id);
+      Lisp_Object rfont_def = AREF (vec, i);
+      Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
+      Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
+      int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
 
-      if (charset->supplementary_p)
-       break;
-      for (i = 0; i < size; i++)
-       if (charset_id_table[i] == XINT (XCAR (list))
-           && ! NILP (AREF (font_group, i + 3)))
-         {
-           new_vec[idx++] = AREF (font_group, i + 3);
-           ASET (font_group, i + 3, Qnil);
-         }
-    }
-  for (i = 0; i < size; i++)
-    if (! NILP (AREF (font_group, i + 3)))
-      new_vec[idx++] = AREF (font_group, i + 3);
+      if (! font_match_p (font_spec, font_object))
+       {
+         Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
 
-  /* At last, update elements of FONT-GROUP.  */
-  for (i = 0; i < size; i++)
-    ASET (font_group, i + 3, new_vec[i]);
-}
+         if (! NILP (encoding))
+           {
+             Lisp_Object tail;
 
+             for (tail = Vcharset_ordered_list;
+                  ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
+                  score += 0x100, tail = XCDR (tail))
+               if (EQ (encoding, XCAR (tail)))
+                 break;
+           }
+         else
+           {
+             Lisp_Object lang = Ffont_get (font_spec, QClang);
 
-/* Load a font matching the font related attributes in FACE->lface and
-   font pattern in FONT_DEF of FONTSET, and return an index of the
-   font.  FONT_DEF has this form:
-       [ FONT-SPEC ENCODING REPERTORY ]
-   If REPERTORY is nil, generate a char-table representing the font
-   repertory by looking into the font itself.  */
+             if (! NILP (lang)
+                 && ! EQ (lang, Vcurrent_iso639_language)
+                 && (! CONSP (Vcurrent_iso639_language)
+                     || NILP (Fmemq (lang, Vcurrent_iso639_language))))
+               score |= 0x100;
+           }
+       }
+      if (RFONT_DEF_SCORE (rfont_def) != score)
+       {
+         RFONT_DEF_SET_SCORE (rfont_def, score);
+         score_changed = 1;
+       }
+    }
 
-extern Lisp_Object QCname;
+  if (score_changed)
+    qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
+          fontset_compare_rfontdef);
+  XSETCAR (font_group, make_number (charset_ordered_list_tick));
+}
 
-static int
-load_font_get_repertory (f, face, font_def, fontset)
-     FRAME_PTR f;
-     struct face *face;
-     Lisp_Object font_def;
-     Lisp_Object fontset;
+/* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
+   character C in FONTSET.  If C is -1, return a fallback font-group.
+   If C is not -1, the value may be Qt (FONTSET doesn't have a font
+   for C even in the fallback group, or 0 (a font for C may be found
+   only in the fallback group).  */
+
+static Lisp_Object
+fontset_get_font_group (Lisp_Object fontset, int c)
 {
-  char *font_name;
-  struct font_info *font_info;
-  int charset;
-  Lisp_Object font_spec, name;
-
-  font_spec = AREF (font_def, 0);
-  name = Ffont_get (font_spec, QCname);
-  if (! NILP (name))
-    font_name = choose_face_font (f, face->lface, name, NULL);
+  Lisp_Object font_group;
+  Lisp_Object base_fontset;
+  int from = 0, to = MAX_CHAR, i;
+
+  xassert (! BASE_FONTSET_P (fontset));
+  if (c >= 0)
+    font_group = CHAR_TABLE_REF (fontset, c);
+  else
+    font_group = FONTSET_FALLBACK (fontset);
+  if (! NILP (font_group))
+    return font_group;
+  base_fontset = FONTSET_BASE (fontset);
+  if (c >= 0)
+    font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
   else
-    font_name = choose_face_font (f, face->lface, font_spec, NULL);
-  charset = XINT (AREF (font_def, 1));
-  if (! (font_info = fs_load_font (f, font_name, charset)))
-    return -1;
-
-  if (NILP (AREF (font_def, 2))
-      && NILP (Fassq (make_number (font_info->font_idx),
-                     FONTSET_REPERTORY (fontset))))
+    font_group = FONTSET_FALLBACK (base_fontset);
+  if (NILP (font_group))
     {
-      /* We must look into the font to get the correct repertory as a
-        char-table.  */
-      Lisp_Object repertory;
-
-      repertory = (*get_font_repertory_func) (f, font_info);
-      FONTSET_REPERTORY (fontset)
-       = Fcons (Fcons (make_number (font_info->font_idx), repertory),
-                FONTSET_REPERTORY (fontset));
+      font_group = make_number (0);
+      if (c >= 0)
+       char_table_set_range (fontset, from, to, font_group);
+      return font_group;
     }
-       
-  return font_info->font_idx;
+  font_group = Fcopy_sequence (font_group);
+  for (i = 0; i < ASIZE (font_group); i++)
+    if (! NILP (AREF (font_group, i)))
+      {
+       Lisp_Object rfont_def;
+
+       RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
+       /* Remember the original order.  */
+       RFONT_DEF_SET_SCORE (rfont_def, i);
+       ASET (font_group, i, rfont_def);
+      }
+  font_group = Fcons (make_number (-1), font_group);
+  if (c >= 0)
+    char_table_set_range (fontset, from, to, font_group);
+  else
+    FONTSET_FALLBACK (fontset) = font_group;
+  return font_group;
 }
 
-static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
-                                         int, int));
-
 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
    character C.  If no font is found, return Qnil if there's a
    possibility that the default fontset or the fallback font groups
@@ -560,217 +530,199 @@ fontset_find_font (fontset, c, face, id, fallback)
      struct face *face;
      int id, fallback;
 {
-  Lisp_Object base_fontset, elt, vec, font_def;
-  int i, from, to;
-  int font_idx;
-  FRAME_PTR f = XFRAME (FONTSET_FRAME (fontset));
+  Lisp_Object vec, font_group;
+  int i, charset_matched = 0, found_index;
+  FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset)))
+    ? XFRAME (selected_frame) : XFRAME (FONTSET_FRAME (fontset));
+  Lisp_Object rfont_def;
 
-  base_fontset = FONTSET_BASE (fontset);
-  if (! fallback)
-    vec = CHAR_TABLE_REF (fontset, c);
-  else
-    vec = FONTSET_FALLBACK (fontset);
+  font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
+  if (! CONSP (font_group))
+    return font_group;
+  vec = XCDR (font_group);
+  if (ASIZE (vec) == 0)
+    return Qnil;
 
-  if (NILP (vec))
+  if (ASIZE (vec) > 1)
     {
-      Lisp_Object range;
+      if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
+       /* We have just created the font-group,
+          or the charset priorities were changed.  */
+       reorder_font_vector (font_group, face->ascii_face->font);
+      if (id >= 0)
+       /* Find a spec matching with the charset ID to try at
+          first.  */
+       for (i = 0; i < ASIZE (vec); i++)
+         {
+           Lisp_Object repertory;
 
-      /* We have not yet decided a font for C.  */
-      if (! face)
-       return Qnil;
-      if (! fallback)
-       {
-         elt = char_table_ref_and_range (base_fontset, c, &from, &to);
-         range = Fcons (make_number (from), make_number (to));
-       }
-      else
-       {
-         elt = FONTSET_FALLBACK (base_fontset);
-       }
-      if (NILP (elt))
-       {
-         /* This fontset doesn't specify any font for C. */
-         vec = make_number (0);
-       }
-      else if (ASIZE (elt) == 1 && NILP (AREF (elt, 0)))
-       {
-         /* Explicitly specified no font.  */
-         vec = Qt;
-       }
-      else
-       {
-         /* Build a vector [ -1 -1 nil NEW-ELT0 NEW-ELT1 NEW-ELT2 ... ],
-            where the first -1 is to force reordering of NEW-ELTn,
-            NEW-ELTn is [nil nil AREF (elt, n) nil].  */
-         int size = ASIZE (elt);
-         int j;
+           rfont_def = AREF (vec, i);
+           if (NILP (rfont_def))
+             break;
+           repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
 
-         vec = Fmake_vector (make_number (size + 3), Qnil);
-         ASET (vec, 0, make_number (-1));
-         ASET (vec, 1, make_number (-1));
-         for (i = j = 0; i < size; i++)
-           if (! NILP (AREF (elt, i)))
+           if (XINT (repertory) == id)
              {
-               Lisp_Object tmp;
-               tmp = Fmake_vector (make_number (5), Qnil);
-               ASET (tmp, 2, AREF (elt, i));
-               ASET (vec, j + 3, tmp);
-               j++;
+               charset_matched = i;
+               break;
              }
-       }
-      /* Then store it in the fontset.  */
-      if (! fallback)
-       FONTSET_SET (fontset, range, vec);
-      else
-       FONTSET_FALLBACK (fontset) = vec;
-
+         }
     }
-  if (! VECTORP (vec))
-    return (EQ (vec, Qt) ? Qt : Qnil);
-
-  if (ASIZE (vec) > 4
-      && (XINT (AREF (vec, 0)) != charset_ordered_list_tick
-         || (id >= 0 && XINT (AREF (vec, 1)) != id)
-         || NILP (Fequal (AREF (vec, 2), face->lface[LFACE_FAMILY_INDEX]))))
-    /* We have just created VEC,
-       or the charset priorities were changed,
-       or the preferred charset was changed,
-       or the preferred family was changed.  */
-    reorder_font_vector (vec, id, face->lface[LFACE_FAMILY_INDEX]);
 
   /* Find the first available font in the vector of RFONT-DEF.  */
-  for (i = 3; i < ASIZE (vec); i++)
+  for (i = 0; i < ASIZE (vec); i++)
     {
-      elt = AREF (vec, i);
-      if (NILP (elt))
-       /* This is the sign of not to try fallback fonts.  */
-       return Qt;
-      /* ELT == [ FACE-ID FONT-INDEX FONT-DEF ... ] */
-      if (INTEGERP (AREF (elt, 1)) && XINT (AREF (elt, 1)) < 0)
-       /* We couldn't open this font last time.  */
-       continue;
-
-      if (!face && NILP (AREF (elt, 1)))
-       /* We have not yet opened the font.  */
-       return Qnil;
-
-      font_def = AREF (elt, 2);
-      /* FONT_DEF == [ FONT-SPEC ENCODING REPERTORY ] */
+      Lisp_Object font_def;
+      Lisp_Object font_entity, font_object;
 
-#ifdef USE_FONT_BACKEND
-      if (enable_font_backend)
+      found_index = i;
+      if (i == 0)
        {
-         /* ELT == [ FACE-ID FONT-INDEX FONT-DEF FONT-ENTITY ]
-            where FONT-ENTITY turns to a font-object once opened.  */
-         Lisp_Object font_entity = AREF (elt, 3);
-         int has_char = 0;
-
-         if (NILP (font_entity))
+         if (charset_matched > 0)
            {
-             font_entity = font_find_for_lface (f, face->lface,
-                                                AREF (font_def, 0), -1);
-             if (NILP (font_entity))
-               {
-                 ASET (elt, 1, make_number (-1));
-                 continue;
-               }
-             ASET (elt, 3, font_entity);
+             /* Try the element matching with the charset ID at first.  */
+             found_index = charset_matched;
+             /* Make this negative so that we don't come here in the
+                next loop.  */
+             charset_matched = - charset_matched;
+             /* We must try the first element in the next loop.  */
+             i--;
            }
-         else if (FONT_ENTITY_P (font_entity))
-           {
-             if (FONT_ENTITY_NOT_LOADABLE (font_entity))
-               continue;
-           }
-         has_char = font_has_char (f, font_entity, c);
-         if (! has_char)
-           continue;
-         if (! FONT_OBJECT_P (font_entity))
+       }
+      else if (i == - charset_matched)
+       {
+         /* We have already tried this element and the followings
+            that have the same font specifications in the first
+            iteration.  So, skip them all.  */
+         rfont_def = AREF (vec, i);
+         font_def = RFONT_DEF_FONT_DEF (rfont_def);
+         for (; i + 1 < ASIZE (vec); i++)
            {
-             Lisp_Object font_object
-               = font_open_for_lface (f, font_entity, face->lface, Qnil);
-
-             if (NILP (font_object))
-               {
-                 FONT_ENTITY_SET_NOT_LOADABLE (font_entity);
-                 continue;
-               }
-             ASET (elt, 3, font_object);
-             if (has_char < 0)
-               {
-                 has_char = font_has_char (f, font_object, c);
-                 if (! has_char)
-                   continue;
-               }
+             rfont_def = AREF (vec, i + 1);
+             if (NILP (rfont_def))
+               break;
+             if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
+               break;
            }
-         /* Decide to use this font.  */
-         ASET (elt, 1, make_number (0));
+         continue;
        }
-      else
-#endif /* USE_FONT_BACKEND */
-
-      if (INTEGERP (AREF (font_def, 2)))
-       {
-         /* The repertory is specified by charset ID.  */
-         struct charset *charset
-           = CHARSET_FROM_ID (XINT (AREF (font_def, 2)));
 
-         if (! CHAR_CHARSET_P (c, charset))
-           /* This font can't display C.  */
-           continue;
-       }
-      else if (CHAR_TABLE_P (AREF (font_def, 2)))
+      rfont_def = AREF (vec, found_index);
+      if (NILP (rfont_def))
        {
-         /* The repertory is specified by a char table.  */
-         if (NILP (CHAR_TABLE_REF (AREF (font_def, 2), c)))
-           /* This font can't display C.  */
+         if (i < 0)
            continue;
+         /* This is a sign of not to try the other fonts.  */
+         return Qt;
        }
-      else
+      if (INTEGERP (RFONT_DEF_FACE (rfont_def))
+         && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
+       /* We couldn't open this font last time.  */
+       continue;
+
+      font_object = RFONT_DEF_OBJECT (rfont_def);
+      if (NILP (font_object))
        {
-         Lisp_Object slot;
+         font_def = RFONT_DEF_FONT_DEF (rfont_def);
 
-         if (! INTEGERP (AREF (elt, 1)))
+         if (! face)
+           /* We have not yet opened the font.  */
+           return Qnil;
+         /* Find a font best-matching with the spec without checking
+            the support of the character C.  That checking is costly,
+            and even without the checking, the found font supports C
+            in high possibility.  */
+         font_entity = font_find_for_lface (f, face->lface,
+                                            FONT_DEF_SPEC (font_def), -1);
+         if (NILP (font_entity))
+           {
+             /* Record that no font matches the spec.  */
+             RFONT_DEF_SET_FACE (rfont_def, -1);
+             continue;
+           }
+         font_object = font_open_for_lface (f, font_entity, face->lface,
+                                            FONT_DEF_SPEC (font_def));
+         if (NILP (font_object))
            {
-             /* We have not yet opened a font matching this spec.
-                Open the best matching font now and register the
-                repertory.  */
-             struct font_info *font_info;
-
-             font_idx = load_font_get_repertory (f, face, font_def, fontset);
-             ASET (elt, 1, make_number (font_idx));
-             if (font_idx < 0)
-               /* This means that we couldn't find a font matching
-                  FONT_DEF.  */
-               continue;
-             font_info = (*get_font_info_func) (f, font_idx);
-             ASET (elt, 3, build_string (font_info->full_name));
+             /* Something strange happened, perhaps because of a
+                Font-backend problem.  Too avoid crashing, record
+                that this spec is unsable.  It may be better to find
+                another font of the same spec, but currently we don't
+                have such an API.  */
+             RFONT_DEF_SET_FACE (rfont_def, -1);
+             continue;
            }
+         RFONT_DEF_SET_OBJECT (rfont_def, font_object);
+       }
 
-         slot = Fassq (AREF (elt, 1), FONTSET_REPERTORY (fontset));
-         xassert (CONSP (slot));
-         if (NILP (CHAR_TABLE_REF (XCDR (slot), c)))
-           /* This font can't display C.  */
-           continue;
+      if (font_has_char (f, font_object, c))
+       goto found;
+
+      /* Find a font already opened, maching with the current spec,
+        and supporting C. */
+      font_def = RFONT_DEF_FONT_DEF (rfont_def);
+      for (; found_index + 1 < ASIZE (vec); found_index++)
+       {
+         rfont_def = AREF (vec, found_index + 1);
+         if (NILP (rfont_def))
+           break;
+         if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
+           break;
+         font_object = RFONT_DEF_OBJECT (rfont_def);
+         if (! NILP (font_object) && font_has_char (f, font_object, c))
+           {
+             found_index++;
+             goto found;
+           }
        }
 
-      /* Now we have decided to use this font spec to display C.  */
-      if (! INTEGERP (AREF (elt, 1)))
+      /* Find a font-entity with the current spec and supporting C.  */
+      font_entity = font_find_for_lface (f, face->lface,
+                                        FONT_DEF_SPEC (font_def), c);
+      if (! NILP (font_entity))
        {
-         /* But not yet opened the best matching font.  */
-         struct font_info *font_info;
+         /* We found a font.  Open it and insert a new element for
+            that font in VEC.  */
+         Lisp_Object new_vec;
+         int j;
 
-         font_idx = load_font_get_repertory (f, face, font_def, fontset);
-         ASET (elt, 1, make_number (font_idx));
-         if (font_idx < 0)
-           /* Can't open it.  Try the other one.  */
+         font_object = font_open_for_lface (f, font_entity, face->lface,
+                                            Qnil);
+         if (NILP (font_object))
            continue;
-         font_info = (*get_font_info_func) (f, font_idx);
-         ASET (elt, 3, build_string (font_info->full_name));
+         RFONT_DEF_NEW (rfont_def, font_def);
+         RFONT_DEF_SET_OBJECT (rfont_def, font_object);
+         RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
+         new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
+         found_index++;
+         for (j = 0; j < found_index; j++)
+           ASET (new_vec, j, AREF (vec, j));
+         ASET (new_vec, j, rfont_def);
+         for (j++; j < ASIZE (new_vec); j++)
+           ASET (new_vec, j, AREF (vec, j - 1));
+         XSETCDR (font_group, new_vec);
+         vec = new_vec;
+         goto found;
        }
-      return elt;
+      if (i >= 0)
+       i = found_index;
     }
 
+  FONTSET_SET (fontset, make_number (c), make_number (0));
   return Qnil;
+
+ found:
+  if (fallback && found_index > 0)
+    {
+      /* The order of fonts in the fallback font-group is not that
+        important, and it is better to move the found font to the
+        first of the group so that the next try will find it
+        quickly. */
+      for (i = found_index; i > 0; i--)
+       ASET (vec, i, AREF (vec, i - 1));
+      ASET (vec, 0, rfont_def);
+    }
+  return rfont_def;
 }
 
 
@@ -781,42 +733,58 @@ fontset_font (fontset, c, face, id)
      struct face *face;
      int id;
 {
-  Lisp_Object rfont_def;
+  Lisp_Object rfont_def, default_rfont_def;
   Lisp_Object base_fontset;
 
-  /* Try a font-group for C. */
+  /* Try a font-group of FONTSET. */
+  FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
   rfont_def = fontset_find_font (fontset, c, face, id, 0);
   if (VECTORP (rfont_def))
     return rfont_def;
-  if (EQ (rfont_def, Qt))
-    return Qnil;
+  if (NILP (rfont_def))
+    FONTSET_SET (fontset, make_number (c), make_number (0));
+
+  /* Try a font-group of the default fontset. */
   base_fontset = FONTSET_BASE (fontset);
-  /* Try a font-group for C of the default fontset. */
   if (! EQ (base_fontset, Vdefault_fontset))
     {
       if (NILP (FONTSET_DEFAULT (fontset)))
        FONTSET_DEFAULT (fontset)
          = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
-      rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
-      if (VECTORP (rfont_def))
-       return (rfont_def);
-      if (! NILP (rfont_def))
-       /* Remeber that we have no font for C.  */
-       FONTSET_SET (fontset, make_number (c), Qt);
+      FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
+      default_rfont_def
+       = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
+      if (VECTORP (default_rfont_def))
+       return default_rfont_def;
+      if (NILP (default_rfont_def))
+       FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
+                    make_number (0));
     }
 
-  /* Try a fallback font-group. */
-  rfont_def = fontset_find_font (fontset, c, face, id, 1);
-  if (! VECTORP (rfont_def)
-      && ! EQ (base_fontset, Vdefault_fontset))
-    /* Try a fallback font-group of the default fontset . */
-    rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
+  /* Try a fallback font-group of FONTSET. */
+  if (! EQ (rfont_def, Qt))
+    {
+      FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
+      rfont_def = fontset_find_font (fontset, c, face, id, 1);
+      if (VECTORP (rfont_def))
+       return rfont_def;
+      /* Remember that FONTSET has no font for C.  */
+      FONTSET_SET (fontset, make_number (c), Qt);
+    }
 
-  if (! VECTORP (rfont_def))
-    /* Remeber that we have no font for C.  */
-    FONTSET_SET (fontset, make_number (c), Qt);
+  /* Try a fallback font-group of the default fontset. */
+  if (! EQ (base_fontset, Vdefault_fontset)
+      && ! EQ (default_rfont_def, Qt))
+    {
+      FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
+      rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
+      if (VECTORP (rfont_def))
+       return rfont_def;
+      /* Remember that the default fontset has no font for C.  */
+      FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
+    }
 
-  return rfont_def;
+  return Qnil;
 }
 
 /* Return a newly created fontset with NAME.  If BASE is nil, make a
@@ -860,23 +828,6 @@ make_fontset (frame, name, base)
   return fontset;
 }
 
-
-/* Set the ASCII font of the default fontset to FONTNAME if that is
-   not yet set.  */
-void
-set_default_ascii_font (fontname)
-     Lisp_Object fontname;
-{
-  if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
-    {
-      int id = fs_query_fontset (fontname, 2);
-
-      if (id >= 0)
-       fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
-      FONTSET_ASCII (Vdefault_fontset)= fontname;
-    }
-}
-
 \f
 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
 
@@ -903,15 +854,25 @@ fontset_ascii (id)
 
   fontset= FONTSET_FROM_ID (id);
   elt = FONTSET_ASCII (fontset);
-#ifdef USE_FONT_BACKEND
   if (CONSP (elt))
     elt = XCAR (elt);
-#endif  /* USE_FONT_BACKEND */
-  /* It is assured that ELT is always a string (i.e. fontname
-     pattern).  */
   return elt;
 }
 
+void
+free_realized_fontset (f, fontset)
+     FRAME_PTR f;
+     Lisp_Object fontset;
+{
+  Lisp_Object tail;
+
+  return;
+  for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
+    {
+      xassert (FONT_OBJECT_P (XCAR (tail)));
+      font_close_object (f, XCAR (tail));
+    }
+}
 
 /* Free fontset of FACE defined on frame F.  Called from
    free_realized_face.  */
@@ -923,23 +884,28 @@ free_face_fontset (f, face)
 {
   Lisp_Object fontset;
 
-  fontset = AREF (Vfontset_table, face->fontset);
-  xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
+  fontset = FONTSET_FROM_ID (face->fontset);
+  if (NILP (fontset))
+    return;
+  xassert (! BASE_FONTSET_P (fontset));
   xassert (f == XFRAME (FONTSET_FRAME (fontset)));
+  free_realized_fontset (f, fontset);
   ASET (Vfontset_table, face->fontset, Qnil);
   if (face->fontset < next_fontset_id)
     next_fontset_id = face->fontset;
   if (! NILP (FONTSET_DEFAULT (fontset)))
     {
       int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
-      
+
       fontset = AREF (Vfontset_table, id);
       xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
       xassert (f == XFRAME (FONTSET_FRAME (fontset)));
+      free_realized_fontset (f, fontset);
       ASET (Vfontset_table, id, Qnil);
       if (id < next_fontset_id)
        next_fontset_id = face->fontset;
     }
+  face->fontset = -1;
 }
 
 
@@ -957,8 +923,8 @@ face_suitable_for_char_p (face, c)
   fontset = FONTSET_FROM_ID (face->fontset);
   rfont_def = fontset_font (fontset, c, NULL, -1);
   return (VECTORP (rfont_def)
-         && INTEGERP (AREF (rfont_def, 0))
-         && face->id == XINT (AREF (rfont_def, 0)));
+         && INTEGERP (RFONT_DEF_FACE (rfont_def))
+         && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
 }
 
 
@@ -973,64 +939,116 @@ face_for_char (f, face, c, pos, object)
      int c, pos;
      Lisp_Object object;
 {
-  Lisp_Object fontset, charset, rfont_def;
+  Lisp_Object fontset, rfont_def, charset;
   int face_id;
   int id;
 
-  if (ASCII_CHAR_P (c))
+  /* If face->fontset is negative (that happens when no font is found
+     for face), just return face->ascii_face because we can't do
+     anything.  Perhaps, we should fix the callers to assure
+     that face->fontset is always valid.  */
+  if (ASCII_CHAR_P (c) || face->fontset < 0)
     return face->ascii_face->id;
 
   xassert (fontset_id_valid_p (face->fontset));
   fontset = FONTSET_FROM_ID (face->fontset);
   xassert (!BASE_FONTSET_P (fontset));
+
   if (pos < 0)
-    id = -1;
+    {
+      id = -1;
+      charset = Qnil;
+    }
   else
     {
       charset = Fget_char_property (make_number (pos), Qcharset, object);
-      if (NILP (charset))
-       id = -1;
-      else if (CHARSETP (charset))
+      if (CHARSETP (charset))
        {
          Lisp_Object val;
 
-         val = assoc_no_quit (charset, Vfont_encoding_charset_alist);
+         val = assq_no_quit (charset, Vfont_encoding_charset_alist);
          if (CONSP (val) && CHARSETP (XCDR (val)))
            charset = XCDR (val);
          id = XINT (CHARSET_SYMBOL_ID (charset));
        }
+      else
+       id = -1;
     }
+
   rfont_def = fontset_font (fontset, c, face, id);
   if (VECTORP (rfont_def))
     {
-#ifdef USE_FONT_BACKEND
-      if (enable_font_backend
-         && NILP (AREF (rfont_def, 0)))
+      if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
+       face_id = XINT (RFONT_DEF_FACE (rfont_def));
+      else
        {
-         struct font *font = XSAVE_VALUE (AREF (rfont_def, 3))->pointer;
+         Lisp_Object font_object;
 
-         face_id = face_for_font (f, font, face);
-         ASET (rfont_def, 0, make_number (face_id));
+         font_object = RFONT_DEF_OBJECT (rfont_def);
+         face_id = face_for_font (f, font_object, face);
+         RFONT_DEF_SET_FACE (rfont_def, face_id);
        }
+    }
+  else
+    {
+      if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
+       face_id = XINT (FONTSET_NOFONT_FACE (fontset));
       else
-#endif /* USE_FONT_BACKEND */
-      if (NILP (AREF (rfont_def, 0)))
        {
-         /* We have not yet made a realized face that uses this font.  */
-         int font_idx = XINT (AREF (rfont_def, 1));
-
-         face_id = lookup_non_ascii_face (f, font_idx, face);
-         ASET (rfont_def, 0, make_number (face_id));
+         face_id = face_for_font (f, Qnil, face);
+         FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
        }
-      return XINT (AREF (rfont_def, 0));
+    }
+  xassert (face_id >= 0);
+  return face_id;
+}
+
+
+Lisp_Object
+font_for_char (face, c, pos, object)
+     struct face *face;
+     int c, pos;
+     Lisp_Object object;
+{
+  Lisp_Object fontset, rfont_def, charset;
+  int id;
+
+  if (ASCII_CHAR_P (c))
+    {
+      Lisp_Object font_object;
+
+      XSETFONT (font_object, face->ascii_face->font);
+      return font_object;
     }
 
-  if (NILP (FONTSET_NOFONT_FACE (fontset)))
+  xassert (fontset_id_valid_p (face->fontset));
+  fontset = FONTSET_FROM_ID (face->fontset);
+  xassert (!BASE_FONTSET_P (fontset));
+  if (pos < 0)
     {
-      face_id = lookup_non_ascii_face (f, -1, face);
-      FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
+      id = -1;
+      charset = Qnil;
     }
-  return XINT (FONTSET_NOFONT_FACE (fontset));
+  else
+    {
+      charset = Fget_char_property (make_number (pos), Qcharset, object);
+      if (CHARSETP (charset))
+       {
+         Lisp_Object val;
+
+         val = assq_no_quit (charset, Vfont_encoding_charset_alist);
+         if (CONSP (val) && CHARSETP (XCDR (val)))
+           charset = XCDR (val);
+         id = XINT (CHARSET_SYMBOL_ID (charset));
+       }
+      else
+       id = -1;
+    }
+
+  rfont_def = fontset_font (fontset, c, face, id);
+  return (VECTORP (rfont_def)
+         ? RFONT_DEF_OBJECT (rfont_def)
+         : Qnil);
 }
 
 
@@ -1063,103 +1081,14 @@ make_fontset_for_ascii_face (f, base_fontset_id, face)
   return XINT (FONTSET_ID (fontset));
 }
 
-
-#if defined(WINDOWSNT) && defined (_MSC_VER)
-#pragma optimize("", off)
-#endif
-
-/* Load a font named FONTNAME on frame F.  Return a pointer to the
-   struct font_info of the loaded font.  If loading fails, return
-   NULL.  CHARSET is an ID of charset to encode characters for this
-   font.  If it is -1, find one from Vfont_encoding_alist.  */
-
-struct font_info *
-fs_load_font (f, fontname, charset)
-     FRAME_PTR f;
-     char *fontname;
-     int charset;
-{
-  struct font_info *fontp;
-  Lisp_Object fullname;
-
-  if (!fontname)
-    /* No way to get fontname.  */
-    return NULL;
-
-  fontp = (*load_font_func) (f, fontname, 0);
-  if (! fontp || fontp->charset >= 0)
-    return fontp;
-
-  fontname = fontp->full_name;
-  fullname = build_string (fontp->full_name);
-
-  if (charset < 0)
-    {
-      Lisp_Object charset_symbol;
-
-      charset_symbol = find_font_encoding (fullname);
-      if (CONSP (charset_symbol))
-       charset_symbol = XCAR (charset_symbol);
-      if (NILP (charset_symbol))
-       charset_symbol = Qascii;
-      charset = XINT (CHARSET_SYMBOL_ID (charset_symbol));
-    }
-  fontp->charset = charset;
-  fontp->vertical_centering = 0;
-  fontp->font_encoder = NULL;
-
-  if (charset != charset_ascii)
-    {
-      fontp->vertical_centering
-       = (STRINGP (Vvertical_centering_font_regexp)
-          && (fast_string_match_ignore_case
-              (Vvertical_centering_font_regexp, fullname) >= 0));
-
-      if (find_ccl_program_func)
-       (*find_ccl_program_func) (fontp);
-    }
-
-  return fontp;
-}
-
-#if defined(WINDOWSNT) && defined (_MSC_VER)
-#pragma optimize("", on)
-#endif
-
 \f
-/* Return ENCODING or a cons of ENCODING and REPERTORY of the font
-   FONTNAME.  ENCODING is a charset symbol that specifies the encoding
-   of the font.  REPERTORY is a charset symbol or nil.  */
-
-
-Lisp_Object
-find_font_encoding (fontname)
-     Lisp_Object fontname;
-{
-  Lisp_Object tail, elt;
-
-  for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
-    {
-      elt = XCAR (tail);
-      if (CONSP (elt)
-         && STRINGP (XCAR (elt))
-         && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
-         && (SYMBOLP (XCDR (elt))
-             ? CHARSETP (XCDR (elt))
-             : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
-       return (XCDR (elt));
-    }
-  /* We don't know the encoding of this font.  Let's assume `ascii'.  */
-  return Qascii;
-}
-
 
 /* Cache data used by fontset_pattern_regexp.  The car part is a
    pattern string containing at least one wild card, the cdr part is
    the corresponding regular expression.  */
 static Lisp_Object Vcached_fontset_data;
 
-#define CACHED_FONTSET_NAME (SDATA (XCAR (Vcached_fontset_data)))
+#define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
 
 /* If fontset name PATTERN contains any wild card, return regular
@@ -1169,17 +1098,17 @@ static Lisp_Object
 fontset_pattern_regexp (pattern)
      Lisp_Object pattern;
 {
-  if (!index (SDATA (pattern), '*')
-      && !index (SDATA (pattern), '?'))
+  if (!index ((char *) SDATA (pattern), '*')
+      && !index ((char *) SDATA (pattern), '?'))
     /* PATTERN does not contain any wild cards.  */
     return Qnil;
 
   if (!CONSP (Vcached_fontset_data)
-      || strcmp (SDATA (pattern), CACHED_FONTSET_NAME))
+      || strcmp ((char *) SDATA (pattern), CACHED_FONTSET_NAME))
     {
       /* We must at first update the cached data.  */
       unsigned char *regex, *p0, *p1;
-      int ndashes = 0, nstars = 0;
+      int ndashes = 0, nstars = 0, nescs = 0;
 
       for (p0 = SDATA (pattern); *p0; p0++)
        {
@@ -1187,15 +1116,20 @@ fontset_pattern_regexp (pattern)
            ndashes++;
          else if (*p0 == '*')
            nstars++;
+         else if (*p0 == '['
+                  || *p0 == '.' || *p0 == '\\'
+                  || *p0 == '+' || *p0 == '^'
+                  || *p0 == '$')
+           nescs++;
        }
 
       /* If PATTERN is not full XLFD we conert "*" to ".*".  Otherwise
         we convert "*" to "[^-]*" which is much faster in regular
         expression matching.  */
       if (ndashes < 14)
-       p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 1);
+       p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
       else
-       p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 1);
+       p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
 
       *p1++ = '^';
       for (p0 = SDATA (pattern); *p0; p0++)
@@ -1210,14 +1144,19 @@ fontset_pattern_regexp (pattern)
            }
          else if (*p0 == '?')
            *p1++ = '.';
+         else if (*p0 == '['
+                  || *p0 == '.' || *p0 == '\\'
+                  || *p0 == '+' || *p0 == '^'
+                  || *p0 == '$')
+           *p1++ = '\\', *p1++ = *p0;
          else
            *p1++ = *p0;
        }
       *p1++ = '$';
       *p1++ = 0;
 
-      Vcached_fontset_data = Fcons (build_string (SDATA (pattern)),
-                                   build_string (regex));
+      Vcached_fontset_data = Fcons (build_string ((char *) SDATA (pattern)),
+                                   build_string ((char *) regex));
     }
 
   return CACHED_FONTSET_REGEX;
@@ -1268,8 +1207,8 @@ fs_query_fontset (name, name_pattern)
 
       this_name = FONTSET_NAME (fontset);
       if (name_pattern == 1
-         ? fast_string_match (name, this_name) >= 0
-         : !strcmp (SDATA (name), SDATA (this_name)))
+         ? fast_string_match_ignore_case (name, this_name) >= 0
+         : !xstrcasecmp (SDATA (name), SDATA (this_name)))
        return i;
     }
   return -1;
@@ -1332,7 +1271,7 @@ list_fontsets (f, pattern, size)
 
       if (STRINGP (regexp)
          ? (fast_string_match (regexp, name) < 0)
-         : strcmp (SDATA (pattern), SDATA (name)))
+         : strcmp ((char *) SDATA (pattern), (char *) SDATA (name)))
        continue;
 
       val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
@@ -1396,25 +1335,39 @@ free_realized_fontsets (base)
 
 /* Check validity of NAME as a fontset name and return the
    corresponding fontset.  If not valid, signal an error.
-   If NAME is t, return Vdefault_fontset.  */
+
+   If NAME is t, return Vdefault_fontset.  If NAME is nil, return the
+   fontset of *FRAME.
+
+   Set *FRAME to the actual frame.  */
 
 static Lisp_Object
-check_fontset_name (name)
-     Lisp_Object name;
+check_fontset_name (name, frame)
+     Lisp_Object name, *frame;
 {
   int id;
 
+  if (NILP (*frame))
+    *frame = selected_frame;
+  CHECK_LIVE_FRAME (*frame);
+
   if (EQ (name, Qt))
     return Vdefault_fontset;
-
-  CHECK_STRING (name);
-  /* First try NAME as literal.  */
-  id = fs_query_fontset (name, 2);
-  if (id < 0)
-    /* For backward compatibility, try again NAME as pattern.  */
-    id = fs_query_fontset (name, 0);
-  if (id < 0)
-    error ("Fontset `%s' does not exist", SDATA (name));
+  if (NILP (name))
+    {
+      id = FRAME_FONTSET (XFRAME (*frame));
+    }
+  else
+    {
+      CHECK_STRING (name);
+      /* First try NAME as literal.  */
+      id = fs_query_fontset (name, 2);
+      if (id < 0)
+       /* For backward compatibility, try again NAME as pattern.  */
+       id = fs_query_fontset (name, 0);
+      if (id < 0)
+       error ("Fontset `%s' does not exist", SDATA (name));
+    }
   return FONTSET_FROM_ID (id);
 }
 
@@ -1432,77 +1385,70 @@ accumulate_script_ranges (arg, range, val)
 }
 
 
-/* Return an ASCII font name generated from fontset name NAME and
-   font-spec ASCII_SPEC.  NAME is a string conforming to XLFD.  */
+/* Callback function for map_charset_chars in Fset_fontset_font.
+   ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
 
-static INLINE Lisp_Object
-generate_ascii_font_name (name, ascii_spec)
-     Lisp_Object name, ascii_spec;
-{
-  Lisp_Object font_spec = Ffont_spec (0, NULL);
-  Lisp_Object vec;
-  int i;
-  char xlfd[256];
-
-  if (font_parse_xlfd (SDATA (name), font_spec) < 0)
-    error ("Not an XLFD font name: %s", SDATA (name));
-  for (i = FONT_FOUNDRY_INDEX; i <= FONT_WIDTH_INDEX; i++)
-    if (! NILP (AREF (ascii_spec, i)))
-      ASET (font_spec, i, AREF (ascii_spec, i));
-  i = font_unparse_xlfd (font_spec, 0, xlfd, 256);
-  if (i < 0)
-    error ("Not an XLFD font name: %s", SDATA (name));
-  return make_unibyte_string (xlfd, i);
-}
-
-/* Variables referred in set_fontset_font.  They are set before
-   map_charset_chars is called in Fset_fontset_font.  */
-static Lisp_Object font_def_arg, add_arg;
-static int from_arg, to_arg;
+   In FONTSET, set FONT_DEF in a fashion specified by ADD for
+   characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
+   The consumed ranges are poped up from SCRIPT_RANGE_LIST, and the
+   new SCRIPT_RANGE_LIST is stored in ARG.
 
-/* Callback function for map_charset_chars in Fset_fontset_font.  In
-   FONTSET, set font_def_arg in a fashion specified by add_arg for
-   characters in RANGE while ignoring the range between from_arg and
-   to_arg.  */
+   If ASCII is nil, don't set FONT_DEF for ASCII characters.  It is
+   assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
+   case.  */
 
 static void
-set_fontset_font (fontset, range)
-     Lisp_Object fontset, range;
+set_fontset_font (arg, range)
+     Lisp_Object arg, range;
 {
-  if (from_arg < to_arg)
-    {
-      int from = XINT (XCAR (range)), to = XINT (XCDR (range));
+  Lisp_Object fontset, font_def, add, ascii, script_range_list;
+  int from = XINT (XCAR (range)), to = XINT (XCDR (range));
 
-      if (from < from_arg)
-       {
-         if (to > to_arg)
-           {
-             Lisp_Object range2;
+  fontset = AREF (arg, 0);
+  font_def = AREF (arg, 1);
+  add = AREF (arg, 2);
+  ascii = AREF (arg, 3);
+  script_range_list = AREF (arg, 4);
 
-             range2 = Fcons (make_number (to_arg), XCDR (range));
-             FONTSET_ADD (fontset, range, font_def_arg, add_arg);
-             to = to_arg;
-           }
-         if (to > from_arg)
-           range = Fcons (XCAR (range), make_number (from_arg));
-       }
-      else if (to <= to_arg)
+  if (NILP (ascii) && from < 0x80)
+    {
+      if (to < 0x80)
        return;
-      else
-       {
-         if (from < to_arg)
-           range = Fcons (make_number (to_arg), XCDR (range));
-       }
+      from = 0x80;
+      range = Fcons (make_number (0x80), XCDR (range));
     }
-  FONTSET_ADD (fontset, range, font_def_arg, add_arg);
+
+#define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
+#define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
+#define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
+
+  for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
+    FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
+  if (CONSP (script_range_list))
+    {
+      if (SCRIPT_FROM < from)
+       range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
+      while (CONSP (script_range_list) && SCRIPT_TO <= to)
+       POP_SCRIPT_RANGE ();
+      if (CONSP (script_range_list) && SCRIPT_FROM <= to)
+       XSETCAR (XCAR (script_range_list), make_number (to + 1));
+    }
+
+  FONTSET_ADD (fontset, range, font_def, add);
+  ASET (arg, 4, script_range_list);
 }
 
 extern Lisp_Object QCfamily, QCregistry;
+static void update_auto_fontset_alist P_ ((Lisp_Object, Lisp_Object));
+
 
 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
        doc: /*
 Modify fontset NAME to use FONT-SPEC for TARGET characters.
 
+NAME is a fontset name string, nil for the fontset of FRAME, or t for
+the default fontset.
+
 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
 In that case, use FONT-SPEC for all characters in the range FROM and
 TO (inclusive).
@@ -1517,14 +1463,15 @@ TARGET may be nil.  In that case, use FONT-SPEC for any characters for
 that no FONT-SPEC is specified.
 
 FONT-SPEC may one of these:
+ * A font-spec object made by the function `font-spec' (which see).
  * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
-   REGISTRY is a font registry name.  FAMILY may contains foundry
-   name, and REGISTRY may contains encoding name.
+   REGISTRY is a font registry name.  FAMILY may contain foundry
+   name, and REGISTRY may contain encoding name.
  * A font name string.
  * nil, which explicitly specifies that there's no font for TARGET.
 
-Optional 4th argument FRAME, if non-nil, is a frame.  This argument is
-kept for backward compatibility and has no meaning.
+Optional 4th argument FRAME is a frame or nil for the selected frame
+that is concerned in the case that NAME is nil.
 
 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
 to the font specifications for TARGET previously set.  If it is
@@ -1535,62 +1482,49 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
 {
   Lisp_Object fontset;
   Lisp_Object font_def, registry, family;
-  Lisp_Object encoding, repertory;
   Lisp_Object range_list;
   struct charset *charset = NULL;
+  Lisp_Object fontname;
+  int ascii_changed = 0;
 
-  fontset = check_fontset_name (name);
-
-  /* The arg FRAME is kept for backward compatibility.  We only check
-     the validity.  */
-  if (!NILP (frame))
-    CHECK_LIVE_FRAME (frame);
+  fontset = check_fontset_name (name, &frame);
 
-  if (VECTORP (font_spec))
+  fontname = Qnil;
+  if (CONSP (font_spec))
     {
-      if (! FONT_SPEC_P (font_spec))
-       Fsignal (Qfont, list2 (build_string ("invalid font-spec"), font_spec));
-    }
-  else if (CONSP (font_spec))
-    {
-      Lisp_Object args[4];
-      int i= 0;
+      Lisp_Object spec = Ffont_spec (0, NULL);
 
-      family = XCAR (font_spec);
-      registry = XCDR (font_spec);
-
-      if (! NILP (family))
-       {
-         CHECK_STRING (family);
-         args[i++] = QCfamily;
-         args[i++] = family;
-       }
-      CHECK_STRING (registry);
-      args[i++] = QCregistry;
-      args[i++] = registry;
-      font_spec = Ffont_spec (i, args);
+      font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
+      font_spec = spec;
+      fontname = Ffont_xlfd_name (font_spec, Qnil);
     }
   else if (STRINGP (font_spec))
     {
       Lisp_Object args[2];
+      extern Lisp_Object QCname;
 
+      fontname = font_spec;
       args[0] = QCname;
       args[1] = font_spec;
       font_spec = Ffont_spec (2, args);
     }
+  else if (FONT_SPEC_P (font_spec))
+    fontname = Ffont_xlfd_name (font_spec, Qnil);
   else if (! NILP (font_spec))
-    wrong_type_argument (intern ("font-spec"), font_spec);
+    Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
 
   if (! NILP (font_spec))
     {
+      Lisp_Object encoding, repertory;
+
       family = AREF (font_spec, FONT_FAMILY_INDEX);
-      if (! NILP (family) && SYMBOLP (family))
+      if (! NILP (family) )
        family = SYMBOL_NAME (family);
       registry = AREF (font_spec, FONT_REGISTRY_INDEX);
-      if (! NILP (registry) && SYMBOLP (registry))
-       registry = SYMBOL_NAME (registry);
-
-      encoding = find_font_encoding (concat2 (family, registry));
+      if (! NILP (registry))
+       registry = Fdowncase (SYMBOL_NAME (registry));
+      encoding = find_font_encoding (concat3 (family, build_string ("-"),
+                                             registry));
       if (NILP (encoding))
        encoding = Qascii;
 
@@ -1611,15 +1545,17 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
              repertory = CHARSET_SYMBOL_ID (repertory);
            }
        }
-      font_def = Fmake_vector (make_number (3), font_spec);
-      ASET (font_def, 1, encoding);
-      ASET (font_def, 2, repertory);
+      FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
     }
   else
     font_def = Qnil;
 
   if (CHARACTERP (target))
-    range_list = Fcons (Fcons (target, target), Qnil);
+    {
+      if (XFASTINT (target) < 0x80)
+       error ("Can't set a font for partial ASCII range.");
+      range_list = Fcons (Fcons (target, target), Qnil);
+    }
   else if (CONSP (target))
     {
       Lisp_Object from, to;
@@ -1628,6 +1564,12 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
       to = Fcdr (target);
       CHECK_CHARACTER (from);
       CHECK_CHARACTER (to);
+      if (XFASTINT (from) < 0x80)
+       {
+         if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
+           error ("Can't set a font for partial ASCII range.");
+         ascii_changed = 1;
+       }
       range_list = Fcons (target, Qnil);
     }
   else if (SYMBOLP (target) && !NILP (target))
@@ -1639,35 +1581,18 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
       script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
       if (! NILP (Fmemq (target, script_list)))
        {
+         if (EQ (target, Qlatin))
+           ascii_changed = 1;
          val = Fcons (target, Qnil);
          map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
                          val);
-         range_list = XCDR (val);
-         if (EQ (target, Qlatin) && NILP (FONTSET_ASCII (fontset)))
-           {
-             if (VECTORP (font_spec))
-               val = generate_ascii_font_name (FONTSET_NAME (fontset),
-                                               font_spec);
-             else
-               val = font_spec;
-             FONTSET_ASCII (fontset) = val;
-           }
+         range_list = Fnreverse (XCDR (val));
        }
       if (CHARSETP (target))
        {
-         if (EQ (target, Qascii) && NILP (FONTSET_ASCII (fontset)))
-           {
-             if (VECTORP (font_spec))
-               font_spec = generate_ascii_font_name (FONTSET_NAME (fontset),
-                                                     font_spec);
-             FONTSET_ASCII (fontset) = font_spec;
-             range_list = Fcons (Fcons (make_number (0), make_number (127)),
-                                 Qnil);
-           }
-         else
-           {
-             CHECK_CHARSET_GET_CHARSET (target, charset);
-           }
+         CHECK_CHARSET_GET_CHARSET (target, charset);
+         if (charset->ascii_compatible_p)
+           ascii_changed = 1;
        }
       else if (NILP (range_list))
        error ("Invalid script or charset name: %s",
@@ -1678,24 +1603,68 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
   else
     error ("Invalid target for setting a font");
 
+  if (ascii_changed)
+    {
+      Lisp_Object val;
+
+      if (NILP (font_spec))
+       error ("Can't set ASCII font to nil");
+      val = CHAR_TABLE_REF (fontset, 0);
+      if (! NILP (val) && EQ (add, Qappend))
+       /* We are going to change just an additional font for ASCII.  */
+       ascii_changed = 0;
+    }
 
   if (charset)
     {
-      font_def_arg = font_def;
-      add_arg = add;
-      if (NILP (range_list))
-       from_arg = to_arg = 0;
-      else
-       from_arg = XINT (XCAR (XCAR (range_list))),
-         to_arg = XINT (XCDR (XCAR (range_list)));
+      Lisp_Object arg;
+
+      arg = Fmake_vector (make_number (5), Qnil);
+      ASET (arg, 0, fontset);
+      ASET (arg, 1, font_def);
+      ASET (arg, 2, add);
+      ASET (arg, 3, ascii_changed ? Qt : Qnil);
+      ASET (arg, 4, range_list);
 
-      map_charset_chars (set_fontset_font, Qnil, fontset, charset,
+      map_charset_chars (set_fontset_font, Qnil, arg, charset,
                         CHARSET_MIN_CODE (charset),
                         CHARSET_MAX_CODE (charset));
+      range_list = AREF (arg, 4);
     }
   for (; CONSP (range_list); range_list = XCDR (range_list))
     FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
 
+  if (ascii_changed)
+    {
+      Lisp_Object tail, frame, alist;
+      int fontset_id = XINT (FONTSET_ID (fontset));
+
+      FONTSET_ASCII (fontset) = fontname;
+      name = FONTSET_NAME (fontset);
+      FOR_EACH_FRAME (tail, frame)
+       {
+         FRAME_PTR f = XFRAME (frame);
+         Lisp_Object font_object;
+         struct face *face;
+
+         if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
+           continue;
+         if (fontset_id != FRAME_FONTSET (f))
+           continue;
+         face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
+         if (face)
+           font_object = font_load_for_lface (f, face->lface, font_spec);
+         else
+           font_object = font_open_by_spec (f, font_spec);
+         if (! NILP (font_object))
+           {
+             update_auto_fontset_alist (font_object, fontset);
+             alist = Fcons (Fcons (Qfont, Fcons (name, font_object)), Qnil);
+             Fmodify_frame_parameters (frame, alist);
+           }
+       }
+    }
+
   /* Free all realized fontsets whose base is FONTSET.  This way, the
      specified character(s) are surely redisplayed by a correct
      font.  */
@@ -1722,33 +1691,38 @@ FONT-SPEC is a vector, a cons, or a string.  See the documentation of
      Lisp_Object name, fontlist;
 {
   Lisp_Object fontset;
-  Lisp_Object val;
   int id;
 
   CHECK_STRING (name);
   CHECK_LIST (fontlist);
 
+  name = Fdowncase (name);
   id = fs_query_fontset (name, 0);
   if (id < 0)
     {
-      name = Fdowncase (name);
-      val = split_font_name_into_vector (name);
-      if (NILP (val) || NILP (AREF (val, 12)) || NILP (AREF (val, 13)))
+      Lisp_Object font_spec = Ffont_spec (0, NULL);
+      Lisp_Object short_name;
+      char xlfd[256];
+      int len;
+
+      if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
        error ("Fontset name must be in XLFD format");
-      if (strcmp (SDATA (AREF (val, 12)), "fontset"))
-       error ("Registry field of fontset name must be \"fontset\"");
-      Vfontset_alias_alist
-       = Fcons (Fcons (name,
-                       concat2 (concat2 (AREF (val, 12), build_string ("-")),
-                                AREF (val, 13))),
-                Vfontset_alias_alist);
-      ASET (val, 12, build_string ("iso8859-1"));
+      short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
+      if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
+         || SBYTES (SYMBOL_NAME (short_name)) < 9)
+       error ("Registry field of fontset name must be \"fontset-*\"");
+      Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
+                                   Vfontset_alias_alist);
+      ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
       fontset = make_fontset (Qnil, name, Qnil);
-      FONTSET_ASCII (fontset) = build_font_name_from_vector (val);
+      len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
+      if (len < 0)
+       error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
+      FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
     }
   else
     {
-      fontset = FONTSET_FROM_ID (id);;
+      fontset = FONTSET_FROM_ID (id);
       free_realized_fontsets (fontset);
       Fset_char_table_range (fontset, Qt, Qnil);
     }
@@ -1771,213 +1745,102 @@ FONT-SPEC is a vector, a cons, or a string.  See the documentation of
 
 
 /* Alist of automatically created fontsets.  Each element is a cons
-   (FONTNAME . FONTSET-ID).  */
+   (FONT-SPEC . FONTSET-ID).  */
 static Lisp_Object auto_fontset_alist;
 
-int
-new_fontset_from_font_name (Lisp_Object fontname)
-{
-  Lisp_Object val;
-  Lisp_Object name;
-  Lisp_Object vec;
-  int id;
-
-  fontname = Fdowncase (fontname);
-  val = Fassoc (fontname, auto_fontset_alist);
-  if (CONSP (val))
-    return XINT (XCDR (val));
-
-  vec = split_font_name_into_vector (fontname);
-  if ( NILP (vec))
-    vec = Fmake_vector (make_number (14), build_string (""));
-  ASET (vec, 12, build_string ("fontset"));
-  if (NILP (auto_fontset_alist))
-    {
-      ASET (vec, 13, build_string ("startup"));
-      name = build_font_name_from_vector (vec);
-    }
-  else
-    {
-      char temp[20];
-      int len = XINT (Flength (auto_fontset_alist));
+/* Number of automatically created fontsets.  */
+static int num_auto_fontsets;
 
-      sprintf (temp, "auto%d", len);
-      ASET (vec, 13, build_string (temp));
-      name = build_font_name_from_vector (vec);
-    }
-  name = Fnew_fontset (name, list2 (list2 (Qascii, fontname),
-                                   list2 (Fcons (make_number (0),
-                                                 make_number (MAX_CHAR)),
-                                          fontname)));
-  id = fs_query_fontset (name, 0);
-  auto_fontset_alist
-    = Fcons (Fcons (fontname, make_number (id)), auto_fontset_alist);
-  return id;
-}
+/* Retun a fontset synthesized from FONT-OBJECT.  This is called from
+   x_new_font when FONT-OBJECT is used for the default ASCII font of a
+   frame, and the returned fontset is used for the default fontset of
+   that frame.  The fontset specifies a font of the same registry as
+   FONT-OBJECT for all characters in the repertory of the registry
+   (see Vfont_encoding_alist).  If the repertory is not known, the
+   fontset specifies the font for all Latin characters assuming that a
+   user intends to use FONT-OBJECT for Latin characters.  */
 
-#ifdef USE_FONT_BACKEND
 int
-new_fontset_from_font (font_object)
+fontset_from_font (font_object)
      Lisp_Object font_object;
 {
   Lisp_Object font_name = font_get_name (font_object);
-  Lisp_Object font_spec = font_get_spec (font_object);
-  Lisp_Object fontset_spec, short_name, name, fontset;
+  Lisp_Object font_spec = Fcopy_font_spec (font_object);
+  Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
+  Lisp_Object fontset_spec, alias, name, fontset;
+  Lisp_Object val;
 
-  if (NILP (auto_fontset_alist))
-    short_name = build_string ("fontset-startup");
+  val = assoc_no_quit (font_spec, auto_fontset_alist);
+  if (CONSP (val))
+    return XINT (FONTSET_ID (XCDR (val)));
+  if (num_auto_fontsets++ == 0)
+    alias = intern ("fontset-startup");
   else
     {
       char temp[32];
-      int len = XINT (Flength (auto_fontset_alist));
 
-      sprintf (temp, "fontset-auto%d", len);
-      short_name = build_string (temp);
+      sprintf (temp, "fontset-auto%d", num_auto_fontsets - 1);
+      alias = intern (temp);
     }
-  fontset_spec = Fcopy_sequence (font_spec);
-  ASET (fontset_spec, FONT_REGISTRY_INDEX, short_name);
-  name = Ffont_xlfd_name (fontset_spec);
+  fontset_spec = Fcopy_font_spec (font_spec);
+  ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
+  name = Ffont_xlfd_name (fontset_spec, Qnil);
   if (NILP (name))
-    {
-      int i;
-
-      for (i = 0; i < FONT_SIZE_INDEX; i++)
-       if ((i != FONT_FAMILY_INDEX) && (i != FONT_REGISTRY_INDEX))
-         ASET (fontset_spec, i, Qnil);
-      name = Ffont_xlfd_name (fontset_spec);
-      if (NILP (name))
-       abort ();
-    }
+    abort ();
   fontset = make_fontset (Qnil, name, Qnil);
+  Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
+                               Vfontset_alias_alist);
+  alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
+  Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
+  auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
+  font_spec = Ffont_spec (0, NULL);
+  ASET (font_spec, FONT_REGISTRY_INDEX, registry);
+  {
+    Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
+
+    if (CONSP (target))
+      target = XCDR (target);
+    if (! CHARSETP (target))
+      target = Qlatin;
+    Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
+    Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
+  }
+
   FONTSET_ASCII (fontset) = font_name;
-  font_spec = Fcons (SYMBOL_NAME (AREF (font_spec, FONT_FAMILY_INDEX)),
-                    SYMBOL_NAME (AREF (font_spec, FONT_REGISTRY_INDEX)));
-  Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qnil);
-  XSETCDR (font_spec, build_string ("iso10646-1"));
-  Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qappend);
-  Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
+
   return XINT (FONTSET_ID (fontset));
 }
 
-struct font *
-fontset_ascii_font (f, id)
-     FRAME_PTR f;
-     int id;
-{
-  Lisp_Object fontset = FONTSET_FROM_ID (id);
-  Lisp_Object ascii_slot = FONTSET_ASCII (fontset);
-  Lisp_Object val, font_object;
-
-  if (CONSP (ascii_slot))
-    {
-      Lisp_Object ascii_font_name = XCAR (ascii_slot);
 
-      font_object = Qnil;
-      for (val = XCDR (ascii_slot); ! NILP (val); val = XCDR (val))
-       {
-         Lisp_Object frame = font_get_frame (XCAR (val));
-
-         if (NILP (frame) || XFRAME (frame) == f)
-           {
-             font_object = XCAR (val);
-             if (XSAVE_VALUE (font_object)->integer == 0)
-               {
-                 font_object = font_open_by_name (f, SDATA (ascii_font_name));
-                 XSETCAR (val, font_object);
-               }
-             break;
-           }
-       }
-      if (NILP (font_object))
-       {
-         font_object = font_open_by_name (f, SDATA (ascii_font_name));
-         XSETCDR (ascii_slot, Fcons (font_object, XCDR (ascii_slot)));
-       }
-    }
-  else
-    {
-      font_object = font_open_by_name (f, SDATA (ascii_slot));
-      FONTSET_ASCII (fontset) = Fcons (ascii_slot, Fcons (font_object, Qnil));
-    }
-  if (NILP (font_object))
-    return NULL;
-  return XSAVE_VALUE (font_object)->pointer;
-}
+/* Update auto_fontset_alist for FONTSET.  When an ASCII font of
+   FONTSET is changed, we delete an entry of FONTSET if any from
+   auto_fontset_alist so that FONTSET is not re-used by
+   fontset_from_font.  */
 
-#endif /* USE_FONT_BACKEND */
-
-DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
-       doc: /* Return information about a font named NAME on frame FRAME.
-If FRAME is omitted or nil, use the selected frame.
-The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
-  HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
-where
-  OPENED-NAME is the name used for opening the font,
-  FULL-NAME is the full name of the font,
-  SIZE is the maximum bound width of the font,
-  HEIGHT is the height of the font,
-  BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
-  RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
-    how to compose characters.
-If the named font is not yet loaded, return nil.  */)
-     (name, frame)
-     Lisp_Object name, frame;
+static void
+update_auto_fontset_alist (font_object, fontset)
+     Lisp_Object font_object, fontset;
 {
-  FRAME_PTR f;
-  struct font_info *fontp;
-  Lisp_Object info;
-  Lisp_Object font_object;
-
-  (*check_window_system_func) ();
-
-  CHECK_STRING (name);
-  name = Fdowncase (name);
-  if (NILP (frame))
-    frame = selected_frame;
-  CHECK_LIVE_FRAME (frame);
-  f = XFRAME (frame);
-
-  if (!query_font_func)
-    error ("Font query function is not supported");
-
-#ifdef USE_FONT_BACKEND
-  if (enable_font_backend)
-    {
-      font_object = font_open_by_name (f, SDATA (name));
-      if (NILP (font_object))
-       fontp = NULL;
-      else
-       fontp = (struct font_info *) XSAVE_VALUE (font_object)->pointer;
-    }
-  else
-#endif /* USE_FONT_BACKEND */
-  fontp = (*query_font_func) (f, SDATA (name));
-  if (!fontp)
-    return Qnil;
-
-  info = Fmake_vector (make_number (7), Qnil);
-
-  XVECTOR (info)->contents[0] = build_string (fontp->name);
-  XVECTOR (info)->contents[1] = build_string (fontp->full_name);
-  XVECTOR (info)->contents[2] = make_number (fontp->size);
-  XVECTOR (info)->contents[3] = make_number (fontp->height);
-  XVECTOR (info)->contents[4] = make_number (fontp->baseline_offset);
-  XVECTOR (info)->contents[5] = make_number (fontp->relative_compose);
-  XVECTOR (info)->contents[6] = make_number (fontp->default_ascent);
-
-#ifdef USE_FONT_BACKEND
-  if (! NILP (font_object))
-    font_close_object (f, font_object);
-#endif /* USE_FONT_BACKEND */
-  return info;
+  Lisp_Object prev, tail;
+
+  for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
+       prev = tail, tail = XCDR (tail))
+    if (EQ (fontset, XCDR (XCAR (tail))))
+      {
+       if (NILP (prev))
+         auto_fontset_alist = XCDR (tail);
+       else
+         XSETCDR (prev, XCDR (tail));
+       break;
+      }
 }
 
 
-/* Return a cons (FONT-NAME . GLYPH-CODE).
-   FONT-NAME is the font name for the character at POSITION in the current
+/* Return a cons (FONT-OBJECT . GLYPH-CODE).
+   FONT-OBJECT is the font for the character at POSITION in the current
    buffer.  This is computed from all the text properties and overlays
    that apply to POSITION.  POSTION may be nil, in which case,
-   FONT-NAME is the font name for display the character CH with the
+   FONT-SPEC is the font for displaying the character CH with the
    default face.
 
    GLYPH-CODE is the glyph code in the font to use for the character.
@@ -2005,12 +1868,11 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
      (position, ch)
      Lisp_Object position, ch;
 {
-  int pos, pos_byte, dummy;
+  EMACS_INT pos, pos_byte, dummy;
   int face_id;
   int c;
   struct frame *f;
   struct face *face;
-  Lisp_Object charset, rfont_def;
   int cs_id;
 
   if (NILP (position))
@@ -2018,7 +1880,7 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
       CHECK_CHARACTER (ch);
       c = XINT (ch);
       f = XFRAME (selected_frame);
-      face_id = DEFAULT_FACE_ID;
+      face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
       pos = -1;
       cs_id = -1;
     }
@@ -2044,7 +1906,8 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
        return Qnil;
       w = XWINDOW (window);
       f = XFRAME (w->frame);
-      face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
+      face_id = face_at_buffer_position (w, pos, -1, -1, &dummy,
+                                        pos + 100, 0, -1);
       charset = Fget_char_property (position, Qcharset, Qnil);
       if (CHARSETP (charset))
        cs_id = XINT (CHARSET_SYMBOL_ID (charset));
@@ -2055,45 +1918,21 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
     return Qnil;
   face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
   face = FACE_FROM_ID (f, face_id);
-  rfont_def = fontset_font (FONTSET_FROM_ID (face->fontset), c, face, cs_id);
-#ifdef USE_FONT_BACKEND
-  if (enable_font_backend)
+  if (face->font)
     {
-      if (VECTORP (rfont_def) && ! NILP (AREF (rfont_def, 3)))
-       {
-         Lisp_Object font_object = AREF (rfont_def, 3);
-         struct font *font = XSAVE_VALUE (font_object)->pointer;
-         unsigned code = font->driver->encode_char (font, c);
-         Lisp_Object fontname = font_get_name (font_object);
+      unsigned code = face->font->driver->encode_char (face->font, c);
+      Lisp_Object font_object;
+      /* Assignment to EMACS_INT stops GCC whining about limited range
+        of data type.  */
+      EMACS_INT cod = code;
 
-         if (code == FONT_INVALID_CODE)
-           return Qnil;
-         if (code <= MOST_POSITIVE_FIXNUM)
-           return Fcons (fontname, make_number (code));
-         return Fcons (fontname, Fcons (make_number (code >> 16),
-                                        make_number (code & 0xFFFF)));
-       }
-      return Qnil;
-    }
-#endif /* USE_FONT_BACKEND */
-  if (VECTORP (rfont_def) && STRINGP (AREF (rfont_def, 3)))
-    {
-      Lisp_Object font_def;
-      struct font_info *fontp;
-      struct charset *charset;
-      XChar2b char2b;
-      int code;
-
-      font_def = AREF (rfont_def, 2);
-      charset = CHARSET_FROM_ID (XINT (AREF (font_def, 1)));
-      code = ENCODE_CHAR (charset, c);
-      if (code == CHARSET_INVALID_CODE (charset))
-       return (Fcons (AREF (rfont_def, 3), Qnil));
-      STORE_XCHAR2B (&char2b, ((code >> 8) & 0xFF), (code & 0xFF));
-      fontp = (*get_font_info_func) (f, XINT (AREF (rfont_def, 1)));
-      FRAME_RIF (f)->encode_char (c, &char2b, fontp, charset, NULL);
-      code = (XCHAR2B_BYTE1 (&char2b) << 8) | XCHAR2B_BYTE2 (&char2b);
-      return (Fcons (AREF (rfont_def, 3), make_number (code)));
+      if (code == FONT_INVALID_CODE)
+       return Qnil;
+      XSETFONT (font_object, face->font);
+      if (cod <= MOST_POSITIVE_FIXNUM)
+       return Fcons (font_object, make_number (code));
+      return Fcons (font_object, Fcons (make_number (code >> 16),
+                                    make_number (code & 0xFFFF)));
     }
   return Qnil;
 }
@@ -2101,21 +1940,22 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
 
 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
        doc: /* Return information about a fontset FONTSET on frame FRAME.
-The value is a char-table of which elements has this form.
 
-    ((FONT-PATTERN OPENED-FONT ...) ...)
+FONTSET is a fontset name string, nil for the fontset of FRAME, or t
+for the default fontset.  FRAME nil means the selected frame.
 
-FONT-PATTERN is a vector:
+The value is a char-table whose elements have this form:
 
-       [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
+    ((FONT OPENED-FONT ...) ...)
 
-or a string of font name pattern.
+FONT is a name of font specified for a range of characters.
 
 OPENED-FONT is a name of a font actually opened.
 
-The char-table has one extra slot.  The value is a char-table
-containing the information about the derived fonts from the default
-fontset.  The format is the same as abobe.  */)
+The char-table has one extra slot.  If FONTSET is not the default
+fontset, the value the extra slot is a char-table containing the
+information about the derived fonts from the default fontset.  The
+format is the same as above.  */)
      (fontset, frame)
      Lisp_Object fontset, frame;
 {
@@ -2126,11 +1966,7 @@ fontset.  The format is the same as abobe.  */)
 
   (*check_window_system_func) ();
 
-  fontset = check_fontset_name (fontset);
-
-  if (NILP (frame))
-    frame = selected_frame;
-  CHECK_LIVE_FRAME (frame);
+  fontset = check_fontset_name (fontset, &frame);
   f = XFRAME (frame);
 
   /* Recode fontsets realized on FRAME from the base fontset FONTSET
@@ -2158,10 +1994,13 @@ fontset.  The format is the same as abobe.  */)
   realized[1][j] = Qnil;
 
   tables[0] = Fmake_char_table (Qfontset_info, Qnil);
-  tables[1] = Fmake_char_table (Qnil, Qnil);
-  XCHAR_TABLE (tables[0])->extras[0] = tables[1];
   fontsets[0] = fontset;
-  fontsets[1] = Vdefault_fontset;
+  if (!EQ (fontset, Vdefault_fontset))
+    {
+      tables[1] = Fmake_char_table (Qnil, Qnil);
+      XCHAR_TABLE (tables[0])->extras[0] = tables[1];
+      fontsets[1] = Vdefault_fontset;
+    }
 
   /* Accumulate information of the fontset in TABLE.  The format of
      each element is ((FONT-SPEC OPENED-FONT ...) ...).  */
@@ -2169,13 +2008,11 @@ fontset.  The format is the same as abobe.  */)
     {
       for (c = 0; c <= MAX_CHAR; )
        {
-         int from, to;
+         int from = c, to = MAX_5_BYTE_CHAR;
 
          if (c <= MAX_5_BYTE_CHAR)
            {
              val = char_table_ref_and_range (fontsets[k], c, &from, &to);
-             if (to > MAX_5_BYTE_CHAR)
-               to = MAX_5_BYTE_CHAR;
            }
          else
            {
@@ -2188,78 +2025,35 @@ fontset.  The format is the same as abobe.  */)
 
              /* At first, set ALIST to ((FONT-SPEC) ...).  */
              for (alist = Qnil, i = 0; i < ASIZE (val); i++)
-               {
-                 if (NILP (AREF (val, i)))
-                   alist = Fcons (Qnil, alist);
-                 else
-                   alist = Fcons (Fcons (AREF (AREF (val, i), 0), Qnil), alist);
-               }
+               if (! NILP (AREF (val, i)))
+                 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
+                                alist);
              alist = Fnreverse (alist);
 
-             /* Then store opend font names to cdr of each elements.  */
+             /* Then store opened font names to cdr of each elements.  */
              for (i = 0; ! NILP (realized[k][i]); i++)
                {
                  if (c <= MAX_5_BYTE_CHAR)
                    val = FONTSET_REF (realized[k][i], c);
                  else
                    val = FONTSET_FALLBACK (realized[k][i]);
-                 if (! VECTORP (val))
+                 if (! CONSP (val) || ! VECTORP (XCDR (val)))
                    continue;
-#ifdef USE_FONT_BACKEND
-                 /* VAL: [int int ?
-                          [FACE-ID FONT-INDEX FONT-DEF FONT-ENTITY/OBJECT]
-                          ...]  */
-                 if (enable_font_backend)
-                   for (j = 3; j < ASIZE (val); j++)
-                     {
-                       elt = AREF (val, j);
-                       if (INTEGERP (AREF (elt, 1))
-                           && XINT (AREF (elt, 1)) >= 0)
-                         {
-                           Lisp_Object font_object = AREF (elt, 3);
-
-                           if (FONT_OBJECT_P (font_object))
-                             {
-                               struct font *font
-                                 = XSAVE_VALUE (font_object)->pointer;
-                               char *name = font->font.full_name;;
-                               int len = strlen (name);
-                               Lisp_Object slot;
-
-                               slot = Fassq (AREF (AREF (elt, 2), 0), alist);
-                               nconc2 (slot,
-                                       Fcons (make_unibyte_string (name, len),
-                                              Qnil));
-                             }
-                         }
-                     }
-                 else
-#endif  /* not USE_FONT_BACKEND */
+                 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ])  */
+                 val = XCDR (val);
+                 for (j = 0; j < ASIZE (val); j++)
                    {
-                     /* VAL is [int int ?
-                                [FACE-ID FONT-INDEX FONT-DEF FONT-NAME] ...].
-                        If a font of an element is already opened,
-                        FONT-NAME is the name of a opened font.  */
-                     for (j = 3; j < ASIZE (val); j++)
-                       if (STRINGP (AREF (AREF (val, j), 3)))
-                         {
-                           Lisp_Object font_idx;
-
-                           font_idx = AREF (AREF (val, j), 1);
-                           elt = Fassq (AREF (AREF (AREF (val, j), 2), 0),
-                                        alist);
-                           if (CONSP (elt)
-                               && NILP (Fmemq (font_idx, XCDR(elt))))
-                             nconc2 (elt, Fcons (font_idx, Qnil));
-                         }
-                     for (val = alist; CONSP (val); val = XCDR (val))
-                       for (elt = XCDR (XCAR (val)); CONSP (elt);
-                            elt = XCDR (elt))
-                         {
-                           struct font_info *font_info
-                             = (*get_font_info_func) (f, XINT (XCAR (elt)));
-                           XSETCAR (elt, build_string (font_info->full_name));
-                         }
+                     elt = AREF (val, j);
+                     if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
+                       {
+                         Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
+                         Lisp_Object slot, name;
+
+                         slot = Fassq (RFONT_DEF_SPEC (elt), alist);
+                         name = AREF (font_object, FONT_NAME_INDEX);
+                         if (NILP (Fmember (name, XCDR (slot))))
+                           nconc2 (slot, Fcons (name, Qnil));
+                       }
                    }
                }
 
@@ -2268,9 +2062,18 @@ fontset.  The format is the same as abobe.  */)
                char_table_set_range (tables[k], c, to, alist);
              else
                XCHAR_TABLE (tables[k])->defalt = alist;
+
+             /* At last, change each elements to font names.  */
+             for (; CONSP (alist); alist = XCDR (alist))
+               {
+                 elt = XCAR (alist);
+                 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
+               }
            }
          c = to + 1;
        }
+      if (EQ (fontset, Vdefault_fontset))
+       break;
     }
 
   return tables[0];
@@ -2280,6 +2083,7 @@ fontset.  The format is the same as abobe.  */)
 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
        doc: /* Return a font name pattern for character CH in fontset NAME.
 If NAME is t, find a pattern in the default fontset.
+If NAME is nil, find a pattern in the fontset of the selected frame.
 
 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
 family name and REGISTRY is a font registry name.  This is actually
@@ -2294,8 +2098,10 @@ patterns.  */)
   int c;
   Lisp_Object fontset, elt, list, repertory, val;
   int i, j;
+  Lisp_Object frame;
 
-  fontset = check_fontset_name (name);
+  frame = Qnil;
+  fontset = check_fontset_name (name, &frame);
 
   CHECK_CHARACTER (ch);
   c = XINT (ch);
@@ -2307,7 +2113,11 @@ patterns.  */)
        if (VECTORP (elt))
          for (j = 0; j < ASIZE (elt); j++)
            {
+             Lisp_Object family, registry;
+
              val = AREF (elt, j);
+             if (NILP (val))
+               break;
              repertory = AREF (val, 1);
              if (INTEGERP (repertory))
                {
@@ -2322,7 +2132,14 @@ patterns.  */)
                    continue;
                }
              val = AREF (val, 0);
-             val = Fcons (AREF (val, 0), AREF (val, 5));
+             /* VAL is a FONT-SPEC */
+             family = AREF (val, FONT_FAMILY_INDEX);
+             if (! NILP (family))
+               family = SYMBOL_NAME (family);
+             registry = AREF (val, FONT_REGISTRY_INDEX);
+             if (! NILP (registry))
+               registry = SYMBOL_NAME (registry);
+             val = Fcons (family, registry);
              if (NILP (all))
                return val;
              list = Fcons (val, list);
@@ -2408,10 +2225,6 @@ DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
 void
 syms_of_fontset ()
 {
-  if (!load_font_func)
-    /* Window system initializer should have set proper functions.  */
-    abort ();
-
   DEFSYM (Qfontset, "fontset");
   Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
   DEFSYM (Qfontset_info, "fontset-info");
@@ -2432,38 +2245,16 @@ syms_of_fontset ()
   FONTSET_ID (Vdefault_fontset) = make_number (0);
   FONTSET_NAME (Vdefault_fontset)
     = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
-  AREF (Vfontset_table, 0) = Vdefault_fontset;
+  ASET (Vfontset_table, 0, Vdefault_fontset);
   next_fontset_id = 1;
 
   auto_fontset_alist = Qnil;
   staticpro (&auto_fontset_alist);
 
-  DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
-              doc: /*
-Alist of fontname patterns vs the corresponding encoding and repertory info.
-Each element looks like (REGEXP . (ENCODING . REPERTORY)),
-where ENCODING is a charset or a char-table,
-and REPERTORY is a charset, a char-table, or nil.
-
-If ENCDING and REPERTORY are the same, the element can have the form
-\(REGEXP . ENCODING).
-
-ENCODING is for converting a character to a glyph code of the font.
-If ENCODING is a charset, encoding a character by the charset gives
-the corresponding glyph code.  If ENCODING is a char-table, looking up
-the table by a character gives the corresponding glyph code.
-
-REPERTORY specifies a repertory of characters supported by the font.
-If REPERTORY is a charset, all characters beloging to the charset are
-supported.  If REPERTORY is a char-table, all characters who have a
-non-nil value in the table are supported.  It REPERTORY is nil, Emacs
-gets the repertory information by an opened font and ENCODING.  */);
-  Vfont_encoding_alist = Qnil;
-
   DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
               doc: /*
 Alist of charsets vs the charsets to determine the preferred font encoding.
-Each element looks like (CHARSET . ENCDOING-CHARSET),
+Each element looks like (CHARSET . ENCODING-CHARSET),
 where ENCODING-CHARSET is a charset registered in the variable
 `font-encoding-alist' as ENCODING.
 
@@ -2475,7 +2266,7 @@ whose encoding corresponds to ENCODING-CHARSET is preferred.  */);
               doc: /*
 Char table of characters whose ascent values should be ignored.
 If an entry for a character is non-nil, the ascent value of the glyph
-is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.
+is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
 
 This affects how a composite character which contains
 such a character is displayed on screen.  */);
@@ -2483,7 +2274,7 @@ such a character is displayed on screen.  */);
 
   DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
               doc: /*
-Char table of characters which is not composed relatively.
+Char table of characters which are not composed relatively.
 If an entry for a character is non-nil, a composition sequence
 which contains that character is displayed so that
 the glyph of that character is put without considering
@@ -2516,7 +2307,6 @@ at the vertical center of lines.  */);
   defsubr (&Squery_fontset);
   defsubr (&Snew_fontset);
   defsubr (&Sset_fontset_font);
-  defsubr (&Sfont_info);
   defsubr (&Sinternal_char_font);
   defsubr (&Sfontset_info);
   defsubr (&Sfontset_font);