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