Merge from trunk.
[emacs.git] / src / xfaces.c
blob78ea913526ebfeb6b9ff85a8044c6c39246f9f69
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2011 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
10 (at 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 foundary 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 correspoinding 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 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 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 <stdio.h>
204 #include <sys/types.h>
205 #include <sys/stat.h>
206 #include <stdio.h> /* This needs to be before termchar.h */
207 #include <setjmp.h>
209 #include "lisp.h"
210 #include "character.h"
211 #include "charset.h"
212 #include "keyboard.h"
213 #include "frame.h"
214 #include "termhooks.h"
216 #ifdef HAVE_X_WINDOWS
217 #include "xterm.h"
218 #ifdef USE_MOTIF
219 #include <Xm/Xm.h>
220 #include <Xm/XmStrDefs.h>
221 #endif /* USE_MOTIF */
222 #endif /* HAVE_X_WINDOWS */
224 #ifdef MSDOS
225 #include "dosfns.h"
226 #endif
228 #ifdef WINDOWSNT
229 #include "w32term.h"
230 #include "fontset.h"
231 /* Redefine X specifics to W32 equivalents to avoid cluttering the
232 code with #ifdef blocks. */
233 #undef FRAME_X_DISPLAY_INFO
234 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
235 #define x_display_info w32_display_info
236 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
237 #define check_x check_w32
238 #define GCGraphicsExposures 0
239 #endif /* WINDOWSNT */
241 #ifdef HAVE_NS
242 #include "nsterm.h"
243 #undef FRAME_X_DISPLAY_INFO
244 #define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
245 #define x_display_info ns_display_info
246 #define FRAME_X_FONT_TABLE FRAME_NS_FONT_TABLE
247 #define check_x check_ns
248 #define GCGraphicsExposures 0
249 #endif /* HAVE_NS */
251 #include "buffer.h"
252 #include "dispextern.h"
253 #include "blockinput.h"
254 #include "window.h"
255 #include "intervals.h"
256 #include "termchar.h"
258 #include "font.h"
259 #ifdef HAVE_WINDOW_SYSTEM
260 #include "fontset.h"
261 #endif /* HAVE_WINDOW_SYSTEM */
263 #ifdef HAVE_X_WINDOWS
265 /* Compensate for a bug in Xos.h on some systems, on which it requires
266 time.h. On some such systems, Xos.h tries to redefine struct
267 timeval and struct timezone if USG is #defined while it is
268 #included. */
270 #ifdef XOS_NEEDS_TIME_H
271 #include <time.h>
272 #undef USG
273 #include <X11/Xos.h>
274 #define USG
275 #define __TIMEVAL__
276 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
277 #endif
278 #else /* not XOS_NEEDS_TIME_H */
279 #include <X11/Xos.h>
280 #endif /* not XOS_NEEDS_TIME_H */
282 #endif /* HAVE_X_WINDOWS */
284 #include <ctype.h>
286 /* Number of pt per inch (from the TeXbook). */
288 #define PT_PER_INCH 72.27
290 /* Non-zero if face attribute ATTR is unspecified. */
292 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
294 /* Non-zero if face attribute ATTR is `ignore-defface'. */
296 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), Qignore_defface)
298 /* Value is the number of elements of VECTOR. */
300 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
302 /* Size of hash table of realized faces in face caches (should be a
303 prime number). */
305 #define FACE_CACHE_BUCKETS_SIZE 1001
307 /* Keyword symbols used for face attribute names. */
309 Lisp_Object QCfamily, QCheight, QCweight, QCslant;
310 static Lisp_Object QCunderline;
311 static Lisp_Object QCinverse_video, QCstipple;
312 Lisp_Object QCforeground, QCbackground;
313 Lisp_Object QCwidth;
314 static Lisp_Object QCfont, QCbold, QCitalic;
315 static Lisp_Object QCreverse_video;
316 static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
317 static Lisp_Object QCfontset;
319 /* Symbols used for attribute values. */
321 Lisp_Object Qnormal;
322 Lisp_Object Qbold;
323 static Lisp_Object Qultra_light, Qextra_light, Qlight;
324 static Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
325 static Lisp_Object Qoblique, Qreverse_oblique, Qreverse_italic;
326 Lisp_Object Qitalic;
327 static Lisp_Object Qultra_condensed, Qextra_condensed;
328 Lisp_Object Qcondensed;
329 static Lisp_Object Qsemi_condensed, Qsemi_expanded, Qextra_expanded;
330 Lisp_Object Qexpanded;
331 static Lisp_Object Qultra_expanded;
332 static Lisp_Object Qreleased_button, Qpressed_button;
333 static Lisp_Object QCstyle, QCcolor, QCline_width;
334 Lisp_Object Qunspecified; /* used in dosfns.c */
335 static Lisp_Object Qignore_defface;
337 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
339 /* The name of the function to call when the background of the frame
340 has changed, frame_set_background_mode. */
342 Lisp_Object Qframe_set_background_mode;
344 /* Names of basic faces. */
346 Lisp_Object Qdefault, Qtool_bar, Qfringe;
347 static Lisp_Object Qregion;
348 Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
349 static Lisp_Object Qborder, Qmouse, Qmenu;
350 Lisp_Object Qmode_line_inactive;
351 static Lisp_Object Qvertical_border;
353 /* The symbol `face-alias'. A symbols having that property is an
354 alias for another face. Value of the property is the name of
355 the aliased face. */
357 static Lisp_Object Qface_alias;
359 /* Alist of alternative font families. Each element is of the form
360 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
361 try FAMILY1, then FAMILY2, ... */
363 Lisp_Object Vface_alternative_font_family_alist;
365 /* Alist of alternative font registries. Each element is of the form
366 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
367 loaded, try REGISTRY1, then REGISTRY2, ... */
369 Lisp_Object Vface_alternative_font_registry_alist;
371 /* Allowed scalable fonts. A value of nil means don't allow any
372 scalable fonts. A value of t means allow the use of any scalable
373 font. Otherwise, value must be a list of regular expressions. A
374 font may be scaled if its name matches a regular expression in the
375 list. */
377 static Lisp_Object Qscalable_fonts_allowed;
379 #define DEFAULT_FONT_LIST_LIMIT 100
381 /* The symbols `foreground-color' and `background-color' which can be
382 used as part of a `face' property. This is for compatibility with
383 Emacs 20.2. */
385 Lisp_Object Qforeground_color, Qbackground_color;
387 /* The symbols `face' and `mouse-face' used as text properties. */
389 Lisp_Object Qface;
391 /* Property for basic faces which other faces cannot inherit. */
393 static Lisp_Object Qface_no_inherit;
395 /* Error symbol for wrong_type_argument in load_pixmap. */
397 static Lisp_Object Qbitmap_spec_p;
399 /* The next ID to assign to Lisp faces. */
401 static int next_lface_id;
403 /* A vector mapping Lisp face Id's to face names. */
405 static Lisp_Object *lface_id_to_name;
406 static int lface_id_to_name_size;
408 /* TTY color-related functions (defined in tty-colors.el). */
410 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
412 /* The name of the function used to compute colors on TTYs. */
414 static Lisp_Object Qtty_color_alist;
416 /* Counter for calls to clear_face_cache. If this counter reaches
417 CLEAR_FONT_TABLE_COUNT, and a frame has more than
418 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
420 static int clear_font_table_count;
421 #define CLEAR_FONT_TABLE_COUNT 100
422 #define CLEAR_FONT_TABLE_NFONTS 10
424 /* Non-zero means face attributes have been changed since the last
425 redisplay. Used in redisplay_internal. */
427 int face_change_count;
429 /* Non-zero means don't display bold text if a face's foreground
430 and background colors are the inverse of the default colors of the
431 display. This is a kluge to suppress `bold black' foreground text
432 which is hard to read on an LCD monitor. */
434 static int tty_suppress_bold_inverse_default_colors_p;
436 /* A list of the form `((x . y))' used to avoid consing in
437 Finternal_set_lisp_face_attribute. */
439 static Lisp_Object Vparam_value_alist;
441 /* The total number of colors currently allocated. */
443 #if GLYPH_DEBUG
444 static int ncolors_allocated;
445 static int npixmaps_allocated;
446 static int ngcs;
447 #endif
449 /* Non-zero means the definition of the `menu' face for new frames has
450 been changed. */
452 static int menu_face_changed_default;
455 /* Function prototypes. */
457 struct table_entry;
458 struct named_merge_point;
460 static void map_tty_color (struct frame *, struct face *,
461 enum lface_attribute_index, int *);
462 static Lisp_Object resolve_face_name (Lisp_Object, int);
463 static void set_font_frame_param (Lisp_Object, Lisp_Object);
464 static int get_lface_attributes (struct frame *, Lisp_Object, Lisp_Object *,
465 int, struct named_merge_point *);
466 static int load_pixmap (struct frame *, Lisp_Object, unsigned *, unsigned *);
467 static struct frame *frame_or_selected_frame (Lisp_Object, int);
468 static void load_face_colors (struct frame *, struct face *, Lisp_Object *);
469 static void free_face_colors (struct frame *, struct face *);
470 static int face_color_gray_p (struct frame *, const char *);
471 static struct face *realize_face (struct face_cache *, Lisp_Object *,
472 int);
473 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
474 struct face *);
475 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
476 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
477 static int realize_basic_faces (struct frame *);
478 static int realize_default_face (struct frame *);
479 static void realize_named_face (struct frame *, Lisp_Object, int);
480 static int lface_fully_specified_p (Lisp_Object *);
481 static int lface_equal_p (Lisp_Object *, Lisp_Object *);
482 static unsigned hash_string_case_insensitive (Lisp_Object);
483 static unsigned lface_hash (Lisp_Object *);
484 static int lface_same_font_attributes_p (Lisp_Object *, Lisp_Object *);
485 static struct face_cache *make_face_cache (struct frame *);
486 static void clear_face_gcs (struct face_cache *);
487 static void free_face_cache (struct face_cache *);
488 static int face_fontset (Lisp_Object *);
489 static void merge_face_vectors (struct frame *, Lisp_Object *, Lisp_Object*,
490 struct named_merge_point *);
491 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
492 int, struct named_merge_point *);
493 static int set_lface_from_font (struct frame *, Lisp_Object, Lisp_Object,
494 int);
495 static Lisp_Object lface_from_face_name (struct frame *, Lisp_Object, int);
496 static struct face *make_realized_face (Lisp_Object *);
497 static void cache_face (struct face_cache *, struct face *, unsigned);
498 static void uncache_face (struct face_cache *, struct face *);
500 #ifdef HAVE_WINDOW_SYSTEM
502 static GC x_create_gc (struct frame *, unsigned long, XGCValues *);
503 static void x_free_gc (struct frame *, GC);
505 #ifdef USE_X_TOOLKIT
506 static void x_update_menu_appearance (struct frame *);
508 extern void free_frame_menubar (struct frame *);
509 #endif /* USE_X_TOOLKIT */
511 #endif /* HAVE_WINDOW_SYSTEM */
514 /***********************************************************************
515 Utilities
516 ***********************************************************************/
518 #ifdef HAVE_X_WINDOWS
520 #ifdef DEBUG_X_COLORS
522 /* The following is a poor mans infrastructure for debugging X color
523 allocation problems on displays with PseudoColor-8. Some X servers
524 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
525 color reference counts completely so that they don't signal an
526 error when a color is freed whose reference count is already 0.
527 Other X servers do. To help me debug this, the following code
528 implements a simple reference counting schema of its own, for a
529 single display/screen. --gerd. */
531 /* Reference counts for pixel colors. */
533 int color_count[256];
535 /* Register color PIXEL as allocated. */
537 void
538 register_color (pixel)
539 unsigned long pixel;
541 xassert (pixel < 256);
542 ++color_count[pixel];
546 /* Register color PIXEL as deallocated. */
548 void
549 unregister_color (pixel)
550 unsigned long pixel;
552 xassert (pixel < 256);
553 if (color_count[pixel] > 0)
554 --color_count[pixel];
555 else
556 abort ();
560 /* Register N colors from PIXELS as deallocated. */
562 void
563 unregister_colors (pixels, n)
564 unsigned long *pixels;
565 int n;
567 int i;
568 for (i = 0; i < n; ++i)
569 unregister_color (pixels[i]);
573 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
574 doc: /* Dump currently allocated colors to stderr. */)
575 (void)
577 int i, n;
579 fputc ('\n', stderr);
581 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
582 if (color_count[i])
584 fprintf (stderr, "%3d: %5d", i, color_count[i]);
585 ++n;
586 if (n % 5 == 0)
587 fputc ('\n', stderr);
588 else
589 fputc ('\t', stderr);
592 if (n % 5 != 0)
593 fputc ('\n', stderr);
594 return Qnil;
597 #endif /* DEBUG_X_COLORS */
600 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
601 color values. Interrupt input must be blocked when this function
602 is called. */
604 void
605 x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
607 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
609 /* If display has an immutable color map, freeing colors is not
610 necessary and some servers don't allow it. So don't do it. */
611 if (class != StaticColor && class != StaticGray && class != TrueColor)
613 #ifdef DEBUG_X_COLORS
614 unregister_colors (pixels, npixels);
615 #endif
616 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
617 pixels, npixels, 0);
622 #ifdef USE_X_TOOLKIT
624 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
625 color values. Interrupt input must be blocked when this function
626 is called. */
628 void
629 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap, long unsigned int *pixels, int npixels)
631 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
632 int class = dpyinfo->visual->class;
634 /* If display has an immutable color map, freeing colors is not
635 necessary and some servers don't allow it. So don't do it. */
636 if (class != StaticColor && class != StaticGray && class != TrueColor)
638 #ifdef DEBUG_X_COLORS
639 unregister_colors (pixels, npixels);
640 #endif
641 XFreeColors (dpy, cmap, pixels, npixels, 0);
644 #endif /* USE_X_TOOLKIT */
646 /* Create and return a GC for use on frame F. GC values and mask
647 are given by XGCV and MASK. */
649 static inline GC
650 x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
652 GC gc;
653 BLOCK_INPUT;
654 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
655 UNBLOCK_INPUT;
656 IF_DEBUG (++ngcs);
657 return gc;
661 /* Free GC which was used on frame F. */
663 static inline void
664 x_free_gc (struct frame *f, GC gc)
666 eassert (interrupt_input_blocked);
667 IF_DEBUG (xassert (--ngcs >= 0));
668 XFreeGC (FRAME_X_DISPLAY (f), gc);
671 #endif /* HAVE_X_WINDOWS */
673 #ifdef WINDOWSNT
674 /* W32 emulation of GCs */
676 static inline GC
677 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
679 GC gc;
680 BLOCK_INPUT;
681 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
682 UNBLOCK_INPUT;
683 IF_DEBUG (++ngcs);
684 return gc;
688 /* Free GC which was used on frame F. */
690 static inline void
691 x_free_gc (struct frame *f, GC gc)
693 IF_DEBUG (xassert (--ngcs >= 0));
694 xfree (gc);
697 #endif /* WINDOWSNT */
699 #ifdef HAVE_NS
700 /* NS emulation of GCs */
702 static inline GC
703 x_create_gc (struct frame *f,
704 unsigned long mask,
705 XGCValues *xgcv)
707 GC gc = xmalloc (sizeof (*gc));
708 if (gc)
709 memcpy (gc, xgcv, sizeof (XGCValues));
710 return gc;
713 static inline void
714 x_free_gc (struct frame *f, GC gc)
716 xfree (gc);
718 #endif /* HAVE_NS */
720 /* Like strcasecmp/stricmp. Used to compare parts of font names which
721 are in ISO8859-1. */
724 xstrcasecmp (const char *s1, const char *s2)
726 while (*s1 && *s2)
728 unsigned char b1 = *s1;
729 unsigned char b2 = *s2;
730 unsigned char c1 = tolower (b1);
731 unsigned char c2 = tolower (b2);
732 if (c1 != c2)
733 return c1 < c2 ? -1 : 1;
734 ++s1, ++s2;
737 if (*s1 == 0)
738 return *s2 == 0 ? 0 : -1;
739 return 1;
743 /* If FRAME is nil, return a pointer to the selected frame.
744 Otherwise, check that FRAME is a live frame, and return a pointer
745 to it. NPARAM is the parameter number of FRAME, for
746 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
747 Lisp function definitions. */
749 static inline struct frame *
750 frame_or_selected_frame (Lisp_Object frame, int nparam)
752 if (NILP (frame))
753 frame = selected_frame;
755 CHECK_LIVE_FRAME (frame);
756 return XFRAME (frame);
760 /***********************************************************************
761 Frames and faces
762 ***********************************************************************/
764 /* Initialize face cache and basic faces for frame F. */
766 void
767 init_frame_faces (struct frame *f)
769 /* Make a face cache, if F doesn't have one. */
770 if (FRAME_FACE_CACHE (f) == NULL)
771 FRAME_FACE_CACHE (f) = make_face_cache (f);
773 #ifdef HAVE_WINDOW_SYSTEM
774 /* Make the image cache. */
775 if (FRAME_WINDOW_P (f))
777 /* We initialize the image cache when creating the first frame
778 on a terminal, and not during terminal creation. This way,
779 `x-open-connection' on a tty won't create an image cache. */
780 if (FRAME_IMAGE_CACHE (f) == NULL)
781 FRAME_IMAGE_CACHE (f) = make_image_cache ();
782 ++FRAME_IMAGE_CACHE (f)->refcount;
784 #endif /* HAVE_WINDOW_SYSTEM */
786 /* Realize basic faces. Must have enough information in frame
787 parameters to realize basic faces at this point. */
788 #ifdef HAVE_X_WINDOWS
789 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
790 #endif
791 #ifdef WINDOWSNT
792 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
793 #endif
794 #ifdef HAVE_NS
795 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
796 #endif
797 if (!realize_basic_faces (f))
798 abort ();
802 /* Free face cache of frame F. Called from delete_frame. */
804 void
805 free_frame_faces (struct frame *f)
807 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
809 if (face_cache)
811 free_face_cache (face_cache);
812 FRAME_FACE_CACHE (f) = NULL;
815 #ifdef HAVE_WINDOW_SYSTEM
816 if (FRAME_WINDOW_P (f))
818 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
819 if (image_cache)
821 --image_cache->refcount;
822 if (image_cache->refcount == 0)
823 free_image_cache (f);
826 #endif /* HAVE_WINDOW_SYSTEM */
830 /* Clear face caches, and recompute basic faces for frame F. Call
831 this after changing frame parameters on which those faces depend,
832 or when realized faces have been freed due to changing attributes
833 of named faces. */
835 void
836 recompute_basic_faces (struct frame *f)
838 if (FRAME_FACE_CACHE (f))
840 clear_face_cache (0);
841 if (!realize_basic_faces (f))
842 abort ();
847 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
848 try to free unused fonts, too. */
850 void
851 clear_face_cache (int clear_fonts_p)
853 #ifdef HAVE_WINDOW_SYSTEM
854 Lisp_Object tail, frame;
856 if (clear_fonts_p
857 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
859 #if 0
860 /* Not yet implemented. */
861 clear_font_cache (frame);
862 #endif
864 /* From time to time see if we can unload some fonts. This also
865 frees all realized faces on all frames. Fonts needed by
866 faces will be loaded again when faces are realized again. */
867 clear_font_table_count = 0;
869 FOR_EACH_FRAME (tail, frame)
871 struct frame *f = XFRAME (frame);
872 if (FRAME_WINDOW_P (f)
873 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
874 free_all_realized_faces (frame);
877 else
879 /* Clear GCs of realized faces. */
880 FOR_EACH_FRAME (tail, frame)
882 struct frame *f = XFRAME (frame);
883 if (FRAME_WINDOW_P (f))
884 clear_face_gcs (FRAME_FACE_CACHE (f));
886 clear_image_caches (Qnil);
888 #endif /* HAVE_WINDOW_SYSTEM */
892 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
893 doc: /* Clear face caches on all frames.
894 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
895 (Lisp_Object thoroughly)
897 clear_face_cache (!NILP (thoroughly));
898 ++face_change_count;
899 ++windows_or_buffers_changed;
900 return Qnil;
904 /***********************************************************************
905 X Pixmaps
906 ***********************************************************************/
908 #ifdef HAVE_WINDOW_SYSTEM
910 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
911 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
912 A bitmap specification is either a string, a file name, or a list
913 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
914 HEIGHT is its height, and DATA is a string containing the bits of
915 the pixmap. Bits are stored row by row, each row occupies
916 \(WIDTH + 7)/8 bytes. */)
917 (Lisp_Object object)
919 int pixmap_p = 0;
921 if (STRINGP (object))
922 /* If OBJECT is a string, it's a file name. */
923 pixmap_p = 1;
924 else if (CONSP (object))
926 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
927 HEIGHT must be integers > 0, and DATA must be string large
928 enough to hold a bitmap of the specified size. */
929 Lisp_Object width, height, data;
931 height = width = data = Qnil;
933 if (CONSP (object))
935 width = XCAR (object);
936 object = XCDR (object);
937 if (CONSP (object))
939 height = XCAR (object);
940 object = XCDR (object);
941 if (CONSP (object))
942 data = XCAR (object);
946 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
948 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
949 / BITS_PER_CHAR);
950 if (SBYTES (data) >= bytes_per_row * XINT (height))
951 pixmap_p = 1;
955 return pixmap_p ? Qt : Qnil;
959 /* Load a bitmap according to NAME (which is either a file name or a
960 pixmap spec) for use on frame F. Value is the bitmap_id (see
961 xfns.c). If NAME is nil, return with a bitmap id of zero. If
962 bitmap cannot be loaded, display a message saying so, and return
963 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
964 if these pointers are not null. */
966 static int
967 load_pixmap (FRAME_PTR f, Lisp_Object name, unsigned int *w_ptr, unsigned int *h_ptr)
969 int bitmap_id;
971 if (NILP (name))
972 return 0;
974 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
976 BLOCK_INPUT;
977 if (CONSP (name))
979 /* Decode a bitmap spec into a bitmap. */
981 int h, w;
982 Lisp_Object bits;
984 w = XINT (Fcar (name));
985 h = XINT (Fcar (Fcdr (name)));
986 bits = Fcar (Fcdr (Fcdr (name)));
988 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
989 w, h);
991 else
993 /* It must be a string -- a file name. */
994 bitmap_id = x_create_bitmap_from_file (f, name);
996 UNBLOCK_INPUT;
998 if (bitmap_id < 0)
1000 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
1001 bitmap_id = 0;
1003 if (w_ptr)
1004 *w_ptr = 0;
1005 if (h_ptr)
1006 *h_ptr = 0;
1008 else
1010 #if GLYPH_DEBUG
1011 ++npixmaps_allocated;
1012 #endif
1013 if (w_ptr)
1014 *w_ptr = x_bitmap_width (f, bitmap_id);
1016 if (h_ptr)
1017 *h_ptr = x_bitmap_height (f, bitmap_id);
1020 return bitmap_id;
1023 #endif /* HAVE_WINDOW_SYSTEM */
1027 /***********************************************************************
1028 X Colors
1029 ***********************************************************************/
1031 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1032 RGB_LIST should contain (at least) 3 lisp integers.
1033 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1035 static int
1036 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
1038 #define PARSE_RGB_LIST_FIELD(field) \
1039 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1041 color->field = XINT (XCAR (rgb_list)); \
1042 rgb_list = XCDR (rgb_list); \
1044 else \
1045 return 0;
1047 PARSE_RGB_LIST_FIELD (red);
1048 PARSE_RGB_LIST_FIELD (green);
1049 PARSE_RGB_LIST_FIELD (blue);
1051 return 1;
1055 /* Lookup on frame F the color described by the lisp string COLOR.
1056 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1057 non-zero, then the `standard' definition of the same color is
1058 returned in it. */
1060 static int
1061 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color, XColor *std_color)
1063 Lisp_Object frame, color_desc;
1065 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1066 return 0;
1068 XSETFRAME (frame, f);
1070 color_desc = call2 (Qtty_color_desc, color, frame);
1071 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1073 Lisp_Object rgb;
1075 if (! INTEGERP (XCAR (XCDR (color_desc))))
1076 return 0;
1078 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1080 rgb = XCDR (XCDR (color_desc));
1081 if (! parse_rgb_list (rgb, tty_color))
1082 return 0;
1084 /* Should we fill in STD_COLOR too? */
1085 if (std_color)
1087 /* Default STD_COLOR to the same as TTY_COLOR. */
1088 *std_color = *tty_color;
1090 /* Do a quick check to see if the returned descriptor is
1091 actually _exactly_ equal to COLOR, otherwise we have to
1092 lookup STD_COLOR separately. If it's impossible to lookup
1093 a standard color, we just give up and use TTY_COLOR. */
1094 if ((!STRINGP (XCAR (color_desc))
1095 || NILP (Fstring_equal (color, XCAR (color_desc))))
1096 && !NILP (Ffboundp (Qtty_color_standard_values)))
1098 /* Look up STD_COLOR separately. */
1099 rgb = call1 (Qtty_color_standard_values, color);
1100 if (! parse_rgb_list (rgb, std_color))
1101 return 0;
1105 return 1;
1107 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1108 /* We were called early during startup, and the colors are not
1109 yet set up in tty-defined-color-alist. Don't return a failure
1110 indication, since this produces the annoying "Unable to
1111 load color" messages in the *Messages* buffer. */
1112 return 1;
1113 else
1114 /* tty-color-desc seems to have returned a bad value. */
1115 return 0;
1118 /* A version of defined_color for non-X frames. */
1120 static int
1121 tty_defined_color (struct frame *f, const char *color_name,
1122 XColor *color_def, int alloc)
1124 int status = 1;
1126 /* Defaults. */
1127 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1128 color_def->red = 0;
1129 color_def->blue = 0;
1130 color_def->green = 0;
1132 if (*color_name)
1133 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1135 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1137 if (strcmp (color_name, "unspecified-fg") == 0)
1138 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1139 else if (strcmp (color_name, "unspecified-bg") == 0)
1140 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1143 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1144 status = 1;
1146 return status;
1150 /* Decide if color named COLOR_NAME is valid for the display
1151 associated with the frame F; if so, return the rgb values in
1152 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1154 This does the right thing for any type of frame. */
1156 static int
1157 defined_color (struct frame *f, const char *color_name, XColor *color_def, int alloc)
1159 if (!FRAME_WINDOW_P (f))
1160 return tty_defined_color (f, color_name, color_def, alloc);
1161 #ifdef HAVE_X_WINDOWS
1162 else if (FRAME_X_P (f))
1163 return x_defined_color (f, color_name, color_def, alloc);
1164 #endif
1165 #ifdef WINDOWSNT
1166 else if (FRAME_W32_P (f))
1167 return w32_defined_color (f, color_name, color_def, alloc);
1168 #endif
1169 #ifdef HAVE_NS
1170 else if (FRAME_NS_P (f))
1171 return ns_defined_color (f, color_name, color_def, alloc, 1);
1172 #endif
1173 else
1174 abort ();
1178 /* Given the index IDX of a tty color on frame F, return its name, a
1179 Lisp string. */
1181 Lisp_Object
1182 tty_color_name (struct frame *f, int idx)
1184 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1186 Lisp_Object frame;
1187 Lisp_Object coldesc;
1189 XSETFRAME (frame, f);
1190 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1192 if (!NILP (coldesc))
1193 return XCAR (coldesc);
1195 #ifdef MSDOS
1196 /* We can have an MSDOG frame under -nw for a short window of
1197 opportunity before internal_terminal_init is called. DTRT. */
1198 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1199 return msdos_stdcolor_name (idx);
1200 #endif
1202 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1203 return build_string (unspecified_fg);
1204 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1205 return build_string (unspecified_bg);
1207 return Qunspecified;
1211 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1212 black) on frame F.
1214 The criterion implemented here is not a terribly sophisticated one. */
1216 static int
1217 face_color_gray_p (struct frame *f, const char *color_name)
1219 XColor color;
1220 int gray_p;
1222 if (defined_color (f, color_name, &color, 0))
1223 gray_p = (/* Any color sufficiently close to black counts as grey. */
1224 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1226 ((eabs (color.red - color.green)
1227 < max (color.red, color.green) / 20)
1228 && (eabs (color.green - color.blue)
1229 < max (color.green, color.blue) / 20)
1230 && (eabs (color.blue - color.red)
1231 < max (color.blue, color.red) / 20)));
1232 else
1233 gray_p = 0;
1235 return gray_p;
1239 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1240 BACKGROUND_P non-zero means the color will be used as background
1241 color. */
1243 static int
1244 face_color_supported_p (struct frame *f, const char *color_name, int background_p)
1246 Lisp_Object frame;
1247 XColor not_used;
1249 XSETFRAME (frame, f);
1250 return
1251 #ifdef HAVE_WINDOW_SYSTEM
1252 FRAME_WINDOW_P (f)
1253 ? (!NILP (Fxw_display_color_p (frame))
1254 || xstrcasecmp (color_name, "black") == 0
1255 || xstrcasecmp (color_name, "white") == 0
1256 || (background_p
1257 && face_color_gray_p (f, color_name))
1258 || (!NILP (Fx_display_grayscale_p (frame))
1259 && face_color_gray_p (f, color_name)))
1261 #endif
1262 tty_defined_color (f, color_name, &not_used, 0);
1266 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1267 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1268 FRAME specifies the frame and thus the display for interpreting COLOR.
1269 If FRAME is nil or omitted, use the selected frame. */)
1270 (Lisp_Object color, Lisp_Object frame)
1272 struct frame *f;
1274 CHECK_STRING (color);
1275 if (NILP (frame))
1276 frame = selected_frame;
1277 else
1278 CHECK_FRAME (frame);
1279 f = XFRAME (frame);
1280 return face_color_gray_p (f, SSDATA (color)) ? Qt : Qnil;
1284 DEFUN ("color-supported-p", Fcolor_supported_p,
1285 Scolor_supported_p, 1, 3, 0,
1286 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1287 BACKGROUND-P non-nil means COLOR is used as a background.
1288 Otherwise, this function tells whether it can be used as a foreground.
1289 If FRAME is nil or omitted, use the selected frame.
1290 COLOR must be a valid color name. */)
1291 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1293 struct frame *f;
1295 CHECK_STRING (color);
1296 if (NILP (frame))
1297 frame = selected_frame;
1298 else
1299 CHECK_FRAME (frame);
1300 f = XFRAME (frame);
1301 if (face_color_supported_p (f, SSDATA (color), !NILP (background_p)))
1302 return Qt;
1303 return Qnil;
1307 /* Load color with name NAME for use by face FACE on frame F.
1308 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1309 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1310 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1311 pixel color. If color cannot be loaded, display a message, and
1312 return the foreground, background or underline color of F, but
1313 record that fact in flags of the face so that we don't try to free
1314 these colors. */
1316 unsigned long
1317 load_color (struct frame *f, struct face *face, Lisp_Object name, enum lface_attribute_index target_index)
1319 XColor color;
1321 xassert (STRINGP (name));
1322 xassert (target_index == LFACE_FOREGROUND_INDEX
1323 || target_index == LFACE_BACKGROUND_INDEX
1324 || target_index == LFACE_UNDERLINE_INDEX
1325 || target_index == LFACE_OVERLINE_INDEX
1326 || target_index == LFACE_STRIKE_THROUGH_INDEX
1327 || target_index == LFACE_BOX_INDEX);
1329 /* if the color map is full, defined_color will return a best match
1330 to the values in an existing cell. */
1331 if (!defined_color (f, SSDATA (name), &color, 1))
1333 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1335 switch (target_index)
1337 case LFACE_FOREGROUND_INDEX:
1338 face->foreground_defaulted_p = 1;
1339 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1340 break;
1342 case LFACE_BACKGROUND_INDEX:
1343 face->background_defaulted_p = 1;
1344 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1345 break;
1347 case LFACE_UNDERLINE_INDEX:
1348 face->underline_defaulted_p = 1;
1349 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1350 break;
1352 case LFACE_OVERLINE_INDEX:
1353 face->overline_color_defaulted_p = 1;
1354 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1355 break;
1357 case LFACE_STRIKE_THROUGH_INDEX:
1358 face->strike_through_color_defaulted_p = 1;
1359 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1360 break;
1362 case LFACE_BOX_INDEX:
1363 face->box_color_defaulted_p = 1;
1364 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1365 break;
1367 default:
1368 abort ();
1371 #if GLYPH_DEBUG
1372 else
1373 ++ncolors_allocated;
1374 #endif
1376 return color.pixel;
1380 #ifdef HAVE_WINDOW_SYSTEM
1382 /* Load colors for face FACE which is used on frame F. Colors are
1383 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1384 of ATTRS. If the background color specified is not supported on F,
1385 try to emulate gray colors with a stipple from Vface_default_stipple. */
1387 static void
1388 load_face_colors (struct frame *f, struct face *face, Lisp_Object *attrs)
1390 Lisp_Object fg, bg;
1392 bg = attrs[LFACE_BACKGROUND_INDEX];
1393 fg = attrs[LFACE_FOREGROUND_INDEX];
1395 /* Swap colors if face is inverse-video. */
1396 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1398 Lisp_Object tmp;
1399 tmp = fg;
1400 fg = bg;
1401 bg = tmp;
1404 /* Check for support for foreground, not for background because
1405 face_color_supported_p is smart enough to know that grays are
1406 "supported" as background because we are supposed to use stipple
1407 for them. */
1408 if (!face_color_supported_p (f, SSDATA (bg), 0)
1409 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1411 x_destroy_bitmap (f, face->stipple);
1412 face->stipple = load_pixmap (f, Vface_default_stipple,
1413 &face->pixmap_w, &face->pixmap_h);
1416 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1417 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1421 /* Free color PIXEL on frame F. */
1423 void
1424 unload_color (struct frame *f, long unsigned int pixel)
1426 #ifdef HAVE_X_WINDOWS
1427 if (pixel != -1)
1429 BLOCK_INPUT;
1430 x_free_colors (f, &pixel, 1);
1431 UNBLOCK_INPUT;
1433 #endif
1437 /* Free colors allocated for FACE. */
1439 static void
1440 free_face_colors (struct frame *f, struct face *face)
1442 /* PENDING(NS): need to do something here? */
1443 #ifdef HAVE_X_WINDOWS
1444 if (face->colors_copied_bitwise_p)
1445 return;
1447 BLOCK_INPUT;
1449 if (!face->foreground_defaulted_p)
1451 x_free_colors (f, &face->foreground, 1);
1452 IF_DEBUG (--ncolors_allocated);
1455 if (!face->background_defaulted_p)
1457 x_free_colors (f, &face->background, 1);
1458 IF_DEBUG (--ncolors_allocated);
1461 if (face->underline_p
1462 && !face->underline_defaulted_p)
1464 x_free_colors (f, &face->underline_color, 1);
1465 IF_DEBUG (--ncolors_allocated);
1468 if (face->overline_p
1469 && !face->overline_color_defaulted_p)
1471 x_free_colors (f, &face->overline_color, 1);
1472 IF_DEBUG (--ncolors_allocated);
1475 if (face->strike_through_p
1476 && !face->strike_through_color_defaulted_p)
1478 x_free_colors (f, &face->strike_through_color, 1);
1479 IF_DEBUG (--ncolors_allocated);
1482 if (face->box != FACE_NO_BOX
1483 && !face->box_color_defaulted_p)
1485 x_free_colors (f, &face->box_color, 1);
1486 IF_DEBUG (--ncolors_allocated);
1489 UNBLOCK_INPUT;
1490 #endif /* HAVE_X_WINDOWS */
1493 #endif /* HAVE_WINDOW_SYSTEM */
1497 /***********************************************************************
1498 XLFD Font Names
1499 ***********************************************************************/
1501 /* An enumerator for each field of an XLFD font name. */
1503 enum xlfd_field
1505 XLFD_FOUNDRY,
1506 XLFD_FAMILY,
1507 XLFD_WEIGHT,
1508 XLFD_SLANT,
1509 XLFD_SWIDTH,
1510 XLFD_ADSTYLE,
1511 XLFD_PIXEL_SIZE,
1512 XLFD_POINT_SIZE,
1513 XLFD_RESX,
1514 XLFD_RESY,
1515 XLFD_SPACING,
1516 XLFD_AVGWIDTH,
1517 XLFD_REGISTRY,
1518 XLFD_ENCODING,
1519 XLFD_LAST
1522 /* An enumerator for each possible slant value of a font. Taken from
1523 the XLFD specification. */
1525 enum xlfd_slant
1527 XLFD_SLANT_UNKNOWN,
1528 XLFD_SLANT_ROMAN,
1529 XLFD_SLANT_ITALIC,
1530 XLFD_SLANT_OBLIQUE,
1531 XLFD_SLANT_REVERSE_ITALIC,
1532 XLFD_SLANT_REVERSE_OBLIQUE,
1533 XLFD_SLANT_OTHER
1536 /* Relative font weight according to XLFD documentation. */
1538 enum xlfd_weight
1540 XLFD_WEIGHT_UNKNOWN,
1541 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1542 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1543 XLFD_WEIGHT_LIGHT, /* 30 */
1544 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1545 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1546 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1547 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1548 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1549 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1552 /* Relative proportionate width. */
1554 enum xlfd_swidth
1556 XLFD_SWIDTH_UNKNOWN,
1557 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1558 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1559 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1560 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1561 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1562 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1563 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1564 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1565 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1568 /* Order by which font selection chooses fonts. The default values
1569 mean `first, find a best match for the font width, then for the
1570 font height, then for weight, then for slant.' This variable can be
1571 set via set-face-font-sort-order. */
1573 static int font_sort_order[4];
1575 #ifdef HAVE_WINDOW_SYSTEM
1577 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1579 static int
1580 compare_fonts_by_sort_order (const void *v1, const void *v2)
1582 Lisp_Object font1 = *(Lisp_Object *) v1;
1583 Lisp_Object font2 = *(Lisp_Object *) v2;
1584 int i;
1586 for (i = 0; i < FONT_SIZE_INDEX; i++)
1588 enum font_property_index idx = font_props_for_sorting[i];
1589 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1590 int result;
1592 if (idx <= FONT_REGISTRY_INDEX)
1594 if (STRINGP (val1))
1595 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1596 else
1597 result = STRINGP (val2) ? 1 : 0;
1599 else
1601 if (INTEGERP (val1))
1602 result = INTEGERP (val2) ? XINT (val1) - XINT (val2) : -1;
1603 else
1604 result = INTEGERP (val2) ? 1 : 0;
1606 if (result)
1607 return result;
1609 return 0;
1612 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1613 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1614 If FAMILY is omitted or nil, list all families.
1615 Otherwise, FAMILY must be a string, possibly containing wildcards
1616 `?' and `*'.
1617 If FRAME is omitted or nil, use the selected frame.
1618 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1619 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1620 FAMILY is the font family name. POINT-SIZE is the size of the
1621 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1622 width, weight and slant of the font. These symbols are the same as for
1623 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1624 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1625 giving the registry and encoding of the font.
1626 The result list is sorted according to the current setting of
1627 the face font sort order. */)
1628 (Lisp_Object family, Lisp_Object frame)
1630 Lisp_Object font_spec, list, *drivers, vec;
1631 int i, nfonts, ndrivers;
1632 Lisp_Object result;
1634 if (NILP (frame))
1635 frame = selected_frame;
1636 CHECK_LIVE_FRAME (frame);
1638 font_spec = Ffont_spec (0, NULL);
1639 if (!NILP (family))
1641 CHECK_STRING (family);
1642 font_parse_family_registry (family, Qnil, font_spec);
1645 list = font_list_entities (frame, font_spec);
1646 if (NILP (list))
1647 return Qnil;
1649 /* Sort the font entities. */
1650 for (i = 0; i < 4; i++)
1651 switch (font_sort_order[i])
1653 case XLFD_SWIDTH:
1654 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1655 case XLFD_POINT_SIZE:
1656 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1657 case XLFD_WEIGHT:
1658 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1659 default:
1660 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1662 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1663 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1664 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1665 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1667 ndrivers = XINT (Flength (list));
1668 drivers = alloca (sizeof (Lisp_Object) * ndrivers);
1669 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1670 drivers[i] = XCAR (list);
1671 vec = Fvconcat (ndrivers, drivers);
1672 nfonts = ASIZE (vec);
1674 qsort (XVECTOR (vec)->contents, nfonts, sizeof (Lisp_Object),
1675 compare_fonts_by_sort_order);
1677 result = Qnil;
1678 for (i = nfonts - 1; i >= 0; --i)
1680 Lisp_Object font = AREF (vec, i);
1681 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
1682 int point;
1683 Lisp_Object spacing;
1685 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1686 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1687 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1688 XFRAME (frame)->resy);
1689 ASET (v, 2, make_number (point));
1690 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1691 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1692 spacing = Ffont_get (font, QCspacing);
1693 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1694 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1695 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1697 result = Fcons (v, result);
1700 return result;
1703 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1704 doc: /* Return a list of the names of available fonts matching PATTERN.
1705 If optional arguments FACE and FRAME are specified, return only fonts
1706 the same size as FACE on FRAME.
1708 PATTERN should be a string containing a font name in the XLFD,
1709 Fontconfig, or GTK format. A font name given in the XLFD format may
1710 contain wildcard characters:
1711 the * character matches any substring, and
1712 the ? character matches any single character.
1713 PATTERN is case-insensitive.
1715 The return value is a list of strings, suitable as arguments to
1716 `set-face-font'.
1718 Fonts Emacs can't use may or may not be excluded
1719 even if they match PATTERN and FACE.
1720 The optional fourth argument MAXIMUM sets a limit on how many
1721 fonts to match. The first MAXIMUM fonts are reported.
1722 The optional fifth argument WIDTH, if specified, is a number of columns
1723 occupied by a character of a font. In that case, return only fonts
1724 the WIDTH times as wide as FACE on FRAME. */)
1725 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame, Lisp_Object maximum, Lisp_Object width)
1727 struct frame *f;
1728 int size, avgwidth IF_LINT (= 0);
1730 check_x ();
1731 CHECK_STRING (pattern);
1733 if (! NILP (maximum))
1734 CHECK_NATNUM (maximum);
1736 if (!NILP (width))
1737 CHECK_NUMBER (width);
1739 /* We can't simply call check_x_frame because this function may be
1740 called before any frame is created. */
1741 if (NILP (frame))
1742 frame = selected_frame;
1743 f = frame_or_selected_frame (frame, 2);
1744 if (! FRAME_WINDOW_P (f))
1746 /* Perhaps we have not yet created any frame. */
1747 f = NULL;
1748 frame = Qnil;
1749 face = Qnil;
1752 /* Determine the width standard for comparison with the fonts we find. */
1754 if (NILP (face))
1755 size = 0;
1756 else
1758 /* This is of limited utility since it works with character
1759 widths. Keep it for compatibility. --gerd. */
1760 int face_id = lookup_named_face (f, face, 0);
1761 struct face *width_face = (face_id < 0
1762 ? NULL
1763 : FACE_FROM_ID (f, face_id));
1765 if (width_face && width_face->font)
1767 size = width_face->font->pixel_size;
1768 avgwidth = width_face->font->average_width;
1770 else
1772 size = FRAME_FONT (f)->pixel_size;
1773 avgwidth = FRAME_FONT (f)->average_width;
1775 if (!NILP (width))
1776 avgwidth *= XINT (width);
1780 Lisp_Object font_spec;
1781 Lisp_Object args[2], tail;
1783 font_spec = font_spec_from_name (pattern);
1784 if (!FONTP (font_spec))
1785 signal_error ("Invalid font name", pattern);
1787 if (size)
1789 Ffont_put (font_spec, QCsize, make_number (size));
1790 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1792 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1793 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1795 Lisp_Object font_entity;
1797 font_entity = XCAR (tail);
1798 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1799 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1800 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1802 /* This is a scalable font. For backward compatibility,
1803 we set the specified size. */
1804 font_entity = copy_font_spec (font_entity);
1805 ASET (font_entity, FONT_SIZE_INDEX,
1806 AREF (font_spec, FONT_SIZE_INDEX));
1808 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1810 if (NILP (frame))
1811 /* We don't have to check fontsets. */
1812 return args[0];
1813 args[1] = list_fontsets (f, pattern, size);
1814 return Fnconc (2, args);
1818 #endif /* HAVE_WINDOW_SYSTEM */
1821 /***********************************************************************
1822 Lisp Faces
1823 ***********************************************************************/
1825 /* Access face attributes of face LFACE, a Lisp vector. */
1827 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1828 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1829 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1830 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1831 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1832 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1833 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1834 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1835 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1836 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1837 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1838 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1839 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1840 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1841 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1842 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1843 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1845 #if XASSERTS
1846 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1847 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1849 #define LFACEP(LFACE) \
1850 (VECTORP (LFACE) \
1851 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1852 && EQ (AREF (LFACE, 0), Qface))
1853 #endif
1856 #if GLYPH_DEBUG
1858 /* Check consistency of Lisp face attribute vector ATTRS. */
1860 static void
1861 check_lface_attrs (Lisp_Object *attrs)
1863 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1864 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1865 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1866 xassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1867 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1868 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1869 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1870 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1871 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1872 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1873 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1874 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1875 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1876 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1877 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1878 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1879 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1880 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1881 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1882 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1883 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1884 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1885 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1886 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
1887 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1888 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1889 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1890 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1891 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1892 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1893 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1894 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1895 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1896 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1897 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1898 || STRINGP (attrs[LFACE_BOX_INDEX])
1899 || INTEGERP (attrs[LFACE_BOX_INDEX])
1900 || CONSP (attrs[LFACE_BOX_INDEX]));
1901 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1902 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1903 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1904 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1905 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1906 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1907 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1908 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1909 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1910 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1911 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1912 || NILP (attrs[LFACE_INHERIT_INDEX])
1913 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1914 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1915 #ifdef HAVE_WINDOW_SYSTEM
1916 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1917 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1918 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1919 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1920 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1921 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1922 || FONTP (attrs[LFACE_FONT_INDEX]));
1923 xassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1924 || STRINGP (attrs[LFACE_FONTSET_INDEX]));
1925 #endif
1929 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1931 static void
1932 check_lface (Lisp_Object lface)
1934 if (!NILP (lface))
1936 xassert (LFACEP (lface));
1937 check_lface_attrs (XVECTOR (lface)->contents);
1941 #else /* GLYPH_DEBUG == 0 */
1943 #define check_lface_attrs(attrs) (void) 0
1944 #define check_lface(lface) (void) 0
1946 #endif /* GLYPH_DEBUG == 0 */
1950 /* Face-merge cycle checking. */
1952 enum named_merge_point_kind
1954 NAMED_MERGE_POINT_NORMAL,
1955 NAMED_MERGE_POINT_REMAP
1958 /* A `named merge point' is simply a point during face-merging where we
1959 look up a face by name. We keep a stack of which named lookups we're
1960 currently processing so that we can easily detect cycles, using a
1961 linked- list of struct named_merge_point structures, typically
1962 allocated on the stack frame of the named lookup functions which are
1963 active (so no consing is required). */
1964 struct named_merge_point
1966 Lisp_Object face_name;
1967 enum named_merge_point_kind named_merge_point_kind;
1968 struct named_merge_point *prev;
1972 /* If a face merging cycle is detected for FACE_NAME, return 0,
1973 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1974 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1975 pointed to by NAMED_MERGE_POINTS, and return 1. */
1977 static inline int
1978 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1979 Lisp_Object face_name,
1980 enum named_merge_point_kind named_merge_point_kind,
1981 struct named_merge_point **named_merge_points)
1983 struct named_merge_point *prev;
1985 for (prev = *named_merge_points; prev; prev = prev->prev)
1986 if (EQ (face_name, prev->face_name))
1988 if (prev->named_merge_point_kind == named_merge_point_kind)
1989 /* A cycle, so fail. */
1990 return 0;
1991 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1992 /* A remap `hides ' any previous normal merge points
1993 (because the remap means that it's actually different face),
1994 so as we know the current merge point must be normal, we
1995 can just assume it's OK. */
1996 break;
1999 new_named_merge_point->face_name = face_name;
2000 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
2001 new_named_merge_point->prev = *named_merge_points;
2003 *named_merge_points = new_named_merge_point;
2005 return 1;
2009 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
2010 to make it a symbol. If FACE_NAME is an alias for another face,
2011 return that face's name.
2013 Return default face in case of errors. */
2015 static Lisp_Object
2016 resolve_face_name (Lisp_Object face_name, int signal_p)
2018 Lisp_Object orig_face;
2019 Lisp_Object tortoise, hare;
2021 if (STRINGP (face_name))
2022 face_name = intern (SSDATA (face_name));
2024 if (NILP (face_name) || !SYMBOLP (face_name))
2025 return face_name;
2027 orig_face = face_name;
2028 tortoise = hare = face_name;
2030 while (1)
2032 face_name = hare;
2033 hare = Fget (hare, Qface_alias);
2034 if (NILP (hare) || !SYMBOLP (hare))
2035 break;
2037 face_name = hare;
2038 hare = Fget (hare, Qface_alias);
2039 if (NILP (hare) || !SYMBOLP (hare))
2040 break;
2042 tortoise = Fget (tortoise, Qface_alias);
2043 if (EQ (hare, tortoise))
2045 if (signal_p)
2046 xsignal1 (Qcircular_list, orig_face);
2047 return Qdefault;
2051 return face_name;
2055 /* Return the face definition of FACE_NAME on frame F. F null means
2056 return the definition for new frames. FACE_NAME may be a string or
2057 a symbol (apparently Emacs 20.2 allowed strings as face names in
2058 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
2059 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
2060 is zero, value is nil if FACE_NAME is not a valid face name. */
2061 static inline Lisp_Object
2062 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name, int signal_p)
2064 Lisp_Object lface;
2066 if (f)
2067 lface = assq_no_quit (face_name, f->face_alist);
2068 else
2069 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2071 if (CONSP (lface))
2072 lface = XCDR (lface);
2073 else if (signal_p)
2074 signal_error ("Invalid face", face_name);
2076 check_lface (lface);
2078 return lface;
2081 /* Return the face definition of FACE_NAME on frame F. F null means
2082 return the definition for new frames. FACE_NAME may be a string or
2083 a symbol (apparently Emacs 20.2 allowed strings as face names in
2084 face text properties; Ediff uses that). If FACE_NAME is an alias
2085 for another face, return that face's definition. If SIGNAL_P is
2086 non-zero, signal an error if FACE_NAME is not a valid face name.
2087 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
2088 name. */
2089 static inline Lisp_Object
2090 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
2092 face_name = resolve_face_name (face_name, signal_p);
2093 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2097 /* Get face attributes of face FACE_NAME from frame-local faces on
2098 frame F. Store the resulting attributes in ATTRS which must point
2099 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2100 is non-zero, signal an error if FACE_NAME does not name a face.
2101 Otherwise, value is zero if FACE_NAME is not a face. */
2103 static inline int
2104 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name, Lisp_Object *attrs, int signal_p)
2106 Lisp_Object lface;
2108 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2110 if (! NILP (lface))
2111 memcpy (attrs, XVECTOR (lface)->contents,
2112 LFACE_VECTOR_SIZE * sizeof *attrs);
2114 return !NILP (lface);
2117 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2118 F. Store the resulting attributes in ATTRS which must point to a
2119 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2120 alias for another face, use that face's definition. If SIGNAL_P is
2121 non-zero, signal an error if FACE_NAME does not name a face.
2122 Otherwise, value is zero if FACE_NAME is not a face. */
2124 static inline int
2125 get_lface_attributes (struct frame *f, Lisp_Object face_name, Lisp_Object *attrs, int signal_p, struct named_merge_point *named_merge_points)
2127 Lisp_Object face_remapping;
2129 face_name = resolve_face_name (face_name, signal_p);
2131 /* See if SYMBOL has been remapped to some other face (usually this
2132 is done buffer-locally). */
2133 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2134 if (CONSP (face_remapping))
2136 struct named_merge_point named_merge_point;
2138 if (push_named_merge_point (&named_merge_point,
2139 face_name, NAMED_MERGE_POINT_REMAP,
2140 &named_merge_points))
2142 int i;
2144 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2145 attrs[i] = Qunspecified;
2147 return merge_face_ref (f, XCDR (face_remapping), attrs,
2148 signal_p, named_merge_points);
2152 /* Default case, no remapping. */
2153 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2157 /* Non-zero if all attributes in face attribute vector ATTRS are
2158 specified, i.e. are non-nil. */
2160 static int
2161 lface_fully_specified_p (Lisp_Object *attrs)
2163 int i;
2165 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2166 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2167 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2168 break;
2170 return i == LFACE_VECTOR_SIZE;
2173 #ifdef HAVE_WINDOW_SYSTEM
2175 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2176 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2177 exception is `font' attribute. It is set to FONT_OBJECT regardless
2178 of FORCE_P. */
2180 static int
2181 set_lface_from_font (struct frame *f, Lisp_Object lface, Lisp_Object font_object, int force_p)
2183 Lisp_Object val;
2184 struct font *font = XFONT_OBJECT (font_object);
2186 /* Set attributes only if unspecified, otherwise face defaults for
2187 new frames would never take effect. If the font doesn't have a
2188 specific property, set a normal value for that. */
2190 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2192 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2194 LFACE_FAMILY (lface) = SYMBOL_NAME (family);
2197 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2199 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2201 LFACE_FOUNDRY (lface) = SYMBOL_NAME (foundry);
2204 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2206 int pt = PIXEL_TO_POINT (font->pixel_size * 10, f->resy);
2208 xassert (pt > 0);
2209 LFACE_HEIGHT (lface) = make_number (pt);
2212 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2214 val = FONT_WEIGHT_FOR_FACE (font_object);
2215 LFACE_WEIGHT (lface) = ! NILP (val) ? val :Qnormal;
2217 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2219 val = FONT_SLANT_FOR_FACE (font_object);
2220 LFACE_SLANT (lface) = ! NILP (val) ? val : Qnormal;
2222 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2224 val = FONT_WIDTH_FOR_FACE (font_object);
2225 LFACE_SWIDTH (lface) = ! NILP (val) ? val : Qnormal;
2228 LFACE_FONT (lface) = font_object;
2229 return 1;
2232 #endif /* HAVE_WINDOW_SYSTEM */
2235 /* Merges the face height FROM with the face height TO, and returns the
2236 merged height. If FROM is an invalid height, then INVALID is
2237 returned instead. FROM and TO may be either absolute face heights or
2238 `relative' heights; the returned value is always an absolute height
2239 unless both FROM and TO are relative. */
2241 static Lisp_Object
2242 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2244 Lisp_Object result = invalid;
2246 if (INTEGERP (from))
2247 /* FROM is absolute, just use it as is. */
2248 result = from;
2249 else if (FLOATP (from))
2250 /* FROM is a scale, use it to adjust TO. */
2252 if (INTEGERP (to))
2253 /* relative X absolute => absolute */
2254 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
2255 else if (FLOATP (to))
2256 /* relative X relative => relative */
2257 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2258 else if (UNSPECIFIEDP (to))
2259 result = from;
2261 else if (FUNCTIONP (from))
2262 /* FROM is a function, which use to adjust TO. */
2264 /* Call function with current height as argument.
2265 From is the new height. */
2266 Lisp_Object args[2];
2268 args[0] = from;
2269 args[1] = to;
2270 result = safe_call (2, args);
2272 /* Ensure that if TO was absolute, so is the result. */
2273 if (INTEGERP (to) && !INTEGERP (result))
2274 result = invalid;
2277 return result;
2281 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2282 store the resulting attributes in TO, which must be already be
2283 completely specified and contain only absolute attributes. Every
2284 specified attribute of FROM overrides the corresponding attribute of
2285 TO; relative attributes in FROM are merged with the absolute value in
2286 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2287 loops in face inheritance/remapping; it should be 0 when called from
2288 other places. */
2290 static inline void
2291 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to, struct named_merge_point *named_merge_points)
2293 int i;
2295 /* If FROM inherits from some other faces, merge their attributes into
2296 TO before merging FROM's direct attributes. Note that an :inherit
2297 attribute of `unspecified' is the same as one of nil; we never
2298 merge :inherit attributes, so nil is more correct, but lots of
2299 other code uses `unspecified' as a generic value for face attributes. */
2300 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2301 && !NILP (from[LFACE_INHERIT_INDEX]))
2302 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2304 i = LFACE_FONT_INDEX;
2305 if (!UNSPECIFIEDP (from[i]))
2307 if (!UNSPECIFIEDP (to[i]))
2308 to[i] = merge_font_spec (from[i], to[i]);
2309 else
2310 to[i] = copy_font_spec (from[i]);
2311 if (! NILP (AREF (to[i], FONT_FOUNDRY_INDEX)))
2312 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (to[i], FONT_FOUNDRY_INDEX));
2313 if (! NILP (AREF (to[i], FONT_FAMILY_INDEX)))
2314 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (to[i], FONT_FAMILY_INDEX));
2315 if (! NILP (AREF (to[i], FONT_WEIGHT_INDEX)))
2316 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (to[i]);
2317 if (! NILP (AREF (to[i], FONT_SLANT_INDEX)))
2318 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (to[i]);
2319 if (! NILP (AREF (to[i], FONT_WIDTH_INDEX)))
2320 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (to[i]);
2321 ASET (to[i], FONT_SIZE_INDEX, Qnil);
2324 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2325 if (!UNSPECIFIEDP (from[i]))
2327 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2329 to[i] = merge_face_heights (from[i], to[i], to[i]);
2330 font_clear_prop (to, FONT_SIZE_INDEX);
2332 else if (i != LFACE_FONT_INDEX
2333 && ! EQ (to[i], from[i]))
2335 to[i] = from[i];
2336 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2337 font_clear_prop (to,
2338 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2339 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2340 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2341 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2342 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2343 : FONT_SLANT_INDEX));
2347 /* TO is always an absolute face, which should inherit from nothing.
2348 We blindly copy the :inherit attribute above and fix it up here. */
2349 to[LFACE_INHERIT_INDEX] = Qnil;
2352 /* Merge the named face FACE_NAME on frame F, into the vector of face
2353 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2354 inheritance. Returns true if FACE_NAME is a valid face name and
2355 merging succeeded. */
2357 static int
2358 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to, struct named_merge_point *named_merge_points)
2360 struct named_merge_point named_merge_point;
2362 if (push_named_merge_point (&named_merge_point,
2363 face_name, NAMED_MERGE_POINT_NORMAL,
2364 &named_merge_points))
2366 struct gcpro gcpro1;
2367 Lisp_Object from[LFACE_VECTOR_SIZE];
2368 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2370 if (ok)
2372 GCPRO1 (named_merge_point.face_name);
2373 merge_face_vectors (f, from, to, named_merge_points);
2374 UNGCPRO;
2377 return ok;
2379 else
2380 return 0;
2384 /* Merge face attributes from the lisp `face reference' FACE_REF on
2385 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2386 problems with FACE_REF cause an error message to be shown. Return
2387 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2388 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2389 list structure; it may be 0 for most callers.
2391 FACE_REF may be a single face specification or a list of such
2392 specifications. Each face specification can be:
2394 1. A symbol or string naming a Lisp face.
2396 2. A property list of the form (KEYWORD VALUE ...) where each
2397 KEYWORD is a face attribute name, and value is an appropriate value
2398 for that attribute.
2400 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2401 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2402 for compatibility with 20.2.
2404 Face specifications earlier in lists take precedence over later
2405 specifications. */
2407 static int
2408 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to, int err_msgs, struct named_merge_point *named_merge_points)
2410 int ok = 1; /* Succeed without an error? */
2412 if (CONSP (face_ref))
2414 Lisp_Object first = XCAR (face_ref);
2416 if (EQ (first, Qforeground_color)
2417 || EQ (first, Qbackground_color))
2419 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2420 . COLOR). COLOR must be a string. */
2421 Lisp_Object color_name = XCDR (face_ref);
2422 Lisp_Object color = first;
2424 if (STRINGP (color_name))
2426 if (EQ (color, Qforeground_color))
2427 to[LFACE_FOREGROUND_INDEX] = color_name;
2428 else
2429 to[LFACE_BACKGROUND_INDEX] = color_name;
2431 else
2433 if (err_msgs)
2434 add_to_log ("Invalid face color", color_name, Qnil);
2435 ok = 0;
2438 else if (SYMBOLP (first)
2439 && *SDATA (SYMBOL_NAME (first)) == ':')
2441 /* Assume this is the property list form. */
2442 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2444 Lisp_Object keyword = XCAR (face_ref);
2445 Lisp_Object value = XCAR (XCDR (face_ref));
2446 int err = 0;
2448 /* Specifying `unspecified' is a no-op. */
2449 if (EQ (value, Qunspecified))
2451 else if (EQ (keyword, QCfamily))
2453 if (STRINGP (value))
2455 to[LFACE_FAMILY_INDEX] = value;
2456 font_clear_prop (to, FONT_FAMILY_INDEX);
2458 else
2459 err = 1;
2461 else if (EQ (keyword, QCfoundry))
2463 if (STRINGP (value))
2465 to[LFACE_FOUNDRY_INDEX] = value;
2466 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2468 else
2469 err = 1;
2471 else if (EQ (keyword, QCheight))
2473 Lisp_Object new_height =
2474 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2476 if (! NILP (new_height))
2478 to[LFACE_HEIGHT_INDEX] = new_height;
2479 font_clear_prop (to, FONT_SIZE_INDEX);
2481 else
2482 err = 1;
2484 else if (EQ (keyword, QCweight))
2486 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2488 to[LFACE_WEIGHT_INDEX] = value;
2489 font_clear_prop (to, FONT_WEIGHT_INDEX);
2491 else
2492 err = 1;
2494 else if (EQ (keyword, QCslant))
2496 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2498 to[LFACE_SLANT_INDEX] = value;
2499 font_clear_prop (to, FONT_SLANT_INDEX);
2501 else
2502 err = 1;
2504 else if (EQ (keyword, QCunderline))
2506 if (EQ (value, Qt)
2507 || NILP (value)
2508 || STRINGP (value))
2509 to[LFACE_UNDERLINE_INDEX] = value;
2510 else
2511 err = 1;
2513 else if (EQ (keyword, QCoverline))
2515 if (EQ (value, Qt)
2516 || NILP (value)
2517 || STRINGP (value))
2518 to[LFACE_OVERLINE_INDEX] = value;
2519 else
2520 err = 1;
2522 else if (EQ (keyword, QCstrike_through))
2524 if (EQ (value, Qt)
2525 || NILP (value)
2526 || STRINGP (value))
2527 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2528 else
2529 err = 1;
2531 else if (EQ (keyword, QCbox))
2533 if (EQ (value, Qt))
2534 value = make_number (1);
2535 if (INTEGERP (value)
2536 || STRINGP (value)
2537 || CONSP (value)
2538 || NILP (value))
2539 to[LFACE_BOX_INDEX] = value;
2540 else
2541 err = 1;
2543 else if (EQ (keyword, QCinverse_video)
2544 || EQ (keyword, QCreverse_video))
2546 if (EQ (value, Qt) || NILP (value))
2547 to[LFACE_INVERSE_INDEX] = value;
2548 else
2549 err = 1;
2551 else if (EQ (keyword, QCforeground))
2553 if (STRINGP (value))
2554 to[LFACE_FOREGROUND_INDEX] = value;
2555 else
2556 err = 1;
2558 else if (EQ (keyword, QCbackground))
2560 if (STRINGP (value))
2561 to[LFACE_BACKGROUND_INDEX] = value;
2562 else
2563 err = 1;
2565 else if (EQ (keyword, QCstipple))
2567 #if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
2568 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2569 if (!NILP (pixmap_p))
2570 to[LFACE_STIPPLE_INDEX] = value;
2571 else
2572 err = 1;
2573 #endif
2575 else if (EQ (keyword, QCwidth))
2577 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2579 to[LFACE_SWIDTH_INDEX] = value;
2580 font_clear_prop (to, FONT_WIDTH_INDEX);
2582 else
2583 err = 1;
2585 else if (EQ (keyword, QCinherit))
2587 /* This is not really very useful; it's just like a
2588 normal face reference. */
2589 if (! merge_face_ref (f, value, to,
2590 err_msgs, named_merge_points))
2591 err = 1;
2593 else
2594 err = 1;
2596 if (err)
2598 add_to_log ("Invalid face attribute %S %S", keyword, value);
2599 ok = 0;
2602 face_ref = XCDR (XCDR (face_ref));
2605 else
2607 /* This is a list of face refs. Those at the beginning of the
2608 list take precedence over what follows, so we have to merge
2609 from the end backwards. */
2610 Lisp_Object next = XCDR (face_ref);
2612 if (! NILP (next))
2613 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2615 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2616 ok = 0;
2619 else
2621 /* FACE_REF ought to be a face name. */
2622 ok = merge_named_face (f, face_ref, to, named_merge_points);
2623 if (!ok && err_msgs)
2624 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2627 return ok;
2631 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2632 Sinternal_make_lisp_face, 1, 2, 0,
2633 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2634 If FACE was not known as a face before, create a new one.
2635 If optional argument FRAME is specified, make a frame-local face
2636 for that frame. Otherwise operate on the global face definition.
2637 Value is a vector of face attributes. */)
2638 (Lisp_Object face, Lisp_Object frame)
2640 Lisp_Object global_lface, lface;
2641 struct frame *f;
2642 int i;
2644 CHECK_SYMBOL (face);
2645 global_lface = lface_from_face_name (NULL, face, 0);
2647 if (!NILP (frame))
2649 CHECK_LIVE_FRAME (frame);
2650 f = XFRAME (frame);
2651 lface = lface_from_face_name (f, face, 0);
2653 else
2654 f = NULL, lface = Qnil;
2656 /* Add a global definition if there is none. */
2657 if (NILP (global_lface))
2659 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2660 Qunspecified);
2661 ASET (global_lface, 0, Qface);
2662 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2663 Vface_new_frame_defaults);
2665 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2666 face id to Lisp face is given by the vector lface_id_to_name.
2667 The mapping from Lisp face to Lisp face id is given by the
2668 property `face' of the Lisp face name. */
2669 if (next_lface_id == lface_id_to_name_size)
2671 int new_size = max (50, 2 * lface_id_to_name_size);
2672 int sz = new_size * sizeof *lface_id_to_name;
2673 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
2674 lface_id_to_name_size = new_size;
2677 lface_id_to_name[next_lface_id] = face;
2678 Fput (face, Qface, make_number (next_lface_id));
2679 ++next_lface_id;
2681 else if (f == NULL)
2682 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2683 ASET (global_lface, i, Qunspecified);
2685 /* Add a frame-local definition. */
2686 if (f)
2688 if (NILP (lface))
2690 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2691 Qunspecified);
2692 ASET (lface, 0, Qface);
2693 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
2695 else
2696 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2697 ASET (lface, i, Qunspecified);
2699 else
2700 lface = global_lface;
2702 /* Changing a named face means that all realized faces depending on
2703 that face are invalid. Since we cannot tell which realized faces
2704 depend on the face, make sure they are all removed. This is done
2705 by incrementing face_change_count. The next call to
2706 init_iterator will then free realized faces. */
2707 if (NILP (Fget (face, Qface_no_inherit)))
2709 ++face_change_count;
2710 ++windows_or_buffers_changed;
2713 xassert (LFACEP (lface));
2714 check_lface (lface);
2715 return lface;
2719 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2720 Sinternal_lisp_face_p, 1, 2, 0,
2721 doc: /* Return non-nil if FACE names a face.
2722 FACE should be a symbol or string.
2723 If optional second argument FRAME is non-nil, check for the
2724 existence of a frame-local face with name FACE on that frame.
2725 Otherwise check for the existence of a global face. */)
2726 (Lisp_Object face, Lisp_Object frame)
2728 Lisp_Object lface;
2730 face = resolve_face_name (face, 1);
2732 if (!NILP (frame))
2734 CHECK_LIVE_FRAME (frame);
2735 lface = lface_from_face_name (XFRAME (frame), face, 0);
2737 else
2738 lface = lface_from_face_name (NULL, face, 0);
2740 return lface;
2744 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2745 Sinternal_copy_lisp_face, 4, 4, 0,
2746 doc: /* Copy face FROM to TO.
2747 If FRAME is t, copy the global face definition of FROM.
2748 Otherwise, copy the frame-local definition of FROM on FRAME.
2749 If NEW-FRAME is a frame, copy that data into the frame-local
2750 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2751 FRAME controls where the data is copied to.
2753 The value is TO. */)
2754 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2756 Lisp_Object lface, copy;
2758 CHECK_SYMBOL (from);
2759 CHECK_SYMBOL (to);
2761 if (EQ (frame, Qt))
2763 /* Copy global definition of FROM. We don't make copies of
2764 strings etc. because 20.2 didn't do it either. */
2765 lface = lface_from_face_name (NULL, from, 1);
2766 copy = Finternal_make_lisp_face (to, Qnil);
2768 else
2770 /* Copy frame-local definition of FROM. */
2771 if (NILP (new_frame))
2772 new_frame = frame;
2773 CHECK_LIVE_FRAME (frame);
2774 CHECK_LIVE_FRAME (new_frame);
2775 lface = lface_from_face_name (XFRAME (frame), from, 1);
2776 copy = Finternal_make_lisp_face (to, new_frame);
2779 memcpy (XVECTOR (copy)->contents, XVECTOR (lface)->contents,
2780 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
2782 /* Changing a named face means that all realized faces depending on
2783 that face are invalid. Since we cannot tell which realized faces
2784 depend on the face, make sure they are all removed. This is done
2785 by incrementing face_change_count. The next call to
2786 init_iterator will then free realized faces. */
2787 if (NILP (Fget (to, Qface_no_inherit)))
2789 ++face_change_count;
2790 ++windows_or_buffers_changed;
2793 return to;
2797 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2798 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2799 doc: /* Set attribute ATTR of FACE to VALUE.
2800 FRAME being a frame means change the face on that frame.
2801 FRAME nil means change the face of the selected frame.
2802 FRAME t means change the default for new frames.
2803 FRAME 0 means change the face on all frames, and change the default
2804 for new frames. */)
2805 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2807 Lisp_Object lface;
2808 Lisp_Object old_value = Qnil;
2809 /* Set one of enum font_property_index (> 0) if ATTR is one of
2810 font-related attributes other than QCfont and QCfontset. */
2811 enum font_property_index prop_index = 0;
2813 CHECK_SYMBOL (face);
2814 CHECK_SYMBOL (attr);
2816 face = resolve_face_name (face, 1);
2818 /* If FRAME is 0, change face on all frames, and change the
2819 default for new frames. */
2820 if (INTEGERP (frame) && XINT (frame) == 0)
2822 Lisp_Object tail;
2823 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2824 FOR_EACH_FRAME (tail, frame)
2825 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2826 return face;
2829 /* Set lface to the Lisp attribute vector of FACE. */
2830 if (EQ (frame, Qt))
2832 lface = lface_from_face_name (NULL, face, 1);
2834 /* When updating face-new-frame-defaults, we put :ignore-defface
2835 where the caller wants `unspecified'. This forces the frame
2836 defaults to ignore the defface value. Otherwise, the defface
2837 will take effect, which is generally not what is intended.
2838 The value of that attribute will be inherited from some other
2839 face during face merging. See internal_merge_in_global_face. */
2840 if (UNSPECIFIEDP (value))
2841 value = Qignore_defface;
2843 else
2845 if (NILP (frame))
2846 frame = selected_frame;
2848 CHECK_LIVE_FRAME (frame);
2849 lface = lface_from_face_name (XFRAME (frame), face, 0);
2851 /* If a frame-local face doesn't exist yet, create one. */
2852 if (NILP (lface))
2853 lface = Finternal_make_lisp_face (face, frame);
2856 if (EQ (attr, QCfamily))
2858 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2860 CHECK_STRING (value);
2861 if (SCHARS (value) == 0)
2862 signal_error ("Invalid face family", value);
2864 old_value = LFACE_FAMILY (lface);
2865 LFACE_FAMILY (lface) = value;
2866 prop_index = FONT_FAMILY_INDEX;
2868 else if (EQ (attr, QCfoundry))
2870 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2872 CHECK_STRING (value);
2873 if (SCHARS (value) == 0)
2874 signal_error ("Invalid face foundry", value);
2876 old_value = LFACE_FOUNDRY (lface);
2877 LFACE_FOUNDRY (lface) = value;
2878 prop_index = FONT_FOUNDRY_INDEX;
2880 else if (EQ (attr, QCheight))
2882 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2884 if (EQ (face, Qdefault))
2886 /* The default face must have an absolute size. */
2887 if (!INTEGERP (value) || XINT (value) <= 0)
2888 signal_error ("Default face height not absolute and positive", value);
2890 else
2892 /* For non-default faces, do a test merge with a random
2893 height to see if VALUE's ok. */
2894 Lisp_Object test = merge_face_heights (value,
2895 make_number (10),
2896 Qnil);
2897 if (!INTEGERP (test) || XINT (test) <= 0)
2898 signal_error ("Face height does not produce a positive integer", value);
2902 old_value = LFACE_HEIGHT (lface);
2903 LFACE_HEIGHT (lface) = value;
2904 prop_index = FONT_SIZE_INDEX;
2906 else if (EQ (attr, QCweight))
2908 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2910 CHECK_SYMBOL (value);
2911 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2912 signal_error ("Invalid face weight", value);
2914 old_value = LFACE_WEIGHT (lface);
2915 LFACE_WEIGHT (lface) = value;
2916 prop_index = FONT_WEIGHT_INDEX;
2918 else if (EQ (attr, QCslant))
2920 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2922 CHECK_SYMBOL (value);
2923 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2924 signal_error ("Invalid face slant", value);
2926 old_value = LFACE_SLANT (lface);
2927 LFACE_SLANT (lface) = value;
2928 prop_index = FONT_SLANT_INDEX;
2930 else if (EQ (attr, QCunderline))
2932 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2933 if ((SYMBOLP (value)
2934 && !EQ (value, Qt)
2935 && !EQ (value, Qnil))
2936 /* Underline color. */
2937 || (STRINGP (value)
2938 && SCHARS (value) == 0))
2939 signal_error ("Invalid face underline", value);
2941 old_value = LFACE_UNDERLINE (lface);
2942 LFACE_UNDERLINE (lface) = value;
2944 else if (EQ (attr, QCoverline))
2946 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2947 if ((SYMBOLP (value)
2948 && !EQ (value, Qt)
2949 && !EQ (value, Qnil))
2950 /* Overline color. */
2951 || (STRINGP (value)
2952 && SCHARS (value) == 0))
2953 signal_error ("Invalid face overline", value);
2955 old_value = LFACE_OVERLINE (lface);
2956 LFACE_OVERLINE (lface) = value;
2958 else if (EQ (attr, QCstrike_through))
2960 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2961 if ((SYMBOLP (value)
2962 && !EQ (value, Qt)
2963 && !EQ (value, Qnil))
2964 /* Strike-through color. */
2965 || (STRINGP (value)
2966 && SCHARS (value) == 0))
2967 signal_error ("Invalid face strike-through", value);
2969 old_value = LFACE_STRIKE_THROUGH (lface);
2970 LFACE_STRIKE_THROUGH (lface) = value;
2972 else if (EQ (attr, QCbox))
2974 int valid_p;
2976 /* Allow t meaning a simple box of width 1 in foreground color
2977 of the face. */
2978 if (EQ (value, Qt))
2979 value = make_number (1);
2981 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2982 valid_p = 1;
2983 else if (NILP (value))
2984 valid_p = 1;
2985 else if (INTEGERP (value))
2986 valid_p = XINT (value) != 0;
2987 else if (STRINGP (value))
2988 valid_p = SCHARS (value) > 0;
2989 else if (CONSP (value))
2991 Lisp_Object tem;
2993 tem = value;
2994 while (CONSP (tem))
2996 Lisp_Object k, v;
2998 k = XCAR (tem);
2999 tem = XCDR (tem);
3000 if (!CONSP (tem))
3001 break;
3002 v = XCAR (tem);
3003 tem = XCDR (tem);
3005 if (EQ (k, QCline_width))
3007 if (!INTEGERP (v) || XINT (v) == 0)
3008 break;
3010 else if (EQ (k, QCcolor))
3012 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
3013 break;
3015 else if (EQ (k, QCstyle))
3017 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3018 break;
3020 else
3021 break;
3024 valid_p = NILP (tem);
3026 else
3027 valid_p = 0;
3029 if (!valid_p)
3030 signal_error ("Invalid face box", value);
3032 old_value = LFACE_BOX (lface);
3033 LFACE_BOX (lface) = value;
3035 else if (EQ (attr, QCinverse_video)
3036 || EQ (attr, QCreverse_video))
3038 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3040 CHECK_SYMBOL (value);
3041 if (!EQ (value, Qt) && !NILP (value))
3042 signal_error ("Invalid inverse-video face attribute value", value);
3044 old_value = LFACE_INVERSE (lface);
3045 LFACE_INVERSE (lface) = value;
3047 else if (EQ (attr, QCforeground))
3049 /* Compatibility with 20.x. */
3050 if (NILP (value))
3051 value = Qunspecified;
3052 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3054 /* Don't check for valid color names here because it depends
3055 on the frame (display) whether the color will be valid
3056 when the face is realized. */
3057 CHECK_STRING (value);
3058 if (SCHARS (value) == 0)
3059 signal_error ("Empty foreground color value", value);
3061 old_value = LFACE_FOREGROUND (lface);
3062 LFACE_FOREGROUND (lface) = value;
3064 else if (EQ (attr, QCbackground))
3066 /* Compatibility with 20.x. */
3067 if (NILP (value))
3068 value = Qunspecified;
3069 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3071 /* Don't check for valid color names here because it depends
3072 on the frame (display) whether the color will be valid
3073 when the face is realized. */
3074 CHECK_STRING (value);
3075 if (SCHARS (value) == 0)
3076 signal_error ("Empty background color value", value);
3078 old_value = LFACE_BACKGROUND (lface);
3079 LFACE_BACKGROUND (lface) = value;
3081 else if (EQ (attr, QCstipple))
3083 #if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
3084 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3085 && !NILP (value)
3086 && NILP (Fbitmap_spec_p (value)))
3087 signal_error ("Invalid stipple attribute", value);
3088 old_value = LFACE_STIPPLE (lface);
3089 LFACE_STIPPLE (lface) = value;
3090 #endif /* HAVE_X_WINDOWS || HAVE_NS */
3092 else if (EQ (attr, QCwidth))
3094 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3096 CHECK_SYMBOL (value);
3097 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3098 signal_error ("Invalid face width", value);
3100 old_value = LFACE_SWIDTH (lface);
3101 LFACE_SWIDTH (lface) = value;
3102 prop_index = FONT_WIDTH_INDEX;
3104 else if (EQ (attr, QCfont))
3106 #ifdef HAVE_WINDOW_SYSTEM
3107 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3109 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3111 FRAME_PTR f;
3113 old_value = LFACE_FONT (lface);
3114 if (! FONTP (value))
3116 if (STRINGP (value))
3118 Lisp_Object name = value;
3119 int fontset = fs_query_fontset (name, 0);
3121 if (fontset >= 0)
3122 name = fontset_ascii (fontset);
3123 value = font_spec_from_name (name);
3124 if (!FONTP (value))
3125 signal_error ("Invalid font name", name);
3127 else
3128 signal_error ("Invalid font or font-spec", value);
3130 if (EQ (frame, Qt))
3131 f = XFRAME (selected_frame);
3132 else
3133 f = XFRAME (frame);
3134 if (! FONT_OBJECT_P (value))
3136 Lisp_Object *attrs = XVECTOR (lface)->contents;
3137 Lisp_Object font_object;
3139 font_object = font_load_for_lface (f, attrs, value);
3140 if (NILP (font_object))
3141 signal_error ("Font not available", value);
3142 value = font_object;
3144 set_lface_from_font (f, lface, value, 1);
3146 else
3147 LFACE_FONT (lface) = value;
3149 #endif /* HAVE_WINDOW_SYSTEM */
3151 else if (EQ (attr, QCfontset))
3153 #ifdef HAVE_WINDOW_SYSTEM
3154 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3156 Lisp_Object tmp;
3158 old_value = LFACE_FONTSET (lface);
3159 tmp = Fquery_fontset (value, Qnil);
3160 if (NILP (tmp))
3161 signal_error ("Invalid fontset name", value);
3162 LFACE_FONTSET (lface) = value = tmp;
3164 #endif /* HAVE_WINDOW_SYSTEM */
3166 else if (EQ (attr, QCinherit))
3168 Lisp_Object tail;
3169 if (SYMBOLP (value))
3170 tail = Qnil;
3171 else
3172 for (tail = value; CONSP (tail); tail = XCDR (tail))
3173 if (!SYMBOLP (XCAR (tail)))
3174 break;
3175 if (NILP (tail))
3176 LFACE_INHERIT (lface) = value;
3177 else
3178 signal_error ("Invalid face inheritance", value);
3180 else if (EQ (attr, QCbold))
3182 old_value = LFACE_WEIGHT (lface);
3183 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
3184 prop_index = FONT_WEIGHT_INDEX;
3186 else if (EQ (attr, QCitalic))
3188 attr = QCslant;
3189 old_value = LFACE_SLANT (lface);
3190 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
3191 prop_index = FONT_SLANT_INDEX;
3193 else
3194 signal_error ("Invalid face attribute name", attr);
3196 if (prop_index)
3198 /* If a font-related attribute other than QCfont and QCfontset
3199 is specified, and if the original QCfont attribute has a font
3200 (font-spec or font-object), set the corresponding property in
3201 the font to nil so that the font selector doesn't think that
3202 the attribute is mandatory. Also, clear the average
3203 width. */
3204 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3207 /* Changing a named face means that all realized faces depending on
3208 that face are invalid. Since we cannot tell which realized faces
3209 depend on the face, make sure they are all removed. This is done
3210 by incrementing face_change_count. The next call to
3211 init_iterator will then free realized faces. */
3212 if (!EQ (frame, Qt)
3213 && NILP (Fget (face, Qface_no_inherit))
3214 && NILP (Fequal (old_value, value)))
3216 ++face_change_count;
3217 ++windows_or_buffers_changed;
3220 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3221 && NILP (Fequal (old_value, value)))
3223 Lisp_Object param;
3225 param = Qnil;
3227 if (EQ (face, Qdefault))
3229 #ifdef HAVE_WINDOW_SYSTEM
3230 /* Changed font-related attributes of the `default' face are
3231 reflected in changed `font' frame parameters. */
3232 if (FRAMEP (frame)
3233 && (prop_index || EQ (attr, QCfont))
3234 && lface_fully_specified_p (XVECTOR (lface)->contents))
3235 set_font_frame_param (frame, lface);
3236 else
3237 #endif /* HAVE_WINDOW_SYSTEM */
3239 if (EQ (attr, QCforeground))
3240 param = Qforeground_color;
3241 else if (EQ (attr, QCbackground))
3242 param = Qbackground_color;
3244 #ifdef HAVE_WINDOW_SYSTEM
3245 #ifndef WINDOWSNT
3246 else if (EQ (face, Qscroll_bar))
3248 /* Changing the colors of `scroll-bar' sets frame parameters
3249 `scroll-bar-foreground' and `scroll-bar-background'. */
3250 if (EQ (attr, QCforeground))
3251 param = Qscroll_bar_foreground;
3252 else if (EQ (attr, QCbackground))
3253 param = Qscroll_bar_background;
3255 #endif /* not WINDOWSNT */
3256 else if (EQ (face, Qborder))
3258 /* Changing background color of `border' sets frame parameter
3259 `border-color'. */
3260 if (EQ (attr, QCbackground))
3261 param = Qborder_color;
3263 else if (EQ (face, Qcursor))
3265 /* Changing background color of `cursor' sets frame parameter
3266 `cursor-color'. */
3267 if (EQ (attr, QCbackground))
3268 param = Qcursor_color;
3270 else if (EQ (face, Qmouse))
3272 /* Changing background color of `mouse' sets frame parameter
3273 `mouse-color'. */
3274 if (EQ (attr, QCbackground))
3275 param = Qmouse_color;
3277 #endif /* HAVE_WINDOW_SYSTEM */
3278 else if (EQ (face, Qmenu))
3280 /* Indicate that we have to update the menu bar when
3281 realizing faces on FRAME. FRAME t change the
3282 default for new frames. We do this by setting
3283 setting the flag in new face caches */
3284 if (FRAMEP (frame))
3286 struct frame *f = XFRAME (frame);
3287 if (FRAME_FACE_CACHE (f) == NULL)
3288 FRAME_FACE_CACHE (f) = make_face_cache (f);
3289 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3291 else
3292 menu_face_changed_default = 1;
3295 if (!NILP (param))
3297 if (EQ (frame, Qt))
3298 /* Update `default-frame-alist', which is used for new frames. */
3300 store_in_alist (&Vdefault_frame_alist, param, value);
3302 else
3303 /* Update the current frame's parameters. */
3305 Lisp_Object cons;
3306 cons = XCAR (Vparam_value_alist);
3307 XSETCAR (cons, param);
3308 XSETCDR (cons, value);
3309 Fmodify_frame_parameters (frame, Vparam_value_alist);
3314 return face;
3318 /* Update the corresponding face when frame parameter PARAM on frame F
3319 has been assigned the value NEW_VALUE. */
3321 void
3322 update_face_from_frame_parameter (struct frame *f, Lisp_Object param, Lisp_Object new_value)
3324 Lisp_Object face = Qnil;
3325 Lisp_Object lface;
3327 /* If there are no faces yet, give up. This is the case when called
3328 from Fx_create_frame, and we do the necessary things later in
3329 face-set-after-frame-defaults. */
3330 if (NILP (f->face_alist))
3331 return;
3333 if (EQ (param, Qforeground_color))
3335 face = Qdefault;
3336 lface = lface_from_face_name (f, face, 1);
3337 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
3338 ? new_value : Qunspecified);
3339 realize_basic_faces (f);
3341 else if (EQ (param, Qbackground_color))
3343 Lisp_Object frame;
3345 /* Changing the background color might change the background
3346 mode, so that we have to load new defface specs.
3347 Call frame-update-face-colors to do that. */
3348 XSETFRAME (frame, f);
3349 call1 (Qframe_set_background_mode, frame);
3351 face = Qdefault;
3352 lface = lface_from_face_name (f, face, 1);
3353 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3354 ? new_value : Qunspecified);
3355 realize_basic_faces (f);
3357 #ifdef HAVE_WINDOW_SYSTEM
3358 else if (EQ (param, Qborder_color))
3360 face = Qborder;
3361 lface = lface_from_face_name (f, face, 1);
3362 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3363 ? new_value : Qunspecified);
3365 else if (EQ (param, Qcursor_color))
3367 face = Qcursor;
3368 lface = lface_from_face_name (f, face, 1);
3369 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3370 ? new_value : Qunspecified);
3372 else if (EQ (param, Qmouse_color))
3374 face = Qmouse;
3375 lface = lface_from_face_name (f, face, 1);
3376 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3377 ? new_value : Qunspecified);
3379 #endif
3381 /* Changing a named face means that all realized faces depending on
3382 that face are invalid. Since we cannot tell which realized faces
3383 depend on the face, make sure they are all removed. This is done
3384 by incrementing face_change_count. The next call to
3385 init_iterator will then free realized faces. */
3386 if (!NILP (face)
3387 && NILP (Fget (face, Qface_no_inherit)))
3389 ++face_change_count;
3390 ++windows_or_buffers_changed;
3395 #ifdef HAVE_WINDOW_SYSTEM
3397 /* Set the `font' frame parameter of FRAME determined from the
3398 font-object set in `default' face attributes LFACE. */
3400 static void
3401 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3403 struct frame *f = XFRAME (frame);
3404 Lisp_Object font;
3406 if (FRAME_WINDOW_P (f)
3407 /* Don't do anything if the font is `unspecified'. This can
3408 happen during frame creation. */
3409 && (font = LFACE_FONT (lface),
3410 ! UNSPECIFIEDP (font)))
3412 if (FONT_SPEC_P (font))
3414 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3415 if (NILP (font))
3416 return;
3417 LFACE_FONT (lface) = font;
3419 f->default_face_done_p = 0;
3420 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font), Qnil));
3425 /* Get the value of X resource RESOURCE, class CLASS for the display
3426 of frame FRAME. This is here because ordinary `x-get-resource'
3427 doesn't take a frame argument. */
3429 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3430 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
3431 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3433 Lisp_Object value = Qnil;
3434 CHECK_STRING (resource);
3435 CHECK_STRING (class);
3436 CHECK_LIVE_FRAME (frame);
3437 BLOCK_INPUT;
3438 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3439 resource, class, Qnil, Qnil);
3440 UNBLOCK_INPUT;
3441 return value;
3445 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3446 If VALUE is "on" or "true", return t. If VALUE is "off" or
3447 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3448 error; if SIGNAL_P is zero, return 0. */
3450 static Lisp_Object
3451 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3453 Lisp_Object result = make_number (0);
3455 xassert (STRINGP (value));
3457 if (xstrcasecmp (SSDATA (value), "on") == 0
3458 || xstrcasecmp (SSDATA (value), "true") == 0)
3459 result = Qt;
3460 else if (xstrcasecmp (SSDATA (value), "off") == 0
3461 || xstrcasecmp (SSDATA (value), "false") == 0)
3462 result = Qnil;
3463 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3464 result = Qunspecified;
3465 else if (signal_p)
3466 signal_error ("Invalid face attribute value from X resource", value);
3468 return result;
3472 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3473 Finternal_set_lisp_face_attribute_from_resource,
3474 Sinternal_set_lisp_face_attribute_from_resource,
3475 3, 4, 0, doc: /* */)
3476 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3478 CHECK_SYMBOL (face);
3479 CHECK_SYMBOL (attr);
3480 CHECK_STRING (value);
3482 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3483 value = Qunspecified;
3484 else if (EQ (attr, QCheight))
3486 value = Fstring_to_number (value, make_number (10));
3487 if (XINT (value) <= 0)
3488 signal_error ("Invalid face height from X resource", value);
3490 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3491 value = face_boolean_x_resource_value (value, 1);
3492 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3493 value = intern (SSDATA (value));
3494 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3495 value = face_boolean_x_resource_value (value, 1);
3496 else if (EQ (attr, QCunderline)
3497 || EQ (attr, QCoverline)
3498 || EQ (attr, QCstrike_through))
3500 Lisp_Object boolean_value;
3502 /* If the result of face_boolean_x_resource_value is t or nil,
3503 VALUE does NOT specify a color. */
3504 boolean_value = face_boolean_x_resource_value (value, 0);
3505 if (SYMBOLP (boolean_value))
3506 value = boolean_value;
3508 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3509 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3511 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3514 #endif /* HAVE_WINDOW_SYSTEM */
3517 /***********************************************************************
3518 Menu face
3519 ***********************************************************************/
3521 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3523 /* Make menus on frame F appear as specified by the `menu' face. */
3525 static void
3526 x_update_menu_appearance (struct frame *f)
3528 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3529 XrmDatabase rdb;
3531 if (dpyinfo
3532 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3533 rdb != NULL))
3535 char line[512];
3536 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3537 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3538 const char *myname = SSDATA (Vx_resource_name);
3539 int changed_p = 0;
3540 #ifdef USE_MOTIF
3541 const char *popup_path = "popup_menu";
3542 #else
3543 const char *popup_path = "menu.popup";
3544 #endif
3546 if (STRINGP (LFACE_FOREGROUND (lface)))
3548 sprintf (line, "%s.%s*foreground: %s",
3549 myname, popup_path,
3550 SDATA (LFACE_FOREGROUND (lface)));
3551 XrmPutLineResource (&rdb, line);
3552 sprintf (line, "%s.pane.menubar*foreground: %s",
3553 myname, SDATA (LFACE_FOREGROUND (lface)));
3554 XrmPutLineResource (&rdb, line);
3555 changed_p = 1;
3558 if (STRINGP (LFACE_BACKGROUND (lface)))
3560 sprintf (line, "%s.%s*background: %s",
3561 myname, popup_path,
3562 SDATA (LFACE_BACKGROUND (lface)));
3563 XrmPutLineResource (&rdb, line);
3564 sprintf (line, "%s.pane.menubar*background: %s",
3565 myname, SDATA (LFACE_BACKGROUND (lface)));
3566 XrmPutLineResource (&rdb, line);
3567 changed_p = 1;
3570 if (face->font
3571 /* On Solaris 5.8, it's been reported that the `menu' face
3572 can be unspecified here, during startup. Why this
3573 happens remains unknown. -- cyd */
3574 && FONTP (LFACE_FONT (lface))
3575 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3576 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3577 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3578 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3579 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3580 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3582 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3583 #ifdef USE_MOTIF
3584 const char *suffix = "List";
3585 Bool motif = True;
3586 #else
3587 #if defined HAVE_X_I18N
3589 const char *suffix = "Set";
3590 #else
3591 const char *suffix = "";
3592 #endif
3593 Bool motif = False;
3594 #endif
3596 if (! NILP (xlfd))
3598 #if defined HAVE_X_I18N
3599 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3600 #else
3601 char *fontsetname = SSDATA (xlfd);
3602 #endif
3603 sprintf (line, "%s.pane.menubar*font%s: %s",
3604 myname, suffix, fontsetname);
3605 XrmPutLineResource (&rdb, line);
3606 sprintf (line, "%s.%s*font%s: %s",
3607 myname, popup_path, suffix, fontsetname);
3608 XrmPutLineResource (&rdb, line);
3609 changed_p = 1;
3610 if (fontsetname != SSDATA (xlfd))
3611 xfree (fontsetname);
3615 if (changed_p && f->output_data.x->menubar_widget)
3616 free_frame_menubar (f);
3620 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3623 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3624 Sface_attribute_relative_p,
3625 2, 2, 0,
3626 doc: /* Check whether a face attribute value is relative.
3627 Specifically, this function returns t if the attribute ATTRIBUTE
3628 with the value VALUE is relative.
3630 A relative value is one that doesn't entirely override whatever is
3631 inherited from another face. For most possible attributes,
3632 the only relative value that users see is `unspecified'.
3633 However, for :height, floating point values are also relative. */)
3634 (Lisp_Object attribute, Lisp_Object value)
3636 if (EQ (value, Qunspecified) || (EQ (value, Qignore_defface)))
3637 return Qt;
3638 else if (EQ (attribute, QCheight))
3639 return INTEGERP (value) ? Qnil : Qt;
3640 else
3641 return Qnil;
3644 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3645 3, 3, 0,
3646 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3647 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3648 the result will be absolute, otherwise it will be relative. */)
3649 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3651 if (EQ (value1, Qunspecified) || EQ (value1, Qignore_defface))
3652 return value2;
3653 else if (EQ (attribute, QCheight))
3654 return merge_face_heights (value1, value2, value1);
3655 else
3656 return value1;
3660 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3661 Sinternal_get_lisp_face_attribute,
3662 2, 3, 0,
3663 doc: /* Return face attribute KEYWORD of face SYMBOL.
3664 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3665 face attribute name, signal an error.
3666 If the optional argument FRAME is given, report on face SYMBOL in that
3667 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3668 frames). If FRAME is omitted or nil, use the selected frame. */)
3669 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3671 Lisp_Object lface, value = Qnil;
3673 CHECK_SYMBOL (symbol);
3674 CHECK_SYMBOL (keyword);
3676 if (EQ (frame, Qt))
3677 lface = lface_from_face_name (NULL, symbol, 1);
3678 else
3680 if (NILP (frame))
3681 frame = selected_frame;
3682 CHECK_LIVE_FRAME (frame);
3683 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3686 if (EQ (keyword, QCfamily))
3687 value = LFACE_FAMILY (lface);
3688 else if (EQ (keyword, QCfoundry))
3689 value = LFACE_FOUNDRY (lface);
3690 else if (EQ (keyword, QCheight))
3691 value = LFACE_HEIGHT (lface);
3692 else if (EQ (keyword, QCweight))
3693 value = LFACE_WEIGHT (lface);
3694 else if (EQ (keyword, QCslant))
3695 value = LFACE_SLANT (lface);
3696 else if (EQ (keyword, QCunderline))
3697 value = LFACE_UNDERLINE (lface);
3698 else if (EQ (keyword, QCoverline))
3699 value = LFACE_OVERLINE (lface);
3700 else if (EQ (keyword, QCstrike_through))
3701 value = LFACE_STRIKE_THROUGH (lface);
3702 else if (EQ (keyword, QCbox))
3703 value = LFACE_BOX (lface);
3704 else if (EQ (keyword, QCinverse_video)
3705 || EQ (keyword, QCreverse_video))
3706 value = LFACE_INVERSE (lface);
3707 else if (EQ (keyword, QCforeground))
3708 value = LFACE_FOREGROUND (lface);
3709 else if (EQ (keyword, QCbackground))
3710 value = LFACE_BACKGROUND (lface);
3711 else if (EQ (keyword, QCstipple))
3712 value = LFACE_STIPPLE (lface);
3713 else if (EQ (keyword, QCwidth))
3714 value = LFACE_SWIDTH (lface);
3715 else if (EQ (keyword, QCinherit))
3716 value = LFACE_INHERIT (lface);
3717 else if (EQ (keyword, QCfont))
3718 value = LFACE_FONT (lface);
3719 else if (EQ (keyword, QCfontset))
3720 value = LFACE_FONTSET (lface);
3721 else
3722 signal_error ("Invalid face attribute name", keyword);
3724 if (IGNORE_DEFFACE_P (value))
3725 return Qunspecified;
3727 return value;
3731 DEFUN ("internal-lisp-face-attribute-values",
3732 Finternal_lisp_face_attribute_values,
3733 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3734 doc: /* Return a list of valid discrete values for face attribute ATTR.
3735 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3736 (Lisp_Object attr)
3738 Lisp_Object result = Qnil;
3740 CHECK_SYMBOL (attr);
3742 if (EQ (attr, QCunderline))
3743 result = Fcons (Qt, Fcons (Qnil, Qnil));
3744 else if (EQ (attr, QCoverline))
3745 result = Fcons (Qt, Fcons (Qnil, Qnil));
3746 else if (EQ (attr, QCstrike_through))
3747 result = Fcons (Qt, Fcons (Qnil, Qnil));
3748 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3749 result = Fcons (Qt, Fcons (Qnil, Qnil));
3751 return result;
3755 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3756 Sinternal_merge_in_global_face, 2, 2, 0,
3757 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3758 Default face attributes override any local face attributes. */)
3759 (Lisp_Object face, Lisp_Object frame)
3761 int i;
3762 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3763 struct frame *f = XFRAME (frame);
3765 CHECK_LIVE_FRAME (frame);
3766 global_lface = lface_from_face_name (NULL, face, 1);
3767 local_lface = lface_from_face_name (f, face, 0);
3768 if (NILP (local_lface))
3769 local_lface = Finternal_make_lisp_face (face, frame);
3771 /* Make every specified global attribute override the local one.
3772 BEWARE!! This is only used from `face-set-after-frame-default' where
3773 the local frame is defined from default specs in `face-defface-spec'
3774 and those should be overridden by global settings. Hence the strange
3775 "global before local" priority. */
3776 lvec = XVECTOR (local_lface)->contents;
3777 gvec = XVECTOR (global_lface)->contents;
3778 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3779 if (IGNORE_DEFFACE_P (gvec[i]))
3780 lvec[i] = Qunspecified;
3781 else if (! UNSPECIFIEDP (gvec[i]))
3782 lvec[i] = gvec[i];
3784 /* If the default face was changed, update the face cache and the
3785 `font' frame parameter. */
3786 if (EQ (face, Qdefault))
3788 struct face_cache *c = FRAME_FACE_CACHE (f);
3789 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3790 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3792 /* This can be NULL (e.g., in batch mode). */
3793 if (oldface)
3795 /* Ensure that the face vector is fully specified by merging
3796 the previously-cached vector. */
3797 memcpy (attrs, oldface->lface, sizeof attrs);
3798 merge_face_vectors (f, lvec, attrs, 0);
3799 memcpy (lvec, attrs, sizeof attrs);
3800 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3802 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3803 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3804 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3805 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3806 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3807 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3808 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3809 && newface->font)
3811 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3812 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, name),
3813 Qnil));
3818 return Qnil;
3822 /* The following function is implemented for compatibility with 20.2.
3823 The function is used in x-resolve-fonts when it is asked to
3824 return fonts with the same size as the font of a face. This is
3825 done in fontset.el. */
3827 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3828 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3829 The font name is, by default, for ASCII characters.
3830 If the optional argument FRAME is given, report on face FACE in that frame.
3831 If FRAME is t, report on the defaults for face FACE (for new frames).
3832 The font default for a face is either nil, or a list
3833 of the form (bold), (italic) or (bold italic).
3834 If FRAME is omitted or nil, use the selected frame. And, in this case,
3835 if the optional third argument CHARACTER is given,
3836 return the font name used for CHARACTER. */)
3837 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3839 if (EQ (frame, Qt))
3841 Lisp_Object result = Qnil;
3842 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3844 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3845 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3846 result = Fcons (Qbold, result);
3848 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3849 && !EQ (LFACE_SLANT (lface), Qnormal))
3850 result = Fcons (Qitalic, result);
3852 return result;
3854 else
3856 struct frame *f = frame_or_selected_frame (frame, 1);
3857 int face_id = lookup_named_face (f, face, 1);
3858 struct face *fface = FACE_FROM_ID (f, face_id);
3860 if (! fface)
3861 return Qnil;
3862 #ifdef HAVE_WINDOW_SYSTEM
3863 if (FRAME_WINDOW_P (f) && !NILP (character))
3865 CHECK_CHARACTER (character);
3866 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3867 fface = FACE_FROM_ID (f, face_id);
3869 return (fface->font
3870 ? fface->font->props[FONT_NAME_INDEX]
3871 : Qnil);
3872 #else /* !HAVE_WINDOW_SYSTEM */
3873 return build_string (FRAME_MSDOS_P (f)
3874 ? "ms-dos"
3875 : FRAME_W32_P (f) ? "w32term"
3876 :"tty");
3877 #endif
3882 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3883 all attributes are `equal'. Tries to be fast because this function
3884 is called quite often. */
3886 static inline int
3887 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3889 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3890 and the other is specified. */
3891 if (XTYPE (v1) != XTYPE (v2))
3892 return 0;
3894 if (EQ (v1, v2))
3895 return 1;
3897 switch (XTYPE (v1))
3899 case Lisp_String:
3900 if (SBYTES (v1) != SBYTES (v2))
3901 return 0;
3903 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3905 case_Lisp_Int:
3906 case Lisp_Symbol:
3907 return 0;
3909 default:
3910 return !NILP (Fequal (v1, v2));
3915 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3916 all attributes are `equal'. Tries to be fast because this function
3917 is called quite often. */
3919 static inline int
3920 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3922 int i, equal_p = 1;
3924 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3925 equal_p = face_attr_equal_p (v1[i], v2[i]);
3927 return equal_p;
3931 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3932 Sinternal_lisp_face_equal_p, 2, 3, 0,
3933 doc: /* True if FACE1 and FACE2 are equal.
3934 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3935 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3936 If FRAME is omitted or nil, use the selected frame. */)
3937 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3939 int equal_p;
3940 struct frame *f;
3941 Lisp_Object lface1, lface2;
3943 if (EQ (frame, Qt))
3944 f = NULL;
3945 else
3946 /* Don't use check_x_frame here because this function is called
3947 before X frames exist. At that time, if FRAME is nil,
3948 selected_frame will be used which is the frame dumped with
3949 Emacs. That frame is not an X frame. */
3950 f = frame_or_selected_frame (frame, 2);
3952 lface1 = lface_from_face_name (f, face1, 1);
3953 lface2 = lface_from_face_name (f, face2, 1);
3954 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3955 XVECTOR (lface2)->contents);
3956 return equal_p ? Qt : Qnil;
3960 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3961 Sinternal_lisp_face_empty_p, 1, 2, 0,
3962 doc: /* True if FACE has no attribute specified.
3963 If the optional argument FRAME is given, report on face FACE in that frame.
3964 If FRAME is t, report on the defaults for face FACE (for new frames).
3965 If FRAME is omitted or nil, use the selected frame. */)
3966 (Lisp_Object face, Lisp_Object frame)
3968 struct frame *f;
3969 Lisp_Object lface;
3970 int i;
3972 if (NILP (frame))
3973 frame = selected_frame;
3974 CHECK_LIVE_FRAME (frame);
3975 f = XFRAME (frame);
3977 if (EQ (frame, Qt))
3978 lface = lface_from_face_name (NULL, face, 1);
3979 else
3980 lface = lface_from_face_name (f, face, 1);
3982 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3983 if (!UNSPECIFIEDP (AREF (lface, i)))
3984 break;
3986 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3990 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3991 0, 1, 0,
3992 doc: /* Return an alist of frame-local faces defined on FRAME.
3993 For internal use only. */)
3994 (Lisp_Object frame)
3996 struct frame *f = frame_or_selected_frame (frame, 0);
3997 return f->face_alist;
4001 /* Return a hash code for Lisp string STRING with case ignored. Used
4002 below in computing a hash value for a Lisp face. */
4004 static inline unsigned
4005 hash_string_case_insensitive (Lisp_Object string)
4007 const unsigned char *s;
4008 unsigned hash = 0;
4009 xassert (STRINGP (string));
4010 for (s = SDATA (string); *s; ++s)
4011 hash = (hash << 1) ^ tolower (*s);
4012 return hash;
4016 /* Return a hash code for face attribute vector V. */
4018 static inline unsigned
4019 lface_hash (Lisp_Object *v)
4021 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4022 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4023 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4024 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4025 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4026 ^ XHASH (v[LFACE_SLANT_INDEX])
4027 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4028 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4032 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4033 considering charsets/registries). They do if they specify the same
4034 family, point size, weight, width, slant, and font. Both
4035 LFACE1 and LFACE2 must be fully-specified. */
4037 static inline int
4038 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4040 xassert (lface_fully_specified_p (lface1)
4041 && lface_fully_specified_p (lface2));
4042 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4043 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4044 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4045 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4046 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4047 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4048 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4049 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4050 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4051 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4052 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4053 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4054 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4055 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4061 /***********************************************************************
4062 Realized Faces
4063 ***********************************************************************/
4065 /* Allocate and return a new realized face for Lisp face attribute
4066 vector ATTR. */
4068 static struct face *
4069 make_realized_face (Lisp_Object *attr)
4071 struct face *face = (struct face *) xmalloc (sizeof *face);
4072 memset (face, 0, sizeof *face);
4073 face->ascii_face = face;
4074 memcpy (face->lface, attr, sizeof face->lface);
4075 return face;
4079 /* Free realized face FACE, including its X resources. FACE may
4080 be null. */
4082 static void
4083 free_realized_face (struct frame *f, struct face *face)
4085 if (face)
4087 #ifdef HAVE_WINDOW_SYSTEM
4088 if (FRAME_WINDOW_P (f))
4090 /* Free fontset of FACE if it is ASCII face. */
4091 if (face->fontset >= 0 && face == face->ascii_face)
4092 free_face_fontset (f, face);
4093 if (face->gc)
4095 BLOCK_INPUT;
4096 if (face->font)
4097 font_done_for_face (f, face);
4098 x_free_gc (f, face->gc);
4099 face->gc = 0;
4100 UNBLOCK_INPUT;
4103 free_face_colors (f, face);
4104 x_destroy_bitmap (f, face->stipple);
4106 #endif /* HAVE_WINDOW_SYSTEM */
4108 xfree (face);
4113 /* Prepare face FACE for subsequent display on frame F. This
4114 allocated GCs if they haven't been allocated yet or have been freed
4115 by clearing the face cache. */
4117 void
4118 prepare_face_for_display (struct frame *f, struct face *face)
4120 #ifdef HAVE_WINDOW_SYSTEM
4121 xassert (FRAME_WINDOW_P (f));
4123 if (face->gc == 0)
4125 XGCValues xgcv;
4126 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4128 xgcv.foreground = face->foreground;
4129 xgcv.background = face->background;
4130 #ifdef HAVE_X_WINDOWS
4131 xgcv.graphics_exposures = False;
4132 #endif
4134 BLOCK_INPUT;
4135 #ifdef HAVE_X_WINDOWS
4136 if (face->stipple)
4138 xgcv.fill_style = FillOpaqueStippled;
4139 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4140 mask |= GCFillStyle | GCStipple;
4142 #endif
4143 face->gc = x_create_gc (f, mask, &xgcv);
4144 if (face->font)
4145 font_prepare_for_face (f, face);
4146 UNBLOCK_INPUT;
4148 #endif /* HAVE_WINDOW_SYSTEM */
4152 /* Returns the `distance' between the colors X and Y. */
4154 static int
4155 color_distance (XColor *x, XColor *y)
4157 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
4158 Quoting from that paper:
4160 This formula has results that are very close to L*u*v* (with the
4161 modified lightness curve) and, more importantly, it is a more even
4162 algorithm: it does not have a range of colours where it suddenly
4163 gives far from optimal results.
4165 See <http://www.compuphase.com/cmetric.htm> for more info. */
4167 long r = (x->red - y->red) >> 8;
4168 long g = (x->green - y->green) >> 8;
4169 long b = (x->blue - y->blue) >> 8;
4170 long r_mean = (x->red + y->red) >> 9;
4172 return
4173 (((512 + r_mean) * r * r) >> 8)
4174 + 4 * g * g
4175 + (((767 - r_mean) * b * b) >> 8);
4179 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4180 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4181 COLOR1 and COLOR2 may be either strings containing the color name,
4182 or lists of the form (RED GREEN BLUE).
4183 If FRAME is unspecified or nil, the current frame is used. */)
4184 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4186 struct frame *f;
4187 XColor cdef1, cdef2;
4189 if (NILP (frame))
4190 frame = selected_frame;
4191 CHECK_LIVE_FRAME (frame);
4192 f = XFRAME (frame);
4194 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4195 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4196 signal_error ("Invalid color", color1);
4197 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4198 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4199 signal_error ("Invalid color", color2);
4201 return make_number (color_distance (&cdef1, &cdef2));
4205 /***********************************************************************
4206 Face Cache
4207 ***********************************************************************/
4209 /* Return a new face cache for frame F. */
4211 static struct face_cache *
4212 make_face_cache (struct frame *f)
4214 struct face_cache *c;
4215 int size;
4217 c = (struct face_cache *) xmalloc (sizeof *c);
4218 memset (c, 0, sizeof *c);
4219 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4220 c->buckets = (struct face **) xmalloc (size);
4221 memset (c->buckets, 0, size);
4222 c->size = 50;
4223 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4224 c->f = f;
4225 c->menu_face_changed_p = menu_face_changed_default;
4226 return c;
4230 /* Clear out all graphics contexts for all realized faces, except for
4231 the basic faces. This should be done from time to time just to avoid
4232 keeping too many graphics contexts that are no longer needed. */
4234 static void
4235 clear_face_gcs (struct face_cache *c)
4237 if (c && FRAME_WINDOW_P (c->f))
4239 #ifdef HAVE_WINDOW_SYSTEM
4240 int i;
4241 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4243 struct face *face = c->faces_by_id[i];
4244 if (face && face->gc)
4246 BLOCK_INPUT;
4247 if (face->font)
4248 font_done_for_face (c->f, face);
4249 x_free_gc (c->f, face->gc);
4250 face->gc = 0;
4251 UNBLOCK_INPUT;
4254 #endif /* HAVE_WINDOW_SYSTEM */
4259 /* Free all realized faces in face cache C, including basic faces.
4260 C may be null. If faces are freed, make sure the frame's current
4261 matrix is marked invalid, so that a display caused by an expose
4262 event doesn't try to use faces we destroyed. */
4264 static void
4265 free_realized_faces (struct face_cache *c)
4267 if (c && c->used)
4269 int i, size;
4270 struct frame *f = c->f;
4272 /* We must block input here because we can't process X events
4273 safely while only some faces are freed, or when the frame's
4274 current matrix still references freed faces. */
4275 BLOCK_INPUT;
4277 for (i = 0; i < c->used; ++i)
4279 free_realized_face (f, c->faces_by_id[i]);
4280 c->faces_by_id[i] = NULL;
4283 c->used = 0;
4284 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4285 memset (c->buckets, 0, size);
4287 /* Must do a thorough redisplay the next time. Mark current
4288 matrices as invalid because they will reference faces freed
4289 above. This function is also called when a frame is
4290 destroyed. In this case, the root window of F is nil. */
4291 if (WINDOWP (f->root_window))
4293 clear_current_matrices (f);
4294 ++windows_or_buffers_changed;
4297 UNBLOCK_INPUT;
4302 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4303 This is done after attributes of a named face have been changed,
4304 because we can't tell which realized faces depend on that face. */
4306 void
4307 free_all_realized_faces (Lisp_Object frame)
4309 if (NILP (frame))
4311 Lisp_Object rest;
4312 FOR_EACH_FRAME (rest, frame)
4313 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4315 else
4316 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4320 /* Free face cache C and faces in it, including their X resources. */
4322 static void
4323 free_face_cache (struct face_cache *c)
4325 if (c)
4327 free_realized_faces (c);
4328 xfree (c->buckets);
4329 xfree (c->faces_by_id);
4330 xfree (c);
4335 /* Cache realized face FACE in face cache C. HASH is the hash value
4336 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4337 FACE), insert the new face to the beginning of the collision list
4338 of the face hash table of C. Otherwise, add the new face to the
4339 end of the collision list. This way, lookup_face can quickly find
4340 that a requested face is not cached. */
4342 static void
4343 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4345 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4347 face->hash = hash;
4349 if (face->ascii_face != face)
4351 struct face *last = c->buckets[i];
4352 if (last)
4354 while (last->next)
4355 last = last->next;
4356 last->next = face;
4357 face->prev = last;
4358 face->next = NULL;
4360 else
4362 c->buckets[i] = face;
4363 face->prev = face->next = NULL;
4366 else
4368 face->prev = NULL;
4369 face->next = c->buckets[i];
4370 if (face->next)
4371 face->next->prev = face;
4372 c->buckets[i] = face;
4375 /* Find a free slot in C->faces_by_id and use the index of the free
4376 slot as FACE->id. */
4377 for (i = 0; i < c->used; ++i)
4378 if (c->faces_by_id[i] == NULL)
4379 break;
4380 face->id = i;
4382 /* Maybe enlarge C->faces_by_id. */
4383 if (i == c->used)
4385 if (c->used == c->size)
4387 int new_size, sz;
4388 new_size = min (2 * c->size, MAX_FACE_ID);
4389 if (new_size == c->size)
4390 abort (); /* Alternatives? ++kfs */
4391 sz = new_size * sizeof *c->faces_by_id;
4392 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4393 c->size = new_size;
4395 c->used++;
4398 #if GLYPH_DEBUG
4399 /* Check that FACE got a unique id. */
4401 int j, n;
4402 struct face *face;
4404 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4405 for (face = c->buckets[j]; face; face = face->next)
4406 if (face->id == i)
4407 ++n;
4409 xassert (n == 1);
4411 #endif /* GLYPH_DEBUG */
4413 c->faces_by_id[i] = face;
4417 /* Remove face FACE from cache C. */
4419 static void
4420 uncache_face (struct face_cache *c, struct face *face)
4422 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4424 if (face->prev)
4425 face->prev->next = face->next;
4426 else
4427 c->buckets[i] = face->next;
4429 if (face->next)
4430 face->next->prev = face->prev;
4432 c->faces_by_id[face->id] = NULL;
4433 if (face->id == c->used)
4434 --c->used;
4438 /* Look up a realized face with face attributes ATTR in the face cache
4439 of frame F. The face will be used to display ASCII characters.
4440 Value is the ID of the face found. If no suitable face is found,
4441 realize a new one. */
4443 static inline int
4444 lookup_face (struct frame *f, Lisp_Object *attr)
4446 struct face_cache *cache = FRAME_FACE_CACHE (f);
4447 unsigned hash;
4448 int i;
4449 struct face *face;
4451 xassert (cache != NULL);
4452 check_lface_attrs (attr);
4454 /* Look up ATTR in the face cache. */
4455 hash = lface_hash (attr);
4456 i = hash % FACE_CACHE_BUCKETS_SIZE;
4458 for (face = cache->buckets[i]; face; face = face->next)
4460 if (face->ascii_face != face)
4462 /* There's no more ASCII face. */
4463 face = NULL;
4464 break;
4466 if (face->hash == hash
4467 && lface_equal_p (face->lface, attr))
4468 break;
4471 /* If not found, realize a new face. */
4472 if (face == NULL)
4473 face = realize_face (cache, attr, -1);
4475 #if GLYPH_DEBUG
4476 xassert (face == FACE_FROM_ID (f, face->id));
4477 #endif /* GLYPH_DEBUG */
4479 return face->id;
4482 #ifdef HAVE_WINDOW_SYSTEM
4483 /* Look up a realized face that has the same attributes as BASE_FACE
4484 except for the font in the face cache of frame F. If FONT-OBJECT
4485 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4486 the face has no font. Value is the ID of the face found. If no
4487 suitable face is found, realize a new one. */
4490 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4492 struct face_cache *cache = FRAME_FACE_CACHE (f);
4493 unsigned hash;
4494 int i;
4495 struct face *face;
4497 xassert (cache != NULL);
4498 base_face = base_face->ascii_face;
4499 hash = lface_hash (base_face->lface);
4500 i = hash % FACE_CACHE_BUCKETS_SIZE;
4502 for (face = cache->buckets[i]; face; face = face->next)
4504 if (face->ascii_face == face)
4505 continue;
4506 if (face->ascii_face == base_face
4507 && face->font == (NILP (font_object) ? NULL
4508 : XFONT_OBJECT (font_object))
4509 && lface_equal_p (face->lface, base_face->lface))
4510 return face->id;
4513 /* If not found, realize a new face. */
4514 face = realize_non_ascii_face (f, font_object, base_face);
4515 return face->id;
4517 #endif /* HAVE_WINDOW_SYSTEM */
4519 /* Return the face id of the realized face for named face SYMBOL on
4520 frame F suitable for displaying ASCII characters. Value is -1 if
4521 the face couldn't be determined, which might happen if the default
4522 face isn't realized and cannot be realized. */
4525 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4527 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4528 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4529 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4531 if (default_face == NULL)
4533 if (!realize_basic_faces (f))
4534 return -1;
4535 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4536 if (default_face == NULL)
4537 abort (); /* realize_basic_faces must have set it up */
4540 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4541 return -1;
4543 memcpy (attrs, default_face->lface, sizeof attrs);
4544 merge_face_vectors (f, symbol_attrs, attrs, 0);
4546 return lookup_face (f, attrs);
4550 /* Return the display face-id of the basic face who's canonical face-id
4551 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4552 basic face has bee remapped via Vface_remapping_alist. This function is
4553 conservative: if something goes wrong, it will simply return FACE_ID
4554 rather than signal an error. */
4557 lookup_basic_face (struct frame *f, int face_id)
4559 Lisp_Object name, mapping;
4560 int remapped_face_id;
4562 if (NILP (Vface_remapping_alist))
4563 return face_id; /* Nothing to do. */
4565 switch (face_id)
4567 case DEFAULT_FACE_ID: name = Qdefault; break;
4568 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4569 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4570 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4571 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4572 case FRINGE_FACE_ID: name = Qfringe; break;
4573 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4574 case BORDER_FACE_ID: name = Qborder; break;
4575 case CURSOR_FACE_ID: name = Qcursor; break;
4576 case MOUSE_FACE_ID: name = Qmouse; break;
4577 case MENU_FACE_ID: name = Qmenu; break;
4579 default:
4580 abort (); /* the caller is supposed to pass us a basic face id */
4583 /* Do a quick scan through Vface_remapping_alist, and return immediately
4584 if there is no remapping for face NAME. This is just an optimization
4585 for the very common no-remapping case. */
4586 mapping = assq_no_quit (name, Vface_remapping_alist);
4587 if (NILP (mapping))
4588 return face_id; /* Give up. */
4590 /* If there is a remapping entry, lookup the face using NAME, which will
4591 handle the remapping too. */
4592 remapped_face_id = lookup_named_face (f, name, 0);
4593 if (remapped_face_id < 0)
4594 return face_id; /* Give up. */
4596 return remapped_face_id;
4600 /* Return a face for charset ASCII that is like the face with id
4601 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4602 STEPS < 0 means larger. Value is the id of the face. */
4605 smaller_face (struct frame *f, int face_id, int steps)
4607 #ifdef HAVE_WINDOW_SYSTEM
4608 struct face *face;
4609 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4610 int pt, last_pt, last_height;
4611 int delta;
4612 int new_face_id;
4613 struct face *new_face;
4615 /* If not called for an X frame, just return the original face. */
4616 if (FRAME_TERMCAP_P (f))
4617 return face_id;
4619 /* Try in increments of 1/2 pt. */
4620 delta = steps < 0 ? 5 : -5;
4621 steps = eabs (steps);
4623 face = FACE_FROM_ID (f, face_id);
4624 memcpy (attrs, face->lface, sizeof attrs);
4625 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4626 new_face_id = face_id;
4627 last_height = FONT_HEIGHT (face->font);
4629 while (steps
4630 && pt + delta > 0
4631 /* Give up if we cannot find a font within 10pt. */
4632 && eabs (last_pt - pt) < 100)
4634 /* Look up a face for a slightly smaller/larger font. */
4635 pt += delta;
4636 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4637 new_face_id = lookup_face (f, attrs);
4638 new_face = FACE_FROM_ID (f, new_face_id);
4640 /* If height changes, count that as one step. */
4641 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4642 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4644 --steps;
4645 last_height = FONT_HEIGHT (new_face->font);
4646 last_pt = pt;
4650 return new_face_id;
4652 #else /* not HAVE_WINDOW_SYSTEM */
4654 return face_id;
4656 #endif /* not HAVE_WINDOW_SYSTEM */
4660 /* Return a face for charset ASCII that is like the face with id
4661 FACE_ID on frame F, but has height HEIGHT. */
4664 face_with_height (struct frame *f, int face_id, int height)
4666 #ifdef HAVE_WINDOW_SYSTEM
4667 struct face *face;
4668 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4670 if (FRAME_TERMCAP_P (f)
4671 || height <= 0)
4672 return face_id;
4674 face = FACE_FROM_ID (f, face_id);
4675 memcpy (attrs, face->lface, sizeof attrs);
4676 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4677 font_clear_prop (attrs, FONT_SIZE_INDEX);
4678 face_id = lookup_face (f, attrs);
4679 #endif /* HAVE_WINDOW_SYSTEM */
4681 return face_id;
4685 /* Return the face id of the realized face for named face SYMBOL on
4686 frame F suitable for displaying ASCII characters, and use
4687 attributes of the face FACE_ID for attributes that aren't
4688 completely specified by SYMBOL. This is like lookup_named_face,
4689 except that the default attributes come from FACE_ID, not from the
4690 default face. FACE_ID is assumed to be already realized. */
4693 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id, int signal_p)
4695 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4696 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4697 struct face *default_face = FACE_FROM_ID (f, face_id);
4699 if (!default_face)
4700 abort ();
4702 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4703 return -1;
4705 memcpy (attrs, default_face->lface, sizeof attrs);
4706 merge_face_vectors (f, symbol_attrs, attrs, 0);
4707 return lookup_face (f, attrs);
4710 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4711 Sface_attributes_as_vector, 1, 1, 0,
4712 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4713 (Lisp_Object plist)
4715 Lisp_Object lface;
4716 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4717 Qunspecified);
4718 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4719 1, 0);
4720 return lface;
4725 /***********************************************************************
4726 Face capability testing
4727 ***********************************************************************/
4730 /* If the distance (as returned by color_distance) between two colors is
4731 less than this, then they are considered the same, for determining
4732 whether a color is supported or not. The range of values is 0-65535. */
4734 #define TTY_SAME_COLOR_THRESHOLD 10000
4736 #ifdef HAVE_WINDOW_SYSTEM
4738 /* Return non-zero if all the face attributes in ATTRS are supported
4739 on the window-system frame F.
4741 The definition of `supported' is somewhat heuristic, but basically means
4742 that a face containing all the attributes in ATTRS, when merged with the
4743 default face for display, can be represented in a way that's
4745 \(1) different in appearance than the default face, and
4746 \(2) `close in spirit' to what the attributes specify, if not exact. */
4748 static int
4749 x_supports_face_attributes_p (struct frame *f, Lisp_Object *attrs, struct face *def_face)
4751 Lisp_Object *def_attrs = def_face->lface;
4753 /* Check that other specified attributes are different that the default
4754 face. */
4755 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4756 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4757 def_attrs[LFACE_UNDERLINE_INDEX]))
4758 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4759 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4760 def_attrs[LFACE_INVERSE_INDEX]))
4761 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4762 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4763 def_attrs[LFACE_FOREGROUND_INDEX]))
4764 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4765 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4766 def_attrs[LFACE_BACKGROUND_INDEX]))
4767 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4768 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4769 def_attrs[LFACE_STIPPLE_INDEX]))
4770 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4771 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4772 def_attrs[LFACE_OVERLINE_INDEX]))
4773 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4774 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4775 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4776 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4777 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4778 def_attrs[LFACE_BOX_INDEX])))
4779 return 0;
4781 /* Check font-related attributes, as those are the most commonly
4782 "unsupported" on a window-system (because of missing fonts). */
4783 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4784 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4785 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4786 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4787 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4788 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4790 int face_id;
4791 struct face *face;
4792 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4793 int i;
4795 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4797 merge_face_vectors (f, attrs, merged_attrs, 0);
4799 face_id = lookup_face (f, merged_attrs);
4800 face = FACE_FROM_ID (f, face_id);
4802 if (! face)
4803 error ("Cannot make face");
4805 /* If the font is the same, or no font is found, then not
4806 supported. */
4807 if (face->font == def_face->font
4808 || ! face->font)
4809 return 0;
4810 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4811 if (! EQ (face->font->props[i], def_face->font->props[i]))
4813 Lisp_Object s1, s2;
4815 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4816 || face->font->driver->case_sensitive)
4817 return 1;
4818 s1 = SYMBOL_NAME (face->font->props[i]);
4819 s2 = SYMBOL_NAME (def_face->font->props[i]);
4820 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4821 s2, make_number (0), Qnil, Qt), Qt))
4822 return 1;
4824 return 0;
4827 /* Everything checks out, this face is supported. */
4828 return 1;
4831 #endif /* HAVE_WINDOW_SYSTEM */
4833 /* Return non-zero if all the face attributes in ATTRS are supported
4834 on the tty frame F.
4836 The definition of `supported' is somewhat heuristic, but basically means
4837 that a face containing all the attributes in ATTRS, when merged
4838 with the default face for display, can be represented in a way that's
4840 \(1) different in appearance than the default face, and
4841 \(2) `close in spirit' to what the attributes specify, if not exact.
4843 Point (2) implies that a `:weight black' attribute will be satisfied
4844 by any terminal that can display bold, and a `:foreground "yellow"' as
4845 long as the terminal can display a yellowish color, but `:slant italic'
4846 will _not_ be satisfied by the tty display code's automatic
4847 substitution of a `dim' face for italic. */
4849 static int
4850 tty_supports_face_attributes_p (struct frame *f, Lisp_Object *attrs, struct face *def_face)
4852 int weight;
4853 Lisp_Object val, fg, bg;
4854 XColor fg_tty_color, fg_std_color;
4855 XColor bg_tty_color, bg_std_color;
4856 unsigned test_caps = 0;
4857 Lisp_Object *def_attrs = def_face->lface;
4860 /* First check some easy-to-check stuff; ttys support none of the
4861 following attributes, so we can just return false if any are requested
4862 (even if `nominal' values are specified, we should still return false,
4863 as that will be the same value that the default face uses). We
4864 consider :slant unsupportable on ttys, even though the face code
4865 actually `fakes' them using a dim attribute if possible. This is
4866 because the faked result is too different from what the face
4867 specifies. */
4868 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4869 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4870 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4871 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4872 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4873 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4874 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4875 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4876 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
4877 return 0;
4880 /* Test for terminal `capabilities' (non-color character attributes). */
4882 /* font weight (bold/dim) */
4883 val = attrs[LFACE_WEIGHT_INDEX];
4884 if (!UNSPECIFIEDP (val)
4885 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4887 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4889 if (weight > 100)
4891 if (def_weight > 100)
4892 return 0; /* same as default */
4893 test_caps = TTY_CAP_BOLD;
4895 else if (weight < 100)
4897 if (def_weight < 100)
4898 return 0; /* same as default */
4899 test_caps = TTY_CAP_DIM;
4901 else if (def_weight == 100)
4902 return 0; /* same as default */
4905 /* underlining */
4906 val = attrs[LFACE_UNDERLINE_INDEX];
4907 if (!UNSPECIFIEDP (val))
4909 if (STRINGP (val))
4910 return 0; /* ttys can't use colored underlines */
4911 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4912 return 0; /* same as default */
4913 else
4914 test_caps |= TTY_CAP_UNDERLINE;
4917 /* inverse video */
4918 val = attrs[LFACE_INVERSE_INDEX];
4919 if (!UNSPECIFIEDP (val))
4921 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4922 return 0; /* same as default */
4923 else
4924 test_caps |= TTY_CAP_INVERSE;
4928 /* Color testing. */
4930 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
4931 we use them when calling `tty_capable_p' below, even if the face
4932 specifies no colors. */
4933 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
4934 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
4936 /* Check if foreground color is close enough. */
4937 fg = attrs[LFACE_FOREGROUND_INDEX];
4938 if (STRINGP (fg))
4940 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4942 if (face_attr_equal_p (fg, def_fg))
4943 return 0; /* same as default */
4944 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4945 return 0; /* not a valid color */
4946 else if (color_distance (&fg_tty_color, &fg_std_color)
4947 > TTY_SAME_COLOR_THRESHOLD)
4948 return 0; /* displayed color is too different */
4949 else
4950 /* Make sure the color is really different than the default. */
4952 XColor def_fg_color;
4953 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4954 && (color_distance (&fg_tty_color, &def_fg_color)
4955 <= TTY_SAME_COLOR_THRESHOLD))
4956 return 0;
4960 /* Check if background color is close enough. */
4961 bg = attrs[LFACE_BACKGROUND_INDEX];
4962 if (STRINGP (bg))
4964 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4966 if (face_attr_equal_p (bg, def_bg))
4967 return 0; /* same as default */
4968 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4969 return 0; /* not a valid color */
4970 else if (color_distance (&bg_tty_color, &bg_std_color)
4971 > TTY_SAME_COLOR_THRESHOLD)
4972 return 0; /* displayed color is too different */
4973 else
4974 /* Make sure the color is really different than the default. */
4976 XColor def_bg_color;
4977 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4978 && (color_distance (&bg_tty_color, &def_bg_color)
4979 <= TTY_SAME_COLOR_THRESHOLD))
4980 return 0;
4984 /* If both foreground and background are requested, see if the
4985 distance between them is OK. We just check to see if the distance
4986 between the tty's foreground and background is close enough to the
4987 distance between the standard foreground and background. */
4988 if (STRINGP (fg) && STRINGP (bg))
4990 int delta_delta
4991 = (color_distance (&fg_std_color, &bg_std_color)
4992 - color_distance (&fg_tty_color, &bg_tty_color));
4993 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4994 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4995 return 0;
4999 /* See if the capabilities we selected above are supported, with the
5000 given colors. */
5001 if (test_caps != 0 &&
5002 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
5003 return 0;
5006 /* Hmmm, everything checks out, this terminal must support this face. */
5007 return 1;
5011 DEFUN ("display-supports-face-attributes-p",
5012 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5013 1, 2, 0,
5014 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5015 The optional argument DISPLAY can be a display name, a frame, or
5016 nil (meaning the selected frame's display).
5018 The definition of `supported' is somewhat heuristic, but basically means
5019 that a face containing all the attributes in ATTRIBUTES, when merged
5020 with the default face for display, can be represented in a way that's
5022 \(1) different in appearance than the default face, and
5023 \(2) `close in spirit' to what the attributes specify, if not exact.
5025 Point (2) implies that a `:weight black' attribute will be satisfied by
5026 any display that can display bold, and a `:foreground \"yellow\"' as long
5027 as it can display a yellowish color, but `:slant italic' will _not_ be
5028 satisfied by the tty display code's automatic substitution of a `dim'
5029 face for italic. */)
5030 (Lisp_Object attributes, Lisp_Object display)
5032 int supports = 0, i;
5033 Lisp_Object frame;
5034 struct frame *f;
5035 struct face *def_face;
5036 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5038 if (noninteractive || !initialized)
5039 /* We may not be able to access low-level face information in batch
5040 mode, or before being dumped, and this function is not going to
5041 be very useful in those cases anyway, so just give up. */
5042 return Qnil;
5044 if (NILP (display))
5045 frame = selected_frame;
5046 else if (FRAMEP (display))
5047 frame = display;
5048 else
5050 /* Find any frame on DISPLAY. */
5051 Lisp_Object fl_tail;
5053 frame = Qnil;
5054 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
5056 frame = XCAR (fl_tail);
5057 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5058 XFRAME (frame)->param_alist)),
5059 display)))
5060 break;
5064 CHECK_LIVE_FRAME (frame);
5065 f = XFRAME (frame);
5067 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5068 attrs[i] = Qunspecified;
5069 merge_face_ref (f, attributes, attrs, 1, 0);
5071 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5072 if (def_face == NULL)
5074 if (! realize_basic_faces (f))
5075 error ("Cannot realize default face");
5076 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5077 if (def_face == NULL)
5078 abort (); /* realize_basic_faces must have set it up */
5081 /* Dispatch to the appropriate handler. */
5082 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5083 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5084 #ifdef HAVE_WINDOW_SYSTEM
5085 else
5086 supports = x_supports_face_attributes_p (f, attrs, def_face);
5087 #endif
5089 return supports ? Qt : Qnil;
5093 /***********************************************************************
5094 Font selection
5095 ***********************************************************************/
5097 DEFUN ("internal-set-font-selection-order",
5098 Finternal_set_font_selection_order,
5099 Sinternal_set_font_selection_order, 1, 1, 0,
5100 doc: /* Set font selection order for face font selection to ORDER.
5101 ORDER must be a list of length 4 containing the symbols `:width',
5102 `:height', `:weight', and `:slant'. Face attributes appearing
5103 first in ORDER are matched first, e.g. if `:height' appears before
5104 `:weight' in ORDER, font selection first tries to find a font with
5105 a suitable height, and then tries to match the font weight.
5106 Value is ORDER. */)
5107 (Lisp_Object order)
5109 Lisp_Object list;
5110 int i;
5111 int indices[DIM (font_sort_order)];
5113 CHECK_LIST (order);
5114 memset (indices, 0, sizeof indices);
5115 i = 0;
5117 for (list = order;
5118 CONSP (list) && i < DIM (indices);
5119 list = XCDR (list), ++i)
5121 Lisp_Object attr = XCAR (list);
5122 int xlfd;
5124 if (EQ (attr, QCwidth))
5125 xlfd = XLFD_SWIDTH;
5126 else if (EQ (attr, QCheight))
5127 xlfd = XLFD_POINT_SIZE;
5128 else if (EQ (attr, QCweight))
5129 xlfd = XLFD_WEIGHT;
5130 else if (EQ (attr, QCslant))
5131 xlfd = XLFD_SLANT;
5132 else
5133 break;
5135 if (indices[i] != 0)
5136 break;
5137 indices[i] = xlfd;
5140 if (!NILP (list) || i != DIM (indices))
5141 signal_error ("Invalid font sort order", order);
5142 for (i = 0; i < DIM (font_sort_order); ++i)
5143 if (indices[i] == 0)
5144 signal_error ("Invalid font sort order", order);
5146 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5148 memcpy (font_sort_order, indices, sizeof font_sort_order);
5149 free_all_realized_faces (Qnil);
5152 font_update_sort_order (font_sort_order);
5154 return Qnil;
5158 DEFUN ("internal-set-alternative-font-family-alist",
5159 Finternal_set_alternative_font_family_alist,
5160 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5161 doc: /* Define alternative font families to try in face font selection.
5162 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5163 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5164 be found. Value is ALIST. */)
5165 (Lisp_Object alist)
5167 Lisp_Object entry, tail, tail2;
5169 CHECK_LIST (alist);
5170 alist = Fcopy_sequence (alist);
5171 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5173 entry = XCAR (tail);
5174 CHECK_LIST (entry);
5175 entry = Fcopy_sequence (entry);
5176 XSETCAR (tail, entry);
5177 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5178 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5181 Vface_alternative_font_family_alist = alist;
5182 free_all_realized_faces (Qnil);
5183 return alist;
5187 DEFUN ("internal-set-alternative-font-registry-alist",
5188 Finternal_set_alternative_font_registry_alist,
5189 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5190 doc: /* Define alternative font registries to try in face font selection.
5191 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5192 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5193 be found. Value is ALIST. */)
5194 (Lisp_Object alist)
5196 Lisp_Object entry, tail, tail2;
5198 CHECK_LIST (alist);
5199 alist = Fcopy_sequence (alist);
5200 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5202 entry = XCAR (tail);
5203 CHECK_LIST (entry);
5204 entry = Fcopy_sequence (entry);
5205 XSETCAR (tail, entry);
5206 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5207 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5209 Vface_alternative_font_registry_alist = alist;
5210 free_all_realized_faces (Qnil);
5211 return alist;
5215 #ifdef HAVE_WINDOW_SYSTEM
5217 /* Return the fontset id of the base fontset name or alias name given
5218 by the fontset attribute of ATTRS. Value is -1 if the fontset
5219 attribute of ATTRS doesn't name a fontset. */
5221 static int
5222 face_fontset (Lisp_Object *attrs)
5224 Lisp_Object name;
5226 name = attrs[LFACE_FONTSET_INDEX];
5227 if (!STRINGP (name))
5228 return -1;
5229 return fs_query_fontset (name, 0);
5232 #endif /* HAVE_WINDOW_SYSTEM */
5236 /***********************************************************************
5237 Face Realization
5238 ***********************************************************************/
5240 /* Realize basic faces on frame F. Value is zero if frame parameters
5241 of F don't contain enough information needed to realize the default
5242 face. */
5244 static int
5245 realize_basic_faces (struct frame *f)
5247 int success_p = 0;
5248 int count = SPECPDL_INDEX ();
5250 /* Block input here so that we won't be surprised by an X expose
5251 event, for instance, without having the faces set up. */
5252 BLOCK_INPUT;
5253 specbind (Qscalable_fonts_allowed, Qt);
5255 if (realize_default_face (f))
5257 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5258 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5259 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5260 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5261 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5262 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5263 realize_named_face (f, Qborder, BORDER_FACE_ID);
5264 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5265 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5266 realize_named_face (f, Qmenu, MENU_FACE_ID);
5267 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5269 /* Reflect changes in the `menu' face in menu bars. */
5270 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5272 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5273 #ifdef USE_X_TOOLKIT
5274 if (FRAME_WINDOW_P (f))
5275 x_update_menu_appearance (f);
5276 #endif
5279 success_p = 1;
5282 unbind_to (count, Qnil);
5283 UNBLOCK_INPUT;
5284 return success_p;
5288 /* Realize the default face on frame F. If the face is not fully
5289 specified, make it fully-specified. Attributes of the default face
5290 that are not explicitly specified are taken from frame parameters. */
5292 static int
5293 realize_default_face (struct frame *f)
5295 struct face_cache *c = FRAME_FACE_CACHE (f);
5296 Lisp_Object lface;
5297 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5298 struct face *face;
5300 /* If the `default' face is not yet known, create it. */
5301 lface = lface_from_face_name (f, Qdefault, 0);
5302 if (NILP (lface))
5304 Lisp_Object frame;
5305 XSETFRAME (frame, f);
5306 lface = Finternal_make_lisp_face (Qdefault, frame);
5309 #ifdef HAVE_WINDOW_SYSTEM
5310 if (FRAME_WINDOW_P (f))
5312 Lisp_Object font_object;
5314 XSETFONT (font_object, FRAME_FONT (f));
5315 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5316 LFACE_FONTSET (lface) = fontset_name (FRAME_FONTSET (f));
5317 f->default_face_done_p = 1;
5319 #endif /* HAVE_WINDOW_SYSTEM */
5321 if (!FRAME_WINDOW_P (f))
5323 LFACE_FAMILY (lface) = build_string ("default");
5324 LFACE_FOUNDRY (lface) = LFACE_FAMILY (lface);
5325 LFACE_SWIDTH (lface) = Qnormal;
5326 LFACE_HEIGHT (lface) = make_number (1);
5327 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5328 LFACE_WEIGHT (lface) = Qnormal;
5329 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5330 LFACE_SLANT (lface) = Qnormal;
5331 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5332 LFACE_FONTSET (lface) = Qnil;
5335 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5336 LFACE_UNDERLINE (lface) = Qnil;
5338 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5339 LFACE_OVERLINE (lface) = Qnil;
5341 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5342 LFACE_STRIKE_THROUGH (lface) = Qnil;
5344 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5345 LFACE_BOX (lface) = Qnil;
5347 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5348 LFACE_INVERSE (lface) = Qnil;
5350 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5352 /* This function is called so early that colors are not yet
5353 set in the frame parameter list. */
5354 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5356 if (CONSP (color) && STRINGP (XCDR (color)))
5357 LFACE_FOREGROUND (lface) = XCDR (color);
5358 else if (FRAME_WINDOW_P (f))
5359 return 0;
5360 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5361 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
5362 else
5363 abort ();
5366 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5368 /* This function is called so early that colors are not yet
5369 set in the frame parameter list. */
5370 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5371 if (CONSP (color) && STRINGP (XCDR (color)))
5372 LFACE_BACKGROUND (lface) = XCDR (color);
5373 else if (FRAME_WINDOW_P (f))
5374 return 0;
5375 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5376 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
5377 else
5378 abort ();
5381 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5382 LFACE_STIPPLE (lface) = Qnil;
5384 /* Realize the face; it must be fully-specified now. */
5385 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5386 check_lface (lface);
5387 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5388 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5390 #ifdef HAVE_WINDOW_SYSTEM
5391 #ifdef HAVE_X_WINDOWS
5392 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5394 /* This can happen when making a frame on a display that does
5395 not support the default font. */
5396 if (!face->font)
5397 return 0;
5399 /* Otherwise, the font specified for the frame was not
5400 acceptable as a font for the default face (perhaps because
5401 auto-scaled fonts are rejected), so we must adjust the frame
5402 font. */
5403 x_set_font (f, LFACE_FONT (lface), Qnil);
5405 #endif /* HAVE_X_WINDOWS */
5406 #endif /* HAVE_WINDOW_SYSTEM */
5407 return 1;
5411 /* Realize basic faces other than the default face in face cache C.
5412 SYMBOL is the face name, ID is the face id the realized face must
5413 have. The default face must have been realized already. */
5415 static void
5416 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5418 struct face_cache *c = FRAME_FACE_CACHE (f);
5419 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5420 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5421 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5423 /* The default face must exist and be fully specified. */
5424 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5425 check_lface_attrs (attrs);
5426 xassert (lface_fully_specified_p (attrs));
5428 /* If SYMBOL isn't know as a face, create it. */
5429 if (NILP (lface))
5431 Lisp_Object frame;
5432 XSETFRAME (frame, f);
5433 lface = Finternal_make_lisp_face (symbol, frame);
5436 /* Merge SYMBOL's face with the default face. */
5437 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5438 merge_face_vectors (f, symbol_attrs, attrs, 0);
5440 /* Realize the face. */
5441 realize_face (c, attrs, id);
5445 /* Realize the fully-specified face with attributes ATTRS in face
5446 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5447 non-negative, it is an ID of face to remove before caching the new
5448 face. Value is a pointer to the newly created realized face. */
5450 static struct face *
5451 realize_face (struct face_cache *cache, Lisp_Object *attrs, int former_face_id)
5453 struct face *face;
5455 /* LFACE must be fully specified. */
5456 xassert (cache != NULL);
5457 check_lface_attrs (attrs);
5459 if (former_face_id >= 0 && cache->used > former_face_id)
5461 /* Remove the former face. */
5462 struct face *former_face = cache->faces_by_id[former_face_id];
5463 uncache_face (cache, former_face);
5464 free_realized_face (cache->f, former_face);
5465 SET_FRAME_GARBAGED (cache->f);
5468 if (FRAME_WINDOW_P (cache->f))
5469 face = realize_x_face (cache, attrs);
5470 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5471 face = realize_tty_face (cache, attrs);
5472 else if (FRAME_INITIAL_P (cache->f))
5474 /* Create a dummy face. */
5475 face = make_realized_face (attrs);
5477 else
5478 abort ();
5480 /* Insert the new face. */
5481 cache_face (cache, face, lface_hash (attrs));
5482 return face;
5486 #ifdef HAVE_WINDOW_SYSTEM
5487 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5488 same attributes as BASE_FACE except for the font on frame F.
5489 FONT-OBJECT may be nil, in which case, realized a face of
5490 no-font. */
5492 static struct face *
5493 realize_non_ascii_face (struct frame *f, Lisp_Object font_object, struct face *base_face)
5495 struct face_cache *cache = FRAME_FACE_CACHE (f);
5496 struct face *face;
5498 face = (struct face *) xmalloc (sizeof *face);
5499 *face = *base_face;
5500 face->gc = 0;
5501 face->extra = NULL;
5502 face->overstrike
5503 = (! NILP (font_object)
5504 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5505 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5507 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5508 face->colors_copied_bitwise_p = 1;
5509 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5510 face->gc = 0;
5512 cache_face (cache, face, face->hash);
5514 return face;
5516 #endif /* HAVE_WINDOW_SYSTEM */
5519 /* Realize the fully-specified face with attributes ATTRS in face
5520 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5521 the new face doesn't share font with the default face, a fontname
5522 is allocated from the heap and set in `font_name' of the new face,
5523 but it is not yet loaded here. Value is a pointer to the newly
5524 created realized face. */
5526 static struct face *
5527 realize_x_face (struct face_cache *cache, Lisp_Object *attrs)
5529 struct face *face = NULL;
5530 #ifdef HAVE_WINDOW_SYSTEM
5531 struct face *default_face;
5532 struct frame *f;
5533 Lisp_Object stipple, overline, strike_through, box;
5535 xassert (FRAME_WINDOW_P (cache->f));
5537 /* Allocate a new realized face. */
5538 face = make_realized_face (attrs);
5539 face->ascii_face = face;
5541 f = cache->f;
5543 /* Determine the font to use. Most of the time, the font will be
5544 the same as the font of the default face, so try that first. */
5545 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5546 if (default_face
5547 && lface_same_font_attributes_p (default_face->lface, attrs))
5549 face->font = default_face->font;
5550 face->fontset
5551 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5553 else
5555 /* If the face attribute ATTRS specifies a fontset, use it as
5556 the base of a new realized fontset. Otherwise, use the same
5557 base fontset as of the default face. The base determines
5558 registry and encoding of a font. It may also determine
5559 foundry and family. The other fields of font name pattern
5560 are constructed from ATTRS. */
5561 int fontset = face_fontset (attrs);
5563 /* If we are realizing the default face, ATTRS should specify a
5564 fontset. In other words, if FONTSET is -1, we are not
5565 realizing the default face, thus the default face should have
5566 already been realized. */
5567 if (fontset == -1)
5569 if (default_face)
5570 fontset = default_face->fontset;
5571 if (fontset == -1)
5572 abort ();
5574 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5575 attrs[LFACE_FONT_INDEX]
5576 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5577 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5579 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5580 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5582 else
5584 face->font = NULL;
5585 face->fontset = -1;
5589 if (face->font
5590 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5591 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5592 face->overstrike = 1;
5594 /* Load colors, and set remaining attributes. */
5596 load_face_colors (f, face, attrs);
5598 /* Set up box. */
5599 box = attrs[LFACE_BOX_INDEX];
5600 if (STRINGP (box))
5602 /* A simple box of line width 1 drawn in color given by
5603 the string. */
5604 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5605 LFACE_BOX_INDEX);
5606 face->box = FACE_SIMPLE_BOX;
5607 face->box_line_width = 1;
5609 else if (INTEGERP (box))
5611 /* Simple box of specified line width in foreground color of the
5612 face. */
5613 xassert (XINT (box) != 0);
5614 face->box = FACE_SIMPLE_BOX;
5615 face->box_line_width = XINT (box);
5616 face->box_color = face->foreground;
5617 face->box_color_defaulted_p = 1;
5619 else if (CONSP (box))
5621 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5622 being one of `raised' or `sunken'. */
5623 face->box = FACE_SIMPLE_BOX;
5624 face->box_color = face->foreground;
5625 face->box_color_defaulted_p = 1;
5626 face->box_line_width = 1;
5628 while (CONSP (box))
5630 Lisp_Object keyword, value;
5632 keyword = XCAR (box);
5633 box = XCDR (box);
5635 if (!CONSP (box))
5636 break;
5637 value = XCAR (box);
5638 box = XCDR (box);
5640 if (EQ (keyword, QCline_width))
5642 if (INTEGERP (value) && XINT (value) != 0)
5643 face->box_line_width = XINT (value);
5645 else if (EQ (keyword, QCcolor))
5647 if (STRINGP (value))
5649 face->box_color = load_color (f, face, value,
5650 LFACE_BOX_INDEX);
5651 face->use_box_color_for_shadows_p = 1;
5654 else if (EQ (keyword, QCstyle))
5656 if (EQ (value, Qreleased_button))
5657 face->box = FACE_RAISED_BOX;
5658 else if (EQ (value, Qpressed_button))
5659 face->box = FACE_SUNKEN_BOX;
5664 /* Text underline, overline, strike-through. */
5666 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5668 /* Use default color (same as foreground color). */
5669 face->underline_p = 1;
5670 face->underline_defaulted_p = 1;
5671 face->underline_color = 0;
5673 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
5675 /* Use specified color. */
5676 face->underline_p = 1;
5677 face->underline_defaulted_p = 0;
5678 face->underline_color
5679 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5680 LFACE_UNDERLINE_INDEX);
5682 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
5684 face->underline_p = 0;
5685 face->underline_defaulted_p = 0;
5686 face->underline_color = 0;
5689 overline = attrs[LFACE_OVERLINE_INDEX];
5690 if (STRINGP (overline))
5692 face->overline_color
5693 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5694 LFACE_OVERLINE_INDEX);
5695 face->overline_p = 1;
5697 else if (EQ (overline, Qt))
5699 face->overline_color = face->foreground;
5700 face->overline_color_defaulted_p = 1;
5701 face->overline_p = 1;
5704 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5705 if (STRINGP (strike_through))
5707 face->strike_through_color
5708 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5709 LFACE_STRIKE_THROUGH_INDEX);
5710 face->strike_through_p = 1;
5712 else if (EQ (strike_through, Qt))
5714 face->strike_through_color = face->foreground;
5715 face->strike_through_color_defaulted_p = 1;
5716 face->strike_through_p = 1;
5719 stipple = attrs[LFACE_STIPPLE_INDEX];
5720 if (!NILP (stipple))
5721 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5722 #endif /* HAVE_WINDOW_SYSTEM */
5724 return face;
5728 /* Map a specified color of face FACE on frame F to a tty color index.
5729 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5730 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5731 default foreground/background colors. */
5733 static void
5734 map_tty_color (struct frame *f, struct face *face, enum lface_attribute_index idx, int *defaulted)
5736 Lisp_Object frame, color, def;
5737 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5738 unsigned long default_pixel =
5739 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5740 unsigned long pixel = default_pixel;
5741 #ifdef MSDOS
5742 unsigned long default_other_pixel =
5743 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5744 #endif
5746 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5748 XSETFRAME (frame, f);
5749 color = face->lface[idx];
5751 if (STRINGP (color)
5752 && SCHARS (color)
5753 && CONSP (Vtty_defined_color_alist)
5754 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5755 CONSP (def)))
5757 /* Associations in tty-defined-color-alist are of the form
5758 (NAME INDEX R G B). We need the INDEX part. */
5759 pixel = XINT (XCAR (XCDR (def)));
5762 if (pixel == default_pixel && STRINGP (color))
5764 pixel = load_color (f, face, color, idx);
5766 #ifdef MSDOS
5767 /* If the foreground of the default face is the default color,
5768 use the foreground color defined by the frame. */
5769 if (FRAME_MSDOS_P (f))
5771 if (pixel == default_pixel
5772 || pixel == FACE_TTY_DEFAULT_COLOR)
5774 if (foreground_p)
5775 pixel = FRAME_FOREGROUND_PIXEL (f);
5776 else
5777 pixel = FRAME_BACKGROUND_PIXEL (f);
5778 face->lface[idx] = tty_color_name (f, pixel);
5779 *defaulted = 1;
5781 else if (pixel == default_other_pixel)
5783 if (foreground_p)
5784 pixel = FRAME_BACKGROUND_PIXEL (f);
5785 else
5786 pixel = FRAME_FOREGROUND_PIXEL (f);
5787 face->lface[idx] = tty_color_name (f, pixel);
5788 *defaulted = 1;
5791 #endif /* MSDOS */
5794 if (foreground_p)
5795 face->foreground = pixel;
5796 else
5797 face->background = pixel;
5801 /* Realize the fully-specified face with attributes ATTRS in face
5802 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5803 Value is a pointer to the newly created realized face. */
5805 static struct face *
5806 realize_tty_face (struct face_cache *cache, Lisp_Object *attrs)
5808 struct face *face;
5809 int weight, slant;
5810 int face_colors_defaulted = 0;
5811 struct frame *f = cache->f;
5813 /* Frame must be a termcap frame. */
5814 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5816 /* Allocate a new realized face. */
5817 face = make_realized_face (attrs);
5818 #if 0
5819 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5820 #endif
5822 /* Map face attributes to TTY appearances. We map slant to
5823 dimmed text because we want italic text to appear differently
5824 and because dimmed text is probably used infrequently. */
5825 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5826 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5827 if (weight > 100)
5828 face->tty_bold_p = 1;
5829 if (weight < 100 || slant != 100)
5830 face->tty_dim_p = 1;
5831 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5832 face->tty_underline_p = 1;
5833 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5834 face->tty_reverse_p = 1;
5836 /* Map color names to color indices. */
5837 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5838 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5840 /* Swap colors if face is inverse-video. If the colors are taken
5841 from the frame colors, they are already inverted, since the
5842 frame-creation function calls x-handle-reverse-video. */
5843 if (face->tty_reverse_p && !face_colors_defaulted)
5845 unsigned long tem = face->foreground;
5846 face->foreground = face->background;
5847 face->background = tem;
5850 if (tty_suppress_bold_inverse_default_colors_p
5851 && face->tty_bold_p
5852 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5853 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5854 face->tty_bold_p = 0;
5856 return face;
5860 DEFUN ("tty-suppress-bold-inverse-default-colors",
5861 Ftty_suppress_bold_inverse_default_colors,
5862 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5863 doc: /* Suppress/allow boldness of faces with inverse default colors.
5864 SUPPRESS non-nil means suppress it.
5865 This affects bold faces on TTYs whose foreground is the default background
5866 color of the display and whose background is the default foreground color.
5867 For such faces, the bold face attribute is ignored if this variable
5868 is non-nil. */)
5869 (Lisp_Object suppress)
5871 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5872 ++face_change_count;
5873 return suppress;
5878 /***********************************************************************
5879 Computing Faces
5880 ***********************************************************************/
5882 /* Return the ID of the face to use to display character CH with face
5883 property PROP on frame F in current_buffer. */
5886 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5888 int face_id;
5890 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5891 ch = 0;
5893 if (NILP (prop))
5895 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5896 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5898 else
5900 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5901 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5902 memcpy (attrs, default_face->lface, sizeof attrs);
5903 merge_face_ref (f, prop, attrs, 1, 0);
5904 face_id = lookup_face (f, attrs);
5907 return face_id;
5910 /* Return the face ID associated with buffer position POS for
5911 displaying ASCII characters. Return in *ENDPTR the position at
5912 which a different face is needed, as far as text properties and
5913 overlays are concerned. W is a window displaying current_buffer.
5915 REGION_BEG, REGION_END delimit the region, so it can be
5916 highlighted.
5918 LIMIT is a position not to scan beyond. That is to limit the time
5919 this function can take.
5921 If MOUSE is non-zero, use the character's mouse-face, not its face.
5923 BASE_FACE_ID, if non-negative, specifies a base face id to use
5924 instead of DEFAULT_FACE_ID.
5926 The face returned is suitable for displaying ASCII characters. */
5929 face_at_buffer_position (struct window *w, EMACS_INT pos,
5930 EMACS_INT region_beg, EMACS_INT region_end,
5931 EMACS_INT *endptr, EMACS_INT limit,
5932 int mouse, int base_face_id)
5934 struct frame *f = XFRAME (w->frame);
5935 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5936 Lisp_Object prop, position;
5937 int i, noverlays;
5938 Lisp_Object *overlay_vec;
5939 Lisp_Object frame;
5940 EMACS_INT endpos;
5941 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5942 Lisp_Object limit1, end;
5943 struct face *default_face;
5945 /* W must display the current buffer. We could write this function
5946 to use the frame and buffer of W, but right now it doesn't. */
5947 /* xassert (XBUFFER (w->buffer) == current_buffer); */
5949 XSETFRAME (frame, f);
5950 XSETFASTINT (position, pos);
5952 endpos = ZV;
5953 if (pos < region_beg && region_beg < endpos)
5954 endpos = region_beg;
5956 /* Get the `face' or `mouse_face' text property at POS, and
5957 determine the next position at which the property changes. */
5958 prop = Fget_text_property (position, propname, w->buffer);
5959 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5960 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
5961 if (INTEGERP (end))
5962 endpos = XINT (end);
5964 /* Look at properties from overlays. */
5966 EMACS_INT next_overlay;
5968 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
5969 if (next_overlay < endpos)
5970 endpos = next_overlay;
5973 *endptr = endpos;
5975 default_face = FACE_FROM_ID (f, base_face_id >= 0 ? base_face_id
5976 : NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
5977 : lookup_basic_face (f, DEFAULT_FACE_ID));
5979 /* Optimize common cases where we can use the default face. */
5980 if (noverlays == 0
5981 && NILP (prop)
5982 && !(pos >= region_beg && pos < region_end))
5983 return default_face->id;
5985 /* Begin with attributes from the default face. */
5986 memcpy (attrs, default_face->lface, sizeof attrs);
5988 /* Merge in attributes specified via text properties. */
5989 if (!NILP (prop))
5990 merge_face_ref (f, prop, attrs, 1, 0);
5992 /* Now merge the overlay data. */
5993 noverlays = sort_overlays (overlay_vec, noverlays, w);
5994 for (i = 0; i < noverlays; i++)
5996 Lisp_Object oend;
5997 int oendpos;
5999 prop = Foverlay_get (overlay_vec[i], propname);
6000 if (!NILP (prop))
6001 merge_face_ref (f, prop, attrs, 1, 0);
6003 oend = OVERLAY_END (overlay_vec[i]);
6004 oendpos = OVERLAY_POSITION (oend);
6005 if (oendpos < endpos)
6006 endpos = oendpos;
6009 /* If in the region, merge in the region face. */
6010 if (pos >= region_beg && pos < region_end)
6012 merge_named_face (f, Qregion, attrs, 0);
6014 if (region_end < endpos)
6015 endpos = region_end;
6018 *endptr = endpos;
6020 /* Look up a realized face with the given face attributes,
6021 or realize a new one for ASCII characters. */
6022 return lookup_face (f, attrs);
6025 /* Return the face ID at buffer position POS for displaying ASCII
6026 characters associated with overlay strings for overlay OVERLAY.
6028 Like face_at_buffer_position except for OVERLAY. Currently it
6029 simply disregards the `face' properties of all overlays. */
6032 face_for_overlay_string (struct window *w, EMACS_INT pos,
6033 EMACS_INT region_beg, EMACS_INT region_end,
6034 EMACS_INT *endptr, EMACS_INT limit,
6035 int mouse, Lisp_Object overlay)
6037 struct frame *f = XFRAME (w->frame);
6038 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6039 Lisp_Object prop, position;
6040 Lisp_Object frame;
6041 int endpos;
6042 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6043 Lisp_Object limit1, end;
6044 struct face *default_face;
6046 /* W must display the current buffer. We could write this function
6047 to use the frame and buffer of W, but right now it doesn't. */
6048 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6050 XSETFRAME (frame, f);
6051 XSETFASTINT (position, pos);
6053 endpos = ZV;
6054 if (pos < region_beg && region_beg < endpos)
6055 endpos = region_beg;
6057 /* Get the `face' or `mouse_face' text property at POS, and
6058 determine the next position at which the property changes. */
6059 prop = Fget_text_property (position, propname, w->buffer);
6060 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6061 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6062 if (INTEGERP (end))
6063 endpos = XINT (end);
6065 *endptr = endpos;
6067 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6069 /* Optimize common cases where we can use the default face. */
6070 if (NILP (prop)
6071 && !(pos >= region_beg && pos < region_end))
6072 return DEFAULT_FACE_ID;
6074 /* Begin with attributes from the default face. */
6075 memcpy (attrs, default_face->lface, sizeof attrs);
6077 /* Merge in attributes specified via text properties. */
6078 if (!NILP (prop))
6079 merge_face_ref (f, prop, attrs, 1, 0);
6081 /* If in the region, merge in the region face. */
6082 if (pos >= region_beg && pos < region_end)
6084 merge_named_face (f, Qregion, attrs, 0);
6086 if (region_end < endpos)
6087 endpos = region_end;
6090 *endptr = endpos;
6092 /* Look up a realized face with the given face attributes,
6093 or realize a new one for ASCII characters. */
6094 return lookup_face (f, attrs);
6098 /* Compute the face at character position POS in Lisp string STRING on
6099 window W, for ASCII characters.
6101 If STRING is an overlay string, it comes from position BUFPOS in
6102 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6103 not an overlay string. W must display the current buffer.
6104 REGION_BEG and REGION_END give the start and end positions of the
6105 region; both are -1 if no region is visible.
6107 BASE_FACE_ID is the id of a face to merge with. For strings coming
6108 from overlays or the `display' property it is the face at BUFPOS.
6110 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6112 Set *ENDPTR to the next position where to check for faces in
6113 STRING; -1 if the face is constant from POS to the end of the
6114 string.
6116 Value is the id of the face to use. The face returned is suitable
6117 for displaying ASCII characters. */
6120 face_at_string_position (struct window *w, Lisp_Object string,
6121 EMACS_INT pos, EMACS_INT bufpos,
6122 EMACS_INT region_beg, EMACS_INT region_end,
6123 EMACS_INT *endptr, enum face_id base_face_id,
6124 int mouse_p)
6126 Lisp_Object prop, position, end, limit;
6127 struct frame *f = XFRAME (WINDOW_FRAME (w));
6128 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6129 struct face *base_face;
6130 int multibyte_p = STRING_MULTIBYTE (string);
6131 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6133 /* Get the value of the face property at the current position within
6134 STRING. Value is nil if there is no face property. */
6135 XSETFASTINT (position, pos);
6136 prop = Fget_text_property (position, prop_name, string);
6138 /* Get the next position at which to check for faces. Value of end
6139 is nil if face is constant all the way to the end of the string.
6140 Otherwise it is a string position where to check faces next.
6141 Limit is the maximum position up to which to check for property
6142 changes in Fnext_single_property_change. Strings are usually
6143 short, so set the limit to the end of the string. */
6144 XSETFASTINT (limit, SCHARS (string));
6145 end = Fnext_single_property_change (position, prop_name, string, limit);
6146 if (INTEGERP (end))
6147 *endptr = XFASTINT (end);
6148 else
6149 *endptr = -1;
6151 base_face = FACE_FROM_ID (f, base_face_id);
6152 xassert (base_face);
6154 /* Optimize the default case that there is no face property and we
6155 are not in the region. */
6156 if (NILP (prop)
6157 && (base_face_id != DEFAULT_FACE_ID
6158 /* BUFPOS <= 0 means STRING is not an overlay string, so
6159 that the region doesn't have to be taken into account. */
6160 || bufpos <= 0
6161 || bufpos < region_beg
6162 || bufpos >= region_end)
6163 && (multibyte_p
6164 /* We can't realize faces for different charsets differently
6165 if we don't have fonts, so we can stop here if not working
6166 on a window-system frame. */
6167 || !FRAME_WINDOW_P (f)
6168 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6169 return base_face->id;
6171 /* Begin with attributes from the base face. */
6172 memcpy (attrs, base_face->lface, sizeof attrs);
6174 /* Merge in attributes specified via text properties. */
6175 if (!NILP (prop))
6176 merge_face_ref (f, prop, attrs, 1, 0);
6178 /* If in the region, merge in the region face. */
6179 if (bufpos
6180 && bufpos >= region_beg
6181 && bufpos < region_end)
6182 merge_named_face (f, Qregion, attrs, 0);
6184 /* Look up a realized face with the given face attributes,
6185 or realize a new one for ASCII characters. */
6186 return lookup_face (f, attrs);
6190 /* Merge a face into a realized face.
6192 F is frame where faces are (to be) realized.
6194 FACE_NAME is named face to merge.
6196 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6198 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6200 BASE_FACE_ID is realized face to merge into.
6202 Return new face id.
6206 merge_faces (struct frame *f, Lisp_Object face_name, EMACS_INT face_id,
6207 int base_face_id)
6209 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6210 struct face *base_face;
6212 base_face = FACE_FROM_ID (f, base_face_id);
6213 if (!base_face)
6214 return base_face_id;
6216 if (EQ (face_name, Qt))
6218 if (face_id < 0 || face_id >= lface_id_to_name_size)
6219 return base_face_id;
6220 face_name = lface_id_to_name[face_id];
6221 /* When called during make-frame, lookup_derived_face may fail
6222 if the faces are uninitialized. Don't signal an error. */
6223 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6224 return (face_id >= 0 ? face_id : base_face_id);
6227 /* Begin with attributes from the base face. */
6228 memcpy (attrs, base_face->lface, sizeof attrs);
6230 if (!NILP (face_name))
6232 if (!merge_named_face (f, face_name, attrs, 0))
6233 return base_face_id;
6235 else
6237 struct face *face;
6238 if (face_id < 0)
6239 return base_face_id;
6240 face = FACE_FROM_ID (f, face_id);
6241 if (!face)
6242 return base_face_id;
6243 merge_face_vectors (f, face->lface, attrs, 0);
6246 /* Look up a realized face with the given face attributes,
6247 or realize a new one for ASCII characters. */
6248 return lookup_face (f, attrs);
6253 #ifndef HAVE_X_WINDOWS
6254 DEFUN ("x-load-color-file", Fx_load_color_file,
6255 Sx_load_color_file, 1, 1, 0,
6256 doc: /* Create an alist of color entries from an external file.
6258 The file should define one named RGB color per line like so:
6259 R G B name
6260 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6261 (Lisp_Object filename)
6263 FILE *fp;
6264 Lisp_Object cmap = Qnil;
6265 Lisp_Object abspath;
6267 CHECK_STRING (filename);
6268 abspath = Fexpand_file_name (filename, Qnil);
6270 fp = fopen (SDATA (abspath), "rt");
6271 if (fp)
6273 char buf[512];
6274 int red, green, blue;
6275 int num;
6277 BLOCK_INPUT;
6279 while (fgets (buf, sizeof (buf), fp) != NULL) {
6280 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6282 char *name = buf + num;
6283 num = strlen (name) - 1;
6284 if (num >= 0 && name[num] == '\n')
6285 name[num] = 0;
6286 cmap = Fcons (Fcons (build_string (name),
6287 #ifdef WINDOWSNT
6288 make_number (RGB (red, green, blue))),
6289 #else
6290 make_number ((red << 16) | (green << 8) | blue)),
6291 #endif
6292 cmap);
6295 fclose (fp);
6297 UNBLOCK_INPUT;
6300 return cmap;
6302 #endif
6305 /***********************************************************************
6306 Tests
6307 ***********************************************************************/
6309 #if GLYPH_DEBUG
6311 /* Print the contents of the realized face FACE to stderr. */
6313 static void
6314 dump_realized_face (struct face *face)
6316 fprintf (stderr, "ID: %d\n", face->id);
6317 #ifdef HAVE_X_WINDOWS
6318 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6319 #endif
6320 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6321 face->foreground,
6322 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6323 fprintf (stderr, "background: 0x%lx (%s)\n",
6324 face->background,
6325 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6326 if (face->font)
6327 fprintf (stderr, "font_name: %s (%s)\n",
6328 SDATA (face->font->props[FONT_NAME_INDEX]),
6329 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6330 #ifdef HAVE_X_WINDOWS
6331 fprintf (stderr, "font = %p\n", face->font);
6332 #endif
6333 fprintf (stderr, "fontset: %d\n", face->fontset);
6334 fprintf (stderr, "underline: %d (%s)\n",
6335 face->underline_p,
6336 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6337 fprintf (stderr, "hash: %d\n", face->hash);
6341 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6342 (Lisp_Object n)
6344 if (NILP (n))
6346 int i;
6348 fprintf (stderr, "font selection order: ");
6349 for (i = 0; i < DIM (font_sort_order); ++i)
6350 fprintf (stderr, "%d ", font_sort_order[i]);
6351 fprintf (stderr, "\n");
6353 fprintf (stderr, "alternative fonts: ");
6354 debug_print (Vface_alternative_font_family_alist);
6355 fprintf (stderr, "\n");
6357 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6358 Fdump_face (make_number (i));
6360 else
6362 struct face *face;
6363 CHECK_NUMBER (n);
6364 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6365 if (face == NULL)
6366 error ("Not a valid face");
6367 dump_realized_face (face);
6370 return Qnil;
6374 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6375 0, 0, 0, doc: /* */)
6376 (void)
6378 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6379 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6380 fprintf (stderr, "number of GCs = %d\n", ngcs);
6381 return Qnil;
6384 #endif /* GLYPH_DEBUG != 0 */
6388 /***********************************************************************
6389 Initialization
6390 ***********************************************************************/
6392 void
6393 syms_of_xfaces (void)
6395 Qface = intern_c_string ("face");
6396 staticpro (&Qface);
6397 Qface_no_inherit = intern_c_string ("face-no-inherit");
6398 staticpro (&Qface_no_inherit);
6399 Qbitmap_spec_p = intern_c_string ("bitmap-spec-p");
6400 staticpro (&Qbitmap_spec_p);
6401 Qframe_set_background_mode = intern_c_string ("frame-set-background-mode");
6402 staticpro (&Qframe_set_background_mode);
6404 /* Lisp face attribute keywords. */
6405 QCfamily = intern_c_string (":family");
6406 staticpro (&QCfamily);
6407 QCheight = intern_c_string (":height");
6408 staticpro (&QCheight);
6409 QCweight = intern_c_string (":weight");
6410 staticpro (&QCweight);
6411 QCslant = intern_c_string (":slant");
6412 staticpro (&QCslant);
6413 QCunderline = intern_c_string (":underline");
6414 staticpro (&QCunderline);
6415 QCinverse_video = intern_c_string (":inverse-video");
6416 staticpro (&QCinverse_video);
6417 QCreverse_video = intern_c_string (":reverse-video");
6418 staticpro (&QCreverse_video);
6419 QCforeground = intern_c_string (":foreground");
6420 staticpro (&QCforeground);
6421 QCbackground = intern_c_string (":background");
6422 staticpro (&QCbackground);
6423 QCstipple = intern_c_string (":stipple");
6424 staticpro (&QCstipple);
6425 QCwidth = intern_c_string (":width");
6426 staticpro (&QCwidth);
6427 QCfont = intern_c_string (":font");
6428 staticpro (&QCfont);
6429 QCfontset = intern_c_string (":fontset");
6430 staticpro (&QCfontset);
6431 QCbold = intern_c_string (":bold");
6432 staticpro (&QCbold);
6433 QCitalic = intern_c_string (":italic");
6434 staticpro (&QCitalic);
6435 QCoverline = intern_c_string (":overline");
6436 staticpro (&QCoverline);
6437 QCstrike_through = intern_c_string (":strike-through");
6438 staticpro (&QCstrike_through);
6439 QCbox = intern_c_string (":box");
6440 staticpro (&QCbox);
6441 QCinherit = intern_c_string (":inherit");
6442 staticpro (&QCinherit);
6444 /* Symbols used for Lisp face attribute values. */
6445 QCcolor = intern_c_string (":color");
6446 staticpro (&QCcolor);
6447 QCline_width = intern_c_string (":line-width");
6448 staticpro (&QCline_width);
6449 QCstyle = intern_c_string (":style");
6450 staticpro (&QCstyle);
6451 Qreleased_button = intern_c_string ("released-button");
6452 staticpro (&Qreleased_button);
6453 Qpressed_button = intern_c_string ("pressed-button");
6454 staticpro (&Qpressed_button);
6455 Qnormal = intern_c_string ("normal");
6456 staticpro (&Qnormal);
6457 Qultra_light = intern_c_string ("ultra-light");
6458 staticpro (&Qultra_light);
6459 Qextra_light = intern_c_string ("extra-light");
6460 staticpro (&Qextra_light);
6461 Qlight = intern_c_string ("light");
6462 staticpro (&Qlight);
6463 Qsemi_light = intern_c_string ("semi-light");
6464 staticpro (&Qsemi_light);
6465 Qsemi_bold = intern_c_string ("semi-bold");
6466 staticpro (&Qsemi_bold);
6467 Qbold = intern_c_string ("bold");
6468 staticpro (&Qbold);
6469 Qextra_bold = intern_c_string ("extra-bold");
6470 staticpro (&Qextra_bold);
6471 Qultra_bold = intern_c_string ("ultra-bold");
6472 staticpro (&Qultra_bold);
6473 Qoblique = intern_c_string ("oblique");
6474 staticpro (&Qoblique);
6475 Qitalic = intern_c_string ("italic");
6476 staticpro (&Qitalic);
6477 Qreverse_oblique = intern_c_string ("reverse-oblique");
6478 staticpro (&Qreverse_oblique);
6479 Qreverse_italic = intern_c_string ("reverse-italic");
6480 staticpro (&Qreverse_italic);
6481 Qultra_condensed = intern_c_string ("ultra-condensed");
6482 staticpro (&Qultra_condensed);
6483 Qextra_condensed = intern_c_string ("extra-condensed");
6484 staticpro (&Qextra_condensed);
6485 Qcondensed = intern_c_string ("condensed");
6486 staticpro (&Qcondensed);
6487 Qsemi_condensed = intern_c_string ("semi-condensed");
6488 staticpro (&Qsemi_condensed);
6489 Qsemi_expanded = intern_c_string ("semi-expanded");
6490 staticpro (&Qsemi_expanded);
6491 Qexpanded = intern_c_string ("expanded");
6492 staticpro (&Qexpanded);
6493 Qextra_expanded = intern_c_string ("extra-expanded");
6494 staticpro (&Qextra_expanded);
6495 Qultra_expanded = intern_c_string ("ultra-expanded");
6496 staticpro (&Qultra_expanded);
6497 Qbackground_color = intern_c_string ("background-color");
6498 staticpro (&Qbackground_color);
6499 Qforeground_color = intern_c_string ("foreground-color");
6500 staticpro (&Qforeground_color);
6501 Qunspecified = intern_c_string ("unspecified");
6502 staticpro (&Qunspecified);
6503 Qignore_defface = intern_c_string (":ignore-defface");
6504 staticpro (&Qignore_defface);
6506 Qface_alias = intern_c_string ("face-alias");
6507 staticpro (&Qface_alias);
6508 Qdefault = intern_c_string ("default");
6509 staticpro (&Qdefault);
6510 Qtool_bar = intern_c_string ("tool-bar");
6511 staticpro (&Qtool_bar);
6512 Qregion = intern_c_string ("region");
6513 staticpro (&Qregion);
6514 Qfringe = intern_c_string ("fringe");
6515 staticpro (&Qfringe);
6516 Qheader_line = intern_c_string ("header-line");
6517 staticpro (&Qheader_line);
6518 Qscroll_bar = intern_c_string ("scroll-bar");
6519 staticpro (&Qscroll_bar);
6520 Qmenu = intern_c_string ("menu");
6521 staticpro (&Qmenu);
6522 Qcursor = intern_c_string ("cursor");
6523 staticpro (&Qcursor);
6524 Qborder = intern_c_string ("border");
6525 staticpro (&Qborder);
6526 Qmouse = intern_c_string ("mouse");
6527 staticpro (&Qmouse);
6528 Qmode_line_inactive = intern_c_string ("mode-line-inactive");
6529 staticpro (&Qmode_line_inactive);
6530 Qvertical_border = intern_c_string ("vertical-border");
6531 staticpro (&Qvertical_border);
6532 Qtty_color_desc = intern_c_string ("tty-color-desc");
6533 staticpro (&Qtty_color_desc);
6534 Qtty_color_standard_values = intern_c_string ("tty-color-standard-values");
6535 staticpro (&Qtty_color_standard_values);
6536 Qtty_color_by_index = intern_c_string ("tty-color-by-index");
6537 staticpro (&Qtty_color_by_index);
6538 Qtty_color_alist = intern_c_string ("tty-color-alist");
6539 staticpro (&Qtty_color_alist);
6540 Qscalable_fonts_allowed = intern_c_string ("scalable-fonts-allowed");
6541 staticpro (&Qscalable_fonts_allowed);
6543 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
6544 staticpro (&Vparam_value_alist);
6545 Vface_alternative_font_family_alist = Qnil;
6546 staticpro (&Vface_alternative_font_family_alist);
6547 Vface_alternative_font_registry_alist = Qnil;
6548 staticpro (&Vface_alternative_font_registry_alist);
6550 defsubr (&Sinternal_make_lisp_face);
6551 defsubr (&Sinternal_lisp_face_p);
6552 defsubr (&Sinternal_set_lisp_face_attribute);
6553 #ifdef HAVE_WINDOW_SYSTEM
6554 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6555 #endif
6556 defsubr (&Scolor_gray_p);
6557 defsubr (&Scolor_supported_p);
6558 #ifndef HAVE_X_WINDOWS
6559 defsubr (&Sx_load_color_file);
6560 #endif
6561 defsubr (&Sface_attribute_relative_p);
6562 defsubr (&Smerge_face_attribute);
6563 defsubr (&Sinternal_get_lisp_face_attribute);
6564 defsubr (&Sinternal_lisp_face_attribute_values);
6565 defsubr (&Sinternal_lisp_face_equal_p);
6566 defsubr (&Sinternal_lisp_face_empty_p);
6567 defsubr (&Sinternal_copy_lisp_face);
6568 defsubr (&Sinternal_merge_in_global_face);
6569 defsubr (&Sface_font);
6570 defsubr (&Sframe_face_alist);
6571 defsubr (&Sdisplay_supports_face_attributes_p);
6572 defsubr (&Scolor_distance);
6573 defsubr (&Sinternal_set_font_selection_order);
6574 defsubr (&Sinternal_set_alternative_font_family_alist);
6575 defsubr (&Sinternal_set_alternative_font_registry_alist);
6576 defsubr (&Sface_attributes_as_vector);
6577 #if GLYPH_DEBUG
6578 defsubr (&Sdump_face);
6579 defsubr (&Sshow_face_resources);
6580 #endif /* GLYPH_DEBUG */
6581 defsubr (&Sclear_face_cache);
6582 defsubr (&Stty_suppress_bold_inverse_default_colors);
6584 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6585 defsubr (&Sdump_colors);
6586 #endif
6588 DEFVAR_LISP ("font-list-limit", Vfont_list_limit,
6589 doc: /* *Limit for font matching.
6590 If an integer > 0, font matching functions won't load more than
6591 that number of fonts when searching for a matching font. */);
6592 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
6594 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6595 doc: /* List of global face definitions (for internal use only.) */);
6596 Vface_new_frame_defaults = Qnil;
6598 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6599 doc: /* *Default stipple pattern used on monochrome displays.
6600 This stipple pattern is used on monochrome displays
6601 instead of shades of gray for a face background color.
6602 See `set-face-stipple' for possible values for this variable. */);
6603 Vface_default_stipple = make_pure_c_string ("gray3");
6605 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6606 doc: /* An alist of defined terminal colors and their RGB values.
6607 See the docstring of `tty-color-alist' for the details. */);
6608 Vtty_defined_color_alist = Qnil;
6610 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6611 doc: /* Allowed scalable fonts.
6612 A value of nil means don't allow any scalable fonts.
6613 A value of t means allow any scalable font.
6614 Otherwise, value must be a list of regular expressions. A font may be
6615 scaled if its name matches a regular expression in the list.
6616 Note that if value is nil, a scalable font might still be used, if no
6617 other font of the appropriate family and registry is available. */);
6618 Vscalable_fonts_allowed = Qnil;
6620 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6621 doc: /* List of ignored fonts.
6622 Each element is a regular expression that matches names of fonts to
6623 ignore. */);
6624 Vface_ignored_fonts = Qnil;
6626 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6627 doc: /* Alist of face remappings.
6628 Each element is of the form:
6630 (FACE REPLACEMENT...),
6632 which causes display of the face FACE to use REPLACEMENT... instead.
6633 REPLACEMENT... is interpreted the same way as the value of a `face'
6634 text property: it may be (1) A face name, (2) A list of face names,
6635 (3) A property-list of face attribute/value pairs, or (4) A list of
6636 face names or lists containing face attribute/value pairs.
6638 Multiple entries in REPLACEMENT... are merged together to form the final
6639 result, with faces or attributes earlier in the list taking precedence
6640 over those that are later.
6642 Face-name remapping cycles are suppressed; recursive references use the
6643 underlying face instead of the remapped face. So a remapping of the form:
6645 (FACE EXTRA-FACE... FACE)
6649 (FACE (FACE-ATTR VAL ...) FACE)
6651 will cause EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6652 existing definition of FACE. Note that for the default face, this isn't
6653 necessary, as every face inherits from the default face.
6655 Making this variable buffer-local is a good way to allow buffer-specific
6656 face definitions. For instance, the mode my-mode could define a face
6657 `my-mode-default', and then in the mode setup function, do:
6659 (set (make-local-variable 'face-remapping-alist)
6660 '((default my-mode-default)))).
6662 Because Emacs normally only redraws screen areas when the underlying
6663 buffer contents change, you may need to call `redraw-display' after
6664 changing this variable for it to take effect. */);
6665 Vface_remapping_alist = Qnil;
6667 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6668 doc: /* Alist of fonts vs the rescaling factors.
6669 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6670 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6671 RESCALE-RATIO is a floating point number to specify how much larger
6672 \(or smaller) font we should use. For instance, if a face requests
6673 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6674 Vface_font_rescale_alist = Qnil;
6676 #ifdef HAVE_WINDOW_SYSTEM
6677 defsubr (&Sbitmap_spec_p);
6678 defsubr (&Sx_list_fonts);
6679 defsubr (&Sinternal_face_x_get_resource);
6680 defsubr (&Sx_family_fonts);
6681 #endif