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