(spam group): Add :version.
[emacs.git] / src / xfaces.c
blob8f604e33cce82d948c7345d181086c4a4c932b87
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation.
5 This file is part of GNU Emacs.
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 2, or (at your option)
10 any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
24 /* Faces.
26 When using Emacs with X, the display style of characters can be
27 changed by defining `faces'. Each face can specify the following
28 display attributes:
30 1. Font family name.
32 2. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 3. Font height in 1/10pt.
37 4. Font weight, e.g. `bold'.
39 5. Font slant, e.g. `italic'.
41 6. Foreground color.
43 7. Background color.
45 8. Whether or not characters should be underlined, and in what color.
47 9. Whether or not characters should be displayed in inverse video.
49 10. A background stipple, a bitmap.
51 11. Whether or not characters should be overlined, and in what color.
53 12. Whether or not characters should be strike-through, and in what
54 color.
56 13. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 14. Font or fontset pattern, or nil. This is a special attribute.
60 When this attribute is specified, the face uses a font opened by
61 that pattern as is. In addition, all the other font-related
62 attributes (1st thru 5th) are generated from the opened font name.
63 On the other hand, if one of the other font-related attributes are
64 specified, this attribute is set to nil. In that case, the face
65 doesn't inherit this attribute from the `default' face, and uses a
66 font determined by the other attributes (those may be inherited
67 from the `default' face).
69 15. A face name or list of face names from which to inherit attributes.
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.
75 Faces are frame-local by nature because Emacs allows to define the
76 same named face (face names are symbols) differently for different
77 frames. Each frame has an alist of face definitions for all named
78 faces. The value of a named face in such an alist is a Lisp vector
79 with the symbol `face' in slot 0, and a slot for each of the face
80 attributes mentioned above.
82 There is also a global face alist `Vface_new_frame_defaults'. Face
83 definitions from this list are used to initialize faces of newly
84 created frames.
86 A face doesn't have to specify all attributes. Those not specified
87 have a value of `unspecified'. Faces specifying all attributes but
88 the 14th are called `fully-specified'.
91 Face merging.
93 The display style of a given character in the text is determined by
94 combining several faces. This process is called `face merging'.
95 Any aspect of the display style that isn't specified by overlays or
96 text properties is taken from the `default' face. Since it is made
97 sure that the default face is always fully-specified, face merging
98 always results in a fully-specified face.
101 Face realization.
103 After all face attributes for a character have been determined by
104 merging faces of that character, that face is `realized'. The
105 realization process maps face attributes to what is physically
106 available on the system where Emacs runs. The result is a
107 `realized face' in form of a struct face which is stored in the
108 face cache of the frame on which it was realized.
110 Face realization is done in the context of the character to display
111 because different fonts may be used for different characters. In
112 other words, for characters that have different font
113 specifications, different realized faces are needed to display
114 them.
116 Font specification is done by fontsets. See the comment in
117 fontset.c for the details. In the current implementation, all ASCII
118 characters share the same font in a fontset.
120 Faces are at first realized for ASCII characters, and, at that
121 time, assigned a specific realized fontset. Hereafter, we call
122 such a face as `ASCII face'. When a face for a multibyte character
123 is realized, it inherits (thus shares) a fontset of an ASCII face
124 that has the same attributes other than font-related ones.
126 Thus, all realized face have a realized fontset.
129 Unibyte text.
131 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
132 font as ASCII characters. That is because it is expected that
133 unibyte text users specify a font that is suitable both for ASCII
134 and raw 8-bit characters.
137 Font selection.
139 Font selection tries to find the best available matching font for a
140 given (character, face) combination.
142 If the face specifies a fontset name, that fontset determines a
143 pattern for fonts of the given character. If the face specifies a
144 font name or the other font-related attributes, a fontset is
145 realized from the default fontset. In that case, that
146 specification determines a pattern for ASCII characters and the
147 default fontset determines a pattern for multibyte characters.
149 Available fonts on the system on which Emacs runs are then matched
150 against the font pattern. The result of font selection is the best
151 match for the given face attributes in this font list.
153 Font selection can be influenced by the user.
155 1. The user can specify the relative importance he gives the face
156 attributes width, height, weight, and slant by setting
157 face-font-selection-order (faces.el) to a list of face attribute
158 names. The default is '(:width :height :weight :slant), and means
159 that font selection first tries to find a good match for the font
160 width specified by a face, then---within fonts with that
161 width---tries to find a best match for the specified font height,
162 etc.
164 2. Setting face-font-family-alternatives allows the user to
165 specify alternative font families to try if a family specified by a
166 face doesn't exist.
168 3. Setting face-font-registry-alternatives allows the user to
169 specify all alternative font registries to try for a face
170 specifying a registry.
172 4. Setting face-ignored-fonts allows the user to ignore specific
173 fonts.
176 Character composition.
178 Usually, the realization process is already finished when Emacs
179 actually reflects the desired glyph matrix on the screen. However,
180 on displaying a composition (sequence of characters to be composed
181 on the screen), a suitable font for the components of the
182 composition is selected and realized while drawing them on the
183 screen, i.e. the realization process is delayed but in principle
184 the same.
187 Initialization of basic faces.
189 The faces `default', `modeline' are considered `basic faces'.
190 When redisplay happens the first time for a newly created frame,
191 basic faces are realized for CHARSET_ASCII. Frame parameters are
192 used to fill in unspecified attributes of the default face. */
194 #include <config.h>
195 #include <stdio.h>
196 #include <sys/types.h>
197 #include <sys/stat.h>
199 #include "lisp.h"
200 #include "charset.h"
201 #include "keyboard.h"
202 #include "frame.h"
204 #ifdef HAVE_WINDOW_SYSTEM
205 #include "fontset.h"
206 #endif /* HAVE_WINDOW_SYSTEM */
208 #ifdef HAVE_X_WINDOWS
209 #include "xterm.h"
210 #ifdef USE_MOTIF
211 #include <Xm/Xm.h>
212 #include <Xm/XmStrDefs.h>
213 #endif /* USE_MOTIF */
214 #endif /* HAVE_X_WINDOWS */
216 #ifdef MSDOS
217 #include "dosfns.h"
218 #endif
220 #ifdef WINDOWSNT
221 #include "w32term.h"
222 #include "fontset.h"
223 /* Redefine X specifics to W32 equivalents to avoid cluttering the
224 code with #ifdef blocks. */
225 #undef FRAME_X_DISPLAY_INFO
226 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
227 #define x_display_info w32_display_info
228 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
229 #define check_x check_w32
230 #define x_list_fonts w32_list_fonts
231 #define GCGraphicsExposures 0
232 #endif /* WINDOWSNT */
234 #ifdef MAC_OS
235 #include "macterm.h"
236 #define x_display_info mac_display_info
237 #define check_x check_mac
238 #endif /* MAC_OS */
240 #include "buffer.h"
241 #include "dispextern.h"
242 #include "blockinput.h"
243 #include "window.h"
244 #include "intervals.h"
246 #ifdef HAVE_X_WINDOWS
248 /* Compensate for a bug in Xos.h on some systems, on which it requires
249 time.h. On some such systems, Xos.h tries to redefine struct
250 timeval and struct timezone if USG is #defined while it is
251 #included. */
253 #ifdef XOS_NEEDS_TIME_H
254 #include <time.h>
255 #undef USG
256 #include <X11/Xos.h>
257 #define USG
258 #define __TIMEVAL__
259 #else /* not XOS_NEEDS_TIME_H */
260 #include <X11/Xos.h>
261 #endif /* not XOS_NEEDS_TIME_H */
263 #endif /* HAVE_X_WINDOWS */
265 #include <ctype.h>
267 #define abs(X) ((X) < 0 ? -(X) : (X))
269 /* Number of pt per inch (from the TeXbook). */
271 #define PT_PER_INCH 72.27
273 /* Non-zero if face attribute ATTR is unspecified. */
275 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
277 /* Value is the number of elements of VECTOR. */
279 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
281 /* Make a copy of string S on the stack using alloca. Value is a pointer
282 to the copy. */
284 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
286 /* Make a copy of the contents of Lisp string S on the stack using
287 alloca. Value is a pointer to the copy. */
289 #define LSTRDUPA(S) STRDUPA (SDATA ((S)))
291 /* Size of hash table of realized faces in face caches (should be a
292 prime number). */
294 #define FACE_CACHE_BUCKETS_SIZE 1001
296 /* Keyword symbols used for face attribute names. */
298 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
299 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
300 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
301 Lisp_Object QCreverse_video;
302 Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
304 /* Symbols used for attribute values. */
306 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
307 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
308 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
309 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
310 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
311 Lisp_Object Qultra_expanded;
312 Lisp_Object Qreleased_button, Qpressed_button;
313 Lisp_Object QCstyle, QCcolor, QCline_width;
314 Lisp_Object Qunspecified;
316 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
318 /* The name of the function to call when the background of the frame
319 has changed, frame_update_face_colors. */
321 Lisp_Object Qframe_update_face_colors;
323 /* Names of basic faces. */
325 Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
326 Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
327 Lisp_Object Qmode_line_inactive;
328 extern Lisp_Object Qmode_line;
330 /* The symbol `face-alias'. A symbols having that property is an
331 alias for another face. Value of the property is the name of
332 the aliased face. */
334 Lisp_Object Qface_alias;
336 /* Default stipple pattern used on monochrome displays. This stipple
337 pattern is used on monochrome displays instead of shades of gray
338 for a face background color. See `set-face-stipple' for possible
339 values for this variable. */
341 Lisp_Object Vface_default_stipple;
343 /* Alist of alternative font families. Each element is of the form
344 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
345 try FAMILY1, then FAMILY2, ... */
347 Lisp_Object Vface_alternative_font_family_alist;
349 /* Alist of alternative font registries. Each element is of the form
350 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
351 loaded, try REGISTRY1, then REGISTRY2, ... */
353 Lisp_Object Vface_alternative_font_registry_alist;
355 /* Allowed scalable fonts. A value of nil means don't allow any
356 scalable fonts. A value of t means allow the use of any scalable
357 font. Otherwise, value must be a list of regular expressions. A
358 font may be scaled if its name matches a regular expression in the
359 list. */
361 Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
363 /* List of regular expressions that matches names of fonts to ignore. */
365 Lisp_Object Vface_ignored_fonts;
367 /* Alist of font name patterns vs the rescaling factor. */
369 Lisp_Object Vface_font_rescale_alist;
371 /* Maximum number of fonts to consider in font_list. If not an
372 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
374 Lisp_Object Vfont_list_limit;
375 #define DEFAULT_FONT_LIST_LIMIT 100
377 /* The symbols `foreground-color' and `background-color' which can be
378 used as part of a `face' property. This is for compatibility with
379 Emacs 20.2. */
381 Lisp_Object Qforeground_color, Qbackground_color;
383 /* The symbols `face' and `mouse-face' used as text properties. */
385 Lisp_Object Qface;
386 extern Lisp_Object Qmouse_face;
388 /* Property for basic faces which other faces cannot inherit. */
390 Lisp_Object Qface_no_inherit;
392 /* Error symbol for wrong_type_argument in load_pixmap. */
394 Lisp_Object Qbitmap_spec_p;
396 /* Alist of global face definitions. Each element is of the form
397 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
398 is a Lisp vector of face attributes. These faces are used
399 to initialize faces for new frames. */
401 Lisp_Object Vface_new_frame_defaults;
403 /* The next ID to assign to Lisp faces. */
405 static int next_lface_id;
407 /* A vector mapping Lisp face Id's to face names. */
409 static Lisp_Object *lface_id_to_name;
410 static int lface_id_to_name_size;
412 /* TTY color-related functions (defined in tty-colors.el). */
414 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
416 /* The name of the function used to compute colors on TTYs. */
418 Lisp_Object Qtty_color_alist;
420 /* An alist of defined terminal colors and their RGB values. */
422 Lisp_Object Vtty_defined_color_alist;
424 /* Counter for calls to clear_face_cache. If this counter reaches
425 CLEAR_FONT_TABLE_COUNT, and a frame has more than
426 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
428 static int clear_font_table_count;
429 #define CLEAR_FONT_TABLE_COUNT 100
430 #define CLEAR_FONT_TABLE_NFONTS 10
432 /* Non-zero means face attributes have been changed since the last
433 redisplay. Used in redisplay_internal. */
435 int face_change_count;
437 /* Non-zero means don't display bold text if a face's foreground
438 and background colors are the inverse of the default colors of the
439 display. This is a kluge to suppress `bold black' foreground text
440 which is hard to read on an LCD monitor. */
442 int tty_suppress_bold_inverse_default_colors_p;
444 /* A list of the form `((x . y))' used to avoid consing in
445 Finternal_set_lisp_face_attribute. */
447 static Lisp_Object Vparam_value_alist;
449 /* The total number of colors currently allocated. */
451 #if GLYPH_DEBUG
452 static int ncolors_allocated;
453 static int npixmaps_allocated;
454 static int ngcs;
455 #endif
457 /* Non-zero means the definition of the `menu' face for new frames has
458 been changed. */
460 int menu_face_changed_default;
463 /* Function prototypes. */
465 struct font_name;
466 struct table_entry;
467 struct named_merge_point;
469 static void map_tty_color P_ ((struct frame *, struct face *,
470 enum lface_attribute_index, int *));
471 static Lisp_Object resolve_face_name P_ ((Lisp_Object));
472 static int may_use_scalable_font_p P_ ((const char *));
473 static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
474 static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
475 int, int));
476 static int x_face_list_fonts P_ ((struct frame *, char *,
477 struct font_name **, int, int));
478 static int font_scalable_p P_ ((struct font_name *));
479 static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
480 static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
481 static unsigned char *xstrlwr P_ ((unsigned char *));
482 static void signal_error P_ ((char *, Lisp_Object));
483 static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
484 static void load_face_font P_ ((struct frame *, struct face *, int));
485 static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
486 static void free_face_colors P_ ((struct frame *, struct face *));
487 static int face_color_gray_p P_ ((struct frame *, char *));
488 static char *build_font_name P_ ((struct font_name *));
489 static void free_font_names P_ ((struct font_name *, int));
490 static int sorted_font_list P_ ((struct frame *, char *,
491 int (*cmpfn) P_ ((const void *, const void *)),
492 struct font_name **));
493 static int font_list_1 P_ ((struct frame *, Lisp_Object, Lisp_Object,
494 Lisp_Object, struct font_name **));
495 static int font_list P_ ((struct frame *, Lisp_Object, Lisp_Object,
496 Lisp_Object, struct font_name **));
497 static int try_font_list P_ ((struct frame *, Lisp_Object *,
498 Lisp_Object, Lisp_Object, struct font_name **,
499 int));
500 static int try_alternative_families P_ ((struct frame *f, Lisp_Object,
501 Lisp_Object, struct font_name **));
502 static int cmp_font_names P_ ((const void *, const void *));
503 static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *, int,
504 struct face *, int));
505 static struct face *realize_x_face P_ ((struct face_cache *,
506 Lisp_Object *, int, struct face *));
507 static struct face *realize_tty_face P_ ((struct face_cache *,
508 Lisp_Object *, int));
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 free_realized_face P_ ((struct frame *, struct face *));
519 static void clear_face_gcs P_ ((struct face_cache *));
520 static void free_face_cache P_ ((struct face_cache *));
521 static int face_numeric_weight P_ ((Lisp_Object));
522 static int face_numeric_slant P_ ((Lisp_Object));
523 static int face_numeric_swidth P_ ((Lisp_Object));
524 static int face_fontset P_ ((Lisp_Object *));
525 static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int, int, int*));
526 static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*,
527 struct named_merge_point *));
528 static int merge_face_ref P_ ((struct frame *, Lisp_Object, Lisp_Object *,
529 int, struct named_merge_point *));
530 static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object,
531 Lisp_Object, int, int));
532 static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
533 static struct face *make_realized_face P_ ((Lisp_Object *));
534 static void free_realized_faces P_ ((struct face_cache *));
535 static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
536 struct font_name *, int, int, int *));
537 static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
538 static void uncache_face P_ ((struct face_cache *, struct face *));
539 static int xlfd_numeric_slant P_ ((struct font_name *));
540 static int xlfd_numeric_weight P_ ((struct font_name *));
541 static int xlfd_numeric_swidth P_ ((struct font_name *));
542 static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
543 static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
544 static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
545 static int xlfd_fixed_p P_ ((struct font_name *));
546 static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
547 int, int));
548 static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
549 struct font_name *, int,
550 Lisp_Object));
551 static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
552 struct font_name *, int));
554 #ifdef HAVE_WINDOW_SYSTEM
556 static int split_font_name P_ ((struct frame *, struct font_name *, int));
557 static int xlfd_point_size P_ ((struct frame *, struct font_name *));
558 static void sort_fonts P_ ((struct frame *, struct font_name *, int,
559 int (*cmpfn) P_ ((const void *, const void *))));
560 static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
561 static void x_free_gc P_ ((struct frame *, GC));
562 static void clear_font_table P_ ((struct x_display_info *));
564 #ifdef WINDOWSNT
565 extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
566 #endif /* WINDOWSNT */
568 #ifdef USE_X_TOOLKIT
569 static void x_update_menu_appearance P_ ((struct frame *));
571 extern void free_frame_menubar P_ ((struct frame *));
572 #endif /* USE_X_TOOLKIT */
574 #endif /* HAVE_WINDOW_SYSTEM */
577 /***********************************************************************
578 Utilities
579 ***********************************************************************/
581 #ifdef HAVE_X_WINDOWS
583 #ifdef DEBUG_X_COLORS
585 /* The following is a poor mans infrastructure for debugging X color
586 allocation problems on displays with PseudoColor-8. Some X servers
587 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
588 color reference counts completely so that they don't signal an
589 error when a color is freed whose reference count is already 0.
590 Other X servers do. To help me debug this, the following code
591 implements a simple reference counting schema of its own, for a
592 single display/screen. --gerd. */
594 /* Reference counts for pixel colors. */
596 int color_count[256];
598 /* Register color PIXEL as allocated. */
600 void
601 register_color (pixel)
602 unsigned long pixel;
604 xassert (pixel < 256);
605 ++color_count[pixel];
609 /* Register color PIXEL as deallocated. */
611 void
612 unregister_color (pixel)
613 unsigned long pixel;
615 xassert (pixel < 256);
616 if (color_count[pixel] > 0)
617 --color_count[pixel];
618 else
619 abort ();
623 /* Register N colors from PIXELS as deallocated. */
625 void
626 unregister_colors (pixels, n)
627 unsigned long *pixels;
628 int n;
630 int i;
631 for (i = 0; i < n; ++i)
632 unregister_color (pixels[i]);
636 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
637 doc: /* Dump currently allocated colors to stderr. */)
640 int i, n;
642 fputc ('\n', stderr);
644 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
645 if (color_count[i])
647 fprintf (stderr, "%3d: %5d", i, color_count[i]);
648 ++n;
649 if (n % 5 == 0)
650 fputc ('\n', stderr);
651 else
652 fputc ('\t', stderr);
655 if (n % 5 != 0)
656 fputc ('\n', stderr);
657 return Qnil;
660 #endif /* DEBUG_X_COLORS */
663 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
664 color values. Interrupt input must be blocked when this function
665 is called. */
667 void
668 x_free_colors (f, pixels, npixels)
669 struct frame *f;
670 unsigned long *pixels;
671 int npixels;
673 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
675 /* If display has an immutable color map, freeing colors is not
676 necessary and some servers don't allow it. So don't do it. */
677 if (class != StaticColor && class != StaticGray && class != TrueColor)
679 #ifdef DEBUG_X_COLORS
680 unregister_colors (pixels, npixels);
681 #endif
682 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
683 pixels, npixels, 0);
688 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
689 color values. Interrupt input must be blocked when this function
690 is called. */
692 void
693 x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
694 Display *dpy;
695 Screen *screen;
696 Colormap cmap;
697 unsigned long *pixels;
698 int npixels;
700 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
701 int class = dpyinfo->visual->class;
703 /* If display has an immutable color map, freeing colors is not
704 necessary and some servers don't allow it. So don't do it. */
705 if (class != StaticColor && class != StaticGray && class != TrueColor)
707 #ifdef DEBUG_X_COLORS
708 unregister_colors (pixels, npixels);
709 #endif
710 XFreeColors (dpy, cmap, pixels, npixels, 0);
715 /* Create and return a GC for use on frame F. GC values and mask
716 are given by XGCV and MASK. */
718 static INLINE GC
719 x_create_gc (f, mask, xgcv)
720 struct frame *f;
721 unsigned long mask;
722 XGCValues *xgcv;
724 GC gc;
725 BLOCK_INPUT;
726 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
727 UNBLOCK_INPUT;
728 IF_DEBUG (++ngcs);
729 return gc;
733 /* Free GC which was used on frame F. */
735 static INLINE void
736 x_free_gc (f, gc)
737 struct frame *f;
738 GC gc;
740 BLOCK_INPUT;
741 xassert (--ngcs >= 0);
742 XFreeGC (FRAME_X_DISPLAY (f), gc);
743 UNBLOCK_INPUT;
746 #endif /* HAVE_X_WINDOWS */
748 #ifdef WINDOWSNT
749 /* W32 emulation of GCs */
751 static INLINE GC
752 x_create_gc (f, mask, xgcv)
753 struct frame *f;
754 unsigned long mask;
755 XGCValues *xgcv;
757 GC gc;
758 BLOCK_INPUT;
759 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
760 UNBLOCK_INPUT;
761 IF_DEBUG (++ngcs);
762 return gc;
766 /* Free GC which was used on frame F. */
768 static INLINE void
769 x_free_gc (f, gc)
770 struct frame *f;
771 GC gc;
773 BLOCK_INPUT;
774 xassert (--ngcs >= 0);
775 xfree (gc);
776 UNBLOCK_INPUT;
779 #endif /* WINDOWSNT */
781 #ifdef MAC_OS
782 /* Mac OS emulation of GCs */
784 extern XGCValues *XCreateGC (void *, Window, unsigned long, XGCValues *);
786 static INLINE GC
787 x_create_gc (f, mask, xgcv)
788 struct frame *f;
789 unsigned long mask;
790 XGCValues *xgcv;
792 GC gc;
793 gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
794 return gc;
797 static INLINE void
798 x_free_gc (f, gc)
799 struct frame *f;
800 GC gc;
802 XFreeGC (FRAME_MAC_DISPLAY (f), gc);
805 #endif /* MAC_OS */
807 /* Like stricmp. Used to compare parts of font names which are in
808 ISO8859-1. */
811 xstricmp (s1, s2)
812 const unsigned char *s1, *s2;
814 while (*s1 && *s2)
816 unsigned char c1 = tolower (*s1);
817 unsigned char c2 = tolower (*s2);
818 if (c1 != c2)
819 return c1 < c2 ? -1 : 1;
820 ++s1, ++s2;
823 if (*s1 == 0)
824 return *s2 == 0 ? 0 : -1;
825 return 1;
829 /* Like strlwr, which might not always be available. */
831 static unsigned char *
832 xstrlwr (s)
833 unsigned char *s;
835 unsigned char *p = s;
837 for (p = s; *p; ++p)
838 *p = tolower (*p);
840 return s;
844 /* Signal `error' with message S, and additional argument ARG. */
846 static void
847 signal_error (s, arg)
848 char *s;
849 Lisp_Object arg;
851 Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
855 /* If FRAME is nil, return a pointer to the selected frame.
856 Otherwise, check that FRAME is a live frame, and return a pointer
857 to it. NPARAM is the parameter number of FRAME, for
858 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
859 Lisp function definitions. */
861 static INLINE struct frame *
862 frame_or_selected_frame (frame, nparam)
863 Lisp_Object frame;
864 int nparam;
866 if (NILP (frame))
867 frame = selected_frame;
869 CHECK_LIVE_FRAME (frame);
870 return XFRAME (frame);
874 /***********************************************************************
875 Frames and faces
876 ***********************************************************************/
878 /* Initialize face cache and basic faces for frame F. */
880 void
881 init_frame_faces (f)
882 struct frame *f;
884 /* Make a face cache, if F doesn't have one. */
885 if (FRAME_FACE_CACHE (f) == NULL)
886 FRAME_FACE_CACHE (f) = make_face_cache (f);
888 #ifdef HAVE_WINDOW_SYSTEM
889 /* Make the image cache. */
890 if (FRAME_WINDOW_P (f))
892 if (FRAME_X_IMAGE_CACHE (f) == NULL)
893 FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
894 ++FRAME_X_IMAGE_CACHE (f)->refcount;
896 #endif /* HAVE_WINDOW_SYSTEM */
898 /* Realize basic faces. Must have enough information in frame
899 parameters to realize basic faces at this point. */
900 #ifdef HAVE_X_WINDOWS
901 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
902 #endif
903 #ifdef WINDOWSNT
904 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
905 #endif
906 #ifdef MAC_OS
907 if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
908 #endif
909 if (!realize_basic_faces (f))
910 abort ();
914 /* Free face cache of frame F. Called from Fdelete_frame. */
916 void
917 free_frame_faces (f)
918 struct frame *f;
920 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
922 if (face_cache)
924 free_face_cache (face_cache);
925 FRAME_FACE_CACHE (f) = NULL;
928 #ifdef HAVE_WINDOW_SYSTEM
929 if (FRAME_WINDOW_P (f))
931 struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
932 if (image_cache)
934 --image_cache->refcount;
935 if (image_cache->refcount == 0)
936 free_image_cache (f);
939 #endif /* HAVE_WINDOW_SYSTEM */
943 /* Clear face caches, and recompute basic faces for frame F. Call
944 this after changing frame parameters on which those faces depend,
945 or when realized faces have been freed due to changing attributes
946 of named faces. */
948 void
949 recompute_basic_faces (f)
950 struct frame *f;
952 if (FRAME_FACE_CACHE (f))
954 clear_face_cache (0);
955 if (!realize_basic_faces (f))
956 abort ();
961 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
962 try to free unused fonts, too. */
964 void
965 clear_face_cache (clear_fonts_p)
966 int clear_fonts_p;
968 #ifdef HAVE_WINDOW_SYSTEM
969 Lisp_Object tail, frame;
970 struct frame *f;
972 if (clear_fonts_p
973 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
975 struct x_display_info *dpyinfo;
977 /* Fonts are common for frames on one display, i.e. on
978 one X screen. */
979 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
980 if (dpyinfo->n_fonts > CLEAR_FONT_TABLE_NFONTS)
981 clear_font_table (dpyinfo);
983 /* From time to time see if we can unload some fonts. This also
984 frees all realized faces on all frames. Fonts needed by
985 faces will be loaded again when faces are realized again. */
986 clear_font_table_count = 0;
988 FOR_EACH_FRAME (tail, frame)
990 struct frame *f = XFRAME (frame);
991 if (FRAME_WINDOW_P (f)
992 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
993 free_all_realized_faces (frame);
996 else
998 /* Clear GCs of realized faces. */
999 FOR_EACH_FRAME (tail, frame)
1001 f = XFRAME (frame);
1002 if (FRAME_WINDOW_P (f))
1004 clear_face_gcs (FRAME_FACE_CACHE (f));
1005 clear_image_cache (f, 0);
1009 #endif /* HAVE_WINDOW_SYSTEM */
1013 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
1014 doc: /* Clear face caches on all frames.
1015 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
1016 (thoroughly)
1017 Lisp_Object thoroughly;
1019 clear_face_cache (!NILP (thoroughly));
1020 ++face_change_count;
1021 ++windows_or_buffers_changed;
1022 return Qnil;
1027 #ifdef HAVE_WINDOW_SYSTEM
1030 /* Remove fonts from the font table of DPYINFO except for the default
1031 ASCII fonts of frames on that display. Called from clear_face_cache
1032 from time to time. */
1034 static void
1035 clear_font_table (dpyinfo)
1036 struct x_display_info *dpyinfo;
1038 int i;
1040 /* Free those fonts that are not used by frames on DPYINFO. */
1041 for (i = 0; i < dpyinfo->n_fonts; ++i)
1043 struct font_info *font_info = dpyinfo->font_table + i;
1044 Lisp_Object tail, frame;
1046 /* Check if slot is already free. */
1047 if (font_info->name == NULL)
1048 continue;
1050 /* Don't free a default font of some frame. */
1051 FOR_EACH_FRAME (tail, frame)
1053 struct frame *f = XFRAME (frame);
1054 if (FRAME_WINDOW_P (f)
1055 && font_info->font == FRAME_FONT (f))
1056 break;
1059 if (!NILP (tail))
1060 continue;
1062 /* Free names. */
1063 if (font_info->full_name != font_info->name)
1064 xfree (font_info->full_name);
1065 xfree (font_info->name);
1067 /* Free the font. */
1068 BLOCK_INPUT;
1069 #ifdef HAVE_X_WINDOWS
1070 XFreeFont (dpyinfo->display, font_info->font);
1071 #endif
1072 #ifdef WINDOWSNT
1073 w32_unload_font (dpyinfo, font_info->font);
1074 #endif
1075 UNBLOCK_INPUT;
1077 /* Mark font table slot free. */
1078 font_info->font = NULL;
1079 font_info->name = font_info->full_name = NULL;
1083 #endif /* HAVE_WINDOW_SYSTEM */
1087 /***********************************************************************
1088 X Pixmaps
1089 ***********************************************************************/
1091 #ifdef HAVE_WINDOW_SYSTEM
1093 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
1094 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
1095 A bitmap specification is either a string, a file name, or a list
1096 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
1097 HEIGHT is its height, and DATA is a string containing the bits of
1098 the pixmap. Bits are stored row by row, each row occupies
1099 \(WIDTH + 7)/8 bytes. */)
1100 (object)
1101 Lisp_Object object;
1103 int pixmap_p = 0;
1105 if (STRINGP (object))
1106 /* If OBJECT is a string, it's a file name. */
1107 pixmap_p = 1;
1108 else if (CONSP (object))
1110 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
1111 HEIGHT must be integers > 0, and DATA must be string large
1112 enough to hold a bitmap of the specified size. */
1113 Lisp_Object width, height, data;
1115 height = width = data = Qnil;
1117 if (CONSP (object))
1119 width = XCAR (object);
1120 object = XCDR (object);
1121 if (CONSP (object))
1123 height = XCAR (object);
1124 object = XCDR (object);
1125 if (CONSP (object))
1126 data = XCAR (object);
1130 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1132 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1133 / BITS_PER_CHAR);
1134 if (SBYTES (data) >= bytes_per_row * XINT (height))
1135 pixmap_p = 1;
1139 return pixmap_p ? Qt : Qnil;
1143 /* Load a bitmap according to NAME (which is either a file name or a
1144 pixmap spec) for use on frame F. Value is the bitmap_id (see
1145 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1146 bitmap cannot be loaded, display a message saying so, and return
1147 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1148 if these pointers are not null. */
1150 static int
1151 load_pixmap (f, name, w_ptr, h_ptr)
1152 FRAME_PTR f;
1153 Lisp_Object name;
1154 unsigned int *w_ptr, *h_ptr;
1156 int bitmap_id;
1157 Lisp_Object tem;
1159 if (NILP (name))
1160 return 0;
1162 tem = Fbitmap_spec_p (name);
1163 if (NILP (tem))
1164 wrong_type_argument (Qbitmap_spec_p, name);
1166 BLOCK_INPUT;
1167 if (CONSP (name))
1169 /* Decode a bitmap spec into a bitmap. */
1171 int h, w;
1172 Lisp_Object bits;
1174 w = XINT (Fcar (name));
1175 h = XINT (Fcar (Fcdr (name)));
1176 bits = Fcar (Fcdr (Fcdr (name)));
1178 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1179 w, h);
1181 else
1183 /* It must be a string -- a file name. */
1184 bitmap_id = x_create_bitmap_from_file (f, name);
1186 UNBLOCK_INPUT;
1188 if (bitmap_id < 0)
1190 add_to_log ("Invalid or undefined bitmap %s", name, Qnil);
1191 bitmap_id = 0;
1193 if (w_ptr)
1194 *w_ptr = 0;
1195 if (h_ptr)
1196 *h_ptr = 0;
1198 else
1200 #if GLYPH_DEBUG
1201 ++npixmaps_allocated;
1202 #endif
1203 if (w_ptr)
1204 *w_ptr = x_bitmap_width (f, bitmap_id);
1206 if (h_ptr)
1207 *h_ptr = x_bitmap_height (f, bitmap_id);
1210 return bitmap_id;
1213 #endif /* HAVE_WINDOW_SYSTEM */
1217 /***********************************************************************
1218 Fonts
1219 ***********************************************************************/
1221 #ifdef HAVE_WINDOW_SYSTEM
1223 /* Load font of face FACE which is used on frame F to display
1224 character C. The name of the font to load is determined by lface
1225 and fontset of FACE. */
1227 static void
1228 load_face_font (f, face, c)
1229 struct frame *f;
1230 struct face *face;
1231 int c;
1233 struct font_info *font_info = NULL;
1234 char *font_name;
1235 int needs_overstrike;
1237 face->font_info_id = -1;
1238 face->font = NULL;
1240 font_name = choose_face_font (f, face->lface, face->fontset, c,
1241 &needs_overstrike);
1242 if (!font_name)
1243 return;
1245 BLOCK_INPUT;
1246 font_info = FS_LOAD_FACE_FONT (f, c, font_name, face);
1247 UNBLOCK_INPUT;
1249 if (font_info)
1251 face->font_info_id = font_info->font_idx;
1252 face->font = font_info->font;
1253 face->font_name = font_info->full_name;
1254 face->overstrike = needs_overstrike;
1255 if (face->gc)
1257 x_free_gc (f, face->gc);
1258 face->gc = 0;
1261 else
1262 add_to_log ("Unable to load font %s",
1263 build_string (font_name), Qnil);
1264 xfree (font_name);
1267 #endif /* HAVE_WINDOW_SYSTEM */
1271 /***********************************************************************
1272 X Colors
1273 ***********************************************************************/
1275 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1276 RGB_LIST should contain (at least) 3 lisp integers.
1277 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1279 static int
1280 parse_rgb_list (rgb_list, color)
1281 Lisp_Object rgb_list;
1282 XColor *color;
1284 #define PARSE_RGB_LIST_FIELD(field) \
1285 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1287 color->field = XINT (XCAR (rgb_list)); \
1288 rgb_list = XCDR (rgb_list); \
1290 else \
1291 return 0;
1293 PARSE_RGB_LIST_FIELD (red);
1294 PARSE_RGB_LIST_FIELD (green);
1295 PARSE_RGB_LIST_FIELD (blue);
1297 return 1;
1301 /* Lookup on frame F the color described by the lisp string COLOR.
1302 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1303 non-zero, then the `standard' definition of the same color is
1304 returned in it. */
1306 static int
1307 tty_lookup_color (f, color, tty_color, std_color)
1308 struct frame *f;
1309 Lisp_Object color;
1310 XColor *tty_color, *std_color;
1312 Lisp_Object frame, color_desc;
1314 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1315 return 0;
1317 XSETFRAME (frame, f);
1319 color_desc = call2 (Qtty_color_desc, color, frame);
1320 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1322 Lisp_Object rgb;
1324 if (! INTEGERP (XCAR (XCDR (color_desc))))
1325 return 0;
1327 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1329 rgb = XCDR (XCDR (color_desc));
1330 if (! parse_rgb_list (rgb, tty_color))
1331 return 0;
1333 /* Should we fill in STD_COLOR too? */
1334 if (std_color)
1336 /* Default STD_COLOR to the same as TTY_COLOR. */
1337 *std_color = *tty_color;
1339 /* Do a quick check to see if the returned descriptor is
1340 actually _exactly_ equal to COLOR, otherwise we have to
1341 lookup STD_COLOR separately. If it's impossible to lookup
1342 a standard color, we just give up and use TTY_COLOR. */
1343 if ((!STRINGP (XCAR (color_desc))
1344 || NILP (Fstring_equal (color, XCAR (color_desc))))
1345 && !NILP (Ffboundp (Qtty_color_standard_values)))
1347 /* Look up STD_COLOR separately. */
1348 rgb = call1 (Qtty_color_standard_values, color);
1349 if (! parse_rgb_list (rgb, std_color))
1350 return 0;
1354 return 1;
1356 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1357 /* We were called early during startup, and the colors are not
1358 yet set up in tty-defined-color-alist. Don't return a failure
1359 indication, since this produces the annoying "Unable to
1360 load color" messages in the *Messages* buffer. */
1361 return 1;
1362 else
1363 /* tty-color-desc seems to have returned a bad value. */
1364 return 0;
1367 /* A version of defined_color for non-X frames. */
1370 tty_defined_color (f, color_name, color_def, alloc)
1371 struct frame *f;
1372 char *color_name;
1373 XColor *color_def;
1374 int alloc;
1376 int status = 1;
1378 /* Defaults. */
1379 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1380 color_def->red = 0;
1381 color_def->blue = 0;
1382 color_def->green = 0;
1384 if (*color_name)
1385 status = tty_lookup_color (f, build_string (color_name), color_def, 0);
1387 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1389 if (strcmp (color_name, "unspecified-fg") == 0)
1390 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1391 else if (strcmp (color_name, "unspecified-bg") == 0)
1392 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1395 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1396 status = 1;
1398 return status;
1402 /* Decide if color named COLOR_NAME is valid for the display
1403 associated with the frame F; if so, return the rgb values in
1404 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1406 This does the right thing for any type of frame. */
1409 defined_color (f, color_name, color_def, alloc)
1410 struct frame *f;
1411 char *color_name;
1412 XColor *color_def;
1413 int alloc;
1415 if (!FRAME_WINDOW_P (f))
1416 return tty_defined_color (f, color_name, color_def, alloc);
1417 #ifdef HAVE_X_WINDOWS
1418 else if (FRAME_X_P (f))
1419 return x_defined_color (f, color_name, color_def, alloc);
1420 #endif
1421 #ifdef WINDOWSNT
1422 else if (FRAME_W32_P (f))
1423 return w32_defined_color (f, color_name, color_def, alloc);
1424 #endif
1425 #ifdef MAC_OS
1426 else if (FRAME_MAC_P (f))
1427 return mac_defined_color (f, color_name, color_def, alloc);
1428 #endif
1429 else
1430 abort ();
1434 /* Given the index IDX of a tty color on frame F, return its name, a
1435 Lisp string. */
1437 Lisp_Object
1438 tty_color_name (f, idx)
1439 struct frame *f;
1440 int idx;
1442 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1444 Lisp_Object frame;
1445 Lisp_Object coldesc;
1447 XSETFRAME (frame, f);
1448 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1450 if (!NILP (coldesc))
1451 return XCAR (coldesc);
1453 #ifdef MSDOS
1454 /* We can have an MSDOG frame under -nw for a short window of
1455 opportunity before internal_terminal_init is called. DTRT. */
1456 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1457 return msdos_stdcolor_name (idx);
1458 #endif
1460 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1461 return build_string (unspecified_fg);
1462 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1463 return build_string (unspecified_bg);
1465 #ifdef WINDOWSNT
1466 return vga_stdcolor_name (idx);
1467 #endif
1469 return Qunspecified;
1473 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1474 black) on frame F. The algorithm is taken from 20.2 faces.el. */
1476 static int
1477 face_color_gray_p (f, color_name)
1478 struct frame *f;
1479 char *color_name;
1481 XColor color;
1482 int gray_p;
1484 if (defined_color (f, color_name, &color, 0))
1485 gray_p = ((abs (color.red - color.green)
1486 < max (color.red, color.green) / 20)
1487 && (abs (color.green - color.blue)
1488 < max (color.green, color.blue) / 20)
1489 && (abs (color.blue - color.red)
1490 < max (color.blue, color.red) / 20));
1491 else
1492 gray_p = 0;
1494 return gray_p;
1498 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1499 BACKGROUND_P non-zero means the color will be used as background
1500 color. */
1502 static int
1503 face_color_supported_p (f, color_name, background_p)
1504 struct frame *f;
1505 char *color_name;
1506 int background_p;
1508 Lisp_Object frame;
1509 XColor not_used;
1511 XSETFRAME (frame, f);
1512 return
1513 #ifdef HAVE_X_WINDOWS
1514 FRAME_WINDOW_P (f)
1515 ? (!NILP (Fxw_display_color_p (frame))
1516 || xstricmp (color_name, "black") == 0
1517 || xstricmp (color_name, "white") == 0
1518 || (background_p
1519 && face_color_gray_p (f, color_name))
1520 || (!NILP (Fx_display_grayscale_p (frame))
1521 && face_color_gray_p (f, color_name)))
1523 #endif
1524 tty_defined_color (f, color_name, &not_used, 0);
1528 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1529 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1530 FRAME specifies the frame and thus the display for interpreting COLOR.
1531 If FRAME is nil or omitted, use the selected frame. */)
1532 (color, frame)
1533 Lisp_Object color, frame;
1535 struct frame *f;
1537 CHECK_STRING (color);
1538 if (NILP (frame))
1539 frame = selected_frame;
1540 else
1541 CHECK_FRAME (frame);
1542 f = XFRAME (frame);
1543 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
1547 DEFUN ("color-supported-p", Fcolor_supported_p,
1548 Scolor_supported_p, 1, 3, 0,
1549 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1550 BACKGROUND-P non-nil means COLOR is used as a background.
1551 Otherwise, this function tells whether it can be used as a foreground.
1552 If FRAME is nil or omitted, use the selected frame.
1553 COLOR must be a valid color name. */)
1554 (color, frame, background_p)
1555 Lisp_Object frame, color, background_p;
1557 struct frame *f;
1559 CHECK_STRING (color);
1560 if (NILP (frame))
1561 frame = selected_frame;
1562 else
1563 CHECK_FRAME (frame);
1564 f = XFRAME (frame);
1565 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
1566 return Qt;
1567 return Qnil;
1571 /* Load color with name NAME for use by face FACE on frame F.
1572 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1573 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1574 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1575 pixel color. If color cannot be loaded, display a message, and
1576 return the foreground, background or underline color of F, but
1577 record that fact in flags of the face so that we don't try to free
1578 these colors. */
1580 unsigned long
1581 load_color (f, face, name, target_index)
1582 struct frame *f;
1583 struct face *face;
1584 Lisp_Object name;
1585 enum lface_attribute_index target_index;
1587 XColor color;
1589 xassert (STRINGP (name));
1590 xassert (target_index == LFACE_FOREGROUND_INDEX
1591 || target_index == LFACE_BACKGROUND_INDEX
1592 || target_index == LFACE_UNDERLINE_INDEX
1593 || target_index == LFACE_OVERLINE_INDEX
1594 || target_index == LFACE_STRIKE_THROUGH_INDEX
1595 || target_index == LFACE_BOX_INDEX);
1597 /* if the color map is full, defined_color will return a best match
1598 to the values in an existing cell. */
1599 if (!defined_color (f, SDATA (name), &color, 1))
1601 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1603 switch (target_index)
1605 case LFACE_FOREGROUND_INDEX:
1606 face->foreground_defaulted_p = 1;
1607 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1608 break;
1610 case LFACE_BACKGROUND_INDEX:
1611 face->background_defaulted_p = 1;
1612 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1613 break;
1615 case LFACE_UNDERLINE_INDEX:
1616 face->underline_defaulted_p = 1;
1617 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1618 break;
1620 case LFACE_OVERLINE_INDEX:
1621 face->overline_color_defaulted_p = 1;
1622 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1623 break;
1625 case LFACE_STRIKE_THROUGH_INDEX:
1626 face->strike_through_color_defaulted_p = 1;
1627 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1628 break;
1630 case LFACE_BOX_INDEX:
1631 face->box_color_defaulted_p = 1;
1632 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1633 break;
1635 default:
1636 abort ();
1639 #if GLYPH_DEBUG
1640 else
1641 ++ncolors_allocated;
1642 #endif
1644 return color.pixel;
1648 #ifdef HAVE_WINDOW_SYSTEM
1650 /* Load colors for face FACE which is used on frame F. Colors are
1651 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1652 of ATTRS. If the background color specified is not supported on F,
1653 try to emulate gray colors with a stipple from Vface_default_stipple. */
1655 static void
1656 load_face_colors (f, face, attrs)
1657 struct frame *f;
1658 struct face *face;
1659 Lisp_Object *attrs;
1661 Lisp_Object fg, bg;
1663 bg = attrs[LFACE_BACKGROUND_INDEX];
1664 fg = attrs[LFACE_FOREGROUND_INDEX];
1666 /* Swap colors if face is inverse-video. */
1667 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1669 Lisp_Object tmp;
1670 tmp = fg;
1671 fg = bg;
1672 bg = tmp;
1675 /* Check for support for foreground, not for background because
1676 face_color_supported_p is smart enough to know that grays are
1677 "supported" as background because we are supposed to use stipple
1678 for them. */
1679 if (!face_color_supported_p (f, SDATA (bg), 0)
1680 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1682 x_destroy_bitmap (f, face->stipple);
1683 face->stipple = load_pixmap (f, Vface_default_stipple,
1684 &face->pixmap_w, &face->pixmap_h);
1687 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1688 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1692 /* Free color PIXEL on frame F. */
1694 void
1695 unload_color (f, pixel)
1696 struct frame *f;
1697 unsigned long pixel;
1699 #ifdef HAVE_X_WINDOWS
1700 if (pixel != -1)
1702 BLOCK_INPUT;
1703 x_free_colors (f, &pixel, 1);
1704 UNBLOCK_INPUT;
1706 #endif
1710 /* Free colors allocated for FACE. */
1712 static void
1713 free_face_colors (f, face)
1714 struct frame *f;
1715 struct face *face;
1717 #ifdef HAVE_X_WINDOWS
1718 if (face->colors_copied_bitwise_p)
1719 return;
1721 BLOCK_INPUT;
1723 if (!face->foreground_defaulted_p)
1725 x_free_colors (f, &face->foreground, 1);
1726 IF_DEBUG (--ncolors_allocated);
1729 if (!face->background_defaulted_p)
1731 x_free_colors (f, &face->background, 1);
1732 IF_DEBUG (--ncolors_allocated);
1735 if (face->underline_p
1736 && !face->underline_defaulted_p)
1738 x_free_colors (f, &face->underline_color, 1);
1739 IF_DEBUG (--ncolors_allocated);
1742 if (face->overline_p
1743 && !face->overline_color_defaulted_p)
1745 x_free_colors (f, &face->overline_color, 1);
1746 IF_DEBUG (--ncolors_allocated);
1749 if (face->strike_through_p
1750 && !face->strike_through_color_defaulted_p)
1752 x_free_colors (f, &face->strike_through_color, 1);
1753 IF_DEBUG (--ncolors_allocated);
1756 if (face->box != FACE_NO_BOX
1757 && !face->box_color_defaulted_p)
1759 x_free_colors (f, &face->box_color, 1);
1760 IF_DEBUG (--ncolors_allocated);
1763 UNBLOCK_INPUT;
1764 #endif /* HAVE_X_WINDOWS */
1767 #endif /* HAVE_WINDOW_SYSTEM */
1771 /***********************************************************************
1772 XLFD Font Names
1773 ***********************************************************************/
1775 /* An enumerator for each field of an XLFD font name. */
1777 enum xlfd_field
1779 XLFD_FOUNDRY,
1780 XLFD_FAMILY,
1781 XLFD_WEIGHT,
1782 XLFD_SLANT,
1783 XLFD_SWIDTH,
1784 XLFD_ADSTYLE,
1785 XLFD_PIXEL_SIZE,
1786 XLFD_POINT_SIZE,
1787 XLFD_RESX,
1788 XLFD_RESY,
1789 XLFD_SPACING,
1790 XLFD_AVGWIDTH,
1791 XLFD_REGISTRY,
1792 XLFD_ENCODING,
1793 XLFD_LAST
1796 /* An enumerator for each possible slant value of a font. Taken from
1797 the XLFD specification. */
1799 enum xlfd_slant
1801 XLFD_SLANT_UNKNOWN,
1802 XLFD_SLANT_ROMAN,
1803 XLFD_SLANT_ITALIC,
1804 XLFD_SLANT_OBLIQUE,
1805 XLFD_SLANT_REVERSE_ITALIC,
1806 XLFD_SLANT_REVERSE_OBLIQUE,
1807 XLFD_SLANT_OTHER
1810 /* Relative font weight according to XLFD documentation. */
1812 enum xlfd_weight
1814 XLFD_WEIGHT_UNKNOWN,
1815 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1816 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1817 XLFD_WEIGHT_LIGHT, /* 30 */
1818 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1819 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1820 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1821 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1822 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1823 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1826 /* Relative proportionate width. */
1828 enum xlfd_swidth
1830 XLFD_SWIDTH_UNKNOWN,
1831 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1832 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1833 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1834 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1835 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1836 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1837 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1838 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1839 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1842 /* Structure used for tables mapping XLFD weight, slant, and width
1843 names to numeric and symbolic values. */
1845 struct table_entry
1847 char *name;
1848 int numeric;
1849 Lisp_Object *symbol;
1852 /* Table of XLFD slant names and their numeric and symbolic
1853 representations. This table must be sorted by slant names in
1854 ascending order. */
1856 static struct table_entry slant_table[] =
1858 {"i", XLFD_SLANT_ITALIC, &Qitalic},
1859 {"o", XLFD_SLANT_OBLIQUE, &Qoblique},
1860 {"ot", XLFD_SLANT_OTHER, &Qitalic},
1861 {"r", XLFD_SLANT_ROMAN, &Qnormal},
1862 {"ri", XLFD_SLANT_REVERSE_ITALIC, &Qreverse_italic},
1863 {"ro", XLFD_SLANT_REVERSE_OBLIQUE, &Qreverse_oblique}
1866 /* Table of XLFD weight names. This table must be sorted by weight
1867 names in ascending order. */
1869 static struct table_entry weight_table[] =
1871 {"black", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold},
1872 {"bold", XLFD_WEIGHT_BOLD, &Qbold},
1873 {"book", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1874 {"demi", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1875 {"demibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1876 {"extralight", XLFD_WEIGHT_EXTRA_LIGHT, &Qextra_light},
1877 {"extrabold", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1878 {"heavy", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1879 {"light", XLFD_WEIGHT_LIGHT, &Qlight},
1880 {"medium", XLFD_WEIGHT_MEDIUM, &Qnormal},
1881 {"normal", XLFD_WEIGHT_MEDIUM, &Qnormal},
1882 {"regular", XLFD_WEIGHT_MEDIUM, &Qnormal},
1883 {"semibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1884 {"semilight", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1885 {"ultralight", XLFD_WEIGHT_ULTRA_LIGHT, &Qultra_light},
1886 {"ultrabold", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold}
1889 /* Table of XLFD width names. This table must be sorted by width
1890 names in ascending order. */
1892 static struct table_entry swidth_table[] =
1894 {"compressed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1895 {"condensed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1896 {"demiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1897 {"expanded", XLFD_SWIDTH_EXPANDED, &Qexpanded},
1898 {"extracondensed", XLFD_SWIDTH_EXTRA_CONDENSED, &Qextra_condensed},
1899 {"extraexpanded", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded},
1900 {"medium", XLFD_SWIDTH_MEDIUM, &Qnormal},
1901 {"narrow", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1902 {"normal", XLFD_SWIDTH_MEDIUM, &Qnormal},
1903 {"regular", XLFD_SWIDTH_MEDIUM, &Qnormal},
1904 {"semicondensed", XLFD_SWIDTH_SEMI_CONDENSED, &Qsemi_condensed},
1905 {"semiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1906 {"ultracondensed", XLFD_SWIDTH_ULTRA_CONDENSED, &Qultra_condensed},
1907 {"ultraexpanded", XLFD_SWIDTH_ULTRA_EXPANDED, &Qultra_expanded},
1908 {"wide", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded}
1911 /* Structure used to hold the result of splitting font names in XLFD
1912 format into their fields. */
1914 struct font_name
1916 /* The original name which is modified destructively by
1917 split_font_name. The pointer is kept here to be able to free it
1918 if it was allocated from the heap. */
1919 char *name;
1921 /* Font name fields. Each vector element points into `name' above.
1922 Fields are NUL-terminated. */
1923 char *fields[XLFD_LAST];
1925 /* Numeric values for those fields that interest us. See
1926 split_font_name for which these are. */
1927 int numeric[XLFD_LAST];
1929 /* If the original name matches one of Vface_font_rescale_alist,
1930 the value is the corresponding rescale ratio. Otherwise, the
1931 value is 1.0. */
1932 double rescale_ratio;
1934 /* Lower value mean higher priority. */
1935 int registry_priority;
1938 /* The frame in effect when sorting font names. Set temporarily in
1939 sort_fonts so that it is available in font comparison functions. */
1941 static struct frame *font_frame;
1943 /* Order by which font selection chooses fonts. The default values
1944 mean `first, find a best match for the font width, then for the
1945 font height, then for weight, then for slant.' This variable can be
1946 set via set-face-font-sort-order. */
1948 #ifdef MAC_OS
1949 static int font_sort_order[4] = {
1950 XLFD_SWIDTH, XLFD_POINT_SIZE, XLFD_WEIGHT, XLFD_SLANT
1952 #else
1953 static int font_sort_order[4];
1954 #endif
1956 /* Look up FONT.fields[FIELD_INDEX] in TABLE which has DIM entries.
1957 TABLE must be sorted by TABLE[i]->name in ascending order. Value
1958 is a pointer to the matching table entry or null if no table entry
1959 matches. */
1961 static struct table_entry *
1962 xlfd_lookup_field_contents (table, dim, font, field_index)
1963 struct table_entry *table;
1964 int dim;
1965 struct font_name *font;
1966 int field_index;
1968 /* Function split_font_name converts fields to lower-case, so there
1969 is no need to use xstrlwr or xstricmp here. */
1970 char *s = font->fields[field_index];
1971 int low, mid, high, cmp;
1973 low = 0;
1974 high = dim - 1;
1976 while (low <= high)
1978 mid = (low + high) / 2;
1979 cmp = strcmp (table[mid].name, s);
1981 if (cmp < 0)
1982 low = mid + 1;
1983 else if (cmp > 0)
1984 high = mid - 1;
1985 else
1986 return table + mid;
1989 return NULL;
1993 /* Return a numeric representation for font name field
1994 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
1995 has DIM entries. Value is the numeric value found or DFLT if no
1996 table entry matches. This function is used to translate weight,
1997 slant, and swidth names of XLFD font names to numeric values. */
1999 static INLINE int
2000 xlfd_numeric_value (table, dim, font, field_index, dflt)
2001 struct table_entry *table;
2002 int dim;
2003 struct font_name *font;
2004 int field_index;
2005 int dflt;
2007 struct table_entry *p;
2008 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2009 return p ? p->numeric : dflt;
2013 /* Return a symbolic representation for font name field
2014 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2015 has DIM entries. Value is the symbolic value found or DFLT if no
2016 table entry matches. This function is used to translate weight,
2017 slant, and swidth names of XLFD font names to symbols. */
2019 static INLINE Lisp_Object
2020 xlfd_symbolic_value (table, dim, font, field_index, dflt)
2021 struct table_entry *table;
2022 int dim;
2023 struct font_name *font;
2024 int field_index;
2025 Lisp_Object dflt;
2027 struct table_entry *p;
2028 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2029 return p ? *p->symbol : dflt;
2033 /* Return a numeric value for the slant of the font given by FONT. */
2035 static INLINE int
2036 xlfd_numeric_slant (font)
2037 struct font_name *font;
2039 return xlfd_numeric_value (slant_table, DIM (slant_table),
2040 font, XLFD_SLANT, XLFD_SLANT_ROMAN);
2044 /* Return a symbol representing the weight of the font given by FONT. */
2046 static INLINE Lisp_Object
2047 xlfd_symbolic_slant (font)
2048 struct font_name *font;
2050 return xlfd_symbolic_value (slant_table, DIM (slant_table),
2051 font, XLFD_SLANT, Qnormal);
2055 /* Return a numeric value for the weight of the font given by FONT. */
2057 static INLINE int
2058 xlfd_numeric_weight (font)
2059 struct font_name *font;
2061 return xlfd_numeric_value (weight_table, DIM (weight_table),
2062 font, XLFD_WEIGHT, XLFD_WEIGHT_MEDIUM);
2066 /* Return a symbol representing the slant of the font given by FONT. */
2068 static INLINE Lisp_Object
2069 xlfd_symbolic_weight (font)
2070 struct font_name *font;
2072 return xlfd_symbolic_value (weight_table, DIM (weight_table),
2073 font, XLFD_WEIGHT, Qnormal);
2077 /* Return a numeric value for the swidth of the font whose XLFD font
2078 name fields are found in FONT. */
2080 static INLINE int
2081 xlfd_numeric_swidth (font)
2082 struct font_name *font;
2084 return xlfd_numeric_value (swidth_table, DIM (swidth_table),
2085 font, XLFD_SWIDTH, XLFD_SWIDTH_MEDIUM);
2089 /* Return a symbolic value for the swidth of FONT. */
2091 static INLINE Lisp_Object
2092 xlfd_symbolic_swidth (font)
2093 struct font_name *font;
2095 return xlfd_symbolic_value (swidth_table, DIM (swidth_table),
2096 font, XLFD_SWIDTH, Qnormal);
2100 /* Look up the entry of SYMBOL in the vector TABLE which has DIM
2101 entries. Value is a pointer to the matching table entry or null if
2102 no element of TABLE contains SYMBOL. */
2104 static struct table_entry *
2105 face_value (table, dim, symbol)
2106 struct table_entry *table;
2107 int dim;
2108 Lisp_Object symbol;
2110 int i;
2112 xassert (SYMBOLP (symbol));
2114 for (i = 0; i < dim; ++i)
2115 if (EQ (*table[i].symbol, symbol))
2116 break;
2118 return i < dim ? table + i : NULL;
2122 /* Return a numeric value for SYMBOL in the vector TABLE which has DIM
2123 entries. Value is -1 if SYMBOL is not found in TABLE. */
2125 static INLINE int
2126 face_numeric_value (table, dim, symbol)
2127 struct table_entry *table;
2128 int dim;
2129 Lisp_Object symbol;
2131 struct table_entry *p = face_value (table, dim, symbol);
2132 return p ? p->numeric : -1;
2136 /* Return a numeric value representing the weight specified by Lisp
2137 symbol WEIGHT. Value is one of the enumerators of enum
2138 xlfd_weight. */
2140 static INLINE int
2141 face_numeric_weight (weight)
2142 Lisp_Object weight;
2144 return face_numeric_value (weight_table, DIM (weight_table), weight);
2148 /* Return a numeric value representing the slant specified by Lisp
2149 symbol SLANT. Value is one of the enumerators of enum xlfd_slant. */
2151 static INLINE int
2152 face_numeric_slant (slant)
2153 Lisp_Object slant;
2155 return face_numeric_value (slant_table, DIM (slant_table), slant);
2159 /* Return a numeric value representing the swidth specified by Lisp
2160 symbol WIDTH. Value is one of the enumerators of enum xlfd_swidth. */
2162 static int
2163 face_numeric_swidth (width)
2164 Lisp_Object width;
2166 return face_numeric_value (swidth_table, DIM (swidth_table), width);
2170 #ifdef HAVE_WINDOW_SYSTEM
2172 /* Return non-zero if FONT is the name of a fixed-pitch font. */
2174 static INLINE int
2175 xlfd_fixed_p (font)
2176 struct font_name *font;
2178 /* Function split_font_name converts fields to lower-case, so there
2179 is no need to use tolower here. */
2180 return *font->fields[XLFD_SPACING] != 'p';
2184 /* Return the point size of FONT on frame F, measured in 1/10 pt.
2186 The actual height of the font when displayed on F depends on the
2187 resolution of both the font and frame. For example, a 10pt font
2188 designed for a 100dpi display will display larger than 10pt on a
2189 75dpi display. (It's not unusual to use fonts not designed for the
2190 display one is using. For example, some intlfonts are available in
2191 72dpi versions, only.)
2193 Value is the real point size of FONT on frame F, or 0 if it cannot
2194 be determined. */
2196 static INLINE int
2197 xlfd_point_size (f, font)
2198 struct frame *f;
2199 struct font_name *font;
2201 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2202 char *pixel_field = font->fields[XLFD_PIXEL_SIZE];
2203 double pixel;
2204 int real_pt;
2206 if (*pixel_field == '[')
2208 /* The pixel size field is `[A B C D]' which specifies
2209 a transformation matrix.
2211 A B 0
2212 C D 0
2213 0 0 1
2215 by which all glyphs of the font are transformed. The spec
2216 says that s scalar value N for the pixel size is equivalent
2217 to A = N * resx/resy, B = C = 0, D = N. */
2218 char *start = pixel_field + 1, *end;
2219 double matrix[4];
2220 int i;
2222 for (i = 0; i < 4; ++i)
2224 matrix[i] = strtod (start, &end);
2225 start = end;
2228 pixel = matrix[3];
2230 else
2231 pixel = atoi (pixel_field);
2233 if (pixel == 0)
2234 real_pt = 0;
2235 else
2236 real_pt = PT_PER_INCH * 10.0 * pixel / resy + 0.5;
2238 return real_pt;
2242 /* Return point size of PIXEL dots while considering Y-resultion (DPI)
2243 of frame F. This function is used to guess a point size of font
2244 when only the pixel height of the font is available. */
2246 static INLINE int
2247 pixel_point_size (f, pixel)
2248 struct frame *f;
2249 int pixel;
2251 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2252 double real_pt;
2253 int int_pt;
2255 /* As one inch is PT_PER_INCH points, PT_PER_INCH/RESY gives the
2256 point size of one dot. */
2257 real_pt = pixel * PT_PER_INCH / resy;
2258 int_pt = real_pt + 0.5;
2260 return int_pt;
2264 /* Return a rescaling ratio of a font of NAME. */
2266 static double
2267 font_rescale_ratio (name)
2268 char *name;
2270 Lisp_Object tail, elt;
2272 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2274 elt = XCAR (tail);
2275 if (STRINGP (XCAR (elt)) && FLOATP (XCDR (elt))
2276 && fast_c_string_match_ignore_case (XCAR (elt), name) >= 0)
2277 return XFLOAT_DATA (XCDR (elt));
2279 return 1.0;
2283 /* Split XLFD font name FONT->name destructively into NUL-terminated,
2284 lower-case fields in FONT->fields. NUMERIC_P non-zero means
2285 compute numeric values for fields XLFD_POINT_SIZE, XLFD_SWIDTH,
2286 XLFD_RESY, XLFD_SLANT, and XLFD_WEIGHT in FONT->numeric. Value is
2287 zero if the font name doesn't have the format we expect. The
2288 expected format is a font name that starts with a `-' and has
2289 XLFD_LAST fields separated by `-'. */
2291 static int
2292 split_font_name (f, font, numeric_p)
2293 struct frame *f;
2294 struct font_name *font;
2295 int numeric_p;
2297 int i = 0;
2298 int success_p;
2299 double rescale_ratio;
2301 if (numeric_p)
2302 /* This must be done before splitting the font name. */
2303 rescale_ratio = font_rescale_ratio (font->name);
2305 if (*font->name == '-')
2307 char *p = xstrlwr (font->name) + 1;
2309 while (i < XLFD_LAST)
2311 font->fields[i] = p;
2312 ++i;
2314 /* Pixel and point size may be of the form `[....]'. For
2315 BNF, see XLFD spec, chapter 4. Negative values are
2316 indicated by tilde characters which we replace with
2317 `-' characters, here. */
2318 if (*p == '['
2319 && (i - 1 == XLFD_PIXEL_SIZE
2320 || i - 1 == XLFD_POINT_SIZE))
2322 char *start, *end;
2323 int j;
2325 for (++p; *p && *p != ']'; ++p)
2326 if (*p == '~')
2327 *p = '-';
2329 /* Check that the matrix contains 4 floating point
2330 numbers. */
2331 for (j = 0, start = font->fields[i - 1] + 1;
2332 j < 4;
2333 ++j, start = end)
2334 if (strtod (start, &end) == 0 && start == end)
2335 break;
2337 if (j < 4)
2338 break;
2341 while (*p && *p != '-')
2342 ++p;
2344 if (*p != '-')
2345 break;
2347 *p++ = 0;
2351 success_p = i == XLFD_LAST;
2353 /* If requested, and font name was in the expected format,
2354 compute numeric values for some fields. */
2355 if (numeric_p && success_p)
2357 font->numeric[XLFD_POINT_SIZE] = xlfd_point_size (f, font);
2358 font->numeric[XLFD_RESY] = atoi (font->fields[XLFD_RESY]);
2359 font->numeric[XLFD_SLANT] = xlfd_numeric_slant (font);
2360 font->numeric[XLFD_WEIGHT] = xlfd_numeric_weight (font);
2361 font->numeric[XLFD_SWIDTH] = xlfd_numeric_swidth (font);
2362 font->numeric[XLFD_AVGWIDTH] = atoi (font->fields[XLFD_AVGWIDTH]);
2363 font->rescale_ratio = rescale_ratio;
2366 /* Initialize it to zero. It will be overridden by font_list while
2367 trying alternate registries. */
2368 font->registry_priority = 0;
2370 return success_p;
2374 /* Build an XLFD font name from font name fields in FONT. Value is a
2375 pointer to the font name, which is allocated via xmalloc. */
2377 static char *
2378 build_font_name (font)
2379 struct font_name *font;
2381 int i;
2382 int size = 100;
2383 char *font_name = (char *) xmalloc (size);
2384 int total_length = 0;
2386 for (i = 0; i < XLFD_LAST; ++i)
2388 /* Add 1 because of the leading `-'. */
2389 int len = strlen (font->fields[i]) + 1;
2391 /* Reallocate font_name if necessary. Add 1 for the final
2392 NUL-byte. */
2393 if (total_length + len + 1 >= size)
2395 int new_size = max (2 * size, size + len + 1);
2396 int sz = new_size * sizeof *font_name;
2397 font_name = (char *) xrealloc (font_name, sz);
2398 size = new_size;
2401 font_name[total_length] = '-';
2402 bcopy (font->fields[i], font_name + total_length + 1, len - 1);
2403 total_length += len;
2406 font_name[total_length] = 0;
2407 return font_name;
2411 /* Free an array FONTS of N font_name structures. This frees FONTS
2412 itself and all `name' fields in its elements. */
2414 static INLINE void
2415 free_font_names (fonts, n)
2416 struct font_name *fonts;
2417 int n;
2419 while (n)
2420 xfree (fonts[--n].name);
2421 xfree (fonts);
2425 /* Sort vector FONTS of font_name structures which contains NFONTS
2426 elements using qsort and comparison function CMPFN. F is the frame
2427 on which the fonts will be used. The global variable font_frame
2428 is temporarily set to F to make it available in CMPFN. */
2430 static INLINE void
2431 sort_fonts (f, fonts, nfonts, cmpfn)
2432 struct frame *f;
2433 struct font_name *fonts;
2434 int nfonts;
2435 int (*cmpfn) P_ ((const void *, const void *));
2437 font_frame = f;
2438 qsort (fonts, nfonts, sizeof *fonts, cmpfn);
2439 font_frame = NULL;
2443 /* Get fonts matching PATTERN on frame F. If F is null, use the first
2444 display in x_display_list. FONTS is a pointer to a vector of
2445 NFONTS font_name structures. TRY_ALTERNATIVES_P non-zero means try
2446 alternative patterns from Valternate_fontname_alist if no fonts are
2447 found matching PATTERN.
2449 For all fonts found, set FONTS[i].name to the name of the font,
2450 allocated via xmalloc, and split font names into fields. Ignore
2451 fonts that we can't parse. Value is the number of fonts found. */
2453 static int
2454 x_face_list_fonts (f, pattern, pfonts, nfonts, try_alternatives_p)
2455 struct frame *f;
2456 char *pattern;
2457 struct font_name **pfonts;
2458 int nfonts, try_alternatives_p;
2460 int n, nignored;
2462 /* NTEMACS_TODO : currently this uses w32_list_fonts, but it may be
2463 better to do it the other way around. */
2464 Lisp_Object lfonts;
2465 Lisp_Object lpattern, tem;
2466 struct font_name *fonts = 0;
2467 int num_fonts = nfonts;
2469 *pfonts = 0;
2470 lpattern = build_string (pattern);
2472 /* Get the list of fonts matching PATTERN. */
2473 #ifdef WINDOWSNT
2474 BLOCK_INPUT;
2475 lfonts = w32_list_fonts (f, lpattern, 0, nfonts);
2476 UNBLOCK_INPUT;
2477 #else
2478 lfonts = x_list_fonts (f, lpattern, -1, nfonts);
2479 #endif
2481 if (nfonts < 0 && CONSP (lfonts))
2482 num_fonts = XFASTINT (Flength (lfonts));
2484 /* Make a copy of the font names we got from X, and
2485 split them into fields. */
2486 n = nignored = 0;
2487 for (tem = lfonts; CONSP (tem) && n < num_fonts; tem = XCDR (tem))
2489 Lisp_Object elt, tail;
2490 const char *name = SDATA (XCAR (tem));
2492 /* Ignore fonts matching a pattern from face-ignored-fonts. */
2493 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2495 elt = XCAR (tail);
2496 if (STRINGP (elt)
2497 && fast_c_string_match_ignore_case (elt, name) >= 0)
2498 break;
2500 if (!NILP (tail))
2502 ++nignored;
2503 continue;
2506 if (! fonts)
2508 *pfonts = (struct font_name *) xmalloc (num_fonts * sizeof **pfonts);
2509 fonts = *pfonts;
2512 /* Make a copy of the font name. */
2513 fonts[n].name = xstrdup (name);
2515 if (split_font_name (f, fonts + n, 1))
2517 if (font_scalable_p (fonts + n)
2518 && !may_use_scalable_font_p (name))
2520 ++nignored;
2521 xfree (fonts[n].name);
2523 else
2524 ++n;
2526 else
2527 xfree (fonts[n].name);
2530 /* If no fonts found, try patterns from Valternate_fontname_alist. */
2531 if (n == 0 && try_alternatives_p)
2533 Lisp_Object list = Valternate_fontname_alist;
2535 if (*pfonts)
2537 xfree (*pfonts);
2538 *pfonts = 0;
2541 while (CONSP (list))
2543 Lisp_Object entry = XCAR (list);
2544 if (CONSP (entry)
2545 && STRINGP (XCAR (entry))
2546 && strcmp (SDATA (XCAR (entry)), pattern) == 0)
2547 break;
2548 list = XCDR (list);
2551 if (CONSP (list))
2553 Lisp_Object patterns = XCAR (list);
2554 Lisp_Object name;
2556 while (CONSP (patterns)
2557 /* If list is screwed up, give up. */
2558 && (name = XCAR (patterns),
2559 STRINGP (name))
2560 /* Ignore patterns equal to PATTERN because we tried that
2561 already with no success. */
2562 && (strcmp (SDATA (name), pattern) == 0
2563 || (n = x_face_list_fonts (f, SDATA (name),
2564 pfonts, nfonts, 0),
2565 n == 0)))
2566 patterns = XCDR (patterns);
2570 return n;
2574 /* Check if a font matching pattern_offset_t on frame F is available
2575 or not. PATTERN may be a cons (FAMILY . REGISTRY), in which case,
2576 a font name pattern is generated from FAMILY and REGISTRY. */
2579 face_font_available_p (f, pattern)
2580 struct frame *f;
2581 Lisp_Object pattern;
2583 Lisp_Object fonts;
2585 if (! STRINGP (pattern))
2587 Lisp_Object family, registry;
2588 char *family_str, *registry_str, *pattern_str;
2590 CHECK_CONS (pattern);
2591 family = XCAR (pattern);
2592 if (NILP (family))
2593 family_str = "*";
2594 else
2596 CHECK_STRING (family);
2597 family_str = (char *) SDATA (family);
2599 registry = XCDR (pattern);
2600 if (NILP (registry))
2601 registry_str = "*";
2602 else
2604 CHECK_STRING (registry);
2605 registry_str = (char *) SDATA (registry);
2608 pattern_str = (char *) alloca (strlen (family_str)
2609 + strlen (registry_str)
2610 + 10);
2611 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2612 strcat (pattern_str, family_str);
2613 strcat (pattern_str, "-*-");
2614 strcat (pattern_str, registry_str);
2615 if (!index (registry_str, '-'))
2617 if (registry_str[strlen (registry_str) - 1] == '*')
2618 strcat (pattern_str, "-*");
2619 else
2620 strcat (pattern_str, "*-*");
2622 pattern = build_string (pattern_str);
2625 /* Get the list of fonts matching PATTERN. */
2626 #ifdef WINDOWSNT
2627 BLOCK_INPUT;
2628 fonts = w32_list_fonts (f, pattern, 0, 1);
2629 UNBLOCK_INPUT;
2630 #else
2631 fonts = x_list_fonts (f, pattern, -1, 1);
2632 #endif
2633 return XINT (Flength (fonts));
2637 /* Determine fonts matching PATTERN on frame F. Sort resulting fonts
2638 using comparison function CMPFN. Value is the number of fonts
2639 found. If value is non-zero, *FONTS is set to a vector of
2640 font_name structures allocated from the heap containing matching
2641 fonts. Each element of *FONTS contains a name member that is also
2642 allocated from the heap. Font names in these structures are split
2643 into fields. Use free_font_names to free such an array. */
2645 static int
2646 sorted_font_list (f, pattern, cmpfn, fonts)
2647 struct frame *f;
2648 char *pattern;
2649 int (*cmpfn) P_ ((const void *, const void *));
2650 struct font_name **fonts;
2652 int nfonts;
2654 /* Get the list of fonts matching pattern. 100 should suffice. */
2655 nfonts = DEFAULT_FONT_LIST_LIMIT;
2656 if (INTEGERP (Vfont_list_limit))
2657 nfonts = XINT (Vfont_list_limit);
2659 *fonts = NULL;
2660 nfonts = x_face_list_fonts (f, pattern, fonts, nfonts, 1);
2662 /* Sort the resulting array and return it in *FONTS. If no
2663 fonts were found, make sure to set *FONTS to null. */
2664 if (nfonts)
2665 sort_fonts (f, *fonts, nfonts, cmpfn);
2666 else if (*fonts)
2668 xfree (*fonts);
2669 *fonts = NULL;
2672 return nfonts;
2676 /* Compare two font_name structures *A and *B. Value is analogous to
2677 strcmp. Sort order is given by the global variable
2678 font_sort_order. Font names are sorted so that, everything else
2679 being equal, fonts with a resolution closer to that of the frame on
2680 which they are used are listed first. The global variable
2681 font_frame is the frame on which we operate. */
2683 static int
2684 cmp_font_names (a, b)
2685 const void *a, *b;
2687 struct font_name *x = (struct font_name *) a;
2688 struct font_name *y = (struct font_name *) b;
2689 int cmp;
2691 /* All strings have been converted to lower-case by split_font_name,
2692 so we can use strcmp here. */
2693 cmp = strcmp (x->fields[XLFD_FAMILY], y->fields[XLFD_FAMILY]);
2694 if (cmp == 0)
2696 int i;
2698 for (i = 0; i < DIM (font_sort_order) && cmp == 0; ++i)
2700 int j = font_sort_order[i];
2701 cmp = x->numeric[j] - y->numeric[j];
2704 if (cmp == 0)
2706 /* Everything else being equal, we prefer fonts with an
2707 y-resolution closer to that of the frame. */
2708 int resy = FRAME_X_DISPLAY_INFO (font_frame)->resy;
2709 int x_resy = x->numeric[XLFD_RESY];
2710 int y_resy = y->numeric[XLFD_RESY];
2711 cmp = abs (resy - x_resy) - abs (resy - y_resy);
2715 return cmp;
2719 /* Get a sorted list of fonts of family FAMILY on frame F. If PATTERN
2720 is non-nil list fonts matching that pattern. Otherwise, if
2721 REGISTRY is non-nil return only fonts with that registry, otherwise
2722 return fonts of any registry. Set *FONTS to a vector of font_name
2723 structures allocated from the heap containing the fonts found.
2724 Value is the number of fonts found. */
2726 static int
2727 font_list_1 (f, pattern, family, registry, fonts)
2728 struct frame *f;
2729 Lisp_Object pattern, family, registry;
2730 struct font_name **fonts;
2732 char *pattern_str, *family_str, *registry_str;
2734 if (NILP (pattern))
2736 family_str = (NILP (family) ? "*" : (char *) SDATA (family));
2737 registry_str = (NILP (registry) ? "*" : (char *) SDATA (registry));
2739 pattern_str = (char *) alloca (strlen (family_str)
2740 + strlen (registry_str)
2741 + 10);
2742 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2743 strcat (pattern_str, family_str);
2744 strcat (pattern_str, "-*-");
2745 strcat (pattern_str, registry_str);
2746 if (!index (registry_str, '-'))
2748 if (registry_str[strlen (registry_str) - 1] == '*')
2749 strcat (pattern_str, "-*");
2750 else
2751 strcat (pattern_str, "*-*");
2754 else
2755 pattern_str = (char *) SDATA (pattern);
2757 return sorted_font_list (f, pattern_str, cmp_font_names, fonts);
2761 /* Concatenate font list FONTS1 and FONTS2. FONTS1 and FONTS2
2762 contains NFONTS1 fonts and NFONTS2 fonts respectively. Return a
2763 pointer to a newly allocated font list. FONTS1 and FONTS2 are
2764 freed. */
2766 static struct font_name *
2767 concat_font_list (fonts1, nfonts1, fonts2, nfonts2)
2768 struct font_name *fonts1, *fonts2;
2769 int nfonts1, nfonts2;
2771 int new_nfonts = nfonts1 + nfonts2;
2772 struct font_name *new_fonts;
2774 new_fonts = (struct font_name *) xmalloc (sizeof *new_fonts * new_nfonts);
2775 bcopy (fonts1, new_fonts, sizeof *new_fonts * nfonts1);
2776 bcopy (fonts2, new_fonts + nfonts1, sizeof *new_fonts * nfonts2);
2777 xfree (fonts1);
2778 xfree (fonts2);
2779 return new_fonts;
2783 /* Get a sorted list of fonts of family FAMILY on frame F.
2785 If PATTERN is non-nil list fonts matching that pattern.
2787 If REGISTRY is non-nil, return fonts with that registry and the
2788 alternative registries from Vface_alternative_font_registry_alist.
2790 If REGISTRY is nil return fonts of any registry.
2792 Set *FONTS to a vector of font_name structures allocated from the
2793 heap containing the fonts found. Value is the number of fonts
2794 found. */
2796 static int
2797 font_list (f, pattern, family, registry, fonts)
2798 struct frame *f;
2799 Lisp_Object pattern, family, registry;
2800 struct font_name **fonts;
2802 int nfonts = font_list_1 (f, pattern, family, registry, fonts);
2804 if (!NILP (registry)
2805 && CONSP (Vface_alternative_font_registry_alist))
2807 Lisp_Object alter;
2809 alter = Fassoc (registry, Vface_alternative_font_registry_alist);
2810 if (CONSP (alter))
2812 int reg_prio, i;
2814 for (alter = XCDR (alter), reg_prio = 1;
2815 CONSP (alter);
2816 alter = XCDR (alter), reg_prio++)
2817 if (STRINGP (XCAR (alter)))
2819 int nfonts2;
2820 struct font_name *fonts2;
2822 nfonts2 = font_list_1 (f, pattern, family, XCAR (alter),
2823 &fonts2);
2824 for (i = 0; i < nfonts2; i++)
2825 fonts2[i].registry_priority = reg_prio;
2826 *fonts = (nfonts > 0
2827 ? concat_font_list (*fonts, nfonts, fonts2, nfonts2)
2828 : fonts2);
2829 nfonts += nfonts2;
2834 return nfonts;
2838 /* Remove elements from LIST whose cars are `equal'. Called from
2839 x-family-fonts and x-font-family-list to remove duplicate font
2840 entries. */
2842 static void
2843 remove_duplicates (list)
2844 Lisp_Object list;
2846 Lisp_Object tail = list;
2848 while (!NILP (tail) && !NILP (XCDR (tail)))
2850 Lisp_Object next = XCDR (tail);
2851 if (!NILP (Fequal (XCAR (next), XCAR (tail))))
2852 XSETCDR (tail, XCDR (next));
2853 else
2854 tail = XCDR (tail);
2859 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
2860 doc: /* Return a list of available fonts of family FAMILY on FRAME.
2861 If FAMILY is omitted or nil, list all families.
2862 Otherwise, FAMILY must be a string, possibly containing wildcards
2863 `?' and `*'.
2864 If FRAME is omitted or nil, use the selected frame.
2865 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
2866 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
2867 FAMILY is the font family name. POINT-SIZE is the size of the
2868 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
2869 width, weight and slant of the font. These symbols are the same as for
2870 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
2871 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
2872 giving the registry and encoding of the font.
2873 The result list is sorted according to the current setting of
2874 the face font sort order. */)
2875 (family, frame)
2876 Lisp_Object family, frame;
2878 struct frame *f = check_x_frame (frame);
2879 struct font_name *fonts;
2880 int i, nfonts;
2881 Lisp_Object result;
2882 struct gcpro gcpro1;
2884 if (!NILP (family))
2885 CHECK_STRING (family);
2887 result = Qnil;
2888 GCPRO1 (result);
2889 nfonts = font_list (f, Qnil, family, Qnil, &fonts);
2890 for (i = nfonts - 1; i >= 0; --i)
2892 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
2893 char *tem;
2895 ASET (v, 0, build_string (fonts[i].fields[XLFD_FAMILY]));
2896 ASET (v, 1, xlfd_symbolic_swidth (fonts + i));
2897 ASET (v, 2, make_number (xlfd_point_size (f, fonts + i)));
2898 ASET (v, 3, xlfd_symbolic_weight (fonts + i));
2899 ASET (v, 4, xlfd_symbolic_slant (fonts + i));
2900 ASET (v, 5, xlfd_fixed_p (fonts + i) ? Qt : Qnil);
2901 tem = build_font_name (fonts + i);
2902 ASET (v, 6, build_string (tem));
2903 sprintf (tem, "%s-%s", fonts[i].fields[XLFD_REGISTRY],
2904 fonts[i].fields[XLFD_ENCODING]);
2905 ASET (v, 7, build_string (tem));
2906 xfree (tem);
2908 result = Fcons (v, result);
2911 remove_duplicates (result);
2912 free_font_names (fonts, nfonts);
2913 UNGCPRO;
2914 return result;
2918 DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
2919 0, 1, 0,
2920 doc: /* Return a list of available font families on FRAME.
2921 If FRAME is omitted or nil, use the selected frame.
2922 Value is a list of conses (FAMILY . FIXED-P) where FAMILY
2923 is a font family, and FIXED-P is non-nil if fonts of that family
2924 are fixed-pitch. */)
2925 (frame)
2926 Lisp_Object frame;
2928 struct frame *f = check_x_frame (frame);
2929 int nfonts, i;
2930 struct font_name *fonts;
2931 Lisp_Object result;
2932 struct gcpro gcpro1;
2933 int count = SPECPDL_INDEX ();
2935 /* Let's consider all fonts. */
2936 specbind (intern ("font-list-limit"), make_number (-1));
2937 nfonts = font_list (f, Qnil, Qnil, Qnil, &fonts);
2939 result = Qnil;
2940 GCPRO1 (result);
2941 for (i = nfonts - 1; i >= 0; --i)
2942 result = Fcons (Fcons (build_string (fonts[i].fields[XLFD_FAMILY]),
2943 xlfd_fixed_p (fonts + i) ? Qt : Qnil),
2944 result);
2946 remove_duplicates (result);
2947 free_font_names (fonts, nfonts);
2948 UNGCPRO;
2949 return unbind_to (count, result);
2953 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
2954 doc: /* Return a list of the names of available fonts matching PATTERN.
2955 If optional arguments FACE and FRAME are specified, return only fonts
2956 the same size as FACE on FRAME.
2957 PATTERN is a string, perhaps with wildcard characters;
2958 the * character matches any substring, and
2959 the ? character matches any single character.
2960 PATTERN is case-insensitive.
2961 FACE is a face name--a symbol.
2963 The return value is a list of strings, suitable as arguments to
2964 set-face-font.
2966 Fonts Emacs can't use may or may not be excluded
2967 even if they match PATTERN and FACE.
2968 The optional fourth argument MAXIMUM sets a limit on how many
2969 fonts to match. The first MAXIMUM fonts are reported.
2970 The optional fifth argument WIDTH, if specified, is a number of columns
2971 occupied by a character of a font. In that case, return only fonts
2972 the WIDTH times as wide as FACE on FRAME. */)
2973 (pattern, face, frame, maximum, width)
2974 Lisp_Object pattern, face, frame, maximum, width;
2976 struct frame *f;
2977 int size;
2978 int maxnames;
2980 check_x ();
2981 CHECK_STRING (pattern);
2983 if (NILP (maximum))
2984 maxnames = -1;
2985 else
2987 CHECK_NATNUM (maximum);
2988 maxnames = XINT (maximum);
2991 if (!NILP (width))
2992 CHECK_NUMBER (width);
2994 /* We can't simply call check_x_frame because this function may be
2995 called before any frame is created. */
2996 f = frame_or_selected_frame (frame, 2);
2997 if (!FRAME_WINDOW_P (f))
2999 /* Perhaps we have not yet created any frame. */
3000 f = NULL;
3001 face = Qnil;
3004 /* Determine the width standard for comparison with the fonts we find. */
3006 if (NILP (face))
3007 size = 0;
3008 else
3010 /* This is of limited utility since it works with character
3011 widths. Keep it for compatibility. --gerd. */
3012 int face_id = lookup_named_face (f, face, 0);
3013 struct face *face = (face_id < 0
3014 ? NULL
3015 : FACE_FROM_ID (f, face_id));
3017 #ifdef WINDOWSNT
3018 /* For historic reasons, FONT_WIDTH refers to average width on W32,
3019 not maximum as on X. Redefine here. */
3020 #undef FONT_WIDTH
3021 #define FONT_WIDTH FONT_MAX_WIDTH
3022 #endif
3024 if (face && face->font)
3025 size = FONT_WIDTH (face->font);
3026 else
3027 size = FONT_WIDTH (FRAME_FONT (f)); /* FRAME_COLUMN_WIDTH (f) */
3029 if (!NILP (width))
3030 size *= XINT (width);
3034 Lisp_Object args[2];
3036 args[0] = x_list_fonts (f, pattern, size, maxnames);
3037 if (f == NULL)
3038 /* We don't have to check fontsets. */
3039 return args[0];
3040 args[1] = list_fontsets (f, pattern, size);
3041 return Fnconc (2, args);
3045 #endif /* HAVE_WINDOW_SYSTEM */
3049 /***********************************************************************
3050 Lisp Faces
3051 ***********************************************************************/
3053 /* Access face attributes of face LFACE, a Lisp vector. */
3055 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
3056 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
3057 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
3058 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
3059 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
3060 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
3061 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
3062 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
3063 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
3064 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
3065 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
3066 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
3067 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
3068 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
3069 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
3070 #define LFACE_AVGWIDTH(LFACE) AREF ((LFACE), LFACE_AVGWIDTH_INDEX)
3072 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
3073 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
3075 #define LFACEP(LFACE) \
3076 (VECTORP (LFACE) \
3077 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
3078 && EQ (AREF (LFACE, 0), Qface))
3081 #if GLYPH_DEBUG
3083 /* Check consistency of Lisp face attribute vector ATTRS. */
3085 static void
3086 check_lface_attrs (attrs)
3087 Lisp_Object *attrs;
3089 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
3090 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
3091 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
3092 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
3093 xassert (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
3094 || INTEGERP (attrs[LFACE_AVGWIDTH_INDEX]));
3095 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
3096 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
3097 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
3098 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
3099 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
3100 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
3101 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
3102 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
3103 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
3104 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
3105 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
3106 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
3107 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
3108 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
3109 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3110 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3111 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
3112 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
3113 || SYMBOLP (attrs[LFACE_BOX_INDEX])
3114 || STRINGP (attrs[LFACE_BOX_INDEX])
3115 || INTEGERP (attrs[LFACE_BOX_INDEX])
3116 || CONSP (attrs[LFACE_BOX_INDEX]));
3117 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
3118 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
3119 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
3120 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
3121 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
3122 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
3123 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
3124 || NILP (attrs[LFACE_INHERIT_INDEX])
3125 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
3126 || CONSP (attrs[LFACE_INHERIT_INDEX]));
3127 #ifdef HAVE_WINDOW_SYSTEM
3128 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
3129 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
3130 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
3131 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
3132 || NILP (attrs[LFACE_FONT_INDEX])
3133 || STRINGP (attrs[LFACE_FONT_INDEX]));
3134 #endif
3138 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
3140 static void
3141 check_lface (lface)
3142 Lisp_Object lface;
3144 if (!NILP (lface))
3146 xassert (LFACEP (lface));
3147 check_lface_attrs (XVECTOR (lface)->contents);
3151 #else /* GLYPH_DEBUG == 0 */
3153 #define check_lface_attrs(attrs) (void) 0
3154 #define check_lface(lface) (void) 0
3156 #endif /* GLYPH_DEBUG == 0 */
3160 /* Face-merge cycle checking. */
3162 /* A `named merge point' is simply a point during face-merging where we
3163 look up a face by name. We keep a stack of which named lookups we're
3164 currently processing so that we can easily detect cycles, using a
3165 linked- list of struct named_merge_point structures, typically
3166 allocated on the stack frame of the named lookup functions which are
3167 active (so no consing is required). */
3168 struct named_merge_point
3170 Lisp_Object face_name;
3171 struct named_merge_point *prev;
3175 /* If a face merging cycle is detected for FACE_NAME, return 0,
3176 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
3177 FACE_NAME, as the head of the linked list pointed to by
3178 NAMED_MERGE_POINTS, and return 1. */
3180 static INLINE int
3181 push_named_merge_point (struct named_merge_point *new_named_merge_point,
3182 Lisp_Object face_name,
3183 struct named_merge_point **named_merge_points)
3185 struct named_merge_point *prev;
3187 for (prev = *named_merge_points; prev; prev = prev->prev)
3188 if (EQ (face_name, prev->face_name))
3189 return 0;
3191 new_named_merge_point->face_name = face_name;
3192 new_named_merge_point->prev = *named_merge_points;
3194 *named_merge_points = new_named_merge_point;
3196 return 1;
3202 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
3203 to make it a symvol. If FACE_NAME is an alias for another face,
3204 return that face's name. */
3206 static Lisp_Object
3207 resolve_face_name (face_name)
3208 Lisp_Object face_name;
3210 Lisp_Object aliased;
3212 if (STRINGP (face_name))
3213 face_name = intern (SDATA (face_name));
3215 while (SYMBOLP (face_name))
3217 aliased = Fget (face_name, Qface_alias);
3218 if (NILP (aliased))
3219 break;
3220 else
3221 face_name = aliased;
3224 return face_name;
3228 /* Return the face definition of FACE_NAME on frame F. F null means
3229 return the definition for new frames. FACE_NAME may be a string or
3230 a symbol (apparently Emacs 20.2 allowed strings as face names in
3231 face text properties; Ediff uses that). If FACE_NAME is an alias
3232 for another face, return that face's definition. If SIGNAL_P is
3233 non-zero, signal an error if FACE_NAME is not a valid face name.
3234 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
3235 name. */
3237 static INLINE Lisp_Object
3238 lface_from_face_name (f, face_name, signal_p)
3239 struct frame *f;
3240 Lisp_Object face_name;
3241 int signal_p;
3243 Lisp_Object lface;
3245 face_name = resolve_face_name (face_name);
3247 if (f)
3248 lface = assq_no_quit (face_name, f->face_alist);
3249 else
3250 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
3252 if (CONSP (lface))
3253 lface = XCDR (lface);
3254 else if (signal_p)
3255 signal_error ("Invalid face", face_name);
3257 check_lface (lface);
3258 return lface;
3262 /* Get face attributes of face FACE_NAME from frame-local faces on
3263 frame F. Store the resulting attributes in ATTRS which must point
3264 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
3265 is non-zero, signal an error if FACE_NAME does not name a face.
3266 Otherwise, value is zero if FACE_NAME is not a face. */
3268 static INLINE int
3269 get_lface_attributes (f, face_name, attrs, signal_p)
3270 struct frame *f;
3271 Lisp_Object face_name;
3272 Lisp_Object *attrs;
3273 int signal_p;
3275 Lisp_Object lface;
3276 int success_p;
3278 lface = lface_from_face_name (f, face_name, signal_p);
3279 if (!NILP (lface))
3281 bcopy (XVECTOR (lface)->contents, attrs,
3282 LFACE_VECTOR_SIZE * sizeof *attrs);
3283 success_p = 1;
3285 else
3286 success_p = 0;
3288 return success_p;
3292 /* Non-zero if all attributes in face attribute vector ATTRS are
3293 specified, i.e. are non-nil. */
3295 static int
3296 lface_fully_specified_p (attrs)
3297 Lisp_Object *attrs;
3299 int i;
3301 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3302 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
3303 && i != LFACE_AVGWIDTH_INDEX)
3304 if (UNSPECIFIEDP (attrs[i])
3305 #ifdef MAC_OS
3306 /* MAC_TODO: No stipple support on Mac OS yet, this index is
3307 always unspecified. */
3308 && i != LFACE_STIPPLE_INDEX
3309 #endif
3311 break;
3313 return i == LFACE_VECTOR_SIZE;
3316 #ifdef HAVE_WINDOW_SYSTEM
3318 /* Set font-related attributes of Lisp face LFACE from the fullname of
3319 the font opened by FONTNAME. If FORCE_P is zero, set only
3320 unspecified attributes of LFACE. The exception is `font'
3321 attribute. It is set to FONTNAME as is regardless of FORCE_P.
3323 If FONTNAME is not available on frame F,
3324 return 0 if MAY_FAIL_P is non-zero, otherwise abort.
3325 If the fullname is not in a valid XLFD format,
3326 return 0 if MAY_FAIL_P is non-zero, otherwise set normal values
3327 in LFACE and return 1.
3328 Otherwise, return 1. */
3330 static int
3331 set_lface_from_font_name (f, lface, fontname, force_p, may_fail_p)
3332 struct frame *f;
3333 Lisp_Object lface;
3334 Lisp_Object fontname;
3335 int force_p, may_fail_p;
3337 struct font_name font;
3338 char *buffer;
3339 int pt;
3340 int have_xlfd_p;
3341 int fontset;
3342 char *font_name = SDATA (fontname);
3343 struct font_info *font_info;
3345 /* If FONTNAME is actually a fontset name, get ASCII font name of it. */
3346 fontset = fs_query_fontset (fontname, 0);
3347 if (fontset >= 0)
3348 font_name = SDATA (fontset_ascii (fontset));
3350 /* Check if FONT_NAME is surely available on the system. Usually
3351 FONT_NAME is already cached for the frame F and FS_LOAD_FONT
3352 returns quickly. But, even if FONT_NAME is not yet cached,
3353 caching it now is not futail because we anyway load the font
3354 later. */
3355 BLOCK_INPUT;
3356 font_info = FS_LOAD_FONT (f, 0, font_name, -1);
3357 UNBLOCK_INPUT;
3359 if (!font_info)
3361 if (may_fail_p)
3362 return 0;
3363 abort ();
3366 font.name = STRDUPA (font_info->full_name);
3367 have_xlfd_p = split_font_name (f, &font, 1);
3369 /* Set attributes only if unspecified, otherwise face defaults for
3370 new frames would never take effect. If we couldn't get a font
3371 name conforming to XLFD, set normal values. */
3373 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
3375 Lisp_Object val;
3376 if (have_xlfd_p)
3378 buffer = (char *) alloca (strlen (font.fields[XLFD_FAMILY])
3379 + strlen (font.fields[XLFD_FOUNDRY])
3380 + 2);
3381 sprintf (buffer, "%s-%s", font.fields[XLFD_FOUNDRY],
3382 font.fields[XLFD_FAMILY]);
3383 val = build_string (buffer);
3385 else
3386 val = build_string ("*");
3387 LFACE_FAMILY (lface) = val;
3390 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
3392 if (have_xlfd_p)
3393 pt = xlfd_point_size (f, &font);
3394 else
3395 pt = pixel_point_size (f, font_info->height * 10);
3396 xassert (pt > 0);
3397 LFACE_HEIGHT (lface) = make_number (pt);
3400 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
3401 LFACE_SWIDTH (lface)
3402 = have_xlfd_p ? xlfd_symbolic_swidth (&font) : Qnormal;
3404 if (force_p || UNSPECIFIEDP (LFACE_AVGWIDTH (lface)))
3405 LFACE_AVGWIDTH (lface)
3406 = (have_xlfd_p
3407 ? make_number (font.numeric[XLFD_AVGWIDTH])
3408 : Qunspecified);
3410 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
3411 LFACE_WEIGHT (lface)
3412 = have_xlfd_p ? xlfd_symbolic_weight (&font) : Qnormal;
3414 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
3415 LFACE_SLANT (lface)
3416 = have_xlfd_p ? xlfd_symbolic_slant (&font) : Qnormal;
3418 LFACE_FONT (lface) = fontname;
3420 return 1;
3423 #endif /* HAVE_WINDOW_SYSTEM */
3426 /* Merges the face height FROM with the face height TO, and returns the
3427 merged height. If FROM is an invalid height, then INVALID is
3428 returned instead. FROM and TO may be either absolute face heights or
3429 `relative' heights; the returned value is always an absolute height
3430 unless both FROM and TO are relative. GCPRO is a lisp value that
3431 will be protected from garbage-collection if this function makes a
3432 call into lisp. */
3434 Lisp_Object
3435 merge_face_heights (from, to, invalid, gcpro)
3436 Lisp_Object from, to, invalid, gcpro;
3438 Lisp_Object result = invalid;
3440 if (INTEGERP (from))
3441 /* FROM is absolute, just use it as is. */
3442 result = from;
3443 else if (FLOATP (from))
3444 /* FROM is a scale, use it to adjust TO. */
3446 if (INTEGERP (to))
3447 /* relative X absolute => absolute */
3448 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
3449 else if (FLOATP (to))
3450 /* relative X relative => relative */
3451 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
3452 else if (UNSPECIFIEDP (to))
3453 result = from;
3455 else if (FUNCTIONP (from))
3456 /* FROM is a function, which use to adjust TO. */
3458 /* Call function with current height as argument.
3459 From is the new height. */
3460 Lisp_Object args[2];
3461 struct gcpro gcpro1;
3463 GCPRO1 (gcpro);
3465 args[0] = from;
3466 args[1] = to;
3467 result = safe_call (2, args);
3469 UNGCPRO;
3471 /* Ensure that if TO was absolute, so is the result. */
3472 if (INTEGERP (to) && !INTEGERP (result))
3473 result = invalid;
3476 return result;
3480 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
3481 store the resulting attributes in TO, which must be already be
3482 completely specified and contain only absolute attributes. Every
3483 specified attribute of FROM overrides the corresponding attribute of
3484 TO; relative attributes in FROM are merged with the absolute value in
3485 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
3486 loops in face inheritance; it should be 0 when called from other
3487 places. */
3489 static INLINE void
3490 merge_face_vectors (f, from, to, named_merge_points)
3491 struct frame *f;
3492 Lisp_Object *from, *to;
3493 struct named_merge_point *named_merge_points;
3495 int i;
3497 /* If FROM inherits from some other faces, merge their attributes into
3498 TO before merging FROM's direct attributes. Note that an :inherit
3499 attribute of `unspecified' is the same as one of nil; we never
3500 merge :inherit attributes, so nil is more correct, but lots of
3501 other code uses `unspecified' as a generic value for face attributes. */
3502 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
3503 && !NILP (from[LFACE_INHERIT_INDEX]))
3504 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
3506 /* If TO specifies a :font attribute, and FROM specifies some
3507 font-related attribute, we need to clear TO's :font attribute
3508 (because it will be inconsistent with whatever FROM specifies, and
3509 FROM takes precedence). */
3510 if (!NILP (to[LFACE_FONT_INDEX])
3511 && (!UNSPECIFIEDP (from[LFACE_FAMILY_INDEX])
3512 || !UNSPECIFIEDP (from[LFACE_HEIGHT_INDEX])
3513 || !UNSPECIFIEDP (from[LFACE_WEIGHT_INDEX])
3514 || !UNSPECIFIEDP (from[LFACE_SLANT_INDEX])
3515 || !UNSPECIFIEDP (from[LFACE_SWIDTH_INDEX])
3516 || !UNSPECIFIEDP (from[LFACE_AVGWIDTH_INDEX])))
3517 to[LFACE_FONT_INDEX] = Qnil;
3519 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3520 if (!UNSPECIFIEDP (from[i]))
3522 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
3523 to[i] = merge_face_heights (from[i], to[i], to[i],
3524 named_merge_points);
3525 else
3526 to[i] = from[i];
3529 /* TO is always an absolute face, which should inherit from nothing.
3530 We blindly copy the :inherit attribute above and fix it up here. */
3531 to[LFACE_INHERIT_INDEX] = Qnil;
3534 /* Merge the named face FACE_NAME on frame F, into the vector of face
3535 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
3536 inheritance. Returns true if FACE_NAME is a valid face name and
3537 merging succeeded. */
3539 static int
3540 merge_named_face (f, face_name, to, named_merge_points)
3541 struct frame *f;
3542 Lisp_Object face_name;
3543 Lisp_Object *to;
3544 struct named_merge_point *named_merge_points;
3546 struct named_merge_point named_merge_point;
3548 if (push_named_merge_point (&named_merge_point,
3549 face_name, &named_merge_points))
3551 Lisp_Object from[LFACE_VECTOR_SIZE];
3552 int ok = get_lface_attributes (f, face_name, from, 0);
3554 if (ok)
3555 merge_face_vectors (f, from, to, named_merge_points);
3557 return ok;
3559 else
3560 return 0;
3564 /* Merge face attributes from the lisp `face reference' FACE_REF on
3565 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
3566 problems with FACE_REF cause an error message to be shown. Return
3567 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
3568 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
3569 list structure; it may be 0 for most callers.
3571 FACE_REF may be a single face specification or a list of such
3572 specifications. Each face specification can be:
3574 1. A symbol or string naming a Lisp face.
3576 2. A property list of the form (KEYWORD VALUE ...) where each
3577 KEYWORD is a face attribute name, and value is an appropriate value
3578 for that attribute.
3580 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
3581 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
3582 for compatibility with 20.2.
3584 Face specifications earlier in lists take precedence over later
3585 specifications. */
3587 static int
3588 merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
3589 struct frame *f;
3590 Lisp_Object face_ref;
3591 Lisp_Object *to;
3592 int err_msgs;
3593 struct named_merge_point *named_merge_points;
3595 int ok = 1; /* Succeed without an error? */
3597 if (CONSP (face_ref))
3599 Lisp_Object first = XCAR (face_ref);
3601 if (EQ (first, Qforeground_color)
3602 || EQ (first, Qbackground_color))
3604 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
3605 . COLOR). COLOR must be a string. */
3606 Lisp_Object color_name = XCDR (face_ref);
3607 Lisp_Object color = first;
3609 if (STRINGP (color_name))
3611 if (EQ (color, Qforeground_color))
3612 to[LFACE_FOREGROUND_INDEX] = color_name;
3613 else
3614 to[LFACE_BACKGROUND_INDEX] = color_name;
3616 else
3618 if (err_msgs)
3619 add_to_log ("Invalid face color", color_name, Qnil);
3620 ok = 0;
3623 else if (SYMBOLP (first)
3624 && *SDATA (SYMBOL_NAME (first)) == ':')
3626 /* Assume this is the property list form. */
3627 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
3629 Lisp_Object keyword = XCAR (face_ref);
3630 Lisp_Object value = XCAR (XCDR (face_ref));
3631 int err = 0;
3633 /* Specifying `unspecified' is a no-op. */
3634 if (EQ (value, Qunspecified))
3636 else if (EQ (keyword, QCfamily))
3638 if (STRINGP (value))
3639 to[LFACE_FAMILY_INDEX] = value;
3640 else
3641 err = 1;
3643 else if (EQ (keyword, QCheight))
3645 Lisp_Object new_height =
3646 merge_face_heights (value, to[LFACE_HEIGHT_INDEX],
3647 Qnil, Qnil);
3649 if (! NILP (new_height))
3650 to[LFACE_HEIGHT_INDEX] = new_height;
3651 else
3652 err = 1;
3654 else if (EQ (keyword, QCweight))
3656 if (SYMBOLP (value)
3657 && face_numeric_weight (value) >= 0)
3658 to[LFACE_WEIGHT_INDEX] = value;
3659 else
3660 err = 1;
3662 else if (EQ (keyword, QCslant))
3664 if (SYMBOLP (value)
3665 && face_numeric_slant (value) >= 0)
3666 to[LFACE_SLANT_INDEX] = value;
3667 else
3668 err = 1;
3670 else if (EQ (keyword, QCunderline))
3672 if (EQ (value, Qt)
3673 || NILP (value)
3674 || STRINGP (value))
3675 to[LFACE_UNDERLINE_INDEX] = value;
3676 else
3677 err = 1;
3679 else if (EQ (keyword, QCoverline))
3681 if (EQ (value, Qt)
3682 || NILP (value)
3683 || STRINGP (value))
3684 to[LFACE_OVERLINE_INDEX] = value;
3685 else
3686 err = 1;
3688 else if (EQ (keyword, QCstrike_through))
3690 if (EQ (value, Qt)
3691 || NILP (value)
3692 || STRINGP (value))
3693 to[LFACE_STRIKE_THROUGH_INDEX] = value;
3694 else
3695 err = 1;
3697 else if (EQ (keyword, QCbox))
3699 if (EQ (value, Qt))
3700 value = make_number (1);
3701 if (INTEGERP (value)
3702 || STRINGP (value)
3703 || CONSP (value)
3704 || NILP (value))
3705 to[LFACE_BOX_INDEX] = value;
3706 else
3707 err = 1;
3709 else if (EQ (keyword, QCinverse_video)
3710 || EQ (keyword, QCreverse_video))
3712 if (EQ (value, Qt) || NILP (value))
3713 to[LFACE_INVERSE_INDEX] = value;
3714 else
3715 err = 1;
3717 else if (EQ (keyword, QCforeground))
3719 if (STRINGP (value))
3720 to[LFACE_FOREGROUND_INDEX] = value;
3721 else
3722 err = 1;
3724 else if (EQ (keyword, QCbackground))
3726 if (STRINGP (value))
3727 to[LFACE_BACKGROUND_INDEX] = value;
3728 else
3729 err = 1;
3731 else if (EQ (keyword, QCstipple))
3733 #ifdef HAVE_X_WINDOWS
3734 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
3735 if (!NILP (pixmap_p))
3736 to[LFACE_STIPPLE_INDEX] = value;
3737 else
3738 err = 1;
3739 #endif
3741 else if (EQ (keyword, QCwidth))
3743 if (SYMBOLP (value)
3744 && face_numeric_swidth (value) >= 0)
3745 to[LFACE_SWIDTH_INDEX] = value;
3746 else
3747 err = 1;
3749 else if (EQ (keyword, QCinherit))
3751 /* This is not really very useful; it's just like a
3752 normal face reference. */
3753 if (! merge_face_ref (f, value, to,
3754 err_msgs, named_merge_points))
3755 err = 1;
3757 else
3758 err = 1;
3760 if (err)
3762 add_to_log ("Invalid face attribute %S %S", keyword, value);
3763 ok = 0;
3766 face_ref = XCDR (XCDR (face_ref));
3769 else
3771 /* This is a list of face refs. Those at the beginning of the
3772 list take precedence over what follows, so we have to merge
3773 from the end backwards. */
3774 Lisp_Object next = XCDR (face_ref);
3776 if (! NILP (next))
3777 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
3779 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
3780 ok = 0;
3783 else
3785 /* FACE_REF ought to be a face name. */
3786 ok = merge_named_face (f, face_ref, to, named_merge_points);
3787 if (!ok && err_msgs)
3788 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
3791 return ok;
3795 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
3796 Sinternal_make_lisp_face, 1, 2, 0,
3797 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
3798 If FACE was not known as a face before, create a new one.
3799 If optional argument FRAME is specified, make a frame-local face
3800 for that frame. Otherwise operate on the global face definition.
3801 Value is a vector of face attributes. */)
3802 (face, frame)
3803 Lisp_Object face, frame;
3805 Lisp_Object global_lface, lface;
3806 struct frame *f;
3807 int i;
3809 CHECK_SYMBOL (face);
3810 global_lface = lface_from_face_name (NULL, face, 0);
3812 if (!NILP (frame))
3814 CHECK_LIVE_FRAME (frame);
3815 f = XFRAME (frame);
3816 lface = lface_from_face_name (f, face, 0);
3818 else
3819 f = NULL, lface = Qnil;
3821 /* Add a global definition if there is none. */
3822 if (NILP (global_lface))
3824 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3825 Qunspecified);
3826 AREF (global_lface, 0) = Qface;
3827 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
3828 Vface_new_frame_defaults);
3830 /* Assign the new Lisp face a unique ID. The mapping from Lisp
3831 face id to Lisp face is given by the vector lface_id_to_name.
3832 The mapping from Lisp face to Lisp face id is given by the
3833 property `face' of the Lisp face name. */
3834 if (next_lface_id == lface_id_to_name_size)
3836 int new_size = max (50, 2 * lface_id_to_name_size);
3837 int sz = new_size * sizeof *lface_id_to_name;
3838 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
3839 lface_id_to_name_size = new_size;
3842 lface_id_to_name[next_lface_id] = face;
3843 Fput (face, Qface, make_number (next_lface_id));
3844 ++next_lface_id;
3846 else if (f == NULL)
3847 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3848 AREF (global_lface, i) = Qunspecified;
3850 /* Add a frame-local definition. */
3851 if (f)
3853 if (NILP (lface))
3855 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3856 Qunspecified);
3857 AREF (lface, 0) = Qface;
3858 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
3860 else
3861 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3862 AREF (lface, i) = Qunspecified;
3864 else
3865 lface = global_lface;
3867 /* Changing a named face means that all realized faces depending on
3868 that face are invalid. Since we cannot tell which realized faces
3869 depend on the face, make sure they are all removed. This is done
3870 by incrementing face_change_count. The next call to
3871 init_iterator will then free realized faces. */
3872 if (NILP (Fget (face, Qface_no_inherit)))
3874 ++face_change_count;
3875 ++windows_or_buffers_changed;
3878 xassert (LFACEP (lface));
3879 check_lface (lface);
3880 return lface;
3884 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
3885 Sinternal_lisp_face_p, 1, 2, 0,
3886 doc: /* Return non-nil if FACE names a face.
3887 If optional second parameter FRAME is non-nil, check for the
3888 existence of a frame-local face with name FACE on that frame.
3889 Otherwise check for the existence of a global face. */)
3890 (face, frame)
3891 Lisp_Object face, frame;
3893 Lisp_Object lface;
3895 if (!NILP (frame))
3897 CHECK_LIVE_FRAME (frame);
3898 lface = lface_from_face_name (XFRAME (frame), face, 0);
3900 else
3901 lface = lface_from_face_name (NULL, face, 0);
3903 return lface;
3907 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
3908 Sinternal_copy_lisp_face, 4, 4, 0,
3909 doc: /* Copy face FROM to TO.
3910 If FRAME is t, copy the global face definition of FROM.
3911 Otherwise, copy the frame-local definition of FROM on FRAME.
3912 If NEW-FRAME is a frame, copy that data into the frame-local
3913 definition of TO on NEW-FRAME. If NEW-FRAME is nil.
3914 FRAME controls where the data is copied to.
3916 The value is TO. */)
3917 (from, to, frame, new_frame)
3918 Lisp_Object from, to, frame, new_frame;
3920 Lisp_Object lface, copy;
3922 CHECK_SYMBOL (from);
3923 CHECK_SYMBOL (to);
3925 if (EQ (frame, Qt))
3927 /* Copy global definition of FROM. We don't make copies of
3928 strings etc. because 20.2 didn't do it either. */
3929 lface = lface_from_face_name (NULL, from, 1);
3930 copy = Finternal_make_lisp_face (to, Qnil);
3932 else
3934 /* Copy frame-local definition of FROM. */
3935 if (NILP (new_frame))
3936 new_frame = frame;
3937 CHECK_LIVE_FRAME (frame);
3938 CHECK_LIVE_FRAME (new_frame);
3939 lface = lface_from_face_name (XFRAME (frame), from, 1);
3940 copy = Finternal_make_lisp_face (to, new_frame);
3943 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3944 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
3946 /* Changing a named face means that all realized faces depending on
3947 that face are invalid. Since we cannot tell which realized faces
3948 depend on the face, make sure they are all removed. This is done
3949 by incrementing face_change_count. The next call to
3950 init_iterator will then free realized faces. */
3951 if (NILP (Fget (to, Qface_no_inherit)))
3953 ++face_change_count;
3954 ++windows_or_buffers_changed;
3957 return to;
3961 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3962 Sinternal_set_lisp_face_attribute, 3, 4, 0,
3963 doc: /* Set attribute ATTR of FACE to VALUE.
3964 FRAME being a frame means change the face on that frame.
3965 FRAME nil means change the face of the selected frame.
3966 FRAME t means change the default for new frames.
3967 FRAME 0 means change the face on all frames, and change the default
3968 for new frames. */)
3969 (face, attr, value, frame)
3970 Lisp_Object face, attr, value, frame;
3972 Lisp_Object lface;
3973 Lisp_Object old_value = Qnil;
3974 /* Set 1 if ATTR is QCfont. */
3975 int font_attr_p = 0;
3976 /* Set 1 if ATTR is one of font-related attributes other than QCfont. */
3977 int font_related_attr_p = 0;
3979 CHECK_SYMBOL (face);
3980 CHECK_SYMBOL (attr);
3982 face = resolve_face_name (face);
3984 /* If FRAME is 0, change face on all frames, and change the
3985 default for new frames. */
3986 if (INTEGERP (frame) && XINT (frame) == 0)
3988 Lisp_Object tail;
3989 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
3990 FOR_EACH_FRAME (tail, frame)
3991 Finternal_set_lisp_face_attribute (face, attr, value, frame);
3992 return face;
3995 /* Set lface to the Lisp attribute vector of FACE. */
3996 if (EQ (frame, Qt))
3997 lface = lface_from_face_name (NULL, face, 1);
3998 else
4000 if (NILP (frame))
4001 frame = selected_frame;
4003 CHECK_LIVE_FRAME (frame);
4004 lface = lface_from_face_name (XFRAME (frame), face, 0);
4006 /* If a frame-local face doesn't exist yet, create one. */
4007 if (NILP (lface))
4008 lface = Finternal_make_lisp_face (face, frame);
4011 if (EQ (attr, QCfamily))
4013 if (!UNSPECIFIEDP (value))
4015 CHECK_STRING (value);
4016 if (SCHARS (value) == 0)
4017 signal_error ("Invalid face family", value);
4019 old_value = LFACE_FAMILY (lface);
4020 LFACE_FAMILY (lface) = value;
4021 font_related_attr_p = 1;
4023 else if (EQ (attr, QCheight))
4025 if (!UNSPECIFIEDP (value))
4027 Lisp_Object test;
4029 test = (EQ (face, Qdefault)
4030 ? value
4031 /* The default face must have an absolute size,
4032 otherwise, we do a test merge with a random
4033 height to see if VALUE's ok. */
4034 : merge_face_heights (value, make_number (10), Qnil, Qnil));
4036 if (!INTEGERP (test) || XINT (test) <= 0)
4037 signal_error ("Invalid face height", value);
4040 old_value = LFACE_HEIGHT (lface);
4041 LFACE_HEIGHT (lface) = value;
4042 font_related_attr_p = 1;
4044 else if (EQ (attr, QCweight))
4046 if (!UNSPECIFIEDP (value))
4048 CHECK_SYMBOL (value);
4049 if (face_numeric_weight (value) < 0)
4050 signal_error ("Invalid face weight", value);
4052 old_value = LFACE_WEIGHT (lface);
4053 LFACE_WEIGHT (lface) = value;
4054 font_related_attr_p = 1;
4056 else if (EQ (attr, QCslant))
4058 if (!UNSPECIFIEDP (value))
4060 CHECK_SYMBOL (value);
4061 if (face_numeric_slant (value) < 0)
4062 signal_error ("Invalid face slant", value);
4064 old_value = LFACE_SLANT (lface);
4065 LFACE_SLANT (lface) = value;
4066 font_related_attr_p = 1;
4068 else if (EQ (attr, QCunderline))
4070 if (!UNSPECIFIEDP (value))
4071 if ((SYMBOLP (value)
4072 && !EQ (value, Qt)
4073 && !EQ (value, Qnil))
4074 /* Underline color. */
4075 || (STRINGP (value)
4076 && SCHARS (value) == 0))
4077 signal_error ("Invalid face underline", value);
4079 old_value = LFACE_UNDERLINE (lface);
4080 LFACE_UNDERLINE (lface) = value;
4082 else if (EQ (attr, QCoverline))
4084 if (!UNSPECIFIEDP (value))
4085 if ((SYMBOLP (value)
4086 && !EQ (value, Qt)
4087 && !EQ (value, Qnil))
4088 /* Overline color. */
4089 || (STRINGP (value)
4090 && SCHARS (value) == 0))
4091 signal_error ("Invalid face overline", value);
4093 old_value = LFACE_OVERLINE (lface);
4094 LFACE_OVERLINE (lface) = value;
4096 else if (EQ (attr, QCstrike_through))
4098 if (!UNSPECIFIEDP (value))
4099 if ((SYMBOLP (value)
4100 && !EQ (value, Qt)
4101 && !EQ (value, Qnil))
4102 /* Strike-through color. */
4103 || (STRINGP (value)
4104 && SCHARS (value) == 0))
4105 signal_error ("Invalid face strike-through", value);
4107 old_value = LFACE_STRIKE_THROUGH (lface);
4108 LFACE_STRIKE_THROUGH (lface) = value;
4110 else if (EQ (attr, QCbox))
4112 int valid_p;
4114 /* Allow t meaning a simple box of width 1 in foreground color
4115 of the face. */
4116 if (EQ (value, Qt))
4117 value = make_number (1);
4119 if (UNSPECIFIEDP (value))
4120 valid_p = 1;
4121 else if (NILP (value))
4122 valid_p = 1;
4123 else if (INTEGERP (value))
4124 valid_p = XINT (value) != 0;
4125 else if (STRINGP (value))
4126 valid_p = SCHARS (value) > 0;
4127 else if (CONSP (value))
4129 Lisp_Object tem;
4131 tem = value;
4132 while (CONSP (tem))
4134 Lisp_Object k, v;
4136 k = XCAR (tem);
4137 tem = XCDR (tem);
4138 if (!CONSP (tem))
4139 break;
4140 v = XCAR (tem);
4141 tem = XCDR (tem);
4143 if (EQ (k, QCline_width))
4145 if (!INTEGERP (v) || XINT (v) == 0)
4146 break;
4148 else if (EQ (k, QCcolor))
4150 if (!STRINGP (v) || SCHARS (v) == 0)
4151 break;
4153 else if (EQ (k, QCstyle))
4155 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
4156 break;
4158 else
4159 break;
4162 valid_p = NILP (tem);
4164 else
4165 valid_p = 0;
4167 if (!valid_p)
4168 signal_error ("Invalid face box", value);
4170 old_value = LFACE_BOX (lface);
4171 LFACE_BOX (lface) = value;
4173 else if (EQ (attr, QCinverse_video)
4174 || EQ (attr, QCreverse_video))
4176 if (!UNSPECIFIEDP (value))
4178 CHECK_SYMBOL (value);
4179 if (!EQ (value, Qt) && !NILP (value))
4180 signal_error ("Invalid inverse-video face attribute value", value);
4182 old_value = LFACE_INVERSE (lface);
4183 LFACE_INVERSE (lface) = value;
4185 else if (EQ (attr, QCforeground))
4187 if (!UNSPECIFIEDP (value))
4189 /* Don't check for valid color names here because it depends
4190 on the frame (display) whether the color will be valid
4191 when the face is realized. */
4192 CHECK_STRING (value);
4193 if (SCHARS (value) == 0)
4194 signal_error ("Empty foreground color value", value);
4196 old_value = LFACE_FOREGROUND (lface);
4197 LFACE_FOREGROUND (lface) = value;
4199 else if (EQ (attr, QCbackground))
4201 if (!UNSPECIFIEDP (value))
4203 /* Don't check for valid color names here because it depends
4204 on the frame (display) whether the color will be valid
4205 when the face is realized. */
4206 CHECK_STRING (value);
4207 if (SCHARS (value) == 0)
4208 signal_error ("Empty background color value", value);
4210 old_value = LFACE_BACKGROUND (lface);
4211 LFACE_BACKGROUND (lface) = value;
4213 else if (EQ (attr, QCstipple))
4215 #ifdef HAVE_X_WINDOWS
4216 if (!UNSPECIFIEDP (value)
4217 && !NILP (value)
4218 && NILP (Fbitmap_spec_p (value)))
4219 signal_error ("Invalid stipple attribute", value);
4220 old_value = LFACE_STIPPLE (lface);
4221 LFACE_STIPPLE (lface) = value;
4222 #endif /* HAVE_X_WINDOWS */
4224 else if (EQ (attr, QCwidth))
4226 if (!UNSPECIFIEDP (value))
4228 CHECK_SYMBOL (value);
4229 if (face_numeric_swidth (value) < 0)
4230 signal_error ("Invalid face width", value);
4232 old_value = LFACE_SWIDTH (lface);
4233 LFACE_SWIDTH (lface) = value;
4234 font_related_attr_p = 1;
4236 else if (EQ (attr, QCfont))
4238 #ifdef HAVE_WINDOW_SYSTEM
4239 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
4241 /* Set font-related attributes of the Lisp face from an XLFD
4242 font name. */
4243 struct frame *f;
4244 Lisp_Object tmp;
4246 if (EQ (frame, Qt))
4247 f = SELECTED_FRAME ();
4248 else
4249 f = check_x_frame (frame);
4251 if (!UNSPECIFIEDP (value))
4253 CHECK_STRING (value);
4255 /* VALUE may be a fontset name or an alias of fontset. In
4256 such a case, use the base fontset name. */
4257 tmp = Fquery_fontset (value, Qnil);
4258 if (!NILP (tmp))
4259 value = tmp;
4261 if (!set_lface_from_font_name (f, lface, value, 1, 1))
4262 signal_error ("Invalid font or fontset name", value);
4265 font_attr_p = 1;
4267 #endif /* HAVE_WINDOW_SYSTEM */
4269 else if (EQ (attr, QCinherit))
4271 Lisp_Object tail;
4272 if (SYMBOLP (value))
4273 tail = Qnil;
4274 else
4275 for (tail = value; CONSP (tail); tail = XCDR (tail))
4276 if (!SYMBOLP (XCAR (tail)))
4277 break;
4278 if (NILP (tail))
4279 LFACE_INHERIT (lface) = value;
4280 else
4281 signal_error ("Invalid face inheritance", value);
4283 else if (EQ (attr, QCbold))
4285 old_value = LFACE_WEIGHT (lface);
4286 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
4287 font_related_attr_p = 1;
4289 else if (EQ (attr, QCitalic))
4291 old_value = LFACE_SLANT (lface);
4292 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
4293 font_related_attr_p = 1;
4295 else
4296 signal_error ("Invalid face attribute name", attr);
4298 if (font_related_attr_p
4299 && !UNSPECIFIEDP (value))
4300 /* If a font-related attribute other than QCfont is specified, the
4301 original `font' attribute nor that of default face is useless
4302 to determine a new font. Thus, we set it to nil so that font
4303 selection mechanism doesn't use it. */
4304 LFACE_FONT (lface) = Qnil;
4306 /* Changing a named face means that all realized faces depending on
4307 that face are invalid. Since we cannot tell which realized faces
4308 depend on the face, make sure they are all removed. This is done
4309 by incrementing face_change_count. The next call to
4310 init_iterator will then free realized faces. */
4311 if (!EQ (frame, Qt)
4312 && NILP (Fget (face, Qface_no_inherit))
4313 && (EQ (attr, QCfont)
4314 || NILP (Fequal (old_value, value))))
4316 ++face_change_count;
4317 ++windows_or_buffers_changed;
4320 if (!UNSPECIFIEDP (value)
4321 && NILP (Fequal (old_value, value)))
4323 Lisp_Object param;
4325 param = Qnil;
4327 if (EQ (face, Qdefault))
4329 #ifdef HAVE_WINDOW_SYSTEM
4330 /* Changed font-related attributes of the `default' face are
4331 reflected in changed `font' frame parameters. */
4332 if (FRAMEP (frame)
4333 && (font_related_attr_p || font_attr_p)
4334 && lface_fully_specified_p (XVECTOR (lface)->contents))
4335 set_font_frame_param (frame, lface);
4336 else
4337 #endif /* HAVE_WINDOW_SYSTEM */
4339 if (EQ (attr, QCforeground))
4340 param = Qforeground_color;
4341 else if (EQ (attr, QCbackground))
4342 param = Qbackground_color;
4344 #ifdef HAVE_WINDOW_SYSTEM
4345 #ifndef WINDOWSNT
4346 else if (EQ (face, Qscroll_bar))
4348 /* Changing the colors of `scroll-bar' sets frame parameters
4349 `scroll-bar-foreground' and `scroll-bar-background'. */
4350 if (EQ (attr, QCforeground))
4351 param = Qscroll_bar_foreground;
4352 else if (EQ (attr, QCbackground))
4353 param = Qscroll_bar_background;
4355 #endif /* not WINDOWSNT */
4356 else if (EQ (face, Qborder))
4358 /* Changing background color of `border' sets frame parameter
4359 `border-color'. */
4360 if (EQ (attr, QCbackground))
4361 param = Qborder_color;
4363 else if (EQ (face, Qcursor))
4365 /* Changing background color of `cursor' sets frame parameter
4366 `cursor-color'. */
4367 if (EQ (attr, QCbackground))
4368 param = Qcursor_color;
4370 else if (EQ (face, Qmouse))
4372 /* Changing background color of `mouse' sets frame parameter
4373 `mouse-color'. */
4374 if (EQ (attr, QCbackground))
4375 param = Qmouse_color;
4377 #endif /* HAVE_WINDOW_SYSTEM */
4378 else if (EQ (face, Qmenu))
4380 /* Indicate that we have to update the menu bar when
4381 realizing faces on FRAME. FRAME t change the
4382 default for new frames. We do this by setting
4383 setting the flag in new face caches */
4384 if (FRAMEP (frame))
4386 struct frame *f = XFRAME (frame);
4387 if (FRAME_FACE_CACHE (f) == NULL)
4388 FRAME_FACE_CACHE (f) = make_face_cache (f);
4389 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
4391 else
4392 menu_face_changed_default = 1;
4395 if (!NILP (param))
4397 if (EQ (frame, Qt))
4398 /* Update `default-frame-alist', which is used for new frames. */
4400 store_in_alist (&Vdefault_frame_alist, param, value);
4402 else
4403 /* Update the current frame's parameters. */
4405 Lisp_Object cons;
4406 cons = XCAR (Vparam_value_alist);
4407 XSETCAR (cons, param);
4408 XSETCDR (cons, value);
4409 Fmodify_frame_parameters (frame, Vparam_value_alist);
4414 return face;
4418 #ifdef HAVE_WINDOW_SYSTEM
4420 /* Set the `font' frame parameter of FRAME determined from `default'
4421 face attributes LFACE. If a face or fontset name is explicitely
4422 specfied in LFACE, use it as is. Otherwise, determine a font name
4423 from the other font-related atrributes of LFACE. In that case, if
4424 there's no matching font, signals an error. */
4426 static void
4427 set_font_frame_param (frame, lface)
4428 Lisp_Object frame, lface;
4430 struct frame *f = XFRAME (frame);
4432 if (FRAME_WINDOW_P (f))
4434 Lisp_Object font_name;
4435 char *font;
4437 if (STRINGP (LFACE_FONT (lface)))
4438 font_name = LFACE_FONT (lface);
4439 else
4441 /* Choose a font name that reflects LFACE's attributes and has
4442 the registry and encoding pattern specified in the default
4443 fontset (3rd arg: -1) for ASCII characters (4th arg: 0). */
4444 font = choose_face_font (f, XVECTOR (lface)->contents, -1, 0, 0);
4445 if (!font)
4446 error ("No font matches the specified attribute");
4447 font_name = build_string (font);
4448 xfree (font);
4451 f->default_face_done_p = 0;
4452 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font_name), Qnil));
4457 /* Update the corresponding face when frame parameter PARAM on frame F
4458 has been assigned the value NEW_VALUE. */
4460 void
4461 update_face_from_frame_parameter (f, param, new_value)
4462 struct frame *f;
4463 Lisp_Object param, new_value;
4465 Lisp_Object face = Qnil;
4466 Lisp_Object lface;
4468 /* If there are no faces yet, give up. This is the case when called
4469 from Fx_create_frame, and we do the necessary things later in
4470 face-set-after-frame-defaults. */
4471 if (NILP (f->face_alist))
4472 return;
4474 if (EQ (param, Qforeground_color))
4476 face = Qdefault;
4477 lface = lface_from_face_name (f, face, 1);
4478 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
4479 ? new_value : Qunspecified);
4480 realize_basic_faces (f);
4482 else if (EQ (param, Qbackground_color))
4484 Lisp_Object frame;
4486 /* Changing the background color might change the background
4487 mode, so that we have to load new defface specs. Call
4488 frame-update-face-colors to do that. */
4489 XSETFRAME (frame, f);
4490 call1 (Qframe_update_face_colors, frame);
4492 face = Qdefault;
4493 lface = lface_from_face_name (f, face, 1);
4494 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4495 ? new_value : Qunspecified);
4496 realize_basic_faces (f);
4498 else if (EQ (param, Qborder_color))
4500 face = Qborder;
4501 lface = lface_from_face_name (f, face, 1);
4502 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4503 ? new_value : Qunspecified);
4505 else if (EQ (param, Qcursor_color))
4507 face = Qcursor;
4508 lface = lface_from_face_name (f, face, 1);
4509 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4510 ? new_value : Qunspecified);
4512 else if (EQ (param, Qmouse_color))
4514 face = Qmouse;
4515 lface = lface_from_face_name (f, face, 1);
4516 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4517 ? new_value : Qunspecified);
4520 /* Changing a named face means that all realized faces depending on
4521 that face are invalid. Since we cannot tell which realized faces
4522 depend on the face, make sure they are all removed. This is done
4523 by incrementing face_change_count. The next call to
4524 init_iterator will then free realized faces. */
4525 if (!NILP (face)
4526 && NILP (Fget (face, Qface_no_inherit)))
4528 ++face_change_count;
4529 ++windows_or_buffers_changed;
4534 /* Get the value of X resource RESOURCE, class CLASS for the display
4535 of frame FRAME. This is here because ordinary `x-get-resource'
4536 doesn't take a frame argument. */
4538 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
4539 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
4540 (resource, class, frame)
4541 Lisp_Object resource, class, frame;
4543 Lisp_Object value = Qnil;
4544 CHECK_STRING (resource);
4545 CHECK_STRING (class);
4546 CHECK_LIVE_FRAME (frame);
4547 BLOCK_INPUT;
4548 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
4549 resource, class, Qnil, Qnil);
4550 UNBLOCK_INPUT;
4551 return value;
4555 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
4556 If VALUE is "on" or "true", return t. If VALUE is "off" or
4557 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
4558 error; if SIGNAL_P is zero, return 0. */
4560 static Lisp_Object
4561 face_boolean_x_resource_value (value, signal_p)
4562 Lisp_Object value;
4563 int signal_p;
4565 Lisp_Object result = make_number (0);
4567 xassert (STRINGP (value));
4569 if (xstricmp (SDATA (value), "on") == 0
4570 || xstricmp (SDATA (value), "true") == 0)
4571 result = Qt;
4572 else if (xstricmp (SDATA (value), "off") == 0
4573 || xstricmp (SDATA (value), "false") == 0)
4574 result = Qnil;
4575 else if (xstricmp (SDATA (value), "unspecified") == 0)
4576 result = Qunspecified;
4577 else if (signal_p)
4578 signal_error ("Invalid face attribute value from X resource", value);
4580 return result;
4584 DEFUN ("internal-set-lisp-face-attribute-from-resource",
4585 Finternal_set_lisp_face_attribute_from_resource,
4586 Sinternal_set_lisp_face_attribute_from_resource,
4587 3, 4, 0, doc: /* */)
4588 (face, attr, value, frame)
4589 Lisp_Object face, attr, value, frame;
4591 CHECK_SYMBOL (face);
4592 CHECK_SYMBOL (attr);
4593 CHECK_STRING (value);
4595 if (xstricmp (SDATA (value), "unspecified") == 0)
4596 value = Qunspecified;
4597 else if (EQ (attr, QCheight))
4599 value = Fstring_to_number (value, make_number (10));
4600 if (XINT (value) <= 0)
4601 signal_error ("Invalid face height from X resource", value);
4603 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
4604 value = face_boolean_x_resource_value (value, 1);
4605 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
4606 value = intern (SDATA (value));
4607 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
4608 value = face_boolean_x_resource_value (value, 1);
4609 else if (EQ (attr, QCunderline)
4610 || EQ (attr, QCoverline)
4611 || EQ (attr, QCstrike_through))
4613 Lisp_Object boolean_value;
4615 /* If the result of face_boolean_x_resource_value is t or nil,
4616 VALUE does NOT specify a color. */
4617 boolean_value = face_boolean_x_resource_value (value, 0);
4618 if (SYMBOLP (boolean_value))
4619 value = boolean_value;
4621 else if (EQ (attr, QCbox))
4622 value = Fcar (Fread_from_string (value, Qnil, Qnil));
4624 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
4627 #endif /* HAVE_WINDOW_SYSTEM */
4630 /***********************************************************************
4631 Menu face
4632 ***********************************************************************/
4634 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
4636 /* Make menus on frame F appear as specified by the `menu' face. */
4638 static void
4639 x_update_menu_appearance (f)
4640 struct frame *f;
4642 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4643 XrmDatabase rdb;
4645 if (dpyinfo
4646 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
4647 rdb != NULL))
4649 char line[512];
4650 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
4651 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
4652 const char *myname = SDATA (Vx_resource_name);
4653 int changed_p = 0;
4654 #ifdef USE_MOTIF
4655 const char *popup_path = "popup_menu";
4656 #else
4657 const char *popup_path = "menu.popup";
4658 #endif
4660 if (STRINGP (LFACE_FOREGROUND (lface)))
4662 sprintf (line, "%s.%s*foreground: %s",
4663 myname, popup_path,
4664 SDATA (LFACE_FOREGROUND (lface)));
4665 XrmPutLineResource (&rdb, line);
4666 sprintf (line, "%s.pane.menubar*foreground: %s",
4667 myname, SDATA (LFACE_FOREGROUND (lface)));
4668 XrmPutLineResource (&rdb, line);
4669 changed_p = 1;
4672 if (STRINGP (LFACE_BACKGROUND (lface)))
4674 sprintf (line, "%s.%s*background: %s",
4675 myname, popup_path,
4676 SDATA (LFACE_BACKGROUND (lface)));
4677 XrmPutLineResource (&rdb, line);
4678 sprintf (line, "%s.pane.menubar*background: %s",
4679 myname, SDATA (LFACE_BACKGROUND (lface)));
4680 XrmPutLineResource (&rdb, line);
4681 changed_p = 1;
4684 if (face->font_name
4685 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
4686 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
4687 || !UNSPECIFIEDP (LFACE_AVGWIDTH (lface))
4688 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
4689 || !UNSPECIFIEDP (LFACE_SLANT (lface))
4690 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
4692 #ifdef USE_MOTIF
4693 const char *suffix = "List";
4694 #else
4695 const char *suffix = "";
4696 #endif
4697 sprintf (line, "%s.pane.menubar*font%s: %s",
4698 myname, suffix, face->font_name);
4699 XrmPutLineResource (&rdb, line);
4700 sprintf (line, "%s.%s*font%s: %s",
4701 myname, popup_path, suffix, face->font_name);
4702 XrmPutLineResource (&rdb, line);
4703 changed_p = 1;
4706 if (changed_p && f->output_data.x->menubar_widget)
4707 free_frame_menubar (f);
4711 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
4714 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
4715 Sface_attribute_relative_p,
4716 2, 2, 0,
4717 doc: /* Return non-nil if face ATTRIBUTE VALUE is relative. */)
4718 (attribute, value)
4719 Lisp_Object attribute, value;
4721 if (EQ (value, Qunspecified))
4722 return Qt;
4723 else if (EQ (attribute, QCheight))
4724 return INTEGERP (value) ? Qnil : Qt;
4725 else
4726 return Qnil;
4729 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
4730 3, 3, 0,
4731 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
4732 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
4733 the result will be absolute, otherwise it will be relative. */)
4734 (attribute, value1, value2)
4735 Lisp_Object attribute, value1, value2;
4737 if (EQ (value1, Qunspecified))
4738 return value2;
4739 else if (EQ (attribute, QCheight))
4740 return merge_face_heights (value1, value2, value1, Qnil);
4741 else
4742 return value1;
4746 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
4747 Sinternal_get_lisp_face_attribute,
4748 2, 3, 0,
4749 doc: /* Return face attribute KEYWORD of face SYMBOL.
4750 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
4751 face attribute name, signal an error.
4752 If the optional argument FRAME is given, report on face SYMBOL in that
4753 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
4754 frames). If FRAME is omitted or nil, use the selected frame. */)
4755 (symbol, keyword, frame)
4756 Lisp_Object symbol, keyword, frame;
4758 Lisp_Object lface, value = Qnil;
4760 CHECK_SYMBOL (symbol);
4761 CHECK_SYMBOL (keyword);
4763 if (EQ (frame, Qt))
4764 lface = lface_from_face_name (NULL, symbol, 1);
4765 else
4767 if (NILP (frame))
4768 frame = selected_frame;
4769 CHECK_LIVE_FRAME (frame);
4770 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
4773 if (EQ (keyword, QCfamily))
4774 value = LFACE_FAMILY (lface);
4775 else if (EQ (keyword, QCheight))
4776 value = LFACE_HEIGHT (lface);
4777 else if (EQ (keyword, QCweight))
4778 value = LFACE_WEIGHT (lface);
4779 else if (EQ (keyword, QCslant))
4780 value = LFACE_SLANT (lface);
4781 else if (EQ (keyword, QCunderline))
4782 value = LFACE_UNDERLINE (lface);
4783 else if (EQ (keyword, QCoverline))
4784 value = LFACE_OVERLINE (lface);
4785 else if (EQ (keyword, QCstrike_through))
4786 value = LFACE_STRIKE_THROUGH (lface);
4787 else if (EQ (keyword, QCbox))
4788 value = LFACE_BOX (lface);
4789 else if (EQ (keyword, QCinverse_video)
4790 || EQ (keyword, QCreverse_video))
4791 value = LFACE_INVERSE (lface);
4792 else if (EQ (keyword, QCforeground))
4793 value = LFACE_FOREGROUND (lface);
4794 else if (EQ (keyword, QCbackground))
4795 value = LFACE_BACKGROUND (lface);
4796 else if (EQ (keyword, QCstipple))
4797 value = LFACE_STIPPLE (lface);
4798 else if (EQ (keyword, QCwidth))
4799 value = LFACE_SWIDTH (lface);
4800 else if (EQ (keyword, QCinherit))
4801 value = LFACE_INHERIT (lface);
4802 else if (EQ (keyword, QCfont))
4803 value = LFACE_FONT (lface);
4804 else
4805 signal_error ("Invalid face attribute name", keyword);
4807 return value;
4811 DEFUN ("internal-lisp-face-attribute-values",
4812 Finternal_lisp_face_attribute_values,
4813 Sinternal_lisp_face_attribute_values, 1, 1, 0,
4814 doc: /* Return a list of valid discrete values for face attribute ATTR.
4815 Value is nil if ATTR doesn't have a discrete set of valid values. */)
4816 (attr)
4817 Lisp_Object attr;
4819 Lisp_Object result = Qnil;
4821 CHECK_SYMBOL (attr);
4823 if (EQ (attr, QCweight)
4824 || EQ (attr, QCslant)
4825 || EQ (attr, QCwidth))
4827 /* Extract permissible symbols from tables. */
4828 struct table_entry *table;
4829 int i, dim;
4831 if (EQ (attr, QCweight))
4832 table = weight_table, dim = DIM (weight_table);
4833 else if (EQ (attr, QCslant))
4834 table = slant_table, dim = DIM (slant_table);
4835 else
4836 table = swidth_table, dim = DIM (swidth_table);
4838 for (i = 0; i < dim; ++i)
4840 Lisp_Object symbol = *table[i].symbol;
4841 Lisp_Object tail = result;
4843 while (!NILP (tail)
4844 && !EQ (XCAR (tail), symbol))
4845 tail = XCDR (tail);
4847 if (NILP (tail))
4848 result = Fcons (symbol, result);
4851 else if (EQ (attr, QCunderline))
4852 result = Fcons (Qt, Fcons (Qnil, Qnil));
4853 else if (EQ (attr, QCoverline))
4854 result = Fcons (Qt, Fcons (Qnil, Qnil));
4855 else if (EQ (attr, QCstrike_through))
4856 result = Fcons (Qt, Fcons (Qnil, Qnil));
4857 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
4858 result = Fcons (Qt, Fcons (Qnil, Qnil));
4860 return result;
4864 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
4865 Sinternal_merge_in_global_face, 2, 2, 0,
4866 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
4867 Default face attributes override any local face attributes. */)
4868 (face, frame)
4869 Lisp_Object face, frame;
4871 int i;
4872 Lisp_Object global_lface, local_lface, *gvec, *lvec;
4874 CHECK_LIVE_FRAME (frame);
4875 global_lface = lface_from_face_name (NULL, face, 1);
4876 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
4877 if (NILP (local_lface))
4878 local_lface = Finternal_make_lisp_face (face, frame);
4880 /* Make every specified global attribute override the local one.
4881 BEWARE!! This is only used from `face-set-after-frame-default' where
4882 the local frame is defined from default specs in `face-defface-spec'
4883 and those should be overridden by global settings. Hence the strange
4884 "global before local" priority. */
4885 lvec = XVECTOR (local_lface)->contents;
4886 gvec = XVECTOR (global_lface)->contents;
4887 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4888 if (! UNSPECIFIEDP (gvec[i]))
4889 lvec[i] = gvec[i];
4891 return Qnil;
4895 /* The following function is implemented for compatibility with 20.2.
4896 The function is used in x-resolve-fonts when it is asked to
4897 return fonts with the same size as the font of a face. This is
4898 done in fontset.el. */
4900 DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
4901 doc: /* Return the font name of face FACE, or nil if it is unspecified.
4902 If the optional argument FRAME is given, report on face FACE in that frame.
4903 If FRAME is t, report on the defaults for face FACE (for new frames).
4904 The font default for a face is either nil, or a list
4905 of the form (bold), (italic) or (bold italic).
4906 If FRAME is omitted or nil, use the selected frame. */)
4907 (face, frame)
4908 Lisp_Object face, frame;
4910 if (EQ (frame, Qt))
4912 Lisp_Object result = Qnil;
4913 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
4915 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
4916 && !EQ (LFACE_WEIGHT (lface), Qnormal))
4917 result = Fcons (Qbold, result);
4919 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
4920 && !EQ (LFACE_SLANT (lface), Qnormal))
4921 result = Fcons (Qitalic, result);
4923 return result;
4925 else
4927 struct frame *f = frame_or_selected_frame (frame, 1);
4928 int face_id = lookup_named_face (f, face, 0);
4929 struct face *face = FACE_FROM_ID (f, face_id);
4930 return face ? build_string (face->font_name) : Qnil;
4935 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
4936 all attributes are `equal'. Tries to be fast because this function
4937 is called quite often. */
4939 static INLINE int
4940 face_attr_equal_p (v1, v2)
4941 Lisp_Object v1, v2;
4943 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
4944 and the other is specified. */
4945 if (XTYPE (v1) != XTYPE (v2))
4946 return 0;
4948 if (EQ (v1, v2))
4949 return 1;
4951 switch (XTYPE (v1))
4953 case Lisp_String:
4954 if (SBYTES (v1) != SBYTES (v2))
4955 return 0;
4957 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
4959 case Lisp_Int:
4960 case Lisp_Symbol:
4961 return 0;
4963 default:
4964 return !NILP (Fequal (v1, v2));
4969 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
4970 all attributes are `equal'. Tries to be fast because this function
4971 is called quite often. */
4973 static INLINE int
4974 lface_equal_p (v1, v2)
4975 Lisp_Object *v1, *v2;
4977 int i, equal_p = 1;
4979 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
4980 equal_p = face_attr_equal_p (v1[i], v2[i]);
4982 return equal_p;
4986 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
4987 Sinternal_lisp_face_equal_p, 2, 3, 0,
4988 doc: /* True if FACE1 and FACE2 are equal.
4989 If the optional argument FRAME is given, report on face FACE in that frame.
4990 If FRAME is t, report on the defaults for face FACE (for new frames).
4991 If FRAME is omitted or nil, use the selected frame. */)
4992 (face1, face2, frame)
4993 Lisp_Object face1, face2, frame;
4995 int equal_p;
4996 struct frame *f;
4997 Lisp_Object lface1, lface2;
4999 if (EQ (frame, Qt))
5000 f = NULL;
5001 else
5002 /* Don't use check_x_frame here because this function is called
5003 before X frames exist. At that time, if FRAME is nil,
5004 selected_frame will be used which is the frame dumped with
5005 Emacs. That frame is not an X frame. */
5006 f = frame_or_selected_frame (frame, 2);
5008 lface1 = lface_from_face_name (NULL, face1, 1);
5009 lface2 = lface_from_face_name (NULL, face2, 1);
5010 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
5011 XVECTOR (lface2)->contents);
5012 return equal_p ? Qt : Qnil;
5016 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
5017 Sinternal_lisp_face_empty_p, 1, 2, 0,
5018 doc: /* True if FACE has no attribute specified.
5019 If the optional argument FRAME is given, report on face FACE in that frame.
5020 If FRAME is t, report on the defaults for face FACE (for new frames).
5021 If FRAME is omitted or nil, use the selected frame. */)
5022 (face, frame)
5023 Lisp_Object face, frame;
5025 struct frame *f;
5026 Lisp_Object lface;
5027 int i;
5029 if (NILP (frame))
5030 frame = selected_frame;
5031 CHECK_LIVE_FRAME (frame);
5032 f = XFRAME (frame);
5034 if (EQ (frame, Qt))
5035 lface = lface_from_face_name (NULL, face, 1);
5036 else
5037 lface = lface_from_face_name (f, face, 1);
5039 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
5040 if (!UNSPECIFIEDP (AREF (lface, i)))
5041 break;
5043 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
5047 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
5048 0, 1, 0,
5049 doc: /* Return an alist of frame-local faces defined on FRAME.
5050 For internal use only. */)
5051 (frame)
5052 Lisp_Object frame;
5054 struct frame *f = frame_or_selected_frame (frame, 0);
5055 return f->face_alist;
5059 /* Return a hash code for Lisp string STRING with case ignored. Used
5060 below in computing a hash value for a Lisp face. */
5062 static INLINE unsigned
5063 hash_string_case_insensitive (string)
5064 Lisp_Object string;
5066 const unsigned char *s;
5067 unsigned hash = 0;
5068 xassert (STRINGP (string));
5069 for (s = SDATA (string); *s; ++s)
5070 hash = (hash << 1) ^ tolower (*s);
5071 return hash;
5075 /* Return a hash code for face attribute vector V. */
5077 static INLINE unsigned
5078 lface_hash (v)
5079 Lisp_Object *v;
5081 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
5082 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
5083 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
5084 ^ XFASTINT (v[LFACE_WEIGHT_INDEX])
5085 ^ XFASTINT (v[LFACE_SLANT_INDEX])
5086 ^ XFASTINT (v[LFACE_SWIDTH_INDEX])
5087 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
5091 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
5092 considering charsets/registries). They do if they specify the same
5093 family, point size, weight, width, slant, and fontset. Both LFACE1
5094 and LFACE2 must be fully-specified. */
5096 static INLINE int
5097 lface_same_font_attributes_p (lface1, lface2)
5098 Lisp_Object *lface1, *lface2;
5100 xassert (lface_fully_specified_p (lface1)
5101 && lface_fully_specified_p (lface2));
5102 return (xstricmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
5103 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
5104 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
5105 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
5106 && EQ (lface1[LFACE_AVGWIDTH_INDEX], lface2[LFACE_AVGWIDTH_INDEX])
5107 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
5108 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
5109 && (EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
5110 || (STRINGP (lface1[LFACE_FONT_INDEX])
5111 && STRINGP (lface2[LFACE_FONT_INDEX])
5112 && xstricmp (SDATA (lface1[LFACE_FONT_INDEX]),
5113 SDATA (lface2[LFACE_FONT_INDEX])))));
5118 /***********************************************************************
5119 Realized Faces
5120 ***********************************************************************/
5122 /* Allocate and return a new realized face for Lisp face attribute
5123 vector ATTR. */
5125 static struct face *
5126 make_realized_face (attr)
5127 Lisp_Object *attr;
5129 struct face *face = (struct face *) xmalloc (sizeof *face);
5130 bzero (face, sizeof *face);
5131 face->ascii_face = face;
5132 bcopy (attr, face->lface, sizeof face->lface);
5133 return face;
5137 /* Free realized face FACE, including its X resources. FACE may
5138 be null. */
5140 static void
5141 free_realized_face (f, face)
5142 struct frame *f;
5143 struct face *face;
5145 if (face)
5147 #ifdef HAVE_WINDOW_SYSTEM
5148 if (FRAME_WINDOW_P (f))
5150 /* Free fontset of FACE if it is ASCII face. */
5151 if (face->fontset >= 0 && face == face->ascii_face)
5152 free_face_fontset (f, face);
5153 if (face->gc)
5155 x_free_gc (f, face->gc);
5156 face->gc = 0;
5159 free_face_colors (f, face);
5160 x_destroy_bitmap (f, face->stipple);
5162 #endif /* HAVE_WINDOW_SYSTEM */
5164 xfree (face);
5169 /* Prepare face FACE for subsequent display on frame F. This
5170 allocated GCs if they haven't been allocated yet or have been freed
5171 by clearing the face cache. */
5173 void
5174 prepare_face_for_display (f, face)
5175 struct frame *f;
5176 struct face *face;
5178 #ifdef HAVE_WINDOW_SYSTEM
5179 xassert (FRAME_WINDOW_P (f));
5181 if (face->gc == 0)
5183 XGCValues xgcv;
5184 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
5186 xgcv.foreground = face->foreground;
5187 xgcv.background = face->background;
5188 #ifdef HAVE_X_WINDOWS
5189 xgcv.graphics_exposures = False;
5190 #endif
5191 /* The font of FACE may be null if we couldn't load it. */
5192 if (face->font)
5194 #ifdef HAVE_X_WINDOWS
5195 xgcv.font = face->font->fid;
5196 #endif
5197 #ifdef WINDOWSNT
5198 xgcv.font = face->font;
5199 #endif
5200 #ifdef MAC_OS
5201 xgcv.font = face->font;
5202 #endif
5203 mask |= GCFont;
5206 BLOCK_INPUT;
5207 #ifdef HAVE_X_WINDOWS
5208 if (face->stipple)
5210 xgcv.fill_style = FillOpaqueStippled;
5211 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
5212 mask |= GCFillStyle | GCStipple;
5214 #endif
5215 face->gc = x_create_gc (f, mask, &xgcv);
5216 UNBLOCK_INPUT;
5218 #endif /* HAVE_WINDOW_SYSTEM */
5222 /* Returns the `distance' between the colors X and Y. */
5224 static int
5225 color_distance (x, y)
5226 XColor *x, *y;
5228 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
5229 Quoting from that paper:
5231 This formula has results that are very close to L*u*v* (with the
5232 modified lightness curve) and, more importantly, it is a more even
5233 algorithm: it does not have a range of colours where it suddenly
5234 gives far from optimal results.
5236 See <http://www.compuphase.com/cmetric.htm> for more info. */
5238 long r = (x->red - y->red) >> 8;
5239 long g = (x->green - y->green) >> 8;
5240 long b = (x->blue - y->blue) >> 8;
5241 long r_mean = (x->red + y->red) >> 9;
5243 return
5244 (((512 + r_mean) * r * r) >> 8)
5245 + 4 * g * g
5246 + (((767 - r_mean) * b * b) >> 8);
5250 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
5251 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
5252 COLOR1 and COLOR2 may be either strings containing the color name,
5253 or lists of the form (RED GREEN BLUE).
5254 If FRAME is unspecified or nil, the current frame is used. */)
5255 (color1, color2, frame)
5256 Lisp_Object color1, color2, frame;
5258 struct frame *f;
5259 XColor cdef1, cdef2;
5261 if (NILP (frame))
5262 frame = selected_frame;
5263 CHECK_LIVE_FRAME (frame);
5264 f = XFRAME (frame);
5266 if ((CONSP (color1) && !parse_rgb_list (color1, &cdef1))
5267 || !STRINGP (color1)
5268 || !defined_color (f, SDATA (color1), &cdef1, 0))
5269 signal_error ("Invalid color", color1);
5270 if ((CONSP (color2) && !parse_rgb_list (color2, &cdef2))
5271 || !STRINGP (color2)
5272 || !defined_color (f, SDATA (color2), &cdef2, 0))
5273 signal_error ("Invalid color", color2);
5275 return make_number (color_distance (&cdef1, &cdef2));
5279 /***********************************************************************
5280 Face Cache
5281 ***********************************************************************/
5283 /* Return a new face cache for frame F. */
5285 static struct face_cache *
5286 make_face_cache (f)
5287 struct frame *f;
5289 struct face_cache *c;
5290 int size;
5292 c = (struct face_cache *) xmalloc (sizeof *c);
5293 bzero (c, sizeof *c);
5294 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5295 c->buckets = (struct face **) xmalloc (size);
5296 bzero (c->buckets, size);
5297 c->size = 50;
5298 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
5299 c->f = f;
5300 c->menu_face_changed_p = menu_face_changed_default;
5301 return c;
5305 /* Clear out all graphics contexts for all realized faces, except for
5306 the basic faces. This should be done from time to time just to avoid
5307 keeping too many graphics contexts that are no longer needed. */
5309 static void
5310 clear_face_gcs (c)
5311 struct face_cache *c;
5313 if (c && FRAME_WINDOW_P (c->f))
5315 #ifdef HAVE_WINDOW_SYSTEM
5316 int i;
5317 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
5319 struct face *face = c->faces_by_id[i];
5320 if (face && face->gc)
5322 x_free_gc (c->f, face->gc);
5323 face->gc = 0;
5326 #endif /* HAVE_WINDOW_SYSTEM */
5331 /* Free all realized faces in face cache C, including basic faces.
5332 C may be null. If faces are freed, make sure the frame's current
5333 matrix is marked invalid, so that a display caused by an expose
5334 event doesn't try to use faces we destroyed. */
5336 static void
5337 free_realized_faces (c)
5338 struct face_cache *c;
5340 if (c && c->used)
5342 int i, size;
5343 struct frame *f = c->f;
5345 /* We must block input here because we can't process X events
5346 safely while only some faces are freed, or when the frame's
5347 current matrix still references freed faces. */
5348 BLOCK_INPUT;
5350 for (i = 0; i < c->used; ++i)
5352 free_realized_face (f, c->faces_by_id[i]);
5353 c->faces_by_id[i] = NULL;
5356 c->used = 0;
5357 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5358 bzero (c->buckets, size);
5360 /* Must do a thorough redisplay the next time. Mark current
5361 matrices as invalid because they will reference faces freed
5362 above. This function is also called when a frame is
5363 destroyed. In this case, the root window of F is nil. */
5364 if (WINDOWP (f->root_window))
5366 clear_current_matrices (f);
5367 ++windows_or_buffers_changed;
5370 UNBLOCK_INPUT;
5375 /* Free all faces realized for multibyte characters on frame F that
5376 has FONTSET. */
5378 void
5379 free_realized_multibyte_face (f, fontset)
5380 struct frame *f;
5381 int fontset;
5383 struct face_cache *cache = FRAME_FACE_CACHE (f);
5384 struct face *face;
5385 int i;
5387 /* We must block input here because we can't process X events safely
5388 while only some faces are freed, or when the frame's current
5389 matrix still references freed faces. */
5390 BLOCK_INPUT;
5392 for (i = 0; i < cache->used; i++)
5394 face = cache->faces_by_id[i];
5395 if (face
5396 && face != face->ascii_face
5397 && face->fontset == fontset)
5399 uncache_face (cache, face);
5400 free_realized_face (f, face);
5404 /* Must do a thorough redisplay the next time. Mark current
5405 matrices as invalid because they will reference faces freed
5406 above. This function is also called when a frame is destroyed.
5407 In this case, the root window of F is nil. */
5408 if (WINDOWP (f->root_window))
5410 clear_current_matrices (f);
5411 ++windows_or_buffers_changed;
5414 UNBLOCK_INPUT;
5418 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
5419 This is done after attributes of a named face have been changed,
5420 because we can't tell which realized faces depend on that face. */
5422 void
5423 free_all_realized_faces (frame)
5424 Lisp_Object frame;
5426 if (NILP (frame))
5428 Lisp_Object rest;
5429 FOR_EACH_FRAME (rest, frame)
5430 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5432 else
5433 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5437 /* Free face cache C and faces in it, including their X resources. */
5439 static void
5440 free_face_cache (c)
5441 struct face_cache *c;
5443 if (c)
5445 free_realized_faces (c);
5446 xfree (c->buckets);
5447 xfree (c->faces_by_id);
5448 xfree (c);
5453 /* Cache realized face FACE in face cache C. HASH is the hash value
5454 of FACE. If FACE->fontset >= 0, add the new face to the end of the
5455 collision list of the face hash table of C. This is done because
5456 otherwise lookup_face would find FACE for every character, even if
5457 faces with the same attributes but for specific characters exist. */
5459 static void
5460 cache_face (c, face, hash)
5461 struct face_cache *c;
5462 struct face *face;
5463 unsigned hash;
5465 int i = hash % FACE_CACHE_BUCKETS_SIZE;
5467 face->hash = hash;
5469 if (face->fontset >= 0)
5471 struct face *last = c->buckets[i];
5472 if (last)
5474 while (last->next)
5475 last = last->next;
5476 last->next = face;
5477 face->prev = last;
5478 face->next = NULL;
5480 else
5482 c->buckets[i] = face;
5483 face->prev = face->next = NULL;
5486 else
5488 face->prev = NULL;
5489 face->next = c->buckets[i];
5490 if (face->next)
5491 face->next->prev = face;
5492 c->buckets[i] = face;
5495 /* Find a free slot in C->faces_by_id and use the index of the free
5496 slot as FACE->id. */
5497 for (i = 0; i < c->used; ++i)
5498 if (c->faces_by_id[i] == NULL)
5499 break;
5500 face->id = i;
5502 /* Maybe enlarge C->faces_by_id. */
5503 if (i == c->used)
5505 if (c->used == c->size)
5507 int new_size, sz;
5508 new_size = min (2 * c->size, MAX_FACE_ID);
5509 if (new_size == c->size)
5510 abort (); /* Alternatives? ++kfs */
5511 sz = new_size * sizeof *c->faces_by_id;
5512 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
5513 c->size = new_size;
5515 c->used++;
5518 #if GLYPH_DEBUG
5519 /* Check that FACE got a unique id. */
5521 int j, n;
5522 struct face *face;
5524 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
5525 for (face = c->buckets[j]; face; face = face->next)
5526 if (face->id == i)
5527 ++n;
5529 xassert (n == 1);
5531 #endif /* GLYPH_DEBUG */
5533 c->faces_by_id[i] = face;
5537 /* Remove face FACE from cache C. */
5539 static void
5540 uncache_face (c, face)
5541 struct face_cache *c;
5542 struct face *face;
5544 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
5546 if (face->prev)
5547 face->prev->next = face->next;
5548 else
5549 c->buckets[i] = face->next;
5551 if (face->next)
5552 face->next->prev = face->prev;
5554 c->faces_by_id[face->id] = NULL;
5555 if (face->id == c->used)
5556 --c->used;
5560 /* Look up a realized face with face attributes ATTR in the face cache
5561 of frame F. The face will be used to display character C. Value
5562 is the ID of the face found. If no suitable face is found, realize
5563 a new one. In that case, if C is a multibyte character, BASE_FACE
5564 is a face that has the same attributes. */
5566 INLINE int
5567 lookup_face (f, attr, c, base_face)
5568 struct frame *f;
5569 Lisp_Object *attr;
5570 int c;
5571 struct face *base_face;
5573 struct face_cache *cache = FRAME_FACE_CACHE (f);
5574 unsigned hash;
5575 int i;
5576 struct face *face;
5578 xassert (cache != NULL);
5579 check_lface_attrs (attr);
5581 /* Look up ATTR in the face cache. */
5582 hash = lface_hash (attr);
5583 i = hash % FACE_CACHE_BUCKETS_SIZE;
5585 for (face = cache->buckets[i]; face; face = face->next)
5586 if (face->hash == hash
5587 && (!FRAME_WINDOW_P (f)
5588 || FACE_SUITABLE_FOR_CHAR_P (face, c))
5589 && lface_equal_p (face->lface, attr))
5590 break;
5592 /* If not found, realize a new face. */
5593 if (face == NULL)
5594 face = realize_face (cache, attr, c, base_face, -1);
5596 #if GLYPH_DEBUG
5597 xassert (face == FACE_FROM_ID (f, face->id));
5599 /* When this function is called from face_for_char (in this case, C is
5600 a multibyte character), a fontset of a face returned by
5601 realize_face is not yet set, i.e. FACE_SUITABLE_FOR_CHAR_P (FACE,
5602 C) is not sutisfied. The fontset is set for this face by
5603 face_for_char later. */
5604 #if 0
5605 if (FRAME_WINDOW_P (f))
5606 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
5607 #endif
5608 #endif /* GLYPH_DEBUG */
5610 return face->id;
5614 /* Return the face id of the realized face for named face SYMBOL on
5615 frame F suitable for displaying character C. Value is -1 if the
5616 face couldn't be determined, which might happen if the default face
5617 isn't realized and cannot be realized. */
5620 lookup_named_face (f, symbol, c)
5621 struct frame *f;
5622 Lisp_Object symbol;
5623 int c;
5625 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5626 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5627 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5629 if (default_face == NULL)
5631 if (!realize_basic_faces (f))
5632 return -1;
5633 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5636 get_lface_attributes (f, symbol, symbol_attrs, 1);
5637 bcopy (default_face->lface, attrs, sizeof attrs);
5638 merge_face_vectors (f, symbol_attrs, attrs, 0);
5640 return lookup_face (f, attrs, c, NULL);
5644 /* Return the ID of the realized ASCII face of Lisp face with ID
5645 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
5648 ascii_face_of_lisp_face (f, lface_id)
5649 struct frame *f;
5650 int lface_id;
5652 int face_id;
5654 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
5656 Lisp_Object face_name = lface_id_to_name[lface_id];
5657 face_id = lookup_named_face (f, face_name, 0);
5659 else
5660 face_id = -1;
5662 return face_id;
5666 /* Return a face for charset ASCII that is like the face with id
5667 FACE_ID on frame F, but has a font that is STEPS steps smaller.
5668 STEPS < 0 means larger. Value is the id of the face. */
5671 smaller_face (f, face_id, steps)
5672 struct frame *f;
5673 int face_id, steps;
5675 #ifdef HAVE_WINDOW_SYSTEM
5676 struct face *face;
5677 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5678 int pt, last_pt, last_height;
5679 int delta;
5680 int new_face_id;
5681 struct face *new_face;
5683 /* If not called for an X frame, just return the original face. */
5684 if (FRAME_TERMCAP_P (f))
5685 return face_id;
5687 /* Try in increments of 1/2 pt. */
5688 delta = steps < 0 ? 5 : -5;
5689 steps = abs (steps);
5691 face = FACE_FROM_ID (f, face_id);
5692 bcopy (face->lface, attrs, sizeof attrs);
5693 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
5694 new_face_id = face_id;
5695 last_height = FONT_HEIGHT (face->font);
5697 while (steps
5698 && pt + delta > 0
5699 /* Give up if we cannot find a font within 10pt. */
5700 && abs (last_pt - pt) < 100)
5702 /* Look up a face for a slightly smaller/larger font. */
5703 pt += delta;
5704 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
5705 new_face_id = lookup_face (f, attrs, 0, NULL);
5706 new_face = FACE_FROM_ID (f, new_face_id);
5708 /* If height changes, count that as one step. */
5709 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
5710 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
5712 --steps;
5713 last_height = FONT_HEIGHT (new_face->font);
5714 last_pt = pt;
5718 return new_face_id;
5720 #else /* not HAVE_WINDOW_SYSTEM */
5722 return face_id;
5724 #endif /* not HAVE_WINDOW_SYSTEM */
5728 /* Return a face for charset ASCII that is like the face with id
5729 FACE_ID on frame F, but has height HEIGHT. */
5732 face_with_height (f, face_id, height)
5733 struct frame *f;
5734 int face_id;
5735 int height;
5737 #ifdef HAVE_WINDOW_SYSTEM
5738 struct face *face;
5739 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5741 if (FRAME_TERMCAP_P (f)
5742 || height <= 0)
5743 return face_id;
5745 face = FACE_FROM_ID (f, face_id);
5746 bcopy (face->lface, attrs, sizeof attrs);
5747 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
5748 face_id = lookup_face (f, attrs, 0, NULL);
5749 #endif /* HAVE_WINDOW_SYSTEM */
5751 return face_id;
5755 /* Return the face id of the realized face for named face SYMBOL on
5756 frame F suitable for displaying character C, and use attributes of
5757 the face FACE_ID for attributes that aren't completely specified by
5758 SYMBOL. This is like lookup_named_face, except that the default
5759 attributes come from FACE_ID, not from the default face. FACE_ID
5760 is assumed to be already realized. */
5763 lookup_derived_face (f, symbol, c, face_id)
5764 struct frame *f;
5765 Lisp_Object symbol;
5766 int c;
5767 int face_id;
5769 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5770 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5771 struct face *default_face = FACE_FROM_ID (f, face_id);
5773 if (!default_face)
5774 abort ();
5776 get_lface_attributes (f, symbol, symbol_attrs, 1);
5777 bcopy (default_face->lface, attrs, sizeof attrs);
5778 merge_face_vectors (f, symbol_attrs, attrs, 0);
5779 return lookup_face (f, attrs, c, default_face);
5782 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
5783 Sface_attributes_as_vector, 1, 1, 0,
5784 doc: /* Return a vector of face attributes corresponding to PLIST. */)
5785 (plist)
5786 Lisp_Object plist;
5788 Lisp_Object lface;
5789 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
5790 Qunspecified);
5791 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
5792 1, 0);
5793 return lface;
5798 /***********************************************************************
5799 Face capability testing
5800 ***********************************************************************/
5803 /* If the distance (as returned by color_distance) between two colors is
5804 less than this, then they are considered the same, for determining
5805 whether a color is supported or not. The range of values is 0-65535. */
5807 #define TTY_SAME_COLOR_THRESHOLD 10000
5809 #ifdef HAVE_WINDOW_SYSTEM
5811 /* Return non-zero if all the face attributes in ATTRS are supported
5812 on the window-system frame F.
5814 The definition of `supported' is somewhat heuristic, but basically means
5815 that a face containing all the attributes in ATTRS, when merged with the
5816 default face for display, can be represented in a way that's
5818 \(1) different in appearance than the default face, and
5819 \(2) `close in spirit' to what the attributes specify, if not exact. */
5821 static int
5822 x_supports_face_attributes_p (f, attrs, def_face)
5823 struct frame *f;
5824 Lisp_Object *attrs;
5825 struct face *def_face;
5827 Lisp_Object *def_attrs = def_face->lface;
5829 /* Check that other specified attributes are different that the default
5830 face. */
5831 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
5832 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
5833 def_attrs[LFACE_UNDERLINE_INDEX]))
5834 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
5835 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
5836 def_attrs[LFACE_INVERSE_INDEX]))
5837 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
5838 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
5839 def_attrs[LFACE_FOREGROUND_INDEX]))
5840 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
5841 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
5842 def_attrs[LFACE_BACKGROUND_INDEX]))
5843 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5844 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
5845 def_attrs[LFACE_STIPPLE_INDEX]))
5846 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5847 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
5848 def_attrs[LFACE_OVERLINE_INDEX]))
5849 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5850 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
5851 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
5852 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5853 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
5854 def_attrs[LFACE_BOX_INDEX])))
5855 return 0;
5857 /* Check font-related attributes, as those are the most commonly
5858 "unsupported" on a window-system (because of missing fonts). */
5859 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5860 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5861 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
5862 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
5863 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5864 || !UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX]))
5866 struct face *face;
5867 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
5869 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
5871 merge_face_vectors (f, attrs, merged_attrs, 0);
5873 face = FACE_FROM_ID (f, lookup_face (f, merged_attrs, 0, 0));
5875 if (! face)
5876 error ("cannot make face");
5878 /* If the font is the same, then not supported. */
5879 if (face->font == def_face->font)
5880 return 0;
5883 /* Everything checks out, this face is supported. */
5884 return 1;
5887 #endif /* HAVE_WINDOW_SYSTEM */
5889 /* Return non-zero if all the face attributes in ATTRS are supported
5890 on the tty frame F.
5892 The definition of `supported' is somewhat heuristic, but basically means
5893 that a face containing all the attributes in ATTRS, when merged
5894 with the default face for display, can be represented in a way that's
5896 \(1) different in appearance than the default face, and
5897 \(2) `close in spirit' to what the attributes specify, if not exact.
5899 Point (2) implies that a `:weight black' attribute will be satisfied
5900 by any terminal that can display bold, and a `:foreground "yellow"' as
5901 long as the terminal can display a yellowish color, but `:slant italic'
5902 will _not_ be satisfied by the tty display code's automatic
5903 substitution of a `dim' face for italic. */
5905 static int
5906 tty_supports_face_attributes_p (f, attrs, def_face)
5907 struct frame *f;
5908 Lisp_Object *attrs;
5909 struct face *def_face;
5911 int weight;
5912 Lisp_Object val, fg, bg;
5913 XColor fg_tty_color, fg_std_color;
5914 XColor bg_tty_color, bg_std_color;
5915 unsigned test_caps = 0;
5916 Lisp_Object *def_attrs = def_face->lface;
5919 /* First check some easy-to-check stuff; ttys support none of the
5920 following attributes, so we can just return false if any are requested
5921 (even if `nominal' values are specified, we should still return false,
5922 as that will be the same value that the default face uses). We
5923 consider :slant unsupportable on ttys, even though the face code
5924 actually `fakes' them using a dim attribute if possible. This is
5925 because the faked result is too different from what the face
5926 specifies. */
5927 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5928 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5929 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5930 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5931 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5932 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5933 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5934 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5935 return 0;
5938 /* Test for terminal `capabilities' (non-color character attributes). */
5940 /* font weight (bold/dim) */
5941 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5942 if (weight >= 0)
5944 int def_weight = face_numeric_weight (def_attrs[LFACE_WEIGHT_INDEX]);
5946 if (weight > XLFD_WEIGHT_MEDIUM)
5948 if (def_weight > XLFD_WEIGHT_MEDIUM)
5949 return 0; /* same as default */
5950 test_caps = TTY_CAP_BOLD;
5952 else if (weight < XLFD_WEIGHT_MEDIUM)
5954 if (def_weight < XLFD_WEIGHT_MEDIUM)
5955 return 0; /* same as default */
5956 test_caps = TTY_CAP_DIM;
5958 else if (def_weight == XLFD_WEIGHT_MEDIUM)
5959 return 0; /* same as default */
5962 /* underlining */
5963 val = attrs[LFACE_UNDERLINE_INDEX];
5964 if (!UNSPECIFIEDP (val))
5966 if (STRINGP (val))
5967 return 0; /* ttys can't use colored underlines */
5968 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5969 return 0; /* same as default */
5970 else
5971 test_caps |= TTY_CAP_UNDERLINE;
5974 /* inverse video */
5975 val = attrs[LFACE_INVERSE_INDEX];
5976 if (!UNSPECIFIEDP (val))
5978 if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5979 return 0; /* same as default */
5980 else
5981 test_caps |= TTY_CAP_INVERSE;
5985 /* Color testing. */
5987 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5988 we use them when calling `tty_capable_p' below, even if the face
5989 specifies no colors. */
5990 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5991 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
5993 /* Check if foreground color is close enough. */
5994 fg = attrs[LFACE_FOREGROUND_INDEX];
5995 if (STRINGP (fg))
5997 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
5999 if (face_attr_equal_p (fg, def_fg))
6000 return 0; /* same as default */
6001 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
6002 return 0; /* not a valid color */
6003 else if (color_distance (&fg_tty_color, &fg_std_color)
6004 > TTY_SAME_COLOR_THRESHOLD)
6005 return 0; /* displayed color is too different */
6006 else
6007 /* Make sure the color is really different than the default. */
6009 XColor def_fg_color;
6010 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
6011 && (color_distance (&fg_tty_color, &def_fg_color)
6012 <= TTY_SAME_COLOR_THRESHOLD))
6013 return 0;
6017 /* Check if background color is close enough. */
6018 bg = attrs[LFACE_BACKGROUND_INDEX];
6019 if (STRINGP (bg))
6021 Lisp_Object def_bg = def_attrs[LFACE_FOREGROUND_INDEX];
6023 if (face_attr_equal_p (bg, def_bg))
6024 return 0; /* same as default */
6025 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
6026 return 0; /* not a valid color */
6027 else if (color_distance (&bg_tty_color, &bg_std_color)
6028 > TTY_SAME_COLOR_THRESHOLD)
6029 return 0; /* displayed color is too different */
6030 else
6031 /* Make sure the color is really different than the default. */
6033 XColor def_bg_color;
6034 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
6035 && (color_distance (&bg_tty_color, &def_bg_color)
6036 <= TTY_SAME_COLOR_THRESHOLD))
6037 return 0;
6041 /* If both foreground and background are requested, see if the
6042 distance between them is OK. We just check to see if the distance
6043 between the tty's foreground and background is close enough to the
6044 distance between the standard foreground and background. */
6045 if (STRINGP (fg) && STRINGP (bg))
6047 int delta_delta
6048 = (color_distance (&fg_std_color, &bg_std_color)
6049 - color_distance (&fg_tty_color, &bg_tty_color));
6050 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
6051 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
6052 return 0;
6056 /* See if the capabilities we selected above are supported, with the
6057 given colors. */
6058 if (test_caps != 0 &&
6059 ! tty_capable_p (f, test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
6060 return 0;
6063 /* Hmmm, everything checks out, this terminal must support this face. */
6064 return 1;
6068 DEFUN ("display-supports-face-attributes-p",
6069 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
6070 1, 2, 0,
6071 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
6072 The optional argument DISPLAY can be a display name, a frame, or
6073 nil (meaning the selected frame's display)
6075 The definition of `supported' is somewhat heuristic, but basically means
6076 that a face containing all the attributes in ATTRIBUTES, when merged
6077 with the default face for display, can be represented in a way that's
6079 \(1) different in appearance than the default face, and
6080 \(2) `close in spirit' to what the attributes specify, if not exact.
6082 Point (2) implies that a `:weight black' attribute will be satisfied by
6083 any display that can display bold, and a `:foreground \"yellow\"' as long
6084 as it can display a yellowish color, but `:slant italic' will _not_ be
6085 satisfied by the tty display code's automatic substitution of a `dim'
6086 face for italic. */)
6087 (attributes, display)
6088 Lisp_Object attributes, display;
6090 int supports, i;
6091 Lisp_Object frame;
6092 struct frame *f;
6093 struct face *def_face;
6094 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6096 if (noninteractive || !initialized)
6097 /* We may not be able to access low-level face information in batch
6098 mode, or before being dumped, and this function is not going to
6099 be very useful in those cases anyway, so just give up. */
6100 return Qnil;
6102 if (NILP (display))
6103 frame = selected_frame;
6104 else if (FRAMEP (display))
6105 frame = display;
6106 else
6108 /* Find any frame on DISPLAY. */
6109 Lisp_Object fl_tail;
6111 frame = Qnil;
6112 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
6114 frame = XCAR (fl_tail);
6115 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
6116 XFRAME (frame)->param_alist)),
6117 display)))
6118 break;
6122 CHECK_LIVE_FRAME (frame);
6123 f = XFRAME (frame);
6125 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
6126 attrs[i] = Qunspecified;
6127 merge_face_ref (f, attributes, attrs, 1, 0);
6129 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6130 if (def_face == NULL)
6132 if (! realize_basic_faces (f))
6133 error ("Cannot realize default face");
6134 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6137 /* Dispatch to the appropriate handler. */
6138 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6139 supports = tty_supports_face_attributes_p (f, attrs, def_face);
6140 #ifdef HAVE_WINDOW_SYSTEM
6141 else
6142 supports = x_supports_face_attributes_p (f, attrs, def_face);
6143 #endif
6145 return supports ? Qt : Qnil;
6149 /***********************************************************************
6150 Font selection
6151 ***********************************************************************/
6153 DEFUN ("internal-set-font-selection-order",
6154 Finternal_set_font_selection_order,
6155 Sinternal_set_font_selection_order, 1, 1, 0,
6156 doc: /* Set font selection order for face font selection to ORDER.
6157 ORDER must be a list of length 4 containing the symbols `:width',
6158 `:height', `:weight', and `:slant'. Face attributes appearing
6159 first in ORDER are matched first, e.g. if `:height' appears before
6160 `:weight' in ORDER, font selection first tries to find a font with
6161 a suitable height, and then tries to match the font weight.
6162 Value is ORDER. */)
6163 (order)
6164 Lisp_Object order;
6166 Lisp_Object list;
6167 int i;
6168 int indices[DIM (font_sort_order)];
6170 CHECK_LIST (order);
6171 bzero (indices, sizeof indices);
6172 i = 0;
6174 for (list = order;
6175 CONSP (list) && i < DIM (indices);
6176 list = XCDR (list), ++i)
6178 Lisp_Object attr = XCAR (list);
6179 int xlfd;
6181 if (EQ (attr, QCwidth))
6182 xlfd = XLFD_SWIDTH;
6183 else if (EQ (attr, QCheight))
6184 xlfd = XLFD_POINT_SIZE;
6185 else if (EQ (attr, QCweight))
6186 xlfd = XLFD_WEIGHT;
6187 else if (EQ (attr, QCslant))
6188 xlfd = XLFD_SLANT;
6189 else
6190 break;
6192 if (indices[i] != 0)
6193 break;
6194 indices[i] = xlfd;
6197 if (!NILP (list) || i != DIM (indices))
6198 signal_error ("Invalid font sort order", order);
6199 for (i = 0; i < DIM (font_sort_order); ++i)
6200 if (indices[i] == 0)
6201 signal_error ("Invalid font sort order", order);
6203 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
6205 bcopy (indices, font_sort_order, sizeof font_sort_order);
6206 free_all_realized_faces (Qnil);
6209 return Qnil;
6213 DEFUN ("internal-set-alternative-font-family-alist",
6214 Finternal_set_alternative_font_family_alist,
6215 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
6216 doc: /* Define alternative font families to try in face font selection.
6217 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6218 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
6219 be found. Value is ALIST. */)
6220 (alist)
6221 Lisp_Object alist;
6223 CHECK_LIST (alist);
6224 Vface_alternative_font_family_alist = alist;
6225 free_all_realized_faces (Qnil);
6226 return alist;
6230 DEFUN ("internal-set-alternative-font-registry-alist",
6231 Finternal_set_alternative_font_registry_alist,
6232 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
6233 doc: /* Define alternative font registries to try in face font selection.
6234 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6235 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
6236 be found. Value is ALIST. */)
6237 (alist)
6238 Lisp_Object alist;
6240 CHECK_LIST (alist);
6241 Vface_alternative_font_registry_alist = alist;
6242 free_all_realized_faces (Qnil);
6243 return alist;
6247 #ifdef HAVE_WINDOW_SYSTEM
6249 /* Value is non-zero if FONT is the name of a scalable font. The
6250 X11R6 XLFD spec says that point size, pixel size, and average width
6251 are zero for scalable fonts. Intlfonts contain at least one
6252 scalable font ("*-muleindian-1") for which this isn't true, so we
6253 just test average width. */
6255 static int
6256 font_scalable_p (font)
6257 struct font_name *font;
6259 char *s = font->fields[XLFD_AVGWIDTH];
6260 return (*s == '0' && *(s + 1) == '\0')
6261 #ifdef WINDOWSNT
6262 /* Windows implementation of XLFD is slightly broken for backward
6263 compatibility with previous broken versions, so test for
6264 wildcards as well as 0. */
6265 || *s == '*'
6266 #endif
6271 /* Ignore the difference of font point size less than this value. */
6273 #define FONT_POINT_SIZE_QUANTUM 5
6275 /* Value is non-zero if FONT1 is a better match for font attributes
6276 VALUES than FONT2. VALUES is an array of face attribute values in
6277 font sort order. COMPARE_PT_P zero means don't compare point
6278 sizes. AVGWIDTH, if not zero, is a specified font average width
6279 to compare with. */
6281 static int
6282 better_font_p (values, font1, font2, compare_pt_p, avgwidth)
6283 int *values;
6284 struct font_name *font1, *font2;
6285 int compare_pt_p, avgwidth;
6287 int i;
6289 for (i = 0; i < DIM (font_sort_order); ++i)
6291 int xlfd_idx = font_sort_order[i];
6293 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
6295 int delta1, delta2;
6297 if (xlfd_idx == XLFD_POINT_SIZE)
6299 delta1 = abs (values[i] - (font1->numeric[xlfd_idx]
6300 / font1->rescale_ratio));
6301 delta2 = abs (values[i] - (font2->numeric[xlfd_idx]
6302 / font2->rescale_ratio));
6303 if (abs (delta1 - delta2) < FONT_POINT_SIZE_QUANTUM)
6304 continue;
6306 else
6308 delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
6309 delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
6312 if (delta1 > delta2)
6313 return 0;
6314 else if (delta1 < delta2)
6315 return 1;
6316 else
6318 /* The difference may be equal because, e.g., the face
6319 specifies `italic' but we have only `regular' and
6320 `oblique'. Prefer `oblique' in this case. */
6321 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
6322 && font1->numeric[xlfd_idx] > values[i]
6323 && font2->numeric[xlfd_idx] < values[i])
6324 return 1;
6329 if (avgwidth)
6331 int delta1 = abs (avgwidth - font1->numeric[XLFD_AVGWIDTH]);
6332 int delta2 = abs (avgwidth - font2->numeric[XLFD_AVGWIDTH]);
6333 if (delta1 > delta2)
6334 return 0;
6335 else if (delta1 < delta2)
6336 return 1;
6339 if (! compare_pt_p)
6341 /* We prefer a real scalable font; i.e. not what autoscaled. */
6342 int auto_scaled_1 = (font1->numeric[XLFD_POINT_SIZE] == 0
6343 && font1->numeric[XLFD_RESY] > 0);
6344 int auto_scaled_2 = (font2->numeric[XLFD_POINT_SIZE] == 0
6345 && font2->numeric[XLFD_RESY] > 0);
6347 if (auto_scaled_1 != auto_scaled_2)
6348 return auto_scaled_2;
6351 return font1->registry_priority < font2->registry_priority;
6355 /* Value is non-zero if FONT is an exact match for face attributes in
6356 SPECIFIED. SPECIFIED is an array of face attribute values in font
6357 sort order. AVGWIDTH, if non-zero, is an average width to compare
6358 with. */
6360 static int
6361 exact_face_match_p (specified, font, avgwidth)
6362 int *specified;
6363 struct font_name *font;
6364 int avgwidth;
6366 int i;
6368 for (i = 0; i < DIM (font_sort_order); ++i)
6369 if (specified[i] != font->numeric[font_sort_order[i]])
6370 break;
6372 return (i == DIM (font_sort_order)
6373 && (avgwidth <= 0
6374 || avgwidth == font->numeric[XLFD_AVGWIDTH]));
6378 /* Value is the name of a scaled font, generated from scalable font
6379 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
6380 Value is allocated from heap. */
6382 static char *
6383 build_scalable_font_name (f, font, specified_pt)
6384 struct frame *f;
6385 struct font_name *font;
6386 int specified_pt;
6388 char pixel_size[20];
6389 int pixel_value;
6390 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
6391 double pt;
6393 /* If scalable font is for a specific resolution, compute
6394 the point size we must specify from the resolution of
6395 the display and the specified resolution of the font. */
6396 if (font->numeric[XLFD_RESY] != 0)
6398 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
6399 pixel_value = font->numeric[XLFD_RESY] / (PT_PER_INCH * 10.0) * pt;
6401 else
6403 pt = specified_pt;
6404 pixel_value = resy / (PT_PER_INCH * 10.0) * pt;
6406 /* We may need a font of the different size. */
6407 pixel_value *= font->rescale_ratio;
6409 /* We should keep POINT_SIZE 0. Otherwise, X server can't open a
6410 font of the specified PIXEL_SIZE. */
6411 #if 0
6412 { /* Set point size of the font. */
6413 char point_size[20];
6414 sprintf (point_size, "%d", (int) pt);
6415 font->fields[XLFD_POINT_SIZE] = point_size;
6416 font->numeric[XLFD_POINT_SIZE] = pt;
6418 #endif
6420 /* Set pixel size. */
6421 sprintf (pixel_size, "%d", pixel_value);
6422 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
6423 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
6425 /* If font doesn't specify its resolution, use the
6426 resolution of the display. */
6427 if (font->numeric[XLFD_RESY] == 0)
6429 char buffer[20];
6430 sprintf (buffer, "%d", (int) resy);
6431 font->fields[XLFD_RESY] = buffer;
6432 font->numeric[XLFD_RESY] = resy;
6435 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
6437 char buffer[20];
6438 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
6439 sprintf (buffer, "%d", resx);
6440 font->fields[XLFD_RESX] = buffer;
6441 font->numeric[XLFD_RESX] = resx;
6444 return build_font_name (font);
6448 /* Value is non-zero if we are allowed to use scalable font FONT. We
6449 can't run a Lisp function here since this function may be called
6450 with input blocked. */
6452 static int
6453 may_use_scalable_font_p (font)
6454 const char *font;
6456 if (EQ (Vscalable_fonts_allowed, Qt))
6457 return 1;
6458 else if (CONSP (Vscalable_fonts_allowed))
6460 Lisp_Object tail, regexp;
6462 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
6464 regexp = XCAR (tail);
6465 if (STRINGP (regexp)
6466 && fast_c_string_match_ignore_case (regexp, font) >= 0)
6467 return 1;
6471 return 0;
6476 /* Return the name of the best matching font for face attributes ATTRS
6477 in the array of font_name structures FONTS which contains NFONTS
6478 elements. WIDTH_RATIO is a factor with which to multiply average
6479 widths if ATTRS specifies such a width.
6481 Value is a font name which is allocated from the heap. FONTS is
6482 freed by this function.
6484 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6485 indicate whether the resulting font should be drawn using overstrike
6486 to simulate bold-face. */
6488 static char *
6489 best_matching_font (f, attrs, fonts, nfonts, width_ratio, needs_overstrike)
6490 struct frame *f;
6491 Lisp_Object *attrs;
6492 struct font_name *fonts;
6493 int nfonts;
6494 int width_ratio;
6495 int *needs_overstrike;
6497 char *font_name;
6498 struct font_name *best;
6499 int i, pt = 0;
6500 int specified[5];
6501 int exact_p, avgwidth;
6503 if (nfonts == 0)
6504 return NULL;
6506 /* Make specified font attributes available in `specified',
6507 indexed by sort order. */
6508 for (i = 0; i < DIM (font_sort_order); ++i)
6510 int xlfd_idx = font_sort_order[i];
6512 if (xlfd_idx == XLFD_SWIDTH)
6513 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
6514 else if (xlfd_idx == XLFD_POINT_SIZE)
6515 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
6516 else if (xlfd_idx == XLFD_WEIGHT)
6517 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
6518 else if (xlfd_idx == XLFD_SLANT)
6519 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
6520 else
6521 abort ();
6524 avgwidth = (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
6526 : XFASTINT (attrs[LFACE_AVGWIDTH_INDEX]) * width_ratio);
6528 exact_p = 0;
6530 if (needs_overstrike)
6531 *needs_overstrike = 0;
6533 /* Start with the first non-scalable font in the list. */
6534 for (i = 0; i < nfonts; ++i)
6535 if (!font_scalable_p (fonts + i))
6536 break;
6538 /* Find the best match among the non-scalable fonts. */
6539 if (i < nfonts)
6541 best = fonts + i;
6543 for (i = 1; i < nfonts; ++i)
6544 if (!font_scalable_p (fonts + i)
6545 && better_font_p (specified, fonts + i, best, 1, avgwidth))
6547 best = fonts + i;
6549 exact_p = exact_face_match_p (specified, best, avgwidth);
6550 if (exact_p)
6551 break;
6554 else
6555 best = NULL;
6557 /* Unless we found an exact match among non-scalable fonts, see if
6558 we can find a better match among scalable fonts. */
6559 if (!exact_p)
6561 /* A scalable font is better if
6563 1. its weight, slant, swidth attributes are better, or.
6565 2. the best non-scalable font doesn't have the required
6566 point size, and the scalable fonts weight, slant, swidth
6567 isn't worse. */
6569 int non_scalable_has_exact_height_p;
6571 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
6572 non_scalable_has_exact_height_p = 1;
6573 else
6574 non_scalable_has_exact_height_p = 0;
6576 for (i = 0; i < nfonts; ++i)
6577 if (font_scalable_p (fonts + i))
6579 if (best == NULL
6580 || better_font_p (specified, fonts + i, best, 0, 0)
6581 || (!non_scalable_has_exact_height_p
6582 && !better_font_p (specified, best, fonts + i, 0, 0)))
6584 non_scalable_has_exact_height_p = 1;
6585 best = fonts + i;
6589 if (needs_overstrike)
6591 enum xlfd_weight want_weight = specified[XLFD_WEIGHT];
6592 enum xlfd_weight got_weight = best->numeric[XLFD_WEIGHT];
6594 if (want_weight > XLFD_WEIGHT_MEDIUM && want_weight > got_weight)
6596 /* We want a bold font, but didn't get one; try to use
6597 overstriking instead to simulate bold-face. However,
6598 don't overstrike an already-bold fontn unless the
6599 desired weight grossly exceeds the available weight. */
6600 if (got_weight > XLFD_WEIGHT_MEDIUM)
6601 *needs_overstrike = (got_weight - want_weight) > 2;
6602 else
6603 *needs_overstrike = 1;
6608 if (font_scalable_p (best))
6609 font_name = build_scalable_font_name (f, best, pt);
6610 else
6611 font_name = build_font_name (best);
6613 /* Free font_name structures. */
6614 free_font_names (fonts, nfonts);
6616 return font_name;
6620 /* Get a list of matching fonts on frame F, considering FAMILY
6621 and alternative font families from Vface_alternative_font_registry_alist.
6623 FAMILY is the font family whose alternatives are considered.
6625 REGISTRY, if a string, specifies a font registry and encoding to
6626 match. A value of nil means include fonts of any registry and
6627 encoding.
6629 Return in *FONTS a pointer to a vector of font_name structures for
6630 the fonts matched. Value is the number of fonts found. */
6632 static int
6633 try_alternative_families (f, family, registry, fonts)
6634 struct frame *f;
6635 Lisp_Object family, registry;
6636 struct font_name **fonts;
6638 Lisp_Object alter;
6639 int nfonts = 0;
6641 nfonts = font_list (f, Qnil, family, registry, fonts);
6642 if (nfonts == 0)
6644 /* Try alternative font families. */
6645 alter = Fassoc (family, Vface_alternative_font_family_alist);
6646 if (CONSP (alter))
6648 for (alter = XCDR (alter);
6649 CONSP (alter) && nfonts == 0;
6650 alter = XCDR (alter))
6652 if (STRINGP (XCAR (alter)))
6653 nfonts = font_list (f, Qnil, XCAR (alter), registry, fonts);
6657 /* Try all scalable fonts before giving up. */
6658 if (nfonts == 0 && ! EQ (Vscalable_fonts_allowed, Qt))
6660 int count = SPECPDL_INDEX ();
6661 specbind (Qscalable_fonts_allowed, Qt);
6662 nfonts = try_alternative_families (f, family, registry, fonts);
6663 unbind_to (count, Qnil);
6666 return nfonts;
6670 /* Get a list of matching fonts on frame F.
6672 FAMILY, if a string, specifies a font family derived from the fontset.
6673 It is only used if the face does not specify any family in ATTRS or
6674 if we cannot find any font of the face's family.
6676 REGISTRY, if a string, specifies a font registry and encoding to
6677 match. A value of nil means include fonts of any registry and
6678 encoding.
6680 If PREFER_FACE_FAMILY is nonzero, perfer face's family to FAMILY.
6681 Otherwise, prefer FAMILY.
6683 Return in *FONTS a pointer to a vector of font_name structures for
6684 the fonts matched. Value is the number of fonts found. */
6686 static int
6687 try_font_list (f, attrs, family, registry, fonts, prefer_face_family)
6688 struct frame *f;
6689 Lisp_Object *attrs;
6690 Lisp_Object family, registry;
6691 struct font_name **fonts;
6692 int prefer_face_family;
6694 int nfonts = 0;
6695 Lisp_Object face_family = attrs[LFACE_FAMILY_INDEX];
6696 Lisp_Object try_family;
6698 try_family = (prefer_face_family || NILP (family)) ? face_family : family;
6700 if (STRINGP (try_family))
6701 nfonts = try_alternative_families (f, try_family, registry, fonts);
6703 #ifdef MAC_OS
6704 /* When realizing the default face and a font spec does not matched
6705 exactly, Emacs looks for ones with the same registry as the
6706 default font. On the Mac, this is mac-roman, which does not work
6707 if the family is -etl-fixed, e.g. The following widens the
6708 choices and fixes that problem. */
6709 if (nfonts == 0 && STRINGP (try_family) && STRINGP (registry)
6710 && xstricmp (SDATA (registry), "mac-roman") == 0)
6711 nfonts = try_alternative_families (f, try_family, Qnil, fonts);
6712 #endif
6714 if (EQ (try_family, family))
6715 family = face_family;
6717 if (nfonts == 0 && STRINGP (family))
6718 nfonts = try_alternative_families (f, family, registry, fonts);
6720 /* Try font family of the default face or "fixed". */
6721 if (nfonts == 0)
6723 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6724 if (default_face)
6725 family = default_face->lface[LFACE_FAMILY_INDEX];
6726 else
6727 family = build_string ("fixed");
6728 nfonts = font_list (f, Qnil, family, registry, fonts);
6731 /* Try any family with the given registry. */
6732 if (nfonts == 0)
6733 nfonts = try_alternative_families (f, Qnil, registry, fonts);
6735 return nfonts;
6739 /* Return the fontset id of the base fontset name or alias name given
6740 by the fontset attribute of ATTRS. Value is -1 if the fontset
6741 attribute of ATTRS doesn't name a fontset. */
6743 static int
6744 face_fontset (attrs)
6745 Lisp_Object *attrs;
6747 Lisp_Object name;
6749 name = attrs[LFACE_FONT_INDEX];
6750 if (!STRINGP (name))
6751 return -1;
6752 return fs_query_fontset (name, 0);
6756 /* Choose a name of font to use on frame F to display character C with
6757 Lisp face attributes specified by ATTRS. The font name is
6758 determined by the font-related attributes in ATTRS and the name
6759 pattern for C in FONTSET. Value is the font name which is
6760 allocated from the heap and must be freed by the caller, or NULL if
6761 we can get no information about the font name of C. It is assured
6762 that we always get some information for a single byte
6763 character.
6765 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6766 indicate whether the resulting font should be drawn using overstrike
6767 to simulate bold-face. */
6769 static char *
6770 choose_face_font (f, attrs, fontset, c, needs_overstrike)
6771 struct frame *f;
6772 Lisp_Object *attrs;
6773 int fontset, c;
6774 int *needs_overstrike;
6776 Lisp_Object pattern;
6777 char *font_name = NULL;
6778 struct font_name *fonts;
6779 int nfonts, width_ratio;
6781 if (needs_overstrike)
6782 *needs_overstrike = 0;
6784 /* Get (foundry and) family name and registry (and encoding) name of
6785 a font for C. */
6786 pattern = fontset_font_pattern (f, fontset, c);
6787 if (NILP (pattern))
6789 xassert (!SINGLE_BYTE_CHAR_P (c));
6790 return NULL;
6793 /* If what we got is a name pattern, return it. */
6794 if (STRINGP (pattern))
6795 return xstrdup (SDATA (pattern));
6797 /* Get a list of fonts matching that pattern and choose the
6798 best match for the specified face attributes from it. */
6799 nfonts = try_font_list (f, attrs, XCAR (pattern), XCDR (pattern), &fonts,
6800 (SINGLE_BYTE_CHAR_P (c)
6801 || CHAR_CHARSET (c) == charset_latin_iso8859_1));
6802 width_ratio = (SINGLE_BYTE_CHAR_P (c)
6804 : CHARSET_WIDTH (CHAR_CHARSET (c)));
6805 font_name = best_matching_font (f, attrs, fonts, nfonts, width_ratio,
6806 needs_overstrike);
6807 return font_name;
6810 #endif /* HAVE_WINDOW_SYSTEM */
6814 /***********************************************************************
6815 Face Realization
6816 ***********************************************************************/
6818 /* Realize basic faces on frame F. Value is zero if frame parameters
6819 of F don't contain enough information needed to realize the default
6820 face. */
6822 static int
6823 realize_basic_faces (f)
6824 struct frame *f;
6826 int success_p = 0;
6827 int count = SPECPDL_INDEX ();
6829 /* Block input here so that we won't be surprised by an X expose
6830 event, for instance, without having the faces set up. */
6831 BLOCK_INPUT;
6832 specbind (Qscalable_fonts_allowed, Qt);
6834 if (realize_default_face (f))
6836 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
6837 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
6838 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
6839 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
6840 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
6841 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
6842 realize_named_face (f, Qborder, BORDER_FACE_ID);
6843 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
6844 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
6845 realize_named_face (f, Qmenu, MENU_FACE_ID);
6847 /* Reflect changes in the `menu' face in menu bars. */
6848 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
6850 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
6851 #ifdef USE_X_TOOLKIT
6852 x_update_menu_appearance (f);
6853 #endif
6856 success_p = 1;
6859 unbind_to (count, Qnil);
6860 UNBLOCK_INPUT;
6861 return success_p;
6865 /* Realize the default face on frame F. If the face is not fully
6866 specified, make it fully-specified. Attributes of the default face
6867 that are not explicitly specified are taken from frame parameters. */
6869 static int
6870 realize_default_face (f)
6871 struct frame *f;
6873 struct face_cache *c = FRAME_FACE_CACHE (f);
6874 Lisp_Object lface;
6875 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6876 Lisp_Object frame_font;
6877 struct face *face;
6879 /* If the `default' face is not yet known, create it. */
6880 lface = lface_from_face_name (f, Qdefault, 0);
6881 if (NILP (lface))
6883 Lisp_Object frame;
6884 XSETFRAME (frame, f);
6885 lface = Finternal_make_lisp_face (Qdefault, frame);
6889 #ifdef HAVE_WINDOW_SYSTEM
6890 if (FRAME_WINDOW_P (f))
6892 /* Set frame_font to the value of the `font' frame parameter. */
6893 frame_font = Fassq (Qfont, f->param_alist);
6894 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
6895 frame_font = XCDR (frame_font);
6896 set_lface_from_font_name (f, lface, frame_font,
6897 f->default_face_done_p, 1);
6898 f->default_face_done_p = 1;
6900 #endif /* HAVE_WINDOW_SYSTEM */
6902 if (!FRAME_WINDOW_P (f))
6904 LFACE_FAMILY (lface) = build_string ("default");
6905 LFACE_SWIDTH (lface) = Qnormal;
6906 LFACE_HEIGHT (lface) = make_number (1);
6907 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
6908 LFACE_WEIGHT (lface) = Qnormal;
6909 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
6910 LFACE_SLANT (lface) = Qnormal;
6911 LFACE_AVGWIDTH (lface) = Qunspecified;
6914 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
6915 LFACE_UNDERLINE (lface) = Qnil;
6917 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
6918 LFACE_OVERLINE (lface) = Qnil;
6920 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
6921 LFACE_STRIKE_THROUGH (lface) = Qnil;
6923 if (UNSPECIFIEDP (LFACE_BOX (lface)))
6924 LFACE_BOX (lface) = Qnil;
6926 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
6927 LFACE_INVERSE (lface) = Qnil;
6929 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
6931 /* This function is called so early that colors are not yet
6932 set in the frame parameter list. */
6933 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
6935 if (CONSP (color) && STRINGP (XCDR (color)))
6936 LFACE_FOREGROUND (lface) = XCDR (color);
6937 else if (FRAME_WINDOW_P (f))
6938 return 0;
6939 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6940 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
6941 else
6942 abort ();
6945 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
6947 /* This function is called so early that colors are not yet
6948 set in the frame parameter list. */
6949 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
6950 if (CONSP (color) && STRINGP (XCDR (color)))
6951 LFACE_BACKGROUND (lface) = XCDR (color);
6952 else if (FRAME_WINDOW_P (f))
6953 return 0;
6954 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6955 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
6956 else
6957 abort ();
6960 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
6961 LFACE_STIPPLE (lface) = Qnil;
6963 /* Realize the face; it must be fully-specified now. */
6964 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
6965 check_lface (lface);
6966 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
6967 face = realize_face (c, attrs, 0, NULL, DEFAULT_FACE_ID);
6968 return 1;
6972 /* Realize basic faces other than the default face in face cache C.
6973 SYMBOL is the face name, ID is the face id the realized face must
6974 have. The default face must have been realized already. */
6976 static void
6977 realize_named_face (f, symbol, id)
6978 struct frame *f;
6979 Lisp_Object symbol;
6980 int id;
6982 struct face_cache *c = FRAME_FACE_CACHE (f);
6983 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
6984 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6985 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
6986 struct face *new_face;
6988 /* The default face must exist and be fully specified. */
6989 get_lface_attributes (f, Qdefault, attrs, 1);
6990 check_lface_attrs (attrs);
6991 xassert (lface_fully_specified_p (attrs));
6993 /* If SYMBOL isn't know as a face, create it. */
6994 if (NILP (lface))
6996 Lisp_Object frame;
6997 XSETFRAME (frame, f);
6998 lface = Finternal_make_lisp_face (symbol, frame);
7001 /* Merge SYMBOL's face with the default face. */
7002 get_lface_attributes (f, symbol, symbol_attrs, 1);
7003 merge_face_vectors (f, symbol_attrs, attrs, 0);
7005 /* Realize the face. */
7006 new_face = realize_face (c, attrs, 0, NULL, id);
7010 /* Realize the fully-specified face with attributes ATTRS in face
7011 cache CACHE for character C. If C is a multibyte character,
7012 BASE_FACE is a face that has the same attributes. Otherwise,
7013 BASE_FACE is ignored. If FORMER_FACE_ID is non-negative, it is an
7014 ID of face to remove before caching the new face. Value is a
7015 pointer to the newly created realized face. */
7017 static struct face *
7018 realize_face (cache, attrs, c, base_face, former_face_id)
7019 struct face_cache *cache;
7020 Lisp_Object *attrs;
7021 int c;
7022 struct face *base_face;
7023 int former_face_id;
7025 struct face *face;
7027 /* LFACE must be fully specified. */
7028 xassert (cache != NULL);
7029 check_lface_attrs (attrs);
7031 if (former_face_id >= 0 && cache->used > former_face_id)
7033 /* Remove the former face. */
7034 struct face *former_face = cache->faces_by_id[former_face_id];
7035 uncache_face (cache, former_face);
7036 free_realized_face (cache->f, former_face);
7039 if (FRAME_WINDOW_P (cache->f))
7040 face = realize_x_face (cache, attrs, c, base_face);
7041 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
7042 face = realize_tty_face (cache, attrs, c);
7043 else
7044 abort ();
7046 /* Insert the new face. */
7047 cache_face (cache, face, lface_hash (attrs));
7048 #ifdef HAVE_WINDOW_SYSTEM
7049 if (FRAME_WINDOW_P (cache->f) && face->font == NULL)
7050 load_face_font (cache->f, face, c);
7051 #endif /* HAVE_WINDOW_SYSTEM */
7052 return face;
7056 /* Realize the fully-specified face with attributes ATTRS in face
7057 cache CACHE for character C. Do it for X frame CACHE->f. If C is
7058 a multibyte character, BASE_FACE is a face that has the same
7059 attributes. Otherwise, BASE_FACE is ignored. If the new face
7060 doesn't share font with the default face, a fontname is allocated
7061 from the heap and set in `font_name' of the new face, but it is not
7062 yet loaded here. Value is a pointer to the newly created realized
7063 face. */
7065 static struct face *
7066 realize_x_face (cache, attrs, c, base_face)
7067 struct face_cache *cache;
7068 Lisp_Object *attrs;
7069 int c;
7070 struct face *base_face;
7072 #ifdef HAVE_WINDOW_SYSTEM
7073 struct face *face, *default_face;
7074 struct frame *f;
7075 Lisp_Object stipple, overline, strike_through, box;
7077 xassert (FRAME_WINDOW_P (cache->f));
7078 xassert (SINGLE_BYTE_CHAR_P (c)
7079 || base_face);
7081 /* Allocate a new realized face. */
7082 face = make_realized_face (attrs);
7084 f = cache->f;
7086 /* If C is a multibyte character, we share all face attirbutes with
7087 BASE_FACE including the realized fontset. But, we must load a
7088 different font. */
7089 if (!SINGLE_BYTE_CHAR_P (c))
7091 bcopy (base_face, face, sizeof *face);
7092 face->gc = 0;
7094 /* Don't try to free the colors copied bitwise from BASE_FACE. */
7095 face->colors_copied_bitwise_p = 1;
7097 /* to force realize_face to load font */
7098 face->font = NULL;
7099 return face;
7102 /* Now we are realizing a face for ASCII (and unibyte) characters. */
7104 /* Determine the font to use. Most of the time, the font will be
7105 the same as the font of the default face, so try that first. */
7106 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7107 if (default_face
7108 && FACE_SUITABLE_FOR_CHAR_P (default_face, c)
7109 && lface_same_font_attributes_p (default_face->lface, attrs))
7111 face->font = default_face->font;
7112 face->fontset = default_face->fontset;
7113 face->font_info_id = default_face->font_info_id;
7114 face->font_name = default_face->font_name;
7115 face->ascii_face = face;
7117 /* But, as we can't share the fontset, make a new realized
7118 fontset that has the same base fontset as of the default
7119 face. */
7120 face->fontset
7121 = make_fontset_for_ascii_face (f, default_face->fontset);
7123 else
7125 /* If the face attribute ATTRS specifies a fontset, use it as
7126 the base of a new realized fontset. Otherwise, use the same
7127 base fontset as of the default face. The base determines
7128 registry and encoding of a font. It may also determine
7129 foundry and family. The other fields of font name pattern
7130 are constructed from ATTRS. */
7131 int fontset = face_fontset (attrs);
7133 if ((fontset == -1) && default_face)
7134 fontset = default_face->fontset;
7135 face->fontset = make_fontset_for_ascii_face (f, fontset);
7136 face->font = NULL; /* to force realize_face to load font */
7139 /* Load colors, and set remaining attributes. */
7141 load_face_colors (f, face, attrs);
7143 /* Set up box. */
7144 box = attrs[LFACE_BOX_INDEX];
7145 if (STRINGP (box))
7147 /* A simple box of line width 1 drawn in color given by
7148 the string. */
7149 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
7150 LFACE_BOX_INDEX);
7151 face->box = FACE_SIMPLE_BOX;
7152 face->box_line_width = 1;
7154 else if (INTEGERP (box))
7156 /* Simple box of specified line width in foreground color of the
7157 face. */
7158 xassert (XINT (box) != 0);
7159 face->box = FACE_SIMPLE_BOX;
7160 face->box_line_width = XINT (box);
7161 face->box_color = face->foreground;
7162 face->box_color_defaulted_p = 1;
7164 else if (CONSP (box))
7166 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
7167 being one of `raised' or `sunken'. */
7168 face->box = FACE_SIMPLE_BOX;
7169 face->box_color = face->foreground;
7170 face->box_color_defaulted_p = 1;
7171 face->box_line_width = 1;
7173 while (CONSP (box))
7175 Lisp_Object keyword, value;
7177 keyword = XCAR (box);
7178 box = XCDR (box);
7180 if (!CONSP (box))
7181 break;
7182 value = XCAR (box);
7183 box = XCDR (box);
7185 if (EQ (keyword, QCline_width))
7187 if (INTEGERP (value) && XINT (value) != 0)
7188 face->box_line_width = XINT (value);
7190 else if (EQ (keyword, QCcolor))
7192 if (STRINGP (value))
7194 face->box_color = load_color (f, face, value,
7195 LFACE_BOX_INDEX);
7196 face->use_box_color_for_shadows_p = 1;
7199 else if (EQ (keyword, QCstyle))
7201 if (EQ (value, Qreleased_button))
7202 face->box = FACE_RAISED_BOX;
7203 else if (EQ (value, Qpressed_button))
7204 face->box = FACE_SUNKEN_BOX;
7209 /* Text underline, overline, strike-through. */
7211 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
7213 /* Use default color (same as foreground color). */
7214 face->underline_p = 1;
7215 face->underline_defaulted_p = 1;
7216 face->underline_color = 0;
7218 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
7220 /* Use specified color. */
7221 face->underline_p = 1;
7222 face->underline_defaulted_p = 0;
7223 face->underline_color
7224 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
7225 LFACE_UNDERLINE_INDEX);
7227 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
7229 face->underline_p = 0;
7230 face->underline_defaulted_p = 0;
7231 face->underline_color = 0;
7234 overline = attrs[LFACE_OVERLINE_INDEX];
7235 if (STRINGP (overline))
7237 face->overline_color
7238 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
7239 LFACE_OVERLINE_INDEX);
7240 face->overline_p = 1;
7242 else if (EQ (overline, Qt))
7244 face->overline_color = face->foreground;
7245 face->overline_color_defaulted_p = 1;
7246 face->overline_p = 1;
7249 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
7250 if (STRINGP (strike_through))
7252 face->strike_through_color
7253 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
7254 LFACE_STRIKE_THROUGH_INDEX);
7255 face->strike_through_p = 1;
7257 else if (EQ (strike_through, Qt))
7259 face->strike_through_color = face->foreground;
7260 face->strike_through_color_defaulted_p = 1;
7261 face->strike_through_p = 1;
7264 stipple = attrs[LFACE_STIPPLE_INDEX];
7265 if (!NILP (stipple))
7266 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
7268 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
7269 return face;
7270 #endif /* HAVE_WINDOW_SYSTEM */
7274 /* Map a specified color of face FACE on frame F to a tty color index.
7275 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
7276 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
7277 default foreground/background colors. */
7279 static void
7280 map_tty_color (f, face, idx, defaulted)
7281 struct frame *f;
7282 struct face *face;
7283 enum lface_attribute_index idx;
7284 int *defaulted;
7286 Lisp_Object frame, color, def;
7287 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
7288 unsigned long default_pixel, default_other_pixel, pixel;
7290 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
7292 if (foreground_p)
7294 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7295 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7297 else
7299 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7300 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7303 XSETFRAME (frame, f);
7304 color = face->lface[idx];
7306 if (STRINGP (color)
7307 && SCHARS (color)
7308 && CONSP (Vtty_defined_color_alist)
7309 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
7310 CONSP (def)))
7312 /* Associations in tty-defined-color-alist are of the form
7313 (NAME INDEX R G B). We need the INDEX part. */
7314 pixel = XINT (XCAR (XCDR (def)));
7317 if (pixel == default_pixel && STRINGP (color))
7319 pixel = load_color (f, face, color, idx);
7321 #if defined (MSDOS) || defined (WINDOWSNT)
7322 /* If the foreground of the default face is the default color,
7323 use the foreground color defined by the frame. */
7324 #ifdef MSDOS
7325 if (FRAME_MSDOS_P (f))
7327 #endif /* MSDOS */
7328 if (pixel == default_pixel
7329 || pixel == FACE_TTY_DEFAULT_COLOR)
7331 if (foreground_p)
7332 pixel = FRAME_FOREGROUND_PIXEL (f);
7333 else
7334 pixel = FRAME_BACKGROUND_PIXEL (f);
7335 face->lface[idx] = tty_color_name (f, pixel);
7336 *defaulted = 1;
7338 else if (pixel == default_other_pixel)
7340 if (foreground_p)
7341 pixel = FRAME_BACKGROUND_PIXEL (f);
7342 else
7343 pixel = FRAME_FOREGROUND_PIXEL (f);
7344 face->lface[idx] = tty_color_name (f, pixel);
7345 *defaulted = 1;
7347 #ifdef MSDOS
7349 #endif
7350 #endif /* MSDOS or WINDOWSNT */
7353 if (foreground_p)
7354 face->foreground = pixel;
7355 else
7356 face->background = pixel;
7360 /* Realize the fully-specified face with attributes ATTRS in face
7361 cache CACHE for character C. Do it for TTY frame CACHE->f. Value is a
7362 pointer to the newly created realized face. */
7364 static struct face *
7365 realize_tty_face (cache, attrs, c)
7366 struct face_cache *cache;
7367 Lisp_Object *attrs;
7368 int c;
7370 struct face *face;
7371 int weight, slant;
7372 int face_colors_defaulted = 0;
7373 struct frame *f = cache->f;
7375 /* Frame must be a termcap frame. */
7376 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
7378 /* Allocate a new realized face. */
7379 face = make_realized_face (attrs);
7380 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
7382 /* Map face attributes to TTY appearances. We map slant to
7383 dimmed text because we want italic text to appear differently
7384 and because dimmed text is probably used infrequently. */
7385 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
7386 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
7388 if (weight > XLFD_WEIGHT_MEDIUM)
7389 face->tty_bold_p = 1;
7390 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
7391 face->tty_dim_p = 1;
7392 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
7393 face->tty_underline_p = 1;
7394 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
7395 face->tty_reverse_p = 1;
7397 /* Map color names to color indices. */
7398 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
7399 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
7401 /* Swap colors if face is inverse-video. If the colors are taken
7402 from the frame colors, they are already inverted, since the
7403 frame-creation function calls x-handle-reverse-video. */
7404 if (face->tty_reverse_p && !face_colors_defaulted)
7406 unsigned long tem = face->foreground;
7407 face->foreground = face->background;
7408 face->background = tem;
7411 if (tty_suppress_bold_inverse_default_colors_p
7412 && face->tty_bold_p
7413 && face->background == FACE_TTY_DEFAULT_FG_COLOR
7414 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
7415 face->tty_bold_p = 0;
7417 return face;
7421 DEFUN ("tty-suppress-bold-inverse-default-colors",
7422 Ftty_suppress_bold_inverse_default_colors,
7423 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
7424 doc: /* Suppress/allow boldness of faces with inverse default colors.
7425 SUPPRESS non-nil means suppress it.
7426 This affects bold faces on TTYs whose foreground is the default background
7427 color of the display and whose background is the default foreground color.
7428 For such faces, the bold face attribute is ignored if this variable
7429 is non-nil. */)
7430 (suppress)
7431 Lisp_Object suppress;
7433 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
7434 ++face_change_count;
7435 return suppress;
7440 /***********************************************************************
7441 Computing Faces
7442 ***********************************************************************/
7444 /* Return the ID of the face to use to display character CH with face
7445 property PROP on frame F in current_buffer. */
7448 compute_char_face (f, ch, prop)
7449 struct frame *f;
7450 int ch;
7451 Lisp_Object prop;
7453 int face_id;
7455 if (NILP (current_buffer->enable_multibyte_characters))
7456 ch = 0;
7458 if (NILP (prop))
7460 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7461 face_id = FACE_FOR_CHAR (f, face, ch);
7463 else
7465 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7466 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7467 bcopy (default_face->lface, attrs, sizeof attrs);
7468 merge_face_ref (f, prop, attrs, 1, 0);
7469 face_id = lookup_face (f, attrs, ch, NULL);
7472 return face_id;
7475 /* Return the face ID associated with buffer position POS for
7476 displaying ASCII characters. Return in *ENDPTR the position at
7477 which a different face is needed, as far as text properties and
7478 overlays are concerned. W is a window displaying current_buffer.
7480 REGION_BEG, REGION_END delimit the region, so it can be
7481 highlighted.
7483 LIMIT is a position not to scan beyond. That is to limit the time
7484 this function can take.
7486 If MOUSE is non-zero, use the character's mouse-face, not its face.
7488 The face returned is suitable for displaying ASCII characters. */
7491 face_at_buffer_position (w, pos, region_beg, region_end,
7492 endptr, limit, mouse)
7493 struct window *w;
7494 int pos;
7495 int region_beg, region_end;
7496 int *endptr;
7497 int limit;
7498 int mouse;
7500 struct frame *f = XFRAME (w->frame);
7501 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7502 Lisp_Object prop, position;
7503 int i, noverlays;
7504 Lisp_Object *overlay_vec;
7505 Lisp_Object frame;
7506 int endpos;
7507 Lisp_Object propname = mouse ? Qmouse_face : Qface;
7508 Lisp_Object limit1, end;
7509 struct face *default_face;
7511 /* W must display the current buffer. We could write this function
7512 to use the frame and buffer of W, but right now it doesn't. */
7513 /* xassert (XBUFFER (w->buffer) == current_buffer); */
7515 XSETFRAME (frame, f);
7516 XSETFASTINT (position, pos);
7518 endpos = ZV;
7519 if (pos < region_beg && region_beg < endpos)
7520 endpos = region_beg;
7522 /* Get the `face' or `mouse_face' text property at POS, and
7523 determine the next position at which the property changes. */
7524 prop = Fget_text_property (position, propname, w->buffer);
7525 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
7526 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
7527 if (INTEGERP (end))
7528 endpos = XINT (end);
7530 /* Look at properties from overlays. */
7532 int next_overlay;
7534 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
7535 if (next_overlay < endpos)
7536 endpos = next_overlay;
7539 *endptr = endpos;
7541 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7543 /* Optimize common cases where we can use the default face. */
7544 if (noverlays == 0
7545 && NILP (prop)
7546 && !(pos >= region_beg && pos < region_end))
7547 return DEFAULT_FACE_ID;
7549 /* Begin with attributes from the default face. */
7550 bcopy (default_face->lface, attrs, sizeof attrs);
7552 /* Merge in attributes specified via text properties. */
7553 if (!NILP (prop))
7554 merge_face_ref (f, prop, attrs, 1, 0);
7556 /* Now merge the overlay data. */
7557 noverlays = sort_overlays (overlay_vec, noverlays, w);
7558 for (i = 0; i < noverlays; i++)
7560 Lisp_Object oend;
7561 int oendpos;
7563 prop = Foverlay_get (overlay_vec[i], propname);
7564 if (!NILP (prop))
7565 merge_face_ref (f, prop, attrs, 1, 0);
7567 oend = OVERLAY_END (overlay_vec[i]);
7568 oendpos = OVERLAY_POSITION (oend);
7569 if (oendpos < endpos)
7570 endpos = oendpos;
7573 /* If in the region, merge in the region face. */
7574 if (pos >= region_beg && pos < region_end)
7576 merge_named_face (f, Qregion, attrs, 0);
7578 if (region_end < endpos)
7579 endpos = region_end;
7582 *endptr = endpos;
7584 /* Look up a realized face with the given face attributes,
7585 or realize a new one for ASCII characters. */
7586 return lookup_face (f, attrs, 0, NULL);
7590 /* Compute the face at character position POS in Lisp string STRING on
7591 window W, for ASCII characters.
7593 If STRING is an overlay string, it comes from position BUFPOS in
7594 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
7595 not an overlay string. W must display the current buffer.
7596 REGION_BEG and REGION_END give the start and end positions of the
7597 region; both are -1 if no region is visible.
7599 BASE_FACE_ID is the id of a face to merge with. For strings coming
7600 from overlays or the `display' property it is the face at BUFPOS.
7602 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
7604 Set *ENDPTR to the next position where to check for faces in
7605 STRING; -1 if the face is constant from POS to the end of the
7606 string.
7608 Value is the id of the face to use. The face returned is suitable
7609 for displaying ASCII characters. */
7612 face_at_string_position (w, string, pos, bufpos, region_beg,
7613 region_end, endptr, base_face_id, mouse_p)
7614 struct window *w;
7615 Lisp_Object string;
7616 int pos, bufpos;
7617 int region_beg, region_end;
7618 int *endptr;
7619 enum face_id base_face_id;
7620 int mouse_p;
7622 Lisp_Object prop, position, end, limit;
7623 struct frame *f = XFRAME (WINDOW_FRAME (w));
7624 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7625 struct face *base_face;
7626 int multibyte_p = STRING_MULTIBYTE (string);
7627 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
7629 /* Get the value of the face property at the current position within
7630 STRING. Value is nil if there is no face property. */
7631 XSETFASTINT (position, pos);
7632 prop = Fget_text_property (position, prop_name, string);
7634 /* Get the next position at which to check for faces. Value of end
7635 is nil if face is constant all the way to the end of the string.
7636 Otherwise it is a string position where to check faces next.
7637 Limit is the maximum position up to which to check for property
7638 changes in Fnext_single_property_change. Strings are usually
7639 short, so set the limit to the end of the string. */
7640 XSETFASTINT (limit, SCHARS (string));
7641 end = Fnext_single_property_change (position, prop_name, string, limit);
7642 if (INTEGERP (end))
7643 *endptr = XFASTINT (end);
7644 else
7645 *endptr = -1;
7647 base_face = FACE_FROM_ID (f, base_face_id);
7648 xassert (base_face);
7650 /* Optimize the default case that there is no face property and we
7651 are not in the region. */
7652 if (NILP (prop)
7653 && (base_face_id != DEFAULT_FACE_ID
7654 /* BUFPOS <= 0 means STRING is not an overlay string, so
7655 that the region doesn't have to be taken into account. */
7656 || bufpos <= 0
7657 || bufpos < region_beg
7658 || bufpos >= region_end)
7659 && (multibyte_p
7660 /* We can't realize faces for different charsets differently
7661 if we don't have fonts, so we can stop here if not working
7662 on a window-system frame. */
7663 || !FRAME_WINDOW_P (f)
7664 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
7665 return base_face->id;
7667 /* Begin with attributes from the base face. */
7668 bcopy (base_face->lface, attrs, sizeof attrs);
7670 /* Merge in attributes specified via text properties. */
7671 if (!NILP (prop))
7672 merge_face_ref (f, prop, attrs, 1, 0);
7674 /* If in the region, merge in the region face. */
7675 if (bufpos
7676 && bufpos >= region_beg
7677 && bufpos < region_end)
7678 merge_named_face (f, Qregion, attrs, 0);
7680 /* Look up a realized face with the given face attributes,
7681 or realize a new one for ASCII characters. */
7682 return lookup_face (f, attrs, 0, NULL);
7687 /***********************************************************************
7688 Tests
7689 ***********************************************************************/
7691 #if GLYPH_DEBUG
7693 /* Print the contents of the realized face FACE to stderr. */
7695 static void
7696 dump_realized_face (face)
7697 struct face *face;
7699 fprintf (stderr, "ID: %d\n", face->id);
7700 #ifdef HAVE_X_WINDOWS
7701 fprintf (stderr, "gc: %d\n", (int) face->gc);
7702 #endif
7703 fprintf (stderr, "foreground: 0x%lx (%s)\n",
7704 face->foreground,
7705 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
7706 fprintf (stderr, "background: 0x%lx (%s)\n",
7707 face->background,
7708 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
7709 fprintf (stderr, "font_name: %s (%s)\n",
7710 face->font_name,
7711 SDATA (face->lface[LFACE_FAMILY_INDEX]));
7712 #ifdef HAVE_X_WINDOWS
7713 fprintf (stderr, "font = %p\n", face->font);
7714 #endif
7715 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
7716 fprintf (stderr, "fontset: %d\n", face->fontset);
7717 fprintf (stderr, "underline: %d (%s)\n",
7718 face->underline_p,
7719 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
7720 fprintf (stderr, "hash: %d\n", face->hash);
7721 fprintf (stderr, "charset: %d\n", face->charset);
7725 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
7727 Lisp_Object n;
7729 if (NILP (n))
7731 int i;
7733 fprintf (stderr, "font selection order: ");
7734 for (i = 0; i < DIM (font_sort_order); ++i)
7735 fprintf (stderr, "%d ", font_sort_order[i]);
7736 fprintf (stderr, "\n");
7738 fprintf (stderr, "alternative fonts: ");
7739 debug_print (Vface_alternative_font_family_alist);
7740 fprintf (stderr, "\n");
7742 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
7743 Fdump_face (make_number (i));
7745 else
7747 struct face *face;
7748 CHECK_NUMBER (n);
7749 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
7750 if (face == NULL)
7751 error ("Not a valid face");
7752 dump_realized_face (face);
7755 return Qnil;
7759 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
7760 0, 0, 0, doc: /* */)
7763 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
7764 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
7765 fprintf (stderr, "number of GCs = %d\n", ngcs);
7766 return Qnil;
7769 #endif /* GLYPH_DEBUG != 0 */
7773 /***********************************************************************
7774 Initialization
7775 ***********************************************************************/
7777 void
7778 syms_of_xfaces ()
7780 Qface = intern ("face");
7781 staticpro (&Qface);
7782 Qface_no_inherit = intern ("face-no-inherit");
7783 staticpro (&Qface_no_inherit);
7784 Qbitmap_spec_p = intern ("bitmap-spec-p");
7785 staticpro (&Qbitmap_spec_p);
7786 Qframe_update_face_colors = intern ("frame-update-face-colors");
7787 staticpro (&Qframe_update_face_colors);
7789 /* Lisp face attribute keywords. */
7790 QCfamily = intern (":family");
7791 staticpro (&QCfamily);
7792 QCheight = intern (":height");
7793 staticpro (&QCheight);
7794 QCweight = intern (":weight");
7795 staticpro (&QCweight);
7796 QCslant = intern (":slant");
7797 staticpro (&QCslant);
7798 QCunderline = intern (":underline");
7799 staticpro (&QCunderline);
7800 QCinverse_video = intern (":inverse-video");
7801 staticpro (&QCinverse_video);
7802 QCreverse_video = intern (":reverse-video");
7803 staticpro (&QCreverse_video);
7804 QCforeground = intern (":foreground");
7805 staticpro (&QCforeground);
7806 QCbackground = intern (":background");
7807 staticpro (&QCbackground);
7808 QCstipple = intern (":stipple");;
7809 staticpro (&QCstipple);
7810 QCwidth = intern (":width");
7811 staticpro (&QCwidth);
7812 QCfont = intern (":font");
7813 staticpro (&QCfont);
7814 QCbold = intern (":bold");
7815 staticpro (&QCbold);
7816 QCitalic = intern (":italic");
7817 staticpro (&QCitalic);
7818 QCoverline = intern (":overline");
7819 staticpro (&QCoverline);
7820 QCstrike_through = intern (":strike-through");
7821 staticpro (&QCstrike_through);
7822 QCbox = intern (":box");
7823 staticpro (&QCbox);
7824 QCinherit = intern (":inherit");
7825 staticpro (&QCinherit);
7827 /* Symbols used for Lisp face attribute values. */
7828 QCcolor = intern (":color");
7829 staticpro (&QCcolor);
7830 QCline_width = intern (":line-width");
7831 staticpro (&QCline_width);
7832 QCstyle = intern (":style");
7833 staticpro (&QCstyle);
7834 Qreleased_button = intern ("released-button");
7835 staticpro (&Qreleased_button);
7836 Qpressed_button = intern ("pressed-button");
7837 staticpro (&Qpressed_button);
7838 Qnormal = intern ("normal");
7839 staticpro (&Qnormal);
7840 Qultra_light = intern ("ultra-light");
7841 staticpro (&Qultra_light);
7842 Qextra_light = intern ("extra-light");
7843 staticpro (&Qextra_light);
7844 Qlight = intern ("light");
7845 staticpro (&Qlight);
7846 Qsemi_light = intern ("semi-light");
7847 staticpro (&Qsemi_light);
7848 Qsemi_bold = intern ("semi-bold");
7849 staticpro (&Qsemi_bold);
7850 Qbold = intern ("bold");
7851 staticpro (&Qbold);
7852 Qextra_bold = intern ("extra-bold");
7853 staticpro (&Qextra_bold);
7854 Qultra_bold = intern ("ultra-bold");
7855 staticpro (&Qultra_bold);
7856 Qoblique = intern ("oblique");
7857 staticpro (&Qoblique);
7858 Qitalic = intern ("italic");
7859 staticpro (&Qitalic);
7860 Qreverse_oblique = intern ("reverse-oblique");
7861 staticpro (&Qreverse_oblique);
7862 Qreverse_italic = intern ("reverse-italic");
7863 staticpro (&Qreverse_italic);
7864 Qultra_condensed = intern ("ultra-condensed");
7865 staticpro (&Qultra_condensed);
7866 Qextra_condensed = intern ("extra-condensed");
7867 staticpro (&Qextra_condensed);
7868 Qcondensed = intern ("condensed");
7869 staticpro (&Qcondensed);
7870 Qsemi_condensed = intern ("semi-condensed");
7871 staticpro (&Qsemi_condensed);
7872 Qsemi_expanded = intern ("semi-expanded");
7873 staticpro (&Qsemi_expanded);
7874 Qexpanded = intern ("expanded");
7875 staticpro (&Qexpanded);
7876 Qextra_expanded = intern ("extra-expanded");
7877 staticpro (&Qextra_expanded);
7878 Qultra_expanded = intern ("ultra-expanded");
7879 staticpro (&Qultra_expanded);
7880 Qbackground_color = intern ("background-color");
7881 staticpro (&Qbackground_color);
7882 Qforeground_color = intern ("foreground-color");
7883 staticpro (&Qforeground_color);
7884 Qunspecified = intern ("unspecified");
7885 staticpro (&Qunspecified);
7887 Qface_alias = intern ("face-alias");
7888 staticpro (&Qface_alias);
7889 Qdefault = intern ("default");
7890 staticpro (&Qdefault);
7891 Qtool_bar = intern ("tool-bar");
7892 staticpro (&Qtool_bar);
7893 Qregion = intern ("region");
7894 staticpro (&Qregion);
7895 Qfringe = intern ("fringe");
7896 staticpro (&Qfringe);
7897 Qheader_line = intern ("header-line");
7898 staticpro (&Qheader_line);
7899 Qscroll_bar = intern ("scroll-bar");
7900 staticpro (&Qscroll_bar);
7901 Qmenu = intern ("menu");
7902 staticpro (&Qmenu);
7903 Qcursor = intern ("cursor");
7904 staticpro (&Qcursor);
7905 Qborder = intern ("border");
7906 staticpro (&Qborder);
7907 Qmouse = intern ("mouse");
7908 staticpro (&Qmouse);
7909 Qmode_line_inactive = intern ("mode-line-inactive");
7910 staticpro (&Qmode_line_inactive);
7911 Qtty_color_desc = intern ("tty-color-desc");
7912 staticpro (&Qtty_color_desc);
7913 Qtty_color_standard_values = intern ("tty-color-standard-values");
7914 staticpro (&Qtty_color_standard_values);
7915 Qtty_color_by_index = intern ("tty-color-by-index");
7916 staticpro (&Qtty_color_by_index);
7917 Qtty_color_alist = intern ("tty-color-alist");
7918 staticpro (&Qtty_color_alist);
7919 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
7920 staticpro (&Qscalable_fonts_allowed);
7922 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
7923 staticpro (&Vparam_value_alist);
7924 Vface_alternative_font_family_alist = Qnil;
7925 staticpro (&Vface_alternative_font_family_alist);
7926 Vface_alternative_font_registry_alist = Qnil;
7927 staticpro (&Vface_alternative_font_registry_alist);
7929 defsubr (&Sinternal_make_lisp_face);
7930 defsubr (&Sinternal_lisp_face_p);
7931 defsubr (&Sinternal_set_lisp_face_attribute);
7932 #ifdef HAVE_WINDOW_SYSTEM
7933 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
7934 #endif
7935 defsubr (&Scolor_gray_p);
7936 defsubr (&Scolor_supported_p);
7937 defsubr (&Sface_attribute_relative_p);
7938 defsubr (&Smerge_face_attribute);
7939 defsubr (&Sinternal_get_lisp_face_attribute);
7940 defsubr (&Sinternal_lisp_face_attribute_values);
7941 defsubr (&Sinternal_lisp_face_equal_p);
7942 defsubr (&Sinternal_lisp_face_empty_p);
7943 defsubr (&Sinternal_copy_lisp_face);
7944 defsubr (&Sinternal_merge_in_global_face);
7945 defsubr (&Sface_font);
7946 defsubr (&Sframe_face_alist);
7947 defsubr (&Sdisplay_supports_face_attributes_p);
7948 defsubr (&Scolor_distance);
7949 defsubr (&Sinternal_set_font_selection_order);
7950 defsubr (&Sinternal_set_alternative_font_family_alist);
7951 defsubr (&Sinternal_set_alternative_font_registry_alist);
7952 defsubr (&Sface_attributes_as_vector);
7953 #if GLYPH_DEBUG
7954 defsubr (&Sdump_face);
7955 defsubr (&Sshow_face_resources);
7956 #endif /* GLYPH_DEBUG */
7957 defsubr (&Sclear_face_cache);
7958 defsubr (&Stty_suppress_bold_inverse_default_colors);
7960 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
7961 defsubr (&Sdump_colors);
7962 #endif
7964 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
7965 doc: /* *Limit for font matching.
7966 If an integer > 0, font matching functions won't load more than
7967 that number of fonts when searching for a matching font. */);
7968 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
7970 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
7971 doc: /* List of global face definitions (for internal use only.) */);
7972 Vface_new_frame_defaults = Qnil;
7974 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
7975 doc: /* *Default stipple pattern used on monochrome displays.
7976 This stipple pattern is used on monochrome displays
7977 instead of shades of gray for a face background color.
7978 See `set-face-stipple' for possible values for this variable. */);
7979 Vface_default_stipple = build_string ("gray3");
7981 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
7982 doc: /* An alist of defined terminal colors and their RGB values. */);
7983 Vtty_defined_color_alist = Qnil;
7985 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
7986 doc: /* Allowed scalable fonts.
7987 A value of nil means don't allow any scalable fonts.
7988 A value of t means allow any scalable font.
7989 Otherwise, value must be a list of regular expressions. A font may be
7990 scaled if its name matches a regular expression in the list.
7991 Note that if value is nil, a scalable font might still be used, if no
7992 other font of the appropriate family and registry is available. */);
7993 Vscalable_fonts_allowed = Qnil;
7995 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
7996 doc: /* List of ignored fonts.
7997 Each element is a regular expression that matches names of fonts to
7998 ignore. */);
7999 Vface_ignored_fonts = Qnil;
8001 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
8002 doc: /* Alist of fonts vs the rescaling factors.
8003 Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
8004 FONT-NAME-PATTERN is a regular expression matching a font name, and
8005 RESCALE-RATIO is a floating point number to specify how much larger
8006 \(or smaller) font we should use. For instance, if a face requests
8007 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
8008 Vface_font_rescale_alist = Qnil;
8010 #ifdef HAVE_WINDOW_SYSTEM
8011 defsubr (&Sbitmap_spec_p);
8012 defsubr (&Sx_list_fonts);
8013 defsubr (&Sinternal_face_x_get_resource);
8014 defsubr (&Sx_family_fonts);
8015 defsubr (&Sx_font_family_list);
8016 #endif /* HAVE_WINDOW_SYSTEM */
8019 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
8020 (do not change this comment) */