Merged from miles@gnu.org--gnu-2005 (patch 59, 240-245)
[bpt/emacs.git] / src / xfaces.c
CommitLineData
82641697 1/* xfaces.c -- "Face" primitives.
97f4312d 2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
d26d6fd9 3 Free Software Foundation.
7b7739b1 4
c115973b
JB
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
7b7739b1 9the Free Software Foundation; either version 2, or (at your option)
c115973b
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
c115973b 21
82641697
GM
22/* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
23
24/* Faces.
25
26 When using Emacs with X, the display style of characters can be
27 changed by defining `faces'. Each face can specify the following
28 display attributes:
29
39506348 30 1. Font family name.
178c5d9c 31
82641697
GM
32 2. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
178c5d9c 34
39506348 35 3. Font height in 1/10pt.
178c5d9c 36
82641697 37 4. Font weight, e.g. `bold'.
178c5d9c 38
82641697 39 5. Font slant, e.g. `italic'.
178c5d9c 40
82641697 41 6. Foreground color.
178c5d9c 42
82641697
GM
43 7. Background color.
44
45 8. Whether or not characters should be underlined, and in what color.
46
47 9. Whether or not characters should be displayed in inverse video.
48
49 10. A background stipple, a bitmap.
50
51 11. Whether or not characters should be overlined, and in what color.
52
53 12. Whether or not characters should be strike-through, and in what
54 color.
55
56 13. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
58
39506348 59 14. Font or fontset pattern, or nil. This is a special attribute.
ec7a10e3 60 When this attribute is specified, the face uses a font opened by
39506348
KH
61 that pattern as is. In addition, all the other font-related
62 attributes (1st thru 5th) are generated from the opened font name.
63 On the other hand, if one of the other font-related attributes are
64 specified, this attribute is set to nil. In that case, the face
65 doesn't inherit this attribute from the `default' face, and uses a
66 font determined by the other attributes (those may be inherited
67 from the `default' face).
68
2c20458f
MB
69 15. A face name or list of face names from which to inherit attributes.
70
a08332c0
GM
71 16. A specified average font width, which is invisible from Lisp,
72 and is used to ensure that a font specified on the command line,
73 for example, can be matched exactly.
74
82641697
GM
75 Faces are frame-local by nature because Emacs allows to define the
76 same named face (face names are symbols) differently for different
77 frames. Each frame has an alist of face definitions for all named
78 faces. The value of a named face in such an alist is a Lisp vector
39506348
KH
79 with the symbol `face' in slot 0, and a slot for each of the face
80 attributes mentioned above.
82641697
GM
81
82 There is also a global face alist `Vface_new_frame_defaults'. Face
83 definitions from this list are used to initialize faces of newly
84 created frames.
178c5d9c 85
82641697 86 A face doesn't have to specify all attributes. Those not specified
39506348
KH
87 have a value of `unspecified'. Faces specifying all attributes but
88 the 14th are called `fully-specified'.
82641697
GM
89
90
91 Face merging.
92
93 The display style of a given character in the text is determined by
94 combining several faces. This process is called `face merging'.
95 Any aspect of the display style that isn't specified by overlays or
96 text properties is taken from the `default' face. Since it is made
97 sure that the default face is always fully-specified, face merging
98 always results in a fully-specified face.
99
100
101 Face realization.
178c5d9c 102
82641697
GM
103 After all face attributes for a character have been determined by
104 merging faces of that character, that face is `realized'. The
105 realization process maps face attributes to what is physically
106 available on the system where Emacs runs. The result is a
107 `realized face' in form of a struct face which is stored in the
108 face cache of the frame on which it was realized.
109
39506348
KH
110 Face realization is done in the context of the character to display
111 because different fonts may be used for different characters. In
112 other words, for characters that have different font
113 specifications, different realized faces are needed to display
82641697
GM
114 them.
115
39506348
KH
116 Font specification is done by fontsets. See the comment in
117 fontset.c for the details. In the current implementation, all ASCII
118 characters share the same font in a fontset.
119
120 Faces are at first realized for ASCII characters, and, at that
121 time, assigned a specific realized fontset. Hereafter, we call
122 such a face as `ASCII face'. When a face for a multibyte character
123 is realized, it inherits (thus shares) a fontset of an ASCII face
124 that has the same attributes other than font-related ones.
125
ec7a10e3 126 Thus, all realized face have a realized fontset.
82641697
GM
127
128
129 Unibyte text.
130
39506348
KH
131 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
132 font as ASCII characters. That is because it is expected that
133 unibyte text users specify a font that is suitable both for ASCII
134 and raw 8-bit characters.
135
82641697
GM
136
137 Font selection.
138
139 Font selection tries to find the best available matching font for a
39506348 140 given (character, face) combination.
82641697 141
39506348
KH
142 If the face specifies a fontset name, that fontset determines a
143 pattern for fonts of the given character. If the face specifies a
144 font name or the other font-related attributes, a fontset is
145 realized from the default fontset. In that case, that
146 specification determines a pattern for ASCII characters and the
147 default fontset determines a pattern for multibyte characters.
82641697
GM
148
149 Available fonts on the system on which Emacs runs are then matched
150 against the font pattern. The result of font selection is the best
151 match for the given face attributes in this font list.
152
153 Font selection can be influenced by the user.
154
155 1. The user can specify the relative importance he gives the face
156 attributes width, height, weight, and slant by setting
157 face-font-selection-order (faces.el) to a list of face attribute
158 names. The default is '(:width :height :weight :slant), and means
159 that font selection first tries to find a good match for the font
160 width specified by a face, then---within fonts with that
161 width---tries to find a best match for the specified font height,
162 etc.
163
c824bfbc 164 2. Setting face-font-family-alternatives allows the user to
82641697
GM
165 specify alternative font families to try if a family specified by a
166 face doesn't exist.
167
c824bfbc
KH
168 3. Setting face-font-registry-alternatives allows the user to
169 specify all alternative font registries to try for a face
170 specifying a registry.
171
172 4. Setting face-ignored-fonts allows the user to ignore specific
173 fonts.
174
82641697 175
ec7a10e3 176 Character composition.
39506348
KH
177
178 Usually, the realization process is already finished when Emacs
179 actually reflects the desired glyph matrix on the screen. However,
180 on displaying a composition (sequence of characters to be composed
181 on the screen), a suitable font for the components of the
182 composition is selected and realized while drawing them on the
183 screen, i.e. the realization process is delayed but in principle
184 the same.
82641697 185
178c5d9c 186
82641697
GM
187 Initialization of basic faces.
188
189 The faces `default', `modeline' are considered `basic faces'.
190 When redisplay happens the first time for a newly created frame,
191 basic faces are realized for CHARSET_ASCII. Frame parameters are
192 used to fill in unspecified attributes of the default face. */
193
68c45bf0 194#include <config.h>
5890e9f7 195#include <stdio.h>
c115973b
JB
196#include <sys/types.h>
197#include <sys/stat.h>
4d553a13 198#include <stdio.h> /* This needs to be before termchar.h */
7ee72033 199
c115973b 200#include "lisp.h"
a8517066 201#include "charset.h"
9763806e 202#include "keyboard.h"
b5c53576 203#include "frame.h"
428a555e 204#include "termhooks.h"
b5c53576 205
f7cc6849
DL
206#ifdef HAVE_WINDOW_SYSTEM
207#include "fontset.h"
d12d0a9b
GM
208#endif /* HAVE_WINDOW_SYSTEM */
209
87485d6f 210#ifdef HAVE_X_WINDOWS
c115973b 211#include "xterm.h"
c7ae3284
GM
212#ifdef USE_MOTIF
213#include <Xm/Xm.h>
214#include <Xm/XmStrDefs.h>
215#endif /* USE_MOTIF */
d12d0a9b 216#endif /* HAVE_X_WINDOWS */
82641697 217
87485d6f
MW
218#ifdef MSDOS
219#include "dosfns.h"
220#endif
82641697 221
c3cee013
JR
222#ifdef WINDOWSNT
223#include "w32term.h"
224#include "fontset.h"
225/* Redefine X specifics to W32 equivalents to avoid cluttering the
226 code with #ifdef blocks. */
9763806e 227#undef FRAME_X_DISPLAY_INFO
c3cee013
JR
228#define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
229#define x_display_info w32_display_info
230#define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
231#define check_x check_w32
232#define x_list_fonts w32_list_fonts
233#define GCGraphicsExposures 0
d12d0a9b 234#endif /* WINDOWSNT */
c3cee013 235
e0f712ba 236#ifdef MAC_OS
1a578e9b
AC
237#include "macterm.h"
238#define x_display_info mac_display_info
239#define check_x check_mac
e0f712ba 240#endif /* MAC_OS */
1a578e9b 241
c115973b 242#include "buffer.h"
f211082d 243#include "dispextern.h"
357f32fc 244#include "blockinput.h"
b6d40e46 245#include "window.h"
bde7c500 246#include "intervals.h"
28d440ab 247#include "termchar.h"
c115973b 248
87485d6f 249#ifdef HAVE_X_WINDOWS
82641697
GM
250
251/* Compensate for a bug in Xos.h on some systems, on which it requires
657070ac
JB
252 time.h. On some such systems, Xos.h tries to redefine struct
253 timeval and struct timezone if USG is #defined while it is
254 #included. */
657070ac 255
82641697 256#ifdef XOS_NEEDS_TIME_H
e11d186d 257#include <time.h>
657070ac
JB
258#undef USG
259#include <X11/Xos.h>
260#define USG
e11d186d 261#define __TIMEVAL__
82641697
GM
262#else /* not XOS_NEEDS_TIME_H */
263#include <X11/Xos.h>
264#endif /* not XOS_NEEDS_TIME_H */
e11d186d 265
82641697 266#endif /* HAVE_X_WINDOWS */
7a4d2269 267
82641697 268#include <ctype.h>
c115973b 269
82641697 270#define abs(X) ((X) < 0 ? -(X) : (X))
cb637678 271
c660ce4e
GM
272/* Number of pt per inch (from the TeXbook). */
273
274#define PT_PER_INCH 72.27
275
82641697
GM
276/* Non-zero if face attribute ATTR is unspecified. */
277
278#define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
279
280/* Value is the number of elements of VECTOR. */
281
282#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
283
178c5d9c 284/* Make a copy of string S on the stack using alloca. Value is a pointer
82641697
GM
285 to the copy. */
286
287#define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
bc0db68d 288
82641697
GM
289/* Make a copy of the contents of Lisp string S on the stack using
290 alloca. Value is a pointer to the copy. */
291
d5db4077 292#define LSTRDUPA(S) STRDUPA (SDATA ((S)))
82641697 293
178c5d9c 294/* Size of hash table of realized faces in face caches (should be a
82641697
GM
295 prime number). */
296
297#define FACE_CACHE_BUCKETS_SIZE 1001
298
299/* Keyword symbols used for face attribute names. */
300
301Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
302Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
303Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
304Lisp_Object QCreverse_video;
2c20458f 305Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
82641697
GM
306
307/* Symbols used for attribute values. */
308
309Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
310Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
311Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
312Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
313Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
314Lisp_Object Qultra_expanded;
315Lisp_Object Qreleased_button, Qpressed_button;
316Lisp_Object QCstyle, QCcolor, QCline_width;
ef917393
EZ
317Lisp_Object Qunspecified;
318
319char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
82641697 320
92610620
GM
321/* The name of the function to call when the background of the frame
322 has changed, frame_update_face_colors. */
323
324Lisp_Object Qframe_update_face_colors;
325
82641697
GM
326/* Names of basic faces. */
327
fb4927c4 328Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
c7ae3284 329Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
039b6394 330Lisp_Object Qmode_line_inactive;
fb4927c4 331extern Lisp_Object Qmode_line;
8bd201d6 332
92610620
GM
333/* The symbol `face-alias'. A symbols having that property is an
334 alias for another face. Value of the property is the name of
335 the aliased face. */
336
337Lisp_Object Qface_alias;
338
82641697
GM
339/* Default stipple pattern used on monochrome displays. This stipple
340 pattern is used on monochrome displays instead of shades of gray
341 for a face background color. See `set-face-stipple' for possible
342 values for this variable. */
343
344Lisp_Object Vface_default_stipple;
345
82641697
GM
346/* Alist of alternative font families. Each element is of the form
347 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
348 try FAMILY1, then FAMILY2, ... */
349
350Lisp_Object Vface_alternative_font_family_alist;
351
32fcc231
GM
352/* Alist of alternative font registries. Each element is of the form
353 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
354 loaded, try REGISTRY1, then REGISTRY2, ... */
355
356Lisp_Object Vface_alternative_font_registry_alist;
357
82641697
GM
358/* Allowed scalable fonts. A value of nil means don't allow any
359 scalable fonts. A value of t means allow the use of any scalable
360 font. Otherwise, value must be a list of regular expressions. A
361 font may be scaled if its name matches a regular expression in the
362 list. */
363
eeffb293 364Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
82641697 365
c824bfbc
KH
366/* List of regular expressions that matches names of fonts to ignore. */
367
368Lisp_Object Vface_ignored_fonts;
369
f70400f2
KH
370/* Alist of font name patterns vs the rescaling factor. */
371
372Lisp_Object Vface_font_rescale_alist;
373
057df17c
GM
374/* Maximum number of fonts to consider in font_list. If not an
375 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
376
377Lisp_Object Vfont_list_limit;
378#define DEFAULT_FONT_LIST_LIMIT 100
379
82641697
GM
380/* The symbols `foreground-color' and `background-color' which can be
381 used as part of a `face' property. This is for compatibility with
382 Emacs 20.2. */
383
384Lisp_Object Qforeground_color, Qbackground_color;
385
386/* The symbols `face' and `mouse-face' used as text properties. */
7b7739b1 387
ff83dbb1 388Lisp_Object Qface;
82641697
GM
389extern Lisp_Object Qmouse_face;
390
46b00436
KS
391/* Property for basic faces which other faces cannot inherit. */
392
393Lisp_Object Qface_no_inherit;
394
82641697
GM
395/* Error symbol for wrong_type_argument in load_pixmap. */
396
fef04523 397Lisp_Object Qbitmap_spec_p;
f211082d 398
82641697
GM
399/* Alist of global face definitions. Each element is of the form
400 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
401 is a Lisp vector of face attributes. These faces are used
402 to initialize faces for new frames. */
cb637678 403
82641697 404Lisp_Object Vface_new_frame_defaults;
18195655 405
82641697 406/* The next ID to assign to Lisp faces. */
cb637678 407
82641697 408static int next_lface_id;
c115973b 409
82641697 410/* A vector mapping Lisp face Id's to face names. */
c115973b 411
82641697
GM
412static Lisp_Object *lface_id_to_name;
413static int lface_id_to_name_size;
c115973b 414
ae4b4ba5
GM
415/* TTY color-related functions (defined in tty-colors.el). */
416
b35df831 417Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
82641697 418
ae4b4ba5
GM
419/* The name of the function used to compute colors on TTYs. */
420
421Lisp_Object Qtty_color_alist;
422
423/* An alist of defined terminal colors and their RGB values. */
424
425Lisp_Object Vtty_defined_color_alist;
426
82641697
GM
427/* Counter for calls to clear_face_cache. If this counter reaches
428 CLEAR_FONT_TABLE_COUNT, and a frame has more than
429 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
430
431static int clear_font_table_count;
432#define CLEAR_FONT_TABLE_COUNT 100
433#define CLEAR_FONT_TABLE_NFONTS 10
434
435/* Non-zero means face attributes have been changed since the last
436 redisplay. Used in redisplay_internal. */
437
438int face_change_count;
439
a4a76b61
GM
440/* Non-zero means don't display bold text if a face's foreground
441 and background colors are the inverse of the default colors of the
442 display. This is a kluge to suppress `bold black' foreground text
443 which is hard to read on an LCD monitor. */
444
445int tty_suppress_bold_inverse_default_colors_p;
446
dbc968b8
GM
447/* A list of the form `((x . y))' used to avoid consing in
448 Finternal_set_lisp_face_attribute. */
449
450static Lisp_Object Vparam_value_alist;
451
82641697
GM
452/* The total number of colors currently allocated. */
453
454#if GLYPH_DEBUG
455static int ncolors_allocated;
456static int npixmaps_allocated;
457static int ngcs;
458#endif
459
ceeda019
GM
460/* Non-zero means the definition of the `menu' face for new frames has
461 been changed. */
462
463int menu_face_changed_default;
82641697
GM
464
465\f
466/* Function prototypes. */
467
468struct font_name;
469struct table_entry;
a0a23346 470struct named_merge_point;
82641697 471
ae4b4ba5
GM
472static void map_tty_color P_ ((struct frame *, struct face *,
473 enum lface_attribute_index, int *));
c7ae3284 474static Lisp_Object resolve_face_name P_ ((Lisp_Object));
53c208f6 475static int may_use_scalable_font_p P_ ((const char *));
82641697
GM
476static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
477static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
a08332c0 478 int, int));
82641697 479static int x_face_list_fonts P_ ((struct frame *, char *,
ee5be7c3 480 struct font_name **, int, int));
82641697 481static int font_scalable_p P_ ((struct font_name *));
82641697
GM
482static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
483static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
82641697
GM
484static unsigned char *xstrlwr P_ ((unsigned char *));
485static void signal_error P_ ((char *, Lisp_Object));
82641697 486static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
39506348 487static void load_face_font P_ ((struct frame *, struct face *, int));
82641697
GM
488static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
489static void free_face_colors P_ ((struct frame *, struct face *));
490static int face_color_gray_p P_ ((struct frame *, char *));
491static char *build_font_name P_ ((struct font_name *));
492static void free_font_names P_ ((struct font_name *, int));
493static int sorted_font_list P_ ((struct frame *, char *,
494 int (*cmpfn) P_ ((const void *, const void *)),
495 struct font_name **));
32fcc231
GM
496static int font_list_1 P_ ((struct frame *, Lisp_Object, Lisp_Object,
497 Lisp_Object, struct font_name **));
39506348
KH
498static int font_list P_ ((struct frame *, Lisp_Object, Lisp_Object,
499 Lisp_Object, struct font_name **));
177c0ea7 500static int try_font_list P_ ((struct frame *, Lisp_Object *,
54580ab2
KH
501 Lisp_Object, Lisp_Object, struct font_name **,
502 int));
4a529c42
GM
503static int try_alternative_families P_ ((struct frame *f, Lisp_Object,
504 Lisp_Object, struct font_name **));
82641697 505static int cmp_font_names P_ ((const void *, const void *));
39506348
KH
506static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *, int,
507 struct face *, int));
82641697 508static struct face *realize_x_face P_ ((struct face_cache *,
39506348 509 Lisp_Object *, int, struct face *));
82641697
GM
510static struct face *realize_tty_face P_ ((struct face_cache *,
511 Lisp_Object *, int));
512static int realize_basic_faces P_ ((struct frame *));
513static int realize_default_face P_ ((struct frame *));
514static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
515static int lface_fully_specified_p P_ ((Lisp_Object *));
516static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
517static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
518static unsigned lface_hash P_ ((Lisp_Object *));
519static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
520static struct face_cache *make_face_cache P_ ((struct frame *));
521static void free_realized_face P_ ((struct frame *, struct face *));
522static void clear_face_gcs P_ ((struct face_cache *));
523static void free_face_cache P_ ((struct face_cache *));
524static int face_numeric_weight P_ ((Lisp_Object));
525static int face_numeric_slant P_ ((Lisp_Object));
526static int face_numeric_swidth P_ ((Lisp_Object));
39506348 527static int face_fontset P_ ((Lisp_Object *));
6ae2cd57 528static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int, int, int*));
a0a23346
MB
529static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*,
530 struct named_merge_point *));
531static int merge_face_ref P_ ((struct frame *, Lisp_Object, Lisp_Object *,
532 int, struct named_merge_point *));
39506348
KH
533static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object,
534 Lisp_Object, int, int));
82641697 535static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
39506348 536static struct face *make_realized_face P_ ((Lisp_Object *));
82641697
GM
537static void free_realized_faces P_ ((struct face_cache *));
538static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
6ae2cd57 539 struct font_name *, int, int, int *));
82641697
GM
540static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
541static void uncache_face P_ ((struct face_cache *, struct face *));
542static int xlfd_numeric_slant P_ ((struct font_name *));
543static int xlfd_numeric_weight P_ ((struct font_name *));
544static int xlfd_numeric_swidth P_ ((struct font_name *));
545static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
546static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
547static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
548static int xlfd_fixed_p P_ ((struct font_name *));
549static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
550 int, int));
551static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
6fc556fd
KR
552 struct font_name *, int,
553 Lisp_Object));
82641697
GM
554static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
555 struct font_name *, int));
556
c3cee013 557#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
558
559static int split_font_name P_ ((struct frame *, struct font_name *, int));
560static int xlfd_point_size P_ ((struct frame *, struct font_name *));
561static void sort_fonts P_ ((struct frame *, struct font_name *, int,
562 int (*cmpfn) P_ ((const void *, const void *))));
563static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
564static void x_free_gc P_ ((struct frame *, GC));
d5641fc5 565static void clear_font_table P_ ((struct x_display_info *));
82641697 566
c3cee013
JR
567#ifdef WINDOWSNT
568extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
569#endif /* WINDOWSNT */
570
bce72079
GM
571#ifdef USE_X_TOOLKIT
572static void x_update_menu_appearance P_ ((struct frame *));
9180dc8c
PJ
573
574extern void free_frame_menubar P_ ((struct frame *));
bce72079
GM
575#endif /* USE_X_TOOLKIT */
576
c3cee013 577#endif /* HAVE_WINDOW_SYSTEM */
c115973b 578
cb637678 579\f
82641697
GM
580/***********************************************************************
581 Utilities
582 ***********************************************************************/
c115973b 583
87485d6f 584#ifdef HAVE_X_WINDOWS
cb637678 585
a435fc2a
GM
586#ifdef DEBUG_X_COLORS
587
588/* The following is a poor mans infrastructure for debugging X color
589 allocation problems on displays with PseudoColor-8. Some X servers
590 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
591 color reference counts completely so that they don't signal an
592 error when a color is freed whose reference count is already 0.
593 Other X servers do. To help me debug this, the following code
594 implements a simple reference counting schema of its own, for a
595 single display/screen. --gerd. */
596
597/* Reference counts for pixel colors. */
598
599int color_count[256];
600
601/* Register color PIXEL as allocated. */
602
603void
604register_color (pixel)
605 unsigned long pixel;
606{
607 xassert (pixel < 256);
608 ++color_count[pixel];
609}
610
611
612/* Register color PIXEL as deallocated. */
613
614void
615unregister_color (pixel)
616 unsigned long pixel;
617{
618 xassert (pixel < 256);
619 if (color_count[pixel] > 0)
620 --color_count[pixel];
621 else
622 abort ();
623}
624
625
626/* Register N colors from PIXELS as deallocated. */
627
628void
629unregister_colors (pixels, n)
630 unsigned long *pixels;
631 int n;
632{
633 int i;
634 for (i = 0; i < n; ++i)
635 unregister_color (pixels[i]);
636}
637
08dc08dc
GM
638
639DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
7ee72033
MB
640 doc: /* Dump currently allocated colors to stderr. */)
641 ()
08dc08dc
GM
642{
643 int i, n;
644
645 fputc ('\n', stderr);
178c5d9c 646
08dc08dc
GM
647 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
648 if (color_count[i])
649 {
650 fprintf (stderr, "%3d: %5d", i, color_count[i]);
651 ++n;
652 if (n % 5 == 0)
653 fputc ('\n', stderr);
654 else
655 fputc ('\t', stderr);
656 }
657
658 if (n % 5 != 0)
659 fputc ('\n', stderr);
660 return Qnil;
661}
662
a435fc2a
GM
663#endif /* DEBUG_X_COLORS */
664
d12d0a9b 665
1f847cf8
GM
666/* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
667 color values. Interrupt input must be blocked when this function
668 is called. */
669
670void
671x_free_colors (f, pixels, npixels)
672 struct frame *f;
673 unsigned long *pixels;
674 int npixels;
675{
676 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
677
678 /* If display has an immutable color map, freeing colors is not
679 necessary and some servers don't allow it. So don't do it. */
680 if (class != StaticColor && class != StaticGray && class != TrueColor)
681 {
a435fc2a 682#ifdef DEBUG_X_COLORS
08dc08dc 683 unregister_colors (pixels, npixels);
a435fc2a 684#endif
513c5806
GM
685 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
686 pixels, npixels, 0);
08dc08dc
GM
687 }
688}
689
690
691/* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
692 color values. Interrupt input must be blocked when this function
693 is called. */
694
695void
696x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
697 Display *dpy;
698 Screen *screen;
699 Colormap cmap;
700 unsigned long *pixels;
701 int npixels;
702{
703 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
704 int class = dpyinfo->visual->class;
705
706 /* If display has an immutable color map, freeing colors is not
707 necessary and some servers don't allow it. So don't do it. */
708 if (class != StaticColor && class != StaticGray && class != TrueColor)
709 {
a435fc2a 710#ifdef DEBUG_X_COLORS
08dc08dc 711 unregister_colors (pixels, npixels);
a435fc2a 712#endif
513c5806 713 XFreeColors (dpy, cmap, pixels, npixels, 0);
1f847cf8
GM
714 }
715}
716
08dc08dc 717
82641697
GM
718/* Create and return a GC for use on frame F. GC values and mask
719 are given by XGCV and MASK. */
720
721static INLINE GC
722x_create_gc (f, mask, xgcv)
cb637678 723 struct frame *f;
82641697
GM
724 unsigned long mask;
725 XGCValues *xgcv;
c115973b
JB
726{
727 GC gc;
82641697
GM
728 BLOCK_INPUT;
729 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
730 UNBLOCK_INPUT;
731 IF_DEBUG (++ngcs);
732 return gc;
733}
c115973b 734
42120bc7 735
82641697
GM
736/* Free GC which was used on frame F. */
737
738static INLINE void
739x_free_gc (f, gc)
740 struct frame *f;
741 GC gc;
742{
660ed669 743 BLOCK_INPUT;
a6d8ba25 744 IF_DEBUG (xassert (--ngcs >= 0));
82641697
GM
745 XFreeGC (FRAME_X_DISPLAY (f), gc);
746 UNBLOCK_INPUT;
747}
660ed669 748
82641697 749#endif /* HAVE_X_WINDOWS */
660ed669 750
c3cee013
JR
751#ifdef WINDOWSNT
752/* W32 emulation of GCs */
753
754static INLINE GC
755x_create_gc (f, mask, xgcv)
756 struct frame *f;
757 unsigned long mask;
758 XGCValues *xgcv;
759{
760 GC gc;
761 BLOCK_INPUT;
762 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
763 UNBLOCK_INPUT;
764 IF_DEBUG (++ngcs);
765 return gc;
766}
767
768
769/* Free GC which was used on frame F. */
770
771static INLINE void
772x_free_gc (f, gc)
773 struct frame *f;
774 GC gc;
775{
776 BLOCK_INPUT;
a6d8ba25 777 IF_DEBUG (xassert (--ngcs >= 0));
c3cee013
JR
778 xfree (gc);
779 UNBLOCK_INPUT;
780}
781
782#endif /* WINDOWSNT */
660ed669 783
e0f712ba
AC
784#ifdef MAC_OS
785/* Mac OS emulation of GCs */
786
787extern XGCValues *XCreateGC (void *, Window, unsigned long, XGCValues *);
788
789static INLINE GC
790x_create_gc (f, mask, xgcv)
791 struct frame *f;
792 unsigned long mask;
793 XGCValues *xgcv;
794{
795 GC gc;
796 gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
797 return gc;
798}
799
800static INLINE void
801x_free_gc (f, gc)
802 struct frame *f;
803 GC gc;
804{
805 XFreeGC (FRAME_MAC_DISPLAY (f), gc);
806}
807
808#endif /* MAC_OS */
809
82641697
GM
810/* Like stricmp. Used to compare parts of font names which are in
811 ISO8859-1. */
812
813int
814xstricmp (s1, s2)
23889aba 815 const unsigned char *s1, *s2;
82641697
GM
816{
817 while (*s1 && *s2)
95887807 818 {
82641697
GM
819 unsigned char c1 = tolower (*s1);
820 unsigned char c2 = tolower (*s2);
821 if (c1 != c2)
822 return c1 < c2 ? -1 : 1;
823 ++s1, ++s2;
95887807 824 }
cd0bb842 825
82641697
GM
826 if (*s1 == 0)
827 return *s2 == 0 ? 0 : -1;
828 return 1;
829}
660ed669 830
660ed669 831
82641697 832/* Like strlwr, which might not always be available. */
42120bc7 833
82641697
GM
834static unsigned char *
835xstrlwr (s)
836 unsigned char *s;
837{
838 unsigned char *p = s;
839
840 for (p = s; *p; ++p)
841 *p = tolower (*p);
842
843 return s;
c115973b 844}
cb637678 845
42120bc7 846
82641697
GM
847/* Signal `error' with message S, and additional argument ARG. */
848
849static void
850signal_error (s, arg)
851 char *s;
852 Lisp_Object arg;
42120bc7 853{
82641697
GM
854 Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
855}
42120bc7 856
82641697 857
c0617987
GM
858/* If FRAME is nil, return a pointer to the selected frame.
859 Otherwise, check that FRAME is a live frame, and return a pointer
860 to it. NPARAM is the parameter number of FRAME, for
861 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
862 Lisp function definitions. */
82641697
GM
863
864static INLINE struct frame *
865frame_or_selected_frame (frame, nparam)
866 Lisp_Object frame;
867 int nparam;
868{
82641697 869 if (NILP (frame))
c0617987 870 frame = selected_frame;
178c5d9c 871
b7826503 872 CHECK_LIVE_FRAME (frame);
c0617987 873 return XFRAME (frame);
42120bc7 874}
82641697 875
42120bc7 876\f
82641697
GM
877/***********************************************************************
878 Frames and faces
879 ***********************************************************************/
cd0bb842 880
82641697 881/* Initialize face cache and basic faces for frame F. */
cb637678 882
82641697
GM
883void
884init_frame_faces (f)
cb637678 885 struct frame *f;
cb637678 886{
82641697
GM
887 /* Make a face cache, if F doesn't have one. */
888 if (FRAME_FACE_CACHE (f) == NULL)
889 FRAME_FACE_CACHE (f) = make_face_cache (f);
178c5d9c 890
c3cee013 891#ifdef HAVE_WINDOW_SYSTEM
82641697 892 /* Make the image cache. */
c3cee013 893 if (FRAME_WINDOW_P (f))
82641697
GM
894 {
895 if (FRAME_X_IMAGE_CACHE (f) == NULL)
896 FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
897 ++FRAME_X_IMAGE_CACHE (f)->refcount;
898 }
c3cee013 899#endif /* HAVE_WINDOW_SYSTEM */
cb637678 900
178c5d9c 901 /* Realize basic faces. Must have enough information in frame
82641697
GM
902 parameters to realize basic faces at this point. */
903#ifdef HAVE_X_WINDOWS
904 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
c3cee013
JR
905#endif
906#ifdef WINDOWSNT
907 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
f00691a3
AC
908#endif
909#ifdef MAC_OS
910 if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
82641697
GM
911#endif
912 if (!realize_basic_faces (f))
913 abort ();
914}
cb637678 915
cb637678 916
82641697 917/* Free face cache of frame F. Called from Fdelete_frame. */
cb637678 918
82641697
GM
919void
920free_frame_faces (f)
cb637678 921 struct frame *f;
cb637678 922{
82641697 923 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
178c5d9c 924
82641697
GM
925 if (face_cache)
926 {
927 free_face_cache (face_cache);
928 FRAME_FACE_CACHE (f) = NULL;
929 }
660ed669 930
c3cee013
JR
931#ifdef HAVE_WINDOW_SYSTEM
932 if (FRAME_WINDOW_P (f))
195f798e 933 {
82641697
GM
934 struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
935 if (image_cache)
195f798e 936 {
82641697
GM
937 --image_cache->refcount;
938 if (image_cache->refcount == 0)
939 free_image_cache (f);
195f798e
RS
940 }
941 }
c3cee013 942#endif /* HAVE_WINDOW_SYSTEM */
cb637678
JB
943}
944
82641697 945
8bd201d6
GM
946/* Clear face caches, and recompute basic faces for frame F. Call
947 this after changing frame parameters on which those faces depend,
948 or when realized faces have been freed due to changing attributes
949 of named faces. */
82641697
GM
950
951void
952recompute_basic_faces (f)
cb637678 953 struct frame *f;
cb637678 954{
82641697
GM
955 if (FRAME_FACE_CACHE (f))
956 {
8bd201d6 957 clear_face_cache (0);
18df9369
GM
958 if (!realize_basic_faces (f))
959 abort ();
82641697
GM
960 }
961}
cb637678 962
cb637678 963
82641697
GM
964/* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
965 try to free unused fonts, too. */
cb637678 966
adfea139 967void
82641697
GM
968clear_face_cache (clear_fonts_p)
969 int clear_fonts_p;
cb637678 970{
c3cee013 971#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
972 Lisp_Object tail, frame;
973 struct frame *f;
828e66d1 974
82641697
GM
975 if (clear_fonts_p
976 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
828e66d1 977 {
d5641fc5 978 struct x_display_info *dpyinfo;
177c0ea7 979
d5641fc5
GM
980 /* Fonts are common for frames on one display, i.e. on
981 one X screen. */
982 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
983 if (dpyinfo->n_fonts > CLEAR_FONT_TABLE_NFONTS)
984 clear_font_table (dpyinfo);
177c0ea7 985
82641697
GM
986 /* From time to time see if we can unload some fonts. This also
987 frees all realized faces on all frames. Fonts needed by
988 faces will be loaded again when faces are realized again. */
989 clear_font_table_count = 0;
195f798e 990
82641697 991 FOR_EACH_FRAME (tail, frame)
195f798e 992 {
d5641fc5 993 struct frame *f = XFRAME (frame);
c3cee013 994 if (FRAME_WINDOW_P (f)
82641697 995 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
d5641fc5 996 free_all_realized_faces (frame);
82641697
GM
997 }
998 }
999 else
1000 {
1001 /* Clear GCs of realized faces. */
1002 FOR_EACH_FRAME (tail, frame)
1003 {
1004 f = XFRAME (frame);
c3cee013 1005 if (FRAME_WINDOW_P (f))
82641697
GM
1006 {
1007 clear_face_gcs (FRAME_FACE_CACHE (f));
1008 clear_image_cache (f, 0);
195f798e
RS
1009 }
1010 }
828e66d1 1011 }
c3cee013 1012#endif /* HAVE_WINDOW_SYSTEM */
cd0bb842
RS
1013}
1014
82641697
GM
1015
1016DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
7ee72033
MB
1017 doc: /* Clear face caches on all frames.
1018Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
1019 (thoroughly)
6a3f48c7 1020 Lisp_Object thoroughly;
cd0bb842 1021{
6a3f48c7 1022 clear_face_cache (!NILP (thoroughly));
ae4b4ba5
GM
1023 ++face_change_count;
1024 ++windows_or_buffers_changed;
82641697
GM
1025 return Qnil;
1026}
1027
1028
1029
c3cee013 1030#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
1031
1032
4cba8c0b
GM
1033/* Remove fonts from the font table of DPYINFO except for the default
1034 ASCII fonts of frames on that display. Called from clear_face_cache
39506348 1035 from time to time. */
82641697
GM
1036
1037static void
d5641fc5
GM
1038clear_font_table (dpyinfo)
1039 struct x_display_info *dpyinfo;
82641697 1040{
82641697
GM
1041 int i;
1042
d5641fc5 1043 /* Free those fonts that are not used by frames on DPYINFO. */
39506348
KH
1044 for (i = 0; i < dpyinfo->n_fonts; ++i)
1045 {
1046 struct font_info *font_info = dpyinfo->font_table + i;
d5641fc5
GM
1047 Lisp_Object tail, frame;
1048
1049 /* Check if slot is already free. */
1050 if (font_info->name == NULL)
1051 continue;
1052
3d90c96c 1053 /* Don't free a default font of some frame. */
d5641fc5
GM
1054 FOR_EACH_FRAME (tail, frame)
1055 {
1056 struct frame *f = XFRAME (frame);
1057 if (FRAME_WINDOW_P (f)
d5641fc5
GM
1058 && font_info->font == FRAME_FONT (f))
1059 break;
1060 }
82641697 1061
d5641fc5 1062 if (!NILP (tail))
39506348 1063 continue;
82641697 1064
39506348
KH
1065 /* Free names. */
1066 if (font_info->full_name != font_info->name)
1067 xfree (font_info->full_name);
1068 xfree (font_info->name);
82641697 1069
39506348
KH
1070 /* Free the font. */
1071 BLOCK_INPUT;
c3cee013 1072#ifdef HAVE_X_WINDOWS
39506348 1073 XFreeFont (dpyinfo->display, font_info->font);
c3cee013
JR
1074#endif
1075#ifdef WINDOWSNT
39506348 1076 w32_unload_font (dpyinfo, font_info->font);
b15325b2
ST
1077#endif
1078#ifdef MAC_OS
1079 mac_unload_font (dpyinfo, font_info->font);
c3cee013 1080#endif
39506348 1081 UNBLOCK_INPUT;
82641697 1082
39506348
KH
1083 /* Mark font table slot free. */
1084 font_info->font = NULL;
1085 font_info->name = font_info->full_name = NULL;
1086 }
82641697
GM
1087}
1088
c3cee013 1089#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
1090
1091
1092\f
1093/***********************************************************************
1094 X Pixmaps
1095 ***********************************************************************/
1096
c3cee013 1097#ifdef HAVE_WINDOW_SYSTEM
82641697 1098
fef04523 1099DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
7ee72033 1100 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
228299fa
GM
1101A bitmap specification is either a string, a file name, or a list
1102\(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
1103HEIGHT is its height, and DATA is a string containing the bits of
1104the pixmap. Bits are stored row by row, each row occupies
7ee72033
MB
1105\(WIDTH + 7)/8 bytes. */)
1106 (object)
82641697
GM
1107 Lisp_Object object;
1108{
c7ae3284 1109 int pixmap_p = 0;
178c5d9c 1110
c7ae3284
GM
1111 if (STRINGP (object))
1112 /* If OBJECT is a string, it's a file name. */
1113 pixmap_p = 1;
1114 else if (CONSP (object))
1115 {
1116 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
1117 HEIGHT must be integers > 0, and DATA must be string large
1118 enough to hold a bitmap of the specified size. */
1119 Lisp_Object width, height, data;
1120
1121 height = width = data = Qnil;
178c5d9c 1122
c7ae3284
GM
1123 if (CONSP (object))
1124 {
1125 width = XCAR (object);
1126 object = XCDR (object);
1127 if (CONSP (object))
1128 {
1129 height = XCAR (object);
1130 object = XCDR (object);
1131 if (CONSP (object))
1132 data = XCAR (object);
1133 }
1134 }
cd0bb842 1135
c7ae3284
GM
1136 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1137 {
1138 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1139 / BITS_PER_CHAR);
d5db4077 1140 if (SBYTES (data) >= bytes_per_row * XINT (height))
c7ae3284
GM
1141 pixmap_p = 1;
1142 }
1143 }
1144
1145 return pixmap_p ? Qt : Qnil;
cd0bb842
RS
1146}
1147
cd0bb842 1148
82641697
GM
1149/* Load a bitmap according to NAME (which is either a file name or a
1150 pixmap spec) for use on frame F. Value is the bitmap_id (see
1151 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1152 bitmap cannot be loaded, display a message saying so, and return
1153 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1154 if these pointers are not null. */
cd0bb842 1155
82641697 1156static int
cd0bb842 1157load_pixmap (f, name, w_ptr, h_ptr)
7812a96f 1158 FRAME_PTR f;
cd0bb842
RS
1159 Lisp_Object name;
1160 unsigned int *w_ptr, *h_ptr;
1161{
1162 int bitmap_id;
1163 Lisp_Object tem;
1164
1165 if (NILP (name))
82641697 1166 return 0;
cd0bb842 1167
fef04523 1168 tem = Fbitmap_spec_p (name);
cd0bb842 1169 if (NILP (tem))
fef04523 1170 wrong_type_argument (Qbitmap_spec_p, name);
cd0bb842
RS
1171
1172 BLOCK_INPUT;
cd0bb842
RS
1173 if (CONSP (name))
1174 {
1175 /* Decode a bitmap spec into a bitmap. */
1176
1177 int h, w;
1178 Lisp_Object bits;
1179
1180 w = XINT (Fcar (name));
1181 h = XINT (Fcar (Fcdr (name)));
1182 bits = Fcar (Fcdr (Fcdr (name)));
1183
d5db4077 1184 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
cd0bb842
RS
1185 w, h);
1186 }
1187 else
1188 {
1189 /* It must be a string -- a file name. */
1190 bitmap_id = x_create_bitmap_from_file (f, name);
1191 }
1192 UNBLOCK_INPUT;
1193
7812a96f 1194 if (bitmap_id < 0)
82641697 1195 {
1b8f7fbc 1196 add_to_log ("Invalid or undefined bitmap %s", name, Qnil);
82641697 1197 bitmap_id = 0;
cd0bb842 1198
82641697
GM
1199 if (w_ptr)
1200 *w_ptr = 0;
1201 if (h_ptr)
1202 *h_ptr = 0;
1203 }
1204 else
1205 {
1206#if GLYPH_DEBUG
1207 ++npixmaps_allocated;
1208#endif
1209 if (w_ptr)
1210 *w_ptr = x_bitmap_width (f, bitmap_id);
1211
1212 if (h_ptr)
1213 *h_ptr = x_bitmap_height (f, bitmap_id);
1214 }
cd0bb842
RS
1215
1216 return bitmap_id;
cb637678 1217}
87485d6f 1218
c3cee013 1219#endif /* HAVE_WINDOW_SYSTEM */
82641697 1220
87485d6f 1221
82641697 1222\f
82641697
GM
1223/***********************************************************************
1224 Fonts
1225 ***********************************************************************/
1226
c3cee013 1227#ifdef HAVE_WINDOW_SYSTEM
82641697 1228
39506348
KH
1229/* Load font of face FACE which is used on frame F to display
1230 character C. The name of the font to load is determined by lface
1231 and fontset of FACE. */
82641697
GM
1232
1233static void
39506348 1234load_face_font (f, face, c)
82641697
GM
1235 struct frame *f;
1236 struct face *face;
39506348 1237 int c;
87485d6f 1238{
82641697 1239 struct font_info *font_info = NULL;
39506348 1240 char *font_name;
6ae2cd57 1241 int needs_overstrike;
178c5d9c 1242
82641697 1243 face->font_info_id = -1;
82641697 1244 face->font = NULL;
39506348 1245
6ae2cd57
MB
1246 font_name = choose_face_font (f, face->lface, face->fontset, c,
1247 &needs_overstrike);
39506348
KH
1248 if (!font_name)
1249 return;
1250
82641697 1251 BLOCK_INPUT;
39506348 1252 font_info = FS_LOAD_FACE_FONT (f, c, font_name, face);
82641697
GM
1253 UNBLOCK_INPUT;
1254
1255 if (font_info)
1256 {
39506348 1257 face->font_info_id = font_info->font_idx;
82641697
GM
1258 face->font = font_info->font;
1259 face->font_name = font_info->full_name;
6ae2cd57 1260 face->overstrike = needs_overstrike;
39506348 1261 if (face->gc)
82641697 1262 {
39506348
KH
1263 x_free_gc (f, face->gc);
1264 face->gc = 0;
82641697
GM
1265 }
1266 }
39506348 1267 else
1b8f7fbc 1268 add_to_log ("Unable to load font %s",
7dbdfcf8 1269 build_string (font_name), Qnil);
39506348 1270 xfree (font_name);
87485d6f
MW
1271}
1272
c3cee013 1273#endif /* HAVE_WINDOW_SYSTEM */
87485d6f 1274
87485d6f 1275
82641697
GM
1276\f
1277/***********************************************************************
1278 X Colors
1279 ***********************************************************************/
1280
b35df831
MB
1281/* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1282 RGB_LIST should contain (at least) 3 lisp integers.
1283 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1284
1285static int
1286parse_rgb_list (rgb_list, color)
1287 Lisp_Object rgb_list;
1288 XColor *color;
1289{
1290#define PARSE_RGB_LIST_FIELD(field) \
1291 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1292 { \
1293 color->field = XINT (XCAR (rgb_list)); \
1294 rgb_list = XCDR (rgb_list); \
1295 } \
1296 else \
1297 return 0;
1298
1299 PARSE_RGB_LIST_FIELD (red);
1300 PARSE_RGB_LIST_FIELD (green);
1301 PARSE_RGB_LIST_FIELD (blue);
1302
1303 return 1;
1304}
1305
1306
1307/* Lookup on frame F the color described by the lisp string COLOR.
1308 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1309 non-zero, then the `standard' definition of the same color is
1310 returned in it. */
1311
1312static int
1313tty_lookup_color (f, color, tty_color, std_color)
1314 struct frame *f;
1315 Lisp_Object color;
1316 XColor *tty_color, *std_color;
1317{
1318 Lisp_Object frame, color_desc;
1319
1320 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1321 return 0;
1322
1323 XSETFRAME (frame, f);
1324
1325 color_desc = call2 (Qtty_color_desc, color, frame);
1326 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1327 {
1328 Lisp_Object rgb;
1329
1330 if (! INTEGERP (XCAR (XCDR (color_desc))))
1331 return 0;
1332
1333 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1334
1335 rgb = XCDR (XCDR (color_desc));
1336 if (! parse_rgb_list (rgb, tty_color))
1337 return 0;
1338
1339 /* Should we fill in STD_COLOR too? */
1340 if (std_color)
1341 {
1342 /* Default STD_COLOR to the same as TTY_COLOR. */
1343 *std_color = *tty_color;
1344
1345 /* Do a quick check to see if the returned descriptor is
1346 actually _exactly_ equal to COLOR, otherwise we have to
1347 lookup STD_COLOR separately. If it's impossible to lookup
1348 a standard color, we just give up and use TTY_COLOR. */
1349 if ((!STRINGP (XCAR (color_desc))
1350 || NILP (Fstring_equal (color, XCAR (color_desc))))
51f86bfc 1351 && !NILP (Ffboundp (Qtty_color_standard_values)))
b35df831
MB
1352 {
1353 /* Look up STD_COLOR separately. */
1354 rgb = call1 (Qtty_color_standard_values, color);
1355 if (! parse_rgb_list (rgb, std_color))
1356 return 0;
1357 }
1358 }
1359
1360 return 1;
1361 }
1362 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1363 /* We were called early during startup, and the colors are not
1364 yet set up in tty-defined-color-alist. Don't return a failure
1365 indication, since this produces the annoying "Unable to
1366 load color" messages in the *Messages* buffer. */
1367 return 1;
1368 else
1369 /* tty-color-desc seems to have returned a bad value. */
1370 return 0;
1371}
1372
2d764c78 1373/* A version of defined_color for non-X frames. */
08dc08dc 1374
2d764c78
EZ
1375int
1376tty_defined_color (f, color_name, color_def, alloc)
1377 struct frame *f;
1378 char *color_name;
1379 XColor *color_def;
1380 int alloc;
1381{
2d764c78
EZ
1382 int status = 1;
1383
b35df831 1384 /* Defaults. */
177c0ea7 1385 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
b35df831
MB
1386 color_def->red = 0;
1387 color_def->blue = 0;
1388 color_def->green = 0;
a61c12d5 1389
b35df831
MB
1390 if (*color_name)
1391 status = tty_lookup_color (f, build_string (color_name), color_def, 0);
1392
1393 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
f9d2fdc4
EZ
1394 {
1395 if (strcmp (color_name, "unspecified-fg") == 0)
b35df831 1396 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
f9d2fdc4 1397 else if (strcmp (color_name, "unspecified-bg") == 0)
b35df831 1398 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
f9d2fdc4
EZ
1399 }
1400
b35df831 1401 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
3b451f74
EZ
1402 status = 1;
1403
2d764c78
EZ
1404 return status;
1405}
1406
08dc08dc
GM
1407
1408/* Decide if color named COLOR_NAME is valid for the display
1409 associated with the frame F; if so, return the rgb values in
1410 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
2d764c78
EZ
1411
1412 This does the right thing for any type of frame. */
08dc08dc 1413
2d764c78
EZ
1414int
1415defined_color (f, color_name, color_def, alloc)
1416 struct frame *f;
1417 char *color_name;
1418 XColor *color_def;
1419 int alloc;
1420{
1421 if (!FRAME_WINDOW_P (f))
1422 return tty_defined_color (f, color_name, color_def, alloc);
82641697 1423#ifdef HAVE_X_WINDOWS
2d764c78
EZ
1424 else if (FRAME_X_P (f))
1425 return x_defined_color (f, color_name, color_def, alloc);
1426#endif
1427#ifdef WINDOWSNT
1428 else if (FRAME_W32_P (f))
2d764c78
EZ
1429 return w32_defined_color (f, color_name, color_def, alloc);
1430#endif
e0f712ba 1431#ifdef MAC_OS
2d764c78 1432 else if (FRAME_MAC_P (f))
2d764c78
EZ
1433 return mac_defined_color (f, color_name, color_def, alloc);
1434#endif
1435 else
1436 abort ();
1437}
1438
08dc08dc
GM
1439
1440/* Given the index IDX of a tty color on frame F, return its name, a
1441 Lisp string. */
2d764c78
EZ
1442
1443Lisp_Object
1444tty_color_name (f, idx)
1445 struct frame *f;
1446 int idx;
1447{
2d764c78
EZ
1448 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1449 {
a61c12d5
EZ
1450 Lisp_Object frame;
1451 Lisp_Object coldesc;
1452
1453 XSETFRAME (frame, f);
1454 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
2d764c78
EZ
1455
1456 if (!NILP (coldesc))
1457 return XCAR (coldesc);
1458 }
1459#ifdef MSDOS
1460 /* We can have an MSDOG frame under -nw for a short window of
1461 opportunity before internal_terminal_init is called. DTRT. */
1462 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1463 return msdos_stdcolor_name (idx);
1464#endif
1465
ef917393
EZ
1466 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1467 return build_string (unspecified_fg);
1468 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1469 return build_string (unspecified_bg);
c3cee013
JR
1470
1471#ifdef WINDOWSNT
1472 return vga_stdcolor_name (idx);
1473#endif
1474
ef917393 1475 return Qunspecified;
2d764c78 1476}
82641697 1477
08dc08dc 1478
82641697
GM
1479/* Return non-zero if COLOR_NAME is a shade of gray (or white or
1480 black) on frame F. The algorithm is taken from 20.2 faces.el. */
1481
1482static int
1483face_color_gray_p (f, color_name)
1484 struct frame *f;
1485 char *color_name;
1486{
1487 XColor color;
1488 int gray_p;
1489
1490 if (defined_color (f, color_name, &color, 0))
1491 gray_p = ((abs (color.red - color.green)
1492 < max (color.red, color.green) / 20)
1493 && (abs (color.green - color.blue)
1494 < max (color.green, color.blue) / 20)
1495 && (abs (color.blue - color.red)
1496 < max (color.blue, color.red) / 20));
87485d6f 1497 else
82641697 1498 gray_p = 0;
178c5d9c 1499
82641697 1500 return gray_p;
87485d6f 1501}
87485d6f 1502
cb637678 1503
82641697
GM
1504/* Return non-zero if color COLOR_NAME can be displayed on frame F.
1505 BACKGROUND_P non-zero means the color will be used as background
1506 color. */
1507
1508static int
1509face_color_supported_p (f, color_name, background_p)
1510 struct frame *f;
1511 char *color_name;
1512 int background_p;
1513{
1514 Lisp_Object frame;
2d764c78 1515 XColor not_used;
82641697
GM
1516
1517 XSETFRAME (frame, f);
066a4c6b 1518 return
6a46b96b 1519#ifdef HAVE_WINDOW_SYSTEM
066a4c6b
SM
1520 FRAME_WINDOW_P (f)
1521 ? (!NILP (Fxw_display_color_p (frame))
1522 || xstricmp (color_name, "black") == 0
1523 || xstricmp (color_name, "white") == 0
1524 || (background_p
1525 && face_color_gray_p (f, color_name))
1526 || (!NILP (Fx_display_grayscale_p (frame))
1527 && face_color_gray_p (f, color_name)))
1528 :
1529#endif
1530 tty_defined_color (f, color_name, &not_used, 0);
2d764c78 1531}
82641697
GM
1532
1533
da47150d 1534DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
7ee72033 1535 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
228299fa 1536FRAME specifies the frame and thus the display for interpreting COLOR.
7ee72033
MB
1537If FRAME is nil or omitted, use the selected frame. */)
1538 (color, frame)
82641697 1539 Lisp_Object color, frame;
cb637678 1540{
2d764c78
EZ
1541 struct frame *f;
1542
b7826503 1543 CHECK_STRING (color);
2d176448
TTN
1544 if (NILP (frame))
1545 frame = selected_frame;
1546 else
1547 CHECK_FRAME (frame);
2d764c78 1548 f = XFRAME (frame);
d5db4077 1549 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
82641697 1550}
660ed669 1551
fffc2367 1552
da47150d 1553DEFUN ("color-supported-p", Fcolor_supported_p,
2e1bb1c3 1554 Scolor_supported_p, 1, 3, 0,
7ee72033 1555 doc: /* Return non-nil if COLOR can be displayed on FRAME.
228299fa 1556BACKGROUND-P non-nil means COLOR is used as a background.
fc820cc5 1557Otherwise, this function tells whether it can be used as a foreground.
228299fa 1558If FRAME is nil or omitted, use the selected frame.
7ee72033
MB
1559COLOR must be a valid color name. */)
1560 (color, frame, background_p)
82641697
GM
1561 Lisp_Object frame, color, background_p;
1562{
2d764c78
EZ
1563 struct frame *f;
1564
b7826503 1565 CHECK_STRING (color);
2d176448
TTN
1566 if (NILP (frame))
1567 frame = selected_frame;
1568 else
1569 CHECK_FRAME (frame);
2d764c78 1570 f = XFRAME (frame);
d5db4077 1571 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
82641697
GM
1572 return Qt;
1573 return Qnil;
1574}
7b37f67b 1575
08dc08dc 1576
82641697
GM
1577/* Load color with name NAME for use by face FACE on frame F.
1578 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1579 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1580 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1581 pixel color. If color cannot be loaded, display a message, and
1582 return the foreground, background or underline color of F, but
1583 record that fact in flags of the face so that we don't try to free
1584 these colors. */
1585
44747bd0 1586unsigned long
82641697
GM
1587load_color (f, face, name, target_index)
1588 struct frame *f;
1589 struct face *face;
1590 Lisp_Object name;
1591 enum lface_attribute_index target_index;
1592{
1593 XColor color;
178c5d9c 1594
82641697
GM
1595 xassert (STRINGP (name));
1596 xassert (target_index == LFACE_FOREGROUND_INDEX
1597 || target_index == LFACE_BACKGROUND_INDEX
1598 || target_index == LFACE_UNDERLINE_INDEX
1599 || target_index == LFACE_OVERLINE_INDEX
1600 || target_index == LFACE_STRIKE_THROUGH_INDEX
1601 || target_index == LFACE_BOX_INDEX);
178c5d9c 1602
82641697
GM
1603 /* if the color map is full, defined_color will return a best match
1604 to the values in an existing cell. */
d5db4077 1605 if (!defined_color (f, SDATA (name), &color, 1))
82641697 1606 {
2d764c78 1607 add_to_log ("Unable to load color \"%s\"", name, Qnil);
178c5d9c 1608
82641697 1609 switch (target_index)
1120eb5e 1610 {
82641697
GM
1611 case LFACE_FOREGROUND_INDEX:
1612 face->foreground_defaulted_p = 1;
1613 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1614 break;
178c5d9c 1615
82641697
GM
1616 case LFACE_BACKGROUND_INDEX:
1617 face->background_defaulted_p = 1;
1618 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1619 break;
178c5d9c 1620
82641697
GM
1621 case LFACE_UNDERLINE_INDEX:
1622 face->underline_defaulted_p = 1;
1623 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1624 break;
178c5d9c 1625
82641697
GM
1626 case LFACE_OVERLINE_INDEX:
1627 face->overline_color_defaulted_p = 1;
1628 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1120eb5e 1629 break;
178c5d9c 1630
82641697
GM
1631 case LFACE_STRIKE_THROUGH_INDEX:
1632 face->strike_through_color_defaulted_p = 1;
1633 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1634 break;
178c5d9c 1635
82641697
GM
1636 case LFACE_BOX_INDEX:
1637 face->box_color_defaulted_p = 1;
1638 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1639 break;
1640
1641 default:
1642 abort ();
1120eb5e 1643 }
82641697
GM
1644 }
1645#if GLYPH_DEBUG
1646 else
1647 ++ncolors_allocated;
1648#endif
178c5d9c 1649
82641697
GM
1650 return color.pixel;
1651}
1120eb5e 1652
08dc08dc 1653
c3cee013 1654#ifdef HAVE_WINDOW_SYSTEM
1120eb5e 1655
82641697
GM
1656/* Load colors for face FACE which is used on frame F. Colors are
1657 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1658 of ATTRS. If the background color specified is not supported on F,
1659 try to emulate gray colors with a stipple from Vface_default_stipple. */
1660
1661static void
1662load_face_colors (f, face, attrs)
1663 struct frame *f;
1664 struct face *face;
1665 Lisp_Object *attrs;
1666{
1667 Lisp_Object fg, bg;
1668
1669 bg = attrs[LFACE_BACKGROUND_INDEX];
1670 fg = attrs[LFACE_FOREGROUND_INDEX];
1671
1672 /* Swap colors if face is inverse-video. */
1673 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1674 {
1675 Lisp_Object tmp;
1676 tmp = fg;
1677 fg = bg;
1678 bg = tmp;
1679 }
1680
1681 /* Check for support for foreground, not for background because
1682 face_color_supported_p is smart enough to know that grays are
1683 "supported" as background because we are supposed to use stipple
1684 for them. */
d5db4077 1685 if (!face_color_supported_p (f, SDATA (bg), 0)
fef04523 1686 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
82641697
GM
1687 {
1688 x_destroy_bitmap (f, face->stipple);
1689 face->stipple = load_pixmap (f, Vface_default_stipple,
1690 &face->pixmap_w, &face->pixmap_h);
1691 }
82641697 1692
be8a72f4 1693 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
82641697 1694 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
cb637678
JB
1695}
1696
660ed669 1697
82641697 1698/* Free color PIXEL on frame F. */
cd0bb842 1699
cb637678 1700void
82641697 1701unload_color (f, pixel)
cb637678 1702 struct frame *f;
82641697 1703 unsigned long pixel;
cb637678 1704{
c3cee013 1705#ifdef HAVE_X_WINDOWS
30a7ac22
GM
1706 if (pixel != -1)
1707 {
1708 BLOCK_INPUT;
1709 x_free_colors (f, &pixel, 1);
1710 UNBLOCK_INPUT;
1711 }
c3cee013 1712#endif
82641697
GM
1713}
1714
1715
1716/* Free colors allocated for FACE. */
1717
1718static void
1719free_face_colors (f, face)
1720 struct frame *f;
1721 struct face *face;
1722{
c3cee013 1723#ifdef HAVE_X_WINDOWS
28a072fe
GM
1724 if (face->colors_copied_bitwise_p)
1725 return;
1726
08dc08dc 1727 BLOCK_INPUT;
178c5d9c 1728
08dc08dc
GM
1729 if (!face->foreground_defaulted_p)
1730 {
1731 x_free_colors (f, &face->foreground, 1);
1732 IF_DEBUG (--ncolors_allocated);
1733 }
178c5d9c 1734
08dc08dc
GM
1735 if (!face->background_defaulted_p)
1736 {
1737 x_free_colors (f, &face->background, 1);
1738 IF_DEBUG (--ncolors_allocated);
1739 }
82641697 1740
08dc08dc
GM
1741 if (face->underline_p
1742 && !face->underline_defaulted_p)
1743 {
1744 x_free_colors (f, &face->underline_color, 1);
1745 IF_DEBUG (--ncolors_allocated);
1746 }
82641697 1747
08dc08dc
GM
1748 if (face->overline_p
1749 && !face->overline_color_defaulted_p)
1750 {
1751 x_free_colors (f, &face->overline_color, 1);
1752 IF_DEBUG (--ncolors_allocated);
1753 }
82641697 1754
08dc08dc
GM
1755 if (face->strike_through_p
1756 && !face->strike_through_color_defaulted_p)
1757 {
1758 x_free_colors (f, &face->strike_through_color, 1);
1759 IF_DEBUG (--ncolors_allocated);
1760 }
82641697 1761
08dc08dc
GM
1762 if (face->box != FACE_NO_BOX
1763 && !face->box_color_defaulted_p)
1764 {
1765 x_free_colors (f, &face->box_color, 1);
1766 IF_DEBUG (--ncolors_allocated);
82641697 1767 }
08dc08dc
GM
1768
1769 UNBLOCK_INPUT;
2d764c78 1770#endif /* HAVE_X_WINDOWS */
c3cee013 1771}
08dc08dc 1772
c3cee013 1773#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
1774
1775
1776\f
1777/***********************************************************************
1778 XLFD Font Names
1779 ***********************************************************************/
1780
1781/* An enumerator for each field of an XLFD font name. */
1782
1783enum xlfd_field
1784{
1785 XLFD_FOUNDRY,
1786 XLFD_FAMILY,
1787 XLFD_WEIGHT,
1788 XLFD_SLANT,
1789 XLFD_SWIDTH,
1790 XLFD_ADSTYLE,
1791 XLFD_PIXEL_SIZE,
1792 XLFD_POINT_SIZE,
1793 XLFD_RESX,
1794 XLFD_RESY,
1795 XLFD_SPACING,
1796 XLFD_AVGWIDTH,
1797 XLFD_REGISTRY,
1798 XLFD_ENCODING,
1799 XLFD_LAST
1800};
1801
178c5d9c 1802/* An enumerator for each possible slant value of a font. Taken from
82641697
GM
1803 the XLFD specification. */
1804
1805enum xlfd_slant
1806{
1807 XLFD_SLANT_UNKNOWN,
1808 XLFD_SLANT_ROMAN,
1809 XLFD_SLANT_ITALIC,
1810 XLFD_SLANT_OBLIQUE,
1811 XLFD_SLANT_REVERSE_ITALIC,
1812 XLFD_SLANT_REVERSE_OBLIQUE,
1813 XLFD_SLANT_OTHER
1814};
1815
1816/* Relative font weight according to XLFD documentation. */
1817
1818enum xlfd_weight
1819{
1820 XLFD_WEIGHT_UNKNOWN,
1821 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1822 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1823 XLFD_WEIGHT_LIGHT, /* 30 */
1824 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1825 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1826 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1827 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1828 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1829 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1830};
1831
1832/* Relative proportionate width. */
1833
1834enum xlfd_swidth
1835{
1836 XLFD_SWIDTH_UNKNOWN,
1837 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1838 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1839 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1840 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1841 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1842 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1843 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1844 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1845 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1846};
1847
1848/* Structure used for tables mapping XLFD weight, slant, and width
1849 names to numeric and symbolic values. */
1850
1851struct table_entry
1852{
1853 char *name;
1854 int numeric;
1855 Lisp_Object *symbol;
1856};
1857
1858/* Table of XLFD slant names and their numeric and symbolic
1859 representations. This table must be sorted by slant names in
1860 ascending order. */
1861
1862static struct table_entry slant_table[] =
1863{
1864 {"i", XLFD_SLANT_ITALIC, &Qitalic},
1865 {"o", XLFD_SLANT_OBLIQUE, &Qoblique},
1866 {"ot", XLFD_SLANT_OTHER, &Qitalic},
1867 {"r", XLFD_SLANT_ROMAN, &Qnormal},
1868 {"ri", XLFD_SLANT_REVERSE_ITALIC, &Qreverse_italic},
1869 {"ro", XLFD_SLANT_REVERSE_OBLIQUE, &Qreverse_oblique}
1870};
1871
1872/* Table of XLFD weight names. This table must be sorted by weight
1873 names in ascending order. */
1874
1875static struct table_entry weight_table[] =
1876{
1877 {"black", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold},
1878 {"bold", XLFD_WEIGHT_BOLD, &Qbold},
1879 {"book", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
a288d0d1 1880 {"demi", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
82641697
GM
1881 {"demibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1882 {"extralight", XLFD_WEIGHT_EXTRA_LIGHT, &Qextra_light},
1883 {"extrabold", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1884 {"heavy", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1885 {"light", XLFD_WEIGHT_LIGHT, &Qlight},
1886 {"medium", XLFD_WEIGHT_MEDIUM, &Qnormal},
1887 {"normal", XLFD_WEIGHT_MEDIUM, &Qnormal},
1888 {"regular", XLFD_WEIGHT_MEDIUM, &Qnormal},
1889 {"semibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1890 {"semilight", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1891 {"ultralight", XLFD_WEIGHT_ULTRA_LIGHT, &Qultra_light},
1892 {"ultrabold", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold}
1893};
1894
1895/* Table of XLFD width names. This table must be sorted by width
1896 names in ascending order. */
1897
1898static struct table_entry swidth_table[] =
1899{
1900 {"compressed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1901 {"condensed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1902 {"demiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1903 {"expanded", XLFD_SWIDTH_EXPANDED, &Qexpanded},
1904 {"extracondensed", XLFD_SWIDTH_EXTRA_CONDENSED, &Qextra_condensed},
1905 {"extraexpanded", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded},
1906 {"medium", XLFD_SWIDTH_MEDIUM, &Qnormal},
1907 {"narrow", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1908 {"normal", XLFD_SWIDTH_MEDIUM, &Qnormal},
1909 {"regular", XLFD_SWIDTH_MEDIUM, &Qnormal},
1910 {"semicondensed", XLFD_SWIDTH_SEMI_CONDENSED, &Qsemi_condensed},
1911 {"semiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1912 {"ultracondensed", XLFD_SWIDTH_ULTRA_CONDENSED, &Qultra_condensed},
1913 {"ultraexpanded", XLFD_SWIDTH_ULTRA_EXPANDED, &Qultra_expanded},
1914 {"wide", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded}
1915};
1916
1917/* Structure used to hold the result of splitting font names in XLFD
1918 format into their fields. */
1919
1920struct font_name
1921{
1922 /* The original name which is modified destructively by
1923 split_font_name. The pointer is kept here to be able to free it
1924 if it was allocated from the heap. */
1925 char *name;
1926
1927 /* Font name fields. Each vector element points into `name' above.
1928 Fields are NUL-terminated. */
1929 char *fields[XLFD_LAST];
1930
1931 /* Numeric values for those fields that interest us. See
1932 split_font_name for which these are. */
1933 int numeric[XLFD_LAST];
8e1b21a7 1934
f70400f2
KH
1935 /* If the original name matches one of Vface_font_rescale_alist,
1936 the value is the corresponding rescale ratio. Otherwise, the
1937 value is 1.0. */
1938 double rescale_ratio;
1939
8e1b21a7
KH
1940 /* Lower value mean higher priority. */
1941 int registry_priority;
82641697
GM
1942};
1943
1944/* The frame in effect when sorting font names. Set temporarily in
1945 sort_fonts so that it is available in font comparison functions. */
1946
1947static struct frame *font_frame;
1948
1949/* Order by which font selection chooses fonts. The default values
1950 mean `first, find a best match for the font width, then for the
1951 font height, then for weight, then for slant.' This variable can be
1952 set via set-face-font-sort-order. */
1953
e0f712ba 1954#ifdef MAC_OS
a08332c0
GM
1955static int font_sort_order[4] = {
1956 XLFD_SWIDTH, XLFD_POINT_SIZE, XLFD_WEIGHT, XLFD_SLANT
1957};
1a578e9b 1958#else
82641697 1959static int font_sort_order[4];
1a578e9b 1960#endif
82641697
GM
1961
1962/* Look up FONT.fields[FIELD_INDEX] in TABLE which has DIM entries.
1963 TABLE must be sorted by TABLE[i]->name in ascending order. Value
1964 is a pointer to the matching table entry or null if no table entry
1965 matches. */
1966
1967static struct table_entry *
1968xlfd_lookup_field_contents (table, dim, font, field_index)
1969 struct table_entry *table;
1970 int dim;
1971 struct font_name *font;
1972 int field_index;
1973{
1974 /* Function split_font_name converts fields to lower-case, so there
1975 is no need to use xstrlwr or xstricmp here. */
1976 char *s = font->fields[field_index];
1977 int low, mid, high, cmp;
1978
1979 low = 0;
1980 high = dim - 1;
1981
1982 while (low <= high)
1983 {
1984 mid = (low + high) / 2;
1985 cmp = strcmp (table[mid].name, s);
178c5d9c 1986
82641697
GM
1987 if (cmp < 0)
1988 low = mid + 1;
1989 else if (cmp > 0)
1990 high = mid - 1;
1991 else
1992 return table + mid;
1993 }
1994
1995 return NULL;
1996}
1997
1998
1999/* Return a numeric representation for font name field
2000 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2001 has DIM entries. Value is the numeric value found or DFLT if no
2002 table entry matches. This function is used to translate weight,
2003 slant, and swidth names of XLFD font names to numeric values. */
2004
2005static INLINE int
2006xlfd_numeric_value (table, dim, font, field_index, dflt)
2007 struct table_entry *table;
2008 int dim;
2009 struct font_name *font;
2010 int field_index;
2011 int dflt;
2012{
2013 struct table_entry *p;
2014 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2015 return p ? p->numeric : dflt;
2016}
2017
2018
2019/* Return a symbolic representation for font name field
2020 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2021 has DIM entries. Value is the symbolic value found or DFLT if no
2022 table entry matches. This function is used to translate weight,
2023 slant, and swidth names of XLFD font names to symbols. */
2024
2025static INLINE Lisp_Object
2026xlfd_symbolic_value (table, dim, font, field_index, dflt)
2027 struct table_entry *table;
2028 int dim;
2029 struct font_name *font;
2030 int field_index;
6fc556fd 2031 Lisp_Object dflt;
82641697
GM
2032{
2033 struct table_entry *p;
2034 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2035 return p ? *p->symbol : dflt;
2036}
2037
2038
2039/* Return a numeric value for the slant of the font given by FONT. */
2040
2041static INLINE int
2042xlfd_numeric_slant (font)
2043 struct font_name *font;
2044{
2045 return xlfd_numeric_value (slant_table, DIM (slant_table),
2046 font, XLFD_SLANT, XLFD_SLANT_ROMAN);
2047}
2048
2049
2050/* Return a symbol representing the weight of the font given by FONT. */
2051
2052static INLINE Lisp_Object
2053xlfd_symbolic_slant (font)
2054 struct font_name *font;
2055{
2056 return xlfd_symbolic_value (slant_table, DIM (slant_table),
2057 font, XLFD_SLANT, Qnormal);
2058}
2059
2060
2061/* Return a numeric value for the weight of the font given by FONT. */
2062
2063static INLINE int
2064xlfd_numeric_weight (font)
2065 struct font_name *font;
2066{
2067 return xlfd_numeric_value (weight_table, DIM (weight_table),
2068 font, XLFD_WEIGHT, XLFD_WEIGHT_MEDIUM);
2069}
2070
2071
2072/* Return a symbol representing the slant of the font given by FONT. */
2073
2074static INLINE Lisp_Object
2075xlfd_symbolic_weight (font)
2076 struct font_name *font;
2077{
2078 return xlfd_symbolic_value (weight_table, DIM (weight_table),
2079 font, XLFD_WEIGHT, Qnormal);
2080}
2081
2082
2083/* Return a numeric value for the swidth of the font whose XLFD font
2084 name fields are found in FONT. */
2085
2086static INLINE int
2087xlfd_numeric_swidth (font)
2088 struct font_name *font;
2089{
2090 return xlfd_numeric_value (swidth_table, DIM (swidth_table),
2091 font, XLFD_SWIDTH, XLFD_SWIDTH_MEDIUM);
2092}
2093
2094
2095/* Return a symbolic value for the swidth of FONT. */
2096
2097static INLINE Lisp_Object
2098xlfd_symbolic_swidth (font)
2099 struct font_name *font;
2100{
2101 return xlfd_symbolic_value (swidth_table, DIM (swidth_table),
2102 font, XLFD_SWIDTH, Qnormal);
2103}
178c5d9c 2104
82641697
GM
2105
2106/* Look up the entry of SYMBOL in the vector TABLE which has DIM
2107 entries. Value is a pointer to the matching table entry or null if
2108 no element of TABLE contains SYMBOL. */
2109
2110static struct table_entry *
2111face_value (table, dim, symbol)
2112 struct table_entry *table;
2113 int dim;
2114 Lisp_Object symbol;
2115{
2116 int i;
2117
2118 xassert (SYMBOLP (symbol));
178c5d9c 2119
82641697
GM
2120 for (i = 0; i < dim; ++i)
2121 if (EQ (*table[i].symbol, symbol))
2122 break;
2123
2124 return i < dim ? table + i : NULL;
2125}
2126
2127
2128/* Return a numeric value for SYMBOL in the vector TABLE which has DIM
2129 entries. Value is -1 if SYMBOL is not found in TABLE. */
2130
2131static INLINE int
2132face_numeric_value (table, dim, symbol)
2133 struct table_entry *table;
2134 int dim;
2135 Lisp_Object symbol;
2136{
2137 struct table_entry *p = face_value (table, dim, symbol);
2138 return p ? p->numeric : -1;
2139}
2140
2141
2142/* Return a numeric value representing the weight specified by Lisp
2143 symbol WEIGHT. Value is one of the enumerators of enum
2144 xlfd_weight. */
2145
2146static INLINE int
2147face_numeric_weight (weight)
2148 Lisp_Object weight;
2149{
2150 return face_numeric_value (weight_table, DIM (weight_table), weight);
2151}
2152
2153
2154/* Return a numeric value representing the slant specified by Lisp
2155 symbol SLANT. Value is one of the enumerators of enum xlfd_slant. */
2156
2157static INLINE int
2158face_numeric_slant (slant)
2159 Lisp_Object slant;
2160{
2161 return face_numeric_value (slant_table, DIM (slant_table), slant);
2162}
2163
2164
2165/* Return a numeric value representing the swidth specified by Lisp
2166 symbol WIDTH. Value is one of the enumerators of enum xlfd_swidth. */
2167
2168static int
2169face_numeric_swidth (width)
2170 Lisp_Object width;
2171{
2172 return face_numeric_value (swidth_table, DIM (swidth_table), width);
2173}
2174
2175
c3cee013 2176#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
2177
2178/* Return non-zero if FONT is the name of a fixed-pitch font. */
2179
2180static INLINE int
2181xlfd_fixed_p (font)
2182 struct font_name *font;
2183{
2184 /* Function split_font_name converts fields to lower-case, so there
2185 is no need to use tolower here. */
2186 return *font->fields[XLFD_SPACING] != 'p';
2187}
2188
2189
2190/* Return the point size of FONT on frame F, measured in 1/10 pt.
2191
2192 The actual height of the font when displayed on F depends on the
2193 resolution of both the font and frame. For example, a 10pt font
2194 designed for a 100dpi display will display larger than 10pt on a
2195 75dpi display. (It's not unusual to use fonts not designed for the
2196 display one is using. For example, some intlfonts are available in
2197 72dpi versions, only.)
2198
2199 Value is the real point size of FONT on frame F, or 0 if it cannot
2200 be determined. */
2201
2202static INLINE int
2203xlfd_point_size (f, font)
2204 struct frame *f;
2205 struct font_name *font;
2206{
2207 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
933b0249
GM
2208 char *pixel_field = font->fields[XLFD_PIXEL_SIZE];
2209 double pixel;
82641697
GM
2210 int real_pt;
2211
933b0249
GM
2212 if (*pixel_field == '[')
2213 {
2214 /* The pixel size field is `[A B C D]' which specifies
2215 a transformation matrix.
2216
2217 A B 0
2218 C D 0
2219 0 0 1
2220
2221 by which all glyphs of the font are transformed. The spec
2222 says that s scalar value N for the pixel size is equivalent
2223 to A = N * resx/resy, B = C = 0, D = N. */
2224 char *start = pixel_field + 1, *end;
2225 double matrix[4];
2226 int i;
2227
2228 for (i = 0; i < 4; ++i)
2229 {
2230 matrix[i] = strtod (start, &end);
2231 start = end;
2232 }
2233
7eb4b061 2234 pixel = matrix[3];
933b0249
GM
2235 }
2236 else
2237 pixel = atoi (pixel_field);
177c0ea7 2238
933b0249 2239 if (pixel == 0)
82641697
GM
2240 real_pt = 0;
2241 else
933b0249 2242 real_pt = PT_PER_INCH * 10.0 * pixel / resy + 0.5;
82641697
GM
2243
2244 return real_pt;
2245}
2246
2247
39506348
KH
2248/* Return point size of PIXEL dots while considering Y-resultion (DPI)
2249 of frame F. This function is used to guess a point size of font
2250 when only the pixel height of the font is available. */
2251
2252static INLINE int
2253pixel_point_size (f, pixel)
2254 struct frame *f;
2255 int pixel;
2256{
2257 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2258 double real_pt;
2259 int int_pt;
2260
c660ce4e
GM
2261 /* As one inch is PT_PER_INCH points, PT_PER_INCH/RESY gives the
2262 point size of one dot. */
2263 real_pt = pixel * PT_PER_INCH / resy;
39506348
KH
2264 int_pt = real_pt + 0.5;
2265
2266 return int_pt;
2267}
2268
2269
f70400f2
KH
2270/* Return a rescaling ratio of a font of NAME. */
2271
2272static double
f0d62075
DL
2273font_rescale_ratio (name)
2274 char *name;
f70400f2 2275{
2d176448 2276 Lisp_Object tail, elt;
f70400f2
KH
2277
2278 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2279 {
2280 elt = XCAR (tail);
2281 if (STRINGP (XCAR (elt)) && FLOATP (XCDR (elt))
2282 && fast_c_string_match_ignore_case (XCAR (elt), name) >= 0)
2283 return XFLOAT_DATA (XCDR (elt));
2284 }
2285 return 1.0;
2286}
2287
2288
82641697
GM
2289/* Split XLFD font name FONT->name destructively into NUL-terminated,
2290 lower-case fields in FONT->fields. NUMERIC_P non-zero means
2291 compute numeric values for fields XLFD_POINT_SIZE, XLFD_SWIDTH,
2292 XLFD_RESY, XLFD_SLANT, and XLFD_WEIGHT in FONT->numeric. Value is
2293 zero if the font name doesn't have the format we expect. The
2294 expected format is a font name that starts with a `-' and has
933b0249 2295 XLFD_LAST fields separated by `-'. */
82641697
GM
2296
2297static int
2298split_font_name (f, font, numeric_p)
2299 struct frame *f;
2300 struct font_name *font;
2301 int numeric_p;
2302{
2303 int i = 0;
2304 int success_p;
f70400f2
KH
2305 double rescale_ratio;
2306
2307 if (numeric_p)
2308 /* This must be done before splitting the font name. */
2309 rescale_ratio = font_rescale_ratio (font->name);
82641697
GM
2310
2311 if (*font->name == '-')
2312 {
2313 char *p = xstrlwr (font->name) + 1;
2314
7b16a6bf 2315 while (i < XLFD_LAST)
82641697
GM
2316 {
2317 font->fields[i] = p;
7b16a6bf 2318 ++i;
933b0249
GM
2319
2320 /* Pixel and point size may be of the form `[....]'. For
2321 BNF, see XLFD spec, chapter 4. Negative values are
2322 indicated by tilde characters which we replace with
2323 `-' characters, here. */
2324 if (*p == '['
d5188d8c
GM
2325 && (i - 1 == XLFD_PIXEL_SIZE
2326 || i - 1 == XLFD_POINT_SIZE))
933b0249
GM
2327 {
2328 char *start, *end;
2329 int j;
177c0ea7 2330
933b0249
GM
2331 for (++p; *p && *p != ']'; ++p)
2332 if (*p == '~')
2333 *p = '-';
2334
2335 /* Check that the matrix contains 4 floating point
2336 numbers. */
3849cbf4 2337 for (j = 0, start = font->fields[i - 1] + 1;
933b0249
GM
2338 j < 4;
2339 ++j, start = end)
2340 if (strtod (start, &end) == 0 && start == end)
2341 break;
2342
2343 if (j < 4)
2344 break;
2345 }
178c5d9c 2346
82641697
GM
2347 while (*p && *p != '-')
2348 ++p;
178c5d9c 2349
82641697
GM
2350 if (*p != '-')
2351 break;
178c5d9c 2352
82641697
GM
2353 *p++ = 0;
2354 }
2355 }
2356
2357 success_p = i == XLFD_LAST;
2358
2359 /* If requested, and font name was in the expected format,
2360 compute numeric values for some fields. */
2361 if (numeric_p && success_p)
2362 {
2363 font->numeric[XLFD_POINT_SIZE] = xlfd_point_size (f, font);
2364 font->numeric[XLFD_RESY] = atoi (font->fields[XLFD_RESY]);
2365 font->numeric[XLFD_SLANT] = xlfd_numeric_slant (font);
2366 font->numeric[XLFD_WEIGHT] = xlfd_numeric_weight (font);
2367 font->numeric[XLFD_SWIDTH] = xlfd_numeric_swidth (font);
a08332c0 2368 font->numeric[XLFD_AVGWIDTH] = atoi (font->fields[XLFD_AVGWIDTH]);
f70400f2 2369 font->rescale_ratio = rescale_ratio;
82641697
GM
2370 }
2371
8e1b21a7
KH
2372 /* Initialize it to zero. It will be overridden by font_list while
2373 trying alternate registries. */
2374 font->registry_priority = 0;
2375
82641697
GM
2376 return success_p;
2377}
2378
2379
2380/* Build an XLFD font name from font name fields in FONT. Value is a
2381 pointer to the font name, which is allocated via xmalloc. */
178c5d9c 2382
82641697
GM
2383static char *
2384build_font_name (font)
2385 struct font_name *font;
2386{
2387 int i;
2388 int size = 100;
2389 char *font_name = (char *) xmalloc (size);
2390 int total_length = 0;
2391
2392 for (i = 0; i < XLFD_LAST; ++i)
2393 {
2394 /* Add 1 because of the leading `-'. */
2395 int len = strlen (font->fields[i]) + 1;
2396
2397 /* Reallocate font_name if necessary. Add 1 for the final
2398 NUL-byte. */
2399 if (total_length + len + 1 >= size)
2400 {
2401 int new_size = max (2 * size, size + len + 1);
2402 int sz = new_size * sizeof *font_name;
2403 font_name = (char *) xrealloc (font_name, sz);
2404 size = new_size;
2405 }
2406
2407 font_name[total_length] = '-';
2408 bcopy (font->fields[i], font_name + total_length + 1, len - 1);
2409 total_length += len;
2410 }
2411
2412 font_name[total_length] = 0;
2413 return font_name;
2414}
2415
2416
2417/* Free an array FONTS of N font_name structures. This frees FONTS
2418 itself and all `name' fields in its elements. */
2419
2420static INLINE void
2421free_font_names (fonts, n)
2422 struct font_name *fonts;
2423 int n;
2424{
2425 while (n)
2426 xfree (fonts[--n].name);
2427 xfree (fonts);
2428}
2429
2430
2431/* Sort vector FONTS of font_name structures which contains NFONTS
2432 elements using qsort and comparison function CMPFN. F is the frame
2433 on which the fonts will be used. The global variable font_frame
2434 is temporarily set to F to make it available in CMPFN. */
2435
2436static INLINE void
2437sort_fonts (f, fonts, nfonts, cmpfn)
2438 struct frame *f;
2439 struct font_name *fonts;
2440 int nfonts;
2441 int (*cmpfn) P_ ((const void *, const void *));
2442{
2443 font_frame = f;
2444 qsort (fonts, nfonts, sizeof *fonts, cmpfn);
2445 font_frame = NULL;
2446}
2447
2448
2449/* Get fonts matching PATTERN on frame F. If F is null, use the first
2450 display in x_display_list. FONTS is a pointer to a vector of
2451 NFONTS font_name structures. TRY_ALTERNATIVES_P non-zero means try
2452 alternative patterns from Valternate_fontname_alist if no fonts are
702a1e8e 2453 found matching PATTERN.
82641697
GM
2454
2455 For all fonts found, set FONTS[i].name to the name of the font,
2456 allocated via xmalloc, and split font names into fields. Ignore
a86110a8 2457 fonts that we can't parse. Value is the number of fonts found. */
82641697
GM
2458
2459static int
ee5be7c3 2460x_face_list_fonts (f, pattern, pfonts, nfonts, try_alternatives_p)
82641697
GM
2461 struct frame *f;
2462 char *pattern;
ee5be7c3 2463 struct font_name **pfonts;
82641697 2464 int nfonts, try_alternatives_p;
82641697 2465{
702a1e8e 2466 int n, nignored;
82641697 2467
c3cee013
JR
2468 /* NTEMACS_TODO : currently this uses w32_list_fonts, but it may be
2469 better to do it the other way around. */
2470 Lisp_Object lfonts;
2471 Lisp_Object lpattern, tem;
ee5be7c3
JD
2472 struct font_name *fonts = 0;
2473 int num_fonts = nfonts;
c3cee013 2474
ee5be7c3 2475 *pfonts = 0;
c3cee013
JR
2476 lpattern = build_string (pattern);
2477
2478 /* Get the list of fonts matching PATTERN. */
1a578e9b 2479#ifdef WINDOWSNT
a86110a8 2480 BLOCK_INPUT;
c3cee013 2481 lfonts = w32_list_fonts (f, lpattern, 0, nfonts);
a86110a8 2482 UNBLOCK_INPUT;
d26d6fd9 2483#else
702a1e8e 2484 lfonts = x_list_fonts (f, lpattern, -1, nfonts);
1a578e9b 2485#endif
c3cee013 2486
ee5be7c3 2487 if (nfonts < 0 && CONSP (lfonts))
2b47dacc 2488 num_fonts = XFASTINT (Flength (lfonts));
2d176448 2489
d26d6fd9
GM
2490 /* Make a copy of the font names we got from X, and
2491 split them into fields. */
702a1e8e 2492 n = nignored = 0;
ee5be7c3 2493 for (tem = lfonts; CONSP (tem) && n < num_fonts; tem = XCDR (tem))
c3cee013 2494 {
d26d6fd9 2495 Lisp_Object elt, tail;
53c208f6 2496 const char *name = SDATA (XCAR (tem));
82641697 2497
702a1e8e 2498 /* Ignore fonts matching a pattern from face-ignored-fonts. */
d26d6fd9 2499 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
82641697 2500 {
d26d6fd9
GM
2501 elt = XCAR (tail);
2502 if (STRINGP (elt)
2503 && fast_c_string_match_ignore_case (elt, name) >= 0)
2504 break;
2505 }
2506 if (!NILP (tail))
702a1e8e
GM
2507 {
2508 ++nignored;
2509 continue;
2510 }
c824bfbc 2511
ee5be7c3
JD
2512 if (! fonts)
2513 {
2514 *pfonts = (struct font_name *) xmalloc (num_fonts * sizeof **pfonts);
2515 fonts = *pfonts;
2516 }
2517
d26d6fd9
GM
2518 /* Make a copy of the font name. */
2519 fonts[n].name = xstrdup (name);
82641697 2520
702a1e8e 2521 if (split_font_name (f, fonts + n, 1))
d26d6fd9 2522 {
702a1e8e
GM
2523 if (font_scalable_p (fonts + n)
2524 && !may_use_scalable_font_p (name))
2525 {
2526 ++nignored;
2527 xfree (fonts[n].name);
2528 }
82641697 2529 else
d26d6fd9 2530 ++n;
82641697 2531 }
d26d6fd9 2532 else
702a1e8e
GM
2533 xfree (fonts[n].name);
2534 }
2535
82641697
GM
2536 /* If no fonts found, try patterns from Valternate_fontname_alist. */
2537 if (n == 0 && try_alternatives_p)
2538 {
2539 Lisp_Object list = Valternate_fontname_alist;
2540
72ecb43c
JD
2541 if (*pfonts)
2542 {
2543 xfree (*pfonts);
2544 *pfonts = 0;
2545 }
2546
82641697
GM
2547 while (CONSP (list))
2548 {
2549 Lisp_Object entry = XCAR (list);
2550 if (CONSP (entry)
2551 && STRINGP (XCAR (entry))
d5db4077 2552 && strcmp (SDATA (XCAR (entry)), pattern) == 0)
82641697
GM
2553 break;
2554 list = XCDR (list);
2555 }
2556
2557 if (CONSP (list))
2558 {
2559 Lisp_Object patterns = XCAR (list);
2560 Lisp_Object name;
178c5d9c 2561
82641697
GM
2562 while (CONSP (patterns)
2563 /* If list is screwed up, give up. */
2564 && (name = XCAR (patterns),
2565 STRINGP (name))
2566 /* Ignore patterns equal to PATTERN because we tried that
2567 already with no success. */
d5db4077
KR
2568 && (strcmp (SDATA (name), pattern) == 0
2569 || (n = x_face_list_fonts (f, SDATA (name),
ee5be7c3 2570 pfonts, nfonts, 0),
82641697
GM
2571 n == 0)))
2572 patterns = XCDR (patterns);
2573 }
2574 }
178c5d9c 2575
82641697
GM
2576 return n;
2577}
178c5d9c 2578
82641697 2579
b64c9a04
KH
2580/* Check if a font matching pattern_offset_t on frame F is available
2581 or not. PATTERN may be a cons (FAMILY . REGISTRY), in which case,
2582 a font name pattern is generated from FAMILY and REGISTRY. */
2583
2584int
2585face_font_available_p (f, pattern)
2586 struct frame *f;
2587 Lisp_Object pattern;
2588{
2589 Lisp_Object fonts;
2590
2591 if (! STRINGP (pattern))
2592 {
2593 Lisp_Object family, registry;
2594 char *family_str, *registry_str, *pattern_str;
2595
2596 CHECK_CONS (pattern);
2597 family = XCAR (pattern);
2598 if (NILP (family))
2599 family_str = "*";
2600 else
2601 {
2602 CHECK_STRING (family);
2603 family_str = (char *) SDATA (family);
2604 }
2605 registry = XCDR (pattern);
2606 if (NILP (registry))
2607 registry_str = "*";
2608 else
2609 {
2610 CHECK_STRING (registry);
2611 registry_str = (char *) SDATA (registry);
2612 }
2613
2614 pattern_str = (char *) alloca (strlen (family_str)
2615 + strlen (registry_str)
2616 + 10);
2617 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2618 strcat (pattern_str, family_str);
2619 strcat (pattern_str, "-*-");
2620 strcat (pattern_str, registry_str);
2621 if (!index (registry_str, '-'))
2622 {
2623 if (registry_str[strlen (registry_str) - 1] == '*')
2624 strcat (pattern_str, "-*");
2625 else
2626 strcat (pattern_str, "*-*");
2627 }
2628 pattern = build_string (pattern_str);
2629 }
2630
2631 /* Get the list of fonts matching PATTERN. */
2632#ifdef WINDOWSNT
2633 BLOCK_INPUT;
2634 fonts = w32_list_fonts (f, pattern, 0, 1);
2635 UNBLOCK_INPUT;
2636#else
2637 fonts = x_list_fonts (f, pattern, -1, 1);
2638#endif
2639 return XINT (Flength (fonts));
2640}
2641
2642
82641697
GM
2643/* Determine fonts matching PATTERN on frame F. Sort resulting fonts
2644 using comparison function CMPFN. Value is the number of fonts
2645 found. If value is non-zero, *FONTS is set to a vector of
2646 font_name structures allocated from the heap containing matching
2647 fonts. Each element of *FONTS contains a name member that is also
2648 allocated from the heap. Font names in these structures are split
2649 into fields. Use free_font_names to free such an array. */
2650
2651static int
2652sorted_font_list (f, pattern, cmpfn, fonts)
2653 struct frame *f;
2654 char *pattern;
2655 int (*cmpfn) P_ ((const void *, const void *));
2656 struct font_name **fonts;
2657{
2658 int nfonts;
178c5d9c 2659
82641697 2660 /* Get the list of fonts matching pattern. 100 should suffice. */
057df17c 2661 nfonts = DEFAULT_FONT_LIST_LIMIT;
ee5be7c3
JD
2662 if (INTEGERP (Vfont_list_limit))
2663 nfonts = XINT (Vfont_list_limit);
178c5d9c 2664
ee5be7c3
JD
2665 *fonts = NULL;
2666 nfonts = x_face_list_fonts (f, pattern, fonts, nfonts, 1);
178c5d9c
SS
2667
2668 /* Sort the resulting array and return it in *FONTS. If no
82641697
GM
2669 fonts were found, make sure to set *FONTS to null. */
2670 if (nfonts)
2671 sort_fonts (f, *fonts, nfonts, cmpfn);
ee5be7c3 2672 else if (*fonts)
82641697
GM
2673 {
2674 xfree (*fonts);
2675 *fonts = NULL;
2676 }
2677
2678 return nfonts;
2679}
2680
2681
2682/* Compare two font_name structures *A and *B. Value is analogous to
2683 strcmp. Sort order is given by the global variable
2684 font_sort_order. Font names are sorted so that, everything else
2685 being equal, fonts with a resolution closer to that of the frame on
2686 which they are used are listed first. The global variable
2687 font_frame is the frame on which we operate. */
2688
2689static int
2690cmp_font_names (a, b)
2691 const void *a, *b;
2692{
2693 struct font_name *x = (struct font_name *) a;
2694 struct font_name *y = (struct font_name *) b;
2695 int cmp;
2696
2697 /* All strings have been converted to lower-case by split_font_name,
2698 so we can use strcmp here. */
2699 cmp = strcmp (x->fields[XLFD_FAMILY], y->fields[XLFD_FAMILY]);
2700 if (cmp == 0)
2701 {
2702 int i;
178c5d9c 2703
82641697
GM
2704 for (i = 0; i < DIM (font_sort_order) && cmp == 0; ++i)
2705 {
2706 int j = font_sort_order[i];
2707 cmp = x->numeric[j] - y->numeric[j];
2708 }
2709
2710 if (cmp == 0)
2711 {
2712 /* Everything else being equal, we prefer fonts with an
2713 y-resolution closer to that of the frame. */
2714 int resy = FRAME_X_DISPLAY_INFO (font_frame)->resy;
2715 int x_resy = x->numeric[XLFD_RESY];
2716 int y_resy = y->numeric[XLFD_RESY];
2717 cmp = abs (resy - x_resy) - abs (resy - y_resy);
2718 }
2719 }
2720
2721 return cmp;
2722}
2723
2724
2725/* Get a sorted list of fonts of family FAMILY on frame F. If PATTERN
39506348
KH
2726 is non-nil list fonts matching that pattern. Otherwise, if
2727 REGISTRY is non-nil return only fonts with that registry, otherwise
2728 return fonts of any registry. Set *FONTS to a vector of font_name
2729 structures allocated from the heap containing the fonts found.
2730 Value is the number of fonts found. */
82641697
GM
2731
2732static int
32fcc231 2733font_list_1 (f, pattern, family, registry, fonts)
82641697 2734 struct frame *f;
39506348 2735 Lisp_Object pattern, family, registry;
82641697
GM
2736 struct font_name **fonts;
2737{
39506348
KH
2738 char *pattern_str, *family_str, *registry_str;
2739
2740 if (NILP (pattern))
82641697 2741 {
d5db4077
KR
2742 family_str = (NILP (family) ? "*" : (char *) SDATA (family));
2743 registry_str = (NILP (registry) ? "*" : (char *) SDATA (registry));
178c5d9c 2744
39506348 2745 pattern_str = (char *) alloca (strlen (family_str)
0d1f7c08
KH
2746 + strlen (registry_str)
2747 + 10);
2748 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2749 strcat (pattern_str, family_str);
2750 strcat (pattern_str, "-*-");
2751 strcat (pattern_str, registry_str);
2752 if (!index (registry_str, '-'))
2753 {
2754 if (registry_str[strlen (registry_str) - 1] == '*')
2755 strcat (pattern_str, "-*");
2756 else
2757 strcat (pattern_str, "*-*");
2758 }
82641697 2759 }
39506348 2760 else
d5db4077 2761 pattern_str = (char *) SDATA (pattern);
178c5d9c 2762
39506348 2763 return sorted_font_list (f, pattern_str, cmp_font_names, fonts);
82641697
GM
2764}
2765
2766
8e1b21a7
KH
2767/* Concatenate font list FONTS1 and FONTS2. FONTS1 and FONTS2
2768 contains NFONTS1 fonts and NFONTS2 fonts respectively. Return a
2769 pointer to a newly allocated font list. FONTS1 and FONTS2 are
2770 freed. */
2771
2772static struct font_name *
2773concat_font_list (fonts1, nfonts1, fonts2, nfonts2)
2774 struct font_name *fonts1, *fonts2;
2775 int nfonts1, nfonts2;
2776{
2777 int new_nfonts = nfonts1 + nfonts2;
2778 struct font_name *new_fonts;
2779
2780 new_fonts = (struct font_name *) xmalloc (sizeof *new_fonts * new_nfonts);
2781 bcopy (fonts1, new_fonts, sizeof *new_fonts * nfonts1);
2782 bcopy (fonts2, new_fonts + nfonts1, sizeof *new_fonts * nfonts2);
2783 xfree (fonts1);
2784 xfree (fonts2);
2785 return new_fonts;
2786}
2787
2788
32fcc231
GM
2789/* Get a sorted list of fonts of family FAMILY on frame F.
2790
2791 If PATTERN is non-nil list fonts matching that pattern.
2792
8e1b21a7
KH
2793 If REGISTRY is non-nil, return fonts with that registry and the
2794 alternative registries from Vface_alternative_font_registry_alist.
177c0ea7 2795
32fcc231
GM
2796 If REGISTRY is nil return fonts of any registry.
2797
2798 Set *FONTS to a vector of font_name structures allocated from the
2799 heap containing the fonts found. Value is the number of fonts
2800 found. */
2801
2802static int
2803font_list (f, pattern, family, registry, fonts)
2804 struct frame *f;
2805 Lisp_Object pattern, family, registry;
2806 struct font_name **fonts;
2807{
2808 int nfonts = font_list_1 (f, pattern, family, registry, fonts);
177c0ea7 2809
8e1b21a7 2810 if (!NILP (registry)
32fcc231
GM
2811 && CONSP (Vface_alternative_font_registry_alist))
2812 {
2813 Lisp_Object alter;
2814
2815 alter = Fassoc (registry, Vface_alternative_font_registry_alist);
2816 if (CONSP (alter))
2817 {
8e1b21a7
KH
2818 int reg_prio, i;
2819
2820 for (alter = XCDR (alter), reg_prio = 1;
2821 CONSP (alter);
2822 alter = XCDR (alter), reg_prio++)
32fcc231 2823 if (STRINGP (XCAR (alter)))
8e1b21a7
KH
2824 {
2825 int nfonts2;
2826 struct font_name *fonts2;
2827
2828 nfonts2 = font_list_1 (f, pattern, family, XCAR (alter),
2829 &fonts2);
2830 for (i = 0; i < nfonts2; i++)
2831 fonts2[i].registry_priority = reg_prio;
2832 *fonts = (nfonts > 0
2833 ? concat_font_list (*fonts, nfonts, fonts2, nfonts2)
2834 : fonts2);
2835 nfonts += nfonts2;
2836 }
32fcc231
GM
2837 }
2838 }
2839
2840 return nfonts;
2841}
2842
2843
82641697 2844/* Remove elements from LIST whose cars are `equal'. Called from
92610620 2845 x-family-fonts and x-font-family-list to remove duplicate font
82641697
GM
2846 entries. */
2847
2848static void
2849remove_duplicates (list)
2850 Lisp_Object list;
2851{
2852 Lisp_Object tail = list;
178c5d9c 2853
82641697
GM
2854 while (!NILP (tail) && !NILP (XCDR (tail)))
2855 {
2856 Lisp_Object next = XCDR (tail);
2857 if (!NILP (Fequal (XCAR (next), XCAR (tail))))
f3fbd155 2858 XSETCDR (tail, XCDR (next));
82641697
GM
2859 else
2860 tail = XCDR (tail);
2861 }
2862}
2863
2864
92610620 2865DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
7ee72033 2866 doc: /* Return a list of available fonts of family FAMILY on FRAME.
228299fa
GM
2867If FAMILY is omitted or nil, list all families.
2868Otherwise, FAMILY must be a string, possibly containing wildcards
2869`?' and `*'.
2870If FRAME is omitted or nil, use the selected frame.
2871Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
2872SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
2873FAMILY is the font family name. POINT-SIZE is the size of the
2874font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
2875width, weight and slant of the font. These symbols are the same as for
2876face attributes. FIXED-P is non-nil if the font is fixed-pitch.
2877FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
2878giving the registry and encoding of the font.
2879The result list is sorted according to the current setting of
7ee72033
MB
2880the face font sort order. */)
2881 (family, frame)
82641697
GM
2882 Lisp_Object family, frame;
2883{
2884 struct frame *f = check_x_frame (frame);
2885 struct font_name *fonts;
2886 int i, nfonts;
2887 Lisp_Object result;
2888 struct gcpro gcpro1;
82641697 2889
39506348 2890 if (!NILP (family))
b7826503 2891 CHECK_STRING (family);
178c5d9c 2892
82641697
GM
2893 result = Qnil;
2894 GCPRO1 (result);
39506348 2895 nfonts = font_list (f, Qnil, family, Qnil, &fonts);
82641697
GM
2896 for (i = nfonts - 1; i >= 0; --i)
2897 {
057df17c
GM
2898 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
2899 char *tem;
82641697 2900
82641697
GM
2901 ASET (v, 0, build_string (fonts[i].fields[XLFD_FAMILY]));
2902 ASET (v, 1, xlfd_symbolic_swidth (fonts + i));
2903 ASET (v, 2, make_number (xlfd_point_size (f, fonts + i)));
2904 ASET (v, 3, xlfd_symbolic_weight (fonts + i));
2905 ASET (v, 4, xlfd_symbolic_slant (fonts + i));
2906 ASET (v, 5, xlfd_fixed_p (fonts + i) ? Qt : Qnil);
057df17c
GM
2907 tem = build_font_name (fonts + i);
2908 ASET (v, 6, build_string (tem));
2909 sprintf (tem, "%s-%s", fonts[i].fields[XLFD_REGISTRY],
2910 fonts[i].fields[XLFD_ENCODING]);
2911 ASET (v, 7, build_string (tem));
2912 xfree (tem);
178c5d9c 2913
82641697 2914 result = Fcons (v, result);
82641697
GM
2915 }
2916
2917 remove_duplicates (result);
2918 free_font_names (fonts, nfonts);
2919 UNGCPRO;
2920 return result;
2921}
2922
2923
2924DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
2925 0, 1, 0,
7ee72033 2926 doc: /* Return a list of available font families on FRAME.
228299fa
GM
2927If FRAME is omitted or nil, use the selected frame.
2928Value is a list of conses (FAMILY . FIXED-P) where FAMILY
2929is a font family, and FIXED-P is non-nil if fonts of that family
7ee72033
MB
2930are fixed-pitch. */)
2931 (frame)
82641697
GM
2932 Lisp_Object frame;
2933{
2934 struct frame *f = check_x_frame (frame);
2935 int nfonts, i;
2936 struct font_name *fonts;
2937 Lisp_Object result;
2938 struct gcpro gcpro1;
aed13378 2939 int count = SPECPDL_INDEX ();
178c5d9c 2940
ee5be7c3
JD
2941 /* Let's consider all fonts. */
2942 specbind (intern ("font-list-limit"), make_number (-1));
2943 nfonts = font_list (f, Qnil, Qnil, Qnil, &fonts);
178c5d9c 2944
82641697
GM
2945 result = Qnil;
2946 GCPRO1 (result);
2947 for (i = nfonts - 1; i >= 0; --i)
2948 result = Fcons (Fcons (build_string (fonts[i].fields[XLFD_FAMILY]),
2949 xlfd_fixed_p (fonts + i) ? Qt : Qnil),
2950 result);
2951
2952 remove_duplicates (result);
2953 free_font_names (fonts, nfonts);
2954 UNGCPRO;
057df17c 2955 return unbind_to (count, result);
82641697
GM
2956}
2957
2958
2959DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
7ee72033 2960 doc: /* Return a list of the names of available fonts matching PATTERN.
228299fa
GM
2961If optional arguments FACE and FRAME are specified, return only fonts
2962the same size as FACE on FRAME.
2963PATTERN is a string, perhaps with wildcard characters;
2964 the * character matches any substring, and
2965 the ? character matches any single character.
2966 PATTERN is case-insensitive.
2967FACE is a face name--a symbol.
2968
2969The return value is a list of strings, suitable as arguments to
2970set-face-font.
2971
2972Fonts Emacs can't use may or may not be excluded
2973even if they match PATTERN and FACE.
2974The optional fourth argument MAXIMUM sets a limit on how many
2975fonts to match. The first MAXIMUM fonts are reported.
2976The optional fifth argument WIDTH, if specified, is a number of columns
2977occupied by a character of a font. In that case, return only fonts
7ee72033
MB
2978the WIDTH times as wide as FACE on FRAME. */)
2979 (pattern, face, frame, maximum, width)
82641697
GM
2980 Lisp_Object pattern, face, frame, maximum, width;
2981{
2982 struct frame *f;
2983 int size;
2984 int maxnames;
2985
2986 check_x ();
b7826503 2987 CHECK_STRING (pattern);
178c5d9c 2988
82641697 2989 if (NILP (maximum))
ee5be7c3 2990 maxnames = -1;
82641697
GM
2991 else
2992 {
b7826503 2993 CHECK_NATNUM (maximum);
82641697
GM
2994 maxnames = XINT (maximum);
2995 }
2996
2997 if (!NILP (width))
b7826503 2998 CHECK_NUMBER (width);
82641697
GM
2999
3000 /* We can't simply call check_x_frame because this function may be
3001 called before any frame is created. */
3002 f = frame_or_selected_frame (frame, 2);
c3cee013 3003 if (!FRAME_WINDOW_P (f))
82641697
GM
3004 {
3005 /* Perhaps we have not yet created any frame. */
3006 f = NULL;
3007 face = Qnil;
3008 }
3009
3010 /* Determine the width standard for comparison with the fonts we find. */
3011
3012 if (NILP (face))
3013 size = 0;
3014 else
3015 {
3016 /* This is of limited utility since it works with character
3017 widths. Keep it for compatibility. --gerd. */
2272e967 3018 int face_id = lookup_named_face (f, face, 0, 0);
b5de343d
GM
3019 struct face *face = (face_id < 0
3020 ? NULL
3021 : FACE_FROM_ID (f, face_id));
82641697 3022
c623bbd3
KS
3023#ifdef WINDOWSNT
3024/* For historic reasons, FONT_WIDTH refers to average width on W32,
3025 not maximum as on X. Redefine here. */
3026#undef FONT_WIDTH
3027#define FONT_WIDTH FONT_MAX_WIDTH
3028#endif
3029
b5de343d 3030 if (face && face->font)
c3cee013 3031 size = FONT_WIDTH (face->font);
82641697 3032 else
c623bbd3 3033 size = FONT_WIDTH (FRAME_FONT (f)); /* FRAME_COLUMN_WIDTH (f) */
82641697
GM
3034
3035 if (!NILP (width))
3036 size *= XINT (width);
3037 }
3038
3039 {
3040 Lisp_Object args[2];
3041
3042 args[0] = x_list_fonts (f, pattern, size, maxnames);
3043 if (f == NULL)
3044 /* We don't have to check fontsets. */
3045 return args[0];
3046 args[1] = list_fontsets (f, pattern, size);
3047 return Fnconc (2, args);
3048 }
3049}
3050
c3cee013 3051#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
3052
3053
3054\f
3055/***********************************************************************
3056 Lisp Faces
3057 ***********************************************************************/
3058
a08332c0
GM
3059/* Access face attributes of face LFACE, a Lisp vector. */
3060
3061#define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
3062#define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
3063#define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
3064#define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
3065#define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
3066#define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
3067#define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
3068#define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
3069#define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
3070#define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
3071#define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
3072#define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
3073#define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
3074#define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
3075#define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
3076#define LFACE_AVGWIDTH(LFACE) AREF ((LFACE), LFACE_AVGWIDTH_INDEX)
82641697
GM
3077
3078/* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
3079 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
3080
3081#define LFACEP(LFACE) \
3082 (VECTORP (LFACE) \
3083 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
a08332c0 3084 && EQ (AREF (LFACE, 0), Qface))
82641697 3085
178c5d9c 3086
82641697
GM
3087#if GLYPH_DEBUG
3088
3089/* Check consistency of Lisp face attribute vector ATTRS. */
3090
3091static void
3092check_lface_attrs (attrs)
3093 Lisp_Object *attrs;
3094{
3095 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
3096 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
3097 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
3098 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
a08332c0
GM
3099 xassert (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
3100 || INTEGERP (attrs[LFACE_AVGWIDTH_INDEX]));
82641697 3101 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
2c20458f
MB
3102 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
3103 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
3104 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
82641697
GM
3105 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
3106 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
3107 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
3108 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
3109 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
3110 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
3111 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
3112 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
3113 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
3114 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
3115 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3116 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3117 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
3118 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
3119 || SYMBOLP (attrs[LFACE_BOX_INDEX])
3120 || STRINGP (attrs[LFACE_BOX_INDEX])
3121 || INTEGERP (attrs[LFACE_BOX_INDEX])
3122 || CONSP (attrs[LFACE_BOX_INDEX]));
3123 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
3124 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
3125 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
3126 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
3127 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
3128 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
2c20458f
MB
3129 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
3130 || NILP (attrs[LFACE_INHERIT_INDEX])
3131 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
3132 || CONSP (attrs[LFACE_INHERIT_INDEX]));
82641697
GM
3133#ifdef HAVE_WINDOW_SYSTEM
3134 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
3135 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
fef04523 3136 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
39506348 3137 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
d2cafc8c
GM
3138 || NILP (attrs[LFACE_FONT_INDEX])
3139 || STRINGP (attrs[LFACE_FONT_INDEX]));
82641697
GM
3140#endif
3141}
3142
3143
3144/* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
3145
3146static void
3147check_lface (lface)
3148 Lisp_Object lface;
3149{
3150 if (!NILP (lface))
3151 {
3152 xassert (LFACEP (lface));
3153 check_lface_attrs (XVECTOR (lface)->contents);
3154 }
3155}
3156
3157#else /* GLYPH_DEBUG == 0 */
3158
3159#define check_lface_attrs(attrs) (void) 0
3160#define check_lface(lface) (void) 0
3161
3162#endif /* GLYPH_DEBUG == 0 */
3163
3164
a0a23346
MB
3165\f
3166/* Face-merge cycle checking. */
3167
3168/* A `named merge point' is simply a point during face-merging where we
3169 look up a face by name. We keep a stack of which named lookups we're
3170 currently processing so that we can easily detect cycles, using a
3171 linked- list of struct named_merge_point structures, typically
3172 allocated on the stack frame of the named lookup functions which are
3173 active (so no consing is required). */
3174struct named_merge_point
3175{
3176 Lisp_Object face_name;
3177 struct named_merge_point *prev;
3178};
3179
3180
3181/* If a face merging cycle is detected for FACE_NAME, return 0,
3182 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
6e87ac8f
MB
3183 FACE_NAME, as the head of the linked list pointed to by
3184 NAMED_MERGE_POINTS, and return 1. */
a0a23346
MB
3185
3186static INLINE int
3187push_named_merge_point (struct named_merge_point *new_named_merge_point,
3188 Lisp_Object face_name,
3189 struct named_merge_point **named_merge_points)
3190{
3191 struct named_merge_point *prev;
3192
3193 for (prev = *named_merge_points; prev; prev = prev->prev)
3194 if (EQ (face_name, prev->face_name))
6e87ac8f 3195 return 0;
a0a23346
MB
3196
3197 new_named_merge_point->face_name = face_name;
3198 new_named_merge_point->prev = *named_merge_points;
3199
3200 *named_merge_points = new_named_merge_point;
3201
3202 return 1;
3203}
3204
3205\f
3206
3207
39506348 3208/* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
c7ae3284
GM
3209 to make it a symvol. If FACE_NAME is an alias for another face,
3210 return that face's name. */
3211
3212static Lisp_Object
3213resolve_face_name (face_name)
3214 Lisp_Object face_name;
3215{
3216 Lisp_Object aliased;
178c5d9c 3217
c7ae3284 3218 if (STRINGP (face_name))
d5db4077 3219 face_name = intern (SDATA (face_name));
c7ae3284 3220
906b3b14 3221 while (SYMBOLP (face_name))
c7ae3284
GM
3222 {
3223 aliased = Fget (face_name, Qface_alias);
3224 if (NILP (aliased))
3225 break;
3226 else
3227 face_name = aliased;
3228 }
3229
3230 return face_name;
3231}
3232
3233
82641697 3234/* Return the face definition of FACE_NAME on frame F. F null means
45d9f1ef
GM
3235 return the definition for new frames. FACE_NAME may be a string or
3236 a symbol (apparently Emacs 20.2 allowed strings as face names in
3237 face text properties; Ediff uses that). If FACE_NAME is an alias
3238 for another face, return that face's definition. If SIGNAL_P is
92610620
GM
3239 non-zero, signal an error if FACE_NAME is not a valid face name.
3240 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
3241 name. */
82641697
GM
3242
3243static INLINE Lisp_Object
3244lface_from_face_name (f, face_name, signal_p)
3245 struct frame *f;
3246 Lisp_Object face_name;
3247 int signal_p;
3248{
c7ae3284 3249 Lisp_Object lface;
82641697 3250
c7ae3284 3251 face_name = resolve_face_name (face_name);
92610620 3252
82641697
GM
3253 if (f)
3254 lface = assq_no_quit (face_name, f->face_alist);
3255 else
3256 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
3257
3258 if (CONSP (lface))
3259 lface = XCDR (lface);
3260 else if (signal_p)
3261 signal_error ("Invalid face", face_name);
3262
3263 check_lface (lface);
3264 return lface;
3265}
3266
3267
3268/* Get face attributes of face FACE_NAME from frame-local faces on
3269 frame F. Store the resulting attributes in ATTRS which must point
3270 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
3271 is non-zero, signal an error if FACE_NAME does not name a face.
3272 Otherwise, value is zero if FACE_NAME is not a face. */
3273
3274static INLINE int
3275get_lface_attributes (f, face_name, attrs, signal_p)
3276 struct frame *f;
3277 Lisp_Object face_name;
3278 Lisp_Object *attrs;
3279 int signal_p;
3280{
3281 Lisp_Object lface;
3282 int success_p;
3283
3284 lface = lface_from_face_name (f, face_name, signal_p);
3285 if (!NILP (lface))
3286 {
3287 bcopy (XVECTOR (lface)->contents, attrs,
3288 LFACE_VECTOR_SIZE * sizeof *attrs);
3289 success_p = 1;
3290 }
3291 else
3292 success_p = 0;
3293
3294 return success_p;
3295}
3296
3297
3298/* Non-zero if all attributes in face attribute vector ATTRS are
3299 specified, i.e. are non-nil. */
3300
3301static int
3302lface_fully_specified_p (attrs)
3303 Lisp_Object *attrs;
3304{
3305 int i;
3306
3307 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
a08332c0
GM
3308 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
3309 && i != LFACE_AVGWIDTH_INDEX)
5202d23d
JD
3310 if (UNSPECIFIEDP (attrs[i])
3311#ifdef MAC_OS
3312 /* MAC_TODO: No stipple support on Mac OS yet, this index is
3313 always unspecified. */
3314 && i != LFACE_STIPPLE_INDEX
3315#endif
3316 )
6288c62f 3317 break;
82641697
GM
3318
3319 return i == LFACE_VECTOR_SIZE;
3320}
3321
c3cee013 3322#ifdef HAVE_WINDOW_SYSTEM
82641697 3323
39506348
KH
3324/* Set font-related attributes of Lisp face LFACE from the fullname of
3325 the font opened by FONTNAME. If FORCE_P is zero, set only
3326 unspecified attributes of LFACE. The exception is `font'
3327 attribute. It is set to FONTNAME as is regardless of FORCE_P.
3328
3329 If FONTNAME is not available on frame F,
3330 return 0 if MAY_FAIL_P is non-zero, otherwise abort.
3331 If the fullname is not in a valid XLFD format,
3332 return 0 if MAY_FAIL_P is non-zero, otherwise set normal values
3333 in LFACE and return 1.
3334 Otherwise, return 1. */
178c5d9c 3335
82641697 3336static int
39506348 3337set_lface_from_font_name (f, lface, fontname, force_p, may_fail_p)
82641697
GM
3338 struct frame *f;
3339 Lisp_Object lface;
39506348 3340 Lisp_Object fontname;
3221576b 3341 int force_p, may_fail_p;
82641697
GM
3342{
3343 struct font_name font;
3344 char *buffer;
3345 int pt;
39506348
KH
3346 int have_xlfd_p;
3347 int fontset;
d5db4077 3348 char *font_name = SDATA (fontname);
39506348 3349 struct font_info *font_info;
82641697 3350
39506348
KH
3351 /* If FONTNAME is actually a fontset name, get ASCII font name of it. */
3352 fontset = fs_query_fontset (fontname, 0);
3353 if (fontset >= 0)
d5db4077 3354 font_name = SDATA (fontset_ascii (fontset));
39506348
KH
3355
3356 /* Check if FONT_NAME is surely available on the system. Usually
3357 FONT_NAME is already cached for the frame F and FS_LOAD_FONT
3358 returns quickly. But, even if FONT_NAME is not yet cached,
3359 caching it now is not futail because we anyway load the font
3360 later. */
3361 BLOCK_INPUT;
3362 font_info = FS_LOAD_FONT (f, 0, font_name, -1);
3363 UNBLOCK_INPUT;
82641697 3364
39506348 3365 if (!font_info)
3221576b
GM
3366 {
3367 if (may_fail_p)
3368 return 0;
39506348 3369 abort ();
3221576b 3370 }
39506348
KH
3371
3372 font.name = STRDUPA (font_info->full_name);
3373 have_xlfd_p = split_font_name (f, &font, 1);
3221576b 3374
82641697 3375 /* Set attributes only if unspecified, otherwise face defaults for
39506348
KH
3376 new frames would never take effect. If we couldn't get a font
3377 name conforming to XLFD, set normal values. */
178c5d9c 3378
82641697
GM
3379 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
3380 {
39506348
KH
3381 Lisp_Object val;
3382 if (have_xlfd_p)
3383 {
3384 buffer = (char *) alloca (strlen (font.fields[XLFD_FAMILY])
3385 + strlen (font.fields[XLFD_FOUNDRY])
3386 + 2);
3387 sprintf (buffer, "%s-%s", font.fields[XLFD_FOUNDRY],
3388 font.fields[XLFD_FAMILY]);
3389 val = build_string (buffer);
3390 }
3391 else
3392 val = build_string ("*");
3393 LFACE_FAMILY (lface) = val;
82641697
GM
3394 }
3395
3396 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
3397 {
39506348
KH
3398 if (have_xlfd_p)
3399 pt = xlfd_point_size (f, &font);
3400 else
3401 pt = pixel_point_size (f, font_info->height * 10);
82641697
GM
3402 xassert (pt > 0);
3403 LFACE_HEIGHT (lface) = make_number (pt);
3404 }
3405
3406 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
39506348
KH
3407 LFACE_SWIDTH (lface)
3408 = have_xlfd_p ? xlfd_symbolic_swidth (&font) : Qnormal;
82641697 3409
a08332c0
GM
3410 if (force_p || UNSPECIFIEDP (LFACE_AVGWIDTH (lface)))
3411 LFACE_AVGWIDTH (lface)
3412 = (have_xlfd_p
3413 ? make_number (font.numeric[XLFD_AVGWIDTH])
3414 : Qunspecified);
3415
82641697 3416 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
39506348
KH
3417 LFACE_WEIGHT (lface)
3418 = have_xlfd_p ? xlfd_symbolic_weight (&font) : Qnormal;
82641697
GM
3419
3420 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
39506348
KH
3421 LFACE_SLANT (lface)
3422 = have_xlfd_p ? xlfd_symbolic_slant (&font) : Qnormal;
82641697 3423
39506348 3424 LFACE_FONT (lface) = fontname;
178c5d9c 3425
82641697
GM
3426 return 1;
3427}
d12d0a9b 3428
c3cee013 3429#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
3430
3431
2c20458f
MB
3432/* Merges the face height FROM with the face height TO, and returns the
3433 merged height. If FROM is an invalid height, then INVALID is
cdfaafa9
MB
3434 returned instead. FROM and TO may be either absolute face heights or
3435 `relative' heights; the returned value is always an absolute height
3436 unless both FROM and TO are relative. GCPRO is a lisp value that
3437 will be protected from garbage-collection if this function makes a
3438 call into lisp. */
2c20458f
MB
3439
3440Lisp_Object
e1e419ec
RS
3441merge_face_heights (from, to, invalid)
3442 Lisp_Object from, to, invalid;
2c20458f 3443{
cdfaafa9 3444 Lisp_Object result = invalid;
2c20458f
MB
3445
3446 if (INTEGERP (from))
cdfaafa9
MB
3447 /* FROM is absolute, just use it as is. */
3448 result = from;
3449 else if (FLOATP (from))
3450 /* FROM is a scale, use it to adjust TO. */
3451 {
3452 if (INTEGERP (to))
3453 /* relative X absolute => absolute */
c319cf24 3454 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
cdfaafa9
MB
3455 else if (FLOATP (to))
3456 /* relative X relative => relative */
3457 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
a0a23346
MB
3458 else if (UNSPECIFIEDP (to))
3459 result = from;
2c20458f 3460 }
2c20458f 3461 else if (FUNCTIONP (from))
cdfaafa9 3462 /* FROM is a function, which use to adjust TO. */
2c20458f
MB
3463 {
3464 /* Call function with current height as argument.
3465 From is the new height. */
cdfaafa9 3466 Lisp_Object args[2];
2c20458f
MB
3467
3468 args[0] = from;
3469 args[1] = to;
cdfaafa9 3470 result = safe_call (2, args);
2c20458f 3471
cdfaafa9
MB
3472 /* Ensure that if TO was absolute, so is the result. */
3473 if (INTEGERP (to) && !INTEGERP (result))
3474 result = invalid;
2c20458f
MB
3475 }
3476
cdfaafa9 3477 return result;
2c20458f
MB
3478}
3479
3480
3481/* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
613fa7f2
MB
3482 store the resulting attributes in TO, which must be already be
3483 completely specified and contain only absolute attributes. Every
3484 specified attribute of FROM overrides the corresponding attribute of
3485 TO; relative attributes in FROM are merged with the absolute value in
a0a23346
MB
3486 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
3487 loops in face inheritance; it should be 0 when called from other
3488 places. */
82641697
GM
3489
3490static INLINE void
a0a23346 3491merge_face_vectors (f, from, to, named_merge_points)
2c20458f 3492 struct frame *f;
82641697 3493 Lisp_Object *from, *to;
a0a23346 3494 struct named_merge_point *named_merge_points;
82641697
GM
3495{
3496 int i;
2c20458f
MB
3497
3498 /* If FROM inherits from some other faces, merge their attributes into
3499 TO before merging FROM's direct attributes. Note that an :inherit
3500 attribute of `unspecified' is the same as one of nil; we never
3501 merge :inherit attributes, so nil is more correct, but lots of
3502 other code uses `unspecified' as a generic value for face attributes. */
3503 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
3504 && !NILP (from[LFACE_INHERIT_INDEX]))
a0a23346 3505 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2c20458f 3506
87188200
MB
3507 /* If TO specifies a :font attribute, and FROM specifies some
3508 font-related attribute, we need to clear TO's :font attribute
3509 (because it will be inconsistent with whatever FROM specifies, and
3510 FROM takes precedence). */
3511 if (!NILP (to[LFACE_FONT_INDEX])
3512 && (!UNSPECIFIEDP (from[LFACE_FAMILY_INDEX])
3513 || !UNSPECIFIEDP (from[LFACE_HEIGHT_INDEX])
3514 || !UNSPECIFIEDP (from[LFACE_WEIGHT_INDEX])
3515 || !UNSPECIFIEDP (from[LFACE_SLANT_INDEX])
a08332c0
GM
3516 || !UNSPECIFIEDP (from[LFACE_SWIDTH_INDEX])
3517 || !UNSPECIFIEDP (from[LFACE_AVGWIDTH_INDEX])))
87188200
MB
3518 to[LFACE_FONT_INDEX] = Qnil;
3519
82641697
GM
3520 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3521 if (!UNSPECIFIEDP (from[i]))
ab8469eb
PJ
3522 {
3523 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
e1e419ec 3524 to[i] = merge_face_heights (from[i], to[i], to[i]);
ab8469eb
PJ
3525 else
3526 to[i] = from[i];
3527 }
2c20458f
MB
3528
3529 /* TO is always an absolute face, which should inherit from nothing.
3530 We blindly copy the :inherit attribute above and fix it up here. */
3531 to[LFACE_INHERIT_INDEX] = Qnil;
3532}
3533
a0a23346
MB
3534/* Merge the named face FACE_NAME on frame F, into the vector of face
3535 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
3536 inheritance. Returns true if FACE_NAME is a valid face name and
3537 merging succeeded. */
87188200 3538
a0a23346
MB
3539static int
3540merge_named_face (f, face_name, to, named_merge_points)
2c20458f 3541 struct frame *f;
a0a23346 3542 Lisp_Object face_name;
2c20458f 3543 Lisp_Object *to;
a0a23346 3544 struct named_merge_point *named_merge_points;
2c20458f 3545{
a0a23346 3546 struct named_merge_point named_merge_point;
2c20458f 3547
a0a23346
MB
3548 if (push_named_merge_point (&named_merge_point,
3549 face_name, &named_merge_points))
2c20458f 3550 {
e1e419ec 3551 struct gcpro gcpro1;
a0a23346
MB
3552 Lisp_Object from[LFACE_VECTOR_SIZE];
3553 int ok = get_lface_attributes (f, face_name, from, 0);
2c20458f 3554
a0a23346 3555 if (ok)
e1e419ec
RS
3556 {
3557 GCPRO1 (named_merge_point.face_name);
3558 merge_face_vectors (f, from, to, named_merge_points);
3559 UNGCPRO;
3560 }
2c20458f 3561
a0a23346 3562 return ok;
2c20458f 3563 }
a0a23346
MB
3564 else
3565 return 0;
82641697
GM
3566}
3567
3568
a0a23346
MB
3569/* Merge face attributes from the lisp `face reference' FACE_REF on
3570 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
3571 problems with FACE_REF cause an error message to be shown. Return
3572 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
3573 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
3574 list structure; it may be 0 for most callers.
3575
3576 FACE_REF may be a single face specification or a list of such
3577 specifications. Each face specification can be:
82641697
GM
3578
3579 1. A symbol or string naming a Lisp face.
3580
3581 2. A property list of the form (KEYWORD VALUE ...) where each
3582 KEYWORD is a face attribute name, and value is an appropriate value
3583 for that attribute.
3584
3585 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
3586 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
3587 for compatibility with 20.2.
3588
3589 Face specifications earlier in lists take precedence over later
3590 specifications. */
178c5d9c 3591
a0a23346
MB
3592static int
3593merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
82641697 3594 struct frame *f;
a0a23346 3595 Lisp_Object face_ref;
82641697 3596 Lisp_Object *to;
a0a23346
MB
3597 int err_msgs;
3598 struct named_merge_point *named_merge_points;
82641697 3599{
a0a23346
MB
3600 int ok = 1; /* Succeed without an error? */
3601
3602 if (CONSP (face_ref))
82641697 3603 {
a0a23346 3604 Lisp_Object first = XCAR (face_ref);
178c5d9c 3605
82641697
GM
3606 if (EQ (first, Qforeground_color)
3607 || EQ (first, Qbackground_color))
3608 {
3609 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
3610 . COLOR). COLOR must be a string. */
a0a23346 3611 Lisp_Object color_name = XCDR (face_ref);
82641697
GM
3612 Lisp_Object color = first;
3613
3614 if (STRINGP (color_name))
3615 {
3616 if (EQ (color, Qforeground_color))
3617 to[LFACE_FOREGROUND_INDEX] = color_name;
3618 else
3619 to[LFACE_BACKGROUND_INDEX] = color_name;
3620 }
3621 else
a0a23346
MB
3622 {
3623 if (err_msgs)
3624 add_to_log ("Invalid face color", color_name, Qnil);
3625 ok = 0;
3626 }
82641697
GM
3627 }
3628 else if (SYMBOLP (first)
d5db4077 3629 && *SDATA (SYMBOL_NAME (first)) == ':')
82641697
GM
3630 {
3631 /* Assume this is the property list form. */
a0a23346 3632 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
82641697 3633 {
a0a23346
MB
3634 Lisp_Object keyword = XCAR (face_ref);
3635 Lisp_Object value = XCAR (XCDR (face_ref));
3636 int err = 0;
82641697 3637
d9639b25
RS
3638 /* Specifying `unspecified' is a no-op. */
3639 if (EQ (value, Qunspecified))
3640 ;
3641 else if (EQ (keyword, QCfamily))
82641697
GM
3642 {
3643 if (STRINGP (value))
3644 to[LFACE_FAMILY_INDEX] = value;
3645 else
a0a23346 3646 err = 1;
82641697
GM
3647 }
3648 else if (EQ (keyword, QCheight))
3649 {
2c20458f 3650 Lisp_Object new_height =
e1e419ec 3651 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2c20458f 3652
a0a23346 3653 if (! NILP (new_height))
2c20458f 3654 to[LFACE_HEIGHT_INDEX] = new_height;
a0a23346
MB
3655 else
3656 err = 1;
82641697
GM
3657 }
3658 else if (EQ (keyword, QCweight))
3659 {
3660 if (SYMBOLP (value)
3661 && face_numeric_weight (value) >= 0)
3662 to[LFACE_WEIGHT_INDEX] = value;
3663 else
a0a23346 3664 err = 1;
82641697
GM
3665 }
3666 else if (EQ (keyword, QCslant))
3667 {
3668 if (SYMBOLP (value)
3669 && face_numeric_slant (value) >= 0)
3670 to[LFACE_SLANT_INDEX] = value;
3671 else
a0a23346 3672 err = 1;
82641697
GM
3673 }
3674 else if (EQ (keyword, QCunderline))
3675 {
3676 if (EQ (value, Qt)
3677 || NILP (value)
3678 || STRINGP (value))
3679 to[LFACE_UNDERLINE_INDEX] = value;
3680 else
a0a23346 3681 err = 1;
82641697
GM
3682 }
3683 else if (EQ (keyword, QCoverline))
3684 {
3685 if (EQ (value, Qt)
3686 || NILP (value)
3687 || STRINGP (value))
3688 to[LFACE_OVERLINE_INDEX] = value;
3689 else
a0a23346 3690 err = 1;
82641697
GM
3691 }
3692 else if (EQ (keyword, QCstrike_through))
3693 {
3694 if (EQ (value, Qt)
3695 || NILP (value)
3696 || STRINGP (value))
3697 to[LFACE_STRIKE_THROUGH_INDEX] = value;
3698 else
a0a23346 3699 err = 1;
82641697
GM
3700 }
3701 else if (EQ (keyword, QCbox))
3702 {
3703 if (EQ (value, Qt))
3704 value = make_number (1);
3705 if (INTEGERP (value)
3706 || STRINGP (value)
3707 || CONSP (value)
3708 || NILP (value))
3709 to[LFACE_BOX_INDEX] = value;
3710 else
a0a23346 3711 err = 1;
82641697
GM
3712 }
3713 else if (EQ (keyword, QCinverse_video)
3714 || EQ (keyword, QCreverse_video))
3715 {
3716 if (EQ (value, Qt) || NILP (value))
3717 to[LFACE_INVERSE_INDEX] = value;
3718 else
a0a23346 3719 err = 1;
82641697
GM
3720 }
3721 else if (EQ (keyword, QCforeground))
3722 {
3723 if (STRINGP (value))
3724 to[LFACE_FOREGROUND_INDEX] = value;
3725 else
a0a23346 3726 err = 1;
82641697
GM
3727 }
3728 else if (EQ (keyword, QCbackground))
3729 {
3730 if (STRINGP (value))
3731 to[LFACE_BACKGROUND_INDEX] = value;
3732 else
a0a23346 3733 err = 1;
82641697
GM
3734 }
3735 else if (EQ (keyword, QCstipple))
3736 {
3737#ifdef HAVE_X_WINDOWS
fef04523 3738 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
82641697
GM
3739 if (!NILP (pixmap_p))
3740 to[LFACE_STIPPLE_INDEX] = value;
3741 else
a0a23346 3742 err = 1;
82641697
GM
3743#endif
3744 }
3745 else if (EQ (keyword, QCwidth))
3746 {
3747 if (SYMBOLP (value)
3748 && face_numeric_swidth (value) >= 0)
3749 to[LFACE_SWIDTH_INDEX] = value;
3750 else
a0a23346 3751 err = 1;
82641697 3752 }
2c20458f
MB
3753 else if (EQ (keyword, QCinherit))
3754 {
a0a23346
MB
3755 /* This is not really very useful; it's just like a
3756 normal face reference. */
3757 if (! merge_face_ref (f, value, to,
3758 err_msgs, named_merge_points))
3759 err = 1;
2c20458f 3760 }
82641697 3761 else
a0a23346 3762 err = 1;
82641697 3763
a0a23346
MB
3764 if (err)
3765 {
3766 add_to_log ("Invalid face attribute %S %S", keyword, value);
3767 ok = 0;
3768 }
82641697 3769
a0a23346 3770 face_ref = XCDR (XCDR (face_ref));
82641697
GM
3771 }
3772 }
3773 else
3774 {
a0a23346
MB
3775 /* This is a list of face refs. Those at the beginning of the
3776 list take precedence over what follows, so we have to merge
3777 from the end backwards. */
3778 Lisp_Object next = XCDR (face_ref);
3779
3780 if (! NILP (next))
3781 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
3782
3783 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
3784 ok = 0;
82641697
GM
3785 }
3786 }
3787 else
3788 {
a0a23346
MB
3789 /* FACE_REF ought to be a face name. */
3790 ok = merge_named_face (f, face_ref, to, named_merge_points);
3791 if (!ok && err_msgs)
3792 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
82641697 3793 }
a0a23346
MB
3794
3795 return ok;
82641697
GM
3796}
3797
3798
3799DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
3800 Sinternal_make_lisp_face, 1, 2, 0,
7ee72033 3801 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
228299fa
GM
3802If FACE was not known as a face before, create a new one.
3803If optional argument FRAME is specified, make a frame-local face
3804for that frame. Otherwise operate on the global face definition.
7ee72033
MB
3805Value is a vector of face attributes. */)
3806 (face, frame)
82641697
GM
3807 Lisp_Object face, frame;
3808{
3809 Lisp_Object global_lface, lface;
3810 struct frame *f;
3811 int i;
3812
b7826503 3813 CHECK_SYMBOL (face);
82641697 3814 global_lface = lface_from_face_name (NULL, face, 0);
178c5d9c 3815
82641697
GM
3816 if (!NILP (frame))
3817 {
b7826503 3818 CHECK_LIVE_FRAME (frame);
82641697
GM
3819 f = XFRAME (frame);
3820 lface = lface_from_face_name (f, face, 0);
3821 }
3822 else
3823 f = NULL, lface = Qnil;
3824
3825 /* Add a global definition if there is none. */
3826 if (NILP (global_lface))
3827 {
3828 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3829 Qunspecified);
a08332c0 3830 AREF (global_lface, 0) = Qface;
178c5d9c 3831 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
82641697 3832 Vface_new_frame_defaults);
178c5d9c 3833
82641697
GM
3834 /* Assign the new Lisp face a unique ID. The mapping from Lisp
3835 face id to Lisp face is given by the vector lface_id_to_name.
3836 The mapping from Lisp face to Lisp face id is given by the
3837 property `face' of the Lisp face name. */
3838 if (next_lface_id == lface_id_to_name_size)
3839 {
3840 int new_size = max (50, 2 * lface_id_to_name_size);
3841 int sz = new_size * sizeof *lface_id_to_name;
3842 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
3843 lface_id_to_name_size = new_size;
3844 }
178c5d9c 3845
82641697
GM
3846 lface_id_to_name[next_lface_id] = face;
3847 Fput (face, Qface, make_number (next_lface_id));
3848 ++next_lface_id;
3849 }
3850 else if (f == NULL)
3851 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
a08332c0 3852 AREF (global_lface, i) = Qunspecified;
178c5d9c 3853
82641697
GM
3854 /* Add a frame-local definition. */
3855 if (f)
3856 {
3857 if (NILP (lface))
3858 {
3859 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3860 Qunspecified);
a08332c0 3861 AREF (lface, 0) = Qface;
82641697
GM
3862 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
3863 }
3864 else
3865 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
a08332c0 3866 AREF (lface, i) = Qunspecified;
82641697
GM
3867 }
3868 else
3869 lface = global_lface;
3870
33565969
RS
3871 /* Changing a named face means that all realized faces depending on
3872 that face are invalid. Since we cannot tell which realized faces
3873 depend on the face, make sure they are all removed. This is done
3874 by incrementing face_change_count. The next call to
3875 init_iterator will then free realized faces. */
46b00436
KS
3876 if (NILP (Fget (face, Qface_no_inherit)))
3877 {
3878 ++face_change_count;
3879 ++windows_or_buffers_changed;
3880 }
33565969 3881
82641697
GM
3882 xassert (LFACEP (lface));
3883 check_lface (lface);
3884 return lface;
3885}
3886
3887
3888DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
3889 Sinternal_lisp_face_p, 1, 2, 0,
7ee72033 3890 doc: /* Return non-nil if FACE names a face.
228299fa
GM
3891If optional second parameter FRAME is non-nil, check for the
3892existence of a frame-local face with name FACE on that frame.
7ee72033
MB
3893Otherwise check for the existence of a global face. */)
3894 (face, frame)
82641697
GM
3895 Lisp_Object face, frame;
3896{
3897 Lisp_Object lface;
178c5d9c 3898
82641697
GM
3899 if (!NILP (frame))
3900 {
b7826503 3901 CHECK_LIVE_FRAME (frame);
82641697
GM
3902 lface = lface_from_face_name (XFRAME (frame), face, 0);
3903 }
3904 else
3905 lface = lface_from_face_name (NULL, face, 0);
3906
3907 return lface;
3908}
3909
3910
3911DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
3912 Sinternal_copy_lisp_face, 4, 4, 0,
7ee72033 3913 doc: /* Copy face FROM to TO.
5e9bab6f
RS
3914If FRAME is t, copy the global face definition of FROM.
3915Otherwise, copy the frame-local definition of FROM on FRAME.
3916If NEW-FRAME is a frame, copy that data into the frame-local
3917definition of TO on NEW-FRAME. If NEW-FRAME is nil.
3918FRAME controls where the data is copied to.
228299fa 3919
5e9bab6f 3920The value is TO. */)
7ee72033 3921 (from, to, frame, new_frame)
82641697
GM
3922 Lisp_Object from, to, frame, new_frame;
3923{
3924 Lisp_Object lface, copy;
178c5d9c 3925
b7826503
PJ
3926 CHECK_SYMBOL (from);
3927 CHECK_SYMBOL (to);
82641697
GM
3928
3929 if (EQ (frame, Qt))
3930 {
3931 /* Copy global definition of FROM. We don't make copies of
3932 strings etc. because 20.2 didn't do it either. */
3933 lface = lface_from_face_name (NULL, from, 1);
3934 copy = Finternal_make_lisp_face (to, Qnil);
3935 }
3936 else
3937 {
3938 /* Copy frame-local definition of FROM. */
5e9bab6f
RS
3939 if (NILP (new_frame))
3940 new_frame = frame;
b7826503
PJ
3941 CHECK_LIVE_FRAME (frame);
3942 CHECK_LIVE_FRAME (new_frame);
82641697
GM
3943 lface = lface_from_face_name (XFRAME (frame), from, 1);
3944 copy = Finternal_make_lisp_face (to, new_frame);
3945 }
178c5d9c 3946
82641697
GM
3947 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3948 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
178c5d9c 3949
33565969
RS
3950 /* Changing a named face means that all realized faces depending on
3951 that face are invalid. Since we cannot tell which realized faces
3952 depend on the face, make sure they are all removed. This is done
3953 by incrementing face_change_count. The next call to
3954 init_iterator will then free realized faces. */
46b00436
KS
3955 if (NILP (Fget (to, Qface_no_inherit)))
3956 {
3957 ++face_change_count;
3958 ++windows_or_buffers_changed;
3959 }
33565969 3960
82641697
GM
3961 return to;
3962}
3963
3964
3965DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3966 Sinternal_set_lisp_face_attribute, 3, 4, 0,
7ee72033 3967 doc: /* Set attribute ATTR of FACE to VALUE.
228299fa
GM
3968FRAME being a frame means change the face on that frame.
3969FRAME nil means change the face of the selected frame.
3970FRAME t means change the default for new frames.
3971FRAME 0 means change the face on all frames, and change the default
7ee72033
MB
3972 for new frames. */)
3973 (face, attr, value, frame)
82641697
GM
3974 Lisp_Object face, attr, value, frame;
3975{
3976 Lisp_Object lface;
3977 Lisp_Object old_value = Qnil;
39506348
KH
3978 /* Set 1 if ATTR is QCfont. */
3979 int font_attr_p = 0;
3980 /* Set 1 if ATTR is one of font-related attributes other than QCfont. */
82641697 3981 int font_related_attr_p = 0;
178c5d9c 3982
b7826503
PJ
3983 CHECK_SYMBOL (face);
3984 CHECK_SYMBOL (attr);
82641697 3985
c7ae3284
GM
3986 face = resolve_face_name (face);
3987
628436fb
GM
3988 /* If FRAME is 0, change face on all frames, and change the
3989 default for new frames. */
3990 if (INTEGERP (frame) && XINT (frame) == 0)
3991 {
3992 Lisp_Object tail;
d12d0a9b 3993 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
628436fb
GM
3994 FOR_EACH_FRAME (tail, frame)
3995 Finternal_set_lisp_face_attribute (face, attr, value, frame);
d12d0a9b 3996 return face;
628436fb
GM
3997 }
3998
82641697
GM
3999 /* Set lface to the Lisp attribute vector of FACE. */
4000 if (EQ (frame, Qt))
4001 lface = lface_from_face_name (NULL, face, 1);
4002 else
4003 {
4004 if (NILP (frame))
c0617987 4005 frame = selected_frame;
178c5d9c 4006
b7826503 4007 CHECK_LIVE_FRAME (frame);
82641697 4008 lface = lface_from_face_name (XFRAME (frame), face, 0);
178c5d9c 4009
82641697
GM
4010 /* If a frame-local face doesn't exist yet, create one. */
4011 if (NILP (lface))
4012 lface = Finternal_make_lisp_face (face, frame);
4013 }
4014
4015 if (EQ (attr, QCfamily))
4016 {
4017 if (!UNSPECIFIEDP (value))
4018 {
b7826503 4019 CHECK_STRING (value);
d5db4077 4020 if (SCHARS (value) == 0)
82641697
GM
4021 signal_error ("Invalid face family", value);
4022 }
4023 old_value = LFACE_FAMILY (lface);
4024 LFACE_FAMILY (lface) = value;
4025 font_related_attr_p = 1;
4026 }
4027 else if (EQ (attr, QCheight))
4028 {
4029 if (!UNSPECIFIEDP (value))
4030 {
d8055bfc 4031 Lisp_Object test;
2c20458f 4032
d8055bfc
GM
4033 test = (EQ (face, Qdefault)
4034 ? value
4035 /* The default face must have an absolute size,
4036 otherwise, we do a test merge with a random
4037 height to see if VALUE's ok. */
e1e419ec 4038 : merge_face_heights (value, make_number (10), Qnil));
d8055bfc
GM
4039
4040 if (!INTEGERP (test) || XINT (test) <= 0)
82641697
GM
4041 signal_error ("Invalid face height", value);
4042 }
2c20458f 4043
82641697
GM
4044 old_value = LFACE_HEIGHT (lface);
4045 LFACE_HEIGHT (lface) = value;
4046 font_related_attr_p = 1;
4047 }
4048 else if (EQ (attr, QCweight))
4049 {
4050 if (!UNSPECIFIEDP (value))
4051 {
b7826503 4052 CHECK_SYMBOL (value);
82641697
GM
4053 if (face_numeric_weight (value) < 0)
4054 signal_error ("Invalid face weight", value);
4055 }
4056 old_value = LFACE_WEIGHT (lface);
4057 LFACE_WEIGHT (lface) = value;
4058 font_related_attr_p = 1;
4059 }
4060 else if (EQ (attr, QCslant))
4061 {
4062 if (!UNSPECIFIEDP (value))
4063 {
b7826503 4064 CHECK_SYMBOL (value);
82641697
GM
4065 if (face_numeric_slant (value) < 0)
4066 signal_error ("Invalid face slant", value);
4067 }
4068 old_value = LFACE_SLANT (lface);
4069 LFACE_SLANT (lface) = value;
4070 font_related_attr_p = 1;
4071 }
4072 else if (EQ (attr, QCunderline))
4073 {
4074 if (!UNSPECIFIEDP (value))
4075 if ((SYMBOLP (value)
4076 && !EQ (value, Qt)
4077 && !EQ (value, Qnil))
4078 /* Underline color. */
4079 || (STRINGP (value)
d5db4077 4080 && SCHARS (value) == 0))
82641697 4081 signal_error ("Invalid face underline", value);
178c5d9c 4082
82641697
GM
4083 old_value = LFACE_UNDERLINE (lface);
4084 LFACE_UNDERLINE (lface) = value;
4085 }
4086 else if (EQ (attr, QCoverline))
4087 {
4088 if (!UNSPECIFIEDP (value))
4089 if ((SYMBOLP (value)
4090 && !EQ (value, Qt)
4091 && !EQ (value, Qnil))
4092 /* Overline color. */
4093 || (STRINGP (value)
d5db4077 4094 && SCHARS (value) == 0))
82641697 4095 signal_error ("Invalid face overline", value);
178c5d9c 4096
82641697
GM
4097 old_value = LFACE_OVERLINE (lface);
4098 LFACE_OVERLINE (lface) = value;
4099 }
4100 else if (EQ (attr, QCstrike_through))
4101 {
4102 if (!UNSPECIFIEDP (value))
4103 if ((SYMBOLP (value)
4104 && !EQ (value, Qt)
4105 && !EQ (value, Qnil))
4106 /* Strike-through color. */
4107 || (STRINGP (value)
d5db4077 4108 && SCHARS (value) == 0))
82641697 4109 signal_error ("Invalid face strike-through", value);
178c5d9c 4110
82641697
GM
4111 old_value = LFACE_STRIKE_THROUGH (lface);
4112 LFACE_STRIKE_THROUGH (lface) = value;
4113 }
4114 else if (EQ (attr, QCbox))
4115 {
4116 int valid_p;
178c5d9c 4117
82641697
GM
4118 /* Allow t meaning a simple box of width 1 in foreground color
4119 of the face. */
4120 if (EQ (value, Qt))
4121 value = make_number (1);
4122
4123 if (UNSPECIFIEDP (value))
4124 valid_p = 1;
4125 else if (NILP (value))
4126 valid_p = 1;
4127 else if (INTEGERP (value))
89624b8b 4128 valid_p = XINT (value) != 0;
82641697 4129 else if (STRINGP (value))
d5db4077 4130 valid_p = SCHARS (value) > 0;
82641697
GM
4131 else if (CONSP (value))
4132 {
4133 Lisp_Object tem;
178c5d9c 4134
82641697
GM
4135 tem = value;
4136 while (CONSP (tem))
4137 {
4138 Lisp_Object k, v;
4139
4140 k = XCAR (tem);
4141 tem = XCDR (tem);
4142 if (!CONSP (tem))
4143 break;
4144 v = XCAR (tem);
4145 tem = XCDR (tem);
178c5d9c 4146
82641697
GM
4147 if (EQ (k, QCline_width))
4148 {
89624b8b 4149 if (!INTEGERP (v) || XINT (v) == 0)
82641697
GM
4150 break;
4151 }
4152 else if (EQ (k, QCcolor))
4153 {
91c212f1 4154 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
82641697
GM
4155 break;
4156 }
4157 else if (EQ (k, QCstyle))
4158 {
4159 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
4160 break;
4161 }
4162 else
4163 break;
4164 }
4165
4166 valid_p = NILP (tem);
4167 }
4168 else
4169 valid_p = 0;
4170
4171 if (!valid_p)
4172 signal_error ("Invalid face box", value);
178c5d9c 4173
82641697
GM
4174 old_value = LFACE_BOX (lface);
4175 LFACE_BOX (lface) = value;
4176 }
4177 else if (EQ (attr, QCinverse_video)
4178 || EQ (attr, QCreverse_video))
4179 {
4180 if (!UNSPECIFIEDP (value))
4181 {
b7826503 4182 CHECK_SYMBOL (value);
82641697
GM
4183 if (!EQ (value, Qt) && !NILP (value))
4184 signal_error ("Invalid inverse-video face attribute value", value);
4185 }
4186 old_value = LFACE_INVERSE (lface);
4187 LFACE_INVERSE (lface) = value;
4188 }
4189 else if (EQ (attr, QCforeground))
4190 {
ef917393 4191 if (!UNSPECIFIEDP (value))
82641697
GM
4192 {
4193 /* Don't check for valid color names here because it depends
4194 on the frame (display) whether the color will be valid
4195 when the face is realized. */
b7826503 4196 CHECK_STRING (value);
d5db4077 4197 if (SCHARS (value) == 0)
82641697
GM
4198 signal_error ("Empty foreground color value", value);
4199 }
4200 old_value = LFACE_FOREGROUND (lface);
4201 LFACE_FOREGROUND (lface) = value;
4202 }
4203 else if (EQ (attr, QCbackground))
4204 {
ef917393 4205 if (!UNSPECIFIEDP (value))
82641697
GM
4206 {
4207 /* Don't check for valid color names here because it depends
4208 on the frame (display) whether the color will be valid
4209 when the face is realized. */
b7826503 4210 CHECK_STRING (value);
d5db4077 4211 if (SCHARS (value) == 0)
82641697
GM
4212 signal_error ("Empty background color value", value);
4213 }
4214 old_value = LFACE_BACKGROUND (lface);
4215 LFACE_BACKGROUND (lface) = value;
4216 }
4217 else if (EQ (attr, QCstipple))
4218 {
4219#ifdef HAVE_X_WINDOWS
4220 if (!UNSPECIFIEDP (value)
4221 && !NILP (value)
fef04523 4222 && NILP (Fbitmap_spec_p (value)))
82641697
GM
4223 signal_error ("Invalid stipple attribute", value);
4224 old_value = LFACE_STIPPLE (lface);
4225 LFACE_STIPPLE (lface) = value;
4226#endif /* HAVE_X_WINDOWS */
4227 }
4228 else if (EQ (attr, QCwidth))
4229 {
4230 if (!UNSPECIFIEDP (value))
4231 {
b7826503 4232 CHECK_SYMBOL (value);
82641697
GM
4233 if (face_numeric_swidth (value) < 0)
4234 signal_error ("Invalid face width", value);
4235 }
4236 old_value = LFACE_SWIDTH (lface);
4237 LFACE_SWIDTH (lface) = value;
4238 font_related_attr_p = 1;
4239 }
4240 else if (EQ (attr, QCfont))
4241 {
c3cee013 4242#ifdef HAVE_WINDOW_SYSTEM
3d90c96c 4243 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
96fbd2c6
EZ
4244 {
4245 /* Set font-related attributes of the Lisp face from an XLFD
4246 font name. */
4247 struct frame *f;
4248 Lisp_Object tmp;
82641697 4249
96fbd2c6
EZ
4250 if (EQ (frame, Qt))
4251 f = SELECTED_FRAME ();
4252 else
4253 f = check_x_frame (frame);
178c5d9c 4254
e67f5ccc
RS
4255 if (!UNSPECIFIEDP (value))
4256 {
4257 CHECK_STRING (value);
39506348 4258
e67f5ccc
RS
4259 /* VALUE may be a fontset name or an alias of fontset. In
4260 such a case, use the base fontset name. */
4261 tmp = Fquery_fontset (value, Qnil);
4262 if (!NILP (tmp))
4263 value = tmp;
4264
4265 if (!set_lface_from_font_name (f, lface, value, 1, 1))
4266 signal_error ("Invalid font or fontset name", value);
4267 }
39506348 4268
96fbd2c6
EZ
4269 font_attr_p = 1;
4270 }
c3cee013 4271#endif /* HAVE_WINDOW_SYSTEM */
82641697 4272 }
2c20458f
MB
4273 else if (EQ (attr, QCinherit))
4274 {
4275 Lisp_Object tail;
4276 if (SYMBOLP (value))
4277 tail = Qnil;
4278 else
4279 for (tail = value; CONSP (tail); tail = XCDR (tail))
4280 if (!SYMBOLP (XCAR (tail)))
4281 break;
4282 if (NILP (tail))
4283 LFACE_INHERIT (lface) = value;
4284 else
6288c62f 4285 signal_error ("Invalid face inheritance", value);
2c20458f 4286 }
82641697
GM
4287 else if (EQ (attr, QCbold))
4288 {
4289 old_value = LFACE_WEIGHT (lface);
4290 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
4291 font_related_attr_p = 1;
4292 }
4293 else if (EQ (attr, QCitalic))
4294 {
4295 old_value = LFACE_SLANT (lface);
4296 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
4297 font_related_attr_p = 1;
4298 }
4299 else
4300 signal_error ("Invalid face attribute name", attr);
4301
39506348
KH
4302 if (font_related_attr_p
4303 && !UNSPECIFIEDP (value))
4304 /* If a font-related attribute other than QCfont is specified, the
4305 original `font' attribute nor that of default face is useless
4306 to determine a new font. Thus, we set it to nil so that font
4307 selection mechanism doesn't use it. */
4308 LFACE_FONT (lface) = Qnil;
4309
82641697
GM
4310 /* Changing a named face means that all realized faces depending on
4311 that face are invalid. Since we cannot tell which realized faces
4312 depend on the face, make sure they are all removed. This is done
4313 by incrementing face_change_count. The next call to
4314 init_iterator will then free realized faces. */
4315 if (!EQ (frame, Qt)
46b00436 4316 && NILP (Fget (face, Qface_no_inherit))
82641697
GM
4317 && (EQ (attr, QCfont)
4318 || NILP (Fequal (old_value, value))))
4319 {
4320 ++face_change_count;
4321 ++windows_or_buffers_changed;
4322 }
4323
0eb2ecde 4324 if (!UNSPECIFIEDP (value)
82641697 4325 && NILP (Fequal (old_value, value)))
8bd201d6
GM
4326 {
4327 Lisp_Object param;
4328
4329 param = Qnil;
178c5d9c 4330
8bd201d6
GM
4331 if (EQ (face, Qdefault))
4332 {
b9c769f8 4333#ifdef HAVE_WINDOW_SYSTEM
8bd201d6
GM
4334 /* Changed font-related attributes of the `default' face are
4335 reflected in changed `font' frame parameters. */
ceeda019
GM
4336 if (FRAMEP (frame)
4337 && (font_related_attr_p || font_attr_p)
8bd201d6
GM
4338 && lface_fully_specified_p (XVECTOR (lface)->contents))
4339 set_font_frame_param (frame, lface);
b9c769f8
EZ
4340 else
4341#endif /* HAVE_WINDOW_SYSTEM */
4342
4343 if (EQ (attr, QCforeground))
8bd201d6
GM
4344 param = Qforeground_color;
4345 else if (EQ (attr, QCbackground))
4346 param = Qbackground_color;
4347 }
b9c769f8 4348#ifdef HAVE_WINDOW_SYSTEM
c3cee013 4349#ifndef WINDOWSNT
8bd201d6
GM
4350 else if (EQ (face, Qscroll_bar))
4351 {
4352 /* Changing the colors of `scroll-bar' sets frame parameters
4353 `scroll-bar-foreground' and `scroll-bar-background'. */
4354 if (EQ (attr, QCforeground))
4355 param = Qscroll_bar_foreground;
4356 else if (EQ (attr, QCbackground))
4357 param = Qscroll_bar_background;
4358 }
d12d0a9b 4359#endif /* not WINDOWSNT */
8bd201d6
GM
4360 else if (EQ (face, Qborder))
4361 {
4362 /* Changing background color of `border' sets frame parameter
4363 `border-color'. */
4364 if (EQ (attr, QCbackground))
4365 param = Qborder_color;
4366 }
4367 else if (EQ (face, Qcursor))
4368 {
4369 /* Changing background color of `cursor' sets frame parameter
4370 `cursor-color'. */
4371 if (EQ (attr, QCbackground))
4372 param = Qcursor_color;
4373 }
4374 else if (EQ (face, Qmouse))
4375 {
4376 /* Changing background color of `mouse' sets frame parameter
4377 `mouse-color'. */
4378 if (EQ (attr, QCbackground))
4379 param = Qmouse_color;
4380 }
b9c769f8 4381#endif /* HAVE_WINDOW_SYSTEM */
563f68f1 4382 else if (EQ (face, Qmenu))
ceeda019
GM
4383 {
4384 /* Indicate that we have to update the menu bar when
4385 realizing faces on FRAME. FRAME t change the
4386 default for new frames. We do this by setting
4387 setting the flag in new face caches */
4388 if (FRAMEP (frame))
4389 {
4390 struct frame *f = XFRAME (frame);
4391 if (FRAME_FACE_CACHE (f) == NULL)
4392 FRAME_FACE_CACHE (f) = make_face_cache (f);
4393 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
4394 }
4395 else
4396 menu_face_changed_default = 1;
4397 }
8bd201d6 4398
39506348 4399 if (!NILP (param))
ab8469eb
PJ
4400 {
4401 if (EQ (frame, Qt))
4402 /* Update `default-frame-alist', which is used for new frames. */
4403 {
4404 store_in_alist (&Vdefault_frame_alist, param, value);
4405 }
4406 else
4407 /* Update the current frame's parameters. */
4408 {
4409 Lisp_Object cons;
4410 cons = XCAR (Vparam_value_alist);
4411 XSETCAR (cons, param);
4412 XSETCDR (cons, value);
4413 Fmodify_frame_parameters (frame, Vparam_value_alist);
4414 }
4415 }
8bd201d6 4416 }
82641697 4417
82641697
GM
4418 return face;
4419}
4420
4421
c3cee013 4422#ifdef HAVE_WINDOW_SYSTEM
82641697 4423
39506348
KH
4424/* Set the `font' frame parameter of FRAME determined from `default'
4425 face attributes LFACE. If a face or fontset name is explicitely
4426 specfied in LFACE, use it as is. Otherwise, determine a font name
4427 from the other font-related atrributes of LFACE. In that case, if
4428 there's no matching font, signals an error. */
82641697
GM
4429
4430static void
4431set_font_frame_param (frame, lface)
4432 Lisp_Object frame, lface;
4433{
4434 struct frame *f = XFRAME (frame);
82641697 4435
482cca61 4436 if (FRAME_WINDOW_P (f))
82641697 4437 {
482cca61
GM
4438 Lisp_Object font_name;
4439 char *font;
177c0ea7 4440
482cca61
GM
4441 if (STRINGP (LFACE_FONT (lface)))
4442 font_name = LFACE_FONT (lface);
4443 else
4444 {
4445 /* Choose a font name that reflects LFACE's attributes and has
4446 the registry and encoding pattern specified in the default
4447 fontset (3rd arg: -1) for ASCII characters (4th arg: 0). */
6ae2cd57 4448 font = choose_face_font (f, XVECTOR (lface)->contents, -1, 0, 0);
482cca61
GM
4449 if (!font)
4450 error ("No font matches the specified attribute");
4451 font_name = build_string (font);
4452 xfree (font);
4453 }
177c0ea7 4454
a5f696ac 4455 f->default_face_done_p = 0;
482cca61
GM
4456 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font_name), Qnil));
4457 }
82641697
GM
4458}
4459
4460
8bd201d6
GM
4461/* Update the corresponding face when frame parameter PARAM on frame F
4462 has been assigned the value NEW_VALUE. */
4463
4464void
4465update_face_from_frame_parameter (f, param, new_value)
4466 struct frame *f;
4467 Lisp_Object param, new_value;
4468{
46b00436 4469 Lisp_Object face = Qnil;
8bd201d6
GM
4470 Lisp_Object lface;
4471
4472 /* If there are no faces yet, give up. This is the case when called
4473 from Fx_create_frame, and we do the necessary things later in
92610620 4474 face-set-after-frame-defaults. */
8bd201d6
GM
4475 if (NILP (f->face_alist))
4476 return;
178c5d9c 4477
8bd201d6
GM
4478 if (EQ (param, Qforeground_color))
4479 {
46b00436
KS
4480 face = Qdefault;
4481 lface = lface_from_face_name (f, face, 1);
8bd201d6
GM
4482 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
4483 ? new_value : Qunspecified);
4484 realize_basic_faces (f);
4485 }
4486 else if (EQ (param, Qbackground_color))
4487 {
92610620
GM
4488 Lisp_Object frame;
4489
4490 /* Changing the background color might change the background
4491 mode, so that we have to load new defface specs. Call
4492 frame-update-face-colors to do that. */
4493 XSETFRAME (frame, f);
4494 call1 (Qframe_update_face_colors, frame);
178c5d9c 4495
46b00436
KS
4496 face = Qdefault;
4497 lface = lface_from_face_name (f, face, 1);
8bd201d6
GM
4498 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4499 ? new_value : Qunspecified);
4500 realize_basic_faces (f);
4501 }
46b00436 4502 else if (EQ (param, Qborder_color))
8bd201d6 4503 {
46b00436
KS
4504 face = Qborder;
4505 lface = lface_from_face_name (f, face, 1);
8bd201d6
GM
4506 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4507 ? new_value : Qunspecified);
4508 }
4509 else if (EQ (param, Qcursor_color))
4510 {
46b00436
KS
4511 face = Qcursor;
4512 lface = lface_from_face_name (f, face, 1);
8bd201d6
GM
4513 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4514 ? new_value : Qunspecified);
4515 }
4516 else if (EQ (param, Qmouse_color))
4517 {
46b00436
KS
4518 face = Qmouse;
4519 lface = lface_from_face_name (f, face, 1);
8bd201d6
GM
4520 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4521 ? new_value : Qunspecified);
4522 }
46b00436
KS
4523
4524 /* Changing a named face means that all realized faces depending on
4525 that face are invalid. Since we cannot tell which realized faces
4526 depend on the face, make sure they are all removed. This is done
4527 by incrementing face_change_count. The next call to
4528 init_iterator will then free realized faces. */
4529 if (!NILP (face)
4530 && NILP (Fget (face, Qface_no_inherit)))
4531 {
4532 ++face_change_count;
4533 ++windows_or_buffers_changed;
4534 }
8bd201d6
GM
4535}
4536
4537
82641697
GM
4538/* Get the value of X resource RESOURCE, class CLASS for the display
4539 of frame FRAME. This is here because ordinary `x-get-resource'
4540 doesn't take a frame argument. */
4541
4542DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
7ee72033
MB
4543 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
4544 (resource, class, frame)
82641697
GM
4545 Lisp_Object resource, class, frame;
4546{
c3cee013 4547 Lisp_Object value = Qnil;
b7826503
PJ
4548 CHECK_STRING (resource);
4549 CHECK_STRING (class);
4550 CHECK_LIVE_FRAME (frame);
82641697
GM
4551 BLOCK_INPUT;
4552 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
4553 resource, class, Qnil, Qnil);
4554 UNBLOCK_INPUT;
4555 return value;
4556}
4557
4558
4559/* Return resource string VALUE as a boolean value, i.e. nil, or t.
4560 If VALUE is "on" or "true", return t. If VALUE is "off" or
4561 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
4562 error; if SIGNAL_P is zero, return 0. */
178c5d9c 4563
82641697
GM
4564static Lisp_Object
4565face_boolean_x_resource_value (value, signal_p)
4566 Lisp_Object value;
4567 int signal_p;
4568{
4569 Lisp_Object result = make_number (0);
4570
4571 xassert (STRINGP (value));
178c5d9c 4572
d5db4077
KR
4573 if (xstricmp (SDATA (value), "on") == 0
4574 || xstricmp (SDATA (value), "true") == 0)
82641697 4575 result = Qt;
d5db4077
KR
4576 else if (xstricmp (SDATA (value), "off") == 0
4577 || xstricmp (SDATA (value), "false") == 0)
82641697 4578 result = Qnil;
d5db4077 4579 else if (xstricmp (SDATA (value), "unspecified") == 0)
82641697
GM
4580 result = Qunspecified;
4581 else if (signal_p)
4582 signal_error ("Invalid face attribute value from X resource", value);
4583
4584 return result;
4585}
4586
4587
4588DEFUN ("internal-set-lisp-face-attribute-from-resource",
4589 Finternal_set_lisp_face_attribute_from_resource,
4590 Sinternal_set_lisp_face_attribute_from_resource,
7ee72033
MB
4591 3, 4, 0, doc: /* */)
4592 (face, attr, value, frame)
82641697
GM
4593 Lisp_Object face, attr, value, frame;
4594{
b7826503
PJ
4595 CHECK_SYMBOL (face);
4596 CHECK_SYMBOL (attr);
4597 CHECK_STRING (value);
82641697 4598
d5db4077 4599 if (xstricmp (SDATA (value), "unspecified") == 0)
82641697
GM
4600 value = Qunspecified;
4601 else if (EQ (attr, QCheight))
4602 {
4603 value = Fstring_to_number (value, make_number (10));
4604 if (XINT (value) <= 0)
4605 signal_error ("Invalid face height from X resource", value);
4606 }
4607 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
4608 value = face_boolean_x_resource_value (value, 1);
4609 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
d5db4077 4610 value = intern (SDATA (value));
82641697
GM
4611 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
4612 value = face_boolean_x_resource_value (value, 1);
4613 else if (EQ (attr, QCunderline)
4614 || EQ (attr, QCoverline)
27188d11 4615 || EQ (attr, QCstrike_through))
82641697
GM
4616 {
4617 Lisp_Object boolean_value;
4618
4619 /* If the result of face_boolean_x_resource_value is t or nil,
4620 VALUE does NOT specify a color. */
4621 boolean_value = face_boolean_x_resource_value (value, 0);
4622 if (SYMBOLP (boolean_value))
4623 value = boolean_value;
4624 }
27188d11
GM
4625 else if (EQ (attr, QCbox))
4626 value = Fcar (Fread_from_string (value, Qnil, Qnil));
82641697
GM
4627
4628 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
4629}
4630
c3cee013 4631#endif /* HAVE_WINDOW_SYSTEM */
82641697 4632
c7ae3284
GM
4633\f
4634/***********************************************************************
4635 Menu face
4636 ***********************************************************************/
4637
bce72079 4638#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
c7ae3284 4639
bce72079
GM
4640/* Make menus on frame F appear as specified by the `menu' face. */
4641
4642static void
4643x_update_menu_appearance (f)
c7ae3284 4644 struct frame *f;
c7ae3284 4645{
a03ad468 4646 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
bce72079 4647 XrmDatabase rdb;
178c5d9c 4648
bce72079
GM
4649 if (dpyinfo
4650 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
4651 rdb != NULL))
c7ae3284 4652 {
bce72079
GM
4653 char line[512];
4654 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
4655 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
53c208f6 4656 const char *myname = SDATA (Vx_resource_name);
bce72079 4657 int changed_p = 0;
8ad582ac
GM
4658#ifdef USE_MOTIF
4659 const char *popup_path = "popup_menu";
4660#else
4661 const char *popup_path = "menu.popup";
4662#endif
177c0ea7 4663
bce72079
GM
4664 if (STRINGP (LFACE_FOREGROUND (lface)))
4665 {
8ad582ac
GM
4666 sprintf (line, "%s.%s*foreground: %s",
4667 myname, popup_path,
d5db4077 4668 SDATA (LFACE_FOREGROUND (lface)));
bce72079
GM
4669 XrmPutLineResource (&rdb, line);
4670 sprintf (line, "%s.pane.menubar*foreground: %s",
d5db4077 4671 myname, SDATA (LFACE_FOREGROUND (lface)));
bce72079
GM
4672 XrmPutLineResource (&rdb, line);
4673 changed_p = 1;
4674 }
178c5d9c 4675
bce72079
GM
4676 if (STRINGP (LFACE_BACKGROUND (lface)))
4677 {
8ad582ac
GM
4678 sprintf (line, "%s.%s*background: %s",
4679 myname, popup_path,
d5db4077 4680 SDATA (LFACE_BACKGROUND (lface)));
bce72079
GM
4681 XrmPutLineResource (&rdb, line);
4682 sprintf (line, "%s.pane.menubar*background: %s",
d5db4077 4683 myname, SDATA (LFACE_BACKGROUND (lface)));
bce72079
GM
4684 XrmPutLineResource (&rdb, line);
4685 changed_p = 1;
4686 }
177c0ea7 4687
bce72079
GM
4688 if (face->font_name
4689 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
4690 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
4691 || !UNSPECIFIEDP (LFACE_AVGWIDTH (lface))
4692 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
4693 || !UNSPECIFIEDP (LFACE_SLANT (lface))
4694 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
4695 {
a03ad468 4696#ifdef USE_MOTIF
bce72079 4697 const char *suffix = "List";
211f2210 4698 Bool motif = True;
a03ad468 4699#else
bce72079 4700 const char *suffix = "";
211f2210 4701 Bool motif = False;
2defe37f 4702#endif
211f2210 4703#if defined HAVE_X_I18N
2defe37f
SM
4704 extern char *xic_create_fontsetname
4705 P_ ((char *base_fontname, Bool motif));
211f2210 4706 char *fontsetname = xic_create_fontsetname (face->font_name, motif);
2defe37f
SM
4707#else
4708 char *fontsetname = face->font_name;
bce72079
GM
4709#endif
4710 sprintf (line, "%s.pane.menubar*font%s: %s",
2defe37f 4711 myname, suffix, fontsetname);
bce72079 4712 XrmPutLineResource (&rdb, line);
2eddf899 4713 sprintf (line, "%s.%s*font%s: %s",
2defe37f 4714 myname, popup_path, suffix, fontsetname);
bce72079
GM
4715 XrmPutLineResource (&rdb, line);
4716 changed_p = 1;
211f2210
SM
4717 if (fontsetname != face->font_name)
4718 xfree (fontsetname);
bce72079 4719 }
c7ae3284 4720
bce72079 4721 if (changed_p && f->output_data.x->menubar_widget)
ac17f0bf 4722 free_frame_menubar (f);
c7ae3284
GM
4723 }
4724}
4725
bce72079 4726#endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
82641697
GM
4727
4728
177c0ea7 4729DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
cdfaafa9
MB
4730 Sface_attribute_relative_p,
4731 2, 2, 0,
4732 doc: /* Return non-nil if face ATTRIBUTE VALUE is relative. */)
4733 (attribute, value)
d0930289 4734 Lisp_Object attribute, value;
cdfaafa9
MB
4735{
4736 if (EQ (value, Qunspecified))
4737 return Qt;
4738 else if (EQ (attribute, QCheight))
4739 return INTEGERP (value) ? Qnil : Qt;
4740 else
4741 return Qnil;
4742}
4743
4744DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
4745 3, 3, 0,
4746 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
4747If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
4748the result will be absolute, otherwise it will be relative. */)
4749 (attribute, value1, value2)
4750 Lisp_Object attribute, value1, value2;
4751{
4752 if (EQ (value1, Qunspecified))
4753 return value2;
4754 else if (EQ (attribute, QCheight))
e1e419ec 4755 return merge_face_heights (value1, value2, value1);
cdfaafa9
MB
4756 else
4757 return value1;
4758}
4759
82641697
GM
4760
4761DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
4762 Sinternal_get_lisp_face_attribute,
4763 2, 3, 0,
7ee72033 4764 doc: /* Return face attribute KEYWORD of face SYMBOL.
228299fa
GM
4765If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
4766face attribute name, signal an error.
7fc92635
JB
4767If the optional argument FRAME is given, report on face SYMBOL in that
4768frame. If FRAME is t, report on the defaults for face SYMBOL (for new
7ee72033
MB
4769frames). If FRAME is omitted or nil, use the selected frame. */)
4770 (symbol, keyword, frame)
82641697
GM
4771 Lisp_Object symbol, keyword, frame;
4772{
4773 Lisp_Object lface, value = Qnil;
178c5d9c 4774
b7826503
PJ
4775 CHECK_SYMBOL (symbol);
4776 CHECK_SYMBOL (keyword);
82641697
GM
4777
4778 if (EQ (frame, Qt))
4779 lface = lface_from_face_name (NULL, symbol, 1);
4780 else
4781 {
4782 if (NILP (frame))
c0617987 4783 frame = selected_frame;
b7826503 4784 CHECK_LIVE_FRAME (frame);
82641697
GM
4785 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
4786 }
4787
4788 if (EQ (keyword, QCfamily))
4789 value = LFACE_FAMILY (lface);
4790 else if (EQ (keyword, QCheight))
4791 value = LFACE_HEIGHT (lface);
4792 else if (EQ (keyword, QCweight))
4793 value = LFACE_WEIGHT (lface);
4794 else if (EQ (keyword, QCslant))
4795 value = LFACE_SLANT (lface);
4796 else if (EQ (keyword, QCunderline))
4797 value = LFACE_UNDERLINE (lface);
4798 else if (EQ (keyword, QCoverline))
4799 value = LFACE_OVERLINE (lface);
4800 else if (EQ (keyword, QCstrike_through))
4801 value = LFACE_STRIKE_THROUGH (lface);
4802 else if (EQ (keyword, QCbox))
4803 value = LFACE_BOX (lface);
4804 else if (EQ (keyword, QCinverse_video)
4805 || EQ (keyword, QCreverse_video))
4806 value = LFACE_INVERSE (lface);
4807 else if (EQ (keyword, QCforeground))
4808 value = LFACE_FOREGROUND (lface);
4809 else if (EQ (keyword, QCbackground))
4810 value = LFACE_BACKGROUND (lface);
4811 else if (EQ (keyword, QCstipple))
4812 value = LFACE_STIPPLE (lface);
4813 else if (EQ (keyword, QCwidth))
4814 value = LFACE_SWIDTH (lface);
2c20458f
MB
4815 else if (EQ (keyword, QCinherit))
4816 value = LFACE_INHERIT (lface);
39506348
KH
4817 else if (EQ (keyword, QCfont))
4818 value = LFACE_FONT (lface);
82641697
GM
4819 else
4820 signal_error ("Invalid face attribute name", keyword);
4821
4822 return value;
4823}
4824
4825
4826DEFUN ("internal-lisp-face-attribute-values",
4827 Finternal_lisp_face_attribute_values,
4828 Sinternal_lisp_face_attribute_values, 1, 1, 0,
7ee72033
MB
4829 doc: /* Return a list of valid discrete values for face attribute ATTR.
4830Value is nil if ATTR doesn't have a discrete set of valid values. */)
4831 (attr)
82641697
GM
4832 Lisp_Object attr;
4833{
4834 Lisp_Object result = Qnil;
178c5d9c 4835
b7826503 4836 CHECK_SYMBOL (attr);
178c5d9c 4837
82641697
GM
4838 if (EQ (attr, QCweight)
4839 || EQ (attr, QCslant)
4840 || EQ (attr, QCwidth))
4841 {
4842 /* Extract permissible symbols from tables. */
4843 struct table_entry *table;
4844 int i, dim;
178c5d9c 4845
82641697
GM
4846 if (EQ (attr, QCweight))
4847 table = weight_table, dim = DIM (weight_table);
4848 else if (EQ (attr, QCslant))
4849 table = slant_table, dim = DIM (slant_table);
4850 else
4851 table = swidth_table, dim = DIM (swidth_table);
4852
4853 for (i = 0; i < dim; ++i)
4854 {
4855 Lisp_Object symbol = *table[i].symbol;
4856 Lisp_Object tail = result;
4857
4858 while (!NILP (tail)
4859 && !EQ (XCAR (tail), symbol))
4860 tail = XCDR (tail);
4861
4862 if (NILP (tail))
4863 result = Fcons (symbol, result);
4864 }
4865 }
4866 else if (EQ (attr, QCunderline))
4867 result = Fcons (Qt, Fcons (Qnil, Qnil));
4868 else if (EQ (attr, QCoverline))
4869 result = Fcons (Qt, Fcons (Qnil, Qnil));
4870 else if (EQ (attr, QCstrike_through))
4871 result = Fcons (Qt, Fcons (Qnil, Qnil));
4872 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
4873 result = Fcons (Qt, Fcons (Qnil, Qnil));
4874
4875 return result;
4876}
178c5d9c 4877
82641697
GM
4878
4879DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
178c5d9c 4880 Sinternal_merge_in_global_face, 2, 2, 0,
e3cd9e7f 4881 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
7ee72033
MB
4882Default face attributes override any local face attributes. */)
4883 (face, frame)
82641697
GM
4884 Lisp_Object face, frame;
4885{
aad40737
MB
4886 int i;
4887 Lisp_Object global_lface, local_lface, *gvec, *lvec;
4888
b7826503 4889 CHECK_LIVE_FRAME (frame);
82641697
GM
4890 global_lface = lface_from_face_name (NULL, face, 1);
4891 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
4892 if (NILP (local_lface))
4893 local_lface = Finternal_make_lisp_face (face, frame);
aad40737 4894
cec33c90
SM
4895 /* Make every specified global attribute override the local one.
4896 BEWARE!! This is only used from `face-set-after-frame-default' where
4897 the local frame is defined from default specs in `face-defface-spec'
4898 and those should be overridden by global settings. Hence the strange
4899 "global before local" priority. */
aad40737
MB
4900 lvec = XVECTOR (local_lface)->contents;
4901 gvec = XVECTOR (global_lface)->contents;
4902 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4903 if (! UNSPECIFIEDP (gvec[i]))
4904 lvec[i] = gvec[i];
334a2e2a
GM
4905
4906 return Qnil;
82641697
GM
4907}
4908
4909
4910/* The following function is implemented for compatibility with 20.2.
4911 The function is used in x-resolve-fonts when it is asked to
4912 return fonts with the same size as the font of a face. This is
4913 done in fontset.el. */
4914
178c5d9c 4915DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
e3cd9e7f 4916 doc: /* Return the font name of face FACE, or nil if it is unspecified.
228299fa
GM
4917If the optional argument FRAME is given, report on face FACE in that frame.
4918If FRAME is t, report on the defaults for face FACE (for new frames).
4919 The font default for a face is either nil, or a list
4920 of the form (bold), (italic) or (bold italic).
7ee72033
MB
4921If FRAME is omitted or nil, use the selected frame. */)
4922 (face, frame)
82641697
GM
4923 Lisp_Object face, frame;
4924{
4925 if (EQ (frame, Qt))
4926 {
4927 Lisp_Object result = Qnil;
4928 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
4929
4930 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
4931 && !EQ (LFACE_WEIGHT (lface), Qnormal))
4932 result = Fcons (Qbold, result);
178c5d9c 4933
0f2c6573 4934 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
82641697
GM
4935 && !EQ (LFACE_SLANT (lface), Qnormal))
4936 result = Fcons (Qitalic, result);
178c5d9c 4937
82641697
GM
4938 return result;
4939 }
4940 else
4941 {
4942 struct frame *f = frame_or_selected_frame (frame, 1);
2272e967 4943 int face_id = lookup_named_face (f, face, 0, 1);
82641697 4944 struct face *face = FACE_FROM_ID (f, face_id);
b5de343d 4945 return face ? build_string (face->font_name) : Qnil;
82641697
GM
4946 }
4947}
4948
4949
9717e36c 4950/* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
82641697
GM
4951 all attributes are `equal'. Tries to be fast because this function
4952 is called quite often. */
4953
4954static INLINE int
9717e36c 4955face_attr_equal_p (v1, v2)
3ebc0939 4956 Lisp_Object v1, v2;
82641697 4957{
9717e36c
MB
4958 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
4959 and the other is specified. */
4960 if (XTYPE (v1) != XTYPE (v2))
4961 return 0;
82641697 4962
9717e36c
MB
4963 if (EQ (v1, v2))
4964 return 1;
82641697 4965
9717e36c
MB
4966 switch (XTYPE (v1))
4967 {
4968 case Lisp_String:
4969 if (SBYTES (v1) != SBYTES (v2))
4970 return 0;
82641697 4971
9717e36c 4972 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
178c5d9c 4973
9717e36c
MB
4974 case Lisp_Int:
4975 case Lisp_Symbol:
4976 return 0;
178c5d9c 4977
9717e36c
MB
4978 default:
4979 return !NILP (Fequal (v1, v2));
82641697 4980 }
9717e36c
MB
4981}
4982
4983
4984/* Compare face vectors V1 and V2 for equality. Value is non-zero if
4985 all attributes are `equal'. Tries to be fast because this function
4986 is called quite often. */
4987
4988static INLINE int
4989lface_equal_p (v1, v2)
4990 Lisp_Object *v1, *v2;
4991{
4992 int i, equal_p = 1;
4993
4994 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
4995 equal_p = face_attr_equal_p (v1[i], v2[i]);
178c5d9c 4996
82641697
GM
4997 return equal_p;
4998}
4999
5000
5001DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
5002 Sinternal_lisp_face_equal_p, 2, 3, 0,
7ee72033 5003 doc: /* True if FACE1 and FACE2 are equal.
228299fa
GM
5004If the optional argument FRAME is given, report on face FACE in that frame.
5005If FRAME is t, report on the defaults for face FACE (for new frames).
7ee72033
MB
5006If FRAME is omitted or nil, use the selected frame. */)
5007 (face1, face2, frame)
82641697
GM
5008 Lisp_Object face1, face2, frame;
5009{
5010 int equal_p;
5011 struct frame *f;
5012 Lisp_Object lface1, lface2;
178c5d9c 5013
82641697
GM
5014 if (EQ (frame, Qt))
5015 f = NULL;
5016 else
5017 /* Don't use check_x_frame here because this function is called
5018 before X frames exist. At that time, if FRAME is nil,
5019 selected_frame will be used which is the frame dumped with
5020 Emacs. That frame is not an X frame. */
5021 f = frame_or_selected_frame (frame, 2);
5022
5023 lface1 = lface_from_face_name (NULL, face1, 1);
5024 lface2 = lface_from_face_name (NULL, face2, 1);
5025 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
5026 XVECTOR (lface2)->contents);
5027 return equal_p ? Qt : Qnil;
5028}
5029
178c5d9c 5030
82641697
GM
5031DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
5032 Sinternal_lisp_face_empty_p, 1, 2, 0,
7ee72033 5033 doc: /* True if FACE has no attribute specified.
228299fa
GM
5034If the optional argument FRAME is given, report on face FACE in that frame.
5035If FRAME is t, report on the defaults for face FACE (for new frames).
7ee72033
MB
5036If FRAME is omitted or nil, use the selected frame. */)
5037 (face, frame)
82641697
GM
5038 Lisp_Object face, frame;
5039{
5040 struct frame *f;
5041 Lisp_Object lface;
5042 int i;
5043
5044 if (NILP (frame))
c0617987 5045 frame = selected_frame;
b7826503 5046 CHECK_LIVE_FRAME (frame);
c0617987 5047 f = XFRAME (frame);
178c5d9c 5048
82641697
GM
5049 if (EQ (frame, Qt))
5050 lface = lface_from_face_name (NULL, face, 1);
5051 else
5052 lface = lface_from_face_name (f, face, 1);
5053
5054 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
a08332c0 5055 if (!UNSPECIFIEDP (AREF (lface, i)))
82641697 5056 break;
178c5d9c 5057
82641697
GM
5058 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
5059}
5060
5061
5062DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
178c5d9c 5063 0, 1, 0,
7ee72033
MB
5064 doc: /* Return an alist of frame-local faces defined on FRAME.
5065For internal use only. */)
5066 (frame)
82641697
GM
5067 Lisp_Object frame;
5068{
5069 struct frame *f = frame_or_selected_frame (frame, 0);
5070 return f->face_alist;
5071}
5072
5073
5074/* Return a hash code for Lisp string STRING with case ignored. Used
5075 below in computing a hash value for a Lisp face. */
5076
5077static INLINE unsigned
5078hash_string_case_insensitive (string)
5079 Lisp_Object string;
5080{
53c208f6 5081 const unsigned char *s;
82641697
GM
5082 unsigned hash = 0;
5083 xassert (STRINGP (string));
d5db4077 5084 for (s = SDATA (string); *s; ++s)
82641697
GM
5085 hash = (hash << 1) ^ tolower (*s);
5086 return hash;
5087}
5088
5089
5090/* Return a hash code for face attribute vector V. */
5091
5092static INLINE unsigned
5093lface_hash (v)
5094 Lisp_Object *v;
5095{
5096 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
5097 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
5098 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
6fc556fd
KR
5099 ^ XFASTINT (v[LFACE_WEIGHT_INDEX])
5100 ^ XFASTINT (v[LFACE_SLANT_INDEX])
5101 ^ XFASTINT (v[LFACE_SWIDTH_INDEX])
82641697
GM
5102 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
5103}
5104
5105
5106/* Return non-zero if LFACE1 and LFACE2 specify the same font (without
5107 considering charsets/registries). They do if they specify the same
39506348
KH
5108 family, point size, weight, width, slant, and fontset. Both LFACE1
5109 and LFACE2 must be fully-specified. */
82641697
GM
5110
5111static INLINE int
5112lface_same_font_attributes_p (lface1, lface2)
5113 Lisp_Object *lface1, *lface2;
5114{
5115 xassert (lface_fully_specified_p (lface1)
5116 && lface_fully_specified_p (lface2));
d5db4077
KR
5117 return (xstricmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
5118 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
2c20458f 5119 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
82641697 5120 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
a08332c0 5121 && EQ (lface1[LFACE_AVGWIDTH_INDEX], lface2[LFACE_AVGWIDTH_INDEX])
82641697 5122 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
39506348
KH
5123 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
5124 && (EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
5125 || (STRINGP (lface1[LFACE_FONT_INDEX])
d00b1b63 5126 && STRINGP (lface2[LFACE_FONT_INDEX])
d5db4077
KR
5127 && xstricmp (SDATA (lface1[LFACE_FONT_INDEX]),
5128 SDATA (lface2[LFACE_FONT_INDEX])))));
82641697
GM
5129}
5130
5131
5132\f
5133/***********************************************************************
5134 Realized Faces
5135 ***********************************************************************/
5136
5137/* Allocate and return a new realized face for Lisp face attribute
39506348 5138 vector ATTR. */
82641697
GM
5139
5140static struct face *
39506348 5141make_realized_face (attr)
82641697 5142 Lisp_Object *attr;
82641697
GM
5143{
5144 struct face *face = (struct face *) xmalloc (sizeof *face);
5145 bzero (face, sizeof *face);
39506348 5146 face->ascii_face = face;
82641697
GM
5147 bcopy (attr, face->lface, sizeof face->lface);
5148 return face;
5149}
5150
5151
5152/* Free realized face FACE, including its X resources. FACE may
5153 be null. */
5154
5155static void
5156free_realized_face (f, face)
5157 struct frame *f;
5158 struct face *face;
5159{
5160 if (face)
5161 {
c3cee013
JR
5162#ifdef HAVE_WINDOW_SYSTEM
5163 if (FRAME_WINDOW_P (f))
82641697 5164 {
39506348
KH
5165 /* Free fontset of FACE if it is ASCII face. */
5166 if (face->fontset >= 0 && face == face->ascii_face)
5167 free_face_fontset (f, face);
82641697
GM
5168 if (face->gc)
5169 {
5170 x_free_gc (f, face->gc);
5171 face->gc = 0;
5172 }
178c5d9c 5173
82641697
GM
5174 free_face_colors (f, face);
5175 x_destroy_bitmap (f, face->stipple);
5176 }
c3cee013 5177#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
5178
5179 xfree (face);
5180 }
5181}
5182
5183
5184/* Prepare face FACE for subsequent display on frame F. This
5185 allocated GCs if they haven't been allocated yet or have been freed
5186 by clearing the face cache. */
5187
5188void
5189prepare_face_for_display (f, face)
5190 struct frame *f;
5191 struct face *face;
5192{
c3cee013
JR
5193#ifdef HAVE_WINDOW_SYSTEM
5194 xassert (FRAME_WINDOW_P (f));
178c5d9c 5195
82641697
GM
5196 if (face->gc == 0)
5197 {
5198 XGCValues xgcv;
5199 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
5200
5201 xgcv.foreground = face->foreground;
5202 xgcv.background = face->background;
c3cee013 5203#ifdef HAVE_X_WINDOWS
82641697 5204 xgcv.graphics_exposures = False;
c3cee013 5205#endif
82641697
GM
5206 /* The font of FACE may be null if we couldn't load it. */
5207 if (face->font)
5208 {
c3cee013 5209#ifdef HAVE_X_WINDOWS
82641697 5210 xgcv.font = face->font->fid;
c3cee013
JR
5211#endif
5212#ifdef WINDOWSNT
5213 xgcv.font = face->font;
1a578e9b 5214#endif
e0f712ba 5215#ifdef MAC_OS
1a578e9b 5216 xgcv.font = face->font;
c3cee013 5217#endif
82641697
GM
5218 mask |= GCFont;
5219 }
5220
5221 BLOCK_INPUT;
c3cee013 5222#ifdef HAVE_X_WINDOWS
82641697
GM
5223 if (face->stipple)
5224 {
be8a72f4 5225 xgcv.fill_style = FillOpaqueStippled;
82641697
GM
5226 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
5227 mask |= GCFillStyle | GCStipple;
5228 }
c3cee013 5229#endif
82641697
GM
5230 face->gc = x_create_gc (f, mask, &xgcv);
5231 UNBLOCK_INPUT;
5232 }
c3cee013 5233#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
5234}
5235
82641697 5236\f
b35df831
MB
5237/* Returns the `distance' between the colors X and Y. */
5238
5239static int
5240color_distance (x, y)
5241 XColor *x, *y;
5242{
5243 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
5244 Quoting from that paper:
5245
5246 This formula has results that are very close to L*u*v* (with the
5247 modified lightness curve) and, more importantly, it is a more even
5248 algorithm: it does not have a range of colours where it suddenly
5249 gives far from optimal results.
5250
5251 See <http://www.compuphase.com/cmetric.htm> for more info. */
5252
5253 long r = (x->red - y->red) >> 8;
5254 long g = (x->green - y->green) >> 8;
5255 long b = (x->blue - y->blue) >> 8;
5256 long r_mean = (x->red + y->red) >> 9;
5257
5258 return
5259 (((512 + r_mean) * r * r) >> 8)
5260 + 4 * g * g
5261 + (((767 - r_mean) * b * b) >> 8);
5262}
5263
5264
5265DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
5266 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
5267COLOR1 and COLOR2 may be either strings containing the color name,
5268or lists of the form (RED GREEN BLUE).
5269If FRAME is unspecified or nil, the current frame is used. */)
5270 (color1, color2, frame)
5271 Lisp_Object color1, color2, frame;
5272{
5273 struct frame *f;
5274 XColor cdef1, cdef2;
5275
5276 if (NILP (frame))
5277 frame = selected_frame;
5278 CHECK_LIVE_FRAME (frame);
5279 f = XFRAME (frame);
5280
5281 if ((CONSP (color1) && !parse_rgb_list (color1, &cdef1))
5282 || !STRINGP (color1)
d5db4077 5283 || !defined_color (f, SDATA (color1), &cdef1, 0))
b35df831
MB
5284 signal_error ("Invalid color", color1);
5285 if ((CONSP (color2) && !parse_rgb_list (color2, &cdef2))
5286 || !STRINGP (color2)
d5db4077 5287 || !defined_color (f, SDATA (color2), &cdef2, 0))
b35df831
MB
5288 signal_error ("Invalid color", color2);
5289
5290 return make_number (color_distance (&cdef1, &cdef2));
5291}
5292
5293\f
82641697
GM
5294/***********************************************************************
5295 Face Cache
5296 ***********************************************************************/
5297
5298/* Return a new face cache for frame F. */
5299
5300static struct face_cache *
5301make_face_cache (f)
5302 struct frame *f;
5303{
5304 struct face_cache *c;
5305 int size;
5306
5307 c = (struct face_cache *) xmalloc (sizeof *c);
5308 bzero (c, sizeof *c);
5309 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5310 c->buckets = (struct face **) xmalloc (size);
5311 bzero (c->buckets, size);
5312 c->size = 50;
5313 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
5314 c->f = f;
ceeda019 5315 c->menu_face_changed_p = menu_face_changed_default;
82641697
GM
5316 return c;
5317}
5318
5319
5320/* Clear out all graphics contexts for all realized faces, except for
5321 the basic faces. This should be done from time to time just to avoid
5322 keeping too many graphics contexts that are no longer needed. */
5323
5324static void
5325clear_face_gcs (c)
5326 struct face_cache *c;
5327{
c3cee013 5328 if (c && FRAME_WINDOW_P (c->f))
82641697 5329 {
c3cee013 5330#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
5331 int i;
5332 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
5333 {
5334 struct face *face = c->faces_by_id[i];
5335 if (face && face->gc)
5336 {
5337 x_free_gc (c->f, face->gc);
5338 face->gc = 0;
5339 }
5340 }
c3cee013 5341#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
5342 }
5343}
5344
5345
7fc92635
JB
5346/* Free all realized faces in face cache C, including basic faces.
5347 C may be null. If faces are freed, make sure the frame's current
82641697
GM
5348 matrix is marked invalid, so that a display caused by an expose
5349 event doesn't try to use faces we destroyed. */
5350
5351static void
5352free_realized_faces (c)
5353 struct face_cache *c;
5354{
5355 if (c && c->used)
5356 {
5357 int i, size;
5358 struct frame *f = c->f;
5359
84ec3b4b
GM
5360 /* We must block input here because we can't process X events
5361 safely while only some faces are freed, or when the frame's
5362 current matrix still references freed faces. */
5363 BLOCK_INPUT;
5364
82641697
GM
5365 for (i = 0; i < c->used; ++i)
5366 {
5367 free_realized_face (f, c->faces_by_id[i]);
5368 c->faces_by_id[i] = NULL;
5369 }
178c5d9c 5370
82641697
GM
5371 c->used = 0;
5372 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5373 bzero (c->buckets, size);
5374
5375 /* Must do a thorough redisplay the next time. Mark current
5376 matrices as invalid because they will reference faces freed
5377 above. This function is also called when a frame is
5378 destroyed. In this case, the root window of F is nil. */
5379 if (WINDOWP (f->root_window))
5380 {
5381 clear_current_matrices (f);
5382 ++windows_or_buffers_changed;
5383 }
84ec3b4b
GM
5384
5385 UNBLOCK_INPUT;
82641697
GM
5386 }
5387}
5388
5389
39506348
KH
5390/* Free all faces realized for multibyte characters on frame F that
5391 has FONTSET. */
5392
5393void
5394free_realized_multibyte_face (f, fontset)
5395 struct frame *f;
5396 int fontset;
5397{
5398 struct face_cache *cache = FRAME_FACE_CACHE (f);
5399 struct face *face;
5400 int i;
5401
84ec3b4b
GM
5402 /* We must block input here because we can't process X events safely
5403 while only some faces are freed, or when the frame's current
5404 matrix still references freed faces. */
5405 BLOCK_INPUT;
178c5d9c 5406
39506348
KH
5407 for (i = 0; i < cache->used; i++)
5408 {
5409 face = cache->faces_by_id[i];
5410 if (face
5411 && face != face->ascii_face
5412 && face->fontset == fontset)
5413 {
5414 uncache_face (cache, face);
5415 free_realized_face (f, face);
5416 }
5417 }
178c5d9c 5418
84ec3b4b
GM
5419 /* Must do a thorough redisplay the next time. Mark current
5420 matrices as invalid because they will reference faces freed
5421 above. This function is also called when a frame is destroyed.
5422 In this case, the root window of F is nil. */
39506348
KH
5423 if (WINDOWP (f->root_window))
5424 {
5425 clear_current_matrices (f);
5426 ++windows_or_buffers_changed;
5427 }
178c5d9c 5428
84ec3b4b 5429 UNBLOCK_INPUT;
39506348
KH
5430}
5431
5432
82641697
GM
5433/* Free all realized faces on FRAME or on all frames if FRAME is nil.
5434 This is done after attributes of a named face have been changed,
5435 because we can't tell which realized faces depend on that face. */
5436
5437void
5438free_all_realized_faces (frame)
5439 Lisp_Object frame;
5440{
5441 if (NILP (frame))
5442 {
5443 Lisp_Object rest;
5444 FOR_EACH_FRAME (rest, frame)
5445 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5446 }
5447 else
5448 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5449}
5450
5451
5452/* Free face cache C and faces in it, including their X resources. */
5453
5454static void
5455free_face_cache (c)
5456 struct face_cache *c;
5457{
5458 if (c)
5459 {
5460 free_realized_faces (c);
5461 xfree (c->buckets);
5462 xfree (c->faces_by_id);
5463 xfree (c);
5464 }
5465}
5466
5467
5468/* Cache realized face FACE in face cache C. HASH is the hash value
5469 of FACE. If FACE->fontset >= 0, add the new face to the end of the
5470 collision list of the face hash table of C. This is done because
39506348
KH
5471 otherwise lookup_face would find FACE for every character, even if
5472 faces with the same attributes but for specific characters exist. */
82641697
GM
5473
5474static void
5475cache_face (c, face, hash)
5476 struct face_cache *c;
5477 struct face *face;
5478 unsigned hash;
5479{
5480 int i = hash % FACE_CACHE_BUCKETS_SIZE;
5481
5482 face->hash = hash;
5483
5484 if (face->fontset >= 0)
5485 {
5486 struct face *last = c->buckets[i];
5487 if (last)
5488 {
5489 while (last->next)
5490 last = last->next;
5491 last->next = face;
5492 face->prev = last;
5493 face->next = NULL;
5494 }
5495 else
5496 {
5497 c->buckets[i] = face;
5498 face->prev = face->next = NULL;
5499 }
5500 }
5501 else
5502 {
5503 face->prev = NULL;
5504 face->next = c->buckets[i];
5505 if (face->next)
5506 face->next->prev = face;
5507 c->buckets[i] = face;
5508 }
5509
5510 /* Find a free slot in C->faces_by_id and use the index of the free
5511 slot as FACE->id. */
5512 for (i = 0; i < c->used; ++i)
5513 if (c->faces_by_id[i] == NULL)
5514 break;
5515 face->id = i;
178c5d9c 5516
82641697 5517 /* Maybe enlarge C->faces_by_id. */
d7078aef 5518 if (i == c->used)
82641697 5519 {
d7078aef
KS
5520 if (c->used == c->size)
5521 {
5522 int new_size, sz;
5523 new_size = min (2 * c->size, MAX_FACE_ID);
5524 if (new_size == c->size)
5525 abort (); /* Alternatives? ++kfs */
5526 sz = new_size * sizeof *c->faces_by_id;
5527 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
5528 c->size = new_size;
5529 }
5530 c->used++;
82641697
GM
5531 }
5532
5533#if GLYPH_DEBUG
5534 /* Check that FACE got a unique id. */
5535 {
5536 int j, n;
5537 struct face *face;
5538
5539 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
5540 for (face = c->buckets[j]; face; face = face->next)
5541 if (face->id == i)
5542 ++n;
5543
5544 xassert (n == 1);
5545 }
5546#endif /* GLYPH_DEBUG */
178c5d9c 5547
82641697 5548 c->faces_by_id[i] = face;
82641697
GM
5549}
5550
5551
5552/* Remove face FACE from cache C. */
5553
5554static void
5555uncache_face (c, face)
5556 struct face_cache *c;
5557 struct face *face;
5558{
5559 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
178c5d9c 5560
82641697
GM
5561 if (face->prev)
5562 face->prev->next = face->next;
5563 else
5564 c->buckets[i] = face->next;
178c5d9c 5565
82641697
GM
5566 if (face->next)
5567 face->next->prev = face->prev;
178c5d9c 5568
82641697
GM
5569 c->faces_by_id[face->id] = NULL;
5570 if (face->id == c->used)
5571 --c->used;
5572}
5573
5574
5575/* Look up a realized face with face attributes ATTR in the face cache
39506348
KH
5576 of frame F. The face will be used to display character C. Value
5577 is the ID of the face found. If no suitable face is found, realize
5578 a new one. In that case, if C is a multibyte character, BASE_FACE
0badc114 5579 is a face that has the same attributes. */
82641697
GM
5580
5581INLINE int
39506348 5582lookup_face (f, attr, c, base_face)
82641697
GM
5583 struct frame *f;
5584 Lisp_Object *attr;
39506348
KH
5585 int c;
5586 struct face *base_face;
82641697 5587{
39506348 5588 struct face_cache *cache = FRAME_FACE_CACHE (f);
82641697
GM
5589 unsigned hash;
5590 int i;
5591 struct face *face;
5592
39506348 5593 xassert (cache != NULL);
82641697
GM
5594 check_lface_attrs (attr);
5595
5596 /* Look up ATTR in the face cache. */
5597 hash = lface_hash (attr);
5598 i = hash % FACE_CACHE_BUCKETS_SIZE;
178c5d9c 5599
39506348 5600 for (face = cache->buckets[i]; face; face = face->next)
82641697 5601 if (face->hash == hash
44747bd0 5602 && (!FRAME_WINDOW_P (f)
39506348 5603 || FACE_SUITABLE_FOR_CHAR_P (face, c))
82641697
GM
5604 && lface_equal_p (face->lface, attr))
5605 break;
5606
5607 /* If not found, realize a new face. */
5608 if (face == NULL)
39506348 5609 face = realize_face (cache, attr, c, base_face, -1);
82641697
GM
5610
5611#if GLYPH_DEBUG
5612 xassert (face == FACE_FROM_ID (f, face->id));
d2ff77da
KH
5613
5614/* When this function is called from face_for_char (in this case, C is
5615 a multibyte character), a fontset of a face returned by
5616 realize_face is not yet set, i.e. FACE_SUITABLE_FOR_CHAR_P (FACE,
5617 C) is not sutisfied. The fontset is set for this face by
5618 face_for_char later. */
c87a1fda 5619#if 0
c3cee013 5620 if (FRAME_WINDOW_P (f))
39506348 5621 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
c87a1fda 5622#endif
82641697 5623#endif /* GLYPH_DEBUG */
178c5d9c 5624
82641697
GM
5625 return face->id;
5626}
5627
5628
5629/* Return the face id of the realized face for named face SYMBOL on
b5de343d
GM
5630 frame F suitable for displaying character C. Value is -1 if the
5631 face couldn't be determined, which might happen if the default face
5632 isn't realized and cannot be realized. */
82641697
GM
5633
5634int
2272e967 5635lookup_named_face (f, symbol, c, signal_p)
82641697
GM
5636 struct frame *f;
5637 Lisp_Object symbol;
39506348 5638 int c;
2272e967 5639 int signal_p;
82641697
GM
5640{
5641 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5642 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5643 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5644
b5de343d
GM
5645 if (default_face == NULL)
5646 {
5647 if (!realize_basic_faces (f))
5648 return -1;
5649 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5650 }
5651
2272e967
KS
5652 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p))
5653 return -1;
5654
82641697 5655 bcopy (default_face->lface, attrs, sizeof attrs);
a0a23346
MB
5656 merge_face_vectors (f, symbol_attrs, attrs, 0);
5657
39506348 5658 return lookup_face (f, attrs, c, NULL);
82641697
GM
5659}
5660
5661
5662/* Return the ID of the realized ASCII face of Lisp face with ID
5663 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
5664
5665int
5666ascii_face_of_lisp_face (f, lface_id)
5667 struct frame *f;
5668 int lface_id;
5669{
5670 int face_id;
178c5d9c 5671
82641697
GM
5672 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
5673 {
5674 Lisp_Object face_name = lface_id_to_name[lface_id];
2272e967 5675 face_id = lookup_named_face (f, face_name, 0, 1);
82641697
GM
5676 }
5677 else
5678 face_id = -1;
5679
5680 return face_id;
5681}
5682
5683
5684/* Return a face for charset ASCII that is like the face with id
5685 FACE_ID on frame F, but has a font that is STEPS steps smaller.
5686 STEPS < 0 means larger. Value is the id of the face. */
5687
5688int
5689smaller_face (f, face_id, steps)
5690 struct frame *f;
5691 int face_id, steps;
39506348 5692{
c3cee013 5693#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
5694 struct face *face;
5695 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5696 int pt, last_pt, last_height;
5697 int delta;
5698 int new_face_id;
5699 struct face *new_face;
5700
5701 /* If not called for an X frame, just return the original face. */
5702 if (FRAME_TERMCAP_P (f))
5703 return face_id;
5704
5705 /* Try in increments of 1/2 pt. */
5706 delta = steps < 0 ? 5 : -5;
5707 steps = abs (steps);
178c5d9c 5708
82641697
GM
5709 face = FACE_FROM_ID (f, face_id);
5710 bcopy (face->lface, attrs, sizeof attrs);
5711 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
5712 new_face_id = face_id;
5713 last_height = FONT_HEIGHT (face->font);
5714
5715 while (steps
5716 && pt + delta > 0
5717 /* Give up if we cannot find a font within 10pt. */
5718 && abs (last_pt - pt) < 100)
5719 {
5720 /* Look up a face for a slightly smaller/larger font. */
5721 pt += delta;
5722 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
39506348 5723 new_face_id = lookup_face (f, attrs, 0, NULL);
82641697
GM
5724 new_face = FACE_FROM_ID (f, new_face_id);
5725
5726 /* If height changes, count that as one step. */
b4c3ca09
GM
5727 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
5728 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
82641697
GM
5729 {
5730 --steps;
5731 last_height = FONT_HEIGHT (new_face->font);
5732 last_pt = pt;
5733 }
5734 }
5735
5736 return new_face_id;
5737
c3cee013 5738#else /* not HAVE_WINDOW_SYSTEM */
82641697
GM
5739
5740 return face_id;
178c5d9c 5741
c3cee013 5742#endif /* not HAVE_WINDOW_SYSTEM */
82641697
GM
5743}
5744
5745
5746/* Return a face for charset ASCII that is like the face with id
5747 FACE_ID on frame F, but has height HEIGHT. */
5748
5749int
5750face_with_height (f, face_id, height)
5751 struct frame *f;
5752 int face_id;
5753 int height;
5754{
c3cee013 5755#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
5756 struct face *face;
5757 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5758
5759 if (FRAME_TERMCAP_P (f)
5760 || height <= 0)
5761 return face_id;
5762
5763 face = FACE_FROM_ID (f, face_id);
5764 bcopy (face->lface, attrs, sizeof attrs);
5765 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
39506348 5766 face_id = lookup_face (f, attrs, 0, NULL);
c3cee013 5767#endif /* HAVE_WINDOW_SYSTEM */
178c5d9c 5768
82641697
GM
5769 return face_id;
5770}
5771
b5de343d 5772
44747bd0 5773/* Return the face id of the realized face for named face SYMBOL on
39506348
KH
5774 frame F suitable for displaying character C, and use attributes of
5775 the face FACE_ID for attributes that aren't completely specified by
5776 SYMBOL. This is like lookup_named_face, except that the default
5777 attributes come from FACE_ID, not from the default face. FACE_ID
5778 is assumed to be already realized. */
44747bd0
EZ
5779
5780int
d196a547 5781lookup_derived_face (f, symbol, c, face_id, signal_p)
44747bd0
EZ
5782 struct frame *f;
5783 Lisp_Object symbol;
39506348 5784 int c;
44747bd0
EZ
5785 int face_id;
5786{
5787 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5788 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5789 struct face *default_face = FACE_FROM_ID (f, face_id);
5790
5791 if (!default_face)
5792 abort ();
5793
d196a547 5794 get_lface_attributes (f, symbol, symbol_attrs, signal_p);
44747bd0 5795 bcopy (default_face->lface, attrs, sizeof attrs);
a0a23346 5796 merge_face_vectors (f, symbol_attrs, attrs, 0);
39506348 5797 return lookup_face (f, attrs, c, default_face);
44747bd0
EZ
5798}
5799
f6608d5c
RS
5800DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
5801 Sface_attributes_as_vector, 1, 1, 0,
4bb962be 5802 doc: /* Return a vector of face attributes corresponding to PLIST. */)
f6608d5c
RS
5803 (plist)
5804 Lisp_Object plist;
5805{
5806 Lisp_Object lface;
5807 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
5808 Qunspecified);
a0a23346
MB
5809 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
5810 1, 0);
f6608d5c
RS
5811 return lface;
5812}
5813
82641697
GM
5814
5815\f
9717e36c
MB
5816/***********************************************************************
5817 Face capability testing
5818 ***********************************************************************/
5819
5820
5821/* If the distance (as returned by color_distance) between two colors is
5822 less than this, then they are considered the same, for determining
5823 whether a color is supported or not. The range of values is 0-65535. */
5824
5825#define TTY_SAME_COLOR_THRESHOLD 10000
5826
ccda4e3c 5827#ifdef HAVE_WINDOW_SYSTEM
9717e36c
MB
5828
5829/* Return non-zero if all the face attributes in ATTRS are supported
5830 on the window-system frame F.
5831
5832 The definition of `supported' is somewhat heuristic, but basically means
5833 that a face containing all the attributes in ATTRS, when merged with the
5834 default face for display, can be represented in a way that's
5835
5836 \(1) different in appearance than the default face, and
8e330b22 5837 \(2) `close in spirit' to what the attributes specify, if not exact. */
9717e36c
MB
5838
5839static int
8e330b22 5840x_supports_face_attributes_p (f, attrs, def_face)
9717e36c
MB
5841 struct frame *f;
5842 Lisp_Object *attrs;
8e330b22 5843 struct face *def_face;
9717e36c 5844{
8e330b22 5845 Lisp_Object *def_attrs = def_face->lface;
9717e36c
MB
5846
5847 /* Check that other specified attributes are different that the default
5848 face. */
5849 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
5850 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
5851 def_attrs[LFACE_UNDERLINE_INDEX]))
5852 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
5853 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
5854 def_attrs[LFACE_INVERSE_INDEX]))
5855 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
5856 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
5857 def_attrs[LFACE_FOREGROUND_INDEX]))
5858 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
5859 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
5860 def_attrs[LFACE_BACKGROUND_INDEX]))
5861 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5862 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
5863 def_attrs[LFACE_STIPPLE_INDEX]))
5864 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5865 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
5866 def_attrs[LFACE_OVERLINE_INDEX]))
5867 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5868 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
5869 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
5870 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5871 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
5872 def_attrs[LFACE_BOX_INDEX])))
5873 return 0;
5874
5875 /* Check font-related attributes, as those are the most commonly
5876 "unsupported" on a window-system (because of missing fonts). */
5877 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5878 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5879 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
5880 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
5881 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5882 || !UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX]))
5883 {
5884 struct face *face;
5885 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
5886
5887 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
5888
a0a23346 5889 merge_face_vectors (f, attrs, merged_attrs, 0);
9717e36c
MB
5890
5891 face = FACE_FROM_ID (f, lookup_face (f, merged_attrs, 0, 0));
5892
5893 if (! face)
734e9514 5894 error ("cannot make face");
9717e36c
MB
5895
5896 /* If the font is the same, then not supported. */
5897 if (face->font == def_face->font)
5898 return 0;
5899 }
5900
5901 /* Everything checks out, this face is supported. */
5902 return 1;
5903}
5904
ccda4e3c 5905#endif /* HAVE_WINDOW_SYSTEM */
9717e36c
MB
5906
5907/* Return non-zero if all the face attributes in ATTRS are supported
5908 on the tty frame F.
5909
5910 The definition of `supported' is somewhat heuristic, but basically means
5911 that a face containing all the attributes in ATTRS, when merged
5912 with the default face for display, can be represented in a way that's
5913
5914 \(1) different in appearance than the default face, and
5915 \(2) `close in spirit' to what the attributes specify, if not exact.
5916
5917 Point (2) implies that a `:weight black' attribute will be satisfied
5918 by any terminal that can display bold, and a `:foreground "yellow"' as
5919 long as the terminal can display a yellowish color, but `:slant italic'
5920 will _not_ be satisfied by the tty display code's automatic
5921 substitution of a `dim' face for italic. */
5922
5923static int
8e330b22 5924tty_supports_face_attributes_p (f, attrs, def_face)
9717e36c
MB
5925 struct frame *f;
5926 Lisp_Object *attrs;
8e330b22 5927 struct face *def_face;
9717e36c 5928{
734e9514 5929 int weight;
9717e36c
MB
5930 Lisp_Object val, fg, bg;
5931 XColor fg_tty_color, fg_std_color;
5932 XColor bg_tty_color, bg_std_color;
5933 unsigned test_caps = 0;
8e330b22 5934 Lisp_Object *def_attrs = def_face->lface;
9717e36c 5935
9717e36c 5936
8e330b22
MB
5937 /* First check some easy-to-check stuff; ttys support none of the
5938 following attributes, so we can just return false if any are requested
5939 (even if `nominal' values are specified, we should still return false,
5940 as that will be the same value that the default face uses). We
5941 consider :slant unsupportable on ttys, even though the face code
5942 actually `fakes' them using a dim attribute if possible. This is
5943 because the faked result is too different from what the face
5944 specifies. */
5945 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5946 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5947 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5948 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5949 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5950 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5951 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5952 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
9717e36c
MB
5953 return 0;
5954
5955
5956 /* Test for terminal `capabilities' (non-color character attributes). */
5957
5958 /* font weight (bold/dim) */
5959 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5960 if (weight >= 0)
5961 {
8e330b22
MB
5962 int def_weight = face_numeric_weight (def_attrs[LFACE_WEIGHT_INDEX]);
5963
9717e36c 5964 if (weight > XLFD_WEIGHT_MEDIUM)
8e330b22
MB
5965 {
5966 if (def_weight > XLFD_WEIGHT_MEDIUM)
5967 return 0; /* same as default */
5968 test_caps = TTY_CAP_BOLD;
5969 }
9717e36c 5970 else if (weight < XLFD_WEIGHT_MEDIUM)
8e330b22
MB
5971 {
5972 if (def_weight < XLFD_WEIGHT_MEDIUM)
5973 return 0; /* same as default */
5974 test_caps = TTY_CAP_DIM;
5975 }
5976 else if (def_weight == XLFD_WEIGHT_MEDIUM)
5977 return 0; /* same as default */
9717e36c
MB
5978 }
5979
5980 /* underlining */
5981 val = attrs[LFACE_UNDERLINE_INDEX];
8e330b22 5982 if (!UNSPECIFIEDP (val))
9717e36c
MB
5983 {
5984 if (STRINGP (val))
8e330b22
MB
5985 return 0; /* ttys can't use colored underlines */
5986 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5987 return 0; /* same as default */
9717e36c
MB
5988 else
5989 test_caps |= TTY_CAP_UNDERLINE;
5990 }
5991
5992 /* inverse video */
5993 val = attrs[LFACE_INVERSE_INDEX];
8e330b22
MB
5994 if (!UNSPECIFIEDP (val))
5995 {
5996 if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5997 return 0; /* same as default */
5998 else
5999 test_caps |= TTY_CAP_INVERSE;
6000 }
9717e36c
MB
6001
6002
6003 /* Color testing. */
6004
6005 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
6006 we use them when calling `tty_capable_p' below, even if the face
6007 specifies no colors. */
6008 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
6009 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
6010
6011 /* Check if foreground color is close enough. */
6012 fg = attrs[LFACE_FOREGROUND_INDEX];
6013 if (STRINGP (fg))
6014 {
8e330b22
MB
6015 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
6016
6017 if (face_attr_equal_p (fg, def_fg))
6018 return 0; /* same as default */
6019 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
6020 return 0; /* not a valid color */
9717e36c
MB
6021 else if (color_distance (&fg_tty_color, &fg_std_color)
6022 > TTY_SAME_COLOR_THRESHOLD)
8e330b22
MB
6023 return 0; /* displayed color is too different */
6024 else
6025 /* Make sure the color is really different than the default. */
6026 {
6027 XColor def_fg_color;
6028 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
6029 && (color_distance (&fg_tty_color, &def_fg_color)
6030 <= TTY_SAME_COLOR_THRESHOLD))
6031 return 0;
6032 }
9717e36c
MB
6033 }
6034
6035 /* Check if background color is close enough. */
6036 bg = attrs[LFACE_BACKGROUND_INDEX];
6037 if (STRINGP (bg))
6038 {
8e330b22
MB
6039 Lisp_Object def_bg = def_attrs[LFACE_FOREGROUND_INDEX];
6040
6041 if (face_attr_equal_p (bg, def_bg))
6042 return 0; /* same as default */
6043 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
6044 return 0; /* not a valid color */
9717e36c
MB
6045 else if (color_distance (&bg_tty_color, &bg_std_color)
6046 > TTY_SAME_COLOR_THRESHOLD)
8e330b22
MB
6047 return 0; /* displayed color is too different */
6048 else
6049 /* Make sure the color is really different than the default. */
6050 {
6051 XColor def_bg_color;
6052 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
6053 && (color_distance (&bg_tty_color, &def_bg_color)
6054 <= TTY_SAME_COLOR_THRESHOLD))
6055 return 0;
6056 }
9717e36c
MB
6057 }
6058
6059 /* If both foreground and background are requested, see if the
6060 distance between them is OK. We just check to see if the distance
6061 between the tty's foreground and background is close enough to the
6062 distance between the standard foreground and background. */
6063 if (STRINGP (fg) && STRINGP (bg))
6064 {
6065 int delta_delta
6066 = (color_distance (&fg_std_color, &bg_std_color)
6067 - color_distance (&fg_tty_color, &bg_tty_color));
6068 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
6069 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
6070 return 0;
6071 }
6072
6073
6074 /* See if the capabilities we selected above are supported, with the
6075 given colors. */
6076 if (test_caps != 0 &&
3d63dd9d 6077 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
9717e36c
MB
6078 return 0;
6079
6080
6081 /* Hmmm, everything checks out, this terminal must support this face. */
6082 return 1;
6083}
6084
6085
6086DEFUN ("display-supports-face-attributes-p",
6087 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
6088 1, 2, 0,
6089 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
6090The optional argument DISPLAY can be a display name, a frame, or
6091nil (meaning the selected frame's display)
6092
6093The definition of `supported' is somewhat heuristic, but basically means
6094that a face containing all the attributes in ATTRIBUTES, when merged
6095with the default face for display, can be represented in a way that's
6096
6097 \(1) different in appearance than the default face, and
6098 \(2) `close in spirit' to what the attributes specify, if not exact.
6099
6100Point (2) implies that a `:weight black' attribute will be satisfied by
6101any display that can display bold, and a `:foreground \"yellow\"' as long
6102as it can display a yellowish color, but `:slant italic' will _not_ be
6103satisfied by the tty display code's automatic substitution of a `dim'
6104face for italic. */)
6105 (attributes, display)
6106 Lisp_Object attributes, display;
6107{
6108 int supports, i;
6109 Lisp_Object frame;
6110 struct frame *f;
8e330b22 6111 struct face *def_face;
9717e36c
MB
6112 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6113
0722292b
MB
6114 if (noninteractive || !initialized)
6115 /* We may not be able to access low-level face information in batch
6116 mode, or before being dumped, and this function is not going to
6117 be very useful in those cases anyway, so just give up. */
6118 return Qnil;
6119
9717e36c
MB
6120 if (NILP (display))
6121 frame = selected_frame;
6122 else if (FRAMEP (display))
6123 frame = display;
6124 else
6125 {
6126 /* Find any frame on DISPLAY. */
6127 Lisp_Object fl_tail;
6128
6129 frame = Qnil;
6130 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
6131 {
6132 frame = XCAR (fl_tail);
6133 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
6134 XFRAME (frame)->param_alist)),
6135 display)))
6136 break;
6137 }
6138 }
6139
6140 CHECK_LIVE_FRAME (frame);
6141 f = XFRAME (frame);
6142
6143 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
6144 attrs[i] = Qunspecified;
a0a23346 6145 merge_face_ref (f, attributes, attrs, 1, 0);
9717e36c 6146
8e330b22
MB
6147 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6148 if (def_face == NULL)
6149 {
6150 if (! realize_basic_faces (f))
734e9514 6151 error ("Cannot realize default face");
8e330b22
MB
6152 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6153 }
6154
9717e36c
MB
6155 /* Dispatch to the appropriate handler. */
6156 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
8e330b22 6157 supports = tty_supports_face_attributes_p (f, attrs, def_face);
ccda4e3c 6158#ifdef HAVE_WINDOW_SYSTEM
9717e36c 6159 else
8e330b22
MB
6160 supports = x_supports_face_attributes_p (f, attrs, def_face);
6161#endif
9717e36c
MB
6162
6163 return supports ? Qt : Qnil;
6164}
6165
6166\f
82641697
GM
6167/***********************************************************************
6168 Font selection
6169 ***********************************************************************/
6170
6171DEFUN ("internal-set-font-selection-order",
6172 Finternal_set_font_selection_order,
6173 Sinternal_set_font_selection_order, 1, 1, 0,
7ee72033 6174 doc: /* Set font selection order for face font selection to ORDER.
228299fa
GM
6175ORDER must be a list of length 4 containing the symbols `:width',
6176`:height', `:weight', and `:slant'. Face attributes appearing
6177first in ORDER are matched first, e.g. if `:height' appears before
6178`:weight' in ORDER, font selection first tries to find a font with
6179a suitable height, and then tries to match the font weight.
7ee72033
MB
6180Value is ORDER. */)
6181 (order)
228299fa 6182 Lisp_Object order;
82641697
GM
6183{
6184 Lisp_Object list;
6185 int i;
a08332c0 6186 int indices[DIM (font_sort_order)];
178c5d9c 6187
b7826503 6188 CHECK_LIST (order);
82641697
GM
6189 bzero (indices, sizeof indices);
6190 i = 0;
6191
6192 for (list = order;
6193 CONSP (list) && i < DIM (indices);
6194 list = XCDR (list), ++i)
6195 {
6196 Lisp_Object attr = XCAR (list);
6197 int xlfd;
6198
6199 if (EQ (attr, QCwidth))
6200 xlfd = XLFD_SWIDTH;
6201 else if (EQ (attr, QCheight))
6202 xlfd = XLFD_POINT_SIZE;
6203 else if (EQ (attr, QCweight))
6204 xlfd = XLFD_WEIGHT;
6205 else if (EQ (attr, QCslant))
6206 xlfd = XLFD_SLANT;
6207 else
6208 break;
6209
6210 if (indices[i] != 0)
6211 break;
6212 indices[i] = xlfd;
6213 }
6214
a08332c0 6215 if (!NILP (list) || i != DIM (indices))
82641697 6216 signal_error ("Invalid font sort order", order);
a08332c0
GM
6217 for (i = 0; i < DIM (font_sort_order); ++i)
6218 if (indices[i] == 0)
6219 signal_error ("Invalid font sort order", order);
82641697
GM
6220
6221 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
6222 {
6223 bcopy (indices, font_sort_order, sizeof font_sort_order);
6224 free_all_realized_faces (Qnil);
6225 }
178c5d9c 6226
82641697
GM
6227 return Qnil;
6228}
6229
6230
6231DEFUN ("internal-set-alternative-font-family-alist",
6232 Finternal_set_alternative_font_family_alist,
6233 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
c71f3632 6234 doc: /* Define alternative font families to try in face font selection.
228299fa
GM
6235ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6236Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
7ee72033
MB
6237be found. Value is ALIST. */)
6238 (alist)
82641697
GM
6239 Lisp_Object alist;
6240{
b7826503 6241 CHECK_LIST (alist);
82641697
GM
6242 Vface_alternative_font_family_alist = alist;
6243 free_all_realized_faces (Qnil);
6244 return alist;
6245}
6246
6247
32fcc231
GM
6248DEFUN ("internal-set-alternative-font-registry-alist",
6249 Finternal_set_alternative_font_registry_alist,
6250 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
e3cd9e7f 6251 doc: /* Define alternative font registries to try in face font selection.
228299fa
GM
6252ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6253Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
7ee72033
MB
6254be found. Value is ALIST. */)
6255 (alist)
32fcc231
GM
6256 Lisp_Object alist;
6257{
b7826503 6258 CHECK_LIST (alist);
32fcc231
GM
6259 Vface_alternative_font_registry_alist = alist;
6260 free_all_realized_faces (Qnil);
6261 return alist;
6262}
6263
6264
c3cee013 6265#ifdef HAVE_WINDOW_SYSTEM
82641697 6266
82641697
GM
6267/* Value is non-zero if FONT is the name of a scalable font. The
6268 X11R6 XLFD spec says that point size, pixel size, and average width
6269 are zero for scalable fonts. Intlfonts contain at least one
6270 scalable font ("*-muleindian-1") for which this isn't true, so we
6271 just test average width. */
6272
6273static int
6274font_scalable_p (font)
6275 struct font_name *font;
6276{
6277 char *s = font->fields[XLFD_AVGWIDTH];
c3cee013
JR
6278 return (*s == '0' && *(s + 1) == '\0')
6279#ifdef WINDOWSNT
6280 /* Windows implementation of XLFD is slightly broken for backward
6281 compatibility with previous broken versions, so test for
6282 wildcards as well as 0. */
6283 || *s == '*'
6284#endif
6285 ;
82641697
GM
6286}
6287
6288
8e1b21a7
KH
6289/* Ignore the difference of font point size less than this value. */
6290
6291#define FONT_POINT_SIZE_QUANTUM 5
6292
82641697
GM
6293/* Value is non-zero if FONT1 is a better match for font attributes
6294 VALUES than FONT2. VALUES is an array of face attribute values in
6295 font sort order. COMPARE_PT_P zero means don't compare point
a08332c0
GM
6296 sizes. AVGWIDTH, if not zero, is a specified font average width
6297 to compare with. */
82641697
GM
6298
6299static int
a08332c0 6300better_font_p (values, font1, font2, compare_pt_p, avgwidth)
82641697
GM
6301 int *values;
6302 struct font_name *font1, *font2;
a08332c0 6303 int compare_pt_p, avgwidth;
82641697
GM
6304{
6305 int i;
178c5d9c 6306
a08332c0 6307 for (i = 0; i < DIM (font_sort_order); ++i)
82641697
GM
6308 {
6309 int xlfd_idx = font_sort_order[i];
6310
6311 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
6312 {
f70400f2
KH
6313 int delta1, delta2;
6314
6315 if (xlfd_idx == XLFD_POINT_SIZE)
6316 {
6317 delta1 = abs (values[i] - (font1->numeric[xlfd_idx]
6318 / font1->rescale_ratio));
6319 delta2 = abs (values[i] - (font2->numeric[xlfd_idx]
6320 / font2->rescale_ratio));
6321 if (abs (delta1 - delta2) < FONT_POINT_SIZE_QUANTUM)
6322 continue;
6323 }
6324 else
6325 {
6326 delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
6327 delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
6328 }
178c5d9c 6329
82641697
GM
6330 if (delta1 > delta2)
6331 return 0;
6332 else if (delta1 < delta2)
6333 return 1;
6334 else
6335 {
6336 /* The difference may be equal because, e.g., the face
6337 specifies `italic' but we have only `regular' and
6338 `oblique'. Prefer `oblique' in this case. */
6339 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
6340 && font1->numeric[xlfd_idx] > values[i]
6341 && font2->numeric[xlfd_idx] < values[i])
6342 return 1;
6343 }
6344 }
6345 }
178c5d9c 6346
a08332c0
GM
6347 if (avgwidth)
6348 {
6349 int delta1 = abs (avgwidth - font1->numeric[XLFD_AVGWIDTH]);
6350 int delta2 = abs (avgwidth - font2->numeric[XLFD_AVGWIDTH]);
6351 if (delta1 > delta2)
6352 return 0;
6353 else if (delta1 < delta2)
6354 return 1;
6355 }
6356
10b7e56f
KH
6357 if (! compare_pt_p)
6358 {
6359 /* We prefer a real scalable font; i.e. not what autoscaled. */
6360 int auto_scaled_1 = (font1->numeric[XLFD_POINT_SIZE] == 0
6361 && font1->numeric[XLFD_RESY] > 0);
6362 int auto_scaled_2 = (font2->numeric[XLFD_POINT_SIZE] == 0
6363 && font2->numeric[XLFD_RESY] > 0);
6364
6365 if (auto_scaled_1 != auto_scaled_2)
6366 return auto_scaled_2;
6367 }
6368
a08332c0 6369 return font1->registry_priority < font2->registry_priority;
82641697
GM
6370}
6371
6372
82641697
GM
6373/* Value is non-zero if FONT is an exact match for face attributes in
6374 SPECIFIED. SPECIFIED is an array of face attribute values in font
a08332c0
GM
6375 sort order. AVGWIDTH, if non-zero, is an average width to compare
6376 with. */
82641697
GM
6377
6378static int
a08332c0 6379exact_face_match_p (specified, font, avgwidth)
82641697
GM
6380 int *specified;
6381 struct font_name *font;
a08332c0 6382 int avgwidth;
82641697
GM
6383{
6384 int i;
178c5d9c 6385
a08332c0 6386 for (i = 0; i < DIM (font_sort_order); ++i)
82641697
GM
6387 if (specified[i] != font->numeric[font_sort_order[i]])
6388 break;
6389
a08332c0
GM
6390 return (i == DIM (font_sort_order)
6391 && (avgwidth <= 0
6392 || avgwidth == font->numeric[XLFD_AVGWIDTH]));
82641697
GM
6393}
6394
6395
6396/* Value is the name of a scaled font, generated from scalable font
6397 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
6398 Value is allocated from heap. */
6399
6400static char *
6401build_scalable_font_name (f, font, specified_pt)
6402 struct frame *f;
6403 struct font_name *font;
6404 int specified_pt;
6405{
a7255152 6406 char pixel_size[20];
82641697
GM
6407 int pixel_value;
6408 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
6409 double pt;
6410
6411 /* If scalable font is for a specific resolution, compute
6412 the point size we must specify from the resolution of
6413 the display and the specified resolution of the font. */
6414 if (font->numeric[XLFD_RESY] != 0)
6415 {
6416 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
c660ce4e 6417 pixel_value = font->numeric[XLFD_RESY] / (PT_PER_INCH * 10.0) * pt;
82641697
GM
6418 }
6419 else
6420 {
6421 pt = specified_pt;
c660ce4e 6422 pixel_value = resy / (PT_PER_INCH * 10.0) * pt;
82641697 6423 }
f70400f2
KH
6424 /* We may need a font of the different size. */
6425 pixel_value *= font->rescale_ratio;
178c5d9c 6426
f70400f2
KH
6427 /* We should keep POINT_SIZE 0. Otherwise, X server can't open a
6428 font of the specified PIXEL_SIZE. */
6429#if 0
a7255152
SM
6430 { /* Set point size of the font. */
6431 char point_size[20];
6432 sprintf (point_size, "%d", (int) pt);
6433 font->fields[XLFD_POINT_SIZE] = point_size;
6434 font->numeric[XLFD_POINT_SIZE] = pt;
6435 }
f70400f2 6436#endif
178c5d9c 6437
82641697
GM
6438 /* Set pixel size. */
6439 sprintf (pixel_size, "%d", pixel_value);
6440 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
6441 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
178c5d9c 6442
82641697
GM
6443 /* If font doesn't specify its resolution, use the
6444 resolution of the display. */
6445 if (font->numeric[XLFD_RESY] == 0)
6446 {
6447 char buffer[20];
6448 sprintf (buffer, "%d", (int) resy);
6449 font->fields[XLFD_RESY] = buffer;
6450 font->numeric[XLFD_RESY] = resy;
6451 }
178c5d9c 6452
82641697
GM
6453 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
6454 {
6455 char buffer[20];
6456 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
6457 sprintf (buffer, "%d", resx);
6458 font->fields[XLFD_RESX] = buffer;
6459 font->numeric[XLFD_RESX] = resx;
6460 }
6461
6462 return build_font_name (font);
6463}
6464
6465
6466/* Value is non-zero if we are allowed to use scalable font FONT. We
6467 can't run a Lisp function here since this function may be called
6468 with input blocked. */
6469
6470static int
702a1e8e 6471may_use_scalable_font_p (font)
53c208f6 6472 const char *font;
82641697
GM
6473{
6474 if (EQ (Vscalable_fonts_allowed, Qt))
6475 return 1;
6476 else if (CONSP (Vscalable_fonts_allowed))
6477 {
6478 Lisp_Object tail, regexp;
178c5d9c 6479
82641697
GM
6480 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
6481 {
6482 regexp = XCAR (tail);
6483 if (STRINGP (regexp)
702a1e8e 6484 && fast_c_string_match_ignore_case (regexp, font) >= 0)
82641697
GM
6485 return 1;
6486 }
6487 }
178c5d9c 6488
82641697
GM
6489 return 0;
6490}
6491
82641697
GM
6492
6493
2e6621ca
GM
6494/* Return the name of the best matching font for face attributes ATTRS
6495 in the array of font_name structures FONTS which contains NFONTS
6496 elements. WIDTH_RATIO is a factor with which to multiply average
6497 widths if ATTRS specifies such a width.
6498
6499 Value is a font name which is allocated from the heap. FONTS is
6ae2cd57
MB
6500 freed by this function.
6501
6502 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6503 indicate whether the resulting font should be drawn using overstrike
6504 to simulate bold-face. */
82641697
GM
6505
6506static char *
6ae2cd57 6507best_matching_font (f, attrs, fonts, nfonts, width_ratio, needs_overstrike)
82641697
GM
6508 struct frame *f;
6509 Lisp_Object *attrs;
6510 struct font_name *fonts;
6511 int nfonts;
2e6621ca 6512 int width_ratio;
6ae2cd57 6513 int *needs_overstrike;
82641697
GM
6514{
6515 char *font_name;
6516 struct font_name *best;
334a2e2a 6517 int i, pt = 0;
a08332c0
GM
6518 int specified[5];
6519 int exact_p, avgwidth;
82641697
GM
6520
6521 if (nfonts == 0)
6522 return NULL;
6523
6524 /* Make specified font attributes available in `specified',
6525 indexed by sort order. */
6526 for (i = 0; i < DIM (font_sort_order); ++i)
6527 {
6528 int xlfd_idx = font_sort_order[i];
178c5d9c 6529
82641697
GM
6530 if (xlfd_idx == XLFD_SWIDTH)
6531 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
6532 else if (xlfd_idx == XLFD_POINT_SIZE)
6533 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
6534 else if (xlfd_idx == XLFD_WEIGHT)
6535 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
6536 else if (xlfd_idx == XLFD_SLANT)
6537 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
6538 else
6539 abort ();
6540 }
6541
a08332c0
GM
6542 avgwidth = (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
6543 ? 0
2e6621ca 6544 : XFASTINT (attrs[LFACE_AVGWIDTH_INDEX]) * width_ratio);
a08332c0 6545
82641697 6546 exact_p = 0;
178c5d9c 6547
6ae2cd57
MB
6548 if (needs_overstrike)
6549 *needs_overstrike = 0;
6550
82641697
GM
6551 /* Start with the first non-scalable font in the list. */
6552 for (i = 0; i < nfonts; ++i)
6553 if (!font_scalable_p (fonts + i))
6554 break;
6555
6556 /* Find the best match among the non-scalable fonts. */
6557 if (i < nfonts)
6558 {
6559 best = fonts + i;
178c5d9c 6560
82641697
GM
6561 for (i = 1; i < nfonts; ++i)
6562 if (!font_scalable_p (fonts + i)
a08332c0 6563 && better_font_p (specified, fonts + i, best, 1, avgwidth))
82641697
GM
6564 {
6565 best = fonts + i;
6566
a08332c0 6567 exact_p = exact_face_match_p (specified, best, avgwidth);
82641697
GM
6568 if (exact_p)
6569 break;
6570 }
82641697
GM
6571 }
6572 else
6573 best = NULL;
6574
6575 /* Unless we found an exact match among non-scalable fonts, see if
6576 we can find a better match among scalable fonts. */
6577 if (!exact_p)
6578 {
6579 /* A scalable font is better if
6580
6581 1. its weight, slant, swidth attributes are better, or.
178c5d9c 6582
82641697
GM
6583 2. the best non-scalable font doesn't have the required
6584 point size, and the scalable fonts weight, slant, swidth
6585 isn't worse. */
6586
6587 int non_scalable_has_exact_height_p;
6588
6589 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
6590 non_scalable_has_exact_height_p = 1;
6591 else
6592 non_scalable_has_exact_height_p = 0;
178c5d9c 6593
82641697
GM
6594 for (i = 0; i < nfonts; ++i)
6595 if (font_scalable_p (fonts + i))
6596 {
6597 if (best == NULL
a08332c0 6598 || better_font_p (specified, fonts + i, best, 0, 0)
82641697 6599 || (!non_scalable_has_exact_height_p
a08332c0 6600 && !better_font_p (specified, best, fonts + i, 0, 0)))
10b7e56f
KH
6601 {
6602 non_scalable_has_exact_height_p = 1;
6603 best = fonts + i;
6604 }
82641697 6605 }
6ae2cd57
MB
6606
6607 if (needs_overstrike)
6608 {
6609 enum xlfd_weight want_weight = specified[XLFD_WEIGHT];
6610 enum xlfd_weight got_weight = best->numeric[XLFD_WEIGHT];
6611
6612 if (want_weight > XLFD_WEIGHT_MEDIUM && want_weight > got_weight)
6613 {
6614 /* We want a bold font, but didn't get one; try to use
6615 overstriking instead to simulate bold-face. However,
6616 don't overstrike an already-bold fontn unless the
6617 desired weight grossly exceeds the available weight. */
6618 if (got_weight > XLFD_WEIGHT_MEDIUM)
6619 *needs_overstrike = (got_weight - want_weight) > 2;
6620 else
6621 *needs_overstrike = 1;
6622 }
6623 }
82641697
GM
6624 }
6625
6626 if (font_scalable_p (best))
6627 font_name = build_scalable_font_name (f, best, pt);
6628 else
6629 font_name = build_font_name (best);
178c5d9c 6630
82641697
GM
6631 /* Free font_name structures. */
6632 free_font_names (fonts, nfonts);
178c5d9c 6633
82641697
GM
6634 return font_name;
6635}
6636
6637
3cf80731
SM
6638/* Get a list of matching fonts on frame F, considering FAMILY
6639 and alternative font families from Vface_alternative_font_registry_alist.
4a529c42
GM
6640
6641 FAMILY is the font family whose alternatives are considered.
6642
6643 REGISTRY, if a string, specifies a font registry and encoding to
6644 match. A value of nil means include fonts of any registry and
6645 encoding.
177c0ea7 6646
4a529c42
GM
6647 Return in *FONTS a pointer to a vector of font_name structures for
6648 the fonts matched. Value is the number of fonts found. */
6649
6650static int
6651try_alternative_families (f, family, registry, fonts)
6652 struct frame *f;
6653 Lisp_Object family, registry;
6654 struct font_name **fonts;
6655{
6656 Lisp_Object alter;
6657 int nfonts = 0;
6658
3cf80731
SM
6659 nfonts = font_list (f, Qnil, family, registry, fonts);
6660 if (nfonts == 0)
4a529c42 6661 {
3cf80731
SM
6662 /* Try alternative font families. */
6663 alter = Fassoc (family, Vface_alternative_font_family_alist);
6664 if (CONSP (alter))
6665 {
6666 for (alter = XCDR (alter);
6667 CONSP (alter) && nfonts == 0;
6668 alter = XCDR (alter))
6669 {
6670 if (STRINGP (XCAR (alter)))
6671 nfonts = font_list (f, Qnil, XCAR (alter), registry, fonts);
6672 }
6673 }
177c0ea7 6674
f9a45bb3
KH
6675 /* Try all scalable fonts before giving up. */
6676 if (nfonts == 0 && ! EQ (Vscalable_fonts_allowed, Qt))
4a529c42 6677 {
331379bf 6678 int count = SPECPDL_INDEX ();
3cf80731
SM
6679 specbind (Qscalable_fonts_allowed, Qt);
6680 nfonts = try_alternative_families (f, family, registry, fonts);
6681 unbind_to (count, Qnil);
4a529c42
GM
6682 }
6683 }
4a529c42
GM
6684 return nfonts;
6685}
6686
6687
702a1e8e
GM
6688/* Get a list of matching fonts on frame F.
6689
3cf80731
SM
6690 FAMILY, if a string, specifies a font family derived from the fontset.
6691 It is only used if the face does not specify any family in ATTRS or
6692 if we cannot find any font of the face's family.
702a1e8e
GM
6693
6694 REGISTRY, if a string, specifies a font registry and encoding to
6695 match. A value of nil means include fonts of any registry and
6696 encoding.
177c0ea7 6697
54580ab2
KH
6698 If PREFER_FACE_FAMILY is nonzero, perfer face's family to FAMILY.
6699 Otherwise, prefer FAMILY.
6700
702a1e8e
GM
6701 Return in *FONTS a pointer to a vector of font_name structures for
6702 the fonts matched. Value is the number of fonts found. */
82641697
GM
6703
6704static int
54580ab2 6705try_font_list (f, attrs, family, registry, fonts, prefer_face_family)
82641697
GM
6706 struct frame *f;
6707 Lisp_Object *attrs;
702a1e8e 6708 Lisp_Object family, registry;
82641697 6709 struct font_name **fonts;
54580ab2 6710 int prefer_face_family;
82641697 6711{
4a529c42 6712 int nfonts = 0;
3cf80731 6713 Lisp_Object face_family = attrs[LFACE_FAMILY_INDEX];
54580ab2
KH
6714 Lisp_Object try_family;
6715
e21880c6 6716 try_family = (prefer_face_family || NILP (family)) ? face_family : family;
82641697 6717
54580ab2
KH
6718 if (STRINGP (try_family))
6719 nfonts = try_alternative_families (f, try_family, registry, fonts);
3cf80731 6720
f00691a3
AC
6721#ifdef MAC_OS
6722 /* When realizing the default face and a font spec does not matched
6723 exactly, Emacs looks for ones with the same registry as the
6724 default font. On the Mac, this is mac-roman, which does not work
6725 if the family is -etl-fixed, e.g. The following widens the
6726 choices and fixes that problem. */
54580ab2 6727 if (nfonts == 0 && STRINGP (try_family) && STRINGP (registry)
d5db4077 6728 && xstricmp (SDATA (registry), "mac-roman") == 0)
54580ab2 6729 nfonts = try_alternative_families (f, try_family, Qnil, fonts);
f00691a3
AC
6730#endif
6731
e21880c6 6732 if (EQ (try_family, family))
54580ab2
KH
6733 family = face_family;
6734
6735 if (nfonts == 0 && STRINGP (family))
3cf80731 6736 nfonts = try_alternative_families (f, family, registry, fonts);
178c5d9c 6737
3cf80731 6738 /* Try font family of the default face or "fixed". */
4a529c42 6739 if (nfonts == 0)
82641697 6740 {
3cf80731
SM
6741 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6742 if (default_face)
6743 family = default_face->lface[LFACE_FAMILY_INDEX];
6744 else
6745 family = build_string ("fixed");
4a529c42 6746 nfonts = font_list (f, Qnil, family, registry, fonts);
82641697 6747 }
177c0ea7 6748
3cf80731
SM
6749 /* Try any family with the given registry. */
6750 if (nfonts == 0)
6aa1813f 6751 nfonts = try_alternative_families (f, Qnil, registry, fonts);
82641697
GM
6752
6753 return nfonts;
6754}
6755
82641697 6756
39506348
KH
6757/* Return the fontset id of the base fontset name or alias name given
6758 by the fontset attribute of ATTRS. Value is -1 if the fontset
6759 attribute of ATTRS doesn't name a fontset. */
82641697
GM
6760
6761static int
39506348 6762face_fontset (attrs)
82641697
GM
6763 Lisp_Object *attrs;
6764{
39506348 6765 Lisp_Object name;
178c5d9c 6766
39506348
KH
6767 name = attrs[LFACE_FONT_INDEX];
6768 if (!STRINGP (name))
6769 return -1;
6770 return fs_query_fontset (name, 0);
82641697
GM
6771}
6772
6773
39506348
KH
6774/* Choose a name of font to use on frame F to display character C with
6775 Lisp face attributes specified by ATTRS. The font name is
6776 determined by the font-related attributes in ATTRS and the name
6777 pattern for C in FONTSET. Value is the font name which is
6778 allocated from the heap and must be freed by the caller, or NULL if
6779 we can get no information about the font name of C. It is assured
6780 that we always get some information for a single byte
6ae2cd57
MB
6781 character.
6782
6783 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6784 indicate whether the resulting font should be drawn using overstrike
6785 to simulate bold-face. */
82641697
GM
6786
6787static char *
6ae2cd57 6788choose_face_font (f, attrs, fontset, c, needs_overstrike)
82641697
GM
6789 struct frame *f;
6790 Lisp_Object *attrs;
39506348 6791 int fontset, c;
6ae2cd57 6792 int *needs_overstrike;
82641697 6793{
39506348 6794 Lisp_Object pattern;
82641697 6795 char *font_name = NULL;
82641697 6796 struct font_name *fonts;
2e6621ca 6797 int nfonts, width_ratio;
178c5d9c 6798
774666fb
MB
6799 if (needs_overstrike)
6800 *needs_overstrike = 0;
6801
39506348
KH
6802 /* Get (foundry and) family name and registry (and encoding) name of
6803 a font for C. */
6804 pattern = fontset_font_pattern (f, fontset, c);
6805 if (NILP (pattern))
6806 {
6807 xassert (!SINGLE_BYTE_CHAR_P (c));
6808 return NULL;
6809 }
177c0ea7 6810
39506348
KH
6811 /* If what we got is a name pattern, return it. */
6812 if (STRINGP (pattern))
d5db4077 6813 return xstrdup (SDATA (pattern));
82641697 6814
178c5d9c 6815 /* Get a list of fonts matching that pattern and choose the
82641697 6816 best match for the specified face attributes from it. */
54580ab2
KH
6817 nfonts = try_font_list (f, attrs, XCAR (pattern), XCDR (pattern), &fonts,
6818 (SINGLE_BYTE_CHAR_P (c)
6819 || CHAR_CHARSET (c) == charset_latin_iso8859_1));
2e6621ca
GM
6820 width_ratio = (SINGLE_BYTE_CHAR_P (c)
6821 ? 1
6822 : CHARSET_WIDTH (CHAR_CHARSET (c)));
6ae2cd57
MB
6823 font_name = best_matching_font (f, attrs, fonts, nfonts, width_ratio,
6824 needs_overstrike);
82641697
GM
6825 return font_name;
6826}
6827
c3cee013 6828#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
6829
6830
6831\f
6832/***********************************************************************
6833 Face Realization
6834 ***********************************************************************/
6835
6836/* Realize basic faces on frame F. Value is zero if frame parameters
6837 of F don't contain enough information needed to realize the default
6838 face. */
6839
6840static int
6841realize_basic_faces (f)
6842 struct frame *f;
6843{
6844 int success_p = 0;
331379bf 6845 int count = SPECPDL_INDEX ();
17e8204b 6846
04386463
GM
6847 /* Block input here so that we won't be surprised by an X expose
6848 event, for instance, without having the faces set up. */
17e8204b 6849 BLOCK_INPUT;
eeffb293 6850 specbind (Qscalable_fonts_allowed, Qt);
178c5d9c 6851
82641697
GM
6852 if (realize_default_face (f))
6853 {
92610620 6854 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
039b6394 6855 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
9ea173e8 6856 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
4e50fa8b 6857 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
045dee35 6858 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
8bd201d6
GM
6859 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
6860 realize_named_face (f, Qborder, BORDER_FACE_ID);
6861 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
6862 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
c7ae3284 6863 realize_named_face (f, Qmenu, MENU_FACE_ID);
563f68f1 6864
b5de343d 6865 /* Reflect changes in the `menu' face in menu bars. */
ceeda019 6866 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
563f68f1 6867 {
ceeda019 6868 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
563f68f1 6869#ifdef USE_X_TOOLKIT
daf01701
KL
6870 if (FRAME_WINDOW_P (f))
6871 x_update_menu_appearance (f);
a03ad468 6872#endif
563f68f1 6873 }
177c0ea7 6874
82641697
GM
6875 success_p = 1;
6876 }
6877
eeffb293 6878 unbind_to (count, Qnil);
17e8204b 6879 UNBLOCK_INPUT;
82641697
GM
6880 return success_p;
6881}
6882
6883
6884/* Realize the default face on frame F. If the face is not fully
6885 specified, make it fully-specified. Attributes of the default face
6886 that are not explicitly specified are taken from frame parameters. */
6887
6888static int
6889realize_default_face (f)
6890 struct frame *f;
6891{
6892 struct face_cache *c = FRAME_FACE_CACHE (f);
6893 Lisp_Object lface;
6894 Lisp_Object attrs[LFACE_VECTOR_SIZE];
82641697
GM
6895 Lisp_Object frame_font;
6896 struct face *face;
82641697
GM
6897
6898 /* If the `default' face is not yet known, create it. */
6899 lface = lface_from_face_name (f, Qdefault, 0);
6900 if (NILP (lface))
07446869
GM
6901 {
6902 Lisp_Object frame;
6903 XSETFRAME (frame, f);
6904 lface = Finternal_make_lisp_face (Qdefault, frame);
6905 }
6906
82641697 6907
c3cee013
JR
6908#ifdef HAVE_WINDOW_SYSTEM
6909 if (FRAME_WINDOW_P (f))
82641697
GM
6910 {
6911 /* Set frame_font to the value of the `font' frame parameter. */
6912 frame_font = Fassq (Qfont, f->param_alist);
6913 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
6914 frame_font = XCDR (frame_font);
a5f696ac
JD
6915 set_lface_from_font_name (f, lface, frame_font,
6916 f->default_face_done_p, 1);
6917 f->default_face_done_p = 1;
82641697 6918 }
c3cee013 6919#endif /* HAVE_WINDOW_SYSTEM */
82641697 6920
44747bd0 6921 if (!FRAME_WINDOW_P (f))
82641697
GM
6922 {
6923 LFACE_FAMILY (lface) = build_string ("default");
6924 LFACE_SWIDTH (lface) = Qnormal;
6925 LFACE_HEIGHT (lface) = make_number (1);
c1e7532d
EZ
6926 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
6927 LFACE_WEIGHT (lface) = Qnormal;
6928 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
6929 LFACE_SLANT (lface) = Qnormal;
a08332c0 6930 LFACE_AVGWIDTH (lface) = Qunspecified;
82641697 6931 }
178c5d9c 6932
82641697
GM
6933 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
6934 LFACE_UNDERLINE (lface) = Qnil;
178c5d9c 6935
82641697
GM
6936 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
6937 LFACE_OVERLINE (lface) = Qnil;
178c5d9c 6938
82641697
GM
6939 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
6940 LFACE_STRIKE_THROUGH (lface) = Qnil;
178c5d9c 6941
82641697
GM
6942 if (UNSPECIFIEDP (LFACE_BOX (lface)))
6943 LFACE_BOX (lface) = Qnil;
178c5d9c 6944
82641697
GM
6945 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
6946 LFACE_INVERSE (lface) = Qnil;
178c5d9c 6947
82641697
GM
6948 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
6949 {
6950 /* This function is called so early that colors are not yet
6951 set in the frame parameter list. */
6952 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
178c5d9c 6953
82641697
GM
6954 if (CONSP (color) && STRINGP (XCDR (color)))
6955 LFACE_FOREGROUND (lface) = XCDR (color);
c3cee013 6956 else if (FRAME_WINDOW_P (f))
82641697 6957 return 0;
3224dac1 6958 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
ef917393 6959 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
f9d2fdc4 6960 else
82641697
GM
6961 abort ();
6962 }
178c5d9c 6963
82641697
GM
6964 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
6965 {
6966 /* This function is called so early that colors are not yet
6967 set in the frame parameter list. */
6968 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
6969 if (CONSP (color) && STRINGP (XCDR (color)))
6970 LFACE_BACKGROUND (lface) = XCDR (color);
c3cee013 6971 else if (FRAME_WINDOW_P (f))
82641697 6972 return 0;
3224dac1 6973 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
ef917393 6974 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
f9d2fdc4 6975 else
82641697
GM
6976 abort ();
6977 }
178c5d9c 6978
82641697
GM
6979 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
6980 LFACE_STIPPLE (lface) = Qnil;
6981
6982 /* Realize the face; it must be fully-specified now. */
6983 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
6984 check_lface (lface);
6985 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
39506348 6986 face = realize_face (c, attrs, 0, NULL, DEFAULT_FACE_ID);
82641697
GM
6987 return 1;
6988}
6989
6990
6991/* Realize basic faces other than the default face in face cache C.
6992 SYMBOL is the face name, ID is the face id the realized face must
6993 have. The default face must have been realized already. */
6994
6995static void
6996realize_named_face (f, symbol, id)
6997 struct frame *f;
6998 Lisp_Object symbol;
6999 int id;
7000{
7001 struct face_cache *c = FRAME_FACE_CACHE (f);
7002 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
7003 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7004 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
7005 struct face *new_face;
7006
7007 /* The default face must exist and be fully specified. */
7008 get_lface_attributes (f, Qdefault, attrs, 1);
7009 check_lface_attrs (attrs);
7010 xassert (lface_fully_specified_p (attrs));
7011
7012 /* If SYMBOL isn't know as a face, create it. */
7013 if (NILP (lface))
7014 {
7015 Lisp_Object frame;
7016 XSETFRAME (frame, f);
7017 lface = Finternal_make_lisp_face (symbol, frame);
7018 }
7019
7020 /* Merge SYMBOL's face with the default face. */
7021 get_lface_attributes (f, symbol, symbol_attrs, 1);
a0a23346 7022 merge_face_vectors (f, symbol_attrs, attrs, 0);
82641697
GM
7023
7024 /* Realize the face. */
39506348 7025 new_face = realize_face (c, attrs, 0, NULL, id);
82641697
GM
7026}
7027
7028
7029/* Realize the fully-specified face with attributes ATTRS in face
39506348 7030 cache CACHE for character C. If C is a multibyte character,
0badc114
KH
7031 BASE_FACE is a face that has the same attributes. Otherwise,
7032 BASE_FACE is ignored. If FORMER_FACE_ID is non-negative, it is an
7033 ID of face to remove before caching the new face. Value is a
7034 pointer to the newly created realized face. */
82641697
GM
7035
7036static struct face *
39506348
KH
7037realize_face (cache, attrs, c, base_face, former_face_id)
7038 struct face_cache *cache;
82641697 7039 Lisp_Object *attrs;
39506348
KH
7040 int c;
7041 struct face *base_face;
7042 int former_face_id;
82641697
GM
7043{
7044 struct face *face;
178c5d9c 7045
82641697 7046 /* LFACE must be fully specified. */
39506348 7047 xassert (cache != NULL);
82641697
GM
7048 check_lface_attrs (attrs);
7049
39506348
KH
7050 if (former_face_id >= 0 && cache->used > former_face_id)
7051 {
7052 /* Remove the former face. */
7053 struct face *former_face = cache->faces_by_id[former_face_id];
7054 uncache_face (cache, former_face);
7055 free_realized_face (cache->f, former_face);
7056 }
7057
7058 if (FRAME_WINDOW_P (cache->f))
7059 face = realize_x_face (cache, attrs, c, base_face);
d448e982 7060 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
39506348 7061 face = realize_tty_face (cache, attrs, c);
d448e982
KL
7062 else if (FRAME_INITIAL_P (cache->f))
7063 {
7064 /* Create a dummy face. */
7065 face = make_realized_face (attrs);
7066 }
82641697
GM
7067 else
7068 abort ();
7069
39506348
KH
7070 /* Insert the new face. */
7071 cache_face (cache, face, lface_hash (attrs));
7072#ifdef HAVE_WINDOW_SYSTEM
192cb6cf 7073 if (FRAME_WINDOW_P (cache->f) && face->font == NULL)
39506348
KH
7074 load_face_font (cache->f, face, c);
7075#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
7076 return face;
7077}
7078
7079
7080/* Realize the fully-specified face with attributes ATTRS in face
39506348 7081 cache CACHE for character C. Do it for X frame CACHE->f. If C is
0badc114
KH
7082 a multibyte character, BASE_FACE is a face that has the same
7083 attributes. Otherwise, BASE_FACE is ignored. If the new face
7084 doesn't share font with the default face, a fontname is allocated
7085 from the heap and set in `font_name' of the new face, but it is not
7086 yet loaded here. Value is a pointer to the newly created realized
7087 face. */
82641697
GM
7088
7089static struct face *
39506348
KH
7090realize_x_face (cache, attrs, c, base_face)
7091 struct face_cache *cache;
82641697 7092 Lisp_Object *attrs;
39506348
KH
7093 int c;
7094 struct face *base_face;
82641697 7095{
c3cee013 7096#ifdef HAVE_WINDOW_SYSTEM
82641697 7097 struct face *face, *default_face;
78d2079c 7098 struct frame *f;
82641697 7099 Lisp_Object stipple, overline, strike_through, box;
82641697 7100
39506348
KH
7101 xassert (FRAME_WINDOW_P (cache->f));
7102 xassert (SINGLE_BYTE_CHAR_P (c)
0badc114 7103 || base_face);
82641697
GM
7104
7105 /* Allocate a new realized face. */
39506348
KH
7106 face = make_realized_face (attrs);
7107
7108 f = cache->f;
7109
7110 /* If C is a multibyte character, we share all face attirbutes with
7111 BASE_FACE including the realized fontset. But, we must load a
7112 different font. */
7113 if (!SINGLE_BYTE_CHAR_P (c))
7114 {
7115 bcopy (base_face, face, sizeof *face);
7116 face->gc = 0;
e911049b
GM
7117
7118 /* Don't try to free the colors copied bitwise from BASE_FACE. */
28a072fe 7119 face->colors_copied_bitwise_p = 1;
178c5d9c 7120
e911049b
GM
7121 /* to force realize_face to load font */
7122 face->font = NULL;
39506348
KH
7123 return face;
7124 }
7125
7126 /* Now we are realizing a face for ASCII (and unibyte) characters. */
82641697
GM
7127
7128 /* Determine the font to use. Most of the time, the font will be
7129 the same as the font of the default face, so try that first. */
7130 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7131 if (default_face
39506348 7132 && FACE_SUITABLE_FOR_CHAR_P (default_face, c)
82641697
GM
7133 && lface_same_font_attributes_p (default_face->lface, attrs))
7134 {
7135 face->font = default_face->font;
7136 face->fontset = default_face->fontset;
7137 face->font_info_id = default_face->font_info_id;
7138 face->font_name = default_face->font_name;
39506348 7139 face->ascii_face = face;
82641697 7140
39506348
KH
7141 /* But, as we can't share the fontset, make a new realized
7142 fontset that has the same base fontset as of the default
7143 face. */
7144 face->fontset
7145 = make_fontset_for_ascii_face (f, default_face->fontset);
82641697
GM
7146 }
7147 else
7148 {
39506348 7149 /* If the face attribute ATTRS specifies a fontset, use it as
fc8c4797
KH
7150 the base of a new realized fontset. Otherwise, use the same
7151 base fontset as of the default face. The base determines
7152 registry and encoding of a font. It may also determine
7153 foundry and family. The other fields of font name pattern
7154 are constructed from ATTRS. */
7155 int fontset = face_fontset (attrs);
7156
178c5d9c 7157 if ((fontset == -1) && default_face)
fc8c4797
KH
7158 fontset = default_face->fontset;
7159 face->fontset = make_fontset_for_ascii_face (f, fontset);
39506348 7160 face->font = NULL; /* to force realize_face to load font */
82641697
GM
7161 }
7162
7163 /* Load colors, and set remaining attributes. */
178c5d9c 7164
82641697 7165 load_face_colors (f, face, attrs);
660ed669 7166
82641697
GM
7167 /* Set up box. */
7168 box = attrs[LFACE_BOX_INDEX];
7169 if (STRINGP (box))
cb637678 7170 {
82641697
GM
7171 /* A simple box of line width 1 drawn in color given by
7172 the string. */
7173 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
7174 LFACE_BOX_INDEX);
7175 face->box = FACE_SIMPLE_BOX;
7176 face->box_line_width = 1;
cb637678 7177 }
82641697 7178 else if (INTEGERP (box))
42120bc7 7179 {
82641697
GM
7180 /* Simple box of specified line width in foreground color of the
7181 face. */
89624b8b 7182 xassert (XINT (box) != 0);
82641697 7183 face->box = FACE_SIMPLE_BOX;
89624b8b 7184 face->box_line_width = XINT (box);
82641697
GM
7185 face->box_color = face->foreground;
7186 face->box_color_defaulted_p = 1;
7187 }
7188 else if (CONSP (box))
7189 {
7190 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
7191 being one of `raised' or `sunken'. */
7192 face->box = FACE_SIMPLE_BOX;
7193 face->box_color = face->foreground;
7194 face->box_color_defaulted_p = 1;
7195 face->box_line_width = 1;
7196
7197 while (CONSP (box))
42120bc7 7198 {
82641697
GM
7199 Lisp_Object keyword, value;
7200
7201 keyword = XCAR (box);
7202 box = XCDR (box);
7203
7204 if (!CONSP (box))
7205 break;
7206 value = XCAR (box);
7207 box = XCDR (box);
7208
7209 if (EQ (keyword, QCline_width))
7210 {
89624b8b
KH
7211 if (INTEGERP (value) && XINT (value) != 0)
7212 face->box_line_width = XINT (value);
82641697
GM
7213 }
7214 else if (EQ (keyword, QCcolor))
7215 {
7216 if (STRINGP (value))
7217 {
7218 face->box_color = load_color (f, face, value,
7219 LFACE_BOX_INDEX);
7220 face->use_box_color_for_shadows_p = 1;
7221 }
7222 }
7223 else if (EQ (keyword, QCstyle))
a8517066 7224 {
82641697
GM
7225 if (EQ (value, Qreleased_button))
7226 face->box = FACE_RAISED_BOX;
7227 else if (EQ (value, Qpressed_button))
7228 face->box = FACE_SUNKEN_BOX;
a8517066 7229 }
42120bc7
RS
7230 }
7231 }
195f798e 7232
82641697 7233 /* Text underline, overline, strike-through. */
178c5d9c 7234
82641697 7235 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
178c5d9c 7236 {
82641697
GM
7237 /* Use default color (same as foreground color). */
7238 face->underline_p = 1;
7239 face->underline_defaulted_p = 1;
7240 face->underline_color = 0;
7241 }
7242 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
195f798e 7243 {
82641697
GM
7244 /* Use specified color. */
7245 face->underline_p = 1;
7246 face->underline_defaulted_p = 0;
7247 face->underline_color
7248 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
7249 LFACE_UNDERLINE_INDEX);
195f798e 7250 }
82641697 7251 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
7b00de84 7252 {
82641697
GM
7253 face->underline_p = 0;
7254 face->underline_defaulted_p = 0;
7255 face->underline_color = 0;
7b00de84
JB
7256 }
7257
82641697
GM
7258 overline = attrs[LFACE_OVERLINE_INDEX];
7259 if (STRINGP (overline))
cb637678 7260 {
82641697
GM
7261 face->overline_color
7262 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
7263 LFACE_OVERLINE_INDEX);
7264 face->overline_p = 1;
cb637678 7265 }
82641697 7266 else if (EQ (overline, Qt))
cb637678 7267 {
82641697
GM
7268 face->overline_color = face->foreground;
7269 face->overline_color_defaulted_p = 1;
7270 face->overline_p = 1;
cb637678
JB
7271 }
7272
82641697
GM
7273 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
7274 if (STRINGP (strike_through))
7275 {
7276 face->strike_through_color
7277 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
7278 LFACE_STRIKE_THROUGH_INDEX);
7279 face->strike_through_p = 1;
7280 }
7281 else if (EQ (strike_through, Qt))
7282 {
7283 face->strike_through_color = face->foreground;
7284 face->strike_through_color_defaulted_p = 1;
7285 face->strike_through_p = 1;
7286 }
867dd159 7287
82641697
GM
7288 stipple = attrs[LFACE_STIPPLE_INDEX];
7289 if (!NILP (stipple))
7290 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
660ed669 7291
39506348 7292 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
82641697 7293 return face;
c3cee013 7294#endif /* HAVE_WINDOW_SYSTEM */
660ed669
JB
7295}
7296
729425b1 7297
ae4b4ba5
GM
7298/* Map a specified color of face FACE on frame F to a tty color index.
7299 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
7300 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
7301 default foreground/background colors. */
7302
7303static void
7304map_tty_color (f, face, idx, defaulted)
7305 struct frame *f;
7306 struct face *face;
7307 enum lface_attribute_index idx;
7308 int *defaulted;
7309{
7310 Lisp_Object frame, color, def;
7311 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
7312 unsigned long default_pixel, default_other_pixel, pixel;
7313
7314 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
7315
7316 if (foreground_p)
7317 {
7318 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7319 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7320 }
7321 else
7322 {
7323 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7324 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7325 }
177c0ea7 7326
ae4b4ba5
GM
7327 XSETFRAME (frame, f);
7328 color = face->lface[idx];
177c0ea7 7329
ae4b4ba5 7330 if (STRINGP (color)
d5db4077 7331 && SCHARS (color)
ae4b4ba5
GM
7332 && CONSP (Vtty_defined_color_alist)
7333 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
7334 CONSP (def)))
7335 {
7336 /* Associations in tty-defined-color-alist are of the form
7337 (NAME INDEX R G B). We need the INDEX part. */
7338 pixel = XINT (XCAR (XCDR (def)));
7339 }
7340
7341 if (pixel == default_pixel && STRINGP (color))
7342 {
7343 pixel = load_color (f, face, color, idx);
7344
7345#if defined (MSDOS) || defined (WINDOWSNT)
7346 /* If the foreground of the default face is the default color,
7347 use the foreground color defined by the frame. */
7348#ifdef MSDOS
7349 if (FRAME_MSDOS_P (f))
7350 {
7351#endif /* MSDOS */
7352 if (pixel == default_pixel
7353 || pixel == FACE_TTY_DEFAULT_COLOR)
7354 {
7355 if (foreground_p)
7356 pixel = FRAME_FOREGROUND_PIXEL (f);
7357 else
7358 pixel = FRAME_BACKGROUND_PIXEL (f);
7359 face->lface[idx] = tty_color_name (f, pixel);
7360 *defaulted = 1;
7361 }
7362 else if (pixel == default_other_pixel)
7363 {
7364 if (foreground_p)
7365 pixel = FRAME_BACKGROUND_PIXEL (f);
7366 else
7367 pixel = FRAME_FOREGROUND_PIXEL (f);
7368 face->lface[idx] = tty_color_name (f, pixel);
7369 *defaulted = 1;
7370 }
7371#ifdef MSDOS
7372 }
7373#endif
7374#endif /* MSDOS or WINDOWSNT */
7375 }
7376
7377 if (foreground_p)
7378 face->foreground = pixel;
7379 else
7380 face->background = pixel;
7381}
7382
7383
82641697 7384/* Realize the fully-specified face with attributes ATTRS in face
39506348
KH
7385 cache CACHE for character C. Do it for TTY frame CACHE->f. Value is a
7386 pointer to the newly created realized face. */
a8517066 7387
82641697 7388static struct face *
39506348
KH
7389realize_tty_face (cache, attrs, c)
7390 struct face_cache *cache;
82641697 7391 Lisp_Object *attrs;
39506348 7392 int c;
82641697
GM
7393{
7394 struct face *face;
7395 int weight, slant;
2d764c78 7396 int face_colors_defaulted = 0;
ae4b4ba5 7397 struct frame *f = cache->f;
729425b1 7398
82641697 7399 /* Frame must be a termcap frame. */
e689ec06 7400 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
178c5d9c 7401
82641697 7402 /* Allocate a new realized face. */
39506348 7403 face = make_realized_face (attrs);
e689ec06 7404 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
82641697 7405
178c5d9c 7406 /* Map face attributes to TTY appearances. We map slant to
82641697
GM
7407 dimmed text because we want italic text to appear differently
7408 and because dimmed text is probably used infrequently. */
7409 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
7410 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
7411
7412 if (weight > XLFD_WEIGHT_MEDIUM)
7413 face->tty_bold_p = 1;
7414 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
7415 face->tty_dim_p = 1;
7416 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
7417 face->tty_underline_p = 1;
7418 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
7419 face->tty_reverse_p = 1;
7420
7421 /* Map color names to color indices. */
ae4b4ba5
GM
7422 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
7423 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
177c0ea7 7424
2d764c78
EZ
7425 /* Swap colors if face is inverse-video. If the colors are taken
7426 from the frame colors, they are already inverted, since the
7427 frame-creation function calls x-handle-reverse-video. */
7428 if (face->tty_reverse_p && !face_colors_defaulted)
44747bd0
EZ
7429 {
7430 unsigned long tem = face->foreground;
44747bd0
EZ
7431 face->foreground = face->background;
7432 face->background = tem;
7433 }
44747bd0 7434
a4a76b61
GM
7435 if (tty_suppress_bold_inverse_default_colors_p
7436 && face->tty_bold_p
7437 && face->background == FACE_TTY_DEFAULT_FG_COLOR
7438 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
7439 face->tty_bold_p = 0;
7440
82641697 7441 return face;
729425b1 7442}
867dd159 7443
82641697 7444
a4a76b61
GM
7445DEFUN ("tty-suppress-bold-inverse-default-colors",
7446 Ftty_suppress_bold_inverse_default_colors,
7447 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
e3cd9e7f 7448 doc: /* Suppress/allow boldness of faces with inverse default colors.
228299fa
GM
7449SUPPRESS non-nil means suppress it.
7450This affects bold faces on TTYs whose foreground is the default background
7451color of the display and whose background is the default foreground color.
7452For such faces, the bold face attribute is ignored if this variable
7ee72033
MB
7453is non-nil. */)
7454 (suppress)
a4a76b61
GM
7455 Lisp_Object suppress;
7456{
7457 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
7458 ++face_change_count;
7459 return suppress;
7460}
7461
7462
82641697
GM
7463\f
7464/***********************************************************************
7465 Computing Faces
7466 ***********************************************************************/
7467
7468/* Return the ID of the face to use to display character CH with face
7469 property PROP on frame F in current_buffer. */
2e16580f
RS
7470
7471int
82641697 7472compute_char_face (f, ch, prop)
2e16580f 7473 struct frame *f;
82641697
GM
7474 int ch;
7475 Lisp_Object prop;
2e16580f 7476{
82641697 7477 int face_id;
39506348
KH
7478
7479 if (NILP (current_buffer->enable_multibyte_characters))
522d42f7 7480 ch = 0;
178c5d9c 7481
82641697 7482 if (NILP (prop))
39506348
KH
7483 {
7484 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7485 face_id = FACE_FOR_CHAR (f, face, ch);
7486 }
82641697 7487 else
2e16580f 7488 {
82641697
GM
7489 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7490 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7491 bcopy (default_face->lface, attrs, sizeof attrs);
a0a23346 7492 merge_face_ref (f, prop, attrs, 1, 0);
39506348 7493 face_id = lookup_face (f, attrs, ch, NULL);
2e16580f
RS
7494 }
7495
82641697 7496 return face_id;
2e16580f 7497}
bc0db68d 7498
82641697
GM
7499/* Return the face ID associated with buffer position POS for
7500 displaying ASCII characters. Return in *ENDPTR the position at
7501 which a different face is needed, as far as text properties and
7502 overlays are concerned. W is a window displaying current_buffer.
7503
7504 REGION_BEG, REGION_END delimit the region, so it can be
7505 highlighted.
6f134486 7506
82641697
GM
7507 LIMIT is a position not to scan beyond. That is to limit the time
7508 this function can take.
7509
7510 If MOUSE is non-zero, use the character's mouse-face, not its face.
7511
39506348 7512 The face returned is suitable for displaying ASCII characters. */
bc0db68d 7513
cb637678 7514int
82641697
GM
7515face_at_buffer_position (w, pos, region_beg, region_end,
7516 endptr, limit, mouse)
f211082d 7517 struct window *w;
7b7739b1 7518 int pos;
bc0db68d 7519 int region_beg, region_end;
7b7739b1 7520 int *endptr;
b349f4fb 7521 int limit;
6f134486 7522 int mouse;
7b7739b1 7523{
82641697
GM
7524 struct frame *f = XFRAME (w->frame);
7525 Lisp_Object attrs[LFACE_VECTOR_SIZE];
b6d40e46 7526 Lisp_Object prop, position;
82641697 7527 int i, noverlays;
7b7739b1 7528 Lisp_Object *overlay_vec;
f211082d 7529 Lisp_Object frame;
f6b98e0b 7530 int endpos;
82641697
GM
7531 Lisp_Object propname = mouse ? Qmouse_face : Qface;
7532 Lisp_Object limit1, end;
7533 struct face *default_face;
f6b98e0b
JB
7534
7535 /* W must display the current buffer. We could write this function
7536 to use the frame and buffer of W, but right now it doesn't. */
060fb5c1 7537 /* xassert (XBUFFER (w->buffer) == current_buffer); */
f211082d 7538
ac22a6c4 7539 XSETFRAME (frame, f);
82641697 7540 XSETFASTINT (position, pos);
7b7739b1 7541
f6b98e0b 7542 endpos = ZV;
bc0db68d
RS
7543 if (pos < region_beg && region_beg < endpos)
7544 endpos = region_beg;
f6b98e0b 7545
82641697
GM
7546 /* Get the `face' or `mouse_face' text property at POS, and
7547 determine the next position at which the property changes. */
6f134486 7548 prop = Fget_text_property (position, propname, w->buffer);
82641697
GM
7549 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
7550 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
7551 if (INTEGERP (end))
7552 endpos = XINT (end);
6f134486 7553
82641697 7554 /* Look at properties from overlays. */
b6d40e46 7555 {
f6b98e0b 7556 int next_overlay;
b6d40e46 7557
0bc90bba 7558 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
f6b98e0b
JB
7559 if (next_overlay < endpos)
7560 endpos = next_overlay;
b6d40e46
JB
7561 }
7562
7563 *endptr = endpos;
7b7739b1 7564
82641697 7565 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
178c5d9c 7566
82641697
GM
7567 /* Optimize common cases where we can use the default face. */
7568 if (noverlays == 0
7569 && NILP (prop)
39506348 7570 && !(pos >= region_beg && pos < region_end))
82641697
GM
7571 return DEFAULT_FACE_ID;
7572
7573 /* Begin with attributes from the default face. */
7574 bcopy (default_face->lface, attrs, sizeof attrs);
7575
7576 /* Merge in attributes specified via text properties. */
7577 if (!NILP (prop))
a0a23346 7578 merge_face_ref (f, prop, attrs, 1, 0);
82641697
GM
7579
7580 /* Now merge the overlay data. */
18195655 7581 noverlays = sort_overlays (overlay_vec, noverlays, w);
18195655 7582 for (i = 0; i < noverlays; i++)
4699e6d2 7583 {
18195655
RS
7584 Lisp_Object oend;
7585 int oendpos;
7586
7587 prop = Foverlay_get (overlay_vec[i], propname);
82641697 7588 if (!NILP (prop))
a0a23346 7589 merge_face_ref (f, prop, attrs, 1, 0);
18195655
RS
7590
7591 oend = OVERLAY_END (overlay_vec[i]);
7592 oendpos = OVERLAY_POSITION (oend);
7593 if (oendpos < endpos)
7594 endpos = oendpos;
7595 }
7596
82641697 7597 /* If in the region, merge in the region face. */
18195655
RS
7598 if (pos >= region_beg && pos < region_end)
7599 {
a0a23346 7600 merge_named_face (f, Qregion, attrs, 0);
178c5d9c 7601
18195655
RS
7602 if (region_end < endpos)
7603 endpos = region_end;
18195655
RS
7604 }
7605
7606 *endptr = endpos;
7607
82641697 7608 /* Look up a realized face with the given face attributes,
39506348
KH
7609 or realize a new one for ASCII characters. */
7610 return lookup_face (f, attrs, 0, NULL);
18195655
RS
7611}
7612
60573a90 7613
82641697 7614/* Compute the face at character position POS in Lisp string STRING on
39506348 7615 window W, for ASCII characters.
7b7739b1 7616
82641697
GM
7617 If STRING is an overlay string, it comes from position BUFPOS in
7618 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
7619 not an overlay string. W must display the current buffer.
7620 REGION_BEG and REGION_END give the start and end positions of the
8714a182
GM
7621 region; both are -1 if no region is visible.
7622
7623 BASE_FACE_ID is the id of a face to merge with. For strings coming
7624 from overlays or the `display' property it is the face at BUFPOS.
178c5d9c 7625
48a4ca99
GM
7626 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
7627
82641697
GM
7628 Set *ENDPTR to the next position where to check for faces in
7629 STRING; -1 if the face is constant from POS to the end of the
7630 string.
18195655 7631
82641697 7632 Value is the id of the face to use. The face returned is suitable
39506348 7633 for displaying ASCII characters. */
fffc2367 7634
82641697
GM
7635int
7636face_at_string_position (w, string, pos, bufpos, region_beg,
48a4ca99 7637 region_end, endptr, base_face_id, mouse_p)
82641697
GM
7638 struct window *w;
7639 Lisp_Object string;
7640 int pos, bufpos;
7641 int region_beg, region_end;
7642 int *endptr;
7643 enum face_id base_face_id;
48a4ca99 7644 int mouse_p;
660ed669 7645{
82641697
GM
7646 Lisp_Object prop, position, end, limit;
7647 struct frame *f = XFRAME (WINDOW_FRAME (w));
7648 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7649 struct face *base_face;
7650 int multibyte_p = STRING_MULTIBYTE (string);
48a4ca99 7651 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
82641697
GM
7652
7653 /* Get the value of the face property at the current position within
7654 STRING. Value is nil if there is no face property. */
7655 XSETFASTINT (position, pos);
48a4ca99 7656 prop = Fget_text_property (position, prop_name, string);
82641697
GM
7657
7658 /* Get the next position at which to check for faces. Value of end
7659 is nil if face is constant all the way to the end of the string.
7660 Otherwise it is a string position where to check faces next.
7661 Limit is the maximum position up to which to check for property
7662 changes in Fnext_single_property_change. Strings are usually
7663 short, so set the limit to the end of the string. */
d5db4077 7664 XSETFASTINT (limit, SCHARS (string));
48a4ca99 7665 end = Fnext_single_property_change (position, prop_name, string, limit);
82641697
GM
7666 if (INTEGERP (end))
7667 *endptr = XFASTINT (end);
7668 else
7669 *endptr = -1;
7670
7671 base_face = FACE_FROM_ID (f, base_face_id);
7672 xassert (base_face);
7673
7674 /* Optimize the default case that there is no face property and we
7675 are not in the region. */
7676 if (NILP (prop)
7677 && (base_face_id != DEFAULT_FACE_ID
7678 /* BUFPOS <= 0 means STRING is not an overlay string, so
7679 that the region doesn't have to be taken into account. */
7680 || bufpos <= 0
7681 || bufpos < region_beg
7682 || bufpos >= region_end)
7683 && (multibyte_p
7684 /* We can't realize faces for different charsets differently
7685 if we don't have fonts, so we can stop here if not working
7686 on a window-system frame. */
7687 || !FRAME_WINDOW_P (f)
39506348 7688 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
82641697
GM
7689 return base_face->id;
7690
7691 /* Begin with attributes from the base face. */
7692 bcopy (base_face->lface, attrs, sizeof attrs);
7693
7694 /* Merge in attributes specified via text properties. */
7695 if (!NILP (prop))
a0a23346 7696 merge_face_ref (f, prop, attrs, 1, 0);
82641697
GM
7697
7698 /* If in the region, merge in the region face. */
7699 if (bufpos
7700 && bufpos >= region_beg
7701 && bufpos < region_end)
a0a23346 7702 merge_named_face (f, Qregion, attrs, 0);
660ed669 7703
82641697 7704 /* Look up a realized face with the given face attributes,
39506348
KH
7705 or realize a new one for ASCII characters. */
7706 return lookup_face (f, attrs, 0, NULL);
660ed669
JB
7707}
7708
7709
fd998c7f
KS
7710/* Merge a face into a realized face.
7711
7712 F is frame where faces are (to be) realized.
7713
dc91a0ed
KS
7714 FACE_NAME is named face to merge.
7715
7716 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
7717
7718 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
fd998c7f
KS
7719
7720 BASE_FACE_ID is realized face to merge into.
7721
dc91a0ed 7722 Return new face id.
fd998c7f
KS
7723*/
7724
7725int
dc91a0ed 7726merge_faces (f, face_name, face_id, base_face_id)
fd998c7f
KS
7727 struct frame *f;
7728 Lisp_Object face_name;
7729 int face_id, base_face_id;
7730{
7731 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7732 struct face *base_face;
7733
7734 base_face = FACE_FROM_ID (f, base_face_id);
7735 if (!base_face)
7736 return base_face_id;
7737
dc91a0ed
KS
7738 if (EQ (face_name, Qt))
7739 {
7740 if (face_id < 0 || face_id >= lface_id_to_name_size)
7741 return base_face_id;
7742 face_name = lface_id_to_name[face_id];
d196a547 7743 face_id = lookup_derived_face (f, face_name, 0, base_face_id, 1);
dc91a0ed
KS
7744 if (face_id >= 0)
7745 return face_id;
7746 return base_face_id;
7747 }
7748
fd998c7f
KS
7749 /* Begin with attributes from the base face. */
7750 bcopy (base_face->lface, attrs, sizeof attrs);
7751
7752 if (!NILP (face_name))
7753 {
7754 if (!merge_named_face (f, face_name, attrs, 0))
7755 return base_face_id;
7756 }
7757 else
7758 {
7759 struct face *face;
dc91a0ed
KS
7760 if (face_id < 0)
7761 return base_face_id;
fd998c7f
KS
7762 face = FACE_FROM_ID (f, face_id);
7763 if (!face)
7764 return base_face_id;
7765 merge_face_vectors (f, face->lface, attrs, 0);
7766 }
7767
7768 /* Look up a realized face with the given face attributes,
7769 or realize a new one for ASCII characters. */
7770 return lookup_face (f, attrs, 0, NULL);
7771}
7772
c115973b 7773\f
82641697
GM
7774/***********************************************************************
7775 Tests
7776 ***********************************************************************/
c115973b 7777
82641697 7778#if GLYPH_DEBUG
c115973b 7779
82641697 7780/* Print the contents of the realized face FACE to stderr. */
c115973b 7781
82641697
GM
7782static void
7783dump_realized_face (face)
7784 struct face *face;
7785{
7786 fprintf (stderr, "ID: %d\n", face->id);
7787#ifdef HAVE_X_WINDOWS
2defe37f 7788 fprintf (stderr, "gc: %ld\n", (long) face->gc);
82641697
GM
7789#endif
7790 fprintf (stderr, "foreground: 0x%lx (%s)\n",
7791 face->foreground,
d5db4077 7792 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
82641697
GM
7793 fprintf (stderr, "background: 0x%lx (%s)\n",
7794 face->background,
d5db4077 7795 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
82641697
GM
7796 fprintf (stderr, "font_name: %s (%s)\n",
7797 face->font_name,
d5db4077 7798 SDATA (face->lface[LFACE_FAMILY_INDEX]));
82641697
GM
7799#ifdef HAVE_X_WINDOWS
7800 fprintf (stderr, "font = %p\n", face->font);
7801#endif
7802 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
7803 fprintf (stderr, "fontset: %d\n", face->fontset);
7804 fprintf (stderr, "underline: %d (%s)\n",
7805 face->underline_p,
d5db4077 7806 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
82641697
GM
7807 fprintf (stderr, "hash: %d\n", face->hash);
7808 fprintf (stderr, "charset: %d\n", face->charset);
c115973b
JB
7809}
7810
7811
7ee72033
MB
7812DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
7813 (n)
82641697 7814 Lisp_Object n;
c115973b 7815{
82641697 7816 if (NILP (n))
c115973b 7817 {
82641697 7818 int i;
178c5d9c 7819
82641697
GM
7820 fprintf (stderr, "font selection order: ");
7821 for (i = 0; i < DIM (font_sort_order); ++i)
7822 fprintf (stderr, "%d ", font_sort_order[i]);
7823 fprintf (stderr, "\n");
7824
7825 fprintf (stderr, "alternative fonts: ");
7826 debug_print (Vface_alternative_font_family_alist);
7827 fprintf (stderr, "\n");
178c5d9c 7828
c0617987 7829 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
82641697 7830 Fdump_face (make_number (i));
c115973b
JB
7831 }
7832 else
f5e278c7 7833 {
82641697 7834 struct face *face;
b7826503 7835 CHECK_NUMBER (n);
c0617987 7836 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
82641697
GM
7837 if (face == NULL)
7838 error ("Not a valid face");
7839 dump_realized_face (face);
f5e278c7 7840 }
178c5d9c 7841
c115973b
JB
7842 return Qnil;
7843}
b5c53576 7844
b5c53576 7845
82641697 7846DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
7ee72033
MB
7847 0, 0, 0, doc: /* */)
7848 ()
b5c53576 7849{
82641697
GM
7850 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
7851 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
7852 fprintf (stderr, "number of GCs = %d\n", ngcs);
7853 return Qnil;
b5c53576
RS
7854}
7855
82641697
GM
7856#endif /* GLYPH_DEBUG != 0 */
7857
b5c53576 7858
c115973b 7859\f
82641697
GM
7860/***********************************************************************
7861 Initialization
7862 ***********************************************************************/
cb637678 7863
c115973b 7864void
f211082d 7865syms_of_xfaces ()
c115973b 7866{
f211082d
JB
7867 Qface = intern ("face");
7868 staticpro (&Qface);
46b00436
KS
7869 Qface_no_inherit = intern ("face-no-inherit");
7870 staticpro (&Qface_no_inherit);
fef04523
GM
7871 Qbitmap_spec_p = intern ("bitmap-spec-p");
7872 staticpro (&Qbitmap_spec_p);
92610620
GM
7873 Qframe_update_face_colors = intern ("frame-update-face-colors");
7874 staticpro (&Qframe_update_face_colors);
178c5d9c 7875
82641697
GM
7876 /* Lisp face attribute keywords. */
7877 QCfamily = intern (":family");
7878 staticpro (&QCfamily);
7879 QCheight = intern (":height");
7880 staticpro (&QCheight);
7881 QCweight = intern (":weight");
7882 staticpro (&QCweight);
7883 QCslant = intern (":slant");
7884 staticpro (&QCslant);
7885 QCunderline = intern (":underline");
7886 staticpro (&QCunderline);
7887 QCinverse_video = intern (":inverse-video");
178c5d9c 7888 staticpro (&QCinverse_video);
82641697
GM
7889 QCreverse_video = intern (":reverse-video");
7890 staticpro (&QCreverse_video);
7891 QCforeground = intern (":foreground");
7892 staticpro (&QCforeground);
7893 QCbackground = intern (":background");
7894 staticpro (&QCbackground);
7895 QCstipple = intern (":stipple");;
7896 staticpro (&QCstipple);
7897 QCwidth = intern (":width");
7898 staticpro (&QCwidth);
7899 QCfont = intern (":font");
7900 staticpro (&QCfont);
7901 QCbold = intern (":bold");
7902 staticpro (&QCbold);
7903 QCitalic = intern (":italic");
7904 staticpro (&QCitalic);
7905 QCoverline = intern (":overline");
7906 staticpro (&QCoverline);
7907 QCstrike_through = intern (":strike-through");
7908 staticpro (&QCstrike_through);
7909 QCbox = intern (":box");
7910 staticpro (&QCbox);
2c20458f
MB
7911 QCinherit = intern (":inherit");
7912 staticpro (&QCinherit);
82641697
GM
7913
7914 /* Symbols used for Lisp face attribute values. */
7915 QCcolor = intern (":color");
7916 staticpro (&QCcolor);
7917 QCline_width = intern (":line-width");
7918 staticpro (&QCline_width);
7919 QCstyle = intern (":style");
7920 staticpro (&QCstyle);
7921 Qreleased_button = intern ("released-button");
7922 staticpro (&Qreleased_button);
7923 Qpressed_button = intern ("pressed-button");
7924 staticpro (&Qpressed_button);
7925 Qnormal = intern ("normal");
7926 staticpro (&Qnormal);
7927 Qultra_light = intern ("ultra-light");
7928 staticpro (&Qultra_light);
7929 Qextra_light = intern ("extra-light");
7930 staticpro (&Qextra_light);
7931 Qlight = intern ("light");
7932 staticpro (&Qlight);
7933 Qsemi_light = intern ("semi-light");
7934 staticpro (&Qsemi_light);
7935 Qsemi_bold = intern ("semi-bold");
7936 staticpro (&Qsemi_bold);
7937 Qbold = intern ("bold");
7938 staticpro (&Qbold);
7939 Qextra_bold = intern ("extra-bold");
7940 staticpro (&Qextra_bold);
7941 Qultra_bold = intern ("ultra-bold");
7942 staticpro (&Qultra_bold);
7943 Qoblique = intern ("oblique");
7944 staticpro (&Qoblique);
7945 Qitalic = intern ("italic");
7946 staticpro (&Qitalic);
7947 Qreverse_oblique = intern ("reverse-oblique");
7948 staticpro (&Qreverse_oblique);
7949 Qreverse_italic = intern ("reverse-italic");
7950 staticpro (&Qreverse_italic);
7951 Qultra_condensed = intern ("ultra-condensed");
7952 staticpro (&Qultra_condensed);
7953 Qextra_condensed = intern ("extra-condensed");
7954 staticpro (&Qextra_condensed);
7955 Qcondensed = intern ("condensed");
7956 staticpro (&Qcondensed);
7957 Qsemi_condensed = intern ("semi-condensed");
7958 staticpro (&Qsemi_condensed);
7959 Qsemi_expanded = intern ("semi-expanded");
7960 staticpro (&Qsemi_expanded);
7961 Qexpanded = intern ("expanded");
7962 staticpro (&Qexpanded);
7963 Qextra_expanded = intern ("extra-expanded");
7964 staticpro (&Qextra_expanded);
7965 Qultra_expanded = intern ("ultra-expanded");
7966 staticpro (&Qultra_expanded);
7967 Qbackground_color = intern ("background-color");
7968 staticpro (&Qbackground_color);
7969 Qforeground_color = intern ("foreground-color");
7970 staticpro (&Qforeground_color);
7971 Qunspecified = intern ("unspecified");
7972 staticpro (&Qunspecified);
7973
92610620
GM
7974 Qface_alias = intern ("face-alias");
7975 staticpro (&Qface_alias);
82641697
GM
7976 Qdefault = intern ("default");
7977 staticpro (&Qdefault);
9ea173e8
GM
7978 Qtool_bar = intern ("tool-bar");
7979 staticpro (&Qtool_bar);
82641697
GM
7980 Qregion = intern ("region");
7981 staticpro (&Qregion);
8bd201d6
GM
7982 Qfringe = intern ("fringe");
7983 staticpro (&Qfringe);
045dee35
GM
7984 Qheader_line = intern ("header-line");
7985 staticpro (&Qheader_line);
8bd201d6
GM
7986 Qscroll_bar = intern ("scroll-bar");
7987 staticpro (&Qscroll_bar);
c7ae3284
GM
7988 Qmenu = intern ("menu");
7989 staticpro (&Qmenu);
8bd201d6
GM
7990 Qcursor = intern ("cursor");
7991 staticpro (&Qcursor);
7992 Qborder = intern ("border");
7993 staticpro (&Qborder);
7994 Qmouse = intern ("mouse");
7995 staticpro (&Qmouse);
039b6394
KS
7996 Qmode_line_inactive = intern ("mode-line-inactive");
7997 staticpro (&Qmode_line_inactive);
2d764c78
EZ
7998 Qtty_color_desc = intern ("tty-color-desc");
7999 staticpro (&Qtty_color_desc);
b35df831
MB
8000 Qtty_color_standard_values = intern ("tty-color-standard-values");
8001 staticpro (&Qtty_color_standard_values);
2d764c78
EZ
8002 Qtty_color_by_index = intern ("tty-color-by-index");
8003 staticpro (&Qtty_color_by_index);
ae4b4ba5
GM
8004 Qtty_color_alist = intern ("tty-color-alist");
8005 staticpro (&Qtty_color_alist);
eeffb293
GM
8006 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
8007 staticpro (&Qscalable_fonts_allowed);
82641697 8008
dbc968b8
GM
8009 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
8010 staticpro (&Vparam_value_alist);
434b9cc5
GM
8011 Vface_alternative_font_family_alist = Qnil;
8012 staticpro (&Vface_alternative_font_family_alist);
32fcc231
GM
8013 Vface_alternative_font_registry_alist = Qnil;
8014 staticpro (&Vface_alternative_font_registry_alist);
434b9cc5 8015
82641697
GM
8016 defsubr (&Sinternal_make_lisp_face);
8017 defsubr (&Sinternal_lisp_face_p);
8018 defsubr (&Sinternal_set_lisp_face_attribute);
c3cee013 8019#ifdef HAVE_WINDOW_SYSTEM
82641697 8020 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
42608ba8 8021#endif
ea4fa0af
GM
8022 defsubr (&Scolor_gray_p);
8023 defsubr (&Scolor_supported_p);
cdfaafa9
MB
8024 defsubr (&Sface_attribute_relative_p);
8025 defsubr (&Smerge_face_attribute);
82641697
GM
8026 defsubr (&Sinternal_get_lisp_face_attribute);
8027 defsubr (&Sinternal_lisp_face_attribute_values);
8028 defsubr (&Sinternal_lisp_face_equal_p);
8029 defsubr (&Sinternal_lisp_face_empty_p);
8030 defsubr (&Sinternal_copy_lisp_face);
8031 defsubr (&Sinternal_merge_in_global_face);
8032 defsubr (&Sface_font);
8033 defsubr (&Sframe_face_alist);
9717e36c 8034 defsubr (&Sdisplay_supports_face_attributes_p);
b35df831 8035 defsubr (&Scolor_distance);
82641697
GM
8036 defsubr (&Sinternal_set_font_selection_order);
8037 defsubr (&Sinternal_set_alternative_font_family_alist);
32fcc231 8038 defsubr (&Sinternal_set_alternative_font_registry_alist);
f6608d5c 8039 defsubr (&Sface_attributes_as_vector);
82641697
GM
8040#if GLYPH_DEBUG
8041 defsubr (&Sdump_face);
8042 defsubr (&Sshow_face_resources);
8043#endif /* GLYPH_DEBUG */
8044 defsubr (&Sclear_face_cache);
a4a76b61 8045 defsubr (&Stty_suppress_bold_inverse_default_colors);
82641697 8046
38426dee 8047#if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
08dc08dc
GM
8048 defsubr (&Sdump_colors);
8049#endif
8050
7ee72033
MB
8051 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
8052 doc: /* *Limit for font matching.
228299fa
GM
8053If an integer > 0, font matching functions won't load more than
8054that number of fonts when searching for a matching font. */);
057df17c
GM
8055 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
8056
7ee72033
MB
8057 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
8058 doc: /* List of global face definitions (for internal use only.) */);
82641697 8059 Vface_new_frame_defaults = Qnil;
178c5d9c 8060
7ee72033
MB
8061 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
8062 doc: /* *Default stipple pattern used on monochrome displays.
228299fa
GM
8063This stipple pattern is used on monochrome displays
8064instead of shades of gray for a face background color.
8065See `set-face-stipple' for possible values for this variable. */);
82641697
GM
8066 Vface_default_stipple = build_string ("gray3");
8067
7ee72033
MB
8068 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
8069 doc: /* An alist of defined terminal colors and their RGB values. */);
ae4b4ba5
GM
8070 Vtty_defined_color_alist = Qnil;
8071
7ee72033
MB
8072 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
8073 doc: /* Allowed scalable fonts.
228299fa
GM
8074A value of nil means don't allow any scalable fonts.
8075A value of t means allow any scalable font.
8076Otherwise, value must be a list of regular expressions. A font may be
8077scaled if its name matches a regular expression in the list.
8078Note that if value is nil, a scalable font might still be used, if no
8079other font of the appropriate family and registry is available. */);
3cf80731 8080 Vscalable_fonts_allowed = Qnil;
b5c53576 8081
7ee72033
MB
8082 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
8083 doc: /* List of ignored fonts.
228299fa
GM
8084Each element is a regular expression that matches names of fonts to
8085ignore. */);
c824bfbc
KH
8086 Vface_ignored_fonts = Qnil;
8087
f70400f2
KH
8088 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
8089 doc: /* Alist of fonts vs the rescaling factors.
8090Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
8091FONT-NAME-PATTERN is a regular expression matching a font name, and
8092RESCALE-RATIO is a floating point number to specify how much larger
8093\(or smaller) font we should use. For instance, if a face requests
8094a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
8095 Vface_font_rescale_alist = Qnil;
8096
c3cee013 8097#ifdef HAVE_WINDOW_SYSTEM
fef04523 8098 defsubr (&Sbitmap_spec_p);
82641697
GM
8099 defsubr (&Sx_list_fonts);
8100 defsubr (&Sinternal_face_x_get_resource);
92610620 8101 defsubr (&Sx_family_fonts);
82641697 8102 defsubr (&Sx_font_family_list);
c3cee013 8103#endif /* HAVE_WINDOW_SYSTEM */
c115973b 8104}
ab5796a9
MB
8105
8106/* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
8107 (do not change this comment) */