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