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