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