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