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