xfaces.c 220 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1
/* xfaces.c -- "Face" primitives.
2
   Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003
3
   Free Software Foundation.
Jim Blandy's avatar
Jim Blandy committed
4

Jim Blandy's avatar
Jim Blandy committed
5 6 7 8
This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Jim Blandy's avatar
Jim Blandy committed
9
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
10 11 12 13 14 15 16 17 18
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

Gerd Moellmann's avatar
Gerd Moellmann committed
22 23 24 25 26 27 28 29
/* New face implementation by Gerd Moellmann <gerd@gnu.org>.  */

/* Faces.

   When using Emacs with X, the display style of characters can be
   changed by defining `faces'.  Each face can specify the following
   display attributes:

30
   1. Font family name.
31

Gerd Moellmann's avatar
Gerd Moellmann committed
32 33
   2. Relative proportionate width, aka character set width or set
   width (swidth), e.g. `semi-compressed'.
34

35
   3. Font height in 1/10pt.
36

Gerd Moellmann's avatar
Gerd Moellmann committed
37
   4. Font weight, e.g. `bold'.
38

Gerd Moellmann's avatar
Gerd Moellmann committed
39
   5. Font slant, e.g. `italic'.
40

Gerd Moellmann's avatar
Gerd Moellmann committed
41
   6. Foreground color.
42

Gerd Moellmann's avatar
Gerd Moellmann committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   7. Background color.

   8. Whether or not characters should be underlined, and in what color.

   9. Whether or not characters should be displayed in inverse video.

   10. A background stipple, a bitmap.

   11. Whether or not characters should be overlined, and in what color.

   12. Whether or not characters should be strike-through, and in what
   color.

   13. Whether or not a box should be drawn around characters, the box
   type, and, for simple boxes, in what color.

59
   14. Font or fontset pattern, or nil.  This is a special attribute.
Stefan Monnier's avatar
Typos.  
Stefan Monnier committed
60
   When this attribute is specified, the face uses a font opened by
61 62 63 64 65 66 67 68
   that pattern as is.  In addition, all the other font-related
   attributes (1st thru 5th) are generated from the opened font name.
   On the other hand, if one of the other font-related attributes are
   specified, this attribute is set to nil.  In that case, the face
   doesn't inherit this attribute from the `default' face, and uses a
   font determined by the other attributes (those may be inherited
   from the `default' face).

Miles Bader's avatar
Miles Bader committed
69 70
   15. A face name or list of face names from which to inherit attributes.

71 72 73 74
   16. A specified average font width, which is invisible from Lisp,
   and is used to ensure that a font specified on the command line,
   for example, can be matched exactly.

Gerd Moellmann's avatar
Gerd Moellmann committed
75 76 77 78
   Faces are frame-local by nature because Emacs allows to define the
   same named face (face names are symbols) differently for different
   frames.  Each frame has an alist of face definitions for all named
   faces.  The value of a named face in such an alist is a Lisp vector
79 80
   with the symbol `face' in slot 0, and a slot for each of the face
   attributes mentioned above.
Gerd Moellmann's avatar
Gerd Moellmann committed
81 82 83 84

   There is also a global face alist `Vface_new_frame_defaults'.  Face
   definitions from this list are used to initialize faces of newly
   created frames.
85

Gerd Moellmann's avatar
Gerd Moellmann committed
86
   A face doesn't have to specify all attributes.  Those not specified
87 88
   have a value of `unspecified'.  Faces specifying all attributes but
   the 14th are called `fully-specified'.
Gerd Moellmann's avatar
Gerd Moellmann committed
89 90 91 92 93 94 95 96 97 98 99 100 101


   Face merging.

   The display style of a given character in the text is determined by
   combining several faces.  This process is called `face merging'.
   Any aspect of the display style that isn't specified by overlays or
   text properties is taken from the `default' face.  Since it is made
   sure that the default face is always fully-specified, face merging
   always results in a fully-specified face.


   Face realization.
102

Gerd Moellmann's avatar
Gerd Moellmann committed
103 104 105 106 107 108 109
   After all face attributes for a character have been determined by
   merging faces of that character, that face is `realized'.  The
   realization process maps face attributes to what is physically
   available on the system where Emacs runs.  The result is a
   `realized face' in form of a struct face which is stored in the
   face cache of the frame on which it was realized.

110 111 112 113
   Face realization is done in the context of the character to display
   because different fonts may be used for different characters.  In
   other words, for characters that have different font
   specifications, different realized faces are needed to display
Gerd Moellmann's avatar
Gerd Moellmann committed
114 115
   them.

116 117 118 119 120 121 122 123 124 125
   Font specification is done by fontsets.  See the comment in
   fontset.c for the details.  In the current implementation, all ASCII
   characters share the same font in a fontset.

   Faces are at first realized for ASCII characters, and, at that
   time, assigned a specific realized fontset.  Hereafter, we call
   such a face as `ASCII face'.  When a face for a multibyte character
   is realized, it inherits (thus shares) a fontset of an ASCII face
   that has the same attributes other than font-related ones.

Stefan Monnier's avatar
Typos.  
Stefan Monnier committed
126
   Thus, all realized face have a realized fontset.
Gerd Moellmann's avatar
Gerd Moellmann committed
127 128 129 130


   Unibyte text.

131 132 133 134 135
   Unibyte text (i.e. raw 8-bit characters) is displayed with the same
   font as ASCII characters.  That is because it is expected that
   unibyte text users specify a font that is suitable both for ASCII
   and raw 8-bit characters.

Gerd Moellmann's avatar
Gerd Moellmann committed
136 137 138 139

   Font selection.

   Font selection tries to find the best available matching font for a
140
   given (character, face) combination.
Gerd Moellmann's avatar
Gerd Moellmann committed
141

142 143 144 145 146 147
   If the face specifies a fontset name, that fontset determines a
   pattern for fonts of the given character.  If the face specifies a
   font name or the other font-related attributes, a fontset is
   realized from the default fontset.  In that case, that
   specification determines a pattern for ASCII characters and the
   default fontset determines a pattern for multibyte characters.
Gerd Moellmann's avatar
Gerd Moellmann committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

   Available fonts on the system on which Emacs runs are then matched
   against the font pattern.  The result of font selection is the best
   match for the given face attributes in this font list.

   Font selection can be influenced by the user.

   1. The user can specify the relative importance he gives the face
   attributes width, height, weight, and slant by setting
   face-font-selection-order (faces.el) to a list of face attribute
   names.  The default is '(:width :height :weight :slant), and means
   that font selection first tries to find a good match for the font
   width specified by a face, then---within fonts with that
   width---tries to find a best match for the specified font height,
   etc.

164
   2. Setting face-font-family-alternatives allows the user to
Gerd Moellmann's avatar
Gerd Moellmann committed
165 166 167
   specify alternative font families to try if a family specified by a
   face doesn't exist.

168 169 170 171 172 173 174
   3. Setting face-font-registry-alternatives allows the user to
   specify all alternative font registries to try for a face
   specifying a registry.

   4. Setting face-ignored-fonts allows the user to ignore specific
   fonts.

Gerd Moellmann's avatar
Gerd Moellmann committed
175

Stefan Monnier's avatar
Typos.  
Stefan Monnier committed
176
   Character composition.
177 178 179 180 181 182 183 184

   Usually, the realization process is already finished when Emacs
   actually reflects the desired glyph matrix on the screen.  However,
   on displaying a composition (sequence of characters to be composed
   on the screen), a suitable font for the components of the
   composition is selected and realized while drawing them on the
   screen, i.e.  the realization process is delayed but in principle
   the same.
Gerd Moellmann's avatar
Gerd Moellmann committed
185

186

Gerd Moellmann's avatar
Gerd Moellmann committed
187 188 189 190 191 192 193
   Initialization of basic faces.

   The faces `default', `modeline' are considered `basic faces'.
   When redisplay happens the first time for a newly created frame,
   basic faces are realized for CHARSET_ASCII.  Frame parameters are
   used to fill in unspecified attributes of the default face.  */

194
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
195 196
#include <sys/types.h>
#include <sys/stat.h>
197

Jim Blandy's avatar
Jim Blandy committed
198
#include "lisp.h"
Karl Heuer's avatar
Karl Heuer committed
199
#include "charset.h"
200
#include "keyboard.h"
201 202
#include "frame.h"

203 204
#ifdef HAVE_WINDOW_SYSTEM
#include "fontset.h"
205 206
#endif /* HAVE_WINDOW_SYSTEM */

Morten Welinder's avatar
Morten Welinder committed
207
#ifdef HAVE_X_WINDOWS
Jim Blandy's avatar
Jim Blandy committed
208
#include "xterm.h"
209 210 211 212
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/XmStrDefs.h>
#endif /* USE_MOTIF */
213
#endif /* HAVE_X_WINDOWS */
Gerd Moellmann's avatar
Gerd Moellmann committed
214

Morten Welinder's avatar
Morten Welinder committed
215 216 217
#ifdef MSDOS
#include "dosfns.h"
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
218

219 220 221 222 223
#ifdef WINDOWSNT
#include "w32term.h"
#include "fontset.h"
/* Redefine X specifics to W32 equivalents to avoid cluttering the
   code with #ifdef blocks. */
224
#undef FRAME_X_DISPLAY_INFO
225 226 227 228 229 230 231 232
#define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
#define x_display_info w32_display_info
#define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
#define check_x check_w32
#define x_list_fonts w32_list_fonts
#define GCGraphicsExposures 0
/* For historic reasons, FONT_WIDTH refers to average width on W32,
   not maximum as on X. Redefine here. */
233
#undef FONT_WIDTH
234
#define FONT_WIDTH FONT_MAX_WIDTH
235
#endif /* WINDOWSNT */
236

237
#ifdef MAC_OS
238 239 240
#include "macterm.h"
#define x_display_info mac_display_info
#define check_x check_mac
241
#endif /* MAC_OS */
242

Jim Blandy's avatar
Jim Blandy committed
243
#include "buffer.h"
Jim Blandy's avatar
Jim Blandy committed
244
#include "dispextern.h"
Jim Blandy's avatar
Jim Blandy committed
245
#include "blockinput.h"
246
#include "window.h"
Karl Heuer's avatar
Karl Heuer committed
247
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
248

Morten Welinder's avatar
Morten Welinder committed
249
#ifdef HAVE_X_WINDOWS
Gerd Moellmann's avatar
Gerd Moellmann committed
250 251

/* Compensate for a bug in Xos.h on some systems, on which it requires
252 253 254 255
   time.h.  On some such systems, Xos.h tries to redefine struct
   timeval and struct timezone if USG is #defined while it is
   #included.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
256
#ifdef XOS_NEEDS_TIME_H
257
#include <time.h>
258 259 260
#undef USG
#include <X11/Xos.h>
#define USG
261
#define __TIMEVAL__
Gerd Moellmann's avatar
Gerd Moellmann committed
262 263 264
#else /* not XOS_NEEDS_TIME_H */
#include <X11/Xos.h>
#endif /* not XOS_NEEDS_TIME_H */
265

Gerd Moellmann's avatar
Gerd Moellmann committed
266
#endif /* HAVE_X_WINDOWS */
267

Gerd Moellmann's avatar
Gerd Moellmann committed
268 269
#include <stdio.h>
#include <ctype.h>
Jim Blandy's avatar
Jim Blandy committed
270

Gerd Moellmann's avatar
Gerd Moellmann committed
271
#define abs(X)		((X) < 0 ? -(X) : (X))
272

Gerd Moellmann's avatar
Gerd Moellmann committed
273 274 275 276
/* Number of pt per inch (from the TeXbook).  */

#define PT_PER_INCH 72.27

Gerd Moellmann's avatar
Gerd Moellmann committed
277 278 279 280 281 282 283 284
/* Non-zero if face attribute ATTR is unspecified.  */

#define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)

/* Value is the number of elements of VECTOR.  */

#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))

285
/* Make a copy of string S on the stack using alloca.  Value is a pointer
Gerd Moellmann's avatar
Gerd Moellmann committed
286 287 288
   to the copy.  */

#define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
289

Gerd Moellmann's avatar
Gerd Moellmann committed
290 291 292
/* Make a copy of the contents of Lisp string S on the stack using
   alloca.  Value is a pointer to the copy.  */

293
#define LSTRDUPA(S) STRDUPA (SDATA ((S)))
Gerd Moellmann's avatar
Gerd Moellmann committed
294

295
/* Size of hash table of realized faces in face caches (should be a
Gerd Moellmann's avatar
Gerd Moellmann committed
296 297 298 299 300 301 302 303 304 305
   prime number).  */

#define FACE_CACHE_BUCKETS_SIZE 1001

/* Keyword symbols used for face attribute names.  */

Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
Lisp_Object QCreverse_video;
Miles Bader's avatar
Miles Bader committed
306
Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
Gerd Moellmann's avatar
Gerd Moellmann committed
307 308 309 310 311 312 313 314 315 316 317

/* Symbols used for attribute values.  */

Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
Lisp_Object Qultra_expanded;
Lisp_Object Qreleased_button, Qpressed_button;
Lisp_Object QCstyle, QCcolor, QCline_width;
318 319 320
Lisp_Object Qunspecified;

char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
Gerd Moellmann's avatar
Gerd Moellmann committed
321

322 323 324 325 326
/* The name of the function to call when the background of the frame
   has changed, frame_update_face_colors.  */

Lisp_Object Qframe_update_face_colors;

Gerd Moellmann's avatar
Gerd Moellmann committed
327 328
/* Names of basic faces.  */

329
Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
330
Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
331
Lisp_Object Qmode_line_inactive;
332
extern Lisp_Object Qmode_line;
333

334 335 336 337 338 339
/* The symbol `face-alias'.  A symbols having that property is an
   alias for another face.  Value of the property is the name of
   the aliased face.  */

Lisp_Object Qface_alias;

Gerd Moellmann's avatar
Gerd Moellmann committed
340 341 342 343 344 345 346 347 348 349 350 351 352
/* Default stipple pattern used on monochrome displays.  This stipple
   pattern is used on monochrome displays instead of shades of gray
   for a face background color.  See `set-face-stipple' for possible
   values for this variable.  */

Lisp_Object Vface_default_stipple;

/* Alist of alternative font families.  Each element is of the form
   (FAMILY FAMILY1 FAMILY2 ...).  If fonts of FAMILY can't be loaded,
   try FAMILY1, then FAMILY2, ...  */

Lisp_Object Vface_alternative_font_family_alist;

353 354 355 356 357 358
/* Alist of alternative font registries.  Each element is of the form
   (REGISTRY REGISTRY1 REGISTRY2...).  If fonts of REGISTRY can't be
   loaded, try REGISTRY1, then REGISTRY2, ...  */

Lisp_Object Vface_alternative_font_registry_alist;

Gerd Moellmann's avatar
Gerd Moellmann committed
359 360 361 362 363 364
/* Allowed scalable fonts.  A value of nil means don't allow any
   scalable fonts.  A value of t means allow the use of any scalable
   font.  Otherwise, value must be a list of regular expressions.  A
   font may be scaled if its name matches a regular expression in the
   list.  */

365
Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
Gerd Moellmann's avatar
Gerd Moellmann committed
366

367 368 369 370
/* List of regular expressions that matches names of fonts to ignore. */

Lisp_Object Vface_ignored_fonts;

371 372 373 374
/* Alist of font name patterns vs the rescaling factor.  */

Lisp_Object Vface_font_rescale_alist;

Gerd Moellmann's avatar
Gerd Moellmann committed
375 376 377 378 379 380
/* Maximum number of fonts to consider in font_list.  If not an
   integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead.  */

Lisp_Object Vfont_list_limit;
#define DEFAULT_FONT_LIST_LIMIT 100

Gerd Moellmann's avatar
Gerd Moellmann committed
381 382 383 384 385 386 387
/* The symbols `foreground-color' and `background-color' which can be
   used as part of a `face' property.  This is for compatibility with
   Emacs 20.2.  */

Lisp_Object Qforeground_color, Qbackground_color;

/* The symbols `face' and `mouse-face' used as text properties.  */
Jim Blandy's avatar
Jim Blandy committed
388

389
Lisp_Object Qface;
Gerd Moellmann's avatar
Gerd Moellmann committed
390 391 392 393
extern Lisp_Object Qmouse_face;

/* Error symbol for wrong_type_argument in load_pixmap.  */

394
Lisp_Object Qbitmap_spec_p;
Jim Blandy's avatar
Jim Blandy committed
395

Gerd Moellmann's avatar
Gerd Moellmann committed
396 397 398 399
/* Alist of global face definitions.  Each element is of the form
   (FACE . LFACE) where FACE is a symbol naming a face and LFACE
   is a Lisp vector of face attributes.  These faces are used
   to initialize faces for new frames.  */
400

Gerd Moellmann's avatar
Gerd Moellmann committed
401
Lisp_Object Vface_new_frame_defaults;
402

Gerd Moellmann's avatar
Gerd Moellmann committed
403
/* The next ID to assign to Lisp faces.  */
404

Gerd Moellmann's avatar
Gerd Moellmann committed
405
static int next_lface_id;
Jim Blandy's avatar
Jim Blandy committed
406

Gerd Moellmann's avatar
Gerd Moellmann committed
407
/* A vector mapping Lisp face Id's to face names.  */
Jim Blandy's avatar
Jim Blandy committed
408

Gerd Moellmann's avatar
Gerd Moellmann committed
409 410
static Lisp_Object *lface_id_to_name;
static int lface_id_to_name_size;
Jim Blandy's avatar
Jim Blandy committed
411

412 413
/* TTY color-related functions (defined in tty-colors.el).  */

414
Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
Gerd Moellmann's avatar
Gerd Moellmann committed
415

416 417 418 419 420 421 422 423
/* The name of the function used to compute colors on TTYs.  */

Lisp_Object Qtty_color_alist;

/* An alist of defined terminal colors and their RGB values.  */

Lisp_Object Vtty_defined_color_alist;

Gerd Moellmann's avatar
Gerd Moellmann committed
424 425 426 427 428 429 430 431 432 433 434 435 436
/* Counter for calls to clear_face_cache.  If this counter reaches
   CLEAR_FONT_TABLE_COUNT, and a frame has more than
   CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed.  */

static int clear_font_table_count;
#define CLEAR_FONT_TABLE_COUNT	100
#define CLEAR_FONT_TABLE_NFONTS	10

/* Non-zero means face attributes have been changed since the last
   redisplay.  Used in redisplay_internal.  */

int face_change_count;

437 438 439 440 441 442 443
/* Non-zero means don't display bold text if a face's foreground
   and background colors are the inverse of the default colors of the
   display.   This is a kluge to suppress `bold black' foreground text
   which is hard to read on an LCD monitor.  */

int tty_suppress_bold_inverse_default_colors_p;

444 445 446 447 448
/* A list of the form `((x . y))' used to avoid consing in
   Finternal_set_lisp_face_attribute.  */

static Lisp_Object Vparam_value_alist;

Gerd Moellmann's avatar
Gerd Moellmann committed
449 450 451 452 453 454 455 456
/* The total number of colors currently allocated.  */

#if GLYPH_DEBUG
static int ncolors_allocated;
static int npixmaps_allocated;
static int ngcs;
#endif

457 458 459 460
/* Non-zero means the definition of the `menu' face for new frames has
   been changed.  */

int menu_face_changed_default;
Gerd Moellmann's avatar
Gerd Moellmann committed
461 462 463 464 465 466 467


/* Function prototypes.  */

struct font_name;
struct table_entry;

468 469
static void map_tty_color P_ ((struct frame *, struct face *,
			       enum lface_attribute_index, int *));
470
static Lisp_Object resolve_face_name P_ ((Lisp_Object));
471
static int may_use_scalable_font_p P_ ((const char *));
Gerd Moellmann's avatar
Gerd Moellmann committed
472 473
static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
474
			      int, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
475
static int x_face_list_fonts P_ ((struct frame *, char *,
476
				  struct font_name **, int, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
477 478 479 480 481 482
static int font_scalable_p P_ ((struct font_name *));
static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
static unsigned char *xstrlwr P_ ((unsigned char *));
static void signal_error P_ ((char *, Lisp_Object));
static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
483
static void load_face_font P_ ((struct frame *, struct face *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
484 485 486 487 488 489 490 491
static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
static void free_face_colors P_ ((struct frame *, struct face *));
static int face_color_gray_p P_ ((struct frame *, char *));
static char *build_font_name P_ ((struct font_name *));
static void free_font_names P_ ((struct font_name *, int));
static int sorted_font_list P_ ((struct frame *, char *,
				 int (*cmpfn) P_ ((const void *, const void *)),
				 struct font_name **));
492 493
static int font_list_1 P_ ((struct frame *, Lisp_Object, Lisp_Object,
			    Lisp_Object, struct font_name **));
494 495
static int font_list P_ ((struct frame *, Lisp_Object, Lisp_Object,
			  Lisp_Object, struct font_name **));
496
static int try_font_list P_ ((struct frame *, Lisp_Object *,
497 498
			      Lisp_Object, Lisp_Object, struct font_name **,
			      int));
499 500
static int try_alternative_families P_ ((struct frame *f, Lisp_Object,
					 Lisp_Object, struct font_name **));
Gerd Moellmann's avatar
Gerd Moellmann committed
501
static int cmp_font_names P_ ((const void *, const void *));
502 503
static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *, int,
				      struct face *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
504
static struct face *realize_x_face P_ ((struct face_cache *,
505
					Lisp_Object *, int, struct face *));
Gerd Moellmann's avatar
Gerd Moellmann committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
static struct face *realize_tty_face P_ ((struct face_cache *,
					  Lisp_Object *, int));
static int realize_basic_faces P_ ((struct frame *));
static int realize_default_face P_ ((struct frame *));
static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
static int lface_fully_specified_p P_ ((Lisp_Object *));
static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
static unsigned lface_hash P_ ((Lisp_Object *));
static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
static struct face_cache *make_face_cache P_ ((struct frame *));
static void free_realized_face P_ ((struct frame *, struct face *));
static void clear_face_gcs P_ ((struct face_cache *));
static void free_face_cache P_ ((struct face_cache *));
static int face_numeric_weight P_ ((Lisp_Object));
static int face_numeric_slant P_ ((Lisp_Object));
static int face_numeric_swidth P_ ((Lisp_Object));
523
static int face_fontset P_ ((Lisp_Object *));
524
static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int, int, int*));
Miles Bader's avatar
Miles Bader committed
525 526 527
static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*, Lisp_Object));
static void merge_face_inheritance P_ ((struct frame *f, Lisp_Object,
					Lisp_Object *, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
528 529
static void merge_face_vector_with_property P_ ((struct frame *, Lisp_Object *,
						 Lisp_Object));
530 531
static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object,
					 Lisp_Object, int, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
532
static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
533
static struct face *make_realized_face P_ ((Lisp_Object *));
Gerd Moellmann's avatar
Gerd Moellmann committed
534 535
static void free_realized_faces P_ ((struct face_cache *));
static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
536
				     struct font_name *, int, int, int *));
Gerd Moellmann's avatar
Gerd Moellmann committed
537 538 539 540 541 542 543 544 545 546 547 548
static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
static void uncache_face P_ ((struct face_cache *, struct face *));
static int xlfd_numeric_slant P_ ((struct font_name *));
static int xlfd_numeric_weight P_ ((struct font_name *));
static int xlfd_numeric_swidth P_ ((struct font_name *));
static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
static int xlfd_fixed_p P_ ((struct font_name *));
static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
				   int, int));
static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
549 550
					    struct font_name *, int,
					    Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
551 552 553
static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
							   struct font_name *, int));

554
#ifdef HAVE_WINDOW_SYSTEM
Gerd Moellmann's avatar
Gerd Moellmann committed
555 556 557 558 559 560 561

static int split_font_name P_ ((struct frame *, struct font_name *, int));
static int xlfd_point_size P_ ((struct frame *, struct font_name *));
static void sort_fonts P_ ((struct frame *, struct font_name *, int,
			       int (*cmpfn) P_ ((const void *, const void *))));
static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
static void x_free_gc P_ ((struct frame *, GC));
562
static void clear_font_table P_ ((struct x_display_info *));
Gerd Moellmann's avatar
Gerd Moellmann committed
563

564 565 566 567
#ifdef WINDOWSNT
extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
#endif /* WINDOWSNT */

568 569
#ifdef USE_X_TOOLKIT
static void x_update_menu_appearance P_ ((struct frame *));
Pavel Janík's avatar
Pavel Janík committed
570 571

extern void free_frame_menubar P_ ((struct frame *));
572 573
#endif /* USE_X_TOOLKIT */

574
#endif /* HAVE_WINDOW_SYSTEM */
Jim Blandy's avatar
Jim Blandy committed
575

576

Gerd Moellmann's avatar
Gerd Moellmann committed
577 578 579
/***********************************************************************
			      Utilities
 ***********************************************************************/
Jim Blandy's avatar
Jim Blandy committed
580

Morten Welinder's avatar
Morten Welinder committed
581
#ifdef HAVE_X_WINDOWS
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
#ifdef DEBUG_X_COLORS

/* The following is a poor mans infrastructure for debugging X color
   allocation problems on displays with PseudoColor-8.  Some X servers
   like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
   color reference counts completely so that they don't signal an
   error when a color is freed whose reference count is already 0.
   Other X servers do.  To help me debug this, the following code
   implements a simple reference counting schema of its own, for a
   single display/screen.  --gerd.  */

/* Reference counts for pixel colors.  */

int color_count[256];

/* Register color PIXEL as allocated.  */

void
register_color (pixel)
     unsigned long pixel;
{
  xassert (pixel < 256);
  ++color_count[pixel];
}


/* Register color PIXEL as deallocated.  */

void
unregister_color (pixel)
     unsigned long pixel;
{
  xassert (pixel < 256);
  if (color_count[pixel] > 0)
    --color_count[pixel];
  else
    abort ();
}


/* Register N colors from PIXELS as deallocated.  */

void
unregister_colors (pixels, n)
     unsigned long *pixels;
     int n;
{
  int i;
  for (i = 0; i < n; ++i)
    unregister_color (pixels[i]);
}

635 636

DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
637 638
       doc: /* Dump currently allocated colors to stderr.  */)
     ()
639 640 641 642
{
  int i, n;

  fputc ('\n', stderr);
643

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
    if (color_count[i])
      {
	fprintf (stderr, "%3d: %5d", i, color_count[i]);
	++n;
	if (n % 5 == 0)
	  fputc ('\n', stderr);
	else
	  fputc ('\t', stderr);
      }

  if (n % 5 != 0)
    fputc ('\n', stderr);
  return Qnil;
}

660 661
#endif /* DEBUG_X_COLORS */

662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
/* Free colors used on frame F.  PIXELS is an array of NPIXELS pixel
   color values.  Interrupt input must be blocked when this function
   is called.  */

void
x_free_colors (f, pixels, npixels)
     struct frame *f;
     unsigned long *pixels;
     int npixels;
{
  int class = FRAME_X_DISPLAY_INFO (f)->visual->class;

  /* If display has an immutable color map, freeing colors is not
     necessary and some servers don't allow it.  So don't do it.  */
  if (class != StaticColor && class != StaticGray && class != TrueColor)
    {
679
#ifdef DEBUG_X_COLORS
680
      unregister_colors (pixels, npixels);
681
#endif
682 683
      XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
		   pixels, npixels, 0);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    }
}


/* Free colors used on frame F.  PIXELS is an array of NPIXELS pixel
   color values.  Interrupt input must be blocked when this function
   is called.  */

void
x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
     Display *dpy;
     Screen *screen;
     Colormap cmap;
     unsigned long *pixels;
     int npixels;
{
  struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
  int class = dpyinfo->visual->class;

  /* If display has an immutable color map, freeing colors is not
     necessary and some servers don't allow it.  So don't do it.  */
  if (class != StaticColor && class != StaticGray && class != TrueColor)
    {
707
#ifdef DEBUG_X_COLORS
708
      unregister_colors (pixels, npixels);
709
#endif
710
      XFreeColors (dpy, cmap, pixels, npixels, 0);
711 712 713
    }
}

714

Gerd Moellmann's avatar
Gerd Moellmann committed
715 716 717 718 719
/* Create and return a GC for use on frame F.  GC values and mask
   are given by XGCV and MASK.  */

static INLINE GC
x_create_gc (f, mask, xgcv)
720
     struct frame *f;
Gerd Moellmann's avatar
Gerd Moellmann committed
721 722
     unsigned long mask;
     XGCValues *xgcv;
Jim Blandy's avatar
Jim Blandy committed
723 724
{
  GC gc;
Gerd Moellmann's avatar
Gerd Moellmann committed
725 726 727 728 729 730
  BLOCK_INPUT;
  gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
  UNBLOCK_INPUT;
  IF_DEBUG (++ngcs);
  return gc;
}
Jim Blandy's avatar
Jim Blandy committed
731

732

Gerd Moellmann's avatar
Gerd Moellmann committed
733 734 735 736 737 738 739
/* Free GC which was used on frame F.  */

static INLINE void
x_free_gc (f, gc)
     struct frame *f;
     GC gc;
{
740
  BLOCK_INPUT;
Gerd Moellmann's avatar
Gerd Moellmann committed
741 742 743 744
  xassert (--ngcs >= 0);
  XFreeGC (FRAME_X_DISPLAY (f), gc);
  UNBLOCK_INPUT;
}
745

Gerd Moellmann's avatar
Gerd Moellmann committed
746
#endif /* HAVE_X_WINDOWS */
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
#ifdef WINDOWSNT
/* W32 emulation of GCs */

static INLINE GC
x_create_gc (f, mask, xgcv)
     struct frame *f;
     unsigned long mask;
     XGCValues *xgcv;
{
  GC gc;
  BLOCK_INPUT;
  gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
  UNBLOCK_INPUT;
  IF_DEBUG (++ngcs);
  return gc;
}


/* Free GC which was used on frame F.  */

static INLINE void
x_free_gc (f, gc)
     struct frame *f;
     GC gc;
{
  BLOCK_INPUT;
  xassert (--ngcs >= 0);
  xfree (gc);
  UNBLOCK_INPUT;
}

#endif  /* WINDOWSNT */
780

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
#ifdef MAC_OS
/* Mac OS emulation of GCs */

extern XGCValues *XCreateGC (void *, Window, unsigned long, XGCValues *);

static INLINE GC
x_create_gc (f, mask, xgcv)
     struct frame *f;
     unsigned long mask;
     XGCValues *xgcv;
{
  GC gc;
  gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
  return gc;
}

static INLINE void
x_free_gc (f, gc)
     struct frame *f;
     GC gc;
{
  XFreeGC (FRAME_MAC_DISPLAY (f), gc);
}

#endif  /* MAC_OS */

Gerd Moellmann's avatar
Gerd Moellmann committed
807 808 809 810 811
/* Like stricmp.  Used to compare parts of font names which are in
   ISO8859-1.  */

int
xstricmp (s1, s2)
812
     const unsigned char *s1, *s2;
Gerd Moellmann's avatar
Gerd Moellmann committed
813 814
{
  while (*s1 && *s2)
815
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
816 817 818 819 820
      unsigned char c1 = tolower (*s1);
      unsigned char c2 = tolower (*s2);
      if (c1 != c2)
	return c1 < c2 ? -1 : 1;
      ++s1, ++s2;
821
    }
822

Gerd Moellmann's avatar
Gerd Moellmann committed
823 824 825 826
  if (*s1 == 0)
    return *s2 == 0 ? 0 : -1;
  return 1;
}
827 828


Gerd Moellmann's avatar
Gerd Moellmann committed
829
/* Like strlwr, which might not always be available.  */
830

Gerd Moellmann's avatar
Gerd Moellmann committed
831 832 833 834 835 836 837 838 839 840
static unsigned char *
xstrlwr (s)
     unsigned char *s;
{
  unsigned char *p = s;

  for (p = s; *p; ++p)
    *p = tolower (*p);

  return s;
Jim Blandy's avatar
Jim Blandy committed
841
}
842

843

Gerd Moellmann's avatar
Gerd Moellmann committed
844 845 846 847 848 849
/* Signal `error' with message S, and additional argument ARG.  */

static void
signal_error (s, arg)
     char *s;
     Lisp_Object arg;
850
{
Gerd Moellmann's avatar
Gerd Moellmann committed
851 852
  Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
}
853

Gerd Moellmann's avatar
Gerd Moellmann committed
854

855 856 857 858 859
/* If FRAME is nil, return a pointer to the selected frame.
   Otherwise, check that FRAME is a live frame, and return a pointer
   to it.  NPARAM is the parameter number of FRAME, for
   CHECK_LIVE_FRAME.  This is here because it's a frequent pattern in
   Lisp function definitions.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
860 861 862 863 864 865 866

static INLINE struct frame *
frame_or_selected_frame (frame, nparam)
     Lisp_Object frame;
     int nparam;
{
  if (NILP (frame))
867
    frame = selected_frame;
868

869
  CHECK_LIVE_FRAME (frame);
870
  return XFRAME (frame);
871
}
Gerd Moellmann's avatar
Gerd Moellmann committed
872

873

Gerd Moellmann's avatar
Gerd Moellmann committed
874 875 876
/***********************************************************************
			   Frames and faces
 ***********************************************************************/
877

Gerd Moellmann's avatar
Gerd Moellmann committed
878
/* Initialize face cache and basic faces for frame F.  */
879

Gerd Moellmann's avatar
Gerd Moellmann committed
880 881
void
init_frame_faces (f)
882 883
     struct frame *f;
{
Gerd Moellmann's avatar
Gerd Moellmann committed
884 885 886
  /* Make a face cache, if F doesn't have one.  */
  if (FRAME_FACE_CACHE (f) == NULL)
    FRAME_FACE_CACHE (f) = make_face_cache (f);
887

888
#ifdef HAVE_WINDOW_SYSTEM
Gerd Moellmann's avatar
Gerd Moellmann committed
889
  /* Make the image cache.  */
890
  if (FRAME_WINDOW_P (f))
Gerd Moellmann's avatar
Gerd Moellmann committed
891 892 893 894 895
    {
      if (FRAME_X_IMAGE_CACHE (f) == NULL)
	FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
      ++FRAME_X_IMAGE_CACHE (f)->refcount;
    }
896
#endif /* HAVE_WINDOW_SYSTEM */
897

898
  /* Realize basic faces.  Must have enough information in frame
Gerd Moellmann's avatar
Gerd Moellmann committed
899 900 901
     parameters to realize basic faces at this point.  */
#ifdef HAVE_X_WINDOWS
  if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
902 903 904
#endif
#ifdef WINDOWSNT
  if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
Andrew Choi's avatar
Andrew Choi committed
905 906 907
#endif
#ifdef MAC_OS
  if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
Gerd Moellmann's avatar
Gerd Moellmann committed
908 909 910 911
#endif
    if (!realize_basic_faces (f))
      abort ();
}
912 913


Gerd Moellmann's avatar
Gerd Moellmann committed
914
/* Free face cache of frame F.  Called from Fdelete_frame.  */
915

Gerd Moellmann's avatar
Gerd Moellmann committed
916 917
void
free_frame_faces (f)
918 919
     struct frame *f;
{
Gerd Moellmann's avatar
Gerd Moellmann committed
920
  struct face_cache *face_cache = FRAME_FACE_CACHE (f);
921

Gerd Moellmann's avatar
Gerd Moellmann committed
922 923 924 925 926
  if (face_cache)
    {
      free_face_cache (face_cache);
      FRAME_FACE_CACHE (f) = NULL;
    }
927

928 929
#ifdef HAVE_WINDOW_SYSTEM
  if (FRAME_WINDOW_P (f))
930
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
931 932
      struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
      if (image_cache)
933
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
934 935 936
	  --image_cache->refcount;
	  if (image_cache->refcount == 0)
	    free_image_cache (f);
937 938
	}
    }
939
#endif /* HAVE_WINDOW_SYSTEM */
940 941
}

Gerd Moellmann's avatar
Gerd Moellmann committed
942

943 944 945 946
/* Clear face caches, and recompute basic faces for frame F.  Call
   this after changing frame parameters on which those faces depend,
   or when realized faces have been freed due to changing attributes
   of named faces. */
Gerd Moellmann's avatar
Gerd Moellmann committed
947 948 949

void
recompute_basic_faces (f)
950 951
     struct frame *f;
{
Gerd Moellmann's avatar
Gerd Moellmann committed
952 953
  if (FRAME_FACE_CACHE (f))
    {
954
      clear_face_cache (0);
955 956
      if (!realize_basic_faces (f))
	abort ();
Gerd Moellmann's avatar
Gerd Moellmann committed
957 958
    }
}
959 960


Gerd Moellmann's avatar
Gerd Moellmann committed
961 962
/* Clear the face caches of all frames.  CLEAR_FONTS_P non-zero means
   try to free unused fonts, too.  */
963

964
void
Gerd Moellmann's avatar
Gerd Moellmann committed
965 966
clear_face_cache (clear_fonts_p)
     int clear_fonts_p;
967
{
968
#ifdef HAVE_WINDOW_SYSTEM
Gerd Moellmann's avatar
Gerd Moellmann committed
969 970
  Lisp_Object tail, frame;
  struct frame *f;
971

Gerd Moellmann's avatar
Gerd Moellmann committed
972 973
  if (clear_fonts_p
      || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
974
    {
975
      struct x_display_info *dpyinfo;
976

977 978 979 980 981
      /* Fonts are common for frames on one display, i.e. on
	 one X screen.  */
      for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
	if (dpyinfo->n_fonts > CLEAR_FONT_TABLE_NFONTS)
	  clear_font_table (dpyinfo);
982

Gerd Moellmann's avatar
Gerd Moellmann committed
983 984 985 986
      /* From time to time see if we can unload some fonts.  This also
	 frees all realized faces on all frames.  Fonts needed by
	 faces will be loaded again when faces are realized again.  */
      clear_font_table_count = 0;
987

Gerd Moellmann's avatar
Gerd Moellmann committed
988
      FOR_EACH_FRAME (tail, frame)
989
	{
990
	  struct frame *f = XFRAME (frame);
991
	  if (FRAME_WINDOW_P (f)
Gerd Moellmann's avatar
Gerd Moellmann committed
992
	      && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
993
	    free_all_realized_faces (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
994 995 996 997 998 999 1000 1001
	}
    }
  else
    {
      /* Clear GCs of realized faces.  */
      FOR_EACH_FRAME (tail, frame)
	{
	  f = XFRAME (frame);
1002
	  if (FRAME_WINDOW_P (f))
Gerd Moellmann's avatar
Gerd Moellmann committed
1003 1004 1005
	    {
	      clear_face_gcs (FRAME_FACE_CACHE (f));
	      clear_image_cache (f, 0);
1006 1007
	    }
	}
1008
    }
1009
#endif /* HAVE_WINDOW_SYSTEM */
1010 1011
}

Gerd Moellmann's avatar
Gerd Moellmann committed
1012 1013

DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
1014 1015 1016
       doc: /* Clear face caches on all frames.
Optional THOROUGHLY non-nil means try to free unused fonts, too.  */)
     (thoroughly)
1017
     Lisp_Object thoroughly;
1018
{
1019
  clear_face_cache (!NILP (thoroughly));
1020 1021
  ++face_change_count;
  ++windows_or_buffers_changed;
Gerd Moellmann's avatar
Gerd Moellmann committed
1022 1023 1024 1025 1026
  return Qnil;
}



1027
#ifdef HAVE_WINDOW_SYSTEM
Gerd Moellmann's avatar
Gerd Moellmann committed
1028 1029


Gerd Moellmann's avatar
Gerd Moellmann committed
1030 1031
/* Remove fonts from the font table of DPYINFO except for the default
   ASCII fonts of frames on that display.  Called from clear_face_cache
1032
   from time to time.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1033 1034

static void
1035 1036
clear_font_table (dpyinfo)
     struct x_display_info *dpyinfo;
Gerd Moellmann's avatar
Gerd Moellmann committed
1037 1038 1039
{
  int i;

1040
  /* Free those fonts that are not used by frames on DPYINFO.  */
1041 1042 1043
  for (i = 0; i < dpyinfo->n_fonts; ++i)
    {
      struct font_info *font_info = dpyinfo->font_table + i;
1044 1045 1046 1047 1048 1049
      Lisp_Object tail, frame;

      /* Check if slot is already free.  */
      if (font_info->name == NULL)
	continue;

1050
      /* Don't free a default font of some frame.  */
1051 1052 1053 1054 1055 1056 1057
      FOR_EACH_FRAME (tail, frame)
	{
	  struct frame *f = XFRAME (frame);
	  if (FRAME_WINDOW_P (f)
	      && font_info->font == FRAME_FONT (f))
	    break;
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
1058

1059
      if (!NILP (tail))
1060
	continue;
Gerd Moellmann's avatar
Gerd Moellmann committed
1061

1062 1063 1064 1065
      /* Free names.  */
      if (font_info->full_name != font_info->name)
	xfree (font_info->full_name);
      xfree (font_info->name);
Gerd Moellmann's avatar
Gerd Moellmann committed
1066

1067 1068
      /* Free the font.  */
      BLOCK_INPUT;
1069
#ifdef HAVE_X_WINDOWS
1070
      XFreeFont (dpyinfo->display, font_info->font);
1071 1072
#endif
#ifdef WINDOWSNT
1073
      w32_unload_font (dpyinfo, font_info->font);
1074
#endif
1075
      UNBLOCK_INPUT;
Gerd Moellmann's avatar
Gerd Moellmann committed
1076

1077 1078 1079 1080
      /* Mark font table slot free.  */
      font_info->font = NULL;
      font_info->name = font_info->full_name = NULL;
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
1081 1082
}

1083
#endif /* HAVE_WINDOW_SYSTEM */
Gerd Moellmann's avatar
Gerd Moellmann committed
1084 1085 1086 1087 1088 1089 1090



/***********************************************************************
			      X Pixmaps
 ***********************************************************************/

1091
#ifdef HAVE_WINDOW_SYSTEM
Gerd Moellmann's avatar
Gerd Moellmann committed
1092

1093
DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
1094
       doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
Gerd Moellmann's avatar
Gerd Moellmann committed
1095 1096 1097 1098
A bitmap specification is either a string, a file name, or a list
\(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
HEIGHT is its height, and DATA is a string containing the bits of
the pixmap.  Bits are stored row by row, each row occupies
1099 1100
\(WIDTH + 7)/8 bytes.  */)
     (object)
Gerd Moellmann's avatar
Gerd Moellmann committed
1101 1102
     Lisp_Object object;
{
1103
  int pixmap_p = 0;
1104

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
  if (STRINGP (object))
    /* If OBJECT is a string, it's a file name.  */
    pixmap_p = 1;
  else if (CONSP (object))
    {
      /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
	 HEIGHT must be integers > 0, and DATA must be string large
	 enough to hold a bitmap of the specified size.  */
      Lisp_Object width, height, data;

      height = width = data = Qnil;
1116

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
      if (CONSP (object))
	{
	  width = XCAR (object);
	  object = XCDR (object);
	  if (CONSP (object))
	    {
	      height = XCAR (object);
	      object = XCDR (object);
	      if (CONSP (object))
		data = XCAR (object);
	    }
	}
1129

1130 1131 1132 1133
      if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
	{
	  int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
			       / BITS_PER_CHAR);
1134
	  if (SBYTES (data) >= bytes_per_row * XINT (height))
1135 1136 1137 1138 1139
	    pixmap_p = 1;
	}
    }

  return pixmap_p ? Qt : Qnil;
1140 1141 1142
}


Gerd Moellmann's avatar
Gerd Moellmann committed
1143 1144 1145 1146 1147 1148
/* Load a bitmap according to NAME (which is either a file name or a
   pixmap spec) for use on frame F.  Value is the bitmap_id (see
   xfns.c).  If NAME is nil, return with a bitmap id of zero.  If
   bitmap cannot be loaded, display a message saying so, and return
   zero.  Store the bitmap width in *W_PTR and its height in *H_PTR,
   if these pointers are not null.  */
1149

Gerd Moellmann's avatar
Gerd Moellmann committed
1150
static int
1151
load_pixmap (f, name, w_ptr, h_ptr)
1152
     FRAME_PTR f;
1153 1154 1155 1156 1157 1158 1159
     Lisp_Object name;
     unsigned int *w_ptr, *h_ptr;
{
  int bitmap_id;
  Lisp_Object tem;

  if (NILP (name))
Gerd Moellmann's avatar
Gerd Moellmann committed
1160
    return 0;
1161

1162
  tem = Fbitmap_spec_p (name);
1163
  if (NILP (tem))
1164
    wrong_type_argument (Qbitmap_spec_p, name);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

  BLOCK_INPUT;
  if (CONSP (name))
    {
      /* Decode a bitmap spec into a bitmap.  */

      int h, w;
      Lisp_Object bits;

      w = XINT (Fcar (name));
      h = XINT (Fcar (Fcdr (name)));
      bits = Fcar (Fcdr (Fcdr (name)));

1178
      bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1179 1180 1181 1182 1183 1184 1185 1186 1187
					     w, h);
    }
  else
    {
      /* It must be a string -- a file name.  */
      bitmap_id = x_create_bitmap_from_file (f, name);
    }
  UNBLOCK_INPUT;

1188
  if (bitmap_id < 0)
Gerd Moellmann's avatar
Gerd Moellmann committed
1189
    {
1190
      add_to_log ("Invalid or undefined bitmap %s", name, Qnil);
Gerd Moellmann's avatar
Gerd Moellmann committed
1191
      bitmap_id = 0;
1192

Gerd Moellmann's avatar
Gerd Moellmann committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
      if (w_ptr)
	*w_ptr = 0;
      if (h_ptr)
	*h_ptr = 0;
    }
  else
    {
#if GLYPH_DEBUG
      ++npixmaps_allocated;
#endif
      if (w_ptr)
	*w_ptr = x_bitmap_width (f, bitmap_id);

      if (h_ptr)
	*h_ptr = x_bitmap_height (f, bitmap_id);
    }
1209 1210

  return bitmap_id;
1211
}
Morten Welinder's avatar
Morten Welinder committed
1212

1213
#endif /* HAVE_WINDOW_SYSTEM */
Gerd Moellmann's avatar
Gerd Moellmann committed
1214

Morten Welinder's avatar
Morten Welinder committed
1215

Gerd Moellmann's avatar
Gerd Moellmann committed
1216 1217 1218 1219 1220

/***********************************************************************
			 Minimum font bounds
 ***********************************************************************/