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