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