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