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