(insert-char-data): Don't output mojikyo-pj-*.
[chise/xemacs-chise.git-] / src / objects-x.c
index bc27fef..536228c 100644 (file)
@@ -28,6 +28,7 @@ Boston, MA 02111-1307, USA.  */
 
 #include <config.h>
 #include "lisp.h"
+#include <limits.h>
 
 #include "console-x.h"
 #include "objects-x.h"
@@ -45,11 +46,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 sucess. */
 int
 allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
                        XColor *color_def)
@@ -114,7 +117,7 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
            status = 1;
          else
            {
-             int rd, gr, bl;   
+             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
@@ -140,30 +143,35 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
     }
   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))
@@ -184,7 +192,10 @@ allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
                             +
                             (((color_def->blue >> 8) - (cells[x].blue >> 8))
                              * ((color_def->blue >> 8) - (cells[x].blue >> 8))));
-             if (trial_delta < nearest_delta)
+
+             /* less? Ignore cells marked as previously failing */
+             if( (trial_delta < nearest_delta) &&
+                 (cells[x].pixel != ULONG_MAX) )
                {
                  nearest = x;
                  nearest_delta = trial_delta;
@@ -193,12 +204,15 @@ 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) {
-           status = 2;
-         } else {
-           status = 0; /* JH: how does this happen??? DOES this happen??? */
-           fprintf(stderr,"allocate_nearest_color returned 0!!!\n");
-         }
+         if (XAllocColor (display, colormap, color_def) != 0)
+             status = 2;
+         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;
@@ -208,17 +222,11 @@ int
 x_parse_nearest_color (struct device *d, XColor *color, Bufbyte *name,
                       Bytecount len, 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;
@@ -229,14 +237,14 @@ x_parse_nearest_color (struct device *d, XColor *color, Bufbyte *name,
   }
   if (!result)
     {
-      maybe_signal_simple_error ("unrecognized color", make_string (name, len),
+      maybe_signal_simple_error ("Unrecognized color", make_string (name, len),
                                 Qcolor, errb);
       return 0;
     }
   result = allocate_nearest_color (dpy, cmap, visual, color);
   if (!result)
     {
-      maybe_signal_simple_error ("couldn't allocate color",
+      maybe_signal_simple_error ("Couldn't allocate color",
                                 make_string (name, len), Qcolor, errb);
       return 0;
     }
@@ -367,7 +375,7 @@ x_initialize_font_instance (struct Lisp_Font_Instance *f, Lisp_Object name,
 
   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,10 +457,9 @@ 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 (struct Lisp_Font_Instance *f)
 {
-  ((markobj) (FONT_INSTANCE_X_TRUENAME (f)));
+  mark_object (FONT_INSTANCE_X_TRUENAME (f));
 }
 
 static void
@@ -498,7 +505,7 @@ x_finalize_font_instance (struct Lisp_Font_Instance *f)
    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
+   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
@@ -509,7 +516,7 @@ x_finalize_font_instance (struct Lisp_Font_Instance *f)
    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.
 
@@ -566,7 +573,7 @@ x_finalize_font_instance (struct Lisp_Font_Instance *f)
 static int
 valid_x_font_name_p (Display *dpy, char *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.
    */
@@ -783,7 +790,7 @@ x_font_instance_truename (struct Lisp_Font_Instance *f, Error_behavior errb)
          Lisp_Object font_instance;
          XSETFONT_INSTANCE (font_instance, f);
 
-         maybe_signal_simple_error ("couldn't determine font truename",
+         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.) */