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