(swap_in_symval_forwarding): Change for Lisp_Object
[bpt/emacs.git] / src / xfaces.c
CommitLineData
82641697
GM
1/* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999 Free Software Foundation.
7b7739b1 3
c115973b
JB
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7b7739b1 8the Free Software Foundation; either version 2, or (at your option)
c115973b
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
c115973b 20
82641697
GM
21/* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22
23/* Faces.
24
25 When using Emacs with X, the display style of characters can be
26 changed by defining `faces'. Each face can specify the following
27 display attributes:
28
29 1. Font family or fontset alias name.
30
31 2. Relative proportionate width, aka character set width or set
32 width (swidth), e.g. `semi-compressed'.
33
34 3. Font height in 1/10pt
35
36 4. Font weight, e.g. `bold'.
37
38 5. Font slant, e.g. `italic'.
39
40 6. Foreground color.
41
42 7. Background color.
43
44 8. Whether or not characters should be underlined, and in what color.
45
46 9. Whether or not characters should be displayed in inverse video.
47
48 10. A background stipple, a bitmap.
49
50 11. Whether or not characters should be overlined, and in what color.
51
52 12. Whether or not characters should be strike-through, and in what
53 color.
54
55 13. Whether or not a box should be drawn around characters, the box
56 type, and, for simple boxes, in what color.
57
58 Faces are frame-local by nature because Emacs allows to define the
59 same named face (face names are symbols) differently for different
60 frames. Each frame has an alist of face definitions for all named
61 faces. The value of a named face in such an alist is a Lisp vector
62 with the symbol `face' in slot 0, and a slot for each each of the
63 face attributes mentioned above.
64
65 There is also a global face alist `Vface_new_frame_defaults'. Face
66 definitions from this list are used to initialize faces of newly
67 created frames.
68
69 A face doesn't have to specify all attributes. Those not specified
70 have a value of `unspecified'. Faces specifying all attributes are
71 called `fully-specified'.
72
73
74 Face merging.
75
76 The display style of a given character in the text is determined by
77 combining several faces. This process is called `face merging'.
78 Any aspect of the display style that isn't specified by overlays or
79 text properties is taken from the `default' face. Since it is made
80 sure that the default face is always fully-specified, face merging
81 always results in a fully-specified face.
82
83
84 Face realization.
85
86 After all face attributes for a character have been determined by
87 merging faces of that character, that face is `realized'. The
88 realization process maps face attributes to what is physically
89 available on the system where Emacs runs. The result is a
90 `realized face' in form of a struct face which is stored in the
91 face cache of the frame on which it was realized.
92
93 Face realization is done in the context of the charset of the
94 character to display because different fonts and encodings are used
95 for different charsets. In other words, for characters of
96 different charsets, different realized faces are needed to display
97 them.
98
99 Except for composite characters (CHARSET_COMPOSITION), faces are
100 always realized for a specific character set and contain a specific
101 font, even if the face being realized specifies a fontset (see
102 `font selection' below). The reason is that the result of the new
103 font selection stage is better than what can be done with
104 statically defined font name patterns in fontsets.
105
106
107 Unibyte text.
108
109 In unibyte text, Emacs' charsets aren't applicable; function
110 `char-charset' reports CHARSET_ASCII for all characters, including
111 those > 0x7f. The X registry and encoding of fonts to use is
112 determined from the variable `x-unibyte-registry-and-encoding' in
113 this case. The variable is initialized at Emacs startup time from
114 the font the user specified for Emacs.
115
116 Currently all unibyte text, i.e. all buffers with
117 enable_multibyte_characters nil are displayed with fonts of the
118 same registry and encoding `x-unibyte-registry-and-encoding'. This
119 is consistent with the fact that languages can also be set
120 globally, only.
121
122
123 Font selection.
124
125 Font selection tries to find the best available matching font for a
126 given (charset, face) combination. This is done slightly
127 differently for faces specifying a fontset, or a font family name.
128
129 If the face specifies a fontset alias name, that fontset determines
130 a pattern for fonts of the given charset. If the face specifies a
131 font family, a font pattern is constructed. Charset symbols have a
132 property `x-charset-registry' for that purpose that maps a charset
133 to an XLFD registry and encoding in the font pattern constructed.
134
135 Available fonts on the system on which Emacs runs are then matched
136 against the font pattern. The result of font selection is the best
137 match for the given face attributes in this font list.
138
139 Font selection can be influenced by the user.
140
141 1. The user can specify the relative importance he gives the face
142 attributes width, height, weight, and slant by setting
143 face-font-selection-order (faces.el) to a list of face attribute
144 names. The default is '(:width :height :weight :slant), and means
145 that font selection first tries to find a good match for the font
146 width specified by a face, then---within fonts with that
147 width---tries to find a best match for the specified font height,
148 etc.
149
150 2. Setting face-alternative-font-family-alist allows the user to
151 specify alternative font families to try if a family specified by a
152 face doesn't exist.
153
154
155 Composite characters.
156
157 Realized faces for composite characters are the only ones having a
158 fontset id >= 0. When a composite character is encoded into a
159 sequence of non-composite characters (in xterm.c), a suitable font
160 for the non-composite characters is then selected and realized,
161 i.e. the realization process is delayed but in principle the same.
162
163
164 Initialization of basic faces.
165
166 The faces `default', `modeline' are considered `basic faces'.
167 When redisplay happens the first time for a newly created frame,
168 basic faces are realized for CHARSET_ASCII. Frame parameters are
169 used to fill in unspecified attributes of the default face. */
170
171/* Define SCALABLE_FONTS to a non-zero value to enable scalable
172 font use. Define it to zero to disable scalable font use.
173
174 Use of too many or too large scalable fonts can crash XFree86
175 servers. That's why I've put the code dealing with scalable fonts
176 in #if's. */
177
178#define SCALABLE_FONTS 1
7b7739b1 179
c115973b
JB
180#include <sys/types.h>
181#include <sys/stat.h>
18160b98 182#include <config.h>
c115973b 183#include "lisp.h"
a8517066 184#include "charset.h"
b5c53576
RS
185#include "frame.h"
186
87485d6f 187#ifdef HAVE_X_WINDOWS
c115973b 188#include "xterm.h"
a8517066 189#include "fontset.h"
87485d6f 190#endif
82641697 191
87485d6f
MW
192#ifdef MSDOS
193#include "dosfns.h"
194#endif
82641697 195
c115973b 196#include "buffer.h"
f211082d 197#include "dispextern.h"
357f32fc 198#include "blockinput.h"
b6d40e46 199#include "window.h"
bde7c500 200#include "intervals.h"
c115973b 201
87485d6f 202#ifdef HAVE_X_WINDOWS
82641697
GM
203
204/* Compensate for a bug in Xos.h on some systems, on which it requires
657070ac
JB
205 time.h. On some such systems, Xos.h tries to redefine struct
206 timeval and struct timezone if USG is #defined while it is
207 #included. */
657070ac 208
82641697 209#ifdef XOS_NEEDS_TIME_H
e11d186d 210#include <time.h>
657070ac
JB
211#undef USG
212#include <X11/Xos.h>
213#define USG
e11d186d 214#define __TIMEVAL__
82641697
GM
215#else /* not XOS_NEEDS_TIME_H */
216#include <X11/Xos.h>
217#endif /* not XOS_NEEDS_TIME_H */
e11d186d 218
82641697 219#endif /* HAVE_X_WINDOWS */
7a4d2269 220
82641697
GM
221#include <stdio.h>
222#include <stdlib.h>
223#include <ctype.h>
224#include "keyboard.h"
c115973b 225
82641697
GM
226#ifndef max
227#define max(A, B) ((A) > (B) ? (A) : (B))
228#define min(A, B) ((A) < (B) ? (A) : (B))
229#define abs(X) ((X) < 0 ? -(X) : (X))
657070ac 230#endif
cb637678 231
82641697
GM
232/* Non-zero if face attribute ATTR is unspecified. */
233
234#define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
235
236/* Value is the number of elements of VECTOR. */
237
238#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
239
240/* Make a copy of string S on the stack using alloca. Value is a pointer
241 to the copy. */
242
243#define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
bc0db68d 244
82641697
GM
245/* Make a copy of the contents of Lisp string S on the stack using
246 alloca. Value is a pointer to the copy. */
247
248#define LSTRDUPA(S) STRDUPA (XSTRING ((S))->data)
249
250/* Size of hash table of realized faces in face caches (should be a
251 prime number). */
252
253#define FACE_CACHE_BUCKETS_SIZE 1001
254
255/* Keyword symbols used for face attribute names. */
256
257Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
258Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
259Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
260Lisp_Object QCreverse_video;
261Lisp_Object QCoverline, QCstrike_through, QCbox;
262
263/* Symbols used for attribute values. */
264
265Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
266Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
267Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
268Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
269Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
270Lisp_Object Qultra_expanded;
271Lisp_Object Qreleased_button, Qpressed_button;
272Lisp_Object QCstyle, QCcolor, QCline_width;
273Lisp_Object Qunspecified;
274
275/* The symbol `x-charset-registry'. This property of charsets defines
276 the X registry and encoding that fonts should have that are used to
277 display characters of that charset. */
278
279Lisp_Object Qx_charset_registry;
280
92610620
GM
281/* The name of the function to call when the background of the frame
282 has changed, frame_update_face_colors. */
283
284Lisp_Object Qframe_update_face_colors;
285
82641697
GM
286/* Names of basic faces. */
287
92610620 288Lisp_Object Qdefault, Qmode_line, Qtool_bar, Qregion, Qfringe;
8bd201d6
GM
289Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse;;
290
92610620
GM
291/* The symbol `face-alias'. A symbols having that property is an
292 alias for another face. Value of the property is the name of
293 the aliased face. */
294
295Lisp_Object Qface_alias;
296
8bd201d6
GM
297/* Names of frame parameters related to faces. */
298
299extern Lisp_Object Qscroll_bar_foreground, Qscroll_bar_background;
300extern Lisp_Object Qborder_color, Qcursor_color, Qmouse_color;
82641697
GM
301
302/* Default stipple pattern used on monochrome displays. This stipple
303 pattern is used on monochrome displays instead of shades of gray
304 for a face background color. See `set-face-stipple' for possible
305 values for this variable. */
306
307Lisp_Object Vface_default_stipple;
308
309/* Default registry and encoding to use for charsets whose charset
310 symbols don't specify one. */
311
312Lisp_Object Vface_default_registry;
313
314/* Alist of alternative font families. Each element is of the form
315 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
316 try FAMILY1, then FAMILY2, ... */
317
318Lisp_Object Vface_alternative_font_family_alist;
319
320/* Allowed scalable fonts. A value of nil means don't allow any
321 scalable fonts. A value of t means allow the use of any scalable
322 font. Otherwise, value must be a list of regular expressions. A
323 font may be scaled if its name matches a regular expression in the
324 list. */
325
326#if SCALABLE_FONTS
327Lisp_Object Vscalable_fonts_allowed;
328#endif
329
057df17c
GM
330/* Maximum number of fonts to consider in font_list. If not an
331 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
332
333Lisp_Object Vfont_list_limit;
334#define DEFAULT_FONT_LIST_LIMIT 100
335
82641697
GM
336/* The symbols `foreground-color' and `background-color' which can be
337 used as part of a `face' property. This is for compatibility with
338 Emacs 20.2. */
339
340Lisp_Object Qforeground_color, Qbackground_color;
341
342/* The symbols `face' and `mouse-face' used as text properties. */
7b7739b1 343
ff83dbb1 344Lisp_Object Qface;
82641697
GM
345extern Lisp_Object Qmouse_face;
346
347/* Error symbol for wrong_type_argument in load_pixmap. */
348
cd0bb842 349Lisp_Object Qpixmap_spec_p;
f211082d 350
82641697
GM
351/* Alist of global face definitions. Each element is of the form
352 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
353 is a Lisp vector of face attributes. These faces are used
354 to initialize faces for new frames. */
cb637678 355
82641697 356Lisp_Object Vface_new_frame_defaults;
18195655 357
82641697 358/* The next ID to assign to Lisp faces. */
cb637678 359
82641697 360static int next_lface_id;
c115973b 361
82641697 362/* A vector mapping Lisp face Id's to face names. */
c115973b 363
82641697
GM
364static Lisp_Object *lface_id_to_name;
365static int lface_id_to_name_size;
c115973b 366
82641697
GM
367/* An alist of elements (COLOR-NAME . INDEX) mapping color names
368 to color indices for tty frames. */
369
370Lisp_Object Vface_tty_color_alist;
371
372/* Counter for calls to clear_face_cache. If this counter reaches
373 CLEAR_FONT_TABLE_COUNT, and a frame has more than
374 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
375
376static int clear_font_table_count;
377#define CLEAR_FONT_TABLE_COUNT 100
378#define CLEAR_FONT_TABLE_NFONTS 10
379
380/* Non-zero means face attributes have been changed since the last
381 redisplay. Used in redisplay_internal. */
382
383int face_change_count;
384
385/* The total number of colors currently allocated. */
386
387#if GLYPH_DEBUG
388static int ncolors_allocated;
389static int npixmaps_allocated;
390static int ngcs;
391#endif
392
393
394\f
395/* Function prototypes. */
396
397struct font_name;
398struct table_entry;
399
400static int may_use_scalable_font_p P_ ((struct font_name *, char *));
401static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
402static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
403 int));
404static int first_font_matching P_ ((struct frame *f, char *,
405 struct font_name *));
406static int x_face_list_fonts P_ ((struct frame *, char *,
407 struct font_name *, int, int, int));
408static int font_scalable_p P_ ((struct font_name *));
409static Lisp_Object deduce_unibyte_registry P_ ((struct frame *, char *));
410static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
411static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
412static char *xstrdup P_ ((char *));
413static unsigned char *xstrlwr P_ ((unsigned char *));
414static void signal_error P_ ((char *, Lisp_Object));
7dbdfcf8 415static void add_to_log P_ ((struct frame *, char *, Lisp_Object, Lisp_Object));
82641697
GM
416static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
417static void load_face_font_or_fontset P_ ((struct frame *, struct face *, char *, int));
82641697
GM
418static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
419static void free_face_colors P_ ((struct frame *, struct face *));
420static int face_color_gray_p P_ ((struct frame *, char *));
421static char *build_font_name P_ ((struct font_name *));
422static void free_font_names P_ ((struct font_name *, int));
423static int sorted_font_list P_ ((struct frame *, char *,
424 int (*cmpfn) P_ ((const void *, const void *)),
425 struct font_name **));
426static int font_list P_ ((struct frame *, char *, char *, char *, struct font_name **));
427static int try_font_list P_ ((struct frame *, Lisp_Object *, char *, char *, char *,
428 struct font_name **));
429static int cmp_font_names P_ ((const void *, const void *));
430static struct face *realize_face P_ ((struct face_cache *,
431 Lisp_Object *, int));
432static struct face *realize_x_face P_ ((struct face_cache *,
433 Lisp_Object *, int));
434static struct face *realize_tty_face P_ ((struct face_cache *,
435 Lisp_Object *, int));
436static int realize_basic_faces P_ ((struct frame *));
437static int realize_default_face P_ ((struct frame *));
438static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
439static int lface_fully_specified_p P_ ((Lisp_Object *));
440static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
441static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
442static unsigned lface_hash P_ ((Lisp_Object *));
443static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
444static struct face_cache *make_face_cache P_ ((struct frame *));
445static void free_realized_face P_ ((struct frame *, struct face *));
446static void clear_face_gcs P_ ((struct face_cache *));
447static void free_face_cache P_ ((struct face_cache *));
448static int face_numeric_weight P_ ((Lisp_Object));
449static int face_numeric_slant P_ ((Lisp_Object));
450static int face_numeric_swidth P_ ((Lisp_Object));
451static int face_fontset P_ ((struct frame *, Lisp_Object *));
452static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int,
453 Lisp_Object));
454static char *choose_face_fontset_font P_ ((struct frame *, Lisp_Object *,
455 int, int));
456static void merge_face_vectors P_ ((Lisp_Object *from, Lisp_Object *));
457static void merge_face_vector_with_property P_ ((struct frame *, Lisp_Object *,
458 Lisp_Object));
459static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object, char *,
460 int));
461static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
462static struct face *make_realized_face P_ ((Lisp_Object *, int, Lisp_Object));
463static void free_realized_faces P_ ((struct face_cache *));
464static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
465 struct font_name *, int));
466static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
467static void uncache_face P_ ((struct face_cache *, struct face *));
468static int xlfd_numeric_slant P_ ((struct font_name *));
469static int xlfd_numeric_weight P_ ((struct font_name *));
470static int xlfd_numeric_swidth P_ ((struct font_name *));
471static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
472static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
473static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
474static int xlfd_fixed_p P_ ((struct font_name *));
475static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
476 int, int));
477static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
478 struct font_name *, int, int));
479static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
480 struct font_name *, int));
481
482#ifdef HAVE_X_WINDOWS
483
484static int split_font_name P_ ((struct frame *, struct font_name *, int));
485static int xlfd_point_size P_ ((struct frame *, struct font_name *));
486static void sort_fonts P_ ((struct frame *, struct font_name *, int,
487 int (*cmpfn) P_ ((const void *, const void *))));
488static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
489static void x_free_gc P_ ((struct frame *, GC));
490static void clear_font_table P_ ((struct frame *));
491
492#endif /* HAVE_X_WINDOWS */
c115973b 493
cb637678 494\f
82641697
GM
495/***********************************************************************
496 Utilities
497 ***********************************************************************/
c115973b 498
87485d6f 499#ifdef HAVE_X_WINDOWS
cb637678 500
82641697
GM
501/* Create and return a GC for use on frame F. GC values and mask
502 are given by XGCV and MASK. */
503
504static INLINE GC
505x_create_gc (f, mask, xgcv)
cb637678 506 struct frame *f;
82641697
GM
507 unsigned long mask;
508 XGCValues *xgcv;
c115973b
JB
509{
510 GC gc;
82641697
GM
511 BLOCK_INPUT;
512 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
513 UNBLOCK_INPUT;
514 IF_DEBUG (++ngcs);
515 return gc;
516}
c115973b 517
42120bc7 518
82641697
GM
519/* Free GC which was used on frame F. */
520
521static INLINE void
522x_free_gc (f, gc)
523 struct frame *f;
524 GC gc;
525{
660ed669 526 BLOCK_INPUT;
82641697
GM
527 xassert (--ngcs >= 0);
528 XFreeGC (FRAME_X_DISPLAY (f), gc);
529 UNBLOCK_INPUT;
530}
660ed669 531
82641697 532#endif /* HAVE_X_WINDOWS */
660ed669 533
660ed669 534
82641697
GM
535/* Like strdup, but uses xmalloc. */
536
537static char *
538xstrdup (s)
539 char *s;
540{
541 int len = strlen (s) + 1;
542 char *p = (char *) xmalloc (len);
543 bcopy (s, p, len);
544 return p;
545}
660ed669 546
660ed669 547
82641697
GM
548/* Like stricmp. Used to compare parts of font names which are in
549 ISO8859-1. */
550
551int
552xstricmp (s1, s2)
553 unsigned char *s1, *s2;
554{
555 while (*s1 && *s2)
95887807 556 {
82641697
GM
557 unsigned char c1 = tolower (*s1);
558 unsigned char c2 = tolower (*s2);
559 if (c1 != c2)
560 return c1 < c2 ? -1 : 1;
561 ++s1, ++s2;
95887807 562 }
cd0bb842 563
82641697
GM
564 if (*s1 == 0)
565 return *s2 == 0 ? 0 : -1;
566 return 1;
567}
660ed669 568
660ed669 569
82641697 570/* Like strlwr, which might not always be available. */
42120bc7 571
82641697
GM
572static unsigned char *
573xstrlwr (s)
574 unsigned char *s;
575{
576 unsigned char *p = s;
577
578 for (p = s; *p; ++p)
579 *p = tolower (*p);
580
581 return s;
c115973b 582}
cb637678 583
42120bc7 584
82641697
GM
585/* Signal `error' with message S, and additional argument ARG. */
586
587static void
588signal_error (s, arg)
589 char *s;
590 Lisp_Object arg;
42120bc7 591{
82641697
GM
592 Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
593}
42120bc7 594
82641697
GM
595
596/* Display a message with format string FORMAT and arguments ARG1 and
597 ARG2 on frame F. Used to display errors if fonts, bitmaps, colors
598 etc. for a realized face on frame F cannot be loaded. (If we would
599 signal an error in these cases, we would end up in an infinite
600 recursion because this would stop realization, and the redisplay
601 triggered by the signal would try to realize that same face again.)
602
603 If basic faces of F are not realized, just add the message to the
604 messages buffer "*Messages*". Because Fmessage calls
605 echo_area_display which tries to realize basic faces again, we would
606 otherwise also end in an infinite recursion. */
607
608static void
7dbdfcf8 609add_to_log (f, format, arg1, arg2)
82641697
GM
610 struct frame *f;
611 char *format;
612 Lisp_Object arg1, arg2;
613{
614 Lisp_Object args[3];
615 Lisp_Object nargs;
7dbdfcf8
GM
616 Lisp_Object msg;
617 char *buffer;
82641697
GM
618 extern int waiting_for_input;
619
620 /* Function note_mouse_highlight calls face_at_buffer_position which
621 may realize a face. If some attribute of that face is invalid,
622 say an invalid color, don't display an error to avoid calling
623 Lisp from XTread_socket. */
624 if (waiting_for_input)
625 return;
626
627 nargs = make_number (DIM (args));
628 args[0] = build_string (format);
629 args[1] = arg1;
630 args[2] = arg2;
7dbdfcf8
GM
631 msg = Fformat (nargs, args);
632
633 /* Log the error, but don't display it in the echo area. This
634 proves to be annoying in many cases. */
635 buffer = LSTRDUPA (msg);
636 message_dolog (buffer, strlen (buffer), 1, 0);
82641697 637}
42120bc7 638
82641697
GM
639
640/* If FRAME is nil, return selected_frame. Otherwise, check that
641 FRAME is a live frame, and return a pointer to it. NPARAM
642 is the parameter number of FRAME, for CHECK_LIVE_FRAME. This is
643 here because it's a frequent pattern in Lisp function definitions. */
644
645static INLINE struct frame *
646frame_or_selected_frame (frame, nparam)
647 Lisp_Object frame;
648 int nparam;
649{
650 struct frame *f;
651
652 if (NILP (frame))
653 f = selected_frame;
654 else
655 {
656 CHECK_LIVE_FRAME (frame, nparam);
657 f = XFRAME (frame);
42120bc7
RS
658 }
659
82641697 660 return f;
42120bc7 661}
82641697 662
42120bc7 663\f
82641697
GM
664/***********************************************************************
665 Frames and faces
666 ***********************************************************************/
cd0bb842 667
82641697 668/* Initialize face cache and basic faces for frame F. */
cb637678 669
82641697
GM
670void
671init_frame_faces (f)
cb637678 672 struct frame *f;
cb637678 673{
82641697
GM
674 /* Make a face cache, if F doesn't have one. */
675 if (FRAME_FACE_CACHE (f) == NULL)
676 FRAME_FACE_CACHE (f) = make_face_cache (f);
677
678#ifdef HAVE_X_WINDOWS
679 /* Make the image cache. */
680 if (FRAME_X_P (f))
681 {
682 if (FRAME_X_IMAGE_CACHE (f) == NULL)
683 FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
684 ++FRAME_X_IMAGE_CACHE (f)->refcount;
685 }
686#endif /* HAVE_X_WINDOWS */
cb637678 687
82641697
GM
688 /* Realize basic faces. Must have enough information in frame
689 parameters to realize basic faces at this point. */
690#ifdef HAVE_X_WINDOWS
691 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
692#endif
693 if (!realize_basic_faces (f))
694 abort ();
695}
cb637678 696
cb637678 697
82641697 698/* Free face cache of frame F. Called from Fdelete_frame. */
cb637678 699
82641697
GM
700void
701free_frame_faces (f)
cb637678 702 struct frame *f;
cb637678 703{
82641697
GM
704 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
705
706 if (face_cache)
707 {
708 free_face_cache (face_cache);
709 FRAME_FACE_CACHE (f) = NULL;
710 }
660ed669 711
82641697
GM
712#ifdef HAVE_X_WINDOWS
713 if (FRAME_X_P (f))
195f798e 714 {
82641697
GM
715 struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
716 if (image_cache)
195f798e 717 {
82641697
GM
718 --image_cache->refcount;
719 if (image_cache->refcount == 0)
720 free_image_cache (f);
195f798e
RS
721 }
722 }
82641697 723#endif /* HAVE_X_WINDOWS */
cb637678
JB
724}
725
82641697 726
8bd201d6
GM
727/* Clear face caches, and recompute basic faces for frame F. Call
728 this after changing frame parameters on which those faces depend,
729 or when realized faces have been freed due to changing attributes
730 of named faces. */
82641697
GM
731
732void
733recompute_basic_faces (f)
cb637678 734 struct frame *f;
cb637678 735{
82641697
GM
736 if (FRAME_FACE_CACHE (f))
737 {
8bd201d6
GM
738 int realized_p;
739 clear_face_cache (0);
740 realized_p = realize_basic_faces (f);
82641697
GM
741 xassert (realized_p);
742 }
743}
cb637678 744
cb637678 745
82641697
GM
746/* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
747 try to free unused fonts, too. */
cb637678 748
adfea139 749void
82641697
GM
750clear_face_cache (clear_fonts_p)
751 int clear_fonts_p;
cb637678 752{
82641697
GM
753#ifdef HAVE_X_WINDOWS
754 Lisp_Object tail, frame;
755 struct frame *f;
828e66d1 756
82641697
GM
757 if (clear_fonts_p
758 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
828e66d1 759 {
82641697
GM
760 /* From time to time see if we can unload some fonts. This also
761 frees all realized faces on all frames. Fonts needed by
762 faces will be loaded again when faces are realized again. */
763 clear_font_table_count = 0;
195f798e 764
82641697 765 FOR_EACH_FRAME (tail, frame)
195f798e 766 {
82641697
GM
767 f = XFRAME (frame);
768 if (FRAME_X_P (f)
769 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
195f798e 770 {
82641697
GM
771 free_all_realized_faces (frame);
772 clear_font_table (f);
773 }
774 }
775 }
776 else
777 {
778 /* Clear GCs of realized faces. */
779 FOR_EACH_FRAME (tail, frame)
780 {
781 f = XFRAME (frame);
782 if (FRAME_X_P (f))
783 {
784 clear_face_gcs (FRAME_FACE_CACHE (f));
785 clear_image_cache (f, 0);
195f798e
RS
786 }
787 }
828e66d1 788 }
82641697 789#endif /* HAVE_X_WINDOWS */
cd0bb842
RS
790}
791
82641697
GM
792
793DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
794 "Clear face caches on all frames.\n\
795Optional THOROUGHLY non-nil means try to free unused fonts, too.")
796 (thorougly)
797 Lisp_Object thorougly;
cd0bb842 798{
82641697
GM
799 clear_face_cache (!NILP (thorougly));
800 return Qnil;
801}
802
803
804
805#ifdef HAVE_X_WINDOWS
806
807
808/* Remove those fonts from the font table of frame F that are not used
809 by fontsets. Called from clear_face_cache from time to time. */
810
811static void
812clear_font_table (f)
813 struct frame *f;
814{
815 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
816 char *used;
817 Lisp_Object rest, frame;
818 int i;
819
820 xassert (FRAME_X_P (f));
821
822 used = (char *) alloca (dpyinfo->n_fonts * sizeof *used);
823 bzero (used, dpyinfo->n_fonts * sizeof *used);
824
825 /* For all frames with the same x_display_info as F, record
826 in `used' those fonts that are in use by fontsets. */
827 FOR_EACH_FRAME (rest, frame)
828 if (FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
829 {
830 struct frame *f = XFRAME (frame);
831 struct fontset_data *fontset_data = FRAME_FONTSET_DATA (f);
832
833 for (i = 0; i < fontset_data->n_fontsets; ++i)
834 {
835 struct fontset_info *info = fontset_data->fontset_table[i];
836 int j;
837
838 for (j = 0; j <= MAX_CHARSET; ++j)
839 {
840 int idx = info->font_indexes[j];
841 if (idx >= 0)
842 used[idx] = 1;
843 }
844 }
845 }
846
847 /* Free those fonts that are not used by fontsets. */
848 for (i = 0; i < dpyinfo->n_fonts; ++i)
849 if (used[i] == 0 && dpyinfo->font_table[i].name)
850 {
851 struct font_info *font_info = dpyinfo->font_table + i;
852
853 /* Free names. In xfns.c there is a comment that full_name
854 should never be freed because it is always shared with
855 something else. I don't think this is true anymore---see
856 x_load_font. It's either equal to font_info->name or
857 allocated via xmalloc, and there seems to be no place in
858 the source files where full_name is transferred to another
859 data structure. */
860 if (font_info->full_name != font_info->name)
861 xfree (font_info->full_name);
862 xfree (font_info->name);
863
864 /* Free the font. */
865 BLOCK_INPUT;
866 XFreeFont (dpyinfo->display, font_info->font);
867 UNBLOCK_INPUT;
868
869 /* Mark font table slot free. */
870 font_info->font = NULL;
871 font_info->name = font_info->full_name = NULL;
872 }
873}
874
875
876#endif /* HAVE_X_WINDOWS */
877
878
879\f
880/***********************************************************************
881 X Pixmaps
882 ***********************************************************************/
883
884#ifdef HAVE_X_WINDOWS
885
886DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
887 "Non-nil if OBJECT is a valid pixmap specification.\n\
888A pixmap specification is either a string, or a list (WIDTH HEIGHT DATA)\n\
889where WIDTH is the pixel width of the pixmap, HEIGHT is its height,\n\
890and DATA contains the bits of the pixmap.")
891 (object)
892 Lisp_Object object;
893{
894 Lisp_Object height, width;
cd0bb842 895
ed791d5b
EN
896 return ((STRINGP (object)
897 || (CONSP (object)
898 && CONSP (XCONS (object)->cdr)
899 && CONSP (XCONS (XCONS (object)->cdr)->cdr)
900 && NILP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->cdr)
901 && (width = XCONS (object)->car, INTEGERP (width))
82641697
GM
902 && (height = XCONS (XCONS (object)->cdr)->car,
903 INTEGERP (height))
ed791d5b 904 && STRINGP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)
cd0bb842
RS
905 && XINT (width) > 0
906 && XINT (height) > 0
907 /* The string must have enough bits for width * height. */
ed791d5b 908 && ((XSTRING (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)->size
68be917d 909 * (BITS_PER_INT / sizeof (int)))
e1befa75 910 >= XFASTINT (width) * XFASTINT (height))))
cd0bb842
RS
911 ? Qt : Qnil);
912}
913
cd0bb842 914
82641697
GM
915/* Load a bitmap according to NAME (which is either a file name or a
916 pixmap spec) for use on frame F. Value is the bitmap_id (see
917 xfns.c). If NAME is nil, return with a bitmap id of zero. If
918 bitmap cannot be loaded, display a message saying so, and return
919 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
920 if these pointers are not null. */
cd0bb842 921
82641697 922static int
cd0bb842 923load_pixmap (f, name, w_ptr, h_ptr)
7812a96f 924 FRAME_PTR f;
cd0bb842
RS
925 Lisp_Object name;
926 unsigned int *w_ptr, *h_ptr;
927{
928 int bitmap_id;
929 Lisp_Object tem;
930
931 if (NILP (name))
82641697 932 return 0;
cd0bb842
RS
933
934 tem = Fpixmap_spec_p (name);
935 if (NILP (tem))
936 wrong_type_argument (Qpixmap_spec_p, name);
937
938 BLOCK_INPUT;
cd0bb842
RS
939 if (CONSP (name))
940 {
941 /* Decode a bitmap spec into a bitmap. */
942
943 int h, w;
944 Lisp_Object bits;
945
946 w = XINT (Fcar (name));
947 h = XINT (Fcar (Fcdr (name)));
948 bits = Fcar (Fcdr (Fcdr (name)));
949
950 bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
951 w, h);
952 }
953 else
954 {
955 /* It must be a string -- a file name. */
956 bitmap_id = x_create_bitmap_from_file (f, name);
957 }
958 UNBLOCK_INPUT;
959
7812a96f 960 if (bitmap_id < 0)
82641697 961 {
7dbdfcf8 962 add_to_log (f, "Invalid or undefined bitmap %s", name, Qnil);
82641697 963 bitmap_id = 0;
cd0bb842 964
82641697
GM
965 if (w_ptr)
966 *w_ptr = 0;
967 if (h_ptr)
968 *h_ptr = 0;
969 }
970 else
971 {
972#if GLYPH_DEBUG
973 ++npixmaps_allocated;
974#endif
975 if (w_ptr)
976 *w_ptr = x_bitmap_width (f, bitmap_id);
977
978 if (h_ptr)
979 *h_ptr = x_bitmap_height (f, bitmap_id);
980 }
cd0bb842
RS
981
982 return bitmap_id;
cb637678 983}
87485d6f 984
82641697
GM
985#endif /* HAVE_X_WINDOWS */
986
87485d6f 987
82641697
GM
988\f
989/***********************************************************************
990 Minimum font bounds
991 ***********************************************************************/
992
993#ifdef HAVE_X_WINDOWS
994
d6fc0a22
GM
995/* Update the line_height of frame F. Return non-zero if line height
996 changes. */
87485d6f 997
82641697
GM
998int
999frame_update_line_height (f)
87485d6f 1000 struct frame *f;
87485d6f 1001{
d6fc0a22
GM
1002 int fontset, line_height, changed_p;
1003
1004 fontset = f->output_data.x->fontset;
1005 if (fontset > 0)
1006 line_height = FRAME_FONTSET_DATA (f)->fontset_table[fontset]->height;
1007 else
1008 line_height = FONT_HEIGHT (f->output_data.x->font);
1009
1010 changed_p = line_height != f->output_data.x->line_height;
1011 f->output_data.x->line_height = line_height;
82641697 1012 return changed_p;
87485d6f
MW
1013}
1014
82641697
GM
1015#endif /* HAVE_X_WINDOWS */
1016
1017\f
1018/***********************************************************************
1019 Fonts
1020 ***********************************************************************/
1021
1022#ifdef HAVE_X_WINDOWS
1023
1024/* Load font or fontset of face FACE which is used on frame F.
1025 FONTSET is the fontset FACE should use or -1, if FACE doesn't use a
1026 fontset. FONT_NAME is the name of the font to load, if no fontset
1027 is used. It is null if no suitable font name could be determined
1028 for the face. */
1029
1030static void
1031load_face_font_or_fontset (f, face, font_name, fontset)
1032 struct frame *f;
1033 struct face *face;
1034 char *font_name;
1035 int fontset;
87485d6f 1036{
82641697
GM
1037 struct font_info *font_info = NULL;
1038
1039 face->font_info_id = -1;
1040 face->fontset = fontset;
1041 face->font = NULL;
1042
1043 BLOCK_INPUT;
1044 if (fontset >= 0)
1045 font_info = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII,
1046 NULL, fontset);
1047 else if (font_name)
1048 font_info = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f), face->charset,
1049 font_name, -1);
1050 UNBLOCK_INPUT;
1051
1052 if (font_info)
1053 {
1054 char *s;
1055 int i;
1056
1057 face->font_info_id = FONT_INFO_ID (f, font_info);
1058 face->font = font_info->font;
1059 face->font_name = font_info->full_name;
1060
1061 /* Make the registry part of the font name readily accessible.
1062 The registry is used to find suitable faces for unibyte text. */
1063 s = font_info->full_name + strlen (font_info->full_name);
1064 i = 0;
1065 while (i < 2 && --s >= font_info->full_name)
1066 if (*s == '-')
1067 ++i;
1068
1069 if (!STRINGP (face->registry)
1070 || xstricmp (XSTRING (face->registry)->data, s + 1) != 0)
1071 {
1072 if (STRINGP (Vface_default_registry)
1073 && !xstricmp (XSTRING (Vface_default_registry)->data, s + 1))
1074 face->registry = Vface_default_registry;
1075 else
1076 face->registry = build_string (s + 1);
1077 }
1078 }
1079 else if (fontset >= 0)
7dbdfcf8
GM
1080 add_to_log (f, "Unable to load ASCII font of fontset %d",
1081 make_number (fontset), Qnil);
82641697 1082 else if (font_name)
7dbdfcf8
GM
1083 add_to_log (f, "Unable to load font %s",
1084 build_string (font_name), Qnil);
87485d6f
MW
1085}
1086
82641697 1087#endif /* HAVE_X_WINDOWS */
87485d6f 1088
87485d6f 1089
82641697
GM
1090\f
1091/***********************************************************************
1092 X Colors
1093 ***********************************************************************/
1094
1095#ifdef HAVE_X_WINDOWS
1096
1097/* Return non-zero if COLOR_NAME is a shade of gray (or white or
1098 black) on frame F. The algorithm is taken from 20.2 faces.el. */
1099
1100static int
1101face_color_gray_p (f, color_name)
1102 struct frame *f;
1103 char *color_name;
1104{
1105 XColor color;
1106 int gray_p;
1107
1108 if (defined_color (f, color_name, &color, 0))
1109 gray_p = ((abs (color.red - color.green)
1110 < max (color.red, color.green) / 20)
1111 && (abs (color.green - color.blue)
1112 < max (color.green, color.blue) / 20)
1113 && (abs (color.blue - color.red)
1114 < max (color.blue, color.red) / 20));
87485d6f 1115 else
82641697
GM
1116 gray_p = 0;
1117
1118 return gray_p;
87485d6f 1119}
87485d6f 1120
cb637678 1121
82641697
GM
1122/* Return non-zero if color COLOR_NAME can be displayed on frame F.
1123 BACKGROUND_P non-zero means the color will be used as background
1124 color. */
1125
1126static int
1127face_color_supported_p (f, color_name, background_p)
1128 struct frame *f;
1129 char *color_name;
1130 int background_p;
1131{
1132 Lisp_Object frame;
1133
1134 XSETFRAME (frame, f);
1135 return (!NILP (Vwindow_system)
1136 && (!NILP (Fx_display_color_p (frame))
1137 || xstricmp (color_name, "black") == 0
1138 || xstricmp (color_name, "white") == 0
1139 || (background_p
1140 && face_color_gray_p (f, color_name))
1141 || (!NILP (Fx_display_grayscale_p (frame))
1142 && face_color_gray_p (f, color_name))));
1143}
1144
1145
1146DEFUN ("face-color-gray-p", Fface_color_gray_p, Sface_color_gray_p, 1, 2, 0,
1147 "Return non-nil if COLOR is a shade of gray (or white or black).\n\
1148FRAME specifies the frame and thus the display for interpreting COLOR.\n\
1149If FRAME is nil or omitted, use the selected frame.")
1150 (color, frame)
1151 Lisp_Object color, frame;
cb637678 1152{
82641697
GM
1153 struct frame *f = check_x_frame (frame);
1154 CHECK_STRING (color, 0);
1155 return face_color_gray_p (f, XSTRING (color)->data) ? Qt : Qnil;
1156}
660ed669 1157
fffc2367 1158
82641697
GM
1159DEFUN ("face-color-supported-p", Fface_color_supported_p,
1160 Sface_color_supported_p, 2, 3, 0,
1161 "Return non-nil if COLOR can be displayed on FRAME.\n\
1162BACKGROUND-P non-nil means COLOR is used as a background.\n\
1163If FRAME is nil or omitted, use the selected frame.\n\
1164COLOR must be a valid color name.")
1165 (frame, color, background_p)
1166 Lisp_Object frame, color, background_p;
1167{
1168 struct frame *f = check_x_frame (frame);
1169 CHECK_STRING (color, 0);
1170 if (face_color_supported_p (f, XSTRING (color)->data, !NILP (background_p)))
1171 return Qt;
1172 return Qnil;
1173}
7b37f67b 1174
82641697
GM
1175/* Load color with name NAME for use by face FACE on frame F.
1176 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1177 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1178 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1179 pixel color. If color cannot be loaded, display a message, and
1180 return the foreground, background or underline color of F, but
1181 record that fact in flags of the face so that we don't try to free
1182 these colors. */
1183
44747bd0 1184unsigned long
82641697
GM
1185load_color (f, face, name, target_index)
1186 struct frame *f;
1187 struct face *face;
1188 Lisp_Object name;
1189 enum lface_attribute_index target_index;
1190{
1191 XColor color;
1192
1193 xassert (STRINGP (name));
1194 xassert (target_index == LFACE_FOREGROUND_INDEX
1195 || target_index == LFACE_BACKGROUND_INDEX
1196 || target_index == LFACE_UNDERLINE_INDEX
1197 || target_index == LFACE_OVERLINE_INDEX
1198 || target_index == LFACE_STRIKE_THROUGH_INDEX
1199 || target_index == LFACE_BOX_INDEX);
1200
1201 /* if the color map is full, defined_color will return a best match
1202 to the values in an existing cell. */
1203 if (!defined_color (f, XSTRING (name)->data, &color, 1))
1204 {
7dbdfcf8 1205 add_to_log (f, "Unable to load color %s", name, Qnil);
82641697
GM
1206
1207 switch (target_index)
1120eb5e 1208 {
82641697
GM
1209 case LFACE_FOREGROUND_INDEX:
1210 face->foreground_defaulted_p = 1;
1211 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1212 break;
1213
1214 case LFACE_BACKGROUND_INDEX:
1215 face->background_defaulted_p = 1;
1216 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1217 break;
1218
1219 case LFACE_UNDERLINE_INDEX:
1220 face->underline_defaulted_p = 1;
1221 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1222 break;
1223
1224 case LFACE_OVERLINE_INDEX:
1225 face->overline_color_defaulted_p = 1;
1226 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1120eb5e 1227 break;
82641697
GM
1228
1229 case LFACE_STRIKE_THROUGH_INDEX:
1230 face->strike_through_color_defaulted_p = 1;
1231 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1232 break;
1233
1234 case LFACE_BOX_INDEX:
1235 face->box_color_defaulted_p = 1;
1236 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1237 break;
1238
1239 default:
1240 abort ();
1120eb5e 1241 }
82641697
GM
1242 }
1243#if GLYPH_DEBUG
1244 else
1245 ++ncolors_allocated;
1246#endif
1247
1248 return color.pixel;
1249}
1120eb5e 1250
1120eb5e 1251
82641697
GM
1252/* Load colors for face FACE which is used on frame F. Colors are
1253 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1254 of ATTRS. If the background color specified is not supported on F,
1255 try to emulate gray colors with a stipple from Vface_default_stipple. */
1256
1257static void
1258load_face_colors (f, face, attrs)
1259 struct frame *f;
1260 struct face *face;
1261 Lisp_Object *attrs;
1262{
1263 Lisp_Object fg, bg;
1264
1265 bg = attrs[LFACE_BACKGROUND_INDEX];
1266 fg = attrs[LFACE_FOREGROUND_INDEX];
1267
1268 /* Swap colors if face is inverse-video. */
1269 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1270 {
1271 Lisp_Object tmp;
1272 tmp = fg;
1273 fg = bg;
1274 bg = tmp;
1275 }
1276
1277 /* Check for support for foreground, not for background because
1278 face_color_supported_p is smart enough to know that grays are
1279 "supported" as background because we are supposed to use stipple
1280 for them. */
1281 if (!face_color_supported_p (f, XSTRING (bg)->data, 0)
1282 && !NILP (Fpixmap_spec_p (Vface_default_stipple)))
1283 {
1284 x_destroy_bitmap (f, face->stipple);
1285 face->stipple = load_pixmap (f, Vface_default_stipple,
1286 &face->pixmap_w, &face->pixmap_h);
1287 }
82641697 1288
be8a72f4 1289 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
82641697 1290 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
cb637678
JB
1291}
1292
660ed669 1293
82641697 1294/* Free color PIXEL on frame F. */
cd0bb842 1295
cb637678 1296void
82641697 1297unload_color (f, pixel)
cb637678 1298 struct frame *f;
82641697 1299 unsigned long pixel;
cb637678 1300{
42120bc7 1301 Display *dpy = FRAME_X_DISPLAY (f);
82641697
GM
1302 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
1303
1304 if (pixel == BLACK_PIX_DEFAULT (f)
1305 || pixel == WHITE_PIX_DEFAULT (f))
1306 return;
cb637678 1307
660ed669 1308 BLOCK_INPUT;
82641697
GM
1309
1310 /* If display has an immutable color map, freeing colors is not
1311 necessary and some servers don't allow it. So don't do it. */
1312 if (! (class == StaticColor || class == StaticGray || class == TrueColor))
1313 {
1314 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
1315 XFreeColors (dpy, cmap, &pixel, 1, 0);
1316 }
1317
1318 UNBLOCK_INPUT;
1319}
1320
1321
1322/* Free colors allocated for FACE. */
1323
1324static void
1325free_face_colors (f, face)
1326 struct frame *f;
1327 struct face *face;
1328{
1329 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
1330
1331 /* If display has an immutable color map, freeing colors is not
1332 necessary and some servers don't allow it. So don't do it. */
1333 if (class != StaticColor
1334 && class != StaticGray
1335 && class != TrueColor)
1336 {
1337 Display *dpy;
1338 Colormap cmap;
1339
1340 BLOCK_INPUT;
1341 dpy = FRAME_X_DISPLAY (f);
1342 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
1343
1344 if (face->foreground != BLACK_PIX_DEFAULT (f)
1345 && face->foreground != WHITE_PIX_DEFAULT (f)
1346 && !face->foreground_defaulted_p)
1347 {
1348 XFreeColors (dpy, cmap, &face->foreground, 1, 0);
1349 IF_DEBUG (--ncolors_allocated);
1350 }
1351
1352 if (face->background != BLACK_PIX_DEFAULT (f)
1353 && face->background != WHITE_PIX_DEFAULT (f)
1354 && !face->background_defaulted_p)
1355 {
1356 XFreeColors (dpy, cmap, &face->background, 1, 0);
1357 IF_DEBUG (--ncolors_allocated);
1358 }
1359
1360 if (face->underline_p
1361 && !face->underline_defaulted_p
1362 && face->underline_color != BLACK_PIX_DEFAULT (f)
1363 && face->underline_color != WHITE_PIX_DEFAULT (f))
1364 {
1365 XFreeColors (dpy, cmap, &face->underline_color, 1, 0);
1366 IF_DEBUG (--ncolors_allocated);
1367 }
1368
1369 if (face->overline_p
1370 && !face->overline_color_defaulted_p
1371 && face->overline_color != BLACK_PIX_DEFAULT (f)
1372 && face->overline_color != WHITE_PIX_DEFAULT (f))
1373 {
1374 XFreeColors (dpy, cmap, &face->overline_color, 1, 0);
1375 IF_DEBUG (--ncolors_allocated);
1376 }
1377
1378 if (face->strike_through_p
1379 && !face->strike_through_color_defaulted_p
1380 && face->strike_through_color != BLACK_PIX_DEFAULT (f)
1381 && face->strike_through_color != WHITE_PIX_DEFAULT (f))
1382 {
1383 XFreeColors (dpy, cmap, &face->strike_through_color, 1, 0);
1384 IF_DEBUG (--ncolors_allocated);
1385 }
1386
1387 if (face->box != FACE_NO_BOX
1388 && !face->box_color_defaulted_p
1389 && face->box_color != BLACK_PIX_DEFAULT (f)
1390 && face->box_color != WHITE_PIX_DEFAULT (f))
1391 {
1392 XFreeColors (dpy, cmap, &face->box_color, 1, 0);
1393 IF_DEBUG (--ncolors_allocated);
1394 }
1395
1396 UNBLOCK_INPUT;
1397 }
1398}
1399
44747bd0
EZ
1400#else /* ! HAVE_X_WINDOWS */
1401
1402#ifdef MSDOS
1403unsigned long
1404load_color (f, face, name, target_index)
1405 struct frame *f;
1406 struct face *face;
1407 Lisp_Object name;
1408 enum lface_attribute_index target_index;
1409{
1410 Lisp_Object color;
1411 int color_idx = FACE_TTY_DEFAULT_COLOR;
1412
1413 if (NILP (name))
1414 return (unsigned long)FACE_TTY_DEFAULT_COLOR;
1415
1416 CHECK_STRING (name, 0);
1417
1418 color = Qnil;
1419 if (XSTRING (name)->size && !NILP (Ffboundp (Qmsdos_color_translate)))
1420 {
1421 color = call1 (Qmsdos_color_translate, name);
1422
1423 if (INTEGERP (color))
1424 return (unsigned long)XINT (color);
1425
7dbdfcf8 1426 add_to_log (f, "Unable to load color %s", name, Qnil);
44747bd0
EZ
1427
1428 switch (target_index)
1429 {
1430 case LFACE_FOREGROUND_INDEX:
1431 face->foreground_defaulted_p = 1;
1432 color_idx = FRAME_FOREGROUND_PIXEL (f);
1433 break;
1434
1435 case LFACE_BACKGROUND_INDEX:
1436 face->background_defaulted_p = 1;
1437 color_idx = FRAME_BACKGROUND_PIXEL (f);
1438 break;
1439
1440 case LFACE_UNDERLINE_INDEX:
1441 face->underline_defaulted_p = 1;
1442 color_idx = FRAME_FOREGROUND_PIXEL (f);
1443 break;
1444
1445 case LFACE_OVERLINE_INDEX:
1446 face->overline_color_defaulted_p = 1;
1447 color_idx = FRAME_FOREGROUND_PIXEL (f);
1448 break;
1449
1450 case LFACE_STRIKE_THROUGH_INDEX:
1451 face->strike_through_color_defaulted_p = 1;
1452 color_idx = FRAME_FOREGROUND_PIXEL (f);
1453 break;
1454
1455 case LFACE_BOX_INDEX:
1456 face->box_color_defaulted_p = 1;
1457 color_idx = FRAME_FOREGROUND_PIXEL (f);
1458 break;
1459 }
1460 }
1461 else
1462 color_idx = msdos_stdcolor_idx (XSTRING (name)->data);
1463
1464 return (unsigned long)color_idx;
1465}
1466#endif /* MSDOS */
1467#endif /* ! HAVE_X_WINDOWS */
82641697
GM
1468
1469
1470\f
1471/***********************************************************************
1472 XLFD Font Names
1473 ***********************************************************************/
1474
1475/* An enumerator for each field of an XLFD font name. */
1476
1477enum xlfd_field
1478{
1479 XLFD_FOUNDRY,
1480 XLFD_FAMILY,
1481 XLFD_WEIGHT,
1482 XLFD_SLANT,
1483 XLFD_SWIDTH,
1484 XLFD_ADSTYLE,
1485 XLFD_PIXEL_SIZE,
1486 XLFD_POINT_SIZE,
1487 XLFD_RESX,
1488 XLFD_RESY,
1489 XLFD_SPACING,
1490 XLFD_AVGWIDTH,
1491 XLFD_REGISTRY,
1492 XLFD_ENCODING,
1493 XLFD_LAST
1494};
1495
1496/* An enumerator for each possible slant value of a font. Taken from
1497 the XLFD specification. */
1498
1499enum xlfd_slant
1500{
1501 XLFD_SLANT_UNKNOWN,
1502 XLFD_SLANT_ROMAN,
1503 XLFD_SLANT_ITALIC,
1504 XLFD_SLANT_OBLIQUE,
1505 XLFD_SLANT_REVERSE_ITALIC,
1506 XLFD_SLANT_REVERSE_OBLIQUE,
1507 XLFD_SLANT_OTHER
1508};
1509
1510/* Relative font weight according to XLFD documentation. */
1511
1512enum xlfd_weight
1513{
1514 XLFD_WEIGHT_UNKNOWN,
1515 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1516 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1517 XLFD_WEIGHT_LIGHT, /* 30 */
1518 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1519 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1520 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1521 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1522 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1523 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1524};
1525
1526/* Relative proportionate width. */
1527
1528enum xlfd_swidth
1529{
1530 XLFD_SWIDTH_UNKNOWN,
1531 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1532 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1533 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1534 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1535 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1536 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1537 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1538 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1539 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1540};
1541
1542/* Structure used for tables mapping XLFD weight, slant, and width
1543 names to numeric and symbolic values. */
1544
1545struct table_entry
1546{
1547 char *name;
1548 int numeric;
1549 Lisp_Object *symbol;
1550};
1551
1552/* Table of XLFD slant names and their numeric and symbolic
1553 representations. This table must be sorted by slant names in
1554 ascending order. */
1555
1556static struct table_entry slant_table[] =
1557{
1558 {"i", XLFD_SLANT_ITALIC, &Qitalic},
1559 {"o", XLFD_SLANT_OBLIQUE, &Qoblique},
1560 {"ot", XLFD_SLANT_OTHER, &Qitalic},
1561 {"r", XLFD_SLANT_ROMAN, &Qnormal},
1562 {"ri", XLFD_SLANT_REVERSE_ITALIC, &Qreverse_italic},
1563 {"ro", XLFD_SLANT_REVERSE_OBLIQUE, &Qreverse_oblique}
1564};
1565
1566/* Table of XLFD weight names. This table must be sorted by weight
1567 names in ascending order. */
1568
1569static struct table_entry weight_table[] =
1570{
1571 {"black", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold},
1572 {"bold", XLFD_WEIGHT_BOLD, &Qbold},
1573 {"book", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1574 {"demibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1575 {"extralight", XLFD_WEIGHT_EXTRA_LIGHT, &Qextra_light},
1576 {"extrabold", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1577 {"heavy", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1578 {"light", XLFD_WEIGHT_LIGHT, &Qlight},
1579 {"medium", XLFD_WEIGHT_MEDIUM, &Qnormal},
1580 {"normal", XLFD_WEIGHT_MEDIUM, &Qnormal},
1581 {"regular", XLFD_WEIGHT_MEDIUM, &Qnormal},
1582 {"semibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1583 {"semilight", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1584 {"ultralight", XLFD_WEIGHT_ULTRA_LIGHT, &Qultra_light},
1585 {"ultrabold", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold}
1586};
1587
1588/* Table of XLFD width names. This table must be sorted by width
1589 names in ascending order. */
1590
1591static struct table_entry swidth_table[] =
1592{
1593 {"compressed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1594 {"condensed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1595 {"demiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1596 {"expanded", XLFD_SWIDTH_EXPANDED, &Qexpanded},
1597 {"extracondensed", XLFD_SWIDTH_EXTRA_CONDENSED, &Qextra_condensed},
1598 {"extraexpanded", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded},
1599 {"medium", XLFD_SWIDTH_MEDIUM, &Qnormal},
1600 {"narrow", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1601 {"normal", XLFD_SWIDTH_MEDIUM, &Qnormal},
1602 {"regular", XLFD_SWIDTH_MEDIUM, &Qnormal},
1603 {"semicondensed", XLFD_SWIDTH_SEMI_CONDENSED, &Qsemi_condensed},
1604 {"semiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1605 {"ultracondensed", XLFD_SWIDTH_ULTRA_CONDENSED, &Qultra_condensed},
1606 {"ultraexpanded", XLFD_SWIDTH_ULTRA_EXPANDED, &Qultra_expanded},
1607 {"wide", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded}
1608};
1609
1610/* Structure used to hold the result of splitting font names in XLFD
1611 format into their fields. */
1612
1613struct font_name
1614{
1615 /* The original name which is modified destructively by
1616 split_font_name. The pointer is kept here to be able to free it
1617 if it was allocated from the heap. */
1618 char *name;
1619
1620 /* Font name fields. Each vector element points into `name' above.
1621 Fields are NUL-terminated. */
1622 char *fields[XLFD_LAST];
1623
1624 /* Numeric values for those fields that interest us. See
1625 split_font_name for which these are. */
1626 int numeric[XLFD_LAST];
1627};
1628
1629/* The frame in effect when sorting font names. Set temporarily in
1630 sort_fonts so that it is available in font comparison functions. */
1631
1632static struct frame *font_frame;
1633
1634/* Order by which font selection chooses fonts. The default values
1635 mean `first, find a best match for the font width, then for the
1636 font height, then for weight, then for slant.' This variable can be
1637 set via set-face-font-sort-order. */
1638
1639static int font_sort_order[4];
1640
1641
1642/* Look up FONT.fields[FIELD_INDEX] in TABLE which has DIM entries.
1643 TABLE must be sorted by TABLE[i]->name in ascending order. Value
1644 is a pointer to the matching table entry or null if no table entry
1645 matches. */
1646
1647static struct table_entry *
1648xlfd_lookup_field_contents (table, dim, font, field_index)
1649 struct table_entry *table;
1650 int dim;
1651 struct font_name *font;
1652 int field_index;
1653{
1654 /* Function split_font_name converts fields to lower-case, so there
1655 is no need to use xstrlwr or xstricmp here. */
1656 char *s = font->fields[field_index];
1657 int low, mid, high, cmp;
1658
1659 low = 0;
1660 high = dim - 1;
1661
1662 while (low <= high)
1663 {
1664 mid = (low + high) / 2;
1665 cmp = strcmp (table[mid].name, s);
1666
1667 if (cmp < 0)
1668 low = mid + 1;
1669 else if (cmp > 0)
1670 high = mid - 1;
1671 else
1672 return table + mid;
1673 }
1674
1675 return NULL;
1676}
1677
1678
1679/* Return a numeric representation for font name field
1680 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
1681 has DIM entries. Value is the numeric value found or DFLT if no
1682 table entry matches. This function is used to translate weight,
1683 slant, and swidth names of XLFD font names to numeric values. */
1684
1685static INLINE int
1686xlfd_numeric_value (table, dim, font, field_index, dflt)
1687 struct table_entry *table;
1688 int dim;
1689 struct font_name *font;
1690 int field_index;
1691 int dflt;
1692{
1693 struct table_entry *p;
1694 p = xlfd_lookup_field_contents (table, dim, font, field_index);
1695 return p ? p->numeric : dflt;
1696}
1697
1698
1699/* Return a symbolic representation for font name field
1700 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
1701 has DIM entries. Value is the symbolic value found or DFLT if no
1702 table entry matches. This function is used to translate weight,
1703 slant, and swidth names of XLFD font names to symbols. */
1704
1705static INLINE Lisp_Object
1706xlfd_symbolic_value (table, dim, font, field_index, dflt)
1707 struct table_entry *table;
1708 int dim;
1709 struct font_name *font;
1710 int field_index;
1711 int dflt;
1712{
1713 struct table_entry *p;
1714 p = xlfd_lookup_field_contents (table, dim, font, field_index);
1715 return p ? *p->symbol : dflt;
1716}
1717
1718
1719/* Return a numeric value for the slant of the font given by FONT. */
1720
1721static INLINE int
1722xlfd_numeric_slant (font)
1723 struct font_name *font;
1724{
1725 return xlfd_numeric_value (slant_table, DIM (slant_table),
1726 font, XLFD_SLANT, XLFD_SLANT_ROMAN);
1727}
1728
1729
1730/* Return a symbol representing the weight of the font given by FONT. */
1731
1732static INLINE Lisp_Object
1733xlfd_symbolic_slant (font)
1734 struct font_name *font;
1735{
1736 return xlfd_symbolic_value (slant_table, DIM (slant_table),
1737 font, XLFD_SLANT, Qnormal);
1738}
1739
1740
1741/* Return a numeric value for the weight of the font given by FONT. */
1742
1743static INLINE int
1744xlfd_numeric_weight (font)
1745 struct font_name *font;
1746{
1747 return xlfd_numeric_value (weight_table, DIM (weight_table),
1748 font, XLFD_WEIGHT, XLFD_WEIGHT_MEDIUM);
1749}
1750
1751
1752/* Return a symbol representing the slant of the font given by FONT. */
1753
1754static INLINE Lisp_Object
1755xlfd_symbolic_weight (font)
1756 struct font_name *font;
1757{
1758 return xlfd_symbolic_value (weight_table, DIM (weight_table),
1759 font, XLFD_WEIGHT, Qnormal);
1760}
1761
1762
1763/* Return a numeric value for the swidth of the font whose XLFD font
1764 name fields are found in FONT. */
1765
1766static INLINE int
1767xlfd_numeric_swidth (font)
1768 struct font_name *font;
1769{
1770 return xlfd_numeric_value (swidth_table, DIM (swidth_table),
1771 font, XLFD_SWIDTH, XLFD_SWIDTH_MEDIUM);
1772}
1773
1774
1775/* Return a symbolic value for the swidth of FONT. */
1776
1777static INLINE Lisp_Object
1778xlfd_symbolic_swidth (font)
1779 struct font_name *font;
1780{
1781 return xlfd_symbolic_value (swidth_table, DIM (swidth_table),
1782 font, XLFD_SWIDTH, Qnormal);
1783}
1784
1785
1786/* Look up the entry of SYMBOL in the vector TABLE which has DIM
1787 entries. Value is a pointer to the matching table entry or null if
1788 no element of TABLE contains SYMBOL. */
1789
1790static struct table_entry *
1791face_value (table, dim, symbol)
1792 struct table_entry *table;
1793 int dim;
1794 Lisp_Object symbol;
1795{
1796 int i;
1797
1798 xassert (SYMBOLP (symbol));
1799
1800 for (i = 0; i < dim; ++i)
1801 if (EQ (*table[i].symbol, symbol))
1802 break;
1803
1804 return i < dim ? table + i : NULL;
1805}
1806
1807
1808/* Return a numeric value for SYMBOL in the vector TABLE which has DIM
1809 entries. Value is -1 if SYMBOL is not found in TABLE. */
1810
1811static INLINE int
1812face_numeric_value (table, dim, symbol)
1813 struct table_entry *table;
1814 int dim;
1815 Lisp_Object symbol;
1816{
1817 struct table_entry *p = face_value (table, dim, symbol);
1818 return p ? p->numeric : -1;
1819}
1820
1821
1822/* Return a numeric value representing the weight specified by Lisp
1823 symbol WEIGHT. Value is one of the enumerators of enum
1824 xlfd_weight. */
1825
1826static INLINE int
1827face_numeric_weight (weight)
1828 Lisp_Object weight;
1829{
1830 return face_numeric_value (weight_table, DIM (weight_table), weight);
1831}
1832
1833
1834/* Return a numeric value representing the slant specified by Lisp
1835 symbol SLANT. Value is one of the enumerators of enum xlfd_slant. */
1836
1837static INLINE int
1838face_numeric_slant (slant)
1839 Lisp_Object slant;
1840{
1841 return face_numeric_value (slant_table, DIM (slant_table), slant);
1842}
1843
1844
1845/* Return a numeric value representing the swidth specified by Lisp
1846 symbol WIDTH. Value is one of the enumerators of enum xlfd_swidth. */
1847
1848static int
1849face_numeric_swidth (width)
1850 Lisp_Object width;
1851{
1852 return face_numeric_value (swidth_table, DIM (swidth_table), width);
1853}
1854
1855
1856#ifdef HAVE_X_WINDOWS
1857
1858/* Return non-zero if FONT is the name of a fixed-pitch font. */
1859
1860static INLINE int
1861xlfd_fixed_p (font)
1862 struct font_name *font;
1863{
1864 /* Function split_font_name converts fields to lower-case, so there
1865 is no need to use tolower here. */
1866 return *font->fields[XLFD_SPACING] != 'p';
1867}
1868
1869
1870/* Return the point size of FONT on frame F, measured in 1/10 pt.
1871
1872 The actual height of the font when displayed on F depends on the
1873 resolution of both the font and frame. For example, a 10pt font
1874 designed for a 100dpi display will display larger than 10pt on a
1875 75dpi display. (It's not unusual to use fonts not designed for the
1876 display one is using. For example, some intlfonts are available in
1877 72dpi versions, only.)
1878
1879 Value is the real point size of FONT on frame F, or 0 if it cannot
1880 be determined. */
1881
1882static INLINE int
1883xlfd_point_size (f, font)
1884 struct frame *f;
1885 struct font_name *font;
1886{
1887 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
1888 double font_resy = atoi (font->fields[XLFD_RESY]);
1889 double font_pt = atoi (font->fields[XLFD_POINT_SIZE]);
1890 int real_pt;
1891
1892 if (font_resy == 0 || font_pt == 0)
1893 real_pt = 0;
1894 else
1895 real_pt = (font_resy / resy) * font_pt + 0.5;
1896
1897 return real_pt;
1898}
1899
1900
1901/* Split XLFD font name FONT->name destructively into NUL-terminated,
1902 lower-case fields in FONT->fields. NUMERIC_P non-zero means
1903 compute numeric values for fields XLFD_POINT_SIZE, XLFD_SWIDTH,
1904 XLFD_RESY, XLFD_SLANT, and XLFD_WEIGHT in FONT->numeric. Value is
1905 zero if the font name doesn't have the format we expect. The
1906 expected format is a font name that starts with a `-' and has
1907 XLFD_LAST fields separated by `-'. (The XLFD specification allows
1908 forms of font names where certain field contents are enclosed in
1909 square brackets. We don't support that, for now. */
1910
1911static int
1912split_font_name (f, font, numeric_p)
1913 struct frame *f;
1914 struct font_name *font;
1915 int numeric_p;
1916{
1917 int i = 0;
1918 int success_p;
1919
1920 if (*font->name == '-')
1921 {
1922 char *p = xstrlwr (font->name) + 1;
1923
1924 while (i < XLFD_LAST)
1925 {
1926 font->fields[i] = p;
1927 ++i;
1928
1929 while (*p && *p != '-')
1930 ++p;
1931
1932 if (*p != '-')
1933 break;
1934
1935 *p++ = 0;
1936 }
1937 }
1938
1939 success_p = i == XLFD_LAST;
1940
1941 /* If requested, and font name was in the expected format,
1942 compute numeric values for some fields. */
1943 if (numeric_p && success_p)
1944 {
1945 font->numeric[XLFD_POINT_SIZE] = xlfd_point_size (f, font);
1946 font->numeric[XLFD_RESY] = atoi (font->fields[XLFD_RESY]);
1947 font->numeric[XLFD_SLANT] = xlfd_numeric_slant (font);
1948 font->numeric[XLFD_WEIGHT] = xlfd_numeric_weight (font);
1949 font->numeric[XLFD_SWIDTH] = xlfd_numeric_swidth (font);
1950 }
1951
1952 return success_p;
1953}
1954
1955
1956/* Build an XLFD font name from font name fields in FONT. Value is a
1957 pointer to the font name, which is allocated via xmalloc. */
1958
1959static char *
1960build_font_name (font)
1961 struct font_name *font;
1962{
1963 int i;
1964 int size = 100;
1965 char *font_name = (char *) xmalloc (size);
1966 int total_length = 0;
1967
1968 for (i = 0; i < XLFD_LAST; ++i)
1969 {
1970 /* Add 1 because of the leading `-'. */
1971 int len = strlen (font->fields[i]) + 1;
1972
1973 /* Reallocate font_name if necessary. Add 1 for the final
1974 NUL-byte. */
1975 if (total_length + len + 1 >= size)
1976 {
1977 int new_size = max (2 * size, size + len + 1);
1978 int sz = new_size * sizeof *font_name;
1979 font_name = (char *) xrealloc (font_name, sz);
1980 size = new_size;
1981 }
1982
1983 font_name[total_length] = '-';
1984 bcopy (font->fields[i], font_name + total_length + 1, len - 1);
1985 total_length += len;
1986 }
1987
1988 font_name[total_length] = 0;
1989 return font_name;
1990}
1991
1992
1993/* Free an array FONTS of N font_name structures. This frees FONTS
1994 itself and all `name' fields in its elements. */
1995
1996static INLINE void
1997free_font_names (fonts, n)
1998 struct font_name *fonts;
1999 int n;
2000{
2001 while (n)
2002 xfree (fonts[--n].name);
2003 xfree (fonts);
2004}
2005
2006
2007/* Sort vector FONTS of font_name structures which contains NFONTS
2008 elements using qsort and comparison function CMPFN. F is the frame
2009 on which the fonts will be used. The global variable font_frame
2010 is temporarily set to F to make it available in CMPFN. */
2011
2012static INLINE void
2013sort_fonts (f, fonts, nfonts, cmpfn)
2014 struct frame *f;
2015 struct font_name *fonts;
2016 int nfonts;
2017 int (*cmpfn) P_ ((const void *, const void *));
2018{
2019 font_frame = f;
2020 qsort (fonts, nfonts, sizeof *fonts, cmpfn);
2021 font_frame = NULL;
2022}
2023
2024
2025/* Get fonts matching PATTERN on frame F. If F is null, use the first
2026 display in x_display_list. FONTS is a pointer to a vector of
2027 NFONTS font_name structures. TRY_ALTERNATIVES_P non-zero means try
2028 alternative patterns from Valternate_fontname_alist if no fonts are
2029 found matching PATTERN. SCALABLE_FONTS_P non-zero means include
2030 scalable fonts.
2031
2032 For all fonts found, set FONTS[i].name to the name of the font,
2033 allocated via xmalloc, and split font names into fields. Ignore
2034 fonts that we can't parse. Value is the number of fonts found.
2035
2036 This is similar to x_list_fonts. The differences are:
2037
2038 1. It avoids consing.
2039 2. It never calls XLoadQueryFont. */
2040
2041static int
2042x_face_list_fonts (f, pattern, fonts, nfonts, try_alternatives_p,
2043 scalable_fonts_p)
2044 struct frame *f;
2045 char *pattern;
2046 struct font_name *fonts;
2047 int nfonts, try_alternatives_p;
2048 int scalable_fonts_p;
2049{
2050 Display *dpy = f ? FRAME_X_DISPLAY (f) : x_display_list->display;
2051 int n, i, j;
2052 char **names;
2053
2054 /* Get the list of fonts matching PATTERN from the X server. */
2055 BLOCK_INPUT;
2056 names = XListFonts (dpy, pattern, nfonts, &n);
2057 UNBLOCK_INPUT;
2058
2059 if (names)
2060 {
2061 /* Make a copy of the font names we got from X, and
2062 split them into fields. */
2063 for (i = j = 0; i < n; ++i)
2064 {
2065 /* Make a copy of the font name. */
2066 fonts[j].name = xstrdup (names[i]);
2067
2068 /* Ignore fonts having a name that we can't parse. */
2069 if (!split_font_name (f, fonts + j, 1))
2070 xfree (fonts[j].name);
2071 else if (font_scalable_p (fonts + j))
2072 {
2073#if SCALABLE_FONTS
2074 if (!scalable_fonts_p
2075 || !may_use_scalable_font_p (fonts + j, names[i]))
2076 xfree (fonts[j].name);
2077 else
2078 ++j;
2079#else /* !SCALABLE_FONTS */
2080 /* Always ignore scalable fonts. */
2081 xfree (fonts[j].name);
2082#endif /* !SCALABLE_FONTS */
2083 }
2084 else
2085 ++j;
2086 }
2087
2088 n = j;
2089
2090 /* Free font names. */
2091 BLOCK_INPUT;
2092 XFreeFontNames (names);
2093 UNBLOCK_INPUT;
2094 }
2095
2096
2097 /* If no fonts found, try patterns from Valternate_fontname_alist. */
2098 if (n == 0 && try_alternatives_p)
2099 {
2100 Lisp_Object list = Valternate_fontname_alist;
2101
2102 while (CONSP (list))
2103 {
2104 Lisp_Object entry = XCAR (list);
2105 if (CONSP (entry)
2106 && STRINGP (XCAR (entry))
2107 && strcmp (XSTRING (XCAR (entry))->data, pattern) == 0)
2108 break;
2109 list = XCDR (list);
2110 }
2111
2112 if (CONSP (list))
2113 {
2114 Lisp_Object patterns = XCAR (list);
2115 Lisp_Object name;
2116
2117 while (CONSP (patterns)
2118 /* If list is screwed up, give up. */
2119 && (name = XCAR (patterns),
2120 STRINGP (name))
2121 /* Ignore patterns equal to PATTERN because we tried that
2122 already with no success. */
2123 && (strcmp (XSTRING (name)->data, pattern) == 0
2124 || (n = x_face_list_fonts (f, XSTRING (name)->data,
2125 fonts, nfonts, 0,
2126 scalable_fonts_p),
2127 n == 0)))
2128 patterns = XCDR (patterns);
2129 }
2130 }
2131
2132 return n;
2133}
2134
2135
2136/* Determine the first font matching PATTERN on frame F. Return in
2137 *FONT the matching font name, split into fields. Value is non-zero
2138 if a match was found. */
2139
2140static int
2141first_font_matching (f, pattern, font)
2142 struct frame *f;
2143 char *pattern;
2144 struct font_name *font;
2145{
2146 int nfonts = 100;
2147 struct font_name *fonts;
2148
2149 fonts = (struct font_name *) xmalloc (nfonts * sizeof *fonts);
2150 nfonts = x_face_list_fonts (f, pattern, fonts, nfonts, 1, 0);
2151
2152 if (nfonts > 0)
2153 {
2154 bcopy (&fonts[0], font, sizeof *font);
2155
2156 fonts[0].name = NULL;
2157 free_font_names (fonts, nfonts);
2158 }
2159
2160 return nfonts > 0;
2161}
2162
2163
2164/* Determine fonts matching PATTERN on frame F. Sort resulting fonts
2165 using comparison function CMPFN. Value is the number of fonts
2166 found. If value is non-zero, *FONTS is set to a vector of
2167 font_name structures allocated from the heap containing matching
2168 fonts. Each element of *FONTS contains a name member that is also
2169 allocated from the heap. Font names in these structures are split
2170 into fields. Use free_font_names to free such an array. */
2171
2172static int
2173sorted_font_list (f, pattern, cmpfn, fonts)
2174 struct frame *f;
2175 char *pattern;
2176 int (*cmpfn) P_ ((const void *, const void *));
2177 struct font_name **fonts;
2178{
2179 int nfonts;
2180
2181 /* Get the list of fonts matching pattern. 100 should suffice. */
057df17c
GM
2182 nfonts = DEFAULT_FONT_LIST_LIMIT;
2183 if (INTEGERP (Vfont_list_limit) && XINT (Vfont_list_limit) > 0)
2184 nfonts = XFASTINT (Vfont_list_limit);
2185
82641697
GM
2186 *fonts = (struct font_name *) xmalloc (nfonts * sizeof **fonts);
2187#if SCALABLE_FONTS
2188 nfonts = x_face_list_fonts (f, pattern, *fonts, nfonts, 1, 1);
2189#else
2190 nfonts = x_face_list_fonts (f, pattern, *fonts, nfonts, 1, 0);
2191#endif
2192
2193 /* Sort the resulting array and return it in *FONTS. If no
2194 fonts were found, make sure to set *FONTS to null. */
2195 if (nfonts)
2196 sort_fonts (f, *fonts, nfonts, cmpfn);
2197 else
2198 {
2199 xfree (*fonts);
2200 *fonts = NULL;
2201 }
2202
2203 return nfonts;
2204}
2205
2206
2207/* Compare two font_name structures *A and *B. Value is analogous to
2208 strcmp. Sort order is given by the global variable
2209 font_sort_order. Font names are sorted so that, everything else
2210 being equal, fonts with a resolution closer to that of the frame on
2211 which they are used are listed first. The global variable
2212 font_frame is the frame on which we operate. */
2213
2214static int
2215cmp_font_names (a, b)
2216 const void *a, *b;
2217{
2218 struct font_name *x = (struct font_name *) a;
2219 struct font_name *y = (struct font_name *) b;
2220 int cmp;
2221
2222 /* All strings have been converted to lower-case by split_font_name,
2223 so we can use strcmp here. */
2224 cmp = strcmp (x->fields[XLFD_FAMILY], y->fields[XLFD_FAMILY]);
2225 if (cmp == 0)
2226 {
2227 int i;
2228
2229 for (i = 0; i < DIM (font_sort_order) && cmp == 0; ++i)
2230 {
2231 int j = font_sort_order[i];
2232 cmp = x->numeric[j] - y->numeric[j];
2233 }
2234
2235 if (cmp == 0)
2236 {
2237 /* Everything else being equal, we prefer fonts with an
2238 y-resolution closer to that of the frame. */
2239 int resy = FRAME_X_DISPLAY_INFO (font_frame)->resy;
2240 int x_resy = x->numeric[XLFD_RESY];
2241 int y_resy = y->numeric[XLFD_RESY];
2242 cmp = abs (resy - x_resy) - abs (resy - y_resy);
2243 }
2244 }
2245
2246 return cmp;
2247}
2248
2249
2250/* Get a sorted list of fonts of family FAMILY on frame F. If PATTERN
2251 is non-null list fonts matching that pattern. Otherwise, if
2252 REGISTRY_AND_ENCODING is non-null return only fonts with that
2253 registry and encoding, otherwise return fonts of any registry and
2254 encoding. Set *FONTS to a vector of font_name structures allocated
2255 from the heap containing the fonts found. Value is the number of
2256 fonts found. */
2257
2258static int
2259font_list (f, pattern, family, registry_and_encoding, fonts)
2260 struct frame *f;
2261 char *pattern;
2262 char *family;
2263 char *registry_and_encoding;
2264 struct font_name **fonts;
2265{
2266 if (pattern == NULL)
2267 {
2268 if (family == NULL)
2269 family = "*";
2270
2271 if (registry_and_encoding == NULL)
2272 registry_and_encoding = "*";
2273
2274 pattern = (char *) alloca (strlen (family)
2275 + strlen (registry_and_encoding)
2276 + 10);
2277 if (index (family, '-'))
2278 sprintf (pattern, "-%s-*-%s", family, registry_and_encoding);
2279 else
2280 sprintf (pattern, "-*-%s-*-%s", family, registry_and_encoding);
2281 }
2282
2283 return sorted_font_list (f, pattern, cmp_font_names, fonts);
2284}
2285
2286
2287/* Remove elements from LIST whose cars are `equal'. Called from
92610620 2288 x-family-fonts and x-font-family-list to remove duplicate font
82641697
GM
2289 entries. */
2290
2291static void
2292remove_duplicates (list)
2293 Lisp_Object list;
2294{
2295 Lisp_Object tail = list;
2296
2297 while (!NILP (tail) && !NILP (XCDR (tail)))
2298 {
2299 Lisp_Object next = XCDR (tail);
2300 if (!NILP (Fequal (XCAR (next), XCAR (tail))))
2301 XCDR (tail) = XCDR (next);
2302 else
2303 tail = XCDR (tail);
2304 }
2305}
2306
2307
92610620 2308DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
82641697
GM
2309 "Return a list of available fonts of family FAMILY on FRAME.\n\
2310If FAMILY is omitted or nil, list all families.\n\
2311Otherwise, FAMILY must be a string, possibly containing wildcards\n\
2312`?' and `*'.\n\
2313If FRAME is omitted or nil, use the selected frame.\n\
2314Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT\n\
057df17c 2315SLANT FIXED-P FULL REGISTRY-AND-ENCODING].\n\
82641697
GM
2316FAMILY is the font family name. POINT-SIZE is the size of the\n\
2317font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the\n\
2318width, weight and slant of the font. These symbols are the same as for\n\
2319face attributes. FIXED-P is non-nil if the font is fixed-pitch.\n\
057df17c
GM
2320FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string\n\
2321giving the registry and encoding of the font.\n\
82641697
GM
2322The result list is sorted according to the current setting of\n\
2323the face font sort order.")
2324 (family, frame)
2325 Lisp_Object family, frame;
2326{
2327 struct frame *f = check_x_frame (frame);
2328 struct font_name *fonts;
2329 int i, nfonts;
2330 Lisp_Object result;
2331 struct gcpro gcpro1;
2332 char *family_pattern;
2333
2334 if (NILP (family))
2335 family_pattern = "*";
2336 else
2337 {
2338 CHECK_STRING (family, 1);
2339 family_pattern = LSTRDUPA (family);
2340 }
2341
2342 result = Qnil;
2343 GCPRO1 (result);
2344 nfonts = font_list (f, NULL, family_pattern, NULL, &fonts);
2345 for (i = nfonts - 1; i >= 0; --i)
2346 {
057df17c
GM
2347 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
2348 char *tem;
82641697
GM
2349
2350#define ASET(VECTOR, IDX, VAL) (XVECTOR (VECTOR)->contents[IDX] = (VAL))
2351
2352 ASET (v, 0, build_string (fonts[i].fields[XLFD_FAMILY]));
2353 ASET (v, 1, xlfd_symbolic_swidth (fonts + i));
2354 ASET (v, 2, make_number (xlfd_point_size (f, fonts + i)));
2355 ASET (v, 3, xlfd_symbolic_weight (fonts + i));
2356 ASET (v, 4, xlfd_symbolic_slant (fonts + i));
2357 ASET (v, 5, xlfd_fixed_p (fonts + i) ? Qt : Qnil);
057df17c
GM
2358 tem = build_font_name (fonts + i);
2359 ASET (v, 6, build_string (tem));
2360 sprintf (tem, "%s-%s", fonts[i].fields[XLFD_REGISTRY],
2361 fonts[i].fields[XLFD_ENCODING]);
2362 ASET (v, 7, build_string (tem));
2363 xfree (tem);
2364
82641697
GM
2365 result = Fcons (v, result);
2366
2367#undef ASET
2368 }
2369
2370 remove_duplicates (result);
2371 free_font_names (fonts, nfonts);
2372 UNGCPRO;
2373 return result;
2374}
2375
2376
2377DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
2378 0, 1, 0,
2379 "Return a list of available font families on FRAME.\n\
2380If FRAME is omitted or nil, use the selected frame.\n\
2381Value is a list of conses (FAMILY . FIXED-P) where FAMILY\n\
2382is a font family, and FIXED-P is non-nil if fonts of that family\n\
2383are fixed-pitch.")
2384 (frame)
2385 Lisp_Object frame;
2386{
2387 struct frame *f = check_x_frame (frame);
2388 int nfonts, i;
2389 struct font_name *fonts;
2390 Lisp_Object result;
2391 struct gcpro gcpro1;
057df17c
GM
2392 int count = specpdl_ptr - specpdl;
2393 int limit;
2394
2395 /* Let's consider all fonts. Increase the limit for matching
2396 fonts until we have them all. */
2397 for (limit = 500;;)
2398 {
2399 specbind (intern ("font-list-limit"), make_number (limit));
2400 nfonts = font_list (f, NULL, "*", NULL, &fonts);
2401
2402 if (nfonts == limit)
2403 {
2404 free_font_names (fonts, nfonts);
2405 limit *= 2;
2406 }
2407 else
2408 break;
2409 }
82641697 2410
82641697
GM
2411 result = Qnil;
2412 GCPRO1 (result);
2413 for (i = nfonts - 1; i >= 0; --i)
2414 result = Fcons (Fcons (build_string (fonts[i].fields[XLFD_FAMILY]),
2415 xlfd_fixed_p (fonts + i) ? Qt : Qnil),
2416 result);
2417
2418 remove_duplicates (result);
2419 free_font_names (fonts, nfonts);
2420 UNGCPRO;
057df17c 2421 return unbind_to (count, result);
82641697
GM
2422}
2423
2424
2425DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
2426 "Return a list of the names of available fonts matching PATTERN.\n\
2427If optional arguments FACE and FRAME are specified, return only fonts\n\
2428the same size as FACE on FRAME.\n\
2429PATTERN is a string, perhaps with wildcard characters;\n\
2430 the * character matches any substring, and\n\
2431 the ? character matches any single character.\n\
2432 PATTERN is case-insensitive.\n\
2433FACE is a face name--a symbol.\n\
2434\n\
2435The return value is a list of strings, suitable as arguments to\n\
2436set-face-font.\n\
2437\n\
2438Fonts Emacs can't use may or may not be excluded\n\
2439even if they match PATTERN and FACE.\n\
2440The optional fourth argument MAXIMUM sets a limit on how many\n\
2441fonts to match. The first MAXIMUM fonts are reported.\n\
2442The optional fifth argument WIDTH, if specified, is a number of columns\n\
2443occupied by a character of a font. In that case, return only fonts\n\
2444the WIDTH times as wide as FACE on FRAME.")
2445 (pattern, face, frame, maximum, width)
2446 Lisp_Object pattern, face, frame, maximum, width;
2447{
2448 struct frame *f;
2449 int size;
2450 int maxnames;
2451
2452 check_x ();
2453 CHECK_STRING (pattern, 0);
2454
2455 if (NILP (maximum))
2456 maxnames = 2000;
2457 else
2458 {
2459 CHECK_NATNUM (maximum, 0);
2460 maxnames = XINT (maximum);
2461 }
2462
2463 if (!NILP (width))
2464 CHECK_NUMBER (width, 4);
2465
2466 /* We can't simply call check_x_frame because this function may be
2467 called before any frame is created. */
2468 f = frame_or_selected_frame (frame, 2);
2469 if (!FRAME_X_P (f))
2470 {
2471 /* Perhaps we have not yet created any frame. */
2472 f = NULL;
2473 face = Qnil;
2474 }
2475
2476 /* Determine the width standard for comparison with the fonts we find. */
2477
2478 if (NILP (face))
2479 size = 0;
2480 else
2481 {
2482 /* This is of limited utility since it works with character
2483 widths. Keep it for compatibility. --gerd. */
2484 int face_id = lookup_named_face (f, face, CHARSET_ASCII);
2485 struct face *face = FACE_FROM_ID (f, face_id);
2486
2487 if (face->font)
2488 size = face->font->max_bounds.width;
2489 else
2490 size = FRAME_FONT (f)->max_bounds.width;
2491
2492 if (!NILP (width))
2493 size *= XINT (width);
2494 }
2495
2496 {
2497 Lisp_Object args[2];
2498
2499 args[0] = x_list_fonts (f, pattern, size, maxnames);
2500 if (f == NULL)
2501 /* We don't have to check fontsets. */
2502 return args[0];
2503 args[1] = list_fontsets (f, pattern, size);
2504 return Fnconc (2, args);
2505 }
2506}
2507
2508#endif /* HAVE_X_WINDOWS */
2509
2510
2511\f
2512/***********************************************************************
2513 Lisp Faces
2514 ***********************************************************************/
2515
2516/* Access face attributes of face FACE, a Lisp vector. */
2517
2518#define LFACE_FAMILY(LFACE) \
2519 XVECTOR (LFACE)->contents[LFACE_FAMILY_INDEX]
2520#define LFACE_HEIGHT(LFACE) \
2521 XVECTOR (LFACE)->contents[LFACE_HEIGHT_INDEX]
2522#define LFACE_WEIGHT(LFACE) \
2523 XVECTOR (LFACE)->contents[LFACE_WEIGHT_INDEX]
2524#define LFACE_SLANT(LFACE) \
2525 XVECTOR (LFACE)->contents[LFACE_SLANT_INDEX]
2526#define LFACE_UNDERLINE(LFACE) \
2527 XVECTOR (LFACE)->contents[LFACE_UNDERLINE_INDEX]
2528#define LFACE_INVERSE(LFACE) \
2529 XVECTOR (LFACE)->contents[LFACE_INVERSE_INDEX]
2530#define LFACE_FOREGROUND(LFACE) \
2531 XVECTOR (LFACE)->contents[LFACE_FOREGROUND_INDEX]
2532#define LFACE_BACKGROUND(LFACE) \
2533 XVECTOR (LFACE)->contents[LFACE_BACKGROUND_INDEX]
2534#define LFACE_STIPPLE(LFACE) \
2535 XVECTOR (LFACE)->contents[LFACE_STIPPLE_INDEX]
2536#define LFACE_SWIDTH(LFACE) \
2537 XVECTOR (LFACE)->contents[LFACE_SWIDTH_INDEX]
2538#define LFACE_OVERLINE(LFACE) \
2539 XVECTOR (LFACE)->contents[LFACE_OVERLINE_INDEX]
2540#define LFACE_STRIKE_THROUGH(LFACE) \
2541 XVECTOR (LFACE)->contents[LFACE_STRIKE_THROUGH_INDEX]
2542#define LFACE_BOX(LFACE) \
2543 XVECTOR (LFACE)->contents[LFACE_BOX_INDEX]
2544
2545/* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
2546 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
2547
2548#define LFACEP(LFACE) \
2549 (VECTORP (LFACE) \
2550 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
2551 && EQ (XVECTOR (LFACE)->contents[0], Qface))
2552
2553
2554#if GLYPH_DEBUG
2555
2556/* Check consistency of Lisp face attribute vector ATTRS. */
2557
2558static void
2559check_lface_attrs (attrs)
2560 Lisp_Object *attrs;
2561{
2562 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
2563 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
2564 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
2565 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
2566 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
2567 || INTEGERP (attrs[LFACE_HEIGHT_INDEX]));
2568 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
2569 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
2570 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
2571 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
2572 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
2573 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
2574 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
2575 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
2576 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
2577 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
2578 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2579 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2580 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
2581 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
2582 || SYMBOLP (attrs[LFACE_BOX_INDEX])
2583 || STRINGP (attrs[LFACE_BOX_INDEX])
2584 || INTEGERP (attrs[LFACE_BOX_INDEX])
2585 || CONSP (attrs[LFACE_BOX_INDEX]));
2586 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
2587 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
2588 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
2589 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
2590 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
2591 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
2592#ifdef HAVE_WINDOW_SYSTEM
2593 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
2594 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
2595 || !NILP (Fpixmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
2596#endif
2597}
2598
2599
2600/* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
2601
2602static void
2603check_lface (lface)
2604 Lisp_Object lface;
2605{
2606 if (!NILP (lface))
2607 {
2608 xassert (LFACEP (lface));
2609 check_lface_attrs (XVECTOR (lface)->contents);
2610 }
2611}
2612
2613#else /* GLYPH_DEBUG == 0 */
2614
2615#define check_lface_attrs(attrs) (void) 0
2616#define check_lface(lface) (void) 0
2617
2618#endif /* GLYPH_DEBUG == 0 */
2619
2620
2621/* Return the face definition of FACE_NAME on frame F. F null means
2622 return the global definition. FACE_NAME may be a string or a
2623 symbol (apparently Emacs 20.2 allows strings as face names in face
92610620
GM
2624 text properties; ediff uses that). If FACE_NAME is an alias for
2625 another face, return that face's definition. If SIGNAL_P is
2626 non-zero, signal an error if FACE_NAME is not a valid face name.
2627 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
2628 name. */
82641697
GM
2629
2630static INLINE Lisp_Object
2631lface_from_face_name (f, face_name, signal_p)
2632 struct frame *f;
2633 Lisp_Object face_name;
2634 int signal_p;
2635{
92610620 2636 Lisp_Object lface, alias;
82641697
GM
2637
2638 if (STRINGP (face_name))
2639 face_name = intern (XSTRING (face_name)->data);
2640
92610620
GM
2641 /* If FACE_NAME is an alias for another face, return the definition
2642 of the aliased face. */
2643 alias = Fget (face_name, Qface_alias);
2644 if (!NILP (alias))
2645 face_name = alias;
2646
82641697
GM
2647 if (f)
2648 lface = assq_no_quit (face_name, f->face_alist);
2649 else
2650 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2651
2652 if (CONSP (lface))
2653 lface = XCDR (lface);
2654 else if (signal_p)
2655 signal_error ("Invalid face", face_name);
2656
2657 check_lface (lface);
2658 return lface;
2659}
2660
2661
2662/* Get face attributes of face FACE_NAME from frame-local faces on
2663 frame F. Store the resulting attributes in ATTRS which must point
2664 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2665 is non-zero, signal an error if FACE_NAME does not name a face.
2666 Otherwise, value is zero if FACE_NAME is not a face. */
2667
2668static INLINE int
2669get_lface_attributes (f, face_name, attrs, signal_p)
2670 struct frame *f;
2671 Lisp_Object face_name;
2672 Lisp_Object *attrs;
2673 int signal_p;
2674{
2675 Lisp_Object lface;
2676 int success_p;
2677
2678 lface = lface_from_face_name (f, face_name, signal_p);
2679 if (!NILP (lface))
2680 {
2681 bcopy (XVECTOR (lface)->contents, attrs,
2682 LFACE_VECTOR_SIZE * sizeof *attrs);
2683 success_p = 1;
2684 }
2685 else
2686 success_p = 0;
2687
2688 return success_p;
2689}
2690
2691
2692/* Non-zero if all attributes in face attribute vector ATTRS are
2693 specified, i.e. are non-nil. */
2694
2695static int
2696lface_fully_specified_p (attrs)
2697 Lisp_Object *attrs;
2698{
2699 int i;
2700
2701 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2702 if (UNSPECIFIEDP (attrs[i]))
2703 break;
2704
2705 return i == LFACE_VECTOR_SIZE;
2706}
2707
2708
2709#ifdef HAVE_X_WINDOWS
2710
2711/* Set font-related attributes of Lisp face LFACE from XLFD font name
2712 FONT_NAME. If FORCE_P is zero, set only unspecified attributes of
2713 LFACE. Ignore fields of FONT_NAME containing wildcards. Value is
2714 zero if not successful because FONT_NAME was not in a valid format.
2715 A valid format is one that is suitable for split_font_name, see the
2716 comment there. */
2717
2718static int
2719set_lface_from_font_name (f, lface, font_name, force_p)
2720 struct frame *f;
2721 Lisp_Object lface;
2722 char *font_name;
2723 int force_p;
2724{
2725 struct font_name font;
2726 char *buffer;
2727 int pt;
2728 int free_font_name_p = 0;
2729
2730 /* If FONT_NAME contains wildcards, use the first matching font. */
2731 if (index (font_name, '*') || index (font_name, '?'))
2732 {
2733 if (!first_font_matching (f, font_name, &font))
2734 return 0;
2735 free_font_name_p = 1;
2736 }
2737 else
2738 {
2739 font.name = STRDUPA (font_name);
2740 if (!split_font_name (f, &font, 1))
2741 {
2742 /* The font name may be something like `6x13'. Make
2743 sure we use the full name. */
2744 struct font_info *font_info;
2745
2746 BLOCK_INPUT;
2747 font_info = fs_load_font (f, FRAME_X_FONT_TABLE (f),
2748 CHARSET_ASCII, font_name, -1);
2749 UNBLOCK_INPUT;
2750
2751 if (!font_info)
2752 return 0;
2753
2754 font.name = STRDUPA (font_info->full_name);
2755 split_font_name (f, &font, 1);
2756 }
2757
2758 /* FONT_NAME should not be a fontset name, here. */
2759 xassert (xstricmp (font.fields[XLFD_REGISTRY], "fontset") != 0);
2760 }
2761
2762 /* Set attributes only if unspecified, otherwise face defaults for
2763 new frames would never take effect. */
2764
2765 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2766 {
2767 buffer = (char *) alloca (strlen (font.fields[XLFD_FAMILY])
2768 + strlen (font.fields[XLFD_FOUNDRY])
2769 + 2);
2770 sprintf (buffer, "%s-%s", font.fields[XLFD_FOUNDRY],
2771 font.fields[XLFD_FAMILY]);
2772 LFACE_FAMILY (lface) = build_string (buffer);
2773 }
2774
2775 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2776 {
2777 pt = xlfd_point_size (f, &font);
2778 xassert (pt > 0);
2779 LFACE_HEIGHT (lface) = make_number (pt);
2780 }
2781
2782 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2783 LFACE_SWIDTH (lface) = xlfd_symbolic_swidth (&font);
2784
2785 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2786 LFACE_WEIGHT (lface) = xlfd_symbolic_weight (&font);
2787
2788 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2789 LFACE_SLANT (lface) = xlfd_symbolic_slant (&font);
2790
2791 if (free_font_name_p)
2792 xfree (font.name);
2793
2794 return 1;
2795}
2796
2797#endif /* HAVE_X_WINDOWS */
2798
2799
2800/* Merge two Lisp face attribute vectors FROM and TO and store the
2801 resulting attributes in TO. Every non-nil attribute of FROM
2802 overrides the corresponding attribute of TO. */
2803
2804static INLINE void
2805merge_face_vectors (from, to)
2806 Lisp_Object *from, *to;
2807{
2808 int i;
2809 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2810 if (!UNSPECIFIEDP (from[i]))
2811 to[i] = from[i];
2812}
2813
2814
2815/* Given a Lisp face attribute vector TO and a Lisp object PROP that
2816 is a face property, determine the resulting face attributes on
2817 frame F, and store them in TO. PROP may be a single face
2818 specification or a list of such specifications. Each face
2819 specification can be
2820
2821 1. A symbol or string naming a Lisp face.
2822
2823 2. A property list of the form (KEYWORD VALUE ...) where each
2824 KEYWORD is a face attribute name, and value is an appropriate value
2825 for that attribute.
2826
2827 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2828 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2829 for compatibility with 20.2.
2830
2831 Face specifications earlier in lists take precedence over later
2832 specifications. */
2833
2834static void
2835merge_face_vector_with_property (f, to, prop)
2836 struct frame *f;
2837 Lisp_Object *to;
2838 Lisp_Object prop;
2839{
2840 if (CONSP (prop))
2841 {
2842 Lisp_Object first = XCAR (prop);
2843
2844 if (EQ (first, Qforeground_color)
2845 || EQ (first, Qbackground_color))
2846 {
2847 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2848 . COLOR). COLOR must be a string. */
2849 Lisp_Object color_name = XCDR (prop);
2850 Lisp_Object color = first;
2851
2852 if (STRINGP (color_name))
2853 {
2854 if (EQ (color, Qforeground_color))
2855 to[LFACE_FOREGROUND_INDEX] = color_name;
2856 else
2857 to[LFACE_BACKGROUND_INDEX] = color_name;
2858 }
2859 else
7dbdfcf8 2860 add_to_log (f, "Invalid face color", color_name, Qnil);
82641697
GM
2861 }
2862 else if (SYMBOLP (first)
2863 && *XSYMBOL (first)->name->data == ':')
2864 {
2865 /* Assume this is the property list form. */
2866 while (CONSP (prop) && CONSP (XCDR (prop)))
2867 {
2868 Lisp_Object keyword = XCAR (prop);
2869 Lisp_Object value = XCAR (XCDR (prop));
2870
2871 if (EQ (keyword, QCfamily))
2872 {
2873 if (STRINGP (value))
2874 to[LFACE_FAMILY_INDEX] = value;
2875 else
7dbdfcf8 2876 add_to_log (f, "Illegal face font family", value, Qnil);
82641697
GM
2877 }
2878 else if (EQ (keyword, QCheight))
2879 {
2880 if (INTEGERP (value))
2881 to[LFACE_HEIGHT_INDEX] = value;
2882 else
7dbdfcf8 2883 add_to_log (f, "Illegal face font height", value, Qnil);
82641697
GM
2884 }
2885 else if (EQ (keyword, QCweight))
2886 {
2887 if (SYMBOLP (value)
2888 && face_numeric_weight (value) >= 0)
2889 to[LFACE_WEIGHT_INDEX] = value;
2890 else
7dbdfcf8 2891 add_to_log (f, "Illegal face weight", value, Qnil);
82641697
GM
2892 }
2893 else if (EQ (keyword, QCslant))
2894 {
2895 if (SYMBOLP (value)
2896 && face_numeric_slant (value) >= 0)
2897 to[LFACE_SLANT_INDEX] = value;
2898 else
7dbdfcf8 2899 add_to_log (f, "Illegal face slant", value, Qnil);
82641697
GM
2900 }
2901 else if (EQ (keyword, QCunderline))
2902 {
2903 if (EQ (value, Qt)
2904 || NILP (value)
2905 || STRINGP (value))
2906 to[LFACE_UNDERLINE_INDEX] = value;
2907 else
7dbdfcf8 2908 add_to_log (f, "Illegal face underline", value, Qnil);
82641697
GM
2909 }
2910 else if (EQ (keyword, QCoverline))
2911 {
2912 if (EQ (value, Qt)
2913 || NILP (value)
2914 || STRINGP (value))
2915 to[LFACE_OVERLINE_INDEX] = value;
2916 else
7dbdfcf8 2917 add_to_log (f, "Illegal face overline", value, Qnil);
82641697
GM
2918 }
2919 else if (EQ (keyword, QCstrike_through))
2920 {
2921 if (EQ (value, Qt)
2922 || NILP (value)
2923 || STRINGP (value))
2924 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2925 else
7dbdfcf8 2926 add_to_log (f, "Illegal face strike-through", value, Qnil);
82641697
GM
2927 }
2928 else if (EQ (keyword, QCbox))
2929 {
2930 if (EQ (value, Qt))
2931 value = make_number (1);
2932 if (INTEGERP (value)
2933 || STRINGP (value)
2934 || CONSP (value)
2935 || NILP (value))
2936 to[LFACE_BOX_INDEX] = value;
2937 else
7dbdfcf8 2938 add_to_log (f, "Illegal face box", value, Qnil);
82641697
GM
2939 }
2940 else if (EQ (keyword, QCinverse_video)
2941 || EQ (keyword, QCreverse_video))
2942 {
2943 if (EQ (value, Qt) || NILP (value))
2944 to[LFACE_INVERSE_INDEX] = value;
2945 else
7dbdfcf8 2946 add_to_log (f, "Illegal face inverse-video", value, Qnil);
82641697
GM
2947 }
2948 else if (EQ (keyword, QCforeground))
2949 {
2950 if (STRINGP (value))
2951 to[LFACE_FOREGROUND_INDEX] = value;
2952 else
7dbdfcf8 2953 add_to_log (f, "Illegal face foreground", value, Qnil);
82641697
GM
2954 }
2955 else if (EQ (keyword, QCbackground))
2956 {
2957 if (STRINGP (value))
2958 to[LFACE_BACKGROUND_INDEX] = value;
2959 else
7dbdfcf8 2960 add_to_log (f, "Illegal face background", value, Qnil);
82641697
GM
2961 }
2962 else if (EQ (keyword, QCstipple))
2963 {
2964#ifdef HAVE_X_WINDOWS
2965 Lisp_Object pixmap_p = Fpixmap_spec_p (value);
2966 if (!NILP (pixmap_p))
2967 to[LFACE_STIPPLE_INDEX] = value;
2968 else
7dbdfcf8 2969 add_to_log (f, "Illegal face stipple", value, Qnil);
82641697
GM
2970#endif
2971 }
2972 else if (EQ (keyword, QCwidth))
2973 {
2974 if (SYMBOLP (value)
2975 && face_numeric_swidth (value) >= 0)
2976 to[LFACE_SWIDTH_INDEX] = value;
2977 else
7dbdfcf8 2978 add_to_log (f, "Illegal face width", value, Qnil);
82641697
GM
2979 }
2980 else
7dbdfcf8
GM
2981 add_to_log (f, "Invalid attribute %s in face property",
2982 keyword, Qnil);
82641697
GM
2983
2984 prop = XCDR (XCDR (prop));
2985 }
2986 }
2987 else
2988 {
2989 /* This is a list of face specs. Specifications at the
2990 beginning of the list take precedence over later
2991 specifications, so we have to merge starting with the
2992 last specification. */
2993 Lisp_Object next = XCDR (prop);
2994 if (!NILP (next))
2995 merge_face_vector_with_property (f, to, next);
2996 merge_face_vector_with_property (f, to, first);
2997 }
2998 }
2999 else
3000 {
3001 /* PROP ought to be a face name. */
3002 Lisp_Object lface = lface_from_face_name (f, prop, 0);
3003 if (NILP (lface))
7dbdfcf8 3004 add_to_log (f, "Invalid face text property value: %s", prop, Qnil);
82641697
GM
3005 else
3006 merge_face_vectors (XVECTOR (lface)->contents, to);
3007 }
3008}
3009
3010
3011DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
3012 Sinternal_make_lisp_face, 1, 2, 0,
3013 "Make FACE, a symbol, a Lisp face with all attributes nil.\n\
3014If FACE was not known as a face before, create a new one.\n\
3015If optional argument FRAME is specified, make a frame-local face\n\
3016for that frame. Otherwise operate on the global face definition.\n\
3017Value is a vector of face attributes.")
3018 (face, frame)
3019 Lisp_Object face, frame;
3020{
3021 Lisp_Object global_lface, lface;
3022 struct frame *f;
3023 int i;
3024
3025 CHECK_SYMBOL (face, 0);
3026 global_lface = lface_from_face_name (NULL, face, 0);
3027
3028 if (!NILP (frame))
3029 {
3030 CHECK_LIVE_FRAME (frame, 1);
3031 f = XFRAME (frame);
3032 lface = lface_from_face_name (f, face, 0);
3033 }
3034 else
3035 f = NULL, lface = Qnil;
3036
3037 /* Add a global definition if there is none. */
3038 if (NILP (global_lface))
3039 {
3040 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3041 Qunspecified);
3042 XVECTOR (global_lface)->contents[0] = Qface;
3043 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
3044 Vface_new_frame_defaults);
3045
3046 /* Assign the new Lisp face a unique ID. The mapping from Lisp
3047 face id to Lisp face is given by the vector lface_id_to_name.
3048 The mapping from Lisp face to Lisp face id is given by the
3049 property `face' of the Lisp face name. */
3050 if (next_lface_id == lface_id_to_name_size)
3051 {
3052 int new_size = max (50, 2 * lface_id_to_name_size);
3053 int sz = new_size * sizeof *lface_id_to_name;
3054 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
3055 lface_id_to_name_size = new_size;
3056 }
3057
3058 lface_id_to_name[next_lface_id] = face;
3059 Fput (face, Qface, make_number (next_lface_id));
3060 ++next_lface_id;
3061 }
3062 else if (f == NULL)
3063 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3064 XVECTOR (global_lface)->contents[i] = Qunspecified;
3065
3066 /* Add a frame-local definition. */
3067 if (f)
3068 {
3069 if (NILP (lface))
3070 {
3071 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3072 Qunspecified);
3073 XVECTOR (lface)->contents[0] = Qface;
3074 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
3075 }
3076 else
3077 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3078 XVECTOR (lface)->contents[i] = Qunspecified;
3079 }
3080 else
3081 lface = global_lface;
3082
3083 xassert (LFACEP (lface));
3084 check_lface (lface);
3085 return lface;
3086}
3087
3088
3089DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
3090 Sinternal_lisp_face_p, 1, 2, 0,
3091 "Return non-nil if FACE names a face.\n\
3092If optional second parameter FRAME is non-nil, check for the\n\
3093existence of a frame-local face with name FACE on that frame.\n\
3094Otherwise check for the existence of a global face.")
3095 (face, frame)
3096 Lisp_Object face, frame;
3097{
3098 Lisp_Object lface;
3099
3100 if (!NILP (frame))
3101 {
3102 CHECK_LIVE_FRAME (frame, 1);
3103 lface = lface_from_face_name (XFRAME (frame), face, 0);
3104 }
3105 else
3106 lface = lface_from_face_name (NULL, face, 0);
3107
3108 return lface;
3109}
3110
3111
3112DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
3113 Sinternal_copy_lisp_face, 4, 4, 0,
3114 "Copy face FROM to TO.\n\
3115If FRAME it t, copy the global face definition of FROM to the\n\
3116global face definition of TO. Otherwise, copy the frame-local\n\
3117definition of FROM on FRAME to the frame-local definition of TO\n\
3118on NEW-FRAME, or FRAME if NEW-FRAME is nil.\n\
3119\n\
3120Value is TO.")
3121 (from, to, frame, new_frame)
3122 Lisp_Object from, to, frame, new_frame;
3123{
3124 Lisp_Object lface, copy;
3125
3126 CHECK_SYMBOL (from, 0);
3127 CHECK_SYMBOL (to, 1);
3128 if (NILP (new_frame))
3129 new_frame = frame;
3130
3131 if (EQ (frame, Qt))
3132 {
3133 /* Copy global definition of FROM. We don't make copies of
3134 strings etc. because 20.2 didn't do it either. */
3135 lface = lface_from_face_name (NULL, from, 1);
3136 copy = Finternal_make_lisp_face (to, Qnil);
3137 }
3138 else
3139 {
3140 /* Copy frame-local definition of FROM. */
3141 CHECK_LIVE_FRAME (frame, 2);
3142 CHECK_LIVE_FRAME (new_frame, 3);
3143 lface = lface_from_face_name (XFRAME (frame), from, 1);
3144 copy = Finternal_make_lisp_face (to, new_frame);
3145 }
3146
3147 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3148 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
3149
3150 return to;
3151}
3152
3153
3154DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3155 Sinternal_set_lisp_face_attribute, 3, 4, 0,
3156 "Set attribute ATTR of FACE to VALUE.\n\
3157If optional argument FRAME is given, set the face attribute of face FACE\n\
3158on that frame. If FRAME is t, set the attribute of the default for face\n\
3159FACE (for new frames). If FRAME is omitted or nil, use the selected\n\
3160frame.")
3161 (face, attr, value, frame)
3162 Lisp_Object face, attr, value, frame;
3163{
3164 Lisp_Object lface;
3165 Lisp_Object old_value = Qnil;
3166 int font_related_attr_p = 0;
3167
3168 CHECK_SYMBOL (face, 0);
3169 CHECK_SYMBOL (attr, 1);
3170
3171 /* Set lface to the Lisp attribute vector of FACE. */
3172 if (EQ (frame, Qt))
3173 lface = lface_from_face_name (NULL, face, 1);
3174 else
3175 {
3176 if (NILP (frame))
3177 XSETFRAME (frame, selected_frame);
3178
3179 CHECK_LIVE_FRAME (frame, 3);
3180 lface = lface_from_face_name (XFRAME (frame), face, 0);
3181
3182 /* If a frame-local face doesn't exist yet, create one. */
3183 if (NILP (lface))
3184 lface = Finternal_make_lisp_face (face, frame);
3185 }
3186
3187 if (EQ (attr, QCfamily))
3188 {
3189 if (!UNSPECIFIEDP (value))
3190 {
3191 CHECK_STRING (value, 3);
3192 if (XSTRING (value)->size == 0)
3193 signal_error ("Invalid face family", value);
3194 }
3195 old_value = LFACE_FAMILY (lface);
3196 LFACE_FAMILY (lface) = value;
3197 font_related_attr_p = 1;
3198 }
3199 else if (EQ (attr, QCheight))
3200 {
3201 if (!UNSPECIFIEDP (value))
3202 {
3203 CHECK_NUMBER (value, 3);
3204 if (XINT (value) <= 0)
3205 signal_error ("Invalid face height", value);
3206 }
3207 old_value = LFACE_HEIGHT (lface);
3208 LFACE_HEIGHT (lface) = value;
3209 font_related_attr_p = 1;
3210 }
3211 else if (EQ (attr, QCweight))
3212 {
3213 if (!UNSPECIFIEDP (value))
3214 {
3215 CHECK_SYMBOL (value, 3);
3216 if (face_numeric_weight (value) < 0)
3217 signal_error ("Invalid face weight", value);
3218 }
3219 old_value = LFACE_WEIGHT (lface);
3220 LFACE_WEIGHT (lface) = value;
3221 font_related_attr_p = 1;
3222 }
3223 else if (EQ (attr, QCslant))
3224 {
3225 if (!UNSPECIFIEDP (value))
3226 {
3227 CHECK_SYMBOL (value, 3);
3228 if (face_numeric_slant (value) < 0)
3229 signal_error ("Invalid face slant", value);
3230 }
3231 old_value = LFACE_SLANT (lface);
3232 LFACE_SLANT (lface) = value;
3233 font_related_attr_p = 1;
3234 }
3235 else if (EQ (attr, QCunderline))
3236 {
3237 if (!UNSPECIFIEDP (value))
3238 if ((SYMBOLP (value)
3239 && !EQ (value, Qt)
3240 && !EQ (value, Qnil))
3241 /* Underline color. */
3242 || (STRINGP (value)
3243 && XSTRING (value)->size == 0))
3244 signal_error ("Invalid face underline", value);
3245
3246 old_value = LFACE_UNDERLINE (lface);
3247 LFACE_UNDERLINE (lface) = value;
3248 }
3249 else if (EQ (attr, QCoverline))
3250 {
3251 if (!UNSPECIFIEDP (value))
3252 if ((SYMBOLP (value)
3253 && !EQ (value, Qt)
3254 && !EQ (value, Qnil))
3255 /* Overline color. */
3256 || (STRINGP (value)
3257 && XSTRING (value)->size == 0))
3258 signal_error ("Invalid face overline", value);
3259
3260 old_value = LFACE_OVERLINE (lface);
3261 LFACE_OVERLINE (lface) = value;
3262 }
3263 else if (EQ (attr, QCstrike_through))
3264 {
3265 if (!UNSPECIFIEDP (value))
3266 if ((SYMBOLP (value)
3267 && !EQ (value, Qt)
3268 && !EQ (value, Qnil))
3269 /* Strike-through color. */
3270 || (STRINGP (value)
3271 && XSTRING (value)->size == 0))
3272 signal_error ("Invalid face strike-through", value);
3273
3274 old_value = LFACE_STRIKE_THROUGH (lface);
3275 LFACE_STRIKE_THROUGH (lface) = value;
3276 }
3277 else if (EQ (attr, QCbox))
3278 {
3279 int valid_p;
3280
3281 /* Allow t meaning a simple box of width 1 in foreground color
3282 of the face. */
3283 if (EQ (value, Qt))
3284 value = make_number (1);
3285
3286 if (UNSPECIFIEDP (value))
3287 valid_p = 1;
3288 else if (NILP (value))
3289 valid_p = 1;
3290 else if (INTEGERP (value))
3291 valid_p = XINT (value) > 0;
3292 else if (STRINGP (value))
3293 valid_p = XSTRING (value)->size > 0;
3294 else if (CONSP (value))
3295 {
3296 Lisp_Object tem;
3297
3298 tem = value;
3299 while (CONSP (tem))
3300 {
3301 Lisp_Object k, v;
3302
3303 k = XCAR (tem);
3304 tem = XCDR (tem);
3305 if (!CONSP (tem))
3306 break;
3307 v = XCAR (tem);
3308 tem = XCDR (tem);
3309
3310 if (EQ (k, QCline_width))
3311 {
3312 if (!INTEGERP (v) || XINT (v) <= 0)
3313 break;
3314 }
3315 else if (EQ (k, QCcolor))
3316 {
3317 if (!STRINGP (v) || XSTRING (v)->size == 0)
3318 break;
3319 }
3320 else if (EQ (k, QCstyle))
3321 {
3322 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3323 break;
3324 }
3325 else
3326 break;
3327 }
3328
3329 valid_p = NILP (tem);
3330 }
3331 else
3332 valid_p = 0;
3333
3334 if (!valid_p)
3335 signal_error ("Invalid face box", value);
3336
3337 old_value = LFACE_BOX (lface);
3338 LFACE_BOX (lface) = value;
3339 }
3340 else if (EQ (attr, QCinverse_video)
3341 || EQ (attr, QCreverse_video))
3342 {
3343 if (!UNSPECIFIEDP (value))
3344 {
3345 CHECK_SYMBOL (value, 3);
3346 if (!EQ (value, Qt) && !NILP (value))
3347 signal_error ("Invalid inverse-video face attribute value", value);
3348 }
3349 old_value = LFACE_INVERSE (lface);
3350 LFACE_INVERSE (lface) = value;
3351 }
3352 else if (EQ (attr, QCforeground))
3353 {
3354 if (!UNSPECIFIEDP (value))
3355 {
3356 /* Don't check for valid color names here because it depends
3357 on the frame (display) whether the color will be valid
3358 when the face is realized. */
3359 CHECK_STRING (value, 3);
3360 if (XSTRING (value)->size == 0)
3361 signal_error ("Empty foreground color value", value);
3362 }
3363 old_value = LFACE_FOREGROUND (lface);
3364 LFACE_FOREGROUND (lface) = value;
3365 }
3366 else if (EQ (attr, QCbackground))
3367 {
3368 if (!UNSPECIFIEDP (value))
3369 {
3370 /* Don't check for valid color names here because it depends
3371 on the frame (display) whether the color will be valid
3372 when the face is realized. */
3373 CHECK_STRING (value, 3);
3374 if (XSTRING (value)->size == 0)
3375 signal_error ("Empty background color value", value);
3376 }
3377 old_value = LFACE_BACKGROUND (lface);
3378 LFACE_BACKGROUND (lface) = value;
3379 }
3380 else if (EQ (attr, QCstipple))
3381 {
3382#ifdef HAVE_X_WINDOWS
3383 if (!UNSPECIFIEDP (value)
3384 && !NILP (value)
3385 && NILP (Fpixmap_spec_p (value)))
3386 signal_error ("Invalid stipple attribute", value);
3387 old_value = LFACE_STIPPLE (lface);
3388 LFACE_STIPPLE (lface) = value;
3389#endif /* HAVE_X_WINDOWS */
3390 }
3391 else if (EQ (attr, QCwidth))
3392 {
3393 if (!UNSPECIFIEDP (value))
3394 {
3395 CHECK_SYMBOL (value, 3);
3396 if (face_numeric_swidth (value) < 0)
3397 signal_error ("Invalid face width", value);
3398 }
3399 old_value = LFACE_SWIDTH (lface);
3400 LFACE_SWIDTH (lface) = value;
3401 font_related_attr_p = 1;
3402 }
3403 else if (EQ (attr, QCfont))
3404 {
3405#ifdef HAVE_X_WINDOWS
3406 /* Set font-related attributes of the Lisp face from an
3407 XLFD font name. */
3408 struct frame *f;
3409
3410 CHECK_STRING (value, 3);
3411 if (EQ (frame, Qt))
3412 f = selected_frame;
3413 else
3414 f = check_x_frame (frame);
3415
3416 if (!set_lface_from_font_name (f, lface, XSTRING (value)->data, 1))
3417 signal_error ("Invalid font name", value);
3418
3419 font_related_attr_p = 1;
3420#endif /* HAVE_X_WINDOWS */
3421 }
3422 else if (EQ (attr, QCbold))
3423 {
3424 old_value = LFACE_WEIGHT (lface);
3425 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
3426 font_related_attr_p = 1;
3427 }
3428 else if (EQ (attr, QCitalic))
3429 {
3430 old_value = LFACE_SLANT (lface);
3431 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
3432 font_related_attr_p = 1;
3433 }
3434 else
3435 signal_error ("Invalid face attribute name", attr);
3436
3437 /* Changing a named face means that all realized faces depending on
3438 that face are invalid. Since we cannot tell which realized faces
3439 depend on the face, make sure they are all removed. This is done
3440 by incrementing face_change_count. The next call to
3441 init_iterator will then free realized faces. */
3442 if (!EQ (frame, Qt)
3443 && (EQ (attr, QCfont)
3444 || NILP (Fequal (old_value, value))))
3445 {
3446 ++face_change_count;
3447 ++windows_or_buffers_changed;
3448 }
3449
3450#ifdef HAVE_X_WINDOWS
8bd201d6
GM
3451
3452 if (!EQ (frame, Qt)
3453 && !UNSPECIFIEDP (value)
82641697 3454 && NILP (Fequal (old_value, value)))
8bd201d6
GM
3455 {
3456 Lisp_Object param;
3457
3458 param = Qnil;
3459
3460 if (EQ (face, Qdefault))
3461 {
3462 /* Changed font-related attributes of the `default' face are
3463 reflected in changed `font' frame parameters. */
3464 if (font_related_attr_p
3465 && lface_fully_specified_p (XVECTOR (lface)->contents))
3466 set_font_frame_param (frame, lface);
3467 else if (EQ (attr, QCforeground))
3468 param = Qforeground_color;
3469 else if (EQ (attr, QCbackground))
3470 param = Qbackground_color;
3471 }
3472 else if (EQ (face, Qscroll_bar))
3473 {
3474 /* Changing the colors of `scroll-bar' sets frame parameters
3475 `scroll-bar-foreground' and `scroll-bar-background'. */
3476 if (EQ (attr, QCforeground))
3477 param = Qscroll_bar_foreground;
3478 else if (EQ (attr, QCbackground))
3479 param = Qscroll_bar_background;
3480 }
3481 else if (EQ (face, Qborder))
3482 {
3483 /* Changing background color of `border' sets frame parameter
3484 `border-color'. */
3485 if (EQ (attr, QCbackground))
3486 param = Qborder_color;
3487 }
3488 else if (EQ (face, Qcursor))
3489 {
3490 /* Changing background color of `cursor' sets frame parameter
3491 `cursor-color'. */
3492 if (EQ (attr, QCbackground))
3493 param = Qcursor_color;
3494 }
3495 else if (EQ (face, Qmouse))
3496 {
3497 /* Changing background color of `mouse' sets frame parameter
3498 `mouse-color'. */
3499 if (EQ (attr, QCbackground))
3500 param = Qmouse_color;
3501 }
3502
3503 if (SYMBOLP (param))
3504 Fmodify_frame_parameters (frame, Fcons (Fcons (param, value), Qnil));
3505 }
82641697
GM
3506
3507#endif /* HAVE_X_WINDOWS */
3508
3509 return face;
3510}
3511
3512
3513#ifdef HAVE_X_WINDOWS
3514
3515/* Set the `font' frame parameter of FRAME according to `default' face
3516 attributes LFACE. */
3517
3518static void
3519set_font_frame_param (frame, lface)
3520 Lisp_Object frame, lface;
3521{
3522 struct frame *f = XFRAME (frame);
3523 Lisp_Object frame_font;
3524 int fontset;
3525 char *font;
3526
3527 /* Get FRAME's font parameter. */
3528 frame_font = Fassq (Qfont, f->param_alist);
3529 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
3530 frame_font = XCDR (frame_font);
3531
3532 fontset = fs_query_fontset (f, XSTRING (frame_font)->data);
3533 if (fontset >= 0)
3534 {
3535 /* Frame parameter is a fontset name. Modify the fontset so
3536 that all its fonts reflect face attributes LFACE. */
3537 int charset;
3538 struct fontset_info *fontset_info;
3539
3540 fontset_info = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
3541
3542 for (charset = 0; charset < MAX_CHARSET; ++charset)
3543 if (fontset_info->fontname[charset])
3544 {
3545 font = choose_face_fontset_font (f, XVECTOR (lface)->contents,
3546 fontset, charset);
3547 Fset_fontset_font (frame_font, CHARSET_SYMBOL (charset),
3548 build_string (font), frame);
3549 xfree (font);
3550 }
3551 }
3552 else
3553 {
3554 /* Frame parameter is an X font name. I believe this can
3555 only happen in unibyte mode. */
3556 font = choose_face_font (f, XVECTOR (lface)->contents,
3557 -1, Vface_default_registry);
3558 if (font)
3559 {
3560 store_frame_param (f, Qfont, build_string (font));
3561 xfree (font);
3562 }
3563 }
3564}
3565
3566
8bd201d6
GM
3567/* Update the corresponding face when frame parameter PARAM on frame F
3568 has been assigned the value NEW_VALUE. */
3569
3570void
3571update_face_from_frame_parameter (f, param, new_value)
3572 struct frame *f;
3573 Lisp_Object param, new_value;
3574{
3575 Lisp_Object lface;
3576
3577 /* If there are no faces yet, give up. This is the case when called
3578 from Fx_create_frame, and we do the necessary things later in
92610620 3579 face-set-after-frame-defaults. */
8bd201d6
GM
3580 if (NILP (f->face_alist))
3581 return;
3582
3583 if (EQ (param, Qforeground_color))
3584 {
3585 lface = lface_from_face_name (f, Qdefault, 1);
3586 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
3587 ? new_value : Qunspecified);
3588 realize_basic_faces (f);
3589 }
3590 else if (EQ (param, Qbackground_color))
3591 {
92610620
GM
3592 Lisp_Object frame;
3593
3594 /* Changing the background color might change the background
3595 mode, so that we have to load new defface specs. Call
3596 frame-update-face-colors to do that. */
3597 XSETFRAME (frame, f);
3598 call1 (Qframe_update_face_colors, frame);
3599
8bd201d6
GM
3600 lface = lface_from_face_name (f, Qdefault, 1);
3601 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3602 ? new_value : Qunspecified);
3603 realize_basic_faces (f);
3604 }
3605 if (EQ (param, Qborder_color))
3606 {
3607 lface = lface_from_face_name (f, Qborder, 1);
3608 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3609 ? new_value : Qunspecified);
3610 }
3611 else if (EQ (param, Qcursor_color))
3612 {
3613 lface = lface_from_face_name (f, Qcursor, 1);
3614 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3615 ? new_value : Qunspecified);
3616 }
3617 else if (EQ (param, Qmouse_color))
3618 {
3619 lface = lface_from_face_name (f, Qmouse, 1);
3620 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3621 ? new_value : Qunspecified);
3622 }
3623}
3624
3625
82641697
GM
3626/* Get the value of X resource RESOURCE, class CLASS for the display
3627 of frame FRAME. This is here because ordinary `x-get-resource'
3628 doesn't take a frame argument. */
3629
3630DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3631 Sinternal_face_x_get_resource, 3, 3, 0, "")
3632 (resource, class, frame)
3633 Lisp_Object resource, class, frame;
3634{
3635 Lisp_Object value;
3636 CHECK_STRING (resource, 0);
3637 CHECK_STRING (class, 1);
3638 CHECK_LIVE_FRAME (frame, 2);
3639 BLOCK_INPUT;
3640 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3641 resource, class, Qnil, Qnil);
3642 UNBLOCK_INPUT;
3643 return value;
3644}
3645
3646
3647/* Return resource string VALUE as a boolean value, i.e. nil, or t.
3648 If VALUE is "on" or "true", return t. If VALUE is "off" or
3649 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3650 error; if SIGNAL_P is zero, return 0. */
3651
3652static Lisp_Object
3653face_boolean_x_resource_value (value, signal_p)
3654 Lisp_Object value;
3655 int signal_p;
3656{
3657 Lisp_Object result = make_number (0);
3658
3659 xassert (STRINGP (value));
3660
3661 if (xstricmp (XSTRING (value)->data, "on") == 0
3662 || xstricmp (XSTRING (value)->data, "true") == 0)
3663 result = Qt;
3664 else if (xstricmp (XSTRING (value)->data, "off") == 0
3665 || xstricmp (XSTRING (value)->data, "false") == 0)
3666 result = Qnil;
3667 else if (xstricmp (XSTRING (value)->data, "unspecified") == 0)
3668 result = Qunspecified;
3669 else if (signal_p)
3670 signal_error ("Invalid face attribute value from X resource", value);
3671
3672 return result;
3673}
3674
3675
3676DEFUN ("internal-set-lisp-face-attribute-from-resource",
3677 Finternal_set_lisp_face_attribute_from_resource,
3678 Sinternal_set_lisp_face_attribute_from_resource,
3679 3, 4, 0, "")
3680 (face, attr, value, frame)
3681 Lisp_Object face, attr, value, frame;
3682{
3683 CHECK_SYMBOL (face, 0);
3684 CHECK_SYMBOL (attr, 1);
3685 CHECK_STRING (value, 2);
3686
3687 if (xstricmp (XSTRING (value)->data, "unspecified") == 0)
3688 value = Qunspecified;
3689 else if (EQ (attr, QCheight))
3690 {
3691 value = Fstring_to_number (value, make_number (10));
3692 if (XINT (value) <= 0)
3693 signal_error ("Invalid face height from X resource", value);
3694 }
3695 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3696 value = face_boolean_x_resource_value (value, 1);
3697 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3698 value = intern (XSTRING (value)->data);
3699 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3700 value = face_boolean_x_resource_value (value, 1);
3701 else if (EQ (attr, QCunderline)
3702 || EQ (attr, QCoverline)
3703 || EQ (attr, QCstrike_through)
3704 || EQ (attr, QCbox))
3705 {
3706 Lisp_Object boolean_value;
3707
3708 /* If the result of face_boolean_x_resource_value is t or nil,
3709 VALUE does NOT specify a color. */
3710 boolean_value = face_boolean_x_resource_value (value, 0);
3711 if (SYMBOLP (boolean_value))
3712 value = boolean_value;
3713 }
3714
3715 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3716}
3717
3718
3719#endif /* HAVE_X_WINDOWS */
3720
3721
3722
3723DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3724 Sinternal_get_lisp_face_attribute,
3725 2, 3, 0,
3726 "Return face attribute KEYWORD of face SYMBOL.\n\
3727If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid\n\
3728face attribute name, signal an error.\n\
3729If the optional argument FRAME is given, report on face FACE in that\n\
3730frame. If FRAME is t, report on the defaults for face FACE (for new\n\
3731frames). If FRAME is omitted or nil, use the selected frame.")
3732 (symbol, keyword, frame)
3733 Lisp_Object symbol, keyword, frame;
3734{
3735 Lisp_Object lface, value = Qnil;
3736
3737 CHECK_SYMBOL (symbol, 0);
3738 CHECK_SYMBOL (keyword, 1);
3739
3740 if (EQ (frame, Qt))
3741 lface = lface_from_face_name (NULL, symbol, 1);
3742 else
3743 {
3744 if (NILP (frame))
3745 XSETFRAME (frame, selected_frame);
3746 CHECK_LIVE_FRAME (frame, 2);
3747 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3748 }
3749
3750 if (EQ (keyword, QCfamily))
3751 value = LFACE_FAMILY (lface);
3752 else if (EQ (keyword, QCheight))
3753 value = LFACE_HEIGHT (lface);
3754 else if (EQ (keyword, QCweight))
3755 value = LFACE_WEIGHT (lface);
3756 else if (EQ (keyword, QCslant))
3757 value = LFACE_SLANT (lface);
3758 else if (EQ (keyword, QCunderline))
3759 value = LFACE_UNDERLINE (lface);
3760 else if (EQ (keyword, QCoverline))
3761 value = LFACE_OVERLINE (lface);
3762 else if (EQ (keyword, QCstrike_through))
3763 value = LFACE_STRIKE_THROUGH (lface);
3764 else if (EQ (keyword, QCbox))
3765 value = LFACE_BOX (lface);
3766 else if (EQ (keyword, QCinverse_video)
3767 || EQ (keyword, QCreverse_video))
3768 value = LFACE_INVERSE (lface);
3769 else if (EQ (keyword, QCforeground))
3770 value = LFACE_FOREGROUND (lface);
3771 else if (EQ (keyword, QCbackground))
3772 value = LFACE_BACKGROUND (lface);
3773 else if (EQ (keyword, QCstipple))
3774 value = LFACE_STIPPLE (lface);
3775 else if (EQ (keyword, QCwidth))
3776 value = LFACE_SWIDTH (lface);
3777 else
3778 signal_error ("Invalid face attribute name", keyword);
3779
3780 return value;
3781}
3782
3783
3784DEFUN ("internal-lisp-face-attribute-values",
3785 Finternal_lisp_face_attribute_values,
3786 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3787 "Return a list of valid discrete values for face attribute ATTR.\n\
3788Value is nil if ATTR doesn't have a discrete set of valid values.")
3789 (attr)
3790 Lisp_Object attr;
3791{
3792 Lisp_Object result = Qnil;
3793
3794 CHECK_SYMBOL (attr, 0);
3795
3796 if (EQ (attr, QCweight)
3797 || EQ (attr, QCslant)
3798 || EQ (attr, QCwidth))
3799 {
3800 /* Extract permissible symbols from tables. */
3801 struct table_entry *table;
3802 int i, dim;
3803
3804 if (EQ (attr, QCweight))
3805 table = weight_table, dim = DIM (weight_table);
3806 else if (EQ (attr, QCslant))
3807 table = slant_table, dim = DIM (slant_table);
3808 else
3809 table = swidth_table, dim = DIM (swidth_table);
3810
3811 for (i = 0; i < dim; ++i)
3812 {
3813 Lisp_Object symbol = *table[i].symbol;
3814 Lisp_Object tail = result;
3815
3816 while (!NILP (tail)
3817 && !EQ (XCAR (tail), symbol))
3818 tail = XCDR (tail);
3819
3820 if (NILP (tail))
3821 result = Fcons (symbol, result);
3822 }
3823 }
3824 else if (EQ (attr, QCunderline))
3825 result = Fcons (Qt, Fcons (Qnil, Qnil));
3826 else if (EQ (attr, QCoverline))
3827 result = Fcons (Qt, Fcons (Qnil, Qnil));
3828 else if (EQ (attr, QCstrike_through))
3829 result = Fcons (Qt, Fcons (Qnil, Qnil));
3830 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3831 result = Fcons (Qt, Fcons (Qnil, Qnil));
3832
3833 return result;
3834}
3835
3836
3837DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3838 Sinternal_merge_in_global_face, 2, 2, 0,
3839 "Add attributes from frame-default definition of FACE to FACE on FRAME.")
3840 (face, frame)
3841 Lisp_Object face, frame;
3842{
3843 Lisp_Object global_lface, local_lface;
3844 CHECK_LIVE_FRAME (frame, 1);
3845 global_lface = lface_from_face_name (NULL, face, 1);
3846 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
3847 if (NILP (local_lface))
3848 local_lface = Finternal_make_lisp_face (face, frame);
3849 merge_face_vectors (XVECTOR (global_lface)->contents,
3850 XVECTOR (local_lface)->contents);
3851 return face;
3852}
3853
3854
3855/* The following function is implemented for compatibility with 20.2.
3856 The function is used in x-resolve-fonts when it is asked to
3857 return fonts with the same size as the font of a face. This is
3858 done in fontset.el. */
3859
3860DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
3861 "Return the font name of face FACE, or nil if it is unspecified.\n\
3862If the optional argument FRAME is given, report on face FACE in that frame.\n\
3863If FRAME is t, report on the defaults for face FACE (for new frames).\n\
3864 The font default for a face is either nil, or a list\n\
3865 of the form (bold), (italic) or (bold italic).\n\
3866If FRAME is omitted or nil, use the selected frame.")
3867 (face, frame)
3868 Lisp_Object face, frame;
3869{
3870 if (EQ (frame, Qt))
3871 {
3872 Lisp_Object result = Qnil;
3873 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3874
3875 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3876 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3877 result = Fcons (Qbold, result);
3878
3879 if (!NILP (LFACE_SLANT (lface))
3880 && !EQ (LFACE_SLANT (lface), Qnormal))
3881 result = Fcons (Qitalic, result);
3882
3883 return result;
3884 }
3885 else
3886 {
3887 struct frame *f = frame_or_selected_frame (frame, 1);
3888 int face_id = lookup_named_face (f, face, CHARSET_ASCII);
3889 struct face *face = FACE_FROM_ID (f, face_id);
3890 return build_string (face->font_name);
3891 }
3892}
3893
3894
3895/* Compare face vectors V1 and V2 for equality. Value is non-zero if
3896 all attributes are `equal'. Tries to be fast because this function
3897 is called quite often. */
3898
3899static INLINE int
3900lface_equal_p (v1, v2)
3901 Lisp_Object *v1, *v2;
3902{
3903 int i, equal_p = 1;
3904
3905 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3906 {
3907 Lisp_Object a = v1[i];
3908 Lisp_Object b = v2[i];
3909
3910 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3911 and the other is specified. */
3912 equal_p = XTYPE (a) == XTYPE (b);
3913 if (!equal_p)
3914 break;
3915
3916 if (!EQ (a, b))
3917 {
3918 switch (XTYPE (a))
3919 {
3920 case Lisp_String:
3921 equal_p = (XSTRING (a)->size == XSTRING (b)->size
3922 && bcmp (XSTRING (a)->data, XSTRING (b)->data,
3923 XSTRING (a)->size) == 0);
3924 break;
3925
3926 case Lisp_Int:
3927 case Lisp_Symbol:
3928 equal_p = 0;
3929 break;
3930
3931 default:
3932 equal_p = !NILP (Fequal (a, b));
3933 break;
3934 }
3935 }
3936 }
3937
3938 return equal_p;
3939}
3940
3941
3942DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3943 Sinternal_lisp_face_equal_p, 2, 3, 0,
3944 "True if FACE1 and FACE2 are equal.\n\
3945If the optional argument FRAME is given, report on face FACE in that frame.\n\
3946If FRAME is t, report on the defaults for face FACE (for new frames).\n\
3947If FRAME is omitted or nil, use the selected frame.")
3948 (face1, face2, frame)
3949 Lisp_Object face1, face2, frame;
3950{
3951 int equal_p;
3952 struct frame *f;
3953 Lisp_Object lface1, lface2;
3954
3955 if (EQ (frame, Qt))
3956 f = NULL;
3957 else
3958 /* Don't use check_x_frame here because this function is called
3959 before X frames exist. At that time, if FRAME is nil,
3960 selected_frame will be used which is the frame dumped with
3961 Emacs. That frame is not an X frame. */
3962 f = frame_or_selected_frame (frame, 2);
3963
3964 lface1 = lface_from_face_name (NULL, face1, 1);
3965 lface2 = lface_from_face_name (NULL, face2, 1);
3966 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3967 XVECTOR (lface2)->contents);
3968 return equal_p ? Qt : Qnil;
3969}
3970
3971
3972DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3973 Sinternal_lisp_face_empty_p, 1, 2, 0,
3974 "True if FACE has no attribute specified.\n\
3975If the optional argument FRAME is given, report on face FACE in that frame.\n\
3976If FRAME is t, report on the defaults for face FACE (for new frames).\n\
3977If FRAME is omitted or nil, use the selected frame.")
3978 (face, frame)
3979 Lisp_Object face, frame;
3980{
3981 struct frame *f;
3982 Lisp_Object lface;
3983 int i;
3984
3985 if (NILP (frame))
3986 f = selected_frame;
3987 else
3988 {
3989 CHECK_LIVE_FRAME (frame, 0);
3990 f = XFRAME (frame);
3991 }
3992
3993 if (EQ (frame, Qt))
3994 lface = lface_from_face_name (NULL, face, 1);
3995 else
3996 lface = lface_from_face_name (f, face, 1);
3997
3998 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3999 if (!UNSPECIFIEDP (XVECTOR (lface)->contents[i]))
4000 break;
4001
4002 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
4003}
4004
4005
4006DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
4007 0, 1, 0,
4008 "Return an alist of frame-local faces defined on FRAME.\n\
4009For internal use only.")
4010 (frame)
4011 Lisp_Object frame;
4012{
4013 struct frame *f = frame_or_selected_frame (frame, 0);
4014 return f->face_alist;
4015}
4016
4017
4018/* Return a hash code for Lisp string STRING with case ignored. Used
4019 below in computing a hash value for a Lisp face. */
4020
4021static INLINE unsigned
4022hash_string_case_insensitive (string)
4023 Lisp_Object string;
4024{
4025 unsigned char *s;
4026 unsigned hash = 0;
4027 xassert (STRINGP (string));
4028 for (s = XSTRING (string)->data; *s; ++s)
4029 hash = (hash << 1) ^ tolower (*s);
4030 return hash;
4031}
4032
4033
4034/* Return a hash code for face attribute vector V. */
4035
4036static INLINE unsigned
4037lface_hash (v)
4038 Lisp_Object *v;
4039{
4040 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4041 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4042 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4043 ^ (unsigned) v[LFACE_WEIGHT_INDEX]
4044 ^ (unsigned) v[LFACE_SLANT_INDEX]
4045 ^ (unsigned) v[LFACE_SWIDTH_INDEX]
4046 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
4047}
4048
4049
4050/* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4051 considering charsets/registries). They do if they specify the same
4052 family, point size, weight, width and slant. Both LFACE1 and
4053 LFACE2 must be fully-specified. */
4054
4055static INLINE int
4056lface_same_font_attributes_p (lface1, lface2)
4057 Lisp_Object *lface1, *lface2;
4058{
4059 xassert (lface_fully_specified_p (lface1)
4060 && lface_fully_specified_p (lface2));
4061 return (xstricmp (XSTRING (lface1[LFACE_FAMILY_INDEX])->data,
4062 XSTRING (lface2[LFACE_FAMILY_INDEX])->data) == 0
4063 && (XFASTINT (lface1[LFACE_HEIGHT_INDEX])
4064 == XFASTINT (lface2[LFACE_HEIGHT_INDEX]))
4065 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4066 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4067 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX]));
4068}
4069
4070
4071\f
4072/***********************************************************************
4073 Realized Faces
4074 ***********************************************************************/
4075
4076/* Allocate and return a new realized face for Lisp face attribute
4077 vector ATTR, charset CHARSET, and registry REGISTRY. */
4078
4079static struct face *
4080make_realized_face (attr, charset, registry)
4081 Lisp_Object *attr;
4082 int charset;
4083 Lisp_Object registry;
4084{
4085 struct face *face = (struct face *) xmalloc (sizeof *face);
4086 bzero (face, sizeof *face);
4087 face->charset = charset;
4088 face->registry = registry;
4089 bcopy (attr, face->lface, sizeof face->lface);
4090 return face;
4091}
4092
4093
4094/* Free realized face FACE, including its X resources. FACE may
4095 be null. */
4096
4097static void
4098free_realized_face (f, face)
4099 struct frame *f;
4100 struct face *face;
4101{
4102 if (face)
4103 {
4104#ifdef HAVE_X_WINDOWS
4105 if (FRAME_X_P (f))
4106 {
4107 if (face->gc)
4108 {
4109 x_free_gc (f, face->gc);
4110 face->gc = 0;
4111 }
4112
4113 free_face_colors (f, face);
4114 x_destroy_bitmap (f, face->stipple);
4115 }
4116#endif /* HAVE_X_WINDOWS */
4117
4118 xfree (face);
4119 }
4120}
4121
4122
4123/* Prepare face FACE for subsequent display on frame F. This
4124 allocated GCs if they haven't been allocated yet or have been freed
4125 by clearing the face cache. */
4126
4127void
4128prepare_face_for_display (f, face)
4129 struct frame *f;
4130 struct face *face;
4131{
4132#ifdef HAVE_X_WINDOWS
4133 xassert (FRAME_X_P (f));
4134
4135 if (face->gc == 0)
4136 {
4137 XGCValues xgcv;
4138 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4139
4140 xgcv.foreground = face->foreground;
4141 xgcv.background = face->background;
4142 xgcv.graphics_exposures = False;
4143
4144 /* The font of FACE may be null if we couldn't load it. */
4145 if (face->font)
4146 {
4147 xgcv.font = face->font->fid;
4148 mask |= GCFont;
4149 }
4150
4151 BLOCK_INPUT;
4152 if (face->stipple)
4153 {
be8a72f4 4154 xgcv.fill_style = FillOpaqueStippled;
82641697
GM
4155 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4156 mask |= GCFillStyle | GCStipple;
4157 }
4158
4159 face->gc = x_create_gc (f, mask, &xgcv);
4160 UNBLOCK_INPUT;
4161 }
4162#endif
4163}
4164
4165
4166/* Non-zero if FACE is suitable for displaying ISO8859-1. Used in
4167 macro FACE_SUITABLE_FOR_CHARSET_P to avoid realizing a new face for
4168 ISO8859-1 if the ASCII face suffices. */
4169
4170int
4171face_suitable_for_iso8859_1_p (face)
4172 struct face *face;
4173{
4174 int len = strlen (face->font_name);
4175 return len >= 9 && xstricmp (face->font_name + len - 9, "iso8859-1") == 0;
4176}
4177
4178
4179/* Value is non-zero if FACE is suitable for displaying characters
4180 of CHARSET. CHARSET < 0 means unibyte text. */
4181
4182INLINE int
4183face_suitable_for_charset_p (face, charset)
4184 struct face *face;
4185 int charset;
4186{
4187 int suitable_p = 0;
4188
4189 if (charset < 0)
4190 {
4191 if (EQ (face->registry, Vface_default_registry)
4192 || !NILP (Fequal (face->registry, Vface_default_registry)))
4193 suitable_p = 1;
4194 }
4195 else if (face->charset == charset)
4196 suitable_p = 1;
4197 else if (face->charset == CHARSET_ASCII
4198 && charset == charset_latin_iso8859_1)
4199 suitable_p = face_suitable_for_iso8859_1_p (face);
4200 else if (face->charset == charset_latin_iso8859_1
4201 && charset == CHARSET_ASCII)
4202 suitable_p = 1;
4203
4204 return suitable_p;
4205}
4206
4207
4208\f
4209/***********************************************************************
4210 Face Cache
4211 ***********************************************************************/
4212
4213/* Return a new face cache for frame F. */
4214
4215static struct face_cache *
4216make_face_cache (f)
4217 struct frame *f;
4218{
4219 struct face_cache *c;
4220 int size;
4221
4222 c = (struct face_cache *) xmalloc (sizeof *c);
4223 bzero (c, sizeof *c);
4224 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4225 c->buckets = (struct face **) xmalloc (size);
4226 bzero (c->buckets, size);
4227 c->size = 50;
4228 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4229 c->f = f;
4230 return c;
4231}
4232
4233
4234/* Clear out all graphics contexts for all realized faces, except for
4235 the basic faces. This should be done from time to time just to avoid
4236 keeping too many graphics contexts that are no longer needed. */
4237
4238static void
4239clear_face_gcs (c)
4240 struct face_cache *c;
4241{
4242 if (c && FRAME_X_P (c->f))
4243 {
4244#ifdef HAVE_X_WINDOWS
4245 int i;
4246 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4247 {
4248 struct face *face = c->faces_by_id[i];
4249 if (face && face->gc)
4250 {
4251 x_free_gc (c->f, face->gc);
4252 face->gc = 0;
4253 }
4254 }
4255#endif /* HAVE_X_WINDOWS */
4256 }
4257}
4258
4259
4260/* Free all realized faces in face cache C, including basic faces. C
4261 may be null. If faces are freed, make sure the frame's current
4262 matrix is marked invalid, so that a display caused by an expose
4263 event doesn't try to use faces we destroyed. */
4264
4265static void
4266free_realized_faces (c)
4267 struct face_cache *c;
4268{
4269 if (c && c->used)
4270 {
4271 int i, size;
4272 struct frame *f = c->f;
4273
4274 for (i = 0; i < c->used; ++i)
4275 {
4276 free_realized_face (f, c->faces_by_id[i]);
4277 c->faces_by_id[i] = NULL;
4278 }
4279
4280 c->used = 0;
4281 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4282 bzero (c->buckets, size);
4283
4284 /* Must do a thorough redisplay the next time. Mark current
4285 matrices as invalid because they will reference faces freed
4286 above. This function is also called when a frame is
4287 destroyed. In this case, the root window of F is nil. */
4288 if (WINDOWP (f->root_window))
4289 {
4290 clear_current_matrices (f);
4291 ++windows_or_buffers_changed;
4292 }
4293 }
4294}
4295
4296
4297/* Free all realized faces on FRAME or on all frames if FRAME is nil.
4298 This is done after attributes of a named face have been changed,
4299 because we can't tell which realized faces depend on that face. */
4300
4301void
4302free_all_realized_faces (frame)
4303 Lisp_Object frame;
4304{
4305 if (NILP (frame))
4306 {
4307 Lisp_Object rest;
4308 FOR_EACH_FRAME (rest, frame)
4309 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4310 }
4311 else
4312 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4313}
4314
4315
4316/* Free face cache C and faces in it, including their X resources. */
4317
4318static void
4319free_face_cache (c)
4320 struct face_cache *c;
4321{
4322 if (c)
4323 {
4324 free_realized_faces (c);
4325 xfree (c->buckets);
4326 xfree (c->faces_by_id);
4327 xfree (c);
4328 }
4329}
4330
4331
4332/* Cache realized face FACE in face cache C. HASH is the hash value
4333 of FACE. If FACE->fontset >= 0, add the new face to the end of the
4334 collision list of the face hash table of C. This is done because
4335 otherwise lookup_face would find FACE for every charset, even if
4336 faces with the same attributes but for specific charsets exist. */
4337
4338static void
4339cache_face (c, face, hash)
4340 struct face_cache *c;
4341 struct face *face;
4342 unsigned hash;
4343{
4344 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4345
4346 face->hash = hash;
4347
4348 if (face->fontset >= 0)
4349 {
4350 struct face *last = c->buckets[i];
4351 if (last)
4352 {
4353 while (last->next)
4354 last = last->next;
4355 last->next = face;
4356 face->prev = last;
4357 face->next = NULL;
4358 }
4359 else
4360 {
4361 c->buckets[i] = face;
4362 face->prev = face->next = NULL;
4363 }
4364 }
4365 else
4366 {
4367 face->prev = NULL;
4368 face->next = c->buckets[i];
4369 if (face->next)
4370 face->next->prev = face;
4371 c->buckets[i] = face;
4372 }
4373
4374 /* Find a free slot in C->faces_by_id and use the index of the free
4375 slot as FACE->id. */
4376 for (i = 0; i < c->used; ++i)
4377 if (c->faces_by_id[i] == NULL)
4378 break;
4379 face->id = i;
4380
4381 /* Maybe enlarge C->faces_by_id. */
4382 if (i == c->used && c->used == c->size)
4383 {
4384 int new_size = 2 * c->size;
4385 int sz = new_size * sizeof *c->faces_by_id;
4386 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4387 c->size = new_size;
4388 }
4389
4390#if GLYPH_DEBUG
4391 /* Check that FACE got a unique id. */
4392 {
4393 int j, n;
4394 struct face *face;
4395
4396 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4397 for (face = c->buckets[j]; face; face = face->next)
4398 if (face->id == i)
4399 ++n;
4400
4401 xassert (n == 1);
4402 }
4403#endif /* GLYPH_DEBUG */
4404
4405 c->faces_by_id[i] = face;
4406 if (i == c->used)
4407 ++c->used;
4408}
4409
4410
4411/* Remove face FACE from cache C. */
4412
4413static void
4414uncache_face (c, face)
4415 struct face_cache *c;
4416 struct face *face;
4417{
4418 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4419
4420 if (face->prev)
4421 face->prev->next = face->next;
4422 else
4423 c->buckets[i] = face->next;
4424
4425 if (face->next)
4426 face->next->prev = face->prev;
4427
4428 c->faces_by_id[face->id] = NULL;
4429 if (face->id == c->used)
4430 --c->used;
4431}
4432
4433
4434/* Look up a realized face with face attributes ATTR in the face cache
4435 of frame F. The face will be used to display characters of
4436 CHARSET. CHARSET < 0 means the face will be used to display
4437 unibyte text. The value of face-default-registry is used to choose
4438 a font for the face in that case. Value is the ID of the face
4439 found. If no suitable face is found, realize a new one. */
4440
4441INLINE int
4442lookup_face (f, attr, charset)
4443 struct frame *f;
4444 Lisp_Object *attr;
4445 int charset;
4446{
4447 struct face_cache *c = FRAME_FACE_CACHE (f);
4448 unsigned hash;
4449 int i;
4450 struct face *face;
4451
4452 xassert (c != NULL);
4453 check_lface_attrs (attr);
4454
4455 /* Look up ATTR in the face cache. */
4456 hash = lface_hash (attr);
4457 i = hash % FACE_CACHE_BUCKETS_SIZE;
4458
4459 for (face = c->buckets[i]; face; face = face->next)
4460 if (face->hash == hash
44747bd0 4461 && (!FRAME_WINDOW_P (f)
82641697
GM
4462 || FACE_SUITABLE_FOR_CHARSET_P (face, charset))
4463 && lface_equal_p (face->lface, attr))
4464 break;
4465
4466 /* If not found, realize a new face. */
4467 if (face == NULL)
4468 {
4469 face = realize_face (c, attr, charset);
4470 cache_face (c, face, hash);
4471 }
4472
4473#if GLYPH_DEBUG
4474 xassert (face == FACE_FROM_ID (f, face->id));
4475 if (FRAME_X_P (f))
4476 xassert (charset < 0 || FACE_SUITABLE_FOR_CHARSET_P (face, charset));
4477#endif /* GLYPH_DEBUG */
4478
4479 return face->id;
4480}
4481
4482
4483/* Return the face id of the realized face for named face SYMBOL on
4484 frame F suitable for displaying characters from CHARSET. CHARSET <
4485 0 means unibyte text. */
4486
4487int
4488lookup_named_face (f, symbol, charset)
4489 struct frame *f;
4490 Lisp_Object symbol;
4491 int charset;
4492{
4493 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4494 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4495 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4496
4497 get_lface_attributes (f, symbol, symbol_attrs, 1);
4498 bcopy (default_face->lface, attrs, sizeof attrs);
4499 merge_face_vectors (symbol_attrs, attrs);
4500 return lookup_face (f, attrs, charset);
4501}
4502
4503
4504/* Return the ID of the realized ASCII face of Lisp face with ID
4505 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
4506
4507int
4508ascii_face_of_lisp_face (f, lface_id)
4509 struct frame *f;
4510 int lface_id;
4511{
4512 int face_id;
4513
4514 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
4515 {
4516 Lisp_Object face_name = lface_id_to_name[lface_id];
4517 face_id = lookup_named_face (f, face_name, CHARSET_ASCII);
4518 }
4519 else
4520 face_id = -1;
4521
4522 return face_id;
4523}
4524
4525
4526/* Return a face for charset ASCII that is like the face with id
4527 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4528 STEPS < 0 means larger. Value is the id of the face. */
4529
4530int
4531smaller_face (f, face_id, steps)
4532 struct frame *f;
4533 int face_id, steps;
4534 {
4535#ifdef HAVE_X_WINDOWS
4536 struct face *face;
4537 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4538 int pt, last_pt, last_height;
4539 int delta;
4540 int new_face_id;
4541 struct face *new_face;
4542
4543 /* If not called for an X frame, just return the original face. */
4544 if (FRAME_TERMCAP_P (f))
4545 return face_id;
4546
4547 /* Try in increments of 1/2 pt. */
4548 delta = steps < 0 ? 5 : -5;
4549 steps = abs (steps);
4550
4551 face = FACE_FROM_ID (f, face_id);
4552 bcopy (face->lface, attrs, sizeof attrs);
4553 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4554 new_face_id = face_id;
4555 last_height = FONT_HEIGHT (face->font);
4556
4557 while (steps
4558 && pt + delta > 0
4559 /* Give up if we cannot find a font within 10pt. */
4560 && abs (last_pt - pt) < 100)
4561 {
4562 /* Look up a face for a slightly smaller/larger font. */
4563 pt += delta;
4564 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4565 new_face_id = lookup_face (f, attrs, CHARSET_ASCII);
4566 new_face = FACE_FROM_ID (f, new_face_id);
4567
4568 /* If height changes, count that as one step. */
4569 if (FONT_HEIGHT (new_face->font) != last_height)
4570 {
4571 --steps;
4572 last_height = FONT_HEIGHT (new_face->font);
4573 last_pt = pt;
4574 }
4575 }
4576
4577 return new_face_id;
4578
4579#else /* not HAVE_X_WINDOWS */
4580
4581 return face_id;
4582
4583#endif /* not HAVE_X_WINDOWS */
4584}
4585
4586
4587/* Return a face for charset ASCII that is like the face with id
4588 FACE_ID on frame F, but has height HEIGHT. */
4589
4590int
4591face_with_height (f, face_id, height)
4592 struct frame *f;
4593 int face_id;
4594 int height;
4595{
4596#ifdef HAVE_X_WINDOWS
4597 struct face *face;
4598 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4599
4600 if (FRAME_TERMCAP_P (f)
4601 || height <= 0)
4602 return face_id;
4603
4604 face = FACE_FROM_ID (f, face_id);
4605 bcopy (face->lface, attrs, sizeof attrs);
4606 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4607 face_id = lookup_face (f, attrs, CHARSET_ASCII);
4608#endif /* HAVE_X_WINDOWS */
4609
4610 return face_id;
4611}
4612
44747bd0
EZ
4613/* Return the face id of the realized face for named face SYMBOL on
4614 frame F suitable for displaying characters from CHARSET (CHARSET <
4615 0 means unibyte text), and use attributes of the face FACE_ID for
4616 attributes that aren't completely specified by SYMBOL. This is
4617 like lookup_named_face, except that the default attributes come
4618 from FACE_ID, not from the default face. FACE_ID is assumed to
4619 be already realized. */
4620
4621int
4622lookup_derived_face (f, symbol, charset, face_id)
4623 struct frame *f;
4624 Lisp_Object symbol;
4625 int charset;
4626 int face_id;
4627{
4628 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4629 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4630 struct face *default_face = FACE_FROM_ID (f, face_id);
4631
4632 if (!default_face)
4633 abort ();
4634
4635 get_lface_attributes (f, symbol, symbol_attrs, 1);
4636 bcopy (default_face->lface, attrs, sizeof attrs);
4637 merge_face_vectors (symbol_attrs, attrs);
4638 return lookup_face (f, attrs, charset);
4639}
4640
82641697
GM
4641
4642\f
4643/***********************************************************************
4644 Font selection
4645 ***********************************************************************/
4646
4647DEFUN ("internal-set-font-selection-order",
4648 Finternal_set_font_selection_order,
4649 Sinternal_set_font_selection_order, 1, 1, 0,
4650 "Set font selection order for face font selection to ORDER.\n\
4651ORDER must be a list of length 4 containing the symbols `:width',\n\
4652`:height', `:weight', and `:slant'. Face attributes appearing\n\
4653first in ORDER are matched first, e.g. if `:height' appears before\n\
4654`:weight' in ORDER, font selection first tries to find a font with\n\
4655a suitable height, and then tries to match the font weight.\n\
4656Value is ORDER.")
4657 (order)
4658 Lisp_Object order;
4659{
4660 Lisp_Object list;
4661 int i;
4662 int indices[4];
4663
4664 CHECK_LIST (order, 0);
4665 bzero (indices, sizeof indices);
4666 i = 0;
4667
4668 for (list = order;
4669 CONSP (list) && i < DIM (indices);
4670 list = XCDR (list), ++i)
4671 {
4672 Lisp_Object attr = XCAR (list);
4673 int xlfd;
4674
4675 if (EQ (attr, QCwidth))
4676 xlfd = XLFD_SWIDTH;
4677 else if (EQ (attr, QCheight))
4678 xlfd = XLFD_POINT_SIZE;
4679 else if (EQ (attr, QCweight))
4680 xlfd = XLFD_WEIGHT;
4681 else if (EQ (attr, QCslant))
4682 xlfd = XLFD_SLANT;
4683 else
4684 break;
4685
4686 if (indices[i] != 0)
4687 break;
4688 indices[i] = xlfd;
4689 }
4690
4691 if (!NILP (list)
4692 || i != DIM (indices)
4693 || indices[0] == 0
4694 || indices[1] == 0
4695 || indices[2] == 0
4696 || indices[3] == 0)
4697 signal_error ("Invalid font sort order", order);
4698
4699 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
4700 {
4701 bcopy (indices, font_sort_order, sizeof font_sort_order);
4702 free_all_realized_faces (Qnil);
4703 }
4704
4705 return Qnil;
4706}
4707
4708
4709DEFUN ("internal-set-alternative-font-family-alist",
4710 Finternal_set_alternative_font_family_alist,
4711 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
4712 "Define alternative font families to try in face font selection.\n\
4713ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.\n\
4714Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can\n\
4715be found. Value is ALIST.")
4716 (alist)
4717 Lisp_Object alist;
4718{
4719 CHECK_LIST (alist, 0);
4720 Vface_alternative_font_family_alist = alist;
4721 free_all_realized_faces (Qnil);
4722 return alist;
4723}
4724
4725
4726#ifdef HAVE_X_WINDOWS
4727
4728/* Return the X registry and encoding of font name FONT_NAME on frame F.
4729 Value is nil if not successful. */
4730
4731static Lisp_Object
4732deduce_unibyte_registry (f, font_name)
4733 struct frame *f;
4734 char *font_name;
4735{
4736 struct font_name font;
4737 Lisp_Object registry = Qnil;
4738
4739 font.name = STRDUPA (font_name);
4740 if (split_font_name (f, &font, 0))
4741 {
4742 char *buffer;
4743
4744 /* Extract registry and encoding. */
4745 buffer = (char *) alloca (strlen (font.fields[XLFD_REGISTRY])
4746 + strlen (font.fields[XLFD_ENCODING])
4747 + 10);
4748 strcpy (buffer, font.fields[XLFD_REGISTRY]);
4749 strcat (buffer, "-");
4750 strcat (buffer, font.fields[XLFD_ENCODING]);
4751 registry = build_string (buffer);
4752 }
4753
4754 return registry;
4755}
4756
4757
4758/* Value is non-zero if FONT is the name of a scalable font. The
4759 X11R6 XLFD spec says that point size, pixel size, and average width
4760 are zero for scalable fonts. Intlfonts contain at least one
4761 scalable font ("*-muleindian-1") for which this isn't true, so we
4762 just test average width. */
4763
4764static int
4765font_scalable_p (font)
4766 struct font_name *font;
4767{
4768 char *s = font->fields[XLFD_AVGWIDTH];
4769 return *s == '0' && *(s + 1) == '\0';
4770}
4771
4772
4773/* Value is non-zero if FONT1 is a better match for font attributes
4774 VALUES than FONT2. VALUES is an array of face attribute values in
4775 font sort order. COMPARE_PT_P zero means don't compare point
4776 sizes. */
4777
4778static int
4779better_font_p (values, font1, font2, compare_pt_p)
4780 int *values;
4781 struct font_name *font1, *font2;
4782 int compare_pt_p;
4783{
4784 int i;
4785
4786 for (i = 0; i < 4; ++i)
4787 {
4788 int xlfd_idx = font_sort_order[i];
4789
4790 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
4791 {
4792 int delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
4793 int delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
4794
4795 if (delta1 > delta2)
4796 return 0;
4797 else if (delta1 < delta2)
4798 return 1;
4799 else
4800 {
4801 /* The difference may be equal because, e.g., the face
4802 specifies `italic' but we have only `regular' and
4803 `oblique'. Prefer `oblique' in this case. */
4804 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
4805 && font1->numeric[xlfd_idx] > values[i]
4806 && font2->numeric[xlfd_idx] < values[i])
4807 return 1;
4808 }
4809 }
4810 }
4811
4812 return 0;
4813}
4814
4815
4816#if SCALABLE_FONTS
4817
4818/* Value is non-zero if FONT is an exact match for face attributes in
4819 SPECIFIED. SPECIFIED is an array of face attribute values in font
4820 sort order. */
4821
4822static int
4823exact_face_match_p (specified, font)
4824 int *specified;
4825 struct font_name *font;
4826{
4827 int i;
4828
4829 for (i = 0; i < 4; ++i)
4830 if (specified[i] != font->numeric[font_sort_order[i]])
4831 break;
4832
4833 return i == 4;
4834}
4835
4836
4837/* Value is the name of a scaled font, generated from scalable font
4838 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
4839 Value is allocated from heap. */
4840
4841static char *
4842build_scalable_font_name (f, font, specified_pt)
4843 struct frame *f;
4844 struct font_name *font;
4845 int specified_pt;
4846{
4847 char point_size[20], pixel_size[20];
4848 int pixel_value;
4849 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
4850 double pt;
4851
4852 /* If scalable font is for a specific resolution, compute
4853 the point size we must specify from the resolution of
4854 the display and the specified resolution of the font. */
4855 if (font->numeric[XLFD_RESY] != 0)
4856 {
4857 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
4858 pixel_value = font->numeric[XLFD_RESY] / 720.0 * pt;
4859 }
4860 else
4861 {
4862 pt = specified_pt;
4863 pixel_value = resy / 720.0 * pt;
4864 }
4865
4866 /* Set point size of the font. */
4867 sprintf (point_size, "%d", (int) pt);
4868 font->fields[XLFD_POINT_SIZE] = point_size;
4869 font->numeric[XLFD_POINT_SIZE] = pt;
4870
4871 /* Set pixel size. */
4872 sprintf (pixel_size, "%d", pixel_value);
4873 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
4874 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
4875
4876 /* If font doesn't specify its resolution, use the
4877 resolution of the display. */
4878 if (font->numeric[XLFD_RESY] == 0)
4879 {
4880 char buffer[20];
4881 sprintf (buffer, "%d", (int) resy);
4882 font->fields[XLFD_RESY] = buffer;
4883 font->numeric[XLFD_RESY] = resy;
4884 }
4885
4886 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
4887 {
4888 char buffer[20];
4889 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
4890 sprintf (buffer, "%d", resx);
4891 font->fields[XLFD_RESX] = buffer;
4892 font->numeric[XLFD_RESX] = resx;
4893 }
4894
4895 return build_font_name (font);
4896}
4897
4898
4899/* Value is non-zero if we are allowed to use scalable font FONT. We
4900 can't run a Lisp function here since this function may be called
4901 with input blocked. */
4902
4903static int
4904may_use_scalable_font_p (font, name)
4905 struct font_name *font;
4906 char *name;
4907{
4908 if (EQ (Vscalable_fonts_allowed, Qt))
4909 return 1;
4910 else if (CONSP (Vscalable_fonts_allowed))
4911 {
4912 Lisp_Object tail, regexp;
4913
4914 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
4915 {
4916 regexp = XCAR (tail);
4917 if (STRINGP (regexp)
4918 && fast_c_string_match_ignore_case (regexp, name) >= 0)
4919 return 1;
4920 }
4921 }
4922
4923 return 0;
4924}
4925
4926#endif /* SCALABLE_FONTS != 0 */
4927
4928
4929/* Return the name of the best matching font for face attributes
4930 ATTRS in the array of font_name structures FONTS which contains
4931 NFONTS elements. Value is a font name which is allocated from
4932 the heap. FONTS is freed by this function. */
4933
4934static char *
4935best_matching_font (f, attrs, fonts, nfonts)
4936 struct frame *f;
4937 Lisp_Object *attrs;
4938 struct font_name *fonts;
4939 int nfonts;
4940{
4941 char *font_name;
4942 struct font_name *best;
4943 int i, pt;
4944 int specified[4];
4945 int exact_p;
4946
4947 if (nfonts == 0)
4948 return NULL;
4949
4950 /* Make specified font attributes available in `specified',
4951 indexed by sort order. */
4952 for (i = 0; i < DIM (font_sort_order); ++i)
4953 {
4954 int xlfd_idx = font_sort_order[i];
4955
4956 if (xlfd_idx == XLFD_SWIDTH)
4957 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
4958 else if (xlfd_idx == XLFD_POINT_SIZE)
4959 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4960 else if (xlfd_idx == XLFD_WEIGHT)
4961 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
4962 else if (xlfd_idx == XLFD_SLANT)
4963 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
4964 else
4965 abort ();
4966 }
4967
4968#if SCALABLE_FONTS
4969
4970 /* Set to 1 */
4971 exact_p = 0;
4972
4973 /* Start with the first non-scalable font in the list. */
4974 for (i = 0; i < nfonts; ++i)
4975 if (!font_scalable_p (fonts + i))
4976 break;
4977
4978 /* Find the best match among the non-scalable fonts. */
4979 if (i < nfonts)
4980 {
4981 best = fonts + i;
4982
4983 for (i = 1; i < nfonts; ++i)
4984 if (!font_scalable_p (fonts + i)
4985 && better_font_p (specified, fonts + i, best, 1))
4986 {
4987 best = fonts + i;
4988
4989 exact_p = exact_face_match_p (specified, best);
4990 if (exact_p)
4991 break;
4992 }
4993
4994 }
4995 else
4996 best = NULL;
4997
4998 /* Unless we found an exact match among non-scalable fonts, see if
4999 we can find a better match among scalable fonts. */
5000 if (!exact_p)
5001 {
5002 /* A scalable font is better if
5003
5004 1. its weight, slant, swidth attributes are better, or.
5005
5006 2. the best non-scalable font doesn't have the required
5007 point size, and the scalable fonts weight, slant, swidth
5008 isn't worse. */
5009
5010 int non_scalable_has_exact_height_p;
5011
5012 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
5013 non_scalable_has_exact_height_p = 1;
5014 else
5015 non_scalable_has_exact_height_p = 0;
5016
5017 for (i = 0; i < nfonts; ++i)
5018 if (font_scalable_p (fonts + i))
5019 {
5020 if (best == NULL
5021 || better_font_p (specified, fonts + i, best, 0)
5022 || (!non_scalable_has_exact_height_p
5023 && !better_font_p (specified, best, fonts + i, 0)))
5024 best = fonts + i;
5025 }
5026 }
5027
5028 if (font_scalable_p (best))
5029 font_name = build_scalable_font_name (f, best, pt);
5030 else
5031 font_name = build_font_name (best);
5032
5033#else /* !SCALABLE_FONTS */
5034
5035 /* Find the best non-scalable font. */
5036 best = fonts;
5037
5038 for (i = 1; i < nfonts; ++i)
5039 {
5040 xassert (!font_scalable_p (fonts + i));
5041 if (better_font_p (specified, fonts + i, best, 1))
5042 best = fonts + i;
5043 }
5044
5045 font_name = build_font_name (best);
5046
5047#endif /* !SCALABLE_FONTS */
5048
5049 /* Free font_name structures. */
5050 free_font_names (fonts, nfonts);
5051
5052 return font_name;
5053}
5054
5055
5056/* Try to get a list of fonts on frame F with font family FAMILY and
5057 registry/encoding REGISTRY. Return in *FONTS a pointer to a vector
5058 of font_name structures for the fonts matched. Value is the number
5059 of fonts found. */
5060
5061static int
5062try_font_list (f, attrs, pattern, family, registry, fonts)
5063 struct frame *f;
5064 Lisp_Object *attrs;
5065 char *pattern, *family, *registry;
5066 struct font_name **fonts;
5067{
5068 int nfonts;
5069
5070 if (family == NULL)
5071 family = LSTRDUPA (attrs[LFACE_FAMILY_INDEX]);
5072
5073 nfonts = font_list (f, pattern, family, registry, fonts);
5074
5075 if (nfonts == 0)
5076 {
5077 Lisp_Object alter;
5078
5079 /* Try alternative font families from
5080 Vface_alternative_font_family_alist. */
5081 alter = Fassoc (build_string (family),
5082 Vface_alternative_font_family_alist);
5083 if (CONSP (alter))
5084 for (alter = XCDR (alter);
5085 CONSP (alter) && nfonts == 0;
5086 alter = XCDR (alter))
5087 {
5088 if (STRINGP (XCAR (alter)))
5089 {
5090 family = LSTRDUPA (XCAR (alter));
5091 nfonts = font_list (f, NULL, family, registry, fonts);
5092 }
5093 }
5094
5095 /* Try font family of the default face or "fixed". */
5096 if (nfonts == 0)
5097 {
5098 struct face *dflt = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5099 if (dflt)
5100 family = LSTRDUPA (dflt->lface[LFACE_FAMILY_INDEX]);
5101 else
5102 family = "fixed";
5103 nfonts = font_list (f, NULL, family, registry, fonts);
5104 }
5105
5106 /* Try any family with the given registry. */
5107 if (nfonts == 0)
5108 nfonts = font_list (f, NULL, "*", registry, fonts);
5109 }
5110
5111 return nfonts;
5112}
5113
5114
5115/* Return the registry and encoding pattern that fonts for CHARSET
5116 should match. Value is allocated from the heap. */
5117
5118char *
5119x_charset_registry (charset)
5120 int charset;
5121{
5122 Lisp_Object prop, charset_plist;
5123 char *registry;
5124
5125 /* Get registry and encoding from the charset's plist. */
5126 charset_plist = CHARSET_TABLE_INFO (charset, CHARSET_PLIST_IDX);
5127 prop = Fplist_get (charset_plist, Qx_charset_registry);
5128
5129 if (STRINGP (prop))
5130 {
5131 if (index (XSTRING (prop)->data, '-'))
5132 registry = xstrdup (XSTRING (prop)->data);
5133 else
5134 {
5135 /* If registry doesn't contain a `-', make it a pattern. */
5136 registry = (char *) xmalloc (STRING_BYTES (XSTRING (prop)) + 5);
5137 strcpy (registry, XSTRING (prop)->data);
5138 strcat (registry, "*-*");
5139 }
5140 }
5141 else if (STRINGP (Vface_default_registry))
5142 registry = xstrdup (XSTRING (Vface_default_registry)->data);
5143 else
5144 registry = xstrdup ("iso8859-1");
5145
5146 return registry;
5147}
5148
5149
5150/* Return the fontset id of the fontset name or alias name given by
5151 the family attribute of ATTRS on frame F. Value is -1 if the
5152 family attribute of ATTRS doesn't name a fontset. */
5153
5154static int
5155face_fontset (f, attrs)
5156 struct frame *f;
5157 Lisp_Object *attrs;
5158{
5159 Lisp_Object name = attrs[LFACE_FAMILY_INDEX];
5160 int fontset;
5161
5162 name = Fquery_fontset (name, Qnil);
5163 if (NILP (name))
5164 fontset = -1;
5165 else
5166 fontset = fs_query_fontset (f, XSTRING (name)->data);
5167
5168 return fontset;
5169}
5170
5171
5172/* Get the font to use for the face realizing the fully-specified Lisp
5173 face ATTRS for charset CHARSET on frame F. CHARSET < 0 means
5174 unibyte text; UNIBYTE_REGISTRY is the registry and encoding to use
5175 in this case. Value is the font name which is allocated from the
5176 heap (which means that it must be freed eventually). */
5177
5178static char *
5179choose_face_font (f, attrs, charset, unibyte_registry)
5180 struct frame *f;
5181 Lisp_Object *attrs;
5182 int charset;
5183 Lisp_Object unibyte_registry;
5184{
5185 struct font_name *fonts;
5186 int nfonts;
5187 char *registry;
5188
5189 /* ATTRS must be fully-specified. */
5190 xassert (lface_fully_specified_p (attrs));
5191
5192 if (STRINGP (unibyte_registry))
5193 registry = xstrdup (XSTRING (unibyte_registry)->data);
5194 else
5195 registry = x_charset_registry (charset);
5196
5197 nfonts = try_font_list (f, attrs, NULL, NULL, registry, &fonts);
5198 xfree (registry);
5199 return best_matching_font (f, attrs, fonts, nfonts);
5200}
5201
5202
5203/* Choose a font to use on frame F to display CHARSET using FONTSET
5204 with Lisp face attributes specified by ATTRS. CHARSET may be any
5205 valid charset except CHARSET_COMPOSITION. CHARSET < 0 means
5206 unibyte text. If the fontset doesn't contain a font pattern for
5207 charset, use the pattern for CHARSET_ASCII. Value is the font name
5208 which is allocated from the heap and must be freed by the caller. */
5209
5210static char *
5211choose_face_fontset_font (f, attrs, fontset, charset)
5212 struct frame *f;
5213 Lisp_Object *attrs;
5214 int fontset, charset;
5215{
5216 char *pattern;
5217 char *font_name = NULL;
5218 struct fontset_info *fontset_info;
5219 struct font_name *fonts;
5220 int nfonts;
5221
5222 xassert (charset != CHARSET_COMPOSITION);
5223 xassert (fontset >= 0 && fontset < FRAME_FONTSET_DATA (f)->n_fontsets);
5224
5225 /* For unibyte text, use the ASCII font of the fontset. Using the
5226 ASCII font seems to be the most reasonable thing we can do in
5227 this case. */
5228 if (charset < 0)
5229 charset = CHARSET_ASCII;
5230
5231 /* Get the font name pattern to use for CHARSET from the fontset. */
5232 fontset_info = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
5233 pattern = fontset_info->fontname[charset];
5234 if (!pattern)
5235 pattern = fontset_info->fontname[CHARSET_ASCII];
5236 xassert (pattern);
5237
5238 /* Get a list of fonts matching that pattern and choose the
5239 best match for the specified face attributes from it. */
5240 nfonts = try_font_list (f, attrs, pattern, NULL, NULL, &fonts);
5241 font_name = best_matching_font (f, attrs, fonts, nfonts);
5242 return font_name;
5243}
5244
5245#endif /* HAVE_X_WINDOWS */
5246
5247
5248\f
5249/***********************************************************************
5250 Face Realization
5251 ***********************************************************************/
5252
5253/* Realize basic faces on frame F. Value is zero if frame parameters
5254 of F don't contain enough information needed to realize the default
5255 face. */
5256
5257static int
5258realize_basic_faces (f)
5259 struct frame *f;
5260{
5261 int success_p = 0;
5262
5263 if (realize_default_face (f))
5264 {
92610620 5265 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
9ea173e8 5266 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
8bd201d6 5267 realize_named_face (f, Qfringe, BITMAP_AREA_FACE_ID);
045dee35 5268 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
8bd201d6
GM
5269 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5270 realize_named_face (f, Qborder, BORDER_FACE_ID);
5271 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5272 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
82641697
GM
5273 success_p = 1;
5274 }
5275
5276 return success_p;
5277}
5278
5279
5280/* Realize the default face on frame F. If the face is not fully
5281 specified, make it fully-specified. Attributes of the default face
5282 that are not explicitly specified are taken from frame parameters. */
5283
5284static int
5285realize_default_face (f)
5286 struct frame *f;
5287{
5288 struct face_cache *c = FRAME_FACE_CACHE (f);
5289 Lisp_Object lface;
5290 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5291 Lisp_Object unibyte_registry;
5292 Lisp_Object frame_font;
5293 struct face *face;
5294 int fontset;
5295
5296 /* If the `default' face is not yet known, create it. */
5297 lface = lface_from_face_name (f, Qdefault, 0);
5298 if (NILP (lface))
5299 {
5300 Lisp_Object frame;
5301 XSETFRAME (frame, f);
5302 lface = Finternal_make_lisp_face (Qdefault, frame);
5303 }
5304
5305#ifdef HAVE_X_WINDOWS
5306 if (FRAME_X_P (f))
5307 {
5308 /* Set frame_font to the value of the `font' frame parameter. */
5309 frame_font = Fassq (Qfont, f->param_alist);
5310 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
5311 frame_font = XCDR (frame_font);
5312
5313 fontset = fs_query_fontset (f, XSTRING (frame_font)->data);
5314 if (fontset >= 0)
5315 {
5316 /* If frame_font is a fontset name, don't use that for
5317 determining font-related attributes of the default face
5318 because it is just an artificial name. Use the ASCII font of
5319 the fontset, instead. */
5320 struct font_info *font_info;
5321 struct font_name font;
5322
5323 BLOCK_INPUT;
5324 font_info = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII,
5325 NULL, fontset);
5326 UNBLOCK_INPUT;
5327
5328 /* Set weight etc. from the ASCII font. */
5329 if (!set_lface_from_font_name (f, lface, font_info->full_name, 0))
5330 return 0;
5331
5332 /* Remember registry and encoding of the frame font. */
5333 unibyte_registry = deduce_unibyte_registry (f, font_info->full_name);
5334 if (STRINGP (unibyte_registry))
5335 Vface_default_registry = unibyte_registry;
5336 else
5337 Vface_default_registry = build_string ("iso8859-1");
5338
5339 /* But set the family to the fontset alias name. Implementation
5340 note: When a font is passed to Emacs via `-fn FONT', a
5341 fontset is created in `x-win.el' whose name ends in
5342 `fontset-startup'. This fontset has an alias name that is
5343 equal to frame_font. */
5344 xassert (STRINGP (frame_font));
5345 font.name = LSTRDUPA (frame_font);
5346
5347 if (!split_font_name (f, &font, 1)
5348 || xstricmp (font.fields[XLFD_REGISTRY], "fontset") != 0
5349 || xstricmp (font.fields[XLFD_ENCODING], "startup") != 0)
5350 LFACE_FAMILY (lface) = frame_font;
5351 }
5352 else
5353 {
5354 /* Frame parameters contain a real font. Fill default face
5355 attributes from that font. */
5356 if (!set_lface_from_font_name (f, lface,
5357 XSTRING (frame_font)->data, 0))
5358 return 0;
5359
5360 /* Remember registry and encoding of the frame font. */
5361 unibyte_registry
5362 = deduce_unibyte_registry (f, XSTRING (frame_font)->data);
5363 if (STRINGP (unibyte_registry))
5364 Vface_default_registry = unibyte_registry;
5365 else
5366 Vface_default_registry = build_string ("iso8859-1");
5367 }
5368 }
5369#endif /* HAVE_X_WINDOWS */
5370
44747bd0 5371 if (!FRAME_WINDOW_P (f))
82641697
GM
5372 {
5373 LFACE_FAMILY (lface) = build_string ("default");
5374 LFACE_SWIDTH (lface) = Qnormal;
5375 LFACE_HEIGHT (lface) = make_number (1);
5376 LFACE_WEIGHT (lface) = Qnormal;
5377 LFACE_SLANT (lface) = Qnormal;
5378 }
5379
5380 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5381 LFACE_UNDERLINE (lface) = Qnil;
5382
5383 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5384 LFACE_OVERLINE (lface) = Qnil;
5385
5386 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5387 LFACE_STRIKE_THROUGH (lface) = Qnil;
5388
5389 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5390 LFACE_BOX (lface) = Qnil;
5391
5392 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5393 LFACE_INVERSE (lface) = Qnil;
5394
5395 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5396 {
5397 /* This function is called so early that colors are not yet
5398 set in the frame parameter list. */
5399 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5400
5401 if (CONSP (color) && STRINGP (XCDR (color)))
5402 LFACE_FOREGROUND (lface) = XCDR (color);
5403 else if (FRAME_X_P (f))
5404 return 0;
44747bd0 5405 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
82641697
GM
5406 /* Frame parameters for terminal frames usually don't contain
5407 a color. Use an empty string to indicate that the face
5408 should use the (unknown) default color of the terminal. */
5409 LFACE_FOREGROUND (lface) = build_string ("");
5410 else
5411 abort ();
5412 }
5413
5414 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5415 {
5416 /* This function is called so early that colors are not yet
5417 set in the frame parameter list. */
5418 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5419 if (CONSP (color) && STRINGP (XCDR (color)))
5420 LFACE_BACKGROUND (lface) = XCDR (color);
5421 else if (FRAME_X_P (f))
5422 return 0;
44747bd0 5423 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
82641697
GM
5424 /* Frame parameters for terminal frames usually don't contain
5425 a color. Use an empty string to indicate that the face
5426 should use the (unknown) default color of the terminal. */
5427 LFACE_BACKGROUND (lface) = build_string ("");
5428 else
5429 abort ();
5430 }
5431
5432 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5433 LFACE_STIPPLE (lface) = Qnil;
5434
5435 /* Realize the face; it must be fully-specified now. */
5436 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5437 check_lface (lface);
5438 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
5439 face = realize_face (c, attrs, CHARSET_ASCII);
5440
5441 /* Remove the former default face. */
5442 if (c->used > DEFAULT_FACE_ID)
5443 {
5444 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5445 uncache_face (c, default_face);
5446 free_realized_face (f, default_face);
5447 }
5448
5449 /* Insert the new default face. */
5450 cache_face (c, face, lface_hash (attrs));
5451 xassert (face->id == DEFAULT_FACE_ID);
5452 return 1;
5453}
5454
5455
5456/* Realize basic faces other than the default face in face cache C.
5457 SYMBOL is the face name, ID is the face id the realized face must
5458 have. The default face must have been realized already. */
5459
5460static void
5461realize_named_face (f, symbol, id)
5462 struct frame *f;
5463 Lisp_Object symbol;
5464 int id;
5465{
5466 struct face_cache *c = FRAME_FACE_CACHE (f);
5467 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5468 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5469 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5470 struct face *new_face;
5471
5472 /* The default face must exist and be fully specified. */
5473 get_lface_attributes (f, Qdefault, attrs, 1);
5474 check_lface_attrs (attrs);
5475 xassert (lface_fully_specified_p (attrs));
5476
5477 /* If SYMBOL isn't know as a face, create it. */
5478 if (NILP (lface))
5479 {
5480 Lisp_Object frame;
5481 XSETFRAME (frame, f);
5482 lface = Finternal_make_lisp_face (symbol, frame);
5483 }
5484
5485 /* Merge SYMBOL's face with the default face. */
5486 get_lface_attributes (f, symbol, symbol_attrs, 1);
5487 merge_face_vectors (symbol_attrs, attrs);
5488
5489 /* Realize the face. */
5490 new_face = realize_face (c, attrs, CHARSET_ASCII);
5491
5492 /* Remove the former face. */
5493 if (c->used > id)
5494 {
5495 struct face *old_face = c->faces_by_id[id];
5496 uncache_face (c, old_face);
5497 free_realized_face (f, old_face);
5498 }
5499
5500 /* Insert the new face. */
5501 cache_face (c, new_face, lface_hash (attrs));
5502 xassert (new_face->id == id);
5503}
5504
5505
5506/* Realize the fully-specified face with attributes ATTRS in face
5507 cache C for character set CHARSET or for unibyte text if CHARSET <
5508 0. Value is a pointer to the newly created realized face. */
5509
5510static struct face *
5511realize_face (c, attrs, charset)
5512 struct face_cache *c;
5513 Lisp_Object *attrs;
5514 int charset;
5515{
5516 struct face *face;
5517
5518 /* LFACE must be fully specified. */
5519 xassert (c != NULL);
5520 check_lface_attrs (attrs);
5521
5522 if (FRAME_X_P (c->f))
5523 face = realize_x_face (c, attrs, charset);
44747bd0 5524 else if (FRAME_TERMCAP_P (c->f) || FRAME_MSDOS_P (c->f))
82641697
GM
5525 face = realize_tty_face (c, attrs, charset);
5526 else
5527 abort ();
5528
5529 return face;
5530}
5531
5532
5533/* Realize the fully-specified face with attributes ATTRS in face
5534 cache C for character set CHARSET or for unibyte text if CHARSET <
5535 0. Do it for X frame C->f. Value is a pointer to the newly
5536 created realized face. */
5537
5538static struct face *
5539realize_x_face (c, attrs, charset)
5540 struct face_cache *c;
5541 Lisp_Object *attrs;
5542 int charset;
5543{
5544#ifdef HAVE_X_WINDOWS
5545 struct face *face, *default_face;
5546 struct frame *f = c->f;
5547 Lisp_Object stipple, overline, strike_through, box;
5548 Lisp_Object unibyte_registry;
5549 struct gcpro gcpro1;
5550
5551 xassert (FRAME_X_P (f));
5552
5553 /* If realizing a face for use in unibyte text, get the X registry
5554 and encoding to use from Vface_default_registry. */
5555 if (charset < 0)
5556 unibyte_registry = (STRINGP (Vface_default_registry)
5557 ? Vface_default_registry
5558 : build_string ("iso8859-1"));
5559 else
5560 unibyte_registry = Qnil;
5561 GCPRO1 (unibyte_registry);
5562
5563 /* Allocate a new realized face. */
5564 face = make_realized_face (attrs, charset, unibyte_registry);
5565
5566 /* Determine the font to use. Most of the time, the font will be
5567 the same as the font of the default face, so try that first. */
5568 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5569 if (default_face
5570 && FACE_SUITABLE_FOR_CHARSET_P (default_face, charset)
5571 && lface_same_font_attributes_p (default_face->lface, attrs))
5572 {
5573 face->font = default_face->font;
5574 face->fontset = default_face->fontset;
5575 face->font_info_id = default_face->font_info_id;
5576 face->font_name = default_face->font_name;
5577 face->registry = default_face->registry;
5578 }
5579 else if (charset >= 0)
5580 {
5581 /* For all charsets except CHARSET_COMPOSITION, we use our own
5582 font selection functions to choose a best matching font for
5583 the specified face attributes. If the face specifies a
5584 fontset alias name, the fontset determines the font name
5585 pattern, otherwise we construct a font pattern from face
5586 attributes and charset.
5587
5588 If charset is CHARSET_COMPOSITION, we always construct a face
5589 with a fontset, even if the face doesn't specify a fontset alias
5590 (we use fontset-standard in that case). When the composite
5591 character is displayed in xterm.c, a suitable concrete font is
5592 loaded in x_get_char_font_and_encoding. */
5593
5594 char *font_name = NULL;
5595 int fontset = face_fontset (f, attrs);
5596
5597 if (charset == CHARSET_COMPOSITION)
5598 fontset = max (0, fontset);
5599 else if (fontset < 0)
5600 font_name = choose_face_font (f, attrs, charset, Qnil);
5601 else
5602 {
5603 font_name = choose_face_fontset_font (f, attrs, fontset, charset);
5604 fontset = -1;
5605 }
5606
5607 load_face_font_or_fontset (f, face, font_name, fontset);
5608 xfree (font_name);
5609 }
5610 else
5611 {
5612 /* Unibyte case, and font is not equal to that of the default
5613 face. UNIBYTE_REGISTRY is the X registry and encoding the
5614 font should have. What is a reasonable thing to do if the
5615 user specified a fontset alias name for the face in this
5616 case? We choose a font by taking the ASCII font of the
5617 fontset, but using UNIBYTE_REGISTRY for its registry and
5618 encoding. */
5619
5620 char *font_name = NULL;
5621 int fontset = face_fontset (f, attrs);
5622
5623 if (fontset < 0)
5624 font_name = choose_face_font (f, attrs, charset, unibyte_registry);
5625 else
5626 font_name = choose_face_fontset_font (f, attrs, fontset, charset);
5627
5628 load_face_font_or_fontset (f, face, font_name, -1);
5629 xfree (font_name);
5630 }
5631
5632 /* Load colors, and set remaining attributes. */
5633
5634 load_face_colors (f, face, attrs);
660ed669 5635
82641697
GM
5636 /* Set up box. */
5637 box = attrs[LFACE_BOX_INDEX];
5638 if (STRINGP (box))
cb637678 5639 {
82641697
GM
5640 /* A simple box of line width 1 drawn in color given by
5641 the string. */
5642 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5643 LFACE_BOX_INDEX);
5644 face->box = FACE_SIMPLE_BOX;
5645 face->box_line_width = 1;
cb637678 5646 }
82641697 5647 else if (INTEGERP (box))
42120bc7 5648 {
82641697
GM
5649 /* Simple box of specified line width in foreground color of the
5650 face. */
5651 xassert (XINT (box) > 0);
5652 face->box = FACE_SIMPLE_BOX;
5653 face->box_line_width = XFASTINT (box);
5654 face->box_color = face->foreground;
5655 face->box_color_defaulted_p = 1;
5656 }
5657 else if (CONSP (box))
5658 {
5659 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5660 being one of `raised' or `sunken'. */
5661 face->box = FACE_SIMPLE_BOX;
5662 face->box_color = face->foreground;
5663 face->box_color_defaulted_p = 1;
5664 face->box_line_width = 1;
5665
5666 while (CONSP (box))
42120bc7 5667 {
82641697
GM
5668 Lisp_Object keyword, value;
5669
5670 keyword = XCAR (box);
5671 box = XCDR (box);
5672
5673 if (!CONSP (box))
5674 break;
5675 value = XCAR (box);
5676 box = XCDR (box);
5677
5678 if (EQ (keyword, QCline_width))
5679 {
5680 if (INTEGERP (value) && XINT (value) > 0)
5681 face->box_line_width = XFASTINT (value);
5682 }
5683 else if (EQ (keyword, QCcolor))
5684 {
5685 if (STRINGP (value))
5686 {
5687 face->box_color = load_color (f, face, value,
5688 LFACE_BOX_INDEX);
5689 face->use_box_color_for_shadows_p = 1;
5690 }
5691 }
5692 else if (EQ (keyword, QCstyle))
a8517066 5693 {
82641697
GM
5694 if (EQ (value, Qreleased_button))
5695 face->box = FACE_RAISED_BOX;
5696 else if (EQ (value, Qpressed_button))
5697 face->box = FACE_SUNKEN_BOX;
a8517066 5698 }
42120bc7
RS
5699 }
5700 }
195f798e 5701
82641697
GM
5702 /* Text underline, overline, strike-through. */
5703
5704 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5705 {
5706 /* Use default color (same as foreground color). */
5707 face->underline_p = 1;
5708 face->underline_defaulted_p = 1;
5709 face->underline_color = 0;
5710 }
5711 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
195f798e 5712 {
82641697
GM
5713 /* Use specified color. */
5714 face->underline_p = 1;
5715 face->underline_defaulted_p = 0;
5716 face->underline_color
5717 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5718 LFACE_UNDERLINE_INDEX);
195f798e 5719 }
82641697 5720 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
7b00de84 5721 {
82641697
GM
5722 face->underline_p = 0;
5723 face->underline_defaulted_p = 0;
5724 face->underline_color = 0;
7b00de84
JB
5725 }
5726
82641697
GM
5727 overline = attrs[LFACE_OVERLINE_INDEX];
5728 if (STRINGP (overline))
cb637678 5729 {
82641697
GM
5730 face->overline_color
5731 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5732 LFACE_OVERLINE_INDEX);
5733 face->overline_p = 1;
cb637678 5734 }
82641697 5735 else if (EQ (overline, Qt))
cb637678 5736 {
82641697
GM
5737 face->overline_color = face->foreground;
5738 face->overline_color_defaulted_p = 1;
5739 face->overline_p = 1;
cb637678
JB
5740 }
5741
82641697
GM
5742 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5743 if (STRINGP (strike_through))
5744 {
5745 face->strike_through_color
5746 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5747 LFACE_STRIKE_THROUGH_INDEX);
5748 face->strike_through_p = 1;
5749 }
5750 else if (EQ (strike_through, Qt))
5751 {
5752 face->strike_through_color = face->foreground;
5753 face->strike_through_color_defaulted_p = 1;
5754 face->strike_through_p = 1;
5755 }
867dd159 5756
82641697
GM
5757 stipple = attrs[LFACE_STIPPLE_INDEX];
5758 if (!NILP (stipple))
5759 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
660ed669 5760
82641697
GM
5761 UNGCPRO;
5762 xassert (face->fontset < 0 || face->charset == CHARSET_COMPOSITION);
5763 xassert (FACE_SUITABLE_FOR_CHARSET_P (face, charset));
5764 return face;
5765#endif /* HAVE_X_WINDOWS */
660ed669
JB
5766}
5767
729425b1 5768
82641697
GM
5769/* Realize the fully-specified face with attributes ATTRS in face
5770 cache C for character set CHARSET or for unibyte text if CHARSET <
5771 0. Do it for TTY frame C->f. Value is a pointer to the newly
5772 created realized face. */
a8517066 5773
82641697
GM
5774static struct face *
5775realize_tty_face (c, attrs, charset)
5776 struct face_cache *c;
5777 Lisp_Object *attrs;
5778 int charset;
5779{
5780 struct face *face;
5781 int weight, slant;
5782 Lisp_Object color;
729425b1 5783
82641697 5784 /* Frame must be a termcap frame. */
44747bd0 5785 xassert (FRAME_TERMCAP_P (c->f) || FRAME_MSDOS_P (c->f));
82641697
GM
5786
5787 /* Allocate a new realized face. */
5788 face = make_realized_face (attrs, charset, Qnil);
44747bd0 5789 face->font_name = FRAME_MSDOS_P (c->f) ? "ms-dos" : "tty";
82641697
GM
5790
5791 /* Map face attributes to TTY appearances. We map slant to
5792 dimmed text because we want italic text to appear differently
5793 and because dimmed text is probably used infrequently. */
5794 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5795 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
5796
5797 if (weight > XLFD_WEIGHT_MEDIUM)
5798 face->tty_bold_p = 1;
5799 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
5800 face->tty_dim_p = 1;
5801 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5802 face->tty_underline_p = 1;
5803 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5804 face->tty_reverse_p = 1;
5805
5806 /* Map color names to color indices. */
5807 face->foreground = face->background = FACE_TTY_DEFAULT_COLOR;
5808
5809 color = attrs[LFACE_FOREGROUND_INDEX];
5810 if (XSTRING (color)->size
5811 && (color = Fassoc (color, Vface_tty_color_alist),
5812 CONSP (color)))
5813 face->foreground = XINT (XCDR (color));
5814
44747bd0
EZ
5815#ifdef MSDOS
5816 if (FRAME_MSDOS_P (c->f) && face->foreground == FACE_TTY_DEFAULT_COLOR)
1697ca38
EZ
5817 {
5818 face->foreground = load_color (c->f, face,
5819 attrs[LFACE_FOREGROUND_INDEX],
5820 LFACE_FOREGROUND_INDEX);
5821 /* If the foreground of the default face is the default color,
5822 use the foreground color defined by the frame. */
5823 if (face->foreground == FACE_TTY_DEFAULT_COLOR)
5824 {
5825 face->foreground = FRAME_FOREGROUND_PIXEL (f);
5826 attrs[LFACE_FOREGROUND_INDEX] =
5827 build_string (msdos_stdcolor_name (face->foreground));
5828 }
5829 }
44747bd0
EZ
5830#endif
5831
82641697
GM
5832 color = attrs[LFACE_BACKGROUND_INDEX];
5833 if (XSTRING (color)->size
5834 && (color = Fassoc (color, Vface_tty_color_alist),
5835 CONSP (color)))
5836 face->background = XINT (XCDR (color));
729425b1 5837
44747bd0
EZ
5838#ifdef MSDOS
5839 if (FRAME_MSDOS_P (c->f) && face->background == FACE_TTY_DEFAULT_COLOR)
1697ca38
EZ
5840 {
5841 face->background = load_color (c->f, face,
5842 attrs[LFACE_BACKGROUND_INDEX],
5843 LFACE_BACKGROUND_INDEX);
5844 /* If the background of the default face is the default color,
5845 use the background color defined by the frame. */
5846 if (face->background == FACE_TTY_DEFAULT_COLOR)
5847 {
5848 face->background = FRAME_BACKGROUND_PIXEL (f);
5849 attrs[LFACE_BACKGROUND_INDEX] =
5850 build_string (msdos_stdcolor_name (face->background));
5851 }
5852 }
44747bd0
EZ
5853
5854 /* Swap colors if face is inverse-video. */
5855 if (face->tty_reverse_p)
5856 {
5857 unsigned long tem = face->foreground;
5858
5859 face->foreground = face->background;
5860 face->background = tem;
5861 }
5862#endif
5863
82641697 5864 return face;
729425b1 5865}
867dd159 5866
82641697
GM
5867
5868DEFUN ("face-register-tty-color", Fface_register_tty_color,
5869 Sface_register_tty_color, 2, 2, 0,
5870 "Say that COLOR is color number NUMBER on the terminal.\n\
5871COLOR is a string, the color name. Value is COLOR.")
5872 (color, number)
5873 Lisp_Object color, number;
7b7739b1 5874{
82641697
GM
5875 Lisp_Object entry;
5876
5877 CHECK_STRING (color, 0);
5878 CHECK_NUMBER (number, 1);
5879 entry = Fassoc (color, Vface_tty_color_alist);
5880 if (NILP (entry))
5881 Vface_tty_color_alist = Fcons (Fcons (color, number),
5882 Vface_tty_color_alist);
5883 else
5884 Fsetcdr (entry, number);
5885 return color;
7b7739b1
JB
5886}
5887
867dd159 5888
82641697
GM
5889DEFUN ("face-clear-tty-colors", Fface_clear_tty_colors,
5890 Sface_clear_tty_colors, 0, 0, 0,
5891 "Unregister all registered tty colors.")
5892 ()
660ed669 5893{
82641697 5894 return Vface_tty_color_alist = Qnil;
660ed669
JB
5895}
5896
867dd159 5897
82641697
GM
5898DEFUN ("tty-defined-colors", Ftty_defined_colors,
5899 Stty_defined_colors, 0, 0, 0,
5900 "Return a list of registered tty colors.")
5901 ()
867dd159 5902{
82641697 5903 Lisp_Object list, colors;
867dd159 5904
82641697
GM
5905 colors = Qnil;
5906 for (list = Vface_tty_color_alist; CONSP (list); list = XCDR (list))
5907 colors = Fcons (XCAR (XCAR (list)), colors);
867dd159 5908
82641697 5909 return colors;
867dd159 5910}
2e16580f 5911
82641697
GM
5912
5913\f
5914/***********************************************************************
5915 Computing Faces
5916 ***********************************************************************/
5917
5918/* Return the ID of the face to use to display character CH with face
5919 property PROP on frame F in current_buffer. */
2e16580f
RS
5920
5921int
82641697 5922compute_char_face (f, ch, prop)
2e16580f 5923 struct frame *f;
82641697
GM
5924 int ch;
5925 Lisp_Object prop;
2e16580f 5926{
82641697
GM
5927 int face_id;
5928 int charset = (NILP (current_buffer->enable_multibyte_characters)
5929 ? -1
5930 : CHAR_CHARSET (ch));
5931
5932 if (NILP (prop))
5933 face_id = FACE_FOR_CHARSET (f, DEFAULT_FACE_ID, charset);
5934 else
2e16580f 5935 {
82641697
GM
5936 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5937 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5938 bcopy (default_face->lface, attrs, sizeof attrs);
5939 merge_face_vector_with_property (f, attrs, prop);
5940 face_id = lookup_face (f, attrs, charset);
2e16580f
RS
5941 }
5942
82641697 5943 return face_id;
2e16580f 5944}
bc0db68d 5945
b349f4fb 5946
82641697
GM
5947/* Return the face ID associated with buffer position POS for
5948 displaying ASCII characters. Return in *ENDPTR the position at
5949 which a different face is needed, as far as text properties and
5950 overlays are concerned. W is a window displaying current_buffer.
5951
5952 REGION_BEG, REGION_END delimit the region, so it can be
5953 highlighted.
6f134486 5954
82641697
GM
5955 LIMIT is a position not to scan beyond. That is to limit the time
5956 this function can take.
5957
5958 If MOUSE is non-zero, use the character's mouse-face, not its face.
5959
5960 The face returned is suitable for displaying CHARSET_ASCII if
5961 current_buffer->enable_multibyte_characters is non-nil. Otherwise,
5962 the face is suitable for displaying unibyte text. */
bc0db68d 5963
cb637678 5964int
82641697
GM
5965face_at_buffer_position (w, pos, region_beg, region_end,
5966 endptr, limit, mouse)
f211082d 5967 struct window *w;
7b7739b1 5968 int pos;
bc0db68d 5969 int region_beg, region_end;
7b7739b1 5970 int *endptr;
b349f4fb 5971 int limit;
6f134486 5972 int mouse;
7b7739b1 5973{
82641697
GM
5974 struct frame *f = XFRAME (w->frame);
5975 Lisp_Object attrs[LFACE_VECTOR_SIZE];
b6d40e46 5976 Lisp_Object prop, position;
82641697 5977 int i, noverlays;
7b7739b1 5978 Lisp_Object *overlay_vec;
f211082d 5979 Lisp_Object frame;
f6b98e0b 5980 int endpos;
82641697
GM
5981 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5982 Lisp_Object limit1, end;
5983 struct face *default_face;
5984 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
f6b98e0b
JB
5985
5986 /* W must display the current buffer. We could write this function
5987 to use the frame and buffer of W, but right now it doesn't. */
060fb5c1 5988 /* xassert (XBUFFER (w->buffer) == current_buffer); */
f211082d 5989
ac22a6c4 5990 XSETFRAME (frame, f);
82641697 5991 XSETFASTINT (position, pos);
7b7739b1 5992
f6b98e0b 5993 endpos = ZV;
bc0db68d
RS
5994 if (pos < region_beg && region_beg < endpos)
5995 endpos = region_beg;
f6b98e0b 5996
82641697
GM
5997 /* Get the `face' or `mouse_face' text property at POS, and
5998 determine the next position at which the property changes. */
6f134486 5999 prop = Fget_text_property (position, propname, w->buffer);
82641697
GM
6000 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6001 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6002 if (INTEGERP (end))
6003 endpos = XINT (end);
6f134486 6004
82641697 6005 /* Look at properties from overlays. */
b6d40e46 6006 {
f6b98e0b 6007 int next_overlay;
9516fe94
RS
6008 int len;
6009
6010 /* First try with room for 40 overlays. */
6011 len = 40;
6012 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7af31819 6013 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
82641697 6014 &next_overlay, NULL);
9516fe94 6015
82641697
GM
6016 /* If there are more than 40, make enough space for all, and try
6017 again. */
9516fe94
RS
6018 if (noverlays > len)
6019 {
6020 len = noverlays;
6021 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
82277c2f 6022 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
82641697 6023 &next_overlay, NULL);
9516fe94 6024 }
b6d40e46 6025
f6b98e0b
JB
6026 if (next_overlay < endpos)
6027 endpos = next_overlay;
b6d40e46
JB
6028 }
6029
6030 *endptr = endpos;
7b7739b1 6031
82641697
GM
6032 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6033
6034 /* Optimize common cases where we can use the default face. */
6035 if (noverlays == 0
6036 && NILP (prop)
6037 && !(pos >= region_beg && pos < region_end)
6038 && (multibyte_p
6039 || !FRAME_WINDOW_P (f)
6040 || FACE_SUITABLE_FOR_CHARSET_P (default_face, -1)))
6041 return DEFAULT_FACE_ID;
6042
6043 /* Begin with attributes from the default face. */
6044 bcopy (default_face->lface, attrs, sizeof attrs);
6045
6046 /* Merge in attributes specified via text properties. */
6047 if (!NILP (prop))
6048 merge_face_vector_with_property (f, attrs, prop);
6049
6050 /* Now merge the overlay data. */
18195655 6051 noverlays = sort_overlays (overlay_vec, noverlays, w);
18195655 6052 for (i = 0; i < noverlays; i++)
4699e6d2 6053 {
18195655
RS
6054 Lisp_Object oend;
6055 int oendpos;
6056
6057 prop = Foverlay_get (overlay_vec[i], propname);
82641697
GM
6058 if (!NILP (prop))
6059 merge_face_vector_with_property (f, attrs, prop);
18195655
RS
6060
6061 oend = OVERLAY_END (overlay_vec[i]);
6062 oendpos = OVERLAY_POSITION (oend);
6063 if (oendpos < endpos)
6064 endpos = oendpos;
6065 }
6066
82641697 6067 /* If in the region, merge in the region face. */
18195655
RS
6068 if (pos >= region_beg && pos < region_end)
6069 {
82641697
GM
6070 Lisp_Object region_face = lface_from_face_name (f, Qregion, 0);
6071 merge_face_vectors (XVECTOR (region_face)->contents, attrs);
6072
18195655
RS
6073 if (region_end < endpos)
6074 endpos = region_end;
18195655
RS
6075 }
6076
6077 *endptr = endpos;
6078
82641697
GM
6079 /* Look up a realized face with the given face attributes,
6080 or realize a new one. Charset is ignored for tty frames. */
6081 return lookup_face (f, attrs, multibyte_p ? CHARSET_ASCII : -1);
18195655
RS
6082}
6083
60573a90 6084
82641697
GM
6085/* Compute the face at character position POS in Lisp string STRING on
6086 window W, for charset CHARSET_ASCII.
7b7739b1 6087
82641697
GM
6088 If STRING is an overlay string, it comes from position BUFPOS in
6089 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6090 not an overlay string. W must display the current buffer.
6091 REGION_BEG and REGION_END give the start and end positions of the
6092 region; both are -1 if no region is visible. BASE_FACE_ID is the
6093 id of the basic face to merge with. It is usually equal to
045dee35 6094 DEFAULT_FACE_ID but can be MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID
82641697
GM
6095 for strings displayed in the mode or top line.
6096
6097 Set *ENDPTR to the next position where to check for faces in
6098 STRING; -1 if the face is constant from POS to the end of the
6099 string.
18195655 6100
82641697
GM
6101 Value is the id of the face to use. The face returned is suitable
6102 for displaying CHARSET_ASCII if STRING is multibyte. Otherwise,
6103 the face is suitable for displaying unibyte text. */
fffc2367 6104
82641697
GM
6105int
6106face_at_string_position (w, string, pos, bufpos, region_beg,
6107 region_end, endptr, base_face_id)
6108 struct window *w;
6109 Lisp_Object string;
6110 int pos, bufpos;
6111 int region_beg, region_end;
6112 int *endptr;
6113 enum face_id base_face_id;
660ed669 6114{
82641697
GM
6115 Lisp_Object prop, position, end, limit;
6116 struct frame *f = XFRAME (WINDOW_FRAME (w));
6117 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6118 struct face *base_face;
6119 int multibyte_p = STRING_MULTIBYTE (string);
6120
6121 /* Get the value of the face property at the current position within
6122 STRING. Value is nil if there is no face property. */
6123 XSETFASTINT (position, pos);
6124 prop = Fget_text_property (position, Qface, string);
6125
6126 /* Get the next position at which to check for faces. Value of end
6127 is nil if face is constant all the way to the end of the string.
6128 Otherwise it is a string position where to check faces next.
6129 Limit is the maximum position up to which to check for property
6130 changes in Fnext_single_property_change. Strings are usually
6131 short, so set the limit to the end of the string. */
6132 XSETFASTINT (limit, XSTRING (string)->size);
6133 end = Fnext_single_property_change (position, Qface, string, limit);
6134 if (INTEGERP (end))
6135 *endptr = XFASTINT (end);
6136 else
6137 *endptr = -1;
6138
6139 base_face = FACE_FROM_ID (f, base_face_id);
6140 xassert (base_face);
6141
6142 /* Optimize the default case that there is no face property and we
6143 are not in the region. */
6144 if (NILP (prop)
6145 && (base_face_id != DEFAULT_FACE_ID
6146 /* BUFPOS <= 0 means STRING is not an overlay string, so
6147 that the region doesn't have to be taken into account. */
6148 || bufpos <= 0
6149 || bufpos < region_beg
6150 || bufpos >= region_end)
6151 && (multibyte_p
6152 /* We can't realize faces for different charsets differently
6153 if we don't have fonts, so we can stop here if not working
6154 on a window-system frame. */
6155 || !FRAME_WINDOW_P (f)
6156 || FACE_SUITABLE_FOR_CHARSET_P (base_face, -1)))
6157 return base_face->id;
6158
6159 /* Begin with attributes from the base face. */
6160 bcopy (base_face->lface, attrs, sizeof attrs);
6161
6162 /* Merge in attributes specified via text properties. */
6163 if (!NILP (prop))
6164 merge_face_vector_with_property (f, attrs, prop);
6165
6166 /* If in the region, merge in the region face. */
6167 if (bufpos
6168 && bufpos >= region_beg
6169 && bufpos < region_end)
a8517066 6170 {
82641697
GM
6171 Lisp_Object region_face = lface_from_face_name (f, Qregion, 0);
6172 merge_face_vectors (XVECTOR (region_face)->contents, attrs);
a8517066 6173 }
660ed669 6174
82641697
GM
6175 /* Look up a realized face with the given face attributes,
6176 or realize a new one. */
6177 return lookup_face (f, attrs, multibyte_p ? CHARSET_ASCII : -1);
660ed669
JB
6178}
6179
6180
c115973b 6181\f
82641697
GM
6182/***********************************************************************
6183 Tests
6184 ***********************************************************************/
c115973b 6185
82641697 6186#if GLYPH_DEBUG
c115973b 6187
82641697 6188/* Print the contents of the realized face FACE to stderr. */
c115973b 6189
82641697
GM
6190static void
6191dump_realized_face (face)
6192 struct face *face;
6193{
6194 fprintf (stderr, "ID: %d\n", face->id);
6195#ifdef HAVE_X_WINDOWS
6196 fprintf (stderr, "gc: %d\n", (int) face->gc);
6197#endif
6198 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6199 face->foreground,
6200 XSTRING (face->lface[LFACE_FOREGROUND_INDEX])->data);
6201 fprintf (stderr, "background: 0x%lx (%s)\n",
6202 face->background,
6203 XSTRING (face->lface[LFACE_BACKGROUND_INDEX])->data);
6204 fprintf (stderr, "font_name: %s (%s)\n",
6205 face->font_name,
6206 XSTRING (face->lface[LFACE_FAMILY_INDEX])->data);
6207#ifdef HAVE_X_WINDOWS
6208 fprintf (stderr, "font = %p\n", face->font);
6209#endif
6210 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
6211 fprintf (stderr, "fontset: %d\n", face->fontset);
6212 fprintf (stderr, "underline: %d (%s)\n",
6213 face->underline_p,
6214 XSTRING (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX]))->data);
6215 fprintf (stderr, "hash: %d\n", face->hash);
6216 fprintf (stderr, "charset: %d\n", face->charset);
c115973b
JB
6217}
6218
6219
82641697
GM
6220DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, "")
6221 (n)
6222 Lisp_Object n;
c115973b 6223{
82641697 6224 if (NILP (n))
c115973b 6225 {
82641697
GM
6226 int i;
6227
6228 fprintf (stderr, "font selection order: ");
6229 for (i = 0; i < DIM (font_sort_order); ++i)
6230 fprintf (stderr, "%d ", font_sort_order[i]);
6231 fprintf (stderr, "\n");
6232
6233 fprintf (stderr, "alternative fonts: ");
6234 debug_print (Vface_alternative_font_family_alist);
6235 fprintf (stderr, "\n");
6236
6237 for (i = 0; i < FRAME_FACE_CACHE (selected_frame)->used; ++i)
6238 Fdump_face (make_number (i));
c115973b
JB
6239 }
6240 else
f5e278c7 6241 {
82641697
GM
6242 struct face *face;
6243 CHECK_NUMBER (n, 0);
6244 face = FACE_FROM_ID (selected_frame, XINT (n));
6245 if (face == NULL)
6246 error ("Not a valid face");
6247 dump_realized_face (face);
f5e278c7 6248 }
82641697 6249
c115973b
JB
6250 return Qnil;
6251}
b5c53576 6252
b5c53576 6253
82641697
GM
6254DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6255 0, 0, 0, "")
6256 ()
b5c53576 6257{
82641697
GM
6258 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6259 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6260 fprintf (stderr, "number of GCs = %d\n", ngcs);
6261 return Qnil;
b5c53576
RS
6262}
6263
82641697
GM
6264#endif /* GLYPH_DEBUG != 0 */
6265
b5c53576 6266
c115973b 6267\f
82641697
GM
6268/***********************************************************************
6269 Initialization
6270 ***********************************************************************/
cb637678 6271
c115973b 6272void
f211082d 6273syms_of_xfaces ()
c115973b 6274{
f211082d
JB
6275 Qface = intern ("face");
6276 staticpro (&Qface);
cd0bb842
RS
6277 Qpixmap_spec_p = intern ("pixmap-spec-p");
6278 staticpro (&Qpixmap_spec_p);
92610620
GM
6279 Qframe_update_face_colors = intern ("frame-update-face-colors");
6280 staticpro (&Qframe_update_face_colors);
6281
82641697
GM
6282 /* Lisp face attribute keywords. */
6283 QCfamily = intern (":family");
6284 staticpro (&QCfamily);
6285 QCheight = intern (":height");
6286 staticpro (&QCheight);
6287 QCweight = intern (":weight");
6288 staticpro (&QCweight);
6289 QCslant = intern (":slant");
6290 staticpro (&QCslant);
6291 QCunderline = intern (":underline");
6292 staticpro (&QCunderline);
6293 QCinverse_video = intern (":inverse-video");
6294 staticpro (&QCinverse_video);
6295 QCreverse_video = intern (":reverse-video");
6296 staticpro (&QCreverse_video);
6297 QCforeground = intern (":foreground");
6298 staticpro (&QCforeground);
6299 QCbackground = intern (":background");
6300 staticpro (&QCbackground);
6301 QCstipple = intern (":stipple");;
6302 staticpro (&QCstipple);
6303 QCwidth = intern (":width");
6304 staticpro (&QCwidth);
6305 QCfont = intern (":font");
6306 staticpro (&QCfont);
6307 QCbold = intern (":bold");
6308 staticpro (&QCbold);
6309 QCitalic = intern (":italic");
6310 staticpro (&QCitalic);
6311 QCoverline = intern (":overline");
6312 staticpro (&QCoverline);
6313 QCstrike_through = intern (":strike-through");
6314 staticpro (&QCstrike_through);
6315 QCbox = intern (":box");
6316 staticpro (&QCbox);
6317
6318 /* Symbols used for Lisp face attribute values. */
6319 QCcolor = intern (":color");
6320 staticpro (&QCcolor);
6321 QCline_width = intern (":line-width");
6322 staticpro (&QCline_width);
6323 QCstyle = intern (":style");
6324 staticpro (&QCstyle);
6325 Qreleased_button = intern ("released-button");
6326 staticpro (&Qreleased_button);
6327 Qpressed_button = intern ("pressed-button");
6328 staticpro (&Qpressed_button);
6329 Qnormal = intern ("normal");
6330 staticpro (&Qnormal);
6331 Qultra_light = intern ("ultra-light");
6332 staticpro (&Qultra_light);
6333 Qextra_light = intern ("extra-light");
6334 staticpro (&Qextra_light);
6335 Qlight = intern ("light");
6336 staticpro (&Qlight);
6337 Qsemi_light = intern ("semi-light");
6338 staticpro (&Qsemi_light);
6339 Qsemi_bold = intern ("semi-bold");
6340 staticpro (&Qsemi_bold);
6341 Qbold = intern ("bold");
6342 staticpro (&Qbold);
6343 Qextra_bold = intern ("extra-bold");
6344 staticpro (&Qextra_bold);
6345 Qultra_bold = intern ("ultra-bold");
6346 staticpro (&Qultra_bold);
6347 Qoblique = intern ("oblique");
6348 staticpro (&Qoblique);
6349 Qitalic = intern ("italic");
6350 staticpro (&Qitalic);
6351 Qreverse_oblique = intern ("reverse-oblique");
6352 staticpro (&Qreverse_oblique);
6353 Qreverse_italic = intern ("reverse-italic");
6354 staticpro (&Qreverse_italic);
6355 Qultra_condensed = intern ("ultra-condensed");
6356 staticpro (&Qultra_condensed);
6357 Qextra_condensed = intern ("extra-condensed");
6358 staticpro (&Qextra_condensed);
6359 Qcondensed = intern ("condensed");
6360 staticpro (&Qcondensed);
6361 Qsemi_condensed = intern ("semi-condensed");
6362 staticpro (&Qsemi_condensed);
6363 Qsemi_expanded = intern ("semi-expanded");
6364 staticpro (&Qsemi_expanded);
6365 Qexpanded = intern ("expanded");
6366 staticpro (&Qexpanded);
6367 Qextra_expanded = intern ("extra-expanded");
6368 staticpro (&Qextra_expanded);
6369 Qultra_expanded = intern ("ultra-expanded");
6370 staticpro (&Qultra_expanded);
6371 Qbackground_color = intern ("background-color");
6372 staticpro (&Qbackground_color);
6373 Qforeground_color = intern ("foreground-color");
6374 staticpro (&Qforeground_color);
6375 Qunspecified = intern ("unspecified");
6376 staticpro (&Qunspecified);
6377
6378 Qx_charset_registry = intern ("x-charset-registry");
6379 staticpro (&Qx_charset_registry);
92610620
GM
6380 Qface_alias = intern ("face-alias");
6381 staticpro (&Qface_alias);
82641697
GM
6382 Qdefault = intern ("default");
6383 staticpro (&Qdefault);
92610620
GM
6384 Qmode_line = intern ("mode-line");
6385 staticpro (&Qmode_line);
9ea173e8
GM
6386 Qtool_bar = intern ("tool-bar");
6387 staticpro (&Qtool_bar);
82641697
GM
6388 Qregion = intern ("region");
6389 staticpro (&Qregion);
8bd201d6
GM
6390 Qfringe = intern ("fringe");
6391 staticpro (&Qfringe);
045dee35
GM
6392 Qheader_line = intern ("header-line");
6393 staticpro (&Qheader_line);
8bd201d6
GM
6394 Qscroll_bar = intern ("scroll-bar");
6395 staticpro (&Qscroll_bar);
6396 Qcursor = intern ("cursor");
6397 staticpro (&Qcursor);
6398 Qborder = intern ("border");
6399 staticpro (&Qborder);
6400 Qmouse = intern ("mouse");
6401 staticpro (&Qmouse);
82641697
GM
6402
6403 defsubr (&Sinternal_make_lisp_face);
6404 defsubr (&Sinternal_lisp_face_p);
6405 defsubr (&Sinternal_set_lisp_face_attribute);
6406#ifdef HAVE_X_WINDOWS
6407 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6408 defsubr (&Sface_color_gray_p);
6409 defsubr (&Sface_color_supported_p);
6410#endif
6411 defsubr (&Sinternal_get_lisp_face_attribute);
6412 defsubr (&Sinternal_lisp_face_attribute_values);
6413 defsubr (&Sinternal_lisp_face_equal_p);
6414 defsubr (&Sinternal_lisp_face_empty_p);
6415 defsubr (&Sinternal_copy_lisp_face);
6416 defsubr (&Sinternal_merge_in_global_face);
6417 defsubr (&Sface_font);
6418 defsubr (&Sframe_face_alist);
6419 defsubr (&Sinternal_set_font_selection_order);
6420 defsubr (&Sinternal_set_alternative_font_family_alist);
6421#if GLYPH_DEBUG
6422 defsubr (&Sdump_face);
6423 defsubr (&Sshow_face_resources);
6424#endif /* GLYPH_DEBUG */
6425 defsubr (&Sclear_face_cache);
6426
057df17c
GM
6427 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
6428 "*Limit for font matching.\n\
6429If an integer > 0, font matching functions won't load more than\n\
6430that number of fonts when searching for a matching font.");
6431 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
6432
82641697
GM
6433 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
6434 "List of global face definitions (for internal use only.)");
6435 Vface_new_frame_defaults = Qnil;
6436
6437 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
6438 "*Default stipple pattern used on monochrome displays.\n\
6439This stipple pattern is used on monochrome displays\n\
6440instead of shades of gray for a face background color.\n\
6441See `set-face-stipple' for possible values for this variable.");
6442 Vface_default_stipple = build_string ("gray3");
6443
6444 DEFVAR_LISP ("face-default-registry", &Vface_default_registry,
6445 "Default registry and encoding to use.\n\
6446This registry and encoding is used for unibyte text. It is set up\n\
6447from the specified frame font when Emacs starts. (For internal use only.)");
6448 Vface_default_registry = Qnil;
6449
6450 DEFVAR_LISP ("face-alternative-font-family-alist",
6451 &Vface_alternative_font_family_alist, "");
6452 Vface_alternative_font_family_alist = Qnil;
6453
6454#if SCALABLE_FONTS
6455
6456 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
6457 "Allowed scalable fonts.\n\
6458A value of nil means don't allow any scalable fonts.\n\
6459A value of t means allow any scalable font.\n\
6460Otherwise, value must be a list of regular expressions. A font may be\n\
6461scaled if its name matches a regular expression in the list.");
6462 Vscalable_fonts_allowed = Qnil;
6463
6464#endif /* SCALABLE_FONTS */
b5c53576 6465
87485d6f 6466#ifdef HAVE_X_WINDOWS
cd0bb842 6467 defsubr (&Spixmap_spec_p);
82641697
GM
6468 defsubr (&Sx_list_fonts);
6469 defsubr (&Sinternal_face_x_get_resource);
92610620 6470 defsubr (&Sx_family_fonts);
82641697
GM
6471 defsubr (&Sx_font_family_list);
6472#endif /* HAVE_X_WINDOWS */
b5c53576 6473
82641697
GM
6474 /* TTY face support. */
6475 defsubr (&Sface_register_tty_color);
6476 defsubr (&Sface_clear_tty_colors);
6477 defsubr (&Stty_defined_colors);
6478 Vface_tty_color_alist = Qnil;
6479 staticpro (&Vface_tty_color_alist);
c115973b 6480}