Include coding.h and termhooks.h.
[bpt/emacs.git] / src / fontset.c
index 264be22..701ab66 100644 (file)
@@ -1,8 +1,8 @@
 /* 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
@@ -27,10 +27,7 @@ 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"
@@ -50,8 +47,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef WINDOWSNT
 #include "w32term.h"
 #endif
-#ifdef MAC_OS
-#include "macterm.h"
+#ifdef HAVE_NS
+#include "nsterm.h"
 #endif
 #include "termhooks.h"
 
@@ -92,7 +89,8 @@ 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 ].
 
-   An element of a realized fontset is nil, t, or a vector of this form:
+   An element of a realized fontset is nil, t, 0, or a vector of this
+   form:
 
        [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
          RFONT-DEF0 RFONT-DEF1 ... ]
@@ -110,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.
 
@@ -208,7 +210,7 @@ static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
                                    Lisp_Object));
 static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
                                          int, int));
-static void reorder_font_vector P_ ((Lisp_Object, Lisp_Object));
+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));
@@ -355,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));
@@ -377,14 +378,14 @@ static int
 fontset_compare_rfontdef (val1, val2)
      const void *val1, *val2;
 {
-  return (RFONT_DEF_SCORE (*(Lisp_Object *) val2)
-         - RFONT_DEF_SCORE (*(Lisp_Object *) val1));
+  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-RFONT-DEF
          RFONT-DEF0 RFONT-DEF1 ... ]
-   Reorder RFONT-DEFs according to the current langauge, and update
+   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
@@ -393,54 +394,57 @@ fontset_compare_rfontdef (val1, val2)
 extern Lisp_Object Fassoc_string ();
 
 static void
-reorder_font_vector (font_group, preferred_family)
+reorder_font_vector (font_group, font)
      Lisp_Object font_group;
-     Lisp_Object preferred_family;
+     struct font *font;
 {
+  Lisp_Object vec, font_object;
   int size;
   int i;
   int score_changed = 0;
 
-  size = ASIZE (font_group);
-  /* Exclude the tailing nil elements from the reordering.  */
-  while (NILP (AREF (font_group, size - 1))) size--;
-  size -= 2;
+  if (font)
+    XSETFONT (font_object, font);
+  else
+    font_object = Qnil;
+
+  vec = XCDR (font_group);
+  size = ASIZE (vec);
+  /* Exclude the tailing nil element from the reordering.  */
+  if (NILP (AREF (vec, size - 1)))
+    size--;
 
   for (i = 0; i < size; i++)
     {
-      Lisp_Object rfont_def = AREF (font_group, i + 2);
+      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);
-      Lisp_Object lang = Ffont_get (font_spec, QClang);
-      Lisp_Object family = AREF (font_spec, FONT_FAMILY_INDEX);
-      Lisp_Object repertory = FONT_DEF_REPERTORY (font_def);
-      int score = 0;
+      int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
 
-      if (! NILP (repertory))
+      if (! font_match_p (font_spec, font_object))
        {
-         Lisp_Object tail;
+         Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
 
-         for (score = 0xFFFF, tail = Vcharset_ordered_list;
-              ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
-              score--, tail = XCDR (tail))
-           if (EQ (repertory, XCAR (tail)))
-             break;
-         if (EQ (tail, Vcharset_non_preferred_head))
-           score = 0;
-       }
-      else if (! NILP (lang))
-       {
-         if (EQ (lang, Vcurrent_iso639_language))
-           score = 0xFFFF;
-         else if (CONSP (Vcurrent_iso639_language))
-           score = ! NILP (Fmemq (lang, Vcurrent_iso639_language));
-       }
+         if (! NILP (encoding))
+           {
+             Lisp_Object tail;
 
-      if (! NILP (preferred_family) && ! NILP (family))
-       {
-         if (fast_string_match_ignore_case (preferred_family,
-                                            SYMBOL_NAME (family)) < 0)
-           score |= 0x10000;
+             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);
+
+             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)
        {
@@ -450,10 +454,53 @@ reorder_font_vector (font_group, preferred_family)
     }
 
   if (score_changed)
-    qsort (XVECTOR (font_group)->contents + 2, size, sizeof (Lisp_Object),
+    qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
           fontset_compare_rfontdef);
+  XSETCAR (font_group, make_number (charset_ordered_list_tick));
+}
 
-  ASET (font_group, 0, make_number (charset_ordered_list_tick));
+static Lisp_Object
+fontset_get_font_group (Lisp_Object fontset, int c)
+{
+  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_group = FONTSET_FALLBACK (base_fontset);
+  if (NILP (font_group))
+    {
+      if (c >= 0)
+       char_table_set_range (fontset, from, to, make_number (0));
+      return Qnil;
+    }
+  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;
 }
 
 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
@@ -476,117 +523,61 @@ fontset_find_font (fontset, c, face, id, fallback)
      struct face *face;
      int id, fallback;
 {
-  Lisp_Object base_fontset, elt, vec;
-  int i, from, to;
-  FRAME_PTR f = XFRAME (FONTSET_FRAME (fontset));
-
-  base_fontset = FONTSET_BASE (fontset);
-  if (! fallback)
-    vec = CHAR_TABLE_REF (fontset, c);
-  else
-    vec = FONTSET_FALLBACK (fontset);
+  Lisp_Object elt, vec, font_group;
+  int i, charset_matched = -1;
+  FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset)))
+    ? XFRAME (selected_frame) : XFRAME (FONTSET_FRAME (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;
-
-      /* 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 font-group vector [ -1 nil RFONT-DEF0 RFONT-DEF1 ... ],
-            where the first -1 is to force reordering of RFONT-DEFs.  */
-         int size = ASIZE (elt);
-         int j;
-
-         vec = Fmake_vector (make_number (size + 2), Qnil);
-         ASET (vec, 0, make_number (-1));
-         for (i = j = 0; i < size; i++)
-           if (! NILP (AREF (elt, i)))
+      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 rfont_def = AREF (vec, i);
+           Lisp_Object repertory
+             = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
+
+           if (XINT (repertory) == id)
              {
-               Lisp_Object rfont_def;
-
-               RFONT_DEF_NEW (rfont_def, AREF (elt, i));
-               ASET (vec, j + 2, rfont_def);
-               j++;
-             }
-       }
-      /* 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          /* multiple RFONT-DEFs */
-      && XINT (AREF (vec, 0)) != charset_ordered_list_tick)
-    /* We have just created VEC,
-       or the charset priorities were changed.  */
-    reorder_font_vector (vec, face->lface[LFACE_FAMILY_INDEX]);
-  i = 2;
-  if (id >= 0)
-    {
-      elt = AREF (vec, 1);
-      if (! NILP (elt)
-         && EQ (make_number (id), RFONT_DEF_REPERTORY (elt)))
-       i = 1;
-      else
-       {
-         for (; i < ASIZE (vec); i++)
-           {
-             elt = AREF (vec, i);
-             if (! NILP (elt)
-                 && EQ (make_number (id), RFONT_DEF_REPERTORY (elt)))
+               charset_matched = i;
                break;
-           }
-         if (i < ASIZE (vec))
-           {
-             ASET (vec, 1, elt);
-             i = 1;
-           }
-         else
-           {
-             ASET (vec, 1, Qnil);
-             i = 2;
-           }
-       }
+             }
+         }
     }
 
   /* Find the first available font in the vector of RFONT-DEF.  */
-  for (; i < ASIZE (vec); i++)
+  for (i = 0; i < ASIZE (vec); i++)
     {
       Lisp_Object font_entity, font_object;
 
-      elt = AREF (vec, i);
+      if (i == 0 && charset_matched >= 0)
+       {
+         /* Try the element matching with the charset ID at first.  */
+         elt = AREF (vec, charset_matched);
+         charset_matched = -1;
+         i--;
+       }
+      else if (i != charset_matched)
+       elt = AREF (vec, i);
+      else
+       continue;
+
       if (NILP (elt))
-       /* This is the sign of not to try fallback fonts.  */
+       /* This is a sign of not to try the other fonts.  */
        return Qt;
-      if (id >= 0 && i > 1 && EQ (AREF (vec, 1), elt))
-       /* This is already checked.  */
-       continue;
       if (INTEGERP (RFONT_DEF_FACE (elt))
          && XINT (AREF (elt, 1)) < 0)
        /* We couldn't open this font last time.  */
@@ -608,7 +599,8 @@ fontset_find_font (fontset, c, face, id, fallback)
              RFONT_DEF_SET_FACE (elt, -1);
              continue;
            }
-         font_object = font_open_for_lface (f, font_entity, face->lface, Qnil);
+         font_object = font_open_for_lface (f, font_entity, face->lface,
+                                            FONT_DEF_SPEC (font_def));
          if (NILP (font_object))
            {
              /* Record that the font is unsable.  */
@@ -677,37 +669,46 @@ fontset_font (fontset, c, face, id)
   Lisp_Object rfont_def;
   Lisp_Object base_fontset;
 
-  /* Try a font-group for C. */
+  /* Try a font-group of FONTSET. */
   rfont_def = fontset_find_font (fontset, c, face, id, 0);
   if (VECTORP (rfont_def))
     return rfont_def;
   if (EQ (rfont_def, Qt))
     return Qnil;
-  /* Try a fallback font-group. */
+
+  /* Try a font-group of the default fontset. */
+  base_fontset = FONTSET_BASE (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 (EQ (rfont_def, Qt))
+       return Qnil;
+    }
+
+  /* Try a fallback font-group of FONTSET. */
   rfont_def = fontset_find_font (fontset, c, face, id, 1);
   if (VECTORP (rfont_def))
     return rfont_def;
   if (EQ (rfont_def, Qt))
     return Qnil;
 
-  base_fontset = FONTSET_BASE (fontset);
-  if (EQ (base_fontset, Vdefault_fontset))
-    return Qnil;
-
-  /* Try a font-group for C of the default 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;
   /* Try a fallback font-group of the default fontset . */
-  rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
-  if (! VECTORP (rfont_def))
-    /* Remeber that we have no font for C.  */
-    FONTSET_SET (fontset, make_number (c), Qt);
+  if (! EQ (base_fontset, Vdefault_fontset))
+    {
+      rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
+      if (VECTORP (rfont_def))
+       return rfont_def;
+    }
+
+  /* Remember that we have no font for C.  */
+  FONTSET_SET (fontset, make_number (c), Qt);
 
-  return rfont_def;
+  return Qnil;
 }
 
 /* Return a newly created fontset with NAME.  If BASE is nil, make a
@@ -879,35 +880,43 @@ face_for_char (f, face, c, pos, object)
      int c, pos;
      Lisp_Object object;
 {
-  Lisp_Object fontset, 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
     {
-      Lisp_Object charset;
-
       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;
     }
+
+  font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
   rfont_def = fontset_font (fontset, c, face, id);
   if (VECTORP (rfont_def))
     {
@@ -937,6 +946,56 @@ face_for_char (f, face, c, pos, object)
 }
 
 
+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 face_id;
+  int id;
+
+  if (ASCII_CHAR_P (c))
+    {
+      Lisp_Object font_object;
+
+      XSETFONT (font_object, face->ascii_face->font);
+      return font_object;
+    }
+
+  xassert (fontset_id_valid_p (face->fontset));
+  fontset = FONTSET_FROM_ID (face->fontset);
+  xassert (!BASE_FONTSET_P (fontset));
+  if (pos < 0)
+    {
+      id = -1;
+      charset = Qnil;
+    }
+  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;
+    }
+
+  font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
+  rfont_def = fontset_font (fontset, c, face, id);
+  return (VECTORP (rfont_def)
+         ? RFONT_DEF_OBJECT (rfont_def)
+         : Qnil);
+}
+
+
 /* Make a realized fontset for ASCII face FACE on frame F from the
    base fontset BASE_FONTSET_ID.  If BASE_FONTSET_ID is -1, use the
    default fontset as the base.  Value is the id of the new fontset.
@@ -993,7 +1052,7 @@ fontset_pattern_regexp (pattern)
     {
       /* 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++)
        {
@@ -1001,15 +1060,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++)
@@ -1024,6 +1088,11 @@ fontset_pattern_regexp (pattern)
            }
          else if (*p0 == '?')
            *p1++ = '.';
+         else if (*p0 == '['
+                  || *p0 == '.' || *p0 == '\\'
+                  || *p0 == '+' || *p0 == '^' 
+                  || *p0 == '$')
+           *p1++ = '\\', *p1++ = *p0;
          else
            *p1++ = *p0;
        }
@@ -1611,72 +1680,25 @@ fontset_from_font (font_object)
   Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
   auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
   FONTSET_ASCII (fontset) = font_name;
-  ASET (font_spec, FONT_FOUNDRY_INDEX, Qnil);
-  ASET (font_spec, FONT_ADSTYLE_INDEX, Qnil);
+  font_spec = Fcopy_font_spec (font_spec);
+  ASET (font_spec, FONT_REGISTRY_INDEX, Qiso10646_1);
   for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
     ASET (font_spec, i, Qnil);
   Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qnil);
-  font_spec = Fcopy_font_spec (font_spec);
-  ASET (font_spec, FONT_REGISTRY_INDEX, Qiso10646_1);
   Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
-  return XINT (FONTSET_ID (fontset));
-}
-
-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;
-{
-  FRAME_PTR f;
-  struct font *font;
-  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);
+#ifdef HAVE_NS
+  nsfont_make_fontset_for_font(name, font_object);
+#endif
 
-  font_object = font_open_by_name (f, (char *) SDATA (name));
-  if (NILP (font_object))
-    return Qnil;
-  font = XFONT_OBJECT (font_object);
-
-  info = Fmake_vector (make_number (7), Qnil);
-  XVECTOR (info)->contents[0] = AREF (font_object, FONT_NAME_INDEX);
-  XVECTOR (info)->contents[1] = AREF (font_object, FONT_NAME_INDEX);
-  XVECTOR (info)->contents[2] = make_number (font->pixel_size);
-  XVECTOR (info)->contents[3] = make_number (font->height);
-  XVECTOR (info)->contents[4] = make_number (font->baseline_offset);
-  XVECTOR (info)->contents[5] = make_number (font->relative_compose);
-  XVECTOR (info)->contents[6] = make_number (font->default_ascent);
-
-  font_close_object (f, font_object);
-  return info;
+  return XINT (FONTSET_ID (fontset));
 }
 
-
-/* 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.
@@ -1716,7 +1738,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;
     }
@@ -1755,18 +1777,18 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
   face = FACE_FROM_ID (f, face_id);
   if (face->font)
     {
-      struct font *font = face->font;
-      unsigned code = font->driver->encode_char (font, c);
-      Lisp_Object fontname = font->props[FONT_NAME_INDEX];
+      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;
+      XSETFONT (font_object, face->font);
       if (cod <= MOST_POSITIVE_FIXNUM)
-       return Fcons (fontname, make_number (code));
-      return Fcons (fontname, Fcons (make_number (code >> 16),
+       return Fcons (font_object, make_number (code));
+      return Fcons (font_object, Fcons (make_number (code >> 16),
                                     make_number (code & 0xFFFF)));
     }
   return Qnil;
@@ -1843,13 +1865,11 @@ fontset.  The format is the same as above.  */)
     {
       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
            {
@@ -1867,7 +1887,7 @@ fontset.  The format is the same as above.  */)
                                 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)
@@ -1944,6 +1964,8 @@ patterns.  */)
        if (VECTORP (elt))
          for (j = 0; j < ASIZE (elt); j++)
            {
+             Lisp_Object family, registry;
+
              val = AREF (elt, j);
              repertory = AREF (val, 1);
              if (INTEGERP (repertory))
@@ -1959,7 +1981,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);
@@ -2127,7 +2156,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);