Stick with debbugs-supported tags in triage
[emacs.git] / src / xfaces.c
blob056f90cb7ac7684350b433d9a4181f488d80ee5f
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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 (at
10 your option) any later version.
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.
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/>. */
20 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22 /* Faces.
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:
28 1. Font family name.
30 2. Font foundry name.
32 3. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 4. Font height in 1/10pt.
37 5. Font weight, e.g. `bold'.
39 6. Font slant, e.g. `italic'.
41 7. Foreground color.
43 8. Background color.
45 9. Whether or not characters should be underlined, and in what color.
47 10. Whether or not characters should be displayed in inverse video.
49 11. A background stipple, a bitmap.
51 12. Whether or not characters should be overlined, and in what color.
53 13. Whether or not characters should be strike-through, and in what
54 color.
56 14. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 15. Font-spec, or nil. This is a special attribute.
61 A font-spec is a collection of font attributes (specs).
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.
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.
71 15. A face name or list of face names from which to inherit attributes.
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.
77 17. A fontset name. This is another special attribute.
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
83 Faces are frame-local by nature because Emacs allows you 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.
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.
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'.
99 Face merging.
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.
109 Face realization.
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 the form of a struct face which is stored in the
116 face cache of the frame on which it was realized.
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.
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.
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.
134 Thus, all realized faces have a realized fontset.
137 Unibyte text.
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.
145 Font selection.
147 Font selection tries to find the best available matching font for a
148 given (character, face) combination.
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.
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.
161 Font selection can be influenced by the user.
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.
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.
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.
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
184 Character composition.
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.
195 Initialization of basic faces.
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. */
202 #include <config.h>
203 #include "sysstdio.h"
204 #include <sys/types.h>
205 #include <sys/stat.h>
207 #include "lisp.h"
208 #include "character.h"
209 #include "frame.h"
211 #ifdef USE_MOTIF
212 #include <Xm/Xm.h>
213 #include <Xm/XmStrDefs.h>
214 #endif /* USE_MOTIF */
216 #ifdef MSDOS
217 #include "dosfns.h"
218 #endif
220 #ifdef HAVE_WINDOW_SYSTEM
221 #include TERM_HEADER
222 #include "fontset.h"
223 #ifdef HAVE_NTGUI
224 #define GCGraphicsExposures 0
225 #endif /* HAVE_NTGUI */
227 #ifdef HAVE_NS
228 #define GCGraphicsExposures 0
229 #endif /* HAVE_NS */
230 #endif /* HAVE_WINDOW_SYSTEM */
232 #include "buffer.h"
233 #include "dispextern.h"
234 #include "blockinput.h"
235 #include "window.h"
236 #include "termchar.h"
238 #include "font.h"
240 #ifdef HAVE_X_WINDOWS
242 /* Compensate for a bug in Xos.h on some systems, on which it requires
243 time.h. On some such systems, Xos.h tries to redefine struct
244 timeval and struct timezone if USG is #defined while it is
245 #included. */
247 #ifdef XOS_NEEDS_TIME_H
248 #include <time.h>
249 #undef USG
250 #include <X11/Xos.h>
251 #define USG
252 #define __TIMEVAL__
253 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
254 #endif
255 #else /* not XOS_NEEDS_TIME_H */
256 #include <X11/Xos.h>
257 #endif /* not XOS_NEEDS_TIME_H */
259 #endif /* HAVE_X_WINDOWS */
261 #include <c-ctype.h>
263 /* True if face attribute ATTR is unspecified. */
265 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
267 /* True if face attribute ATTR is `ignore-defface'. */
269 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
271 /* Size of hash table of realized faces in face caches (should be a
272 prime number). */
274 #define FACE_CACHE_BUCKETS_SIZE 1001
276 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
278 /* Alist of alternative font families. Each element is of the form
279 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
280 try FAMILY1, then FAMILY2, ... */
282 Lisp_Object Vface_alternative_font_family_alist;
284 /* Alist of alternative font registries. Each element is of the form
285 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
286 loaded, try REGISTRY1, then REGISTRY2, ... */
288 Lisp_Object Vface_alternative_font_registry_alist;
290 /* The next ID to assign to Lisp faces. */
292 static int next_lface_id;
294 /* A vector mapping Lisp face Id's to face names. */
296 static Lisp_Object *lface_id_to_name;
297 static ptrdiff_t lface_id_to_name_size;
299 #ifdef HAVE_WINDOW_SYSTEM
301 /* Counter for calls to clear_face_cache. If this counter reaches
302 CLEAR_FONT_TABLE_COUNT, and a frame has more than
303 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
305 static int clear_font_table_count;
306 #define CLEAR_FONT_TABLE_COUNT 100
307 #define CLEAR_FONT_TABLE_NFONTS 10
309 #endif /* HAVE_WINDOW_SYSTEM */
311 /* True means face attributes have been changed since the last
312 redisplay. Used in redisplay_internal. */
314 bool face_change;
316 /* True means don't display bold text if a face's foreground
317 and background colors are the inverse of the default colors of the
318 display. This is a kluge to suppress `bold black' foreground text
319 which is hard to read on an LCD monitor. */
321 static bool tty_suppress_bold_inverse_default_colors_p;
323 /* A list of the form `((x . y))' used to avoid consing in
324 Finternal_set_lisp_face_attribute. */
326 static Lisp_Object Vparam_value_alist;
328 /* The total number of colors currently allocated. */
330 #ifdef GLYPH_DEBUG
331 static int ncolors_allocated;
332 static int npixmaps_allocated;
333 static int ngcs;
334 #endif
336 /* True means the definition of the `menu' face for new frames has
337 been changed. */
339 static bool menu_face_changed_default;
341 struct named_merge_point;
343 static struct face *realize_face (struct face_cache *, Lisp_Object *,
344 int);
345 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
346 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
347 static bool realize_basic_faces (struct frame *);
348 static bool realize_default_face (struct frame *);
349 static void realize_named_face (struct frame *, Lisp_Object, int);
350 static struct face_cache *make_face_cache (struct frame *);
351 static void free_face_cache (struct face_cache *);
352 static bool merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
353 bool, struct named_merge_point *);
354 static int color_distance (XColor *x, XColor *y);
356 #ifdef HAVE_WINDOW_SYSTEM
357 static void set_font_frame_param (Lisp_Object, Lisp_Object);
358 static void clear_face_gcs (struct face_cache *);
359 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
360 struct face *);
361 #endif /* HAVE_WINDOW_SYSTEM */
363 /***********************************************************************
364 Utilities
365 ***********************************************************************/
367 #ifdef HAVE_X_WINDOWS
369 #ifdef DEBUG_X_COLORS
371 /* The following is a poor mans infrastructure for debugging X color
372 allocation problems on displays with PseudoColor-8. Some X servers
373 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
374 color reference counts completely so that they don't signal an
375 error when a color is freed whose reference count is already 0.
376 Other X servers do. To help me debug this, the following code
377 implements a simple reference counting schema of its own, for a
378 single display/screen. --gerd. */
380 /* Reference counts for pixel colors. */
382 int color_count[256];
384 /* Register color PIXEL as allocated. */
386 void
387 register_color (unsigned long pixel)
389 eassert (pixel < 256);
390 ++color_count[pixel];
394 /* Register color PIXEL as deallocated. */
396 void
397 unregister_color (unsigned long pixel)
399 eassert (pixel < 256);
400 if (color_count[pixel] > 0)
401 --color_count[pixel];
402 else
403 emacs_abort ();
407 /* Register N colors from PIXELS as deallocated. */
409 void
410 unregister_colors (unsigned long *pixels, int n)
412 int i;
413 for (i = 0; i < n; ++i)
414 unregister_color (pixels[i]);
418 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
419 doc: /* Dump currently allocated colors to stderr. */)
420 (void)
422 int i, n;
424 fputc ('\n', stderr);
426 for (i = n = 0; i < ARRAYELTS (color_count); ++i)
427 if (color_count[i])
429 fprintf (stderr, "%3d: %5d", i, color_count[i]);
430 ++n;
431 if (n % 5 == 0)
432 fputc ('\n', stderr);
433 else
434 fputc ('\t', stderr);
437 if (n % 5 != 0)
438 fputc ('\n', stderr);
439 return Qnil;
442 #endif /* DEBUG_X_COLORS */
445 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
446 color values. Interrupt input must be blocked when this function
447 is called. */
449 void
450 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
452 /* If display has an immutable color map, freeing colors is not
453 necessary and some servers don't allow it. So don't do it. */
454 if (x_mutable_colormap (FRAME_X_VISUAL (f)))
456 #ifdef DEBUG_X_COLORS
457 unregister_colors (pixels, npixels);
458 #endif
459 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
460 pixels, npixels, 0);
465 #ifdef USE_X_TOOLKIT
467 /* Free colors used on display DPY. PIXELS is an array of NPIXELS pixel
468 color values. Interrupt input must be blocked when this function
469 is called. */
471 void
472 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
473 unsigned long *pixels, int npixels)
475 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
477 /* If display has an immutable color map, freeing colors is not
478 necessary and some servers don't allow it. So don't do it. */
479 if (x_mutable_colormap (dpyinfo->visual))
481 #ifdef DEBUG_X_COLORS
482 unregister_colors (pixels, npixels);
483 #endif
484 XFreeColors (dpy, cmap, pixels, npixels, 0);
487 #endif /* USE_X_TOOLKIT */
489 /* Create and return a GC for use on frame F. GC values and mask
490 are given by XGCV and MASK. */
492 static GC
493 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
495 GC gc;
496 block_input ();
497 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
498 unblock_input ();
499 IF_DEBUG (++ngcs);
500 return gc;
504 /* Free GC which was used on frame F. */
506 static void
507 x_free_gc (struct frame *f, GC gc)
509 eassert (input_blocked_p ());
510 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
511 XFreeGC (FRAME_X_DISPLAY (f), gc);
514 #endif /* HAVE_X_WINDOWS */
516 #ifdef HAVE_NTGUI
517 /* W32 emulation of GCs */
519 static GC
520 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
522 GC gc;
523 block_input ();
524 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
525 unblock_input ();
526 IF_DEBUG (++ngcs);
527 return gc;
531 /* Free GC which was used on frame F. */
533 static void
534 x_free_gc (struct frame *f, GC gc)
536 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
537 xfree (gc);
540 #endif /* HAVE_NTGUI */
542 #ifdef HAVE_NS
543 /* NS emulation of GCs */
545 static GC
546 x_create_gc (struct frame *f,
547 unsigned long mask,
548 XGCValues *xgcv)
550 GC gc = xmalloc (sizeof *gc);
551 *gc = *xgcv;
552 return gc;
555 static void
556 x_free_gc (struct frame *f, GC gc)
558 xfree (gc);
560 #endif /* HAVE_NS */
562 /***********************************************************************
563 Frames and faces
564 ***********************************************************************/
566 /* Initialize face cache and basic faces for frame F. */
568 void
569 init_frame_faces (struct frame *f)
571 /* Make a face cache, if F doesn't have one. */
572 if (FRAME_FACE_CACHE (f) == NULL)
573 FRAME_FACE_CACHE (f) = make_face_cache (f);
575 #ifdef HAVE_WINDOW_SYSTEM
576 /* Make the image cache. */
577 if (FRAME_WINDOW_P (f))
579 /* We initialize the image cache when creating the first frame
580 on a terminal, and not during terminal creation. This way,
581 `x-open-connection' on a tty won't create an image cache. */
582 if (FRAME_IMAGE_CACHE (f) == NULL)
583 FRAME_IMAGE_CACHE (f) = make_image_cache ();
584 ++FRAME_IMAGE_CACHE (f)->refcount;
586 #endif /* HAVE_WINDOW_SYSTEM */
588 /* Realize faces early (Bug#17889). */
589 if (!realize_basic_faces (f))
590 emacs_abort ();
594 /* Free face cache of frame F. Called from frame-dependent
595 resource freeing function, e.g. (x|tty)_free_frame_resources. */
597 void
598 free_frame_faces (struct frame *f)
600 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
602 if (face_cache)
604 free_face_cache (face_cache);
605 FRAME_FACE_CACHE (f) = NULL;
608 #ifdef HAVE_WINDOW_SYSTEM
609 if (FRAME_WINDOW_P (f))
611 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
612 if (image_cache)
614 --image_cache->refcount;
615 if (image_cache->refcount == 0)
616 free_image_cache (f);
619 #endif /* HAVE_WINDOW_SYSTEM */
623 /* Clear face caches, and recompute basic faces for frame F. Call
624 this after changing frame parameters on which those faces depend,
625 or when realized faces have been freed due to changing attributes
626 of named faces. */
628 void
629 recompute_basic_faces (struct frame *f)
631 if (FRAME_FACE_CACHE (f))
633 clear_face_cache (false);
634 if (!realize_basic_faces (f))
635 emacs_abort ();
640 /* Clear the face caches of all frames. CLEAR_FONTS_P means
641 try to free unused fonts, too. */
643 void
644 clear_face_cache (bool clear_fonts_p)
646 #ifdef HAVE_WINDOW_SYSTEM
647 Lisp_Object tail, frame;
649 if (clear_fonts_p
650 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
652 /* From time to time see if we can unload some fonts. This also
653 frees all realized faces on all frames. Fonts needed by
654 faces will be loaded again when faces are realized again. */
655 clear_font_table_count = 0;
657 FOR_EACH_FRAME (tail, frame)
659 struct frame *f = XFRAME (frame);
660 if (FRAME_WINDOW_P (f)
661 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
663 clear_font_cache (f);
664 free_all_realized_faces (frame);
668 else
670 /* Clear GCs of realized faces. */
671 FOR_EACH_FRAME (tail, frame)
673 struct frame *f = XFRAME (frame);
674 if (FRAME_WINDOW_P (f))
675 clear_face_gcs (FRAME_FACE_CACHE (f));
677 clear_image_caches (Qnil);
679 #endif /* HAVE_WINDOW_SYSTEM */
682 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
683 doc: /* Clear face caches on all frames.
684 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
685 (Lisp_Object thoroughly)
687 clear_face_cache (!NILP (thoroughly));
688 face_change = true;
689 windows_or_buffers_changed = 53;
690 return Qnil;
694 /***********************************************************************
695 X Pixmaps
696 ***********************************************************************/
698 #ifdef HAVE_WINDOW_SYSTEM
700 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
701 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
702 A bitmap specification is either a string, a file name, or a list
703 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
704 HEIGHT is its height, and DATA is a string containing the bits of
705 the pixmap. Bits are stored row by row, each row occupies
706 \(WIDTH + 7)/8 bytes. */)
707 (Lisp_Object object)
709 bool pixmap_p = false;
711 if (STRINGP (object))
712 /* If OBJECT is a string, it's a file name. */
713 pixmap_p = true;
714 else if (CONSP (object))
716 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
717 HEIGHT must be ints > 0, and DATA must be string large
718 enough to hold a bitmap of the specified size. */
719 Lisp_Object width, height, data;
721 height = width = data = Qnil;
723 if (CONSP (object))
725 width = XCAR (object);
726 object = XCDR (object);
727 if (CONSP (object))
729 height = XCAR (object);
730 object = XCDR (object);
731 if (CONSP (object))
732 data = XCAR (object);
736 if (STRINGP (data)
737 && RANGED_INTEGERP (1, width, INT_MAX)
738 && RANGED_INTEGERP (1, height, INT_MAX))
740 int bytes_per_row = (XINT (width) + CHAR_BIT - 1) / CHAR_BIT;
741 if (XINT (height) <= SBYTES (data) / bytes_per_row)
742 pixmap_p = true;
746 return pixmap_p ? Qt : Qnil;
750 /* Load a bitmap according to NAME (which is either a file name or a
751 pixmap spec) for use on frame F. Value is the bitmap_id (see
752 xfns.c). If NAME is nil, return with a bitmap id of zero. If
753 bitmap cannot be loaded, display a message saying so, and return
754 zero. */
756 static ptrdiff_t
757 load_pixmap (struct frame *f, Lisp_Object name)
759 ptrdiff_t bitmap_id;
761 if (NILP (name))
762 return 0;
764 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
766 block_input ();
767 if (CONSP (name))
769 /* Decode a bitmap spec into a bitmap. */
771 int h, w;
772 Lisp_Object bits;
774 w = XINT (Fcar (name));
775 h = XINT (Fcar (Fcdr (name)));
776 bits = Fcar (Fcdr (Fcdr (name)));
778 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
779 w, h);
781 else
783 /* It must be a string -- a file name. */
784 bitmap_id = x_create_bitmap_from_file (f, name);
786 unblock_input ();
788 if (bitmap_id < 0)
790 add_to_log ("Invalid or undefined bitmap `%s'", name);
791 bitmap_id = 0;
793 else
795 #ifdef GLYPH_DEBUG
796 ++npixmaps_allocated;
797 #endif
800 return bitmap_id;
803 #endif /* HAVE_WINDOW_SYSTEM */
807 /***********************************************************************
808 X Colors
809 ***********************************************************************/
811 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
812 RGB_LIST should contain (at least) 3 lisp integers.
813 Return true iff RGB_LIST is OK. */
815 static bool
816 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
818 #define PARSE_RGB_LIST_FIELD(field) \
819 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
821 color->field = XINT (XCAR (rgb_list)); \
822 rgb_list = XCDR (rgb_list); \
824 else \
825 return false;
827 PARSE_RGB_LIST_FIELD (red);
828 PARSE_RGB_LIST_FIELD (green);
829 PARSE_RGB_LIST_FIELD (blue);
831 return true;
835 /* Lookup on frame F the color described by the lisp string COLOR.
836 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
837 non-zero, then the `standard' definition of the same color is
838 returned in it. */
840 static bool
841 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
842 XColor *std_color)
844 Lisp_Object frame, color_desc;
846 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
847 return false;
849 XSETFRAME (frame, f);
851 color_desc = call2 (Qtty_color_desc, color, frame);
852 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
854 Lisp_Object rgb;
856 if (! INTEGERP (XCAR (XCDR (color_desc))))
857 return false;
859 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
861 rgb = XCDR (XCDR (color_desc));
862 if (! parse_rgb_list (rgb, tty_color))
863 return false;
865 /* Should we fill in STD_COLOR too? */
866 if (std_color)
868 /* Default STD_COLOR to the same as TTY_COLOR. */
869 *std_color = *tty_color;
871 /* Do a quick check to see if the returned descriptor is
872 actually _exactly_ equal to COLOR, otherwise we have to
873 lookup STD_COLOR separately. If it's impossible to lookup
874 a standard color, we just give up and use TTY_COLOR. */
875 if ((!STRINGP (XCAR (color_desc))
876 || NILP (Fstring_equal (color, XCAR (color_desc))))
877 && !NILP (Ffboundp (Qtty_color_standard_values)))
879 /* Look up STD_COLOR separately. */
880 rgb = call1 (Qtty_color_standard_values, color);
881 if (! parse_rgb_list (rgb, std_color))
882 return false;
886 return true;
888 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
889 /* We were called early during startup, and the colors are not
890 yet set up in tty-defined-color-alist. Don't return a failure
891 indication, since this produces the annoying "Unable to
892 load color" messages in the *Messages* buffer. */
893 return true;
894 else
895 /* tty-color-desc seems to have returned a bad value. */
896 return false;
899 /* A version of defined_color for non-X frames. */
901 static bool
902 tty_defined_color (struct frame *f, const char *color_name,
903 XColor *color_def, bool alloc)
905 bool status = true;
907 /* Defaults. */
908 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
909 color_def->red = 0;
910 color_def->blue = 0;
911 color_def->green = 0;
913 if (*color_name)
914 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
916 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
918 if (strcmp (color_name, "unspecified-fg") == 0)
919 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
920 else if (strcmp (color_name, "unspecified-bg") == 0)
921 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
924 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
925 status = true;
927 return status;
931 /* Decide if color named COLOR_NAME is valid for the display
932 associated with the frame F; if so, return the rgb values in
933 COLOR_DEF. If ALLOC, allocate a new colormap cell.
935 This does the right thing for any type of frame. */
937 static bool
938 defined_color (struct frame *f, const char *color_name, XColor *color_def,
939 bool alloc)
941 if (!FRAME_WINDOW_P (f))
942 return tty_defined_color (f, color_name, color_def, alloc);
943 #ifdef HAVE_X_WINDOWS
944 else if (FRAME_X_P (f))
945 return x_defined_color (f, color_name, color_def, alloc);
946 #endif
947 #ifdef HAVE_NTGUI
948 else if (FRAME_W32_P (f))
949 return w32_defined_color (f, color_name, color_def, alloc);
950 #endif
951 #ifdef HAVE_NS
952 else if (FRAME_NS_P (f))
953 return ns_defined_color (f, color_name, color_def, alloc, true);
954 #endif
955 else
956 emacs_abort ();
960 /* Given the index IDX of a tty color on frame F, return its name, a
961 Lisp string. */
963 Lisp_Object
964 tty_color_name (struct frame *f, int idx)
966 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
968 Lisp_Object frame;
969 Lisp_Object coldesc;
971 XSETFRAME (frame, f);
972 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
974 if (!NILP (coldesc))
975 return XCAR (coldesc);
977 #ifdef MSDOS
978 /* We can have an MS-DOS frame under -nw for a short window of
979 opportunity before internal_terminal_init is called. DTRT. */
980 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
981 return msdos_stdcolor_name (idx);
982 #endif
984 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
985 return build_string (unspecified_fg);
986 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
987 return build_string (unspecified_bg);
989 return Qunspecified;
993 /* Return true if COLOR_NAME is a shade of gray (or white or
994 black) on frame F.
996 The criterion implemented here is not a terribly sophisticated one. */
998 static bool
999 face_color_gray_p (struct frame *f, const char *color_name)
1001 XColor color;
1002 bool gray_p;
1004 if (defined_color (f, color_name, &color, false))
1005 gray_p = (/* Any color sufficiently close to black counts as gray. */
1006 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1008 ((eabs (color.red - color.green)
1009 < max (color.red, color.green) / 20)
1010 && (eabs (color.green - color.blue)
1011 < max (color.green, color.blue) / 20)
1012 && (eabs (color.blue - color.red)
1013 < max (color.blue, color.red) / 20)));
1014 else
1015 gray_p = false;
1017 return gray_p;
1021 /* Return true if color COLOR_NAME can be displayed on frame F.
1022 BACKGROUND_P means the color will be used as background color. */
1024 static bool
1025 face_color_supported_p (struct frame *f, const char *color_name,
1026 bool background_p)
1028 Lisp_Object frame;
1029 XColor not_used;
1031 XSETFRAME (frame, f);
1032 return
1033 #ifdef HAVE_WINDOW_SYSTEM
1034 FRAME_WINDOW_P (f)
1035 ? (!NILP (Fxw_display_color_p (frame))
1036 || xstrcasecmp (color_name, "black") == 0
1037 || xstrcasecmp (color_name, "white") == 0
1038 || (background_p
1039 && face_color_gray_p (f, color_name))
1040 || (!NILP (Fx_display_grayscale_p (frame))
1041 && face_color_gray_p (f, color_name)))
1043 #endif
1044 tty_defined_color (f, color_name, &not_used, false);
1048 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1049 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1050 FRAME specifies the frame and thus the display for interpreting COLOR.
1051 If FRAME is nil or omitted, use the selected frame. */)
1052 (Lisp_Object color, Lisp_Object frame)
1054 CHECK_STRING (color);
1055 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1056 ? Qt : Qnil);
1060 DEFUN ("color-supported-p", Fcolor_supported_p,
1061 Scolor_supported_p, 1, 3, 0,
1062 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1063 BACKGROUND-P non-nil means COLOR is used as a background.
1064 Otherwise, this function tells whether it can be used as a foreground.
1065 If FRAME is nil or omitted, use the selected frame.
1066 COLOR must be a valid color name. */)
1067 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1069 CHECK_STRING (color);
1070 return (face_color_supported_p (decode_any_frame (frame),
1071 SSDATA (color), !NILP (background_p))
1072 ? Qt : Qnil);
1076 static unsigned long
1077 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1078 enum lface_attribute_index target_index, XColor *color)
1080 eassert (STRINGP (name));
1081 eassert (target_index == LFACE_FOREGROUND_INDEX
1082 || target_index == LFACE_BACKGROUND_INDEX
1083 || target_index == LFACE_UNDERLINE_INDEX
1084 || target_index == LFACE_OVERLINE_INDEX
1085 || target_index == LFACE_STRIKE_THROUGH_INDEX
1086 || target_index == LFACE_BOX_INDEX);
1088 /* if the color map is full, defined_color will return a best match
1089 to the values in an existing cell. */
1090 if (!defined_color (f, SSDATA (name), color, true))
1092 add_to_log ("Unable to load color \"%s\"", name);
1094 switch (target_index)
1096 case LFACE_FOREGROUND_INDEX:
1097 face->foreground_defaulted_p = true;
1098 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1099 break;
1101 case LFACE_BACKGROUND_INDEX:
1102 face->background_defaulted_p = true;
1103 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1104 break;
1106 case LFACE_UNDERLINE_INDEX:
1107 face->underline_defaulted_p = true;
1108 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1109 break;
1111 case LFACE_OVERLINE_INDEX:
1112 face->overline_color_defaulted_p = true;
1113 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1114 break;
1116 case LFACE_STRIKE_THROUGH_INDEX:
1117 face->strike_through_color_defaulted_p = true;
1118 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1119 break;
1121 case LFACE_BOX_INDEX:
1122 face->box_color_defaulted_p = true;
1123 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1124 break;
1126 default:
1127 emacs_abort ();
1130 #ifdef GLYPH_DEBUG
1131 else
1132 ++ncolors_allocated;
1133 #endif
1135 return color->pixel;
1138 /* Load color with name NAME for use by face FACE on frame F.
1139 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1140 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1141 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1142 pixel color. If color cannot be loaded, display a message, and
1143 return the foreground, background or underline color of F, but
1144 record that fact in flags of the face so that we don't try to free
1145 these colors. */
1147 unsigned long
1148 load_color (struct frame *f, struct face *face, Lisp_Object name,
1149 enum lface_attribute_index target_index)
1151 XColor color;
1152 return load_color2 (f, face, name, target_index, &color);
1156 #ifdef HAVE_WINDOW_SYSTEM
1158 #define NEAR_SAME_COLOR_THRESHOLD 30000
1160 /* Load colors for face FACE which is used on frame F. Colors are
1161 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1162 of ATTRS. If the background color specified is not supported on F,
1163 try to emulate gray colors with a stipple from Vface_default_stipple. */
1165 static void
1166 load_face_colors (struct frame *f, struct face *face,
1167 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1169 Lisp_Object fg, bg, dfg;
1170 XColor xfg, xbg;
1172 bg = attrs[LFACE_BACKGROUND_INDEX];
1173 fg = attrs[LFACE_FOREGROUND_INDEX];
1175 /* Swap colors if face is inverse-video. */
1176 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1178 Lisp_Object tmp;
1179 tmp = fg;
1180 fg = bg;
1181 bg = tmp;
1184 /* Check for support for foreground, not for background because
1185 face_color_supported_p is smart enough to know that grays are
1186 "supported" as background because we are supposed to use stipple
1187 for them. */
1188 if (!face_color_supported_p (f, SSDATA (bg), false)
1189 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1191 x_destroy_bitmap (f, face->stipple);
1192 face->stipple = load_pixmap (f, Vface_default_stipple);
1195 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1196 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1198 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1199 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1200 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1202 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1203 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1204 else
1205 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1209 #ifdef HAVE_X_WINDOWS
1211 /* Free color PIXEL on frame F. */
1213 void
1214 unload_color (struct frame *f, unsigned long pixel)
1216 if (pixel != -1)
1218 block_input ();
1219 x_free_colors (f, &pixel, 1);
1220 unblock_input ();
1224 /* Free colors allocated for FACE. */
1226 static void
1227 free_face_colors (struct frame *f, struct face *face)
1229 /* PENDING(NS): need to do something here? */
1231 if (face->colors_copied_bitwise_p)
1232 return;
1234 block_input ();
1236 if (!face->foreground_defaulted_p)
1238 x_free_colors (f, &face->foreground, 1);
1239 IF_DEBUG (--ncolors_allocated);
1242 if (!face->background_defaulted_p)
1244 x_free_colors (f, &face->background, 1);
1245 IF_DEBUG (--ncolors_allocated);
1248 if (face->underline_p
1249 && !face->underline_defaulted_p)
1251 x_free_colors (f, &face->underline_color, 1);
1252 IF_DEBUG (--ncolors_allocated);
1255 if (face->overline_p
1256 && !face->overline_color_defaulted_p)
1258 x_free_colors (f, &face->overline_color, 1);
1259 IF_DEBUG (--ncolors_allocated);
1262 if (face->strike_through_p
1263 && !face->strike_through_color_defaulted_p)
1265 x_free_colors (f, &face->strike_through_color, 1);
1266 IF_DEBUG (--ncolors_allocated);
1269 if (face->box != FACE_NO_BOX
1270 && !face->box_color_defaulted_p)
1272 x_free_colors (f, &face->box_color, 1);
1273 IF_DEBUG (--ncolors_allocated);
1276 unblock_input ();
1279 #endif /* HAVE_X_WINDOWS */
1281 #endif /* HAVE_WINDOW_SYSTEM */
1285 /***********************************************************************
1286 XLFD Font Names
1287 ***********************************************************************/
1289 /* An enumerator for each field of an XLFD font name. */
1291 enum xlfd_field
1293 XLFD_FOUNDRY,
1294 XLFD_FAMILY,
1295 XLFD_WEIGHT,
1296 XLFD_SLANT,
1297 XLFD_SWIDTH,
1298 XLFD_ADSTYLE,
1299 XLFD_PIXEL_SIZE,
1300 XLFD_POINT_SIZE,
1301 XLFD_RESX,
1302 XLFD_RESY,
1303 XLFD_SPACING,
1304 XLFD_AVGWIDTH,
1305 XLFD_REGISTRY,
1306 XLFD_ENCODING,
1307 XLFD_LAST
1310 /* An enumerator for each possible slant value of a font. Taken from
1311 the XLFD specification. */
1313 enum xlfd_slant
1315 XLFD_SLANT_UNKNOWN,
1316 XLFD_SLANT_ROMAN,
1317 XLFD_SLANT_ITALIC,
1318 XLFD_SLANT_OBLIQUE,
1319 XLFD_SLANT_REVERSE_ITALIC,
1320 XLFD_SLANT_REVERSE_OBLIQUE,
1321 XLFD_SLANT_OTHER
1324 /* Relative font weight according to XLFD documentation. */
1326 enum xlfd_weight
1328 XLFD_WEIGHT_UNKNOWN,
1329 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1330 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1331 XLFD_WEIGHT_LIGHT, /* 30 */
1332 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1333 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1334 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1335 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1336 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1337 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1340 /* Relative proportionate width. */
1342 enum xlfd_swidth
1344 XLFD_SWIDTH_UNKNOWN,
1345 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1346 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1347 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1348 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1349 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1350 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1351 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1352 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1353 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1356 /* Order by which font selection chooses fonts. The default values
1357 mean `first, find a best match for the font width, then for the
1358 font height, then for weight, then for slant.' This variable can be
1359 set via set-face-font-sort-order. */
1361 static int font_sort_order[4];
1363 #ifdef HAVE_WINDOW_SYSTEM
1365 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1367 static int
1368 compare_fonts_by_sort_order (const void *v1, const void *v2)
1370 Lisp_Object const *p1 = v1;
1371 Lisp_Object const *p2 = v2;
1372 Lisp_Object font1 = *p1;
1373 Lisp_Object font2 = *p2;
1374 int i;
1376 for (i = 0; i < FONT_SIZE_INDEX; i++)
1378 enum font_property_index idx = font_props_for_sorting[i];
1379 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1380 int result;
1382 if (idx <= FONT_REGISTRY_INDEX)
1384 if (STRINGP (val1))
1385 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1386 else
1387 result = STRINGP (val2) ? 1 : 0;
1389 else
1391 if (INTEGERP (val1))
1392 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1393 ? XINT (val1) > XINT (val2)
1394 : -1);
1395 else
1396 result = INTEGERP (val2) ? 1 : 0;
1398 if (result)
1399 return result;
1401 return 0;
1404 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1405 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1406 If FAMILY is omitted or nil, list all families.
1407 Otherwise, FAMILY must be a string, possibly containing wildcards
1408 `?' and `*'.
1409 If FRAME is omitted or nil, use the selected frame.
1410 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1411 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1412 FAMILY is the font family name. POINT-SIZE is the size of the
1413 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1414 width, weight and slant of the font. These symbols are the same as for
1415 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1416 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1417 giving the registry and encoding of the font.
1418 The result list is sorted according to the current setting of
1419 the face font sort order. */)
1420 (Lisp_Object family, Lisp_Object frame)
1422 Lisp_Object font_spec, list, *drivers, vec;
1423 struct frame *f = decode_live_frame (frame);
1424 ptrdiff_t i, nfonts;
1425 EMACS_INT ndrivers;
1426 Lisp_Object result;
1427 USE_SAFE_ALLOCA;
1429 font_spec = Ffont_spec (0, NULL);
1430 if (!NILP (family))
1432 CHECK_STRING (family);
1433 font_parse_family_registry (family, Qnil, font_spec);
1436 list = font_list_entities (f, font_spec);
1437 if (NILP (list))
1438 return Qnil;
1440 /* Sort the font entities. */
1441 for (i = 0; i < 4; i++)
1442 switch (font_sort_order[i])
1444 case XLFD_SWIDTH:
1445 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1446 case XLFD_POINT_SIZE:
1447 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1448 case XLFD_WEIGHT:
1449 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1450 default:
1451 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1453 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1454 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1455 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1456 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1458 ndrivers = XINT (Flength (list));
1459 SAFE_ALLOCA_LISP (drivers, ndrivers);
1460 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1461 drivers[i] = XCAR (list);
1462 vec = Fvconcat (ndrivers, drivers);
1463 nfonts = ASIZE (vec);
1465 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1466 compare_fonts_by_sort_order);
1468 result = Qnil;
1469 for (i = nfonts - 1; i >= 0; --i)
1471 Lisp_Object font = AREF (vec, i);
1472 Lisp_Object v = make_uninit_vector (8);
1473 int point;
1474 Lisp_Object spacing;
1476 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1477 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1478 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1479 FRAME_RES_Y (f));
1480 ASET (v, 2, make_number (point));
1481 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1482 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1483 spacing = Ffont_get (font, QCspacing);
1484 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1485 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1486 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1488 result = Fcons (v, result);
1491 SAFE_FREE ();
1492 return result;
1495 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1496 doc: /* Return a list of the names of available fonts matching PATTERN.
1497 If optional arguments FACE and FRAME are specified, return only fonts
1498 the same size as FACE on FRAME.
1500 PATTERN should be a string containing a font name in the XLFD,
1501 Fontconfig, or GTK format. A font name given in the XLFD format may
1502 contain wildcard characters:
1503 the * character matches any substring, and
1504 the ? character matches any single character.
1505 PATTERN is case-insensitive.
1507 The return value is a list of strings, suitable as arguments to
1508 `set-face-font'.
1510 Fonts Emacs can't use may or may not be excluded
1511 even if they match PATTERN and FACE.
1512 The optional fourth argument MAXIMUM sets a limit on how many
1513 fonts to match. The first MAXIMUM fonts are reported.
1514 The optional fifth argument WIDTH, if specified, is a number of columns
1515 occupied by a character of a font. In that case, return only fonts
1516 the WIDTH times as wide as FACE on FRAME. */)
1517 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1518 Lisp_Object maximum, Lisp_Object width)
1520 struct frame *f;
1521 int size, avgwidth;
1523 check_window_system (NULL);
1524 CHECK_STRING (pattern);
1526 if (! NILP (maximum))
1527 CHECK_NATNUM (maximum);
1529 if (!NILP (width))
1530 CHECK_NUMBER (width);
1532 /* We can't simply call decode_window_system_frame because
1533 this function may be called before any frame is created. */
1534 f = decode_live_frame (frame);
1535 if (! FRAME_WINDOW_P (f))
1537 /* Perhaps we have not yet created any frame. */
1538 f = NULL;
1539 frame = Qnil;
1540 face = Qnil;
1542 else
1543 XSETFRAME (frame, f);
1545 /* Determine the width standard for comparison with the fonts we find. */
1547 if (NILP (face))
1548 size = 0;
1549 else
1551 /* This is of limited utility since it works with character
1552 widths. Keep it for compatibility. --gerd. */
1553 int face_id = lookup_named_face (f, face, false);
1554 struct face *width_face = FACE_FROM_ID_OR_NULL (f, face_id);
1556 if (width_face && width_face->font)
1558 size = width_face->font->pixel_size;
1559 avgwidth = width_face->font->average_width;
1561 else
1563 size = FRAME_FONT (f)->pixel_size;
1564 avgwidth = FRAME_FONT (f)->average_width;
1566 if (!NILP (width))
1567 avgwidth *= XINT (width);
1570 Lisp_Object font_spec = font_spec_from_name (pattern);
1571 if (!FONTP (font_spec))
1572 signal_error ("Invalid font name", pattern);
1574 if (size)
1576 Ffont_put (font_spec, QCsize, make_number (size));
1577 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1579 Lisp_Object fonts = Flist_fonts (font_spec, frame, maximum, font_spec);
1580 for (Lisp_Object tail = fonts; CONSP (tail); tail = XCDR (tail))
1582 Lisp_Object font_entity;
1584 font_entity = XCAR (tail);
1585 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1586 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1587 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1589 /* This is a scalable font. For backward compatibility,
1590 we set the specified size. */
1591 font_entity = copy_font_spec (font_entity);
1592 ASET (font_entity, FONT_SIZE_INDEX,
1593 AREF (font_spec, FONT_SIZE_INDEX));
1595 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1597 if (NILP (frame))
1598 /* We don't have to check fontsets. */
1599 return fonts;
1600 Lisp_Object fontsets = list_fontsets (f, pattern, size);
1601 return CALLN (Fnconc, fonts, fontsets);
1604 #endif /* HAVE_WINDOW_SYSTEM */
1607 /***********************************************************************
1608 Lisp Faces
1609 ***********************************************************************/
1611 /* Access face attributes of face LFACE, a Lisp vector. */
1613 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1614 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1615 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1616 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1617 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1618 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1619 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1620 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1621 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1622 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1623 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1624 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1625 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1626 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1627 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1628 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1629 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1630 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1631 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1633 /* True if LFACE is a Lisp face. A Lisp face is a vector of size
1634 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1636 #define LFACEP(LFACE) \
1637 (VECTORP (LFACE) \
1638 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1639 && EQ (AREF (LFACE, 0), Qface))
1642 #ifdef GLYPH_DEBUG
1644 /* Check consistency of Lisp face attribute vector ATTRS. */
1646 static void
1647 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1649 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1650 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1651 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1652 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1653 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1654 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1655 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1656 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1657 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1658 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1659 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1660 || NUMBERP (attrs[LFACE_HEIGHT_INDEX])
1661 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1662 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1663 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1664 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1665 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1666 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1667 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1668 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1669 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1670 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1671 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1672 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1673 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1674 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1675 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1676 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1677 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1678 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1679 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1680 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1681 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1682 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1683 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1684 || STRINGP (attrs[LFACE_BOX_INDEX])
1685 || INTEGERP (attrs[LFACE_BOX_INDEX])
1686 || CONSP (attrs[LFACE_BOX_INDEX]));
1687 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1688 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1689 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1690 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1691 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1692 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1693 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1694 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1695 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1696 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1697 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1698 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1699 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1700 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1701 || NILP (attrs[LFACE_INHERIT_INDEX])
1702 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1703 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1704 #ifdef HAVE_WINDOW_SYSTEM
1705 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1706 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1707 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1708 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1709 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1710 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1711 || FONTP (attrs[LFACE_FONT_INDEX]));
1712 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1713 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1714 || NILP (attrs[LFACE_FONTSET_INDEX]));
1715 #endif
1719 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1721 static void
1722 check_lface (Lisp_Object lface)
1724 if (!NILP (lface))
1726 eassert (LFACEP (lface));
1727 check_lface_attrs (XVECTOR (lface)->contents);
1731 #else /* not GLYPH_DEBUG */
1733 #define check_lface_attrs(attrs) (void) 0
1734 #define check_lface(lface) (void) 0
1736 #endif /* GLYPH_DEBUG */
1740 /* Face-merge cycle checking. */
1742 enum named_merge_point_kind
1744 NAMED_MERGE_POINT_NORMAL,
1745 NAMED_MERGE_POINT_REMAP
1748 /* A `named merge point' is simply a point during face-merging where we
1749 look up a face by name. We keep a stack of which named lookups we're
1750 currently processing so that we can easily detect cycles, using a
1751 linked- list of struct named_merge_point structures, typically
1752 allocated on the stack frame of the named lookup functions which are
1753 active (so no consing is required). */
1754 struct named_merge_point
1756 Lisp_Object face_name;
1757 enum named_merge_point_kind named_merge_point_kind;
1758 struct named_merge_point *prev;
1762 /* If a face merging cycle is detected for FACE_NAME, return false,
1763 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1764 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1765 pointed to by NAMED_MERGE_POINTS, and return true. */
1767 static bool
1768 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1769 Lisp_Object face_name,
1770 enum named_merge_point_kind named_merge_point_kind,
1771 struct named_merge_point **named_merge_points)
1773 struct named_merge_point *prev;
1775 for (prev = *named_merge_points; prev; prev = prev->prev)
1776 if (EQ (face_name, prev->face_name))
1778 if (prev->named_merge_point_kind == named_merge_point_kind)
1779 /* A cycle, so fail. */
1780 return false;
1781 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1782 /* A remap `hides ' any previous normal merge points
1783 (because the remap means that it's actually different face),
1784 so as we know the current merge point must be normal, we
1785 can just assume it's OK. */
1786 break;
1789 new_named_merge_point->face_name = face_name;
1790 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1791 new_named_merge_point->prev = *named_merge_points;
1793 *named_merge_points = new_named_merge_point;
1795 return true;
1799 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1800 to make it a symbol. If FACE_NAME is an alias for another face,
1801 return that face's name.
1803 Return default face in case of errors. */
1805 static Lisp_Object
1806 resolve_face_name (Lisp_Object face_name, bool signal_p)
1808 Lisp_Object orig_face;
1809 Lisp_Object tortoise, hare;
1811 if (STRINGP (face_name))
1812 face_name = Fintern (face_name, Qnil);
1814 if (NILP (face_name) || !SYMBOLP (face_name))
1815 return face_name;
1817 orig_face = face_name;
1818 tortoise = hare = face_name;
1820 while (true)
1822 face_name = hare;
1823 hare = Fget (hare, Qface_alias);
1824 if (NILP (hare) || !SYMBOLP (hare))
1825 break;
1827 face_name = hare;
1828 hare = Fget (hare, Qface_alias);
1829 if (NILP (hare) || !SYMBOLP (hare))
1830 break;
1832 tortoise = Fget (tortoise, Qface_alias);
1833 if (EQ (hare, tortoise))
1835 if (signal_p)
1836 xsignal1 (Qcircular_list, orig_face);
1837 return Qdefault;
1841 return face_name;
1845 /* Return the face definition of FACE_NAME on frame F. F null means
1846 return the definition for new frames. FACE_NAME may be a string or
1847 a symbol (apparently Emacs 20.2 allowed strings as face names in
1848 face text properties; Ediff uses that).
1849 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1850 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1851 static Lisp_Object
1852 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1853 bool signal_p)
1855 Lisp_Object lface;
1857 if (f)
1858 lface = assq_no_quit (face_name, f->face_alist);
1859 else
1860 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1862 if (CONSP (lface))
1863 lface = XCDR (lface);
1864 else if (signal_p)
1865 signal_error ("Invalid face", face_name);
1867 check_lface (lface);
1869 return lface;
1872 /* Return the face definition of FACE_NAME on frame F. F null means
1873 return the definition for new frames. FACE_NAME may be a string or
1874 a symbol (apparently Emacs 20.2 allowed strings as face names in
1875 face text properties; Ediff uses that). If FACE_NAME is an alias
1876 for another face, return that face's definition.
1877 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1878 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1879 static Lisp_Object
1880 lface_from_face_name (struct frame *f, Lisp_Object face_name, bool signal_p)
1882 face_name = resolve_face_name (face_name, signal_p);
1883 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1887 /* Get face attributes of face FACE_NAME from frame-local faces on
1888 frame F. Store the resulting attributes in ATTRS which must point
1889 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE.
1890 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1891 Otherwise, return true iff FACE_NAME is a face. */
1893 static bool
1894 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
1895 Lisp_Object attrs[LFACE_VECTOR_SIZE],
1896 bool signal_p)
1898 Lisp_Object lface;
1900 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
1902 if (! NILP (lface))
1903 memcpy (attrs, XVECTOR (lface)->contents,
1904 LFACE_VECTOR_SIZE * sizeof *attrs);
1906 return !NILP (lface);
1909 /* Get face attributes of face FACE_NAME from frame-local faces on frame
1910 F. Store the resulting attributes in ATTRS which must point to a
1911 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
1912 alias for another face, use that face's definition.
1913 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1914 Otherwise, return true iff FACE_NAME is a face. */
1916 static bool
1917 get_lface_attributes (struct frame *f, Lisp_Object face_name,
1918 Lisp_Object attrs[LFACE_VECTOR_SIZE], bool signal_p,
1919 struct named_merge_point *named_merge_points)
1921 Lisp_Object face_remapping;
1923 face_name = resolve_face_name (face_name, signal_p);
1925 /* See if SYMBOL has been remapped to some other face (usually this
1926 is done buffer-locally). */
1927 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
1928 if (CONSP (face_remapping))
1930 struct named_merge_point named_merge_point;
1932 if (push_named_merge_point (&named_merge_point,
1933 face_name, NAMED_MERGE_POINT_REMAP,
1934 &named_merge_points))
1936 int i;
1938 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1939 attrs[i] = Qunspecified;
1941 return merge_face_ref (f, XCDR (face_remapping), attrs,
1942 signal_p, named_merge_points);
1946 /* Default case, no remapping. */
1947 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
1951 /* True iff all attributes in face attribute vector ATTRS are
1952 specified, i.e. are non-nil. */
1954 static bool
1955 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1957 int i;
1959 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1960 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
1961 && i != LFACE_DISTANT_FOREGROUND_INDEX)
1962 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
1963 break;
1965 return i == LFACE_VECTOR_SIZE;
1968 #ifdef HAVE_WINDOW_SYSTEM
1970 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
1971 If FORCE_P, set only unspecified attributes of LFACE. The
1972 exception is `font' attribute. It is set to FONT_OBJECT regardless
1973 of FORCE_P. */
1975 static void
1976 set_lface_from_font (struct frame *f, Lisp_Object lface,
1977 Lisp_Object font_object, bool force_p)
1979 Lisp_Object val;
1980 struct font *font = XFONT_OBJECT (font_object);
1982 /* Set attributes only if unspecified, otherwise face defaults for
1983 new frames would never take effect. If the font doesn't have a
1984 specific property, set a normal value for that. */
1986 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
1988 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
1990 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
1993 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
1995 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
1997 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2000 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2002 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2004 eassert (pt > 0);
2005 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2008 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2010 val = FONT_WEIGHT_FOR_FACE (font_object);
2011 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2013 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2015 val = FONT_SLANT_FOR_FACE (font_object);
2016 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2018 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2020 val = FONT_WIDTH_FOR_FACE (font_object);
2021 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2024 ASET (lface, LFACE_FONT_INDEX, font_object);
2027 #endif /* HAVE_WINDOW_SYSTEM */
2030 /* Merges the face height FROM with the face height TO, and returns the
2031 merged height. If FROM is an invalid height, then INVALID is
2032 returned instead. FROM and TO may be either absolute face heights or
2033 `relative' heights; the returned value is always an absolute height
2034 unless both FROM and TO are relative. */
2036 static Lisp_Object
2037 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2039 Lisp_Object result = invalid;
2041 if (INTEGERP (from))
2042 /* FROM is absolute, just use it as is. */
2043 result = from;
2044 else if (FLOATP (from))
2045 /* FROM is a scale, use it to adjust TO. */
2047 if (INTEGERP (to))
2048 /* relative X absolute => absolute */
2049 result = make_number (XFLOAT_DATA (from) * XINT (to));
2050 else if (FLOATP (to))
2051 /* relative X relative => relative */
2052 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2053 else if (UNSPECIFIEDP (to))
2054 result = from;
2056 else if (FUNCTIONP (from))
2057 /* FROM is a function, which use to adjust TO. */
2059 /* Call function with current height as argument.
2060 From is the new height. */
2061 result = safe_call1 (from, to);
2063 /* Ensure that if TO was absolute, so is the result. */
2064 if (INTEGERP (to) && !INTEGERP (result))
2065 result = invalid;
2068 return result;
2072 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2073 store the resulting attributes in TO, which must be already be
2074 completely specified and contain only absolute attributes. Every
2075 specified attribute of FROM overrides the corresponding attribute of
2076 TO; relative attributes in FROM are merged with the absolute value in
2077 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2078 loops in face inheritance/remapping; it should be 0 when called from
2079 other places. */
2081 static void
2082 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2083 struct named_merge_point *named_merge_points)
2085 int i;
2086 Lisp_Object font = Qnil;
2088 /* If FROM inherits from some other faces, merge their attributes into
2089 TO before merging FROM's direct attributes. Note that an :inherit
2090 attribute of `unspecified' is the same as one of nil; we never
2091 merge :inherit attributes, so nil is more correct, but lots of
2092 other code uses `unspecified' as a generic value for face attributes. */
2093 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2094 && !NILP (from[LFACE_INHERIT_INDEX]))
2095 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, false, named_merge_points);
2097 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2099 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2100 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2101 else
2102 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2103 to[LFACE_FONT_INDEX] = font;
2106 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2107 if (!UNSPECIFIEDP (from[i]))
2109 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2111 to[i] = merge_face_heights (from[i], to[i], to[i]);
2112 font_clear_prop (to, FONT_SIZE_INDEX);
2114 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2116 to[i] = from[i];
2117 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2118 font_clear_prop (to,
2119 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2120 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2121 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2122 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2123 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2124 : FONT_SLANT_INDEX));
2128 /* If FROM specifies a font spec, make its contents take precedence
2129 over :family and other attributes. This is needed for face
2130 remapping using :font to work. */
2132 if (!NILP (font))
2134 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2135 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2136 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2137 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2138 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2139 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2140 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2141 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2142 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2143 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2144 ASET (font, FONT_SIZE_INDEX, Qnil);
2147 /* TO is always an absolute face, which should inherit from nothing.
2148 We blindly copy the :inherit attribute above and fix it up here. */
2149 to[LFACE_INHERIT_INDEX] = Qnil;
2152 /* Merge the named face FACE_NAME on frame F, into the vector of face
2153 attributes TO. Use NAMED_MERGE_POINTS to detect loops in face
2154 inheritance. Return true if FACE_NAME is a valid face name and
2155 merging succeeded. */
2157 static bool
2158 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2159 struct named_merge_point *named_merge_points)
2161 struct named_merge_point named_merge_point;
2163 if (push_named_merge_point (&named_merge_point,
2164 face_name, NAMED_MERGE_POINT_NORMAL,
2165 &named_merge_points))
2167 Lisp_Object from[LFACE_VECTOR_SIZE];
2168 bool ok = get_lface_attributes (f, face_name, from, false,
2169 named_merge_points);
2171 if (ok)
2172 merge_face_vectors (f, from, to, named_merge_points);
2174 return ok;
2176 else
2177 return false;
2181 /* Merge face attributes from the lisp `face reference' FACE_REF on
2182 frame F into the face attribute vector TO. If ERR_MSGS,
2183 problems with FACE_REF cause an error message to be shown. Return
2184 true if no errors occurred (regardless of the value of ERR_MSGS).
2185 Use NAMED_MERGE_POINTS to detect loops in face inheritance or
2186 list structure; it may be 0 for most callers.
2188 FACE_REF may be a single face specification or a list of such
2189 specifications. Each face specification can be:
2191 1. A symbol or string naming a Lisp face.
2193 2. A property list of the form (KEYWORD VALUE ...) where each
2194 KEYWORD is a face attribute name, and value is an appropriate value
2195 for that attribute.
2197 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2198 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2199 for compatibility with 20.2.
2201 Face specifications earlier in lists take precedence over later
2202 specifications. */
2204 static bool
2205 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2206 bool err_msgs, struct named_merge_point *named_merge_points)
2208 bool ok = true; /* Succeed without an error? */
2210 if (CONSP (face_ref))
2212 Lisp_Object first = XCAR (face_ref);
2214 if (EQ (first, Qforeground_color)
2215 || EQ (first, Qbackground_color))
2217 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2218 . COLOR). COLOR must be a string. */
2219 Lisp_Object color_name = XCDR (face_ref);
2220 Lisp_Object color = first;
2222 if (STRINGP (color_name))
2224 if (EQ (color, Qforeground_color))
2225 to[LFACE_FOREGROUND_INDEX] = color_name;
2226 else
2227 to[LFACE_BACKGROUND_INDEX] = color_name;
2229 else
2231 if (err_msgs)
2232 add_to_log ("Invalid face color %S", color_name);
2233 ok = false;
2236 else if (SYMBOLP (first)
2237 && *SDATA (SYMBOL_NAME (first)) == ':')
2239 /* Assume this is the property list form. */
2240 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2242 Lisp_Object keyword = XCAR (face_ref);
2243 Lisp_Object value = XCAR (XCDR (face_ref));
2244 bool err = false;
2246 /* Specifying `unspecified' is a no-op. */
2247 if (EQ (value, Qunspecified))
2249 else if (EQ (keyword, QCfamily))
2251 if (STRINGP (value))
2253 to[LFACE_FAMILY_INDEX] = value;
2254 font_clear_prop (to, FONT_FAMILY_INDEX);
2256 else
2257 err = true;
2259 else if (EQ (keyword, QCfoundry))
2261 if (STRINGP (value))
2263 to[LFACE_FOUNDRY_INDEX] = value;
2264 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2266 else
2267 err = true;
2269 else if (EQ (keyword, QCheight))
2271 Lisp_Object new_height =
2272 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2274 if (! NILP (new_height))
2276 to[LFACE_HEIGHT_INDEX] = new_height;
2277 font_clear_prop (to, FONT_SIZE_INDEX);
2279 else
2280 err = true;
2282 else if (EQ (keyword, QCweight))
2284 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2286 to[LFACE_WEIGHT_INDEX] = value;
2287 font_clear_prop (to, FONT_WEIGHT_INDEX);
2289 else
2290 err = true;
2292 else if (EQ (keyword, QCslant))
2294 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2296 to[LFACE_SLANT_INDEX] = value;
2297 font_clear_prop (to, FONT_SLANT_INDEX);
2299 else
2300 err = true;
2302 else if (EQ (keyword, QCunderline))
2304 if (EQ (value, Qt)
2305 || NILP (value)
2306 || STRINGP (value)
2307 || CONSP (value))
2308 to[LFACE_UNDERLINE_INDEX] = value;
2309 else
2310 err = true;
2312 else if (EQ (keyword, QCoverline))
2314 if (EQ (value, Qt)
2315 || NILP (value)
2316 || STRINGP (value))
2317 to[LFACE_OVERLINE_INDEX] = value;
2318 else
2319 err = true;
2321 else if (EQ (keyword, QCstrike_through))
2323 if (EQ (value, Qt)
2324 || NILP (value)
2325 || STRINGP (value))
2326 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2327 else
2328 err = true;
2330 else if (EQ (keyword, QCbox))
2332 if (EQ (value, Qt))
2333 value = make_number (1);
2334 if (INTEGERP (value)
2335 || STRINGP (value)
2336 || CONSP (value)
2337 || NILP (value))
2338 to[LFACE_BOX_INDEX] = value;
2339 else
2340 err = true;
2342 else if (EQ (keyword, QCinverse_video)
2343 || EQ (keyword, QCreverse_video))
2345 if (EQ (value, Qt) || NILP (value))
2346 to[LFACE_INVERSE_INDEX] = value;
2347 else
2348 err = true;
2350 else if (EQ (keyword, QCforeground))
2352 if (STRINGP (value))
2353 to[LFACE_FOREGROUND_INDEX] = value;
2354 else
2355 err = true;
2357 else if (EQ (keyword, QCdistant_foreground))
2359 if (STRINGP (value))
2360 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2361 else
2362 err = true;
2364 else if (EQ (keyword, QCbackground))
2366 if (STRINGP (value))
2367 to[LFACE_BACKGROUND_INDEX] = value;
2368 else
2369 err = true;
2371 else if (EQ (keyword, QCstipple))
2373 #if defined (HAVE_WINDOW_SYSTEM)
2374 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2375 if (!NILP (pixmap_p))
2376 to[LFACE_STIPPLE_INDEX] = value;
2377 else
2378 err = true;
2379 #endif /* HAVE_WINDOW_SYSTEM */
2381 else if (EQ (keyword, QCwidth))
2383 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2385 to[LFACE_SWIDTH_INDEX] = value;
2386 font_clear_prop (to, FONT_WIDTH_INDEX);
2388 else
2389 err = true;
2391 else if (EQ (keyword, QCfont))
2393 if (FONTP (value))
2394 to[LFACE_FONT_INDEX] = value;
2395 else
2396 err = true;
2398 else if (EQ (keyword, QCinherit))
2400 /* This is not really very useful; it's just like a
2401 normal face reference. */
2402 if (! merge_face_ref (f, value, to,
2403 err_msgs, named_merge_points))
2404 err = true;
2406 else
2407 err = true;
2409 if (err)
2411 add_to_log ("Invalid face attribute %S %S", keyword, value);
2412 ok = false;
2415 face_ref = XCDR (XCDR (face_ref));
2418 else
2420 /* This is a list of face refs. Those at the beginning of the
2421 list take precedence over what follows, so we have to merge
2422 from the end backwards. */
2423 Lisp_Object next = XCDR (face_ref);
2425 if (! NILP (next))
2426 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2428 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2429 ok = false;
2432 else
2434 /* FACE_REF ought to be a face name. */
2435 ok = merge_named_face (f, face_ref, to, named_merge_points);
2436 if (!ok && err_msgs)
2437 add_to_log ("Invalid face reference: %s", face_ref);
2440 return ok;
2444 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2445 Sinternal_make_lisp_face, 1, 2, 0,
2446 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2447 If FACE was not known as a face before, create a new one.
2448 If optional argument FRAME is specified, make a frame-local face
2449 for that frame. Otherwise operate on the global face definition.
2450 Value is a vector of face attributes. */)
2451 (Lisp_Object face, Lisp_Object frame)
2453 Lisp_Object global_lface, lface;
2454 struct frame *f;
2455 int i;
2457 CHECK_SYMBOL (face);
2458 global_lface = lface_from_face_name (NULL, face, false);
2460 if (!NILP (frame))
2462 CHECK_LIVE_FRAME (frame);
2463 f = XFRAME (frame);
2464 lface = lface_from_face_name (f, face, false);
2466 else
2467 f = NULL, lface = Qnil;
2469 /* Add a global definition if there is none. */
2470 if (NILP (global_lface))
2472 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2473 Qunspecified);
2474 ASET (global_lface, 0, Qface);
2475 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2476 Vface_new_frame_defaults);
2478 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2479 face id to Lisp face is given by the vector lface_id_to_name.
2480 The mapping from Lisp face to Lisp face id is given by the
2481 property `face' of the Lisp face name. */
2482 if (next_lface_id == lface_id_to_name_size)
2483 lface_id_to_name =
2484 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2485 sizeof *lface_id_to_name);
2487 lface_id_to_name[next_lface_id] = face;
2488 Fput (face, Qface, make_number (next_lface_id));
2489 ++next_lface_id;
2491 else if (f == NULL)
2492 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2493 ASET (global_lface, i, Qunspecified);
2495 /* Add a frame-local definition. */
2496 if (f)
2498 if (NILP (lface))
2500 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2501 Qunspecified);
2502 ASET (lface, 0, Qface);
2503 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2505 else
2506 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2507 ASET (lface, i, Qunspecified);
2509 else
2510 lface = global_lface;
2512 /* Changing a named face means that all realized faces depending on
2513 that face are invalid. Since we cannot tell which realized faces
2514 depend on the face, make sure they are all removed. This is done
2515 by setting face_change. The next call to init_iterator will then
2516 free realized faces. */
2517 if (NILP (Fget (face, Qface_no_inherit)))
2519 if (f)
2521 f->face_change = true;
2522 fset_redisplay (f);
2524 else
2526 face_change = true;
2527 windows_or_buffers_changed = 54;
2531 eassert (LFACEP (lface));
2532 check_lface (lface);
2533 return lface;
2537 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2538 Sinternal_lisp_face_p, 1, 2, 0,
2539 doc: /* Return non-nil if FACE names a face.
2540 FACE should be a symbol or string.
2541 If optional second argument FRAME is non-nil, check for the
2542 existence of a frame-local face with name FACE on that frame.
2543 Otherwise check for the existence of a global face. */)
2544 (Lisp_Object face, Lisp_Object frame)
2546 Lisp_Object lface;
2548 face = resolve_face_name (face, true);
2550 if (!NILP (frame))
2552 CHECK_LIVE_FRAME (frame);
2553 lface = lface_from_face_name (XFRAME (frame), face, false);
2555 else
2556 lface = lface_from_face_name (NULL, face, false);
2558 return lface;
2562 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2563 Sinternal_copy_lisp_face, 4, 4, 0,
2564 doc: /* Copy face FROM to TO.
2565 If FRAME is t, copy the global face definition of FROM.
2566 Otherwise, copy the frame-local definition of FROM on FRAME.
2567 If NEW-FRAME is a frame, copy that data into the frame-local
2568 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2569 FRAME controls where the data is copied to.
2571 The value is TO. */)
2572 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2574 Lisp_Object lface, copy;
2575 struct frame *f;
2577 CHECK_SYMBOL (from);
2578 CHECK_SYMBOL (to);
2580 if (EQ (frame, Qt))
2582 /* Copy global definition of FROM. We don't make copies of
2583 strings etc. because 20.2 didn't do it either. */
2584 lface = lface_from_face_name (NULL, from, true);
2585 copy = Finternal_make_lisp_face (to, Qnil);
2586 f = NULL;
2588 else
2590 /* Copy frame-local definition of FROM. */
2591 if (NILP (new_frame))
2592 new_frame = frame;
2593 CHECK_LIVE_FRAME (frame);
2594 CHECK_LIVE_FRAME (new_frame);
2595 lface = lface_from_face_name (XFRAME (frame), from, true);
2596 copy = Finternal_make_lisp_face (to, new_frame);
2597 f = XFRAME (new_frame);
2600 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2602 /* Changing a named face means that all realized faces depending on
2603 that face are invalid. Since we cannot tell which realized faces
2604 depend on the face, make sure they are all removed. This is done
2605 by setting face_change. The next call to init_iterator will then
2606 free realized faces. */
2607 if (NILP (Fget (to, Qface_no_inherit)))
2609 if (f)
2611 f->face_change = true;
2612 fset_redisplay (f);
2614 else
2616 face_change = true;
2617 windows_or_buffers_changed = 55;
2621 return to;
2625 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2626 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2627 doc: /* Set attribute ATTR of FACE to VALUE.
2628 FRAME being a frame means change the face on that frame.
2629 FRAME nil means change the face of the selected frame.
2630 FRAME t means change the default for new frames.
2631 FRAME 0 means change the face on all frames, and change the default
2632 for new frames. */)
2633 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2635 Lisp_Object lface;
2636 Lisp_Object old_value = Qnil;
2637 /* Set one of enum font_property_index (> 0) if ATTR is one of
2638 font-related attributes other than QCfont and QCfontset. */
2639 enum font_property_index prop_index = 0;
2640 struct frame *f;
2642 CHECK_SYMBOL (face);
2643 CHECK_SYMBOL (attr);
2645 face = resolve_face_name (face, true);
2647 /* If FRAME is 0, change face on all frames, and change the
2648 default for new frames. */
2649 if (INTEGERP (frame) && XINT (frame) == 0)
2651 Lisp_Object tail;
2652 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2653 FOR_EACH_FRAME (tail, frame)
2654 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2655 return face;
2658 /* Set lface to the Lisp attribute vector of FACE. */
2659 if (EQ (frame, Qt))
2661 f = NULL;
2662 lface = lface_from_face_name (NULL, face, true);
2664 /* When updating face-new-frame-defaults, we put :ignore-defface
2665 where the caller wants `unspecified'. This forces the frame
2666 defaults to ignore the defface value. Otherwise, the defface
2667 will take effect, which is generally not what is intended.
2668 The value of that attribute will be inherited from some other
2669 face during face merging. See internal_merge_in_global_face. */
2670 if (UNSPECIFIEDP (value))
2671 value = QCignore_defface;
2673 else
2675 if (NILP (frame))
2676 frame = selected_frame;
2678 CHECK_LIVE_FRAME (frame);
2679 f = XFRAME (frame);
2681 lface = lface_from_face_name (f, face, false);
2683 /* If a frame-local face doesn't exist yet, create one. */
2684 if (NILP (lface))
2685 lface = Finternal_make_lisp_face (face, frame);
2688 if (EQ (attr, QCfamily))
2690 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2692 CHECK_STRING (value);
2693 if (SCHARS (value) == 0)
2694 signal_error ("Invalid face family", value);
2696 old_value = LFACE_FAMILY (lface);
2697 ASET (lface, LFACE_FAMILY_INDEX, value);
2698 prop_index = FONT_FAMILY_INDEX;
2700 else if (EQ (attr, QCfoundry))
2702 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2704 CHECK_STRING (value);
2705 if (SCHARS (value) == 0)
2706 signal_error ("Invalid face foundry", value);
2708 old_value = LFACE_FOUNDRY (lface);
2709 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2710 prop_index = FONT_FOUNDRY_INDEX;
2712 else if (EQ (attr, QCheight))
2714 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2716 if (EQ (face, Qdefault))
2718 /* The default face must have an absolute size. */
2719 if (!INTEGERP (value) || XINT (value) <= 0)
2720 signal_error ("Default face height not absolute and positive",
2721 value);
2723 else
2725 /* For non-default faces, do a test merge with a random
2726 height to see if VALUE's ok. */
2727 Lisp_Object test = merge_face_heights (value,
2728 make_number (10),
2729 Qnil);
2730 if (!INTEGERP (test) || XINT (test) <= 0)
2731 signal_error ("Face height does not produce a positive integer",
2732 value);
2736 old_value = LFACE_HEIGHT (lface);
2737 ASET (lface, LFACE_HEIGHT_INDEX, value);
2738 prop_index = FONT_SIZE_INDEX;
2740 else if (EQ (attr, QCweight))
2742 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2744 CHECK_SYMBOL (value);
2745 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2746 signal_error ("Invalid face weight", value);
2748 old_value = LFACE_WEIGHT (lface);
2749 ASET (lface, LFACE_WEIGHT_INDEX, value);
2750 prop_index = FONT_WEIGHT_INDEX;
2752 else if (EQ (attr, QCslant))
2754 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2756 CHECK_SYMBOL (value);
2757 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2758 signal_error ("Invalid face slant", value);
2760 old_value = LFACE_SLANT (lface);
2761 ASET (lface, LFACE_SLANT_INDEX, value);
2762 prop_index = FONT_SLANT_INDEX;
2764 else if (EQ (attr, QCunderline))
2766 bool valid_p = false;
2768 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2769 valid_p = true;
2770 else if (NILP (value) || EQ (value, Qt))
2771 valid_p = true;
2772 else if (STRINGP (value) && SCHARS (value) > 0)
2773 valid_p = true;
2774 else if (CONSP (value))
2776 Lisp_Object key, val, list;
2778 list = value;
2779 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2780 (defface foo '((t :underline 'foo) "doc")
2781 Maybe this is intentional, maybe it isn't.
2782 Non-nil symbols other than t are not documented as being valid.
2783 Eg compare with inverse-video, which explicitly rejects them.
2785 valid_p = true;
2787 while (!NILP (CAR_SAFE(list)))
2789 key = CAR_SAFE (list);
2790 list = CDR_SAFE (list);
2791 val = CAR_SAFE (list);
2792 list = CDR_SAFE (list);
2794 if (NILP (key) || NILP (val))
2796 valid_p = false;
2797 break;
2800 else if (EQ (key, QCcolor)
2801 && !(EQ (val, Qforeground_color)
2802 || (STRINGP (val) && SCHARS (val) > 0)))
2804 valid_p = false;
2805 break;
2808 else if (EQ (key, QCstyle)
2809 && !(EQ (val, Qline) || EQ (val, Qwave)))
2811 valid_p = false;
2812 break;
2817 if (!valid_p)
2818 signal_error ("Invalid face underline", value);
2820 old_value = LFACE_UNDERLINE (lface);
2821 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2823 else if (EQ (attr, QCoverline))
2825 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2826 if ((SYMBOLP (value)
2827 && !EQ (value, Qt)
2828 && !EQ (value, Qnil))
2829 /* Overline color. */
2830 || (STRINGP (value)
2831 && SCHARS (value) == 0))
2832 signal_error ("Invalid face overline", value);
2834 old_value = LFACE_OVERLINE (lface);
2835 ASET (lface, LFACE_OVERLINE_INDEX, value);
2837 else if (EQ (attr, QCstrike_through))
2839 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2840 if ((SYMBOLP (value)
2841 && !EQ (value, Qt)
2842 && !EQ (value, Qnil))
2843 /* Strike-through color. */
2844 || (STRINGP (value)
2845 && SCHARS (value) == 0))
2846 signal_error ("Invalid face strike-through", value);
2848 old_value = LFACE_STRIKE_THROUGH (lface);
2849 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2851 else if (EQ (attr, QCbox))
2853 bool valid_p;
2855 /* Allow t meaning a simple box of width 1 in foreground color
2856 of the face. */
2857 if (EQ (value, Qt))
2858 value = make_number (1);
2860 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2861 valid_p = true;
2862 else if (NILP (value))
2863 valid_p = true;
2864 else if (INTEGERP (value))
2865 valid_p = XINT (value) != 0;
2866 else if (STRINGP (value))
2867 valid_p = SCHARS (value) > 0;
2868 else if (CONSP (value))
2870 Lisp_Object tem;
2872 tem = value;
2873 while (CONSP (tem))
2875 Lisp_Object k, v;
2877 k = XCAR (tem);
2878 tem = XCDR (tem);
2879 if (!CONSP (tem))
2880 break;
2881 v = XCAR (tem);
2882 tem = XCDR (tem);
2884 if (EQ (k, QCline_width))
2886 if (!INTEGERP (v) || XINT (v) == 0)
2887 break;
2889 else if (EQ (k, QCcolor))
2891 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2892 break;
2894 else if (EQ (k, QCstyle))
2896 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2897 break;
2899 else
2900 break;
2903 valid_p = NILP (tem);
2905 else
2906 valid_p = false;
2908 if (!valid_p)
2909 signal_error ("Invalid face box", value);
2911 old_value = LFACE_BOX (lface);
2912 ASET (lface, LFACE_BOX_INDEX, value);
2914 else if (EQ (attr, QCinverse_video)
2915 || EQ (attr, QCreverse_video))
2917 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2919 CHECK_SYMBOL (value);
2920 if (!EQ (value, Qt) && !NILP (value))
2921 signal_error ("Invalid inverse-video face attribute value", value);
2923 old_value = LFACE_INVERSE (lface);
2924 ASET (lface, LFACE_INVERSE_INDEX, value);
2926 else if (EQ (attr, QCforeground))
2928 /* Compatibility with 20.x. */
2929 if (NILP (value))
2930 value = Qunspecified;
2931 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2933 /* Don't check for valid color names here because it depends
2934 on the frame (display) whether the color will be valid
2935 when the face is realized. */
2936 CHECK_STRING (value);
2937 if (SCHARS (value) == 0)
2938 signal_error ("Empty foreground color value", value);
2940 old_value = LFACE_FOREGROUND (lface);
2941 ASET (lface, LFACE_FOREGROUND_INDEX, value);
2943 else if (EQ (attr, QCdistant_foreground))
2945 /* Compatibility with 20.x. */
2946 if (NILP (value))
2947 value = Qunspecified;
2948 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2950 /* Don't check for valid color names here because it depends
2951 on the frame (display) whether the color will be valid
2952 when the face is realized. */
2953 CHECK_STRING (value);
2954 if (SCHARS (value) == 0)
2955 signal_error ("Empty distant-foreground color value", value);
2957 old_value = LFACE_DISTANT_FOREGROUND (lface);
2958 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
2960 else if (EQ (attr, QCbackground))
2962 /* Compatibility with 20.x. */
2963 if (NILP (value))
2964 value = Qunspecified;
2965 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2967 /* Don't check for valid color names here because it depends
2968 on the frame (display) whether the color will be valid
2969 when the face is realized. */
2970 CHECK_STRING (value);
2971 if (SCHARS (value) == 0)
2972 signal_error ("Empty background color value", value);
2974 old_value = LFACE_BACKGROUND (lface);
2975 ASET (lface, LFACE_BACKGROUND_INDEX, value);
2977 else if (EQ (attr, QCstipple))
2979 #if defined (HAVE_WINDOW_SYSTEM)
2980 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
2981 && !NILP (value)
2982 && NILP (Fbitmap_spec_p (value)))
2983 signal_error ("Invalid stipple attribute", value);
2984 old_value = LFACE_STIPPLE (lface);
2985 ASET (lface, LFACE_STIPPLE_INDEX, value);
2986 #endif /* HAVE_WINDOW_SYSTEM */
2988 else if (EQ (attr, QCwidth))
2990 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2992 CHECK_SYMBOL (value);
2993 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
2994 signal_error ("Invalid face width", value);
2996 old_value = LFACE_SWIDTH (lface);
2997 ASET (lface, LFACE_SWIDTH_INDEX, value);
2998 prop_index = FONT_WIDTH_INDEX;
3000 else if (EQ (attr, QCfont))
3002 #ifdef HAVE_WINDOW_SYSTEM
3003 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3005 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3007 struct frame *f1;
3009 old_value = LFACE_FONT (lface);
3010 if (! FONTP (value))
3012 if (STRINGP (value))
3014 Lisp_Object name = value;
3015 int fontset = fs_query_fontset (name, 0);
3017 if (fontset >= 0)
3018 name = fontset_ascii (fontset);
3019 value = font_spec_from_name (name);
3020 if (!FONTP (value))
3021 signal_error ("Invalid font name", name);
3023 else
3024 signal_error ("Invalid font or font-spec", value);
3026 if (EQ (frame, Qt))
3027 f1 = XFRAME (selected_frame);
3028 else
3029 f1 = XFRAME (frame);
3031 /* FIXME:
3032 If frame is t, and selected frame is a tty frame, the font
3033 can't be realized. An improvement would be to loop over frames
3034 for a non-tty frame and use that. See discussion in Bug#18573.
3035 For a daemon, frame may be an initial frame (Bug#18869). */
3036 if (FRAME_WINDOW_P (f1))
3038 if (! FONT_OBJECT_P (value))
3040 Lisp_Object *attrs = XVECTOR (lface)->contents;
3041 Lisp_Object font_object;
3043 font_object = font_load_for_lface (f1, attrs, value);
3044 if (NILP (font_object))
3045 signal_error ("Font not available", value);
3046 value = font_object;
3048 set_lface_from_font (f1, lface, value, true);
3049 f1->face_change = 1;
3052 else
3053 ASET (lface, LFACE_FONT_INDEX, value);
3055 #endif /* HAVE_WINDOW_SYSTEM */
3057 else if (EQ (attr, QCfontset))
3059 #ifdef HAVE_WINDOW_SYSTEM
3060 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3062 Lisp_Object tmp;
3064 old_value = LFACE_FONTSET (lface);
3065 tmp = Fquery_fontset (value, Qnil);
3066 if (NILP (tmp))
3067 signal_error ("Invalid fontset name", value);
3068 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3070 #endif /* HAVE_WINDOW_SYSTEM */
3072 else if (EQ (attr, QCinherit))
3074 Lisp_Object tail;
3075 if (SYMBOLP (value))
3076 tail = Qnil;
3077 else
3078 for (tail = value; CONSP (tail); tail = XCDR (tail))
3079 if (!SYMBOLP (XCAR (tail)))
3080 break;
3081 if (NILP (tail))
3082 ASET (lface, LFACE_INHERIT_INDEX, value);
3083 else
3084 signal_error ("Invalid face inheritance", value);
3086 else if (EQ (attr, QCbold))
3088 old_value = LFACE_WEIGHT (lface);
3089 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3090 prop_index = FONT_WEIGHT_INDEX;
3092 else if (EQ (attr, QCitalic))
3094 attr = QCslant;
3095 old_value = LFACE_SLANT (lface);
3096 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3097 prop_index = FONT_SLANT_INDEX;
3099 else
3100 signal_error ("Invalid face attribute name", attr);
3102 if (prop_index)
3104 /* If a font-related attribute other than QCfont and QCfontset
3105 is specified, and if the original QCfont attribute has a font
3106 (font-spec or font-object), set the corresponding property in
3107 the font to nil so that the font selector doesn't think that
3108 the attribute is mandatory. Also, clear the average
3109 width. */
3110 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3113 /* Changing a named face means that all realized faces depending on
3114 that face are invalid. Since we cannot tell which realized faces
3115 depend on the face, make sure they are all removed. This is done
3116 by setting face_change. The next call to init_iterator will then
3117 free realized faces. */
3118 if (!EQ (frame, Qt)
3119 && NILP (Fget (face, Qface_no_inherit))
3120 && NILP (Fequal (old_value, value)))
3122 f->face_change = true;
3123 fset_redisplay (f);
3126 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3127 && NILP (Fequal (old_value, value)))
3129 Lisp_Object param;
3131 param = Qnil;
3133 if (EQ (face, Qdefault))
3135 #ifdef HAVE_WINDOW_SYSTEM
3136 /* Changed font-related attributes of the `default' face are
3137 reflected in changed `font' frame parameters. */
3138 if (FRAMEP (frame)
3139 && (prop_index || EQ (attr, QCfont))
3140 && lface_fully_specified_p (XVECTOR (lface)->contents))
3141 set_font_frame_param (frame, lface);
3142 else
3143 #endif /* HAVE_WINDOW_SYSTEM */
3145 if (EQ (attr, QCforeground))
3146 param = Qforeground_color;
3147 else if (EQ (attr, QCbackground))
3148 param = Qbackground_color;
3150 #ifdef HAVE_WINDOW_SYSTEM
3151 #ifndef HAVE_NTGUI
3152 else if (EQ (face, Qscroll_bar))
3154 /* Changing the colors of `scroll-bar' sets frame parameters
3155 `scroll-bar-foreground' and `scroll-bar-background'. */
3156 if (EQ (attr, QCforeground))
3157 param = Qscroll_bar_foreground;
3158 else if (EQ (attr, QCbackground))
3159 param = Qscroll_bar_background;
3161 #endif /* not HAVE_NTGUI */
3162 else if (EQ (face, Qborder))
3164 /* Changing background color of `border' sets frame parameter
3165 `border-color'. */
3166 if (EQ (attr, QCbackground))
3167 param = Qborder_color;
3169 else if (EQ (face, Qcursor))
3171 /* Changing background color of `cursor' sets frame parameter
3172 `cursor-color'. */
3173 if (EQ (attr, QCbackground))
3174 param = Qcursor_color;
3176 else if (EQ (face, Qmouse))
3178 /* Changing background color of `mouse' sets frame parameter
3179 `mouse-color'. */
3180 if (EQ (attr, QCbackground))
3181 param = Qmouse_color;
3183 #endif /* HAVE_WINDOW_SYSTEM */
3184 else if (EQ (face, Qmenu))
3186 /* Indicate that we have to update the menu bar when realizing
3187 faces on FRAME. FRAME t change the default for new frames.
3188 We do this by setting the flag in new face caches. */
3189 if (FRAMEP (frame))
3191 struct frame *f = XFRAME (frame);
3192 if (FRAME_FACE_CACHE (f) == NULL)
3193 FRAME_FACE_CACHE (f) = make_face_cache (f);
3194 FRAME_FACE_CACHE (f)->menu_face_changed_p = true;
3196 else
3197 menu_face_changed_default = true;
3200 if (!NILP (param))
3202 if (EQ (frame, Qt))
3203 /* Update `default-frame-alist', which is used for new frames. */
3205 store_in_alist (&Vdefault_frame_alist, param, value);
3207 else
3208 /* Update the current frame's parameters. */
3210 Lisp_Object cons;
3211 cons = XCAR (Vparam_value_alist);
3212 XSETCAR (cons, param);
3213 XSETCDR (cons, value);
3214 Fmodify_frame_parameters (frame, Vparam_value_alist);
3219 return face;
3223 /* Update the corresponding face when frame parameter PARAM on frame F
3224 has been assigned the value NEW_VALUE. */
3226 void
3227 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3228 Lisp_Object new_value)
3230 Lisp_Object face = Qnil;
3231 Lisp_Object lface;
3233 /* If there are no faces yet, give up. This is the case when called
3234 from Fx_create_frame, and we do the necessary things later in
3235 face-set-after-frame-defaults. */
3236 if (NILP (f->face_alist))
3237 return;
3239 if (EQ (param, Qforeground_color))
3241 face = Qdefault;
3242 lface = lface_from_face_name (f, face, true);
3243 ASET (lface, LFACE_FOREGROUND_INDEX,
3244 (STRINGP (new_value) ? new_value : Qunspecified));
3245 realize_basic_faces (f);
3247 else if (EQ (param, Qbackground_color))
3249 Lisp_Object frame;
3251 /* Changing the background color might change the background
3252 mode, so that we have to load new defface specs.
3253 Call frame-set-background-mode to do that. */
3254 XSETFRAME (frame, f);
3255 call1 (Qframe_set_background_mode, frame);
3257 face = Qdefault;
3258 lface = lface_from_face_name (f, face, true);
3259 ASET (lface, LFACE_BACKGROUND_INDEX,
3260 (STRINGP (new_value) ? new_value : Qunspecified));
3261 realize_basic_faces (f);
3263 #ifdef HAVE_WINDOW_SYSTEM
3264 else if (EQ (param, Qborder_color))
3266 face = Qborder;
3267 lface = lface_from_face_name (f, face, true);
3268 ASET (lface, LFACE_BACKGROUND_INDEX,
3269 (STRINGP (new_value) ? new_value : Qunspecified));
3271 else if (EQ (param, Qcursor_color))
3273 face = Qcursor;
3274 lface = lface_from_face_name (f, face, true);
3275 ASET (lface, LFACE_BACKGROUND_INDEX,
3276 (STRINGP (new_value) ? new_value : Qunspecified));
3278 else if (EQ (param, Qmouse_color))
3280 face = Qmouse;
3281 lface = lface_from_face_name (f, face, true);
3282 ASET (lface, LFACE_BACKGROUND_INDEX,
3283 (STRINGP (new_value) ? new_value : Qunspecified));
3285 #endif
3287 /* Changing a named face means that all realized faces depending on
3288 that face are invalid. Since we cannot tell which realized faces
3289 depend on the face, make sure they are all removed. This is done
3290 by setting face_change. The next call to init_iterator will then
3291 free realized faces. */
3292 if (!NILP (face)
3293 && NILP (Fget (face, Qface_no_inherit)))
3295 f->face_change = true;
3296 fset_redisplay (f);
3301 #ifdef HAVE_WINDOW_SYSTEM
3303 /* Set the `font' frame parameter of FRAME determined from the
3304 font-object set in `default' face attributes LFACE. */
3306 static void
3307 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3309 struct frame *f = XFRAME (frame);
3310 Lisp_Object font;
3312 if (FRAME_WINDOW_P (f)
3313 /* Don't do anything if the font is `unspecified'. This can
3314 happen during frame creation. */
3315 && (font = LFACE_FONT (lface),
3316 ! UNSPECIFIEDP (font)))
3318 if (FONT_SPEC_P (font))
3320 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3321 if (NILP (font))
3322 return;
3323 ASET (lface, LFACE_FONT_INDEX, font);
3325 f->default_face_done_p = false;
3326 AUTO_FRAME_ARG (arg, Qfont, font);
3327 Fmodify_frame_parameters (frame, arg);
3331 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3332 Sinternal_face_x_get_resource, 2, 3, 0,
3333 doc: /* Get the value of X resource RESOURCE, class CLASS.
3334 Returned value is for the display of frame FRAME. If FRAME is not
3335 specified or nil, use selected frame. This function exists because
3336 ordinary `x-get-resource' doesn't take a frame argument. */)
3337 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3339 Lisp_Object value = Qnil;
3340 struct frame *f;
3342 CHECK_STRING (resource);
3343 CHECK_STRING (class);
3344 f = decode_live_frame (frame);
3345 block_input ();
3346 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3347 resource, class, Qnil, Qnil);
3348 unblock_input ();
3349 return value;
3353 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3354 If VALUE is "on" or "true", return t. If VALUE is "off" or
3355 "false", return nil. Otherwise, if SIGNAL_P, signal an
3356 error; if !SIGNAL_P, return 0. */
3358 static Lisp_Object
3359 face_boolean_x_resource_value (Lisp_Object value, bool signal_p)
3361 Lisp_Object result = make_number (0);
3363 eassert (STRINGP (value));
3365 if (xstrcasecmp (SSDATA (value), "on") == 0
3366 || xstrcasecmp (SSDATA (value), "true") == 0)
3367 result = Qt;
3368 else if (xstrcasecmp (SSDATA (value), "off") == 0
3369 || xstrcasecmp (SSDATA (value), "false") == 0)
3370 result = Qnil;
3371 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3372 result = Qunspecified;
3373 else if (signal_p)
3374 signal_error ("Invalid face attribute value from X resource", value);
3376 return result;
3380 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3381 Finternal_set_lisp_face_attribute_from_resource,
3382 Sinternal_set_lisp_face_attribute_from_resource,
3383 3, 4, 0, doc: /* */)
3384 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3386 CHECK_SYMBOL (face);
3387 CHECK_SYMBOL (attr);
3388 CHECK_STRING (value);
3390 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3391 value = Qunspecified;
3392 else if (EQ (attr, QCheight))
3394 value = Fstring_to_number (value, make_number (10));
3395 if (XINT (value) <= 0)
3396 signal_error ("Invalid face height from X resource", value);
3398 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3399 value = face_boolean_x_resource_value (value, true);
3400 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3401 value = intern (SSDATA (value));
3402 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3403 value = face_boolean_x_resource_value (value, true);
3404 else if (EQ (attr, QCunderline)
3405 || EQ (attr, QCoverline)
3406 || EQ (attr, QCstrike_through))
3408 Lisp_Object boolean_value;
3410 /* If the result of face_boolean_x_resource_value is t or nil,
3411 VALUE does NOT specify a color. */
3412 boolean_value = face_boolean_x_resource_value (value, false);
3413 if (SYMBOLP (boolean_value))
3414 value = boolean_value;
3416 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3417 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3419 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3422 #endif /* HAVE_WINDOW_SYSTEM */
3425 /***********************************************************************
3426 Menu face
3427 ***********************************************************************/
3429 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3431 /* Make menus on frame F appear as specified by the `menu' face. */
3433 static void
3434 x_update_menu_appearance (struct frame *f)
3436 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3437 XrmDatabase rdb;
3439 if (dpyinfo
3440 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3441 rdb != NULL))
3443 char line[512];
3444 char *buf = line;
3445 ptrdiff_t bufsize = sizeof line;
3446 Lisp_Object lface = lface_from_face_name (f, Qmenu, true);
3447 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3448 const char *myname = SSDATA (Vx_resource_name);
3449 bool changed_p = false;
3450 #ifdef USE_MOTIF
3451 const char *popup_path = "popup_menu";
3452 #else
3453 const char *popup_path = "menu.popup";
3454 #endif
3456 if (STRINGP (LFACE_FOREGROUND (lface)))
3458 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3459 myname, popup_path,
3460 SDATA (LFACE_FOREGROUND (lface)));
3461 XrmPutLineResource (&rdb, line);
3462 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3463 myname, SDATA (LFACE_FOREGROUND (lface)));
3464 XrmPutLineResource (&rdb, line);
3465 changed_p = true;
3468 if (STRINGP (LFACE_BACKGROUND (lface)))
3470 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3471 myname, popup_path,
3472 SDATA (LFACE_BACKGROUND (lface)));
3473 XrmPutLineResource (&rdb, line);
3475 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3476 myname, SDATA (LFACE_BACKGROUND (lface)));
3477 XrmPutLineResource (&rdb, line);
3478 changed_p = true;
3481 if (face->font
3482 /* On Solaris 5.8, it's been reported that the `menu' face
3483 can be unspecified here, during startup. Why this
3484 happens remains unknown. -- cyd */
3485 && FONTP (LFACE_FONT (lface))
3486 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3487 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3488 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3489 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3490 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3491 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3493 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3494 #ifdef USE_MOTIF
3495 const char *suffix = "List";
3496 bool motif = true;
3497 #else
3498 #if defined HAVE_X_I18N
3500 const char *suffix = "Set";
3501 #else
3502 const char *suffix = "";
3503 #endif
3504 bool motif = false;
3505 #endif
3507 if (! NILP (xlfd))
3509 #if defined HAVE_X_I18N
3510 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3511 #else
3512 char *fontsetname = SSDATA (xlfd);
3513 #endif
3514 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3515 myname, suffix, fontsetname);
3516 XrmPutLineResource (&rdb, line);
3518 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3519 myname, popup_path, suffix, fontsetname);
3520 XrmPutLineResource (&rdb, line);
3521 changed_p = true;
3522 if (fontsetname != SSDATA (xlfd))
3523 xfree (fontsetname);
3527 if (changed_p && f->output_data.x->menubar_widget)
3528 free_frame_menubar (f);
3530 if (buf != line)
3531 xfree (buf);
3535 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3538 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3539 Sface_attribute_relative_p,
3540 2, 2, 0,
3541 doc: /* Check whether a face attribute value is relative.
3542 Specifically, this function returns t if the attribute ATTRIBUTE
3543 with the value VALUE is relative.
3545 A relative value is one that doesn't entirely override whatever is
3546 inherited from another face. For most possible attributes,
3547 the only relative value that users see is `unspecified'.
3548 However, for :height, floating point values are also relative. */
3549 attributes: const)
3550 (Lisp_Object attribute, Lisp_Object value)
3552 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3553 return Qt;
3554 else if (EQ (attribute, QCheight))
3555 return INTEGERP (value) ? Qnil : Qt;
3556 else
3557 return Qnil;
3560 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3561 3, 3, 0,
3562 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3563 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3564 the result will be absolute, otherwise it will be relative. */)
3565 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3567 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3568 return value2;
3569 else if (EQ (attribute, QCheight))
3570 return merge_face_heights (value1, value2, value1);
3571 else
3572 return value1;
3576 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3577 Sinternal_get_lisp_face_attribute,
3578 2, 3, 0,
3579 doc: /* Return face attribute KEYWORD of face SYMBOL.
3580 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3581 face attribute name, signal an error.
3582 If the optional argument FRAME is given, report on face SYMBOL in that
3583 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3584 frames). If FRAME is omitted or nil, use the selected frame. */)
3585 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3587 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3588 Lisp_Object lface = lface_from_face_name (f, symbol, true), value = Qnil;
3590 CHECK_SYMBOL (symbol);
3591 CHECK_SYMBOL (keyword);
3593 if (EQ (keyword, QCfamily))
3594 value = LFACE_FAMILY (lface);
3595 else if (EQ (keyword, QCfoundry))
3596 value = LFACE_FOUNDRY (lface);
3597 else if (EQ (keyword, QCheight))
3598 value = LFACE_HEIGHT (lface);
3599 else if (EQ (keyword, QCweight))
3600 value = LFACE_WEIGHT (lface);
3601 else if (EQ (keyword, QCslant))
3602 value = LFACE_SLANT (lface);
3603 else if (EQ (keyword, QCunderline))
3604 value = LFACE_UNDERLINE (lface);
3605 else if (EQ (keyword, QCoverline))
3606 value = LFACE_OVERLINE (lface);
3607 else if (EQ (keyword, QCstrike_through))
3608 value = LFACE_STRIKE_THROUGH (lface);
3609 else if (EQ (keyword, QCbox))
3610 value = LFACE_BOX (lface);
3611 else if (EQ (keyword, QCinverse_video)
3612 || EQ (keyword, QCreverse_video))
3613 value = LFACE_INVERSE (lface);
3614 else if (EQ (keyword, QCforeground))
3615 value = LFACE_FOREGROUND (lface);
3616 else if (EQ (keyword, QCdistant_foreground))
3617 value = LFACE_DISTANT_FOREGROUND (lface);
3618 else if (EQ (keyword, QCbackground))
3619 value = LFACE_BACKGROUND (lface);
3620 else if (EQ (keyword, QCstipple))
3621 value = LFACE_STIPPLE (lface);
3622 else if (EQ (keyword, QCwidth))
3623 value = LFACE_SWIDTH (lface);
3624 else if (EQ (keyword, QCinherit))
3625 value = LFACE_INHERIT (lface);
3626 else if (EQ (keyword, QCfont))
3627 value = LFACE_FONT (lface);
3628 else if (EQ (keyword, QCfontset))
3629 value = LFACE_FONTSET (lface);
3630 else
3631 signal_error ("Invalid face attribute name", keyword);
3633 if (IGNORE_DEFFACE_P (value))
3634 return Qunspecified;
3636 return value;
3640 DEFUN ("internal-lisp-face-attribute-values",
3641 Finternal_lisp_face_attribute_values,
3642 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3643 doc: /* Return a list of valid discrete values for face attribute ATTR.
3644 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3645 (Lisp_Object attr)
3647 Lisp_Object result = Qnil;
3649 CHECK_SYMBOL (attr);
3651 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3652 || EQ (attr, QCstrike_through)
3653 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3654 result = list2 (Qt, Qnil);
3656 return result;
3660 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3661 Sinternal_merge_in_global_face, 2, 2, 0,
3662 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3663 Default face attributes override any local face attributes. */)
3664 (Lisp_Object face, Lisp_Object frame)
3666 int i;
3667 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3668 struct frame *f = XFRAME (frame);
3670 CHECK_LIVE_FRAME (frame);
3671 global_lface = lface_from_face_name (NULL, face, true);
3672 local_lface = lface_from_face_name (f, face, false);
3673 if (NILP (local_lface))
3674 local_lface = Finternal_make_lisp_face (face, frame);
3676 /* Make every specified global attribute override the local one.
3677 BEWARE!! This is only used from `face-set-after-frame-default' where
3678 the local frame is defined from default specs in `face-defface-spec'
3679 and those should be overridden by global settings. Hence the strange
3680 "global before local" priority. */
3681 lvec = XVECTOR (local_lface)->contents;
3682 gvec = XVECTOR (global_lface)->contents;
3683 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3684 if (IGNORE_DEFFACE_P (gvec[i]))
3685 ASET (local_lface, i, Qunspecified);
3686 else if (! UNSPECIFIEDP (gvec[i]))
3687 ASET (local_lface, i, AREF (global_lface, i));
3689 /* If the default face was changed, update the face cache and the
3690 `font' frame parameter. */
3691 if (EQ (face, Qdefault))
3693 struct face_cache *c = FRAME_FACE_CACHE (f);
3694 struct face *newface, *oldface = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
3695 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3697 /* This can be NULL (e.g., in batch mode). */
3698 if (oldface)
3700 /* Ensure that the face vector is fully specified by merging
3701 the previously-cached vector. */
3702 memcpy (attrs, oldface->lface, sizeof attrs);
3703 merge_face_vectors (f, lvec, attrs, 0);
3704 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3705 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3707 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3708 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3709 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3710 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3711 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3712 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3713 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3714 && newface->font)
3716 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3717 AUTO_FRAME_ARG (arg, Qfont, name);
3718 Fmodify_frame_parameters (frame, arg);
3721 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3723 AUTO_FRAME_ARG (arg, Qforeground_color,
3724 gvec[LFACE_FOREGROUND_INDEX]);
3725 Fmodify_frame_parameters (frame, arg);
3728 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3730 AUTO_FRAME_ARG (arg, Qbackground_color,
3731 gvec[LFACE_BACKGROUND_INDEX]);
3732 Fmodify_frame_parameters (frame, arg);
3737 return Qnil;
3741 /* The following function is implemented for compatibility with 20.2.
3742 The function is used in x-resolve-fonts when it is asked to
3743 return fonts with the same size as the font of a face. This is
3744 done in fontset.el. */
3746 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3747 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3748 The font name is, by default, for ASCII characters.
3749 If the optional argument FRAME is given, report on face FACE in that frame.
3750 If FRAME is t, report on the defaults for face FACE (for new frames).
3751 The font default for a face is either nil, or a list
3752 of the form (bold), (italic) or (bold italic).
3753 If FRAME is omitted or nil, use the selected frame. And, in this case,
3754 if the optional third argument CHARACTER is given,
3755 return the font name used for CHARACTER. */)
3756 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3758 if (EQ (frame, Qt))
3760 Lisp_Object result = Qnil;
3761 Lisp_Object lface = lface_from_face_name (NULL, face, true);
3763 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3764 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3765 result = Fcons (Qbold, result);
3767 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3768 && !EQ (LFACE_SLANT (lface), Qnormal))
3769 result = Fcons (Qitalic, result);
3771 return result;
3773 else
3775 struct frame *f = decode_live_frame (frame);
3776 int face_id = lookup_named_face (f, face, true);
3777 struct face *fface = FACE_FROM_ID_OR_NULL (f, face_id);
3779 if (! fface)
3780 return Qnil;
3781 #ifdef HAVE_WINDOW_SYSTEM
3782 if (FRAME_WINDOW_P (f) && !NILP (character))
3784 CHECK_CHARACTER (character);
3785 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3786 fface = FACE_FROM_ID_OR_NULL (f, face_id);
3788 return ((fface && fface->font)
3789 ? fface->font->props[FONT_NAME_INDEX]
3790 : Qnil);
3791 #else /* !HAVE_WINDOW_SYSTEM */
3792 return build_string (FRAME_MSDOS_P (f)
3793 ? "ms-dos"
3794 : FRAME_W32_P (f) ? "w32term"
3795 :"tty");
3796 #endif
3801 /* Compare face-attribute values v1 and v2 for equality. Value is true if
3802 all attributes are `equal'. Tries to be fast because this function
3803 is called quite often. */
3805 static bool
3806 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3808 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3809 and the other is specified. */
3810 if (XTYPE (v1) != XTYPE (v2))
3811 return false;
3813 if (EQ (v1, v2))
3814 return true;
3816 switch (XTYPE (v1))
3818 case Lisp_String:
3819 if (SBYTES (v1) != SBYTES (v2))
3820 return false;
3822 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3824 case_Lisp_Int:
3825 case Lisp_Symbol:
3826 return false;
3828 default:
3829 return !NILP (Fequal (v1, v2));
3834 /* Compare face vectors V1 and V2 for equality. Value is true if
3835 all attributes are `equal'. Tries to be fast because this function
3836 is called quite often. */
3838 static bool
3839 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3841 int i;
3842 bool equal_p = true;
3844 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3845 equal_p = face_attr_equal_p (v1[i], v2[i]);
3847 return equal_p;
3851 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3852 Sinternal_lisp_face_equal_p, 2, 3, 0,
3853 doc: /* True if FACE1 and FACE2 are equal.
3854 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3855 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3856 If FRAME is omitted or nil, use the selected frame. */)
3857 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3859 bool equal_p;
3860 struct frame *f;
3861 Lisp_Object lface1, lface2;
3863 /* Don't use decode_window_system_frame here because this function
3864 is called before X frames exist. At that time, if FRAME is nil,
3865 selected_frame will be used which is the frame dumped with
3866 Emacs. That frame is not an X frame. */
3867 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3869 lface1 = lface_from_face_name (f, face1, true);
3870 lface2 = lface_from_face_name (f, face2, true);
3871 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3872 XVECTOR (lface2)->contents);
3873 return equal_p ? Qt : Qnil;
3877 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3878 Sinternal_lisp_face_empty_p, 1, 2, 0,
3879 doc: /* True if FACE has no attribute specified.
3880 If the optional argument FRAME is given, report on face FACE in that frame.
3881 If FRAME is t, report on the defaults for face FACE (for new frames).
3882 If FRAME is omitted or nil, use the selected frame. */)
3883 (Lisp_Object face, Lisp_Object frame)
3885 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3886 Lisp_Object lface = lface_from_face_name (f, face, true);
3887 int i;
3889 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3890 if (!UNSPECIFIEDP (AREF (lface, i)))
3891 break;
3893 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3897 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3898 0, 1, 0,
3899 doc: /* Return an alist of frame-local faces defined on FRAME.
3900 For internal use only. */)
3901 (Lisp_Object frame)
3903 return decode_live_frame (frame)->face_alist;
3907 /* Return a hash code for Lisp string STRING with case ignored. Used
3908 below in computing a hash value for a Lisp face. */
3910 static unsigned
3911 hash_string_case_insensitive (Lisp_Object string)
3913 const unsigned char *s;
3914 unsigned hash = 0;
3915 eassert (STRINGP (string));
3916 for (s = SDATA (string); *s; ++s)
3917 hash = (hash << 1) ^ c_tolower (*s);
3918 return hash;
3922 /* Return a hash code for face attribute vector V. */
3924 static unsigned
3925 lface_hash (Lisp_Object *v)
3927 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3928 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3929 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3930 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3931 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3932 ^ XHASH (v[LFACE_SLANT_INDEX])
3933 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3934 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3937 #ifdef HAVE_WINDOW_SYSTEM
3939 /* Return true if LFACE1 and LFACE2 specify the same font (without
3940 considering charsets/registries). They do if they specify the same
3941 family, point size, weight, width, slant, and font. Both
3942 LFACE1 and LFACE2 must be fully-specified. */
3944 static bool
3945 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3947 eassert (lface_fully_specified_p (lface1)
3948 && lface_fully_specified_p (lface2));
3949 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3950 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
3951 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
3952 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
3953 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
3954 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3955 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3956 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
3957 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
3958 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
3959 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
3960 && STRINGP (lface2[LFACE_FONTSET_INDEX])
3961 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
3962 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
3966 #endif /* HAVE_WINDOW_SYSTEM */
3968 /***********************************************************************
3969 Realized Faces
3970 ***********************************************************************/
3972 /* Allocate and return a new realized face for Lisp face attribute
3973 vector ATTR. */
3975 static struct face *
3976 make_realized_face (Lisp_Object *attr)
3978 enum { off = offsetof (struct face, id) };
3979 struct face *face = xmalloc (sizeof *face);
3981 memcpy (face->lface, attr, sizeof face->lface);
3982 memset (&face->id, 0, sizeof *face - off);
3983 face->ascii_face = face;
3985 return face;
3989 /* Free realized face FACE, including its X resources. FACE may
3990 be null. */
3992 static void
3993 free_realized_face (struct frame *f, struct face *face)
3995 if (face)
3997 #ifdef HAVE_WINDOW_SYSTEM
3998 if (FRAME_WINDOW_P (f))
4000 /* Free fontset of FACE if it is ASCII face. */
4001 if (face->fontset >= 0 && face == face->ascii_face)
4002 free_face_fontset (f, face);
4003 if (face->gc)
4005 block_input ();
4006 if (face->font)
4007 font_done_for_face (f, face);
4008 x_free_gc (f, face->gc);
4009 face->gc = 0;
4010 unblock_input ();
4012 #ifdef HAVE_X_WINDOWS
4013 free_face_colors (f, face);
4014 #endif /* HAVE_X_WINDOWS */
4015 x_destroy_bitmap (f, face->stipple);
4017 #endif /* HAVE_WINDOW_SYSTEM */
4019 xfree (face);
4023 #ifdef HAVE_WINDOW_SYSTEM
4025 /* Prepare face FACE for subsequent display on frame F. This must be called
4026 before using X resources of FACE to allocate GCs if they haven't been
4027 allocated yet or have been freed by clearing the face cache. */
4029 void
4030 prepare_face_for_display (struct frame *f, struct face *face)
4032 eassert (FRAME_WINDOW_P (f));
4034 if (face->gc == 0)
4036 XGCValues xgcv;
4037 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4039 xgcv.foreground = face->foreground;
4040 xgcv.background = face->background;
4041 #ifdef HAVE_X_WINDOWS
4042 xgcv.graphics_exposures = False;
4043 #endif
4045 block_input ();
4046 #ifdef HAVE_X_WINDOWS
4047 if (face->stipple)
4049 xgcv.fill_style = FillOpaqueStippled;
4050 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4051 mask |= GCFillStyle | GCStipple;
4053 #endif
4054 face->gc = x_create_gc (f, mask, &xgcv);
4055 if (face->font)
4056 font_prepare_for_face (f, face);
4057 unblock_input ();
4061 #endif /* HAVE_WINDOW_SYSTEM */
4063 /* Returns the `distance' between the colors X and Y. */
4065 static int
4066 color_distance (XColor *x, XColor *y)
4068 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4069 Quoting from that paper:
4071 This formula has results that are very close to L*u*v* (with the
4072 modified lightness curve) and, more importantly, it is a more even
4073 algorithm: it does not have a range of colors where it suddenly
4074 gives far from optimal results.
4076 See <http://www.compuphase.com/cmetric.htm> for more info. */
4078 long r = (x->red - y->red) >> 8;
4079 long g = (x->green - y->green) >> 8;
4080 long b = (x->blue - y->blue) >> 8;
4081 long r_mean = (x->red + y->red) >> 9;
4083 return
4084 (((512 + r_mean) * r * r) >> 8)
4085 + 4 * g * g
4086 + (((767 - r_mean) * b * b) >> 8);
4090 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4091 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4092 COLOR1 and COLOR2 may be either strings containing the color name,
4093 or lists of the form (RED GREEN BLUE).
4094 If FRAME is unspecified or nil, the current frame is used. */)
4095 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4097 struct frame *f = decode_live_frame (frame);
4098 XColor cdef1, cdef2;
4100 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4101 && !(STRINGP (color1)
4102 && defined_color (f, SSDATA (color1), &cdef1, false)))
4103 signal_error ("Invalid color", color1);
4104 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4105 && !(STRINGP (color2)
4106 && defined_color (f, SSDATA (color2), &cdef2, false)))
4107 signal_error ("Invalid color", color2);
4109 return make_number (color_distance (&cdef1, &cdef2));
4113 /***********************************************************************
4114 Face Cache
4115 ***********************************************************************/
4117 /* Return a new face cache for frame F. */
4119 static struct face_cache *
4120 make_face_cache (struct frame *f)
4122 struct face_cache *c = xmalloc (sizeof *c);
4124 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4125 c->size = 50;
4126 c->used = 0;
4127 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4128 c->f = f;
4129 c->menu_face_changed_p = menu_face_changed_default;
4130 return c;
4133 #ifdef HAVE_WINDOW_SYSTEM
4135 /* Clear out all graphics contexts for all realized faces, except for
4136 the basic faces. This should be done from time to time just to avoid
4137 keeping too many graphics contexts that are no longer needed. */
4139 static void
4140 clear_face_gcs (struct face_cache *c)
4142 if (c && FRAME_WINDOW_P (c->f))
4144 int i;
4145 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4147 struct face *face = c->faces_by_id[i];
4148 if (face && face->gc)
4150 block_input ();
4151 if (face->font)
4152 font_done_for_face (c->f, face);
4153 x_free_gc (c->f, face->gc);
4154 face->gc = 0;
4155 unblock_input ();
4161 #endif /* HAVE_WINDOW_SYSTEM */
4163 /* Free all realized faces in face cache C, including basic faces.
4164 C may be null. If faces are freed, make sure the frame's current
4165 matrix is marked invalid, so that a display caused by an expose
4166 event doesn't try to use faces we destroyed. */
4168 static void
4169 free_realized_faces (struct face_cache *c)
4171 if (c && c->used)
4173 int i, size;
4174 struct frame *f = c->f;
4176 /* We must block input here because we can't process X events
4177 safely while only some faces are freed, or when the frame's
4178 current matrix still references freed faces. */
4179 block_input ();
4181 for (i = 0; i < c->used; ++i)
4183 free_realized_face (f, c->faces_by_id[i]);
4184 c->faces_by_id[i] = NULL;
4187 /* Forget the escape-glyph and glyphless-char faces. */
4188 forget_escape_and_glyphless_faces ();
4189 c->used = 0;
4190 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4191 memset (c->buckets, 0, size);
4193 /* Must do a thorough redisplay the next time. Mark current
4194 matrices as invalid because they will reference faces freed
4195 above. This function is also called when a frame is
4196 destroyed. In this case, the root window of F is nil. */
4197 if (WINDOWP (f->root_window))
4199 clear_current_matrices (f);
4200 fset_redisplay (f);
4203 unblock_input ();
4208 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4209 This is done after attributes of a named face have been changed,
4210 because we can't tell which realized faces depend on that face. */
4212 void
4213 free_all_realized_faces (Lisp_Object frame)
4215 if (NILP (frame))
4217 Lisp_Object rest;
4218 FOR_EACH_FRAME (rest, frame)
4219 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4220 windows_or_buffers_changed = 58;
4222 else
4223 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4227 /* Free face cache C and faces in it, including their X resources. */
4229 static void
4230 free_face_cache (struct face_cache *c)
4232 if (c)
4234 free_realized_faces (c);
4235 xfree (c->buckets);
4236 xfree (c->faces_by_id);
4237 xfree (c);
4242 /* Cache realized face FACE in face cache C. HASH is the hash value
4243 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4244 FACE), insert the new face to the beginning of the collision list
4245 of the face hash table of C. Otherwise, add the new face to the
4246 end of the collision list. This way, lookup_face can quickly find
4247 that a requested face is not cached. */
4249 static void
4250 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4252 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4254 face->hash = hash;
4256 if (face->ascii_face != face)
4258 struct face *last = c->buckets[i];
4259 if (last)
4261 while (last->next)
4262 last = last->next;
4263 last->next = face;
4264 face->prev = last;
4265 face->next = NULL;
4267 else
4269 c->buckets[i] = face;
4270 face->prev = face->next = NULL;
4273 else
4275 face->prev = NULL;
4276 face->next = c->buckets[i];
4277 if (face->next)
4278 face->next->prev = face;
4279 c->buckets[i] = face;
4282 /* Find a free slot in C->faces_by_id and use the index of the free
4283 slot as FACE->id. */
4284 for (i = 0; i < c->used; ++i)
4285 if (c->faces_by_id[i] == NULL)
4286 break;
4287 face->id = i;
4289 #ifdef GLYPH_DEBUG
4290 /* Check that FACE got a unique id. */
4292 int j, n;
4293 struct face *face1;
4295 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4296 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4297 if (face1->id == i)
4298 ++n;
4300 eassert (n == 1);
4302 #endif /* GLYPH_DEBUG */
4304 /* Maybe enlarge C->faces_by_id. */
4305 if (i == c->used)
4307 if (c->used == c->size)
4308 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4309 sizeof *c->faces_by_id);
4310 c->used++;
4313 c->faces_by_id[i] = face;
4317 /* Remove face FACE from cache C. */
4319 static void
4320 uncache_face (struct face_cache *c, struct face *face)
4322 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4324 if (face->prev)
4325 face->prev->next = face->next;
4326 else
4327 c->buckets[i] = face->next;
4329 if (face->next)
4330 face->next->prev = face->prev;
4332 c->faces_by_id[face->id] = NULL;
4333 if (face->id == c->used)
4334 --c->used;
4338 /* Look up a realized face with face attributes ATTR in the face cache
4339 of frame F. The face will be used to display ASCII characters.
4340 Value is the ID of the face found. If no suitable face is found,
4341 realize a new one. */
4343 static int
4344 lookup_face (struct frame *f, Lisp_Object *attr)
4346 struct face_cache *cache = FRAME_FACE_CACHE (f);
4347 unsigned hash;
4348 int i;
4349 struct face *face;
4351 eassert (cache != NULL);
4352 check_lface_attrs (attr);
4354 /* Look up ATTR in the face cache. */
4355 hash = lface_hash (attr);
4356 i = hash % FACE_CACHE_BUCKETS_SIZE;
4358 for (face = cache->buckets[i]; face; face = face->next)
4360 if (face->ascii_face != face)
4362 /* There's no more ASCII face. */
4363 face = NULL;
4364 break;
4366 if (face->hash == hash
4367 && lface_equal_p (face->lface, attr))
4368 break;
4371 /* If not found, realize a new face. */
4372 if (face == NULL)
4373 face = realize_face (cache, attr, -1);
4375 #ifdef GLYPH_DEBUG
4376 eassert (face == FACE_FROM_ID_OR_NULL (f, face->id));
4377 #endif /* GLYPH_DEBUG */
4379 return face->id;
4382 #ifdef HAVE_WINDOW_SYSTEM
4383 /* Look up a realized face that has the same attributes as BASE_FACE
4384 except for the font in the face cache of frame F. If FONT-OBJECT
4385 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4386 the face has no font. Value is the ID of the face found. If no
4387 suitable face is found, realize a new one. */
4390 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4392 struct face_cache *cache = FRAME_FACE_CACHE (f);
4393 unsigned hash;
4394 int i;
4395 struct face *face;
4397 eassert (cache != NULL);
4398 base_face = base_face->ascii_face;
4399 hash = lface_hash (base_face->lface);
4400 i = hash % FACE_CACHE_BUCKETS_SIZE;
4402 for (face = cache->buckets[i]; face; face = face->next)
4404 if (face->ascii_face == face)
4405 continue;
4406 if (face->ascii_face == base_face
4407 && face->font == (NILP (font_object) ? NULL
4408 : XFONT_OBJECT (font_object))
4409 && lface_equal_p (face->lface, base_face->lface))
4410 return face->id;
4413 /* If not found, realize a new face. */
4414 face = realize_non_ascii_face (f, font_object, base_face);
4415 return face->id;
4417 #endif /* HAVE_WINDOW_SYSTEM */
4419 /* Return the face id of the realized face for named face SYMBOL on
4420 frame F suitable for displaying ASCII characters. Value is -1 if
4421 the face couldn't be determined, which might happen if the default
4422 face isn't realized and cannot be realized. */
4425 lookup_named_face (struct frame *f, Lisp_Object symbol, bool signal_p)
4427 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4428 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4429 struct face *default_face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
4431 if (default_face == NULL)
4433 if (!realize_basic_faces (f))
4434 return -1;
4435 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4438 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4439 return -1;
4441 memcpy (attrs, default_face->lface, sizeof attrs);
4442 merge_face_vectors (f, symbol_attrs, attrs, 0);
4444 return lookup_face (f, attrs);
4448 /* Return the display face-id of the basic face whose canonical face-id
4449 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4450 basic face has bee remapped via Vface_remapping_alist. This function is
4451 conservative: if something goes wrong, it will simply return FACE_ID
4452 rather than signal an error. */
4455 lookup_basic_face (struct frame *f, int face_id)
4457 Lisp_Object name, mapping;
4458 int remapped_face_id;
4460 if (NILP (Vface_remapping_alist))
4461 return face_id; /* Nothing to do. */
4463 switch (face_id)
4465 case DEFAULT_FACE_ID: name = Qdefault; break;
4466 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4467 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4468 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4469 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4470 case FRINGE_FACE_ID: name = Qfringe; break;
4471 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4472 case BORDER_FACE_ID: name = Qborder; break;
4473 case CURSOR_FACE_ID: name = Qcursor; break;
4474 case MOUSE_FACE_ID: name = Qmouse; break;
4475 case MENU_FACE_ID: name = Qmenu; break;
4477 default:
4478 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4481 /* Do a quick scan through Vface_remapping_alist, and return immediately
4482 if there is no remapping for face NAME. This is just an optimization
4483 for the very common no-remapping case. */
4484 mapping = assq_no_quit (name, Vface_remapping_alist);
4485 if (NILP (mapping))
4486 return face_id; /* Give up. */
4488 /* If there is a remapping entry, lookup the face using NAME, which will
4489 handle the remapping too. */
4490 remapped_face_id = lookup_named_face (f, name, false);
4491 if (remapped_face_id < 0)
4492 return face_id; /* Give up. */
4494 return remapped_face_id;
4498 /* Return a face for charset ASCII that is like the face with id
4499 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4500 STEPS < 0 means larger. Value is the id of the face. */
4503 smaller_face (struct frame *f, int face_id, int steps)
4505 #ifdef HAVE_WINDOW_SYSTEM
4506 struct face *face;
4507 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4508 int pt, last_pt, last_height;
4509 int delta;
4510 int new_face_id;
4511 struct face *new_face;
4513 /* If not called for an X frame, just return the original face. */
4514 if (FRAME_TERMCAP_P (f))
4515 return face_id;
4517 /* Try in increments of 1/2 pt. */
4518 delta = steps < 0 ? 5 : -5;
4519 steps = eabs (steps);
4521 face = FACE_FROM_ID (f, face_id);
4522 memcpy (attrs, face->lface, sizeof attrs);
4523 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4524 new_face_id = face_id;
4525 last_height = FONT_HEIGHT (face->font);
4527 while (steps
4528 && pt + delta > 0
4529 /* Give up if we cannot find a font within 10pt. */
4530 && eabs (last_pt - pt) < 100)
4532 /* Look up a face for a slightly smaller/larger font. */
4533 pt += delta;
4534 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4535 new_face_id = lookup_face (f, attrs);
4536 new_face = FACE_FROM_ID (f, new_face_id);
4538 /* If height changes, count that as one step. */
4539 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4540 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4542 --steps;
4543 last_height = FONT_HEIGHT (new_face->font);
4544 last_pt = pt;
4548 return new_face_id;
4550 #else /* not HAVE_WINDOW_SYSTEM */
4552 return face_id;
4554 #endif /* not HAVE_WINDOW_SYSTEM */
4558 /* Return a face for charset ASCII that is like the face with id
4559 FACE_ID on frame F, but has height HEIGHT. */
4562 face_with_height (struct frame *f, int face_id, int height)
4564 #ifdef HAVE_WINDOW_SYSTEM
4565 struct face *face;
4566 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4568 if (FRAME_TERMCAP_P (f)
4569 || height <= 0)
4570 return face_id;
4572 face = FACE_FROM_ID (f, face_id);
4573 memcpy (attrs, face->lface, sizeof attrs);
4574 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4575 font_clear_prop (attrs, FONT_SIZE_INDEX);
4576 face_id = lookup_face (f, attrs);
4577 #endif /* HAVE_WINDOW_SYSTEM */
4579 return face_id;
4583 /* Return the face id of the realized face for named face SYMBOL on
4584 frame F suitable for displaying ASCII characters, and use
4585 attributes of the face FACE_ID for attributes that aren't
4586 completely specified by SYMBOL. This is like lookup_named_face,
4587 except that the default attributes come from FACE_ID, not from the
4588 default face. FACE_ID is assumed to be already realized. */
4591 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4592 bool signal_p)
4594 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4595 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4596 struct face *default_face;
4598 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4599 return -1;
4601 default_face = FACE_FROM_ID (f, face_id);
4602 memcpy (attrs, default_face->lface, sizeof attrs);
4603 merge_face_vectors (f, symbol_attrs, attrs, 0);
4604 return lookup_face (f, attrs);
4607 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4608 Sface_attributes_as_vector, 1, 1, 0,
4609 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4610 (Lisp_Object plist)
4612 Lisp_Object lface;
4613 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4614 Qunspecified);
4615 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4616 true, 0);
4617 return lface;
4622 /***********************************************************************
4623 Face capability testing
4624 ***********************************************************************/
4627 /* If the distance (as returned by color_distance) between two colors is
4628 less than this, then they are considered the same, for determining
4629 whether a color is supported or not. The range of values is 0-65535. */
4631 #define TTY_SAME_COLOR_THRESHOLD 10000
4633 #ifdef HAVE_WINDOW_SYSTEM
4635 /* Return true if all the face attributes in ATTRS are supported
4636 on the window-system frame F.
4638 The definition of `supported' is somewhat heuristic, but basically means
4639 that a face containing all the attributes in ATTRS, when merged with the
4640 default face for display, can be represented in a way that's
4642 (1) different in appearance than the default face, and
4643 (2) `close in spirit' to what the attributes specify, if not exact. */
4645 static bool
4646 x_supports_face_attributes_p (struct frame *f,
4647 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4648 struct face *def_face)
4650 Lisp_Object *def_attrs = def_face->lface;
4652 /* Check that other specified attributes are different that the default
4653 face. */
4654 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4655 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4656 def_attrs[LFACE_UNDERLINE_INDEX]))
4657 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4658 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4659 def_attrs[LFACE_INVERSE_INDEX]))
4660 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4661 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4662 def_attrs[LFACE_FOREGROUND_INDEX]))
4663 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4664 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4665 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4666 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4667 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4668 def_attrs[LFACE_BACKGROUND_INDEX]))
4669 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4670 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4671 def_attrs[LFACE_STIPPLE_INDEX]))
4672 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4673 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4674 def_attrs[LFACE_OVERLINE_INDEX]))
4675 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4676 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4677 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4678 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4679 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4680 def_attrs[LFACE_BOX_INDEX])))
4681 return false;
4683 /* Check font-related attributes, as those are the most commonly
4684 "unsupported" on a window-system (because of missing fonts). */
4685 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4686 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4687 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4688 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4689 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4690 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4692 int face_id;
4693 struct face *face;
4694 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4695 int i;
4697 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4699 merge_face_vectors (f, attrs, merged_attrs, 0);
4701 face_id = lookup_face (f, merged_attrs);
4702 face = FACE_FROM_ID_OR_NULL (f, face_id);
4704 if (! face)
4705 error ("Cannot make face");
4707 /* If the font is the same, or no font is found, then not
4708 supported. */
4709 if (face->font == def_face->font
4710 || ! face->font)
4711 return false;
4712 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4713 if (! EQ (face->font->props[i], def_face->font->props[i]))
4715 Lisp_Object s1, s2;
4717 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4718 || face->font->driver->case_sensitive)
4719 return true;
4720 s1 = SYMBOL_NAME (face->font->props[i]);
4721 s2 = SYMBOL_NAME (def_face->font->props[i]);
4722 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4723 s2, make_number (0), Qnil, Qt), Qt))
4724 return true;
4726 return false;
4729 /* Everything checks out, this face is supported. */
4730 return true;
4733 #endif /* HAVE_WINDOW_SYSTEM */
4735 /* Return true if all the face attributes in ATTRS are supported
4736 on the tty frame F.
4738 The definition of `supported' is somewhat heuristic, but basically means
4739 that a face containing all the attributes in ATTRS, when merged
4740 with the default face for display, can be represented in a way that's
4742 (1) different in appearance than the default face, and
4743 (2) `close in spirit' to what the attributes specify, if not exact.
4745 Point (2) implies that a `:weight black' attribute will be satisfied
4746 by any terminal that can display bold, and a `:foreground "yellow"' as
4747 long as the terminal can display a yellowish color, but `:slant italic'
4748 will _not_ be satisfied by the tty display code's automatic
4749 substitution of a `dim' face for italic. */
4751 static bool
4752 tty_supports_face_attributes_p (struct frame *f,
4753 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4754 struct face *def_face)
4756 int weight, slant;
4757 Lisp_Object val, fg, bg;
4758 XColor fg_tty_color, fg_std_color;
4759 XColor bg_tty_color, bg_std_color;
4760 unsigned test_caps = 0;
4761 Lisp_Object *def_attrs = def_face->lface;
4763 /* First check some easy-to-check stuff; ttys support none of the
4764 following attributes, so we can just return false if any are requested
4765 (even if `nominal' values are specified, we should still return false,
4766 as that will be the same value that the default face uses). We
4767 consider :slant unsupportable on ttys, even though the face code
4768 actually `fakes' them using a dim attribute if possible. This is
4769 because the faked result is too different from what the face
4770 specifies. */
4771 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4772 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4773 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4774 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4775 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4776 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4777 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4778 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4779 return false;
4781 /* Test for terminal `capabilities' (non-color character attributes). */
4783 /* font weight (bold/dim) */
4784 val = attrs[LFACE_WEIGHT_INDEX];
4785 if (!UNSPECIFIEDP (val)
4786 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4788 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4790 if (weight > 100)
4792 if (def_weight > 100)
4793 return false; /* same as default */
4794 test_caps = TTY_CAP_BOLD;
4796 else if (weight < 100)
4798 if (def_weight < 100)
4799 return false; /* same as default */
4800 test_caps = TTY_CAP_DIM;
4802 else if (def_weight == 100)
4803 return false; /* same as default */
4806 /* font slant */
4807 val = attrs[LFACE_SLANT_INDEX];
4808 if (!UNSPECIFIEDP (val)
4809 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4811 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4812 if (slant == 100 || slant == def_slant)
4813 return false; /* same as default */
4814 else
4815 test_caps |= TTY_CAP_ITALIC;
4818 /* underlining */
4819 val = attrs[LFACE_UNDERLINE_INDEX];
4820 if (!UNSPECIFIEDP (val))
4822 if (STRINGP (val))
4823 return false; /* ttys can't use colored underlines */
4824 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4825 return false; /* ttys can't use wave underlines */
4826 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4827 return false; /* same as default */
4828 else
4829 test_caps |= TTY_CAP_UNDERLINE;
4832 /* inverse video */
4833 val = attrs[LFACE_INVERSE_INDEX];
4834 if (!UNSPECIFIEDP (val))
4836 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4837 return false; /* same as default */
4838 else
4839 test_caps |= TTY_CAP_INVERSE;
4843 /* Color testing. */
4845 /* Check if foreground color is close enough. */
4846 fg = attrs[LFACE_FOREGROUND_INDEX];
4847 if (STRINGP (fg))
4849 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4851 if (face_attr_equal_p (fg, def_fg))
4852 return false; /* same as default */
4853 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4854 return false; /* not a valid color */
4855 else if (color_distance (&fg_tty_color, &fg_std_color)
4856 > TTY_SAME_COLOR_THRESHOLD)
4857 return false; /* displayed color is too different */
4858 else
4859 /* Make sure the color is really different than the default. */
4861 XColor def_fg_color;
4862 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4863 && (color_distance (&fg_tty_color, &def_fg_color)
4864 <= TTY_SAME_COLOR_THRESHOLD))
4865 return false;
4869 /* Check if background color is close enough. */
4870 bg = attrs[LFACE_BACKGROUND_INDEX];
4871 if (STRINGP (bg))
4873 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4875 if (face_attr_equal_p (bg, def_bg))
4876 return false; /* same as default */
4877 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4878 return false; /* not a valid color */
4879 else if (color_distance (&bg_tty_color, &bg_std_color)
4880 > TTY_SAME_COLOR_THRESHOLD)
4881 return false; /* displayed color is too different */
4882 else
4883 /* Make sure the color is really different than the default. */
4885 XColor def_bg_color;
4886 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4887 && (color_distance (&bg_tty_color, &def_bg_color)
4888 <= TTY_SAME_COLOR_THRESHOLD))
4889 return false;
4893 /* If both foreground and background are requested, see if the
4894 distance between them is OK. We just check to see if the distance
4895 between the tty's foreground and background is close enough to the
4896 distance between the standard foreground and background. */
4897 if (STRINGP (fg) && STRINGP (bg))
4899 int delta_delta
4900 = (color_distance (&fg_std_color, &bg_std_color)
4901 - color_distance (&fg_tty_color, &bg_tty_color));
4902 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4903 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4904 return false;
4908 /* See if the capabilities we selected above are supported, with the
4909 given colors. */
4910 return tty_capable_p (FRAME_TTY (f), test_caps);
4914 DEFUN ("display-supports-face-attributes-p",
4915 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4916 1, 2, 0,
4917 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4918 The optional argument DISPLAY can be a display name, a frame, or
4919 nil (meaning the selected frame's display).
4921 The definition of `supported' is somewhat heuristic, but basically means
4922 that a face containing all the attributes in ATTRIBUTES, when merged
4923 with the default face for display, can be represented in a way that's
4925 (1) different in appearance than the default face, and
4926 (2) `close in spirit' to what the attributes specify, if not exact.
4928 Point (2) implies that a `:weight black' attribute will be satisfied by
4929 any display that can display bold, and a `:foreground \"yellow\"' as long
4930 as it can display a yellowish color, but `:slant italic' will _not_ be
4931 satisfied by the tty display code's automatic substitution of a `dim'
4932 face for italic. */)
4933 (Lisp_Object attributes, Lisp_Object display)
4935 bool supports = false;
4936 int i;
4937 Lisp_Object frame;
4938 struct frame *f;
4939 struct face *def_face;
4940 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4942 if (noninteractive || !initialized)
4943 /* We may not be able to access low-level face information in batch
4944 mode, or before being dumped, and this function is not going to
4945 be very useful in those cases anyway, so just give up. */
4946 return Qnil;
4948 if (NILP (display))
4949 frame = selected_frame;
4950 else if (FRAMEP (display))
4951 frame = display;
4952 else
4954 /* Find any frame on DISPLAY. */
4955 Lisp_Object tail;
4957 frame = Qnil;
4958 FOR_EACH_FRAME (tail, frame)
4959 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
4960 XFRAME (frame)->param_alist)),
4961 display)))
4962 break;
4965 CHECK_LIVE_FRAME (frame);
4966 f = XFRAME (frame);
4968 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
4969 attrs[i] = Qunspecified;
4970 merge_face_ref (f, attributes, attrs, true, 0);
4972 def_face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
4973 if (def_face == NULL)
4975 if (! realize_basic_faces (f))
4976 error ("Cannot realize default face");
4977 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4980 /* Dispatch to the appropriate handler. */
4981 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4982 supports = tty_supports_face_attributes_p (f, attrs, def_face);
4983 #ifdef HAVE_WINDOW_SYSTEM
4984 else
4985 supports = x_supports_face_attributes_p (f, attrs, def_face);
4986 #endif
4988 return supports ? Qt : Qnil;
4992 /***********************************************************************
4993 Font selection
4994 ***********************************************************************/
4996 DEFUN ("internal-set-font-selection-order",
4997 Finternal_set_font_selection_order,
4998 Sinternal_set_font_selection_order, 1, 1, 0,
4999 doc: /* Set font selection order for face font selection to ORDER.
5000 ORDER must be a list of length 4 containing the symbols `:width',
5001 `:height', `:weight', and `:slant'. Face attributes appearing
5002 first in ORDER are matched first, e.g. if `:height' appears before
5003 `:weight' in ORDER, font selection first tries to find a font with
5004 a suitable height, and then tries to match the font weight.
5005 Value is ORDER. */)
5006 (Lisp_Object order)
5008 Lisp_Object list;
5009 int i;
5010 int indices[ARRAYELTS (font_sort_order)];
5012 CHECK_LIST (order);
5013 memset (indices, 0, sizeof indices);
5014 i = 0;
5016 for (list = order;
5017 CONSP (list) && i < ARRAYELTS (indices);
5018 list = XCDR (list), ++i)
5020 Lisp_Object attr = XCAR (list);
5021 int xlfd;
5023 if (EQ (attr, QCwidth))
5024 xlfd = XLFD_SWIDTH;
5025 else if (EQ (attr, QCheight))
5026 xlfd = XLFD_POINT_SIZE;
5027 else if (EQ (attr, QCweight))
5028 xlfd = XLFD_WEIGHT;
5029 else if (EQ (attr, QCslant))
5030 xlfd = XLFD_SLANT;
5031 else
5032 break;
5034 if (indices[i] != 0)
5035 break;
5036 indices[i] = xlfd;
5039 if (!NILP (list) || i != ARRAYELTS (indices))
5040 signal_error ("Invalid font sort order", order);
5041 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
5042 if (indices[i] == 0)
5043 signal_error ("Invalid font sort order", order);
5045 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5047 memcpy (font_sort_order, indices, sizeof font_sort_order);
5048 free_all_realized_faces (Qnil);
5051 font_update_sort_order (font_sort_order);
5053 return Qnil;
5057 DEFUN ("internal-set-alternative-font-family-alist",
5058 Finternal_set_alternative_font_family_alist,
5059 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5060 doc: /* Define alternative font families to try in face font selection.
5061 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5062 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5063 be found. Value is ALIST. */)
5064 (Lisp_Object alist)
5066 Lisp_Object entry, tail, tail2;
5068 CHECK_LIST (alist);
5069 alist = Fcopy_sequence (alist);
5070 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5072 entry = XCAR (tail);
5073 CHECK_LIST (entry);
5074 entry = Fcopy_sequence (entry);
5075 XSETCAR (tail, entry);
5076 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5077 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5080 Vface_alternative_font_family_alist = alist;
5081 free_all_realized_faces (Qnil);
5082 return alist;
5086 DEFUN ("internal-set-alternative-font-registry-alist",
5087 Finternal_set_alternative_font_registry_alist,
5088 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5089 doc: /* Define alternative font registries to try in face font selection.
5090 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5091 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5092 be found. Value is ALIST. */)
5093 (Lisp_Object alist)
5095 Lisp_Object entry, tail, tail2;
5097 CHECK_LIST (alist);
5098 alist = Fcopy_sequence (alist);
5099 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5101 entry = XCAR (tail);
5102 CHECK_LIST (entry);
5103 entry = Fcopy_sequence (entry);
5104 XSETCAR (tail, entry);
5105 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5106 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5108 Vface_alternative_font_registry_alist = alist;
5109 free_all_realized_faces (Qnil);
5110 return alist;
5114 #ifdef HAVE_WINDOW_SYSTEM
5116 /* Return the fontset id of the base fontset name or alias name given
5117 by the fontset attribute of ATTRS. Value is -1 if the fontset
5118 attribute of ATTRS doesn't name a fontset. */
5120 static int
5121 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5123 Lisp_Object name;
5125 name = attrs[LFACE_FONTSET_INDEX];
5126 if (!STRINGP (name))
5127 return -1;
5128 return fs_query_fontset (name, 0);
5131 #endif /* HAVE_WINDOW_SYSTEM */
5135 /***********************************************************************
5136 Face Realization
5137 ***********************************************************************/
5139 /* Realize basic faces on frame F. Value is zero if frame parameters
5140 of F don't contain enough information needed to realize the default
5141 face. */
5143 static bool
5144 realize_basic_faces (struct frame *f)
5146 bool success_p = false;
5148 /* Block input here so that we won't be surprised by an X expose
5149 event, for instance, without having the faces set up. */
5150 block_input ();
5152 if (realize_default_face (f))
5154 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5155 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5156 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5157 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5158 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5159 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5160 realize_named_face (f, Qborder, BORDER_FACE_ID);
5161 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5162 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5163 realize_named_face (f, Qmenu, MENU_FACE_ID);
5164 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5165 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5166 realize_named_face (f, Qwindow_divider_first_pixel,
5167 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5168 realize_named_face (f, Qwindow_divider_last_pixel,
5169 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5171 /* Reflect changes in the `menu' face in menu bars. */
5172 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5174 FRAME_FACE_CACHE (f)->menu_face_changed_p = false;
5175 #ifdef USE_X_TOOLKIT
5176 if (FRAME_WINDOW_P (f))
5177 x_update_menu_appearance (f);
5178 #endif
5181 success_p = true;
5184 unblock_input ();
5185 return success_p;
5189 /* Realize the default face on frame F. If the face is not fully
5190 specified, make it fully-specified. Attributes of the default face
5191 that are not explicitly specified are taken from frame parameters. */
5193 static bool
5194 realize_default_face (struct frame *f)
5196 struct face_cache *c = FRAME_FACE_CACHE (f);
5197 Lisp_Object lface;
5198 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5200 /* If the `default' face is not yet known, create it. */
5201 lface = lface_from_face_name (f, Qdefault, false);
5202 if (NILP (lface))
5204 Lisp_Object frame;
5205 XSETFRAME (frame, f);
5206 lface = Finternal_make_lisp_face (Qdefault, frame);
5209 #ifdef HAVE_WINDOW_SYSTEM
5210 if (FRAME_WINDOW_P (f))
5212 Lisp_Object font_object;
5214 XSETFONT (font_object, FRAME_FONT (f));
5215 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5216 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5217 f->default_face_done_p = true;
5219 #endif /* HAVE_WINDOW_SYSTEM */
5221 if (!FRAME_WINDOW_P (f))
5223 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5224 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5225 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5226 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5227 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5228 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5229 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5230 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5231 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5232 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5235 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5236 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5238 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5239 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5241 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5242 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5244 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5245 ASET (lface, LFACE_BOX_INDEX, Qnil);
5247 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5248 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5250 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5252 /* This function is called so early that colors are not yet
5253 set in the frame parameter list. */
5254 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5256 if (CONSP (color) && STRINGP (XCDR (color)))
5257 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5258 else if (FRAME_WINDOW_P (f))
5259 return false;
5260 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5261 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5262 else
5263 emacs_abort ();
5266 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5268 /* This function is called so early that colors are not yet
5269 set in the frame parameter list. */
5270 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5271 if (CONSP (color) && STRINGP (XCDR (color)))
5272 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5273 else if (FRAME_WINDOW_P (f))
5274 return false;
5275 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5276 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5277 else
5278 emacs_abort ();
5281 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5282 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5284 /* Realize the face; it must be fully-specified now. */
5285 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5286 check_lface (lface);
5287 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5288 struct face *face = realize_face (c, attrs, DEFAULT_FACE_ID);
5290 #ifndef HAVE_WINDOW_SYSTEM
5291 (void) face;
5292 #else
5293 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5295 /* This can happen when making a frame on a display that does
5296 not support the default font. */
5297 if (!face->font)
5298 return false;
5300 /* Otherwise, the font specified for the frame was not
5301 acceptable as a font for the default face (perhaps because
5302 auto-scaled fonts are rejected), so we must adjust the frame
5303 font. */
5304 x_set_font (f, LFACE_FONT (lface), Qnil);
5306 #endif
5307 return true;
5311 /* Realize basic faces other than the default face in face cache C.
5312 SYMBOL is the face name, ID is the face id the realized face must
5313 have. The default face must have been realized already. */
5315 static void
5316 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5318 struct face_cache *c = FRAME_FACE_CACHE (f);
5319 Lisp_Object lface = lface_from_face_name (f, symbol, false);
5320 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5321 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5323 /* The default face must exist and be fully specified. */
5324 get_lface_attributes_no_remap (f, Qdefault, attrs, true);
5325 check_lface_attrs (attrs);
5326 eassert (lface_fully_specified_p (attrs));
5328 /* If SYMBOL isn't know as a face, create it. */
5329 if (NILP (lface))
5331 Lisp_Object frame;
5332 XSETFRAME (frame, f);
5333 lface = Finternal_make_lisp_face (symbol, frame);
5336 /* Merge SYMBOL's face with the default face. */
5337 get_lface_attributes_no_remap (f, symbol, symbol_attrs, true);
5338 merge_face_vectors (f, symbol_attrs, attrs, 0);
5340 /* Realize the face. */
5341 realize_face (c, attrs, id);
5345 /* Realize the fully-specified face with attributes ATTRS in face
5346 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5347 non-negative, it is an ID of face to remove before caching the new
5348 face. Value is a pointer to the newly created realized face. */
5350 static struct face *
5351 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5352 int former_face_id)
5354 struct face *face;
5356 /* LFACE must be fully specified. */
5357 eassert (cache != NULL);
5358 check_lface_attrs (attrs);
5360 if (former_face_id >= 0 && cache->used > former_face_id)
5362 /* Remove the former face. */
5363 struct face *former_face = cache->faces_by_id[former_face_id];
5364 uncache_face (cache, former_face);
5365 free_realized_face (cache->f, former_face);
5366 SET_FRAME_GARBAGED (cache->f);
5369 if (FRAME_WINDOW_P (cache->f))
5370 face = realize_x_face (cache, attrs);
5371 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5372 face = realize_tty_face (cache, attrs);
5373 else if (FRAME_INITIAL_P (cache->f))
5375 /* Create a dummy face. */
5376 face = make_realized_face (attrs);
5378 else
5379 emacs_abort ();
5381 /* Insert the new face. */
5382 cache_face (cache, face, lface_hash (attrs));
5383 return face;
5387 #ifdef HAVE_WINDOW_SYSTEM
5388 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5389 same attributes as BASE_FACE except for the font on frame F.
5390 FONT-OBJECT may be nil, in which case, realized a face of
5391 no-font. */
5393 static struct face *
5394 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5395 struct face *base_face)
5397 struct face_cache *cache = FRAME_FACE_CACHE (f);
5398 struct face *face;
5400 face = xmalloc (sizeof *face);
5401 *face = *base_face;
5402 face->gc = 0;
5403 face->overstrike
5404 = (! NILP (font_object)
5405 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5406 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5408 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5409 face->colors_copied_bitwise_p = true;
5410 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5411 face->gc = 0;
5413 cache_face (cache, face, face->hash);
5415 return face;
5417 #endif /* HAVE_WINDOW_SYSTEM */
5420 /* Realize the fully-specified face with attributes ATTRS in face
5421 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5422 the new face doesn't share font with the default face, a fontname
5423 is allocated from the heap and set in `font_name' of the new face,
5424 but it is not yet loaded here. Value is a pointer to the newly
5425 created realized face. */
5427 static struct face *
5428 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5430 struct face *face = NULL;
5431 #ifdef HAVE_WINDOW_SYSTEM
5432 struct face *default_face;
5433 struct frame *f;
5434 Lisp_Object stipple, underline, overline, strike_through, box;
5436 eassert (FRAME_WINDOW_P (cache->f));
5438 /* Allocate a new realized face. */
5439 face = make_realized_face (attrs);
5440 face->ascii_face = face;
5442 f = cache->f;
5444 /* Determine the font to use. Most of the time, the font will be
5445 the same as the font of the default face, so try that first. */
5446 default_face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
5447 if (default_face
5448 && lface_same_font_attributes_p (default_face->lface, attrs))
5450 face->font = default_face->font;
5451 face->fontset
5452 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5454 else
5456 /* If the face attribute ATTRS specifies a fontset, use it as
5457 the base of a new realized fontset. Otherwise, use the same
5458 base fontset as of the default face. The base determines
5459 registry and encoding of a font. It may also determine
5460 foundry and family. The other fields of font name pattern
5461 are constructed from ATTRS. */
5462 int fontset = face_fontset (attrs);
5464 /* If we are realizing the default face, ATTRS should specify a
5465 fontset. In other words, if FONTSET is -1, we are not
5466 realizing the default face, thus the default face should have
5467 already been realized. */
5468 if (fontset == -1)
5470 if (default_face)
5471 fontset = default_face->fontset;
5472 if (fontset == -1)
5473 emacs_abort ();
5475 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5476 attrs[LFACE_FONT_INDEX]
5477 = font_load_for_lface (f, attrs, Ffont_spec (0, NULL));
5478 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5480 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5481 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5483 else
5485 face->font = NULL;
5486 face->fontset = -1;
5490 if (face->font
5491 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5492 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5493 face->overstrike = true;
5495 /* Load colors, and set remaining attributes. */
5497 load_face_colors (f, face, attrs);
5499 /* Set up box. */
5500 box = attrs[LFACE_BOX_INDEX];
5501 if (STRINGP (box))
5503 /* A simple box of line width 1 drawn in color given by
5504 the string. */
5505 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5506 LFACE_BOX_INDEX);
5507 face->box = FACE_SIMPLE_BOX;
5508 face->box_line_width = 1;
5510 else if (INTEGERP (box))
5512 /* Simple box of specified line width in foreground color of the
5513 face. */
5514 eassert (XINT (box) != 0);
5515 face->box = FACE_SIMPLE_BOX;
5516 face->box_line_width = XINT (box);
5517 face->box_color = face->foreground;
5518 face->box_color_defaulted_p = true;
5520 else if (CONSP (box))
5522 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5523 being one of `raised' or `sunken'. */
5524 face->box = FACE_SIMPLE_BOX;
5525 face->box_color = face->foreground;
5526 face->box_color_defaulted_p = true;
5527 face->box_line_width = 1;
5529 while (CONSP (box))
5531 Lisp_Object keyword, value;
5533 keyword = XCAR (box);
5534 box = XCDR (box);
5536 if (!CONSP (box))
5537 break;
5538 value = XCAR (box);
5539 box = XCDR (box);
5541 if (EQ (keyword, QCline_width))
5543 if (INTEGERP (value) && XINT (value) != 0)
5544 face->box_line_width = XINT (value);
5546 else if (EQ (keyword, QCcolor))
5548 if (STRINGP (value))
5550 face->box_color = load_color (f, face, value,
5551 LFACE_BOX_INDEX);
5552 face->use_box_color_for_shadows_p = true;
5555 else if (EQ (keyword, QCstyle))
5557 if (EQ (value, Qreleased_button))
5558 face->box = FACE_RAISED_BOX;
5559 else if (EQ (value, Qpressed_button))
5560 face->box = FACE_SUNKEN_BOX;
5565 /* Text underline, overline, strike-through. */
5567 underline = attrs[LFACE_UNDERLINE_INDEX];
5568 if (EQ (underline, Qt))
5570 /* Use default color (same as foreground color). */
5571 face->underline_p = true;
5572 face->underline_type = FACE_UNDER_LINE;
5573 face->underline_defaulted_p = true;
5574 face->underline_color = 0;
5576 else if (STRINGP (underline))
5578 /* Use specified color. */
5579 face->underline_p = true;
5580 face->underline_type = FACE_UNDER_LINE;
5581 face->underline_defaulted_p = false;
5582 face->underline_color
5583 = load_color (f, face, underline,
5584 LFACE_UNDERLINE_INDEX);
5586 else if (NILP (underline))
5588 face->underline_p = false;
5589 face->underline_defaulted_p = false;
5590 face->underline_color = 0;
5592 else if (CONSP (underline))
5594 /* `(:color COLOR :style STYLE)'.
5595 STYLE being one of `line' or `wave'. */
5596 face->underline_p = true;
5597 face->underline_color = 0;
5598 face->underline_defaulted_p = true;
5599 face->underline_type = FACE_UNDER_LINE;
5601 /* FIXME? This is also not robust about checking the precise form.
5602 See comments in Finternal_set_lisp_face_attribute. */
5603 while (CONSP (underline))
5605 Lisp_Object keyword, value;
5607 keyword = XCAR (underline);
5608 underline = XCDR (underline);
5610 if (!CONSP (underline))
5611 break;
5612 value = XCAR (underline);
5613 underline = XCDR (underline);
5615 if (EQ (keyword, QCcolor))
5617 if (EQ (value, Qforeground_color))
5619 face->underline_defaulted_p = true;
5620 face->underline_color = 0;
5622 else if (STRINGP (value))
5624 face->underline_defaulted_p = false;
5625 face->underline_color = load_color (f, face, value,
5626 LFACE_UNDERLINE_INDEX);
5629 else if (EQ (keyword, QCstyle))
5631 if (EQ (value, Qline))
5632 face->underline_type = FACE_UNDER_LINE;
5633 else if (EQ (value, Qwave))
5634 face->underline_type = FACE_UNDER_WAVE;
5639 overline = attrs[LFACE_OVERLINE_INDEX];
5640 if (STRINGP (overline))
5642 face->overline_color
5643 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5644 LFACE_OVERLINE_INDEX);
5645 face->overline_p = true;
5647 else if (EQ (overline, Qt))
5649 face->overline_color = face->foreground;
5650 face->overline_color_defaulted_p = true;
5651 face->overline_p = true;
5654 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5655 if (STRINGP (strike_through))
5657 face->strike_through_color
5658 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5659 LFACE_STRIKE_THROUGH_INDEX);
5660 face->strike_through_p = true;
5662 else if (EQ (strike_through, Qt))
5664 face->strike_through_color = face->foreground;
5665 face->strike_through_color_defaulted_p = true;
5666 face->strike_through_p = true;
5669 stipple = attrs[LFACE_STIPPLE_INDEX];
5670 if (!NILP (stipple))
5671 face->stipple = load_pixmap (f, stipple);
5672 #endif /* HAVE_WINDOW_SYSTEM */
5674 return face;
5678 /* Map a specified color of face FACE on frame F to a tty color index.
5679 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5680 specifies which color to map. Set *DEFAULTED to true if mapping to the
5681 default foreground/background colors. */
5683 static void
5684 map_tty_color (struct frame *f, struct face *face,
5685 enum lface_attribute_index idx, bool *defaulted)
5687 Lisp_Object frame, color, def;
5688 bool foreground_p = idx == LFACE_FOREGROUND_INDEX;
5689 unsigned long default_pixel =
5690 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5691 unsigned long pixel = default_pixel;
5692 #ifdef MSDOS
5693 unsigned long default_other_pixel =
5694 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5695 #endif
5697 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5699 XSETFRAME (frame, f);
5700 color = face->lface[idx];
5702 if (STRINGP (color)
5703 && SCHARS (color)
5704 && CONSP (Vtty_defined_color_alist)
5705 && (def = assoc_no_quit (color, call1 (Qtty_color_alist, frame)),
5706 CONSP (def)))
5708 /* Associations in tty-defined-color-alist are of the form
5709 (NAME INDEX R G B). We need the INDEX part. */
5710 pixel = XINT (XCAR (XCDR (def)));
5713 if (pixel == default_pixel && STRINGP (color))
5715 pixel = load_color (f, face, color, idx);
5717 #ifdef MSDOS
5718 /* If the foreground of the default face is the default color,
5719 use the foreground color defined by the frame. */
5720 if (FRAME_MSDOS_P (f))
5722 if (pixel == default_pixel
5723 || pixel == FACE_TTY_DEFAULT_COLOR)
5725 if (foreground_p)
5726 pixel = FRAME_FOREGROUND_PIXEL (f);
5727 else
5728 pixel = FRAME_BACKGROUND_PIXEL (f);
5729 face->lface[idx] = tty_color_name (f, pixel);
5730 *defaulted = true;
5732 else if (pixel == default_other_pixel)
5734 if (foreground_p)
5735 pixel = FRAME_BACKGROUND_PIXEL (f);
5736 else
5737 pixel = FRAME_FOREGROUND_PIXEL (f);
5738 face->lface[idx] = tty_color_name (f, pixel);
5739 *defaulted = true;
5742 #endif /* MSDOS */
5745 if (foreground_p)
5746 face->foreground = pixel;
5747 else
5748 face->background = pixel;
5752 /* Realize the fully-specified face with attributes ATTRS in face
5753 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5754 Value is a pointer to the newly created realized face. */
5756 static struct face *
5757 realize_tty_face (struct face_cache *cache,
5758 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5760 struct face *face;
5761 int weight, slant;
5762 bool face_colors_defaulted = false;
5763 struct frame *f = cache->f;
5765 /* Frame must be a termcap frame. */
5766 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5768 /* Allocate a new realized face. */
5769 face = make_realized_face (attrs);
5770 #if false
5771 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5772 #endif
5774 /* Map face attributes to TTY appearances. */
5775 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5776 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5777 if (weight > 100)
5778 face->tty_bold_p = true;
5779 if (slant != 100)
5780 face->tty_italic_p = true;
5781 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5782 face->tty_underline_p = true;
5783 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5784 face->tty_reverse_p = true;
5786 /* Map color names to color indices. */
5787 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5788 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5790 /* Swap colors if face is inverse-video. If the colors are taken
5791 from the frame colors, they are already inverted, since the
5792 frame-creation function calls x-handle-reverse-video. */
5793 if (face->tty_reverse_p && !face_colors_defaulted)
5795 unsigned long tem = face->foreground;
5796 face->foreground = face->background;
5797 face->background = tem;
5800 if (tty_suppress_bold_inverse_default_colors_p
5801 && face->tty_bold_p
5802 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5803 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5804 face->tty_bold_p = false;
5806 return face;
5810 DEFUN ("tty-suppress-bold-inverse-default-colors",
5811 Ftty_suppress_bold_inverse_default_colors,
5812 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5813 doc: /* Suppress/allow boldness of faces with inverse default colors.
5814 SUPPRESS non-nil means suppress it.
5815 This affects bold faces on TTYs whose foreground is the default background
5816 color of the display and whose background is the default foreground color.
5817 For such faces, the bold face attribute is ignored if this variable
5818 is non-nil. */)
5819 (Lisp_Object suppress)
5821 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5822 face_change = true;
5823 return suppress;
5828 /***********************************************************************
5829 Computing Faces
5830 ***********************************************************************/
5832 /* Return the ID of the face to use to display character CH with face
5833 property PROP on frame F in current_buffer. */
5836 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5838 int face_id;
5840 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5841 ch = 0;
5843 if (NILP (prop))
5845 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5846 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5848 else
5850 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5851 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5852 memcpy (attrs, default_face->lface, sizeof attrs);
5853 merge_face_ref (f, prop, attrs, true, 0);
5854 face_id = lookup_face (f, attrs);
5857 return face_id;
5860 /* Return the face ID associated with buffer position POS for
5861 displaying ASCII characters. Return in *ENDPTR the position at
5862 which a different face is needed, as far as text properties and
5863 overlays are concerned. W is a window displaying current_buffer.
5865 REGION_BEG, REGION_END delimit the region, so it can be
5866 highlighted.
5868 LIMIT is a position not to scan beyond. That is to limit the time
5869 this function can take.
5871 If MOUSE, use the character's mouse-face, not its face.
5873 BASE_FACE_ID, if non-negative, specifies a base face id to use
5874 instead of DEFAULT_FACE_ID.
5876 The face returned is suitable for displaying ASCII characters. */
5879 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5880 ptrdiff_t *endptr, ptrdiff_t limit,
5881 bool mouse, int base_face_id)
5883 struct frame *f = XFRAME (w->frame);
5884 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5885 Lisp_Object prop, position;
5886 ptrdiff_t i, noverlays;
5887 Lisp_Object *overlay_vec;
5888 ptrdiff_t endpos;
5889 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5890 Lisp_Object limit1, end;
5891 struct face *default_face;
5893 /* W must display the current buffer. We could write this function
5894 to use the frame and buffer of W, but right now it doesn't. */
5895 /* eassert (XBUFFER (w->contents) == current_buffer); */
5897 XSETFASTINT (position, pos);
5899 endpos = ZV;
5901 /* Get the `face' or `mouse_face' text property at POS, and
5902 determine the next position at which the property changes. */
5903 prop = Fget_text_property (position, propname, w->contents);
5904 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5905 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5906 if (INTEGERP (end))
5907 endpos = XINT (end);
5909 /* Look at properties from overlays. */
5910 USE_SAFE_ALLOCA;
5912 ptrdiff_t next_overlay;
5914 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, false);
5915 if (next_overlay < endpos)
5916 endpos = next_overlay;
5919 *endptr = endpos;
5922 int face_id;
5924 if (base_face_id >= 0)
5925 face_id = base_face_id;
5926 else if (NILP (Vface_remapping_alist))
5927 face_id = DEFAULT_FACE_ID;
5928 else
5929 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5931 default_face = FACE_FROM_ID (f, face_id);
5934 /* Optimize common cases where we can use the default face. */
5935 if (noverlays == 0
5936 && NILP (prop))
5938 SAFE_FREE ();
5939 return default_face->id;
5942 /* Begin with attributes from the default face. */
5943 memcpy (attrs, default_face->lface, sizeof attrs);
5945 /* Merge in attributes specified via text properties. */
5946 if (!NILP (prop))
5947 merge_face_ref (f, prop, attrs, true, 0);
5949 /* Now merge the overlay data. */
5950 noverlays = sort_overlays (overlay_vec, noverlays, w);
5951 for (i = 0; i < noverlays; i++)
5953 Lisp_Object oend;
5954 ptrdiff_t oendpos;
5956 prop = Foverlay_get (overlay_vec[i], propname);
5957 if (!NILP (prop))
5958 merge_face_ref (f, prop, attrs, true, 0);
5960 oend = OVERLAY_END (overlay_vec[i]);
5961 oendpos = OVERLAY_POSITION (oend);
5962 if (oendpos < endpos)
5963 endpos = oendpos;
5966 *endptr = endpos;
5968 SAFE_FREE ();
5970 /* Look up a realized face with the given face attributes,
5971 or realize a new one for ASCII characters. */
5972 return lookup_face (f, attrs);
5975 /* Return the face ID at buffer position POS for displaying ASCII
5976 characters associated with overlay strings for overlay OVERLAY.
5978 Like face_at_buffer_position except for OVERLAY. Currently it
5979 simply disregards the `face' properties of all overlays. */
5982 face_for_overlay_string (struct window *w, ptrdiff_t pos,
5983 ptrdiff_t *endptr, ptrdiff_t limit,
5984 bool mouse, Lisp_Object overlay)
5986 struct frame *f = XFRAME (w->frame);
5987 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5988 Lisp_Object prop, position;
5989 ptrdiff_t endpos;
5990 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5991 Lisp_Object limit1, end;
5992 struct face *default_face;
5994 /* W must display the current buffer. We could write this function
5995 to use the frame and buffer of W, but right now it doesn't. */
5996 /* eassert (XBUFFER (w->contents) == current_buffer); */
5998 XSETFASTINT (position, pos);
6000 endpos = ZV;
6002 /* Get the `face' or `mouse_face' text property at POS, and
6003 determine the next position at which the property changes. */
6004 prop = Fget_text_property (position, propname, w->contents);
6005 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6006 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6007 if (INTEGERP (end))
6008 endpos = XINT (end);
6010 *endptr = endpos;
6012 /* Optimize common case where we can use the default face. */
6013 if (NILP (prop)
6014 && NILP (Vface_remapping_alist))
6015 return DEFAULT_FACE_ID;
6017 /* Begin with attributes from the default face. */
6018 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6019 memcpy (attrs, default_face->lface, sizeof attrs);
6021 /* Merge in attributes specified via text properties. */
6022 if (!NILP (prop))
6023 merge_face_ref (f, prop, attrs, true, 0);
6025 *endptr = endpos;
6027 /* Look up a realized face with the given face attributes,
6028 or realize a new one for ASCII characters. */
6029 return lookup_face (f, attrs);
6033 /* Compute the face at character position POS in Lisp string STRING on
6034 window W, for ASCII characters.
6036 If STRING is an overlay string, it comes from position BUFPOS in
6037 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6038 not an overlay string. W must display the current buffer.
6039 REGION_BEG and REGION_END give the start and end positions of the
6040 region; both are -1 if no region is visible.
6042 BASE_FACE_ID is the id of a face to merge with. For strings coming
6043 from overlays or the `display' property it is the face at BUFPOS.
6045 If MOUSE_P, use the character's mouse-face, not its face.
6047 Set *ENDPTR to the next position where to check for faces in
6048 STRING; -1 if the face is constant from POS to the end of the
6049 string.
6051 Value is the id of the face to use. The face returned is suitable
6052 for displaying ASCII characters. */
6055 face_at_string_position (struct window *w, Lisp_Object string,
6056 ptrdiff_t pos, ptrdiff_t bufpos,
6057 ptrdiff_t *endptr, enum face_id base_face_id,
6058 bool mouse_p)
6060 Lisp_Object prop, position, end, limit;
6061 struct frame *f = XFRAME (WINDOW_FRAME (w));
6062 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6063 struct face *base_face;
6064 bool multibyte_p = STRING_MULTIBYTE (string);
6065 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6067 /* Get the value of the face property at the current position within
6068 STRING. Value is nil if there is no face property. */
6069 XSETFASTINT (position, pos);
6070 prop = Fget_text_property (position, prop_name, string);
6072 /* Get the next position at which to check for faces. Value of end
6073 is nil if face is constant all the way to the end of the string.
6074 Otherwise it is a string position where to check faces next.
6075 Limit is the maximum position up to which to check for property
6076 changes in Fnext_single_property_change. Strings are usually
6077 short, so set the limit to the end of the string. */
6078 XSETFASTINT (limit, SCHARS (string));
6079 end = Fnext_single_property_change (position, prop_name, string, limit);
6080 if (INTEGERP (end))
6081 *endptr = XFASTINT (end);
6082 else
6083 *endptr = -1;
6085 base_face = FACE_FROM_ID (f, base_face_id);
6087 /* Optimize the default case that there is no face property. */
6088 if (NILP (prop)
6089 && (multibyte_p
6090 /* We can't realize faces for different charsets differently
6091 if we don't have fonts, so we can stop here if not working
6092 on a window-system frame. */
6093 || !FRAME_WINDOW_P (f)
6094 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face)))
6095 return base_face->id;
6097 /* Begin with attributes from the base face. */
6098 memcpy (attrs, base_face->lface, sizeof attrs);
6100 /* Merge in attributes specified via text properties. */
6101 if (!NILP (prop))
6102 merge_face_ref (f, prop, attrs, true, 0);
6104 /* Look up a realized face with the given face attributes,
6105 or realize a new one for ASCII characters. */
6106 return lookup_face (f, attrs);
6110 /* Merge a face into a realized face.
6112 F is frame where faces are (to be) realized.
6114 FACE_NAME is named face to merge.
6116 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6118 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6120 BASE_FACE_ID is realized face to merge into.
6122 Return new face id.
6126 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6127 int base_face_id)
6129 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6130 struct face *base_face;
6132 base_face = FACE_FROM_ID_OR_NULL (f, base_face_id);
6133 if (!base_face)
6134 return base_face_id;
6136 if (EQ (face_name, Qt))
6138 if (face_id < 0 || face_id >= lface_id_to_name_size)
6139 return base_face_id;
6140 face_name = lface_id_to_name[face_id];
6141 /* When called during make-frame, lookup_derived_face may fail
6142 if the faces are uninitialized. Don't signal an error. */
6143 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6144 return (face_id >= 0 ? face_id : base_face_id);
6147 /* Begin with attributes from the base face. */
6148 memcpy (attrs, base_face->lface, sizeof attrs);
6150 if (!NILP (face_name))
6152 if (!merge_named_face (f, face_name, attrs, 0))
6153 return base_face_id;
6155 else
6157 struct face *face;
6158 if (face_id < 0)
6159 return base_face_id;
6160 face = FACE_FROM_ID_OR_NULL (f, face_id);
6161 if (!face)
6162 return base_face_id;
6163 merge_face_vectors (f, face->lface, attrs, 0);
6166 /* Look up a realized face with the given face attributes,
6167 or realize a new one for ASCII characters. */
6168 return lookup_face (f, attrs);
6173 #ifndef HAVE_X_WINDOWS
6174 DEFUN ("x-load-color-file", Fx_load_color_file,
6175 Sx_load_color_file, 1, 1, 0,
6176 doc: /* Create an alist of color entries from an external file.
6178 The file should define one named RGB color per line like so:
6179 R G B name
6180 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6181 (Lisp_Object filename)
6183 FILE *fp;
6184 Lisp_Object cmap = Qnil;
6185 Lisp_Object abspath;
6187 CHECK_STRING (filename);
6188 abspath = Fexpand_file_name (filename, Qnil);
6190 block_input ();
6191 fp = emacs_fopen (SSDATA (abspath), "r" FOPEN_TEXT);
6192 if (fp)
6194 char buf[512];
6195 int red, green, blue;
6196 int num;
6198 while (fgets (buf, sizeof (buf), fp) != NULL) {
6199 if (sscanf (buf, "%d %d %d %n", &red, &green, &blue, &num) == 3)
6201 #ifdef HAVE_NTGUI
6202 int color = RGB (red, green, blue);
6203 #else
6204 int color = (red << 16) | (green << 8) | blue;
6205 #endif
6206 char *name = buf + num;
6207 ptrdiff_t len = strlen (name);
6208 len -= 0 < len && name[len - 1] == '\n';
6209 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6210 cmap);
6213 fclose (fp);
6215 unblock_input ();
6216 return cmap;
6218 #endif
6221 /***********************************************************************
6222 Tests
6223 ***********************************************************************/
6225 #ifdef GLYPH_DEBUG
6227 /* Print the contents of the realized face FACE to stderr. */
6229 static void
6230 dump_realized_face (struct face *face)
6232 fprintf (stderr, "ID: %d\n", face->id);
6233 #ifdef HAVE_X_WINDOWS
6234 fprintf (stderr, "gc: %p\n", face->gc);
6235 #endif
6236 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6237 face->foreground,
6238 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6239 fprintf (stderr, "background: 0x%lx (%s)\n",
6240 face->background,
6241 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6242 if (face->font)
6243 fprintf (stderr, "font_name: %s (%s)\n",
6244 SDATA (face->font->props[FONT_NAME_INDEX]),
6245 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6246 #ifdef HAVE_X_WINDOWS
6247 fprintf (stderr, "font = %p\n", face->font);
6248 #endif
6249 fprintf (stderr, "fontset: %d\n", face->fontset);
6250 fprintf (stderr, "underline: %d (%s)\n",
6251 face->underline_p,
6252 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6253 fprintf (stderr, "hash: %d\n", face->hash);
6257 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6258 (Lisp_Object n)
6260 if (NILP (n))
6262 int i;
6264 fprintf (stderr, "font selection order: ");
6265 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
6266 fprintf (stderr, "%d ", font_sort_order[i]);
6267 fprintf (stderr, "\n");
6269 fprintf (stderr, "alternative fonts: ");
6270 debug_print (Vface_alternative_font_family_alist);
6271 fprintf (stderr, "\n");
6273 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6274 Fdump_face (make_number (i));
6276 else
6278 struct face *face;
6279 CHECK_NUMBER (n);
6280 face = FACE_FROM_ID_OR_NULL (SELECTED_FRAME (), XINT (n));
6281 if (face == NULL)
6282 error ("Not a valid face");
6283 dump_realized_face (face);
6286 return Qnil;
6290 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6291 0, 0, 0, doc: /* */)
6292 (void)
6294 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6295 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6296 fprintf (stderr, "number of GCs = %d\n", ngcs);
6297 return Qnil;
6300 #endif /* GLYPH_DEBUG */
6304 /***********************************************************************
6305 Initialization
6306 ***********************************************************************/
6308 void
6309 syms_of_xfaces (void)
6311 /* The symbols `face' and `mouse-face' used as text properties. */
6312 DEFSYM (Qface, "face");
6314 /* Property for basic faces which other faces cannot inherit. */
6315 DEFSYM (Qface_no_inherit, "face-no-inherit");
6317 /* Error symbol for wrong_type_argument in load_pixmap. */
6318 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6320 /* The name of the function to call when the background of the frame
6321 has changed, frame_set_background_mode. */
6322 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6324 /* Lisp face attribute keywords. */
6325 DEFSYM (QCfamily, ":family");
6326 DEFSYM (QCheight, ":height");
6327 DEFSYM (QCweight, ":weight");
6328 DEFSYM (QCslant, ":slant");
6329 DEFSYM (QCunderline, ":underline");
6330 DEFSYM (QCinverse_video, ":inverse-video");
6331 DEFSYM (QCreverse_video, ":reverse-video");
6332 DEFSYM (QCforeground, ":foreground");
6333 DEFSYM (QCbackground, ":background");
6334 DEFSYM (QCstipple, ":stipple");
6335 DEFSYM (QCwidth, ":width");
6336 DEFSYM (QCfont, ":font");
6337 DEFSYM (QCfontset, ":fontset");
6338 DEFSYM (QCdistant_foreground, ":distant-foreground");
6339 DEFSYM (QCbold, ":bold");
6340 DEFSYM (QCitalic, ":italic");
6341 DEFSYM (QCoverline, ":overline");
6342 DEFSYM (QCstrike_through, ":strike-through");
6343 DEFSYM (QCbox, ":box");
6344 DEFSYM (QCinherit, ":inherit");
6346 /* Symbols used for Lisp face attribute values. */
6347 DEFSYM (QCcolor, ":color");
6348 DEFSYM (QCline_width, ":line-width");
6349 DEFSYM (QCstyle, ":style");
6350 DEFSYM (Qline, "line");
6351 DEFSYM (Qwave, "wave");
6352 DEFSYM (Qreleased_button, "released-button");
6353 DEFSYM (Qpressed_button, "pressed-button");
6354 DEFSYM (Qnormal, "normal");
6355 DEFSYM (Qextra_light, "extra-light");
6356 DEFSYM (Qlight, "light");
6357 DEFSYM (Qsemi_light, "semi-light");
6358 DEFSYM (Qsemi_bold, "semi-bold");
6359 DEFSYM (Qbold, "bold");
6360 DEFSYM (Qextra_bold, "extra-bold");
6361 DEFSYM (Qultra_bold, "ultra-bold");
6362 DEFSYM (Qoblique, "oblique");
6363 DEFSYM (Qitalic, "italic");
6365 /* The symbols `foreground-color' and `background-color' which can be
6366 used as part of a `face' property. This is for compatibility with
6367 Emacs 20.2. */
6368 DEFSYM (Qbackground_color, "background-color");
6369 DEFSYM (Qforeground_color, "foreground-color");
6371 DEFSYM (Qunspecified, "unspecified");
6372 DEFSYM (QCignore_defface, ":ignore-defface");
6374 /* The symbol `face-alias'. A symbol having that property is an
6375 alias for another face. Value of the property is the name of
6376 the aliased face. */
6377 DEFSYM (Qface_alias, "face-alias");
6379 /* Names of basic faces. */
6380 DEFSYM (Qdefault, "default");
6381 DEFSYM (Qtool_bar, "tool-bar");
6382 DEFSYM (Qfringe, "fringe");
6383 DEFSYM (Qheader_line, "header-line");
6384 DEFSYM (Qscroll_bar, "scroll-bar");
6385 DEFSYM (Qmenu, "menu");
6386 DEFSYM (Qcursor, "cursor");
6387 DEFSYM (Qborder, "border");
6388 DEFSYM (Qmouse, "mouse");
6389 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6390 DEFSYM (Qvertical_border, "vertical-border");
6392 /* TTY color-related functions (defined in tty-colors.el). */
6393 DEFSYM (Qwindow_divider, "window-divider");
6394 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6395 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6396 DEFSYM (Qtty_color_desc, "tty-color-desc");
6397 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6398 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6400 /* The name of the function used to compute colors on TTYs. */
6401 DEFSYM (Qtty_color_alist, "tty-color-alist");
6403 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6404 staticpro (&Vparam_value_alist);
6405 Vface_alternative_font_family_alist = Qnil;
6406 staticpro (&Vface_alternative_font_family_alist);
6407 Vface_alternative_font_registry_alist = Qnil;
6408 staticpro (&Vface_alternative_font_registry_alist);
6410 defsubr (&Sinternal_make_lisp_face);
6411 defsubr (&Sinternal_lisp_face_p);
6412 defsubr (&Sinternal_set_lisp_face_attribute);
6413 #ifdef HAVE_WINDOW_SYSTEM
6414 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6415 #endif
6416 defsubr (&Scolor_gray_p);
6417 defsubr (&Scolor_supported_p);
6418 #ifndef HAVE_X_WINDOWS
6419 defsubr (&Sx_load_color_file);
6420 #endif
6421 defsubr (&Sface_attribute_relative_p);
6422 defsubr (&Smerge_face_attribute);
6423 defsubr (&Sinternal_get_lisp_face_attribute);
6424 defsubr (&Sinternal_lisp_face_attribute_values);
6425 defsubr (&Sinternal_lisp_face_equal_p);
6426 defsubr (&Sinternal_lisp_face_empty_p);
6427 defsubr (&Sinternal_copy_lisp_face);
6428 defsubr (&Sinternal_merge_in_global_face);
6429 defsubr (&Sface_font);
6430 defsubr (&Sframe_face_alist);
6431 defsubr (&Sdisplay_supports_face_attributes_p);
6432 defsubr (&Scolor_distance);
6433 defsubr (&Sinternal_set_font_selection_order);
6434 defsubr (&Sinternal_set_alternative_font_family_alist);
6435 defsubr (&Sinternal_set_alternative_font_registry_alist);
6436 defsubr (&Sface_attributes_as_vector);
6437 #ifdef GLYPH_DEBUG
6438 defsubr (&Sdump_face);
6439 defsubr (&Sshow_face_resources);
6440 #endif /* GLYPH_DEBUG */
6441 defsubr (&Sclear_face_cache);
6442 defsubr (&Stty_suppress_bold_inverse_default_colors);
6444 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6445 defsubr (&Sdump_colors);
6446 #endif
6448 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6449 doc: /* List of global face definitions (for internal use only.) */);
6450 Vface_new_frame_defaults = Qnil;
6452 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6453 doc: /* Default stipple pattern used on monochrome displays.
6454 This stipple pattern is used on monochrome displays
6455 instead of shades of gray for a face background color.
6456 See `set-face-stipple' for possible values for this variable. */);
6457 Vface_default_stipple = build_pure_c_string ("gray3");
6459 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6460 doc: /* An alist of defined terminal colors and their RGB values.
6461 See the docstring of `tty-color-alist' for the details. */);
6462 Vtty_defined_color_alist = Qnil;
6464 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6465 doc: /* Allowed scalable fonts.
6466 A value of nil means don't allow any scalable fonts.
6467 A value of t means allow any scalable font.
6468 Otherwise, value must be a list of regular expressions. A font may be
6469 scaled if its name matches a regular expression in the list.
6470 Note that if value is nil, a scalable font might still be used, if no
6471 other font of the appropriate family and registry is available. */);
6472 Vscalable_fonts_allowed = Qnil;
6474 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6475 doc: /* List of ignored fonts.
6476 Each element is a regular expression that matches names of fonts to
6477 ignore. */);
6478 Vface_ignored_fonts = Qnil;
6480 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6481 doc: /* Alist of face remappings.
6482 Each element is of the form:
6484 (FACE . REPLACEMENT),
6486 which causes display of the face FACE to use REPLACEMENT instead.
6487 REPLACEMENT is a face specification, i.e. one of the following:
6489 (1) a face name
6490 (2) a property list of attribute/value pairs, or
6491 (3) a list in which each element has the form of (1) or (2).
6493 List values for REPLACEMENT are merged to form the final face
6494 specification, with earlier entries taking precedence, in the same way
6495 as with the `face' text property.
6497 Face-name remapping cycles are suppressed; recursive references use
6498 the underlying face instead of the remapped face. So a remapping of
6499 the form:
6501 (FACE EXTRA-FACE... FACE)
6505 (FACE (FACE-ATTR VAL ...) FACE)
6507 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6508 existing definition of FACE. Note that this isn't necessary for the
6509 default face, since every face inherits from the default face.
6511 If this variable is made buffer-local, the face remapping takes effect
6512 only in that buffer. For instance, the mode my-mode could define a
6513 face `my-mode-default', and then in the mode setup function, do:
6515 (set (make-local-variable \\='face-remapping-alist)
6516 \\='((default my-mode-default)))).
6518 Because Emacs normally only redraws screen areas when the underlying
6519 buffer contents change, you may need to call `redraw-display' after
6520 changing this variable for it to take effect. */);
6521 Vface_remapping_alist = Qnil;
6523 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6524 doc: /* Alist of fonts vs the rescaling factors.
6525 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6526 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6527 RESCALE-RATIO is a floating point number to specify how much larger
6528 \(or smaller) font we should use. For instance, if a face requests
6529 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6530 Vface_font_rescale_alist = Qnil;
6532 #ifdef HAVE_WINDOW_SYSTEM
6533 defsubr (&Sbitmap_spec_p);
6534 defsubr (&Sx_list_fonts);
6535 defsubr (&Sinternal_face_x_get_resource);
6536 defsubr (&Sx_family_fonts);
6537 #endif