(tar-summarize-buffer): Fix reporter initialization.
[emacs.git] / src / xfaces.c
blob093fde0b39aee6ee94d480a715d37e279967dd54
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 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. Relative proportionate width, aka character set width or set
31 width (swidth), e.g. `semi-compressed'.
33 3. Font height in 1/10pt.
35 4. Font weight, e.g. `bold'.
37 5. Font slant, e.g. `italic'.
39 6. Foreground color.
41 7. Background color.
43 8. Whether or not characters should be underlined, and in what color.
45 9. Whether or not characters should be displayed in inverse video.
47 10. A background stipple, a bitmap.
49 11. Whether or not characters should be overlined, and in what color.
51 12. Whether or not characters should be strike-through, and in what
52 color.
54 13. Whether or not a box should be drawn around characters, the box
55 type, and, for simple boxes, in what color.
57 14. Font-spec, or nil. This is a special attribute.
59 A font-spec is a collection of font attributes (specs).
61 When this attribute is specified, the face uses a font matching
62 with the specs as is except for what overwritten by the specs in
63 the fontset (see below). In addition, the other font-related
64 attributes (1st thru 5th) are updated from the spec.
66 On the other hand, if one of the other font-related attributes are
67 specified, the correspoinding specs in this attribute is set to nil.
69 15. A face name or list of face names from which to inherit attributes.
71 16. A specified average font width, which is invisible from Lisp,
72 and is used to ensure that a font specified on the command line,
73 for example, can be matched exactly.
75 17. A fontset name. This is another special attribute.
77 A fontset is a mappings from characters to font-specs, and the
78 specs overwrite the font-spec in the 14th attribute.
81 Faces are frame-local by nature because Emacs allows to define the
82 same named face (face names are symbols) differently for different
83 frames. Each frame has an alist of face definitions for all named
84 faces. The value of a named face in such an alist is a Lisp vector
85 with the symbol `face' in slot 0, and a slot for each of the face
86 attributes mentioned above.
88 There is also a global face alist `Vface_new_frame_defaults'. Face
89 definitions from this list are used to initialize faces of newly
90 created frames.
92 A face doesn't have to specify all attributes. Those not specified
93 have a value of `unspecified'. Faces specifying all attributes but
94 the 14th are called `fully-specified'.
97 Face merging.
99 The display style of a given character in the text is determined by
100 combining several faces. This process is called `face merging'.
101 Any aspect of the display style that isn't specified by overlays or
102 text properties is taken from the `default' face. Since it is made
103 sure that the default face is always fully-specified, face merging
104 always results in a fully-specified face.
107 Face realization.
109 After all face attributes for a character have been determined by
110 merging faces of that character, that face is `realized'. The
111 realization process maps face attributes to what is physically
112 available on the system where Emacs runs. The result is a
113 `realized face' in form of a struct face which is stored in the
114 face cache of the frame on which it was realized.
116 Face realization is done in the context of the character to display
117 because different fonts may be used for different characters. In
118 other words, for characters that have different font
119 specifications, different realized faces are needed to display
120 them.
122 Font specification is done by fontsets. See the comment in
123 fontset.c for the details. In the current implementation, all ASCII
124 characters share the same font in a fontset.
126 Faces are at first realized for ASCII characters, and, at that
127 time, assigned a specific realized fontset. Hereafter, we call
128 such a face as `ASCII face'. When a face for a multibyte character
129 is realized, it inherits (thus shares) a fontset of an ASCII face
130 that has the same attributes other than font-related ones.
132 Thus, all realized faces have a realized fontset.
135 Unibyte text.
137 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
138 font as ASCII characters. That is because it is expected that
139 unibyte text users specify a font that is suitable both for ASCII
140 and raw 8-bit characters.
143 Font selection.
145 Font selection tries to find the best available matching font for a
146 given (character, face) combination.
148 If the face specifies a fontset name, that fontset determines a
149 pattern for fonts of the given character. If the face specifies a
150 font name or the other font-related attributes, a fontset is
151 realized from the default fontset. In that case, that
152 specification determines a pattern for ASCII characters and the
153 default fontset determines a pattern for multibyte characters.
155 Available fonts on the system on which Emacs runs are then matched
156 against the font pattern. The result of font selection is the best
157 match for the given face attributes in this font list.
159 Font selection can be influenced by the user.
161 1. The user can specify the relative importance he gives the face
162 attributes width, height, weight, and slant by setting
163 face-font-selection-order (faces.el) to a list of face attribute
164 names. The default is '(:width :height :weight :slant), and means
165 that font selection first tries to find a good match for the font
166 width specified by a face, then---within fonts with that
167 width---tries to find a best match for the specified font height,
168 etc.
170 2. Setting face-font-family-alternatives allows the user to
171 specify alternative font families to try if a family specified by a
172 face doesn't exist.
174 3. Setting face-font-registry-alternatives allows the user to
175 specify all alternative font registries to try for a face
176 specifying a registry.
178 4. Setting face-ignored-fonts allows the user to ignore specific
179 fonts.
182 Character composition.
184 Usually, the realization process is already finished when Emacs
185 actually reflects the desired glyph matrix on the screen. However,
186 on displaying a composition (sequence of characters to be composed
187 on the screen), a suitable font for the components of the
188 composition is selected and realized while drawing them on the
189 screen, i.e. the realization process is delayed but in principle
190 the same.
193 Initialization of basic faces.
195 The faces `default', `modeline' are considered `basic faces'.
196 When redisplay happens the first time for a newly created frame,
197 basic faces are realized for CHARSET_ASCII. Frame parameters are
198 used to fill in unspecified attributes of the default face. */
200 #include <config.h>
201 #include <stdio.h>
202 #include <sys/types.h>
203 #include <sys/stat.h>
204 #include <stdio.h> /* This needs to be before termchar.h */
206 #include "lisp.h"
207 #include "character.h"
208 #include "charset.h"
209 #include "keyboard.h"
210 #include "frame.h"
211 #include "termhooks.h"
213 #ifdef HAVE_X_WINDOWS
214 #include "xterm.h"
215 #ifdef USE_MOTIF
216 #include <Xm/Xm.h>
217 #include <Xm/XmStrDefs.h>
218 #endif /* USE_MOTIF */
219 #endif /* HAVE_X_WINDOWS */
221 #ifdef MSDOS
222 #include "dosfns.h"
223 #endif
225 #ifdef WINDOWSNT
226 #include "w32term.h"
227 #include "fontset.h"
228 /* Redefine X specifics to W32 equivalents to avoid cluttering the
229 code with #ifdef blocks. */
230 #undef FRAME_X_DISPLAY_INFO
231 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
232 #define x_display_info w32_display_info
233 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
234 #define check_x check_w32
235 #define x_list_fonts w32_list_fonts
236 #define GCGraphicsExposures 0
237 #endif /* WINDOWSNT */
239 #ifdef MAC_OS
240 #include "macterm.h"
241 #define x_display_info mac_display_info
242 #define check_x check_mac
243 #endif /* MAC_OS */
245 #include "buffer.h"
246 #include "dispextern.h"
247 #include "blockinput.h"
248 #include "window.h"
249 #include "intervals.h"
250 #include "termchar.h"
252 #include "font.h"
253 #ifdef HAVE_WINDOW_SYSTEM
254 #include "fontset.h"
255 #endif /* HAVE_WINDOW_SYSTEM */
257 #ifdef HAVE_X_WINDOWS
259 /* Compensate for a bug in Xos.h on some systems, on which it requires
260 time.h. On some such systems, Xos.h tries to redefine struct
261 timeval and struct timezone if USG is #defined while it is
262 #included. */
264 #ifdef XOS_NEEDS_TIME_H
265 #include <time.h>
266 #undef USG
267 #include <X11/Xos.h>
268 #define USG
269 #define __TIMEVAL__
270 #else /* not XOS_NEEDS_TIME_H */
271 #include <X11/Xos.h>
272 #endif /* not XOS_NEEDS_TIME_H */
274 #endif /* HAVE_X_WINDOWS */
276 #include <ctype.h>
278 /* Number of pt per inch (from the TeXbook). */
280 #define PT_PER_INCH 72.27
282 /* Non-zero if face attribute ATTR is unspecified. */
284 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
286 /* Non-zero if face attribute ATTR is `ignore-defface'. */
288 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), Qignore_defface)
290 /* Value is the number of elements of VECTOR. */
292 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
294 /* Make a copy of string S on the stack using alloca. Value is a pointer
295 to the copy. */
297 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
299 /* Make a copy of the contents of Lisp string S on the stack using
300 alloca. Value is a pointer to the copy. */
302 #define LSTRDUPA(S) STRDUPA (SDATA ((S)))
304 /* Size of hash table of realized faces in face caches (should be a
305 prime number). */
307 #define FACE_CACHE_BUCKETS_SIZE 1001
309 /* Keyword symbols used for face attribute names. */
311 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
312 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
313 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
314 Lisp_Object QCreverse_video;
315 Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
316 Lisp_Object QCfontset;
318 /* Keywords symbols used for font properties. */
319 extern Lisp_Object QCfoundry, QCadstyle, QCregistry;
320 extern Lisp_Object QCspacing, QCsize, QCavgwidth;
321 extern Lisp_Object Qp;
323 /* Symbols used for attribute values. */
325 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
326 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
327 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
328 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
329 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
330 Lisp_Object Qultra_expanded;
331 Lisp_Object Qreleased_button, Qpressed_button;
332 Lisp_Object QCstyle, QCcolor, QCline_width;
333 Lisp_Object Qunspecified;
334 Lisp_Object Qignore_defface;
336 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
338 /* The name of the function to call when the background of the frame
339 has changed, frame_set_background_mode. */
341 Lisp_Object Qframe_set_background_mode;
343 /* Names of basic faces. */
345 Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
346 Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
347 Lisp_Object Qmode_line_inactive, Qvertical_border;
348 extern Lisp_Object Qmode_line;
350 /* The symbol `face-alias'. A symbols having that property is an
351 alias for another face. Value of the property is the name of
352 the aliased face. */
354 Lisp_Object Qface_alias;
356 extern Lisp_Object Qcircular_list;
358 /* Default stipple pattern used on monochrome displays. This stipple
359 pattern is used on monochrome displays instead of shades of gray
360 for a face background color. See `set-face-stipple' for possible
361 values for this variable. */
363 Lisp_Object Vface_default_stipple;
365 /* Alist of alternative font families. Each element is of the form
366 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
367 try FAMILY1, then FAMILY2, ... */
369 Lisp_Object Vface_alternative_font_family_alist;
371 /* Alist of alternative font registries. Each element is of the form
372 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
373 loaded, try REGISTRY1, then REGISTRY2, ... */
375 Lisp_Object Vface_alternative_font_registry_alist;
377 /* Allowed scalable fonts. A value of nil means don't allow any
378 scalable fonts. A value of t means allow the use of any scalable
379 font. Otherwise, value must be a list of regular expressions. A
380 font may be scaled if its name matches a regular expression in the
381 list. */
383 Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
385 /* List of regular expressions that matches names of fonts to ignore. */
387 Lisp_Object Vface_ignored_fonts;
389 /* Alist of font name patterns vs the rescaling factor. */
391 Lisp_Object Vface_font_rescale_alist;
393 /* Maximum number of fonts to consider in font_list. If not an
394 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
396 Lisp_Object Vfont_list_limit;
397 #define DEFAULT_FONT_LIST_LIMIT 100
399 /* The symbols `foreground-color' and `background-color' which can be
400 used as part of a `face' property. This is for compatibility with
401 Emacs 20.2. */
403 Lisp_Object Qforeground_color, Qbackground_color;
405 /* The symbols `face' and `mouse-face' used as text properties. */
407 Lisp_Object Qface;
408 extern Lisp_Object Qmouse_face;
410 /* Property for basic faces which other faces cannot inherit. */
412 Lisp_Object Qface_no_inherit;
414 /* Error symbol for wrong_type_argument in load_pixmap. */
416 Lisp_Object Qbitmap_spec_p;
418 /* Alist of global face definitions. Each element is of the form
419 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
420 is a Lisp vector of face attributes. These faces are used
421 to initialize faces for new frames. */
423 Lisp_Object Vface_new_frame_defaults;
425 /* The next ID to assign to Lisp faces. */
427 static int next_lface_id;
429 /* A vector mapping Lisp face Id's to face names. */
431 static Lisp_Object *lface_id_to_name;
432 static int lface_id_to_name_size;
434 /* TTY color-related functions (defined in tty-colors.el). */
436 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
438 /* The name of the function used to compute colors on TTYs. */
440 Lisp_Object Qtty_color_alist;
442 /* An alist of defined terminal colors and their RGB values. */
444 Lisp_Object Vtty_defined_color_alist;
446 /* Counter for calls to clear_face_cache. If this counter reaches
447 CLEAR_FONT_TABLE_COUNT, and a frame has more than
448 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
450 static int clear_font_table_count;
451 #define CLEAR_FONT_TABLE_COUNT 100
452 #define CLEAR_FONT_TABLE_NFONTS 10
454 /* Non-zero means face attributes have been changed since the last
455 redisplay. Used in redisplay_internal. */
457 int face_change_count;
459 /* Non-zero means don't display bold text if a face's foreground
460 and background colors are the inverse of the default colors of the
461 display. This is a kluge to suppress `bold black' foreground text
462 which is hard to read on an LCD monitor. */
464 int tty_suppress_bold_inverse_default_colors_p;
466 /* A list of the form `((x . y))' used to avoid consing in
467 Finternal_set_lisp_face_attribute. */
469 static Lisp_Object Vparam_value_alist;
471 /* The total number of colors currently allocated. */
473 #if GLYPH_DEBUG
474 static int ncolors_allocated;
475 static int npixmaps_allocated;
476 static int ngcs;
477 #endif
479 /* Non-zero means the definition of the `menu' face for new frames has
480 been changed. */
482 int menu_face_changed_default;
485 /* Function prototypes. */
487 struct font_name;
488 struct table_entry;
489 struct named_merge_point;
491 static void map_tty_color P_ ((struct frame *, struct face *,
492 enum lface_attribute_index, int *));
493 static Lisp_Object resolve_face_name P_ ((Lisp_Object, int));
494 static int may_use_scalable_font_p P_ ((const char *));
495 static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
496 static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
497 static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
498 static unsigned char *xstrlwr P_ ((unsigned char *));
499 static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
500 static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
501 static void free_face_colors P_ ((struct frame *, struct face *));
502 static int face_color_gray_p P_ ((struct frame *, char *));
503 static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *,
504 int));
505 static struct face *realize_non_ascii_face P_ ((struct frame *, Lisp_Object,
506 struct face *));
507 static struct face *realize_x_face P_ ((struct face_cache *, Lisp_Object *));
508 static struct face *realize_tty_face P_ ((struct face_cache *, Lisp_Object *));
509 static int realize_basic_faces P_ ((struct frame *));
510 static int realize_default_face P_ ((struct frame *));
511 static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
512 static int lface_fully_specified_p P_ ((Lisp_Object *));
513 static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
514 static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
515 static unsigned lface_hash P_ ((Lisp_Object *));
516 static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
517 static struct face_cache *make_face_cache P_ ((struct frame *));
518 static void clear_face_gcs P_ ((struct face_cache *));
519 static void free_face_cache P_ ((struct face_cache *));
520 static int face_fontset P_ ((Lisp_Object *));
521 static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*,
522 struct named_merge_point *));
523 static int merge_face_ref P_ ((struct frame *, Lisp_Object, Lisp_Object *,
524 int, struct named_merge_point *));
525 static int set_lface_from_font P_ ((struct frame *, Lisp_Object, Lisp_Object,
526 int));
527 static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
528 static struct face *make_realized_face P_ ((Lisp_Object *));
529 static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
530 static void uncache_face P_ ((struct face_cache *, struct face *));
532 #ifdef HAVE_WINDOW_SYSTEM
534 static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
535 static void x_free_gc P_ ((struct frame *, GC));
537 #ifdef WINDOWSNT
538 extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
539 #endif /* WINDOWSNT */
541 #ifdef USE_X_TOOLKIT
542 static void x_update_menu_appearance P_ ((struct frame *));
544 extern void free_frame_menubar P_ ((struct frame *));
545 #endif /* USE_X_TOOLKIT */
547 #endif /* HAVE_WINDOW_SYSTEM */
550 /***********************************************************************
551 Utilities
552 ***********************************************************************/
554 #ifdef HAVE_X_WINDOWS
556 #ifdef DEBUG_X_COLORS
558 /* The following is a poor mans infrastructure for debugging X color
559 allocation problems on displays with PseudoColor-8. Some X servers
560 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
561 color reference counts completely so that they don't signal an
562 error when a color is freed whose reference count is already 0.
563 Other X servers do. To help me debug this, the following code
564 implements a simple reference counting schema of its own, for a
565 single display/screen. --gerd. */
567 /* Reference counts for pixel colors. */
569 int color_count[256];
571 /* Register color PIXEL as allocated. */
573 void
574 register_color (pixel)
575 unsigned long pixel;
577 xassert (pixel < 256);
578 ++color_count[pixel];
582 /* Register color PIXEL as deallocated. */
584 void
585 unregister_color (pixel)
586 unsigned long pixel;
588 xassert (pixel < 256);
589 if (color_count[pixel] > 0)
590 --color_count[pixel];
591 else
592 abort ();
596 /* Register N colors from PIXELS as deallocated. */
598 void
599 unregister_colors (pixels, n)
600 unsigned long *pixels;
601 int n;
603 int i;
604 for (i = 0; i < n; ++i)
605 unregister_color (pixels[i]);
609 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
610 doc: /* Dump currently allocated colors to stderr. */)
613 int i, n;
615 fputc ('\n', stderr);
617 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
618 if (color_count[i])
620 fprintf (stderr, "%3d: %5d", i, color_count[i]);
621 ++n;
622 if (n % 5 == 0)
623 fputc ('\n', stderr);
624 else
625 fputc ('\t', stderr);
628 if (n % 5 != 0)
629 fputc ('\n', stderr);
630 return Qnil;
633 #endif /* DEBUG_X_COLORS */
636 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
637 color values. Interrupt input must be blocked when this function
638 is called. */
640 void
641 x_free_colors (f, pixels, npixels)
642 struct frame *f;
643 unsigned long *pixels;
644 int npixels;
646 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
648 /* If display has an immutable color map, freeing colors is not
649 necessary and some servers don't allow it. So don't do it. */
650 if (class != StaticColor && class != StaticGray && class != TrueColor)
652 #ifdef DEBUG_X_COLORS
653 unregister_colors (pixels, npixels);
654 #endif
655 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
656 pixels, npixels, 0);
661 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
662 color values. Interrupt input must be blocked when this function
663 is called. */
665 void
666 x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
667 Display *dpy;
668 Screen *screen;
669 Colormap cmap;
670 unsigned long *pixels;
671 int npixels;
673 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
674 int class = dpyinfo->visual->class;
676 /* If display has an immutable color map, freeing colors is not
677 necessary and some servers don't allow it. So don't do it. */
678 if (class != StaticColor && class != StaticGray && class != TrueColor)
680 #ifdef DEBUG_X_COLORS
681 unregister_colors (pixels, npixels);
682 #endif
683 XFreeColors (dpy, cmap, pixels, npixels, 0);
688 /* Create and return a GC for use on frame F. GC values and mask
689 are given by XGCV and MASK. */
691 static INLINE GC
692 x_create_gc (f, mask, xgcv)
693 struct frame *f;
694 unsigned long mask;
695 XGCValues *xgcv;
697 GC gc;
698 BLOCK_INPUT;
699 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
700 UNBLOCK_INPUT;
701 IF_DEBUG (++ngcs);
702 return gc;
706 /* Free GC which was used on frame F. */
708 static INLINE void
709 x_free_gc (f, gc)
710 struct frame *f;
711 GC gc;
713 eassert (interrupt_input_blocked);
714 IF_DEBUG (xassert (--ngcs >= 0));
715 XFreeGC (FRAME_X_DISPLAY (f), gc);
718 #endif /* HAVE_X_WINDOWS */
720 #ifdef WINDOWSNT
721 /* W32 emulation of GCs */
723 static INLINE GC
724 x_create_gc (f, mask, xgcv)
725 struct frame *f;
726 unsigned long mask;
727 XGCValues *xgcv;
729 GC gc;
730 BLOCK_INPUT;
731 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
732 UNBLOCK_INPUT;
733 IF_DEBUG (++ngcs);
734 return gc;
738 /* Free GC which was used on frame F. */
740 static INLINE void
741 x_free_gc (f, gc)
742 struct frame *f;
743 GC gc;
745 IF_DEBUG (xassert (--ngcs >= 0));
746 xfree (gc);
749 #endif /* WINDOWSNT */
751 #ifdef MAC_OS
752 /* Mac OS emulation of GCs */
754 static INLINE GC
755 x_create_gc (f, mask, xgcv)
756 struct frame *f;
757 unsigned long mask;
758 XGCValues *xgcv;
760 GC gc;
761 BLOCK_INPUT;
762 gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
763 UNBLOCK_INPUT;
764 IF_DEBUG (++ngcs);
765 return gc;
768 static INLINE void
769 x_free_gc (f, gc)
770 struct frame *f;
771 GC gc;
773 eassert (interrupt_input_blocked);
774 IF_DEBUG (xassert (--ngcs >= 0));
775 XFreeGC (FRAME_MAC_DISPLAY (f), gc);
778 #endif /* MAC_OS */
780 /* Like strcasecmp/stricmp. Used to compare parts of font names which
781 are in ISO8859-1. */
784 xstrcasecmp (s1, s2)
785 const unsigned char *s1, *s2;
787 while (*s1 && *s2)
789 unsigned char c1 = tolower (*s1);
790 unsigned char c2 = tolower (*s2);
791 if (c1 != c2)
792 return c1 < c2 ? -1 : 1;
793 ++s1, ++s2;
796 if (*s1 == 0)
797 return *s2 == 0 ? 0 : -1;
798 return 1;
802 /* If FRAME is nil, return a pointer to the selected frame.
803 Otherwise, check that FRAME is a live frame, and return a pointer
804 to it. NPARAM is the parameter number of FRAME, for
805 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
806 Lisp function definitions. */
808 static INLINE struct frame *
809 frame_or_selected_frame (frame, nparam)
810 Lisp_Object frame;
811 int nparam;
813 if (NILP (frame))
814 frame = selected_frame;
816 CHECK_LIVE_FRAME (frame);
817 return XFRAME (frame);
821 /***********************************************************************
822 Frames and faces
823 ***********************************************************************/
825 /* Initialize face cache and basic faces for frame F. */
827 void
828 init_frame_faces (f)
829 struct frame *f;
831 /* Make a face cache, if F doesn't have one. */
832 if (FRAME_FACE_CACHE (f) == NULL)
833 FRAME_FACE_CACHE (f) = make_face_cache (f);
835 #ifdef HAVE_WINDOW_SYSTEM
836 /* Make the image cache. */
837 if (FRAME_WINDOW_P (f))
839 if (FRAME_IMAGE_CACHE (f) == NULL)
840 /* Is that ever possible?? --Stef */
841 FRAME_IMAGE_CACHE (f) = make_image_cache ();
842 ++FRAME_IMAGE_CACHE (f)->refcount;
844 #endif /* HAVE_WINDOW_SYSTEM */
846 /* Realize basic faces. Must have enough information in frame
847 parameters to realize basic faces at this point. */
848 #ifdef HAVE_X_WINDOWS
849 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
850 #endif
851 #ifdef WINDOWSNT
852 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
853 #endif
854 #ifdef MAC_OS
855 if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
856 #endif
857 if (!realize_basic_faces (f))
858 abort ();
862 /* Free face cache of frame F. Called from Fdelete_frame. */
864 void
865 free_frame_faces (f)
866 struct frame *f;
868 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
870 if (face_cache)
872 free_face_cache (face_cache);
873 FRAME_FACE_CACHE (f) = NULL;
876 #ifdef HAVE_WINDOW_SYSTEM
877 if (FRAME_WINDOW_P (f))
879 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
880 if (image_cache)
882 --image_cache->refcount;
883 if (image_cache->refcount == 0)
884 free_image_cache (f);
887 #endif /* HAVE_WINDOW_SYSTEM */
891 /* Clear face caches, and recompute basic faces for frame F. Call
892 this after changing frame parameters on which those faces depend,
893 or when realized faces have been freed due to changing attributes
894 of named faces. */
896 void
897 recompute_basic_faces (f)
898 struct frame *f;
900 if (FRAME_FACE_CACHE (f))
902 clear_face_cache (0);
903 if (!realize_basic_faces (f))
904 abort ();
909 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
910 try to free unused fonts, too. */
912 void
913 clear_face_cache (clear_fonts_p)
914 int clear_fonts_p;
916 #ifdef HAVE_WINDOW_SYSTEM
917 Lisp_Object tail, frame;
918 struct frame *f;
920 if (clear_fonts_p
921 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
923 #if 0
924 /* Not yet implemented. */
925 clear_font_cache (frame);
926 #endif
928 /* From time to time see if we can unload some fonts. This also
929 frees all realized faces on all frames. Fonts needed by
930 faces will be loaded again when faces are realized again. */
931 clear_font_table_count = 0;
933 FOR_EACH_FRAME (tail, frame)
935 struct frame *f = XFRAME (frame);
936 if (FRAME_WINDOW_P (f)
937 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
938 free_all_realized_faces (frame);
941 else
943 /* Clear GCs of realized faces. */
944 FOR_EACH_FRAME (tail, frame)
946 f = XFRAME (frame);
947 if (FRAME_WINDOW_P (f))
948 clear_face_gcs (FRAME_FACE_CACHE (f));
950 clear_image_caches (Qnil);
952 #endif /* HAVE_WINDOW_SYSTEM */
956 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
957 doc: /* Clear face caches on all frames.
958 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
959 (thoroughly)
960 Lisp_Object thoroughly;
962 clear_face_cache (!NILP (thoroughly));
963 ++face_change_count;
964 ++windows_or_buffers_changed;
965 return Qnil;
969 /***********************************************************************
970 X Pixmaps
971 ***********************************************************************/
973 #ifdef HAVE_WINDOW_SYSTEM
975 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
976 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
977 A bitmap specification is either a string, a file name, or a list
978 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
979 HEIGHT is its height, and DATA is a string containing the bits of
980 the pixmap. Bits are stored row by row, each row occupies
981 \(WIDTH + 7)/8 bytes. */)
982 (object)
983 Lisp_Object object;
985 int pixmap_p = 0;
987 if (STRINGP (object))
988 /* If OBJECT is a string, it's a file name. */
989 pixmap_p = 1;
990 else if (CONSP (object))
992 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
993 HEIGHT must be integers > 0, and DATA must be string large
994 enough to hold a bitmap of the specified size. */
995 Lisp_Object width, height, data;
997 height = width = data = Qnil;
999 if (CONSP (object))
1001 width = XCAR (object);
1002 object = XCDR (object);
1003 if (CONSP (object))
1005 height = XCAR (object);
1006 object = XCDR (object);
1007 if (CONSP (object))
1008 data = XCAR (object);
1012 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1014 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1015 / BITS_PER_CHAR);
1016 if (SBYTES (data) >= bytes_per_row * XINT (height))
1017 pixmap_p = 1;
1021 return pixmap_p ? Qt : Qnil;
1025 /* Load a bitmap according to NAME (which is either a file name or a
1026 pixmap spec) for use on frame F. Value is the bitmap_id (see
1027 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1028 bitmap cannot be loaded, display a message saying so, and return
1029 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1030 if these pointers are not null. */
1032 static int
1033 load_pixmap (f, name, w_ptr, h_ptr)
1034 FRAME_PTR f;
1035 Lisp_Object name;
1036 unsigned int *w_ptr, *h_ptr;
1038 int bitmap_id;
1040 if (NILP (name))
1041 return 0;
1043 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
1045 BLOCK_INPUT;
1046 if (CONSP (name))
1048 /* Decode a bitmap spec into a bitmap. */
1050 int h, w;
1051 Lisp_Object bits;
1053 w = XINT (Fcar (name));
1054 h = XINT (Fcar (Fcdr (name)));
1055 bits = Fcar (Fcdr (Fcdr (name)));
1057 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1058 w, h);
1060 else
1062 /* It must be a string -- a file name. */
1063 bitmap_id = x_create_bitmap_from_file (f, name);
1065 UNBLOCK_INPUT;
1067 if (bitmap_id < 0)
1069 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
1070 bitmap_id = 0;
1072 if (w_ptr)
1073 *w_ptr = 0;
1074 if (h_ptr)
1075 *h_ptr = 0;
1077 else
1079 #if GLYPH_DEBUG
1080 ++npixmaps_allocated;
1081 #endif
1082 if (w_ptr)
1083 *w_ptr = x_bitmap_width (f, bitmap_id);
1085 if (h_ptr)
1086 *h_ptr = x_bitmap_height (f, bitmap_id);
1089 return bitmap_id;
1092 #endif /* HAVE_WINDOW_SYSTEM */
1096 /***********************************************************************
1097 X Colors
1098 ***********************************************************************/
1100 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1101 RGB_LIST should contain (at least) 3 lisp integers.
1102 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1104 static int
1105 parse_rgb_list (rgb_list, color)
1106 Lisp_Object rgb_list;
1107 XColor *color;
1109 #define PARSE_RGB_LIST_FIELD(field) \
1110 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1112 color->field = XINT (XCAR (rgb_list)); \
1113 rgb_list = XCDR (rgb_list); \
1115 else \
1116 return 0;
1118 PARSE_RGB_LIST_FIELD (red);
1119 PARSE_RGB_LIST_FIELD (green);
1120 PARSE_RGB_LIST_FIELD (blue);
1122 return 1;
1126 /* Lookup on frame F the color described by the lisp string COLOR.
1127 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1128 non-zero, then the `standard' definition of the same color is
1129 returned in it. */
1131 static int
1132 tty_lookup_color (f, color, tty_color, std_color)
1133 struct frame *f;
1134 Lisp_Object color;
1135 XColor *tty_color, *std_color;
1137 Lisp_Object frame, color_desc;
1139 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1140 return 0;
1142 XSETFRAME (frame, f);
1144 color_desc = call2 (Qtty_color_desc, color, frame);
1145 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1147 Lisp_Object rgb;
1149 if (! INTEGERP (XCAR (XCDR (color_desc))))
1150 return 0;
1152 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1154 rgb = XCDR (XCDR (color_desc));
1155 if (! parse_rgb_list (rgb, tty_color))
1156 return 0;
1158 /* Should we fill in STD_COLOR too? */
1159 if (std_color)
1161 /* Default STD_COLOR to the same as TTY_COLOR. */
1162 *std_color = *tty_color;
1164 /* Do a quick check to see if the returned descriptor is
1165 actually _exactly_ equal to COLOR, otherwise we have to
1166 lookup STD_COLOR separately. If it's impossible to lookup
1167 a standard color, we just give up and use TTY_COLOR. */
1168 if ((!STRINGP (XCAR (color_desc))
1169 || NILP (Fstring_equal (color, XCAR (color_desc))))
1170 && !NILP (Ffboundp (Qtty_color_standard_values)))
1172 /* Look up STD_COLOR separately. */
1173 rgb = call1 (Qtty_color_standard_values, color);
1174 if (! parse_rgb_list (rgb, std_color))
1175 return 0;
1179 return 1;
1181 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1182 /* We were called early during startup, and the colors are not
1183 yet set up in tty-defined-color-alist. Don't return a failure
1184 indication, since this produces the annoying "Unable to
1185 load color" messages in the *Messages* buffer. */
1186 return 1;
1187 else
1188 /* tty-color-desc seems to have returned a bad value. */
1189 return 0;
1192 /* A version of defined_color for non-X frames. */
1195 tty_defined_color (f, color_name, color_def, alloc)
1196 struct frame *f;
1197 char *color_name;
1198 XColor *color_def;
1199 int alloc;
1201 int status = 1;
1203 /* Defaults. */
1204 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1205 color_def->red = 0;
1206 color_def->blue = 0;
1207 color_def->green = 0;
1209 if (*color_name)
1210 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1212 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1214 if (strcmp (color_name, "unspecified-fg") == 0)
1215 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1216 else if (strcmp (color_name, "unspecified-bg") == 0)
1217 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1220 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1221 status = 1;
1223 return status;
1227 /* Decide if color named COLOR_NAME is valid for the display
1228 associated with the frame F; if so, return the rgb values in
1229 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1231 This does the right thing for any type of frame. */
1234 defined_color (f, color_name, color_def, alloc)
1235 struct frame *f;
1236 char *color_name;
1237 XColor *color_def;
1238 int alloc;
1240 if (!FRAME_WINDOW_P (f))
1241 return tty_defined_color (f, color_name, color_def, alloc);
1242 #ifdef HAVE_X_WINDOWS
1243 else if (FRAME_X_P (f))
1244 return x_defined_color (f, color_name, color_def, alloc);
1245 #endif
1246 #ifdef WINDOWSNT
1247 else if (FRAME_W32_P (f))
1248 return w32_defined_color (f, color_name, color_def, alloc);
1249 #endif
1250 #ifdef MAC_OS
1251 else if (FRAME_MAC_P (f))
1252 return mac_defined_color (f, color_name, color_def, alloc);
1253 #endif
1254 else
1255 abort ();
1259 /* Given the index IDX of a tty color on frame F, return its name, a
1260 Lisp string. */
1262 Lisp_Object
1263 tty_color_name (f, idx)
1264 struct frame *f;
1265 int idx;
1267 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1269 Lisp_Object frame;
1270 Lisp_Object coldesc;
1272 XSETFRAME (frame, f);
1273 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1275 if (!NILP (coldesc))
1276 return XCAR (coldesc);
1278 #ifdef MSDOS
1279 /* We can have an MSDOG frame under -nw for a short window of
1280 opportunity before internal_terminal_init is called. DTRT. */
1281 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1282 return msdos_stdcolor_name (idx);
1283 #endif
1285 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1286 return build_string (unspecified_fg);
1287 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1288 return build_string (unspecified_bg);
1290 return Qunspecified;
1294 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1295 black) on frame F.
1297 The criterion implemented here is not a terribly sophisticated one. */
1299 static int
1300 face_color_gray_p (f, color_name)
1301 struct frame *f;
1302 char *color_name;
1304 XColor color;
1305 int gray_p;
1307 if (defined_color (f, color_name, &color, 0))
1308 gray_p = (/* Any color sufficiently close to black counts as grey. */
1309 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1311 ((eabs (color.red - color.green)
1312 < max (color.red, color.green) / 20)
1313 && (eabs (color.green - color.blue)
1314 < max (color.green, color.blue) / 20)
1315 && (eabs (color.blue - color.red)
1316 < max (color.blue, color.red) / 20)));
1317 else
1318 gray_p = 0;
1320 return gray_p;
1324 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1325 BACKGROUND_P non-zero means the color will be used as background
1326 color. */
1328 static int
1329 face_color_supported_p (f, color_name, background_p)
1330 struct frame *f;
1331 char *color_name;
1332 int background_p;
1334 Lisp_Object frame;
1335 XColor not_used;
1337 XSETFRAME (frame, f);
1338 return
1339 #ifdef HAVE_WINDOW_SYSTEM
1340 FRAME_WINDOW_P (f)
1341 ? (!NILP (Fxw_display_color_p (frame))
1342 || xstrcasecmp (color_name, "black") == 0
1343 || xstrcasecmp (color_name, "white") == 0
1344 || (background_p
1345 && face_color_gray_p (f, color_name))
1346 || (!NILP (Fx_display_grayscale_p (frame))
1347 && face_color_gray_p (f, color_name)))
1349 #endif
1350 tty_defined_color (f, color_name, &not_used, 0);
1354 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1355 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1356 FRAME specifies the frame and thus the display for interpreting COLOR.
1357 If FRAME is nil or omitted, use the selected frame. */)
1358 (color, frame)
1359 Lisp_Object color, frame;
1361 struct frame *f;
1363 CHECK_STRING (color);
1364 if (NILP (frame))
1365 frame = selected_frame;
1366 else
1367 CHECK_FRAME (frame);
1368 f = XFRAME (frame);
1369 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
1373 DEFUN ("color-supported-p", Fcolor_supported_p,
1374 Scolor_supported_p, 1, 3, 0,
1375 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1376 BACKGROUND-P non-nil means COLOR is used as a background.
1377 Otherwise, this function tells whether it can be used as a foreground.
1378 If FRAME is nil or omitted, use the selected frame.
1379 COLOR must be a valid color name. */)
1380 (color, frame, background_p)
1381 Lisp_Object frame, color, background_p;
1383 struct frame *f;
1385 CHECK_STRING (color);
1386 if (NILP (frame))
1387 frame = selected_frame;
1388 else
1389 CHECK_FRAME (frame);
1390 f = XFRAME (frame);
1391 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
1392 return Qt;
1393 return Qnil;
1397 /* Load color with name NAME for use by face FACE on frame F.
1398 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1399 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1400 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1401 pixel color. If color cannot be loaded, display a message, and
1402 return the foreground, background or underline color of F, but
1403 record that fact in flags of the face so that we don't try to free
1404 these colors. */
1406 unsigned long
1407 load_color (f, face, name, target_index)
1408 struct frame *f;
1409 struct face *face;
1410 Lisp_Object name;
1411 enum lface_attribute_index target_index;
1413 XColor color;
1415 xassert (STRINGP (name));
1416 xassert (target_index == LFACE_FOREGROUND_INDEX
1417 || target_index == LFACE_BACKGROUND_INDEX
1418 || target_index == LFACE_UNDERLINE_INDEX
1419 || target_index == LFACE_OVERLINE_INDEX
1420 || target_index == LFACE_STRIKE_THROUGH_INDEX
1421 || target_index == LFACE_BOX_INDEX);
1423 /* if the color map is full, defined_color will return a best match
1424 to the values in an existing cell. */
1425 if (!defined_color (f, SDATA (name), &color, 1))
1427 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1429 switch (target_index)
1431 case LFACE_FOREGROUND_INDEX:
1432 face->foreground_defaulted_p = 1;
1433 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1434 break;
1436 case LFACE_BACKGROUND_INDEX:
1437 face->background_defaulted_p = 1;
1438 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1439 break;
1441 case LFACE_UNDERLINE_INDEX:
1442 face->underline_defaulted_p = 1;
1443 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1444 break;
1446 case LFACE_OVERLINE_INDEX:
1447 face->overline_color_defaulted_p = 1;
1448 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1449 break;
1451 case LFACE_STRIKE_THROUGH_INDEX:
1452 face->strike_through_color_defaulted_p = 1;
1453 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1454 break;
1456 case LFACE_BOX_INDEX:
1457 face->box_color_defaulted_p = 1;
1458 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1459 break;
1461 default:
1462 abort ();
1465 #if GLYPH_DEBUG
1466 else
1467 ++ncolors_allocated;
1468 #endif
1470 return color.pixel;
1474 #ifdef HAVE_WINDOW_SYSTEM
1476 /* Load colors for face FACE which is used on frame F. Colors are
1477 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1478 of ATTRS. If the background color specified is not supported on F,
1479 try to emulate gray colors with a stipple from Vface_default_stipple. */
1481 static void
1482 load_face_colors (f, face, attrs)
1483 struct frame *f;
1484 struct face *face;
1485 Lisp_Object *attrs;
1487 Lisp_Object fg, bg;
1489 bg = attrs[LFACE_BACKGROUND_INDEX];
1490 fg = attrs[LFACE_FOREGROUND_INDEX];
1492 /* Swap colors if face is inverse-video. */
1493 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1495 Lisp_Object tmp;
1496 tmp = fg;
1497 fg = bg;
1498 bg = tmp;
1501 /* Check for support for foreground, not for background because
1502 face_color_supported_p is smart enough to know that grays are
1503 "supported" as background because we are supposed to use stipple
1504 for them. */
1505 if (!face_color_supported_p (f, SDATA (bg), 0)
1506 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1508 x_destroy_bitmap (f, face->stipple);
1509 face->stipple = load_pixmap (f, Vface_default_stipple,
1510 &face->pixmap_w, &face->pixmap_h);
1513 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1514 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1518 /* Free color PIXEL on frame F. */
1520 void
1521 unload_color (f, pixel)
1522 struct frame *f;
1523 unsigned long pixel;
1525 #ifdef HAVE_X_WINDOWS
1526 if (pixel != -1)
1528 BLOCK_INPUT;
1529 x_free_colors (f, &pixel, 1);
1530 UNBLOCK_INPUT;
1532 #endif
1536 /* Free colors allocated for FACE. */
1538 static void
1539 free_face_colors (f, face)
1540 struct frame *f;
1541 struct face *face;
1543 #ifdef HAVE_X_WINDOWS
1544 if (face->colors_copied_bitwise_p)
1545 return;
1547 BLOCK_INPUT;
1549 if (!face->foreground_defaulted_p)
1551 x_free_colors (f, &face->foreground, 1);
1552 IF_DEBUG (--ncolors_allocated);
1555 if (!face->background_defaulted_p)
1557 x_free_colors (f, &face->background, 1);
1558 IF_DEBUG (--ncolors_allocated);
1561 if (face->underline_p
1562 && !face->underline_defaulted_p)
1564 x_free_colors (f, &face->underline_color, 1);
1565 IF_DEBUG (--ncolors_allocated);
1568 if (face->overline_p
1569 && !face->overline_color_defaulted_p)
1571 x_free_colors (f, &face->overline_color, 1);
1572 IF_DEBUG (--ncolors_allocated);
1575 if (face->strike_through_p
1576 && !face->strike_through_color_defaulted_p)
1578 x_free_colors (f, &face->strike_through_color, 1);
1579 IF_DEBUG (--ncolors_allocated);
1582 if (face->box != FACE_NO_BOX
1583 && !face->box_color_defaulted_p)
1585 x_free_colors (f, &face->box_color, 1);
1586 IF_DEBUG (--ncolors_allocated);
1589 UNBLOCK_INPUT;
1590 #endif /* HAVE_X_WINDOWS */
1593 #endif /* HAVE_WINDOW_SYSTEM */
1597 /***********************************************************************
1598 XLFD Font Names
1599 ***********************************************************************/
1601 /* An enumerator for each field of an XLFD font name. */
1603 enum xlfd_field
1605 XLFD_FOUNDRY,
1606 XLFD_FAMILY,
1607 XLFD_WEIGHT,
1608 XLFD_SLANT,
1609 XLFD_SWIDTH,
1610 XLFD_ADSTYLE,
1611 XLFD_PIXEL_SIZE,
1612 XLFD_POINT_SIZE,
1613 XLFD_RESX,
1614 XLFD_RESY,
1615 XLFD_SPACING,
1616 XLFD_AVGWIDTH,
1617 XLFD_REGISTRY,
1618 XLFD_ENCODING,
1619 XLFD_LAST
1622 /* An enumerator for each possible slant value of a font. Taken from
1623 the XLFD specification. */
1625 enum xlfd_slant
1627 XLFD_SLANT_UNKNOWN,
1628 XLFD_SLANT_ROMAN,
1629 XLFD_SLANT_ITALIC,
1630 XLFD_SLANT_OBLIQUE,
1631 XLFD_SLANT_REVERSE_ITALIC,
1632 XLFD_SLANT_REVERSE_OBLIQUE,
1633 XLFD_SLANT_OTHER
1636 /* Relative font weight according to XLFD documentation. */
1638 enum xlfd_weight
1640 XLFD_WEIGHT_UNKNOWN,
1641 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1642 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1643 XLFD_WEIGHT_LIGHT, /* 30 */
1644 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1645 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1646 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1647 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1648 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1649 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1652 /* Relative proportionate width. */
1654 enum xlfd_swidth
1656 XLFD_SWIDTH_UNKNOWN,
1657 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1658 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1659 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1660 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1661 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1662 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1663 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1664 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1665 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1668 /* Order by which font selection chooses fonts. The default values
1669 mean `first, find a best match for the font width, then for the
1670 font height, then for weight, then for slant.' This variable can be
1671 set via set-face-font-sort-order. */
1673 #ifdef MAC_OS
1674 static int font_sort_order[4] = {
1675 XLFD_SWIDTH, XLFD_POINT_SIZE, XLFD_WEIGHT, XLFD_SLANT
1677 #else
1678 static int font_sort_order[4];
1679 #endif
1682 #ifdef HAVE_WINDOW_SYSTEM
1684 /* Return a rescaling ratio of a font of NAME. */
1686 static double
1687 font_rescale_ratio (name)
1688 char *name;
1690 Lisp_Object tail, elt;
1692 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
1694 elt = XCAR (tail);
1695 if (STRINGP (XCAR (elt)) && FLOATP (XCDR (elt))
1696 && fast_c_string_match_ignore_case (XCAR (elt), name) >= 0)
1697 return XFLOAT_DATA (XCDR (elt));
1699 return 1.0;
1702 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1704 static int
1705 compare_fonts_by_sort_order (v1, v2)
1706 const void *v1, *v2;
1708 Lisp_Object font1 = *(Lisp_Object *) v1;
1709 Lisp_Object font2 = *(Lisp_Object *) v2;
1710 int i;
1712 for (i = 0; i < FONT_SIZE_INDEX; i++)
1714 enum font_property_index idx = font_props_for_sorting[i];
1715 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1716 int result;
1718 if (idx <= FONT_REGISTRY_INDEX)
1720 if (STRINGP (val1))
1721 result = STRINGP (val2) ? strcmp (SDATA (val1), SDATA (val2)) : -1;
1722 else
1723 result = STRINGP (val2) ? 1 : 0;
1725 else
1727 if (INTEGERP (val1))
1728 result = INTEGERP (val2) ? XINT (val1) - XINT (val2) : -1;
1729 else
1730 result = INTEGERP (val2) ? 1 : 0;
1732 if (result)
1733 return result;
1735 return 0;
1738 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1739 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1740 If FAMILY is omitted or nil, list all families.
1741 Otherwise, FAMILY must be a string, possibly containing wildcards
1742 `?' and `*'.
1743 If FRAME is omitted or nil, use the selected frame.
1744 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1745 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1746 FAMILY is the font family name. POINT-SIZE is the size of the
1747 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1748 width, weight and slant of the font. These symbols are the same as for
1749 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1750 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1751 giving the registry and encoding of the font.
1752 The result list is sorted according to the current setting of
1753 the face font sort order. */)
1754 (family, frame)
1755 Lisp_Object family, frame;
1757 struct frame *f = check_x_frame (frame);
1758 Lisp_Object font_spec = Qnil, vec;
1759 int i, nfonts;
1760 Lisp_Object result;
1762 if (!NILP (family))
1764 CHECK_STRING (family);
1765 font_spec = Ffont_spec (0, NULL);
1766 Ffont_put (font_spec, QCfamily, family);
1768 vec = font_list_entities (frame, font_spec);
1769 nfonts = ASIZE (vec);
1770 if (nfonts == 0)
1771 return Qnil;
1772 if (nfonts > 1)
1774 for (i = 0; i < 4; i++)
1775 switch (font_sort_order[i])
1777 case XLFD_SWIDTH:
1778 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1779 case XLFD_POINT_SIZE:
1780 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1781 case XLFD_WEIGHT:
1782 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1783 default:
1784 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1786 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1787 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1788 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1789 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1791 qsort (XVECTOR (vec)->contents, nfonts, sizeof (Lisp_Object),
1792 compare_fonts_by_sort_order);
1795 result = Qnil;
1796 for (i = nfonts - 1; i >= 0; --i)
1798 Lisp_Object font = AREF (vec, i);
1799 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
1800 int point;
1801 Lisp_Object spacing;
1803 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1804 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1805 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1806 f->resy);
1807 ASET (v, 2, make_number (point));
1808 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1809 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1810 spacing = Ffont_get (font, QCspacing);
1811 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1812 ASET (v, 6, AREF (font, FONT_NAME_INDEX));
1813 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1815 result = Fcons (v, result);
1818 return result;
1822 DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
1823 0, 1, 0,
1824 doc: /* Return a list of available font families on FRAME.
1825 If FRAME is omitted or nil, use the selected frame.
1826 Value is a list of conses (FAMILY . FIXED-P) where FAMILY
1827 is a font family, and FIXED-P is non-nil if fonts of that family
1828 are fixed-pitch. */)
1829 (frame)
1830 Lisp_Object frame;
1832 return Ffont_family_list (frame);
1836 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1837 doc: /* Return a list of the names of available fonts matching PATTERN.
1838 If optional arguments FACE and FRAME are specified, return only fonts
1839 the same size as FACE on FRAME.
1840 PATTERN is a string, perhaps with wildcard characters;
1841 the * character matches any substring, and
1842 the ? character matches any single character.
1843 PATTERN is case-insensitive.
1844 FACE is a face name--a symbol.
1846 The return value is a list of strings, suitable as arguments to
1847 set-face-font.
1849 Fonts Emacs can't use may or may not be excluded
1850 even if they match PATTERN and FACE.
1851 The optional fourth argument MAXIMUM sets a limit on how many
1852 fonts to match. The first MAXIMUM fonts are reported.
1853 The optional fifth argument WIDTH, if specified, is a number of columns
1854 occupied by a character of a font. In that case, return only fonts
1855 the WIDTH times as wide as FACE on FRAME. */)
1856 (pattern, face, frame, maximum, width)
1857 Lisp_Object pattern, face, frame, maximum, width;
1859 struct frame *f;
1860 int size, avgwidth;
1862 check_x ();
1863 CHECK_STRING (pattern);
1865 if (! NILP (maximum))
1866 CHECK_NATNUM (maximum);
1868 if (!NILP (width))
1869 CHECK_NUMBER (width);
1871 /* We can't simply call check_x_frame because this function may be
1872 called before any frame is created. */
1873 if (NILP (frame))
1874 frame = selected_frame;
1875 f = frame_or_selected_frame (frame, 2);
1876 if (! FRAME_WINDOW_P (f))
1878 /* Perhaps we have not yet created any frame. */
1879 f = NULL;
1880 frame = Qnil;
1881 face = Qnil;
1884 /* Determine the width standard for comparison with the fonts we find. */
1886 if (NILP (face))
1887 size = 0;
1888 else
1890 /* This is of limited utility since it works with character
1891 widths. Keep it for compatibility. --gerd. */
1892 int face_id = lookup_named_face (f, face, 0);
1893 struct face *face = (face_id < 0
1894 ? NULL
1895 : FACE_FROM_ID (f, face_id));
1897 if (face && face->font)
1899 size = face->font->pixel_size;
1900 avgwidth = face->font->average_width;
1902 else
1904 size = FRAME_FONT (f)->pixel_size;
1905 avgwidth = FRAME_FONT (f)->average_width;
1907 if (!NILP (width))
1908 avgwidth *= XINT (width);
1912 Lisp_Object font_spec;
1913 Lisp_Object args[2];
1915 font_spec = font_spec_from_name (pattern);
1916 if (size)
1918 Ffont_put (font_spec, QCsize, make_number (size));
1919 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1921 args[0] = Flist_fonts (font_spec, frame, maximum, Qnil);
1922 if (NILP (frame))
1923 /* We don't have to check fontsets. */
1924 return args[0];
1925 args[1] = list_fontsets (f, pattern, size);
1926 return Fnconc (2, args);
1930 #endif /* HAVE_WINDOW_SYSTEM */
1934 /***********************************************************************
1935 Lisp Faces
1936 ***********************************************************************/
1938 /* Access face attributes of face LFACE, a Lisp vector. */
1940 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1941 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1942 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1943 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1944 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1945 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1946 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1947 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1948 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1949 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1950 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1951 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1952 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1953 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1954 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1955 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1957 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1958 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1960 #define LFACEP(LFACE) \
1961 (VECTORP (LFACE) \
1962 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
1963 && EQ (AREF (LFACE, 0), Qface))
1966 #if GLYPH_DEBUG
1968 /* Check consistency of Lisp face attribute vector ATTRS. */
1970 static void
1971 check_lface_attrs (attrs)
1972 Lisp_Object *attrs;
1974 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1975 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1976 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1977 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1978 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1979 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1980 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1981 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1982 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1983 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1984 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1985 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1986 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1987 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1988 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1989 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1990 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1991 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1992 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1993 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1994 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
1995 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1996 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1997 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1998 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1999 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2000 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
2001 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2002 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
2003 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
2004 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
2005 || SYMBOLP (attrs[LFACE_BOX_INDEX])
2006 || STRINGP (attrs[LFACE_BOX_INDEX])
2007 || INTEGERP (attrs[LFACE_BOX_INDEX])
2008 || CONSP (attrs[LFACE_BOX_INDEX]));
2009 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
2010 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
2011 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
2012 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
2013 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
2014 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
2015 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
2016 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
2017 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
2018 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
2019 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
2020 || NILP (attrs[LFACE_INHERIT_INDEX])
2021 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
2022 || CONSP (attrs[LFACE_INHERIT_INDEX]));
2023 #ifdef HAVE_WINDOW_SYSTEM
2024 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
2025 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
2026 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
2027 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
2028 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
2029 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
2030 || FONTP (attrs[LFACE_FONT_INDEX]));
2031 xassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
2032 || STRINGP (attrs[LFACE_FONTSET_INDEX]));
2033 #endif
2037 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
2039 static void
2040 check_lface (lface)
2041 Lisp_Object lface;
2043 if (!NILP (lface))
2045 xassert (LFACEP (lface));
2046 check_lface_attrs (XVECTOR (lface)->contents);
2050 #else /* GLYPH_DEBUG == 0 */
2052 #define check_lface_attrs(attrs) (void) 0
2053 #define check_lface(lface) (void) 0
2055 #endif /* GLYPH_DEBUG == 0 */
2059 /* Face-merge cycle checking. */
2061 /* A `named merge point' is simply a point during face-merging where we
2062 look up a face by name. We keep a stack of which named lookups we're
2063 currently processing so that we can easily detect cycles, using a
2064 linked- list of struct named_merge_point structures, typically
2065 allocated on the stack frame of the named lookup functions which are
2066 active (so no consing is required). */
2067 struct named_merge_point
2069 Lisp_Object face_name;
2070 struct named_merge_point *prev;
2074 /* If a face merging cycle is detected for FACE_NAME, return 0,
2075 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
2076 FACE_NAME, as the head of the linked list pointed to by
2077 NAMED_MERGE_POINTS, and return 1. */
2079 static INLINE int
2080 push_named_merge_point (struct named_merge_point *new_named_merge_point,
2081 Lisp_Object face_name,
2082 struct named_merge_point **named_merge_points)
2084 struct named_merge_point *prev;
2086 for (prev = *named_merge_points; prev; prev = prev->prev)
2087 if (EQ (face_name, prev->face_name))
2088 return 0;
2090 new_named_merge_point->face_name = face_name;
2091 new_named_merge_point->prev = *named_merge_points;
2093 *named_merge_points = new_named_merge_point;
2095 return 1;
2100 #if 0 /* Seems to be unused. */
2101 static Lisp_Object
2102 internal_resolve_face_name (nargs, args)
2103 int nargs;
2104 Lisp_Object *args;
2106 return Fget (args[0], args[1]);
2109 static Lisp_Object
2110 resolve_face_name_error (ignore)
2111 Lisp_Object ignore;
2113 return Qnil;
2115 #endif
2117 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
2118 to make it a symbol. If FACE_NAME is an alias for another face,
2119 return that face's name.
2121 Return default face in case of errors. */
2123 static Lisp_Object
2124 resolve_face_name (face_name, signal_p)
2125 Lisp_Object face_name;
2126 int signal_p;
2128 Lisp_Object orig_face;
2129 Lisp_Object tortoise, hare;
2131 if (STRINGP (face_name))
2132 face_name = intern (SDATA (face_name));
2134 if (NILP (face_name) || !SYMBOLP (face_name))
2135 return face_name;
2137 orig_face = face_name;
2138 tortoise = hare = face_name;
2140 while (1)
2142 face_name = hare;
2143 hare = Fget (hare, Qface_alias);
2144 if (NILP (hare) || !SYMBOLP (hare))
2145 break;
2147 face_name = hare;
2148 hare = Fget (hare, Qface_alias);
2149 if (NILP (hare) || !SYMBOLP (hare))
2150 break;
2152 tortoise = Fget (tortoise, Qface_alias);
2153 if (EQ (hare, tortoise))
2155 if (signal_p)
2156 xsignal1 (Qcircular_list, orig_face);
2157 return Qdefault;
2161 return face_name;
2165 /* Return the face definition of FACE_NAME on frame F. F null means
2166 return the definition for new frames. FACE_NAME may be a string or
2167 a symbol (apparently Emacs 20.2 allowed strings as face names in
2168 face text properties; Ediff uses that). If FACE_NAME is an alias
2169 for another face, return that face's definition. If SIGNAL_P is
2170 non-zero, signal an error if FACE_NAME is not a valid face name.
2171 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
2172 name. */
2174 static INLINE Lisp_Object
2175 lface_from_face_name (f, face_name, signal_p)
2176 struct frame *f;
2177 Lisp_Object face_name;
2178 int signal_p;
2180 Lisp_Object lface;
2182 face_name = resolve_face_name (face_name, signal_p);
2184 if (f)
2185 lface = assq_no_quit (face_name, f->face_alist);
2186 else
2187 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2189 if (CONSP (lface))
2190 lface = XCDR (lface);
2191 else if (signal_p)
2192 signal_error ("Invalid face", face_name);
2194 check_lface (lface);
2195 return lface;
2199 /* Get face attributes of face FACE_NAME from frame-local faces on
2200 frame F. Store the resulting attributes in ATTRS which must point
2201 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2202 is non-zero, signal an error if FACE_NAME does not name a face.
2203 Otherwise, value is zero if FACE_NAME is not a face. */
2205 static INLINE int
2206 get_lface_attributes (f, face_name, attrs, signal_p)
2207 struct frame *f;
2208 Lisp_Object face_name;
2209 Lisp_Object *attrs;
2210 int signal_p;
2212 Lisp_Object lface;
2213 int success_p;
2215 lface = lface_from_face_name (f, face_name, signal_p);
2216 if (!NILP (lface))
2218 bcopy (XVECTOR (lface)->contents, attrs,
2219 LFACE_VECTOR_SIZE * sizeof *attrs);
2220 success_p = 1;
2222 else
2223 success_p = 0;
2225 return success_p;
2229 /* Non-zero if all attributes in face attribute vector ATTRS are
2230 specified, i.e. are non-nil. */
2232 static int
2233 lface_fully_specified_p (attrs)
2234 Lisp_Object *attrs;
2236 int i;
2238 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2239 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2240 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i]))
2241 #ifdef MAC_OS
2242 /* MAC_TODO: No stipple support on Mac OS yet, this index is
2243 always unspecified. */
2244 && i != LFACE_STIPPLE_INDEX
2245 #endif
2247 break;
2249 return i == LFACE_VECTOR_SIZE;
2252 #ifdef HAVE_WINDOW_SYSTEM
2254 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2255 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2256 exception is `font' attribute. It is set to FONT_OBJECT regardless
2257 of FORCE_P. */
2259 static int
2260 set_lface_from_font (f, lface, font_object, force_p)
2261 struct frame *f;
2262 Lisp_Object lface, font_object;
2263 int force_p;
2265 Lisp_Object val;
2266 struct font *font = XFONT_OBJECT (font_object);
2268 /* Set attributes only if unspecified, otherwise face defaults for
2269 new frames would never take effect. If the font doesn't have a
2270 specific property, set a normal value for that. */
2272 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2274 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2275 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2277 if (! NILP (foundry))
2279 if (! NILP (family))
2280 val = concat3 (SYMBOL_NAME (foundry), build_string ("-"),
2281 SYMBOL_NAME (family));
2282 else
2283 val = concat2 (SYMBOL_NAME (foundry), build_string ("-*"));
2285 else
2287 if (! NILP (family))
2288 val = SYMBOL_NAME (family);
2289 else
2290 val = build_string ("*");
2292 LFACE_FAMILY (lface) = val;
2295 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2297 int pt = PIXEL_TO_POINT (font->pixel_size * 10, f->resy);
2299 xassert (pt > 0);
2300 LFACE_HEIGHT (lface) = make_number (pt);
2303 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2305 val = FONT_WEIGHT_FOR_FACE (font_object);
2306 LFACE_WEIGHT (lface) = ! NILP (val) ? val :Qnormal;
2308 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2310 val = FONT_SLANT_FOR_FACE (font_object);
2311 LFACE_SLANT (lface) = ! NILP (val) ? val : Qnormal;
2313 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2315 val = FONT_WIDTH_FOR_FACE (font_object);
2316 LFACE_SWIDTH (lface) = ! NILP (val) ? val : Qnormal;
2319 LFACE_FONT (lface) = font_object;
2320 return 1;
2323 #endif /* HAVE_WINDOW_SYSTEM */
2326 /* Merges the face height FROM with the face height TO, and returns the
2327 merged height. If FROM is an invalid height, then INVALID is
2328 returned instead. FROM and TO may be either absolute face heights or
2329 `relative' heights; the returned value is always an absolute height
2330 unless both FROM and TO are relative. GCPRO is a lisp value that
2331 will be protected from garbage-collection if this function makes a
2332 call into lisp. */
2334 Lisp_Object
2335 merge_face_heights (from, to, invalid)
2336 Lisp_Object from, to, invalid;
2338 Lisp_Object result = invalid;
2340 if (INTEGERP (from))
2341 /* FROM is absolute, just use it as is. */
2342 result = from;
2343 else if (FLOATP (from))
2344 /* FROM is a scale, use it to adjust TO. */
2346 if (INTEGERP (to))
2347 /* relative X absolute => absolute */
2348 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
2349 else if (FLOATP (to))
2350 /* relative X relative => relative */
2351 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2352 else if (UNSPECIFIEDP (to))
2353 result = from;
2355 else if (FUNCTIONP (from))
2356 /* FROM is a function, which use to adjust TO. */
2358 /* Call function with current height as argument.
2359 From is the new height. */
2360 Lisp_Object args[2];
2362 args[0] = from;
2363 args[1] = to;
2364 result = safe_call (2, args);
2366 /* Ensure that if TO was absolute, so is the result. */
2367 if (INTEGERP (to) && !INTEGERP (result))
2368 result = invalid;
2371 return result;
2375 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2376 store the resulting attributes in TO, which must be already be
2377 completely specified and contain only absolute attributes. Every
2378 specified attribute of FROM overrides the corresponding attribute of
2379 TO; relative attributes in FROM are merged with the absolute value in
2380 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2381 loops in face inheritance; it should be 0 when called from other
2382 places. */
2384 static INLINE void
2385 merge_face_vectors (f, from, to, named_merge_points)
2386 struct frame *f;
2387 Lisp_Object *from, *to;
2388 struct named_merge_point *named_merge_points;
2390 int i;
2392 /* If FROM inherits from some other faces, merge their attributes into
2393 TO before merging FROM's direct attributes. Note that an :inherit
2394 attribute of `unspecified' is the same as one of nil; we never
2395 merge :inherit attributes, so nil is more correct, but lots of
2396 other code uses `unspecified' as a generic value for face attributes. */
2397 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2398 && !NILP (from[LFACE_INHERIT_INDEX]))
2399 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2401 i = LFACE_FONT_INDEX;
2402 if (!UNSPECIFIEDP (from[i]))
2404 if (!UNSPECIFIEDP (to[i]))
2405 to[i] = Fmerge_font_spec (from[i], to[i]);
2406 else
2407 to[i] = Fcopy_font_spec (from[i]);
2408 ASET (to[i], FONT_SIZE_INDEX, Qnil);
2411 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2412 if (!UNSPECIFIEDP (from[i]))
2414 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2416 to[i] = merge_face_heights (from[i], to[i], to[i]);
2417 font_clear_prop (to, FONT_SIZE_INDEX);
2419 else if (i != LFACE_FONT_INDEX)
2421 to[i] = from[i];
2422 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2423 font_clear_prop (to,
2424 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2425 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2426 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2427 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2428 : FONT_SLANT_INDEX));
2432 /* If `font' attribute is specified, reflect the font properties in
2433 it to the other attributes. */
2434 if (0 && !UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2435 font_update_lface (f, to);
2437 /* TO is always an absolute face, which should inherit from nothing.
2438 We blindly copy the :inherit attribute above and fix it up here. */
2439 to[LFACE_INHERIT_INDEX] = Qnil;
2442 /* Merge the named face FACE_NAME on frame F, into the vector of face
2443 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2444 inheritance. Returns true if FACE_NAME is a valid face name and
2445 merging succeeded. */
2447 static int
2448 merge_named_face (f, face_name, to, named_merge_points)
2449 struct frame *f;
2450 Lisp_Object face_name;
2451 Lisp_Object *to;
2452 struct named_merge_point *named_merge_points;
2454 struct named_merge_point named_merge_point;
2456 if (push_named_merge_point (&named_merge_point,
2457 face_name, &named_merge_points))
2459 struct gcpro gcpro1;
2460 Lisp_Object from[LFACE_VECTOR_SIZE];
2461 int ok = get_lface_attributes (f, face_name, from, 0);
2463 if (ok)
2465 GCPRO1 (named_merge_point.face_name);
2466 merge_face_vectors (f, from, to, named_merge_points);
2467 UNGCPRO;
2470 return ok;
2472 else
2473 return 0;
2477 /* Merge face attributes from the lisp `face reference' FACE_REF on
2478 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2479 problems with FACE_REF cause an error message to be shown. Return
2480 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2481 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2482 list structure; it may be 0 for most callers.
2484 FACE_REF may be a single face specification or a list of such
2485 specifications. Each face specification can be:
2487 1. A symbol or string naming a Lisp face.
2489 2. A property list of the form (KEYWORD VALUE ...) where each
2490 KEYWORD is a face attribute name, and value is an appropriate value
2491 for that attribute.
2493 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2494 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2495 for compatibility with 20.2.
2497 Face specifications earlier in lists take precedence over later
2498 specifications. */
2500 static int
2501 merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
2502 struct frame *f;
2503 Lisp_Object face_ref;
2504 Lisp_Object *to;
2505 int err_msgs;
2506 struct named_merge_point *named_merge_points;
2508 int ok = 1; /* Succeed without an error? */
2510 if (CONSP (face_ref))
2512 Lisp_Object first = XCAR (face_ref);
2514 if (EQ (first, Qforeground_color)
2515 || EQ (first, Qbackground_color))
2517 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2518 . COLOR). COLOR must be a string. */
2519 Lisp_Object color_name = XCDR (face_ref);
2520 Lisp_Object color = first;
2522 if (STRINGP (color_name))
2524 if (EQ (color, Qforeground_color))
2525 to[LFACE_FOREGROUND_INDEX] = color_name;
2526 else
2527 to[LFACE_BACKGROUND_INDEX] = color_name;
2529 else
2531 if (err_msgs)
2532 add_to_log ("Invalid face color", color_name, Qnil);
2533 ok = 0;
2536 else if (SYMBOLP (first)
2537 && *SDATA (SYMBOL_NAME (first)) == ':')
2539 /* Assume this is the property list form. */
2540 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2542 Lisp_Object keyword = XCAR (face_ref);
2543 Lisp_Object value = XCAR (XCDR (face_ref));
2544 int err = 0;
2546 /* Specifying `unspecified' is a no-op. */
2547 if (EQ (value, Qunspecified))
2549 else if (EQ (keyword, QCfamily))
2551 if (STRINGP (value))
2553 to[LFACE_FAMILY_INDEX] = value;
2554 font_clear_prop (to, FONT_FAMILY_INDEX);
2556 else
2557 err = 1;
2559 else if (EQ (keyword, QCheight))
2561 Lisp_Object new_height =
2562 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2564 if (! NILP (new_height))
2566 to[LFACE_HEIGHT_INDEX] = new_height;
2567 font_clear_prop (to, FONT_SIZE_INDEX);
2569 else
2570 err = 1;
2572 else if (EQ (keyword, QCweight))
2574 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2576 to[LFACE_WEIGHT_INDEX] = value;
2577 font_clear_prop (to, FONT_WEIGHT_INDEX);
2579 else
2580 err = 1;
2582 else if (EQ (keyword, QCslant))
2584 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2586 to[LFACE_SLANT_INDEX] = value;
2587 font_clear_prop (to, FONT_SLANT_INDEX);
2589 else
2590 err = 1;
2592 else if (EQ (keyword, QCunderline))
2594 if (EQ (value, Qt)
2595 || NILP (value)
2596 || STRINGP (value))
2597 to[LFACE_UNDERLINE_INDEX] = value;
2598 else
2599 err = 1;
2601 else if (EQ (keyword, QCoverline))
2603 if (EQ (value, Qt)
2604 || NILP (value)
2605 || STRINGP (value))
2606 to[LFACE_OVERLINE_INDEX] = value;
2607 else
2608 err = 1;
2610 else if (EQ (keyword, QCstrike_through))
2612 if (EQ (value, Qt)
2613 || NILP (value)
2614 || STRINGP (value))
2615 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2616 else
2617 err = 1;
2619 else if (EQ (keyword, QCbox))
2621 if (EQ (value, Qt))
2622 value = make_number (1);
2623 if (INTEGERP (value)
2624 || STRINGP (value)
2625 || CONSP (value)
2626 || NILP (value))
2627 to[LFACE_BOX_INDEX] = value;
2628 else
2629 err = 1;
2631 else if (EQ (keyword, QCinverse_video)
2632 || EQ (keyword, QCreverse_video))
2634 if (EQ (value, Qt) || NILP (value))
2635 to[LFACE_INVERSE_INDEX] = value;
2636 else
2637 err = 1;
2639 else if (EQ (keyword, QCforeground))
2641 if (STRINGP (value))
2642 to[LFACE_FOREGROUND_INDEX] = value;
2643 else
2644 err = 1;
2646 else if (EQ (keyword, QCbackground))
2648 if (STRINGP (value))
2649 to[LFACE_BACKGROUND_INDEX] = value;
2650 else
2651 err = 1;
2653 else if (EQ (keyword, QCstipple))
2655 #ifdef HAVE_X_WINDOWS
2656 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2657 if (!NILP (pixmap_p))
2658 to[LFACE_STIPPLE_INDEX] = value;
2659 else
2660 err = 1;
2661 #endif
2663 else if (EQ (keyword, QCwidth))
2665 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2667 to[LFACE_SWIDTH_INDEX] = value;
2668 font_clear_prop (to, FONT_WIDTH_INDEX);
2670 else
2671 err = 1;
2673 else if (EQ (keyword, QCinherit))
2675 /* This is not really very useful; it's just like a
2676 normal face reference. */
2677 if (! merge_face_ref (f, value, to,
2678 err_msgs, named_merge_points))
2679 err = 1;
2681 else
2682 err = 1;
2684 if (err)
2686 add_to_log ("Invalid face attribute %S %S", keyword, value);
2687 ok = 0;
2690 face_ref = XCDR (XCDR (face_ref));
2693 else
2695 /* This is a list of face refs. Those at the beginning of the
2696 list take precedence over what follows, so we have to merge
2697 from the end backwards. */
2698 Lisp_Object next = XCDR (face_ref);
2700 if (! NILP (next))
2701 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2703 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2704 ok = 0;
2707 else
2709 /* FACE_REF ought to be a face name. */
2710 ok = merge_named_face (f, face_ref, to, named_merge_points);
2711 if (!ok && err_msgs)
2712 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2715 return ok;
2719 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2720 Sinternal_make_lisp_face, 1, 2, 0,
2721 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2722 If FACE was not known as a face before, create a new one.
2723 If optional argument FRAME is specified, make a frame-local face
2724 for that frame. Otherwise operate on the global face definition.
2725 Value is a vector of face attributes. */)
2726 (face, frame)
2727 Lisp_Object face, frame;
2729 Lisp_Object global_lface, lface;
2730 struct frame *f;
2731 int i;
2733 CHECK_SYMBOL (face);
2734 global_lface = lface_from_face_name (NULL, face, 0);
2736 if (!NILP (frame))
2738 CHECK_LIVE_FRAME (frame);
2739 f = XFRAME (frame);
2740 lface = lface_from_face_name (f, face, 0);
2742 else
2743 f = NULL, lface = Qnil;
2745 /* Add a global definition if there is none. */
2746 if (NILP (global_lface))
2748 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2749 Qunspecified);
2750 ASET (global_lface, 0, Qface);
2751 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2752 Vface_new_frame_defaults);
2754 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2755 face id to Lisp face is given by the vector lface_id_to_name.
2756 The mapping from Lisp face to Lisp face id is given by the
2757 property `face' of the Lisp face name. */
2758 if (next_lface_id == lface_id_to_name_size)
2760 int new_size = max (50, 2 * lface_id_to_name_size);
2761 int sz = new_size * sizeof *lface_id_to_name;
2762 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
2763 lface_id_to_name_size = new_size;
2766 lface_id_to_name[next_lface_id] = face;
2767 Fput (face, Qface, make_number (next_lface_id));
2768 ++next_lface_id;
2770 else if (f == NULL)
2771 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2772 ASET (global_lface, i, Qunspecified);
2774 /* Add a frame-local definition. */
2775 if (f)
2777 if (NILP (lface))
2779 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2780 Qunspecified);
2781 ASET (lface, 0, Qface);
2782 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
2784 else
2785 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2786 ASET (lface, i, Qunspecified);
2788 else
2789 lface = global_lface;
2791 /* Changing a named face means that all realized faces depending on
2792 that face are invalid. Since we cannot tell which realized faces
2793 depend on the face, make sure they are all removed. This is done
2794 by incrementing face_change_count. The next call to
2795 init_iterator will then free realized faces. */
2796 if (NILP (Fget (face, Qface_no_inherit)))
2798 ++face_change_count;
2799 ++windows_or_buffers_changed;
2802 xassert (LFACEP (lface));
2803 check_lface (lface);
2804 return lface;
2808 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2809 Sinternal_lisp_face_p, 1, 2, 0,
2810 doc: /* Return non-nil if FACE names a face.
2811 If optional second argument FRAME is non-nil, check for the
2812 existence of a frame-local face with name FACE on that frame.
2813 Otherwise check for the existence of a global face. */)
2814 (face, frame)
2815 Lisp_Object face, frame;
2817 Lisp_Object lface;
2819 face = resolve_face_name (face, 1);
2821 if (!NILP (frame))
2823 CHECK_LIVE_FRAME (frame);
2824 lface = lface_from_face_name (XFRAME (frame), face, 0);
2826 else
2827 lface = lface_from_face_name (NULL, face, 0);
2829 return lface;
2833 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2834 Sinternal_copy_lisp_face, 4, 4, 0,
2835 doc: /* Copy face FROM to TO.
2836 If FRAME is t, copy the global face definition of FROM.
2837 Otherwise, copy the frame-local definition of FROM on FRAME.
2838 If NEW-FRAME is a frame, copy that data into the frame-local
2839 definition of TO on NEW-FRAME. If NEW-FRAME is nil.
2840 FRAME controls where the data is copied to.
2842 The value is TO. */)
2843 (from, to, frame, new_frame)
2844 Lisp_Object from, to, frame, new_frame;
2846 Lisp_Object lface, copy;
2848 CHECK_SYMBOL (from);
2849 CHECK_SYMBOL (to);
2851 if (EQ (frame, Qt))
2853 /* Copy global definition of FROM. We don't make copies of
2854 strings etc. because 20.2 didn't do it either. */
2855 lface = lface_from_face_name (NULL, from, 1);
2856 copy = Finternal_make_lisp_face (to, Qnil);
2858 else
2860 /* Copy frame-local definition of FROM. */
2861 if (NILP (new_frame))
2862 new_frame = frame;
2863 CHECK_LIVE_FRAME (frame);
2864 CHECK_LIVE_FRAME (new_frame);
2865 lface = lface_from_face_name (XFRAME (frame), from, 1);
2866 copy = Finternal_make_lisp_face (to, new_frame);
2869 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
2870 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
2872 /* Changing a named face means that all realized faces depending on
2873 that face are invalid. Since we cannot tell which realized faces
2874 depend on the face, make sure they are all removed. This is done
2875 by incrementing face_change_count. The next call to
2876 init_iterator will then free realized faces. */
2877 if (NILP (Fget (to, Qface_no_inherit)))
2879 ++face_change_count;
2880 ++windows_or_buffers_changed;
2883 return to;
2887 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2888 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2889 doc: /* Set attribute ATTR of FACE to VALUE.
2890 FRAME being a frame means change the face on that frame.
2891 FRAME nil means change the face of the selected frame.
2892 FRAME t means change the default for new frames.
2893 FRAME 0 means change the face on all frames, and change the default
2894 for new frames. */)
2895 (face, attr, value, frame)
2896 Lisp_Object face, attr, value, frame;
2898 Lisp_Object lface;
2899 Lisp_Object old_value = Qnil;
2900 /* Set one of enum font_property_index (> 0) if ATTR is one of
2901 font-related attributes other than QCfont and QCfontset. */
2902 enum font_property_index prop_index = 0;
2904 CHECK_SYMBOL (face);
2905 CHECK_SYMBOL (attr);
2907 face = resolve_face_name (face, 1);
2909 /* If FRAME is 0, change face on all frames, and change the
2910 default for new frames. */
2911 if (INTEGERP (frame) && XINT (frame) == 0)
2913 Lisp_Object tail;
2914 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2915 FOR_EACH_FRAME (tail, frame)
2916 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2917 return face;
2920 /* Set lface to the Lisp attribute vector of FACE. */
2921 if (EQ (frame, Qt))
2923 lface = lface_from_face_name (NULL, face, 1);
2925 /* When updating face-new-frame-defaults, we put :ignore-defface
2926 where the caller wants `unspecified'. This forces the frame
2927 defaults to ignore the defface value. Otherwise, the defface
2928 will take effect, which is generally not what is intended.
2929 The value of that attribute will be inherited from some other
2930 face during face merging. See internal_merge_in_global_face. */
2931 if (UNSPECIFIEDP (value))
2932 value = Qignore_defface;
2934 else
2936 if (NILP (frame))
2937 frame = selected_frame;
2939 CHECK_LIVE_FRAME (frame);
2940 lface = lface_from_face_name (XFRAME (frame), face, 0);
2942 /* If a frame-local face doesn't exist yet, create one. */
2943 if (NILP (lface))
2944 lface = Finternal_make_lisp_face (face, frame);
2947 if (EQ (attr, QCfamily))
2949 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2951 CHECK_STRING (value);
2952 if (SCHARS (value) == 0)
2953 signal_error ("Invalid face family", value);
2955 old_value = LFACE_FAMILY (lface);
2956 LFACE_FAMILY (lface) = value;
2957 prop_index = FONT_FAMILY_INDEX;
2959 else if (EQ (attr, QCheight))
2961 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2963 Lisp_Object test;
2965 test = (EQ (face, Qdefault)
2966 ? value
2967 /* The default face must have an absolute size,
2968 otherwise, we do a test merge with a random
2969 height to see if VALUE's ok. */
2970 : merge_face_heights (value, make_number (10), Qnil));
2972 if (!INTEGERP (test) || XINT (test) <= 0)
2973 signal_error ("Invalid face height", value);
2976 old_value = LFACE_HEIGHT (lface);
2977 LFACE_HEIGHT (lface) = value;
2978 prop_index = FONT_SIZE_INDEX;
2980 else if (EQ (attr, QCweight))
2982 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2984 CHECK_SYMBOL (value);
2985 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2986 signal_error ("Invalid face weight", value);
2988 old_value = LFACE_WEIGHT (lface);
2989 LFACE_WEIGHT (lface) = value;
2990 prop_index = FONT_WEIGHT_INDEX;
2992 else if (EQ (attr, QCslant))
2994 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2996 CHECK_SYMBOL (value);
2997 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2998 signal_error ("Invalid face slant", value);
3000 old_value = LFACE_SLANT (lface);
3001 LFACE_SLANT (lface) = value;
3002 prop_index = FONT_SLANT_INDEX;
3004 else if (EQ (attr, QCunderline))
3006 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3007 if ((SYMBOLP (value)
3008 && !EQ (value, Qt)
3009 && !EQ (value, Qnil))
3010 /* Underline color. */
3011 || (STRINGP (value)
3012 && SCHARS (value) == 0))
3013 signal_error ("Invalid face underline", value);
3015 old_value = LFACE_UNDERLINE (lface);
3016 LFACE_UNDERLINE (lface) = value;
3018 else if (EQ (attr, QCoverline))
3020 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3021 if ((SYMBOLP (value)
3022 && !EQ (value, Qt)
3023 && !EQ (value, Qnil))
3024 /* Overline color. */
3025 || (STRINGP (value)
3026 && SCHARS (value) == 0))
3027 signal_error ("Invalid face overline", value);
3029 old_value = LFACE_OVERLINE (lface);
3030 LFACE_OVERLINE (lface) = value;
3032 else if (EQ (attr, QCstrike_through))
3034 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3035 if ((SYMBOLP (value)
3036 && !EQ (value, Qt)
3037 && !EQ (value, Qnil))
3038 /* Strike-through color. */
3039 || (STRINGP (value)
3040 && SCHARS (value) == 0))
3041 signal_error ("Invalid face strike-through", value);
3043 old_value = LFACE_STRIKE_THROUGH (lface);
3044 LFACE_STRIKE_THROUGH (lface) = value;
3046 else if (EQ (attr, QCbox))
3048 int valid_p;
3050 /* Allow t meaning a simple box of width 1 in foreground color
3051 of the face. */
3052 if (EQ (value, Qt))
3053 value = make_number (1);
3055 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
3056 valid_p = 1;
3057 else if (NILP (value))
3058 valid_p = 1;
3059 else if (INTEGERP (value))
3060 valid_p = XINT (value) != 0;
3061 else if (STRINGP (value))
3062 valid_p = SCHARS (value) > 0;
3063 else if (CONSP (value))
3065 Lisp_Object tem;
3067 tem = value;
3068 while (CONSP (tem))
3070 Lisp_Object k, v;
3072 k = XCAR (tem);
3073 tem = XCDR (tem);
3074 if (!CONSP (tem))
3075 break;
3076 v = XCAR (tem);
3077 tem = XCDR (tem);
3079 if (EQ (k, QCline_width))
3081 if (!INTEGERP (v) || XINT (v) == 0)
3082 break;
3084 else if (EQ (k, QCcolor))
3086 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
3087 break;
3089 else if (EQ (k, QCstyle))
3091 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3092 break;
3094 else
3095 break;
3098 valid_p = NILP (tem);
3100 else
3101 valid_p = 0;
3103 if (!valid_p)
3104 signal_error ("Invalid face box", value);
3106 old_value = LFACE_BOX (lface);
3107 LFACE_BOX (lface) = value;
3109 else if (EQ (attr, QCinverse_video)
3110 || EQ (attr, QCreverse_video))
3112 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3114 CHECK_SYMBOL (value);
3115 if (!EQ (value, Qt) && !NILP (value))
3116 signal_error ("Invalid inverse-video face attribute value", value);
3118 old_value = LFACE_INVERSE (lface);
3119 LFACE_INVERSE (lface) = value;
3121 else if (EQ (attr, QCforeground))
3123 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3125 /* Don't check for valid color names here because it depends
3126 on the frame (display) whether the color will be valid
3127 when the face is realized. */
3128 CHECK_STRING (value);
3129 if (SCHARS (value) == 0)
3130 signal_error ("Empty foreground color value", value);
3132 old_value = LFACE_FOREGROUND (lface);
3133 LFACE_FOREGROUND (lface) = value;
3135 else if (EQ (attr, QCbackground))
3137 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3139 /* Don't check for valid color names here because it depends
3140 on the frame (display) whether the color will be valid
3141 when the face is realized. */
3142 CHECK_STRING (value);
3143 if (SCHARS (value) == 0)
3144 signal_error ("Empty background color value", value);
3146 old_value = LFACE_BACKGROUND (lface);
3147 LFACE_BACKGROUND (lface) = value;
3149 else if (EQ (attr, QCstipple))
3151 #ifdef HAVE_X_WINDOWS
3152 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3153 && !NILP (value)
3154 && NILP (Fbitmap_spec_p (value)))
3155 signal_error ("Invalid stipple attribute", value);
3156 old_value = LFACE_STIPPLE (lface);
3157 LFACE_STIPPLE (lface) = value;
3158 #endif /* HAVE_X_WINDOWS */
3160 else if (EQ (attr, QCwidth))
3162 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3164 CHECK_SYMBOL (value);
3165 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3166 signal_error ("Invalid face width", value);
3168 old_value = LFACE_SWIDTH (lface);
3169 LFACE_SWIDTH (lface) = value;
3170 prop_index = FONT_WIDTH_INDEX;
3172 else if (EQ (attr, QCfont))
3174 #ifdef HAVE_WINDOW_SYSTEM
3175 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3177 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3179 FRAME_PTR f;
3181 old_value = LFACE_FONT (lface);
3182 if (! FONTP (value))
3184 if (STRINGP (value))
3186 int fontset = fs_query_fontset (value, 0);
3188 if (fontset >= 0)
3189 value = fontset_ascii (fontset);
3190 value = font_spec_from_name (value);
3192 else
3193 signal_error ("Invalid font or font-spec", value);
3195 if (EQ (frame, Qt))
3196 f = XFRAME (selected_frame);
3197 else
3198 f = XFRAME (frame);
3199 if (! FONT_OBJECT_P (value))
3201 Lisp_Object *attrs = XVECTOR (lface)->contents;
3202 Lisp_Object font_object;
3204 font_object = font_load_for_lface (f, attrs, value);
3205 if (NILP (font_object))
3206 signal_error ("Font not available", value);
3207 value = font_object;
3209 set_lface_from_font (f, lface, value, 1);
3211 else
3212 LFACE_FONT (lface) = value;
3214 #endif /* HAVE_WINDOW_SYSTEM */
3216 else if (EQ (attr, QCfontset))
3218 #ifdef HAVE_WINDOW_SYSTEM
3219 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3221 Lisp_Object tmp;
3223 old_value = LFACE_FONTSET (lface);
3224 tmp = Fquery_fontset (value, Qnil);
3225 if (NILP (tmp))
3226 signal_error ("Invalid fontset name", value);
3227 LFACE_FONTSET (lface) = value = tmp;
3229 #endif /* HAVE_WINDOW_SYSTEM */
3231 else if (EQ (attr, QCinherit))
3233 Lisp_Object tail;
3234 if (SYMBOLP (value))
3235 tail = Qnil;
3236 else
3237 for (tail = value; CONSP (tail); tail = XCDR (tail))
3238 if (!SYMBOLP (XCAR (tail)))
3239 break;
3240 if (NILP (tail))
3241 LFACE_INHERIT (lface) = value;
3242 else
3243 signal_error ("Invalid face inheritance", value);
3245 else if (EQ (attr, QCbold))
3247 old_value = LFACE_WEIGHT (lface);
3248 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
3249 prop_index = FONT_WEIGHT_INDEX;
3251 else if (EQ (attr, QCitalic))
3253 attr = QCslant;
3254 old_value = LFACE_SLANT (lface);
3255 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
3256 prop_index = FONT_SLANT_INDEX;
3258 else
3259 signal_error ("Invalid face attribute name", attr);
3261 if (prop_index)
3262 /* If a font-related attribute other than QCfont and QCfontset is
3263 specified, and if the original QCfont attribute has a font
3264 (font-spec or font-object), set the corresponding property in
3265 the font to nil so that the font selector doesn't think that
3266 the attribute is mandatory. */
3267 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3269 /* Changing a named face means that all realized faces depending on
3270 that face are invalid. Since we cannot tell which realized faces
3271 depend on the face, make sure they are all removed. This is done
3272 by incrementing face_change_count. The next call to
3273 init_iterator will then free realized faces. */
3274 if (!EQ (frame, Qt)
3275 && NILP (Fget (face, Qface_no_inherit))
3276 && NILP (Fequal (old_value, value)))
3278 ++face_change_count;
3279 ++windows_or_buffers_changed;
3282 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3283 && NILP (Fequal (old_value, value)))
3285 Lisp_Object param;
3287 param = Qnil;
3289 if (EQ (face, Qdefault))
3291 #ifdef HAVE_WINDOW_SYSTEM
3292 /* Changed font-related attributes of the `default' face are
3293 reflected in changed `font' frame parameters. */
3294 if (FRAMEP (frame)
3295 && (prop_index || EQ (attr, QCfont))
3296 && lface_fully_specified_p (XVECTOR (lface)->contents))
3297 set_font_frame_param (frame, lface);
3298 else
3299 #endif /* HAVE_WINDOW_SYSTEM */
3301 if (EQ (attr, QCforeground))
3302 param = Qforeground_color;
3303 else if (EQ (attr, QCbackground))
3304 param = Qbackground_color;
3306 #ifdef HAVE_WINDOW_SYSTEM
3307 #ifndef WINDOWSNT
3308 else if (EQ (face, Qscroll_bar))
3310 /* Changing the colors of `scroll-bar' sets frame parameters
3311 `scroll-bar-foreground' and `scroll-bar-background'. */
3312 if (EQ (attr, QCforeground))
3313 param = Qscroll_bar_foreground;
3314 else if (EQ (attr, QCbackground))
3315 param = Qscroll_bar_background;
3317 #endif /* not WINDOWSNT */
3318 else if (EQ (face, Qborder))
3320 /* Changing background color of `border' sets frame parameter
3321 `border-color'. */
3322 if (EQ (attr, QCbackground))
3323 param = Qborder_color;
3325 else if (EQ (face, Qcursor))
3327 /* Changing background color of `cursor' sets frame parameter
3328 `cursor-color'. */
3329 if (EQ (attr, QCbackground))
3330 param = Qcursor_color;
3332 else if (EQ (face, Qmouse))
3334 /* Changing background color of `mouse' sets frame parameter
3335 `mouse-color'. */
3336 if (EQ (attr, QCbackground))
3337 param = Qmouse_color;
3339 #endif /* HAVE_WINDOW_SYSTEM */
3340 else if (EQ (face, Qmenu))
3342 /* Indicate that we have to update the menu bar when
3343 realizing faces on FRAME. FRAME t change the
3344 default for new frames. We do this by setting
3345 setting the flag in new face caches */
3346 if (FRAMEP (frame))
3348 struct frame *f = XFRAME (frame);
3349 if (FRAME_FACE_CACHE (f) == NULL)
3350 FRAME_FACE_CACHE (f) = make_face_cache (f);
3351 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3353 else
3354 menu_face_changed_default = 1;
3357 if (!NILP (param))
3359 if (EQ (frame, Qt))
3360 /* Update `default-frame-alist', which is used for new frames. */
3362 store_in_alist (&Vdefault_frame_alist, param, value);
3364 else
3365 /* Update the current frame's parameters. */
3367 Lisp_Object cons;
3368 cons = XCAR (Vparam_value_alist);
3369 XSETCAR (cons, param);
3370 XSETCDR (cons, value);
3371 Fmodify_frame_parameters (frame, Vparam_value_alist);
3376 return face;
3380 #ifdef HAVE_WINDOW_SYSTEM
3382 /* Set the `font' frame parameter of FRAME determined from the
3383 font-object set in `default' face attributes LFACE. */
3385 static void
3386 set_font_frame_param (frame, lface)
3387 Lisp_Object frame, lface;
3389 struct frame *f = XFRAME (frame);
3391 if (FRAME_WINDOW_P (f))
3393 Lisp_Object font = LFACE_FONT (lface);
3395 if (FONT_SPEC_P (font))
3397 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3398 if (NILP (font))
3399 return;
3400 LFACE_FONT (lface) = font;
3402 f->default_face_done_p = 0;
3403 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font), Qnil));
3408 /* Update the corresponding face when frame parameter PARAM on frame F
3409 has been assigned the value NEW_VALUE. */
3411 void
3412 update_face_from_frame_parameter (f, param, new_value)
3413 struct frame *f;
3414 Lisp_Object param, new_value;
3416 Lisp_Object face = Qnil;
3417 Lisp_Object lface;
3419 /* If there are no faces yet, give up. This is the case when called
3420 from Fx_create_frame, and we do the necessary things later in
3421 face-set-after-frame-defaults. */
3422 if (NILP (f->face_alist))
3423 return;
3425 if (EQ (param, Qforeground_color))
3427 face = Qdefault;
3428 lface = lface_from_face_name (f, face, 1);
3429 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
3430 ? new_value : Qunspecified);
3431 realize_basic_faces (f);
3433 else if (EQ (param, Qbackground_color))
3435 Lisp_Object frame;
3437 /* Changing the background color might change the background
3438 mode, so that we have to load new defface specs.
3439 Call frame-set-background-mode to do that. */
3440 XSETFRAME (frame, f);
3441 call1 (Qframe_set_background_mode, frame);
3443 face = Qdefault;
3444 lface = lface_from_face_name (f, face, 1);
3445 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3446 ? new_value : Qunspecified);
3447 realize_basic_faces (f);
3449 else if (EQ (param, Qborder_color))
3451 face = Qborder;
3452 lface = lface_from_face_name (f, face, 1);
3453 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3454 ? new_value : Qunspecified);
3456 else if (EQ (param, Qcursor_color))
3458 face = Qcursor;
3459 lface = lface_from_face_name (f, face, 1);
3460 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3461 ? new_value : Qunspecified);
3463 else if (EQ (param, Qmouse_color))
3465 face = Qmouse;
3466 lface = lface_from_face_name (f, face, 1);
3467 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3468 ? new_value : Qunspecified);
3471 /* Changing a named face means that all realized faces depending on
3472 that face are invalid. Since we cannot tell which realized faces
3473 depend on the face, make sure they are all removed. This is done
3474 by incrementing face_change_count. The next call to
3475 init_iterator will then free realized faces. */
3476 if (!NILP (face)
3477 && NILP (Fget (face, Qface_no_inherit)))
3479 ++face_change_count;
3480 ++windows_or_buffers_changed;
3485 /* Get the value of X resource RESOURCE, class CLASS for the display
3486 of frame FRAME. This is here because ordinary `x-get-resource'
3487 doesn't take a frame argument. */
3489 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3490 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
3491 (resource, class, frame)
3492 Lisp_Object resource, class, frame;
3494 Lisp_Object value = Qnil;
3495 CHECK_STRING (resource);
3496 CHECK_STRING (class);
3497 CHECK_LIVE_FRAME (frame);
3498 BLOCK_INPUT;
3499 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3500 resource, class, Qnil, Qnil);
3501 UNBLOCK_INPUT;
3502 return value;
3506 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3507 If VALUE is "on" or "true", return t. If VALUE is "off" or
3508 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3509 error; if SIGNAL_P is zero, return 0. */
3511 static Lisp_Object
3512 face_boolean_x_resource_value (value, signal_p)
3513 Lisp_Object value;
3514 int signal_p;
3516 Lisp_Object result = make_number (0);
3518 xassert (STRINGP (value));
3520 if (xstrcasecmp (SDATA (value), "on") == 0
3521 || xstrcasecmp (SDATA (value), "true") == 0)
3522 result = Qt;
3523 else if (xstrcasecmp (SDATA (value), "off") == 0
3524 || xstrcasecmp (SDATA (value), "false") == 0)
3525 result = Qnil;
3526 else if (xstrcasecmp (SDATA (value), "unspecified") == 0)
3527 result = Qunspecified;
3528 else if (signal_p)
3529 signal_error ("Invalid face attribute value from X resource", value);
3531 return result;
3535 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3536 Finternal_set_lisp_face_attribute_from_resource,
3537 Sinternal_set_lisp_face_attribute_from_resource,
3538 3, 4, 0, doc: /* */)
3539 (face, attr, value, frame)
3540 Lisp_Object face, attr, value, frame;
3542 CHECK_SYMBOL (face);
3543 CHECK_SYMBOL (attr);
3544 CHECK_STRING (value);
3546 if (xstrcasecmp (SDATA (value), "unspecified") == 0)
3547 value = Qunspecified;
3548 else if (EQ (attr, QCheight))
3550 value = Fstring_to_number (value, make_number (10));
3551 if (XINT (value) <= 0)
3552 signal_error ("Invalid face height from X resource", value);
3554 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3555 value = face_boolean_x_resource_value (value, 1);
3556 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3557 value = intern (SDATA (value));
3558 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3559 value = face_boolean_x_resource_value (value, 1);
3560 else if (EQ (attr, QCunderline)
3561 || EQ (attr, QCoverline)
3562 || EQ (attr, QCstrike_through))
3564 Lisp_Object boolean_value;
3566 /* If the result of face_boolean_x_resource_value is t or nil,
3567 VALUE does NOT specify a color. */
3568 boolean_value = face_boolean_x_resource_value (value, 0);
3569 if (SYMBOLP (boolean_value))
3570 value = boolean_value;
3572 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3573 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3575 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3578 #endif /* HAVE_WINDOW_SYSTEM */
3581 /***********************************************************************
3582 Menu face
3583 ***********************************************************************/
3585 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3587 /* Make menus on frame F appear as specified by the `menu' face. */
3589 static void
3590 x_update_menu_appearance (f)
3591 struct frame *f;
3593 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3594 XrmDatabase rdb;
3596 if (dpyinfo
3597 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3598 rdb != NULL))
3600 char line[512];
3601 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3602 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3603 const char *myname = SDATA (Vx_resource_name);
3604 int changed_p = 0;
3605 #ifdef USE_MOTIF
3606 const char *popup_path = "popup_menu";
3607 #else
3608 const char *popup_path = "menu.popup";
3609 #endif
3611 if (STRINGP (LFACE_FOREGROUND (lface)))
3613 sprintf (line, "%s.%s*foreground: %s",
3614 myname, popup_path,
3615 SDATA (LFACE_FOREGROUND (lface)));
3616 XrmPutLineResource (&rdb, line);
3617 sprintf (line, "%s.pane.menubar*foreground: %s",
3618 myname, SDATA (LFACE_FOREGROUND (lface)));
3619 XrmPutLineResource (&rdb, line);
3620 changed_p = 1;
3623 if (STRINGP (LFACE_BACKGROUND (lface)))
3625 sprintf (line, "%s.%s*background: %s",
3626 myname, popup_path,
3627 SDATA (LFACE_BACKGROUND (lface)));
3628 XrmPutLineResource (&rdb, line);
3629 sprintf (line, "%s.pane.menubar*background: %s",
3630 myname, SDATA (LFACE_BACKGROUND (lface)));
3631 XrmPutLineResource (&rdb, line);
3632 changed_p = 1;
3635 if (face->font
3636 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3637 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3638 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3639 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3640 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3642 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3643 #ifdef USE_MOTIF
3644 const char *suffix = "List";
3645 Bool motif = True;
3646 #else
3647 #if defined HAVE_X_I18N
3649 const char *suffix = "Set";
3650 #else
3651 const char *suffix = "";
3652 #endif
3653 Bool motif = False;
3654 #endif
3656 if (! NILP (xlfd))
3658 #if defined HAVE_X_I18N
3659 extern char *xic_create_fontsetname
3660 P_ ((char *base_fontname, Bool motif));
3661 char *fontsetname = xic_create_fontsetname (SDATA (xlfd), motif);
3662 #else
3663 char *fontsetname = (char *) SDATA (xlfd);
3664 #endif
3665 sprintf (line, "%s.pane.menubar*font%s: %s",
3666 myname, suffix, fontsetname);
3667 XrmPutLineResource (&rdb, line);
3668 sprintf (line, "%s.%s*font%s: %s",
3669 myname, popup_path, suffix, fontsetname);
3670 XrmPutLineResource (&rdb, line);
3671 changed_p = 1;
3672 if (fontsetname != (char *) SDATA (xlfd))
3673 xfree (fontsetname);
3677 if (changed_p && f->output_data.x->menubar_widget)
3678 free_frame_menubar (f);
3682 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3685 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3686 Sface_attribute_relative_p,
3687 2, 2, 0,
3688 doc: /* Check whether a face attribute value is relative.
3689 Specifically, this function returns t if the attribute ATTRIBUTE
3690 with the value VALUE is relative.
3692 A relative value is one that doesn't entirely override whatever is
3693 inherited from another face. For most possible attributes,
3694 the only relative value that users see is `unspecified'.
3695 However, for :height, floating point values are also relative. */)
3696 (attribute, value)
3697 Lisp_Object attribute, value;
3699 if (EQ (value, Qunspecified) || (EQ (value, Qignore_defface)))
3700 return Qt;
3701 else if (EQ (attribute, QCheight))
3702 return INTEGERP (value) ? Qnil : Qt;
3703 else
3704 return Qnil;
3707 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3708 3, 3, 0,
3709 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3710 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3711 the result will be absolute, otherwise it will be relative. */)
3712 (attribute, value1, value2)
3713 Lisp_Object attribute, value1, value2;
3715 if (EQ (value1, Qunspecified) || EQ (value1, Qignore_defface))
3716 return value2;
3717 else if (EQ (attribute, QCheight))
3718 return merge_face_heights (value1, value2, value1);
3719 else
3720 return value1;
3724 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3725 Sinternal_get_lisp_face_attribute,
3726 2, 3, 0,
3727 doc: /* Return face attribute KEYWORD of face SYMBOL.
3728 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3729 face attribute name, signal an error.
3730 If the optional argument FRAME is given, report on face SYMBOL in that
3731 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3732 frames). If FRAME is omitted or nil, use the selected frame. */)
3733 (symbol, keyword, frame)
3734 Lisp_Object symbol, keyword, frame;
3736 Lisp_Object lface, value = Qnil;
3738 CHECK_SYMBOL (symbol);
3739 CHECK_SYMBOL (keyword);
3741 if (EQ (frame, Qt))
3742 lface = lface_from_face_name (NULL, symbol, 1);
3743 else
3745 if (NILP (frame))
3746 frame = selected_frame;
3747 CHECK_LIVE_FRAME (frame);
3748 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3751 if (EQ (keyword, QCfamily))
3752 value = LFACE_FAMILY (lface);
3753 else if (EQ (keyword, QCheight))
3754 value = LFACE_HEIGHT (lface);
3755 else if (EQ (keyword, QCweight))
3756 value = LFACE_WEIGHT (lface);
3757 else if (EQ (keyword, QCslant))
3758 value = LFACE_SLANT (lface);
3759 else if (EQ (keyword, QCunderline))
3760 value = LFACE_UNDERLINE (lface);
3761 else if (EQ (keyword, QCoverline))
3762 value = LFACE_OVERLINE (lface);
3763 else if (EQ (keyword, QCstrike_through))
3764 value = LFACE_STRIKE_THROUGH (lface);
3765 else if (EQ (keyword, QCbox))
3766 value = LFACE_BOX (lface);
3767 else if (EQ (keyword, QCinverse_video)
3768 || EQ (keyword, QCreverse_video))
3769 value = LFACE_INVERSE (lface);
3770 else if (EQ (keyword, QCforeground))
3771 value = LFACE_FOREGROUND (lface);
3772 else if (EQ (keyword, QCbackground))
3773 value = LFACE_BACKGROUND (lface);
3774 else if (EQ (keyword, QCstipple))
3775 value = LFACE_STIPPLE (lface);
3776 else if (EQ (keyword, QCwidth))
3777 value = LFACE_SWIDTH (lface);
3778 else if (EQ (keyword, QCinherit))
3779 value = LFACE_INHERIT (lface);
3780 else if (EQ (keyword, QCfont))
3781 value = LFACE_FONT (lface);
3782 else if (EQ (keyword, QCfontset))
3783 value = LFACE_FONTSET (lface);
3784 else
3785 signal_error ("Invalid face attribute name", keyword);
3787 if (IGNORE_DEFFACE_P (value))
3788 return Qunspecified;
3790 return value;
3794 DEFUN ("internal-lisp-face-attribute-values",
3795 Finternal_lisp_face_attribute_values,
3796 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3797 doc: /* Return a list of valid discrete values for face attribute ATTR.
3798 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3799 (attr)
3800 Lisp_Object attr;
3802 Lisp_Object result = Qnil;
3804 CHECK_SYMBOL (attr);
3806 if (EQ (attr, QCunderline))
3807 result = Fcons (Qt, Fcons (Qnil, Qnil));
3808 else if (EQ (attr, QCoverline))
3809 result = Fcons (Qt, Fcons (Qnil, Qnil));
3810 else if (EQ (attr, QCstrike_through))
3811 result = Fcons (Qt, Fcons (Qnil, Qnil));
3812 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3813 result = Fcons (Qt, Fcons (Qnil, Qnil));
3815 return result;
3819 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3820 Sinternal_merge_in_global_face, 2, 2, 0,
3821 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3822 Default face attributes override any local face attributes. */)
3823 (face, frame)
3824 Lisp_Object face, frame;
3826 int i;
3827 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3829 CHECK_LIVE_FRAME (frame);
3830 global_lface = lface_from_face_name (NULL, face, 1);
3831 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
3832 if (NILP (local_lface))
3833 local_lface = Finternal_make_lisp_face (face, frame);
3835 /* Make every specified global attribute override the local one.
3836 BEWARE!! This is only used from `face-set-after-frame-default' where
3837 the local frame is defined from default specs in `face-defface-spec'
3838 and those should be overridden by global settings. Hence the strange
3839 "global before local" priority. */
3840 lvec = XVECTOR (local_lface)->contents;
3841 gvec = XVECTOR (global_lface)->contents;
3842 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3843 if (! UNSPECIFIEDP (gvec[i]))
3845 if (IGNORE_DEFFACE_P (gvec[i]))
3846 lvec[i] = Qunspecified;
3847 else
3848 lvec[i] = gvec[i];
3851 return Qnil;
3855 /* The following function is implemented for compatibility with 20.2.
3856 The function is used in x-resolve-fonts when it is asked to
3857 return fonts with the same size as the font of a face. This is
3858 done in fontset.el. */
3860 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3861 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3862 The font name is, by default, for ASCII characters.
3863 If the optional argument FRAME is given, report on face FACE in that frame.
3864 If FRAME is t, report on the defaults for face FACE (for new frames).
3865 The font default for a face is either nil, or a list
3866 of the form (bold), (italic) or (bold italic).
3867 If FRAME is omitted or nil, use the selected frame. And, in this case,
3868 if the optional third argument CHARACTER is given,
3869 return the font name used for CHARACTER. */)
3870 (face, frame, character)
3871 Lisp_Object face, frame, character;
3873 if (EQ (frame, Qt))
3875 Lisp_Object result = Qnil;
3876 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3878 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3879 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3880 result = Fcons (Qbold, result);
3882 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3883 && !EQ (LFACE_SLANT (lface), Qnormal))
3884 result = Fcons (Qitalic, result);
3886 return result;
3888 else
3890 struct frame *f = frame_or_selected_frame (frame, 1);
3891 int face_id = lookup_named_face (f, face, 1);
3892 struct face *face = FACE_FROM_ID (f, face_id);
3894 if (! face)
3895 return Qnil;
3896 #ifdef HAVE_WINDOW_SYSTEM
3897 if (FRAME_WINDOW_P (f) && !NILP (character))
3899 CHECK_CHARACTER (character);
3900 face_id = FACE_FOR_CHAR (f, face, XINT (character), -1, Qnil);
3901 face = FACE_FROM_ID (f, face_id);
3903 return (face->font
3904 ? face->font->props[FONT_NAME_INDEX]
3905 : Qnil);
3906 #else /* !HAVE_WINDOW_SYSTEM */
3907 return build_string (FRAME_MSDOS_P (f)
3908 ? "ms-dos"
3909 : FRAME_W32_P (f) ? "w32term"
3910 :"tty");
3911 #endif
3916 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3917 all attributes are `equal'. Tries to be fast because this function
3918 is called quite often. */
3920 static INLINE int
3921 face_attr_equal_p (v1, v2)
3922 Lisp_Object v1, v2;
3924 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3925 and the other is specified. */
3926 if (XTYPE (v1) != XTYPE (v2))
3927 return 0;
3929 if (EQ (v1, v2))
3930 return 1;
3932 switch (XTYPE (v1))
3934 case Lisp_String:
3935 if (SBYTES (v1) != SBYTES (v2))
3936 return 0;
3938 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3940 case Lisp_Int:
3941 case Lisp_Symbol:
3942 return 0;
3944 default:
3945 return !NILP (Fequal (v1, v2));
3950 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3951 all attributes are `equal'. Tries to be fast because this function
3952 is called quite often. */
3954 static INLINE int
3955 lface_equal_p (v1, v2)
3956 Lisp_Object *v1, *v2;
3958 int i, equal_p = 1;
3960 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3961 equal_p = face_attr_equal_p (v1[i], v2[i]);
3963 return equal_p;
3967 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3968 Sinternal_lisp_face_equal_p, 2, 3, 0,
3969 doc: /* True if FACE1 and FACE2 are equal.
3970 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3971 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3972 If FRAME is omitted or nil, use the selected frame. */)
3973 (face1, face2, frame)
3974 Lisp_Object face1, face2, frame;
3976 int equal_p;
3977 struct frame *f;
3978 Lisp_Object lface1, lface2;
3980 if (EQ (frame, Qt))
3981 f = NULL;
3982 else
3983 /* Don't use check_x_frame here because this function is called
3984 before X frames exist. At that time, if FRAME is nil,
3985 selected_frame will be used which is the frame dumped with
3986 Emacs. That frame is not an X frame. */
3987 f = frame_or_selected_frame (frame, 2);
3989 lface1 = lface_from_face_name (f, face1, 1);
3990 lface2 = lface_from_face_name (f, face2, 1);
3991 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3992 XVECTOR (lface2)->contents);
3993 return equal_p ? Qt : Qnil;
3997 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3998 Sinternal_lisp_face_empty_p, 1, 2, 0,
3999 doc: /* True if FACE has no attribute specified.
4000 If the optional argument FRAME is given, report on face FACE in that frame.
4001 If FRAME is t, report on the defaults for face FACE (for new frames).
4002 If FRAME is omitted or nil, use the selected frame. */)
4003 (face, frame)
4004 Lisp_Object face, frame;
4006 struct frame *f;
4007 Lisp_Object lface;
4008 int i;
4010 if (NILP (frame))
4011 frame = selected_frame;
4012 CHECK_LIVE_FRAME (frame);
4013 f = XFRAME (frame);
4015 if (EQ (frame, Qt))
4016 lface = lface_from_face_name (NULL, face, 1);
4017 else
4018 lface = lface_from_face_name (f, face, 1);
4020 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4021 if (!UNSPECIFIEDP (AREF (lface, i)))
4022 break;
4024 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
4028 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
4029 0, 1, 0,
4030 doc: /* Return an alist of frame-local faces defined on FRAME.
4031 For internal use only. */)
4032 (frame)
4033 Lisp_Object frame;
4035 struct frame *f = frame_or_selected_frame (frame, 0);
4036 return f->face_alist;
4040 /* Return a hash code for Lisp string STRING with case ignored. Used
4041 below in computing a hash value for a Lisp face. */
4043 static INLINE unsigned
4044 hash_string_case_insensitive (string)
4045 Lisp_Object string;
4047 const unsigned char *s;
4048 unsigned hash = 0;
4049 xassert (STRINGP (string));
4050 for (s = SDATA (string); *s; ++s)
4051 hash = (hash << 1) ^ tolower (*s);
4052 return hash;
4056 /* Return a hash code for face attribute vector V. */
4058 static INLINE unsigned
4059 lface_hash (v)
4060 Lisp_Object *v;
4062 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4063 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4064 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4065 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4066 ^ XHASH (v[LFACE_SLANT_INDEX])
4067 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4068 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4072 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4073 considering charsets/registries). They do if they specify the same
4074 family, point size, weight, width, slant, and font. Both
4075 LFACE1 and LFACE2 must be fully-specified. */
4077 static INLINE int
4078 lface_same_font_attributes_p (lface1, lface2)
4079 Lisp_Object *lface1, *lface2;
4081 xassert (lface_fully_specified_p (lface1)
4082 && lface_fully_specified_p (lface2));
4083 return (xstrcasecmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
4084 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4085 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4086 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4087 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4088 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4089 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4090 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4091 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4092 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4093 && ! xstrcasecmp (SDATA (lface1[LFACE_FONTSET_INDEX]),
4094 SDATA (lface2[LFACE_FONTSET_INDEX]))))
4100 /***********************************************************************
4101 Realized Faces
4102 ***********************************************************************/
4104 /* Allocate and return a new realized face for Lisp face attribute
4105 vector ATTR. */
4107 static struct face *
4108 make_realized_face (attr)
4109 Lisp_Object *attr;
4111 struct face *face = (struct face *) xmalloc (sizeof *face);
4112 bzero (face, sizeof *face);
4113 face->ascii_face = face;
4114 bcopy (attr, face->lface, sizeof face->lface);
4115 return face;
4119 /* Free realized face FACE, including its X resources. FACE may
4120 be null. */
4122 void
4123 free_realized_face (f, face)
4124 struct frame *f;
4125 struct face *face;
4127 if (face)
4129 #ifdef HAVE_WINDOW_SYSTEM
4130 if (FRAME_WINDOW_P (f))
4132 /* Free fontset of FACE if it is ASCII face. */
4133 if (face->fontset >= 0 && face == face->ascii_face)
4134 free_face_fontset (f, face);
4135 if (face->gc)
4137 BLOCK_INPUT;
4138 if (face->font)
4139 font_done_for_face (f, face);
4140 x_free_gc (f, face->gc);
4141 face->gc = 0;
4142 UNBLOCK_INPUT;
4145 free_face_colors (f, face);
4146 x_destroy_bitmap (f, face->stipple);
4148 #endif /* HAVE_WINDOW_SYSTEM */
4150 xfree (face);
4155 /* Prepare face FACE for subsequent display on frame F. This
4156 allocated GCs if they haven't been allocated yet or have been freed
4157 by clearing the face cache. */
4159 void
4160 prepare_face_for_display (f, face)
4161 struct frame *f;
4162 struct face *face;
4164 #ifdef HAVE_WINDOW_SYSTEM
4165 xassert (FRAME_WINDOW_P (f));
4167 if (face->gc == 0)
4169 XGCValues xgcv;
4170 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4172 xgcv.foreground = face->foreground;
4173 xgcv.background = face->background;
4174 #ifdef HAVE_X_WINDOWS
4175 xgcv.graphics_exposures = False;
4176 #endif
4178 BLOCK_INPUT;
4179 #ifdef HAVE_X_WINDOWS
4180 if (face->stipple)
4182 xgcv.fill_style = FillOpaqueStippled;
4183 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4184 mask |= GCFillStyle | GCStipple;
4186 #endif
4187 face->gc = x_create_gc (f, mask, &xgcv);
4188 if (face->font)
4189 font_prepare_for_face (f, face);
4190 UNBLOCK_INPUT;
4192 #endif /* HAVE_WINDOW_SYSTEM */
4196 /* Returns the `distance' between the colors X and Y. */
4198 static int
4199 color_distance (x, y)
4200 XColor *x, *y;
4202 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
4203 Quoting from that paper:
4205 This formula has results that are very close to L*u*v* (with the
4206 modified lightness curve) and, more importantly, it is a more even
4207 algorithm: it does not have a range of colours where it suddenly
4208 gives far from optimal results.
4210 See <http://www.compuphase.com/cmetric.htm> for more info. */
4212 long r = (x->red - y->red) >> 8;
4213 long g = (x->green - y->green) >> 8;
4214 long b = (x->blue - y->blue) >> 8;
4215 long r_mean = (x->red + y->red) >> 9;
4217 return
4218 (((512 + r_mean) * r * r) >> 8)
4219 + 4 * g * g
4220 + (((767 - r_mean) * b * b) >> 8);
4224 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4225 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4226 COLOR1 and COLOR2 may be either strings containing the color name,
4227 or lists of the form (RED GREEN BLUE).
4228 If FRAME is unspecified or nil, the current frame is used. */)
4229 (color1, color2, frame)
4230 Lisp_Object color1, color2, frame;
4232 struct frame *f;
4233 XColor cdef1, cdef2;
4235 if (NILP (frame))
4236 frame = selected_frame;
4237 CHECK_LIVE_FRAME (frame);
4238 f = XFRAME (frame);
4240 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4241 && !(STRINGP (color1) && defined_color (f, SDATA (color1), &cdef1, 0)))
4242 signal_error ("Invalid color", color1);
4243 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4244 && !(STRINGP (color2) && defined_color (f, SDATA (color2), &cdef2, 0)))
4245 signal_error ("Invalid color", color2);
4247 return make_number (color_distance (&cdef1, &cdef2));
4251 /***********************************************************************
4252 Face Cache
4253 ***********************************************************************/
4255 /* Return a new face cache for frame F. */
4257 static struct face_cache *
4258 make_face_cache (f)
4259 struct frame *f;
4261 struct face_cache *c;
4262 int size;
4264 c = (struct face_cache *) xmalloc (sizeof *c);
4265 bzero (c, sizeof *c);
4266 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4267 c->buckets = (struct face **) xmalloc (size);
4268 bzero (c->buckets, size);
4269 c->size = 50;
4270 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4271 c->f = f;
4272 c->menu_face_changed_p = menu_face_changed_default;
4273 return c;
4277 /* Clear out all graphics contexts for all realized faces, except for
4278 the basic faces. This should be done from time to time just to avoid
4279 keeping too many graphics contexts that are no longer needed. */
4281 static void
4282 clear_face_gcs (c)
4283 struct face_cache *c;
4285 if (c && FRAME_WINDOW_P (c->f))
4287 #ifdef HAVE_WINDOW_SYSTEM
4288 int i;
4289 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4291 struct face *face = c->faces_by_id[i];
4292 if (face && face->gc)
4294 BLOCK_INPUT;
4295 if (face->font)
4296 font_done_for_face (c->f, face);
4297 x_free_gc (c->f, face->gc);
4298 face->gc = 0;
4299 UNBLOCK_INPUT;
4302 #endif /* HAVE_WINDOW_SYSTEM */
4307 /* Free all realized faces in face cache C, including basic faces.
4308 C may be null. If faces are freed, make sure the frame's current
4309 matrix is marked invalid, so that a display caused by an expose
4310 event doesn't try to use faces we destroyed. */
4312 static void
4313 free_realized_faces (c)
4314 struct face_cache *c;
4316 if (c && c->used)
4318 int i, size;
4319 struct frame *f = c->f;
4321 /* We must block input here because we can't process X events
4322 safely while only some faces are freed, or when the frame's
4323 current matrix still references freed faces. */
4324 BLOCK_INPUT;
4326 for (i = 0; i < c->used; ++i)
4328 free_realized_face (f, c->faces_by_id[i]);
4329 c->faces_by_id[i] = NULL;
4332 c->used = 0;
4333 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4334 bzero (c->buckets, size);
4336 /* Must do a thorough redisplay the next time. Mark current
4337 matrices as invalid because they will reference faces freed
4338 above. This function is also called when a frame is
4339 destroyed. In this case, the root window of F is nil. */
4340 if (WINDOWP (f->root_window))
4342 clear_current_matrices (f);
4343 ++windows_or_buffers_changed;
4346 UNBLOCK_INPUT;
4351 /* Free all realized faces that are using FONTSET on frame F. */
4353 void
4354 free_realized_faces_for_fontset (f, fontset)
4355 struct frame *f;
4356 int fontset;
4358 struct face_cache *cache = FRAME_FACE_CACHE (f);
4359 struct face *face;
4360 int i;
4362 /* We must block input here because we can't process X events safely
4363 while only some faces are freed, or when the frame's current
4364 matrix still references freed faces. */
4365 BLOCK_INPUT;
4367 for (i = 0; i < cache->used; i++)
4369 face = cache->faces_by_id[i];
4370 if (face
4371 && face->fontset == fontset)
4373 uncache_face (cache, face);
4374 free_realized_face (f, face);
4378 /* Must do a thorough redisplay the next time. Mark current
4379 matrices as invalid because they will reference faces freed
4380 above. This function is also called when a frame is destroyed.
4381 In this case, the root window of F is nil. */
4382 if (WINDOWP (f->root_window))
4384 clear_current_matrices (f);
4385 ++windows_or_buffers_changed;
4388 UNBLOCK_INPUT;
4392 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4393 This is done after attributes of a named face have been changed,
4394 because we can't tell which realized faces depend on that face. */
4396 void
4397 free_all_realized_faces (frame)
4398 Lisp_Object frame;
4400 if (NILP (frame))
4402 Lisp_Object rest;
4403 FOR_EACH_FRAME (rest, frame)
4404 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4406 else
4407 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4411 /* Free face cache C and faces in it, including their X resources. */
4413 static void
4414 free_face_cache (c)
4415 struct face_cache *c;
4417 if (c)
4419 free_realized_faces (c);
4420 xfree (c->buckets);
4421 xfree (c->faces_by_id);
4422 xfree (c);
4427 /* Cache realized face FACE in face cache C. HASH is the hash value
4428 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4429 FACE), insert the new face to the beginning of the collision list
4430 of the face hash table of C. Otherwise, add the new face to the
4431 end of the collision list. This way, lookup_face can quickly find
4432 that a requested face is not cached. */
4434 static void
4435 cache_face (c, face, hash)
4436 struct face_cache *c;
4437 struct face *face;
4438 unsigned hash;
4440 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4442 face->hash = hash;
4444 if (face->ascii_face != face)
4446 struct face *last = c->buckets[i];
4447 if (last)
4449 while (last->next)
4450 last = last->next;
4451 last->next = face;
4452 face->prev = last;
4453 face->next = NULL;
4455 else
4457 c->buckets[i] = face;
4458 face->prev = face->next = NULL;
4461 else
4463 face->prev = NULL;
4464 face->next = c->buckets[i];
4465 if (face->next)
4466 face->next->prev = face;
4467 c->buckets[i] = face;
4470 /* Find a free slot in C->faces_by_id and use the index of the free
4471 slot as FACE->id. */
4472 for (i = 0; i < c->used; ++i)
4473 if (c->faces_by_id[i] == NULL)
4474 break;
4475 face->id = i;
4477 /* Maybe enlarge C->faces_by_id. */
4478 if (i == c->used)
4480 if (c->used == c->size)
4482 int new_size, sz;
4483 new_size = min (2 * c->size, MAX_FACE_ID);
4484 if (new_size == c->size)
4485 abort (); /* Alternatives? ++kfs */
4486 sz = new_size * sizeof *c->faces_by_id;
4487 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4488 c->size = new_size;
4490 c->used++;
4493 #if GLYPH_DEBUG
4494 /* Check that FACE got a unique id. */
4496 int j, n;
4497 struct face *face;
4499 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4500 for (face = c->buckets[j]; face; face = face->next)
4501 if (face->id == i)
4502 ++n;
4504 xassert (n == 1);
4506 #endif /* GLYPH_DEBUG */
4508 c->faces_by_id[i] = face;
4512 /* Remove face FACE from cache C. */
4514 static void
4515 uncache_face (c, face)
4516 struct face_cache *c;
4517 struct face *face;
4519 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4521 if (face->prev)
4522 face->prev->next = face->next;
4523 else
4524 c->buckets[i] = face->next;
4526 if (face->next)
4527 face->next->prev = face->prev;
4529 c->faces_by_id[face->id] = NULL;
4530 if (face->id == c->used)
4531 --c->used;
4535 /* Look up a realized face with face attributes ATTR in the face cache
4536 of frame F. The face will be used to display ASCII characters.
4537 Value is the ID of the face found. If no suitable face is found,
4538 realize a new one. */
4540 INLINE int
4541 lookup_face (f, attr)
4542 struct frame *f;
4543 Lisp_Object *attr;
4545 struct face_cache *cache = FRAME_FACE_CACHE (f);
4546 unsigned hash;
4547 int i;
4548 struct face *face;
4550 xassert (cache != NULL);
4551 check_lface_attrs (attr);
4553 /* Look up ATTR in the face cache. */
4554 hash = lface_hash (attr);
4555 i = hash % FACE_CACHE_BUCKETS_SIZE;
4557 for (face = cache->buckets[i]; face; face = face->next)
4559 if (face->ascii_face != face)
4561 /* There's no more ASCII face. */
4562 face = NULL;
4563 break;
4565 if (face->hash == hash
4566 && lface_equal_p (face->lface, attr))
4567 break;
4570 /* If not found, realize a new face. */
4571 if (face == NULL)
4572 face = realize_face (cache, attr, -1);
4574 #if GLYPH_DEBUG
4575 xassert (face == FACE_FROM_ID (f, face->id));
4576 #endif /* GLYPH_DEBUG */
4578 return face->id;
4581 #ifdef HAVE_WINDOW_SYSTEM
4582 /* Look up a realized face that has the same attributes as BASE_FACE
4583 except for the font in the face cache of frame F. If FONT-OBJECT
4584 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4585 the face has no font. Value is the ID of the face found. If no
4586 suitable face is found, realize a new one. */
4589 face_for_font (f, font_object, base_face)
4590 struct frame *f;
4591 Lisp_Object font_object;
4592 struct face *base_face;
4594 struct face_cache *cache = FRAME_FACE_CACHE (f);
4595 unsigned hash;
4596 int i;
4597 struct face *face;
4599 xassert (cache != NULL);
4600 base_face = base_face->ascii_face;
4601 hash = lface_hash (base_face->lface);
4602 i = hash % FACE_CACHE_BUCKETS_SIZE;
4604 for (face = cache->buckets[i]; face; face = face->next)
4606 if (face->ascii_face == face)
4607 continue;
4608 if (face->ascii_face == base_face
4609 && face->font == (NILP (font_object) ? NULL
4610 : XFONT_OBJECT (font_object))
4611 && lface_equal_p (face->lface, base_face->lface))
4612 return face->id;
4615 /* If not found, realize a new face. */
4616 face = realize_non_ascii_face (f, font_object, base_face);
4617 return face->id;
4619 #endif /* HAVE_WINDOW_SYSTEM */
4621 /* Return the face id of the realized face for named face SYMBOL on
4622 frame F suitable for displaying ASCII characters. Value is -1 if
4623 the face couldn't be determined, which might happen if the default
4624 face isn't realized and cannot be realized. */
4627 lookup_named_face (f, symbol, signal_p)
4628 struct frame *f;
4629 Lisp_Object symbol;
4630 int signal_p;
4632 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4633 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4634 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4636 if (default_face == NULL)
4638 if (!realize_basic_faces (f))
4639 return -1;
4640 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4641 if (default_face == NULL)
4642 abort (); /* realize_basic_faces must have set it up */
4645 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p))
4646 return -1;
4648 bcopy (default_face->lface, attrs, sizeof attrs);
4649 merge_face_vectors (f, symbol_attrs, attrs, 0);
4651 return lookup_face (f, attrs);
4655 /* Return the ID of the realized ASCII face of Lisp face with ID
4656 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
4659 ascii_face_of_lisp_face (f, lface_id)
4660 struct frame *f;
4661 int lface_id;
4663 int face_id;
4665 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
4667 Lisp_Object face_name = lface_id_to_name[lface_id];
4668 face_id = lookup_named_face (f, face_name, 1);
4670 else
4671 face_id = -1;
4673 return face_id;
4677 /* Return a face for charset ASCII that is like the face with id
4678 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4679 STEPS < 0 means larger. Value is the id of the face. */
4682 smaller_face (f, face_id, steps)
4683 struct frame *f;
4684 int face_id, steps;
4686 #ifdef HAVE_WINDOW_SYSTEM
4687 struct face *face;
4688 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4689 int pt, last_pt, last_height;
4690 int delta;
4691 int new_face_id;
4692 struct face *new_face;
4694 /* If not called for an X frame, just return the original face. */
4695 if (FRAME_TERMCAP_P (f))
4696 return face_id;
4698 /* Try in increments of 1/2 pt. */
4699 delta = steps < 0 ? 5 : -5;
4700 steps = eabs (steps);
4702 face = FACE_FROM_ID (f, face_id);
4703 bcopy (face->lface, attrs, sizeof attrs);
4704 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4705 new_face_id = face_id;
4706 last_height = FONT_HEIGHT (face->font);
4708 while (steps
4709 && pt + delta > 0
4710 /* Give up if we cannot find a font within 10pt. */
4711 && eabs (last_pt - pt) < 100)
4713 /* Look up a face for a slightly smaller/larger font. */
4714 pt += delta;
4715 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4716 new_face_id = lookup_face (f, attrs);
4717 new_face = FACE_FROM_ID (f, new_face_id);
4719 /* If height changes, count that as one step. */
4720 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4721 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4723 --steps;
4724 last_height = FONT_HEIGHT (new_face->font);
4725 last_pt = pt;
4729 return new_face_id;
4731 #else /* not HAVE_WINDOW_SYSTEM */
4733 return face_id;
4735 #endif /* not HAVE_WINDOW_SYSTEM */
4739 /* Return a face for charset ASCII that is like the face with id
4740 FACE_ID on frame F, but has height HEIGHT. */
4743 face_with_height (f, face_id, height)
4744 struct frame *f;
4745 int face_id;
4746 int height;
4748 #ifdef HAVE_WINDOW_SYSTEM
4749 struct face *face;
4750 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4752 if (FRAME_TERMCAP_P (f)
4753 || height <= 0)
4754 return face_id;
4756 face = FACE_FROM_ID (f, face_id);
4757 bcopy (face->lface, attrs, sizeof attrs);
4758 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4759 face_id = lookup_face (f, attrs);
4760 #endif /* HAVE_WINDOW_SYSTEM */
4762 return face_id;
4766 /* Return the face id of the realized face for named face SYMBOL on
4767 frame F suitable for displaying ASCII characters, and use
4768 attributes of the face FACE_ID for attributes that aren't
4769 completely specified by SYMBOL. This is like lookup_named_face,
4770 except that the default attributes come from FACE_ID, not from the
4771 default face. FACE_ID is assumed to be already realized. */
4774 lookup_derived_face (f, symbol, face_id, signal_p)
4775 struct frame *f;
4776 Lisp_Object symbol;
4777 int face_id;
4778 int signal_p;
4780 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4781 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4782 struct face *default_face = FACE_FROM_ID (f, face_id);
4784 if (!default_face)
4785 abort ();
4787 get_lface_attributes (f, symbol, symbol_attrs, signal_p);
4788 bcopy (default_face->lface, attrs, sizeof attrs);
4789 merge_face_vectors (f, symbol_attrs, attrs, 0);
4790 return lookup_face (f, attrs);
4793 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4794 Sface_attributes_as_vector, 1, 1, 0,
4795 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4796 (plist)
4797 Lisp_Object plist;
4799 Lisp_Object lface;
4800 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4801 Qunspecified);
4802 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4803 1, 0);
4804 return lface;
4809 /***********************************************************************
4810 Face capability testing
4811 ***********************************************************************/
4814 /* If the distance (as returned by color_distance) between two colors is
4815 less than this, then they are considered the same, for determining
4816 whether a color is supported or not. The range of values is 0-65535. */
4818 #define TTY_SAME_COLOR_THRESHOLD 10000
4820 #ifdef HAVE_WINDOW_SYSTEM
4822 /* Return non-zero if all the face attributes in ATTRS are supported
4823 on the window-system frame F.
4825 The definition of `supported' is somewhat heuristic, but basically means
4826 that a face containing all the attributes in ATTRS, when merged with the
4827 default face for display, can be represented in a way that's
4829 \(1) different in appearance than the default face, and
4830 \(2) `close in spirit' to what the attributes specify, if not exact. */
4832 static int
4833 x_supports_face_attributes_p (f, attrs, def_face)
4834 struct frame *f;
4835 Lisp_Object *attrs;
4836 struct face *def_face;
4838 Lisp_Object *def_attrs = def_face->lface;
4840 /* Check that other specified attributes are different that the default
4841 face. */
4842 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4843 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4844 def_attrs[LFACE_UNDERLINE_INDEX]))
4845 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4846 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4847 def_attrs[LFACE_INVERSE_INDEX]))
4848 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4849 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4850 def_attrs[LFACE_FOREGROUND_INDEX]))
4851 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4852 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4853 def_attrs[LFACE_BACKGROUND_INDEX]))
4854 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4855 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4856 def_attrs[LFACE_STIPPLE_INDEX]))
4857 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4858 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4859 def_attrs[LFACE_OVERLINE_INDEX]))
4860 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4861 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4862 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4863 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4864 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4865 def_attrs[LFACE_BOX_INDEX])))
4866 return 0;
4868 /* Check font-related attributes, as those are the most commonly
4869 "unsupported" on a window-system (because of missing fonts). */
4870 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4871 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4872 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4873 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4874 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4876 int face_id;
4877 struct face *face;
4878 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4879 int i;
4881 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
4883 merge_face_vectors (f, attrs, merged_attrs, 0);
4885 face_id = lookup_face (f, merged_attrs);
4886 face = FACE_FROM_ID (f, face_id);
4888 if (! face)
4889 error ("Cannot make face");
4891 /* If the font is the same, then not supported. */
4892 if (face->font == def_face->font)
4893 return 0;
4894 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4895 if (! EQ (face->font->props[i], def_face->font->props[i]))
4897 Lisp_Object s1, s2;
4899 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4900 || face->font->driver->case_sensitive)
4901 return 1;
4902 s1 = SYMBOL_NAME (face->font->props[i]);
4903 s2 = SYMBOL_NAME (def_face->font->props[i]);
4904 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4905 s2, make_number (0), Qnil, Qt), Qt))
4906 return 1;
4908 return 0;
4911 /* Everything checks out, this face is supported. */
4912 return 1;
4915 #endif /* HAVE_WINDOW_SYSTEM */
4917 /* Return non-zero if all the face attributes in ATTRS are supported
4918 on the tty frame F.
4920 The definition of `supported' is somewhat heuristic, but basically means
4921 that a face containing all the attributes in ATTRS, when merged
4922 with the default face for display, can be represented in a way that's
4924 \(1) different in appearance than the default face, and
4925 \(2) `close in spirit' to what the attributes specify, if not exact.
4927 Point (2) implies that a `:weight black' attribute will be satisfied
4928 by any terminal that can display bold, and a `:foreground "yellow"' as
4929 long as the terminal can display a yellowish color, but `:slant italic'
4930 will _not_ be satisfied by the tty display code's automatic
4931 substitution of a `dim' face for italic. */
4933 static int
4934 tty_supports_face_attributes_p (f, attrs, def_face)
4935 struct frame *f;
4936 Lisp_Object *attrs;
4937 struct face *def_face;
4939 int weight;
4940 Lisp_Object val, fg, bg;
4941 XColor fg_tty_color, fg_std_color;
4942 XColor bg_tty_color, bg_std_color;
4943 unsigned test_caps = 0;
4944 Lisp_Object *def_attrs = def_face->lface;
4947 /* First check some easy-to-check stuff; ttys support none of the
4948 following attributes, so we can just return false if any are requested
4949 (even if `nominal' values are specified, we should still return false,
4950 as that will be the same value that the default face uses). We
4951 consider :slant unsupportable on ttys, even though the face code
4952 actually `fakes' them using a dim attribute if possible. This is
4953 because the faked result is too different from what the face
4954 specifies. */
4955 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4956 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4957 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4958 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4959 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4960 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4961 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4962 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
4963 return 0;
4966 /* Test for terminal `capabilities' (non-color character attributes). */
4968 /* font weight (bold/dim) */
4969 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
4970 if (weight >= 0)
4972 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4974 if (weight > 100)
4976 if (def_weight > 100)
4977 return 0; /* same as default */
4978 test_caps = TTY_CAP_BOLD;
4980 else if (weight < 100)
4982 if (def_weight < 100)
4983 return 0; /* same as default */
4984 test_caps = TTY_CAP_DIM;
4986 else if (def_weight == 100)
4987 return 0; /* same as default */
4990 /* underlining */
4991 val = attrs[LFACE_UNDERLINE_INDEX];
4992 if (!UNSPECIFIEDP (val))
4994 if (STRINGP (val))
4995 return 0; /* ttys can't use colored underlines */
4996 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4997 return 0; /* same as default */
4998 else
4999 test_caps |= TTY_CAP_UNDERLINE;
5002 /* inverse video */
5003 val = attrs[LFACE_INVERSE_INDEX];
5004 if (!UNSPECIFIEDP (val))
5006 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
5007 return 0; /* same as default */
5008 else
5009 test_caps |= TTY_CAP_INVERSE;
5013 /* Color testing. */
5015 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5016 we use them when calling `tty_capable_p' below, even if the face
5017 specifies no colors. */
5018 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5019 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
5021 /* Check if foreground color is close enough. */
5022 fg = attrs[LFACE_FOREGROUND_INDEX];
5023 if (STRINGP (fg))
5025 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
5027 if (face_attr_equal_p (fg, def_fg))
5028 return 0; /* same as default */
5029 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
5030 return 0; /* not a valid color */
5031 else if (color_distance (&fg_tty_color, &fg_std_color)
5032 > TTY_SAME_COLOR_THRESHOLD)
5033 return 0; /* displayed color is too different */
5034 else
5035 /* Make sure the color is really different than the default. */
5037 XColor def_fg_color;
5038 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
5039 && (color_distance (&fg_tty_color, &def_fg_color)
5040 <= TTY_SAME_COLOR_THRESHOLD))
5041 return 0;
5045 /* Check if background color is close enough. */
5046 bg = attrs[LFACE_BACKGROUND_INDEX];
5047 if (STRINGP (bg))
5049 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
5051 if (face_attr_equal_p (bg, def_bg))
5052 return 0; /* same as default */
5053 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
5054 return 0; /* not a valid color */
5055 else if (color_distance (&bg_tty_color, &bg_std_color)
5056 > TTY_SAME_COLOR_THRESHOLD)
5057 return 0; /* displayed color is too different */
5058 else
5059 /* Make sure the color is really different than the default. */
5061 XColor def_bg_color;
5062 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
5063 && (color_distance (&bg_tty_color, &def_bg_color)
5064 <= TTY_SAME_COLOR_THRESHOLD))
5065 return 0;
5069 /* If both foreground and background are requested, see if the
5070 distance between them is OK. We just check to see if the distance
5071 between the tty's foreground and background is close enough to the
5072 distance between the standard foreground and background. */
5073 if (STRINGP (fg) && STRINGP (bg))
5075 int delta_delta
5076 = (color_distance (&fg_std_color, &bg_std_color)
5077 - color_distance (&fg_tty_color, &bg_tty_color));
5078 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
5079 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
5080 return 0;
5084 /* See if the capabilities we selected above are supported, with the
5085 given colors. */
5086 if (test_caps != 0 &&
5087 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
5088 return 0;
5091 /* Hmmm, everything checks out, this terminal must support this face. */
5092 return 1;
5096 DEFUN ("display-supports-face-attributes-p",
5097 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5098 1, 2, 0,
5099 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5100 The optional argument DISPLAY can be a display name, a frame, or
5101 nil (meaning the selected frame's display).
5103 The definition of `supported' is somewhat heuristic, but basically means
5104 that a face containing all the attributes in ATTRIBUTES, when merged
5105 with the default face for display, can be represented in a way that's
5107 \(1) different in appearance than the default face, and
5108 \(2) `close in spirit' to what the attributes specify, if not exact.
5110 Point (2) implies that a `:weight black' attribute will be satisfied by
5111 any display that can display bold, and a `:foreground \"yellow\"' as long
5112 as it can display a yellowish color, but `:slant italic' will _not_ be
5113 satisfied by the tty display code's automatic substitution of a `dim'
5114 face for italic. */)
5115 (attributes, display)
5116 Lisp_Object attributes, display;
5118 int supports = 0, i;
5119 Lisp_Object frame;
5120 struct frame *f;
5121 struct face *def_face;
5122 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5124 if (noninteractive || !initialized)
5125 /* We may not be able to access low-level face information in batch
5126 mode, or before being dumped, and this function is not going to
5127 be very useful in those cases anyway, so just give up. */
5128 return Qnil;
5130 if (NILP (display))
5131 frame = selected_frame;
5132 else if (FRAMEP (display))
5133 frame = display;
5134 else
5136 /* Find any frame on DISPLAY. */
5137 Lisp_Object fl_tail;
5139 frame = Qnil;
5140 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
5142 frame = XCAR (fl_tail);
5143 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5144 XFRAME (frame)->param_alist)),
5145 display)))
5146 break;
5150 CHECK_LIVE_FRAME (frame);
5151 f = XFRAME (frame);
5153 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5154 attrs[i] = Qunspecified;
5155 merge_face_ref (f, attributes, attrs, 1, 0);
5157 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5158 if (def_face == NULL)
5160 if (! realize_basic_faces (f))
5161 error ("Cannot realize default face");
5162 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5163 if (def_face == NULL)
5164 abort (); /* realize_basic_faces must have set it up */
5167 /* Dispatch to the appropriate handler. */
5168 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5169 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5170 #ifdef HAVE_WINDOW_SYSTEM
5171 else
5172 supports = x_supports_face_attributes_p (f, attrs, def_face);
5173 #endif
5175 return supports ? Qt : Qnil;
5179 /***********************************************************************
5180 Font selection
5181 ***********************************************************************/
5183 DEFUN ("internal-set-font-selection-order",
5184 Finternal_set_font_selection_order,
5185 Sinternal_set_font_selection_order, 1, 1, 0,
5186 doc: /* Set font selection order for face font selection to ORDER.
5187 ORDER must be a list of length 4 containing the symbols `:width',
5188 `:height', `:weight', and `:slant'. Face attributes appearing
5189 first in ORDER are matched first, e.g. if `:height' appears before
5190 `:weight' in ORDER, font selection first tries to find a font with
5191 a suitable height, and then tries to match the font weight.
5192 Value is ORDER. */)
5193 (order)
5194 Lisp_Object order;
5196 Lisp_Object list;
5197 int i;
5198 int indices[DIM (font_sort_order)];
5200 CHECK_LIST (order);
5201 bzero (indices, sizeof indices);
5202 i = 0;
5204 for (list = order;
5205 CONSP (list) && i < DIM (indices);
5206 list = XCDR (list), ++i)
5208 Lisp_Object attr = XCAR (list);
5209 int xlfd;
5211 if (EQ (attr, QCwidth))
5212 xlfd = XLFD_SWIDTH;
5213 else if (EQ (attr, QCheight))
5214 xlfd = XLFD_POINT_SIZE;
5215 else if (EQ (attr, QCweight))
5216 xlfd = XLFD_WEIGHT;
5217 else if (EQ (attr, QCslant))
5218 xlfd = XLFD_SLANT;
5219 else
5220 break;
5222 if (indices[i] != 0)
5223 break;
5224 indices[i] = xlfd;
5227 if (!NILP (list) || i != DIM (indices))
5228 signal_error ("Invalid font sort order", order);
5229 for (i = 0; i < DIM (font_sort_order); ++i)
5230 if (indices[i] == 0)
5231 signal_error ("Invalid font sort order", order);
5233 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
5235 bcopy (indices, font_sort_order, sizeof font_sort_order);
5236 free_all_realized_faces (Qnil);
5239 font_update_sort_order (font_sort_order);
5241 return Qnil;
5245 DEFUN ("internal-set-alternative-font-family-alist",
5246 Finternal_set_alternative_font_family_alist,
5247 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5248 doc: /* Define alternative font families to try in face font selection.
5249 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5250 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5251 be found. Value is ALIST. */)
5252 (alist)
5253 Lisp_Object alist;
5255 CHECK_LIST (alist);
5256 Vface_alternative_font_family_alist = alist;
5257 free_all_realized_faces (Qnil);
5258 return alist;
5262 DEFUN ("internal-set-alternative-font-registry-alist",
5263 Finternal_set_alternative_font_registry_alist,
5264 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5265 doc: /* Define alternative font registries to try in face font selection.
5266 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5267 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5268 be found. Value is ALIST. */)
5269 (alist)
5270 Lisp_Object alist;
5272 CHECK_LIST (alist);
5273 Vface_alternative_font_registry_alist = alist;
5274 free_all_realized_faces (Qnil);
5275 return alist;
5279 #ifdef HAVE_WINDOW_SYSTEM
5281 /* Ignore the difference of font point size less than this value. */
5283 #define FONT_POINT_SIZE_QUANTUM 5
5285 /* Return the fontset id of the base fontset name or alias name given
5286 by the fontset attribute of ATTRS. Value is -1 if the fontset
5287 attribute of ATTRS doesn't name a fontset. */
5289 static int
5290 face_fontset (attrs)
5291 Lisp_Object *attrs;
5293 Lisp_Object name;
5295 name = attrs[LFACE_FONTSET_INDEX];
5296 if (!STRINGP (name))
5297 return -1;
5298 return fs_query_fontset (name, 0);
5301 #endif /* HAVE_WINDOW_SYSTEM */
5305 /***********************************************************************
5306 Face Realization
5307 ***********************************************************************/
5309 /* Realize basic faces on frame F. Value is zero if frame parameters
5310 of F don't contain enough information needed to realize the default
5311 face. */
5313 static int
5314 realize_basic_faces (f)
5315 struct frame *f;
5317 int success_p = 0;
5318 int count = SPECPDL_INDEX ();
5320 /* Block input here so that we won't be surprised by an X expose
5321 event, for instance, without having the faces set up. */
5322 BLOCK_INPUT;
5323 specbind (Qscalable_fonts_allowed, Qt);
5325 if (realize_default_face (f))
5327 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5328 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5329 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5330 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5331 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5332 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5333 realize_named_face (f, Qborder, BORDER_FACE_ID);
5334 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5335 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5336 realize_named_face (f, Qmenu, MENU_FACE_ID);
5337 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5339 /* Reflect changes in the `menu' face in menu bars. */
5340 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5342 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5343 #ifdef USE_X_TOOLKIT
5344 if (FRAME_WINDOW_P (f))
5345 x_update_menu_appearance (f);
5346 #endif
5349 success_p = 1;
5352 unbind_to (count, Qnil);
5353 UNBLOCK_INPUT;
5354 return success_p;
5358 /* Realize the default face on frame F. If the face is not fully
5359 specified, make it fully-specified. Attributes of the default face
5360 that are not explicitly specified are taken from frame parameters. */
5362 static int
5363 realize_default_face (f)
5364 struct frame *f;
5366 struct face_cache *c = FRAME_FACE_CACHE (f);
5367 Lisp_Object lface;
5368 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5369 struct face *face;
5371 /* If the `default' face is not yet known, create it. */
5372 lface = lface_from_face_name (f, Qdefault, 0);
5373 if (NILP (lface))
5375 Lisp_Object frame;
5376 XSETFRAME (frame, f);
5377 lface = Finternal_make_lisp_face (Qdefault, frame);
5380 #ifdef HAVE_WINDOW_SYSTEM
5381 if (FRAME_WINDOW_P (f))
5383 Lisp_Object font_object;
5385 XSETFONT (font_object, FRAME_FONT (f));
5386 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5387 LFACE_FONTSET (lface) = fontset_name (FRAME_FONTSET (f));
5388 f->default_face_done_p = 1;
5390 #endif /* HAVE_WINDOW_SYSTEM */
5392 if (!FRAME_WINDOW_P (f))
5394 LFACE_FAMILY (lface) = build_string ("default");
5395 LFACE_SWIDTH (lface) = Qnormal;
5396 LFACE_HEIGHT (lface) = make_number (1);
5397 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5398 LFACE_WEIGHT (lface) = Qnormal;
5399 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5400 LFACE_SLANT (lface) = Qnormal;
5403 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5404 LFACE_UNDERLINE (lface) = Qnil;
5406 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5407 LFACE_OVERLINE (lface) = Qnil;
5409 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5410 LFACE_STRIKE_THROUGH (lface) = Qnil;
5412 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5413 LFACE_BOX (lface) = Qnil;
5415 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5416 LFACE_INVERSE (lface) = Qnil;
5418 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5420 /* This function is called so early that colors are not yet
5421 set in the frame parameter list. */
5422 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5424 if (CONSP (color) && STRINGP (XCDR (color)))
5425 LFACE_FOREGROUND (lface) = XCDR (color);
5426 else if (FRAME_WINDOW_P (f))
5427 return 0;
5428 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5429 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
5430 else
5431 abort ();
5434 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5436 /* This function is called so early that colors are not yet
5437 set in the frame parameter list. */
5438 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5439 if (CONSP (color) && STRINGP (XCDR (color)))
5440 LFACE_BACKGROUND (lface) = XCDR (color);
5441 else if (FRAME_WINDOW_P (f))
5442 return 0;
5443 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5444 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
5445 else
5446 abort ();
5449 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5450 LFACE_STIPPLE (lface) = Qnil;
5452 /* Realize the face; it must be fully-specified now. */
5453 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5454 check_lface (lface);
5455 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
5456 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5458 #ifdef HAVE_WINDOW_SYSTEM
5459 #ifdef HAVE_X_WINDOWS
5460 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5462 /* This can happen when making a frame on a display that does
5463 not support the default font. */
5464 if (!face->font)
5465 return 0;
5467 /* Otherwise, the font specified for the frame was not
5468 acceptable as a font for the default face (perhaps because
5469 auto-scaled fonts are rejected), so we must adjust the frame
5470 font. */
5471 x_set_font (f, LFACE_FONT (lface), Qnil);
5473 #endif /* HAVE_X_WINDOWS */
5474 #endif /* HAVE_WINDOW_SYSTEM */
5475 return 1;
5479 /* Realize basic faces other than the default face in face cache C.
5480 SYMBOL is the face name, ID is the face id the realized face must
5481 have. The default face must have been realized already. */
5483 static void
5484 realize_named_face (f, symbol, id)
5485 struct frame *f;
5486 Lisp_Object symbol;
5487 int id;
5489 struct face_cache *c = FRAME_FACE_CACHE (f);
5490 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5491 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5492 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5493 struct face *new_face;
5495 /* The default face must exist and be fully specified. */
5496 get_lface_attributes (f, Qdefault, attrs, 1);
5497 check_lface_attrs (attrs);
5498 xassert (lface_fully_specified_p (attrs));
5500 /* If SYMBOL isn't know as a face, create it. */
5501 if (NILP (lface))
5503 Lisp_Object frame;
5504 XSETFRAME (frame, f);
5505 lface = Finternal_make_lisp_face (symbol, frame);
5508 /* Merge SYMBOL's face with the default face. */
5509 get_lface_attributes (f, symbol, symbol_attrs, 1);
5510 merge_face_vectors (f, symbol_attrs, attrs, 0);
5512 /* Realize the face. */
5513 new_face = realize_face (c, attrs, id);
5517 /* Realize the fully-specified face with attributes ATTRS in face
5518 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5519 non-negative, it is an ID of face to remove before caching the new
5520 face. Value is a pointer to the newly created realized face. */
5522 static struct face *
5523 realize_face (cache, attrs, former_face_id)
5524 struct face_cache *cache;
5525 Lisp_Object *attrs;
5526 int former_face_id;
5528 struct face *face;
5530 /* LFACE must be fully specified. */
5531 xassert (cache != NULL);
5532 check_lface_attrs (attrs);
5534 if (former_face_id >= 0 && cache->used > former_face_id)
5536 /* Remove the former face. */
5537 struct face *former_face = cache->faces_by_id[former_face_id];
5538 uncache_face (cache, former_face);
5539 free_realized_face (cache->f, former_face);
5542 if (FRAME_WINDOW_P (cache->f))
5543 face = realize_x_face (cache, attrs);
5544 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5545 face = realize_tty_face (cache, attrs);
5546 else if (FRAME_INITIAL_P (cache->f))
5548 /* Create a dummy face. */
5549 face = make_realized_face (attrs);
5551 else
5552 abort ();
5554 /* Insert the new face. */
5555 cache_face (cache, face, lface_hash (attrs));
5556 return face;
5560 #ifdef HAVE_WINDOW_SYSTEM
5561 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5562 same attributes as BASE_FACE except for the font on frame F.
5563 FONT-OBJECT may be nil, in which case, realized a face of
5564 no-font. */
5566 static struct face *
5567 realize_non_ascii_face (f, font_object, base_face)
5568 struct frame *f;
5569 Lisp_Object font_object;
5570 struct face *base_face;
5572 struct face_cache *cache = FRAME_FACE_CACHE (f);
5573 struct face *face;
5575 face = (struct face *) xmalloc (sizeof *face);
5576 *face = *base_face;
5577 face->gc = 0;
5578 face->extra = NULL;
5579 face->overstrike
5580 = (! NILP (font_object)
5581 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5582 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5584 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5585 face->colors_copied_bitwise_p = 1;
5586 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5587 face->gc = 0;
5589 cache_face (cache, face, face->hash);
5591 return face;
5593 #endif /* HAVE_WINDOW_SYSTEM */
5596 /* Realize the fully-specified face with attributes ATTRS in face
5597 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5598 the new face doesn't share font with the default face, a fontname
5599 is allocated from the heap and set in `font_name' of the new face,
5600 but it is not yet loaded here. Value is a pointer to the newly
5601 created realized face. */
5603 static struct face *
5604 realize_x_face (cache, attrs)
5605 struct face_cache *cache;
5606 Lisp_Object *attrs;
5608 struct face *face = NULL;
5609 #ifdef HAVE_WINDOW_SYSTEM
5610 struct face *default_face;
5611 struct frame *f;
5612 Lisp_Object stipple, overline, strike_through, box;
5614 xassert (FRAME_WINDOW_P (cache->f));
5616 /* Allocate a new realized face. */
5617 face = make_realized_face (attrs);
5618 face->ascii_face = face;
5620 f = cache->f;
5622 /* Determine the font to use. Most of the time, the font will be
5623 the same as the font of the default face, so try that first. */
5624 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5625 if (default_face
5626 && lface_same_font_attributes_p (default_face->lface, attrs))
5628 face->font = default_face->font;
5629 face->fontset = make_fontset_for_ascii_face (f, -1, face);
5631 else
5633 /* If the face attribute ATTRS specifies a fontset, use it as
5634 the base of a new realized fontset. Otherwise, use the same
5635 base fontset as of the default face. The base determines
5636 registry and encoding of a font. It may also determine
5637 foundry and family. The other fields of font name pattern
5638 are constructed from ATTRS. */
5639 int fontset = face_fontset (attrs);
5641 /* If we are realizing the default face, ATTRS should specify a
5642 fontset. In other words, if FONTSET is -1, we are not
5643 realizing the default face, thus the default face should have
5644 already been realized. */
5645 if (fontset == -1)
5646 fontset = default_face->fontset;
5647 if (fontset == -1)
5648 abort ();
5649 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5650 attrs[LFACE_FONT_INDEX]
5651 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5652 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5654 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5655 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5657 else
5659 face->font = NULL;
5660 face->fontset = -1;
5664 if (face->font
5665 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5666 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5667 face->overstrike = 1;
5669 /* Load colors, and set remaining attributes. */
5671 load_face_colors (f, face, attrs);
5673 /* Set up box. */
5674 box = attrs[LFACE_BOX_INDEX];
5675 if (STRINGP (box))
5677 /* A simple box of line width 1 drawn in color given by
5678 the string. */
5679 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5680 LFACE_BOX_INDEX);
5681 face->box = FACE_SIMPLE_BOX;
5682 face->box_line_width = 1;
5684 else if (INTEGERP (box))
5686 /* Simple box of specified line width in foreground color of the
5687 face. */
5688 xassert (XINT (box) != 0);
5689 face->box = FACE_SIMPLE_BOX;
5690 face->box_line_width = XINT (box);
5691 face->box_color = face->foreground;
5692 face->box_color_defaulted_p = 1;
5694 else if (CONSP (box))
5696 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5697 being one of `raised' or `sunken'. */
5698 face->box = FACE_SIMPLE_BOX;
5699 face->box_color = face->foreground;
5700 face->box_color_defaulted_p = 1;
5701 face->box_line_width = 1;
5703 while (CONSP (box))
5705 Lisp_Object keyword, value;
5707 keyword = XCAR (box);
5708 box = XCDR (box);
5710 if (!CONSP (box))
5711 break;
5712 value = XCAR (box);
5713 box = XCDR (box);
5715 if (EQ (keyword, QCline_width))
5717 if (INTEGERP (value) && XINT (value) != 0)
5718 face->box_line_width = XINT (value);
5720 else if (EQ (keyword, QCcolor))
5722 if (STRINGP (value))
5724 face->box_color = load_color (f, face, value,
5725 LFACE_BOX_INDEX);
5726 face->use_box_color_for_shadows_p = 1;
5729 else if (EQ (keyword, QCstyle))
5731 if (EQ (value, Qreleased_button))
5732 face->box = FACE_RAISED_BOX;
5733 else if (EQ (value, Qpressed_button))
5734 face->box = FACE_SUNKEN_BOX;
5739 /* Text underline, overline, strike-through. */
5741 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5743 /* Use default color (same as foreground color). */
5744 face->underline_p = 1;
5745 face->underline_defaulted_p = 1;
5746 face->underline_color = 0;
5748 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
5750 /* Use specified color. */
5751 face->underline_p = 1;
5752 face->underline_defaulted_p = 0;
5753 face->underline_color
5754 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5755 LFACE_UNDERLINE_INDEX);
5757 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
5759 face->underline_p = 0;
5760 face->underline_defaulted_p = 0;
5761 face->underline_color = 0;
5764 overline = attrs[LFACE_OVERLINE_INDEX];
5765 if (STRINGP (overline))
5767 face->overline_color
5768 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5769 LFACE_OVERLINE_INDEX);
5770 face->overline_p = 1;
5772 else if (EQ (overline, Qt))
5774 face->overline_color = face->foreground;
5775 face->overline_color_defaulted_p = 1;
5776 face->overline_p = 1;
5779 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5780 if (STRINGP (strike_through))
5782 face->strike_through_color
5783 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5784 LFACE_STRIKE_THROUGH_INDEX);
5785 face->strike_through_p = 1;
5787 else if (EQ (strike_through, Qt))
5789 face->strike_through_color = face->foreground;
5790 face->strike_through_color_defaulted_p = 1;
5791 face->strike_through_p = 1;
5794 stipple = attrs[LFACE_STIPPLE_INDEX];
5795 if (!NILP (stipple))
5796 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5797 #endif /* HAVE_WINDOW_SYSTEM */
5799 return face;
5803 /* Map a specified color of face FACE on frame F to a tty color index.
5804 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5805 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5806 default foreground/background colors. */
5808 static void
5809 map_tty_color (f, face, idx, defaulted)
5810 struct frame *f;
5811 struct face *face;
5812 enum lface_attribute_index idx;
5813 int *defaulted;
5815 Lisp_Object frame, color, def;
5816 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5817 unsigned long default_pixel, default_other_pixel, pixel;
5819 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5821 if (foreground_p)
5823 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5824 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5826 else
5828 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5829 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5832 XSETFRAME (frame, f);
5833 color = face->lface[idx];
5835 if (STRINGP (color)
5836 && SCHARS (color)
5837 && CONSP (Vtty_defined_color_alist)
5838 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5839 CONSP (def)))
5841 /* Associations in tty-defined-color-alist are of the form
5842 (NAME INDEX R G B). We need the INDEX part. */
5843 pixel = XINT (XCAR (XCDR (def)));
5846 if (pixel == default_pixel && STRINGP (color))
5848 pixel = load_color (f, face, color, idx);
5850 #ifdef MSDOS
5851 /* If the foreground of the default face is the default color,
5852 use the foreground color defined by the frame. */
5853 if (FRAME_MSDOS_P (f))
5855 if (pixel == default_pixel
5856 || pixel == FACE_TTY_DEFAULT_COLOR)
5858 if (foreground_p)
5859 pixel = FRAME_FOREGROUND_PIXEL (f);
5860 else
5861 pixel = FRAME_BACKGROUND_PIXEL (f);
5862 face->lface[idx] = tty_color_name (f, pixel);
5863 *defaulted = 1;
5865 else if (pixel == default_other_pixel)
5867 if (foreground_p)
5868 pixel = FRAME_BACKGROUND_PIXEL (f);
5869 else
5870 pixel = FRAME_FOREGROUND_PIXEL (f);
5871 face->lface[idx] = tty_color_name (f, pixel);
5872 *defaulted = 1;
5875 #endif /* MSDOS */
5878 if (foreground_p)
5879 face->foreground = pixel;
5880 else
5881 face->background = pixel;
5885 /* Realize the fully-specified face with attributes ATTRS in face
5886 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5887 Value is a pointer to the newly created realized face. */
5889 static struct face *
5890 realize_tty_face (cache, attrs)
5891 struct face_cache *cache;
5892 Lisp_Object *attrs;
5894 struct face *face;
5895 int weight, slant;
5896 int face_colors_defaulted = 0;
5897 struct frame *f = cache->f;
5899 /* Frame must be a termcap frame. */
5900 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5902 /* Allocate a new realized face. */
5903 face = make_realized_face (attrs);
5904 #if 0
5905 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5906 #endif
5908 /* Map face attributes to TTY appearances. We map slant to
5909 dimmed text because we want italic text to appear differently
5910 and because dimmed text is probably used infrequently. */
5911 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5912 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5913 if (weight > 100)
5914 face->tty_bold_p = 1;
5915 if (weight < 100 || slant != 100)
5916 face->tty_dim_p = 1;
5917 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5918 face->tty_underline_p = 1;
5919 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5920 face->tty_reverse_p = 1;
5922 /* Map color names to color indices. */
5923 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5924 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5926 /* Swap colors if face is inverse-video. If the colors are taken
5927 from the frame colors, they are already inverted, since the
5928 frame-creation function calls x-handle-reverse-video. */
5929 if (face->tty_reverse_p && !face_colors_defaulted)
5931 unsigned long tem = face->foreground;
5932 face->foreground = face->background;
5933 face->background = tem;
5936 if (tty_suppress_bold_inverse_default_colors_p
5937 && face->tty_bold_p
5938 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5939 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5940 face->tty_bold_p = 0;
5942 return face;
5946 DEFUN ("tty-suppress-bold-inverse-default-colors",
5947 Ftty_suppress_bold_inverse_default_colors,
5948 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5949 doc: /* Suppress/allow boldness of faces with inverse default colors.
5950 SUPPRESS non-nil means suppress it.
5951 This affects bold faces on TTYs whose foreground is the default background
5952 color of the display and whose background is the default foreground color.
5953 For such faces, the bold face attribute is ignored if this variable
5954 is non-nil. */)
5955 (suppress)
5956 Lisp_Object suppress;
5958 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5959 ++face_change_count;
5960 return suppress;
5965 /***********************************************************************
5966 Computing Faces
5967 ***********************************************************************/
5969 /* Return the ID of the face to use to display character CH with face
5970 property PROP on frame F in current_buffer. */
5973 compute_char_face (f, ch, prop)
5974 struct frame *f;
5975 int ch;
5976 Lisp_Object prop;
5978 int face_id;
5980 if (NILP (current_buffer->enable_multibyte_characters))
5981 ch = 0;
5983 if (NILP (prop))
5985 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5986 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5988 else
5990 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5991 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5992 bcopy (default_face->lface, attrs, sizeof attrs);
5993 merge_face_ref (f, prop, attrs, 1, 0);
5994 face_id = lookup_face (f, attrs);
5997 return face_id;
6000 /* Return the face ID associated with buffer position POS for
6001 displaying ASCII characters. Return in *ENDPTR the position at
6002 which a different face is needed, as far as text properties and
6003 overlays are concerned. W is a window displaying current_buffer.
6005 REGION_BEG, REGION_END delimit the region, so it can be
6006 highlighted.
6008 LIMIT is a position not to scan beyond. That is to limit the time
6009 this function can take.
6011 If MOUSE is non-zero, use the character's mouse-face, not its face.
6013 The face returned is suitable for displaying ASCII characters. */
6016 face_at_buffer_position (w, pos, region_beg, region_end,
6017 endptr, limit, mouse)
6018 struct window *w;
6019 EMACS_INT pos;
6020 EMACS_INT region_beg, region_end;
6021 EMACS_INT *endptr;
6022 EMACS_INT limit;
6023 int mouse;
6025 struct frame *f = XFRAME (w->frame);
6026 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6027 Lisp_Object prop, position;
6028 int i, noverlays;
6029 Lisp_Object *overlay_vec;
6030 Lisp_Object frame;
6031 EMACS_INT endpos;
6032 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6033 Lisp_Object limit1, end;
6034 struct face *default_face;
6036 /* W must display the current buffer. We could write this function
6037 to use the frame and buffer of W, but right now it doesn't. */
6038 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6040 XSETFRAME (frame, f);
6041 XSETFASTINT (position, pos);
6043 endpos = ZV;
6044 if (pos < region_beg && region_beg < endpos)
6045 endpos = region_beg;
6047 /* Get the `face' or `mouse_face' text property at POS, and
6048 determine the next position at which the property changes. */
6049 prop = Fget_text_property (position, propname, w->buffer);
6050 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6051 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6052 if (INTEGERP (end))
6053 endpos = XINT (end);
6055 /* Look at properties from overlays. */
6057 EMACS_INT next_overlay;
6059 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6060 if (next_overlay < endpos)
6061 endpos = next_overlay;
6064 *endptr = endpos;
6066 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6068 /* Optimize common cases where we can use the default face. */
6069 if (noverlays == 0
6070 && NILP (prop)
6071 && !(pos >= region_beg && pos < region_end))
6072 return DEFAULT_FACE_ID;
6074 /* Begin with attributes from the default face. */
6075 bcopy (default_face->lface, attrs, sizeof attrs);
6077 /* Merge in attributes specified via text properties. */
6078 if (!NILP (prop))
6079 merge_face_ref (f, prop, attrs, 1, 0);
6081 /* Now merge the overlay data. */
6082 noverlays = sort_overlays (overlay_vec, noverlays, w);
6083 for (i = 0; i < noverlays; i++)
6085 Lisp_Object oend;
6086 int oendpos;
6088 prop = Foverlay_get (overlay_vec[i], propname);
6089 if (!NILP (prop))
6090 merge_face_ref (f, prop, attrs, 1, 0);
6092 oend = OVERLAY_END (overlay_vec[i]);
6093 oendpos = OVERLAY_POSITION (oend);
6094 if (oendpos < endpos)
6095 endpos = oendpos;
6098 /* If in the region, merge in the region face. */
6099 if (pos >= region_beg && pos < region_end)
6101 merge_named_face (f, Qregion, attrs, 0);
6103 if (region_end < endpos)
6104 endpos = region_end;
6107 *endptr = endpos;
6109 /* Look up a realized face with the given face attributes,
6110 or realize a new one for ASCII characters. */
6111 return lookup_face (f, attrs);
6114 /* Return the face ID at buffer position POS for displaying ASCII
6115 characters associated with overlay strings for overlay OVERLAY.
6117 Like face_at_buffer_position except for OVERLAY. Currently it
6118 simply disregards the `face' properties of all overlays. */
6121 face_for_overlay_string (w, pos, region_beg, region_end,
6122 endptr, limit, mouse, overlay)
6123 struct window *w;
6124 EMACS_INT pos;
6125 EMACS_INT region_beg, region_end;
6126 EMACS_INT *endptr;
6127 EMACS_INT limit;
6128 int mouse;
6129 Lisp_Object overlay;
6131 struct frame *f = XFRAME (w->frame);
6132 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6133 Lisp_Object prop, position;
6134 Lisp_Object frame;
6135 int endpos;
6136 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6137 Lisp_Object limit1, end;
6138 struct face *default_face;
6140 /* W must display the current buffer. We could write this function
6141 to use the frame and buffer of W, but right now it doesn't. */
6142 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6144 XSETFRAME (frame, f);
6145 XSETFASTINT (position, pos);
6147 endpos = ZV;
6148 if (pos < region_beg && region_beg < endpos)
6149 endpos = region_beg;
6151 /* Get the `face' or `mouse_face' text property at POS, and
6152 determine the next position at which the property changes. */
6153 prop = Fget_text_property (position, propname, w->buffer);
6154 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6155 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6156 if (INTEGERP (end))
6157 endpos = XINT (end);
6159 *endptr = endpos;
6161 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6163 /* Optimize common cases where we can use the default face. */
6164 if (NILP (prop)
6165 && !(pos >= region_beg && pos < region_end))
6166 return DEFAULT_FACE_ID;
6168 /* Begin with attributes from the default face. */
6169 bcopy (default_face->lface, attrs, sizeof attrs);
6171 /* Merge in attributes specified via text properties. */
6172 if (!NILP (prop))
6173 merge_face_ref (f, prop, attrs, 1, 0);
6175 /* If in the region, merge in the region face. */
6176 if (pos >= region_beg && pos < region_end)
6178 merge_named_face (f, Qregion, attrs, 0);
6180 if (region_end < endpos)
6181 endpos = region_end;
6184 *endptr = endpos;
6186 /* Look up a realized face with the given face attributes,
6187 or realize a new one for ASCII characters. */
6188 return lookup_face (f, attrs);
6192 /* Compute the face at character position POS in Lisp string STRING on
6193 window W, for ASCII characters.
6195 If STRING is an overlay string, it comes from position BUFPOS in
6196 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6197 not an overlay string. W must display the current buffer.
6198 REGION_BEG and REGION_END give the start and end positions of the
6199 region; both are -1 if no region is visible.
6201 BASE_FACE_ID is the id of a face to merge with. For strings coming
6202 from overlays or the `display' property it is the face at BUFPOS.
6204 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6206 Set *ENDPTR to the next position where to check for faces in
6207 STRING; -1 if the face is constant from POS to the end of the
6208 string.
6210 Value is the id of the face to use. The face returned is suitable
6211 for displaying ASCII characters. */
6214 face_at_string_position (w, string, pos, bufpos, region_beg,
6215 region_end, endptr, base_face_id, mouse_p)
6216 struct window *w;
6217 Lisp_Object string;
6218 EMACS_INT pos, bufpos;
6219 EMACS_INT region_beg, region_end;
6220 EMACS_INT *endptr;
6221 enum face_id base_face_id;
6222 int mouse_p;
6224 Lisp_Object prop, position, end, limit;
6225 struct frame *f = XFRAME (WINDOW_FRAME (w));
6226 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6227 struct face *base_face;
6228 int multibyte_p = STRING_MULTIBYTE (string);
6229 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6231 /* Get the value of the face property at the current position within
6232 STRING. Value is nil if there is no face property. */
6233 XSETFASTINT (position, pos);
6234 prop = Fget_text_property (position, prop_name, string);
6236 /* Get the next position at which to check for faces. Value of end
6237 is nil if face is constant all the way to the end of the string.
6238 Otherwise it is a string position where to check faces next.
6239 Limit is the maximum position up to which to check for property
6240 changes in Fnext_single_property_change. Strings are usually
6241 short, so set the limit to the end of the string. */
6242 XSETFASTINT (limit, SCHARS (string));
6243 end = Fnext_single_property_change (position, prop_name, string, limit);
6244 if (INTEGERP (end))
6245 *endptr = XFASTINT (end);
6246 else
6247 *endptr = -1;
6249 base_face = FACE_FROM_ID (f, base_face_id);
6250 xassert (base_face);
6252 /* Optimize the default case that there is no face property and we
6253 are not in the region. */
6254 if (NILP (prop)
6255 && (base_face_id != DEFAULT_FACE_ID
6256 /* BUFPOS <= 0 means STRING is not an overlay string, so
6257 that the region doesn't have to be taken into account. */
6258 || bufpos <= 0
6259 || bufpos < region_beg
6260 || bufpos >= region_end)
6261 && (multibyte_p
6262 /* We can't realize faces for different charsets differently
6263 if we don't have fonts, so we can stop here if not working
6264 on a window-system frame. */
6265 || !FRAME_WINDOW_P (f)
6266 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
6267 return base_face->id;
6269 /* Begin with attributes from the base face. */
6270 bcopy (base_face->lface, attrs, sizeof attrs);
6272 /* Merge in attributes specified via text properties. */
6273 if (!NILP (prop))
6274 merge_face_ref (f, prop, attrs, 1, 0);
6276 /* If in the region, merge in the region face. */
6277 if (bufpos
6278 && bufpos >= region_beg
6279 && bufpos < region_end)
6280 merge_named_face (f, Qregion, attrs, 0);
6282 /* Look up a realized face with the given face attributes,
6283 or realize a new one for ASCII characters. */
6284 return lookup_face (f, attrs);
6288 /* Merge a face into a realized face.
6290 F is frame where faces are (to be) realized.
6292 FACE_NAME is named face to merge.
6294 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6296 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6298 BASE_FACE_ID is realized face to merge into.
6300 Return new face id.
6304 merge_faces (f, face_name, face_id, base_face_id)
6305 struct frame *f;
6306 Lisp_Object face_name;
6307 int face_id, base_face_id;
6309 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6310 struct face *base_face;
6312 base_face = FACE_FROM_ID (f, base_face_id);
6313 if (!base_face)
6314 return base_face_id;
6316 if (EQ (face_name, Qt))
6318 if (face_id < 0 || face_id >= lface_id_to_name_size)
6319 return base_face_id;
6320 face_name = lface_id_to_name[face_id];
6321 face_id = lookup_derived_face (f, face_name, base_face_id, 1);
6322 if (face_id >= 0)
6323 return face_id;
6324 return base_face_id;
6327 /* Begin with attributes from the base face. */
6328 bcopy (base_face->lface, attrs, sizeof attrs);
6330 if (!NILP (face_name))
6332 if (!merge_named_face (f, face_name, attrs, 0))
6333 return base_face_id;
6335 else
6337 struct face *face;
6338 if (face_id < 0)
6339 return base_face_id;
6340 face = FACE_FROM_ID (f, face_id);
6341 if (!face)
6342 return base_face_id;
6343 merge_face_vectors (f, face->lface, attrs, 0);
6346 /* Look up a realized face with the given face attributes,
6347 or realize a new one for ASCII characters. */
6348 return lookup_face (f, attrs);
6352 /***********************************************************************
6353 Tests
6354 ***********************************************************************/
6356 #if GLYPH_DEBUG
6358 /* Print the contents of the realized face FACE to stderr. */
6360 static void
6361 dump_realized_face (face)
6362 struct face *face;
6364 fprintf (stderr, "ID: %d\n", face->id);
6365 #ifdef HAVE_X_WINDOWS
6366 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6367 #endif
6368 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6369 face->foreground,
6370 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6371 fprintf (stderr, "background: 0x%lx (%s)\n",
6372 face->background,
6373 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6374 if (face->font)
6375 fprintf (stderr, "font_name: %s (%s)\n",
6376 SDATA (face->font->props[FONT_NAME_INDEX]),
6377 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6378 #ifdef HAVE_X_WINDOWS
6379 fprintf (stderr, "font = %p\n", face->font);
6380 #endif
6381 fprintf (stderr, "fontset: %d\n", face->fontset);
6382 fprintf (stderr, "underline: %d (%s)\n",
6383 face->underline_p,
6384 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6385 fprintf (stderr, "hash: %d\n", face->hash);
6389 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6391 Lisp_Object n;
6393 if (NILP (n))
6395 int i;
6397 fprintf (stderr, "font selection order: ");
6398 for (i = 0; i < DIM (font_sort_order); ++i)
6399 fprintf (stderr, "%d ", font_sort_order[i]);
6400 fprintf (stderr, "\n");
6402 fprintf (stderr, "alternative fonts: ");
6403 debug_print (Vface_alternative_font_family_alist);
6404 fprintf (stderr, "\n");
6406 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6407 Fdump_face (make_number (i));
6409 else
6411 struct face *face;
6412 CHECK_NUMBER (n);
6413 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6414 if (face == NULL)
6415 error ("Not a valid face");
6416 dump_realized_face (face);
6419 return Qnil;
6423 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6424 0, 0, 0, doc: /* */)
6427 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6428 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6429 fprintf (stderr, "number of GCs = %d\n", ngcs);
6430 return Qnil;
6433 #endif /* GLYPH_DEBUG != 0 */
6437 /***********************************************************************
6438 Initialization
6439 ***********************************************************************/
6441 void
6442 syms_of_xfaces ()
6444 Qface = intern ("face");
6445 staticpro (&Qface);
6446 Qface_no_inherit = intern ("face-no-inherit");
6447 staticpro (&Qface_no_inherit);
6448 Qbitmap_spec_p = intern ("bitmap-spec-p");
6449 staticpro (&Qbitmap_spec_p);
6450 Qframe_set_background_mode = intern ("frame-set-background-mode");
6451 staticpro (&Qframe_set_background_mode);
6453 /* Lisp face attribute keywords. */
6454 QCfamily = intern (":family");
6455 staticpro (&QCfamily);
6456 QCheight = intern (":height");
6457 staticpro (&QCheight);
6458 QCweight = intern (":weight");
6459 staticpro (&QCweight);
6460 QCslant = intern (":slant");
6461 staticpro (&QCslant);
6462 QCunderline = intern (":underline");
6463 staticpro (&QCunderline);
6464 QCinverse_video = intern (":inverse-video");
6465 staticpro (&QCinverse_video);
6466 QCreverse_video = intern (":reverse-video");
6467 staticpro (&QCreverse_video);
6468 QCforeground = intern (":foreground");
6469 staticpro (&QCforeground);
6470 QCbackground = intern (":background");
6471 staticpro (&QCbackground);
6472 QCstipple = intern (":stipple");
6473 staticpro (&QCstipple);
6474 QCwidth = intern (":width");
6475 staticpro (&QCwidth);
6476 QCfont = intern (":font");
6477 staticpro (&QCfont);
6478 QCfontset = intern (":fontset");
6479 staticpro (&QCfontset);
6480 QCbold = intern (":bold");
6481 staticpro (&QCbold);
6482 QCitalic = intern (":italic");
6483 staticpro (&QCitalic);
6484 QCoverline = intern (":overline");
6485 staticpro (&QCoverline);
6486 QCstrike_through = intern (":strike-through");
6487 staticpro (&QCstrike_through);
6488 QCbox = intern (":box");
6489 staticpro (&QCbox);
6490 QCinherit = intern (":inherit");
6491 staticpro (&QCinherit);
6493 /* Symbols used for Lisp face attribute values. */
6494 QCcolor = intern (":color");
6495 staticpro (&QCcolor);
6496 QCline_width = intern (":line-width");
6497 staticpro (&QCline_width);
6498 QCstyle = intern (":style");
6499 staticpro (&QCstyle);
6500 Qreleased_button = intern ("released-button");
6501 staticpro (&Qreleased_button);
6502 Qpressed_button = intern ("pressed-button");
6503 staticpro (&Qpressed_button);
6504 Qnormal = intern ("normal");
6505 staticpro (&Qnormal);
6506 Qultra_light = intern ("ultra-light");
6507 staticpro (&Qultra_light);
6508 Qextra_light = intern ("extra-light");
6509 staticpro (&Qextra_light);
6510 Qlight = intern ("light");
6511 staticpro (&Qlight);
6512 Qsemi_light = intern ("semi-light");
6513 staticpro (&Qsemi_light);
6514 Qsemi_bold = intern ("semi-bold");
6515 staticpro (&Qsemi_bold);
6516 Qbold = intern ("bold");
6517 staticpro (&Qbold);
6518 Qextra_bold = intern ("extra-bold");
6519 staticpro (&Qextra_bold);
6520 Qultra_bold = intern ("ultra-bold");
6521 staticpro (&Qultra_bold);
6522 Qoblique = intern ("oblique");
6523 staticpro (&Qoblique);
6524 Qitalic = intern ("italic");
6525 staticpro (&Qitalic);
6526 Qreverse_oblique = intern ("reverse-oblique");
6527 staticpro (&Qreverse_oblique);
6528 Qreverse_italic = intern ("reverse-italic");
6529 staticpro (&Qreverse_italic);
6530 Qultra_condensed = intern ("ultra-condensed");
6531 staticpro (&Qultra_condensed);
6532 Qextra_condensed = intern ("extra-condensed");
6533 staticpro (&Qextra_condensed);
6534 Qcondensed = intern ("condensed");
6535 staticpro (&Qcondensed);
6536 Qsemi_condensed = intern ("semi-condensed");
6537 staticpro (&Qsemi_condensed);
6538 Qsemi_expanded = intern ("semi-expanded");
6539 staticpro (&Qsemi_expanded);
6540 Qexpanded = intern ("expanded");
6541 staticpro (&Qexpanded);
6542 Qextra_expanded = intern ("extra-expanded");
6543 staticpro (&Qextra_expanded);
6544 Qultra_expanded = intern ("ultra-expanded");
6545 staticpro (&Qultra_expanded);
6546 Qbackground_color = intern ("background-color");
6547 staticpro (&Qbackground_color);
6548 Qforeground_color = intern ("foreground-color");
6549 staticpro (&Qforeground_color);
6550 Qunspecified = intern ("unspecified");
6551 staticpro (&Qunspecified);
6552 Qignore_defface = intern (":ignore-defface");
6553 staticpro (&Qignore_defface);
6555 Qface_alias = intern ("face-alias");
6556 staticpro (&Qface_alias);
6557 Qdefault = intern ("default");
6558 staticpro (&Qdefault);
6559 Qtool_bar = intern ("tool-bar");
6560 staticpro (&Qtool_bar);
6561 Qregion = intern ("region");
6562 staticpro (&Qregion);
6563 Qfringe = intern ("fringe");
6564 staticpro (&Qfringe);
6565 Qheader_line = intern ("header-line");
6566 staticpro (&Qheader_line);
6567 Qscroll_bar = intern ("scroll-bar");
6568 staticpro (&Qscroll_bar);
6569 Qmenu = intern ("menu");
6570 staticpro (&Qmenu);
6571 Qcursor = intern ("cursor");
6572 staticpro (&Qcursor);
6573 Qborder = intern ("border");
6574 staticpro (&Qborder);
6575 Qmouse = intern ("mouse");
6576 staticpro (&Qmouse);
6577 Qmode_line_inactive = intern ("mode-line-inactive");
6578 staticpro (&Qmode_line_inactive);
6579 Qvertical_border = intern ("vertical-border");
6580 staticpro (&Qvertical_border);
6581 Qtty_color_desc = intern ("tty-color-desc");
6582 staticpro (&Qtty_color_desc);
6583 Qtty_color_standard_values = intern ("tty-color-standard-values");
6584 staticpro (&Qtty_color_standard_values);
6585 Qtty_color_by_index = intern ("tty-color-by-index");
6586 staticpro (&Qtty_color_by_index);
6587 Qtty_color_alist = intern ("tty-color-alist");
6588 staticpro (&Qtty_color_alist);
6589 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
6590 staticpro (&Qscalable_fonts_allowed);
6592 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
6593 staticpro (&Vparam_value_alist);
6594 Vface_alternative_font_family_alist = Qnil;
6595 staticpro (&Vface_alternative_font_family_alist);
6596 Vface_alternative_font_registry_alist = Qnil;
6597 staticpro (&Vface_alternative_font_registry_alist);
6599 defsubr (&Sinternal_make_lisp_face);
6600 defsubr (&Sinternal_lisp_face_p);
6601 defsubr (&Sinternal_set_lisp_face_attribute);
6602 #ifdef HAVE_WINDOW_SYSTEM
6603 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6604 #endif
6605 defsubr (&Scolor_gray_p);
6606 defsubr (&Scolor_supported_p);
6607 defsubr (&Sface_attribute_relative_p);
6608 defsubr (&Smerge_face_attribute);
6609 defsubr (&Sinternal_get_lisp_face_attribute);
6610 defsubr (&Sinternal_lisp_face_attribute_values);
6611 defsubr (&Sinternal_lisp_face_equal_p);
6612 defsubr (&Sinternal_lisp_face_empty_p);
6613 defsubr (&Sinternal_copy_lisp_face);
6614 defsubr (&Sinternal_merge_in_global_face);
6615 defsubr (&Sface_font);
6616 defsubr (&Sframe_face_alist);
6617 defsubr (&Sdisplay_supports_face_attributes_p);
6618 defsubr (&Scolor_distance);
6619 defsubr (&Sinternal_set_font_selection_order);
6620 defsubr (&Sinternal_set_alternative_font_family_alist);
6621 defsubr (&Sinternal_set_alternative_font_registry_alist);
6622 defsubr (&Sface_attributes_as_vector);
6623 #if GLYPH_DEBUG
6624 defsubr (&Sdump_face);
6625 defsubr (&Sshow_face_resources);
6626 #endif /* GLYPH_DEBUG */
6627 defsubr (&Sclear_face_cache);
6628 defsubr (&Stty_suppress_bold_inverse_default_colors);
6630 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6631 defsubr (&Sdump_colors);
6632 #endif
6634 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
6635 doc: /* *Limit for font matching.
6636 If an integer > 0, font matching functions won't load more than
6637 that number of fonts when searching for a matching font. */);
6638 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
6640 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
6641 doc: /* List of global face definitions (for internal use only.) */);
6642 Vface_new_frame_defaults = Qnil;
6644 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
6645 doc: /* *Default stipple pattern used on monochrome displays.
6646 This stipple pattern is used on monochrome displays
6647 instead of shades of gray for a face background color.
6648 See `set-face-stipple' for possible values for this variable. */);
6649 Vface_default_stipple = build_string ("gray3");
6651 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
6652 doc: /* An alist of defined terminal colors and their RGB values. */);
6653 Vtty_defined_color_alist = Qnil;
6655 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
6656 doc: /* Allowed scalable fonts.
6657 A value of nil means don't allow any scalable fonts.
6658 A value of t means allow any scalable font.
6659 Otherwise, value must be a list of regular expressions. A font may be
6660 scaled if its name matches a regular expression in the list.
6661 Note that if value is nil, a scalable font might still be used, if no
6662 other font of the appropriate family and registry is available. */);
6663 Vscalable_fonts_allowed = Qnil;
6665 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
6666 doc: /* List of ignored fonts.
6667 Each element is a regular expression that matches names of fonts to
6668 ignore. */);
6669 Vface_ignored_fonts = Qnil;
6671 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
6672 doc: /* Alist of fonts vs the rescaling factors.
6673 Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
6674 FONT-NAME-PATTERN is a regular expression matching a font name, and
6675 RESCALE-RATIO is a floating point number to specify how much larger
6676 \(or smaller) font we should use. For instance, if a face requests
6677 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6678 Vface_font_rescale_alist = Qnil;
6680 #ifdef HAVE_WINDOW_SYSTEM
6681 defsubr (&Sbitmap_spec_p);
6682 defsubr (&Sx_list_fonts);
6683 defsubr (&Sinternal_face_x_get_resource);
6684 defsubr (&Sx_family_fonts);
6685 defsubr (&Sx_font_family_list);
6686 #endif /* HAVE_WINDOW_SYSTEM */
6689 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
6690 (do not change this comment) */