(U+6215): Apply new conventions for glyph granularity.
[chise/xemacs-chise.git.1] / src / objects-x.c
index bc27fef..25a19c9 100644 (file)
@@ -2,7 +2,7 @@
    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
    Copyright (C) 1995 Board of Trustees, University of Illinois.
    Copyright (C) 1995 Tinker Systems.
-   Copyright (C) 1995, 1996 Ben Wing.
+   Copyright (C) 1995, 1996, 2000 Ben Wing.
    Copyright (C) 1995 Sun Microsystems, Inc.
 
 This file is part of XEmacs.
@@ -26,6 +26,8 @@ Boston, MA 02111-1307, USA.  */
 
 /* Authors: Jamie Zawinski, Chuck Thompson, Ben Wing */
 
+/* This file Mule-ized by Ben Wing, 7-10-00. */
+
 #include <config.h>
 #include "lisp.h"
 
@@ -45,11 +47,13 @@ int x_handle_non_fully_specified_fonts;
 
 /* Replacement for XAllocColor() that tries to return the nearest
    available color if the colormap is full.  Original was from FSFmacs,
-   but rewritten by Jareth Hein <jareth@camelot-soft.com> 97/11/25 */
+   but rewritten by Jareth Hein <jareth@camelot-soft.com> 97/11/25
+   Modified by Lee Kindness <lkindness@csl.co.uk> 31/08/99 to handle previous
+   total failure which was due to a read/write colorcell being the nearest
+   match - tries the next nearest...
 
-/* Return value is 1 for normal success, 2 for nearest color success,
-   3 for Non-deallocable sucess, and 0 for absolute failure (shouldn't
-   happen?) */
+   Return value is 1 for normal success, 2 for nearest color success,
+   3 for Non-deallocable success. */
 int
 allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
                        XColor *color_def)
@@ -66,7 +70,7 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
        {
          /* We're dealing with a TrueColor/DirectColor visual, so play games
             with the RGB values in the XColor struct. */
-         /* ### JH: I'm not sure how a call to XAllocColor can fail in a
+         /* #### JH: I'm not sure how a call to XAllocColor can fail in a
             TrueColor or DirectColor visual, so I will just reformat the
             request to match the requirements of the visual, and re-issue
             the request.  If this fails for anybody, I wanna know about it
@@ -114,8 +118,8 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
            status = 1;
          else
            {
-             int rd, gr, bl;   
-             /* ### JH: I'm punting here, knowing that doing this will at
+             int rd, gr, bl;
+             /* #### JH: I'm punting here, knowing that doing this will at
                 least draw the color correctly.  However, unless we convert
                 all of the functions that allocate colors (graphics
                 libraries, etc) to use this function doing this is very
@@ -133,58 +137,71 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
                bl = color_def->blue << (bbits - 8);
              else
                bl = color_def->blue >> (8 - bbits);
-             color_def->pixel = (rd << rshift) | (gr << gshift) | (bl << bshift);
+             color_def->pixel = (rd << rshift) | (gr << gshift) | (bl <<
+                                                                   bshift);
              status = 3;
            }
        }
     }
   else
     {
+      XColor *cells = NULL;
+      /* JH: I can't believe there's no way to go backwards from a
+        colormap ID and get its visual and number of entries, but X
+        apparently isn't built that way... */
+      int no_cells = visual->map_entries;
+      status = 0;
+
       if (XAllocColor (display, colormap, color_def) != 0)
        status = 1;
-      else
+      else while( status != 2 )
        {
          /* If we got to this point, the colormap is full, so we're
             going to try and get the next closest color.  The algorithm used
             is a least-squares matching, which is what X uses for closest
             color matching with StaticColor visuals. */
-         XColor *cells;
-         /* JH: I can't believe there's no way to go backwards from a
-            colormap ID and get its visual and number of entries, but X
-            apparently isn't built that way... */
-         int no_cells = visual->map_entries;
          int nearest;
          long nearest_delta, trial_delta;
          int x;
 
-         cells = alloca_array (XColor, no_cells);
+         if( cells == NULL )
+           {
+             cells = alloca_array (XColor, no_cells);
+             for (x = 0; x < no_cells; x++)
+               cells[x].pixel = x;
 
-         for (x = 0; x < no_cells; x++)
-           cells[x].pixel = x;
+             /* read the current colormap */
+             XQueryColors (display, colormap, cells, no_cells);
+           }
 
-         /* read the current colormap */
-         XQueryColors (display, colormap, cells, no_cells);
          nearest = 0;
          /* I'm assuming CSE so I'm not going to condense this. */
          nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8))
                            * ((color_def->red >> 8) - (cells[0].red >> 8)))
                           +
                           (((color_def->green >> 8) - (cells[0].green >> 8))
-                           * ((color_def->green >> 8) - (cells[0].green >> 8)))
+                           * ((color_def->green >> 8) - (cells[0].green >>
+                                                         8)))
                           +
                           (((color_def->blue >> 8) - (cells[0].blue >> 8))
-                           * ((color_def->blue >> 8) - (cells[0].blue >> 8))));
+                           * ((color_def->blue >> 8) - (cells[0].blue >>
+                                                        8))));
          for (x = 1; x < no_cells; x++)
            {
              trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8))
                              * ((color_def->red >> 8) - (cells[x].red >> 8)))
                             +
                             (((color_def->green >> 8) - (cells[x].green >> 8))
-                             * ((color_def->green >> 8) - (cells[x].green >> 8)))
+                             * ((color_def->green >> 8) - (cells[x].green >>
+                                                           8)))
                             +
                             (((color_def->blue >> 8) - (cells[x].blue >> 8))
-                             * ((color_def->blue >> 8) - (cells[x].blue >> 8))));
-             if (trial_delta < nearest_delta)
+                             * ((color_def->blue >> 8) - (cells[x].blue >>
+                                                          8))));
+
+             /* less? Ignore cells marked as previously failing */
+             if( (trial_delta < nearest_delta) &&
+                 (cells[x].pixel != ULONG_MAX) )
                {
                  nearest = x;
                  nearest_delta = trial_delta;
@@ -193,51 +210,46 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
          color_def->red = cells[nearest].red;
          color_def->green = cells[nearest].green;
          color_def->blue = cells[nearest].blue;
-         if (XAllocColor (display, colormap, color_def) != 0) {
+         if (XAllocColor (display, colormap, color_def) != 0)
            status = 2;
-         } else {
-           status = 0; /* JH: how does this happen??? DOES this happen??? */
-           fprintf(stderr,"allocate_nearest_color returned 0!!!\n");
-         }
+         else
+           /* LSK: Either the colour map has changed since
+            * we read it, or the colour is allocated
+            * read/write... Mark this cmap entry so it's
+            * ignored in the next iteration.
+            */
+           cells[nearest].pixel = ULONG_MAX;
        }
     }
   return status;
 }
 
-int
-x_parse_nearest_color (struct device *d, XColor *color, Bufbyte *name,
-                      Bytecount len, Error_behavior errb)
+static int
+x_parse_nearest_color (struct device *d, XColor *color, Lisp_Object name,
+                      Error_behavior errb)
 {
-  Display *dpy;
-  Screen *xs;
-  Colormap cmap;
-  Visual *visual;
+  Display *dpy   = DEVICE_X_DISPLAY  (d);
+  Colormap cmap  = DEVICE_X_COLORMAP (d);
+  Visual *visual = DEVICE_X_VISUAL   (d);
   int result;
 
-  dpy = DEVICE_X_DISPLAY (d);
-  xs = DefaultScreenOfDisplay (dpy);
-  cmap = DEVICE_X_COLORMAP(d);
-  visual = DEVICE_X_VISUAL (d);
-
   xzero (*color);
   {
-    CONST Extbyte *extname;
-    Extcount extnamelen;
+    const Extbyte *extname;
 
-    GET_CHARPTR_EXT_BINARY_DATA_ALLOCA (name, len, extname, extnamelen);
-    result = XParseColor (dpy, cmap, (char *) extname, color);
+    LISP_STRING_TO_EXTERNAL (name, extname, Qx_color_name_encoding);
+    result = XParseColor (dpy, cmap, extname, color);
   }
   if (!result)
     {
-      maybe_signal_simple_error ("unrecognized color", make_string (name, len),
-                                Qcolor, errb);
+      maybe_signal_simple_error ("Unrecognized color", name, Qcolor, errb);
       return 0;
     }
   result = allocate_nearest_color (dpy, cmap, visual, color);
   if (!result)
     {
-      maybe_signal_simple_error ("couldn't allocate color",
-                                make_string (name, len), Qcolor, errb);
+      maybe_signal_simple_error ("Couldn't allocate color", name, Qcolor,
+                                errb);
       return 0;
     }
 
@@ -245,16 +257,13 @@ x_parse_nearest_color (struct device *d, XColor *color, Bufbyte *name,
 }
 
 static int
-x_initialize_color_instance (struct Lisp_Color_Instance *c, Lisp_Object name,
+x_initialize_color_instance (Lisp_Color_Instance *c, Lisp_Object name,
                             Lisp_Object device, Error_behavior errb)
 {
   XColor color;
   int result;
 
-  result = x_parse_nearest_color (XDEVICE (device), &color,
-                                 XSTRING_DATA   (name),
-                                 XSTRING_LENGTH (name),
-                                 errb);
+  result = x_parse_nearest_color (XDEVICE (device), &color, name, errb);
 
   if (!result)
     return 0;
@@ -271,7 +280,7 @@ x_initialize_color_instance (struct Lisp_Color_Instance *c, Lisp_Object name,
 }
 
 static void
-x_print_color_instance (struct Lisp_Color_Instance *c,
+x_print_color_instance (Lisp_Color_Instance *c,
                        Lisp_Object printcharfun,
                        int escapeflag)
 {
@@ -283,7 +292,7 @@ x_print_color_instance (struct Lisp_Color_Instance *c,
 }
 
 static void
-x_finalize_color_instance (struct Lisp_Color_Instance *c)
+x_finalize_color_instance (Lisp_Color_Instance *c)
 {
   if (c->data)
     {
@@ -291,7 +300,8 @@ x_finalize_color_instance (struct Lisp_Color_Instance *c)
        {
          if (COLOR_INSTANCE_X_DEALLOC (c))
            {
-             XFreeColors (DEVICE_X_DISPLAY (XDEVICE (c->device)), DEVICE_X_COLORMAP (XDEVICE (c->device)),
+             XFreeColors (DEVICE_X_DISPLAY (XDEVICE (c->device)),
+                          DEVICE_X_COLORMAP (XDEVICE (c->device)),
                           &COLOR_INSTANCE_X_COLOR (c).pixel, 1, 0);
            }
        }
@@ -306,8 +316,8 @@ x_finalize_color_instance (struct Lisp_Color_Instance *c)
    be comparing their names or pixel values instead. */
 
 static int
-x_color_instance_equal (struct Lisp_Color_Instance *c1,
-                       struct Lisp_Color_Instance *c2,
+x_color_instance_equal (Lisp_Color_Instance *c1,
+                       Lisp_Color_Instance *c2,
                        int depth)
 {
   XColor color1 = COLOR_INSTANCE_X_COLOR (c1);
@@ -318,14 +328,14 @@ x_color_instance_equal (struct Lisp_Color_Instance *c1,
 }
 
 static unsigned long
-x_color_instance_hash (struct Lisp_Color_Instance *c, int depth)
+x_color_instance_hash (Lisp_Color_Instance *c, int depth)
 {
   XColor color = COLOR_INSTANCE_X_COLOR (c);
   return HASH3 (color.red, color.green, color.blue);
 }
 
 static Lisp_Object
-x_color_instance_rgb_components (struct Lisp_Color_Instance *c)
+x_color_instance_rgb_components (Lisp_Color_Instance *c)
 {
   XColor color = COLOR_INSTANCE_X_COLOR (c);
   return (list3 (make_int (color.red),
@@ -339,13 +349,11 @@ x_valid_color_name_p (struct device *d, Lisp_Object color)
   XColor c;
   Display *dpy = DEVICE_X_DISPLAY (d);
   Colormap cmap = DEVICE_X_COLORMAP (d);
+  const Extbyte *extname;
 
-  CONST char *extname;
+  LISP_STRING_TO_EXTERNAL (color, extname, Qx_color_name_encoding);
 
-  GET_C_STRING_CTEXT_DATA_ALLOCA (color, extname);
-
-  return XParseColor (dpy, cmap,
-                     extname, &c);
+  return XParseColor (dpy, cmap, extname, &c);
 }
 
 \f
@@ -354,20 +362,19 @@ x_valid_color_name_p (struct device *d, Lisp_Object color)
 /************************************************************************/
 
 static int
-x_initialize_font_instance (struct Lisp_Font_Instance *f, Lisp_Object name,
+x_initialize_font_instance (Lisp_Font_Instance *f, Lisp_Object name,
                            Lisp_Object device, Error_behavior errb)
 {
-  Display *dpy;
+  Display *dpy = DEVICE_X_DISPLAY (XDEVICE (device));
   XFontStruct *xf;
-  CONST char *extname;
+  const Extbyte *extname;
 
-  dpy = DEVICE_X_DISPLAY (XDEVICE (device));
-  GET_C_STRING_CTEXT_DATA_ALLOCA (f->name, extname);
+  LISP_STRING_TO_EXTERNAL (f->name, extname, Qx_font_name_encoding);
   xf = XLoadQueryFont (dpy, extname);
 
   if (!xf)
     {
-      maybe_signal_simple_error ("couldn't load font", f->name,
+      maybe_signal_simple_error ("Couldn't load font", f->name,
                                 Qfont, errb);
       return 0;
     }
@@ -449,14 +456,13 @@ x_initialize_font_instance (struct Lisp_Font_Instance *f, Lisp_Object name,
 }
 
 static void
-x_mark_font_instance (struct Lisp_Font_Instance *f,
-                      void (*markobj) (Lisp_Object))
+x_mark_font_instance (Lisp_Font_Instance *f)
 {
-  ((markobj) (FONT_INSTANCE_X_TRUENAME (f)));
+  mark_object (FONT_INSTANCE_X_TRUENAME (f));
 }
 
 static void
-x_print_font_instance (struct Lisp_Font_Instance *f,
+x_print_font_instance (Lisp_Font_Instance *f,
                       Lisp_Object printcharfun,
                       int escapeflag)
 {
@@ -466,7 +472,7 @@ x_print_font_instance (struct Lisp_Font_Instance *f,
 }
 
 static void
-x_finalize_font_instance (struct Lisp_Font_Instance *f)
+x_finalize_font_instance (Lisp_Font_Instance *f)
 {
 
   if (f->data)
@@ -497,19 +503,19 @@ x_finalize_font_instance (struct Lisp_Font_Instance *f)
    "bitstream" fonts even if the bitstream fonts are earlier in the path, and
    also picking 100dpi adobe fonts over 75dpi adobe fonts even though the
    75dpi are in the path earlier) but sometimes appears to be doing something
-   else entirely (for example, removing the bitsream fonts from the path will
-   cause the 75dpi adobe fonts to be used instead of the100dpi, even though
+   else entirely (for example, removing the bitstream fonts from the path will
+   cause the 75dpi adobe fonts to be used instead of the 100dpi, even though
    their relative positions in the path (and their names!) have not changed).
 
    The documentation for XSetFontPath() seems to indicate that the order of
-   entries in the font path means something, but it's pretty noncommital about
+   entries in the font path means something, but it's pretty noncommittal about
    it, and the spirit of the law is apparently not being obeyed...
 
    All the fonts I've seen have a property named `FONT' which contains the
    truename of the font.  However, there are two problems with using this: the
    first is that the X Protocol Document is quite explicit that all properties
    are optional, so we can't depend on it being there.  The second is that
-   it's concievable that this alleged truename isn't actually accessible as a
+   it's conceivable that this alleged truename isn't actually accessible as a
    font, due to some difference of opinion between the font designers and
    whoever installed the font on the system.
 
@@ -564,14 +570,14 @@ x_finalize_font_instance (struct Lisp_Font_Instance *f)
    that the various servers are actually doing, please let me know!  -- jwz. */
 
 static int
-valid_x_font_name_p (Display *dpy, char *name)
+valid_x_font_name_p (Display *dpy, Extbyte *name)
 {
-  /* Maybe this should be implemented by callign XLoadFont and trapping
+  /* Maybe this should be implemented by calling XLoadFont and trapping
      the error.  That would be a lot of work, and wasteful as hell, but
      might be more correct.
    */
   int nnames = 0;
-  char **names = 0;
+  Extbyte **names = 0;
   if (! name)
     return 0;
   names = XListFonts (dpy, name, 1, &nnames);
@@ -580,11 +586,11 @@ valid_x_font_name_p (Display *dpy, char *name)
   return (nnames != 0);
 }
 
-static char *
+static Extbyte *
 truename_via_FONT_prop (Display *dpy, XFontStruct *font)
 {
   unsigned long value = 0;
-  char *result = 0;
+  Extbyte *result = 0;
   if (XGetFontProperty (font, XA_FONT, &value))
     result = XGetAtomName (dpy, value);
   /* result is now 0, or the string value of the FONT property. */
@@ -600,19 +606,19 @@ truename_via_FONT_prop (Display *dpy, XFontStruct *font)
   return result;       /* this must be freed by caller if non-0 */
 }
 
-static char *
+static Extbyte *
 truename_via_random_props (Display *dpy, XFontStruct *font)
 {
   struct device *d = get_device_from_display (dpy);
   unsigned long value = 0;
-  char *foundry, *family, *weight, *slant, *setwidth, *add_style;
+  Extbyte *foundry, *family, *weight, *slant, *setwidth, *add_style;
   unsigned long pixel, point, res_x, res_y;
-  char *spacing;
+  Extbyte *spacing;
   unsigned long avg_width;
-  char *registry, *encoding;
-  char composed_name [2048];
+  Extbyte *registry, *encoding;
+  Extbyte composed_name [2048];
   int ok = 0;
-  char *result;
+  Extbyte *result;
 
 #define get_string(atom,var)                           \
   if (XGetFontProperty (font, (atom), &value))         \
@@ -655,7 +661,7 @@ truename_via_random_props (Display *dpy, XFontStruct *font)
   if (ok)
     {
       int L = strlen (composed_name) + 1;
-      result = (char *) xmalloc (L);
+      result = (Extbyte *) xmalloc (L);
       strncpy (result, composed_name, L);
     }
   else
@@ -675,13 +681,13 @@ truename_via_random_props (Display *dpy, XFontStruct *font)
 }
 
 /* Unbounded, for sufficiently small values of infinity... */
-#define MAX_FONT_COUNT 5000
+#define MAX_FONT_COUNT INT_MAX
 
-static char *
-truename_via_XListFonts (Display *dpy, char *font_name)
+static Extbyte *
+truename_via_XListFonts (Display *dpy, Extbyte *font_name)
 {
-  char *result = 0;
-  char **names;
+  Extbyte *result = 0;
+  Extbyte **names;
   int count = 0;
 
 #ifndef XOPENFONT_SORTS
@@ -693,6 +699,7 @@ truename_via_XListFonts (Display *dpy, char *font_name)
   /* But the world I live in is much more perverse. */
   names = XListFonts (dpy, font_name, MAX_FONT_COUNT, &count);
   while (count--)
+    /* !!#### Not Mule-friendly */
     /* If names[count] is lexicographically less than result, use it.
        (#### Should we be comparing case-insensitively?) */
     if (result == 0 || (strcmp (result, names [count]) < 0))
@@ -708,11 +715,11 @@ truename_via_XListFonts (Display *dpy, char *font_name)
 }
 
 static Lisp_Object
-x_font_truename (Display *dpy, char *name, XFontStruct *font)
+x_font_truename (Display *dpy, Extbyte *name, XFontStruct *font)
 {
-  char *truename_FONT = 0;
-  char *truename_random = 0;
-  char *truename = 0;
+  Extbyte *truename_FONT = 0;
+  Extbyte *truename_random = 0;
+  Extbyte *truename = 0;
 
   /* The search order is:
      - if FONT property exists, and is a valid name, return it.
@@ -757,7 +764,7 @@ x_font_truename (Display *dpy, char *name, XFontStruct *font)
 
   if (truename)
     {
-      Lisp_Object result = build_string (truename);
+      Lisp_Object result = build_ext_string (truename, Qx_font_name_encoding);
       XFree (truename);
       return result;
     }
@@ -766,52 +773,58 @@ x_font_truename (Display *dpy, char *name, XFontStruct *font)
 }
 
 static Lisp_Object
-x_font_instance_truename (struct Lisp_Font_Instance *f, Error_behavior errb)
+x_font_instance_truename (Lisp_Font_Instance *f, Error_behavior errb)
 {
   struct device *d = XDEVICE (f->device);
 
   if (NILP (FONT_INSTANCE_X_TRUENAME (f)))
     {
       Display *dpy = DEVICE_X_DISPLAY (d);
-      char *name = (char *) XSTRING_DATA (f->name);
       {
+       Extbyte *nameext;
+
+       LISP_STRING_TO_EXTERNAL (f->name, nameext, Qx_font_name_encoding);
        FONT_INSTANCE_X_TRUENAME (f) =
-         x_font_truename (dpy, name, FONT_INSTANCE_X_FONT (f));
+         x_font_truename (dpy, nameext, FONT_INSTANCE_X_FONT (f));
       }
       if (NILP (FONT_INSTANCE_X_TRUENAME (f)))
        {
          Lisp_Object font_instance;
          XSETFONT_INSTANCE (font_instance, f);
 
-         maybe_signal_simple_error ("couldn't determine font truename",
-                                  font_instance, Qfont, errb);
+         maybe_signal_simple_error ("Couldn't determine font truename",
+                                    font_instance, Qfont, errb);
          /* Ok, just this once, return the font name as the truename.
             (This is only used by Fequal() right now.) */
          return f->name;
        }
     }
-  return (FONT_INSTANCE_X_TRUENAME (f));
+  return FONT_INSTANCE_X_TRUENAME (f);
 }
 
 static Lisp_Object
-x_font_instance_properties (struct Lisp_Font_Instance *f)
+x_font_instance_properties (Lisp_Font_Instance *f)
 {
   struct device *d = XDEVICE (f->device);
   int i;
   Lisp_Object result = Qnil;
-  XFontProp *props;
-  Display *dpy;
+  Display *dpy = DEVICE_X_DISPLAY (d);
+  XFontProp *props = FONT_INSTANCE_X_FONT (f)->properties;
 
-  dpy = DEVICE_X_DISPLAY (d);
-  props = FONT_INSTANCE_X_FONT (f)->properties;
   for (i = FONT_INSTANCE_X_FONT (f)->n_properties - 1; i >= 0; i--)
     {
-      char *name_str = 0;
-      char *val_str = 0;
       Lisp_Object name, value;
       Atom atom = props [i].name;
-      name_str = XGetAtomName (dpy, atom);
-      name = (name_str ? intern (name_str) : Qnil);
+      Bufbyte *name_str = 0;
+      size_t name_len;
+      Extbyte *namestrext = XGetAtomName (dpy, atom);
+
+      if (namestrext)
+       TO_INTERNAL_FORMAT (C_STRING, namestrext,
+                           ALLOCA, (name_str, name_len),
+                           Qx_atom_name_encoding);
+
+      name = (name_str ? intern ((char *) name_str) : Qnil);
       if (name_str &&
          (atom == XA_FONT ||
           atom == DEVICE_XATOM_FOUNDRY (d) ||
@@ -823,24 +836,26 @@ x_font_instance_properties (struct Lisp_Font_Instance *f)
           atom == DEVICE_XATOM_SPACING (d) ||
           atom == DEVICE_XATOM_CHARSET_REGISTRY (d) ||
           atom == DEVICE_XATOM_CHARSET_ENCODING (d) ||
-          !strcmp (name_str, "CHARSET_COLLECTIONS") ||
-          !strcmp (name_str, "FONTNAME_REGISTRY") ||
-          !strcmp (name_str, "CLASSIFICATION") ||
-          !strcmp (name_str, "COPYRIGHT") ||
-          !strcmp (name_str, "DEVICE_FONT_NAME") ||
-          !strcmp (name_str, "FULL_NAME") ||
-          !strcmp (name_str, "MONOSPACED") ||
-          !strcmp (name_str, "QUALITY") ||
-          !strcmp (name_str, "RELATIVE_SET") ||
-          !strcmp (name_str, "RELATIVE_WEIGHT") ||
-          !strcmp (name_str, "STYLE")))
+          !bufbyte_strcmp (name_str, "CHARSET_COLLECTIONS") ||
+          !bufbyte_strcmp (name_str, "FONTNAME_REGISTRY") ||
+          !bufbyte_strcmp (name_str, "CLASSIFICATION") ||
+          !bufbyte_strcmp (name_str, "COPYRIGHT") ||
+          !bufbyte_strcmp (name_str, "DEVICE_FONT_NAME") ||
+          !bufbyte_strcmp (name_str, "FULL_NAME") ||
+          !bufbyte_strcmp (name_str, "MONOSPACED") ||
+          !bufbyte_strcmp (name_str, "QUALITY") ||
+          !bufbyte_strcmp (name_str, "RELATIVE_SET") ||
+          !bufbyte_strcmp (name_str, "RELATIVE_WEIGHT") ||
+          !bufbyte_strcmp (name_str, "STYLE")))
        {
-         val_str = XGetAtomName (dpy, props [i].card32);
-         value = (val_str ? build_string (val_str) : Qnil);
+         Extbyte *val_str = XGetAtomName (dpy, props [i].card32);
+
+         value = (val_str ? build_ext_string (val_str, Qx_atom_name_encoding)
+                  : Qnil);
        }
       else
        value = make_int (props [i].card32);
-      if (name_str) XFree (name_str);
+      if (namestrext) XFree (namestrext);
       result = Fcons (Fcons (name, value), result);
     }
   return result;
@@ -849,17 +864,18 @@ x_font_instance_properties (struct Lisp_Font_Instance *f)
 static Lisp_Object
 x_list_fonts (Lisp_Object pattern, Lisp_Object device)
 {
-  char **names;
+  Extbyte **names;
   int count = 0;
   Lisp_Object result = Qnil;
-  CONST char *patternext;
+  const Extbyte *patternext;
 
-  GET_C_STRING_BINARY_DATA_ALLOCA (pattern, patternext);
+  LISP_STRING_TO_EXTERNAL (pattern, patternext, Qx_font_name_encoding);
 
   names = XListFonts (DEVICE_X_DISPLAY (XDEVICE (device)),
                      patternext, MAX_FONT_COUNT, &count);
   while (count--)
-    result = Fcons (build_ext_string (names [count], FORMAT_BINARY), result);
+    result = Fcons (build_ext_string (names[count], Qx_font_name_encoding),
+                   result);
   if (names)
     XFreeFontNames (names);
   return result;
@@ -869,7 +885,7 @@ x_list_fonts (Lisp_Object pattern, Lisp_Object device)
 
 static int
 x_font_spec_matches_charset (struct device *d, Lisp_Object charset,
-                            CONST Bufbyte *nonreloc, Lisp_Object reloc,
+                            const Bufbyte *nonreloc, Lisp_Object reloc,
                             Bytecount offset, Bytecount length)
 {
   if (UNBOUNDP (charset))
@@ -881,7 +897,7 @@ x_font_spec_matches_charset (struct device *d, Lisp_Object charset,
      */
   if (EQ (charset, Vcharset_ascii))
     {
-      CONST Bufbyte *the_nonreloc = nonreloc;
+      const Bufbyte *the_nonreloc = nonreloc;
       int i;
       Bytecount the_length = length;
 
@@ -893,7 +909,7 @@ x_font_spec_matches_charset (struct device *d, Lisp_Object charset,
        {
          for (i = 0;; i++)
            {
-             CONST Bufbyte *new_nonreloc = (CONST Bufbyte *)
+             const Bufbyte *new_nonreloc = (const Bufbyte *)
                memchr (the_nonreloc, '-', the_length);
              if (!new_nonreloc)
                break;
@@ -921,26 +937,29 @@ x_font_spec_matches_charset (struct device *d, Lisp_Object charset,
 static Lisp_Object
 x_find_charset_font (Lisp_Object device, Lisp_Object font, Lisp_Object charset)
 {
-  char **names;
+  Extbyte **names;
   int count = 0;
   Lisp_Object result = Qnil;
-  CONST char *patternext;
+  const Extbyte *patternext;
   int i;
 
-  GET_C_STRING_BINARY_DATA_ALLOCA (font, patternext);
+  LISP_STRING_TO_EXTERNAL (font, patternext, Qx_font_name_encoding);
 
   names = XListFonts (DEVICE_X_DISPLAY (XDEVICE (device)),
                      patternext, MAX_FONT_COUNT, &count);
-  /* ### This code seems awfully bogus -- mrb */
+  /* #### This code seems awfully bogus -- mrb */
   for (i = 0; i < count; i ++)
     {
-      CONST Bufbyte *intname;
+      const Bufbyte *intname;
+      Bytecount intlen;
 
-      GET_C_CHARPTR_INT_BINARY_DATA_ALLOCA (names[i], intname);
+      TO_INTERNAL_FORMAT (C_STRING, names[i],
+                         ALLOCA, (intname, intlen),
+                         Qx_font_name_encoding);
       if (x_font_spec_matches_charset (XDEVICE (device), charset,
                                       intname, Qnil, 0, -1))
        {
-         result = build_string ((char *) intname);
+         result = make_string (intname, intlen);
          break;
        }
     }