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