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