]> code.delx.au - gnu-emacs/blobdiff - src/fontset.c
*** empty log message ***
[gnu-emacs] / src / fontset.c
index ed15c70e44dd8ee3d20ae287808831c54b4e9491..62ff3542b9067917a47ec72a18f71a43ba425af4 100644 (file)
@@ -1,6 +1,8 @@
 /* Fontset handler.
-   Copyright (C) 1995, 1997, 2000 Electrotechnical Laboratory, JAPAN.
-   Licensed to the Free Software Foundation.
+   Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1995, 1997, 1998, 2000, 2003, 2004, 2005
+     National Institute of Advanced Industrial Science and Technology (AIST)
+     Registration Number H14PRO021
 
 This file is part of GNU Emacs.
 
@@ -16,8 +18,8 @@ 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., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* #define FONTSET_DEBUG */
 
@@ -31,10 +33,20 @@ Boston, MA 02111-1307, USA.  */
 #include "buffer.h"
 #include "charset.h"
 #include "ccl.h"
+#include "keyboard.h"
 #include "frame.h"
 #include "dispextern.h"
 #include "fontset.h"
 #include "window.h"
+#ifdef HAVE_X_WINDOWS
+#include "xterm.h"
+#endif
+#ifdef WINDOWSNT
+#include "w32term.h"
+#endif
+#ifdef MAC_OS
+#include "macterm.h"
+#endif
 
 #ifdef FONTSET_DEBUG
 #undef xassert
@@ -54,14 +66,14 @@ Boston, MA 02111-1307, USA.  */
    face is also realized for multibyte characters based on an ASCII
    face.  All of the multibyte faces based on the same ASCII face
    share the same realized fontset.
-   
+
    A fontset object is implemented by a char-table.
 
    An element of a base fontset is:
        (INDEX . FONTNAME) or
        (INDEX . (FOUNDRY . REGISTRY ))
    FONTNAME is a font name pattern for the corresponding character.
-   FOUNDRY and REGISTRY are respectively foundy and regisry fields of
+   FOUNDRY and REGISTRY are respectively foundry and registry fields of
    a font name for the corresponding character.  INDEX specifies for
    which character (or generic character) the element is defined.  It
    may be different from an index to access this element.  For
@@ -70,14 +82,11 @@ Boston, MA 02111-1307, USA.  */
    charset.  REGISTRY is the
 
    An element of a realized fontset is FACE-ID which is a face to use
-   for displaying the correspnding character.
+   for displaying the corresponding character.
 
-   All single byte charaters (ASCII and 8bit-unibyte) share the same
-   element in a fontset.  The element is stored in `defalt' slot of
-   the fontset.  And this slot is never used as a default value of
-   multibyte characters.  That means that the first 256 elements of a
-   fontset are always nil (as this is not efficient, we may implement
-   a fontset in a different way in the future).
+   All single byte characters (ASCII and 8bit-unibyte) share the same
+   element in a fontset.  The element is stored in the first element
+   of the fontset.
 
    To access or set each element, use macros FONTSET_REF and
    FONTSET_SET respectively for efficiency.
@@ -95,26 +104,24 @@ Boston, MA 02111-1307, USA.  */
    A parent of a base fontset is nil.  A parent of a realized fontset
    is a base fontset.
 
-   All fontsets (except for the default fontset described below) are
-   recorded in Vfontset_table.
+   All fontsets are recorded in Vfontset_table.
 
 
    DEFAULT FONTSET
 
    There's a special fontset named `default fontset' which defines a
-   default fontname that contains only REGISTRY field for each
-   character.  When a base fontset doesn't specify a font for a
-   specific character, the corresponding value in the default fontset
-   is used.  The format is the same as a base fontset.
+   default fontname pattern.  When a base fontset doesn't specify a
+   font for a specific character, the corresponding value in the
+   default fontset is used.  The format is the same as a base fontset.
 
-   The parent of realized fontsets created for faces that have no
-   fontset is the default fontset.
+   The parent of a realized fontset created for such a face that has
+   no fontset is the default fontset.
 
 
    These structures are hidden from the other codes than this file.
    The other codes handle fontsets only by their ID numbers.  They
    usually use variable name `fontset' for IDs.  But, in this file, we
-   always use varialbe name `id' for IDs, and name `fontset' for the
+   always use variable name `id' for IDs, and name `fontset' for the
    actual fontset objects.
 
 */
@@ -127,7 +134,7 @@ Lisp_Object Qfontset;
 /* Vector containing all fontsets.  */
 static Lisp_Object Vfontset_table;
 
-/* Next possibly free fontset ID.  Usually this keeps the mininum
+/* Next possibly free fontset ID.  Usually this keeps the minimum
    fontset ID not yet used.  */
 static int next_fontset_id;
 
@@ -135,13 +142,15 @@ static int next_fontset_id;
    font for each characters.  */
 static Lisp_Object Vdefault_fontset;
 
+/* Alist of font specifications.  It override the font specification
+   in the default fontset.  */
+static Lisp_Object Voverriding_fontspec_alist;
+
 Lisp_Object Vfont_encoding_alist;
 Lisp_Object Vuse_default_ascent;
 Lisp_Object Vignore_relative_composition;
 Lisp_Object Valternate_fontname_alist;
 Lisp_Object Vfontset_alias_alist;
-Lisp_Object Vhighlight_wrong_size_font;
-Lisp_Object Vclip_large_size_font;
 Lisp_Object Vvertical_centering_font_regexp;
 
 /* The following six are declarations of callback functions depending
@@ -151,8 +160,8 @@ Lisp_Object Vvertical_centering_font_regexp;
 /* 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 document of
-   `x-list-fonts' for more detail.  */
+/* 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,
@@ -172,7 +181,7 @@ void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
 
 /* 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 memer `encoder' of the structure.  */
+   This function set the member `encoder' of the structure.  */
 void (*find_ccl_program_func) P_ ((struct font_info *));
 
 /* Check if any window system is used now.  */
@@ -181,27 +190,25 @@ void (*check_window_system_func) P_ ((void));
 
 /* Prototype declarations for static functions.  */
 static Lisp_Object fontset_ref P_ ((Lisp_Object, int));
+static Lisp_Object lookup_overriding_fontspec P_ ((Lisp_Object, int));
 static void fontset_set P_ ((Lisp_Object, int, Lisp_Object));
 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
 static int fontset_id_valid_p P_ ((int));
 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
-static Lisp_Object font_family_registry P_ ((Lisp_Object));
+static Lisp_Object font_family_registry P_ ((Lisp_Object, int));
+static Lisp_Object regularize_fontname P_ ((Lisp_Object));
 
 \f
 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
 
-/* Macros for Lisp vector.  */
-#define AREF(V, IDX)   XVECTOR (V)->contents[IDX]
-#define ASIZE(V)       XVECTOR (V)->size
-
 /* Return the fontset with ID.  No check of ID's validness.  */
 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
 
-/* Macros to access extra, default, and parent slots, of fontset.  */
+/* Macros to access special values of FONTSET.  */
 #define FONTSET_ID(fontset)            XCHAR_TABLE (fontset)->extras[0]
 #define FONTSET_NAME(fontset)          XCHAR_TABLE (fontset)->extras[1]
 #define FONTSET_FRAME(fontset)         XCHAR_TABLE (fontset)->extras[2]
-#define FONTSET_ASCII(fontset)         XCHAR_TABLE (fontset)->defalt
+#define FONTSET_ASCII(fontset)         XCHAR_TABLE (fontset)->contents[0]
 #define FONTSET_BASE(fontset)          XCHAR_TABLE (fontset)->parent
 
 #define BASE_FONTSET_P(fontset)                NILP (FONTSET_BASE(fontset))
@@ -211,19 +218,18 @@ static Lisp_Object font_family_registry P_ ((Lisp_Object));
 
 #define FONTSET_REF(fontset, c)        fontset_ref (fontset, c)
 
-static INLINE Lisp_Object
+static Lisp_Object
 fontset_ref (fontset, c)
      Lisp_Object fontset;
      int c;
 {
   int charset, c1, c2;
   Lisp_Object elt, defalt;
-  int i;
 
   if (SINGLE_BYTE_CHAR_P (c))
     return FONTSET_ASCII (fontset);
 
-  SPLIT_NON_ASCII_CHAR (c, charset, c1, c2);
+  SPLIT_CHAR (c, charset, c1, c2);
   elt = XCHAR_TABLE (fontset)->contents[charset + 128];
   if (!SUB_CHAR_TABLE_P (elt))
     return elt;
@@ -243,9 +249,49 @@ fontset_ref (fontset, c)
 }
 
 
+static Lisp_Object
+lookup_overriding_fontspec (frame, c)
+     Lisp_Object frame;
+     int c;
+{
+  Lisp_Object tail;
+
+  for (tail = Voverriding_fontspec_alist; CONSP (tail); tail = XCDR (tail))
+    {
+      Lisp_Object val, target, elt;
+
+      val = XCAR (tail);
+      target = XCAR (val);
+      val = XCDR (val);
+      /* Now VAL is (NO-FRAME-LIST OK-FRAME-LIST CHAR FONTNAME).  */
+      if (NILP (Fmemq (frame, XCAR (val)))
+         && (CHAR_TABLE_P (target)
+             ? ! NILP (CHAR_TABLE_REF (target, c))
+             : XINT (target) == CHAR_CHARSET (c)))
+       {
+         val = XCDR (val);
+         elt = XCDR (val);
+         if (NILP (Fmemq (frame, XCAR (val))))
+           {
+             if (! face_font_available_p (XFRAME (frame), XCDR (elt)))
+               {
+                 val = XCDR (XCAR (tail));
+                 XSETCAR (val, Fcons (frame, XCAR (val)));
+                 continue;
+               }
+             XSETCAR (val, Fcons (frame, XCAR (val)));
+           }
+         if (NILP (XCAR (elt)))
+           XSETCAR (elt, make_number (c));
+         return elt;
+       }
+    }
+  return Qnil;
+}
+
 #define FONTSET_REF_VIA_BASE(fontset, c) fontset_ref_via_base (fontset, &c)
 
-static INLINE Lisp_Object
+static Lisp_Object
 fontset_ref_via_base (fontset, c)
      Lisp_Object fontset;
      int *c;
@@ -256,12 +302,18 @@ fontset_ref_via_base (fontset, c)
   if (SINGLE_BYTE_CHAR_P (*c))
     return FONTSET_ASCII (fontset);
 
-  elt = FONTSET_REF (FONTSET_BASE (fontset), *c);
+  elt = Qnil;
+  if (! EQ (FONTSET_BASE (fontset), Vdefault_fontset))
+    elt = FONTSET_REF (FONTSET_BASE (fontset), *c);
+  if (NILP (elt))
+    elt = lookup_overriding_fontspec (FONTSET_FRAME (fontset), *c);
+  if (NILP (elt))
+    elt = FONTSET_REF (Vdefault_fontset, *c);
   if (NILP (elt))
     return Qnil;
 
   *c = XINT (XCAR (elt));
-  SPLIT_NON_ASCII_CHAR (*c, charset, c1, c2);
+  SPLIT_CHAR (*c, charset, c1, c2);
   elt = XCHAR_TABLE (fontset)->contents[charset + 128];
   if (c1 < 32)
     return (SUB_CHAR_TABLE_P (elt) ? XCHAR_TABLE (elt)->defalt : elt);
@@ -277,7 +329,7 @@ fontset_ref_via_base (fontset, c)
 }
 
 
-/* Store into the element of FONTSET at index C the value NEWETL.  */
+/* Store into the element of FONTSET at index C the value NEWELT.  */
 #define FONTSET_SET(fontset, c, newelt) fontset_set(fontset, c, newelt)
 
 static void
@@ -287,8 +339,8 @@ fontset_set (fontset, c, newelt)
      Lisp_Object newelt;
 {
   int charset, code[3];
-  Lisp_Object *elt, tmp;
-  int i, j;
+  Lisp_Object *elt;
+  int i;
 
   if (SINGLE_BYTE_CHAR_P (c))
     {
@@ -296,13 +348,17 @@ fontset_set (fontset, c, newelt)
       return;
     }
 
-  SPLIT_NON_ASCII_CHAR (c, charset, code[0], code[1]);
+  SPLIT_CHAR (c, charset, code[0], code[1]);
   code[2] = 0;                 /* anchor */
   elt = &XCHAR_TABLE (fontset)->contents[charset + 128];
   for (i = 0; code[i] > 0; i++)
     {
       if (!SUB_CHAR_TABLE_P (*elt))
-       *elt = make_sub_char_table (*elt);
+       {
+         Lisp_Object val = *elt;
+         *elt = make_sub_char_table (Qnil);
+         XCHAR_TABLE (*elt)->defalt = val;
+       }
       elt = &XCHAR_TABLE (*elt)->contents[code[i]];
     }
   if (SUB_CHAR_TABLE_P (*elt))
@@ -320,22 +376,21 @@ static Lisp_Object
 make_fontset (frame, name, base)
      Lisp_Object frame, name, base;
 {
-  Lisp_Object fontset, elt, base_elt;
+  Lisp_Object fontset;
   int size = ASIZE (Vfontset_table);
   int id = next_fontset_id;
-  int i, j;
 
   /* Find a free slot in Vfontset_table.  Usually, next_fontset_id is
      the next available fontset ID.  So it is expected that this loop
      terminates quickly.  In addition, as the last element of
-     Vfotnset_table is always nil, we don't have to check the range of
+     Vfontset_table is always nil, we don't have to check the range of
      id.  */
   while (!NILP (AREF (Vfontset_table, id))) id++;
 
   if (id + 1 == size)
     {
       Lisp_Object tem;
-      int i; 
+      int i;
 
       tem = Fmake_vector (make_number (size + 8), Qnil);
       for (i = 0; i < size; i++)
@@ -343,10 +398,7 @@ make_fontset (frame, name, base)
       Vfontset_table = tem;
     }
 
-  if (NILP (base))
-    fontset = Fcopy_sequence (Vdefault_fontset);
-  else
-    fontset = Fmake_char_table (Qfontset, Qnil);
+  fontset = Fmake_char_table (Qfontset, Qnil);
 
   FONTSET_ID (fontset) = make_number (id);
   FONTSET_NAME (fontset) = name;
@@ -369,21 +421,31 @@ fontset_id_valid_p (id)
 }
 
 
-/* Extract `family' and `registry' string from FONTNAME and set in
-   *FAMILY and *REGISTRY respectively.  Actually, `family' may also
-   contain `foundry', `registry' may also contain `encoding' of
-   FONTNAME.  */
+/* Extract `family' and `registry' string from FONTNAME and a cons of
+   them.  Actually, `family' may also contain `foundry', `registry'
+   may also contain `encoding' of FONTNAME.  But, if FONTNAME doesn't
+   conform to XLFD nor explicitely specifies the other fields
+   (i.e. not using wildcard `*'), return FONTNAME.  If FORCE is
+   nonzero, specifications of the other fields are ignored, and return
+   a cons as far as FONTNAME conform to XLFD.  */
 
 static Lisp_Object
-font_family_registry (fontname)
+font_family_registry (fontname, force)
      Lisp_Object fontname;
+     int force;
 {
   Lisp_Object family, registry;
-  char *p = XSTRING (fontname)->data;
-  char *sep[15];
+  const char *p = SDATA (fontname);
+  const char *sep[15];
   int i = 0;
-  
-  while (*p && i < 15) if (*p++ == '-') sep[i++] = p;
+
+  while (*p && i < 15)
+    if (*p++ == '-')
+      {
+       if (!force && i >= 2 && i <= 11 && *p != '*' && p[1] != '-')
+         return fontname;
+       sep[i++] = p;
+      }
   if (i != 14)
     return fontname;
 
@@ -393,7 +455,7 @@ font_family_registry (fontname)
 }
 
 \f
-/********** INTERFACES TO xfaces.c and dispextern.h **********/ 
+/********** INTERFACES TO xfaces.c and dispextern.h **********/
 
 /* Return name of the fontset with ID.  */
 
@@ -484,7 +546,7 @@ face_for_char (f, face, c)
   /* No face is recorded for C in the fontset of FACE.  Make a new
      realized face for C that has the same fontset.  */
   face_id = lookup_face (f, face->lface, c, face);
-  
+
   /* Record the face ID in FONTSET at the same index as the
      information in the base fontset.  */
   FONTSET_SET (fontset, c, make_number (face_id));
@@ -502,7 +564,7 @@ make_fontset_for_ascii_face (f, base_fontset_id)
      FRAME_PTR f;
      int base_fontset_id;
 {
-  Lisp_Object base_fontset, fontset, name, frame;
+  Lisp_Object base_fontset, fontset, frame;
 
   XSETFRAME (frame, f);
   if (base_fontset_id >= 0)
@@ -521,10 +583,10 @@ make_fontset_for_ascii_face (f, base_fontset_id)
 
 
 /* Return the font name pattern for C that is recorded in the fontset
-   with ID.  A font is opened by that pattern to get the fullname.  If
-   the fullname conform to XLFD, extract foundry-family field and
-   registry-encoding field, and return the cons of them.  Otherwise
-   return the fullname.  If ID is -1, or the fontset doesn't contain
+   with ID.  If a font name pattern is specified (instead of a cons of
+   family and registry), check if a font can be opened by that pattern
+   to get the fullname.  If a font is opened, return that name.
+   Otherwise, return nil.  If ID is -1, or the fontset doesn't contain
    information about C, get the registry and encoding of C from the
    default fontset.  Called from choose_face_font.  */
 
@@ -535,17 +597,24 @@ fontset_font_pattern (f, id, c)
 {
   Lisp_Object fontset, elt;
   struct font_info *fontp;
-  Lisp_Object family_registry;
-  
+
   elt = Qnil;
   if (fontset_id_valid_p (id))
     {
       fontset = FONTSET_FROM_ID (id);
       xassert (!BASE_FONTSET_P (fontset));
       fontset = FONTSET_BASE (fontset);
-      elt = FONTSET_REF (fontset, c);
+      if (! EQ (fontset, Vdefault_fontset))
+       elt = FONTSET_REF (fontset, c);
     }
-  else
+  if (NILP (elt))
+    {
+      Lisp_Object frame;
+
+      XSETFRAME (frame, f);
+      elt = lookup_overriding_fontspec (frame, c);
+    }
+  if (NILP (elt))
     elt = FONTSET_REF (Vdefault_fontset, c);
 
   if (!CONSP (elt))
@@ -554,24 +623,26 @@ fontset_font_pattern (f, id, c)
     return XCDR (elt);
 
   /* The fontset specifies only a font name pattern (not cons of
-     family and registry).  Try to open a font by that pattern and get
-     a registry from the full name of the opened font.  We ignore
-     family name here because it should be wild card in the fontset
-     specification.  */
+     family and registry).  If a font can be opened by that pattern,
+     return the name of opened font.  Otherwise return nil.  The
+     exception is a font for single byte characters.  In that case, we
+     return a cons of FAMILY and REGISTRY extracted from the opened
+     font name.  */
   elt = XCDR (elt);
   xassert (STRINGP (elt));
-  fontp = FS_LOAD_FONT (f, c, XSTRING (elt)->data, -1);
+  fontp = FS_LOAD_FONT (f, c, SDATA (elt), -1);
   if (!fontp)
     return Qnil;
 
-  family_registry = font_family_registry (build_string (fontp->full_name));
-  if (!CONSP (family_registry))
-    return family_registry;
-  XCAR (family_registry) = Qnil;
-  return family_registry;
+  return font_family_registry (build_string (fontp->full_name),
+                              SINGLE_BYTE_CHAR_P (c));
 }
 
 
+#if defined(WINDOWSNT) && defined (_MSC_VER)
+#pragma optimize("", off)
+#endif
+
 /* Load a font named FONTNAME to display character C on frame F.
    Return a pointer to the struct font_info of the loaded font.  If
    loading fails, return NULL.  If FACE is non-zero and a fontset is
@@ -588,8 +659,7 @@ fs_load_font (f, c, fontname, id, face)
      struct face *face;
 {
   Lisp_Object fontset;
-  Lisp_Object list, elt;
-  int font_idx;
+  Lisp_Object list, elt, fullname;
   int size = 0;
   struct font_info *fontp;
   int charset = CHAR_CHARSET (c);
@@ -619,7 +689,7 @@ fs_load_font (f, c, fontname, id, face)
       if (!fontname && charset == CHARSET_ASCII)
        {
          elt = FONTSET_ASCII (fontset);
-         fontname = XSTRING (XCDR (elt))->data;
+         fontname = SDATA (XCDR (elt));
        }
     }
 
@@ -635,10 +705,11 @@ fs_load_font (f, c, fontname, id, face)
      font_info structure that are not set by (*load_font_func).  */
   fontp->charset = charset;
 
+  fullname = build_string (fontp->full_name);
   fontp->vertical_centering
     = (STRINGP (Vvertical_centering_font_regexp)
-       && (fast_c_string_match_ignore_case 
-          (Vvertical_centering_font_regexp, fontp->full_name) >= 0));
+       && (fast_string_match_ignore_case
+          (Vvertical_centering_font_regexp, fullname) >= 0));
 
   if (fontp->encoding[1] != FONT_ENCODING_NOT_DECIDED)
     {
@@ -655,7 +726,6 @@ fs_load_font (f, c, fontname, id, face)
       /* The font itself doesn't have information about encoding.  */
       int i;
 
-      fontname = fontp->full_name;
       /* By default, encoding of ASCII chars is 0 (i.e. 0x00..0x7F),
         others is 1 (i.e. 0x80..0xFF).  */
       fontp->encoding[0] = 0;
@@ -667,8 +737,7 @@ fs_load_font (f, c, fontname, id, face)
          elt = XCAR (list);
          if (CONSP (elt)
              && STRINGP (XCAR (elt)) && CONSP (XCDR (elt))
-             && (fast_c_string_match_ignore_case (XCAR (elt), fontname)
-                 >= 0))
+             && (fast_string_match_ignore_case (XCAR (elt), fullname) >= 0))
            {
              Lisp_Object tmp;
 
@@ -684,9 +753,7 @@ fs_load_font (f, c, fontname, id, face)
        }
     }
 
-  fontp->font_encoder = (struct ccl_program *) 0;
-
-  if (find_ccl_program_func)
+  if (! fontp->font_encoder && find_ccl_program_func)
     (*find_ccl_program_func) (fontp);
 
   /* If we loaded a font for a face that has fontset, record the face
@@ -698,13 +765,34 @@ fs_load_font (f, c, fontname, id, face)
   return fontp;
 }
 
+#if defined(WINDOWSNT) && defined (_MSC_VER)
+#pragma optimize("", on)
+#endif
+
+/* 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 (! CONSP (FONTSET_ASCII (Vdefault_fontset)))
+    {
+      int id = fs_query_fontset (fontname, 2);
+
+      if (id >= 0)
+       fontname = XCDR (FONTSET_ASCII (FONTSET_FROM_ID (id)));
+      FONTSET_ASCII (Vdefault_fontset)
+       = Fcons (make_number (0), fontname);
+    }
+}
+
 \f
 /* 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 (XSTRING (XCAR (Vcached_fontset_data))->data)
+#define CACHED_FONTSET_NAME (SDATA (XCAR (Vcached_fontset_data)))
 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
 
 /* If fontset name PATTERN contains any wild card, return regular
@@ -714,25 +802,43 @@ static Lisp_Object
 fontset_pattern_regexp (pattern)
      Lisp_Object pattern;
 {
-  if (!index (XSTRING (pattern)->data, '*')
-      && !index (XSTRING (pattern)->data, '?'))
+  if (!index (SDATA (pattern), '*')
+      && !index (SDATA (pattern), '?'))
     /* PATTERN does not contain any wild cards.  */
     return Qnil;
 
   if (!CONSP (Vcached_fontset_data)
-      || strcmp (XSTRING (pattern)->data, CACHED_FONTSET_NAME))
+      || strcmp (SDATA (pattern), CACHED_FONTSET_NAME))
     {
       /* We must at first update the cached data.  */
-      char *regex = (char *) alloca (XSTRING (pattern)->size * 2);
-      char *p0, *p1 = regex;
+      unsigned char *regex, *p0, *p1;
+      int ndashes = 0, nstars = 0;
+
+      for (p0 = SDATA (pattern); *p0; p0++)
+       {
+         if (*p0 == '-')
+           ndashes++;
+         else if (*p0 == '*')
+           nstars++;
+       }
+
+      /* 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);
+      else
+       p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 1);
 
-      /* Convert "*" to ".*", "?" to ".".  */
       *p1++ = '^';
-      for (p0 = (char *) XSTRING (pattern)->data; *p0; p0++)
+      for (p0 = SDATA (pattern); *p0; p0++)
        {
          if (*p0 == '*')
            {
-             *p1++ = '.';
+             if (ndashes < 14)
+               *p1++ = '.';
+             else
+               *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
              *p1++ = '*';
            }
          else if (*p0 == '?')
@@ -743,7 +849,7 @@ fontset_pattern_regexp (pattern)
       *p1++ = '$';
       *p1++ = 0;
 
-      Vcached_fontset_data = Fcons (build_string (XSTRING (pattern)->data),
+      Vcached_fontset_data = Fcons (build_string (SDATA (pattern)),
                                    build_string (regex));
     }
 
@@ -751,47 +857,50 @@ fontset_pattern_regexp (pattern)
 }
 
 /* Return ID of the base fontset named NAME.  If there's no such
-   fontset, return -1.  */
+   fontset, return -1.  NAME_PATTERN specifies how to treat NAME as this:
+     0: pattern containing '*' and '?' as wildcards
+     1: regular expression
+     2: literal fontset name
+*/
 
 int
-fs_query_fontset (name, regexpp)
+fs_query_fontset (name, name_pattern)
      Lisp_Object name;
-     int regexpp;
+     int name_pattern;
 {
-  Lisp_Object fontset, tem;
+  Lisp_Object tem;
   int i;
 
   name = Fdowncase (name);
-  if (!regexpp)
+  if (name_pattern != 1)
     {
       tem = Frassoc (name, Vfontset_alias_alist);
       if (CONSP (tem) && STRINGP (XCAR (tem)))
        name = XCAR (tem);
-      else
+      else if (name_pattern == 0)
        {
          tem = fontset_pattern_regexp (name);
          if (STRINGP (tem))
            {
              name = tem;
-             regexpp = 1;
+             name_pattern = 1;
            }
        }
     }
 
   for (i = 0; i < ASIZE (Vfontset_table); i++)
     {
-      Lisp_Object fontset;
-      unsigned char *this_name;
+      Lisp_Object fontset, this_name;
 
       fontset = FONTSET_FROM_ID (i);
       if (NILP (fontset)
          || !BASE_FONTSET_P (fontset))
        continue;
 
-      this_name = XSTRING (FONTSET_NAME (fontset))->data;
-      if (regexpp
-         ? fast_c_string_match_ignore_case (name, this_name) >= 0
-         : !strcmp (XSTRING (name)->data, this_name))
+      this_name = FONTSET_NAME (fontset);
+      if (name_pattern == 1
+         ? fast_string_match (name, this_name) >= 0
+         : !strcmp (SDATA (name), SDATA (this_name)))
        return i;
     }
   return -1;
@@ -799,12 +908,12 @@ fs_query_fontset (name, regexpp)
 
 
 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
-  "Return the name of a fontset that matches PATTERN.\n\
-The value is nil if there is no matching fontset.\n\
-PATTERN can contain `*' or `?' as a wildcard\n\
-just as X font name matching algorithm allows.\n\
-If REGEXPP is non-nil, PATTERN is a regular expression.")
-  (pattern, regexpp)
+       doc: /* Return the name of a fontset that matches PATTERN.
+The value is nil if there is no matching fontset.
+PATTERN can contain `*' or `?' as a wildcard
+just as X font name matching algorithm allows.
+If REGEXPP is non-nil, PATTERN is a regular expression.  */)
+     (pattern, regexpp)
      Lisp_Object pattern, regexpp;
 {
   Lisp_Object fontset;
@@ -812,9 +921,9 @@ If REGEXPP is non-nil, PATTERN is a regular expression.")
 
   (*check_window_system_func) ();
 
-  CHECK_STRING (pattern, 0);
+  CHECK_STRING (pattern);
 
-  if (XSTRING (pattern)->size == 0)
+  if (SCHARS (pattern) == 0)
     return Qnil;
 
   id = fs_query_fontset (pattern, !NILP (regexpp));
@@ -827,7 +936,7 @@ If REGEXPP is non-nil, PATTERN is a regular expression.")
 
 /* Return a list of base fontset names matching PATTERN on frame F.
    If SIZE is not 0, it is the size (maximum bound width) of fontsets
-   to be listed. */
+   to be listed.  */
 
 Lisp_Object
 list_fontsets (f, pattern, size)
@@ -835,7 +944,7 @@ list_fontsets (f, pattern, size)
      Lisp_Object pattern;
      int size;
 {
-  Lisp_Object frame, regexp, val, tail;
+  Lisp_Object frame, regexp, val;
   int id;
 
   XSETFRAME (frame, f);
@@ -845,19 +954,18 @@ list_fontsets (f, pattern, size)
 
   for (id = 0; id < ASIZE (Vfontset_table); id++)
     {
-      Lisp_Object fontset;
-      unsigned char *name;
+      Lisp_Object fontset, name;
 
       fontset = FONTSET_FROM_ID (id);
       if (NILP (fontset)
          || !BASE_FONTSET_P (fontset)
          || !EQ (frame, FONTSET_FRAME (fontset)))
        continue;
-      name = XSTRING (FONTSET_NAME (fontset))->data;
+      name = FONTSET_NAME (fontset);
 
       if (!NILP (regexp)
-         ? (fast_c_string_match_ignore_case (regexp, name) < 0)
-         : strcmp (XSTRING (pattern)->data, name))
+         ? (fast_string_match (regexp, name) < 0)
+         : strcmp (SDATA (pattern), SDATA (name)))
        continue;
 
       if (size)
@@ -874,40 +982,48 @@ list_fontsets (f, pattern, size)
 }
 
 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
-  "Create a new fontset NAME that contains font information in FONTLIST.\n\
-FONTLIST is an alist of charsets vs corresponding font name patterns.")
-  (name, fontlist)
+       doc: /* Create a new fontset NAME that contains font information in FONTLIST.
+FONTLIST is an alist of charsets vs corresponding font name patterns.  */)
+     (name, fontlist)
      Lisp_Object name, fontlist;
 {
   Lisp_Object fontset, elements, ascii_font;
   Lisp_Object tem, tail, elt;
+  int id;
 
   (*check_window_system_func) ();
 
-  CHECK_STRING (name, 0);
-  CHECK_LIST (fontlist, 1);
+  CHECK_STRING (name);
+  CHECK_LIST (fontlist);
 
   name = Fdowncase (name);
-  tem = Fquery_fontset (name, Qnil);
-  if (!NILP (tem))
-    error ("Fontset `%s' matches the existing fontset `%s'",
-          XSTRING (name)->data, XSTRING (tem)->data);
+  id = fs_query_fontset (name, 2);
+  if (id >= 0)
+    {
+      fontset = FONTSET_FROM_ID (id);
+      tem = FONTSET_NAME (fontset);
+      error ("Fontset `%s' matches the existing fontset `%s'",
+            SDATA (name),  SDATA (tem));
+    }
 
   /* Check the validity of FONTLIST while creating a template for
      fontset elements.  */
   elements = ascii_font = Qnil;
   for (tail = fontlist; CONSP (tail); tail = XCDR (tail))
     {
-      Lisp_Object family, registry;
       int c, charset;
 
       tem = XCAR (tail);
       if (!CONSP (tem)
          || (charset = get_charset_id (XCAR (tem))) < 0
-         || !STRINGP (XCDR (tem)))
-       error ("Elements of fontlist must be a cons of charset and font name");
+         || (!STRINGP (XCDR (tem)) && !CONSP (XCDR (tem))))
+       error ("Elements of fontlist must be a cons of charset and font name pattern");
 
-      tem = Fdowncase (XCDR (tem));
+      tem = XCDR (tem);
+      if (STRINGP (tem))
+       tem = Fdowncase (tem);
+      else
+       tem = Fcons (Fdowncase (Fcar (tem)), Fdowncase (Fcdr (tem)));
       if (charset == CHARSET_ASCII)
        ascii_font = tem;
       else
@@ -925,7 +1041,10 @@ FONTLIST is an alist of charsets vs corresponding font name patterns.")
   for (; CONSP (elements); elements = XCDR (elements))
     {
       elt = XCAR (elements);
-      tem = Fcons (XCAR (elt), font_family_registry (XCDR (elt)));
+      tem = XCDR (elt);
+      if (STRINGP (tem))
+       tem = font_family_registry (tem, 0);
+      tem = Fcons (XCAR (elt), tem);
       FONTSET_SET (fontset, XINT (XCAR (elt)), tem);
     }
 
@@ -946,29 +1065,9 @@ clear_fontset_elements (fontset)
 }
 
 
-/* Return 1 iff REGISTRY is a valid string as the font registry and
-   encoding.  It is valid if it doesn't start with `-' and the number
-   of `-' in the string is at most 1.  */
-
-static int
-check_registry_encoding (registry)
-     Lisp_Object registry;
-{
-  unsigned char *str = XSTRING (registry)->data;
-  unsigned char *p = str;
-  int i;
-
-  if (!*p || *p++ == '-')
-    return 0;
-  for (i = 0; *p; p++)
-    if (*p == '-') i++;
-  return (i < 2);
-}
-
-
 /* 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 nil, return Vdefault_fontset.  */
 
 static Lisp_Object
 check_fontset_name (name)
@@ -976,27 +1075,60 @@ check_fontset_name (name)
 {
   int id;
 
-  if (EQ (name, Qt))
+  if (EQ (name, Qnil))
     return Vdefault_fontset;
 
-  CHECK_STRING (name, 0);
-  id = fs_query_fontset (name, 0);
+  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", XSTRING (name)->data);
+    error ("Fontset `%s' does not exist", SDATA (name));
   return FONTSET_FROM_ID (id);
 }
 
+/* Downcase FONTNAME or car and cdr of FONTNAME.  If FONTNAME is a
+   string, maybe change FONTNAME to (FAMILY . REGISTRY).  */
+
+static Lisp_Object
+regularize_fontname (Lisp_Object fontname)
+{
+  Lisp_Object family, registry;
+
+  if (STRINGP (fontname))
+    return font_family_registry (Fdowncase (fontname), 0);
+
+  CHECK_CONS (fontname);
+  family = XCAR (fontname);
+  registry = XCDR (fontname);
+  if (!NILP (family))
+    {
+      CHECK_STRING (family);
+      family = Fdowncase (family);
+    }
+  if (!NILP (registry))
+    {
+      CHECK_STRING (registry);
+      registry = Fdowncase (registry);
+    }
+  return Fcons (family, registry);
+}
+
 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 4, 0,
-  "Modify fontset NAME to use FONTNAME for CHARACTER.\n\
-\n\
-CHARACTER may be a cons; (FROM . TO), where FROM and TO are\n\
-non-generic characters.  In that case, use FONTNAME\n\
-for all characters in the range FROM and TO (inclusive).\n\
-\n\
-If NAME is t, an entry in the default fontset is modified.\n\
-In that case, FONTNAME should be a registry and encoding name\n\
-of a font for CHARACTER.")
-  (name, character, fontname, frame)
+       doc: /* Modify fontset NAME to use FONTNAME for CHARACTER.
+
+If NAME is nil, modify the default fontset.
+CHARACTER may be a cons; (FROM . TO), where FROM and TO are
+non-generic characters.  In that case, use FONTNAME
+for all characters in the range FROM and TO (inclusive).
+CHARACTER may be a charset.  In that case, use FONTNAME
+for all character in the charsets.
+
+FONTNAME may be a cons; (FAMILY . REGISTRY), where FAMILY is a family
+name of a font, REGISTRY is a registry name of a font.  */)
+     (name, character, fontname, frame)
      Lisp_Object name, character, fontname, frame;
 {
   Lisp_Object fontset, elt;
@@ -1010,19 +1142,27 @@ of a font for CHARACTER.")
     {
       /* CH should be (FROM . TO) where FROM and TO are non-generic
         characters.  */
-      CHECK_NUMBER (XCAR (character), 1);
-      CHECK_NUMBER (XCDR (character), 1);
+      CHECK_NUMBER_CAR (character);
+      CHECK_NUMBER_CDR (character);
       from = XINT (XCAR (character));
       to = XINT (XCDR (character));
       if (!char_valid_p (from, 0) || !char_valid_p (to, 0))
-       error ("Character range should be by non-generic characters.");
+       error ("Character range should be by non-generic characters");
       if (!NILP (name)
          && (SINGLE_BYTE_CHAR_P (from) || SINGLE_BYTE_CHAR_P (to)))
        error ("Can't change font for a single byte character");
     }
+  else if (SYMBOLP (character))
+    {
+      elt = Fget (character, Qcharset);
+      if (!VECTORP (elt) || ASIZE (elt) < 1 || !NATNUMP (AREF (elt, 0)))
+       error ("Invalid charset: %s", SDATA (SYMBOL_NAME (character)));
+      from = MAKE_CHAR (XINT (AREF (elt, 0)), 0, 0);
+      to = from;
+    }
   else
     {
-      CHECK_NUMBER (character, 1);
+      CHECK_NUMBER (character);
       from = XINT (character);
       to = from;
     }
@@ -1038,23 +1178,12 @@ of a font for CHARACTER.")
        error ("Can't change font for a single byte character");
     }
 
-  CHECK_STRING (fontname, 2);
-  fontname = Fdowncase (fontname);
-  if (EQ (fontset, Vdefault_fontset))
-    {
-      if (!check_registry_encoding (fontname))
-       error ("Invalid registry and encoding name: %s",
-              XSTRING (fontname)->data);
-      elt = Fcons (make_number (from), Fcons (Qnil, fontname));
-    }
-  else
-    elt = Fcons (make_number (from), font_family_registry (fontname));
-
   /* The arg FRAME is kept for backward compatibility.  We only check
      the validity.  */
   if (!NILP (frame))
-    CHECK_LIVE_FRAME (frame, 3);
+    CHECK_LIVE_FRAME (frame);
 
+  elt = Fcons (make_number (from), regularize_fontname (fontname));
   for (; from <= to; from++)
     FONTSET_SET (fontset, from, elt);
   Foptimize_char_table (fontset);
@@ -1080,20 +1209,20 @@ of a font for CHARACTER.")
 }
 
 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
-  "Return information about a font named NAME on frame FRAME.\n\
-If FRAME is omitted or nil, use the selected frame.\n\
-The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,\n\
-  HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,\n\
-where\n\
-  OPENED-NAME is the name used for opening the font,\n\
-  FULL-NAME is the full name of the font,\n\
-  SIZE is the maximum bound width of the font,\n\
-  HEIGHT is the height of the font,\n\
-  BASELINE-OFFSET is the upward offset pixels from ASCII baseline,\n\
-  RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling\n\
-    how to compose characters.\n\
-If the named font is not yet loaded, return nil.")
-  (name, frame)
+       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;
 {
   FRAME_PTR f;
@@ -1102,17 +1231,17 @@ If the named font is not yet loaded, return nil.")
 
   (*check_window_system_func) ();
 
-  CHECK_STRING (name, 0);
+  CHECK_STRING (name);
   name = Fdowncase (name);
   if (NILP (frame))
     frame = selected_frame;
-  CHECK_LIVE_FRAME (frame, 1);
+  CHECK_LIVE_FRAME (frame);
   f = XFRAME (frame);
 
   if (!query_font_func)
     error ("Font query function is not supported");
 
-  fontp = (*query_font_func) (f, XSTRING (name)->data);
+  fontp = (*query_font_func) (f, SDATA (name));
   if (!fontp)
     return Qnil;
 
@@ -1130,71 +1259,128 @@ If the named font is not yet loaded, return nil.")
 }
 
 
-/* Return the font name for the character at POSITION in the current
+/* Return a cons (FONT-NAME . GLYPH-CODE).
+   FONT-NAME is the font name for the character at POSITION in the current
    buffer.  This is computed from all the text properties and overlays
-   that apply to POSITION.  It returns nil in the following cases:
+   that apply to POSITION.  POSTION may be nil, in which case,
+   FONT-NAME is the font name for display the character CH with the
+   default face.
+
+   GLYPH-CODE is the glyph code in the font to use for the character.
+
+   If the 2nd optional arg CH is non-nil, it is a character to check
+   the font instead of the character at POSITION.
+
+   It returns nil in the following cases:
 
    (1) The window system doesn't have a font for the character (thus
    it is displayed by an empty box).
 
    (2) The character code is invalid.
 
-   (3) The current buffer is not displayed in any window.
+   (3) If POSITION is not nil, and the current buffer is not displayed
+   in any window.
 
    In addition, the returned font name may not take into account of
    such redisplay engine hooks as what used in jit-lock-mode if
    POSITION is currently not visible.  */
 
 
-DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 1, 0,
-  "For internal use only.")
-  (position)
-     Lisp_Object position;
+DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
+       doc: /* For internal use only.  */)
+     (position, ch)
+     Lisp_Object position, ch;
 {
   int pos, pos_byte, dummy;
   int face_id;
-  int c;
-  Lisp_Object window;
-  struct window *w;
+  int c, code;
   struct frame *f;
   struct face *face;
 
-  CHECK_NUMBER_COERCE_MARKER (position, 0);
-  pos = XINT (position);
-  if (pos < BEGV || pos >= ZV)
-    args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
-  pos_byte = CHAR_TO_BYTE (pos);
-  c = FETCH_CHAR (pos_byte);
+  if (NILP (position))
+    {
+      CHECK_NATNUM (ch);
+      c = XINT (ch);
+      f = XFRAME (selected_frame);
+      face_id = DEFAULT_FACE_ID;
+    }
+  else
+    {
+      Lisp_Object window;
+      struct window *w;
+
+      CHECK_NUMBER_COERCE_MARKER (position);
+      pos = XINT (position);
+      if (pos < BEGV || pos >= ZV)
+       args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
+      pos_byte = CHAR_TO_BYTE (pos);
+      if (NILP (ch))
+       c = FETCH_CHAR (pos_byte);
+      else
+       {
+         CHECK_NATNUM (ch);
+         c = XINT (ch);
+       }
+      window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
+      if (NILP (window))
+       return Qnil;
+      w = XWINDOW (window);
+      f = XFRAME (w->frame);
+      face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
+    }
   if (! CHAR_VALID_P (c, 0))
     return Qnil;
-  window = Fget_buffer_window (Fcurrent_buffer (), Qt);
-  if (NILP (window))
-    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_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c);
   face = FACE_FROM_ID (f, face_id);
-  return (face->font && face->font_name
-         ? build_string (face->font_name)
-         : Qnil);
+  if (! face->font || ! face->font_name)
+    return Qnil;
+
+  {
+    struct font_info *fontp = (*get_font_info_func) (f, face->font_info_id);
+    XChar2b char2b;
+    int c1, c2, charset;
+
+    SPLIT_CHAR (c, charset, c1, c2);
+    if (c2 > 0)
+      STORE_XCHAR2B (&char2b, c1, c2);
+    else
+      STORE_XCHAR2B (&char2b, 0, c1);
+    rif->encode_char (c, &char2b, fontp, NULL);
+    code = (XCHAR2B_BYTE1 (&char2b) << 8) | XCHAR2B_BYTE2 (&char2b);
+  }
+  return Fcons (build_string (face->font_name), make_number (code));
 }
 
 
+/* Called from Ffontset_info via map_char_table on each leaf of
+   fontset.  ARG is a copy of the default fontset.  The current leaf
+   is indexed by CHARACTER and has value ELT.  This function override
+   the copy by ELT if ELT is not nil.  */
+
+static void
+override_font_info (fontset, character, elt)
+     Lisp_Object fontset, character, elt;
+{
+  if (! NILP (elt))
+    Faset (fontset, character, elt);
+}
+
 /* Called from Ffontset_info via map_char_table on each leaf of
    fontset.  ARG is a list (LAST FONT-INFO ...), where LAST is `(last
    ARG)' and FONT-INFOs have this form:
        (CHAR FONT-SPEC) or ((FROM . TO) FONT-SPEC)
    The current leaf is indexed by CHARACTER and has value ELT.  This
    function add the information of the current leaf to ARG by
-   appending a new element or modifying the last element..  */
+   appending a new element or modifying the last element.  */
 
 static void
 accumulate_font_info (arg, character, elt)
      Lisp_Object arg, character, elt;
 {
-  Lisp_Object last, last_char, last_elt, tmp;
+  Lisp_Object last, last_char, last_elt;
 
+  if (!CONSP (elt) && !SINGLE_BYTE_CHAR_P (XINT (character)))
+    elt = FONTSET_REF (Vdefault_fontset, XINT (character));
   if (!CONSP (elt))
     return;
   last = XCAR (arg);
@@ -1209,40 +1395,42 @@ accumulate_font_info (arg, character, elt)
        {
          if (this_charset == CHAR_CHARSET (XINT (XCAR (last_char))))
            {
-             XCDR (last_char) = character;
+             XSETCDR (last_char, character);
              return;
            }
        }
-      else
+      else if (XINT (last_char) == XINT (character))
+       return;
+      else if (this_charset == CHAR_CHARSET (XINT (last_char)))
        {
-         if (this_charset == CHAR_CHARSET (XINT (last_char)))
-           {
-             XCAR (XCAR (last)) = Fcons (last_char, character);
-             return;
-           }
+         XSETCAR (XCAR (last), Fcons (last_char, character));
+         return;
        }
     }
-  XCDR (last) = Fcons (Fcons (character, Fcons (elt, Qnil)), Qnil);
-  XCAR (arg) = XCDR (last);
+  XSETCDR (last, Fcons (Fcons (character, Fcons (elt, Qnil)), Qnil));
+  XSETCAR (arg, XCDR (last));
 }
 
 
 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
-  "Return information about a fontset named NAME on frame FRAME.\n\
-The value is a list:\n\
-  \(FONTSET-NAME (CHARSET-OR-RANGE FONT-SPEC OPENED ...) ...),\n\
-where,\n\
-  FONTSET-NAME is a full name of the fontset.\n\
-  CHARSET-OR-RANGE is a charset, a character (may be a generic character)\n\ 
-    or a cons of two characters specifying the range of characters.\n\ 
-  FONT-SPEC is a fontname pattern string or a cons (FAMILY . REGISTRY),\n\
-    where FAMILY is a `FAMILY' field of a XLFD font name,\n\
-    REGISTRY is a `CHARSET_REGISTRY' field of a XLDF font name.\n\
-    FAMILY may contain a `FOUNDARY' field at the head.\n\
-    REGISTRY may contain a `CHARSET_ENCODING' field at the tail.\n\
-  OPENEDs are names of fonts actually opened.\n\
-If FRAME is omitted, it defaults to the currently selected frame.")
-  (name, frame)
+       doc: /* Return information about a fontset named NAME on frame FRAME.
+If NAME is nil, return information about the default fontset.
+The value is a vector:
+  [ SIZE HEIGHT ((CHARSET-OR-RANGE FONT-SPEC OPENED ...) ...) ],
+where,
+  SIZE is the maximum bound width of ASCII font in the fontset,
+  HEIGHT is the maximum bound height of ASCII font in the fontset,
+  CHARSET-OR-RANGE is a charset, a character (may be a generic character)
+    or a cons of two characters specifying the range of characters.
+  FONT-SPEC is a fontname pattern string or a cons (FAMILY . REGISTRY),
+    where FAMILY is a `FAMILY' field of a XLFD font name,
+    REGISTRY is a `CHARSET_REGISTRY' field of a XLFD font name.
+    FAMILY may contain a `FOUNDRY' field at the head.
+    REGISTRY may contain a `CHARSET_ENCODING' field at the tail.
+  OPENEDs are names of fonts actually opened.
+If the ASCII font is not yet opened, SIZE and HEIGHT are 0.
+If FRAME is omitted, it defaults to the currently selected frame.  */)
+     (name, frame)
      Lisp_Object name, frame;
 {
   Lisp_Object fontset;
@@ -1250,19 +1438,20 @@ If FRAME is omitted, it defaults to the currently selected frame.")
   Lisp_Object indices[3];
   Lisp_Object val, tail, elt;
   Lisp_Object *realized;
+  struct font_info *fontp = NULL;
   int n_realized = 0;
   int i;
-  
+
   (*check_window_system_func) ();
 
   fontset = check_fontset_name (name);
 
   if (NILP (frame))
     frame = selected_frame;
-  CHECK_LIVE_FRAME (frame, 1);
+  CHECK_LIVE_FRAME (frame);
   f = XFRAME (frame);
 
-  /* Recodeq realized fontsets whose base is FONTSET in the table
+  /* Recode realized fontsets whose base is FONTSET in the table
      `realized'.  */
   realized = (Lisp_Object *) alloca (sizeof (Lisp_Object)
                                     * ASIZE (Vfontset_table));
@@ -1274,6 +1463,14 @@ If FRAME is omitted, it defaults to the currently selected frame.")
        realized[n_realized++] = elt;
     }
 
+  if (! EQ (fontset, Vdefault_fontset))
+    {
+      /* Merge FONTSET onto the default fontset.  */
+      val = Fcopy_sequence (Vdefault_fontset);
+      map_char_table (override_font_info, Qnil, fontset, fontset, val, 0, indices);
+      fontset = val;
+    }
+
   /* Accumulate information of the fontset in VAL.  The format is
      (LAST FONT-INFO FONT-INFO ...), where FONT-INFO is (CHAR-OR-RANGE
      FONT-SPEC).  See the comment for accumulate_font_info for the
@@ -1282,11 +1479,11 @@ If FRAME is omitted, it defaults to the currently selected frame.")
                      Fcons (XCDR (FONTSET_ASCII (fontset)), Qnil)),
               Qnil);
   val = Fcons (val, val);
-  map_char_table (accumulate_font_info, Qnil, fontset, val, 0, indices);
+  map_char_table (accumulate_font_info, Qnil, fontset, fontset, val, 0, indices);
   val = XCDR (val);
 
   /* For each FONT-INFO, if CHAR_OR_RANGE (car part) is a generic
-     character for a charset, replace it wiht the charset symbol.  If
+     character for a charset, replace it with the charset symbol.  If
      fonts are opened for FONT-SPEC, append the names of the fonts to
      FONT-SPEC.  */
   for (tail = val; CONSP (tail); tail = XCDR (tail))
@@ -1299,7 +1496,7 @@ If FRAME is omitted, it defaults to the currently selected frame.")
          c = XINT (XCAR (elt));
          SPLIT_CHAR (c, charset, c1, c2);
          if (c1 == 0)
-           XCAR (elt) = CHARSET_SYMBOL (charset);
+           XSETCAR (elt, CHARSET_SYMBOL (charset));
        }
       else
        c = XINT (XCAR (XCAR (elt)));
@@ -1312,30 +1509,40 @@ If FRAME is omitted, it defaults to the currently selected frame.")
          if (INTEGERP (face_id))
            {
              face = FACE_FROM_ID (f, XINT (face_id));
-             if (face->font && face->font_name)
+             if (face && face->font && face->font_name)
                {
                  font = build_string (face->font_name);
                  if (NILP (Fmember (font, XCDR (XCDR (elt)))))
-                   XCDR (XCDR (elt)) = Fcons (font, XCDR (XCDR (elt)));
+                   XSETCDR (XCDR (elt), Fcons (font, XCDR (XCDR (elt))));
                }
            }
        }
     }
-  return Fcons (FONTSET_NAME (fontset), val);
+
+  elt = Fcdr (Fcdr (Fassq (CHARSET_SYMBOL (CHARSET_ASCII), val)));
+  if (CONSP (elt))
+    {
+      elt = XCAR (elt);
+      fontp = (*query_font_func) (f, SDATA (elt));
+    }
+  val = Fmake_vector (make_number (3), val);
+  AREF (val, 0) = fontp ? make_number (fontp->size) : make_number (0);
+  AREF (val, 1) = fontp ? make_number (fontp->height) : make_number (0);
+  return val;
 }
 
 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 2, 0,
-  "Return a font name pattern for character CH in fontset NAME.\n\
-If NAME is t, find a font name pattern in the default fontset.")
-  (name, ch)
+       doc: /* Return a font name pattern for character CH in fontset NAME.
+If NAME is nil, find a font name pattern in the default fontset.  */)
+     (name, ch)
      Lisp_Object name, ch;
 {
-  int c, id;
+  int c;
   Lisp_Object fontset, elt;
 
   fontset = check_fontset_name (name);
 
-  CHECK_NUMBER (ch, 1);
+  CHECK_NUMBER (ch);
   c = XINT (ch);
   if (!char_valid_p (c, 1))
     invalid_character (c);
@@ -1346,11 +1553,10 @@ If NAME is t, find a font name pattern in the default fontset.")
 
   return elt;
 }
-  
 
 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
-  "Return a list of all defined fontset names.")
-  ()
+       doc: /* Return a list of all defined fontset names.  */)
+     ()
 {
   Lisp_Object fontset, list;
   int i;
@@ -1367,11 +1573,63 @@ DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
   return list;
 }
 
+DEFUN ("set-overriding-fontspec-internal", Fset_overriding_fontspec_internal,
+       Sset_overriding_fontspec_internal, 1, 1, 0,
+       doc: /* Internal use only.
+
+FONTLIST is an alist of TARGET vs FONTNAME, where TARGET is a charset
+or a char-table, FONTNAME have the same meanings as in
+`set-fontset-font'.
+
+It overrides the font specifications for each TARGET in the default
+fontset by the corresponding FONTNAME.
+
+If TARGET is a charset, targets are all characters in the charset.  If
+TARGET is a char-table, targets are characters whose value is non-nil
+in the table.
+
+It is intended that this function is called only from
+`set-language-environment'.  */)
+     (fontlist)
+     Lisp_Object fontlist;
+{
+  Lisp_Object tail;
+
+  fontlist = Fcopy_sequence (fontlist);
+  /* Now FONTLIST is ((TARGET . FONTNAME) ...).  Reform it to ((TARGET
+     nil nil nil FONTSPEC) ...), where TARGET is a charset-id or a
+     char-table.  */
+  for (tail = fontlist; CONSP (tail); tail = XCDR (tail))
+    {
+      Lisp_Object elt, target;
+
+      elt = XCAR (tail);
+      target = Fcar (elt);
+      elt = Fcons (Qnil, regularize_fontname (Fcdr (elt)));
+      if (! CHAR_TABLE_P (target))
+       {
+         int charset, c;
+
+         CHECK_SYMBOL (target);
+         charset = get_charset_id (target);
+         if (charset < 0)
+           error ("Invalid charset %s", SDATA (SYMBOL_NAME (target)));
+         target = make_number (charset);
+         c = MAKE_CHAR (charset, 0, 0);
+         XSETCAR (elt, make_number (c));
+       }
+      elt = Fcons (target, Fcons (Qnil, Fcons (Qnil, elt)));
+      XSETCAR (tail, elt);
+    }
+  Voverriding_fontspec_alist = fontlist;
+  clear_face_cache (0);
+  ++windows_or_buffers_changed;
+  return Qnil;
+}
+
 void
 syms_of_fontset ()
 {
-  int i;
-
   if (!load_font_func)
     /* Window system initializer should have set proper functions.  */
     abort ();
@@ -1391,74 +1649,67 @@ syms_of_fontset ()
   FONTSET_ID (Vdefault_fontset) = make_number (0);
   FONTSET_NAME (Vdefault_fontset)
     = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
-  FONTSET_ASCII (Vdefault_fontset)
-    = Fcons (make_number (0), Fcons (Qnil, build_string ("iso8859-1")));
   AREF (Vfontset_table, 0) = Vdefault_fontset;
   next_fontset_id = 1;
 
+  Voverriding_fontspec_alist = Qnil;
+  staticpro (&Voverriding_fontspec_alist);
+
   DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
-    "Alist of fontname patterns vs corresponding encoding info.\n\
-Each element looks like (REGEXP . ENCODING-INFO),\n\
- where ENCODING-INFO is an alist of CHARSET vs ENCODING.\n\
-ENCODING is one of the following integer values:\n\
-       0: code points 0x20..0x7F or 0x2020..0x7F7F are used,\n\
-       1: code points 0xA0..0xFF or 0xA0A0..0xFFFF are used,\n\
-       2: code points 0x20A0..0x7FFF are used,\n\
-       3: code points 0xA020..0xFF7F are used.");
+              doc: /* Alist of fontname patterns vs corresponding encoding info.
+Each element looks like (REGEXP . ENCODING-INFO),
+ where ENCODING-INFO is an alist of CHARSET vs ENCODING.
+ENCODING is one of the following integer values:
+       0: code points 0x20..0x7F or 0x2020..0x7F7F are used,
+       1: code points 0xA0..0xFF or 0xA0A0..0xFFFF are used,
+       2: code points 0x20A0..0x7FFF are used,
+       3: code points 0xA020..0xFF7F are used.  */);
   Vfont_encoding_alist = Qnil;
+  Vfont_encoding_alist
+    = Fcons (Fcons (build_string ("JISX0201"),
+                   Fcons (Fcons (intern ("latin-jisx0201"), make_number (0)),
+                          Qnil)),
+            Vfont_encoding_alist);
+  Vfont_encoding_alist
+    = Fcons (Fcons (build_string ("ISO8859-1"),
+                   Fcons (Fcons (intern ("ascii"), make_number (0)),
+                          Qnil)),
+            Vfont_encoding_alist);
 
   DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
-     "Char table of characters whose ascent values should be ignored.\n\
-If an entry for a character is non-nil, the ascent value of the glyph\n\
-is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.\n\
-\n\
-This affects how a composite character which contains\n\
-such a character is displayed on screen.");
+              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.
+
+This affects how a composite character which contains
+such a character is displayed on screen.  */);
   Vuse_default_ascent = Qnil;
 
   DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
-     "Char table of characters which is not composed relatively.\n\
-If an entry for a character is non-nil, a composition sequence\n\
-which contains that character is displayed so that\n\
-the glyph of that character is put without considering\n\
-an ascent and descent value of a previous character.");
+              doc: /* Char table of characters which is 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
+an ascent and descent value of a previous character.  */);
   Vignore_relative_composition = Qnil;
 
   DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
-     "Alist of fontname vs list of the alternate fontnames.\n\
-When a specified font name is not found, the corresponding\n\
-alternate fontnames (if any) are tried instead.");
+              doc: /* Alist of fontname vs list of the alternate fontnames.
+When a specified font name is not found, the corresponding
+alternate fontnames (if any) are tried instead.  */);
   Valternate_fontname_alist = Qnil;
 
   DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
-     "Alist of fontset names vs the aliases.");
+              doc: /* Alist of fontset names vs the aliases.  */);
   Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
                                       build_string ("fontset-default")),
                                Qnil);
 
-  DEFVAR_LISP ("highlight-wrong-size-font", &Vhighlight_wrong_size_font,
-     "*Non-nil means highlight characters shown in wrong size fonts somehow.\n\
-The way to highlight them depends on window system on which Emacs runs.\n\
-On X11, a rectangle is shown around each such character.");
-  Vhighlight_wrong_size_font = Qnil;
-
-  DEFVAR_LISP ("clip-large-size-font", &Vclip_large_size_font,
-     "*Non-nil means characters shown in overlarge fonts are clipped.\n\
-The height of clipping area is the same as that of an ASCII character.\n\
-The width of the area is the same as that of an ASCII character,\n\
-or twice as wide, depending on the character set's column-width.\n\
-\n\
-If the only font you have for a specific character set is too large,\n\
-and clipping these characters makes them hard to read,\n\
-you can set this variable to nil to display the characters without clipping.\n\
-The drawback is that you will get some garbage left on your screen.");
-  Vclip_large_size_font = Qt;
-
   DEFVAR_LISP ("vertical-centering-font-regexp",
               &Vvertical_centering_font_regexp,
-    "*Regexp matching font names that require vertical centering on display.\n\
-When a character is displayed with such fonts, the character is displayed\n\
-at the vertival center of lines.");
+              doc: /* *Regexp matching font names that require vertical centering on display.
+When a character is displayed with such fonts, the character is displayed
+at the vertical center of lines.  */);
   Vvertical_centering_font_regexp = Qnil;
 
   defsubr (&Squery_fontset);
@@ -1469,4 +1720,8 @@ at the vertival center of lines.");
   defsubr (&Sfontset_info);
   defsubr (&Sfontset_font);
   defsubr (&Sfontset_list);
+  defsubr (&Sset_overriding_fontspec_internal);
 }
+
+/* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
+   (do not change this comment) */